blob: d0fa5e4c74dfb4243ef2abc73fc9a85db6c87695 [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
dan sinclair61b2fc72016-03-23 19:21:44 -04009#include "core/fpdfapi/fpdf_font/include/cpdf_font.h"
10#include "core/fpdfapi/fpdf_font/include/cpdf_fontencoding.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040011#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
dsinclaircedaa552016-08-24 11:12:19 -070012#include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040013#include "core/fpdfapi/include/cpdf_modulemgr.h"
dsinclairc7a73492016-04-05 12:01:42 -070014#include "core/fpdfdoc/include/ipvt_fontmap.h"
dan sinclair89e904b2016-03-23 19:29:15 -040015#include "fpdfsdk/pdfwindow/PWL_Wnd.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016
Lei Zhang1b976642016-01-08 14:24:37 -080017namespace {
18
19const char kDefaultFontName[] = "Helvetica";
20
21const char* const g_sDEStandardFontName[] = {"Courier",
22 "Courier-Bold",
23 "Courier-BoldOblique",
24 "Courier-Oblique",
25 "Helvetica",
26 "Helvetica-Bold",
27 "Helvetica-BoldOblique",
28 "Helvetica-Oblique",
29 "Times-Roman",
30 "Times-Bold",
31 "Times-Italic",
32 "Times-BoldItalic",
33 "Symbol",
34 "ZapfDingbats"};
35
36} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070037
dsinclairb9590102016-04-27 06:38:59 -070038CPWL_FontMap::CPWL_FontMap(CFX_SystemHandler* pSystemHandler)
weili2d5b0202016-08-03 11:06:49 -070039 : m_pSystemHandler(pSystemHandler) {
Lei Zhang96660d62015-12-14 18:27:25 -080040 ASSERT(m_pSystemHandler);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041}
42
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043CPWL_FontMap::~CPWL_FontMap() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 Empty();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045}
46
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047CPDF_Document* CPWL_FontMap::GetDocument() {
48 if (!m_pPDFDoc) {
49 if (CPDF_ModuleMgr::Get()) {
dsinclaircedaa552016-08-24 11:12:19 -070050 m_pPDFDoc.reset(new CPDF_Document(std::unique_ptr<CPDF_Parser>()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 m_pPDFDoc->CreateNewDoc();
52 }
53 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070054
weili2d5b0202016-08-03 11:06:49 -070055 return m_pPDFDoc.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056}
57
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058CPDF_Font* CPWL_FontMap::GetPDFFont(int32_t nFontIndex) {
59 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
60 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
61 return pData->pFont;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070062 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064
thestig1cd352e2016-06-07 17:53:06 -070065 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066}
67
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068CFX_ByteString CPWL_FontMap::GetPDFFontAlias(int32_t nFontIndex) {
69 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
70 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
71 return pData->sFontName;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070072 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076}
77
Tom Sepez62a70f92016-03-21 15:00:20 -070078FX_BOOL CPWL_FontMap::KnowWord(int32_t nFontIndex, uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
80 if (m_aData.GetAt(nFontIndex)) {
81 return CharCodeFromUnicode(nFontIndex, word) >= 0;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070082 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086}
87
Tom Sepez62a70f92016-03-21 15:00:20 -070088int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 int32_t nCharset,
90 int32_t nFontIndex) {
91 if (nFontIndex > 0) {
92 if (KnowWord(nFontIndex, word))
93 return nFontIndex;
94 } else {
95 if (const CPWL_FontMap_Data* pData = GetFontMapData(0)) {
96 if (nCharset == DEFAULT_CHARSET || pData->nCharset == SYMBOL_CHARSET ||
97 nCharset == pData->nCharset) {
98 if (KnowWord(0, word))
99 return 0;
100 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700101 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 int32_t nNewFontIndex =
105 GetFontIndex(GetNativeFontName(nCharset), nCharset, TRUE);
106 if (nNewFontIndex >= 0) {
107 if (KnowWord(nNewFontIndex, word))
108 return nNewFontIndex;
109 }
110 nNewFontIndex = GetFontIndex("Arial Unicode MS", DEFAULT_CHARSET, FALSE);
111 if (nNewFontIndex >= 0) {
112 if (KnowWord(nNewFontIndex, word))
113 return nNewFontIndex;
114 }
115 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116}
117
Tom Sepez62a70f92016-03-21 15:00:20 -0700118int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) {
thestig8ea3f512016-06-27 11:55:24 -0700119 CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex);
120 if (!pData)
121 return -1;
122
123 if (!pData->pFont)
124 return -1;
125
126 if (pData->pFont->IsUnicodeCompatible())
127 return pData->pFont->CharCodeFromUnicode(word);
128
129 return word < 0xFF ? word : -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700130}
131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132CFX_ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset) {
133 // searching native font is slow, so we must save time
134 for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++) {
135 if (CPWL_FontMap_Native* pData = m_aNativeFont.GetAt(i)) {
136 if (pData->nCharset == nCharset)
137 return pData->sFontName;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700138 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141 CFX_ByteString sNew = GetNativeFont(nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143 if (!sNew.IsEmpty()) {
144 CPWL_FontMap_Native* pNewData = new CPWL_FontMap_Native;
145 pNewData->nCharset = nCharset;
146 pNewData->sFontName = sNew;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700147
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148 m_aNativeFont.Add(pNewData);
149 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700150
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 return sNew;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152}
153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154void CPWL_FontMap::Empty() {
155 {
156 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++)
157 delete m_aData.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700158
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 m_aData.RemoveAll();
160 }
161 {
162 for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++)
163 delete m_aNativeFont.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700164
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 m_aNativeFont.RemoveAll();
166 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700167}
168
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800169void CPWL_FontMap::Initialize() {
Lei Zhang1b976642016-01-08 14:24:37 -0800170 GetFontIndex(kDefaultFontName, ANSI_CHARSET, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171}
Lei Zhang60f507b2015-06-13 00:41:00 -0700172
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173FX_BOOL CPWL_FontMap::IsStandardFont(const CFX_ByteString& sFontName) {
Wei Li89409932016-03-28 10:33:33 -0700174 for (size_t i = 0; i < FX_ArraySize(g_sDEStandardFontName); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 if (sFontName == g_sDEStandardFontName[i])
176 return TRUE;
177 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180}
181
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182int32_t CPWL_FontMap::FindFont(const CFX_ByteString& sFontName,
183 int32_t nCharset) {
184 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) {
185 if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) {
186 if (nCharset == DEFAULT_CHARSET || nCharset == pData->nCharset) {
187 if (sFontName.IsEmpty() || pData->sFontName == sFontName)
188 return i;
189 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700190 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 return -1;
194}
195
196int32_t CPWL_FontMap::GetFontIndex(const CFX_ByteString& sFontName,
197 int32_t nCharset,
198 FX_BOOL bFind) {
199 int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset);
200 if (nFontIndex >= 0)
201 return nFontIndex;
202
203 CFX_ByteString sAlias;
thestig1cd352e2016-06-07 17:53:06 -0700204 CPDF_Font* pFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 if (bFind)
206 pFont = FindFontSameCharset(sAlias, nCharset);
207
208 if (!pFont) {
209 CFX_ByteString sTemp = sFontName;
210 pFont = AddFontToDocument(GetDocument(), sTemp, nCharset);
211 sAlias = EncodeFontAlias(sTemp, nCharset);
212 }
213 AddedFont(pFont, sAlias);
214 return AddFontData(pFont, sAlias, nCharset);
215}
216
Tom Sepez62a70f92016-03-21 15:00:20 -0700217int32_t CPWL_FontMap::GetPWLFontIndex(uint16_t word, int32_t nCharset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 int32_t nFind = -1;
219
220 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) {
221 if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) {
222 if (pData->nCharset == nCharset) {
223 nFind = i;
224 break;
225 }
226 }
227 }
228
229 CPDF_Font* pNewFont = GetPDFFont(nFind);
230
231 if (!pNewFont)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700232 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 CFX_ByteString sAlias = EncodeFontAlias("Arial_Chrome", nCharset);
235 AddedFont(pNewFont, sAlias);
236
237 return AddFontData(pNewFont, sAlias, nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700238}
239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias,
241 int32_t nCharset) {
thestig1cd352e2016-06-07 17:53:06 -0700242 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700243}
244
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont,
246 const CFX_ByteString& sFontAlias,
247 int32_t nCharset) {
248 CPWL_FontMap_Data* pNewData = new CPWL_FontMap_Data;
249 pNewData->pFont = pFont;
250 pNewData->sFontName = sFontAlias;
251 pNewData->nCharset = nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 m_aData.Add(pNewData);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700254
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255 return m_aData.GetSize() - 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256}
257
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258void CPWL_FontMap::AddedFont(CPDF_Font* pFont,
259 const CFX_ByteString& sFontAlias) {}
260
261CFX_ByteString CPWL_FontMap::GetFontName(int32_t nFontIndex) {
262 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
263 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
264 return pData->sFontName;
265 }
266 }
267
268 return "";
269}
270
271CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) {
272 if (nCharset == DEFAULT_CHARSET)
273 nCharset = GetNativeCharset();
274
275 CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset);
dsinclair0e3e8902016-08-24 11:39:24 -0700276 if (m_pSystemHandler->FindNativeTrueTypeFont(sFontName))
277 return sFontName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278
dsinclair0e3e8902016-08-24 11:39:24 -0700279 sFontName.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 return sFontName;
281}
282
283CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc,
284 CFX_ByteString& sFontName,
285 uint8_t nCharset) {
286 if (IsStandardFont(sFontName))
287 return AddStandardFont(pDoc, sFontName);
288
289 return AddSystemFont(pDoc, sFontName, nCharset);
290}
291
292CPDF_Font* CPWL_FontMap::AddStandardFont(CPDF_Document* pDoc,
293 CFX_ByteString& sFontName) {
294 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700295 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296
thestig1cd352e2016-06-07 17:53:06 -0700297 CPDF_Font* pFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800299 if (sFontName == "ZapfDingbats") {
thestig1cd352e2016-06-07 17:53:06 -0700300 pFont = pDoc->AddStandardFont(sFontName.c_str(), nullptr);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800301 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302 CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI);
tsepezb4c9f3f2016-04-13 15:41:21 -0700303 pFont = pDoc->AddStandardFont(sFontName.c_str(), &fe);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 }
305
306 return pFont;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700307}
308
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309CPDF_Font* CPWL_FontMap::AddSystemFont(CPDF_Document* pDoc,
310 CFX_ByteString& sFontName,
311 uint8_t nCharset) {
312 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700313 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314
315 if (sFontName.IsEmpty())
316 sFontName = GetNativeFont(nCharset);
317 if (nCharset == DEFAULT_CHARSET)
318 nCharset = GetNativeCharset();
319
dsinclair0e3e8902016-08-24 11:39:24 -0700320 return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName,
321 nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700322}
323
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName,
325 int32_t nCharset) {
326 CFX_ByteString sPostfix;
327 sPostfix.Format("_%02X", nCharset);
328 return EncodeFontAlias(sFontName) + sPostfix;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700329}
330
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) {
332 CFX_ByteString sRet = sFontName;
333 sRet.Remove(' ');
334 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700335}
336
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337int32_t CPWL_FontMap::GetFontMapCount() const {
338 return m_aData.GetSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700339}
340
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const {
342 if (nIndex >= 0 && nIndex < m_aData.GetSize()) {
343 return m_aData.GetAt(nIndex);
344 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700345
thestig1cd352e2016-06-07 17:53:06 -0700346 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700347}
348
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349int32_t CPWL_FontMap::GetNativeCharset() {
350 uint8_t nCharset = ANSI_CHARSET;
351 int32_t iCodePage = FXSYS_GetACP();
352 switch (iCodePage) {
353 case 932: // Japan
354 nCharset = SHIFTJIS_CHARSET;
355 break;
356 case 936: // Chinese (PRC, Singapore)
357 nCharset = GB2312_CHARSET;
358 break;
359 case 950: // Chinese (Taiwan; Hong Kong SAR, PRC)
360 nCharset = GB2312_CHARSET;
361 break;
362 case 1252: // Windows 3.1 Latin 1 (US, Western Europe)
363 nCharset = ANSI_CHARSET;
364 break;
365 case 874: // Thai
366 nCharset = THAI_CHARSET;
367 break;
368 case 949: // Korean
369 nCharset = HANGUL_CHARSET;
370 break;
371 case 1200: // Unicode (BMP of ISO 10646)
372 nCharset = ANSI_CHARSET;
373 break;
374 case 1250: // Windows 3.1 Eastern European
375 nCharset = EASTEUROPE_CHARSET;
376 break;
377 case 1251: // Windows 3.1 Cyrillic
378 nCharset = RUSSIAN_CHARSET;
379 break;
380 case 1253: // Windows 3.1 Greek
381 nCharset = GREEK_CHARSET;
382 break;
383 case 1254: // Windows 3.1 Turkish
384 nCharset = TURKISH_CHARSET;
385 break;
386 case 1255: // Hebrew
387 nCharset = HEBREW_CHARSET;
388 break;
389 case 1256: // Arabic
390 nCharset = ARABIC_CHARSET;
391 break;
392 case 1257: // Baltic
393 nCharset = BALTIC_CHARSET;
394 break;
395 case 1258: // Vietnamese
396 nCharset = VIETNAMESE_CHARSET;
397 break;
398 case 1361: // Korean(Johab)
399 nCharset = JOHAB_CHARSET;
400 break;
401 }
402 return nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700403}
404
weili2d5b0202016-08-03 11:06:49 -0700405const FPDF_CharsetFontMap CPWL_FontMap::defaultTTFMap[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 {ANSI_CHARSET, "Helvetica"}, {GB2312_CHARSET, "SimSun"},
407 {CHINESEBIG5_CHARSET, "MingLiU"}, {SHIFTJIS_CHARSET, "MS Gothic"},
408 {HANGUL_CHARSET, "Batang"}, {RUSSIAN_CHARSET, "Arial"},
409#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \
410 _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
411 {EASTEUROPE_CHARSET, "Arial"},
Bo Xudbd4c062014-05-29 11:32:56 -0700412#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413 {EASTEUROPE_CHARSET, "Tahoma"},
Bo Xudbd4c062014-05-29 11:32:56 -0700414#endif
thestig1cd352e2016-06-07 17:53:06 -0700415 {ARABIC_CHARSET, "Arial"}, {-1, nullptr}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700416
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700417CFX_ByteString CPWL_FontMap::GetDefaultFontByCharset(int32_t nCharset) {
418 int i = 0;
419 while (defaultTTFMap[i].charset != -1) {
420 if (nCharset == defaultTTFMap[i].charset)
421 return defaultTTFMap[i].fontname;
422 ++i;
423 }
424 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700425}
426
Tom Sepez62a70f92016-03-21 15:00:20 -0700427int32_t CPWL_FontMap::CharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428 // to avoid CJK Font to show ASCII
429 if (word < 0x7F)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700430 return ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 // follow the old charset
432 if (nOldCharset != DEFAULT_CHARSET)
433 return nOldCharset;
434
435 // find new charset
436 if ((word >= 0x4E00 && word <= 0x9FA5) ||
437 (word >= 0xE7C7 && word <= 0xE7F3) ||
438 (word >= 0x3000 && word <= 0x303F) ||
439 (word >= 0x2000 && word <= 0x206F)) {
440 return GB2312_CHARSET;
441 }
442
443 if (((word >= 0x3040) && (word <= 0x309F)) ||
444 ((word >= 0x30A0) && (word <= 0x30FF)) ||
445 ((word >= 0x31F0) && (word <= 0x31FF)) ||
446 ((word >= 0xFF00) && (word <= 0xFFEF))) {
447 return SHIFTJIS_CHARSET;
448 }
449
450 if (((word >= 0xAC00) && (word <= 0xD7AF)) ||
451 ((word >= 0x1100) && (word <= 0x11FF)) ||
452 ((word >= 0x3130) && (word <= 0x318F))) {
453 return HANGUL_CHARSET;
454 }
455
456 if (word >= 0x0E00 && word <= 0x0E7F)
457 return THAI_CHARSET;
458
459 if ((word >= 0x0370 && word <= 0x03FF) || (word >= 0x1F00 && word <= 0x1FFF))
460 return GREEK_CHARSET;
461
462 if ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC))
463 return ARABIC_CHARSET;
464
465 if (word >= 0x0590 && word <= 0x05FF)
466 return HEBREW_CHARSET;
467
468 if (word >= 0x0400 && word <= 0x04FF)
469 return RUSSIAN_CHARSET;
470
471 if (word >= 0x0100 && word <= 0x024F)
472 return EASTEUROPE_CHARSET;
473
474 if (word >= 0x1E00 && word <= 0x1EFF)
475 return VIETNAMESE_CHARSET;
476
477 return ANSI_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700478}