blob: fadea905d462a8b24de54507aaa4e337d34a8190 [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
Lei Zhangbde53d22015-11-12 22:21:30 -08007#include "fpdfsdk/include/pdfwindow/PDFWindow.h"
8#include "fpdfsdk/include/pdfwindow/PWL_FontMap.h"
9#include "fpdfsdk/include/pdfwindow/PWL_Wnd.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070010
Nico Weber9d8ec5a2015-08-04 13:00:21 -070011#define DEFAULT_FONT_NAME "Helvetica"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070012
13/* ------------------------------ CPWL_FontMap ------------------------------ */
14
Nico Weber9d8ec5a2015-08-04 13:00:21 -070015CPWL_FontMap::CPWL_FontMap(IFX_SystemHandler* pSystemHandler)
16 : m_pPDFDoc(NULL), m_pSystemHandler(pSystemHandler) {
Lei Zhang96660d62015-12-14 18:27:25 -080017 ASSERT(m_pSystemHandler);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018}
19
Nico Weber9d8ec5a2015-08-04 13:00:21 -070020CPWL_FontMap::~CPWL_FontMap() {
21 delete m_pPDFDoc;
22 m_pPDFDoc = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 Empty();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025}
26
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027void CPWL_FontMap::SetSystemHandler(IFX_SystemHandler* pSystemHandler) {
28 m_pSystemHandler = pSystemHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029}
30
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031CPDF_Document* CPWL_FontMap::GetDocument() {
32 if (!m_pPDFDoc) {
33 if (CPDF_ModuleMgr::Get()) {
Tom Sepezae51c812015-08-05 12:34:06 -070034 m_pPDFDoc = new CPDF_Document;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035 m_pPDFDoc->CreateNewDoc();
36 }
37 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 return m_pPDFDoc;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040}
41
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042CPDF_Font* CPWL_FontMap::GetPDFFont(int32_t nFontIndex) {
43 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
44 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
45 return pData->pFont;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070046 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050}
51
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052CFX_ByteString CPWL_FontMap::GetPDFFontAlias(int32_t nFontIndex) {
53 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
54 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
55 return pData->sFontName;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070056 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070058
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060}
61
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062FX_BOOL CPWL_FontMap::KnowWord(int32_t nFontIndex, FX_WORD word) {
63 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
64 if (m_aData.GetAt(nFontIndex)) {
65 return CharCodeFromUnicode(nFontIndex, word) >= 0;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070066 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070}
71
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072int32_t CPWL_FontMap::GetWordFontIndex(FX_WORD word,
73 int32_t nCharset,
74 int32_t nFontIndex) {
75 if (nFontIndex > 0) {
76 if (KnowWord(nFontIndex, word))
77 return nFontIndex;
78 } else {
79 if (const CPWL_FontMap_Data* pData = GetFontMapData(0)) {
80 if (nCharset == DEFAULT_CHARSET || pData->nCharset == SYMBOL_CHARSET ||
81 nCharset == pData->nCharset) {
82 if (KnowWord(0, word))
83 return 0;
84 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -070085 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 int32_t nNewFontIndex =
89 GetFontIndex(GetNativeFontName(nCharset), nCharset, TRUE);
90 if (nNewFontIndex >= 0) {
91 if (KnowWord(nNewFontIndex, word))
92 return nNewFontIndex;
93 }
94 nNewFontIndex = GetFontIndex("Arial Unicode MS", DEFAULT_CHARSET, FALSE);
95 if (nNewFontIndex >= 0) {
96 if (KnowWord(nNewFontIndex, word))
97 return nNewFontIndex;
98 }
99 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100}
101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, FX_WORD word) {
103 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
104 if (pData->pFont) {
105 if (pData->pFont->IsUnicodeCompatible()) {
106 int nCharCode = pData->pFont->CharCodeFromUnicode(word);
107 pData->pFont->GlyphFromCharCode(nCharCode);
108 return nCharCode;
109 }
110 if (word < 0xFF)
111 return word;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700112 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 }
114 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115}
116
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117CFX_ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset) {
118 // searching native font is slow, so we must save time
119 for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++) {
120 if (CPWL_FontMap_Native* pData = m_aNativeFont.GetAt(i)) {
121 if (pData->nCharset == nCharset)
122 return pData->sFontName;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700123 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 CFX_ByteString sNew = GetNativeFont(nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700127
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 if (!sNew.IsEmpty()) {
129 CPWL_FontMap_Native* pNewData = new CPWL_FontMap_Native;
130 pNewData->nCharset = nCharset;
131 pNewData->sFontName = sNew;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 m_aNativeFont.Add(pNewData);
134 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 return sNew;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137}
138
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139void CPWL_FontMap::Empty() {
140 {
141 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++)
142 delete m_aData.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700143
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 m_aData.RemoveAll();
145 }
146 {
147 for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++)
148 delete m_aNativeFont.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700149
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 m_aNativeFont.RemoveAll();
151 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152}
153
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800154void CPWL_FontMap::Initialize() {
155 GetFontIndex(DEFAULT_FONT_NAME, ANSI_CHARSET, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700156}
Lei Zhang60f507b2015-06-13 00:41:00 -0700157
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700158/*
159List of currently supported standard fonts:
160Courier, Courier-Bold, Courier-BoldOblique, Courier-Oblique
161Helvetica, Helvetica-Bold, Helvetica-BoldOblique, Helvetica-Oblique
162Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic
163Symbol, ZapfDingbats
164*/
165
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166const char* g_sDEStandardFontName[] = {"Courier",
167 "Courier-Bold",
168 "Courier-BoldOblique",
169 "Courier-Oblique",
170 "Helvetica",
171 "Helvetica-Bold",
172 "Helvetica-BoldOblique",
173 "Helvetica-Oblique",
174 "Times-Roman",
175 "Times-Bold",
176 "Times-Italic",
177 "Times-BoldItalic",
178 "Symbol",
179 "ZapfDingbats"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181FX_BOOL CPWL_FontMap::IsStandardFont(const CFX_ByteString& sFontName) {
182 for (int32_t i = 0; i < 14; i++) {
183 if (sFontName == g_sDEStandardFontName[i])
184 return TRUE;
185 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700186
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700188}
189
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190int32_t CPWL_FontMap::FindFont(const CFX_ByteString& sFontName,
191 int32_t nCharset) {
192 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) {
193 if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) {
194 if (nCharset == DEFAULT_CHARSET || nCharset == pData->nCharset) {
195 if (sFontName.IsEmpty() || pData->sFontName == sFontName)
196 return i;
197 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700198 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 return -1;
202}
203
204int32_t CPWL_FontMap::GetFontIndex(const CFX_ByteString& sFontName,
205 int32_t nCharset,
206 FX_BOOL bFind) {
207 int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset);
208 if (nFontIndex >= 0)
209 return nFontIndex;
210
211 CFX_ByteString sAlias;
212 CPDF_Font* pFont = NULL;
213 if (bFind)
214 pFont = FindFontSameCharset(sAlias, nCharset);
215
216 if (!pFont) {
217 CFX_ByteString sTemp = sFontName;
218 pFont = AddFontToDocument(GetDocument(), sTemp, nCharset);
219 sAlias = EncodeFontAlias(sTemp, nCharset);
220 }
221 AddedFont(pFont, sAlias);
222 return AddFontData(pFont, sAlias, nCharset);
223}
224
225int32_t CPWL_FontMap::GetPWLFontIndex(FX_WORD word, int32_t nCharset) {
226 int32_t nFind = -1;
227
228 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) {
229 if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) {
230 if (pData->nCharset == nCharset) {
231 nFind = i;
232 break;
233 }
234 }
235 }
236
237 CPDF_Font* pNewFont = GetPDFFont(nFind);
238
239 if (!pNewFont)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700240 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241
242 /*
243 if (CPDF_Font* pFont = GetPDFFont(nFind))
244 {
245 PWLFont.AddWordToFontDict(pFontDict, word);
246 }
247 */
248
249 CFX_ByteString sAlias = EncodeFontAlias("Arial_Chrome", nCharset);
250 AddedFont(pNewFont, sAlias);
251
252 return AddFontData(pNewFont, sAlias, nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700253}
254
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias,
256 int32_t nCharset) {
257 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258}
259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont,
261 const CFX_ByteString& sFontAlias,
262 int32_t nCharset) {
263 CPWL_FontMap_Data* pNewData = new CPWL_FontMap_Data;
264 pNewData->pFont = pFont;
265 pNewData->sFontName = sFontAlias;
266 pNewData->nCharset = nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700267
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 m_aData.Add(pNewData);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700269
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 return m_aData.GetSize() - 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271}
272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273void CPWL_FontMap::AddedFont(CPDF_Font* pFont,
274 const CFX_ByteString& sFontAlias) {}
275
276CFX_ByteString CPWL_FontMap::GetFontName(int32_t nFontIndex) {
277 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
278 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
279 return pData->sFontName;
280 }
281 }
282
283 return "";
284}
285
286CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) {
287 if (nCharset == DEFAULT_CHARSET)
288 nCharset = GetNativeCharset();
289
290 CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset);
291 if (m_pSystemHandler) {
292 if (m_pSystemHandler->FindNativeTrueTypeFont(nCharset, sFontName))
293 return sFontName;
294
295 sFontName = m_pSystemHandler->GetNativeTrueTypeFont(nCharset);
296 }
297 return sFontName;
298}
299
300CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc,
301 CFX_ByteString& sFontName,
302 uint8_t nCharset) {
303 if (IsStandardFont(sFontName))
304 return AddStandardFont(pDoc, sFontName);
305
306 return AddSystemFont(pDoc, sFontName, nCharset);
307}
308
309CPDF_Font* CPWL_FontMap::AddStandardFont(CPDF_Document* pDoc,
310 CFX_ByteString& sFontName) {
311 if (!pDoc)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700312 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313
314 CPDF_Font* pFont = NULL;
315
316 if (sFontName == "ZapfDingbats")
317 pFont = pDoc->AddStandardFont(sFontName, NULL);
318 else {
319 CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI);
320 pFont = pDoc->AddStandardFont(sFontName, &fe);
321 }
322
323 return pFont;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700324}
325
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326CPDF_Font* CPWL_FontMap::AddSystemFont(CPDF_Document* pDoc,
327 CFX_ByteString& sFontName,
328 uint8_t nCharset) {
329 if (!pDoc)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700330 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331
332 if (sFontName.IsEmpty())
333 sFontName = GetNativeFont(nCharset);
334 if (nCharset == DEFAULT_CHARSET)
335 nCharset = GetNativeCharset();
336
337 if (m_pSystemHandler)
338 return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName,
339 nCharset);
340
341 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700342}
343
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName,
345 int32_t nCharset) {
346 CFX_ByteString sPostfix;
347 sPostfix.Format("_%02X", nCharset);
348 return EncodeFontAlias(sFontName) + sPostfix;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700349}
350
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) {
352 CFX_ByteString sRet = sFontName;
353 sRet.Remove(' ');
354 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700355}
356
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357int32_t CPWL_FontMap::GetFontMapCount() const {
358 return m_aData.GetSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700359}
360
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const {
362 if (nIndex >= 0 && nIndex < m_aData.GetSize()) {
363 return m_aData.GetAt(nIndex);
364 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700365
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700367}
368
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369int32_t CPWL_FontMap::GetNativeCharset() {
370 uint8_t nCharset = ANSI_CHARSET;
371 int32_t iCodePage = FXSYS_GetACP();
372 switch (iCodePage) {
373 case 932: // Japan
374 nCharset = SHIFTJIS_CHARSET;
375 break;
376 case 936: // Chinese (PRC, Singapore)
377 nCharset = GB2312_CHARSET;
378 break;
379 case 950: // Chinese (Taiwan; Hong Kong SAR, PRC)
380 nCharset = GB2312_CHARSET;
381 break;
382 case 1252: // Windows 3.1 Latin 1 (US, Western Europe)
383 nCharset = ANSI_CHARSET;
384 break;
385 case 874: // Thai
386 nCharset = THAI_CHARSET;
387 break;
388 case 949: // Korean
389 nCharset = HANGUL_CHARSET;
390 break;
391 case 1200: // Unicode (BMP of ISO 10646)
392 nCharset = ANSI_CHARSET;
393 break;
394 case 1250: // Windows 3.1 Eastern European
395 nCharset = EASTEUROPE_CHARSET;
396 break;
397 case 1251: // Windows 3.1 Cyrillic
398 nCharset = RUSSIAN_CHARSET;
399 break;
400 case 1253: // Windows 3.1 Greek
401 nCharset = GREEK_CHARSET;
402 break;
403 case 1254: // Windows 3.1 Turkish
404 nCharset = TURKISH_CHARSET;
405 break;
406 case 1255: // Hebrew
407 nCharset = HEBREW_CHARSET;
408 break;
409 case 1256: // Arabic
410 nCharset = ARABIC_CHARSET;
411 break;
412 case 1257: // Baltic
413 nCharset = BALTIC_CHARSET;
414 break;
415 case 1258: // Vietnamese
416 nCharset = VIETNAMESE_CHARSET;
417 break;
418 case 1361: // Korean(Johab)
419 nCharset = JOHAB_CHARSET;
420 break;
421 }
422 return nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700423}
424
425const CPWL_FontMap::CharsetFontMap CPWL_FontMap::defaultTTFMap[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 {ANSI_CHARSET, "Helvetica"}, {GB2312_CHARSET, "SimSun"},
427 {CHINESEBIG5_CHARSET, "MingLiU"}, {SHIFTJIS_CHARSET, "MS Gothic"},
428 {HANGUL_CHARSET, "Batang"}, {RUSSIAN_CHARSET, "Arial"},
429#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \
430 _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
431 {EASTEUROPE_CHARSET, "Arial"},
Bo Xudbd4c062014-05-29 11:32:56 -0700432#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 {EASTEUROPE_CHARSET, "Tahoma"},
Bo Xudbd4c062014-05-29 11:32:56 -0700434#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 {ARABIC_CHARSET, "Arial"}, {-1, NULL}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700436
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437CFX_ByteString CPWL_FontMap::GetDefaultFontByCharset(int32_t nCharset) {
438 int i = 0;
439 while (defaultTTFMap[i].charset != -1) {
440 if (nCharset == defaultTTFMap[i].charset)
441 return defaultTTFMap[i].fontname;
442 ++i;
443 }
444 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700445}
446
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447int32_t CPWL_FontMap::CharSetFromUnicode(FX_WORD word, int32_t nOldCharset) {
448 if (m_pSystemHandler && (-1 != m_pSystemHandler->GetCharSet()))
449 return m_pSystemHandler->GetCharSet();
450 // to avoid CJK Font to show ASCII
451 if (word < 0x7F)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700452 return ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 // follow the old charset
454 if (nOldCharset != DEFAULT_CHARSET)
455 return nOldCharset;
456
457 // find new charset
458 if ((word >= 0x4E00 && word <= 0x9FA5) ||
459 (word >= 0xE7C7 && word <= 0xE7F3) ||
460 (word >= 0x3000 && word <= 0x303F) ||
461 (word >= 0x2000 && word <= 0x206F)) {
462 return GB2312_CHARSET;
463 }
464
465 if (((word >= 0x3040) && (word <= 0x309F)) ||
466 ((word >= 0x30A0) && (word <= 0x30FF)) ||
467 ((word >= 0x31F0) && (word <= 0x31FF)) ||
468 ((word >= 0xFF00) && (word <= 0xFFEF))) {
469 return SHIFTJIS_CHARSET;
470 }
471
472 if (((word >= 0xAC00) && (word <= 0xD7AF)) ||
473 ((word >= 0x1100) && (word <= 0x11FF)) ||
474 ((word >= 0x3130) && (word <= 0x318F))) {
475 return HANGUL_CHARSET;
476 }
477
478 if (word >= 0x0E00 && word <= 0x0E7F)
479 return THAI_CHARSET;
480
481 if ((word >= 0x0370 && word <= 0x03FF) || (word >= 0x1F00 && word <= 0x1FFF))
482 return GREEK_CHARSET;
483
484 if ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC))
485 return ARABIC_CHARSET;
486
487 if (word >= 0x0590 && word <= 0x05FF)
488 return HEBREW_CHARSET;
489
490 if (word >= 0x0400 && word <= 0x04FF)
491 return RUSSIAN_CHARSET;
492
493 if (word >= 0x0100 && word <= 0x024F)
494 return EASTEUROPE_CHARSET;
495
496 if (word >= 0x1E00 && word <= 0x1EFF)
497 return VIETNAMESE_CHARSET;
498
499 return ANSI_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700500}
501
502/* ------------------------ CPWL_DocFontMap ------------------------ */
503
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700504CPWL_DocFontMap::CPWL_DocFontMap(IFX_SystemHandler* pSystemHandler,
505 CPDF_Document* pAttachedDoc)
506 : CPWL_FontMap(pSystemHandler), m_pAttachedDoc(pAttachedDoc) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700507
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508CPWL_DocFontMap::~CPWL_DocFontMap() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700509
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510CPDF_Document* CPWL_DocFontMap::GetDocument() {
511 return m_pAttachedDoc;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700512}