OLD | NEW |
---|---|
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/fxge/include/fx_font.h" | 7 #include "core/fxge/include/fx_font.h" |
8 | 8 |
9 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h" | 9 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h" |
10 #include "core/fxge/ge/fx_text_int.h" | 10 #include "core/fxge/ge/fx_text_int.h" |
(...skipping 14 matching lines...) Expand all Loading... | |
25 FXFM_ENCODING_ADOBE_LATIN_1, FXFM_ENCODING_OLD_LATIN_2, | 25 FXFM_ENCODING_ADOBE_LATIN_1, FXFM_ENCODING_OLD_LATIN_2, |
26 FXFM_ENCODING_APPLE_ROMAN, | 26 FXFM_ENCODING_APPLE_ROMAN, |
27 }; | 27 }; |
28 | 28 |
29 CFX_UnicodeEncodingEx* _FXFM_CreateFontEncoding(CFX_Font* pFont, | 29 CFX_UnicodeEncodingEx* _FXFM_CreateFontEncoding(CFX_Font* pFont, |
30 uint32_t nEncodingID) { | 30 uint32_t nEncodingID) { |
31 if (FXFT_Select_Charmap(pFont->GetFace(), nEncodingID)) | 31 if (FXFT_Select_Charmap(pFont->GetFace(), nEncodingID)) |
32 return nullptr; | 32 return nullptr; |
33 return new CFX_UnicodeEncodingEx(pFont, nEncodingID); | 33 return new CFX_UnicodeEncodingEx(pFont, nEncodingID); |
34 } | 34 } |
35 | |
36 unsigned long FTStreamRead(FXFT_Stream stream, | |
37 unsigned long offset, | |
38 unsigned char* buffer, | |
39 unsigned long count) { | |
40 if (count == 0) { | |
dsinclair
2016/07/21 19:39:59
nit: no {}'s
Wei Li
2016/07/21 21:15:02
Done.
| |
41 return 0; | |
42 } | |
43 IFX_FileRead* pFile = (IFX_FileRead*)stream->descriptor.pointer; | |
dsinclair
2016/07/21 19:39:59
nit: static_cast
Wei Li
2016/07/21 21:15:02
Done.
| |
44 int res = pFile->ReadBlock(buffer, offset, count); | |
45 if (res) { | |
dsinclair
2016/07/21 19:39:59
ditto
Wei Li
2016/07/21 21:15:02
Removed this.
| |
46 return count; | |
47 } | |
48 return 0; | |
dsinclair
2016/07/21 19:39:59
return res ? count : 0; ?
Wei Li
2016/07/21 21:15:03
Done.
| |
49 } | |
50 | |
51 void FTStreamClose(FXFT_Stream stream) {} | |
52 | |
53 FX_BOOL LoadFileImp(FXFT_Library library, | |
54 FXFT_Face* Face, | |
55 IFX_FileRead* pFile, | |
56 int32_t faceIndex, | |
57 std::unique_ptr<FXFT_StreamRec>* stream) { | |
58 std::unique_ptr<FXFT_StreamRec> stream1(new FXFT_StreamRec()); | |
59 stream1->base = nullptr; | |
60 stream1->size = (unsigned long)pFile->GetSize(); | |
dsinclair
2016/07/21 19:39:59
nit: static_cast
Wei Li
2016/07/21 21:15:02
Done.
| |
61 stream1->pos = 0; | |
62 stream1->descriptor.pointer = pFile; | |
63 stream1->close = FTStreamClose; | |
64 stream1->read = FTStreamRead; | |
65 FXFT_Open_Args args; | |
66 args.flags = FT_OPEN_STREAM; | |
67 args.stream = stream1.get(); | |
68 if (FXFT_Open_Face(library, &args, faceIndex, Face)) | |
69 return FALSE; | |
70 if (stream) | |
71 *stream = std::move(stream1); | |
72 return TRUE; | |
73 } | |
35 #endif // PDF_ENABLE_XFA | 74 #endif // PDF_ENABLE_XFA |
36 | 75 |
37 FXFT_Face FT_LoadFont(const uint8_t* pData, int size) { | 76 FXFT_Face FT_LoadFont(const uint8_t* pData, int size) { |
38 return CFX_GEModule::Get()->GetFontMgr()->GetFixedFace(pData, size, 0); | 77 return CFX_GEModule::Get()->GetFontMgr()->GetFixedFace(pData, size, 0); |
39 } | 78 } |
40 | 79 |
41 } // namespace | 80 } // namespace |
42 | 81 |
43 CFX_Font::CFX_Font() | 82 CFX_Font::CFX_Font() |
44 #ifdef PDF_ENABLE_XFA | 83 #ifdef PDF_ENABLE_XFA |
45 : m_bLogic(FALSE), | 84 : m_bLogic(FALSE), |
46 m_pOwnedStream(nullptr), | 85 m_pOwnedStream(nullptr), |
47 m_Face(nullptr), | 86 m_Face(nullptr), |
48 #else | 87 #else |
49 : m_Face(nullptr), | 88 : m_Face(nullptr), |
50 #endif // PDF_ENABLE_XFA | 89 #endif // PDF_ENABLE_XFA |
51 m_pSubstFont(nullptr), | |
52 m_pFontDataAllocation(nullptr), | |
53 m_pFontData(nullptr), | 90 m_pFontData(nullptr), |
54 m_pGsubData(nullptr), | 91 m_pGsubData(nullptr), |
55 m_dwSize(0), | 92 m_dwSize(0), |
56 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | 93 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
57 m_pPlatformFont(nullptr), | 94 m_pPlatformFont(nullptr), |
58 #endif | 95 #endif |
59 m_bEmbedded(FALSE), | 96 m_bEmbedded(FALSE), |
60 m_bVertical(FALSE) { | 97 m_bVertical(FALSE) { |
61 } | 98 } |
62 | 99 |
63 #ifdef PDF_ENABLE_XFA | 100 #ifdef PDF_ENABLE_XFA |
64 FX_BOOL CFX_Font::LoadClone(const CFX_Font* pFont) { | 101 FX_BOOL CFX_Font::LoadClone(const CFX_Font* pFont) { |
65 if (!pFont) | 102 if (!pFont) |
66 return FALSE; | 103 return FALSE; |
67 | 104 |
68 m_bLogic = TRUE; | 105 m_bLogic = TRUE; |
69 if (pFont->m_pSubstFont) { | 106 if (pFont->m_pSubstFont) { |
70 m_pSubstFont = new CFX_SubstFont; | 107 m_pSubstFont.reset(new CFX_SubstFont); |
71 m_pSubstFont->m_Charset = pFont->m_pSubstFont->m_Charset; | 108 m_pSubstFont->m_Charset = pFont->m_pSubstFont->m_Charset; |
72 m_pSubstFont->m_ExtHandle = pFont->m_pSubstFont->m_ExtHandle; | 109 m_pSubstFont->m_ExtHandle = pFont->m_pSubstFont->m_ExtHandle; |
73 m_pSubstFont->m_SubstFlags = pFont->m_pSubstFont->m_SubstFlags; | 110 m_pSubstFont->m_SubstFlags = pFont->m_pSubstFont->m_SubstFlags; |
74 m_pSubstFont->m_Weight = pFont->m_pSubstFont->m_Weight; | 111 m_pSubstFont->m_Weight = pFont->m_pSubstFont->m_Weight; |
75 m_pSubstFont->m_Family = pFont->m_pSubstFont->m_Family; | 112 m_pSubstFont->m_Family = pFont->m_pSubstFont->m_Family; |
76 m_pSubstFont->m_ItalicAngle = pFont->m_pSubstFont->m_ItalicAngle; | 113 m_pSubstFont->m_ItalicAngle = pFont->m_pSubstFont->m_ItalicAngle; |
77 } | 114 } |
78 if (pFont->m_OtfFontData.GetSize()) { | 115 if (pFont->m_OtfFontData.GetSize()) { |
79 m_OtfFontData.AttachData(pFont->m_OtfFontData.GetBuffer(), | 116 m_OtfFontData.AttachData(pFont->m_OtfFontData.GetBuffer(), |
80 pFont->m_OtfFontData.GetSize()); | 117 pFont->m_OtfFontData.GetSize()); |
81 } | 118 } |
82 m_Face = pFont->m_Face; | 119 m_Face = pFont->m_Face; |
83 m_bEmbedded = pFont->m_bEmbedded; | 120 m_bEmbedded = pFont->m_bEmbedded; |
84 m_bVertical = pFont->m_bVertical; | 121 m_bVertical = pFont->m_bVertical; |
85 m_dwSize = pFont->m_dwSize; | 122 m_dwSize = pFont->m_dwSize; |
86 m_pFontData = pFont->m_pFontData; | 123 m_pFontData = pFont->m_pFontData; |
87 m_pGsubData = pFont->m_pGsubData; | 124 m_pGsubData = pFont->m_pGsubData; |
88 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | 125 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
89 m_pPlatformFont = pFont->m_pPlatformFont; | 126 m_pPlatformFont = pFont->m_pPlatformFont; |
90 #endif | 127 #endif |
91 m_pOwnedStream = pFont->m_pOwnedStream; | 128 m_pOwnedStream = pFont->m_pOwnedStream; |
92 return TRUE; | 129 return TRUE; |
93 } | 130 } |
94 #endif // PDF_ENABLE_XFA | 131 #endif // PDF_ENABLE_XFA |
95 | 132 |
96 CFX_Font::~CFX_Font() { | 133 CFX_Font::~CFX_Font() { |
97 delete m_pSubstFont; | |
98 FX_Free(m_pFontDataAllocation); | |
99 #ifdef PDF_ENABLE_XFA | 134 #ifdef PDF_ENABLE_XFA |
100 if (m_bLogic) { | 135 if (m_bLogic) { |
101 m_OtfFontData.DetachBuffer(); | 136 m_OtfFontData.DetachBuffer(); |
102 return; | 137 return; |
103 } | 138 } |
104 #endif // PDF_ENABLE_XFA | 139 #endif // PDF_ENABLE_XFA |
105 if (m_Face) { | 140 if (m_Face) { |
106 #ifndef PDF_ENABLE_XFA | 141 #ifndef PDF_ENABLE_XFA |
107 if (FXFT_Get_Face_External_Stream(m_Face)) { | 142 if (FXFT_Get_Face_External_Stream(m_Face)) { |
108 FXFT_Clear_Face_External_Stream(m_Face); | 143 FXFT_Clear_Face_External_Stream(m_Face); |
109 } | 144 } |
110 #endif // PDF_ENABLE_XFA | 145 #endif // PDF_ENABLE_XFA |
111 if (m_bEmbedded) { | 146 if (m_bEmbedded) { |
112 DeleteFace(); | 147 DeleteFace(); |
113 } else { | 148 } else { |
114 CFX_GEModule::Get()->GetFontMgr()->ReleaseFace(m_Face); | 149 CFX_GEModule::Get()->GetFontMgr()->ReleaseFace(m_Face); |
115 } | 150 } |
116 } | 151 } |
117 #ifdef PDF_ENABLE_XFA | 152 #ifdef PDF_ENABLE_XFA |
118 FX_Free(m_pOwnedStream); | 153 FX_Free(m_pOwnedStream); |
119 #endif // PDF_ENABLE_XFA | 154 #endif // PDF_ENABLE_XFA |
120 FX_Free(m_pGsubData); | 155 FX_Free(m_pGsubData); |
121 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | 156 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
122 ReleasePlatformResource(); | 157 ReleasePlatformResource(); |
123 #endif | 158 #endif |
124 } | 159 } |
160 | |
125 void CFX_Font::DeleteFace() { | 161 void CFX_Font::DeleteFace() { |
126 FXFT_Done_Face(m_Face); | 162 FXFT_Done_Face(m_Face); |
127 m_Face = nullptr; | 163 m_Face = nullptr; |
128 } | 164 } |
165 | |
129 void CFX_Font::LoadSubst(const CFX_ByteString& face_name, | 166 void CFX_Font::LoadSubst(const CFX_ByteString& face_name, |
130 FX_BOOL bTrueType, | 167 FX_BOOL bTrueType, |
131 uint32_t flags, | 168 uint32_t flags, |
132 int weight, | 169 int weight, |
133 int italic_angle, | 170 int italic_angle, |
134 int CharsetCP, | 171 int CharsetCP, |
135 FX_BOOL bVertical) { | 172 FX_BOOL bVertical) { |
136 m_bEmbedded = FALSE; | 173 m_bEmbedded = FALSE; |
137 m_bVertical = bVertical; | 174 m_bVertical = bVertical; |
138 m_pSubstFont = new CFX_SubstFont; | 175 m_pSubstFont.reset(new CFX_SubstFont); |
139 m_Face = CFX_GEModule::Get()->GetFontMgr()->FindSubstFont( | 176 m_Face = CFX_GEModule::Get()->GetFontMgr()->FindSubstFont( |
140 face_name, bTrueType, flags, weight, italic_angle, CharsetCP, | 177 face_name, bTrueType, flags, weight, italic_angle, CharsetCP, |
141 m_pSubstFont); | 178 m_pSubstFont.get()); |
142 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | 179 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
143 if (m_pSubstFont->m_ExtHandle) { | 180 if (m_pSubstFont->m_ExtHandle) { |
144 m_pPlatformFont = m_pSubstFont->m_ExtHandle; | 181 m_pPlatformFont = m_pSubstFont->m_ExtHandle; |
145 m_pSubstFont->m_ExtHandle = nullptr; | 182 m_pSubstFont->m_ExtHandle = nullptr; |
146 } | 183 } |
147 #endif | 184 #endif |
148 if (m_Face) { | 185 if (m_Face) { |
149 m_pFontData = FXFT_Get_Face_Stream_Base(m_Face); | 186 m_pFontData = FXFT_Get_Face_Stream_Base(m_Face); |
150 m_dwSize = FXFT_Get_Face_Stream_Size(m_Face); | 187 m_dwSize = FXFT_Get_Face_Stream_Size(m_Face); |
151 } | 188 } |
152 } | 189 } |
190 | |
153 #ifdef PDF_ENABLE_XFA | 191 #ifdef PDF_ENABLE_XFA |
154 extern "C" { | |
155 unsigned long _FTStreamRead(FXFT_Stream stream, | |
156 unsigned long offset, | |
157 unsigned char* buffer, | |
158 unsigned long count) { | |
159 if (count == 0) { | |
160 return 0; | |
161 } | |
162 IFX_FileRead* pFile = (IFX_FileRead*)stream->descriptor.pointer; | |
163 int res = pFile->ReadBlock(buffer, offset, count); | |
164 if (res) { | |
165 return count; | |
166 } | |
167 return 0; | |
168 } | |
169 void _FTStreamClose(FXFT_Stream stream) {} | |
170 }; | |
171 FX_BOOL _LoadFile(FXFT_Library library, | |
172 FXFT_Face* Face, | |
173 IFX_FileRead* pFile, | |
174 FXFT_Stream* stream, | |
175 int32_t faceIndex = 0) { | |
176 FXFT_Stream stream1 = (FXFT_Stream)FX_Alloc(uint8_t, sizeof(FXFT_StreamRec)); | |
177 stream1->base = nullptr; | |
178 stream1->size = (unsigned long)pFile->GetSize(); | |
179 stream1->pos = 0; | |
180 stream1->descriptor.pointer = pFile; | |
181 stream1->close = _FTStreamClose; | |
182 stream1->read = _FTStreamRead; | |
183 FXFT_Open_Args args; | |
184 args.flags = FT_OPEN_STREAM; | |
185 args.stream = stream1; | |
186 if (FXFT_Open_Face(library, &args, faceIndex, Face)) { | |
187 FX_Free(stream1); | |
188 return FALSE; | |
189 } | |
190 if (stream) { | |
191 *stream = stream1; | |
192 } | |
193 return TRUE; | |
194 } | |
195 | |
196 FX_BOOL CFX_Font::LoadFile(IFX_FileRead* pFile, | 192 FX_BOOL CFX_Font::LoadFile(IFX_FileRead* pFile, |
197 int nFaceIndex, | 193 int nFaceIndex, |
198 int* pFaceCount) { | 194 int* pFaceCount) { |
199 m_bEmbedded = FALSE; | 195 m_bEmbedded = FALSE; |
200 | 196 |
201 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr(); | 197 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr(); |
202 pFontMgr->InitFTLibrary(); | 198 pFontMgr->InitFTLibrary(); |
203 FXFT_Library library = pFontMgr->GetFTLibrary(); | 199 FXFT_Library library = pFontMgr->GetFTLibrary(); |
204 | 200 |
205 FXFT_Stream stream = nullptr; | 201 std::unique_ptr<FXFT_StreamRec> stream; |
206 if (!_LoadFile(library, &m_Face, pFile, &stream, nFaceIndex)) | 202 if (!LoadFileImp(library, &m_Face, pFile, nFaceIndex, &stream)) |
207 return FALSE; | 203 return FALSE; |
208 | 204 |
209 if (pFaceCount) | 205 if (pFaceCount) |
210 *pFaceCount = (int)m_Face->num_faces; | 206 *pFaceCount = (int)m_Face->num_faces; |
211 m_pOwnedStream = stream; | 207 m_pOwnedStream = stream.release(); |
212 FXFT_Set_Pixel_Sizes(m_Face, 0, 64); | 208 FXFT_Set_Pixel_Sizes(m_Face, 0, 64); |
213 return TRUE; | 209 return TRUE; |
214 } | 210 } |
215 #endif // PDF_ENABLE_XFA | 211 #endif // PDF_ENABLE_XFA |
216 | 212 |
217 int CFX_Font::GetGlyphWidth(uint32_t glyph_index) { | 213 int CFX_Font::GetGlyphWidth(uint32_t glyph_index) { |
218 if (!m_Face) { | 214 if (!m_Face) { |
219 return 0; | 215 return 0; |
220 } | 216 } |
221 if (m_pSubstFont && (m_pSubstFont->m_SubstFlags & FXFONT_SUBST_MM)) { | 217 if (m_pSubstFont && (m_pSubstFont->m_SubstFlags & FXFONT_SUBST_MM)) { |
222 AdjustMMParams(glyph_index, 0, 0); | 218 AdjustMMParams(glyph_index, 0, 0); |
223 } | 219 } |
224 int err = FXFT_Load_Glyph( | 220 int err = FXFT_Load_Glyph( |
225 m_Face, glyph_index, | 221 m_Face, glyph_index, |
226 FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH); | 222 FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH); |
227 if (err) { | 223 if (err) { |
228 return 0; | 224 return 0; |
229 } | 225 } |
230 int width = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), | 226 int width = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
231 FXFT_Get_Glyph_HoriAdvance(m_Face)); | 227 FXFT_Get_Glyph_HoriAdvance(m_Face)); |
232 return width; | 228 return width; |
233 } | 229 } |
234 | 230 |
235 FX_BOOL CFX_Font::LoadEmbedded(const uint8_t* data, uint32_t size) { | 231 FX_BOOL CFX_Font::LoadEmbedded(const uint8_t* data, uint32_t size) { |
236 m_pFontDataAllocation = FX_Alloc(uint8_t, size); | 232 std::vector<uint8_t> temp(data, data + size / sizeof(uint8_t)); |
dsinclair
2016/07/21 19:39:59
Why does this divide by uint8_t? Size is the numbe
Wei Li
2016/07/21 21:15:02
Right, thanks
| |
237 FXSYS_memcpy(m_pFontDataAllocation, data, size); | 233 m_pFontDataAllocation.swap(temp); |
238 m_Face = FT_LoadFont(m_pFontDataAllocation, size); | 234 m_Face = FT_LoadFont(m_pFontDataAllocation.data(), size); |
239 m_pFontData = m_pFontDataAllocation; | 235 m_pFontData = m_pFontDataAllocation.data(); |
240 m_bEmbedded = TRUE; | 236 m_bEmbedded = TRUE; |
241 m_dwSize = size; | 237 m_dwSize = size; |
242 return !!m_Face; | 238 return !!m_Face; |
243 } | 239 } |
244 | 240 |
245 FX_BOOL CFX_Font::IsTTFont() const { | 241 FX_BOOL CFX_Font::IsTTFont() const { |
246 if (!m_Face) | 242 if (!m_Face) |
247 return FALSE; | 243 return FALSE; |
248 return FXFT_Is_Face_TT_OT(m_Face) == FXFT_FACE_FLAG_SFNT; | 244 return FXFT_Is_Face_TT_OT(m_Face) == FXFT_FACE_FLAG_SFNT; |
249 } | 245 } |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
531 for (size_t i = 0; i < FX_ArraySize(g_EncodingID); ++i) { | 527 for (size_t i = 0; i < FX_ArraySize(g_EncodingID); ++i) { |
532 CFX_UnicodeEncodingEx* pFontEncoding = | 528 CFX_UnicodeEncodingEx* pFontEncoding = |
533 _FXFM_CreateFontEncoding(pFont, g_EncodingID[i]); | 529 _FXFM_CreateFontEncoding(pFont, g_EncodingID[i]); |
534 if (pFontEncoding) { | 530 if (pFontEncoding) { |
535 return pFontEncoding; | 531 return pFontEncoding; |
536 } | 532 } |
537 } | 533 } |
538 return nullptr; | 534 return nullptr; |
539 } | 535 } |
540 #endif // PDF_ENABLE_XFA | 536 #endif // PDF_ENABLE_XFA |
OLD | NEW |