[xfa] Vastly simplify CFWL_FontManager. It is only ever called with a single fontname and flags, so just cache the one font we need. Change-Id: I14738e76685243624814f0df43e52bdd609c57eb Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/93793 Commit-Queue: Tom Sepez <tsepez@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/xfa/fwl/theme/cfwl_fontmanager.cpp b/xfa/fwl/theme/cfwl_fontmanager.cpp index ebf48e0..01fca70 100644 --- a/xfa/fwl/theme/cfwl_fontmanager.cpp +++ b/xfa/fwl/theme/cfwl_fontmanager.cpp
@@ -34,40 +34,9 @@ CFWL_FontManager::~CFWL_FontManager() = default; -RetainPtr<CFGAS_GEFont> CFWL_FontManager::FindFont(WideStringView wsFontFamily, - uint32_t dwFontStyles) { - for (const auto& pData : m_FontsArray) { - if (pData->Equal(wsFontFamily, dwFontStyles)) - return pData->GetFont(); +RetainPtr<CFGAS_GEFont> CFWL_FontManager::GetFWLFont() { + if (!m_pFWLFont) { + m_pFWLFont = CFGAS_GEFont::LoadFont(L"Helvetica", 0, FX_CodePage::kDefANSI); } - auto pFontData = std::make_unique<FontData>(); - if (!pFontData->LoadFont(wsFontFamily, dwFontStyles)) - return nullptr; - - m_FontsArray.push_back(std::move(pFontData)); - return m_FontsArray.back()->GetFont(); -} - -CFWL_FontManager::FontData::FontData() = default; - -CFWL_FontManager::FontData::~FontData() = default; - -bool CFWL_FontManager::FontData::Equal(WideStringView wsFontFamily, - uint32_t dwFontStyles) { - return m_wsFamily == wsFontFamily && m_dwStyles == dwFontStyles; -} - -bool CFWL_FontManager::FontData::LoadFont(WideStringView wsFontFamily, - uint32_t dwFontStyles) { - m_wsFamily = wsFontFamily; - m_dwStyles = dwFontStyles; - - // TODO(tsepez): check usage of c_str() below. - m_pFont = CFGAS_GEFont::LoadFont(wsFontFamily.unterminated_c_str(), - dwFontStyles, FX_CodePage::kDefANSI); - return !!m_pFont; -} - -RetainPtr<CFGAS_GEFont> CFWL_FontManager::FontData::GetFont() const { - return m_pFont; + return m_pFWLFont; }
diff --git a/xfa/fwl/theme/cfwl_fontmanager.h b/xfa/fwl/theme/cfwl_fontmanager.h index 36162ce..c08be4e 100644 --- a/xfa/fwl/theme/cfwl_fontmanager.h +++ b/xfa/fwl/theme/cfwl_fontmanager.h
@@ -7,13 +7,7 @@ #ifndef XFA_FWL_THEME_CFWL_FONTMANAGER_H_ #define XFA_FWL_THEME_CFWL_FONTMANAGER_H_ -#include <stdint.h> - -#include <memory> -#include <vector> - #include "core/fxcrt/retain_ptr.h" -#include "core/fxcrt/widestring.h" class CFGAS_GEFont; @@ -22,30 +16,13 @@ static CFWL_FontManager* GetInstance(); static void DestroyInstance(); - RetainPtr<CFGAS_GEFont> FindFont(WideStringView wsFontFamily, - uint32_t dwFontStyles); + RetainPtr<CFGAS_GEFont> GetFWLFont(); private: - class FontData final { - public: - FontData(); - ~FontData(); - - bool Equal(WideStringView wsFontFamily, uint32_t dwFontStyles); - bool LoadFont(WideStringView wsFontFamily, uint32_t dwFontStyles); - - RetainPtr<CFGAS_GEFont> GetFont() const; - - private: - WideString m_wsFamily; - uint32_t m_dwStyles = 0; - RetainPtr<CFGAS_GEFont> m_pFont; - }; - CFWL_FontManager(); ~CFWL_FontManager(); - std::vector<std::unique_ptr<FontData>> m_FontsArray; + RetainPtr<CFGAS_GEFont> m_pFWLFont; }; #endif // XFA_FWL_THEME_CFWL_FONTMANAGER_H_
diff --git a/xfa/fwl/theme/cfwl_widgettp.cpp b/xfa/fwl/theme/cfwl_widgettp.cpp index 7220296..34193fb 100644 --- a/xfa/fwl/theme/cfwl_widgettp.cpp +++ b/xfa/fwl/theme/cfwl_widgettp.cpp
@@ -78,7 +78,7 @@ if (m_pTextOut) return; - m_pFGASFont = CFWL_FontManager::GetInstance()->FindFont(L"Helvetica", 0); + m_pFGASFont = CFWL_FontManager::GetInstance()->GetFWLFont(); m_pTextOut = std::make_unique<CFDE_TextOut>(); m_pTextOut->SetFont(m_pFGASFont); m_pTextOut->SetFontSize(FWLTHEME_CAPACITY_FontSize);