blob: 9a2962cd5675cbab62de16864130354b148dedda [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 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070058
weili2d5b0202016-08-03 11:06:49 -070059 return m_pPDFDoc.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060}
61
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062CPDF_Font* CPWL_FontMap::GetPDFFont(int32_t nFontIndex) {
tsepez6745f962017-01-04 10:09:45 -080063 if (nFontIndex >= 0 && nFontIndex < pdfium::CollectionSize<int32_t>(m_Data)) {
64 if (m_Data[nFontIndex])
65 return m_Data[nFontIndex]->pFont;
Nico Weber9d8ec5a2015-08-04 13:00:21 -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) {
tsepez6745f962017-01-04 10:09:45 -080071 if (nFontIndex >= 0 && nFontIndex < pdfium::CollectionSize<int32_t>(m_Data)) {
72 if (m_Data[nFontIndex])
73 return m_Data[nFontIndex]->sFontName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 }
tsepez6745f962017-01-04 10:09:45 -080075 return CFX_ByteString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076}
77
tsepez4cf55152016-11-02 14:37:54 -070078bool CPWL_FontMap::KnowWord(int32_t nFontIndex, uint16_t word) {
tsepez6745f962017-01-04 10:09:45 -080079 if (nFontIndex >= 0 && nFontIndex < pdfium::CollectionSize<int32_t>(m_Data)) {
80 if (m_Data[nFontIndex])
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 return CharCodeFromUnicode(nFontIndex, word) >= 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 }
tsepez4cf55152016-11-02 14:37:54 -070083 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084}
85
Tom Sepez62a70f92016-03-21 15:00:20 -070086int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 int32_t nCharset,
88 int32_t nFontIndex) {
89 if (nFontIndex > 0) {
90 if (KnowWord(nFontIndex, word))
91 return nFontIndex;
92 } else {
93 if (const CPWL_FontMap_Data* pData = GetFontMapData(0)) {
npmea3c3be2016-09-19 07:24:33 -070094 if (nCharset == FXFONT_DEFAULT_CHARSET ||
95 pData->nCharset == FXFONT_SYMBOL_CHARSET ||
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 nCharset == pData->nCharset) {
97 if (KnowWord(0, word))
98 return 0;
99 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700100 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 int32_t nNewFontIndex =
tsepez4cf55152016-11-02 14:37:54 -0700104 GetFontIndex(GetNativeFontName(nCharset), nCharset, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 if (nNewFontIndex >= 0) {
106 if (KnowWord(nNewFontIndex, word))
107 return nNewFontIndex;
108 }
npmea3c3be2016-09-19 07:24:33 -0700109 nNewFontIndex =
tsepez4cf55152016-11-02 14:37:54 -0700110 GetFontIndex("Arial Unicode MS", FXFONT_DEFAULT_CHARSET, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 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) {
tsepez6745f962017-01-04 10:09:45 -0800119 if (nFontIndex < 0 || nFontIndex >= pdfium::CollectionSize<int32_t>(m_Data))
thestig8ea3f512016-06-27 11:55:24 -0700120 return -1;
121
tsepez6745f962017-01-04 10:09:45 -0800122 CPWL_FontMap_Data* pData = m_Data[nFontIndex].get();
123 if (!pData || !pData->pFont)
thestig8ea3f512016-06-27 11:55:24 -0700124 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) {
tsepez6745f962017-01-04 10:09:45 -0800133 for (const auto& pData : m_NativeFont) {
134 if (pData && pData->nCharset == nCharset)
135 return pData->sFontName;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 CFX_ByteString sNew = GetNativeFont(nCharset);
tsepez6745f962017-01-04 10:09:45 -0800139 if (sNew.IsEmpty())
140 return CFX_ByteString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141
tsepez6745f962017-01-04 10:09:45 -0800142 auto pNewData = pdfium::MakeUnique<CPWL_FontMap_Native>();
143 pNewData->nCharset = nCharset;
144 pNewData->sFontName = sNew;
145 m_NativeFont.push_back(std::move(pNewData));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 return sNew;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700147}
148
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149void CPWL_FontMap::Empty() {
tsepez6745f962017-01-04 10:09:45 -0800150 m_Data.clear();
151 m_NativeFont.clear();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152}
153
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800154void CPWL_FontMap::Initialize() {
tsepez4cf55152016-11-02 14:37:54 -0700155 GetFontIndex(kDefaultFontName, FXFONT_ANSI_CHARSET, false);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700156}
Lei Zhang60f507b2015-06-13 00:41:00 -0700157
tsepez4cf55152016-11-02 14:37:54 -0700158bool CPWL_FontMap::IsStandardFont(const CFX_ByteString& sFontName) {
Wei Li89409932016-03-28 10:33:33 -0700159 for (size_t i = 0; i < FX_ArraySize(g_sDEStandardFontName); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 if (sFontName == g_sDEStandardFontName[i])
tsepez4cf55152016-11-02 14:37:54 -0700161 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700163
tsepez4cf55152016-11-02 14:37:54 -0700164 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700165}
166
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167int32_t CPWL_FontMap::FindFont(const CFX_ByteString& sFontName,
168 int32_t nCharset) {
tsepez6745f962017-01-04 10:09:45 -0800169 int32_t i = 0;
170 for (const auto& pData : m_Data) {
171 if (pData &&
172 (nCharset == FXFONT_DEFAULT_CHARSET || nCharset == pData->nCharset) &&
173 (sFontName.IsEmpty() || pData->sFontName == sFontName)) {
174 return i;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700175 }
tsepez6745f962017-01-04 10:09:45 -0800176 ++i;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 return -1;
179}
180
181int32_t CPWL_FontMap::GetFontIndex(const CFX_ByteString& sFontName,
182 int32_t nCharset,
tsepez4cf55152016-11-02 14:37:54 -0700183 bool bFind) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset);
185 if (nFontIndex >= 0)
186 return nFontIndex;
187
188 CFX_ByteString sAlias;
thestig1cd352e2016-06-07 17:53:06 -0700189 CPDF_Font* pFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 if (bFind)
191 pFont = FindFontSameCharset(sAlias, nCharset);
192
193 if (!pFont) {
194 CFX_ByteString sTemp = sFontName;
195 pFont = AddFontToDocument(GetDocument(), sTemp, nCharset);
196 sAlias = EncodeFontAlias(sTemp, nCharset);
197 }
198 AddedFont(pFont, sAlias);
199 return AddFontData(pFont, sAlias, nCharset);
200}
201
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias,
203 int32_t nCharset) {
thestig1cd352e2016-06-07 17:53:06 -0700204 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700205}
206
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont,
208 const CFX_ByteString& sFontAlias,
209 int32_t nCharset) {
tsepez6745f962017-01-04 10:09:45 -0800210 auto pNewData = pdfium::MakeUnique<CPWL_FontMap_Data>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 pNewData->pFont = pFont;
212 pNewData->sFontName = sFontAlias;
213 pNewData->nCharset = nCharset;
tsepez6745f962017-01-04 10:09:45 -0800214 m_Data.push_back(std::move(pNewData));
215 return pdfium::CollectionSize<int32_t>(m_Data) - 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700216}
217
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218void CPWL_FontMap::AddedFont(CPDF_Font* pFont,
219 const CFX_ByteString& sFontAlias) {}
220
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) {
npmea3c3be2016-09-19 07:24:33 -0700222 if (nCharset == FXFONT_DEFAULT_CHARSET)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 nCharset = GetNativeCharset();
224
225 CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset);
tsepez6745f962017-01-04 10:09:45 -0800226 if (!m_pSystemHandler->FindNativeTrueTypeFont(sFontName))
227 return CFX_ByteString();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 return sFontName;
230}
231
232CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc,
233 CFX_ByteString& sFontName,
234 uint8_t nCharset) {
235 if (IsStandardFont(sFontName))
236 return AddStandardFont(pDoc, sFontName);
237
238 return AddSystemFont(pDoc, sFontName, nCharset);
239}
240
241CPDF_Font* CPWL_FontMap::AddStandardFont(CPDF_Document* pDoc,
242 CFX_ByteString& sFontName) {
243 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700244 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245
thestig1cd352e2016-06-07 17:53:06 -0700246 CPDF_Font* pFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800248 if (sFontName == "ZapfDingbats") {
thestig1cd352e2016-06-07 17:53:06 -0700249 pFont = pDoc->AddStandardFont(sFontName.c_str(), nullptr);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800250 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI);
tsepezb4c9f3f2016-04-13 15:41:21 -0700252 pFont = pDoc->AddStandardFont(sFontName.c_str(), &fe);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 }
254
255 return pFont;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256}
257
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258CPDF_Font* CPWL_FontMap::AddSystemFont(CPDF_Document* pDoc,
259 CFX_ByteString& sFontName,
260 uint8_t nCharset) {
261 if (!pDoc)
thestig1cd352e2016-06-07 17:53:06 -0700262 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263
264 if (sFontName.IsEmpty())
265 sFontName = GetNativeFont(nCharset);
npmea3c3be2016-09-19 07:24:33 -0700266 if (nCharset == FXFONT_DEFAULT_CHARSET)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 nCharset = GetNativeCharset();
268
dsinclair0e3e8902016-08-24 11:39:24 -0700269 return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName,
270 nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271}
272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName,
274 int32_t nCharset) {
275 CFX_ByteString sPostfix;
276 sPostfix.Format("_%02X", nCharset);
277 return EncodeFontAlias(sFontName) + sPostfix;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278}
279
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) {
281 CFX_ByteString sRet = sFontName;
282 sRet.Remove(' ');
283 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700284}
285
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const {
tsepez6745f962017-01-04 10:09:45 -0800287 if (nIndex < 0 || nIndex >= pdfium::CollectionSize<int32_t>(m_Data))
288 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700289
tsepez6745f962017-01-04 10:09:45 -0800290 return m_Data[nIndex].get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291}
292
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293int32_t CPWL_FontMap::GetNativeCharset() {
npmea3c3be2016-09-19 07:24:33 -0700294 uint8_t nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 int32_t iCodePage = FXSYS_GetACP();
296 switch (iCodePage) {
297 case 932: // Japan
npmea3c3be2016-09-19 07:24:33 -0700298 nCharset = FXFONT_SHIFTJIS_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 break;
300 case 936: // Chinese (PRC, Singapore)
npmea3c3be2016-09-19 07:24:33 -0700301 nCharset = FXFONT_GB2312_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302 break;
303 case 950: // Chinese (Taiwan; Hong Kong SAR, PRC)
npmea3c3be2016-09-19 07:24:33 -0700304 nCharset = FXFONT_GB2312_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 break;
306 case 1252: // Windows 3.1 Latin 1 (US, Western Europe)
npmea3c3be2016-09-19 07:24:33 -0700307 nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 break;
309 case 874: // Thai
npmea3c3be2016-09-19 07:24:33 -0700310 nCharset = FXFONT_THAI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 break;
312 case 949: // Korean
npmea3c3be2016-09-19 07:24:33 -0700313 nCharset = FXFONT_HANGUL_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314 break;
315 case 1200: // Unicode (BMP of ISO 10646)
npmea3c3be2016-09-19 07:24:33 -0700316 nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 break;
318 case 1250: // Windows 3.1 Eastern European
npmea3c3be2016-09-19 07:24:33 -0700319 nCharset = FXFONT_EASTEUROPE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 break;
321 case 1251: // Windows 3.1 Cyrillic
npmea3c3be2016-09-19 07:24:33 -0700322 nCharset = FXFONT_RUSSIAN_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 break;
324 case 1253: // Windows 3.1 Greek
npmea3c3be2016-09-19 07:24:33 -0700325 nCharset = FXFONT_GREEK_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 break;
327 case 1254: // Windows 3.1 Turkish
npmea3c3be2016-09-19 07:24:33 -0700328 nCharset = FXFONT_TURKISH_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329 break;
330 case 1255: // Hebrew
npmea3c3be2016-09-19 07:24:33 -0700331 nCharset = FXFONT_HEBREW_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332 break;
333 case 1256: // Arabic
npmea3c3be2016-09-19 07:24:33 -0700334 nCharset = FXFONT_ARABIC_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 break;
336 case 1257: // Baltic
npmea3c3be2016-09-19 07:24:33 -0700337 nCharset = FXFONT_BALTIC_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338 break;
339 case 1258: // Vietnamese
npmea3c3be2016-09-19 07:24:33 -0700340 nCharset = FXFONT_VIETNAMESE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 break;
342 case 1361: // Korean(Johab)
npmea3c3be2016-09-19 07:24:33 -0700343 nCharset = FXFONT_JOHAB_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 break;
345 }
346 return nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700347}
348
weili2d5b0202016-08-03 11:06:49 -0700349const FPDF_CharsetFontMap CPWL_FontMap::defaultTTFMap[] = {
npmea3c3be2016-09-19 07:24:33 -0700350 {FXFONT_ANSI_CHARSET, "Helvetica"},
351 {FXFONT_GB2312_CHARSET, "SimSun"},
352 {FXFONT_CHINESEBIG5_CHARSET, "MingLiU"},
353 {FXFONT_SHIFTJIS_CHARSET, "MS Gothic"},
354 {FXFONT_HANGUL_CHARSET, "Batang"},
355 {FXFONT_RUSSIAN_CHARSET, "Arial"},
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \
357 _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
npmea3c3be2016-09-19 07:24:33 -0700358 {FXFONT_EASTEUROPE_CHARSET, "Arial"},
Bo Xudbd4c062014-05-29 11:32:56 -0700359#else
npmea3c3be2016-09-19 07:24:33 -0700360 {FXFONT_EASTEUROPE_CHARSET, "Tahoma"},
Bo Xudbd4c062014-05-29 11:32:56 -0700361#endif
npmea3c3be2016-09-19 07:24:33 -0700362 {FXFONT_ARABIC_CHARSET, "Arial"},
363 {-1, nullptr}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700364
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365CFX_ByteString CPWL_FontMap::GetDefaultFontByCharset(int32_t nCharset) {
366 int i = 0;
367 while (defaultTTFMap[i].charset != -1) {
368 if (nCharset == defaultTTFMap[i].charset)
369 return defaultTTFMap[i].fontname;
370 ++i;
371 }
372 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700373}
374
Tom Sepez62a70f92016-03-21 15:00:20 -0700375int32_t CPWL_FontMap::CharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 // to avoid CJK Font to show ASCII
377 if (word < 0x7F)
npmea3c3be2016-09-19 07:24:33 -0700378 return FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 // follow the old charset
npmea3c3be2016-09-19 07:24:33 -0700380 if (nOldCharset != FXFONT_DEFAULT_CHARSET)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 return nOldCharset;
382
383 // find new charset
384 if ((word >= 0x4E00 && word <= 0x9FA5) ||
385 (word >= 0xE7C7 && word <= 0xE7F3) ||
386 (word >= 0x3000 && word <= 0x303F) ||
387 (word >= 0x2000 && word <= 0x206F)) {
npmea3c3be2016-09-19 07:24:33 -0700388 return FXFONT_GB2312_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 }
390
391 if (((word >= 0x3040) && (word <= 0x309F)) ||
392 ((word >= 0x30A0) && (word <= 0x30FF)) ||
393 ((word >= 0x31F0) && (word <= 0x31FF)) ||
394 ((word >= 0xFF00) && (word <= 0xFFEF))) {
npmea3c3be2016-09-19 07:24:33 -0700395 return FXFONT_SHIFTJIS_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 }
397
398 if (((word >= 0xAC00) && (word <= 0xD7AF)) ||
399 ((word >= 0x1100) && (word <= 0x11FF)) ||
400 ((word >= 0x3130) && (word <= 0x318F))) {
npmea3c3be2016-09-19 07:24:33 -0700401 return FXFONT_HANGUL_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402 }
403
404 if (word >= 0x0E00 && word <= 0x0E7F)
npmea3c3be2016-09-19 07:24:33 -0700405 return FXFONT_THAI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406
407 if ((word >= 0x0370 && word <= 0x03FF) || (word >= 0x1F00 && word <= 0x1FFF))
npmea3c3be2016-09-19 07:24:33 -0700408 return FXFONT_GREEK_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409
410 if ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC))
npmea3c3be2016-09-19 07:24:33 -0700411 return FXFONT_ARABIC_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412
413 if (word >= 0x0590 && word <= 0x05FF)
npmea3c3be2016-09-19 07:24:33 -0700414 return FXFONT_HEBREW_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415
416 if (word >= 0x0400 && word <= 0x04FF)
npmea3c3be2016-09-19 07:24:33 -0700417 return FXFONT_RUSSIAN_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418
419 if (word >= 0x0100 && word <= 0x024F)
npmea3c3be2016-09-19 07:24:33 -0700420 return FXFONT_EASTEUROPE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421
422 if (word >= 0x1E00 && word <= 0x1EFF)
npmea3c3be2016-09-19 07:24:33 -0700423 return FXFONT_VIETNAMESE_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424
npmea3c3be2016-09-19 07:24:33 -0700425 return FXFONT_ANSI_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700426}