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 <dsinclair@chromium.org>
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 2cf6552..aad97e9 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1488,6 +1488,8 @@
       "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 @@
       "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 8e97036..946486b 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 c710ed9..404b23c 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> CFGAS_GEFont::LoadFont(const wchar_t* pszFontFamily,
diff --git a/xfa/fgas/font/cfgas_gefont.h b/xfa/fgas/font/cfgas_gefont.h
index 13ac48f..c83fc43 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 @@
   bool GetBBox(CFX_Rect* bbox);
   CFX_RetainPtr<CFGAS_GEFont> 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 @@
   bool m_bExternalFont;
   CFX_RetainPtr<CFGAS_GEFont> 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<CFX_SeekableStreamProxy> m_pStream;
   CFX_RetainPtr<IFX_SeekableReadStream> m_pFileRead;
   std::unique_ptr<CFX_UnicodeEncoding> m_pFontEncoding;
diff --git a/xfa/fxfa/cxfa_pdffontmgr.cpp b/xfa/fgas/font/cfgas_pdffontmgr.cpp
similarity index 70%
rename from xfa/fxfa/cxfa_pdffontmgr.cpp
rename to xfa/fgas/font/cfgas_pdffontmgr.cpp
index 9a90e54..2b8e30e 100644
--- a/xfa/fxfa/cxfa_pdffontmgr.cpp
+++ b/xfa/fgas/font/cfgas_pdffontmgr.cpp
@@ -4,13 +4,14 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
-#include "xfa/fxfa/cxfa_pdffontmgr.h"
+#include "xfa/fgas/font/cfgas_pdffontmgr.h"
 
 #include <algorithm>
 
 #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"
-#include "xfa/fxfa/cxfa_ffapp.h"
 
 namespace {
 
@@ -23,22 +24,22 @@
 
 }  // namespace
 
-CXFA_PDFFontMgr::CXFA_PDFFontMgr(CXFA_FFDoc* pDoc) : m_pDoc(pDoc) {}
+CFGAS_PDFFontMgr::CFGAS_PDFFontMgr(CPDF_Document* pDoc, CFGAS_FontMgr* pFontMgr)
+    : m_pDoc(pDoc), m_pFontMgr(pFontMgr) {
+  ASSERT(pDoc);
+  ASSERT(pFontMgr);
+}
 
-CXFA_PDFFontMgr::~CXFA_PDFFontMgr() {}
+CFGAS_PDFFontMgr::~CFGAS_PDFFontMgr() {}
 
-CFX_RetainPtr<CFGAS_GEFont> CXFA_PDFFontMgr::FindFont(
+CFX_RetainPtr<CFGAS_GEFont> CFGAS_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");
+      m_pDoc->GetRoot()->GetDictFor("AcroForm")->GetDictFor("DR");
   if (!pFontSetDict)
     return nullptr;
 
@@ -48,7 +49,6 @@
 
   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();
@@ -57,23 +57,23 @@
       continue;
     }
     CPDF_Dictionary* pFontDict = ToDictionary(pObj->GetDirect());
-    if (!pFontDict || pFontDict->GetStringFor("Type") != "Font") {
+    if (!pFontDict || pFontDict->GetStringFor("Type") != "Font")
       return nullptr;
-    }
-    CPDF_Font* pPDFFont = pDoc->LoadFont(pFontDict);
-    if (!pPDFFont) {
+
+    CPDF_Font* pPDFFont = m_pDoc->LoadFont(pFontDict);
+    if (!pPDFFont)
       return nullptr;
-    }
+
     if (!pPDFFont->IsEmbedded()) {
       *pDstPDFFont = pPDFFont;
       return nullptr;
     }
-    return CFGAS_GEFont::LoadFont(pPDFFont->GetFont(), pFDEFontMgr);
+    return CFGAS_GEFont::LoadFont(pPDFFont->GetFont(), m_pFontMgr.Get());
   }
   return nullptr;
 }
 
