blob: 6b0f4fe881fbe55a2b321c87106b67fdba166e04 [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"
dsinclairc6c425a2016-09-29 12:01:30 -070012#include "core/fpdfapi/fpdf_parser/cpdf_document.h"
13#include "core/fpdfapi/fpdf_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()) {
tsepez36eb4bd2016-10-03 15:24:27 -070051 m_pPDFDoc =
52 pdfium::MakeUnique<CPDF_Document>(std::unique_ptr<CPDF_Parser>());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 m_pPDFDoc->CreateNewDoc();
54 }
55 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056
weili2d5b0202016-08-03 11:06:49 -070057 return m_pPDFDoc.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070058}
59
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060CPDF_Font* CPWL_FontMap::GetPDFFont(int32_t nFontIndex) {
61 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
62 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
63 return pData->pFont;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070064 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066
thestig1cd352e2016-06-07 17:53:06 -070067 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068}
69
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070CFX_ByteString CPWL_FontMap::GetPDFFontAlias(int32_t nFontIndex) {
71 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
72 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
73 return pData->sFontName;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070074 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078}
79
Tom Sepez62a70f92016-03-21 15:00:20 -070080FX_BOOL CPWL_FontMap::KnowWord(int32_t nFontIndex, uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
82 if (m_aData.GetAt(nFontIndex)) {
83 return CharCodeFromUnicode(nFontIndex, word) >= 0;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070084 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088}
89
Tom Sepez62a70f92016-03-21 15:00:20 -070090int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 int32_t nCharset,
92 int32_t nFontIndex) {
93 if (nFontIndex > 0) {
94 if (KnowWord(nFontIndex, word))
95 return nFontIndex;
96 } else {
97 if (const CPWL_FontMap_Data* pData = GetFontMapData(0)) {
npmea3c3be2016-09-19 07:24:33 -070098 if (nCharset == FXFONT_DEFAULT_CHARSET ||
99 pData->nCharset == FXFONT_SYMBOL_CHARSET ||
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 nCharset == pData->nCharset) {
101 if (KnowWord(0, word))
102 return 0;
103 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700104 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 int32_t nNewFontIndex =
108 GetFontIndex(GetNativeFontName(nCharset), nCharset, TRUE);
109 if (nNewFontIndex >= 0) {
110 if (KnowWord(nNewFontIndex, word))
111 return nNewFontIndex;
112 }
npmea3c3be2016-09-19 07:24:33 -0700113 nNewFontIndex =
114 GetFontIndex("Arial Unicode MS", FXFONT_DEFAULT_CHARSET, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 if (nNewFontIndex >= 0) {
116 if (KnowWord(nNewFontIndex, word))
117 return nNewFontIndex;
118 }
119 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120}
121
Tom Sepez62a70f92016-03-21 15:00:20 -0700122int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) {
thestig8ea3f512016-06-27 11:55:24 -0700123 CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex);
124 if (!pData)
125 return -1;
126
127 if (!pData->pFont)
128 return -1;
129
130 if (pData->pFont->IsUnicodeCompatible())
131 return pData->pFont->CharCodeFromUnicode(word);
132
133 return word < 0xFF ? word : -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700134}
135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136CFX_ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset) {
137 // searching native font is slow, so we must save time
138 for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++) {
139 if (CPWL_FontMap_Native* pData = m_aNativeFont.GetAt(i)) {
140 if (pData->nCharset == nCharset)
141 return pData->sFontName;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700142 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 CFX_ByteString sNew = GetNativeFont(nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 if (!sNew.IsEmpty()) {
148 CPWL_FontMap_Native* pNewData = new CPWL_FontMap_Native;
149 pNewData->nCharset = nCharset;
150 pNewData->sFontName = sNew;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700151
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 m_aNativeFont.Add(pNewData);
153 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 return sNew;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700156}
157
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158void CPWL_FontMap::Empty() {
159 {
160 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++)
161 delete m_aData.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 m_aData.RemoveAll();
164 }
165 {
166 for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++)
167 delete m_aNativeFont.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 m_aNativeFont.RemoveAll();
170 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171}
172
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800173void CPWL_FontMap::Initialize() {
npmea3c3be2016-09-19 07:24:33 -0700174 GetFontIndex(kDefaultFontName, FXFONT_ANSI_CHARSET, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700175}
Lei Zhang60f507b2015-06-13 00:41:00 -0700176
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177FX_BOOL CPWL_FontMap::IsStandardFont(const CFX_ByteString& sFontName) {
Wei Li89409932016-03-28 10:33:33 -0700178 for (size_t i = 0; i < FX_ArraySize(g_sDEStandardFontName); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179 if (sFontName == g_sDEStandardFontName[i])
180 return TRUE;
181 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700182
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184}
185
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186int32_t CPWL_FontMap::FindFont(const CFX_ByteString& sFontName,
187 int32_t nCharset) {
188 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) {
189 if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) {
npmea3c3be2016-09-19 07:24:33 -0700190 if (nCharset == FXFONT_DEFAULT_CHARSET || nCharset == pData->nCharset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 if (sFontName.IsEmpty() || pData->sFontName == sFontName)
192 return i;
193 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700194 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700196
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 return -1;
198}
199
200int32_t CPWL_FontMap::GetFontIndex(const CFX_ByteString& sFontName,
201 int32_t nCharset,
202 FX_BOOL bFind) {
203 int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset);
204 if (nFontIndex >= 0)
205 return nFontIndex;
206
207 CFX_ByteString sAlias;
thestig1cd352e2016-06-07 17:53:06 -0700208 CPDF_Font* pFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 if (bFind)
210 pFont = FindFontSameCharset(sAlias, nCharset);
211
212 if (!pFont) {
213 CFX_ByteString sTemp = sFontName;
214 pFont = AddFontToDocument(GetDocument(), sTemp, nCharset);
215 sAlias = EncodeFontAlias(sTemp, nCharset);
216 }
217 AddedFont(pFont, sAlias);
218 return AddFontData(pFont, sAlias, nCharset);
219}
220
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias,
222 int32_t nCharset) {
thestig1cd352e2016-06-07 17:53:06 -0700223 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700224}
225
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont,
227 const CFX_ByteString& sFontAlias,
228 int32_t nCharset) {
229 CPWL_FontMap_Data* pNewData = new CPWL_FontMap_Data;
230 pNewData->pFont = pFont;
231 pNewData->sFontName = sFontAlias;
232 pNewData->nCharset = nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 m_aData.Add(pNewData);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700235
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 return m_aData.GetSize() - 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237}
238
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239void CPWL_FontMap::AddedFont(CPDF_Font* pFont,
240 const CFX_ByteString& sFontAlias) {}
241
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) {
npmea3c3be2016-09-19 07:24:33 -0700243 if (nCharset == FXFONT_DEFAULT_CHARSET)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 nCharset = GetNativeCharset();
245
246 CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset);
dsinclair0e3e8902016-08-24 11:39:24 -0700247 if (m_pSystemHandler->FindNativeTrueTypeFont(sFontName))
248 return sFontName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249
dsinclair0e3e8902016-08-24 11:39:24 -0700250 sFontName.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 return sFontName;
252}
253
254CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc,
255 CFX_ByteString& sFontName,
256 uint8_t nCharset) {
257 if (IsStandardFont(sFontName))
258 return AddStandardFont(pDoc, sFontName);
259
260 return AddSystemFont(pDoc, sFontName, nCharset);
261}
262
263CPDF_Font* CPWL_FontMap::AddStandardFont(CPDF_Document* pDoc,
264 CFX_ByteString& sFontName) {
265 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700266 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267
thestig1cd352e2016-06-07 17:53:06 -0700268 CPDF_Font* pFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800270 if (sFontName == "ZapfDingbats") {
thestig1cd352e2016-06-07 17:53:06 -0700271 pFont = pDoc->AddStandardFont(sFontName.c_str(), nullptr);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800272 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI);
tsepezb4c9f3f2016-04-13 15:41:21 -0700274 pFont = pDoc->AddStandardFont(sFontName.c_str(), &fe);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275 }
276
277 return pFont;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278}
279
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280CPDF_Font* CPWL_FontMap::AddSystemFont(CPDF_Document* pDoc,
281 CFX_ByteString& sFontName,
282 uint8_t nCharset) {
283 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700284 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285
286 if (sFontName.IsEmpty())
287 sFontName = GetNativeFont(nCharset);
npmea3c3be2016-09-19 07:24:33 -0700288 if (nCharset == FXFONT_DEFAULT_CHARSET)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 nCharset = GetNativeCharset();
290
dsinclair0e3e8902016-08-24 11:39:24 -0700291 return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName,
292 nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700293}
294
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName,
296 int32_t nCharset) {
297 CFX_ByteString sPostfix;
298 sPostfix.Format("_%02X", nCharset);
299 return EncodeFontAlias(sFontName) + sPostfix;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700300}
301
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) {
303 CFX_ByteString sRet = sFontName;
304 sRet.Remove(' ');
305 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700306}
307
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const {
309 if (nIndex >= 0 && nIndex < m_aData.GetSize()) {
310 return m_aData.GetAt(nIndex);
311 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700312
thestig1cd352e2016-06-07 17:53:06 -0700313 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700314}
315
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316int32_t CPWL_FontMap::GetNativeCharset() {
npmea3c3be2016-09-19 07:24:33 -0700317 uint8_t nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318 int32_t iCodePage = FXSYS_GetACP();
319 switch (iCodePage) {
320 case 932: // Japan
npmea3c3be2016-09-19 07:24:33 -0700321 nCharset = FXFONT_SHIFTJIS_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 break;
323 case 936: // Chinese (PRC, Singapore)
npmea3c3be2016-09-19 07:24:33 -0700324 nCharset = FXFONT_GB2312_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 break;
326 case 950: // Chinese (Taiwan; Hong Kong SAR, PRC)
npmea3c3be2016-09-19 07:24:33 -0700327 nCharset = FXFONT_GB2312_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 break;
329 case 1252: // Windows 3.1 Latin 1 (US, Western Europe)
npmea3c3be2016-09-19 07:24:33 -0700330 nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331 break;
332 case 874: // Thai
npmea3c3be2016-09-19 07:24:33 -0700333 nCharset = FXFONT_THAI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 break;
335 case 949: // Korean
npmea3c3be2016-09-19 07:24:33 -0700336 nCharset = FXFONT_HANGUL_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 break;
338 case 1200: // Unicode (BMP of ISO 10646)
npmea3c3be2016-09-19 07:24:33 -0700339 nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 break;
341 case 1250: // Windows 3.1 Eastern European
npmea3c3be2016-09-19 07:24:33 -0700342 nCharset = FXFONT_EASTEUROPE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343 break;
344 case 1251: // Windows 3.1 Cyrillic
npmea3c3be2016-09-19 07:24:33 -0700345 nCharset = FXFONT_RUSSIAN_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700346 break;
347 case 1253: // Windows 3.1 Greek
npmea3c3be2016-09-19 07:24:33 -0700348 nCharset = FXFONT_GREEK_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 break;
350 case 1254: // Windows 3.1 Turkish
npmea3c3be2016-09-19 07:24:33 -0700351 nCharset = FXFONT_TURKISH_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352 break;
353 case 1255: // Hebrew
npmea3c3be2016-09-19 07:24:33 -0700354 nCharset = FXFONT_HEBREW_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 break;
356 case 1256: // Arabic
npmea3c3be2016-09-19 07:24:33 -0700357 nCharset = FXFONT_ARABIC_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700358 break;
359 case 1257: // Baltic
npmea3c3be2016-09-19 07:24:33 -0700360 nCharset = FXFONT_BALTIC_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361 break;
362 case 1258: // Vietnamese
npmea3c3be2016-09-19 07:24:33 -0700363 nCharset = FXFONT_VIETNAMESE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364 break;
365 case 1361: // Korean(Johab)
npmea3c3be2016-09-19 07:24:33 -0700366 nCharset = FXFONT_JOHAB_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 break;
368 }
369 return nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700370}
371
weili2d5b0202016-08-03 11:06:49 -0700372const FPDF_CharsetFontMap CPWL_FontMap::defaultTTFMap[] = {
npmea3c3be2016-09-19 07:24:33 -0700373 {FXFONT_ANSI_CHARSET, "Helvetica"},
374 {FXFONT_GB2312_CHARSET, "SimSun"},
375 {FXFONT_CHINESEBIG5_CHARSET, "MingLiU"},
376 {FXFONT_SHIFTJIS_CHARSET, "MS Gothic"},
377 {FXFONT_HANGUL_CHARSET, "Batang"},
378 {FXFONT_RUSSIAN_CHARSET, "Arial"},
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \
380 _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
npmea3c3be2016-09-19 07:24:33 -0700381 {FXFONT_EASTEUROPE_CHARSET, "Arial"},
Bo Xudbd4c062014-05-29 11:32:56 -0700382#else
npmea3c3be2016-09-19 07:24:33 -0700383 {FXFONT_EASTEUROPE_CHARSET, "Tahoma"},
Bo Xudbd4c062014-05-29 11:32:56 -0700384#endif
npmea3c3be2016-09-19 07:24:33 -0700385 {FXFONT_ARABIC_CHARSET, "Arial"},
386 {-1, nullptr}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700387
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388CFX_ByteString CPWL_FontMap::GetDefaultFontByCharset(int32_t nCharset) {
389 int i = 0;
390 while (defaultTTFMap[i].charset != -1) {
391 if (nCharset == defaultTTFMap[i].charset)
392 return defaultTTFMap[i].fontname;
393 ++i;
394 }
395 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700396}
397
Tom Sepez62a70f92016-03-21 15:00:20 -0700398int32_t CPWL_FontMap::CharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 // to avoid CJK Font to show ASCII
400 if (word < 0x7F)
npmea3c3be2016-09-19 07:24:33 -0700401 return FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402 // follow the old charset
npmea3c3be2016-09-19 07:24:33 -0700403 if (nOldCharset != FXFONT_DEFAULT_CHARSET)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404 return nOldCharset;
405
406 // find new charset
407 if ((word >= 0x4E00 && word <= 0x9FA5) ||
408 (word >= 0xE7C7 && word <= 0xE7F3) ||
409 (word >= 0x3000 && word <= 0x303F) ||
410 (word >= 0x2000 && word <= 0x206F)) {
npmea3c3be2016-09-19 07:24:33 -0700411 return FXFONT_GB2312_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412 }
413
414 if (((word >= 0x3040) && (word <= 0x309F)) ||
415 ((word >= 0x30A0) && (word <= 0x30FF)) ||
416 ((word >= 0x31F0) && (word <= 0x31FF)) ||
417 ((word >= 0xFF00) && (word <= 0xFFEF))) {
npmea3c3be2016-09-19 07:24:33 -0700418 return FXFONT_SHIFTJIS_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419 }
420
421 if (((word >= 0xAC00) && (word <= 0xD7AF)) ||
422 ((word >= 0x1100) && (word <= 0x11FF)) ||
423 ((word >= 0x3130) && (word <= 0x318F))) {
npmea3c3be2016-09-19 07:24:33 -0700424 return FXFONT_HANGUL_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425 }
426
427 if (word >= 0x0E00 && word <= 0x0E7F)
npmea3c3be2016-09-19 07:24:33 -0700428 return FXFONT_THAI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700429
430 if ((word >= 0x0370 && word <= 0x03FF) || (word >= 0x1F00 && word <= 0x1FFF))
npmea3c3be2016-09-19 07:24:33 -0700431 return FXFONT_GREEK_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700432
433 if ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC))
npmea3c3be2016-09-19 07:24:33 -0700434 return FXFONT_ARABIC_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435
436 if (word >= 0x0590 && word <= 0x05FF)
npmea3c3be2016-09-19 07:24:33 -0700437 return FXFONT_HEBREW_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700438
439 if (word >= 0x0400 && word <= 0x04FF)
npmea3c3be2016-09-19 07:24:33 -0700440 return FXFONT_RUSSIAN_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700441
442 if (word >= 0x0100 && word <= 0x024F)
npmea3c3be2016-09-19 07:24:33 -0700443 return FXFONT_EASTEUROPE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444
445 if (word >= 0x1E00 && word <= 0x1EFF)
npmea3c3be2016-09-19 07:24:33 -0700446 return FXFONT_VIETNAMESE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447
npmea3c3be2016-09-19 07:24:33 -0700448 return FXFONT_ANSI_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700449}