Make some CFGAS_PDFFontMgr methods into anonymous functions
In CFGAS_PDFFontMgr, some of the private methods do not use any class
members. Turn them into standalone function inside cfgas_pdffontmgr.cpp
instead.
Change-Id: If36318b7bad3a7a09d8b449126b258a50a5d497a
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/120491
Reviewed-by: Thomas Sepez <tsepez@google.com>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/xfa/fgas/font/cfgas_pdffontmgr.cpp b/xfa/fgas/font/cfgas_pdffontmgr.cpp
index 01a32f2..dd89b0f 100644
--- a/xfa/fgas/font/cfgas_pdffontmgr.cpp
+++ b/xfa/fgas/font/cfgas_pdffontmgr.cpp
@@ -32,78 +32,9 @@
"MyriadPro-LightIt", "MyriadPro-SemiboldIt"}},
});
-} // namespace
-
-CFGAS_PDFFontMgr::CFGAS_PDFFontMgr(const CPDF_Document* pDoc) : m_pDoc(pDoc) {
- DCHECK(pDoc);
-}
-
-CFGAS_PDFFontMgr::~CFGAS_PDFFontMgr() = default;
-
-RetainPtr<CFGAS_GEFont> CFGAS_PDFFontMgr::FindFont(const ByteString& strPsName,
- bool bBold,
- bool bItalic,
- bool bStrictMatch) {
- RetainPtr<const 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(' ');
-
- auto* pData = CPDF_DocPageData::FromDocument(m_pDoc);
- CPDF_DictionaryLocker locker(pFontSetDict);
- for (const auto& it : locker) {
- const ByteString& key = it.first;
- const RetainPtr<CPDF_Object>& pObj = it.second;
- if (!PsNameMatchDRFontName(name.AsStringView(), bBold, bItalic, key,
- bStrictMatch)) {
- continue;
- }
- RetainPtr<CPDF_Dictionary> pFontDict =
- ToDictionary(pObj->GetMutableDirect());
- if (!ValidateDictType(pFontDict.Get(), "Font"))
- return nullptr;
-
- RetainPtr<CPDF_Font> pPDFFont = pData->GetFont(pFontDict);
- if (!pPDFFont || !pPDFFont->IsEmbedded())
- return nullptr;
-
- return CFGAS_GEFont::LoadFont(std::move(pPDFFont));
- }
- return nullptr;
-}
-
-RetainPtr<CFGAS_GEFont> CFGAS_PDFFontMgr::GetFont(
- const WideString& wsFontFamily,
- uint32_t dwFontStyles,
- bool bStrictMatch) {
- auto key = std::make_pair(wsFontFamily, dwFontStyles);
- auto it = m_FontMap.find(key);
- if (it != m_FontMap.end())
- return it->second;
-
- ByteString bsPsName = WideString(wsFontFamily).ToDefANSI();
- bool bBold = FontStyleIsForceBold(dwFontStyles);
- bool bItalic = FontStyleIsItalic(dwFontStyles);
- ByteString strFontName = PsNameToFontName(bsPsName, bBold, bItalic);
- RetainPtr<CFGAS_GEFont> pFont =
- FindFont(strFontName, bBold, bItalic, bStrictMatch);
- if (!pFont)
- return nullptr;
-
- m_FontMap[key] = pFont;
- return pFont;
-}
-
-ByteString CFGAS_PDFFontMgr::PsNameToFontName(const ByteString& strPsName,
- bool bBold,
- bool bItalic) {
+ByteString PsNameToFontName(const ByteString& strPsName,
+ bool bBold,
+ bool bItalic) {
for (const auto& entry : kXFAPDFFontNameTable) {
if (strPsName == entry[0]) {
size_t index = 1;
@@ -119,11 +50,11 @@
return strPsName;
}
-bool CFGAS_PDFFontMgr::PsNameMatchDRFontName(ByteStringView bsPsName,
- bool bBold,
- bool bItalic,
- const ByteString& bsDRFontName,
- bool bStrictMatch) {
+bool PsNameMatchDRFontName(ByteStringView bsPsName,
+ bool bBold,
+ bool bItalic,
+ const ByteString& bsDRFontName,
+ bool bStrictMatch) {
ByteString bsDRName = bsDRFontName;
bsDRName.Remove('-');
size_t iPsLen = bsPsName.GetLength();
@@ -190,3 +121,78 @@
}
return true;
}
+
+} // namespace
+
+CFGAS_PDFFontMgr::CFGAS_PDFFontMgr(const CPDF_Document* pDoc) : m_pDoc(pDoc) {
+ DCHECK(pDoc);
+}
+
+CFGAS_PDFFontMgr::~CFGAS_PDFFontMgr() = default;
+
+RetainPtr<CFGAS_GEFont> CFGAS_PDFFontMgr::FindFont(const ByteString& strPsName,
+ bool bBold,
+ bool bItalic,
+ bool bStrictMatch) {
+ RetainPtr<const 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(' ');
+
+ auto* pData = CPDF_DocPageData::FromDocument(m_pDoc);
+ CPDF_DictionaryLocker locker(pFontSetDict);
+ for (const auto& it : locker) {
+ const ByteString& key = it.first;
+ const RetainPtr<CPDF_Object>& pObj = it.second;
+ if (!PsNameMatchDRFontName(name.AsStringView(), bBold, bItalic, key,
+ bStrictMatch)) {
+ continue;
+ }
+ RetainPtr<CPDF_Dictionary> pFontDict =
+ ToDictionary(pObj->GetMutableDirect());
+ if (!ValidateDictType(pFontDict.Get(), "Font")) {
+ return nullptr;
+ }
+
+ RetainPtr<CPDF_Font> pPDFFont = pData->GetFont(pFontDict);
+ if (!pPDFFont || !pPDFFont->IsEmbedded()) {
+ return nullptr;
+ }
+
+ return CFGAS_GEFont::LoadFont(std::move(pPDFFont));
+ }
+ return nullptr;
+}
+
+RetainPtr<CFGAS_GEFont> CFGAS_PDFFontMgr::GetFont(
+ const WideString& wsFontFamily,
+ uint32_t dwFontStyles,
+ bool bStrictMatch) {
+ auto key = std::make_pair(wsFontFamily, dwFontStyles);
+ auto it = m_FontMap.find(key);
+ if (it != m_FontMap.end()) {
+ return it->second;
+ }
+
+ ByteString bsPsName = WideString(wsFontFamily).ToDefANSI();
+ bool bBold = FontStyleIsForceBold(dwFontStyles);
+ bool bItalic = FontStyleIsItalic(dwFontStyles);
+ ByteString strFontName = PsNameToFontName(bsPsName, bBold, bItalic);
+ RetainPtr<CFGAS_GEFont> pFont =
+ FindFont(strFontName, bBold, bItalic, bStrictMatch);
+ if (!pFont) {
+ return nullptr;
+ }
+
+ m_FontMap[key] = pFont;
+ return pFont;
+}
diff --git a/xfa/fgas/font/cfgas_pdffontmgr.h b/xfa/fgas/font/cfgas_pdffontmgr.h
index a014744..f022aa2 100644
--- a/xfa/fgas/font/cfgas_pdffontmgr.h
+++ b/xfa/fgas/font/cfgas_pdffontmgr.h
@@ -32,14 +32,6 @@
bool bBold,
bool bItalic,
bool bStrictMatch);
- ByteString PsNameToFontName(const ByteString& strPsName,
- bool bBold,
- bool bItalic);
- bool PsNameMatchDRFontName(ByteStringView bsPsName,
- bool bBold,
- bool bItalic,
- const ByteString& bsDRFontName,
- bool bStrictMatch);
UnownedPtr<const CPDF_Document> const m_pDoc;
std::map<std::pair<WideString, uint32_t>, RetainPtr<CFGAS_GEFont>> m_FontMap;