OLD | NEW |
| (Empty) |
1 // Copyright 2014 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 #include "core/fxge/include/fx_font.h" | |
8 | |
9 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h" | |
10 #include "core/fxge/ge/fx_text_int.h" | |
11 #include "core/fxge/include/cfx_fontmgr.h" | |
12 #include "core/fxge/include/cfx_gemodule.h" | |
13 #include "core/fxge/include/cfx_substfont.h" | |
14 #include "core/fxge/include/fx_freetype.h" | |
15 | |
16 #define EM_ADJUST(em, a) (em == 0 ? (a) : (a)*1000 / em) | |
17 | |
18 namespace { | |
19 | |
20 #ifdef PDF_ENABLE_XFA | |
21 | |
22 unsigned long FTStreamRead(FXFT_Stream stream, | |
23 unsigned long offset, | |
24 unsigned char* buffer, | |
25 unsigned long count) { | |
26 if (count == 0) | |
27 return 0; | |
28 | |
29 IFX_FileRead* pFile = static_cast<IFX_FileRead*>(stream->descriptor.pointer); | |
30 return pFile->ReadBlock(buffer, offset, count) ? count : 0; | |
31 } | |
32 | |
33 void FTStreamClose(FXFT_Stream stream) {} | |
34 | |
35 FX_BOOL LoadFileImp(FXFT_Library library, | |
36 FXFT_Face* Face, | |
37 IFX_FileRead* pFile, | |
38 int32_t faceIndex, | |
39 std::unique_ptr<FXFT_StreamRec>* stream) { | |
40 std::unique_ptr<FXFT_StreamRec> stream1(new FXFT_StreamRec()); | |
41 stream1->base = nullptr; | |
42 stream1->size = static_cast<unsigned long>(pFile->GetSize()); | |
43 stream1->pos = 0; | |
44 stream1->descriptor.pointer = pFile; | |
45 stream1->close = FTStreamClose; | |
46 stream1->read = FTStreamRead; | |
47 FXFT_Open_Args args; | |
48 args.flags = FT_OPEN_STREAM; | |
49 args.stream = stream1.get(); | |
50 if (FXFT_Open_Face(library, &args, faceIndex, Face)) | |
51 return FALSE; | |
52 if (stream) | |
53 *stream = std::move(stream1); | |
54 return TRUE; | |
55 } | |
56 #endif // PDF_ENABLE_XFA | |
57 | |
58 FXFT_Face FT_LoadFont(const uint8_t* pData, int size) { | |
59 return CFX_GEModule::Get()->GetFontMgr()->GetFixedFace(pData, size, 0); | |
60 } | |
61 | |
62 } // namespace | |
63 | |
64 CFX_Font::CFX_Font() | |
65 #ifdef PDF_ENABLE_XFA | |
66 : m_bLogic(FALSE), | |
67 m_pOwnedStream(nullptr), | |
68 m_Face(nullptr), | |
69 #else | |
70 : m_Face(nullptr), | |
71 #endif // PDF_ENABLE_XFA | |
72 m_pFontData(nullptr), | |
73 m_pGsubData(nullptr), | |
74 m_dwSize(0), | |
75 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
76 m_pPlatformFont(nullptr), | |
77 #endif | |
78 m_bEmbedded(FALSE), | |
79 m_bVertical(FALSE) { | |
80 } | |
81 | |
82 #ifdef PDF_ENABLE_XFA | |
83 FX_BOOL CFX_Font::LoadClone(const CFX_Font* pFont) { | |
84 if (!pFont) | |
85 return FALSE; | |
86 | |
87 m_bLogic = TRUE; | |
88 if (pFont->m_pSubstFont) { | |
89 m_pSubstFont.reset(new CFX_SubstFont); | |
90 m_pSubstFont->m_Charset = pFont->m_pSubstFont->m_Charset; | |
91 m_pSubstFont->m_SubstFlags = pFont->m_pSubstFont->m_SubstFlags; | |
92 m_pSubstFont->m_Weight = pFont->m_pSubstFont->m_Weight; | |
93 m_pSubstFont->m_Family = pFont->m_pSubstFont->m_Family; | |
94 m_pSubstFont->m_ItalicAngle = pFont->m_pSubstFont->m_ItalicAngle; | |
95 } | |
96 if (pFont->m_OtfFontData.GetSize()) { | |
97 m_OtfFontData.AttachData(pFont->m_OtfFontData.GetBuffer(), | |
98 pFont->m_OtfFontData.GetSize()); | |
99 } | |
100 m_Face = pFont->m_Face; | |
101 m_bEmbedded = pFont->m_bEmbedded; | |
102 m_bVertical = pFont->m_bVertical; | |
103 m_dwSize = pFont->m_dwSize; | |
104 m_pFontData = pFont->m_pFontData; | |
105 m_pGsubData = pFont->m_pGsubData; | |
106 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
107 m_pPlatformFont = pFont->m_pPlatformFont; | |
108 #endif | |
109 m_pOwnedStream = pFont->m_pOwnedStream; | |
110 return TRUE; | |
111 } | |
112 #endif // PDF_ENABLE_XFA | |
113 | |
114 CFX_Font::~CFX_Font() { | |
115 #ifdef PDF_ENABLE_XFA | |
116 if (m_bLogic) { | |
117 m_OtfFontData.DetachBuffer(); | |
118 return; | |
119 } | |
120 #endif // PDF_ENABLE_XFA | |
121 if (m_Face) { | |
122 #ifndef PDF_ENABLE_XFA | |
123 if (FXFT_Get_Face_External_Stream(m_Face)) { | |
124 FXFT_Clear_Face_External_Stream(m_Face); | |
125 } | |
126 #endif // PDF_ENABLE_XFA | |
127 if (m_bEmbedded) { | |
128 DeleteFace(); | |
129 } else { | |
130 CFX_GEModule::Get()->GetFontMgr()->ReleaseFace(m_Face); | |
131 } | |
132 } | |
133 #ifdef PDF_ENABLE_XFA | |
134 FX_Free(m_pOwnedStream); | |
135 #endif // PDF_ENABLE_XFA | |
136 FX_Free(m_pGsubData); | |
137 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ && !defined _SKIA_SUPPORT_ | |
138 ReleasePlatformResource(); | |
139 #endif | |
140 } | |
141 | |
142 void CFX_Font::DeleteFace() { | |
143 FXFT_Done_Face(m_Face); | |
144 m_Face = nullptr; | |
145 } | |
146 | |
147 void CFX_Font::LoadSubst(const CFX_ByteString& face_name, | |
148 FX_BOOL bTrueType, | |
149 uint32_t flags, | |
150 int weight, | |
151 int italic_angle, | |
152 int CharsetCP, | |
153 FX_BOOL bVertical) { | |
154 m_bEmbedded = FALSE; | |
155 m_bVertical = bVertical; | |
156 m_pSubstFont.reset(new CFX_SubstFont); | |
157 m_Face = CFX_GEModule::Get()->GetFontMgr()->FindSubstFont( | |
158 face_name, bTrueType, flags, weight, italic_angle, CharsetCP, | |
159 m_pSubstFont.get()); | |
160 if (m_Face) { | |
161 m_pFontData = FXFT_Get_Face_Stream_Base(m_Face); | |
162 m_dwSize = FXFT_Get_Face_Stream_Size(m_Face); | |
163 } | |
164 } | |
165 | |
166 #ifdef PDF_ENABLE_XFA | |
167 FX_BOOL CFX_Font::LoadFile(IFX_FileRead* pFile, | |
168 int nFaceIndex, | |
169 int* pFaceCount) { | |
170 m_bEmbedded = FALSE; | |
171 | |
172 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr(); | |
173 pFontMgr->InitFTLibrary(); | |
174 FXFT_Library library = pFontMgr->GetFTLibrary(); | |
175 | |
176 std::unique_ptr<FXFT_StreamRec> stream; | |
177 if (!LoadFileImp(library, &m_Face, pFile, nFaceIndex, &stream)) | |
178 return FALSE; | |
179 | |
180 if (pFaceCount) | |
181 *pFaceCount = (int)m_Face->num_faces; | |
182 m_pOwnedStream = stream.release(); | |
183 FXFT_Set_Pixel_Sizes(m_Face, 0, 64); | |
184 return TRUE; | |
185 } | |
186 #endif // PDF_ENABLE_XFA | |
187 | |
188 int CFX_Font::GetGlyphWidth(uint32_t glyph_index) { | |
189 if (!m_Face) { | |
190 return 0; | |
191 } | |
192 if (m_pSubstFont && (m_pSubstFont->m_SubstFlags & FXFONT_SUBST_MM)) { | |
193 AdjustMMParams(glyph_index, 0, 0); | |
194 } | |
195 int err = FXFT_Load_Glyph( | |
196 m_Face, glyph_index, | |
197 FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH); | |
198 if (err) { | |
199 return 0; | |
200 } | |
201 int width = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), | |
202 FXFT_Get_Glyph_HoriAdvance(m_Face)); | |
203 return width; | |
204 } | |
205 | |
206 FX_BOOL CFX_Font::LoadEmbedded(const uint8_t* data, uint32_t size) { | |
207 std::vector<uint8_t> temp(data, data + size); | |
208 m_pFontDataAllocation.swap(temp); | |
209 m_Face = FT_LoadFont(m_pFontDataAllocation.data(), size); | |
210 m_pFontData = m_pFontDataAllocation.data(); | |
211 m_bEmbedded = TRUE; | |
212 m_dwSize = size; | |
213 return !!m_Face; | |
214 } | |
215 | |
216 FX_BOOL CFX_Font::IsTTFont() const { | |
217 if (!m_Face) | |
218 return FALSE; | |
219 return FXFT_Is_Face_TT_OT(m_Face) == FXFT_FACE_FLAG_SFNT; | |
220 } | |
221 | |
222 int CFX_Font::GetAscent() const { | |
223 if (!m_Face) | |
224 return 0; | |
225 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), | |
226 FXFT_Get_Face_Ascender(m_Face)); | |
227 } | |
228 | |
229 int CFX_Font::GetDescent() const { | |
230 if (!m_Face) | |
231 return 0; | |
232 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), | |
233 FXFT_Get_Face_Descender(m_Face)); | |
234 } | |
235 | |
236 FX_BOOL CFX_Font::GetGlyphBBox(uint32_t glyph_index, FX_RECT& bbox) { | |
237 if (!m_Face) | |
238 return FALSE; | |
239 | |
240 if (FXFT_Is_Face_Tricky(m_Face)) { | |
241 int error = FXFT_Set_Char_Size(m_Face, 0, 1000 * 64, 72, 72); | |
242 if (error) { | |
243 return FALSE; | |
244 } | |
245 error = FXFT_Load_Glyph(m_Face, glyph_index, | |
246 FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH); | |
247 if (error) { | |
248 return FALSE; | |
249 } | |
250 FXFT_BBox cbox; | |
251 FT_Glyph glyph; | |
252 error = FXFT_Get_Glyph(((FXFT_Face)m_Face)->glyph, &glyph); | |
253 if (error) { | |
254 return FALSE; | |
255 } | |
256 FXFT_Glyph_Get_CBox(glyph, FXFT_GLYPH_BBOX_PIXELS, &cbox); | |
257 int pixel_size_x = ((FXFT_Face)m_Face)->size->metrics.x_ppem, | |
258 pixel_size_y = ((FXFT_Face)m_Face)->size->metrics.y_ppem; | |
259 if (pixel_size_x == 0 || pixel_size_y == 0) { | |
260 bbox.left = cbox.xMin; | |
261 bbox.right = cbox.xMax; | |
262 bbox.top = cbox.yMax; | |
263 bbox.bottom = cbox.yMin; | |
264 } else { | |
265 bbox.left = cbox.xMin * 1000 / pixel_size_x; | |
266 bbox.right = cbox.xMax * 1000 / pixel_size_x; | |
267 bbox.top = cbox.yMax * 1000 / pixel_size_y; | |
268 bbox.bottom = cbox.yMin * 1000 / pixel_size_y; | |
269 } | |
270 if (bbox.top > FXFT_Get_Face_Ascender(m_Face)) { | |
271 bbox.top = FXFT_Get_Face_Ascender(m_Face); | |
272 } | |
273 if (bbox.bottom < FXFT_Get_Face_Descender(m_Face)) { | |
274 bbox.bottom = FXFT_Get_Face_Descender(m_Face); | |
275 } | |
276 FT_Done_Glyph(glyph); | |
277 return FXFT_Set_Pixel_Sizes(m_Face, 0, 64) == 0; | |
278 } | |
279 if (FXFT_Load_Glyph( | |
280 m_Face, glyph_index, | |
281 FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH)) { | |
282 return FALSE; | |
283 } | |
284 int em = FXFT_Get_Face_UnitsPerEM(m_Face); | |
285 if (em == 0) { | |
286 bbox.left = FXFT_Get_Glyph_HoriBearingX(m_Face); | |
287 bbox.bottom = FXFT_Get_Glyph_HoriBearingY(m_Face); | |
288 bbox.top = bbox.bottom - FXFT_Get_Glyph_Height(m_Face); | |
289 bbox.right = bbox.left + FXFT_Get_Glyph_Width(m_Face); | |
290 } else { | |
291 bbox.left = FXFT_Get_Glyph_HoriBearingX(m_Face) * 1000 / em; | |
292 bbox.top = | |
293 (FXFT_Get_Glyph_HoriBearingY(m_Face) - FXFT_Get_Glyph_Height(m_Face)) * | |
294 1000 / em; | |
295 bbox.right = | |
296 (FXFT_Get_Glyph_HoriBearingX(m_Face) + FXFT_Get_Glyph_Width(m_Face)) * | |
297 1000 / em; | |
298 bbox.bottom = (FXFT_Get_Glyph_HoriBearingY(m_Face)) * 1000 / em; | |
299 } | |
300 return TRUE; | |
301 } | |
302 | |
303 FX_BOOL CFX_Font::IsItalic() const { | |
304 if (!m_Face) | |
305 return FALSE; | |
306 | |
307 FX_BOOL ret = FXFT_Is_Face_Italic(m_Face) == FXFT_STYLE_FLAG_ITALIC; | |
308 if (!ret) { | |
309 CFX_ByteString str(FXFT_Get_Face_Style_Name(m_Face)); | |
310 str.MakeLower(); | |
311 if (str.Find("italic") != -1) { | |
312 ret = TRUE; | |
313 } | |
314 } | |
315 return ret; | |
316 } | |
317 | |
318 FX_BOOL CFX_Font::IsBold() const { | |
319 if (!m_Face) | |
320 return FALSE; | |
321 return FXFT_Is_Face_Bold(m_Face) == FXFT_STYLE_FLAG_BOLD; | |
322 } | |
323 | |
324 FX_BOOL CFX_Font::IsFixedWidth() const { | |
325 if (!m_Face) | |
326 return FALSE; | |
327 return FXFT_Is_Face_fixedwidth(m_Face); | |
328 } | |
329 | |
330 CFX_ByteString CFX_Font::GetPsName() const { | |
331 if (!m_Face) | |
332 return CFX_ByteString(); | |
333 | |
334 CFX_ByteString psName = FXFT_Get_Postscript_Name(m_Face); | |
335 if (psName.IsEmpty()) | |
336 psName = "Untitled"; | |
337 return psName; | |
338 } | |
339 | |
340 CFX_ByteString CFX_Font::GetFamilyName() const { | |
341 if (!m_Face && !m_pSubstFont) { | |
342 return CFX_ByteString(); | |
343 } | |
344 if (m_Face) { | |
345 return CFX_ByteString(FXFT_Get_Face_Family_Name(m_Face)); | |
346 } | |
347 return m_pSubstFont->m_Family; | |
348 } | |
349 CFX_ByteString CFX_Font::GetFaceName() const { | |
350 if (!m_Face && !m_pSubstFont) { | |
351 return CFX_ByteString(); | |
352 } | |
353 if (m_Face) { | |
354 CFX_ByteString facename; | |
355 CFX_ByteString style = CFX_ByteString(FXFT_Get_Face_Style_Name(m_Face)); | |
356 facename = GetFamilyName(); | |
357 if (facename.IsEmpty()) { | |
358 facename = "Untitled"; | |
359 } | |
360 if (!style.IsEmpty() && style != "Regular") { | |
361 facename += " " + style; | |
362 } | |
363 return facename; | |
364 } | |
365 return m_pSubstFont->m_Family; | |
366 } | |
367 FX_BOOL CFX_Font::GetBBox(FX_RECT& bbox) { | |
368 if (!m_Face) { | |
369 return FALSE; | |
370 } | |
371 int em = FXFT_Get_Face_UnitsPerEM(m_Face); | |
372 if (em == 0) { | |
373 bbox.left = FXFT_Get_Face_xMin(m_Face); | |
374 bbox.bottom = FXFT_Get_Face_yMax(m_Face); | |
375 bbox.top = FXFT_Get_Face_yMin(m_Face); | |
376 bbox.right = FXFT_Get_Face_xMax(m_Face); | |
377 } else { | |
378 bbox.left = FXFT_Get_Face_xMin(m_Face) * 1000 / em; | |
379 bbox.top = FXFT_Get_Face_yMin(m_Face) * 1000 / em; | |
380 bbox.right = FXFT_Get_Face_xMax(m_Face) * 1000 / em; | |
381 bbox.bottom = FXFT_Get_Face_yMax(m_Face) * 1000 / em; | |
382 } | |
383 return TRUE; | |
384 } | |
385 | |
386 int CFX_Font::GetHeight() const { | |
387 if (!m_Face) | |
388 return 0; | |
389 | |
390 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), | |
391 FXFT_Get_Face_Height(m_Face)); | |
392 } | |
393 | |
394 int CFX_Font::GetMaxAdvanceWidth() const { | |
395 if (!m_Face) | |
396 return 0; | |
397 | |
398 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), | |
399 FXFT_Get_Face_MaxAdvanceWidth(m_Face)); | |
400 } | |
401 | |
402 int CFX_Font::GetULPos() const { | |
403 if (!m_Face) | |
404 return 0; | |
405 | |
406 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), | |
407 FXFT_Get_Face_UnderLinePosition(m_Face)); | |
408 } | |
409 | |
410 int CFX_Font::GetULthickness() const { | |
411 if (!m_Face) | |
412 return 0; | |
413 | |
414 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), | |
415 FXFT_Get_Face_UnderLineThickness(m_Face)); | |
416 } | |
OLD | NEW |