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/ge/fx_text_int.h" | 7 #include "core/fxge/ge/fx_text_int.h" |
8 #include "core/include/fxge/fx_freetype.h" | 8 #include "core/include/fxge/fx_freetype.h" |
9 #include "core/include/fxge/fx_ge.h" | 9 #include "core/include/fxge/fx_ge.h" |
10 | 10 |
11 #define EM_ADJUST(em, a) (em == 0 ? (a) : (a)*1000 / em) | 11 #define EM_ADJUST(em, a) (em == 0 ? (a) : (a)*1000 / em) |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 #ifdef PDF_ENABLE_XFA | 15 #ifdef PDF_ENABLE_XFA |
16 const FX_DWORD g_EncodingID[] = { | 16 const uint32_t g_EncodingID[] = { |
17 FXFM_ENCODING_MS_SYMBOL, FXFM_ENCODING_UNICODE, | 17 FXFM_ENCODING_MS_SYMBOL, FXFM_ENCODING_UNICODE, |
18 FXFM_ENCODING_MS_SJIS, FXFM_ENCODING_MS_GB2312, | 18 FXFM_ENCODING_MS_SJIS, FXFM_ENCODING_MS_GB2312, |
19 FXFM_ENCODING_MS_BIG5, FXFM_ENCODING_MS_WANSUNG, | 19 FXFM_ENCODING_MS_BIG5, FXFM_ENCODING_MS_WANSUNG, |
20 FXFM_ENCODING_MS_JOHAB, FXFM_ENCODING_ADOBE_STANDARD, | 20 FXFM_ENCODING_MS_JOHAB, FXFM_ENCODING_ADOBE_STANDARD, |
21 FXFM_ENCODING_ADOBE_EXPERT, FXFM_ENCODING_ADOBE_CUSTOM, | 21 FXFM_ENCODING_ADOBE_EXPERT, FXFM_ENCODING_ADOBE_CUSTOM, |
22 FXFM_ENCODING_ADOBE_LATIN_1, FXFM_ENCODING_OLD_LATIN_2, | 22 FXFM_ENCODING_ADOBE_LATIN_1, FXFM_ENCODING_OLD_LATIN_2, |
23 FXFM_ENCODING_APPLE_ROMAN, | 23 FXFM_ENCODING_APPLE_ROMAN, |
24 }; | 24 }; |
25 | 25 |
26 CFX_UnicodeEncodingEx* _FXFM_CreateFontEncoding(CFX_Font* pFont, | 26 CFX_UnicodeEncodingEx* _FXFM_CreateFontEncoding(CFX_Font* pFont, |
27 FX_DWORD nEncodingID) { | 27 uint32_t nEncodingID) { |
28 if (FXFT_Select_Charmap(pFont->GetFace(), nEncodingID)) | 28 if (FXFT_Select_Charmap(pFont->GetFace(), nEncodingID)) |
29 return nullptr; | 29 return nullptr; |
30 return new CFX_UnicodeEncodingEx(pFont, nEncodingID); | 30 return new CFX_UnicodeEncodingEx(pFont, nEncodingID); |
31 } | 31 } |
32 #endif // PDF_ENABLE_XFA | 32 #endif // PDF_ENABLE_XFA |
33 | 33 |
34 FXFT_Face FT_LoadFont(const uint8_t* pData, int size) { | 34 FXFT_Face FT_LoadFont(const uint8_t* pData, int size) { |
35 return CFX_GEModule::Get()->GetFontMgr()->GetFixedFace(pData, size, 0); | 35 return CFX_GEModule::Get()->GetFontMgr()->GetFixedFace(pData, size, 0); |
36 } | 36 } |
37 | 37 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | 120 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
121 ReleasePlatformResource(); | 121 ReleasePlatformResource(); |
122 #endif | 122 #endif |
123 } | 123 } |
124 void CFX_Font::DeleteFace() { | 124 void CFX_Font::DeleteFace() { |
125 FXFT_Done_Face(m_Face); | 125 FXFT_Done_Face(m_Face); |
126 m_Face = NULL; | 126 m_Face = NULL; |
127 } | 127 } |
128 void CFX_Font::LoadSubst(const CFX_ByteString& face_name, | 128 void CFX_Font::LoadSubst(const CFX_ByteString& face_name, |
129 FX_BOOL bTrueType, | 129 FX_BOOL bTrueType, |
130 FX_DWORD flags, | 130 uint32_t flags, |
131 int weight, | 131 int weight, |
132 int italic_angle, | 132 int italic_angle, |
133 int CharsetCP, | 133 int CharsetCP, |
134 FX_BOOL bVertical) { | 134 FX_BOOL bVertical) { |
135 m_bEmbedded = FALSE; | 135 m_bEmbedded = FALSE; |
136 m_bVertical = bVertical; | 136 m_bVertical = bVertical; |
137 m_pSubstFont = new CFX_SubstFont; | 137 m_pSubstFont = new CFX_SubstFont; |
138 m_Face = CFX_GEModule::Get()->GetFontMgr()->FindSubstFont( | 138 m_Face = CFX_GEModule::Get()->GetFontMgr()->FindSubstFont( |
139 face_name, bTrueType, flags, weight, italic_angle, CharsetCP, | 139 face_name, bTrueType, flags, weight, italic_angle, CharsetCP, |
140 m_pSubstFont); | 140 m_pSubstFont); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 return FALSE; | 206 return FALSE; |
207 | 207 |
208 if (pFaceCount) | 208 if (pFaceCount) |
209 *pFaceCount = (int)m_Face->num_faces; | 209 *pFaceCount = (int)m_Face->num_faces; |
210 m_pOwnedStream = stream; | 210 m_pOwnedStream = stream; |
211 FXFT_Set_Pixel_Sizes(m_Face, 0, 64); | 211 FXFT_Set_Pixel_Sizes(m_Face, 0, 64); |
212 return TRUE; | 212 return TRUE; |
213 } | 213 } |
214 #endif // PDF_ENABLE_XFA | 214 #endif // PDF_ENABLE_XFA |
215 | 215 |
216 int CFX_Font::GetGlyphWidth(FX_DWORD glyph_index) { | 216 int CFX_Font::GetGlyphWidth(uint32_t glyph_index) { |
217 if (!m_Face) { | 217 if (!m_Face) { |
218 return 0; | 218 return 0; |
219 } | 219 } |
220 if (m_pSubstFont && (m_pSubstFont->m_SubstFlags & FXFONT_SUBST_MM)) { | 220 if (m_pSubstFont && (m_pSubstFont->m_SubstFlags & FXFONT_SUBST_MM)) { |
221 AdjustMMParams(glyph_index, 0, 0); | 221 AdjustMMParams(glyph_index, 0, 0); |
222 } | 222 } |
223 int err = FXFT_Load_Glyph( | 223 int err = FXFT_Load_Glyph( |
224 m_Face, glyph_index, | 224 m_Face, glyph_index, |
225 FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH); | 225 FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH); |
226 if (err) { | 226 if (err) { |
227 return 0; | 227 return 0; |
228 } | 228 } |
229 int width = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), | 229 int width = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
230 FXFT_Get_Glyph_HoriAdvance(m_Face)); | 230 FXFT_Get_Glyph_HoriAdvance(m_Face)); |
231 return width; | 231 return width; |
232 } | 232 } |
233 | 233 |
234 FX_BOOL CFX_Font::LoadEmbedded(const uint8_t* data, FX_DWORD size) { | 234 FX_BOOL CFX_Font::LoadEmbedded(const uint8_t* data, uint32_t size) { |
235 m_pFontDataAllocation = FX_Alloc(uint8_t, size); | 235 m_pFontDataAllocation = FX_Alloc(uint8_t, size); |
236 FXSYS_memcpy(m_pFontDataAllocation, data, size); | 236 FXSYS_memcpy(m_pFontDataAllocation, data, size); |
237 m_Face = FT_LoadFont(m_pFontDataAllocation, size); | 237 m_Face = FT_LoadFont(m_pFontDataAllocation, size); |
238 m_pFontData = m_pFontDataAllocation; | 238 m_pFontData = m_pFontDataAllocation; |
239 m_bEmbedded = TRUE; | 239 m_bEmbedded = TRUE; |
240 m_dwSize = size; | 240 m_dwSize = size; |
241 return m_Face != NULL; | 241 return m_Face != NULL; |
242 } | 242 } |
243 | 243 |
244 FX_BOOL CFX_Font::IsTTFont() const { | 244 FX_BOOL CFX_Font::IsTTFont() const { |
245 if (!m_Face) | 245 if (!m_Face) |
246 return FALSE; | 246 return FALSE; |
247 return FXFT_Is_Face_TT_OT(m_Face) == FXFT_FACE_FLAG_SFNT; | 247 return FXFT_Is_Face_TT_OT(m_Face) == FXFT_FACE_FLAG_SFNT; |
248 } | 248 } |
249 | 249 |
250 int CFX_Font::GetAscent() const { | 250 int CFX_Font::GetAscent() const { |
251 if (!m_Face) | 251 if (!m_Face) |
252 return 0; | 252 return 0; |
253 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), | 253 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
254 FXFT_Get_Face_Ascender(m_Face)); | 254 FXFT_Get_Face_Ascender(m_Face)); |
255 } | 255 } |
256 | 256 |
257 int CFX_Font::GetDescent() const { | 257 int CFX_Font::GetDescent() const { |
258 if (!m_Face) | 258 if (!m_Face) |
259 return 0; | 259 return 0; |
260 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), | 260 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
261 FXFT_Get_Face_Descender(m_Face)); | 261 FXFT_Get_Face_Descender(m_Face)); |
262 } | 262 } |
263 | 263 |
264 FX_BOOL CFX_Font::GetGlyphBBox(FX_DWORD glyph_index, FX_RECT& bbox) { | 264 FX_BOOL CFX_Font::GetGlyphBBox(uint32_t glyph_index, FX_RECT& bbox) { |
265 if (!m_Face) | 265 if (!m_Face) |
266 return FALSE; | 266 return FALSE; |
267 | 267 |
268 if (FXFT_Is_Face_Tricky(m_Face)) { | 268 if (FXFT_Is_Face_Tricky(m_Face)) { |
269 int error = FXFT_Set_Char_Size(m_Face, 0, 1000 * 64, 72, 72); | 269 int error = FXFT_Set_Char_Size(m_Face, 0, 1000 * 64, 72, 72); |
270 if (error) { | 270 if (error) { |
271 return FALSE; | 271 return FALSE; |
272 } | 272 } |
273 error = FXFT_Load_Glyph(m_Face, glyph_index, | 273 error = FXFT_Load_Glyph(m_Face, glyph_index, |
274 FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH); | 274 FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH); |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 return 0; | 441 return 0; |
442 | 442 |
443 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), | 443 return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
444 FXFT_Get_Face_UnderLineThickness(m_Face)); | 444 FXFT_Get_Face_UnderLineThickness(m_Face)); |
445 } | 445 } |
446 | 446 |
447 CFX_UnicodeEncoding::CFX_UnicodeEncoding(CFX_Font* pFont) : m_pFont(pFont) {} | 447 CFX_UnicodeEncoding::CFX_UnicodeEncoding(CFX_Font* pFont) : m_pFont(pFont) {} |
448 | 448 |
449 CFX_UnicodeEncoding::~CFX_UnicodeEncoding() {} | 449 CFX_UnicodeEncoding::~CFX_UnicodeEncoding() {} |
450 | 450 |
451 FX_DWORD CFX_UnicodeEncoding::GlyphFromCharCode(FX_DWORD charcode) { | 451 uint32_t CFX_UnicodeEncoding::GlyphFromCharCode(uint32_t charcode) { |
452 FXFT_Face face = m_pFont->GetFace(); | 452 FXFT_Face face = m_pFont->GetFace(); |
453 if (!face) | 453 if (!face) |
454 return charcode; | 454 return charcode; |
455 | 455 |
456 if (FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE) == 0) | 456 if (FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE) == 0) |
457 return FXFT_Get_Char_Index(face, charcode); | 457 return FXFT_Get_Char_Index(face, charcode); |
458 | 458 |
459 if (m_pFont->GetSubstFont() && m_pFont->GetSubstFont()->m_Charset == 2) { | 459 if (m_pFont->GetSubstFont() && m_pFont->GetSubstFont()->m_Charset == 2) { |
460 FX_DWORD index = 0; | 460 uint32_t index = 0; |
461 if (FXFT_Select_Charmap(face, FXFT_ENCODING_MS_SYMBOL) == 0) | 461 if (FXFT_Select_Charmap(face, FXFT_ENCODING_MS_SYMBOL) == 0) |
462 index = FXFT_Get_Char_Index(face, charcode); | 462 index = FXFT_Get_Char_Index(face, charcode); |
463 if (!index && !FXFT_Select_Charmap(face, FXFT_ENCODING_APPLE_ROMAN)) | 463 if (!index && !FXFT_Select_Charmap(face, FXFT_ENCODING_APPLE_ROMAN)) |
464 return FXFT_Get_Char_Index(face, charcode); | 464 return FXFT_Get_Char_Index(face, charcode); |
465 } | 465 } |
466 return charcode; | 466 return charcode; |
467 } | 467 } |
468 | 468 |
469 #ifdef PDF_ENABLE_XFA | 469 #ifdef PDF_ENABLE_XFA |
470 CFX_UnicodeEncodingEx::CFX_UnicodeEncodingEx(CFX_Font* pFont, | 470 CFX_UnicodeEncodingEx::CFX_UnicodeEncodingEx(CFX_Font* pFont, |
471 FX_DWORD EncodingID) | 471 uint32_t EncodingID) |
472 : CFX_UnicodeEncoding(pFont), m_nEncodingID(EncodingID) {} | 472 : CFX_UnicodeEncoding(pFont), m_nEncodingID(EncodingID) {} |
473 | 473 |
474 CFX_UnicodeEncodingEx::~CFX_UnicodeEncodingEx() {} | 474 CFX_UnicodeEncodingEx::~CFX_UnicodeEncodingEx() {} |
475 | 475 |
476 FX_DWORD CFX_UnicodeEncodingEx::GlyphFromCharCode(FX_DWORD charcode) { | 476 uint32_t CFX_UnicodeEncodingEx::GlyphFromCharCode(uint32_t charcode) { |
477 FXFT_Face face = m_pFont->GetFace(); | 477 FXFT_Face face = m_pFont->GetFace(); |
478 FT_UInt nIndex = FXFT_Get_Char_Index(face, charcode); | 478 FT_UInt nIndex = FXFT_Get_Char_Index(face, charcode); |
479 if (nIndex > 0) { | 479 if (nIndex > 0) { |
480 return nIndex; | 480 return nIndex; |
481 } | 481 } |
482 int nmaps = FXFT_Get_Face_CharmapCount(face); | 482 int nmaps = FXFT_Get_Face_CharmapCount(face); |
483 int m = 0; | 483 int m = 0; |
484 while (m < nmaps) { | 484 while (m < nmaps) { |
485 FX_DWORD nEncodingID = | 485 uint32_t nEncodingID = |
486 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[m++]); | 486 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[m++]); |
487 if (m_nEncodingID == nEncodingID) { | 487 if (m_nEncodingID == nEncodingID) { |
488 continue; | 488 continue; |
489 } | 489 } |
490 int error = FXFT_Select_Charmap(face, nEncodingID); | 490 int error = FXFT_Select_Charmap(face, nEncodingID); |
491 if (error) { | 491 if (error) { |
492 continue; | 492 continue; |
493 } | 493 } |
494 nIndex = FXFT_Get_Char_Index(face, charcode); | 494 nIndex = FXFT_Get_Char_Index(face, charcode); |
495 if (nIndex > 0) { | 495 if (nIndex > 0) { |
496 m_nEncodingID = nEncodingID; | 496 m_nEncodingID = nEncodingID; |
497 return nIndex; | 497 return nIndex; |
498 } | 498 } |
499 } | 499 } |
500 FXFT_Select_Charmap(face, m_nEncodingID); | 500 FXFT_Select_Charmap(face, m_nEncodingID); |
501 return 0; | 501 return 0; |
502 } | 502 } |
503 | 503 |
504 FX_DWORD CFX_UnicodeEncodingEx::CharCodeFromUnicode(FX_WCHAR Unicode) const { | 504 uint32_t CFX_UnicodeEncodingEx::CharCodeFromUnicode(FX_WCHAR Unicode) const { |
505 if (m_nEncodingID == FXFM_ENCODING_UNICODE || | 505 if (m_nEncodingID == FXFM_ENCODING_UNICODE || |
506 m_nEncodingID == FXFM_ENCODING_MS_SYMBOL) { | 506 m_nEncodingID == FXFM_ENCODING_MS_SYMBOL) { |
507 return Unicode; | 507 return Unicode; |
508 } | 508 } |
509 FXFT_Face face = m_pFont->GetFace(); | 509 FXFT_Face face = m_pFont->GetFace(); |
510 int nmaps = FXFT_Get_Face_CharmapCount(face); | 510 int nmaps = FXFT_Get_Face_CharmapCount(face); |
511 for (int i = 0; i < nmaps; i++) { | 511 for (int i = 0; i < nmaps; i++) { |
512 int nEncodingID = | 512 int nEncodingID = |
513 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[i]); | 513 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[i]); |
514 if (nEncodingID == FXFM_ENCODING_UNICODE || | 514 if (nEncodingID == FXFM_ENCODING_UNICODE || |
515 nEncodingID == FXFM_ENCODING_MS_SYMBOL) { | 515 nEncodingID == FXFM_ENCODING_MS_SYMBOL) { |
516 return Unicode; | 516 return Unicode; |
517 } | 517 } |
518 } | 518 } |
519 return static_cast<FX_DWORD>(-1); | 519 return static_cast<uint32_t>(-1); |
520 } | 520 } |
521 | 521 |
522 CFX_UnicodeEncodingEx* FX_CreateFontEncodingEx(CFX_Font* pFont, | 522 CFX_UnicodeEncodingEx* FX_CreateFontEncodingEx(CFX_Font* pFont, |
523 FX_DWORD nEncodingID) { | 523 uint32_t nEncodingID) { |
524 if (!pFont || !pFont->GetFace()) | 524 if (!pFont || !pFont->GetFace()) |
525 return nullptr; | 525 return nullptr; |
526 | 526 |
527 if (nEncodingID != FXFM_ENCODING_NONE) | 527 if (nEncodingID != FXFM_ENCODING_NONE) |
528 return _FXFM_CreateFontEncoding(pFont, nEncodingID); | 528 return _FXFM_CreateFontEncoding(pFont, nEncodingID); |
529 | 529 |
530 for (size_t i = 0; i < FX_ArraySize(g_EncodingID); ++i) { | 530 for (size_t i = 0; i < FX_ArraySize(g_EncodingID); ++i) { |
531 CFX_UnicodeEncodingEx* pFontEncoding = | 531 CFX_UnicodeEncodingEx* pFontEncoding = |
532 _FXFM_CreateFontEncoding(pFont, g_EncodingID[i]); | 532 _FXFM_CreateFontEncoding(pFont, g_EncodingID[i]); |
533 if (pFontEncoding) { | 533 if (pFontEncoding) { |
534 return pFontEncoding; | 534 return pFontEncoding; |
535 } | 535 } |
536 } | 536 } |
537 return NULL; | 537 return NULL; |
538 } | 538 } |
539 #endif // PDF_ENABLE_XFA | 539 #endif // PDF_ENABLE_XFA |
OLD | NEW |