Line data Source code
1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "UnscaledFontFreeType.h"
7 :
8 : #include FT_TRUETYPE_TABLES_H
9 :
10 : #include <fcntl.h>
11 : #include <unistd.h>
12 : #include <sys/types.h>
13 : #include <sys/stat.h>
14 : #include <sys/mman.h>
15 :
16 : namespace mozilla {
17 : namespace gfx {
18 :
19 : bool
20 0 : UnscaledFontFreeType::GetFontFileData(FontFileDataOutput aDataCallback, void* aBaton)
21 : {
22 0 : if (!mFile.empty()) {
23 0 : int fd = open(mFile.c_str(), O_RDONLY);
24 0 : if (fd < 0) {
25 0 : return false;
26 : }
27 : struct stat buf;
28 0 : if (fstat(fd, &buf) < 0 ||
29 : // Don't erroneously read directories as files.
30 0 : !S_ISREG(buf.st_mode) ||
31 : // Verify the file size fits in a uint32_t.
32 0 : buf.st_size <= 0 ||
33 0 : off_t(uint32_t(buf.st_size)) != buf.st_size) {
34 0 : close(fd);
35 0 : return false;
36 : }
37 0 : uint32_t length = buf.st_size;
38 : uint8_t* fontData =
39 : reinterpret_cast<uint8_t*>(
40 0 : mmap(nullptr, length, PROT_READ, MAP_PRIVATE, fd, 0));
41 0 : close(fd);
42 0 : if (fontData == MAP_FAILED) {
43 0 : return false;
44 : }
45 0 : aDataCallback(fontData, length, mIndex, aBaton);
46 0 : munmap(fontData, length);
47 0 : return true;
48 : }
49 :
50 0 : bool success = false;
51 0 : FT_ULong length = 0;
52 : // Request the SFNT file. This may not always succeed for all font types.
53 0 : if (FT_Load_Sfnt_Table(mFace, 0, 0, nullptr, &length) == FT_Err_Ok) {
54 0 : uint8_t* fontData = new uint8_t[length];
55 0 : if (FT_Load_Sfnt_Table(mFace, 0, 0, fontData, &length) == FT_Err_Ok) {
56 0 : aDataCallback(fontData, length, 0, aBaton);
57 0 : success = true;
58 : }
59 0 : delete[] fontData;
60 : }
61 0 : return success;
62 : }
63 :
64 : bool
65 0 : UnscaledFontFreeType::GetFontDescriptor(FontDescriptorOutput aCb, void* aBaton)
66 : {
67 0 : if (mFile.empty()) {
68 0 : return false;
69 : }
70 :
71 0 : const char* path = mFile.c_str();
72 0 : size_t pathLength = strlen(path) + 1;
73 0 : size_t dataLength = sizeof(FontDescriptor) + pathLength;
74 0 : uint8_t* data = new uint8_t[dataLength];
75 0 : FontDescriptor* desc = reinterpret_cast<FontDescriptor*>(data);
76 0 : desc->mPathLength = pathLength;
77 0 : desc->mIndex = mIndex;
78 0 : memcpy(data + sizeof(FontDescriptor), path, pathLength);
79 :
80 0 : aCb(data, dataLength, aBaton);
81 0 : delete[] data;
82 0 : return true;
83 : }
84 :
85 : } // namespace gfx
86 : } // namespace mozilla
87 :
|