Revert of Pdfium: Fix fonts leaking on ClosePage. (patchset #10 id:180001 of https://codereview.chromium.org/2158023002/ )

Reason for revert:
Causes heap-use-after-free. See crbug.com/647612.

Original issue's description:
> Fix memory leaking on ClosePage.
> CFX_FontCache refactoring:
>   after this CL: Only one global CFX_FontCache used. Any cached items from it, are released, when its are not used.
>
> BUG=79367,48791
>
> The fonts was not cleared after unloading pages.
>
> Test pdf:
>
> http://www.nasa.gov/pdf/750614main_NASA_FY_2014_Budget_Estimates-508.pdf
>
> For this file, we have ~5 fonts per page, which equal ~1 Mb per page.
> In this PDF we have 670 pages, as result after slow scrolling(reading) full document we have ~600 Mb fonts data in memory.
>
> memory usage of PDF Plugin:
>   before this CL: ~660 Mb
>   after this CL: ~100 Mb
>
> Committed: https://pdfium.googlesource.com/pdfium/+/cde5101eb15b24519e89fa500fe37038bc8e2201

TBR=tsepez@chromium.org,brucedawson@chromium.org,npm@chromium.org,art-snake@yandex-team.ru
# Not skipping CQ checks because original CL landed more than 1 days ago.
BUG=79367,48791

Review-Url: https://codereview.chromium.org/2350763002
diff --git a/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp b/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp
index f83d6fa..9e586e3 100644
--- a/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp
+++ b/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp
@@ -214,10 +214,9 @@
     return;
 
   pFontData->RemoveRef();
-  if (pFontData->use_count() > 1)
+  if (pFontData->use_count() != 0)
     return;
 
-  // We have font data only in m_FontMap cache. Clean it.
   pFontData->clear();
 }
 
@@ -331,10 +330,9 @@
     return;
 
   pCountedColorSpace->RemoveRef();
-  if (pCountedColorSpace->use_count() > 1)
+  if (pCountedColorSpace->use_count() != 0)
     return;
 
-  // We have item only in m_ColorSpaceMap cache. Clean it.
   pCountedColorSpace->get()->ReleaseCS();
   pCountedColorSpace->reset(nullptr);
 }
@@ -393,10 +391,9 @@
     return;
 
   pPattern->RemoveRef();
-  if (pPattern->use_count() > 1)
+  if (pPattern->use_count() != 0)
     return;
 
-  // We have item only in m_PatternMap cache. Clean it.
   pPattern->clear();
 }
 
@@ -432,10 +429,9 @@
     return;
 
   pCountedImage->RemoveRef();
-  if (pCountedImage->use_count() > 1)
+  if (pCountedImage->use_count() != 0)
     return;
 
-  // We have item only in m_ImageMap cache. Clean it.
   delete pCountedImage->get();
   delete pCountedImage;
   m_ImageMap.erase(it);
@@ -458,8 +454,7 @@
   auto hash_it = m_HashProfileMap.find(bsDigest);
   if (hash_it != m_HashProfileMap.end()) {
     auto it_copied_stream = m_IccProfileMap.find(hash_it->second);
-    if (it_copied_stream != m_IccProfileMap.end())
-      return it_copied_stream->second->AddRef();
+    return it_copied_stream->second->AddRef();
   }
   CPDF_IccProfile* pProfile =
       new CPDF_IccProfile(stream.GetData(), stream.GetSize());
@@ -478,8 +473,7 @@
       continue;
 
     profile->RemoveRef();
