blob: 96acb78ec2aa0538730af46e8dfd5d3701152b7e [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"
12#include "core/fpdfapi/include/cpdf_modulemgr.h"
dsinclairc7a73492016-04-05 12:01:42 -070013#include "core/fpdfdoc/include/ipvt_fontmap.h"
dan sinclair89e904b2016-03-23 19:29:15 -040014#include "fpdfsdk/pdfwindow/PWL_Wnd.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015
Lei Zhang1b976642016-01-08 14:24:37 -080016namespace {
17
18const char kDefaultFontName[] = "Helvetica";
19
20const char* const g_sDEStandardFontName[] = {"Courier",
21 "Courier-Bold",
22 "Courier-BoldOblique",
23 "Courier-Oblique",
24 "Helvetica",
25 "Helvetica-Bold",
26 "Helvetica-BoldOblique",
27 "Helvetica-Oblique",
28 "Times-Roman",
29 "Times-Bold",
30 "Times-Italic",
31 "Times-BoldItalic",
32 "Symbol",
33 "ZapfDingbats"};
34
35} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036
dsinclairb9590102016-04-27 06:38:59 -070037CPWL_FontMap::CPWL_FontMap(CFX_SystemHandler* pSystemHandler)
weili2d5b0202016-08-03 11:06:49 -070038 : m_pSystemHandler(pSystemHandler) {
Lei Zhang96660d62015-12-14 18:27:25 -080039 ASSERT(m_pSystemHandler);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040}
41
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042CPWL_FontMap::~CPWL_FontMap() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 Empty();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044}
45
dsinclairb9590102016-04-27 06:38:59 -070046void CPWL_FontMap::SetSystemHandler(CFX_SystemHandler* pSystemHandler) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 m_pSystemHandler = pSystemHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048}
49
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050CPDF_Document* CPWL_FontMap::GetDocument() {
51 if (!m_pPDFDoc) {
52 if (CPDF_ModuleMgr::Get()) {
weili2d5b0202016-08-03 11:06:49 -070053 m_pPDFDoc.reset(new CPDF_Document(nullptr));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 m_pPDFDoc->CreateNewDoc();
55 }
56 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057
weili2d5b0202016-08-03 11:06:49 -070058 return m_pPDFDoc.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059}
60
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061CPDF_Font* CPWL_FontMap::GetPDFFont(int32_t nFontIndex) {
62 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
63 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
64 return pData->pFont;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070065 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067
thestig1cd352e2016-06-07 17:53:06 -070068 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070069}
70
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071CFX_ByteString CPWL_FontMap::GetPDFFontAlias(int32_t nFontIndex) {
72 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
73 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
74 return pData->sFontName;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070075 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079}
80
Tom Sepez62a70f92016-03-21 15:00:20 -070081FX_BOOL CPWL_FontMap::KnowWord(int32_t nFontIndex, uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
83 if (m_aData.GetAt(nFontIndex)) {
84 return CharCodeFromUnicode(nFontIndex, word) >= 0;
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 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089}
90
Tom Sepez62a70f92016-03-21 15:00:20 -070091int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 int32_t nCharset,
93 int32_t nFontIndex) {
94 if (nFontIndex > 0) {
95 if (KnowWord(nFontIndex, word))
96 return nFontIndex;
97 } else {
98 if (const CPWL_FontMap_Data* pData = GetFontMapData(0)) {
99 if (nCharset == DEFAULT_CHARSET || pData->nCharset == SYMBOL_CHARSET ||
100 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 }
113 nNewFontIndex = GetFontIndex("Arial Unicode MS", DEFAULT_CHARSET, FALSE);
114 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() {
Lei Zhang1b976642016-01-08 14:24:37 -0800173 GetFontIndex(kDefaultFontName, 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)) {
189 if (nCharset == DEFAULT_CHARSET || nCharset == pData->nCharset) {
190 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
Tom Sepez62a70f92016-03-21 15:00:20 -0700220int32_t CPWL_FontMap::GetPWLFontIndex(uint16_t word, int32_t nCharset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 int32_t nFind = -1;
222
223 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) {
224 if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) {
225 if (pData->nCharset == nCharset) {
226 nFind = i;
227 break;
228 }
229 }
230 }
231
232 CPDF_Font* pNewFont = GetPDFFont(nFind);
233
234 if (!pNewFont)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700235 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 CFX_ByteString sAlias = EncodeFontAlias("Arial_Chrome", nCharset);
238 AddedFont(pNewFont, sAlias);
239
240 return AddFontData(pNewFont, sAlias, nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700241}
242
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias,
244 int32_t nCharset) {
thestig1cd352e2016-06-07 17:53:06 -0700245 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246}
247
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont,
249 const CFX_ByteString& sFontAlias,
250 int32_t nCharset) {
251 CPWL_FontMap_Data* pNewData = new CPWL_FontMap_Data;
252 pNewData->pFont = pFont;
253 pNewData->sFontName = sFontAlias;
254 pNewData->nCharset = nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 m_aData.Add(pNewData);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700257
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 return m_aData.GetSize() - 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259}
260
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261void CPWL_FontMap::AddedFont(CPDF_Font* pFont,
262 const CFX_ByteString& sFontAlias) {}
263
264CFX_ByteString CPWL_FontMap::GetFontName(int32_t nFontIndex) {
265 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
266 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
267 return pData->sFontName;
268 }
269 }
270
271 return "";
272}
273
274CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) {
275 if (nCharset == DEFAULT_CHARSET)
276 nCharset = GetNativeCharset();
277
278 CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset);
279 if (m_pSystemHandler) {
thestig907a5222016-06-21 14:38:27 -0700280 if (m_pSystemHandler->FindNativeTrueTypeFont(sFontName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 return sFontName;
282
thestig907a5222016-06-21 14:38:27 -0700283 sFontName.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 }
285 return sFontName;
286}
287
288CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc,
289 CFX_ByteString& sFontName,
290 uint8_t nCharset) {
291 if (IsStandardFont(sFontName))
292 return AddStandardFont(pDoc, sFontName);
293
294 return AddSystemFont(pDoc, sFontName, nCharset);
295}
296
297CPDF_Font* CPWL_FontMap::AddStandardFont(CPDF_Document* pDoc,
298 CFX_ByteString& sFontName) {
299 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700300 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301
thestig1cd352e2016-06-07 17:53:06 -0700302 CPDF_Font* pFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800304 if (sFontName == "ZapfDingbats") {
thestig1cd352e2016-06-07 17:53:06 -0700305 pFont = pDoc->AddStandardFont(sFontName.c_str(), nullptr);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800306 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307 CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI);
tsepezb4c9f3f2016-04-13 15:41:21 -0700308 pFont = pDoc->AddStandardFont(sFontName.c_str(), &fe);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309 }
310
311 return pFont;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700312}
313
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314CPDF_Font* CPWL_FontMap::AddSystemFont(CPDF_Document* pDoc,
315 CFX_ByteString& sFontName,
316 uint8_t nCharset) {
317 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700318 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319
320 if (sFontName.IsEmpty())
321 sFontName = GetNativeFont(nCharset);
322 if (nCharset == DEFAULT_CHARSET)
323 nCharset = GetNativeCharset();
324
325 if (m_pSystemHandler)
326 return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName,
327 nCharset);
328
thestig1cd352e2016-06-07 17:53:06 -0700329 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700330}
331
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName,
333 int32_t nCharset) {
334 CFX_ByteString sPostfix;
335 sPostfix.Format("_%02X", nCharset);
336 return EncodeFontAlias(sFontName) + sPostfix;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700337}
338
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700339CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) {
340 CFX_ByteString sRet = sFontName;
341 sRet.Remove(' ');
342 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700343}
344
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345int32_t CPWL_FontMap::GetFontMapCount() const {
346 return m_aData.GetSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700347}
348
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const {
350 if (nIndex >= 0 && nIndex < m_aData.GetSize()) {
351 return m_aData.GetAt(nIndex);
352 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700353
thestig1cd352e2016-06-07 17:53:06 -0700354 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700355}
356
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357int32_t CPWL_FontMap::GetNativeCharset() {
358 uint8_t nCharset = ANSI_CHARSET;
359 int32_t iCodePage = FXSYS_GetACP();
360 switch (iCodePage) {
361 case 932: // Japan
362 nCharset = SHIFTJIS_CHARSET;
363 break;
364 case 936: // Chinese (PRC, Singapore)
365 nCharset = GB2312_CHARSET;
366 break;
367 case 950: // Chinese (Taiwan; Hong Kong SAR, PRC)
368 nCharset = GB2312_CHARSET;
369 break;
370 case 1252: // Windows 3.1 Latin 1 (US, Western Europe)
371 nCharset = ANSI_CHARSET;
372 break;
373 case 874: // Thai
374 nCharset = THAI_CHARSET;
375 break;
376 case 949: // Korean
377 nCharset = HANGUL_CHARSET;
378 break;
379 case 1200: // Unicode (BMP of ISO 10646)
380 nCharset = ANSI_CHARSET;
381 break;
382 case 1250: // Windows 3.1 Eastern European
383 nCharset = EASTEUROPE_CHARSET;
384 break;
385 case 1251: // Windows 3.1 Cyrillic
386 nCharset = RUSSIAN_CHARSET;
387 break;
388 case 1253: // Windows 3.1 Greek
389 nCharset = GREEK_CHARSET;
390 break;
391 case 1254: // Windows 3.1 Turkish
392 nCharset = TURKISH_CHARSET;
393 break;
394 case 1255: // Hebrew
395 nCharset = HEBREW_CHARSET;
396 break;
397 case 1256: // Arabic
398 nCharset = ARABIC_CHARSET;
399 break;
400 case 1257: // Baltic
401 nCharset = BALTIC_CHARSET;
402 break;
403 case 1258: // Vietnamese
404 nCharset = VIETNAMESE_CHARSET;
405 break;
406 case 1361: // Korean(Johab)
407 nCharset = JOHAB_CHARSET;
408 break;
409 }
410 return nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700411}
412
weili2d5b0202016-08-03 11:06:49 -0700413const FPDF_CharsetFontMap CPWL_FontMap::defaultTTFMap[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414 {ANSI_CHARSET, "Helvetica"}, {GB2312_CHARSET, "SimSun"},
415 {CHINESEBIG5_CHARSET, "MingLiU"}, {SHIFTJIS_CHARSET, "MS Gothic"},
416 {HANGUL_CHARSET, "Batang"}, {RUSSIAN_CHARSET, "Arial"},
417#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \
418 _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
419 {EASTEUROPE_CHARSET, "Arial"},
Bo Xudbd4c062014-05-29 11:32:56 -0700420#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421 {EASTEUROPE_CHARSET, "Tahoma"},
Bo Xudbd4c062014-05-29 11:32:56 -0700422#endif
thestig1cd352e2016-06-07 17:53:06 -0700423 {ARABIC_CHARSET, "Arial"}, {-1, nullptr}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700424
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425CFX_ByteString CPWL_FontMap::GetDefaultFontByCharset(int32_t nCharset) {
426 int i = 0;
427 while (defaultTTFMap[i].charset != -1) {
428 if (nCharset == defaultTTFMap[i].charset)
429 return defaultTTFMap[i].fontname;
430 ++i;
431 }
432 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700433}
434
Tom Sepez62a70f92016-03-21 15:00:20 -0700435int32_t CPWL_FontMap::CharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436 // to avoid CJK Font to show ASCII
437 if (word < 0x7F)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700438 return ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 // follow the old charset
440 if (nOldCharset != DEFAULT_CHARSET)
441 return nOldCharset;
442
443 // find new charset
444 if ((word >= 0x4E00 && word <= 0x9FA5) ||
445 (word >= 0xE7C7 && word <= 0xE7F3) ||
446 (word >= 0x3000 && word <= 0x303F) ||
447 (word >= 0x2000 && word <= 0x206F)) {
448 return GB2312_CHARSET;
449 }
450
451 if (((word >= 0x3040) && (word <= 0x309F)) ||
452 ((word >= 0x30A0) && (word <= 0x30FF)) ||
453 ((word >= 0x31F0) && (word <= 0x31FF)) ||
454 ((word >= 0xFF00) && (word <= 0xFFEF))) {
455 return SHIFTJIS_CHARSET;
456 }
457
458 if (((word >= 0xAC00) && (word <= 0xD7AF)) ||
459 ((word >= 0x1100) && (word <= 0x11FF)) ||
460 ((word >= 0x3130) && (word <= 0x318F))) {
461 return HANGUL_CHARSET;
462 }
463
464 if (word >= 0x0E00 && word <= 0x0E7F)
465 return THAI_CHARSET;
466
467 if ((word >= 0x0370 && word <= 0x03FF) || (word >= 0x1F00 && word <= 0x1FFF))
468 return GREEK_CHARSET;
469
470 if ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC))
471 return ARABIC_CHARSET;
472
473 if (word >= 0x0590 && word <= 0x05FF)
474 return HEBREW_CHARSET;
475
476 if (word >= 0x0400 && word <= 0x04FF)
477 return RUSSIAN_CHARSET;
478
479 if (word >= 0x0100 && word <= 0x024F)
480 return EASTEUROPE_CHARSET;
481
482 if (word >= 0x1E00 && word <= 0x1EFF)
483 return VIETNAMESE_CHARSET;
484
485 return ANSI_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700486}
487
dsinclairb9590102016-04-27 06:38:59 -0700488CPWL_DocFontMap::CPWL_DocFontMap(CFX_SystemHandler* pSystemHandler,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700489 CPDF_Document* pAttachedDoc)
490 : CPWL_FontMap(pSystemHandler), m_pAttachedDoc(pAttachedDoc) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700491
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700492CPWL_DocFontMap::~CPWL_DocFontMap() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700493
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494CPDF_Document* CPWL_DocFontMap::GetDocument() {
495 return m_pAttachedDoc;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700496}