| OLD | NEW |
| (Empty) |
| 1 // 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. | |
| 4 | |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
| 6 | |
| 7 #include "fpdfsdk/include/pdfwindow/PWL_FontMap.h" | |
| 8 | |
| 9 #include "core/include/fpdfapi/cpdf_document.h" | |
| 10 #include "core/include/fpdfapi/fpdf_module.h" | |
| 11 #include "fpdfsdk/include/pdfwindow/PWL_Wnd.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 const char kDefaultFontName[] = "Helvetica"; | |
| 16 | |
| 17 const char* const g_sDEStandardFontName[] = {"Courier", | |
| 18 "Courier-Bold", | |
| 19 "Courier-BoldOblique", | |
| 20 "Courier-Oblique", | |
| 21 "Helvetica", | |
| 22 "Helvetica-Bold", | |
| 23 "Helvetica-BoldOblique", | |
| 24 "Helvetica-Oblique", | |
| 25 "Times-Roman", | |
| 26 "Times-Bold", | |
| 27 "Times-Italic", | |
| 28 "Times-BoldItalic", | |
| 29 "Symbol", | |
| 30 "ZapfDingbats"}; | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 CPWL_FontMap::CPWL_FontMap(IFX_SystemHandler* pSystemHandler) | |
| 35 : m_pPDFDoc(NULL), m_pSystemHandler(pSystemHandler) { | |
| 36 ASSERT(m_pSystemHandler); | |
| 37 } | |
| 38 | |
| 39 CPWL_FontMap::~CPWL_FontMap() { | |
| 40 delete m_pPDFDoc; | |
| 41 m_pPDFDoc = NULL; | |
| 42 | |
| 43 Empty(); | |
| 44 } | |
| 45 | |
| 46 void CPWL_FontMap::SetSystemHandler(IFX_SystemHandler* pSystemHandler) { | |
| 47 m_pSystemHandler = pSystemHandler; | |
| 48 } | |
| 49 | |
| 50 CPDF_Document* CPWL_FontMap::GetDocument() { | |
| 51 if (!m_pPDFDoc) { | |
| 52 if (CPDF_ModuleMgr::Get()) { | |
| 53 m_pPDFDoc = new CPDF_Document; | |
| 54 m_pPDFDoc->CreateNewDoc(); | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 return m_pPDFDoc; | |
| 59 } | |
| 60 | |
| 61 CPDF_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; | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 return NULL; | |
| 69 } | |
| 70 | |
| 71 CFX_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; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 return ""; | |
| 79 } | |
| 80 | |
| 81 FX_BOOL CPWL_FontMap::KnowWord(int32_t nFontIndex, FX_WORD word) { | |
| 82 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) { | |
| 83 if (m_aData.GetAt(nFontIndex)) { | |
| 84 return CharCodeFromUnicode(nFontIndex, word) >= 0; | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 return FALSE; | |
| 89 } | |
| 90 | |
| 91 int32_t CPWL_FontMap::GetWordFontIndex(FX_WORD word, | |
| 92 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 } | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 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; | |
| 119 } | |
| 120 | |
| 121 int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, FX_WORD word) { | |
| 122 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) { | |
| 123 if (pData->pFont) { | |
| 124 if (pData->pFont->IsUnicodeCompatible()) { | |
| 125 int nCharCode = pData->pFont->CharCodeFromUnicode(word); | |
| 126 pData->pFont->GlyphFromCharCode(nCharCode); | |
| 127 return nCharCode; | |
| 128 } | |
| 129 if (word < 0xFF) | |
| 130 return word; | |
| 131 } | |
| 132 } | |
| 133 return -1; | |
| 134 } | |
| 135 | |
| 136 CFX_ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset) { | |
| 137 // searching native font is slow, so we must save time | |
| 138 for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++) { | |
| 139 if (CPWL_FontMap_Native* pData = m_aNativeFont.GetAt(i)) { | |
| 140 if (pData->nCharset == nCharset) | |
| 141 return pData->sFontName; | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 CFX_ByteString sNew = GetNativeFont(nCharset); | |
| 146 | |
| 147 if (!sNew.IsEmpty()) { | |
| 148 CPWL_FontMap_Native* pNewData = new CPWL_FontMap_Native; | |
| 149 pNewData->nCharset = nCharset; | |
| 150 pNewData->sFontName = sNew; | |
| 151 | |
| 152 m_aNativeFont.Add(pNewData); | |
| 153 } | |
| 154 | |
| 155 return sNew; | |
| 156 } | |
| 157 | |
| 158 void CPWL_FontMap::Empty() { | |
| 159 { | |
| 160 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) | |
| 161 delete m_aData.GetAt(i); | |
| 162 | |
| 163 m_aData.RemoveAll(); | |
| 164 } | |
| 165 { | |
| 166 for (int32_t i = 0, sz = m_aNativeFont.GetSize(); i < sz; i++) | |
| 167 delete m_aNativeFont.GetAt(i); | |
| 168 | |
| 169 m_aNativeFont.RemoveAll(); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 void CPWL_FontMap::Initialize() { | |
| 174 GetFontIndex(kDefaultFontName, ANSI_CHARSET, FALSE); | |
| 175 } | |
| 176 | |
| 177 FX_BOOL CPWL_FontMap::IsStandardFont(const CFX_ByteString& sFontName) { | |
| 178 for (int32_t i = 0; i < FX_ArraySize(g_sDEStandardFontName); ++i) { | |
| 179 if (sFontName == g_sDEStandardFontName[i]) | |
| 180 return TRUE; | |
| 181 } | |
| 182 | |
| 183 return FALSE; | |
| 184 } | |
| 185 | |
| 186 int32_t CPWL_FontMap::FindFont(const CFX_ByteString& sFontName, | |
| 187 int32_t nCharset) { | |
| 188 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) { | |
| 189 if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) { | |
| 190 if (nCharset == DEFAULT_CHARSET || nCharset == pData->nCharset) { | |
| 191 if (sFontName.IsEmpty() || pData->sFontName == sFontName) | |
| 192 return i; | |
| 193 } | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 return -1; | |
| 198 } | |
| 199 | |
| 200 int32_t CPWL_FontMap::GetFontIndex(const CFX_ByteString& sFontName, | |
| 201 int32_t nCharset, | |
| 202 FX_BOOL bFind) { | |
| 203 int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset); | |
| 204 if (nFontIndex >= 0) | |
| 205 return nFontIndex; | |
| 206 | |
| 207 CFX_ByteString sAlias; | |
| 208 CPDF_Font* pFont = NULL; | |
| 209 if (bFind) | |
| 210 pFont = FindFontSameCharset(sAlias, nCharset); | |
| 211 | |
| 212 if (!pFont) { | |
| 213 CFX_ByteString sTemp = sFontName; | |
| 214 pFont = AddFontToDocument(GetDocument(), sTemp, nCharset); | |
| 215 sAlias = EncodeFontAlias(sTemp, nCharset); | |
| 216 } | |
| 217 AddedFont(pFont, sAlias); | |
| 218 return AddFontData(pFont, sAlias, nCharset); | |
| 219 } | |
| 220 | |
| 221 int32_t CPWL_FontMap::GetPWLFontIndex(FX_WORD word, int32_t nCharset) { | |
| 222 int32_t nFind = -1; | |
| 223 | |
| 224 for (int32_t i = 0, sz = m_aData.GetSize(); i < sz; i++) { | |
| 225 if (CPWL_FontMap_Data* pData = m_aData.GetAt(i)) { | |
| 226 if (pData->nCharset == nCharset) { | |
| 227 nFind = i; | |
| 228 break; | |
| 229 } | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 CPDF_Font* pNewFont = GetPDFFont(nFind); | |
| 234 | |
| 235 if (!pNewFont) | |
| 236 return -1; | |
| 237 | |
| 238 CFX_ByteString sAlias = EncodeFontAlias("Arial_Chrome", nCharset); | |
| 239 AddedFont(pNewFont, sAlias); | |
| 240 | |
| 241 return AddFontData(pNewFont, sAlias, nCharset); | |
| 242 } | |
| 243 | |
| 244 CPDF_Font* CPWL_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias, | |
| 245 int32_t nCharset) { | |
| 246 return NULL; | |
| 247 } | |
| 248 | |
| 249 int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont, | |
| 250 const CFX_ByteString& sFontAlias, | |
| 251 int32_t nCharset) { | |
| 252 CPWL_FontMap_Data* pNewData = new CPWL_FontMap_Data; | |
| 253 pNewData->pFont = pFont; | |
| 254 pNewData->sFontName = sFontAlias; | |
| 255 pNewData->nCharset = nCharset; | |
| 256 | |
| 257 m_aData.Add(pNewData); | |
| 258 | |
| 259 return m_aData.GetSize() - 1; | |
| 260 } | |
| 261 | |
| 262 void CPWL_FontMap::AddedFont(CPDF_Font* pFont, | |
| 263 const CFX_ByteString& sFontAlias) {} | |
| 264 | |
| 265 CFX_ByteString CPWL_FontMap::GetFontName(int32_t nFontIndex) { | |
| 266 if (nFontIndex >= 0 && nFontIndex < m_aData.GetSize()) { | |
| 267 if (CPWL_FontMap_Data* pData = m_aData.GetAt(nFontIndex)) { | |
| 268 return pData->sFontName; | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 return ""; | |
| 273 } | |
| 274 | |
| 275 CFX_ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) { | |
| 276 if (nCharset == DEFAULT_CHARSET) | |
| 277 nCharset = GetNativeCharset(); | |
| 278 | |
| 279 CFX_ByteString sFontName = GetDefaultFontByCharset(nCharset); | |
| 280 if (m_pSystemHandler) { | |
| 281 if (m_pSystemHandler->FindNativeTrueTypeFont(nCharset, sFontName)) | |
| 282 return sFontName; | |
| 283 | |
| 284 sFontName = m_pSystemHandler->GetNativeTrueTypeFont(nCharset); | |
| 285 } | |
| 286 return sFontName; | |
| 287 } | |
| 288 | |
| 289 CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc, | |
| 290 CFX_ByteString& sFontName, | |
| 291 uint8_t nCharset) { | |
| 292 if (IsStandardFont(sFontName)) | |
| 293 return AddStandardFont(pDoc, sFontName); | |
| 294 | |
| 295 return AddSystemFont(pDoc, sFontName, nCharset); | |
| 296 } | |
| 297 | |
| 298 CPDF_Font* CPWL_FontMap::AddStandardFont(CPDF_Document* pDoc, | |
| 299 CFX_ByteString& sFontName) { | |
| 300 if (!pDoc) | |
| 301 return NULL; | |
| 302 | |
| 303 CPDF_Font* pFont = NULL; | |
| 304 | |
| 305 if (sFontName == "ZapfDingbats") { | |
| 306 pFont = pDoc->AddStandardFont(sFontName, NULL); | |
| 307 } else { | |
| 308 CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI); | |
| 309 pFont = pDoc->AddStandardFont(sFontName, &fe); | |
| 310 } | |
| 311 | |
| 312 return pFont; | |
| 313 } | |
| 314 | |
| 315 CPDF_Font* CPWL_FontMap::AddSystemFont(CPDF_Document* pDoc, | |
| 316 CFX_ByteString& sFontName, | |
| 317 uint8_t nCharset) { | |
| 318 if (!pDoc) | |
| 319 return NULL; | |
| 320 | |
| 321 if (sFontName.IsEmpty()) | |
| 322 sFontName = GetNativeFont(nCharset); | |
| 323 if (nCharset == DEFAULT_CHARSET) | |
| 324 nCharset = GetNativeCharset(); | |
| 325 | |
| 326 if (m_pSystemHandler) | |
| 327 return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName, | |
| 328 nCharset); | |
| 329 | |
| 330 return NULL; | |
| 331 } | |
| 332 | |
| 333 CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName, | |
| 334 int32_t nCharset) { | |
| 335 CFX_ByteString sPostfix; | |
| 336 sPostfix.Format("_%02X", nCharset); | |
| 337 return EncodeFontAlias(sFontName) + sPostfix; | |
| 338 } | |
| 339 | |
| 340 CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) { | |
| 341 CFX_ByteString sRet = sFontName; | |
| 342 sRet.Remove(' '); | |
| 343 return sRet; | |
| 344 } | |
| 345 | |
| 346 int32_t CPWL_FontMap::GetFontMapCount() const { | |
| 347 return m_aData.GetSize(); | |
| 348 } | |
| 349 | |
| 350 const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const { | |
| 351 if (nIndex >= 0 && nIndex < m_aData.GetSize()) { | |
| 352 return m_aData.GetAt(nIndex); | |
| 353 } | |
| 354 | |
| 355 return NULL; | |
| 356 } | |
| 357 | |
| 358 int32_t CPWL_FontMap::GetNativeCharset() { | |
| 359 uint8_t nCharset = ANSI_CHARSET; | |
| 360 int32_t iCodePage = FXSYS_GetACP(); | |
| 361 switch (iCodePage) { | |
| 362 case 932: // Japan | |
| 363 nCharset = SHIFTJIS_CHARSET; | |
| 364 break; | |
| 365 case 936: // Chinese (PRC, Singapore) | |
| 366 nCharset = GB2312_CHARSET; | |
| 367 break; | |
| 368 case 950: // Chinese (Taiwan; Hong Kong SAR, PRC) | |
| 369 nCharset = GB2312_CHARSET; | |
| 370 break; | |
| 371 case 1252: // Windows 3.1 Latin 1 (US, Western Europe) | |
| 372 nCharset = ANSI_CHARSET; | |
| 373 break; | |
| 374 case 874: // Thai | |
| 375 nCharset = THAI_CHARSET; | |
| 376 break; | |
| 377 case 949: // Korean | |
| 378 nCharset = HANGUL_CHARSET; | |
| 379 break; | |
| 380 case 1200: // Unicode (BMP of ISO 10646) | |
| 381 nCharset = ANSI_CHARSET; | |
| 382 break; | |
| 383 case 1250: // Windows 3.1 Eastern European | |
| 384 nCharset = EASTEUROPE_CHARSET; | |
| 385 break; | |
| 386 case 1251: // Windows 3.1 Cyrillic | |
| 387 nCharset = RUSSIAN_CHARSET; | |
| 388 break; | |
| 389 case 1253: // Windows 3.1 Greek | |
| 390 nCharset = GREEK_CHARSET; | |
| 391 break; | |
| 392 case 1254: // Windows 3.1 Turkish | |
| 393 nCharset = TURKISH_CHARSET; | |
| 394 break; | |
| 395 case 1255: // Hebrew | |
| 396 nCharset = HEBREW_CHARSET; | |
| 397 break; | |
| 398 case 1256: // Arabic | |
| 399 nCharset = ARABIC_CHARSET; | |
| 400 break; | |
| 401 case 1257: // Baltic | |
| 402 nCharset = BALTIC_CHARSET; | |
| 403 break; | |
| 404 case 1258: // Vietnamese | |
| 405 nCharset = VIETNAMESE_CHARSET; | |
| 406 break; | |
| 407 case 1361: // Korean(Johab) | |
| 408 nCharset = JOHAB_CHARSET; | |
| 409 break; | |
| 410 } | |
| 411 return nCharset; | |
| 412 } | |
| 413 | |
| 414 const CPWL_FontMap::CharsetFontMap CPWL_FontMap::defaultTTFMap[] = { | |
| 415 {ANSI_CHARSET, "Helvetica"}, {GB2312_CHARSET, "SimSun"}, | |
| 416 {CHINESEBIG5_CHARSET, "MingLiU"}, {SHIFTJIS_CHARSET, "MS Gothic"}, | |
| 417 {HANGUL_CHARSET, "Batang"}, {RUSSIAN_CHARSET, "Arial"}, | |
| 418 #if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \ | |
| 419 _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
| 420 {EASTEUROPE_CHARSET, "Arial"}, | |
| 421 #else | |
| 422 {EASTEUROPE_CHARSET, "Tahoma"}, | |
| 423 #endif | |
| 424 {ARABIC_CHARSET, "Arial"}, {-1, NULL}}; | |
| 425 | |
| 426 CFX_ByteString CPWL_FontMap::GetDefaultFontByCharset(int32_t nCharset) { | |
| 427 int i = 0; | |
| 428 while (defaultTTFMap[i].charset != -1) { | |
| 429 if (nCharset == defaultTTFMap[i].charset) | |
| 430 return defaultTTFMap[i].fontname; | |
| 431 ++i; | |
| 432 } | |
| 433 return ""; | |
| 434 } | |
| 435 | |
| 436 int32_t CPWL_FontMap::CharSetFromUnicode(FX_WORD word, int32_t nOldCharset) { | |
| 437 if (m_pSystemHandler && (-1 != m_pSystemHandler->GetCharSet())) | |
| 438 return m_pSystemHandler->GetCharSet(); | |
| 439 // to avoid CJK Font to show ASCII | |
| 440 if (word < 0x7F) | |
| 441 return ANSI_CHARSET; | |
| 442 // 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; | |
| 489 } | |
| 490 | |
| 491 CPWL_DocFontMap::CPWL_DocFontMap(IFX_SystemHandler* pSystemHandler, | |
| 492 CPDF_Document* pAttachedDoc) | |
| 493 : CPWL_FontMap(pSystemHandler), m_pAttachedDoc(pAttachedDoc) {} | |
| 494 | |
| 495 CPWL_DocFontMap::~CPWL_DocFontMap() {} | |
| 496 | |
| 497 CPDF_Document* CPWL_DocFontMap::GetDocument() { | |
| 498 return m_pAttachedDoc; | |
| 499 } | |
| OLD | NEW |