Bug 1444940 [wpt PR 9917] - Writable streams: test changes to abort() under error...
[gecko.git] / gfx / 2d / UnscaledFontFreeType.cpp
blob0550ee0200565913b390e29cdf99fc775b7958eb
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "UnscaledFontFreeType.h"
9 #include FT_TRUETYPE_TABLES_H
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <sys/mman.h>
17 namespace mozilla {
18 namespace gfx {
20 bool
21 UnscaledFontFreeType::GetFontFileData(FontFileDataOutput aDataCallback, void* aBaton)
23 if (!mFile.empty()) {
24 int fd = open(mFile.c_str(), O_RDONLY);
25 if (fd < 0) {
26 return false;
28 struct stat buf;
29 if (fstat(fd, &buf) < 0 ||
30 // Don't erroneously read directories as files.
31 !S_ISREG(buf.st_mode) ||
32 // Verify the file size fits in a uint32_t.
33 buf.st_size <= 0 ||
34 off_t(uint32_t(buf.st_size)) != buf.st_size) {
35 close(fd);
36 return false;
38 uint32_t length = buf.st_size;
39 uint8_t* fontData =
40 reinterpret_cast<uint8_t*>(
41 mmap(nullptr, length, PROT_READ, MAP_PRIVATE, fd, 0));
42 close(fd);
43 if (fontData == MAP_FAILED) {
44 return false;
46 aDataCallback(fontData, length, mIndex, aBaton);
47 munmap(fontData, length);
48 return true;
51 bool success = false;
52 FT_ULong length = 0;
53 // Request the SFNT file. This may not always succeed for all font types.
54 if (FT_Load_Sfnt_Table(mFace, 0, 0, nullptr, &length) == FT_Err_Ok) {
55 uint8_t* fontData = new uint8_t[length];
56 if (FT_Load_Sfnt_Table(mFace, 0, 0, fontData, &length) == FT_Err_Ok) {
57 aDataCallback(fontData, length, 0, aBaton);
58 success = true;
60 delete[] fontData;
62 return success;
65 bool
66 UnscaledFontFreeType::GetFontDescriptor(FontDescriptorOutput aCb, void* aBaton)
68 if (mFile.empty()) {
69 return false;
72 const char* path = mFile.c_str();
73 size_t pathLength = strlen(path) + 1;
74 aCb(reinterpret_cast<const uint8_t*>(path), pathLength, mIndex, aBaton);
75 return true;
78 } // namespace gfx
79 } // namespace mozilla