| 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 "../../../include/fxge/fx_ge.h" | 7 #include "../../../include/fxge/fx_ge.h" |
| 8 #include "../../../include/fxge/fx_freetype.h" | 8 #include "../../../include/fxge/fx_freetype.h" |
| 9 #include "text_int.h" | 9 #include "text_int.h" |
| 10 |
| 10 #define EM_ADJUST(em, a) (em == 0 ? (a) : (a)*1000 / em) | 11 #define EM_ADJUST(em, a) (em == 0 ? (a) : (a)*1000 / em) |
| 11 extern void _FPDFAPI_GetInternalFontData(int id1, | 12 extern void _FPDFAPI_GetInternalFontData(int id1, |
| 12 const uint8_t*& data, | 13 const uint8_t*& data, |
| 13 FX_DWORD& size); | 14 FX_DWORD& size); |
| 15 |
| 16 namespace { |
| 17 |
| 18 const FX_DWORD g_EncodingID[] = { |
| 19 FXFM_ENCODING_MS_SYMBOL, |
| 20 FXFM_ENCODING_UNICODE, |
| 21 FXFM_ENCODING_MS_SJIS, |
| 22 FXFM_ENCODING_MS_GB2312, |
| 23 FXFM_ENCODING_MS_BIG5, |
| 24 FXFM_ENCODING_MS_WANSUNG, |
| 25 FXFM_ENCODING_MS_JOHAB, |
| 26 FXFM_ENCODING_ADOBE_STANDARD, |
| 27 FXFM_ENCODING_ADOBE_EXPERT, |
| 28 FXFM_ENCODING_ADOBE_CUSTOM, |
| 29 FXFM_ENCODING_ADOBE_LATIN_1, |
| 30 FXFM_ENCODING_OLD_LATIN_2, |
| 31 FXFM_ENCODING_APPLE_ROMAN, |
| 32 }; |
| 33 |
| 34 CFX_UnicodeEncodingEx* _FXFM_CreateFontEncoding(CFX_Font* pFont, |
| 35 FX_DWORD nEncodingID) { |
| 36 if (FXFT_Select_Charmap(pFont->m_Face, nEncodingID)) |
| 37 return nullptr; |
| 38 return new CFX_UnicodeEncodingEx(pFont, nEncodingID); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 14 CFX_Font::CFX_Font() { | 43 CFX_Font::CFX_Font() { |
| 15 m_pSubstFont = NULL; | 44 m_pSubstFont = NULL; |
| 16 m_Face = NULL; | 45 m_Face = NULL; |
| 17 m_bEmbedded = FALSE; | 46 m_bEmbedded = FALSE; |
| 18 m_bVertical = FALSE; | 47 m_bVertical = FALSE; |
| 19 m_pFontData = NULL; | 48 m_pFontData = NULL; |
| 20 m_pFontDataAllocation = NULL; | 49 m_pFontDataAllocation = NULL; |
| 21 m_dwSize = 0; | 50 m_dwSize = 0; |
| 22 m_pOwnedStream = NULL; | 51 m_pOwnedStream = NULL; |
| 23 m_pGsubData = NULL; | 52 m_pGsubData = NULL; |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 return pos; | 442 return pos; |
| 414 } | 443 } |
| 415 int CFX_Font::GetULthickness() { | 444 int CFX_Font::GetULthickness() { |
| 416 if (m_Face == NULL) { | 445 if (m_Face == NULL) { |
| 417 return 0; | 446 return 0; |
| 418 } | 447 } |
| 419 int thickness = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), | 448 int thickness = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), |
| 420 FXFT_Get_Face_UnderLineThickness(m_Face)); | 449 FXFT_Get_Face_UnderLineThickness(m_Face)); |
| 421 return thickness; | 450 return thickness; |
| 422 } | 451 } |
| 423 CFX_UnicodeEncoding::CFX_UnicodeEncoding(CFX_Font* pFont) { | 452 |
| 424 m_pFont = pFont; | 453 CFX_UnicodeEncoding::CFX_UnicodeEncoding(CFX_Font* pFont) : m_pFont(pFont) { |
| 425 } | 454 } |
| 455 |
| 456 CFX_UnicodeEncoding::~CFX_UnicodeEncoding() { |
| 457 } |
| 458 |
| 426 FX_DWORD CFX_UnicodeEncoding::GlyphFromCharCode(FX_DWORD charcode) { | 459 FX_DWORD CFX_UnicodeEncoding::GlyphFromCharCode(FX_DWORD charcode) { |
| 427 FXFT_Face face = m_pFont->GetFace(); | 460 FXFT_Face face = m_pFont->GetFace(); |
| 428 if (!face) { | 461 if (!face) |
| 429 return charcode; | 462 return charcode; |
| 430 } | 463 |
| 431 if (FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE) == 0) { | 464 if (FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE) == 0) |
| 432 return FXFT_Get_Char_Index(face, charcode); | 465 return FXFT_Get_Char_Index(face, charcode); |
| 433 } | 466 |
| 434 if (m_pFont->m_pSubstFont && m_pFont->m_pSubstFont->m_Charset == 2) { | 467 if (m_pFont->m_pSubstFont && m_pFont->m_pSubstFont->m_Charset == 2) { |
| 435 FX_DWORD index = 0; | 468 FX_DWORD index = 0; |
| 436 if (FXFT_Select_Charmap(face, FXFT_ENCODING_MS_SYMBOL) == 0) { | 469 if (FXFT_Select_Charmap(face, FXFT_ENCODING_MS_SYMBOL) == 0) |
| 437 index = FXFT_Get_Char_Index(face, charcode); | 470 index = FXFT_Get_Char_Index(face, charcode); |
| 438 } | 471 if (!index && !FXFT_Select_Charmap(face, FXFT_ENCODING_APPLE_ROMAN)) |
| 439 if (!index && !FXFT_Select_Charmap(face, FXFT_ENCODING_APPLE_ROMAN)) { | |
| 440 return FXFT_Get_Char_Index(face, charcode); | 472 return FXFT_Get_Char_Index(face, charcode); |
| 441 } | |
| 442 } | 473 } |
| 443 return charcode; | 474 return charcode; |
| 444 } | 475 } |
| 445 FX_DWORD CFX_UnicodeEncoding::GlyphFromCharCodeEx(FX_DWORD charcode, | 476 |
| 446 int encoding) { | 477 CFX_UnicodeEncodingEx::CFX_UnicodeEncodingEx(CFX_Font* pFont, |
| 478 FX_DWORD EncodingID) |
| 479 : CFX_UnicodeEncoding(pFont), m_nEncodingID(EncodingID) { |
| 480 } |
| 481 |
| 482 CFX_UnicodeEncodingEx::~CFX_UnicodeEncodingEx() { |
| 483 } |
| 484 |
| 485 FX_DWORD CFX_UnicodeEncodingEx::GlyphFromCharCode(FX_DWORD charcode) { |
| 447 FXFT_Face face = m_pFont->GetFace(); | 486 FXFT_Face face = m_pFont->GetFace(); |
| 448 if (!face) { | |
| 449 return charcode; | |
| 450 } | |
| 451 if (encoding == ENCODING_UNICODE) { | |
| 452 return GlyphFromCharCode(charcode); | |
| 453 } | |
| 454 int nmaps = FXFT_Get_Face_CharmapCount(m_pFont->m_Face); | |
| 455 int i = 0; | |
| 456 while (i < nmaps) { | |
| 457 int encoding = FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[i++]); | |
| 458 if (encoding != FXFT_ENCODING_UNICODE) { | |
| 459 FXFT_Select_Charmap(face, encoding); | |
| 460 break; | |
| 461 } | |
| 462 } | |
| 463 return FXFT_Get_Char_Index(face, charcode); | |
| 464 } | |
| 465 IFX_FontEncoding* FXGE_CreateUnicodeEncoding(CFX_Font* pFont) { | |
| 466 CFX_UnicodeEncoding* pEncoding = NULL; | |
| 467 pEncoding = new CFX_UnicodeEncoding(pFont); | |
| 468 return pEncoding; | |
| 469 } | |
| 470 CFX_FontEncodingEX::CFX_FontEncodingEX() { | |
| 471 m_pFont = NULL; | |
| 472 m_nEncodingID = FXFM_ENCODING_NONE; | |
| 473 } | |
| 474 FX_BOOL CFX_FontEncodingEX::Init(CFX_Font* pFont, FX_DWORD EncodingID) { | |
| 475 if (!pFont) { | |
| 476 return FALSE; | |
| 477 } | |
| 478 m_pFont = pFont; | |
| 479 m_nEncodingID = EncodingID; | |
| 480 return TRUE; | |
| 481 } | |
| 482 FX_DWORD CFX_FontEncodingEX::GlyphFromCharCode(FX_DWORD charcode) { | |
| 483 FXFT_Face face = m_pFont->m_Face; | |
| 484 FT_UInt nIndex = FXFT_Get_Char_Index(face, charcode); | 487 FT_UInt nIndex = FXFT_Get_Char_Index(face, charcode); |
| 485 if (nIndex > 0) { | 488 if (nIndex > 0) { |
| 486 return nIndex; | 489 return nIndex; |
| 487 } | 490 } |
| 488 int nmaps = FXFT_Get_Face_CharmapCount(face); | 491 int nmaps = FXFT_Get_Face_CharmapCount(face); |
| 489 int m = 0; | 492 int m = 0; |
| 490 while (m < nmaps) { | 493 while (m < nmaps) { |
| 491 int nEncodingID = | 494 int nEncodingID = |
| 492 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[m++]); | 495 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[m++]); |
| 493 if (m_nEncodingID == nEncodingID) { | 496 if (m_nEncodingID == nEncodingID) { |
| 494 continue; | 497 continue; |
| 495 } | 498 } |
| 496 int error = FXFT_Select_Charmap(face, nEncodingID); | 499 int error = FXFT_Select_Charmap(face, nEncodingID); |
| 497 if (error) { | 500 if (error) { |
| 498 continue; | 501 continue; |
| 499 } | 502 } |
| 500 nIndex = FXFT_Get_Char_Index(face, charcode); | 503 nIndex = FXFT_Get_Char_Index(face, charcode); |
| 501 if (nIndex > 0) { | 504 if (nIndex > 0) { |
| 502 m_nEncodingID = nEncodingID; | 505 m_nEncodingID = nEncodingID; |
| 503 return nIndex; | 506 return nIndex; |
| 504 } | 507 } |
| 505 } | 508 } |
| 506 FXFT_Select_Charmap(face, m_nEncodingID); | 509 FXFT_Select_Charmap(face, m_nEncodingID); |
| 507 return 0; | 510 return 0; |
| 508 } | 511 } |
| 509 CFX_WideString CFX_FontEncodingEX::UnicodeFromCharCode( | 512 |
| 510 FX_DWORD charcode) const { | 513 FX_DWORD CFX_UnicodeEncodingEx::CharCodeFromUnicode(FX_WCHAR Unicode) const { |
| 511 if (m_nEncodingID == FXFM_ENCODING_UNICODE) { | |
| 512 return CFX_WideString((FX_WCHAR)charcode); | |
| 513 } | |
| 514 return CFX_WideString((FX_WCHAR)0); | |
| 515 } | |
| 516 FX_DWORD CFX_FontEncodingEX::CharCodeFromUnicode(FX_WCHAR Unicode) const { | |
| 517 if (m_nEncodingID == FXFM_ENCODING_UNICODE || | 514 if (m_nEncodingID == FXFM_ENCODING_UNICODE || |
| 518 m_nEncodingID == FXFM_ENCODING_MS_SYMBOL) { | 515 m_nEncodingID == FXFM_ENCODING_MS_SYMBOL) { |
| 519 return Unicode; | 516 return Unicode; |
| 520 } | 517 } |
| 521 FXFT_Face face = m_pFont->m_Face; | 518 FXFT_Face face = m_pFont->m_Face; |
| 522 int nmaps = FXFT_Get_Face_CharmapCount(face); | 519 int nmaps = FXFT_Get_Face_CharmapCount(face); |
| 523 for (int i = 0; i < nmaps; i++) { | 520 for (int i = 0; i < nmaps; i++) { |
| 524 int nEncodingID = | 521 int nEncodingID = |
| 525 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[i]); | 522 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[i]); |
| 526 if (nEncodingID == FXFM_ENCODING_UNICODE || | 523 if (nEncodingID == FXFM_ENCODING_UNICODE || |
| 527 nEncodingID == FXFM_ENCODING_MS_SYMBOL) { | 524 nEncodingID == FXFM_ENCODING_MS_SYMBOL) { |
| 528 return Unicode; | 525 return Unicode; |
| 529 } | 526 } |
| 530 } | 527 } |
| 531 return -1; | 528 return -1; |
| 532 } | 529 } |
| 533 FX_BOOL CFX_FontEncodingEX::IsUnicodeCompatible() const { | 530 |
| 534 return m_nEncodingID == FXFM_ENCODING_UNICODE; | 531 CFX_UnicodeEncodingEx* FX_CreateFontEncodingEx(CFX_Font* pFont, |
| 535 } | 532 FX_DWORD nEncodingID) { |
| 536 FX_DWORD CFX_FontEncodingEX::GlyphIndexFromName(const FX_CHAR* pStrName) { | 533 if (!pFont || !pFont->m_Face) |
| 537 FXFT_Face face = m_pFont->m_Face; | 534 return nullptr; |
| 538 return FT_Get_Name_Index(face, (FT_String*)pStrName); | 535 |
| 539 } | 536 if (nEncodingID != FXFM_ENCODING_NONE) |
| 540 CFX_ByteString CFX_FontEncodingEX::NameFromGlyphIndex(FX_DWORD dwGlyphIndex) { | |
| 541 FXFT_Face face = m_pFont->m_Face; | |
| 542 CFX_ByteString glyphName(" "); | |
| 543 if (FT_HAS_GLYPH_NAMES(((FT_Face)face))) { | |
| 544 if (FT_Get_Glyph_Name((FT_Face)face, dwGlyphIndex, | |
| 545 (FT_Pointer)(const FX_CHAR*)glyphName, 16)) { | |
| 546 glyphName.Empty(); | |
| 547 return glyphName; | |
| 548 } | |
| 549 return glyphName; | |
| 550 } else { | |
| 551 return glyphName; | |
| 552 } | |
| 553 } | |
| 554 FX_DWORD CFX_FontEncodingEX::CharCodeFromGlyphIndex(FX_DWORD dwGlyphIndex) { | |
| 555 FXFT_Face face = m_pFont->GetFace(); | |
| 556 FX_DWORD charcode; | |
| 557 FT_UInt gid; | |
| 558 charcode = FT_Get_First_Char((FT_Face)face, &gid); | |
| 559 while (gid != 0) { | |
| 560 if (dwGlyphIndex == gid) { | |
| 561 return charcode; | |
| 562 } | |
| 563 charcode = FT_Get_Next_Char((FT_Face)face, charcode, &gid); | |
| 564 } | |
| 565 int nmaps = FXFT_Get_Face_CharmapCount(face); | |
| 566 int m = 0; | |
| 567 while (m < nmaps) { | |
| 568 int nEncodingID = | |
| 569 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[m++]); | |
| 570 if (m_nEncodingID == nEncodingID) { | |
| 571 continue; | |
| 572 } | |
| 573 int error = FXFT_Select_Charmap(face, nEncodingID); | |
| 574 if (error) { | |
| 575 continue; | |
| 576 } | |
| 577 charcode = FT_Get_First_Char((FT_Face)face, &gid); | |
| 578 while (gid != 0) { | |
| 579 if (dwGlyphIndex == gid) { | |
| 580 m_nEncodingID = nEncodingID; | |
| 581 return charcode; | |
| 582 } | |
| 583 charcode = FT_Get_Next_Char((FT_Face)face, charcode, &gid); | |
| 584 } | |
| 585 } | |
| 586 return (FX_DWORD)-1; | |
| 587 } | |
| 588 static const FX_DWORD gs_EncodingID[] = { | |
| 589 FXFM_ENCODING_MS_SYMBOL, FXFM_ENCODING_UNICODE, | |
| 590 FXFM_ENCODING_MS_SJIS, FXFM_ENCODING_MS_GB2312, | |
| 591 FXFM_ENCODING_MS_BIG5, FXFM_ENCODING_MS_WANSUNG, | |
| 592 FXFM_ENCODING_MS_JOHAB, FXFM_ENCODING_ADOBE_STANDARD, | |
| 593 FXFM_ENCODING_ADOBE_EXPERT, FXFM_ENCODING_ADOBE_CUSTOM, | |
| 594 FXFM_ENCODING_ADOBE_LATIN_1, FXFM_ENCODING_OLD_LATIN_2, | |
| 595 FXFM_ENCODING_APPLE_ROMAN}; | |
| 596 static IFX_FontEncodingEx* _FXFM_CreateFontEncoding(CFX_Font* pFont, | |
| 597 FX_DWORD nEncodingID) { | |
| 598 int error = FXFT_Select_Charmap(pFont->m_Face, nEncodingID); | |
| 599 if (error) { | |
| 600 return NULL; | |
| 601 } | |
| 602 CFX_FontEncodingEX* pFontEncoding = new CFX_FontEncodingEX; | |
| 603 if (pFontEncoding && !pFontEncoding->Init(pFont, nEncodingID)) { | |
| 604 delete pFontEncoding; | |
| 605 pFontEncoding = NULL; | |
| 606 } | |
| 607 return pFontEncoding; | |
| 608 } | |
| 609 IFX_FontEncodingEx* FX_CreateFontEncodingEx(CFX_Font* pFont, | |
| 610 FX_DWORD nEncodingID) { | |
| 611 if (!pFont || !pFont->m_Face) { | |
| 612 return NULL; | |
| 613 } | |
| 614 if (nEncodingID != FXFM_ENCODING_NONE) { | |
| 615 return _FXFM_CreateFontEncoding(pFont, nEncodingID); | 537 return _FXFM_CreateFontEncoding(pFont, nEncodingID); |
| 616 } | 538 |
| 617 static int s_count = sizeof(gs_EncodingID) / sizeof(FX_DWORD); | 539 for (size_t i = 0; i < FX_ArraySize(g_EncodingID); ++i) { |
| 618 for (int i = 0; i < s_count; i++) { | 540 CFX_UnicodeEncodingEx* pFontEncoding = |
| 619 IFX_FontEncodingEx* pFontEncoding = | 541 _FXFM_CreateFontEncoding(pFont, g_EncodingID[i]); |
| 620 _FXFM_CreateFontEncoding(pFont, gs_EncodingID[i]); | |
| 621 if (pFontEncoding) { | 542 if (pFontEncoding) { |
| 622 return pFontEncoding; | 543 return pFontEncoding; |
| 623 } | 544 } |
| 624 } | 545 } |
| 625 return NULL; | 546 return NULL; |
| 626 } | 547 } |
| OLD | NEW |