| OLD | NEW |
| 1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h" | 7 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h" |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <vector> |
| 10 | 11 |
| 11 #include "core/fpdfapi/fpdf_font/cpdf_truetypefont.h" | 12 #include "core/fpdfapi/fpdf_font/cpdf_truetypefont.h" |
| 12 #include "core/fpdfapi/fpdf_font/cpdf_type1font.h" | 13 #include "core/fpdfapi/fpdf_font/cpdf_type1font.h" |
| 13 #include "core/fpdfapi/fpdf_font/cpdf_type3font.h" | 14 #include "core/fpdfapi/fpdf_font/cpdf_type3font.h" |
| 14 #include "core/fpdfapi/fpdf_font/font_int.h" | 15 #include "core/fpdfapi/fpdf_font/font_int.h" |
| 15 #include "core/fpdfapi/fpdf_font/include/cpdf_fontencoding.h" | 16 #include "core/fpdfapi/fpdf_font/include/cpdf_fontencoding.h" |
| 16 #include "core/fpdfapi/fpdf_page/cpdf_pagemodule.h" | 17 #include "core/fpdfapi/fpdf_page/cpdf_pagemodule.h" |
| 17 #include "core/fpdfapi/fpdf_page/pageint.h" | 18 #include "core/fpdfapi/fpdf_page/pageint.h" |
| 18 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | 19 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
| 19 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" | 20 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" |
| 20 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" | 21 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
| 21 #include "core/fpdfapi/fpdf_parser/include/cpdf_name.h" | 22 #include "core/fpdfapi/fpdf_parser/include/cpdf_name.h" |
| 22 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" | 23 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" |
| 23 #include "core/fpdfapi/include/cpdf_modulemgr.h" | 24 #include "core/fpdfapi/include/cpdf_modulemgr.h" |
| 24 #include "core/fxge/include/fx_freetype.h" | 25 #include "core/fxge/include/fx_freetype.h" |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 const uint8_t kChineseFontNames[][5] = {{0xCB, 0xCE, 0xCC, 0xE5, 0x00}, | 29 const uint8_t kChineseFontNames[][5] = {{0xCB, 0xCE, 0xCC, 0xE5, 0x00}, |
| 29 {0xBF, 0xAC, 0xCC, 0xE5, 0x00}, | 30 {0xBF, 0xAC, 0xCC, 0xE5, 0x00}, |
| 30 {0xBA, 0xDA, 0xCC, 0xE5, 0x00}, | 31 {0xBA, 0xDA, 0xCC, 0xE5, 0x00}, |
| 31 {0xB7, 0xC2, 0xCB, 0xCE, 0x00}, | 32 {0xB7, 0xC2, 0xCB, 0xCE, 0x00}, |
| 32 {0xD0, 0xC2, 0xCB, 0xCE, 0x00}}; | 33 {0xD0, 0xC2, 0xCB, 0xCE, 0x00}}; |
| 33 | 34 |
| 34 void GetPredefinedEncoding(int& basemap, const CFX_ByteString& value) { | 35 void GetPredefinedEncoding(const CFX_ByteString& value, int* basemap) { |
| 35 if (value == "WinAnsiEncoding") | 36 if (value == "WinAnsiEncoding") |
| 36 basemap = PDFFONT_ENCODING_WINANSI; | 37 *basemap = PDFFONT_ENCODING_WINANSI; |
| 37 else if (value == "MacRomanEncoding") | 38 else if (value == "MacRomanEncoding") |
| 38 basemap = PDFFONT_ENCODING_MACROMAN; | 39 *basemap = PDFFONT_ENCODING_MACROMAN; |
| 39 else if (value == "MacExpertEncoding") | 40 else if (value == "MacExpertEncoding") |
| 40 basemap = PDFFONT_ENCODING_MACEXPERT; | 41 *basemap = PDFFONT_ENCODING_MACEXPERT; |
| 41 else if (value == "PDFDocEncoding") | 42 else if (value == "PDFDocEncoding") |
| 42 basemap = PDFFONT_ENCODING_PDFDOC; | 43 *basemap = PDFFONT_ENCODING_PDFDOC; |
| 43 } | 44 } |
| 44 | 45 |
| 45 } // namespace | 46 } // namespace |
| 46 | 47 |
| 47 CPDF_Font::CPDF_Font() | 48 CPDF_Font::CPDF_Font() |
| 48 : m_pFontFile(nullptr), | 49 : m_pFontFile(nullptr), |
| 49 m_pFontDict(nullptr), | 50 m_pFontDict(nullptr), |
| 50 m_bToUnicodeLoaded(FALSE), | 51 m_bToUnicodeLoaded(FALSE), |
| 51 m_Flags(0), | 52 m_Flags(0), |
| 52 m_StemV(0), | 53 m_StemV(0), |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 int& offset) const { | 350 int& offset) const { |
| 350 if (offset < 0 || nStrLen < 1) { | 351 if (offset < 0 || nStrLen < 1) { |
| 351 return 0; | 352 return 0; |
| 352 } | 353 } |
| 353 uint8_t ch = offset < nStrLen ? pString[offset++] : pString[nStrLen - 1]; | 354 uint8_t ch = offset < nStrLen ? pString[offset++] : pString[nStrLen - 1]; |
| 354 return static_cast<uint32_t>(ch); | 355 return static_cast<uint32_t>(ch); |
| 355 } | 356 } |
| 356 | 357 |
| 357 void CPDF_Font::LoadPDFEncoding(CPDF_Object* pEncoding, | 358 void CPDF_Font::LoadPDFEncoding(CPDF_Object* pEncoding, |
| 358 int& iBaseEncoding, | 359 int& iBaseEncoding, |
| 359 CFX_ByteString*& pCharNames, | 360 std::vector<CFX_ByteString>* pCharNames, |
| 360 FX_BOOL bEmbedded, | 361 FX_BOOL bEmbedded, |
| 361 FX_BOOL bTrueType) { | 362 FX_BOOL bTrueType) { |
| 362 if (!pEncoding) { | 363 if (!pEncoding) { |
| 363 if (m_BaseFont == "Symbol") { | 364 if (m_BaseFont == "Symbol") { |
| 364 iBaseEncoding = bTrueType ? PDFFONT_ENCODING_MS_SYMBOL | 365 iBaseEncoding = bTrueType ? PDFFONT_ENCODING_MS_SYMBOL |
| 365 : PDFFONT_ENCODING_ADOBE_SYMBOL; | 366 : PDFFONT_ENCODING_ADOBE_SYMBOL; |
| 366 } else if (!bEmbedded && iBaseEncoding == PDFFONT_ENCODING_BUILTIN) { | 367 } else if (!bEmbedded && iBaseEncoding == PDFFONT_ENCODING_BUILTIN) { |
| 367 iBaseEncoding = PDFFONT_ENCODING_WINANSI; | 368 iBaseEncoding = PDFFONT_ENCODING_WINANSI; |
| 368 } | 369 } |
| 369 return; | 370 return; |
| 370 } | 371 } |
| 371 if (pEncoding->IsName()) { | 372 if (pEncoding->IsName()) { |
| 372 if (iBaseEncoding == PDFFONT_ENCODING_ADOBE_SYMBOL || | 373 if (iBaseEncoding == PDFFONT_ENCODING_ADOBE_SYMBOL || |
| 373 iBaseEncoding == PDFFONT_ENCODING_ZAPFDINGBATS) { | 374 iBaseEncoding == PDFFONT_ENCODING_ZAPFDINGBATS) { |
| 374 return; | 375 return; |
| 375 } | 376 } |
| 376 if ((m_Flags & PDFFONT_SYMBOLIC) && m_BaseFont == "Symbol") { | 377 if ((m_Flags & PDFFONT_SYMBOLIC) && m_BaseFont == "Symbol") { |
| 377 if (!bTrueType) { | 378 if (!bTrueType) { |
| 378 iBaseEncoding = PDFFONT_ENCODING_ADOBE_SYMBOL; | 379 iBaseEncoding = PDFFONT_ENCODING_ADOBE_SYMBOL; |
| 379 } | 380 } |
| 380 return; | 381 return; |
| 381 } | 382 } |
| 382 CFX_ByteString bsEncoding = pEncoding->GetString(); | 383 CFX_ByteString bsEncoding = pEncoding->GetString(); |
| 383 if (bsEncoding.Compare("MacExpertEncoding") == 0) { | 384 if (bsEncoding.Compare("MacExpertEncoding") == 0) { |
| 384 bsEncoding = "WinAnsiEncoding"; | 385 bsEncoding = "WinAnsiEncoding"; |
| 385 } | 386 } |
| 386 GetPredefinedEncoding(iBaseEncoding, bsEncoding); | 387 GetPredefinedEncoding(bsEncoding, &iBaseEncoding); |
| 387 return; | 388 return; |
| 388 } | 389 } |
| 389 | 390 |
| 390 CPDF_Dictionary* pDict = pEncoding->AsDictionary(); | 391 CPDF_Dictionary* pDict = pEncoding->AsDictionary(); |
| 391 if (!pDict) | 392 if (!pDict) |
| 392 return; | 393 return; |
| 393 | 394 |
| 394 if (iBaseEncoding != PDFFONT_ENCODING_ADOBE_SYMBOL && | 395 if (iBaseEncoding != PDFFONT_ENCODING_ADOBE_SYMBOL && |
| 395 iBaseEncoding != PDFFONT_ENCODING_ZAPFDINGBATS) { | 396 iBaseEncoding != PDFFONT_ENCODING_ZAPFDINGBATS) { |
| 396 CFX_ByteString bsEncoding = pDict->GetStringBy("BaseEncoding"); | 397 CFX_ByteString bsEncoding = pDict->GetStringBy("BaseEncoding"); |
| 397 if (bsEncoding.Compare("MacExpertEncoding") == 0 && bTrueType) { | 398 if (bsEncoding.Compare("MacExpertEncoding") == 0 && bTrueType) { |
| 398 bsEncoding = "WinAnsiEncoding"; | 399 bsEncoding = "WinAnsiEncoding"; |
| 399 } | 400 } |
| 400 GetPredefinedEncoding(iBaseEncoding, bsEncoding); | 401 GetPredefinedEncoding(bsEncoding, &iBaseEncoding); |
| 401 } | 402 } |
| 402 if ((!bEmbedded || bTrueType) && iBaseEncoding == PDFFONT_ENCODING_BUILTIN) { | 403 if ((!bEmbedded || bTrueType) && iBaseEncoding == PDFFONT_ENCODING_BUILTIN) |
| 403 iBaseEncoding = PDFFONT_ENCODING_STANDARD; | 404 iBaseEncoding = PDFFONT_ENCODING_STANDARD; |
| 404 } | 405 |
| 405 CPDF_Array* pDiffs = pDict->GetArrayBy("Differences"); | 406 CPDF_Array* pDiffs = pDict->GetArrayBy("Differences"); |
| 406 if (!pDiffs) { | 407 if (!pDiffs) |
| 407 return; | 408 return; |
| 408 } | 409 |
| 409 pCharNames = new CFX_ByteString[256]; | 410 pCharNames->resize(256); |
| 410 uint32_t cur_code = 0; | 411 uint32_t cur_code = 0; |
| 411 for (uint32_t i = 0; i < pDiffs->GetCount(); i++) { | 412 for (uint32_t i = 0; i < pDiffs->GetCount(); i++) { |
| 412 CPDF_Object* pElement = pDiffs->GetDirectObjectAt(i); | 413 CPDF_Object* pElement = pDiffs->GetDirectObjectAt(i); |
| 413 if (!pElement) | 414 if (!pElement) |
| 414 continue; | 415 continue; |
| 415 | 416 |
| 416 if (CPDF_Name* pName = pElement->AsName()) { | 417 if (CPDF_Name* pName = pElement->AsName()) { |
| 417 if (cur_code < 256) | 418 if (cur_code < 256) |
| 418 pCharNames[cur_code] = pName->GetString(); | 419 (*pCharNames)[cur_code] = pName->GetString(); |
| 419 cur_code++; | 420 cur_code++; |
| 420 } else { | 421 } else { |
| 421 cur_code = pElement->GetInteger(); | 422 cur_code = pElement->GetInteger(); |
| 422 } | 423 } |
| 423 } | 424 } |
| 424 } | 425 } |
| 425 | 426 |
| 426 FX_BOOL CPDF_Font::IsStandardFont() const { | 427 FX_BOOL CPDF_Font::IsStandardFont() const { |
| 427 if (!IsType1Font()) | 428 if (!IsType1Font()) |
| 428 return FALSE; | 429 return FALSE; |
| 429 if (m_pFontFile) | 430 if (m_pFontFile) |
| 430 return FALSE; | 431 return FALSE; |
| 431 if (AsType1Font()->GetBase14Font() < 0) | 432 if (AsType1Font()->GetBase14Font() < 0) |
| 432 return FALSE; | 433 return FALSE; |
| 433 return TRUE; | 434 return TRUE; |
| 434 } | 435 } |
| 435 | 436 |
| 436 const FX_CHAR* CPDF_Font::GetAdobeCharName(int iBaseEncoding, | 437 const FX_CHAR* CPDF_Font::GetAdobeCharName( |
| 437 const CFX_ByteString* pCharNames, | 438 int iBaseEncoding, |
| 438 int charcode) { | 439 const std::vector<CFX_ByteString>& charnames, |
| 440 int charcode) { |
| 439 if (charcode < 0 || charcode >= 256) { | 441 if (charcode < 0 || charcode >= 256) { |
| 440 ASSERT(false); | 442 ASSERT(false); |
| 441 return nullptr; | 443 return nullptr; |
| 442 } | 444 } |
| 443 | 445 |
| 444 if (pCharNames && !pCharNames[charcode].IsEmpty()) | 446 if (!charnames.empty() && !charnames[charcode].IsEmpty()) |
| 445 return pCharNames[charcode].c_str(); | 447 return charnames[charcode].c_str(); |
| 446 | 448 |
| 447 const FX_CHAR* name = nullptr; | 449 const FX_CHAR* name = nullptr; |
| 448 if (iBaseEncoding) | 450 if (iBaseEncoding) |
| 449 name = PDF_CharNameFromPredefinedCharSet(iBaseEncoding, charcode); | 451 name = PDF_CharNameFromPredefinedCharSet(iBaseEncoding, charcode); |
| 450 return name && name[0] ? name : nullptr; | 452 return name && name[0] ? name : nullptr; |
| 451 } | 453 } |
| OLD | NEW |