• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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/cfx_unicodeencodingex.h"
8 
9 #include "core/fpdfapi/font/cpdf_font.h"
10 #include "core/fxge/fx_font.h"
11 #include "core/fxge/fx_freetype.h"
12 
13 namespace {
14 
15 const uint32_t g_EncodingID[] = {
16     FXFM_ENCODING_MS_SYMBOL,     FXFM_ENCODING_UNICODE,
17     FXFM_ENCODING_MS_SJIS,       FXFM_ENCODING_MS_GB2312,
18     FXFM_ENCODING_MS_BIG5,       FXFM_ENCODING_MS_WANSUNG,
19     FXFM_ENCODING_MS_JOHAB,      FXFM_ENCODING_ADOBE_STANDARD,
20     FXFM_ENCODING_ADOBE_EXPERT,  FXFM_ENCODING_ADOBE_CUSTOM,
21     FXFM_ENCODING_ADOBE_LATIN_1, FXFM_ENCODING_OLD_LATIN_2,
22     FXFM_ENCODING_APPLE_ROMAN,
23 };
24 
FXFM_CreateFontEncoding(CFX_Font * pFont,uint32_t nEncodingID)25 CFX_UnicodeEncodingEx* FXFM_CreateFontEncoding(CFX_Font* pFont,
26                                                uint32_t nEncodingID) {
27   if (FXFT_Select_Charmap(pFont->GetFace(), nEncodingID))
28     return nullptr;
29   return new CFX_UnicodeEncodingEx(pFont, nEncodingID);
30 }
31 
32 }  // namespace
33 
CFX_UnicodeEncodingEx(CFX_Font * pFont,uint32_t EncodingID)34 CFX_UnicodeEncodingEx::CFX_UnicodeEncodingEx(CFX_Font* pFont,
35                                              uint32_t EncodingID)
36     : CFX_UnicodeEncoding(pFont), m_nEncodingID(EncodingID) {}
37 
~CFX_UnicodeEncodingEx()38 CFX_UnicodeEncodingEx::~CFX_UnicodeEncodingEx() {}
39 
GlyphFromCharCode(uint32_t charcode)40 uint32_t CFX_UnicodeEncodingEx::GlyphFromCharCode(uint32_t charcode) {
41   FXFT_Face face = m_pFont->GetFace();
42   FT_UInt nIndex = FXFT_Get_Char_Index(face, charcode);
43   if (nIndex > 0)
44     return nIndex;
45   int nmaps = FXFT_Get_Face_CharmapCount(face);
46   int m = 0;
47   while (m < nmaps) {
48     uint32_t nEncodingID =
49         FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[m++]);
50     if (m_nEncodingID == nEncodingID)
51       continue;
52     int error = FXFT_Select_Charmap(face, nEncodingID);
53     if (error)
54       continue;
55     nIndex = FXFT_Get_Char_Index(face, charcode);
56     if (nIndex > 0) {
57       m_nEncodingID = nEncodingID;
58       return nIndex;
59     }
60   }
61   FXFT_Select_Charmap(face, m_nEncodingID);
62   return 0;
63 }
64 
CharCodeFromUnicode(FX_WCHAR Unicode) const65 uint32_t CFX_UnicodeEncodingEx::CharCodeFromUnicode(FX_WCHAR Unicode) const {
66   if (m_nEncodingID == FXFM_ENCODING_UNICODE ||
67       m_nEncodingID == FXFM_ENCODING_MS_SYMBOL) {
68     return Unicode;
69   }
70   FXFT_Face face = m_pFont->GetFace();
71   int nmaps = FXFT_Get_Face_CharmapCount(face);
72   for (int i = 0; i < nmaps; i++) {
73     int nEncodingID =
74         FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[i]);
75     if (nEncodingID == FXFM_ENCODING_UNICODE ||
76         nEncodingID == FXFM_ENCODING_MS_SYMBOL) {
77       return Unicode;
78     }
79   }
80   return CPDF_Font::kInvalidCharCode;
81 }
82 
FX_CreateFontEncodingEx(CFX_Font * pFont,uint32_t nEncodingID)83 CFX_UnicodeEncodingEx* FX_CreateFontEncodingEx(CFX_Font* pFont,
84                                                uint32_t nEncodingID) {
85   if (!pFont || !pFont->GetFace())
86     return nullptr;
87 
88   if (nEncodingID != FXFM_ENCODING_NONE)
89     return FXFM_CreateFontEncoding(pFont, nEncodingID);
90 
91   for (size_t i = 0; i < FX_ArraySize(g_EncodingID); ++i) {
92     CFX_UnicodeEncodingEx* pFontEncoding =
93         FXFM_CreateFontEncoding(pFont, g_EncodingID[i]);
94     if (pFontEncoding)
95       return pFontEncoding;
96   }
97   return nullptr;
98 }
99