blob: 9192afd5e8e0549453e8991056faede4f39a013a [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)
thestig1cd352e2016-06-07 17:53:06 -070038 : m_pPDFDoc(nullptr), 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() {
43 delete m_pPDFDoc;
thestig1cd352e2016-06-07 17:53:06 -070044 m_pPDFDoc = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 Empty();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047}
48
dsinclairb9590102016-04-27 06:38:59 -070049void CPWL_FontMap::SetSystemHandler(CFX_SystemHandler* pSystemHandler) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 m_pSystemHandler = pSystemHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051}
52
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053CPDF_Document* CPWL_FontMap::GetDocument() {
54 if (!m_pPDFDoc) {
55 if (CPDF_ModuleMgr::Get()) {
thestig931bf372016-04-26 22:24:30 -070056 m_pPDFDoc = new CPDF_Document(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 m_pPDFDoc->CreateNewDoc();
58 }
59 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 return m_pPDFDoc;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062}
63
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064CPDF_Font* CPWL_FontMap::GetPDFFont(int32_t nFontIndex) {
65 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
66 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
67 return pData->pFont;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070068 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070
thestig1cd352e2016-06-07 17:53:06 -070071 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070072}
73
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074CFX_ByteString CPWL_FontMap::GetPDFFontAlias(int32_t nFontIndex) {
75 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
76 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
77 return pData->sFontName;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070078 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082}
83
Tom Sepez62a70f92016-03-21 15:00:20 -070084FX_BOOL CPWL_FontMap::KnowWord(int32_t nFontIndex, uint16_t word) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
86 if (m_aData.GetAt(nFontIndex)) {
87 return CharCodeFromUnicode(nFontIndex, word) >= 0;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070088 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092}
93
Tom Sepez62a70f92016-03-21 15:00:20 -070094int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 int32_t nCharset,
96 int32_t nFontIndex) {
97 if (nFontIndex > 0) {
98 if (KnowWord(nFontIndex, word))
99 return nFontIndex;
100 } else {
101 if (const CPWL_FontMap_Data* pData = GetFontMapData(0)) {
102 if (nCharset == DEFAULT_CHARSET || pData->nCharset == SYMBOL_CHARSET ||
103 nCharset == pData->nCharset) {
104 if (KnowWord(0, word))
105 return 0;
106 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700107 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700109
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 int32_t nNewFontIndex =
111 GetFontIndex(GetNativeFontName(nCharset), nCharset, TRUE);
112 if (nNewFontIndex >= 0) {
113 if (KnowWord(nNewFontIndex, word))
114 return nNewFontIndex;
115 }
116 nNewFontIndex = GetFontIndex("Arial Unicode MS", DEFAULT_CHARSET, FALSE);
117 if (nNewFontIndex >= 0) {
118 if (KnowWord(nNewFontIndex, word))
119 return nNewFontIndex;
120 }
121 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700122}
123
Tom Sepez62a70f92016-03-21 15:00:20 -0700124int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) {
thestig8ea3f512016-06-27 11:55:24 -0700125 CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex);
126 if (!pData)
127 return -1;
128
129 if (!pData->pFont)
130 return -1;
131
132 if (pData->pFont->IsUnicodeCompatible())
133 return pData->pFont->CharCodeFromUnicode(word);
134
135 return word < 0xFF ? word : -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700136}
137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138CFX_ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset) {
139 // searching native font is slow, so we must save time
140 for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++) {
141 if (CPWL_FontMap_Native* pData = m_aNativeFont.GetAt(i)) {
142 if (pData->nCharset == nCharset)
143 return pData->sFontName;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700144 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 CFX_ByteString sNew = GetNativeFont(nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700148
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149 if (!sNew.IsEmpty()) {
150 CPWL_FontMap_Native* pNewData = new CPWL_FontMap_Native;
151 pNewData->nCharset = nCharset;
152 pNewData->sFontName = sNew;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 m_aNativeFont.Add(pNewData);
155 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700156
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 return sNew;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700158}
159
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160void CPWL_FontMap::Empty() {
161 {
162 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++)
163 delete m_aData.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700164
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 m_aData.RemoveAll();
166 }
167 {
168 for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++)
169 delete m_aNativeFont.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700170
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 m_aNativeFont.RemoveAll();
172 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700173}
174
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800175void CPWL_FontMap::Initialize() {
Lei Zhang1b976642016-01-08 14:24:37 -0800176 GetFontIndex(kDefaultFontName, ANSI_CHARSET, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700177}
Lei Zhang60f507b2015-06-13 00:41:00 -0700178
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179FX_BOOL CPWL_FontMap::IsStandardFont(const CFX_ByteString& sFontName) {
Wei Li89409932016-03-28 10:33:33 -0700180 for (size_t i = 0; i < FX_ArraySize(g_sDEStandardFontName); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181 if (sFontName == g_sDEStandardFontName[i])
182 return TRUE;
183 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700186}
187
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188int32_t CPWL_FontMap::FindFont(const CFX_ByteString& sFontName,
189 int32_t nCharset) {
190 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) {
191 if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) {
192 if (nCharset == DEFAULT_CHARSET || nCharset == pData->nCharset) {
193 if (sFontName.IsEmpty() || pData->sFontName == sFontName)
194 return i;
195 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700196 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 return -1;
200}
201
202int32_t CPWL_FontMap::GetFontIndex(const CFX_ByteString& sFontName,
203 int32_t nCharset,
204 FX_BOOL bFind) {
205 int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset);
206 if (nFontIndex >= 0)
207 return nFontIndex;
208
209 CFX_ByteString sAlias;
thestig1cd352e2016-06-07 17:53:06 -0700210 CPDF_Font* pFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 if (bFind)
212 pFont = FindFontSameCharset(sAlias, nCharset);
213
214 if (!pFont) {
215 CFX_ByteString sTemp = sFontName;
216 pFont = AddFontToDocument(GetDocument(), sTemp, nCharset);
217 sAlias = EncodeFontAlias(sTemp, nCharset);
218 }
219 AddedFont(pFont, sAlias);
220 return AddFontData(pFont, sAlias, nCharset);
221}
222
Tom Sepez62a70f92016-03-21 15:00:20 -0700223int32_t CPWL_FontMap::GetPWLFontIndex(uint16_t word, int32_t nCharset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 int32_t nFind = -1;
225
226 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) {
227 if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) {
228 if (pData->nCharset == nCharset) {
229 nFind = i;
230 break;
231 }
232 }
233 }
234
235 CPDF_Font* pNewFont = GetPDFFont(nFind);
236
237 if (!pNewFont)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700238 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 CFX_ByteString sAlias = EncodeFontAlias("Arial_Chrome", nCharset);
241 AddedFont(pNewFont, sAlias);
242
243 return AddFontData(pNewFont, sAlias, nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700244}
245
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias,
247 int32_t nCharset) {
thestig1cd352e2016-06-07 17:53:06 -0700248 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700249}
250
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont,
252 const CFX_ByteString& sFontAlias,
253 int32_t nCharset) {
254 CPWL_FontMap_Data* pNewData = new CPWL_FontMap_Data;
255 pNewData->pFont = pFont;
256 pNewData->sFontName = sFontAlias;
257 pNewData->nCharset = nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 m_aData.Add(pNewData);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 return m_aData.GetSize() - 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262}
263
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264void CPWL_FontMap::AddedFont(CPDF_Font* pFont,
265 const CFX_ByteString& sFontAlias) {}
266
267CFX_ByteString CPWL_FontMap::GetFontName(int32_t nFontIndex) {
268 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) {
269 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) {
270 return pData->sFontName;
271 }
272 }
273
274 return "";
275}
276
277CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) {
278 if (nCharset == DEFAULT_CHARSET)
279 nCharset = GetNativeCharset();
280
281 CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset);
282 if (m_pSystemHandler) {
thestig907a5222016-06-21 14:38:27 -0700283 if (m_pSystemHandler->FindNativeTrueTypeFont(sFontName))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 return sFontName;
285
thestig907a5222016-06-21 14:38:27 -0700286 sFontName.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 }
288 return sFontName;
289}
290
291CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc,
292 CFX_ByteString& sFontName,
293 uint8_t nCharset) {
294 if (IsStandardFont(sFontName))
295 return AddStandardFont(pDoc, sFontName);
296
297 return AddSystemFont(pDoc, sFontName, nCharset);
298}
299
300CPDF_Font* CPWL_FontMap::AddStandardFont(CPDF_Document* pDoc,
301 CFX_ByteString& sFontName) {
302 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700303 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304
thestig1cd352e2016-06-07 17:53:06 -0700305 CPDF_Font* pFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700306
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800307 if (sFontName == "ZapfDingbats") {
thestig1cd352e2016-06-07 17:53:06 -0700308 pFont = pDoc->AddStandardFont(sFontName.c_str(), nullptr);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800309 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI);
tsepezb4c9f3f2016-04-13 15:41:21 -0700311 pFont = pDoc->AddStandardFont(sFontName.c_str(), &fe);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312 }
313
314 return pFont;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700315}
316
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317CPDF_Font* CPWL_FontMap::AddSystemFont(CPDF_Document* pDoc,
318 CFX_ByteString& sFontName,
319 uint8_t nCharset) {
320 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700321 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322
323 if (sFontName.IsEmpty())
324 sFontName = GetNativeFont(nCharset);
325 if (nCharset == DEFAULT_CHARSET)
326 nCharset = GetNativeCharset();
327
328 if (m_pSystemHandler)
329 return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName,
330 nCharset);
331
thestig1cd352e2016-06-07 17:53:06 -0700332 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700333}
334
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName,
336 int32_t nCharset) {
337 CFX_ByteString sPostfix;
338 sPostfix.Format("_%02X", nCharset);
339 return EncodeFontAlias(sFontName) + sPostfix;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700340}
341
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) {
343 CFX_ByteString sRet = sFontName;
344 sRet.Remove(' ');
345 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700346}
347
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348int32_t CPWL_FontMap::GetFontMapCount() const {
349 return m_aData.GetSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700350}
351
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const {
353 if (nIndex >= 0 && nIndex < m_aData.GetSize()) {
354 return m_aData.GetAt(nIndex);
355 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700356
thestig1cd352e2016-06-07 17:53:06 -0700357 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700358}
359
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360int32_t CPWL_FontMap::GetNativeCharset() {
361 uint8_t nCharset = ANSI_CHARSET;
362 int32_t iCodePage = FXSYS_GetACP();
363 switch (iCodePage) {
364 case 932: // Japan
365 nCharset = SHIFTJIS_CHARSET;
366 break;
367 case 936: // Chinese (PRC, Singapore)
368 nCharset = GB2312_CHARSET;
369 break;
370 case 950: // Chinese (Taiwan; Hong Kong SAR, PRC)
371 nCharset = GB2312_CHARSET;
372 break;
373 case 1252: // Windows 3.1 Latin 1 (US, Western Europe)
374 nCharset = ANSI_CHARSET;
375 break;
376 case 874: // Thai
377 nCharset = THAI_CHARSET;
378 break;
379 case 949: // Korean
380 nCharset = HANGUL_CHARSET;
381 break;
382 case 1200: // Unicode (BMP of ISO 10646)
383 nCharset = ANSI_CHARSET;
384 break;
385 case 1250: // Windows 3.1 Eastern European
386 nCharset = EASTEUROPE_CHARSET;
387 break;
388 case 1251: // Windows 3.1 Cyrillic
389 nCharset = RUSSIAN_CHARSET;
390 break;
391 case 1253: // Windows 3.1 Greek
392 nCharset = GREEK_CHARSET;
393 break;
394 case 1254: // Windows 3.1 Turkish
395 nCharset = TURKISH_CHARSET;
396 break;
397 case 1255: // Hebrew
398 nCharset = HEBREW_CHARSET;
399 break;
400 case 1256: // Arabic
401 nCharset = ARABIC_CHARSET;
402 break;
403 case 1257: // Baltic
404 nCharset = BALTIC_CHARSET;
405 break;
406 case 1258: // Vietnamese
407 nCharset = VIETNAMESE_CHARSET;
408 break;
409 case 1361: // Korean(Johab)
410 nCharset = JOHAB_CHARSET;
411 break;
412 }
413 return nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700414}
415
416const CPWL_FontMap::CharsetFontMap CPWL_FontMap::defaultTTFMap[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700417 {ANSI_CHARSET, "Helvetica"}, {GB2312_CHARSET, "SimSun"},
418 {CHINESEBIG5_CHARSET, "MingLiU"}, {SHIFTJIS_CHARSET, "MS Gothic"},
419 {HANGUL_CHARSET, "Batang"}, {RUSSIAN_CHARSET, "Arial"},
420#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \
421 _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
422 {EASTEUROPE_CHARSET, "Arial"},
Bo Xudbd4c062014-05-29 11:32:56 -0700423#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424 {EASTEUROPE_CHARSET, "Tahoma"},
Bo Xudbd4c062014-05-29 11:32:56 -0700425#endif
thestig1cd352e2016-06-07 17:53:06 -0700426 {ARABIC_CHARSET, "Arial"}, {-1, nullptr}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700427
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428CFX_ByteString CPWL_FontMap::GetDefaultFontByCharset(int32_t nCharset) {
429 int i = 0;
430 while (defaultTTFMap[i].charset != -1) {
431 if (nCharset == defaultTTFMap[i].charset)
432 return defaultTTFMap[i].fontname;
433 ++i;
434 }
435 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700436}
437
Tom Sepez62a70f92016-03-21 15:00:20 -0700438int32_t CPWL_FontMap::CharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 // to avoid CJK Font to show ASCII
440 if (word < 0x7F)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700441 return ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 // follow the old charset
443 if (nOldCharset != DEFAULT_CHARSET)
444 return nOldCharset;
445
446 // find new charset
447 if ((word >= 0x4E00 && word <= 0x9FA5) ||
448 (word >= 0xE7C7 && word <= 0xE7F3) ||
449 (word >= 0x3000 && word <= 0x303F) ||
450 (word >= 0x2000 && word <= 0x206F)) {
451 return GB2312_CHARSET;
452 }
453
454 if (((word >= 0x3040) && (word <= 0x309F)) ||
455 ((word >= 0x30A0) && (word <= 0x30FF)) ||
456 ((word >= 0x31F0) && (word <= 0x31FF)) ||
457 ((word >= 0xFF00) && (word <= 0xFFEF))) {
458 return SHIFTJIS_CHARSET;
459 }
460
461 if (((word >= 0xAC00) && (word <= 0xD7AF)) ||
462 ((word >= 0x1100) && (word <= 0x11FF)) ||
463 ((word >= 0x3130) && (word <= 0x318F))) {
464 return HANGUL_CHARSET;
465 }
466
467 if (word >= 0x0E00 && word <= 0x0E7F)
468 return THAI_CHARSET;
469
470 if ((word >= 0x0370 && word <= 0x03FF) || (word >= 0x1F00 && word <= 0x1FFF))
471 return GREEK_CHARSET;
472
473 if ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC))
474 return ARABIC_CHARSET;
475
476 if (word >= 0x0590 && word <= 0x05FF)
477 return HEBREW_CHARSET;
478
479 if (word >= 0x0400 && word <= 0x04FF)
480 return RUSSIAN_CHARSET;
481
482 if (word >= 0x0100 && word <= 0x024F)
483 return EASTEUROPE_CHARSET;
484
485 if (word >= 0x1E00 && word <= 0x1EFF)
486 return VIETNAMESE_CHARSET;
487
488 return ANSI_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700489}
490
dsinclairb9590102016-04-27 06:38:59 -0700491CPWL_DocFontMap::CPWL_DocFontMap(CFX_SystemHandler* pSystemHandler,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700492 CPDF_Document* pAttachedDoc)
493 : CPWL_FontMap(pSystemHandler), m_pAttachedDoc(pAttachedDoc) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700494
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495CPWL_DocFontMap::~CPWL_DocFontMap() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700496
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700497CPDF_Document* CPWL_DocFontMap::GetDocument() {
498 return m_pAttachedDoc;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700499}