-CFX_RetainPtr<CFGAS_GEFont> CXFA_PDFFontMgr::GetFont(
+CFX_RetainPtr<CFGAS_GEFont> CFGAS_PDFFontMgr::GetFont(
     const WideStringView& wsFontFamily,
     uint32_t dwFontStyles,
     CPDF_Font** pPDFFont,
@@ -84,6 +84,7 @@
   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;
@@ -92,12 +93,13 @@
       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) {
+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;
@@ -111,11 +113,11 @@
   return strPsName;
 }
 
-bool CXFA_PDFFontMgr::PsNameMatchDRFontName(const ByteStringView& bsPsName,
-                                            bool bBold,
-                                            bool bItalic,
-                                            const ByteString& bsDRFontName,
-                                            bool bStrictMatch) {
+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();
@@ -137,15 +139,15 @@
                                bsDRName.GetLength() - iBoldIndex.value() - 4);
     }
     bool bItalicFont = true;
-    if (bsDRName.Contains("Italic")) {
+    if (bsDRName.Contains("Italic"))
       iDifferLength -= 6;
-    } else if (bsDRName.Contains("It")) {
+    else if (bsDRName.Contains("It"))
       iDifferLength -= 2;
-    } else if (bsDRName.Contains("Oblique")) {
+    else if (bsDRName.Contains("Oblique"))
       iDifferLength -= 7;
-    } else {
+    else
       bItalicFont = false;
-    }
+
     if (bItalic != bItalicFont)
       return false;
 
@@ -160,21 +162,20 @@
 
       bool bMatch = false;
       switch (bsPsName[iPsLen - 1]) {
-        case 'L': {
-          if (bsDRName.Right(5) == "Light") {
+        case 'L':
+          if (bsDRName.Right(5) == "Light")
             bMatch = true;
-          }
-        } break;
-        case 'R': {
-          if (bsDRName.Right(7) == "Regular" || bsDRName.Right(3) == "Reg") {
+
+          break;
+        case 'R':
+          if (bsDRName.Right(7) == "Regular" || bsDRName.Right(3) == "Reg")
             bMatch = true;
-          }
-        } break;
-        case 'M': {
-          if (bsDRName.Right(5) == "Medium") {
+
+          break;
+        case 'M':
+          if (bsDRName.Right(5) == "Medium")
             bMatch = true;
-          }
-        } break;
+          break;
         default:
           break;
       }
@@ -184,10 +185,10 @@
   return true;
 }
 
