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 <memory>
10
11 #include "core/fxge/cfx_font.h"
12 #include "core/fxge/fx_font.h"
13 #include "core/fxge/fx_freetype.h"
14 #include "third_party/base/ptr_util.h"
15
16 #define FXFM_ENC_TAG(a, b, c, d) \
17 (((uint32_t)(a) << 24) | ((uint32_t)(b) << 16) | ((uint32_t)(c) << 8) | \
18 (uint32_t)(d))
19 #define FXFM_ENCODING_MS_SYMBOL FXFM_ENC_TAG('s', 'y', 'm', 'b')
20 #define FXFM_ENCODING_UNICODE FXFM_ENC_TAG('u', 'n', 'i', 'c')
21 #define FXFM_ENCODING_MS_SJIS FXFM_ENC_TAG('s', 'j', 'i', 's')
22 #define FXFM_ENCODING_MS_GB2312 FXFM_ENC_TAG('g', 'b', ' ', ' ')
23 #define FXFM_ENCODING_MS_BIG5 FXFM_ENC_TAG('b', 'i', 'g', '5')
24 #define FXFM_ENCODING_MS_WANSUNG FXFM_ENC_TAG('w', 'a', 'n', 's')
25 #define FXFM_ENCODING_MS_JOHAB FXFM_ENC_TAG('j', 'o', 'h', 'a')
26 #define FXFM_ENCODING_ADOBE_STANDARD FXFM_ENC_TAG('A', 'D', 'O', 'B')
27 #define FXFM_ENCODING_ADOBE_EXPERT FXFM_ENC_TAG('A', 'D', 'B', 'E')
28 #define FXFM_ENCODING_ADOBE_CUSTOM FXFM_ENC_TAG('A', 'D', 'B', 'C')
29 #define FXFM_ENCODING_ADOBE_LATIN_1 FXFM_ENC_TAG('l', 'a', 't', '1')
30 #define FXFM_ENCODING_OLD_LATIN_2 FXFM_ENC_TAG('l', 'a', 't', '2')
31 #define FXFM_ENCODING_APPLE_ROMAN FXFM_ENC_TAG('a', 'r', 'm', 'n')
32
33 namespace {
34
35 const uint32_t g_EncodingID[] = {
36 FXFM_ENCODING_MS_SYMBOL, FXFM_ENCODING_UNICODE,
37 FXFM_ENCODING_MS_SJIS, FXFM_ENCODING_MS_GB2312,
38 FXFM_ENCODING_MS_BIG5, FXFM_ENCODING_MS_WANSUNG,
39 FXFM_ENCODING_MS_JOHAB, FXFM_ENCODING_ADOBE_STANDARD,
40 FXFM_ENCODING_ADOBE_EXPERT, FXFM_ENCODING_ADOBE_CUSTOM,
41 FXFM_ENCODING_ADOBE_LATIN_1, FXFM_ENCODING_OLD_LATIN_2,
42 FXFM_ENCODING_APPLE_ROMAN,
43 };
44
FXFM_CreateFontEncoding(CFX_Font * pFont,uint32_t nEncodingID)45 std::unique_ptr<CFX_UnicodeEncodingEx> FXFM_CreateFontEncoding(
46 CFX_Font* pFont,
47 uint32_t nEncodingID) {
48 if (FXFT_Select_Charmap(pFont->GetFaceRec(), nEncodingID))
49 return nullptr;
50 return pdfium::MakeUnique<CFX_UnicodeEncodingEx>(pFont, nEncodingID);
51 }
52
53 } // namespace
54
CFX_UnicodeEncodingEx(CFX_Font * pFont,uint32_t EncodingID)55 CFX_UnicodeEncodingEx::CFX_UnicodeEncodingEx(CFX_Font* pFont,
56 uint32_t EncodingID)
57 : CFX_UnicodeEncoding(pFont), m_nEncodingID(EncodingID) {}
58
~CFX_UnicodeEncodingEx()59 CFX_UnicodeEncodingEx::~CFX_UnicodeEncodingEx() {}
60
GlyphFromCharCode(uint32_t charcode)61 uint32_t CFX_UnicodeEncodingEx::GlyphFromCharCode(uint32_t charcode) {
62 FXFT_FaceRec* face = m_pFont->GetFaceRec();
63 FT_UInt nIndex = FT_Get_Char_Index(face, charcode);
64 if (nIndex > 0)
65 return nIndex;
66 int nmaps = FXFT_Get_Face_CharmapCount(face);
67 int m = 0;
68 while (m < nmaps) {
69 uint32_t nEncodingID =
70 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[m++]);
71 if (m_nEncodingID == nEncodingID)
72 continue;
73 int error = FXFT_Select_Charmap(face, nEncodingID);
74 if (error)
75 continue;
76 nIndex = FT_Get_Char_Index(face, charcode);
77 if (nIndex > 0) {
78 m_nEncodingID = nEncodingID;
79 return nIndex;
80 }
81 }
82 FXFT_Select_Charmap(face, m_nEncodingID);
83 return 0;
84 }
85
CharCodeFromUnicode(wchar_t Unicode) const86 uint32_t CFX_UnicodeEncodingEx::CharCodeFromUnicode(wchar_t Unicode) const {
87 if (m_nEncodingID == FXFM_ENCODING_UNICODE ||
88 m_nEncodingID == FXFM_ENCODING_MS_SYMBOL) {
89 return Unicode;
90 }
91 FXFT_FaceRec* face = m_pFont->GetFaceRec();
92 int nmaps = FXFT_Get_Face_CharmapCount(face);
93 for (int i = 0; i < nmaps; i++) {
94 int nEncodingID =
95 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[i]);
96 if (nEncodingID == FXFM_ENCODING_UNICODE ||
97 nEncodingID == FXFM_ENCODING_MS_SYMBOL) {
98 return Unicode;
99 }
100 }
101 return kInvalidCharCode;
102 }
103
FX_CreateFontEncodingEx(CFX_Font * pFont)104 std::unique_ptr<CFX_UnicodeEncodingEx> FX_CreateFontEncodingEx(
105 CFX_Font* pFont) {
106 if (!pFont || !pFont->GetFaceRec())
107 return nullptr;
108
109 for (uint32_t id : g_EncodingID) {
110 auto pFontEncoding = FXFM_CreateFontEncoding(pFont, id);
111 if (pFontEncoding)
112 return pFontEncoding;
113 }
114 return nullptr;
115 }
116