Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: core/src/fxge/ge/fx_ge_font.cpp

Issue 1297083002: Merge to XFA: Cleanup CFX_UnicodeEncoding and remove IFX_FontEncoding. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 gs_EncodingID[] = {
19 FXFM_ENCODING_MS_SYMBOL,
Tom Sepez 2015/08/17 23:49:29 nit: indent seems off (2 vs 4 sp)?
Lei Zhang 2015/08/18 01:36:39 git cl format did this. :\ +thakis FYI
Nico 2015/08/18 02:19:34 That's intentional. clang-format formats braced in
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 int error = FXFT_Select_Charmap(pFont->m_Face, nEncodingID);
Tom Sepez 2015/08/17 23:49:28 nit: hardly seems worth having the local |error|.
Lei Zhang 2015/08/18 01:36:39 Done.
37 if (error)
38 return nullptr;
39 return new CFX_UnicodeEncodingEx(pFont, nEncodingID);
40 }
41
42 } // namespace
43
14 CFX_Font::CFX_Font() { 44 CFX_Font::CFX_Font() {
15 m_pSubstFont = NULL; 45 m_pSubstFont = NULL;
16 m_Face = NULL; 46 m_Face = NULL;
17 m_bEmbedded = FALSE; 47 m_bEmbedded = FALSE;
18 m_bVertical = FALSE; 48 m_bVertical = FALSE;
19 m_pFontData = NULL; 49 m_pFontData = NULL;
20 m_pFontDataAllocation = NULL; 50 m_pFontDataAllocation = NULL;
21 m_dwSize = 0; 51 m_dwSize = 0;
22 m_pOwnedStream = NULL; 52 m_pOwnedStream = NULL;
23 m_pGsubData = NULL; 53 m_pGsubData = NULL;
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 return pos; 443 return pos;
414 } 444 }
415 int CFX_Font::GetULthickness() { 445 int CFX_Font::GetULthickness() {
416 if (m_Face == NULL) { 446 if (m_Face == NULL) {
417 return 0; 447 return 0;
418 } 448 }
419 int thickness = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), 449 int thickness = EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face),
420 FXFT_Get_Face_UnderLineThickness(m_Face)); 450 FXFT_Get_Face_UnderLineThickness(m_Face));
421 return thickness; 451 return thickness;
422 } 452 }
423 CFX_UnicodeEncoding::CFX_UnicodeEncoding(CFX_Font* pFont) { 453
424 m_pFont = pFont; 454 CFX_UnicodeEncoding::CFX_UnicodeEncoding(CFX_Font* pFont) : m_pFont(pFont) {
425 } 455 }
456
457 CFX_UnicodeEncoding::~CFX_UnicodeEncoding() {
458 }
459
426 FX_DWORD CFX_UnicodeEncoding::GlyphFromCharCode(FX_DWORD charcode) { 460 FX_DWORD CFX_UnicodeEncoding::GlyphFromCharCode(FX_DWORD charcode) {
427 FXFT_Face face = m_pFont->GetFace(); 461 FXFT_Face face = m_pFont->GetFace();
428 if (!face) { 462 if (!face)
429 return charcode; 463 return charcode;
430 } 464
431 if (FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE) == 0) { 465 if (FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE) == 0)
432 return FXFT_Get_Char_Index(face, charcode); 466 return FXFT_Get_Char_Index(face, charcode);
433 } 467
434 if (m_pFont->m_pSubstFont && m_pFont->m_pSubstFont->m_Charset == 2) { 468 if (m_pFont->m_pSubstFont && m_pFont->m_pSubstFont->m_Charset == 2) {
435 FX_DWORD index = 0; 469 FX_DWORD index = 0;
436 if (FXFT_Select_Charmap(face, FXFT_ENCODING_MS_SYMBOL) == 0) { 470 if (FXFT_Select_Charmap(face, FXFT_ENCODING_MS_SYMBOL) == 0)
437 index = FXFT_Get_Char_Index(face, charcode); 471 index = FXFT_Get_Char_Index(face, charcode);
438 } 472 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); 473 return FXFT_Get_Char_Index(face, charcode);
441 }
442 } 474 }
443 return charcode; 475 return charcode;
444 } 476 }
445 FX_DWORD CFX_UnicodeEncoding::GlyphFromCharCodeEx(FX_DWORD charcode, 477
446 int encoding) { 478 CFX_UnicodeEncodingEx::CFX_UnicodeEncodingEx(CFX_Font* pFont,
479 FX_DWORD EncodingID)
480 : CFX_UnicodeEncoding(pFont), m_nEncodingID(EncodingID) {
481 }
482
483 CFX_UnicodeEncodingEx::~CFX_UnicodeEncodingEx() {
484 }
485
486 FX_DWORD CFX_UnicodeEncodingEx::GlyphFromCharCode(FX_DWORD charcode) {
447 FXFT_Face face = m_pFont->GetFace(); 487 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); 488 FT_UInt nIndex = FXFT_Get_Char_Index(face, charcode);
485 if (nIndex > 0) { 489 if (nIndex > 0) {
486 return nIndex; 490 return nIndex;
487 } 491 }
488 int nmaps = FXFT_Get_Face_CharmapCount(face); 492 int nmaps = FXFT_Get_Face_CharmapCount(face);
489 int m = 0; 493 int m = 0;
490 while (m < nmaps) { 494 while (m < nmaps) {
491 int nEncodingID = 495 int nEncodingID =
492 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[m++]); 496 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[m++]);
493 if (m_nEncodingID == nEncodingID) { 497 if (m_nEncodingID == nEncodingID) {
494 continue; 498 continue;
495 } 499 }
496 int error = FXFT_Select_Charmap(face, nEncodingID); 500 int error = FXFT_Select_Charmap(face, nEncodingID);
497 if (error) { 501 if (error) {
498 continue; 502 continue;
499 } 503 }
500 nIndex = FXFT_Get_Char_Index(face, charcode); 504 nIndex = FXFT_Get_Char_Index(face, charcode);
501 if (nIndex > 0) { 505 if (nIndex > 0) {
502 m_nEncodingID = nEncodingID; 506 m_nEncodingID = nEncodingID;
503 return nIndex; 507 return nIndex;
504 } 508 }
505 } 509 }
506 FXFT_Select_Charmap(face, m_nEncodingID); 510 FXFT_Select_Charmap(face, m_nEncodingID);
507 return 0; 511 return 0;
508 } 512 }
509 CFX_WideString CFX_FontEncodingEX::UnicodeFromCharCode( 513
510 FX_DWORD charcode) const { 514 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 || 515 if (m_nEncodingID == FXFM_ENCODING_UNICODE ||
518 m_nEncodingID == FXFM_ENCODING_MS_SYMBOL) { 516 m_nEncodingID == FXFM_ENCODING_MS_SYMBOL) {
519 return Unicode; 517 return Unicode;
520 } 518 }
521 FXFT_Face face = m_pFont->m_Face; 519 FXFT_Face face = m_pFont->m_Face;
522 int nmaps = FXFT_Get_Face_CharmapCount(face); 520 int nmaps = FXFT_Get_Face_CharmapCount(face);
523 for (int i = 0; i < nmaps; i++) { 521 for (int i = 0; i < nmaps; i++) {
524 int nEncodingID = 522 int nEncodingID =
525 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[i]); 523 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[i]);
526 if (nEncodingID == FXFM_ENCODING_UNICODE || 524 if (nEncodingID == FXFM_ENCODING_UNICODE ||
527 nEncodingID == FXFM_ENCODING_MS_SYMBOL) { 525 nEncodingID == FXFM_ENCODING_MS_SYMBOL) {
528 return Unicode; 526 return Unicode;
529 } 527 }
530 } 528 }
531 return -1; 529 return -1;
532 } 530 }
533 FX_BOOL CFX_FontEncodingEX::IsUnicodeCompatible() const { 531
534 return m_nEncodingID == FXFM_ENCODING_UNICODE; 532 CFX_UnicodeEncodingEx* FX_CreateFontEncodingEx(CFX_Font* pFont,
535 } 533 FX_DWORD nEncodingID) {
536 FX_DWORD CFX_FontEncodingEX::GlyphIndexFromName(const FX_CHAR* pStrName) { 534 if (!pFont || !pFont->m_Face)
537 FXFT_Face face = m_pFont->m_Face; 535 return nullptr;
538 return FT_Get_Name_Index(face, (FT_String*)pStrName); 536
539 } 537 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); 538 return _FXFM_CreateFontEncoding(pFont, nEncodingID);
616 } 539
617 static int s_count = sizeof(gs_EncodingID) / sizeof(FX_DWORD); 540 for (size_t i = 0; i < FX_ArraySize(gs_EncodingID); ++i) {
618 for (int i = 0; i < s_count; i++) { 541 CFX_UnicodeEncodingEx* pFontEncoding =
619 IFX_FontEncodingEx* pFontEncoding =
620 _FXFM_CreateFontEncoding(pFont, gs_EncodingID[i]); 542 _FXFM_CreateFontEncoding(pFont, gs_EncodingID[i]);
621 if (pFontEncoding) { 543 if (pFontEncoding) {
622 return pFontEncoding; 544 return pFontEncoding;
623 } 545 }
624 } 546 }
625 return NULL; 547 return NULL;
626 } 548 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698