Remove CFWL_FontManager

Make font loading occur through IFWL callbacks to a higher layer,
getting the FWL layer out of the font loading business.

Change-Id: I64cc52c7124c332ef29e79fc88dcb9d66c534508
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/93870
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/xfa/fwl/BUILD.gn b/xfa/fwl/BUILD.gn
index 6036e6a..8b094ad 100644
--- a/xfa/fwl/BUILD.gn
+++ b/xfa/fwl/BUILD.gn
@@ -92,8 +92,6 @@
     "theme/cfwl_datetimepickertp.h",
     "theme/cfwl_edittp.cpp",
     "theme/cfwl_edittp.h",
-    "theme/cfwl_fontmanager.cpp",
-    "theme/cfwl_fontmanager.h",
     "theme/cfwl_listboxtp.cpp",
     "theme/cfwl_listboxtp.h",
     "theme/cfwl_monthcalendartp.cpp",
diff --git a/xfa/fwl/ifwl_themeprovider.h b/xfa/fwl/ifwl_themeprovider.h
index 8f938eb..8793926 100644
--- a/xfa/fwl/ifwl_themeprovider.h
+++ b/xfa/fwl/ifwl_themeprovider.h
@@ -36,8 +36,8 @@
   virtual float GetCYBorderSize() const = 0;
   virtual CFX_RectF GetUIMargin(const CFWL_ThemePart& pThemePart) const = 0;
   virtual float GetFontSize(const CFWL_ThemePart& pThemePart) const = 0;
-  virtual RetainPtr<CFGAS_GEFont> GetFont(
-      const CFWL_ThemePart& pThemePart) const = 0;
+  virtual RetainPtr<CFGAS_GEFont> GetFont(const CFWL_ThemePart& pThemePart) = 0;
+  virtual RetainPtr<CFGAS_GEFont> GetFWLFont() = 0;
   virtual float GetLineHeight(const CFWL_ThemePart& pThemePart) const = 0;
   virtual float GetScrollBarWidth() const = 0;
   virtual FX_COLORREF GetTextColor(const CFWL_ThemePart& pThemePart) const = 0;