-bool CXFA_PDFFontMgr::GetCharWidth(const CFX_RetainPtr<CFGAS_GEFont>& pFont,
-                                   wchar_t wUnicode,
-                                   bool bCharCode,
-                                   int32_t* pWidth) {
+bool CFGAS_PDFFontMgr::GetCharWidth(const CFX_RetainPtr<CFGAS_GEFont>& pFont,
+                                    wchar_t wUnicode,
+                                    bool bCharCode,
+                                    int32_t* pWidth) {
   if (wUnicode != 0x20 || bCharCode)
     return false;
 
@@ -200,7 +201,7 @@
   return true;
 }
 
-void CXFA_PDFFontMgr::SetFont(const CFX_RetainPtr<CFGAS_GEFont>& pFont,
-                              CPDF_Font* pPDFFont) {
+void CFGAS_PDFFontMgr::SetFont(const CFX_RetainPtr<CFGAS_GEFont>& pFont,
+                               CPDF_Font* pPDFFont) {
   m_FDE2PDFFont[pFont] = pPDFFont;
 }
diff --git a/xfa/fxfa/cxfa_pdffontmgr.h b/xfa/fgas/font/cfgas_pdffontmgr.h
similarity index 80%
rename from xfa/fxfa/cxfa_pdffontmgr.h
rename to xfa/fgas/font/cfgas_pdffontmgr.h
index 4c8941f..8a09a95 100644
--- a/xfa/fxfa/cxfa_pdffontmgr.h
+++ b/xfa/fgas/font/cfgas_pdffontmgr.h
@@ -4,8 +4,8 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
-#ifndef XFA_FXFA_CXFA_PDFFONTMGR_H_
-#define XFA_FXFA_CXFA_PDFFONTMGR_H_
+#ifndef XFA_FGAS_FONT_CFGAS_PDFFONTMGR_H_
+#define XFA_FGAS_FONT_CFGAS_PDFFONTMGR_H_
 
 #include <map>
 
@@ -14,16 +14,16 @@
 #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_FontMgr;
 class CFGAS_GEFont;
+class CPDF_Document;
 class CPDF_Font;
-class CXFA_FFDoc;
 
-class CXFA_PDFFontMgr : public CFX_Observable<CXFA_PDFFontMgr> {
+class CFGAS_PDFFontMgr : public CFX_Observable<CFGAS_PDFFontMgr> {
  public:
-  explicit CXFA_PDFFontMgr(CXFA_FFDoc* pDoc);
-  ~CXFA_PDFFontMgr();
+  explicit CFGAS_PDFFontMgr(CPDF_Document* pDoc, CFGAS_FontMgr* pFontMgr);
+  ~CFGAS_PDFFontMgr();
 
   CFX_RetainPtr<CFGAS_GEFont> GetFont(const WideStringView& wsFontFamily,
                                       uint32_t dwFontStyles,
@@ -50,9 +50,10 @@
                              const ByteString& bsDRFontName,
                              bool bStrictMatch);
 
-  CFX_UnownedPtr<CXFA_FFDoc> const m_pDoc;
+  CFX_UnownedPtr<CPDF_Document> const m_pDoc;
+  CFX_UnownedPtr<CFGAS_FontMgr> const m_pFontMgr;
   std::map<CFX_RetainPtr<CFGAS_GEFont>, CPDF_Font*> m_FDE2PDFFont;
   std::map<ByteString, CFX_RetainPtr<CFGAS_GEFont>> m_FontMap;
 };
 
-#endif  // XFA_FXFA_CXFA_PDFFONTMGR_H_
+#endif  // XFA_FGAS_FONT_CFGAS_PDFFONTMGR_H_
diff --git a/xfa/fxfa/cxfa_ffdatetimeedit.cpp b/xfa/fxfa/cxfa_ffdatetimeedit.cpp
index 643d384..5ed1690 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 9b87c40..d6e1def 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 e050a69..7e2f6cd 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 8f68368..dbfb52e 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 b47ce6b..7d2ad70 100644
--- a/xfa/fxfa/cxfa_fontmgr.cpp
+++ b/xfa/fxfa/cxfa_fontmgr.cpp
@@ -1748,7 +1748,7 @@
 
   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<CFGAS_GEFont> pFont;
@@ -1784,7 +1784,8 @@
 
 void CXFA_FontMgr::LoadDocFonts(CXFA_FFDoc* hDoc) {
   if (!m_PDFFontMgrMap[hDoc])
-    m_PDFFontMgrMap[hDoc] = pdfium::MakeUnique<CXFA_PDFFontMgr>(hDoc);
+    m_PDFFontMgrMap[hDoc] = pdfium::MakeUnique<CFGAS_PDFFontMgr>(
+        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 e5db565..2e33562 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 @@
 
  private:
   std::unique_ptr<CXFA_DefFontMgr> m_pDefFontMgr;
-  std::map<CXFA_FFDoc*, std::unique_ptr<CXFA_PDFFontMgr>> m_PDFFontMgrMap;
+  std::map<CXFA_FFDoc*, std::unique_ptr<CFGAS_PDFFontMgr>> m_PDFFontMgrMap;
   std::map<ByteString, CFX_RetainPtr<CFGAS_GEFont>> m_FontMap;
 };