-    if (profile->use_count() == 1) {
-      // We have item only in m_IccProfileMap cache. Clean it.
+    if (profile->use_count() == 0) {
       delete profile->get();
       delete profile;
       m_IccProfileMap.erase(it);
@@ -524,10 +518,9 @@
     return;
 
   pCountedStream->RemoveRef();
-  if (pCountedStream->use_count() > 1)
+  if (pCountedStream->use_count() != 0)
     return;
 
-  // We have item only in m_FontFileMap cache. Clean it.
   delete pCountedStream->get();
   delete pCountedStream;
   m_FontFileMap.erase(it);
diff --git a/core/fpdfapi/fpdf_parser/cpdf_document.cpp b/core/fpdfapi/fpdf_parser/cpdf_document.cpp
index d3909ce..b2a3cd4 100644
--- a/core/fpdfapi/fpdf_parser/cpdf_document.cpp
+++ b/core/fpdfapi/fpdf_parser/cpdf_document.cpp
@@ -21,6 +21,7 @@
 #include "core/fpdfapi/fpdf_render/render_int.h"
 #include "core/fpdfapi/include/cpdf_modulemgr.h"
 #include "core/fxcodec/include/JBig2_DocumentContext.h"
+#include "core/fxge/include/cfx_fontcache.h"
 #include "core/fxge/include/cfx_unicodeencoding.h"
 #include "core/fxge/include/fx_font.h"
 #include "third_party/base/stl_util.h"
diff --git a/core/fpdfapi/fpdf_render/fpdf_render.cpp b/core/fpdfapi/fpdf_render/fpdf_render.cpp
index 8043f93..59c8397 100644
--- a/core/fpdfapi/fpdf_render/fpdf_render.cpp
+++ b/core/fpdfapi/fpdf_render/fpdf_render.cpp
@@ -30,13 +30,14 @@
 #include "core/fpdfapi/fpdf_render/include/cpdf_textrenderer.h"
 #include "core/fpdfapi/include/cpdf_modulemgr.h"
 #include "core/fpdfdoc/include/cpdf_occontext.h"
+#include "core/fxge/include/cfx_fontcache.h"
 #include "core/fxge/include/cfx_fxgedevice.h"
 #include "core/fxge/include/cfx_graphstatedata.h"
 #include "core/fxge/include/cfx_pathdata.h"
 #include "core/fxge/include/cfx_renderdevice.h"
 
 CPDF_DocRenderData::CPDF_DocRenderData(CPDF_Document* pPDFDoc)
-    : m_pPDFDoc(pPDFDoc) {}
+    : m_pPDFDoc(pPDFDoc), m_pFontCache(new CFX_FontCache) {}
 
 CPDF_DocRenderData::~CPDF_DocRenderData() {
   Clear(TRUE);
@@ -62,6 +63,15 @@
       m_TransferFuncMap.erase(curr_it);
     }
   }
+
+  if (m_pFontCache) {
+    if (bRelease) {
+      delete m_pFontCache;
+      m_pFontCache = nullptr;
+    } else {
+      m_pFontCache->FreeCache(FALSE);
+    }
+  }
 }
 
 CPDF_Type3Cache* CPDF_DocRenderData::GetCachedType3(CPDF_Type3Font* pFont) {
diff --git a/core/fpdfapi/fpdf_render/fpdf_render_text.cpp b/core/fpdfapi/fpdf_render/fpdf_render_text.cpp
index 991b57a..265948f 100644
--- a/core/fpdfapi/fpdf_render/fpdf_render_text.cpp
+++ b/core/fpdfapi/fpdf_render/fpdf_render_text.cpp
@@ -25,6 +25,7 @@
 #include "core/fpdfapi/fpdf_render/include/cpdf_textrenderer.h"
 #include "core/fxge/include/cfx_autofontcache.h"
 #include "core/fxge/include/cfx_facecache.h"
+#include "core/fxge/include/cfx_fontcache.h"
 #include "core/fxge/include/cfx_fxgedevice.h"
 #include "core/fxge/include/cfx_gemodule.h"
 #include "core/fxge/include/cfx_graphstatedata.h"
@@ -434,6 +435,9 @@
                                         FX_ARGB stroke_argb,
                                         CFX_PathData* pClippingPath,
                                         int nFlag) {
+  CFX_FontCache* pCache =
+      pFont->m_pDocument ? pFont->m_pDocument->GetRenderData()->GetFontCache()
+                         : nullptr;
   CPDF_CharPosList CharPosList;
   CharPosList.Load(nChars, pCharCodes, pCharPos, pFont, font_size);
   if (CharPosList.m_nChars == 0)
@@ -448,10 +452,10 @@
     auto* font = fontPosition == -1
                      ? &pFont->m_Font
                      : pFont->m_FontFallbacks[fontPosition].get();
-    if (!pDevice->DrawTextPath(i - startIndex,
-                               CharPosList.m_pCharPos + startIndex, font,
-                               font_size, pText2User, pUser2Device, pGraphState,
-                               fill_argb, stroke_argb, pClippingPath, nFlag)) {
+    if (!pDevice->DrawTextPath(
+            i - startIndex, CharPosList.m_pCharPos + startIndex, font, pCache,
+            font_size, pText2User, pUser2Device, pGraphState, fill_argb,
+            stroke_argb, pClippingPath, nFlag)) {
       bDraw = false;
     }
     fontPosition = curFontPosition;
@@ -460,7 +464,7 @@
   auto* font = fontPosition == -1 ? &pFont->m_Font
                                   : pFont->m_FontFallbacks[fontPosition].get();
   if (!pDevice->DrawTextPath(CharPosList.m_nChars - startIndex,
-                             CharPosList.m_pCharPos + startIndex, font,
+                             CharPosList.m_pCharPos + startIndex, font, pCache,
                              font_size, pText2User, pUser2Device, pGraphState,
                              fill_argb, stroke_argb, pClippingPath, nFlag)) {
     bDraw = false;
@@ -536,6 +540,9 @@
                                           const CFX_Matrix* pText2Device,
                                           FX_ARGB fill_argb,
                                           const CPDF_RenderOptions* pOptions) {
+  CFX_FontCache* pCache =
+      pFont->m_pDocument ? pFont->m_pDocument->GetRenderData()->GetFontCache()
+                         : nullptr;
   CPDF_CharPosList CharPosList;
   CharPosList.Load(nChars, pCharCodes, pCharPos, pFont, font_size);
   if (CharPosList.m_nChars == 0)
@@ -578,7 +585,7 @@
                      ? &pFont->m_Font
                      : pFont->m_FontFallbacks[fontPosition].get();
     if (!pDevice->DrawNormalText(
-            i - startIndex, CharPosList.m_pCharPos + startIndex, font,
+            i - startIndex, CharPosList.m_pCharPos + startIndex, font, pCache,
             font_size, pText2Device, fill_argb, FXGE_flags)) {
       bDraw = false;
     }
@@ -589,7 +596,7 @@
                                   : pFont->m_FontFallbacks[fontPosition].get();
   if (!pDevice->DrawNormalText(CharPosList.m_nChars - startIndex,
                                CharPosList.m_pCharPos + startIndex, font,
-                               font_size, pText2Device, fill_argb,
+                               pCache, font_size, pText2Device, fill_argb,
                                FXGE_flags)) {
     bDraw = false;
   }
@@ -620,9 +627,23 @@
     RenderSingleObject(&path, pObj2Device);
     return;
   }
+  CFX_FontCache* pCache;
+  if (pFont->m_pDocument) {
+    pCache = pFont->m_pDocument->GetRenderData()->GetFontCache();
+  } else {
+    pCache = CFX_GEModule::Get()->GetFontCache();
+  }
   CPDF_CharPosList CharPosList;
   CharPosList.Load(textobj->m_nChars, textobj->m_pCharCodes,
                    textobj->m_pCharPos, pFont, font_size);
+  std::vector<CFX_FaceCache*> faceCaches;
+  std::vector<CFX_AutoFontCache> autoFontCaches;
+  faceCaches.push_back(pCache->GetCachedFace(&pFont->m_Font));
+  autoFontCaches.push_back(CFX_AutoFontCache(pCache, &pFont->m_Font));
+  for (const auto& font : pFont->m_FontFallbacks) {
+    faceCaches.push_back(pCache->GetCachedFace(font.get()));
+    autoFontCaches.push_back(CFX_AutoFontCache(pCache, font.get()));
+  }
   for (uint32_t i = 0; i < CharPosList.m_nChars; i++) {
     FXTEXT_CHARPOS& charpos = CharPosList.m_pCharPos[i];
     auto font =
@@ -630,7 +651,8 @@
             ? &pFont->m_Font
             : pFont->m_FontFallbacks[charpos.m_FallbackFontPosition].get();
     const CFX_PathData* pPath =
-        font->LoadGlyphPath(charpos.m_GlyphIndex, charpos.m_FontCharWidth);
+        faceCaches[charpos.m_FallbackFontPosition + 1]->LoadGlyphPath(
+            font, charpos.m_GlyphIndex, charpos.m_FontCharWidth);
     if (!pPath) {
       continue;
     }
diff --git a/core/fpdfapi/fpdf_render/render_int.h b/core/fpdfapi/fpdf_render/render_int.h
index afd9c83..672e592 100644
--- a/core/fpdfapi/fpdf_render/render_int.h
+++ b/core/fpdfapi/fpdf_render/render_int.h
@@ -21,6 +21,7 @@
 
 class CCodec_Jbig2Context;
 class CCodec_ScanlineDecoder;
+class CFX_FontCache;
 class CFX_GlyphBitmap;
 class CFX_ImageTransformer;
 class CFX_PathData;
@@ -70,6 +71,7 @@
   ~CPDF_DocRenderData();
   CPDF_Type3Cache* GetCachedType3(CPDF_Type3Font* pFont);
   CPDF_TransferFunc* GetTransferFunc(CPDF_Object* pObj);
+  CFX_FontCache* GetFontCache() { return m_pFontCache; }
   void Clear(FX_BOOL bRelease = FALSE);
   void ReleaseCachedType3(CPDF_Type3Font* pFont);
   void ReleaseTransferFunc(CPDF_Object* pObj);
@@ -81,6 +83,7 @@
       std::map<CPDF_Object*, CPDF_CountedObject<CPDF_TransferFunc>*>;
 
   CPDF_Document* m_pPDFDoc;
+  CFX_FontCache* m_pFontCache;
   CPDF_Type3CacheMap m_Type3FaceMap;
   CPDF_TransferFuncMap m_TransferFuncMap;
 };
diff --git a/core/fxge/agg/fx_agg_driver.cpp b/core/fxge/agg/fx_agg_driver.cpp
index bb3d7af..9db10ac 100644
--- a/core/fxge/agg/fx_agg_driver.cpp
+++ b/core/fxge/agg/fx_agg_driver.cpp
@@ -13,6 +13,7 @@
 #include "core/fxge/dib/dib_int.h"
 #include "core/fxge/ge/cfx_cliprgn.h"
 #include "core/fxge/ge/fx_text_int.h"
+#include "core/fxge/include/cfx_fontcache.h"
 #include "core/fxge/include/cfx_fxgedevice.h"
 #include "core/fxge/include/cfx_gemodule.h"
 #include "core/fxge/include/cfx_graphstatedata.h"
@@ -458,6 +459,7 @@
 FX_BOOL CFX_AggDeviceDriver::DrawDeviceText(int nChars,
                                             const FXTEXT_CHARPOS* pCharPos,
                                             CFX_Font* pFont,
+                                            CFX_FontCache* pCache,
                                             const CFX_Matrix* pObject2Device,
                                             FX_FLOAT font_size,
                                             uint32_t color) {
diff --git a/core/fxge/agg/fx_agg_driver.h b/core/fxge/agg/fx_agg_driver.h
index 47ae470..db6807c 100644
--- a/core/fxge/agg/fx_agg_driver.h
+++ b/core/fxge/agg/fx_agg_driver.h
@@ -16,6 +16,7 @@
 #include "third_party/agg23/agg_rasterizer_scanline_aa.h"
 
 class CFX_ClipRgn;
+class CFX_FontCache;
 class CFX_GraphStateData;
 class CFX_Matrix;
 class CFX_PathData;
@@ -92,6 +93,7 @@
   FX_BOOL DrawDeviceText(int nChars,
                          const FXTEXT_CHARPOS* pCharPos,
                          CFX_Font* pFont,
+                         CFX_FontCache* pCache,
                          const CFX_Matrix* pObject2Device,
                          FX_FLOAT font_size,
                          uint32_t color) override;
diff --git a/core/fxge/apple/apple_int.h b/core/fxge/apple/apple_int.h
index 9443b5d..a401086 100644
--- a/core/fxge/apple/apple_int.h
+++ b/core/fxge/apple/apple_int.h
@@ -21,6 +21,8 @@
 #include <Carbon/Carbon.h>
 #endif
 
+class CFX_FontCache;
+
 class CQuartz2D {
  public:
   void* createGraphics(CFX_DIBitmap* bitmap);
@@ -108,6 +110,7 @@
   FX_BOOL DrawDeviceText(int nChars,
                          const FXTEXT_CHARPOS* pCharPos,
                          CFX_Font* pFont,
+                         CFX_FontCache* pCache,
                          const CFX_Matrix* pObject2Device,
                          FX_FLOAT font_size,
                          uint32_t color) override;
@@ -124,6 +127,7 @@
   FX_BOOL CG_DrawGlyphRun(int nChars,
                           const FXTEXT_CHARPOS* pCharPos,
                           CFX_Font* pFont,
+                          CFX_FontCache* pCache,
                           const CFX_Matrix* pGlyphMatrix,
                           const CFX_Matrix* pObject2Device,
                           FX_FLOAT font_size,
diff --git a/core/fxge/apple/fx_apple_platform.cpp b/core/fxge/apple/fx_apple_platform.cpp
index 239a6e5..be429e9 100644
--- a/core/fxge/apple/fx_apple_platform.cpp
+++ b/core/fxge/apple/fx_apple_platform.cpp
@@ -18,6 +18,7 @@
 #include "core/fxge/ge/cfx_cliprgn.h"
 #include "core/fxge/ge/fx_text_int.h"
 #include "core/fxge/include/cfx_facecache.h"
+#include "core/fxge/include/cfx_fontcache.h"
 #include "core/fxge/include/cfx_gemodule.h"
 #include "core/fxge/include/cfx_renderdevice.h"
 #include "core/fxge/include/fx_freetype.h"
@@ -32,6 +33,7 @@
                        int nChars,
                        const FXTEXT_CHARPOS* pCharPos,
                        CFX_Font* pFont,
+                       CFX_FontCache* pCache,
                        const CFX_Matrix* pObject2Device,
                        FX_FLOAT font_size,
                        uint32_t argb) {
@@ -104,6 +106,7 @@
 FX_BOOL CFX_AggDeviceDriver::DrawDeviceText(int nChars,
                                             const FXTEXT_CHARPOS* pCharPos,
                                             CFX_Font* pFont,
+                                            CFX_FontCache* pCache,
                                             const CFX_Matrix* pObject2Device,
                                             FX_FLOAT font_size,
                                             uint32_t argb) {
@@ -152,8 +155,8 @@
   else
     CGContextClipToRect(ctx, rect_cg);
 
-  FX_BOOL ret = CGDrawGlyphRun(ctx, nChars, pCharPos, pFont, pObject2Device,
-                               font_size, argb);
+  FX_BOOL ret = CGDrawGlyphRun(ctx, nChars, pCharPos, pFont, pCache,
+                               pObject2Device, font_size, argb);
   if (pImageCG)
     CGImageRelease(pImageCG);
   CGContextRestoreGState(ctx);
@@ -167,7 +170,7 @@
 void CFX_FaceCache::DestroyPlatform() {}
 
 CFX_GlyphBitmap* CFX_FaceCache::RenderGlyph_Nativetext(
-    const CFX_Font* pFont,
+    CFX_Font* pFont,
     uint32_t glyph_index,
     const CFX_Matrix* pMatrix,
     int dest_width,
diff --git a/core/fxge/apple/fx_quartz_device.cpp b/core/fxge/apple/fx_quartz_device.cpp
index 6a0260b..400e290 100644
--- a/core/fxge/apple/fx_quartz_device.cpp
+++ b/core/fxge/apple/fx_quartz_device.cpp
@@ -13,6 +13,7 @@
 #include "core/fxcrt/include/fx_memory.h"
 #include "core/fxge/dib/dib_int.h"
 #include "core/fxge/ge/fx_text_int.h"
+#include "core/fxge/include/cfx_fontcache.h"
 #include "core/fxge/include/cfx_gemodule.h"
 #include "core/fxge/include/cfx_graphstatedata.h"
 #include "core/fxge/include/cfx_pathdata.h"
@@ -745,6 +746,7 @@
     int nChars,
     const FXTEXT_CHARPOS* pCharPos,
     CFX_Font* pFont,
+    CFX_FontCache* pCache,
     const CFX_Matrix* pGlyphMatrix,
     const CFX_Matrix* pObject2Device,
     FX_FLOAT font_size,
@@ -810,6 +812,7 @@
 FX_BOOL CFX_QuartzDeviceDriver::DrawDeviceText(int nChars,
                                                const FXTEXT_CHARPOS* pCharPos,
                                                CFX_Font* pFont,
+                                               CFX_FontCache* pCache,
                                                const CFX_Matrix* pObject2Device,
                                                FX_FLOAT font_size,
                                                uint32_t color) {
@@ -829,8 +832,8 @@
   while (i < nChars) {
     if (pCharPos[i].m_bGlyphAdjust || font_size < 0) {
       if (i > 0) {
-        ret = CG_DrawGlyphRun(i, pCharPos, pFont, nullptr, pObject2Device,
-                              font_size, color);
+        ret = CG_DrawGlyphRun(i, pCharPos, pFont, pCache, nullptr,
+                              pObject2Device, font_size, color);
         if (!ret) {
           RestoreState(false);
           return ret;
@@ -846,8 +849,8 @@
             char_pos->m_AdjustMatrix[0], char_pos->m_AdjustMatrix[1],
             char_pos->m_AdjustMatrix[2], char_pos->m_AdjustMatrix[3], 0, 0);
       }
-      ret = CG_DrawGlyphRun(1, char_pos, pFont, &glphy_matrix, pObject2Device,
-                            font_size, color);
+      ret = CG_DrawGlyphRun(1, char_pos, pFont, pCache, &glphy_matrix,
+                            pObject2Device, font_size, color);
       if (!ret) {
         RestoreState(false);
         return ret;
@@ -861,7 +864,7 @@
     }
   }
   if (i > 0) {
-    ret = CG_DrawGlyphRun(i, pCharPos, pFont, nullptr, pObject2Device,
+    ret = CG_DrawGlyphRun(i, pCharPos, pFont, pCache, nullptr, pObject2Device,
                           font_size, color);
   }
   RestoreState(false);
diff --git a/core/fxge/ge/cfx_facecache.cpp b/core/fxge/ge/cfx_facecache.cpp
index b78dd5b..2b84059 100644
--- a/core/fxge/ge/cfx_facecache.cpp
+++ b/core/fxge/ge/cfx_facecache.cpp
@@ -89,7 +89,7 @@
 #endif
 }
 
-CFX_GlyphBitmap* CFX_FaceCache::RenderGlyph(const CFX_Font* pFont,
+CFX_GlyphBitmap* CFX_FaceCache::RenderGlyph(CFX_Font* pFont,
                                             uint32_t glyph_index,
                                             FX_BOOL bFontStyle,
                                             const CFX_Matrix* pMatrix,
@@ -223,7 +223,7 @@
   return pGlyphBitmap;
 }
 
-const CFX_PathData* CFX_FaceCache::LoadGlyphPath(const CFX_Font* pFont,
+const CFX_PathData* CFX_FaceCache::LoadGlyphPath(CFX_Font* pFont,
                                                  uint32_t glyph_index,
                                                  int dest_width) {
   if (!m_Face || glyph_index == kInvalidGlyphIndex || dest_width < 0)
@@ -247,12 +247,12 @@
   if (it != m_PathMap.end())
     return it->second.get();
 
-  CFX_PathData* pGlyphPath = pFont->LoadGlyphPathImpl(glyph_index, dest_width);
+  CFX_PathData* pGlyphPath = pFont->LoadGlyphPath(glyph_index, dest_width);
   m_PathMap[key] = std::unique_ptr<CFX_PathData>(pGlyphPath);
   return pGlyphPath;
 }
 
-const CFX_GlyphBitmap* CFX_FaceCache::LoadGlyphBitmap(const CFX_Font* pFont,
+const CFX_GlyphBitmap* CFX_FaceCache::LoadGlyphBitmap(CFX_Font* pFont,
                                                       uint32_t glyph_index,
                                                       FX_BOOL bFontStyle,
                                                       const CFX_Matrix* pMatrix,
@@ -349,7 +349,7 @@
 }
 
 #ifdef _SKIA_SUPPORT_
-CFX_TypeFace* CFX_FaceCache::GetDeviceCache(const CFX_Font* pFont) {
+CFX_TypeFace* CFX_FaceCache::GetDeviceCache(CFX_Font* pFont) {
   if (!m_pTypeface) {
     m_pTypeface =
         SkTypeface::MakeFromStream(
@@ -365,7 +365,7 @@
 #endif
 
 CFX_GlyphBitmap* CFX_FaceCache::LookUpGlyphBitmap(
-    const CFX_Font* pFont,
+    CFX_Font* pFont,
     const CFX_Matrix* pMatrix,
     const CFX_ByteString& FaceGlyphsKey,
     uint32_t glyph_index,
diff --git a/core/fxge/ge/cfx_font.cpp b/core/fxge/ge/cfx_font.cpp
index b184711..feea8b0 100644
--- a/core/fxge/ge/cfx_font.cpp
+++ b/core/fxge/ge/cfx_font.cpp
@@ -8,8 +8,6 @@
 
 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h"
 #include "core/fxge/ge/fx_text_int.h"
-#include "core/fxge/include/cfx_facecache.h"
-#include "core/fxge/include/cfx_fontcache.h"
 #include "core/fxge/include/cfx_fontmgr.h"
 #include "core/fxge/include/cfx_gemodule.h"
 #include "core/fxge/include/cfx_pathdata.h"
@@ -226,7 +224,6 @@
       m_pOwnedStream(nullptr),
 #endif  // PDF_ENABLE_XFA
       m_Face(nullptr),
-      m_FaceCache(nullptr),
       m_pFontData(nullptr),
       m_pGsubData(nullptr),
       m_dwSize(0),
@@ -265,15 +262,8 @@
   m_pPlatformFont = pFont->m_pPlatformFont;
 #endif
   m_pOwnedStream = pFont->m_pOwnedStream;
-  m_FaceCache = pFont->GetFaceCache();
   return TRUE;
 }
-
-void CFX_Font::SetFace(FXFT_Face face) {
-  ClearFaceCache();
-  m_Face = face;
-}
-
 #endif  // PDF_ENABLE_XFA
 
 CFX_Font::~CFX_Font() {
@@ -289,7 +279,10 @@
       FXFT_Clear_Face_External_Stream(m_Face);
     }
 #endif  // PDF_ENABLE_XFA
-    DeleteFace();
+    if (m_bEmbedded)
+      DeleteFace();
+    else
+      CFX_GEModule::Get()->GetFontMgr()->ReleaseFace(m_Face);
   }
 #ifdef PDF_ENABLE_XFA
   delete m_pOwnedStream;
@@ -301,12 +294,7 @@
 }
 
 void CFX_Font::DeleteFace() {
-  ClearFaceCache();
-  if (m_bEmbedded) {
-    FXFT_Done_Face(m_Face);
-  } else {
-    CFX_GEModule::Get()->GetFontMgr()->ReleaseFace(m_Face);
-  }
+  FXFT_Done_Face(m_Face);
   m_Face = nullptr;
 }
 
@@ -549,20 +537,6 @@
                    FXFT_Get_Face_MaxAdvanceWidth(m_Face));
 }
 
-CFX_FaceCache* CFX_Font::GetFaceCache() const {
-  if (!m_FaceCache) {
-    m_FaceCache = CFX_GEModule::Get()->GetFontCache()->GetCachedFace(this);
-  }
-  return m_FaceCache;
-}
-
-void CFX_Font::ClearFaceCache() {
-  if (!m_FaceCache)
-    return;
-  CFX_GEModule::Get()->GetFontCache()->ReleaseCachedFace(this);
-  m_FaceCache = nullptr;
-}
-
 int CFX_Font::GetULPos() const {
   if (!m_Face)
     return 0;
@@ -579,9 +553,7 @@
                    FXFT_Get_Face_UnderLineThickness(m_Face));
 }
 
-void CFX_Font::AdjustMMParams(int glyph_index,
-                              int dest_width,
-                              int weight) const {
+void CFX_Font::AdjustMMParams(int glyph_index, int dest_width, int weight) {
   FXFT_MM_Var pMasters = nullptr;
   FXFT_Get_MM_Var(m_Face, &pMasters);
   if (!pMasters)
@@ -621,8 +593,7 @@
   FXFT_Set_MM_Design_Coordinates(m_Face, 2, coords);
 }
 
-CFX_PathData* CFX_Font::LoadGlyphPathImpl(uint32_t glyph_index,
-                                          int dest_width) const {
+CFX_PathData* CFX_Font::LoadGlyphPath(uint32_t glyph_index, int dest_width) {
   if (!m_Face)
     return nullptr;
   FXFT_Set_Pixel_Sizes(m_Face, 0, 64);
@@ -691,24 +662,3 @@
     pPath->GetPoints()[params.m_PointCount - 1].m_Flag |= FXPT_CLOSEFIGURE;
   return pPath;
 }
-
-const CFX_GlyphBitmap* CFX_Font::LoadGlyphBitmap(uint32_t glyph_index,
-                                                 FX_BOOL bFontStyle,
-                                                 const CFX_Matrix* pMatrix,
-                                                 int dest_width,
-                                                 int anti_alias,
-                                                 int& text_flags) const {
-  return GetFaceCache()->LoadGlyphBitmap(this, glyph_index, bFontStyle, pMatrix,
-                                         dest_width, anti_alias, text_flags);
-}
-
-const CFX_PathData* CFX_Font::LoadGlyphPath(uint32_t glyph_index,
-                                            int dest_width) const {
-  return GetFaceCache()->LoadGlyphPath(this, glyph_index, dest_width);
-}
-
-#ifdef _SKIA_SUPPORT_
-CFX_TypeFace* CFX_Font::GetDeviceCache() const {
-  return GetFaceCache()->GetDeviceCache(this);
-}
-#endif
diff --git a/core/fxge/ge/cfx_fontcache.cpp b/core/fxge/ge/cfx_fontcache.cpp
index acae018..3ecd83c 100644
--- a/core/fxge/ge/cfx_fontcache.cpp
+++ b/core/fxge/ge/cfx_fontcache.cpp
@@ -10,43 +10,38 @@
 #include "core/fxge/include/fx_font.h"
 #include "core/fxge/include/fx_freetype.h"
 
-CFX_FontCache::CountedFaceCache::CountedFaceCache() {}
-
-CFX_FontCache::CountedFaceCache::~CountedFaceCache() {}
-
 CFX_FontCache::CFX_FontCache() {}
 
 CFX_FontCache::~CFX_FontCache() {
-  ASSERT(m_ExtFaceMap.empty());
-  ASSERT(m_FTFaceMap.empty());
+  FreeCache(TRUE);
 }
 
-CFX_FaceCache* CFX_FontCache::GetCachedFace(const CFX_Font* pFont) {
+CFX_FaceCache* CFX_FontCache::GetCachedFace(CFX_Font* pFont) {
   FXFT_Face face = pFont->GetFace();
   const bool bExternal = !face;
   CFX_FTCacheMap& map = bExternal ? m_ExtFaceMap : m_FTFaceMap;
   auto it = map.find(face);
   if (it != map.end()) {
-    CountedFaceCache* counted_face_cache = it->second.get();
+    CFX_CountedFaceCache* counted_face_cache = it->second;
     counted_face_cache->m_nCount++;
-    return counted_face_cache->m_Obj.get();
+    return counted_face_cache->m_Obj;
   }
 
-  std::unique_ptr<CountedFaceCache> counted_face_cache(new CountedFaceCache);
-  counted_face_cache->m_nCount = 2;
   CFX_FaceCache* face_cache = new CFX_FaceCache(bExternal ? nullptr : face);
-  counted_face_cache->m_Obj.reset(face_cache);
-  map[face] = std::move(counted_face_cache);
+  CFX_CountedFaceCache* counted_face_cache = new CFX_CountedFaceCache;
+  counted_face_cache->m_nCount = 2;
+  counted_face_cache->m_Obj = face_cache;
+  map[face] = counted_face_cache;
   return face_cache;
 }
 
 #ifdef _SKIA_SUPPORT_
-CFX_TypeFace* CFX_FontCache::GetDeviceCache(const CFX_Font* pFont) {
+CFX_TypeFace* CFX_FontCache::GetDeviceCache(CFX_Font* pFont) {
   return GetCachedFace(pFont)->GetDeviceCache(pFont);
 }
 #endif
 
-void CFX_FontCache::ReleaseCachedFace(const CFX_Font* pFont) {
+void CFX_FontCache::ReleaseCachedFace(CFX_Font* pFont) {
   FXFT_Face face = pFont->GetFace();
   const bool bExternal = !face;
   CFX_FTCacheMap& map = bExternal ? m_ExtFaceMap : m_FTFaceMap;
@@ -55,10 +50,30 @@
   if (it == map.end())
     return;
 
-  CountedFaceCache* counted_face_cache = it->second.get();
-  if (counted_face_cache->m_nCount > 2) {
+  CFX_CountedFaceCache* counted_face_cache = it->second;
+  if (counted_face_cache->m_nCount > 1) {
     counted_face_cache->m_nCount--;
-  } else {
-    map.erase(it);
+  }
+}
+
+void CFX_FontCache::FreeCache(FX_BOOL bRelease) {
+  for (auto it = m_FTFaceMap.begin(); it != m_FTFaceMap.end();) {
+    auto curr_it = it++;
+    CFX_CountedFaceCache* cache = curr_it->second;
+    if (bRelease || cache->m_nCount < 2) {
+      delete cache->m_Obj;
+      delete cache;
+      m_FTFaceMap.erase(curr_it);
+    }
+  }
+
+  for (auto it = m_ExtFaceMap.begin(); it != m_ExtFaceMap.end();) {
+    auto curr_it = it++;
+    CFX_CountedFaceCache* cache = curr_it->second;
+    if (bRelease || cache->m_nCount < 2) {
+      delete cache->m_Obj;
+      delete cache;
+      m_ExtFaceMap.erase(curr_it);
+    }
   }
 }
diff --git a/core/fxge/ge/cfx_renderdevice.cpp b/core/fxge/ge/cfx_renderdevice.cpp
index 11f0087..ad05ef7 100644
--- a/core/fxge/ge/cfx_renderdevice.cpp
+++ b/core/fxge/ge/cfx_renderdevice.cpp
@@ -9,6 +9,7 @@
 #include "core/fxcrt/include/fx_safe_types.h"
 #include "core/fxge/include/cfx_autofontcache.h"
 #include "core/fxge/include/cfx_facecache.h"
+#include "core/fxge/include/cfx_fontcache.h"
 #include "core/fxge/include/cfx_fxgedevice.h"
 #include "core/fxge/include/cfx_graphstatedata.h"
 #include "core/fxge/include/cfx_pathdata.h"
@@ -823,6 +824,7 @@
 FX_BOOL CFX_RenderDevice::DrawNormalText(int nChars,
                                          const FXTEXT_CHARPOS* pCharPos,
                                          CFX_Font* pFont,
+                                         CFX_FontCache* pCache,
                                          FX_FLOAT font_size,
                                          const CFX_Matrix* pText2Device,
                                          uint32_t fill_color,
@@ -831,8 +833,9 @@
   if (m_DeviceClass != FXDC_DISPLAY) {
     if (!(text_flags & FXTEXT_PRINTGRAPHICTEXT)) {
       if (ShouldDrawDeviceText(pFont, text_flags) &&
-          m_pDeviceDriver->DrawDeviceText(nChars, pCharPos, pFont, pText2Device,
-                                          font_size, fill_color)) {
+          m_pDeviceDriver->DrawDeviceText(nChars, pCharPos, pFont, pCache,
+                                          pText2Device, font_size,
+                                          fill_color)) {
         return TRUE;
       }
     }
@@ -840,8 +843,8 @@
       return FALSE;
   } else if (!(text_flags & FXTEXT_NO_NATIVETEXT)) {
     if (ShouldDrawDeviceText(pFont, text_flags) &&
-        m_pDeviceDriver->DrawDeviceText(nChars, pCharPos, pFont, pText2Device,
-                                        font_size, fill_color)) {
+        m_pDeviceDriver->DrawDeviceText(nChars, pCharPos, pFont, pCache,
+                                        pText2Device, font_size, fill_color)) {
       return TRUE;
     }
   }
@@ -859,8 +862,9 @@
         (pFont->GetSubstFont()->m_SubstFlags & FXFONT_SUBST_GLYPHPATH)) {
       int nPathFlags =
           (text_flags & FXTEXT_NOSMOOTH) == 0 ? 0 : FXFILL_NOPATHSMOOTH;
-      return DrawTextPath(nChars, pCharPos, pFont, font_size, pText2Device,
-                          nullptr, nullptr, fill_color, 0, nullptr, nPathFlags);
+      return DrawTextPath(nChars, pCharPos, pFont, pCache, font_size,
+                          pText2Device, nullptr, nullptr, fill_color, 0,
+                          nullptr, nPathFlags);
     }
   }
   int anti_alias = FXFT_RENDER_MODE_MONO;
@@ -891,6 +895,10 @@
       }
     }
   }
+  if (!pCache)
+    pCache = CFX_GEModule::Get()->GetFontCache();
+  CFX_FaceCache* pFaceCache = pCache->GetCachedFace(pFont);
+  CFX_AutoFontCache autoFontCache(pCache, pFont);
   std::vector<FXTEXT_GLYPHPOS> glyphs(nChars);
   CFX_Matrix matrixCTM = GetCTM();
   FX_FLOAT scale_x = FXSYS_fabs(matrixCTM.a);
@@ -914,12 +922,12 @@
           charpos.m_AdjustMatrix[0], charpos.m_AdjustMatrix[1],
           charpos.m_AdjustMatrix[2], charpos.m_AdjustMatrix[3], 0, 0);
       new_matrix.Concat(deviceCtm);
-      glyph.m_pGlyph = pFont->LoadGlyphBitmap(
-          charpos.m_GlyphIndex, charpos.m_bFontStyle, &new_matrix,
+      glyph.m_pGlyph = pFaceCache->LoadGlyphBitmap(
+          pFont, charpos.m_GlyphIndex, charpos.m_bFontStyle, &new_matrix,
           charpos.m_FontCharWidth, anti_alias, nativetext_flags);
     } else {
-      glyph.m_pGlyph = pFont->LoadGlyphBitmap(
-          charpos.m_GlyphIndex, charpos.m_bFontStyle, &deviceCtm,
+      glyph.m_pGlyph = pFaceCache->LoadGlyphBitmap(
+          pFont, charpos.m_GlyphIndex, charpos.m_bFontStyle, &deviceCtm,
           charpos.m_FontCharWidth, anti_alias, nativetext_flags);
     }
   }
@@ -1040,6 +1048,7 @@
 FX_BOOL CFX_RenderDevice::DrawTextPath(int nChars,
                                        const FXTEXT_CHARPOS* pCharPos,
                                        CFX_Font* pFont,
+                                       CFX_FontCache* pCache,
                                        FX_FLOAT font_size,
                                        const CFX_Matrix* pText2User,
                                        const CFX_Matrix* pUser2Device,
@@ -1048,6 +1057,10 @@
                                        FX_ARGB stroke_color,
                                        CFX_PathData* pClippingPath,
                                        int nFlag) {
+  if (!pCache)
+    pCache = CFX_GEModule::Get()->GetFontCache();
+  CFX_FaceCache* pFaceCache = pCache->GetCachedFace(pFont);
+  CFX_AutoFontCache autoFontCache(pCache, pFont);
   for (int iChar = 0; iChar < nChars; iChar++) {
     const FXTEXT_CHARPOS& charpos = pCharPos[iChar];
     CFX_Matrix matrix;
@@ -1057,8 +1070,8 @@
     }
     matrix.Concat(font_size, 0, 0, font_size, charpos.m_OriginX,
                   charpos.m_OriginY);
-    const CFX_PathData* pPath =
-        pFont->LoadGlyphPath(charpos.m_GlyphIndex, charpos.m_FontCharWidth);
+    const CFX_PathData* pPath = pFaceCache->LoadGlyphPath(
+        pFont, charpos.m_GlyphIndex, charpos.m_FontCharWidth);
     if (!pPath)
       continue;
     matrix.Concat(*pText2User);
diff --git a/core/fxge/ifx_renderdevicedriver.cpp b/core/fxge/ifx_renderdevicedriver.cpp
index 5acfae4..69b8ac9 100644
--- a/core/fxge/ifx_renderdevicedriver.cpp
+++ b/core/fxge/ifx_renderdevicedriver.cpp
@@ -10,6 +10,8 @@
 #include "core/fxge/include/cfx_pathdata.h"
 #include "core/fxge/include/cfx_renderdevice.h"
 
+class CFX_FontCache;
+
 IFX_RenderDeviceDriver::~IFX_RenderDeviceDriver() {}
 
 CFX_Matrix IFX_RenderDeviceDriver::GetCTM() const {
@@ -68,6 +70,7 @@
 FX_BOOL IFX_RenderDeviceDriver::DrawDeviceText(int nChars,
                                                const FXTEXT_CHARPOS* pCharPos,
                                                CFX_Font* pFont,
+                                               CFX_FontCache* pCache,
                                                const CFX_Matrix* pObject2Device,
                                                FX_FLOAT font_size,
                                                uint32_t color) {
diff --git a/core/fxge/include/cfx_facecache.h b/core/fxge/include/cfx_facecache.h
index fc1b28b..6e437b4 100644
--- a/core/fxge/include/cfx_facecache.h
+++ b/core/fxge/include/cfx_facecache.h
@@ -16,34 +16,34 @@
  public:
   explicit CFX_FaceCache(FXFT_Face face);
   ~CFX_FaceCache();
-  const CFX_GlyphBitmap* LoadGlyphBitmap(const CFX_Font* pFont,
+  const CFX_GlyphBitmap* LoadGlyphBitmap(CFX_Font* pFont,
                                          uint32_t glyph_index,
                                          FX_BOOL bFontStyle,
                                          const CFX_Matrix* pMatrix,
                                          int dest_width,
                                          int anti_alias,
                                          int& text_flags);
-  const CFX_PathData* LoadGlyphPath(const CFX_Font* pFont,
+  const CFX_PathData* LoadGlyphPath(CFX_Font* pFont,
                                     uint32_t glyph_index,
                                     int dest_width);
 
 #ifdef _SKIA_SUPPORT_
-  CFX_TypeFace* GetDeviceCache(const CFX_Font* pFont);
+  CFX_TypeFace* GetDeviceCache(CFX_Font* pFont);
 #endif
 
  private:
-  CFX_GlyphBitmap* RenderGlyph(const CFX_Font* pFont,
+  CFX_GlyphBitmap* RenderGlyph(CFX_Font* pFont,
                                uint32_t glyph_index,
                                FX_BOOL bFontStyle,
                                const CFX_Matrix* pMatrix,
                                int dest_width,
                                int anti_alias);
-  CFX_GlyphBitmap* RenderGlyph_Nativetext(const CFX_Font* pFont,
+  CFX_GlyphBitmap* RenderGlyph_Nativetext(CFX_Font* pFont,
                                           uint32_t glyph_index,
                                           const CFX_Matrix* pMatrix,
                                           int dest_width,
                                           int anti_alias);
-  CFX_GlyphBitmap* LookUpGlyphBitmap(const CFX_Font* pFont,
+  CFX_GlyphBitmap* LookUpGlyphBitmap(CFX_Font* pFont,
                                      const CFX_Matrix* pMatrix,
                                      const CFX_ByteString& FaceGlyphsKey,
                                      uint32_t glyph_index,
diff --git a/core/fxge/include/cfx_fontcache.h b/core/fxge/include/cfx_fontcache.h
index dd9a176..d4a4ddd 100644
--- a/core/fxge/include/cfx_fontcache.h
+++ b/core/fxge/include/cfx_fontcache.h
@@ -8,7 +8,6 @@
 #define CORE_FXGE_INCLUDE_CFX_FONTCACHE_H_
 
 #include <map>
-#include <memory>
 
 #include "core/fxcrt/include/fx_system.h"
 #include "core/fxge/include/fx_font.h"
@@ -20,21 +19,15 @@
  public:
   CFX_FontCache();
   ~CFX_FontCache();
-  CFX_FaceCache* GetCachedFace(const CFX_Font* pFont);
-  void ReleaseCachedFace(const CFX_Font* pFont);
+  CFX_FaceCache* GetCachedFace(CFX_Font* pFont);
+  void ReleaseCachedFace(CFX_Font* pFont);
+  void FreeCache(FX_BOOL bRelease = FALSE);
 #ifdef _SKIA_SUPPORT_
-  CFX_TypeFace* GetDeviceCache(const CFX_Font* pFont);
+  CFX_TypeFace* GetDeviceCache(CFX_Font* pFont);
 #endif
 
  private:
-  struct CountedFaceCache {
-    CountedFaceCache();
-    ~CountedFaceCache();
-    std::unique_ptr<CFX_FaceCache> m_Obj;
-    uint32_t m_nCount;
-  };
-
-  using CFX_FTCacheMap = std::map<FXFT_Face, std::unique_ptr<CountedFaceCache>>;
+  using CFX_FTCacheMap = std::map<FXFT_Face, CFX_CountedFaceCache*>;
   CFX_FTCacheMap m_FTFaceMap;
   CFX_FTCacheMap m_ExtFaceMap;
 };
diff --git a/core/fxge/include/cfx_renderdevice.h b/core/fxge/include/cfx_renderdevice.h
index 3b9c93f..9ca9a07 100644
--- a/core/fxge/include/cfx_renderdevice.h
+++ b/core/fxge/include/cfx_renderdevice.h
@@ -14,6 +14,7 @@
 #include "core/fxge/include/fx_font.h"
 
 class CFX_Font;
+class CFX_FontCache;
 class CFX_GraphStateData;
 class IFX_RenderDeviceDriver;
 
@@ -207,6 +208,7 @@
   FX_BOOL DrawNormalText(int nChars,
                          const FXTEXT_CHARPOS* pCharPos,
                          CFX_Font* pFont,
+                         CFX_FontCache* pCache,
                          FX_FLOAT font_size,
                          const CFX_Matrix* pText2Device,
                          uint32_t fill_color,
@@ -214,6 +216,7 @@
   FX_BOOL DrawTextPath(int nChars,
                                 const FXTEXT_CHARPOS* pCharPos,
                                 CFX_Font* pFont,
+                                CFX_FontCache* pCache,
                                 FX_FLOAT font_size,
                                 const CFX_Matrix* pText2User,
                                 const CFX_Matrix* pUser2Device,
diff --git a/core/fxge/include/fx_font.h b/core/fxge/include/fx_font.h
index 58d12bb..fc207a6 100644
--- a/core/fxge/include/fx_font.h
+++ b/core/fxge/include/fx_font.h
@@ -19,7 +19,7 @@
 typedef void* FXFT_Library;
 
 class CFX_FaceCache;
-class CFX_GlyphBitmap;
+class CFX_FontCache;
 class CFX_PathData;
 class CFX_SizeGlyphCache;
 
@@ -115,24 +115,13 @@
                    int* pFaceCount = nullptr);
 
   FX_BOOL LoadClone(const CFX_Font* pFont);
-  void SetFace(FXFT_Face face);
+  void SetFace(FXFT_Face face) { m_Face = face; }
   void SetSubstFont(std::unique_ptr<CFX_SubstFont> subst) {
     m_pSubstFont = std::move(subst);
   }
 #endif  // PDF_ENABLE_XFA
 
-  const CFX_GlyphBitmap* LoadGlyphBitmap(uint32_t glyph_index,
-                                         FX_BOOL bFontStyle,
-                                         const CFX_Matrix* pMatrix,
-                                         int dest_width,
-                                         int anti_alias,
-                                         int& text_flags) const;
-  const CFX_PathData* LoadGlyphPath(uint32_t glyph_index, int dest_width) const;
-
-#ifdef _SKIA_SUPPORT_
-  CFX_TypeFace* GetDeviceCache() const;
-#endif
-
+  CFX_PathData* LoadGlyphPath(uint32_t glyph_index, int dest_width = 0);
   int GetGlyphWidth(uint32_t glyph_index);
   int GetAscent() const;
   int GetDescent() const;
@@ -159,7 +148,7 @@
 #endif
   uint8_t* GetFontData() const { return m_pFontData; }
   uint32_t GetSize() const { return m_dwSize; }
-  void AdjustMMParams(int glyph_index, int width, int weight) const;
+  void AdjustMMParams(int glyph_index, int width, int weight);
 
   static const size_t kAngleSkewArraySize = 30;
   static const char s_AngleSkew[kAngleSkewArraySize];
@@ -176,20 +165,10 @@
 #endif  // PDF_ENABLE_XFA
 
  private:
-  friend class CFX_FaceCache;
-  CFX_PathData* LoadGlyphPathImpl(uint32_t glyph_index,
-                                  int dest_width = 0) const;
-
- private:
-  CFX_FaceCache* GetFaceCache() const;
-
   void ReleasePlatformResource();
   void DeleteFace();
 
-  void ClearFaceCache();
-
   FXFT_Face m_Face;
-  mutable CFX_FaceCache* m_FaceCache;  // not owned.
   std::unique_ptr<CFX_SubstFont> m_pSubstFont;
   std::vector<uint8_t> m_pFontDataAllocation;
   uint8_t* m_pFontData;
@@ -219,6 +198,12 @@
   uint32_t m_Charsets;
 };
 
+class CFX_CountedFaceCache {
+ public:
+  CFX_FaceCache* m_Obj;
+  uint32_t m_nCount;
+};
+
 class CFX_GlyphBitmap {
  public:
   int m_Top;
diff --git a/core/fxge/include/ifx_renderdevicedriver.h b/core/fxge/include/ifx_renderdevicedriver.h
index e9dbed9..8b20cf0 100644
--- a/core/fxge/include/ifx_renderdevicedriver.h
+++ b/core/fxge/include/ifx_renderdevicedriver.h
@@ -12,6 +12,7 @@
 class CFX_DIBitmap;
 class CFX_DIBSource;
 class CFX_Font;
+class CFX_FontCache;
 class CFX_GraphStateData;
 class CFX_Matrix;
 class CFX_PathData;
@@ -86,6 +87,7 @@
   virtual FX_BOOL DrawDeviceText(int nChars,
                                  const FXTEXT_CHARPOS* pCharPos,
                                  CFX_Font* pFont,
+                                 CFX_FontCache* pCache,
                                  const CFX_Matrix* pObject2Device,
                                  FX_FLOAT font_size,
                                  uint32_t color);
diff --git a/core/fxge/skia/fx_skia_device.cpp b/core/fxge/skia/fx_skia_device.cpp
index b1c47bf..d2e4abc 100644
--- a/core/fxge/skia/fx_skia_device.cpp
+++ b/core/fxge/skia/fx_skia_device.cpp
@@ -14,6 +14,7 @@
 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h"
 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h"
+#include "core/fxge/include/cfx_fontcache.h"
 #include "core/fxge/include/cfx_fxgedevice.h"
 #include "core/fxge/include/cfx_gemodule.h"
 #include "core/fxge/include/cfx_graphstatedata.h"
@@ -485,6 +486,7 @@
   // mark all cached state as uninitialized
   SkiaState()
       : m_pFont(nullptr),
+        m_pCache(nullptr),
         m_fontSize(0),
         m_fillColor(0),
         m_strokeColor(0),
@@ -578,6 +580,7 @@
   bool DrawText(int nChars,
                 const FXTEXT_CHARPOS* pCharPos,
                 CFX_Font* pFont,
+                CFX_FontCache* pCache,
                 const CFX_Matrix* pMatrix,
                 FX_FLOAT font_size,
                 uint32_t color,
@@ -588,12 +591,13 @@
       FlushCommands(pDriver);
     if (m_drawPath)
       FlushPath(pDriver);
-    if (m_drawText && FontChanged(pFont, pMatrix, font_size, color))
+    if (m_drawText && FontChanged(pFont, pCache, pMatrix, font_size, color))
       FlushText(pDriver);
     if (!m_drawText) {
       m_positions.setCount(0);
       m_glyphs.setCount(0);
       m_pFont = pFont;
+      m_pCache = pCache;
       m_fontSize = font_size;
       m_fillColor = color;
       m_drawMatrix = *pMatrix;
@@ -622,8 +626,8 @@
     SkPaint skPaint;
     skPaint.setAntiAlias(true);
     skPaint.setColor(m_fillColor);
-    if (m_pFont->GetFace()) {  // exclude placeholder test fonts
-      sk_sp<SkTypeface> typeface(SkSafeRef(m_pFont->GetDeviceCache()));
+    if (m_pFont->GetFace() && m_pCache) {  // exclude placeholder test fonts
+      sk_sp<SkTypeface> typeface(SkSafeRef(m_pCache->GetDeviceCache(m_pFont)));
       skPaint.setTypeface(typeface);
     }
     skPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
@@ -779,11 +783,13 @@
   }
 
   bool FontChanged(CFX_Font* pFont,
+                   CFX_FontCache* pCache,
                    const CFX_Matrix* pMatrix,
                    FX_FLOAT font_size,
                    uint32_t color) {
-    return pFont != m_pFont || MatrixChanged(pMatrix, m_drawMatrix) ||
-           font_size != m_fontSize || color != m_fillColor;
+    return pFont != m_pFont || pCache != m_pCache ||
+           MatrixChanged(pMatrix, m_drawMatrix) || font_size != m_fontSize ||
+           color != m_fillColor;
   }
 
   bool MatrixChanged(const CFX_Matrix* pMatrix, const CFX_Matrix& refMatrix) {
@@ -866,6 +872,7 @@
   CFX_GraphStateData m_drawState;
   CFX_Matrix m_clipMatrix;
   CFX_Font* m_pFont;
+  CFX_FontCache* m_pCache;
   FX_FLOAT m_fontSize;
   uint32_t m_fillColor;
   uint32_t m_strokeColor;
@@ -998,14 +1005,18 @@
 FX_BOOL CFX_SkiaDeviceDriver::DrawDeviceText(int nChars,
                                              const FXTEXT_CHARPOS* pCharPos,
                                              CFX_Font* pFont,
+                                             CFX_FontCache* pCache,
                                              const CFX_Matrix* pObject2Device,
                                              FX_FLOAT font_size,
                                              uint32_t color) {
-  if (m_pCache->DrawText(nChars, pCharPos, pFont, pObject2Device, font_size,
-                         color, this)) {
+  if (!pCache)
+    pCache = CFX_GEModule::Get()->GetFontCache();
+  if (m_pCache->DrawText(nChars, pCharPos, pFont, pCache, pObject2Device,
+                         font_size, color, this)) {
     return TRUE;
   }
-  sk_sp<SkTypeface> typeface(SkSafeRef(pFont->GetDeviceCache()));
+  sk_sp<SkTypeface> typeface(
+      SkSafeRef(pCache ? pCache->GetDeviceCache(pFont) : nullptr));
   SkPaint paint;
   paint.setAntiAlias(true);
   paint.setColor(color);
diff --git a/core/fxge/skia/fx_skia_device.h b/core/fxge/skia/fx_skia_device.h
index 78d8e32..8a1bf69 100644
--- a/core/fxge/skia/fx_skia_device.h
+++ b/core/fxge/skia/fx_skia_device.h
@@ -10,6 +10,7 @@
 #include "core/fxge/include/cfx_pathdata.h"
 #include "core/fxge/include/ifx_renderdevicedriver.h"
 
+class CFX_FontCache;
 class SkCanvas;
 class SkMatrix;
 class SkPaint;
@@ -121,6 +122,7 @@
   FX_BOOL DrawDeviceText(int nChars,
                          const FXTEXT_CHARPOS* pCharPos,
                          CFX_Font* pFont,
+                         CFX_FontCache* pCache,
                          const CFX_Matrix* pObject2Device,
                          FX_FLOAT font_size,
                          uint32_t color) override;
diff --git a/core/fxge/skia/fx_skia_device_unittest.cpp b/core/fxge/skia/fx_skia_device_unittest.cpp
index 5666fee..5162e9c 100644
--- a/core/fxge/skia/fx_skia_device_unittest.cpp
+++ b/core/fxge/skia/fx_skia_device_unittest.cpp
@@ -2,6 +2,7 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#include "core/fxge/include/cfx_fontcache.h"
 #include "core/fxge/include/cfx_fxgedevice.h"
 #include "core/fxge/include/cfx_graphstatedata.h"
 #include "core/fxge/include/cfx_pathdata.h"
@@ -38,6 +39,7 @@
   FXTEXT_CHARPOS charPos[] = {{{0, 0, 0, 0}, 0, 1, 1, 4, false, false}};
   CFX_Font font;
   FX_FLOAT fontSize = 1;
+  CFX_FontCache cache;
   CFX_PathData clipPath, clipPath2;
   clipPath.AppendRect(0, 0, 3, 1);
   clipPath2.AppendRect(0, 0, 2, 1);
@@ -57,8 +59,8 @@
     driver->DrawPath(&path1, &matrix, &graphState, 0xFF112233, 0,
                      FXFILL_WINDING, 0);
   } else if (state.m_graphic == State::Graphic::kText) {
-    driver->DrawDeviceText(SK_ARRAY_COUNT(charPos), charPos, &font, &matrix,
-                           fontSize, 0xFF445566);
+    driver->DrawDeviceText(SK_ARRAY_COUNT(charPos), charPos, &font, &cache,
+                           &matrix, fontSize, 0xFF445566);
   }
   if (state.m_save == State::Save::kYes)
     driver->RestoreState(true);
@@ -80,8 +82,8 @@
     driver->DrawPath(&path2, &matrix2, &graphState, 0xFF112233, 0,
                      FXFILL_WINDING, 0);
   } else if (state.m_graphic == State::Graphic::kText) {
-    driver->DrawDeviceText(SK_ARRAY_COUNT(charPos), charPos, &font, &matrix2,
-                           fontSize, 0xFF445566);
+    driver->DrawDeviceText(SK_ARRAY_COUNT(charPos), charPos, &font, &cache,
+                           &matrix2, fontSize, 0xFF445566);
   }
   if (state.m_save == State::Save::kYes)
     driver->RestoreState(false);
diff --git a/core/fxge/win32/fx_win32_print.cpp b/core/fxge/win32/fx_win32_print.cpp
index 2a52a60..bc5bb92 100644
--- a/core/fxge/win32/fx_win32_print.cpp
+++ b/core/fxge/win32/fx_win32_print.cpp
@@ -14,6 +14,7 @@
 
 #include "core/fxge/dib/dib_int.h"
 #include "core/fxge/ge/fx_text_int.h"
+#include "core/fxge/include/cfx_fontcache.h"
 #include "core/fxge/include/cfx_renderdevice.h"
 #include "core/fxge/include/cfx_windowsdevice.h"
 #include "core/fxge/include/fx_freetype.h"
@@ -198,6 +199,7 @@
 FX_BOOL CGdiPrinterDriver::DrawDeviceText(int nChars,
                                           const FXTEXT_CHARPOS* pCharPos,
                                           CFX_Font* pFont,
+                                          CFX_FontCache* pCache,
                                           const CFX_Matrix* pObject2Device,
                                           FX_FLOAT font_size,
                                           uint32_t color) {
diff --git a/core/fxge/win32/win32_int.h b/core/fxge/win32/win32_int.h
index 7e35793..8c3443b 100644
--- a/core/fxge/win32/win32_int.h
+++ b/core/fxge/win32/win32_int.h
@@ -11,6 +11,7 @@
 #include "core/fxge/include/ifx_renderdevicedriver.h"
 #include "core/fxge/win32/dwrite_int.h"
 
+class CFX_FontCache;
 struct FXTEXT_CHARPOS;
 struct WINDIB_Open_Args_;
 
@@ -245,6 +246,7 @@
   FX_BOOL DrawDeviceText(int nChars,
                          const FXTEXT_CHARPOS* pCharPos,
                          CFX_Font* pFont,
+                         CFX_FontCache* pCache,
                          const CFX_Matrix* pObject2Device,
                          FX_FLOAT font_size,
                          uint32_t color) override;
diff --git a/xfa/fde/fde_gedevice.cpp b/xfa/fde/fde_gedevice.cpp
index 15c849f..8279e77 100644
--- a/xfa/fde/fde_gedevice.cpp
+++ b/xfa/fde/fde_gedevice.cpp
@@ -8,6 +8,7 @@
 
 #include <algorithm>
 
+#include "core/fxge/include/cfx_fontcache.h"
 #include "core/fxge/include/cfx_gemodule.h"
 #include "core/fxge/include/cfx_graphstatedata.h"
 #include "core/fxge/include/cfx_renderdevice.h"
@@ -112,6 +113,7 @@
                                       FX_FLOAT fFontSize,
                                       const CFX_Matrix* pMatrix) {
   ASSERT(pBrush && pFont && pCharPos && iCount > 0);
+  CFX_FontCache* pCache = CFX_GEModule::Get()->GetFontCache();
   CFX_Font* pFxFont = pFont->GetDevFont();
   FX_ARGB argb = pBrush->GetColor();
   if ((pFont->GetFontStyles() & FX_FONTSTYLE_Italic) != 0 &&
@@ -152,12 +154,12 @@
         pFxFont = pCurFont->GetDevFont();
 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
         FxFont.SetFace(pFxFont->GetFace());
-        m_pDevice->DrawNormalText(iCurCount, pCurCP, &FxFont, -fFontSize,
-                                  (const CFX_Matrix*)pMatrix, argb,
+        m_pDevice->DrawNormalText(iCurCount, pCurCP, &FxFont, pCache,
+                                  -fFontSize, (const CFX_Matrix*)pMatrix, argb,
                                   FXTEXT_CLEARTYPE);
 #else
-        m_pDevice->DrawNormalText(iCurCount, pCurCP, pFxFont, -fFontSize,
-                                  (const CFX_Matrix*)pMatrix, argb,
+        m_pDevice->DrawNormalText(iCurCount, pCurCP, pFxFont, pCache,
+                                  -fFontSize, (const CFX_Matrix*)pMatrix, argb,
                                   FXTEXT_CLEARTYPE);
 #endif  // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
       }
@@ -174,14 +176,14 @@
 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
     FxFont.SetFace(pFxFont->GetFace());
     FX_BOOL bRet = m_pDevice->DrawNormalText(
-        iCurCount, pCurCP, &FxFont, -fFontSize, (const CFX_Matrix*)pMatrix,
-        argb, FXTEXT_CLEARTYPE);
+        iCurCount, pCurCP, &FxFont, pCache, -fFontSize,
+        (const CFX_Matrix*)pMatrix, argb, FXTEXT_CLEARTYPE);
     FxFont.SetFace(nullptr);
     return bRet;
 #else
-    return m_pDevice->DrawNormalText(iCurCount, pCurCP, pFxFont, -fFontSize,
-                                     (const CFX_Matrix*)pMatrix, argb,
-                                     FXTEXT_CLEARTYPE);
+    return m_pDevice->DrawNormalText(iCurCount, pCurCP, pFxFont, pCache,
+                                     -fFontSize, (const CFX_Matrix*)pMatrix,
+                                     argb, FXTEXT_CLEARTYPE);
 #endif  // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
   }
 
diff --git a/xfa/fxbarcode/oned/BC_OneDimWriter.cpp b/xfa/fxbarcode/oned/BC_OneDimWriter.cpp
index 80f76fb..5f10e84 100644
--- a/xfa/fxbarcode/oned/BC_OneDimWriter.cpp
+++ b/xfa/fxbarcode/oned/BC_OneDimWriter.cpp
@@ -219,9 +219,9 @@
   if (matrix) {
     affine_matrix.Concat(*matrix);
   }
-  device->DrawNormalText(str.GetLength(), pCharPos, m_pFont,
-                         (FX_FLOAT)iFontSize, &affine_matrix, m_fontColor,
-                         FXTEXT_CLEARTYPE);
+  device->DrawNormalText(
+      str.GetLength(), pCharPos, m_pFont, CFX_GEModule::Get()->GetFontCache(),
+      (FX_FLOAT)iFontSize, &affine_matrix, m_fontColor, FXTEXT_CLEARTYPE);
 }
 
 void CBC_OneDimWriter::ShowBitmapChars(CFX_DIBitmap* pOutBitmap,
@@ -238,7 +238,8 @@
   FX_RECT geRect(0, 0, (int)geWidth, iTextHeight);
   ge.FillRect(&geRect, m_backgroundColor);
   CFX_Matrix affine_matrix(1.0, 0.0, 0.0, -1.0, 0.0, (FX_FLOAT)iFontSize);
-  ge.DrawNormalText(str.GetLength(), pCharPos, m_pFont, (FX_FLOAT)iFontSize,
+  ge.DrawNormalText(str.GetLength(), pCharPos, m_pFont,
+                    CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize,
                     &affine_matrix, m_fontColor, FXTEXT_CLEARTYPE);
   CFX_FxgeDevice geBitmap;
   geBitmap.Attach(pOutBitmap, false, nullptr, false);
diff --git a/xfa/fxbarcode/oned/BC_OnedEAN13Writer.cpp b/xfa/fxbarcode/oned/BC_OnedEAN13Writer.cpp
index 6327387..e2bbfd5 100644
--- a/xfa/fxbarcode/oned/BC_OnedEAN13Writer.cpp
+++ b/xfa/fxbarcode/oned/BC_OnedEAN13Writer.cpp
@@ -231,7 +231,8 @@
     ge.Create(strWidth, iTextHeight, FXDIB_Argb, nullptr);
     FX_RECT rect(0, 0, strWidth, iTextHeight);
     ge.FillRect(&rect, m_backgroundColor);
-    ge.DrawNormalText(iLen, pCharPos + 1, m_pFont, (FX_FLOAT)iFontSize,
+    ge.DrawNormalText(iLen, pCharPos + 1, m_pFont,
+                      CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize,
                       &affine_matrix, m_fontColor, FXTEXT_CLEARTYPE);
     geBitmap.SetDIBits(ge.GetBitmap(), leftPosition, m_Height - iTextHeight);
   } else {
@@ -241,8 +242,9 @@
     if (matrix) {
       affine_matrix1.Concat(*matrix);
     }
-    device->DrawNormalText(iLen, pCharPos + 1, m_pFont, (FX_FLOAT)iFontSize,
-                           &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
+    device->DrawNormalText(
+        iLen, pCharPos + 1, m_pFont, CFX_GEModule::Get()->GetFontCache(),
+        (FX_FLOAT)iFontSize, &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
   }
   tempStr = str.Mid(7, 6);
   iLen = tempStr.GetLength();
@@ -251,7 +253,8 @@
   if (pOutBitmap) {
     FX_RECT rect1(0, 0, strWidth, iTextHeight);
     ge.FillRect(&rect1, m_backgroundColor);
-    ge.DrawNormalText(iLen, pCharPos + 7, m_pFont, (FX_FLOAT)iFontSize,
+    ge.DrawNormalText(iLen, pCharPos + 7, m_pFont,
+                      CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize,
                       &affine_matrix, m_fontColor, FXTEXT_CLEARTYPE);
     geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 47 * multiple,
                        m_Height - iTextHeight);
@@ -263,8 +266,9 @@
     if (matrix) {
       affine_matrix1.Concat(*matrix);
     }
-    device->DrawNormalText(iLen, pCharPos + 7, m_pFont, (FX_FLOAT)iFontSize,
-                           &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
+    device->DrawNormalText(
+        iLen, pCharPos + 7, m_pFont, CFX_GEModule::Get()->GetFontCache(),
+        (FX_FLOAT)iFontSize, &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
   }
   tempStr = str.Mid(0, 1);
   iLen = tempStr.GetLength();
@@ -278,7 +282,8 @@
     delete ge.GetBitmap();
     ge.Create(strWidth, iTextHeight, FXDIB_Argb, nullptr);
     ge.GetBitmap()->Clear(m_backgroundColor);
-    ge.DrawNormalText(iLen, pCharPos, m_pFont, (FX_FLOAT)iFontSize,
+    ge.DrawNormalText(iLen, pCharPos, m_pFont,
+                      CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize,
                       &affine_matrix, m_fontColor, FXTEXT_CLEARTYPE);
     geBitmap.SetDIBits(ge.GetBitmap(), 0, m_Height - iTextHeight);
   } else {
@@ -287,8 +292,9 @@
     if (matrix) {
       affine_matrix1.Concat(*matrix);
     }
-    device->DrawNormalText(iLen, pCharPos, m_pFont, (FX_FLOAT)iFontSize,
-                           &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
+    device->DrawNormalText(
+        iLen, pCharPos, m_pFont, CFX_GEModule::Get()->GetFontCache(),
+        (FX_FLOAT)iFontSize, &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
   }
   FX_Free(pCharPos);
 }
diff --git a/xfa/fxbarcode/oned/BC_OnedEAN8Writer.cpp b/xfa/fxbarcode/oned/BC_OnedEAN8Writer.cpp
index fb95f26..0a7f3a8 100644
--- a/xfa/fxbarcode/oned/BC_OnedEAN8Writer.cpp
+++ b/xfa/fxbarcode/oned/BC_OnedEAN8Writer.cpp
@@ -222,7 +222,8 @@
     delete ge.GetBitmap();
     ge.Create(strWidth, iTextHeight, FXDIB_Argb, nullptr);
     ge.GetBitmap()->Clear(m_backgroundColor);
-    ge.DrawNormalText(iLen, pCharPos, m_pFont, (FX_FLOAT)iFontSize,
+    ge.DrawNormalText(iLen, pCharPos, m_pFont,
+                      CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize,
                       &affine_matrix, m_fontColor, FXTEXT_CLEARTYPE);
     geBitmap.SetDIBits(ge.GetBitmap(), leftPosition, m_Height - iTextHeight);
   } else {
@@ -230,8 +231,9 @@
                               (FX_FLOAT)leftPosition * m_outputHScale,
                               (FX_FLOAT)(m_Height - iTextHeight + iFontSize));
     affine_matrix1.Concat(*matrix);
-    device->DrawNormalText(iLen, pCharPos, m_pFont, (FX_FLOAT)iFontSize,
-                           &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
+    device->DrawNormalText(
+        iLen, pCharPos, m_pFont, CFX_GEModule::Get()->GetFontCache(),
+        (FX_FLOAT)iFontSize, &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
   }
   tempStr = str.Mid(4, 4);
   iLen = tempStr.GetLength();
@@ -241,7 +243,8 @@
     delete ge.GetBitmap();
     ge.Create(strWidth, iTextHeight, FXDIB_Argb, nullptr);
     ge.GetBitmap()->Clear(m_backgroundColor);
-    ge.DrawNormalText(iLen, pCharPos + 4, m_pFont, (FX_FLOAT)iFontSize,
+    ge.DrawNormalText(iLen, pCharPos + 4, m_pFont,
+                      CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize,
                       &affine_matrix, m_fontColor, FXTEXT_CLEARTYPE);
     geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 33 * multiple,
                        m_Height - iTextHeight);
@@ -253,8 +256,9 @@
     if (matrix) {
       affine_matrix1.Concat(*matrix);
     }
-    device->DrawNormalText(iLen, pCharPos + 4, m_pFont, (FX_FLOAT)iFontSize,
-                           &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
+    device->DrawNormalText(
+        iLen, pCharPos + 4, m_pFont, CFX_GEModule::Get()->GetFontCache(),
+        (FX_FLOAT)iFontSize, &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
   }
   FX_Free(pCharPos);
 }
diff --git a/xfa/fxbarcode/oned/BC_OnedUPCAWriter.cpp b/xfa/fxbarcode/oned/BC_OnedUPCAWriter.cpp
index 12acb52..01660b7 100644
--- a/xfa/fxbarcode/oned/BC_OnedUPCAWriter.cpp
+++ b/xfa/fxbarcode/oned/BC_OnedUPCAWriter.cpp
@@ -190,7 +190,8 @@
   if (pOutBitmap) {
     ge.Create((int)strWidth, iTextHeight, FXDIB_Argb, nullptr);
     ge.GetBitmap()->Clear(m_backgroundColor);
-    ge.DrawNormalText(iLen, pCharPos + 1, m_pFont, (FX_FLOAT)iFontSize,
+    ge.DrawNormalText(iLen, pCharPos + 1, m_pFont,
+                      CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize,
                       &affine_matrix, m_fontColor, FXTEXT_CLEARTYPE);
     geBitmap.SetDIBits(ge.GetBitmap(), leftPosition, m_Height - iTextHeight);
   } else {
@@ -200,8 +201,9 @@
     if (matrix) {
       affine_matrix1.Concat(*matrix);
     }
-    device->DrawNormalText(iLen, pCharPos + 1, m_pFont, (FX_FLOAT)iFontSize,
-                           &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
+    device->DrawNormalText(
+        iLen, pCharPos + 1, m_pFont, CFX_GEModule::Get()->GetFontCache(),
+        (FX_FLOAT)iFontSize, &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
   }
   tempStr = str.Mid(6, 5);
   iLen = tempStr.GetLength();
@@ -209,7 +211,8 @@
   if (pOutBitmap) {
     FX_RECT rect2(0, 0, (int)strWidth, iTextHeight);
     ge.FillRect(&rect2, m_backgroundColor);
-    ge.DrawNormalText(iLen, pCharPos + 6, m_pFont, (FX_FLOAT)iFontSize,
+    ge.DrawNormalText(iLen, pCharPos + 6, m_pFont,
+                      CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize,
                       &affine_matrix, m_fontColor, FXTEXT_CLEARTYPE);
     geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 40 * multiple,
                        m_Height - iTextHeight);
@@ -221,8 +224,9 @@
     if (matrix) {
       affine_matrix1.Concat(*matrix);
     }
-    device->DrawNormalText(iLen, pCharPos + 6, m_pFont, (FX_FLOAT)iFontSize,
-                           &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
+    device->DrawNormalText(
+        iLen, pCharPos + 6, m_pFont, CFX_GEModule::Get()->GetFontCache(),
+        (FX_FLOAT)iFontSize, &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
   }
   tempStr = str.Mid(0, 1);
   iLen = tempStr.GetLength();
@@ -235,7 +239,8 @@
     delete ge.GetBitmap();
     ge.Create((int)strWidth, iTextHeight, FXDIB_Argb, nullptr);
     ge.GetBitmap()->Clear(m_backgroundColor);
-    ge.DrawNormalText(iLen, pCharPos, m_pFont, (FX_FLOAT)iFontSize,
+    ge.DrawNormalText(iLen, pCharPos, m_pFont,
+                      CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize,
                       &affine_matrix, m_fontColor, FXTEXT_CLEARTYPE);
     geBitmap.SetDIBits(ge.GetBitmap(), 0, m_Height - iTextHeight);
   } else {
@@ -244,8 +249,9 @@
     if (matrix) {
       affine_matrix1.Concat(*matrix);
     }
-    device->DrawNormalText(iLen, pCharPos, m_pFont, (FX_FLOAT)iFontSize,
-                           &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
+    device->DrawNormalText(
+        iLen, pCharPos, m_pFont, CFX_GEModule::Get()->GetFontCache(),
+        (FX_FLOAT)iFontSize, &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
   }
   tempStr = str.Mid(11, 1);
   iLen = tempStr.GetLength();
@@ -254,7 +260,8 @@
     delete ge.GetBitmap();
     ge.Create((int)strWidth, iTextHeight, FXDIB_Argb, nullptr);
     ge.GetBitmap()->Clear(m_backgroundColor);
-    ge.DrawNormalText(iLen, pCharPos + 11, m_pFont, (FX_FLOAT)iFontSize,
+    ge.DrawNormalText(iLen, pCharPos + 11, m_pFont,
+                      CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize,
                       &affine_matrix, m_fontColor, FXTEXT_CLEARTYPE);
     geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 85 * multiple,
                        m_Height - iTextHeight);
@@ -266,8 +273,9 @@
     if (matrix) {
       affine_matrix1.Concat(*matrix);
     }
-    device->DrawNormalText(iLen, pCharPos + 11, m_pFont, (FX_FLOAT)iFontSize,
-                           &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
+    device->DrawNormalText(
+        iLen, pCharPos + 11, m_pFont, CFX_GEModule::Get()->GetFontCache(),
+        (FX_FLOAT)iFontSize, &affine_matrix1, m_fontColor, FXTEXT_CLEARTYPE);
   }
   FX_Free(pCharPos);
 }
diff --git a/xfa/fxgraphics/cfx_graphics.cpp b/xfa/fxgraphics/cfx_graphics.cpp
index b9f8287..068ed01 100644
--- a/xfa/fxgraphics/cfx_graphics.cpp
+++ b/xfa/fxgraphics/cfx_graphics.cpp
@@ -1294,8 +1294,9 @@
     m.Concat(*matrix);
   }
   FX_BOOL result = m_renderDevice->DrawNormalText(
-      length, charPos, m_info.font, -m_info.fontSize * m_info.fontHScale, &m,
-      m_info.fillColor->m_info.argb, FXTEXT_CLEARTYPE);
+      length, charPos, m_info.font, CFX_GEModule::Get()->GetFontCache(),
+      -m_info.fontSize * m_info.fontHScale, &m, m_info.fillColor->m_info.argb,
+      FXTEXT_CLEARTYPE);
   if (!result)
     return FWL_Error::Indefinite;
   FX_Free(charPos);