diff --git a/xfa/fwl/theme/cfwl_checkboxtp.cpp b/xfa/fwl/theme/cfwl_checkboxtp.cpp
index 0270e84..3753c30 100644
--- a/xfa/fwl/theme/cfwl_checkboxtp.cpp
+++ b/xfa/fwl/theme/cfwl_checkboxtp.cpp
@@ -38,7 +38,7 @@
 CFWL_CheckBoxTP::~CFWL_CheckBoxTP() = default;
 
 void CFWL_CheckBoxTP::DrawText(const CFWL_ThemeText& pParams) {
-  EnsureTTOInitialized();
+  EnsureTTOInitialized(pParams.GetWidget()->GetThemeProvider());
   m_pTextOut->SetTextColor(pParams.m_dwStates & CFWL_PartState::kDisabled
                                ? FWLTHEME_CAPACITY_TextDisColor
                                : FWLTHEME_CAPACITY_TextColor);
diff --git a/xfa/fwl/theme/cfwl_fontmanager.cpp b/xfa/fwl/theme/cfwl_fontmanager.cpp
deleted file mode 100644
index 01fca70..0000000
--- a/xfa/fwl/theme/cfwl_fontmanager.cpp
+++ /dev/null
@@ -1,42 +0,0 @@
-// Copyright 2020 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/fwl/theme/cfwl_fontmanager.h"
-
-#include <utility>
-
-#include "core/fxcrt/fx_codepage.h"
-#include "xfa/fgas/font/cfgas_gefont.h"
-
-namespace {
-
-CFWL_FontManager* g_FontManager = nullptr;
-
-}  // namespace
-
-// static
-CFWL_FontManager* CFWL_FontManager::GetInstance() {
-  if (!g_FontManager)
-    g_FontManager = new CFWL_FontManager;
-  return g_FontManager;
-}
-
-// static
-void CFWL_FontManager::DestroyInstance() {
-  delete g_FontManager;
-  g_FontManager = nullptr;
-}
-
-CFWL_FontManager::CFWL_FontManager() = default;
-
-CFWL_FontManager::~CFWL_FontManager() = default;
-
-RetainPtr<CFGAS_GEFont> CFWL_FontManager::GetFWLFont() {
-  if (!m_pFWLFont) {
-    m_pFWLFont = CFGAS_GEFont::LoadFont(L"Helvetica", 0, FX_CodePage::kDefANSI);
-  }
-  return m_pFWLFont;
-}
diff --git a/xfa/fwl/theme/cfwl_fontmanager.h b/xfa/fwl/theme/cfwl_fontmanager.h
deleted file mode 100644
index c08be4e..0000000
--- a/xfa/fwl/theme/cfwl_fontmanager.h
+++ /dev/null
@@ -1,28 +0,0 @@
-// Copyright 2020 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_FWL_THEME_CFWL_FONTMANAGER_H_
-#define XFA_FWL_THEME_CFWL_FONTMANAGER_H_
-
-#include "core/fxcrt/retain_ptr.h"
-
-class CFGAS_GEFont;
-
-class CFWL_FontManager final {
- public:
-  static CFWL_FontManager* GetInstance();
-  static void DestroyInstance();
-
-  RetainPtr<CFGAS_GEFont> GetFWLFont();
-
- private:
-  CFWL_FontManager();
-  ~CFWL_FontManager();
-
-  RetainPtr<CFGAS_GEFont> m_pFWLFont;
-};
-
-#endif  // XFA_FWL_THEME_CFWL_FONTMANAGER_H_
diff --git a/xfa/fwl/theme/cfwl_monthcalendartp.cpp b/xfa/fwl/theme/cfwl_monthcalendartp.cpp
index 8b85af6..8ffc33e 100644
--- a/xfa/fwl/theme/cfwl_monthcalendartp.cpp
+++ b/xfa/fwl/theme/cfwl_monthcalendartp.cpp
@@ -84,7 +84,7 @@
 }
 
 void CFWL_MonthCalendarTP::DrawText(const CFWL_ThemeText& pParams) {
-  EnsureTTOInitialized();
+  EnsureTTOInitialized(pParams.GetWidget()->GetThemeProvider());
   if (pParams.m_iPart == CFWL_ThemePart::Part::kDatesIn &&
       !(pParams.m_dwStates & CFWL_PartState::kFlagged) &&
       (pParams.m_dwStates & Mask<CFWL_PartState>{CFWL_PartState::kHovered,
diff --git a/xfa/fwl/theme/cfwl_widgettp.cpp b/xfa/fwl/theme/cfwl_widgettp.cpp
index 34193fb..6aec514 100644
--- a/xfa/fwl/theme/cfwl_widgettp.cpp
+++ b/xfa/fwl/theme/cfwl_widgettp.cpp
@@ -20,7 +20,6 @@
 #include "xfa/fwl/cfwl_widget.h"
 #include "xfa/fwl/cfwl_widgetmgr.h"
 #include "xfa/fwl/ifwl_themeprovider.h"
-#include "xfa/fwl/theme/cfwl_fontmanager.h"
 
 CFWL_WidgetTP::CFWL_WidgetTP() = default;
 
@@ -31,7 +30,7 @@
 void CFWL_WidgetTP::DrawBackground(const CFWL_ThemeBackground& pParams) {}
 
 void CFWL_WidgetTP::DrawText(const CFWL_ThemeText& pParams) {
-  EnsureTTOInitialized();
+  EnsureTTOInitialized(pParams.GetWidget()->GetThemeProvider());
   if (pParams.m_wsText.IsEmpty())
     return;
 
@@ -47,10 +46,6 @@
                             pParams.m_PartRect);
 }
 
-const RetainPtr<CFGAS_GEFont>& CFWL_WidgetTP::GetFont() const {
-  return m_pFGASFont;
-}
-
 void CFWL_WidgetTP::InitializeArrowColorData() {
   if (m_pColorData)
     return;
@@ -74,13 +69,12 @@
   m_pColorData->clrSign[3] = ArgbEncode(255, 128, 128, 128);
 }
 
-void CFWL_WidgetTP::EnsureTTOInitialized() {
+void CFWL_WidgetTP::EnsureTTOInitialized(IFWL_ThemeProvider* pProvider) {
   if (m_pTextOut)
     return;
 
-  m_pFGASFont = CFWL_FontManager::GetInstance()->GetFWLFont();
   m_pTextOut = std::make_unique<CFDE_TextOut>();
-  m_pTextOut->SetFont(m_pFGASFont);
+  m_pTextOut->SetFont(pProvider->GetFWLFont());
   m_pTextOut->SetFontSize(FWLTHEME_CAPACITY_FontSize);
   m_pTextOut->SetTextColor(FWLTHEME_CAPACITY_TextColor);
 }
diff --git a/xfa/fwl/theme/cfwl_widgettp.h b/xfa/fwl/theme/cfwl_widgettp.h
index b206d73..684fb92 100644
--- a/xfa/fwl/theme/cfwl_widgettp.h
+++ b/xfa/fwl/theme/cfwl_widgettp.h
@@ -17,10 +17,10 @@
 #include "xfa/fwl/theme/cfwl_utils.h"
 
 class CFDE_TextOut;
-class CFGAS_GEFont;
 class CFGAS_GEGraphics;
 class CFWL_ThemeBackground;
 class CFWL_ThemeText;
+class IFWL_ThemeProvider;
 
 class CFWL_WidgetTP : public cppgc::GarbageCollected<CFWL_WidgetTP> {
  public:
@@ -29,8 +29,6 @@
   virtual void DrawBackground(const CFWL_ThemeBackground& pParams);
   virtual void DrawText(const CFWL_ThemeText& pParams);
 
-  const RetainPtr<CFGAS_GEFont>& GetFont() const;
-
   // Non-virtual, nothing to trace in subclasses at present.
   void Trace(cppgc::Visitor* visitor) const;
 
@@ -45,7 +43,7 @@
   CFWL_WidgetTP();
 
   void InitializeArrowColorData();
-  void EnsureTTOInitialized();
+  void EnsureTTOInitialized(IFWL_ThemeProvider* pProvider);
 
   void DrawBorder(CFGAS_GEGraphics* pGraphics,
                   const CFX_RectF& rect,
@@ -76,7 +74,6 @@
                     const CFX_Matrix& matrix);
 
   std::unique_ptr<CFDE_TextOut> m_pTextOut;
-  RetainPtr<CFGAS_GEFont> m_pFGASFont;
   std::unique_ptr<CColorData> m_pColorData;
 };
 
diff --git a/xfa/fxfa/cxfa_fwltheme.cpp b/xfa/fxfa/cxfa_fwltheme.cpp
index ca00815..5fd98e4 100644
--- a/xfa/fxfa/cxfa_fwltheme.cpp
+++ b/xfa/fxfa/cxfa_fwltheme.cpp
@@ -25,7 +25,6 @@
 #include "xfa/fwl/cfwl_scrollbar.h"
 #include "xfa/fwl/cfwl_themebackground.h"
 #include "xfa/fwl/cfwl_themetext.h"
-#include "xfa/fwl/theme/cfwl_fontmanager.h"
 #include "xfa/fwl/theme/cfwl_widgettp.h"
 #include "xfa/fxfa/cxfa_ffapp.h"
 #include "xfa/fxfa/cxfa_ffwidget.h"
@@ -59,7 +58,6 @@
 
 void CXFA_FWLTheme::PreFinalize() {
   m_pTextOut.reset();
-  CFWL_FontManager::DestroyInstance();
 }
 
 void CXFA_FWLTheme::Trace(cppgc::Visitor* visitor) const {
@@ -185,10 +183,18 @@
 }
 
 RetainPtr<CFGAS_GEFont> CXFA_FWLTheme::GetFont(
-    const CFWL_ThemePart& pThemePart) const {
+    const CFWL_ThemePart& pThemePart) {
   if (CXFA_FFWidget* pWidget = GetOutmostFFWidget(pThemePart.GetWidget()))
     return pWidget->GetNode()->GetFGASFont(pWidget->GetDoc());
-  return GetTheme(pThemePart.GetWidget())->GetFont();
+
+  return GetFWLFont();
+}
+
+RetainPtr<CFGAS_GEFont> CXFA_FWLTheme::GetFWLFont() {
+  if (!m_pFWLFont)
+    m_pFWLFont = CFGAS_GEFont::LoadFont(L"Helvetica", 0, FX_CodePage::kDefANSI);
+
+  return m_pFWLFont;
 }
 
 float CXFA_FWLTheme::GetLineHeight(const CFWL_ThemePart& pThemePart) const {
diff --git a/xfa/fxfa/cxfa_fwltheme.h b/xfa/fxfa/cxfa_fwltheme.h
index 23b5243..719fccf 100644
--- a/xfa/fxfa/cxfa_fwltheme.h
+++ b/xfa/fxfa/cxfa_fwltheme.h
@@ -40,8 +40,8 @@
   float GetCYBorderSize() const override;
   CFX_RectF GetUIMargin(const CFWL_ThemePart& pThemePart) const override;
   float GetFontSize(const CFWL_ThemePart& pThemePart) const override;
-  RetainPtr<CFGAS_GEFont> GetFont(
-      const CFWL_ThemePart& pThemePart) const override;
+  RetainPtr<CFGAS_GEFont> GetFont(const CFWL_ThemePart& pThemePart) override;
+  RetainPtr<CFGAS_GEFont> GetFWLFont() override;
   float GetLineHeight(const CFWL_ThemePart& pThemePart) const override;
   float GetScrollBarWidth() const override;
   FX_COLORREF GetTextColor(const CFWL_ThemePart& pThemePart) const override;
@@ -53,6 +53,7 @@
   CXFA_FWLTheme(cppgc::Heap* pHeap, CXFA_FFApp* pApp);
 
   std::unique_ptr<CFDE_TextOut> m_pTextOut;
+  RetainPtr<CFGAS_GEFont> m_pFWLFont;
   RetainPtr<CFGAS_GEFont> m_pCalendarFont;
   cppgc::Member<CXFA_FFApp> const m_pApp;
   WideString m_wsResource;