blob: 5fb9c99c3abf9efacf77632dc2ac015a2437e73a [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
7#include "../../include/pdfwindow/PDFWindow.h"
8#include "../../include/pdfwindow/PWL_Wnd.h"
9#include "../../include/pdfwindow/PWL_FontMap.h"
10
Tom Sepez2f2ffec2015-07-23 14:42:09 -070011#define DEFAULT_FONT_NAME "Helvetica"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070012
13/* ------------------------------ CPWL_FontMap ------------------------------ */
14
Lei Zhang60f507b2015-06-13 00:41:00 -070015CPWL_FontMap::CPWL_FontMap(IFX_SystemHandler* pSystemHandler) :
Tom Sepez2f2ffec2015-07-23 14:42:09 -070016 m_pPDFDoc(NULL),
17 m_pSystemHandler(pSystemHandler)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018{
Tom Sepez2f2ffec2015-07-23 14:42:09 -070019 ASSERT(m_pSystemHandler != NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020}
21
22CPWL_FontMap::~CPWL_FontMap()
23{
Lei Zhang796c9082015-07-16 18:45:22 -070024 delete m_pPDFDoc;
25 m_pPDFDoc = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
Lei Zhang796c9082015-07-16 18:45:22 -070027 Empty();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028}
29
30void CPWL_FontMap::SetSystemHandler(IFX_SystemHandler* pSystemHandler)
31{
Tom Sepez2f2ffec2015-07-23 14:42:09 -070032 m_pSystemHandler = pSystemHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033}
34
35CPDF_Document* CPWL_FontMap::GetDocument()
36{
37 if (!m_pPDFDoc)
38 {
39 if (CPDF_ModuleMgr::Get())
40 {
41 m_pPDFDoc = FX_NEW CPDF_Document;
42 m_pPDFDoc->CreateNewDoc();
43 }
44 }
45
Tom Sepez2f2ffec2015-07-23 14:42:09 -070046 return m_pPDFDoc;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047}
48
Tom Sepezbfa9a822015-06-09 13:24:12 -070049CPDF_Font* CPWL_FontMap::GetPDFFont(int32_t nFontIndex)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050{
Tom Sepez2f2ffec2015-07-23 14:42:09 -070051 if (nFontIndex >=0 && nFontIndex < m_aData.GetSize())
52 {
53 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex))
54 {
55 return pData->pFont;
56 }
57 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070058
Tom Sepez2f2ffec2015-07-23 14:42:09 -070059 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060}
61
Tom Sepezbfa9a822015-06-09 13:24:12 -070062CFX_ByteString CPWL_FontMap::GetPDFFontAlias(int32_t nFontIndex)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063{
Tom Sepez2f2ffec2015-07-23 14:42:09 -070064 if (nFontIndex >=0 && nFontIndex < m_aData.GetSize())
65 {
66 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex))
67 {
68 return pData->sFontName;
69 }
70 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070071
Tom Sepez2f2ffec2015-07-23 14:42:09 -070072 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070073}
74
Tom Sepezbfa9a822015-06-09 13:24:12 -070075FX_BOOL CPWL_FontMap::KnowWord(int32_t nFontIndex, FX_WORD word)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076{
Tom Sepez2f2ffec2015-07-23 14:42:09 -070077 if (nFontIndex >=0 && nFontIndex < m_aData.GetSize())
78 {
79 if (m_aData.GetAt(nFontIndex))
80 {
81 return CharCodeFromUnicode(nFontIndex, word) >= 0;
82 }
83 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084
Tom Sepez2f2ffec2015-07-23 14:42:09 -070085 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086}
87
Tom Sepezbfa9a822015-06-09 13:24:12 -070088int32_t CPWL_FontMap::GetWordFontIndex(FX_WORD word, int32_t nCharset, int32_t nFontIndex)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089{
Tom Sepez2f2ffec2015-07-23 14:42:09 -070090 if (nFontIndex > 0)
91 {
92 if (KnowWord(nFontIndex, word))
93 return nFontIndex;
94 }
95 else
96 {
97 if (const CPWL_FontMap_Data* pData = GetFontMapData(0))
98 {
99 if (nCharset == DEFAULT_CHARSET ||
100 pData->nCharset == SYMBOL_CHARSET ||
101 nCharset == pData->nCharset)
102 {
103 if (KnowWord(0, word))
104 return 0;
105 }
106 }
107 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700109 int32_t nNewFontIndex = GetFontIndex(GetNativeFontName(nCharset), nCharset, TRUE);
110 if (nNewFontIndex >= 0)
111 {
112 if (KnowWord(nNewFontIndex, word))
113 return nNewFontIndex;
114 }
115 nNewFontIndex = GetFontIndex("Arial Unicode MS", DEFAULT_CHARSET, FALSE);
116 if (nNewFontIndex >= 0)
117 {
118 if (KnowWord(nNewFontIndex, word))
119 return nNewFontIndex;
120 }
121 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700122}
123
Tom Sepezbfa9a822015-06-09 13:24:12 -0700124int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, FX_WORD word)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700126 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex))
127 {
128 if (pData->pFont)
129 {
130 if (pData->pFont->IsUnicodeCompatible())
131 {
132 int nCharCode = pData->pFont->CharCodeFromUnicode(word);
133 pData->pFont->GlyphFromCharCode(nCharCode);
134 return nCharCode;
135 }
136 if (word < 0xFF)
137 return word;
138 }
139 }
140 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141}
142
Tom Sepezbfa9a822015-06-09 13:24:12 -0700143CFX_ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700145 //searching native font is slow, so we must save time
146 for (int32_t i=0,sz=m_aNativeFont.GetSize(); i<sz; i++)
147 {
148 if (CPWL_FontMap_Native* pData = m_aNativeFont.GetAt(i))
149 {
150 if (pData->nCharset == nCharset)
151 return pData->sFontName;
152 }
153 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700155 CFX_ByteString sNew = GetNativeFont(nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700156
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700157 if (!sNew.IsEmpty())
158 {
159 CPWL_FontMap_Native* pNewData = new CPWL_FontMap_Native;
160 pNewData->nCharset = nCharset;
161 pNewData->sFontName = sNew;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700163 m_aNativeFont.Add(pNewData);
164 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700165
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700166 return sNew;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700167}
168
169void CPWL_FontMap::Empty()
170{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700171 {
172 for (int32_t i=0, sz=m_aData.GetSize(); i<sz; i++)
173 delete m_aData.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700175 m_aData.RemoveAll();
176 }
177 {
178 for (int32_t i=0, sz=m_aNativeFont.GetSize(); i<sz; i++)
179 delete m_aNativeFont.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700181 m_aNativeFont.RemoveAll();
182 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183}
184
Tom Sepezd7e5cc72015-06-10 14:33:37 -0700185void CPWL_FontMap::Initial(const FX_CHAR* fontname)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700186{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700187 CFX_ByteString sFontName = fontname;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700188
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700189 if (sFontName.IsEmpty())
190 sFontName = DEFAULT_FONT_NAME;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700192 GetFontIndex(sFontName, ANSI_CHARSET, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700193}
Lei Zhang60f507b2015-06-13 00:41:00 -0700194
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195
196/*
197List of currently supported standard fonts:
198Courier, Courier-Bold, Courier-BoldOblique, Courier-Oblique
199Helvetica, Helvetica-Bold, Helvetica-BoldOblique, Helvetica-Oblique
200Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic
201Symbol, ZapfDingbats
202*/
203
204const char* g_sDEStandardFontName[] = {"Courier", "Courier-Bold", "Courier-BoldOblique", "Courier-Oblique",
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700205 "Helvetica", "Helvetica-Bold", "Helvetica-BoldOblique", "Helvetica-Oblique",
206 "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic",
207 "Symbol", "ZapfDingbats"};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700208
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700209FX_BOOL CPWL_FontMap::IsStandardFont(const CFX_ByteString& sFontName)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700210{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700211 for (int32_t i=0; i<14; i++)
212 {
213 if (sFontName == g_sDEStandardFontName[i])
214 return TRUE;
215 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700216
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700217 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700218}
219
Tom Sepezbfa9a822015-06-09 13:24:12 -0700220int32_t CPWL_FontMap::FindFont(const CFX_ByteString& sFontName, int32_t nCharset)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700221{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700222 for (int32_t i=0,sz=m_aData.GetSize(); i<sz; i++)
223 {
224 if (CPWL_FontMap_Data* pData = m_aData.GetAt(i))
225 {
226 if (nCharset == DEFAULT_CHARSET || nCharset == pData->nCharset)
227 {
228 if (sFontName.IsEmpty() || pData->sFontName == sFontName)
229 return i;
230 }
231 }
232 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700234 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700235}
236
Tom Sepezbfa9a822015-06-09 13:24:12 -0700237int32_t CPWL_FontMap::GetFontIndex(const CFX_ByteString& sFontName, int32_t nCharset, FX_BOOL bFind)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700238{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700239 int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset);
240 if (nFontIndex >= 0)
241 return nFontIndex;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700242
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700243 CFX_ByteString sAlias;
244 CPDF_Font* pFont = NULL;
245 if (bFind)
246 pFont = FindFontSameCharset(sAlias, nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700248 if (!pFont)
249 {
250 CFX_ByteString sTemp = sFontName;
251 pFont = AddFontToDocument(GetDocument(), sTemp, nCharset);
252 sAlias = EncodeFontAlias(sTemp, nCharset);
253 }
254 AddedFont(pFont, sAlias);
255 return AddFontData(pFont, sAlias, nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256}
257
Tom Sepezbfa9a822015-06-09 13:24:12 -0700258int32_t CPWL_FontMap::GetPWLFontIndex(FX_WORD word, int32_t nCharset)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700260 int32_t nFind = -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700261
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700262 for (int32_t i=0,sz=m_aData.GetSize(); i<sz; i++)
263 {
264 if (CPWL_FontMap_Data* pData = m_aData.GetAt(i))
265 {
266 if (pData->nCharset == nCharset)
267 {
268 nFind = i;
269 break;
270 }
271 }
272 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700273
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700274 CPDF_Font* pNewFont = GetPDFFont(nFind);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700276 if (!pNewFont) return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700277
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700278 /*
279 if (CPDF_Font* pFont = GetPDFFont(nFind))
280 {
281 PWLFont.AddWordToFontDict(pFontDict, word);
282 }
283 */
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700284
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700285 CFX_ByteString sAlias = EncodeFontAlias("Arial_Chrome", nCharset);
286 AddedFont(pNewFont, sAlias);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700288 return AddFontData(pNewFont, sAlias, nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700289}
290
Tom Sepezbfa9a822015-06-09 13:24:12 -0700291CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias, int32_t nCharset)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700292{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700293 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700294}
295
Tom Sepezbfa9a822015-06-09 13:24:12 -0700296int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont, const CFX_ByteString& sFontAlias, int32_t nCharset)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700297{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700298 CPWL_FontMap_Data* pNewData = new CPWL_FontMap_Data;
299 pNewData->pFont = pFont;
300 pNewData->sFontName = sFontAlias;
301 pNewData->nCharset = nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700302
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700303 m_aData.Add(pNewData);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700304
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700305 return m_aData.GetSize() -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700306}
307
308void CPWL_FontMap::AddedFont(CPDF_Font* pFont, const CFX_ByteString& sFontAlias)
309{
310}
311
Tom Sepezbfa9a822015-06-09 13:24:12 -0700312CFX_ByteString CPWL_FontMap::GetFontName(int32_t nFontIndex)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700313{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700314 if (nFontIndex >=0 && nFontIndex < m_aData.GetSize())
315 {
316 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex))
317 {
318 return pData->sFontName;
319 }
320 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700321
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700322 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700323}
324
Tom Sepezbfa9a822015-06-09 13:24:12 -0700325CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700326{
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700327
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700328 if (nCharset == DEFAULT_CHARSET)
329 nCharset = GetNativeCharset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700330
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700331 CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset);
332 if (m_pSystemHandler)
333 {
334 if (m_pSystemHandler->FindNativeTrueTypeFont(nCharset, sFontName))
335 return sFontName;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700336
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700337 sFontName = m_pSystemHandler->GetNativeTrueTypeFont(nCharset);
338 }
339 return sFontName;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700340}
341
Tom Sepezbfa9a822015-06-09 13:24:12 -0700342CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc, CFX_ByteString& sFontName, uint8_t nCharset)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700343{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700344 if (IsStandardFont(sFontName))
345 return AddStandardFont(pDoc, sFontName);
346
347 return AddSystemFont(pDoc, sFontName, nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700348}
349
350CPDF_Font* CPWL_FontMap::AddStandardFont(CPDF_Document* pDoc, CFX_ByteString& sFontName)
351{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700352 if (!pDoc) return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700353
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700354 CPDF_Font* pFont = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700355
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700356 if (sFontName == "ZapfDingbats")
357 pFont = pDoc->AddStandardFont(sFontName, NULL);
358 else
359 {
360 CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI);
361 pFont = pDoc->AddStandardFont(sFontName, &fe);
362 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700363
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700364 return pFont;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700365}
366
Tom Sepezbfa9a822015-06-09 13:24:12 -0700367CPDF_Font* CPWL_FontMap::AddSystemFont(CPDF_Document* pDoc, CFX_ByteString& sFontName, uint8_t nCharset)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700368{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700369 if (!pDoc) return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700370
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700371 if (sFontName.IsEmpty()) sFontName = GetNativeFont(nCharset);
372 if (nCharset == DEFAULT_CHARSET) nCharset = GetNativeCharset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700373
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700374 if (m_pSystemHandler)
375 return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName, nCharset);
Lei Zhang60f507b2015-06-13 00:41:00 -0700376
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700377 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700378}
379
Tom Sepezbfa9a822015-06-09 13:24:12 -0700380CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName, int32_t nCharset)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700381{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700382 CFX_ByteString sPostfix;
383 sPostfix.Format("_%02X", nCharset);
384 return EncodeFontAlias(sFontName) + sPostfix;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700385}
386
387CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName)
388{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700389 CFX_ByteString sRet = sFontName;
390 sRet.Remove(' ');
391 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700392}
393
Tom Sepezbfa9a822015-06-09 13:24:12 -0700394int32_t CPWL_FontMap::GetFontMapCount() const
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700395{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700396 return m_aData.GetSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700397}
398
Tom Sepezbfa9a822015-06-09 13:24:12 -0700399const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700400{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700401 if (nIndex >=0 && nIndex < m_aData.GetSize())
402 {
403 return m_aData.GetAt(nIndex);
404 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700405
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700406 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700407}
408
Tom Sepezbfa9a822015-06-09 13:24:12 -0700409int32_t CPWL_FontMap::GetNativeCharset()
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700410{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700411 uint8_t nCharset = ANSI_CHARSET;
412 int32_t iCodePage = FXSYS_GetACP();
413 switch (iCodePage)
414 {
415 case 932://Japan
416 nCharset = SHIFTJIS_CHARSET;
417 break;
418 case 936://Chinese (PRC, Singapore)
419 nCharset = GB2312_CHARSET;
420 break;
421 case 950://Chinese (Taiwan; Hong Kong SAR, PRC)
422 nCharset = GB2312_CHARSET;
423 break;
424 case 1252://Windows 3.1 Latin 1 (US, Western Europe)
425 nCharset = ANSI_CHARSET;
426 break;
427 case 874://Thai
428 nCharset = THAI_CHARSET;
429 break;
430 case 949://Korean
431 nCharset = HANGUL_CHARSET;
432 break;
433 case 1200://Unicode (BMP of ISO 10646)
434 nCharset = ANSI_CHARSET;
435 break;
436 case 1250://Windows 3.1 Eastern European
437 nCharset = EASTEUROPE_CHARSET;
438 break;
439 case 1251://Windows 3.1 Cyrillic
440 nCharset = RUSSIAN_CHARSET;
441 break;
442 case 1253://Windows 3.1 Greek
443 nCharset = GREEK_CHARSET;
444 break;
445 case 1254://Windows 3.1 Turkish
446 nCharset = TURKISH_CHARSET;
447 break;
448 case 1255://Hebrew
449 nCharset = HEBREW_CHARSET;
450 break;
451 case 1256://Arabic
452 nCharset = ARABIC_CHARSET;
453 break;
454 case 1257://Baltic
455 nCharset = BALTIC_CHARSET;
456 break;
457 case 1258://Vietnamese
458 nCharset = VIETNAMESE_CHARSET;
459 break;
460 case 1361://Korean(Johab)
461 nCharset = JOHAB_CHARSET;
462 break;
463 }
464 return nCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700465}
466
467const CPWL_FontMap::CharsetFontMap CPWL_FontMap::defaultTTFMap[] = {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700468 { ANSI_CHARSET, "Helvetica" },
469 { GB2312_CHARSET, "SimSun" },
470 { CHINESEBIG5_CHARSET, "MingLiU" },
471 { SHIFTJIS_CHARSET, "MS Gothic" },
472 { HANGUL_CHARSET, "Batang" },
473 { RUSSIAN_CHARSET, "Arial" },
Bo Xudbd4c062014-05-29 11:32:56 -0700474#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700475 { EASTEUROPE_CHARSET, "Arial" },
Bo Xudbd4c062014-05-29 11:32:56 -0700476#else
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700477 { EASTEUROPE_CHARSET, "Tahoma" },
Bo Xudbd4c062014-05-29 11:32:56 -0700478#endif
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700479 { ARABIC_CHARSET, "Arial" },
480 { -1, NULL }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700481};
482
Tom Sepezbfa9a822015-06-09 13:24:12 -0700483CFX_ByteString CPWL_FontMap::GetDefaultFontByCharset(int32_t nCharset)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700484{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700485 int i = 0;
486 while (defaultTTFMap[i].charset != -1) {
487 if (nCharset == defaultTTFMap[i].charset)
488 return defaultTTFMap[i].fontname;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700489 ++i;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700490 }
491 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700492}
493
Tom Sepezbfa9a822015-06-09 13:24:12 -0700494int32_t CPWL_FontMap::CharSetFromUnicode(FX_WORD word, int32_t nOldCharset)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700495{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700496 if(m_pSystemHandler && (-1 != m_pSystemHandler->GetCharSet()))
497 return m_pSystemHandler->GetCharSet();
498 //to avoid CJK Font to show ASCII
499 if (word < 0x7F) return ANSI_CHARSET;
500 //follow the old charset
501 if (nOldCharset != DEFAULT_CHARSET) return nOldCharset;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700502
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700503 //find new charset
504 if ((word >= 0x4E00 && word <= 0x9FA5) ||
505 (word >= 0xE7C7 && word <= 0xE7F3) ||
506 (word >= 0x3000 && word <= 0x303F) ||
507 (word >= 0x2000 && word <= 0x206F))
508 {
509 return GB2312_CHARSET;
510 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700511
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700512 if (((word >= 0x3040) && (word <= 0x309F)) ||
513 ((word >= 0x30A0) && (word <= 0x30FF)) ||
514 ((word >= 0x31F0) && (word <= 0x31FF)) ||
515 ((word >= 0xFF00) && (word <= 0xFFEF)) )
516 {
517 return SHIFTJIS_CHARSET;
518 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700519
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700520 if (((word >= 0xAC00) && (word <= 0xD7AF)) ||
521 ((word >= 0x1100) && (word <= 0x11FF)) ||
522 ((word >= 0x3130) && (word <= 0x318F)))
523 {
524 return HANGUL_CHARSET;
525 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700526
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700527 if (word >= 0x0E00 && word <= 0x0E7F)
528 return THAI_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700529
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700530 if ((word >= 0x0370 && word <= 0x03FF) ||
531 (word >= 0x1F00 && word <= 0x1FFF))
532 return GREEK_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700533
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700534 if ((word >= 0x0600 && word <= 0x06FF) ||
535 (word >= 0xFB50 && word <= 0xFEFC))
536 return ARABIC_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700537
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700538 if (word >= 0x0590 && word <= 0x05FF)
539 return HEBREW_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700540
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700541 if (word >= 0x0400 && word <= 0x04FF)
542 return RUSSIAN_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700543
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700544 if (word >= 0x0100 && word <= 0x024F)
545 return EASTEUROPE_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700546
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700547 if (word >= 0x1E00 && word <= 0x1EFF)
548 return VIETNAMESE_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700549
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700550 return ANSI_CHARSET;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700551}
552
553/* ------------------------ CPWL_DocFontMap ------------------------ */
554
Lei Zhang60f507b2015-06-13 00:41:00 -0700555CPWL_DocFontMap::CPWL_DocFontMap(IFX_SystemHandler* pSystemHandler, CPDF_Document* pAttachedDoc)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700556 : CPWL_FontMap(pSystemHandler),
557 m_pAttachedDoc(pAttachedDoc)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700558{
559}
560
561CPWL_DocFontMap::~CPWL_DocFontMap()
562{
563}
564
565CPDF_Document* CPWL_DocFontMap::GetDocument()
566{
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700567 return m_pAttachedDoc;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700568}