blob: ddf496fbb0303e6b7f65fb6d22ea7dee93c2c14c [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
tsepez6745f962017-01-04 10:09:45 -08009#include <utility>
10
dsinclair39c62fd2016-09-29 12:49:17 -070011#include "core/fpdfapi/cpdf_modulemgr.h"
dsinclairbc5e6d22016-10-04 11:08:49 -070012#include "core/fpdfapi/font/cpdf_font.h"
13#include "core/fpdfapi/font/cpdf_fontencoding.h"
dsinclair488b7ad2016-10-04 11:55:50 -070014#include "core/fpdfapi/parser/cpdf_document.h"
15#include "core/fpdfapi/parser/cpdf_parser.h"
dsinclair1727aee2016-09-29 13:12:56 -070016#include "core/fpdfdoc/ipvt_fontmap.h"
dan sinclair89e904b2016-03-23 19:29:15 -040017#include "fpdfsdk/pdfwindow/PWL_Wnd.h"
tsepez36eb4bd2016-10-03 15:24:27 -070018#include "third_party/base/ptr_util.h"
tsepez6745f962017-01-04 10:09:45 -080019#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
Lei Zhang1b976642016-01-08 14:24:37 -080021namespace {
22
23const char kDefaultFontName[] = "Helvetica";
24
25const char* const g_sDEStandardFontName[] = {"Courier",
26 "Courier-Bold",
27 "Courier-BoldOblique",
28 "Courier-Oblique",
29 "Helvetica",
30 "Helvetica-Bold",
31 "Helvetica-BoldOblique",
32 "Helvetica-Oblique",
33 "Times-Roman",
34 "Times-Bold",
35 "Times-Italic",
36 "Times-BoldItalic",
37 "Symbol",
38 "ZapfDingbats"};
39
40} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041
dsinclairb9590102016-04-27 06:38:59 -070042CPWL_FontMap::CPWL_FontMap(CFX_SystemHandler* pSystemHandler)
weili2d5b0202016-08-03 11:06:49 -070043 : m_pSystemHandler(pSystemHandler) {
Lei Zhang96660d62015-12-14 18:27:25 -080044 ASSERT(m_pSystemHandler);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045}
46
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047CPWL_FontMap::~CPWL_FontMap() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 Empty();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070049}
50
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051CPDF_Document* CPWL_FontMap::GetDocument() {
52 if (!m_pPDFDoc) {
53 if (CPDF_ModuleMgr::Get()) {
tsepeze5cb0b12016-10-26 15:06:11 -070054 m_pPDFDoc = pdfium::MakeUnique<CPDF_Document>(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 m_pPDFDoc->CreateNewDoc();
56 }
57 }
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) {
Tom Sepez193e6ca2017-03-14 15:53:36 -070062 if (pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex])
63 return m_Data[nFontIndex]->pFont;
64
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) {
Tom Sepez193e6ca2017-03-14 15:53:36 -070069 if (pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex])
70 return m_Data[nFontIndex]->sFontName;
71
tsepez6745f962017-01-04 10:09:45 -080072 return CFX_ByteString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070073}
74
tsepez4cf55152016-11-02 14:37:54 -070075bool CPWL_FontMap::KnowWord(int32_t nFontIndex, uint16_t word) {
Tom Sepez193e6ca2017-03-14 15:53:36 -070076 return pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex] &&
77 CharCodeFromUnicode(nFontIndex, word) >= 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078}
79
Tom Sepez62a70f92016-03-21 15:00:20 -070080int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 int32_t nCharset,
82 int32_t nFontIndex) {
83 if (nFontIndex > 0) {
84 if (KnowWord(nFontIndex, word))
85 return nFontIndex;
86 } else {
87 if (const CPWL_FontMap_Data* pData = GetFontMapData(0)) {
npmea3c3be2016-09-19 07:24:33 -070088 if (nCharset == FXFONT_DEFAULT_CHARSET ||
89 pData->nCharset == FXFONT_SYMBOL_CHARSET ||
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 nCharset == pData->nCharset) {
91 if (KnowWord(0, word))
92 return 0;
93 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -070094 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 int32_t nNewFontIndex =
tsepez4cf55152016-11-02 14:37:54 -070098 GetFontIndex(GetNativeFontName(nCharset), nCharset, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 if (nNewFontIndex >= 0) {
100 if (KnowWord(nNewFontIndex, word))
101 return nNewFontIndex;
102 }
npmea3c3be2016-09-19 07:24:33 -0700103 nNewFontIndex =
tsepez4cf55152016-11-02 14:37:54 -0700104 GetFontIndex("Arial Unicode MS", FXFONT_DEFAULT_CHARSET, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 if (nNewFontIndex >= 0) {
106 if (KnowWord(nNewFontIndex, word))
107 return nNewFontIndex;
108 }
109 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700110}
111
Tom Sepez62a70f92016-03-21 15:00:20 -0700112int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) {
Tom Sepez193e6ca2017-03-14 15:53:36 -0700113 if (!pdfium::IndexInBounds(m_Data, nFontIndex))
thestig8ea3f512016-06-27 11:55:24 -0700114 return -1;
115
tsepez6745f962017-01-04 10:09:45 -0800116 CPWL_FontMap_Data* pData = m_Data[nFontIndex].get();
117 if (!pData || !pData->pFont)
thestig8ea3f512016-06-27 11:55:24 -0700118 return -1;
119
120 if (pData->pFont->IsUnicodeCompatible())
121 return pData->pFont->CharCodeFromUnicode(word);
122
123 return word < 0xFF ? word : -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700124}
125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126CFX_ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset) {
tsepez6745f962017-01-04 10:09:45 -0800127 for (const auto& pData : m_NativeFont) {
128 if (pData && pData->nCharset == nCharset)
129 return pData->sFontName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 CFX_ByteString sNew = GetNativeFont(nCharset);
tsepez6745f962017-01-04 10:09:45 -0800133 if (sNew.IsEmpty())
134 return CFX_ByteString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700135
tsepez6745f962017-01-04 10:09:45 -0800136 auto pNewData = pdfium::MakeUnique<CPWL_FontMap_Native>();
137 pNewData->nCharset = nCharset;
138 pNewData->sFontName = sNew;
139 m_NativeFont.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 return sNew;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141}
142
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143void CPWL_FontMap::Empty() {
tsepez6745f962017-01-04 10:09:45 -0800144 m_Data.clear();
145 m_NativeFont.clear();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146}
147
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800148void CPWL_FontMap::Initialize() {
tsepez4cf55152016-11-02 14:37:54 -0700149 GetFontIndex(kDefaultFontName, FXFONT_ANSI_CHARSET, false);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700150}
Lei Zhang60f507b2015-06-13 00:41:00 -0700151
tsepez4cf55152016-11-02 14:37:54 -0700152bool CPWL_FontMap::IsStandardFont(const CFX_ByteString& sFontName) {
Wei Li89409932016-03-28 10:33:33 -0700153 for (size_t i = 0; i < FX_ArraySize(g_sDEStandardFontName); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 if (sFontName == g_sDEStandardFontName[i])
tsepez4cf55152016-11-02 14:37:54 -0700155 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700157
tsepez4cf55152016-11-02 14:37:54 -0700158 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159}
160
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161int32_t CPWL_FontMap::FindFont(const CFX_ByteString& sFontName,
162 int32_t nCharset) {
tsepez6745f962017-01-04 10:09:45 -0800163 int32_t i = 0;
164 for (const auto& pData : m_Data) {
165 if (pData &&
166 (nCharset == FXFONT_DEFAULT_CHARSET || nCharset == pData->nCharset) &&
167 (sFontName.IsEmpty() || pData->sFontName == sFontName)) {
168 return i;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700169 }
tsepez6745f962017-01-04 10:09:45 -0800170 ++i;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 return -1;
173}
174
175int32_t CPWL_FontMap::GetFontIndex(const CFX_ByteString& sFontName,
176 int32_t nCharset,
tsepez4cf55152016-11-02 14:37:54 -0700177 bool bFind) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset);
179 if (nFontIndex >= 0)
180 return nFontIndex;
181
182 CFX_ByteString sAlias;
Tom Sepezec8ff7d2017-04-07 16:58:00 -0700183 CPDF_Font* pFont = bFind ? FindFontSameCharset(&sAlias, nCharset) : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 if (!pFont) {
185 CFX_ByteString sTemp = sFontName;
186 pFont = AddFontToDocument(GetDocument(), sTemp, nCharset);
187 sAlias = EncodeFontAlias(sTemp, nCharset);
188 }
189 AddedFont(pFont, sAlias);
190 return AddFontData(pFont, sAlias, nCharset);
191}
192
Tom Sepezec8ff7d2017-04-07 16:58:00 -0700193CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString* sFontAlias,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194 int32_t nCharset) {
thestig1cd352e2016-06-07 17:53:06 -0700195 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700196}
197
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont,
199 const CFX_ByteString& sFontAlias,
200 int32_t nCharset) {
tsepez6745f962017-01-04 10:09:45 -0800201 auto pNewData = pdfium::MakeUnique<CPWL_FontMap_Data>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 pNewData->pFont = pFont;
203 pNewData->sFontName = sFontAlias;
204 pNewData->nCharset = nCharset;
tsepez6745f962017-01-04 10:09:45 -0800205 m_Data.push_back(std::move(pNewData));
206 return pdfium::CollectionSize<int32_t>(m_Data) - 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207}
208
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209void CPWL_FontMap::AddedFont(CPDF_Font* pFont,
210 const CFX_ByteString& sFontAlias) {}
211
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) {
npmea3c3be2016-09-19 07:24:33 -0700213 if (nCharset == FXFONT_DEFAULT_CHARSET)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 nCharset = GetNativeCharset();
215
216 CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset);
tsepez6745f962017-01-04 10:09:45 -0800217 if (!m_pSystemHandler->FindNativeTrueTypeFont(sFontName))
218 return CFX_ByteString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 return sFontName;
221}
222
223CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc,
224 CFX_ByteString& sFontName,
225 uint8_t nCharset) {
226 if (IsStandardFont(sFontName))
227 return AddStandardFont(pDoc, sFontName);
228
229 return AddSystemFont(pDoc, sFontName, nCharset);
230}
231
232CPDF_Font* CPWL_FontMap::AddStandardFont(CPDF_Document* pDoc,
233 CFX_ByteString& sFontName) {
234 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700235 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236
thestig1cd352e2016-06-07 17:53:06 -0700237 CPDF_Font* pFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800239 if (sFontName == "ZapfDingbats") {
thestig1cd352e2016-06-07 17:53:06 -0700240 pFont = pDoc->AddStandardFont(sFontName.c_str(), nullptr);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800241 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI);
tsepezb4c9f3f2016-04-13 15:41:21 -0700243 pFont = pDoc->AddStandardFont(sFontName.c_str(), &fe);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 }
245
246 return pFont;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247}
248
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249CPDF_Font* CPWL_FontMap::AddSystemFont(CPDF_Document* pDoc,
250 CFX_ByteString& sFontName,
251 uint8_t nCharset) {
252 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700253 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254
255 if (sFontName.IsEmpty())
256 sFontName = GetNativeFont(nCharset);
npmea3c3be2016-09-19 07:24:33 -0700257 if (nCharset == FXFONT_DEFAULT_CHARSET)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 nCharset = GetNativeCharset();
259
dsinclair0e3e8902016-08-24 11:39:24 -0700260 return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName,
261 nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262}
263
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName,
265 int32_t nCharset) {
266 CFX_ByteString sPostfix;
267 sPostfix.Format("_%02X", nCharset);
268 return EncodeFontAlias(sFontName) + sPostfix;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700269}
270
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) {
272 CFX_ByteString sRet = sFontName;
273 sRet.Remove(' ');
274 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275}
276
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const {
Tom Sepez193e6ca2017-03-14 15:53:36 -0700278 return pdfium::IndexInBounds(m_Data, nIndex) ? m_Data[nIndex].get() : nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279}
280
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281int32_t CPWL_FontMap::GetNativeCharset() {
npmea3c3be2016-09-19 07:24:33 -0700282 uint8_t nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 int32_t iCodePage = FXSYS_GetACP();
284 switch (iCodePage) {
285 case 932: // Japan
npmea3c3be2016-09-19 07:24:33 -0700286 nCharset = FXFONT_SHIFTJIS_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 break;
288 case 936: // Chinese (PRC, Singapore)
npmea3c3be2016-09-19 07:24:33 -0700289 nCharset = FXFONT_GB2312_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 break;
291 case 950: // Chinese (Taiwan; Hong Kong SAR, PRC)
npmea3c3be2016-09-19 07:24:33 -0700292 nCharset = FXFONT_GB2312_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 break;
294 case 1252: // Windows 3.1 Latin 1 (US, Western Europe)
npmea3c3be2016-09-19 07:24:33 -0700295 nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296 break;
297 case 874: // Thai
npmea3c3be2016-09-19 07:24:33 -0700298 nCharset = FXFONT_THAI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 break;
300 case 949: // Korean
npmea3c3be2016-09-19 07:24:33 -0700301 nCharset = FXFONT_HANGUL_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302 break;
303 case 1200: // Unicode (BMP of ISO 10646)
npmea3c3be2016-09-19 07:24:33 -0700304 nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 break;
306 case 1250: // Windows 3.1 Eastern European
npmea3c3be2016-09-19 07:24:33 -0700307 nCharset = FXFONT_EASTEUROPE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 break;
309 case 1251: // Windows 3.1 Cyrillic
npmea3c3be2016-09-19 07:24:33 -0700310 nCharset = FXFONT_RUSSIAN_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 break;
312 case 1253: // Windows 3.1 Greek
npmea3c3be2016-09-19 07:24:33 -0700313 nCharset = FXFONT_GREEK_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314 break;
315 case 1254: // Windows 3.1 Turkish
npmea3c3be2016-09-19 07:24:33 -0700316 nCharset = FXFONT_TURKISH_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 break;
318 case 1255: // Hebrew
npmea3c3be2016-09-19 07:24:33 -0700319 nCharset = FXFONT_HEBREW_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 break;
321 case 1256: // Arabic
npmea3c3be2016-09-19 07:24:33 -0700322 nCharset = FXFONT_ARABIC_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 break;
324 case 1257: // Baltic
npmea3c3be2016-09-19 07:24:33 -0700325 nCharset = FXFONT_BALTIC_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 break;
327 case 1258: // Vietnamese
npmea3c3be2016-09-19 07:24:33 -0700328 nCharset = FXFONT_VIETNAMESE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329 break;
330 case 1361: // Korean(Johab)
npmea3c3be2016-09-19 07:24:33 -0700331 nCharset = FXFONT_JOHAB_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332 break;
333 }
334 return nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700335}
336
weili2d5b0202016-08-03 11:06:49 -0700337const FPDF_CharsetFontMap CPWL_FontMap::defaultTTFMap[] = {
npmea3c3be2016-09-19 07:24:33 -0700338 {FXFONT_ANSI_CHARSET, "Helvetica"},
339 {FXFONT_GB2312_CHARSET, "SimSun"},
340 {FXFONT_CHINESEBIG5_CHARSET, "MingLiU"},
341 {FXFONT_SHIFTJIS_CHARSET, "MS Gothic"},
342 {FXFONT_HANGUL_CHARSET, "Batang"},
343 {FXFONT_RUSSIAN_CHARSET, "Arial"},
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \
345 _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
npmea3c3be2016-09-19 07:24:33 -0700346 {FXFONT_EASTEUROPE_CHARSET, "Arial"},
Bo Xudbd4c062014-05-29 11:32:56 -0700347#else
npmea3c3be2016-09-19 07:24:33 -0700348 {FXFONT_EASTEUROPE_CHARSET, "Tahoma"},
Bo Xudbd4c062014-05-29 11:32:56 -0700349#endif
npmea3c3be2016-09-19 07:24:33 -0700350 {FXFONT_ARABIC_CHARSET, "Arial"},
351 {-1, nullptr}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353CFX_ByteString CPWL_FontMap::GetDefaultFontByCharset(int32_t nCharset) {
354 int i = 0;
355 while (defaultTTFMap[i].charset != -1) {
356 if (nCharset == defaultTTFMap[i].charset)
357 return defaultTTFMap[i].fontname;
358 ++i;
359 }
360 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700361}
362
Tom Sepez62a70f92016-03-21 15:00:20 -0700363int32_t CPWL_FontMap::CharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364 // to avoid CJK Font to show ASCII
365 if (word < 0x7F)
npmea3c3be2016-09-19 07:24:33 -0700366 return FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 // follow the old charset
npmea3c3be2016-09-19 07:24:33 -0700368 if (nOldCharset != FXFONT_DEFAULT_CHARSET)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 return nOldCharset;
370
371 // find new charset
372 if ((word >= 0x4E00 && word <= 0x9FA5) ||
373 (word >= 0xE7C7 && word <= 0xE7F3) ||
374 (word >= 0x3000 && word <= 0x303F) ||
375 (word >= 0x2000 && word <= 0x206F)) {
npmea3c3be2016-09-19 07:24:33 -0700376 return FXFONT_GB2312_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377 }
378
379 if (((word >= 0x3040) && (word <= 0x309F)) ||
380 ((word >= 0x30A0) && (word <= 0x30FF)) ||
381 ((word >= 0x31F0) && (word <= 0x31FF)) ||
382 ((word >= 0xFF00) && (word <= 0xFFEF))) {
npmea3c3be2016-09-19 07:24:33 -0700383 return FXFONT_SHIFTJIS_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384 }
385
386 if (((word >= 0xAC00) && (word <= 0xD7AF)) ||
387 ((word >= 0x1100) && (word <= 0x11FF)) ||
388 ((word >= 0x3130) && (word <= 0x318F))) {
npmea3c3be2016-09-19 07:24:33 -0700389 return FXFONT_HANGUL_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 }
391
392 if (word >= 0x0E00 && word <= 0x0E7F)
npmea3c3be2016-09-19 07:24:33 -0700393 return FXFONT_THAI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394
395 if ((word >= 0x0370 && word <= 0x03FF) || (word >= 0x1F00 && word <= 0x1FFF))
npmea3c3be2016-09-19 07:24:33 -0700396 return FXFONT_GREEK_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397
398 if ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC))
npmea3c3be2016-09-19 07:24:33 -0700399 return FXFONT_ARABIC_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400
401 if (word >= 0x0590 && word <= 0x05FF)
npmea3c3be2016-09-19 07:24:33 -0700402 return FXFONT_HEBREW_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403
404 if (word >= 0x0400 && word <= 0x04FF)
npmea3c3be2016-09-19 07:24:33 -0700405 return FXFONT_RUSSIAN_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406
407 if (word >= 0x0100 && word <= 0x024F)
npmea3c3be2016-09-19 07:24:33 -0700408 return FXFONT_EASTEUROPE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409
410 if (word >= 0x1E00 && word <= 0x1EFF)
npmea3c3be2016-09-19 07:24:33 -0700411 return FXFONT_VIETNAMESE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412
npmea3c3be2016-09-19 07:24:33 -0700413 return FXFONT_ANSI_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700414}