From db0312e6acd7cc15fef0f64e05bd463cb74c70e4 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 21 Sep 2017 09:46:03 -0400 Subject: Move CXFA_PDFFontMgr to CFGAS_PDFFontMgr XFA bits passed into the PDFFontMgr are just used to get the CPDF_Document and the CFGAS_FontMgr. This CL changes the code to pass those two things into the constructor. Then, the fxfa/cxfa_pdffontmgr.* code has been moved to fgas/cfgas_pdffontmgr.* to show it doesn't depend on any of the XFA classes. Change-Id: I21d791869e2a32ffedfd7c87bb3bbd035232d991 Reviewed-on: https://pdfium-review.googlesource.com/14550 Commit-Queue: dsinclair Reviewed-by: Henrique Nakashima Reviewed-by: Tom Sepez --- BUILD.gn | 4 +- xfa/fgas/font/cfgas_fontmgr.h | 2 +- xfa/fgas/font/cfgas_gefont.cpp | 1 - xfa/fgas/font/cfgas_gefont.h | 6 +- xfa/fgas/font/cfgas_pdffontmgr.cpp | 207 +++++++++++++++++++++++++++++++++++++ xfa/fgas/font/cfgas_pdffontmgr.h | 59 +++++++++++ xfa/fxfa/cxfa_ffdatetimeedit.cpp | 1 + xfa/fxfa/cxfa_ffnumericedit.cpp | 1 + xfa/fxfa/cxfa_ffpasswordedit.cpp | 1 + xfa/fxfa/cxfa_fftextedit.cpp | 1 + xfa/fxfa/cxfa_fontmgr.cpp | 5 +- xfa/fxfa/cxfa_fontmgr.h | 4 +- xfa/fxfa/cxfa_pdffontmgr.cpp | 206 ------------------------------------ xfa/fxfa/cxfa_pdffontmgr.h | 58 ----------- 14 files changed, 281 insertions(+), 275 deletions(-) create mode 100644 xfa/fgas/font/cfgas_pdffontmgr.cpp create mode 100644 xfa/fgas/font/cfgas_pdffontmgr.h delete mode 100644 xfa/fxfa/cxfa_pdffontmgr.cpp delete mode 100644 xfa/fxfa/cxfa_pdffontmgr.h diff --git a/BUILD.gn b/BUILD.gn index 2cf6552f21..aad97e97e5 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1488,6 +1488,8 @@ if (pdf_enable_xfa) { "xfa/fgas/font/cfgas_fontmgr.h", "xfa/fgas/font/cfgas_gefont.cpp", "xfa/fgas/font/cfgas_gefont.h", + "xfa/fgas/font/cfgas_pdffontmgr.cpp", + "xfa/fgas/font/cfgas_pdffontmgr.h", "xfa/fgas/font/fgas_fontutils.cpp", "xfa/fgas/font/fgas_fontutils.h", "xfa/fgas/layout/cfx_break.cpp", @@ -1690,8 +1692,6 @@ if (pdf_enable_xfa) { "xfa/fxfa/cxfa_linkuserdata.h", "xfa/fxfa/cxfa_loadercontext.cpp", "xfa/fxfa/cxfa_loadercontext.h", - "xfa/fxfa/cxfa_pdffontmgr.cpp", - "xfa/fxfa/cxfa_pdffontmgr.h", "xfa/fxfa/cxfa_pieceline.cpp", "xfa/fxfa/cxfa_pieceline.h", "xfa/fxfa/cxfa_rendercontext.cpp", diff --git a/xfa/fgas/font/cfgas_fontmgr.h b/xfa/fgas/font/cfgas_fontmgr.h index 8e970361b4..946486be2a 100644 --- a/xfa/fgas/font/cfgas_fontmgr.h +++ b/xfa/fgas/font/cfgas_fontmgr.h @@ -21,7 +21,7 @@ #include "core/fxge/cfx_fontmapper.h" #include "core/fxge/fx_freetype.h" #include "core/fxge/ifx_systemfontinfo.h" -#include "xfa/fxfa/cxfa_pdffontmgr.h" +#include "xfa/fgas/font/cfgas_pdffontmgr.h" #define FX_FONTSTYLE_Normal 0x00 #define FX_FONTSTYLE_FixedPitch 0x01 diff --git a/xfa/fgas/font/cfgas_gefont.cpp b/xfa/fgas/font/cfgas_gefont.cpp index c710ed96b0..404b23cf7b 100644 --- a/xfa/fgas/font/cfgas_gefont.cpp +++ b/xfa/fgas/font/cfgas_gefont.cpp @@ -15,7 +15,6 @@ #include "core/fxge/cfx_unicodeencodingex.h" #include "third_party/base/ptr_util.h" #include "xfa/fgas/font/fgas_fontutils.h" -#include "xfa/fxfa/cxfa_fontmgr.h" // static CFX_RetainPtr CFGAS_GEFont::LoadFont(const wchar_t* pszFontFamily, diff --git a/xfa/fgas/font/cfgas_gefont.h b/xfa/fgas/font/cfgas_gefont.h index 13ac48f9cb..c83fc4319d 100644 --- a/xfa/fgas/font/cfgas_gefont.h +++ b/xfa/fgas/font/cfgas_gefont.h @@ -15,7 +15,7 @@ #include "core/fxcrt/cfx_unowned_ptr.h" #include "core/fxcrt/fx_memory.h" #include "xfa/fgas/font/cfgas_fontmgr.h" -#include "xfa/fxfa/cxfa_pdffontmgr.h" +#include "xfa/fgas/font/cfgas_pdffontmgr.h" #define FXFONT_SUBST_ITALIC 0x02 @@ -50,7 +50,7 @@ class CFGAS_GEFont : public CFX_Retainable { bool GetBBox(CFX_Rect* bbox); CFX_RetainPtr GetSubstFont(int32_t iGlyphIndex); CFX_Font* GetDevFont() const { return m_pFont; } - void SetFontProvider(CXFA_PDFFontMgr* pProvider) { + void SetFontProvider(CFGAS_PDFFontMgr* pProvider) { m_pProvider.Reset(pProvider); } #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ @@ -99,7 +99,7 @@ class CFGAS_GEFont : public CFX_Retainable { bool m_bExternalFont; CFX_RetainPtr m_pSrcFont; // Only set by ctor, so no cycles. CFGAS_FontMgr::ObservedPtr m_pFontMgr; - CXFA_PDFFontMgr::ObservedPtr m_pProvider; + CFGAS_PDFFontMgr::ObservedPtr m_pProvider; CFX_RetainPtr m_pStream; CFX_RetainPtr m_pFileRead; std::unique_ptr m_pFontEncoding; diff --git a/xfa/fgas/font/cfgas_pdffontmgr.cpp b/xfa/fgas/font/cfgas_pdffontmgr.cpp new file mode 100644 index 0000000000..2b8e30e5ee --- /dev/null +++ b/xfa/fgas/font/cfgas_pdffontmgr.cpp @@ -0,0 +1,207 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "xfa/fgas/font/cfgas_pdffontmgr.h" + +#include + +#include "core/fpdfapi/font/cpdf_font.h" +#include "core/fpdfapi/parser/cpdf_document.h" +#include "xfa/fgas/font/cfgas_fontmgr.h" +#include "xfa/fgas/font/cfgas_gefont.h" + +namespace { + +// The 5 names per entry are: PsName, Normal, Bold, Italic, BoldItalic. +const char* const g_XFAPDFFontName[][5] = { + {"Adobe PI Std", "AdobePIStd", "AdobePIStd", "AdobePIStd", "AdobePIStd"}, + {"Myriad Pro Light", "MyriadPro-Light", "MyriadPro-Semibold", + "MyriadPro-LightIt", "MyriadPro-SemiboldIt"}, +}; + +} // namespace + +CFGAS_PDFFontMgr::CFGAS_PDFFontMgr(CPDF_Document* pDoc, CFGAS_FontMgr* pFontMgr) + : m_pDoc(pDoc), m_pFontMgr(pFontMgr) { + ASSERT(pDoc); + ASSERT(pFontMgr); +} + +CFGAS_PDFFontMgr::~CFGAS_PDFFontMgr() {} + +CFX_RetainPtr CFGAS_PDFFontMgr::FindFont( + const ByteString& strPsName, + bool bBold, + bool bItalic, + CPDF_Font** pDstPDFFont, + bool bStrictMatch) { + CPDF_Dictionary* pFontSetDict = + m_pDoc->GetRoot()->GetDictFor("AcroForm")->GetDictFor("DR"); + if (!pFontSetDict) + return nullptr; + + pFontSetDict = pFontSetDict->GetDictFor("Font"); + if (!pFontSetDict) + return nullptr; + + ByteString name = strPsName; + name.Remove(' '); + for (const auto& it : *pFontSetDict) { + const ByteString& key = it.first; + CPDF_Object* pObj = it.second.get(); + if (!PsNameMatchDRFontName(name.AsStringView(), bBold, bItalic, key, + bStrictMatch)) { + continue; + } + CPDF_Dictionary* pFontDict = ToDictionary(pObj->GetDirect()); + if (!pFontDict || pFontDict->GetStringFor("Type") != "Font") + return nullptr; + + CPDF_Font* pPDFFont = m_pDoc->LoadFont(pFontDict); + if (!pPDFFont) + return nullptr; + + if (!pPDFFont->IsEmbedded()) { + *pDstPDFFont = pPDFFont; + return nullptr; + } + return CFGAS_GEFont::LoadFont(pPDFFont->GetFont(), m_pFontMgr.Get()); + } + return nullptr; +} + +CFX_RetainPtr CFGAS_PDFFontMgr::GetFont( + const WideStringView& wsFontFamily, + uint32_t dwFontStyles, + CPDF_Font** pPDFFont, + bool bStrictMatch) { + uint32_t dwHashCode = FX_HashCode_GetW(wsFontFamily, false); + ByteString strKey; + strKey.Format("%u%u", dwHashCode, dwFontStyles); + auto it = m_FontMap.find(strKey); + if (it != m_FontMap.end()) + return it->second; + + ByteString bsPsName = ByteString::FromUnicode(WideString(wsFontFamily)); + bool bBold = (dwFontStyles & FX_FONTSTYLE_Bold) == FX_FONTSTYLE_Bold; + bool bItalic = (dwFontStyles & FX_FONTSTYLE_Italic) == FX_FONTSTYLE_Italic; + ByteString strFontName = PsNameToFontName(bsPsName, bBold, bItalic); + CFX_RetainPtr pFont = + FindFont(strFontName, bBold, bItalic, pPDFFont, bStrictMatch); + if (pFont) + m_FontMap[strKey] = pFont; + + return pFont; +} + +ByteString CFGAS_PDFFontMgr::PsNameToFontName(const ByteString& strPsName, + bool bBold, + bool bItalic) { + for (size_t i = 0; i < FX_ArraySize(g_XFAPDFFontName); ++i) { + if (strPsName == g_XFAPDFFontName[i][0]) { + size_t index = 1; + if (bBold) + ++index; + if (bItalic) + index += 2; + return g_XFAPDFFontName[i][index]; + } + } + return strPsName; +} + +bool CFGAS_PDFFontMgr::PsNameMatchDRFontName(const ByteStringView& bsPsName, + bool bBold, + bool bItalic, + const ByteString& bsDRFontName, + bool bStrictMatch) { + ByteString bsDRName = bsDRFontName; + bsDRName.Remove('-'); + FX_STRSIZE iPsLen = bsPsName.GetLength(); + auto nIndex = bsDRName.Find(bsPsName); + if (nIndex.has_value() && !bStrictMatch) + return true; + + if (!nIndex.has_value() || nIndex.value() != 0) + return false; + + FX_STRSIZE iDifferLength = bsDRName.GetLength() - iPsLen; + if (iDifferLength > 1 || (bBold || bItalic)) { + auto iBoldIndex = bsDRName.Find("Bold"); + if (bBold != iBoldIndex.has_value()) + return false; + + if (iBoldIndex.has_value()) { + iDifferLength = std::min(iDifferLength - 4, + bsDRName.GetLength() - iBoldIndex.value() - 4); + } + bool bItalicFont = true; + if (bsDRName.Contains("Italic")) + iDifferLength -= 6; + else if (bsDRName.Contains("It")) + iDifferLength -= 2; + else if (bsDRName.Contains("Oblique")) + iDifferLength -= 7; + else + bItalicFont = false; + + if (bItalic != bItalicFont) + return false; + + if (iDifferLength > 1) { + ByteString bsDRTailer = bsDRName.Right(iDifferLength); + if (bsDRTailer == "MT" || bsDRTailer == "PSMT" || + bsDRTailer == "Regular" || bsDRTailer == "Reg") { + return true; + } + if (iBoldIndex.has_value() || bItalicFont) + return false; + + bool bMatch = false; + switch (bsPsName[iPsLen - 1]) { + case 'L': + if (bsDRName.Right(5) == "Light") + bMatch = true; + + break; + case 'R': + if (bsDRName.Right(7) == "Regular" || bsDRName.Right(3) == "Reg") + bMatch = true; + + break; + case 'M': + if (bsDRName.Right(5) == "Medium") + bMatch = true; + break; + default: + break; + } + return bMatch; + } + } + return true; +} + +bool CFGAS_PDFFontMgr::GetCharWidth(const CFX_RetainPtr& pFont, + wchar_t wUnicode, + bool bCharCode, + int32_t* pWidth) { + if (wUnicode != 0x20 || bCharCode) + return false; + + auto it = m_FDE2PDFFont.find(pFont); + if (it == m_FDE2PDFFont.end()) + return false; + + CPDF_Font* pPDFFont = it->second; + *pWidth = pPDFFont->GetCharWidthF(pPDFFont->CharCodeFromUnicode(wUnicode)); + return true; +} + +void CFGAS_PDFFontMgr::SetFont(const CFX_RetainPtr& pFont, + CPDF_Font* pPDFFont) { + m_FDE2PDFFont[pFont] = pPDFFont; +} diff --git a/xfa/fgas/font/cfgas_pdffontmgr.h b/xfa/fgas/font/cfgas_pdffontmgr.h new file mode 100644 index 0000000000..8a09a95bd1 --- /dev/null +++ b/xfa/fgas/font/cfgas_pdffontmgr.h @@ -0,0 +1,59 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FGAS_FONT_CFGAS_PDFFONTMGR_H_ +#define XFA_FGAS_FONT_CFGAS_PDFFONTMGR_H_ + +#include + +#include "core/fpdfapi/parser/cpdf_dictionary.h" +#include "core/fpdfapi/parser/cpdf_document.h" +#include "core/fxcrt/cfx_observable.h" +#include "core/fxcrt/cfx_retain_ptr.h" +#include "core/fxcrt/fx_string.h" + +class CFGAS_FontMgr; +class CFGAS_GEFont; +class CPDF_Document; +class CPDF_Font; + +class CFGAS_PDFFontMgr : public CFX_Observable { + public: + explicit CFGAS_PDFFontMgr(CPDF_Document* pDoc, CFGAS_FontMgr* pFontMgr); + ~CFGAS_PDFFontMgr(); + + CFX_RetainPtr GetFont(const WideStringView& wsFontFamily, + uint32_t dwFontStyles, + CPDF_Font** pPDFFont, + bool bStrictMatch); + bool GetCharWidth(const CFX_RetainPtr& pFont, + wchar_t wUnicode, + bool bCharCode, + int32_t* pWidth); + void SetFont(const CFX_RetainPtr& pFont, CPDF_Font* pPDFFont); + + private: + CFX_RetainPtr FindFont(const ByteString& strFamilyName, + bool bBold, + bool bItalic, + CPDF_Font** pPDFFont, + bool bStrictMatch); + ByteString PsNameToFontName(const ByteString& strPsName, + bool bBold, + bool bItalic); + bool PsNameMatchDRFontName(const ByteStringView& bsPsName, + bool bBold, + bool bItalic, + const ByteString& bsDRFontName, + bool bStrictMatch); + + CFX_UnownedPtr const m_pDoc; + CFX_UnownedPtr const m_pFontMgr; + std::map, CPDF_Font*> m_FDE2PDFFont; + std::map> m_FontMap; +}; + +#endif // XFA_FGAS_FONT_CFGAS_PDFFONTMGR_H_ diff --git a/xfa/fxfa/cxfa_ffdatetimeedit.cpp b/xfa/fxfa/cxfa_ffdatetimeedit.cpp index 643d384efd..5ed1690e30 100644 --- a/xfa/fxfa/cxfa_ffdatetimeedit.cpp +++ b/xfa/fxfa/cxfa_ffdatetimeedit.cpp @@ -13,6 +13,7 @@ #include "xfa/fwl/cfwl_notedriver.h" #include "xfa/fwl/cfwl_widget.h" #include "xfa/fxfa/cxfa_eventparam.h" +#include "xfa/fxfa/cxfa_ffdoc.h" #include "xfa/fxfa/parser/cxfa_localevalue.h" CXFA_FFDateTimeEdit::CXFA_FFDateTimeEdit(CXFA_WidgetAcc* pDataAcc) diff --git a/xfa/fxfa/cxfa_ffnumericedit.cpp b/xfa/fxfa/cxfa_ffnumericedit.cpp index 9b87c406d7..d6e1defca4 100644 --- a/xfa/fxfa/cxfa_ffnumericedit.cpp +++ b/xfa/fxfa/cxfa_ffnumericedit.cpp @@ -11,6 +11,7 @@ #include "xfa/fwl/cfwl_edit.h" #include "xfa/fwl/cfwl_eventvalidate.h" #include "xfa/fwl/cfwl_notedriver.h" +#include "xfa/fxfa/cxfa_ffdoc.h" #include "xfa/fxfa/parser/cxfa_localevalue.h" CXFA_FFNumericEdit::CXFA_FFNumericEdit(CXFA_WidgetAcc* pDataAcc) diff --git a/xfa/fxfa/cxfa_ffpasswordedit.cpp b/xfa/fxfa/cxfa_ffpasswordedit.cpp index e050a69e86..7e2f6cd29a 100644 --- a/xfa/fxfa/cxfa_ffpasswordedit.cpp +++ b/xfa/fxfa/cxfa_ffpasswordedit.cpp @@ -10,6 +10,7 @@ #include "xfa/fwl/cfwl_edit.h" #include "xfa/fwl/cfwl_notedriver.h" +#include "xfa/fxfa/cxfa_ffdoc.h" CXFA_FFPasswordEdit::CXFA_FFPasswordEdit(CXFA_WidgetAcc* pDataAcc) : CXFA_FFTextEdit(pDataAcc) {} diff --git a/xfa/fxfa/cxfa_fftextedit.cpp b/xfa/fxfa/cxfa_fftextedit.cpp index 8f68368757..dbfb52e085 100644 --- a/xfa/fxfa/cxfa_fftextedit.cpp +++ b/xfa/fxfa/cxfa_fftextedit.cpp @@ -18,6 +18,7 @@ #include "xfa/fwl/cfwl_notedriver.h" #include "xfa/fxfa/cxfa_eventparam.h" #include "xfa/fxfa/cxfa_ffapp.h" +#include "xfa/fxfa/cxfa_ffdoc.h" #include "xfa/fxfa/parser/cxfa_node.h" namespace { diff --git a/xfa/fxfa/cxfa_fontmgr.cpp b/xfa/fxfa/cxfa_fontmgr.cpp index b47ce6b21d..7d2ad70f48 100644 --- a/xfa/fxfa/cxfa_fontmgr.cpp +++ b/xfa/fxfa/cxfa_fontmgr.cpp @@ -1748,7 +1748,7 @@ CFX_RetainPtr CXFA_FontMgr::GetFont( WideString wsEnglishName = XFA_LocalFontNameToEnglishName(wsFontFamily); auto it = m_PDFFontMgrMap.find(hDoc); - CXFA_PDFFontMgr* pMgr = + CFGAS_PDFFontMgr* pMgr = it != m_PDFFontMgrMap.end() ? it->second.get() : nullptr; CPDF_Font* pPDFFont = nullptr; CFX_RetainPtr pFont; @@ -1784,7 +1784,8 @@ CFX_RetainPtr CXFA_FontMgr::GetFont( void CXFA_FontMgr::LoadDocFonts(CXFA_FFDoc* hDoc) { if (!m_PDFFontMgrMap[hDoc]) - m_PDFFontMgrMap[hDoc] = pdfium::MakeUnique(hDoc); + m_PDFFontMgrMap[hDoc] = pdfium::MakeUnique( + hDoc->GetPDFDoc(), hDoc->GetApp()->GetFDEFontMgr()); } void CXFA_FontMgr::ReleaseDocFonts(CXFA_FFDoc* hDoc) { diff --git a/xfa/fxfa/cxfa_fontmgr.h b/xfa/fxfa/cxfa_fontmgr.h index e5db565147..2e335622c6 100644 --- a/xfa/fxfa/cxfa_fontmgr.h +++ b/xfa/fxfa/cxfa_fontmgr.h @@ -15,8 +15,8 @@ #include "core/fxcrt/fx_extension.h" #include "core/fxcrt/fx_system.h" #include "xfa/fgas/font/cfgas_fontmgr.h" +#include "xfa/fgas/font/cfgas_pdffontmgr.h" #include "xfa/fxfa/cxfa_deffontmgr.h" -#include "xfa/fxfa/cxfa_pdffontmgr.h" #include "xfa/fxfa/fxfa.h" class CPDF_Font; @@ -47,7 +47,7 @@ class CXFA_FontMgr { private: std::unique_ptr m_pDefFontMgr; - std::map> m_PDFFontMgrMap; + std::map> m_PDFFontMgrMap; std::map> m_FontMap; }; diff --git a/xfa/fxfa/cxfa_pdffontmgr.cpp b/xfa/fxfa/cxfa_pdffontmgr.cpp deleted file mode 100644 index 9a90e54dec..0000000000 --- a/xfa/fxfa/cxfa_pdffontmgr.cpp +++ /dev/null @@ -1,206 +0,0 @@ -// Copyright 2017 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "xfa/fxfa/cxfa_pdffontmgr.h" - -#include - -#include "core/fpdfapi/font/cpdf_font.h" -#include "xfa/fgas/font/cfgas_gefont.h" -#include "xfa/fxfa/cxfa_ffapp.h" - -namespace { - -// The 5 names per entry are: PsName, Normal, Bold, Italic, BoldItalic. -const char* const g_XFAPDFFontName[][5] = { - {"Adobe PI Std", "AdobePIStd", "AdobePIStd", "AdobePIStd", "AdobePIStd"}, - {"Myriad Pro Light", "MyriadPro-Light", "MyriadPro-Semibold", - "MyriadPro-LightIt", "MyriadPro-SemiboldIt"}, -}; - -} // namespace - -CXFA_PDFFontMgr::CXFA_PDFFontMgr(CXFA_FFDoc* pDoc) : m_pDoc(pDoc) {} - -CXFA_PDFFontMgr::~CXFA_PDFFontMgr() {} - -CFX_RetainPtr CXFA_PDFFontMgr::FindFont( - const ByteString& strPsName, - bool bBold, - bool bItalic, - CPDF_Font** pDstPDFFont, - bool bStrictMatch) { - CPDF_Document* pDoc = m_pDoc->GetPDFDoc(); - if (!pDoc) - return nullptr; - - CPDF_Dictionary* pFontSetDict = - pDoc->GetRoot()->GetDictFor("AcroForm")->GetDictFor("DR"); - if (!pFontSetDict) - return nullptr; - - pFontSetDict = pFontSetDict->GetDictFor("Font"); - if (!pFontSetDict) - return nullptr; - - ByteString name = strPsName; - name.Remove(' '); - CFGAS_FontMgr* pFDEFontMgr = m_pDoc->GetApp()->GetFDEFontMgr(); - for (const auto& it : *pFontSetDict) { - const ByteString& key = it.first; - CPDF_Object* pObj = it.second.get(); - if (!PsNameMatchDRFontName(name.AsStringView(), bBold, bItalic, key, - bStrictMatch)) { - continue; - } - CPDF_Dictionary* pFontDict = ToDictionary(pObj->GetDirect()); - if (!pFontDict || pFontDict->GetStringFor("Type") != "Font") { - return nullptr; - } - CPDF_Font* pPDFFont = pDoc->LoadFont(pFontDict); - if (!pPDFFont) { - return nullptr; - } - if (!pPDFFont->IsEmbedded()) { - *pDstPDFFont = pPDFFont; - return nullptr; - } - return CFGAS_GEFont::LoadFont(pPDFFont->GetFont(), pFDEFontMgr); - } - return nullptr; -} - -CFX_RetainPtr CXFA_PDFFontMgr::GetFont( - const WideStringView& wsFontFamily, - uint32_t dwFontStyles, - CPDF_Font** pPDFFont, - bool bStrictMatch) { - uint32_t dwHashCode = FX_HashCode_GetW(wsFontFamily, false); - ByteString strKey; - strKey.Format("%u%u", dwHashCode, dwFontStyles); - auto it = m_FontMap.find(strKey); - if (it != m_FontMap.end()) - return it->second; - ByteString bsPsName = ByteString::FromUnicode(WideString(wsFontFamily)); - bool bBold = (dwFontStyles & FX_FONTSTYLE_Bold) == FX_FONTSTYLE_Bold; - bool bItalic = (dwFontStyles & FX_FONTSTYLE_Italic) == FX_FONTSTYLE_Italic; - ByteString strFontName = PsNameToFontName(bsPsName, bBold, bItalic); - CFX_RetainPtr pFont = - FindFont(strFontName, bBold, bItalic, pPDFFont, bStrictMatch); - if (pFont) - m_FontMap[strKey] = pFont; - return pFont; -} - -ByteString CXFA_PDFFontMgr::PsNameToFontName(const ByteString& strPsName, - bool bBold, - bool bItalic) { - for (size_t i = 0; i < FX_ArraySize(g_XFAPDFFontName); ++i) { - if (strPsName == g_XFAPDFFontName[i][0]) { - size_t index = 1; - if (bBold) - ++index; - if (bItalic) - index += 2; - return g_XFAPDFFontName[i][index]; - } - } - return strPsName; -} - -bool CXFA_PDFFontMgr::PsNameMatchDRFontName(const ByteStringView& bsPsName, - bool bBold, - bool bItalic, - const ByteString& bsDRFontName, - bool bStrictMatch) { - ByteString bsDRName = bsDRFontName; - bsDRName.Remove('-'); - FX_STRSIZE iPsLen = bsPsName.GetLength(); - auto nIndex = bsDRName.Find(bsPsName); - if (nIndex.has_value() && !bStrictMatch) - return true; - - if (!nIndex.has_value() || nIndex.value() != 0) - return false; - - FX_STRSIZE iDifferLength = bsDRName.GetLength() - iPsLen; - if (iDifferLength > 1 || (bBold || bItalic)) { - auto iBoldIndex = bsDRName.Find("Bold"); - if (bBold != iBoldIndex.has_value()) - return false; - - if (iBoldIndex.has_value()) { - iDifferLength = std::min(iDifferLength - 4, - bsDRName.GetLength() - iBoldIndex.value() - 4); - } - bool bItalicFont = true; - if (bsDRName.Contains("Italic")) { - iDifferLength -= 6; - } else if (bsDRName.Contains("It")) { - iDifferLength -= 2; - } else if (bsDRName.Contains("Oblique")) { - iDifferLength -= 7; - } else { - bItalicFont = false; - } - if (bItalic != bItalicFont) - return false; - - if (iDifferLength > 1) { - ByteString bsDRTailer = bsDRName.Right(iDifferLength); - if (bsDRTailer == "MT" || bsDRTailer == "PSMT" || - bsDRTailer == "Regular" || bsDRTailer == "Reg") { - return true; - } - if (iBoldIndex.has_value() || bItalicFont) - return false; - - bool bMatch = false; - switch (bsPsName[iPsLen - 1]) { - case 'L': { - if (bsDRName.Right(5) == "Light") { - bMatch = true; - } - } break; - case 'R': { - if (bsDRName.Right(7) == "Regular" || bsDRName.Right(3) == "Reg") { - bMatch = true; - } - } break; - case 'M': { - if (bsDRName.Right(5) == "Medium") { - bMatch = true; - } - } break; - default: - break; - } - return bMatch; - } - } - return true; -} - -bool CXFA_PDFFontMgr::GetCharWidth(const CFX_RetainPtr& pFont, - wchar_t wUnicode, - bool bCharCode, - int32_t* pWidth) { - if (wUnicode != 0x20 || bCharCode) - return false; - - auto it = m_FDE2PDFFont.find(pFont); - if (it == m_FDE2PDFFont.end()) - return false; - - CPDF_Font* pPDFFont = it->second; - *pWidth = pPDFFont->GetCharWidthF(pPDFFont->CharCodeFromUnicode(wUnicode)); - return true; -} - -void CXFA_PDFFontMgr::SetFont(const CFX_RetainPtr& pFont, - CPDF_Font* pPDFFont) { - m_FDE2PDFFont[pFont] = pPDFFont; -} diff --git a/xfa/fxfa/cxfa_pdffontmgr.h b/xfa/fxfa/cxfa_pdffontmgr.h deleted file mode 100644 index 4c8941f8f0..0000000000 --- a/xfa/fxfa/cxfa_pdffontmgr.h +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2017 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef XFA_FXFA_CXFA_PDFFONTMGR_H_ -#define XFA_FXFA_CXFA_PDFFONTMGR_H_ - -#include - -#include "core/fpdfapi/parser/cpdf_dictionary.h" -#include "core/fpdfapi/parser/cpdf_document.h" -#include "core/fxcrt/cfx_observable.h" -#include "core/fxcrt/cfx_retain_ptr.h" -#include "core/fxcrt/fx_string.h" -#include "xfa/fxfa/cxfa_ffdoc.h" - -class CFGAS_GEFont; -class CPDF_Font; -class CXFA_FFDoc; - -class CXFA_PDFFontMgr : public CFX_Observable { - public: - explicit CXFA_PDFFontMgr(CXFA_FFDoc* pDoc); - ~CXFA_PDFFontMgr(); - - CFX_RetainPtr GetFont(const WideStringView& wsFontFamily, - uint32_t dwFontStyles, - CPDF_Font** pPDFFont, - bool bStrictMatch); - bool GetCharWidth(const CFX_RetainPtr& pFont, - wchar_t wUnicode, - bool bCharCode, - int32_t* pWidth); - void SetFont(const CFX_RetainPtr& pFont, CPDF_Font* pPDFFont); - - private: - CFX_RetainPtr FindFont(const ByteString& strFamilyName, - bool bBold, - bool bItalic, - CPDF_Font** pPDFFont, - bool bStrictMatch); - ByteString PsNameToFontName(const ByteString& strPsName, - bool bBold, - bool bItalic); - bool PsNameMatchDRFontName(const ByteStringView& bsPsName, - bool bBold, - bool bItalic, - const ByteString& bsDRFontName, - bool bStrictMatch); - - CFX_UnownedPtr const m_pDoc; - std::map, CPDF_Font*> m_FDE2PDFFont; - std::map> m_FontMap; -}; - -#endif // XFA_FXFA_CXFA_PDFFONTMGR_H_ -- cgit v1.2.3