Use compound key in CXFA_FontMgr::m_FontMap.
Avoid hashing values into a dubious string key.
-- Avoid string allocations converting views back into strings.
Change-Id: I9e644ee7d6cba02ad1deefb88f245270bd27a8b3
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/92710
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/xfa/fgas/font/cfgas_defaultfontmanager.cpp b/xfa/fgas/font/cfgas_defaultfontmanager.cpp
index dc9f4c0..fa10fca 100644
--- a/xfa/fgas/font/cfgas_defaultfontmanager.cpp
+++ b/xfa/fgas/font/cfgas_defaultfontmanager.cpp
@@ -16,9 +16,8 @@
// static
RetainPtr<CFGAS_GEFont> CFGAS_DefaultFontManager::GetFont(
- WideStringView wsFontFamily,
+ WideString wsFontName,
uint32_t dwFontStyles) {
- WideString wsFontName(wsFontFamily);
CFGAS_FontMgr* pFontMgr = CFGAS_GEModule::Get()->GetFontMgr();
RetainPtr<CFGAS_GEFont> pFont = pFontMgr->LoadFont(
wsFontName.c_str(), dwFontStyles, FX_CodePage::kFailure);
diff --git a/xfa/fgas/font/cfgas_defaultfontmanager.h b/xfa/fgas/font/cfgas_defaultfontmanager.h
index 9c70060..9e4cb1c 100644
--- a/xfa/fgas/font/cfgas_defaultfontmanager.h
+++ b/xfa/fgas/font/cfgas_defaultfontmanager.h
@@ -14,7 +14,7 @@
class CFGAS_DefaultFontManager {
public:
- static RetainPtr<CFGAS_GEFont> GetFont(WideStringView wsFontFamily,
+ static RetainPtr<CFGAS_GEFont> GetFont(WideString wsFontName,
uint32_t dwFontStyles);
static RetainPtr<CFGAS_GEFont> GetDefaultFont(uint32_t dwFontStyles);
diff --git a/xfa/fgas/font/fgas_fontutils.cpp b/xfa/fgas/font/fgas_fontutils.cpp
index 4e26044..2c393fa 100644
--- a/xfa/fgas/font/fgas_fontutils.cpp
+++ b/xfa/fgas/font/fgas_fontutils.cpp
@@ -2447,8 +2447,9 @@
return nullptr;
}
-WideString FGAS_FontNameToEnglishName(WideStringView wsLocalName) {
- uint32_t dwLocalNameHash = FX_HashCode_GetLoweredW(wsLocalName);
+WideString FGAS_FontNameToEnglishName(const WideString& wsLocalName) {
+ uint32_t dwLocalNameHash =
+ FX_HashCode_GetLoweredW(wsLocalName.AsStringView());
const FGAS_FontInfo* pEnd = kXFAFontsMap + std::size(kXFAFontsMap);
const FGAS_FontInfo* pFontInfo =
std::lower_bound(kXFAFontsMap, pEnd, dwLocalNameHash,
@@ -2457,7 +2458,7 @@
});
if (pFontInfo < pEnd && pFontInfo->dwFontNameHash == dwLocalNameHash)
return WideString::FromASCII(ByteStringView(pFontInfo->pPsName));
- return WideString(wsLocalName);
+ return wsLocalName;
}
const FGAS_FontInfo* FGAS_FontInfoByFontName(WideStringView wsFontName) {
diff --git a/xfa/fgas/font/fgas_fontutils.h b/xfa/fgas/font/fgas_fontutils.h
index 03df79b..4225ac5 100644
--- a/xfa/fgas/font/fgas_fontutils.h
+++ b/xfa/fgas/font/fgas_fontutils.h
@@ -31,7 +31,7 @@
FX_CodePage wCodePage;
};
-WideString FGAS_FontNameToEnglishName(WideStringView wsLocalName);
+WideString FGAS_FontNameToEnglishName(const WideString& wsLocalName);
const FGAS_FontInfo* FGAS_FontInfoByFontName(WideStringView wsFontName);
#endif // XFA_FGAS_FONT_FGAS_FONTUTILS_H_
diff --git a/xfa/fxfa/cxfa_fontmgr.cpp b/xfa/fxfa/cxfa_fontmgr.cpp
index 882b4f7..9ec76c8 100644
--- a/xfa/fxfa/cxfa_fontmgr.cpp
+++ b/xfa/fxfa/cxfa_fontmgr.cpp
@@ -23,11 +23,10 @@
void CXFA_FontMgr::Trace(cppgc::Visitor* visitor) const {}
RetainPtr<CFGAS_GEFont> CXFA_FontMgr::GetFont(CXFA_FFDoc* hDoc,
- WideStringView wsFontFamily,
+ const WideString& wsFontFamily,
uint32_t dwFontStyles) {
- uint32_t dwHash = FX_HashCode_GetW(wsFontFamily);
- ByteString bsKey = ByteString::Format("%u%u%u", dwHash, dwFontStyles, 0xFFFF);
- auto iter = m_FontMap.find(bsKey);
+ auto key = std::make_pair(wsFontFamily, dwFontStyles);
+ auto iter = m_FontMap.find(key);
if (iter != m_FontMap.end())
return iter->second;
@@ -52,11 +51,11 @@
if (!pFont) {
pFont = CFGAS_GEFont::LoadStockFont(
- hDoc->GetPDFDoc(),
- ByteString::Format("%ls", WideString(wsFontFamily).c_str()));
+ hDoc->GetPDFDoc(), ByteString::Format("%ls", wsFontFamily.c_str()));
}
- if (pFont)
- m_FontMap[bsKey] = pFont;
+ if (!pFont)
+ return nullptr;
+ m_FontMap[key] = pFont;
return pFont;
}
diff --git a/xfa/fxfa/cxfa_fontmgr.h b/xfa/fxfa/cxfa_fontmgr.h
index d905265..800293d 100644
--- a/xfa/fxfa/cxfa_fontmgr.h
+++ b/xfa/fxfa/cxfa_fontmgr.h
@@ -8,6 +8,7 @@
#define XFA_FXFA_CXFA_FONTMGR_H_
#include <map>
+#include <utility>
#include "core/fxcrt/fx_string.h"
#include "core/fxcrt/retain_ptr.h"
@@ -24,13 +25,13 @@
void Trace(cppgc::Visitor* visitor) const;
RetainPtr<CFGAS_GEFont> GetFont(CXFA_FFDoc* hDoc,
- WideStringView wsFontFamily,
+ const WideString& wsFontFamily,
uint32_t dwFontStyles);
private:
CXFA_FontMgr();
- std::map<ByteString, RetainPtr<CFGAS_GEFont>> m_FontMap;
+ std::map<std::pair<WideString, uint32_t>, RetainPtr<CFGAS_GEFont>> m_FontMap;
};
#endif // XFA_FXFA_CXFA_FONTMGR_H_
diff --git a/xfa/fxfa/cxfa_textparser.cpp b/xfa/fxfa/cxfa_textparser.cpp
index 1877eeb..b01048d 100644
--- a/xfa/fxfa/cxfa_textparser.cpp
+++ b/xfa/fxfa/cxfa_textparser.cpp
@@ -363,7 +363,7 @@
}
CXFA_FontMgr* pFontMgr = doc->GetApp()->GetXFAFontMgr();
- return pFontMgr->GetFont(doc, wsFamily.AsStringView(), dwStyle);
+ return pFontMgr->GetFont(doc, std::move(wsFamily), dwStyle);
}
float CXFA_TextParser::GetFontSize(CXFA_TextProvider* pTextProvider,
diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp
index 5f1e444..15d859f 100644
--- a/xfa/fxfa/parser/cxfa_node.cpp
+++ b/xfa/fxfa/parser/cxfa_node.cpp
@@ -3936,8 +3936,7 @@
wsFontName = font->GetTypeface();
}
- return doc->GetApp()->GetXFAFontMgr()->GetFont(doc, wsFontName.AsStringView(),
- dwFontStyle);
+ return doc->GetApp()->GetXFAFontMgr()->GetFont(doc, wsFontName, dwFontStyle);
}
bool CXFA_Node::HasButtonRollover() const {