blob: b2a1f2580da52fa8a092d722dbfa13785f98f808 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 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.
Lei Zhang60f507b2015-06-13 00:41:00 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
dan sinclair89e904b2016-03-23 19:29:15 -04007#include "fpdfsdk/pdfwindow/PWL_FontMap.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -08008
dsinclair39c62fd2016-09-29 12:49:17 -07009#include "core/fpdfapi/cpdf_modulemgr.h"
dsinclairbc5e6d22016-10-04 11:08:49 -070010#include "core/fpdfapi/font/cpdf_font.h"
11#include "core/fpdfapi/font/cpdf_fontencoding.h"
dsinclair488b7ad2016-10-04 11:55:50 -070012#include "core/fpdfapi/parser/cpdf_document.h"
13#include "core/fpdfapi/parser/cpdf_parser.h"
dsinclair1727aee2016-09-29 13:12:56 -070014#include "core/fpdfdoc/ipvt_fontmap.h"
dan sinclair89e904b2016-03-23 19:29:15 -040015#include "fpdfsdk/pdfwindow/PWL_Wnd.h"
tsepez36eb4bd2016-10-03 15:24:27 -070016#include "third_party/base/ptr_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017
Lei Zhang1b976642016-01-08 14:24:37 -080018namespace {
19
20const char kDefaultFontName[] = "Helvetica";
21
22const char* const g_sDEStandardFontName[] = {"Courier",
23 "Courier-Bold",
24 "Courier-BoldOblique",
25 "Courier-Oblique",
26 "Helvetica",
27 "Helvetica-Bold",
28 "Helvetica-BoldOblique",
29 "Helvetica-Oblique",
30 "Times-Roman",
31 "Times-Bold",
32 "Times-Italic",
33 "Times-BoldItalic",
34 "Symbol",
35 "ZapfDingbats"};
36
37} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038
dsinclairb9590102016-04-27 06:38:59 -070039CPWL_FontMap::CPWL_FontMap(CFX_SystemHandler* pSystemHandler)
weili2d5b0202016-08-03 11:06:49 -070040 : m_pSystemHandler(pSystemHandler) {
Lei Zhang96660d62015-12-14 18:27:25 -080041 ASSERT(m_pSystemHandler);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042}
43
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044CPWL_FontMap::~CPWL_FontMap() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 Empty();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046}
47
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048CPDF_Document* CPWL_FontMap::GetDocument() {
49 if (!m_pPDFDoc) {
50 if (CPDF_ModuleMgr::Get()) {
tsepeze5cb0b12016-10-26 15:06:11 -070051 m_pPDFDoc = pdfium::MakeUnique<CPDF_Document>(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 m_pPDFDoc->CreateNewDoc();
53 }
54 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055
weili2d5b0202016-08-03 11:06:49 -070056 return m_pPDFDoc.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057}
58
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059CPDF_Font* CPWL_FontMap::GetPDFFont(int32_t nFontIndex) {
60 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
61 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
62 return pData->pFont;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070063 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065
thestig1cd352e2016-06-07 17:53:06 -070066 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067}
68
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069CFX_ByteString CPWL_FontMap::GetPDFFontAlias(int32_t nFontIndex) {
70 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
71 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
72 return pData->sFontName;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070073 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077}
78
Tom Sepez62a70f92016-03-21 15:00:20 -070079FX_BOOL CPWL_FontMap::KnowWord(int32_t nFontIndex, uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
81 if (m_aData.GetAt(nFontIndex)) {
82 return CharCodeFromUnicode(nFontIndex, word) >= 0;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070083 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087}
88
Tom Sepez62a70f92016-03-21 15:00:20 -070089int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 int32_t nCharset,
91 int32_t nFontIndex) {
92 if (nFontIndex > 0) {
93 if (KnowWord(nFontIndex, word))
94 return nFontIndex;
95 } else {
96 if (const CPWL_FontMap_Data* pData = GetFontMapData(0)) {
npmea3c3be2016-09-19 07:24:33 -070097 if (nCharset == FXFONT_DEFAULT_CHARSET ||
98 pData->nCharset == FXFONT_SYMBOL_CHARSET ||
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 nCharset == pData->nCharset) {
100 if (KnowWord(0, word))
101 return 0;
102 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700103 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700105
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 int32_t nNewFontIndex =
107 GetFontIndex(GetNativeFontName(nCharset), nCharset, TRUE);
108 if (nNewFontIndex >= 0) {
109 if (KnowWord(nNewFontIndex, word))
110 return nNewFontIndex;
111 }
npmea3c3be2016-09-19 07:24:33 -0700112 nNewFontIndex =
113 GetFontIndex("Arial Unicode MS", FXFONT_DEFAULT_CHARSET, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 if (nNewFontIndex >= 0) {
115 if (KnowWord(nNewFontIndex, word))
116 return nNewFontIndex;
117 }
118 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700119}
120
Tom Sepez62a70f92016-03-21 15:00:20 -0700121int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) {
thestig8ea3f512016-06-27 11:55:24 -0700122 CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex);
123 if (!pData)
124 return -1;
125
126 if (!pData->pFont)
127 return -1;
128
129 if (pData->pFont->IsUnicodeCompatible())
130 return pData->pFont->CharCodeFromUnicode(word);
131
132 return word < 0xFF ? word : -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700133}
134
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135CFX_ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset) {
136 // searching native font is slow, so we must save time
137 for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++) {
138 if (CPWL_FontMap_Native* pData = m_aNativeFont.GetAt(i)) {
139 if (pData->nCharset == nCharset)
140 return pData->sFontName;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700141 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700143
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 CFX_ByteString sNew = GetNativeFont(nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700145
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 if (!sNew.IsEmpty()) {
147 CPWL_FontMap_Native* pNewData = new CPWL_FontMap_Native;
148 pNewData->nCharset = nCharset;
149 pNewData->sFontName = sNew;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700150
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 m_aNativeFont.Add(pNewData);
152 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 return sNew;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700155}
156
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157void CPWL_FontMap::Empty() {
158 {
159 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++)
160 delete m_aData.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 m_aData.RemoveAll();
163 }
164 {
165 for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++)
166 delete m_aNativeFont.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700167
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 m_aNativeFont.RemoveAll();
169 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700170}
171
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800172void CPWL_FontMap::Initialize() {
npmea3c3be2016-09-19 07:24:33 -0700173 GetFontIndex(kDefaultFontName, FXFONT_ANSI_CHARSET, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174}
Lei Zhang60f507b2015-06-13 00:41:00 -0700175
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176FX_BOOL CPWL_FontMap::IsStandardFont(const CFX_ByteString& sFontName) {
Wei Li89409932016-03-28 10:33:33 -0700177 for (size_t i = 0; i < FX_ArraySize(g_sDEStandardFontName); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 if (sFontName == g_sDEStandardFontName[i])
179 return TRUE;
180 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700181
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183}
184
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185int32_t CPWL_FontMap::FindFont(const CFX_ByteString& sFontName,
186 int32_t nCharset) {
187 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) {
188 if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) {
npmea3c3be2016-09-19 07:24:33 -0700189 if (nCharset == FXFONT_DEFAULT_CHARSET || nCharset == pData->nCharset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 if (sFontName.IsEmpty() || pData->sFontName == sFontName)
191 return i;
192 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700193 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 return -1;
197}
198
199int32_t CPWL_FontMap::GetFontIndex(const CFX_ByteString& sFontName,
200 int32_t nCharset,
201 FX_BOOL bFind) {
202 int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset);
203 if (nFontIndex >= 0)
204 return nFontIndex;
205
206 CFX_ByteString sAlias;
thestig1cd352e2016-06-07 17:53:06 -0700207 CPDF_Font* pFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 if (bFind)
209 pFont = FindFontSameCharset(sAlias, nCharset);
210
211 if (!pFont) {
212 CFX_ByteString sTemp = sFontName;
213 pFont = AddFontToDocument(GetDocument(), sTemp, nCharset);
214 sAlias = EncodeFontAlias(sTemp, nCharset);
215 }
216 AddedFont(pFont, sAlias);
217 return AddFontData(pFont, sAlias, nCharset);
218}
219
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias,
221 int32_t nCharset) {
thestig1cd352e2016-06-07 17:53:06 -0700222 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700223}
224
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont,
226 const CFX_ByteString& sFontAlias,
227 int32_t nCharset) {
228 CPWL_FontMap_Data* pNewData = new CPWL_FontMap_Data;
229 pNewData->pFont = pFont;
230 pNewData->sFontName = sFontAlias;
231 pNewData->nCharset = nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700232
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 m_aData.Add(pNewData);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700234
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 return m_aData.GetSize() - 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700236}
237
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238void CPWL_FontMap::AddedFont(CPDF_Font* pFont,
239 const CFX_ByteString& sFontAlias) {}
240
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) {
npmea3c3be2016-09-19 07:24:33 -0700242 if (nCharset == FXFONT_DEFAULT_CHARSET)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 nCharset = GetNativeCharset();
244
245 CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset);
dsinclair0e3e8902016-08-24 11:39:24 -0700246 if (m_pSystemHandler->FindNativeTrueTypeFont(sFontName))
247 return sFontName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248
dsinclair0e3e8902016-08-24 11:39:24 -0700249 sFontName.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 return sFontName;
251}
252
253CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc,
254 CFX_ByteString& sFontName,
255 uint8_t nCharset) {
256 if (IsStandardFont(sFontName))
257 return AddStandardFont(pDoc, sFontName);
258
259 return AddSystemFont(pDoc, sFontName, nCharset);
260}
261
262CPDF_Font* CPWL_FontMap::AddStandardFont(CPDF_Document* pDoc,
263 CFX_ByteString& sFontName) {
264 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700265 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266
thestig1cd352e2016-06-07 17:53:06 -0700267 CPDF_Font* pFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800269 if (sFontName == "ZapfDingbats") {
thestig1cd352e2016-06-07 17:53:06 -0700270 pFont = pDoc->AddStandardFont(sFontName.c_str(), nullptr);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800271 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI);
tsepezb4c9f3f2016-04-13 15:41:21 -0700273 pFont = pDoc->AddStandardFont(sFontName.c_str(), &fe);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 }
275
276 return pFont;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700277}
278
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279CPDF_Font* CPWL_FontMap::AddSystemFont(CPDF_Document* pDoc,
280 CFX_ByteString& sFontName,
281 uint8_t nCharset) {
282 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700283 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284
285 if (sFontName.IsEmpty())
286 sFontName = GetNativeFont(nCharset);
npmea3c3be2016-09-19 07:24:33 -0700287 if (nCharset == FXFONT_DEFAULT_CHARSET)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 nCharset = GetNativeCharset();
289
dsinclair0e3e8902016-08-24 11:39:24 -0700290 return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName,
291 nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700292}
293
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName,
295 int32_t nCharset) {
296 CFX_ByteString sPostfix;
297 sPostfix.Format("_%02X", nCharset);
298 return EncodeFontAlias(sFontName) + sPostfix;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700299}
300
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) {
302 CFX_ByteString sRet = sFontName;
303 sRet.Remove(' ');
304 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700305}
306
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const {
308 if (nIndex >= 0 && nIndex < m_aData.GetSize()) {
309 return m_aData.GetAt(nIndex);
310 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700311
thestig1cd352e2016-06-07 17:53:06 -0700312 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700313}
314
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315int32_t CPWL_FontMap::GetNativeCharset() {
npmea3c3be2016-09-19 07:24:33 -0700316 uint8_t nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 int32_t iCodePage = FXSYS_GetACP();
318 switch (iCodePage) {
319 case 932: // Japan
npmea3c3be2016-09-19 07:24:33 -0700320 nCharset = FXFONT_SHIFTJIS_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 break;
322 case 936: // Chinese (PRC, Singapore)
npmea3c3be2016-09-19 07:24:33 -0700323 nCharset = FXFONT_GB2312_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324 break;
325 case 950: // Chinese (Taiwan; Hong Kong SAR, PRC)
npmea3c3be2016-09-19 07:24:33 -0700326 nCharset = FXFONT_GB2312_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327 break;
328 case 1252: // Windows 3.1 Latin 1 (US, Western Europe)
npmea3c3be2016-09-19 07:24:33 -0700329 nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 break;
331 case 874: // Thai
npmea3c3be2016-09-19 07:24:33 -0700332 nCharset = FXFONT_THAI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 break;
334 case 949: // Korean
npmea3c3be2016-09-19 07:24:33 -0700335 nCharset = FXFONT_HANGUL_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 break;
337 case 1200: // Unicode (BMP of ISO 10646)
npmea3c3be2016-09-19 07:24:33 -0700338 nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700339 break;
340 case 1250: // Windows 3.1 Eastern European
npmea3c3be2016-09-19 07:24:33 -0700341 nCharset = FXFONT_EASTEUROPE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 break;
343 case 1251: // Windows 3.1 Cyrillic
npmea3c3be2016-09-19 07:24:33 -0700344 nCharset = FXFONT_RUSSIAN_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345 break;
346 case 1253: // Windows 3.1 Greek
npmea3c3be2016-09-19 07:24:33 -0700347 nCharset = FXFONT_GREEK_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348 break;
349 case 1254: // Windows 3.1 Turkish
npmea3c3be2016-09-19 07:24:33 -0700350 nCharset = FXFONT_TURKISH_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 break;
352 case 1255: // Hebrew
npmea3c3be2016-09-19 07:24:33 -0700353 nCharset = FXFONT_HEBREW_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 break;
355 case 1256: // Arabic
npmea3c3be2016-09-19 07:24:33 -0700356 nCharset = FXFONT_ARABIC_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357 break;
358 case 1257: // Baltic
npmea3c3be2016-09-19 07:24:33 -0700359 nCharset = FXFONT_BALTIC_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 break;
361 case 1258: // Vietnamese
npmea3c3be2016-09-19 07:24:33 -0700362 nCharset = FXFONT_VIETNAMESE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363 break;
364 case 1361: // Korean(Johab)
npmea3c3be2016-09-19 07:24:33 -0700365 nCharset = FXFONT_JOHAB_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 break;
367 }
368 return nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700369}
370
weili2d5b0202016-08-03 11:06:49 -0700371const FPDF_CharsetFontMap CPWL_FontMap::defaultTTFMap[] = {
npmea3c3be2016-09-19 07:24:33 -0700372 {FXFONT_ANSI_CHARSET, "Helvetica"},
373 {FXFONT_GB2312_CHARSET, "SimSun"},
374 {FXFONT_CHINESEBIG5_CHARSET, "MingLiU"},
375 {FXFONT_SHIFTJIS_CHARSET, "MS Gothic"},
376 {FXFONT_HANGUL_CHARSET, "Batang"},
377 {FXFONT_RUSSIAN_CHARSET, "Arial"},
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \
379 _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
npmea3c3be2016-09-19 07:24:33 -0700380 {FXFONT_EASTEUROPE_CHARSET, "Arial"},
Bo Xudbd4c062014-05-29 11:32:56 -0700381#else
npmea3c3be2016-09-19 07:24:33 -0700382 {FXFONT_EASTEUROPE_CHARSET, "Tahoma"},
Bo Xudbd4c062014-05-29 11:32:56 -0700383#endif
npmea3c3be2016-09-19 07:24:33 -0700384 {FXFONT_ARABIC_CHARSET, "Arial"},
385 {-1, nullptr}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700386
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387CFX_ByteString CPWL_FontMap::GetDefaultFontByCharset(int32_t nCharset) {
388 int i = 0;
389 while (defaultTTFMap[i].charset != -1) {
390 if (nCharset == defaultTTFMap[i].charset)
391 return defaultTTFMap[i].fontname;
392 ++i;
393 }
394 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700395}
396
Tom Sepez62a70f92016-03-21 15:00:20 -0700397int32_t CPWL_FontMap::CharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398 // to avoid CJK Font to show ASCII
399 if (word < 0x7F)
npmea3c3be2016-09-19 07:24:33 -0700400 return FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401 // follow the old charset
npmea3c3be2016-09-19 07:24:33 -0700402 if (nOldCharset != FXFONT_DEFAULT_CHARSET)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 return nOldCharset;
404
405 // find new charset
406 if ((word >= 0x4E00 && word <= 0x9FA5) ||
407 (word >= 0xE7C7 && word <= 0xE7F3) ||
408 (word >= 0x3000 && word <= 0x303F) ||
409 (word >= 0x2000 && word <= 0x206F)) {
npmea3c3be2016-09-19 07:24:33 -0700410 return FXFONT_GB2312_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 }
412
413 if (((word >= 0x3040) && (word <= 0x309F)) ||
414 ((word >= 0x30A0) && (word <= 0x30FF)) ||
415 ((word >= 0x31F0) && (word <= 0x31FF)) ||
416 ((word >= 0xFF00) && (word <= 0xFFEF))) {
npmea3c3be2016-09-19 07:24:33 -0700417 return FXFONT_SHIFTJIS_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 }
419
420 if (((word >= 0xAC00) && (word <= 0xD7AF)) ||
421 ((word >= 0x1100) && (word <= 0x11FF)) ||
422 ((word >= 0x3130) && (word <= 0x318F))) {
npmea3c3be2016-09-19 07:24:33 -0700423 return FXFONT_HANGUL_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424 }
425
426 if (word >= 0x0E00 && word <= 0x0E7F)
npmea3c3be2016-09-19 07:24:33 -0700427 return FXFONT_THAI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428
429 if ((word >= 0x0370 && word <= 0x03FF) || (word >= 0x1F00 && word <= 0x1FFF))
npmea3c3be2016-09-19 07:24:33 -0700430 return FXFONT_GREEK_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431
432 if ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC))
npmea3c3be2016-09-19 07:24:33 -0700433 return FXFONT_ARABIC_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700434
435 if (word >= 0x0590 && word <= 0x05FF)
npmea3c3be2016-09-19 07:24:33 -0700436 return FXFONT_HEBREW_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437
438 if (word >= 0x0400 && word <= 0x04FF)
npmea3c3be2016-09-19 07:24:33 -0700439 return FXFONT_RUSSIAN_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440
441 if (word >= 0x0100 && word <= 0x024F)
npmea3c3be2016-09-19 07:24:33 -0700442 return FXFONT_EASTEUROPE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443
444 if (word >= 0x1E00 && word <= 0x1EFF)
npmea3c3be2016-09-19 07:24:33 -0700445 return FXFONT_VIETNAMESE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446
npmea3c3be2016-09-19 07:24:33 -0700447 return FXFONT_ANSI_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700448}