Add CPDF_Font::GetMutableFontDict()

-- Pass retained references to CPDF_Font() constructors.
-- Mark const otherwise where possible.

Change-Id: I3096a7fde65e5c4afe8ba87963f854a63e63daf6
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/94612
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index 5bb0ff0..3a428a4 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -567,7 +567,7 @@
   if (maybe_name.has_value()) {
     dictName = std::move(maybe_name.value());
   } else {
-    CPDF_Object* pIndirectFont = pFont->GetFontDict();
+    const CPDF_Object* pIndirectFont = pFont->GetFontDict();
     if (pIndirectFont->IsInline()) {
       // In this case we assume it must be a standard font
       auto pFontDict = pdfium::MakeRetain<CPDF_Dictionary>();
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
index 55e8bd1..7ebd924 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
@@ -319,7 +319,7 @@
   {
     // Set the text object font and text
     auto pTextObj = std::make_unique<CPDF_TextObject>();
-    CPDF_Dictionary* pDict = pDoc->NewIndirect<CPDF_Dictionary>();
+    RetainPtr<CPDF_Dictionary> pDict(pDoc->NewIndirect<CPDF_Dictionary>());
     pDict->SetNewFor<CPDF_Name>("Type", "Font");
     pDict->SetNewFor<CPDF_Name>("Subtype", "TrueType");
 
diff --git a/core/fpdfapi/font/cfx_stockfontarray.cpp b/core/fpdfapi/font/cfx_stockfontarray.cpp
index adc7069..231a1b5 100644
--- a/core/fpdfapi/font/cfx_stockfontarray.cpp
+++ b/core/fpdfapi/font/cfx_stockfontarray.cpp
@@ -17,7 +17,10 @@
 CFX_StockFontArray::~CFX_StockFontArray() {
   for (size_t i = 0; i < std::size(m_StockFonts); ++i) {
     if (m_StockFonts[i]) {
-      RetainPtr<CPDF_Dictionary> destroy(m_StockFonts[i]->GetFontDict());
+      // Ensure m_StockFonts[i]'s dict is cleared before releasing what
+      // may be the last reference to it.
+      RetainPtr<CPDF_Dictionary> destroy =
+          m_StockFonts[i]->GetMutableFontDict();
       m_StockFonts[i]->ClearFontDict();
     }
   }
diff --git a/core/fpdfapi/font/cpdf_cidfont.cpp b/core/fpdfapi/font/cpdf_cidfont.cpp
index 46b595e..d5d8b2e 100644
--- a/core/fpdfapi/font/cpdf_cidfont.cpp
+++ b/core/fpdfapi/font/cpdf_cidfont.cpp
@@ -8,6 +8,7 @@
 
 #include <algorithm>
 #include <limits>
+#include <utility>
 #include <vector>
 
 #include "build/build_config.h"
@@ -275,8 +276,9 @@
 
 }  // namespace
 
-CPDF_CIDFont::CPDF_CIDFont(CPDF_Document* pDocument, CPDF_Dictionary* pFontDict)
-    : CPDF_Font(pDocument, pFontDict) {
+CPDF_CIDFont::CPDF_CIDFont(CPDF_Document* pDocument,
+                           RetainPtr<CPDF_Dictionary> pFontDict)
+    : CPDF_Font(pDocument, std::move(pFontDict)) {
   for (size_t i = 0; i < std::size(m_CharBBox); ++i)
     m_CharBBox[i] = FX_RECT(-1, -1, -1, -1);
 }
diff --git a/core/fpdfapi/font/cpdf_cidfont.h b/core/fpdfapi/font/cpdf_cidfont.h
index 17cb56e..2eec6f5 100644
--- a/core/fpdfapi/font/cpdf_cidfont.h
+++ b/core/fpdfapi/font/cpdf_cidfont.h
@@ -68,7 +68,7 @@
     kTrueType  // CIDFontType2
   };
 
-  CPDF_CIDFont(CPDF_Document* pDocument, CPDF_Dictionary* pFontDict);
+  CPDF_CIDFont(CPDF_Document* pDocument, RetainPtr<CPDF_Dictionary> pFontDict);
 
   void LoadGB2312();
   int GetGlyphIndex(uint32_t unicodeb, bool* pVertGlyph);
diff --git a/core/fpdfapi/font/cpdf_cidfont_unittest.cpp b/core/fpdfapi/font/cpdf_cidfont_unittest.cpp
index 9287780..9b0cd92 100644
--- a/core/fpdfapi/font/cpdf_cidfont_unittest.cpp
+++ b/core/fpdfapi/font/cpdf_cidfont_unittest.cpp
@@ -31,7 +31,7 @@
     font_dict->SetFor("DescendantFonts", std::move(descendant_fonts));
   }
 
-  auto font = pdfium::MakeRetain<CPDF_CIDFont>(&doc, font_dict.Get());
+  auto font = pdfium::MakeRetain<CPDF_CIDFont>(&doc, std::move(font_dict));
   ASSERT_TRUE(font->Load());
 
   // It would be nice if we can test more values here. However, the glyph
diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp
index caa37a2..835bd11 100644
--- a/core/fpdfapi/font/cpdf_font.cpp
+++ b/core/fpdfapi/font/cpdf_font.cpp
@@ -47,10 +47,11 @@
 
 }  // namespace
 
-CPDF_Font::CPDF_Font(CPDF_Document* pDocument, CPDF_Dictionary* pFontDict)
+CPDF_Font::CPDF_Font(CPDF_Document* pDocument,
+                     RetainPtr<CPDF_Dictionary> pFontDict)
     : m_pDocument(pDocument),
-      m_pFontDict(pFontDict),
-      m_BaseFontName(pFontDict->GetStringFor("BaseFont")) {}
+      m_pFontDict(std::move(pFontDict)),
+      m_BaseFontName(m_pFontDict->GetStringFor("BaseFont")) {}
 
 CPDF_Font::~CPDF_Font() {
   if (m_pFontFile) {
@@ -295,14 +296,14 @@
   pDict->SetNewFor<CPDF_Name>("BaseFont", fontname);
   pDict->SetNewFor<CPDF_Name>("Encoding",
                               pdfium::font_encodings::kWinAnsiEncoding);
-  pFont = CPDF_Font::Create(nullptr, pDict.Get(), nullptr);
+  pFont = CPDF_Font::Create(nullptr, std::move(pDict), nullptr);
   pFontGlobals->Set(pDoc, font_id.value(), pFont);
   return pFont;
 }
 
 // static
 RetainPtr<CPDF_Font> CPDF_Font::Create(CPDF_Document* pDoc,
-                                       CPDF_Dictionary* pFontDict,
+                                       RetainPtr<CPDF_Dictionary> pFontDict,
                                        FormFactoryIface* pFactory) {
   ByteString type = pFontDict->GetStringFor("Subtype");
   RetainPtr<CPDF_Font> pFont;
@@ -313,18 +314,19 @@
         const CPDF_Dictionary* pFontDesc =
             pFontDict->GetDictFor("FontDescriptor");
         if (!pFontDesc || !pFontDesc->KeyExist("FontFile2"))
-          pFont = pdfium::MakeRetain<CPDF_CIDFont>(pDoc, pFontDict);
+          pFont = pdfium::MakeRetain<CPDF_CIDFont>(pDoc, std::move(pFontDict));
         break;
       }
     }
     if (!pFont)
-      pFont = pdfium::MakeRetain<CPDF_TrueTypeFont>(pDoc, pFontDict);
+      pFont = pdfium::MakeRetain<CPDF_TrueTypeFont>(pDoc, std::move(pFontDict));
   } else if (type == "Type3") {
-    pFont = pdfium::MakeRetain<CPDF_Type3Font>(pDoc, pFontDict, pFactory);
+    pFont = pdfium::MakeRetain<CPDF_Type3Font>(pDoc, std::move(pFontDict),
+                                               pFactory);
   } else if (type == "Type0") {
-    pFont = pdfium::MakeRetain<CPDF_CIDFont>(pDoc, pFontDict);
+    pFont = pdfium::MakeRetain<CPDF_CIDFont>(pDoc, std::move(pFontDict));
   } else {
-    pFont = pdfium::MakeRetain<CPDF_Type1Font>(pDoc, pFontDict);
+    pFont = pdfium::MakeRetain<CPDF_Type1Font>(pDoc, std::move(pFontDict));
   }
   if (!pFont->Load())
     return nullptr;
diff --git a/core/fpdfapi/font/cpdf_font.h b/core/fpdfapi/font/cpdf_font.h
index 14a9ea8..43a0e82 100644
--- a/core/fpdfapi/font/cpdf_font.h
+++ b/core/fpdfapi/font/cpdf_font.h
@@ -63,7 +63,7 @@
 
   // |pFactory| only required for Type3 fonts.
   static RetainPtr<CPDF_Font> Create(CPDF_Document* pDoc,
-                                     CPDF_Dictionary* pFontDict,
+                                     RetainPtr<CPDF_Dictionary> pFontDict,
                                      FormFactoryIface* pFactory);
   static RetainPtr<CPDF_Font> GetStockFont(CPDF_Document* pDoc,
                                            ByteStringView fontname);
@@ -100,7 +100,8 @@
   ByteString GetBaseFontName() const { return m_BaseFontName; }
   CFX_SubstFont* GetSubstFont() const { return m_Font.GetSubstFont(); }
   bool IsEmbedded() const { return IsType3Font() || m_pFontFile != nullptr; }
-  CPDF_Dictionary* GetFontDict() const { return m_pFontDict.Get(); }
+  RetainPtr<CPDF_Dictionary> GetMutableFontDict() { return m_pFontDict; }
+  const CPDF_Dictionary* GetFontDict() const { return m_pFontDict.Get(); }
   void ClearFontDict() { m_pFontDict = nullptr; }
   bool IsStandardFont() const;
   bool HasFace() const { return !!m_Font.GetFaceRec(); }
@@ -129,7 +130,7 @@
   CFX_Font* GetFontFallback(int position);
 
  protected:
-  CPDF_Font(CPDF_Document* pDocument, CPDF_Dictionary* pFontDict);
+  CPDF_Font(CPDF_Document* pDocument, RetainPtr<CPDF_Dictionary> pFontDict);
 
   static int TT2PDF(FT_Pos m, FXFT_FaceRec* face);
 
diff --git a/core/fpdfapi/font/cpdf_simplefont.cpp b/core/fpdfapi/font/cpdf_simplefont.cpp
index 06d60ac..9f710ac 100644
--- a/core/fpdfapi/font/cpdf_simplefont.cpp
+++ b/core/fpdfapi/font/cpdf_simplefont.cpp
@@ -8,6 +8,7 @@
 
 #include <algorithm>
 #include <iterator>
+#include <utility>
 
 #include "constants/font_encodings.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
@@ -34,8 +35,8 @@
 }  // namespace
 
 CPDF_SimpleFont::CPDF_SimpleFont(CPDF_Document* pDocument,
-                                 CPDF_Dictionary* pFontDict)
-    : CPDF_Font(pDocument, pFontDict) {
+                                 RetainPtr<CPDF_Dictionary> pFontDict)
+    : CPDF_Font(pDocument, std::move(pFontDict)) {
   memset(m_CharWidth, 0xff, sizeof(m_CharWidth));
   memset(m_GlyphIndex, 0xff, sizeof(m_GlyphIndex));
   for (size_t i = 0; i < std::size(m_CharBBox); ++i)
diff --git a/core/fpdfapi/font/cpdf_simplefont.h b/core/fpdfapi/font/cpdf_simplefont.h
index b854f79..353a91a 100644
--- a/core/fpdfapi/font/cpdf_simplefont.h
+++ b/core/fpdfapi/font/cpdf_simplefont.h
@@ -34,7 +34,8 @@
  protected:
   static constexpr size_t kInternalTableSize = 256;
 
-  CPDF_SimpleFont(CPDF_Document* pDocument, CPDF_Dictionary* pFontDict);
+  CPDF_SimpleFont(CPDF_Document* pDocument,
+                  RetainPtr<CPDF_Dictionary> pFontDict);
 
   virtual void LoadGlyphMap() = 0;
 
diff --git a/core/fpdfapi/font/cpdf_truetypefont.cpp b/core/fpdfapi/font/cpdf_truetypefont.cpp
index a8f6ca9..a9188b8 100644
--- a/core/fpdfapi/font/cpdf_truetypefont.cpp
+++ b/core/fpdfapi/font/cpdf_truetypefont.cpp
@@ -7,6 +7,7 @@
 #include "core/fpdfapi/font/cpdf_truetypefont.h"
 
 #include <algorithm>
+#include <utility>
 
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fxge/fx_font.h"
@@ -34,8 +35,8 @@
 }  // namespace
 
 CPDF_TrueTypeFont::CPDF_TrueTypeFont(CPDF_Document* pDocument,
-                                     CPDF_Dictionary* pFontDict)
-    : CPDF_SimpleFont(pDocument, pFontDict) {}
+                                     RetainPtr<CPDF_Dictionary> pFontDict)
+    : CPDF_SimpleFont(pDocument, std::move(pFontDict)) {}
 
 CPDF_TrueTypeFont::~CPDF_TrueTypeFont() = default;
 
diff --git a/core/fpdfapi/font/cpdf_truetypefont.h b/core/fpdfapi/font/cpdf_truetypefont.h
index 12025cb..0e5273b 100644
--- a/core/fpdfapi/font/cpdf_truetypefont.h
+++ b/core/fpdfapi/font/cpdf_truetypefont.h
@@ -23,7 +23,8 @@
  private:
   enum class CharmapType { kMSUnicode, kMSSymbol, kMacRoman, kOther };
 
-  CPDF_TrueTypeFont(CPDF_Document* pDocument, CPDF_Dictionary* pFontDict);
+  CPDF_TrueTypeFont(CPDF_Document* pDocument,
+                    RetainPtr<CPDF_Dictionary> pFontDict);
 
   // CPDF_Font:
   bool Load() override;
diff --git a/core/fpdfapi/font/cpdf_type1font.cpp b/core/fpdfapi/font/cpdf_type1font.cpp
index bacb04d..0bbe416 100644
--- a/core/fpdfapi/font/cpdf_type1font.cpp
+++ b/core/fpdfapi/font/cpdf_type1font.cpp
@@ -8,6 +8,7 @@
 
 #include <algorithm>
 #include <iterator>
+#include <utility>
 
 #include "build/build_config.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
@@ -63,8 +64,8 @@
 }  // namespace
 
 CPDF_Type1Font::CPDF_Type1Font(CPDF_Document* pDocument,
-                               CPDF_Dictionary* pFontDict)
-    : CPDF_SimpleFont(pDocument, pFontDict) {
+                               RetainPtr<CPDF_Dictionary> pFontDict)
+    : CPDF_SimpleFont(pDocument, std::move(pFontDict)) {
 #if BUILDFLAG(IS_APPLE)
   memset(m_ExtGID, 0xff, sizeof(m_ExtGID));
 #endif
diff --git a/core/fpdfapi/font/cpdf_type1font.h b/core/fpdfapi/font/cpdf_type1font.h
index 0f68f43..4756aec 100644
--- a/core/fpdfapi/font/cpdf_type1font.h
+++ b/core/fpdfapi/font/cpdf_type1font.h
@@ -30,7 +30,8 @@
   bool IsBase14Font() const { return m_Base14Font.has_value(); }
 
  private:
-  CPDF_Type1Font(CPDF_Document* pDocument, CPDF_Dictionary* pFontDict);
+  CPDF_Type1Font(CPDF_Document* pDocument,
+                 RetainPtr<CPDF_Dictionary> pFontDict);
 
   // CPDF_Font:
   bool Load() override;
diff --git a/core/fpdfapi/font/cpdf_type3font.cpp b/core/fpdfapi/font/cpdf_type3font.cpp
index 82ea835..f2ed02b 100644
--- a/core/fpdfapi/font/cpdf_type3font.cpp
+++ b/core/fpdfapi/font/cpdf_type3font.cpp
@@ -26,9 +26,10 @@
 }  // namespace
 
 CPDF_Type3Font::CPDF_Type3Font(CPDF_Document* pDocument,
-                               CPDF_Dictionary* pFontDict,
+                               RetainPtr<CPDF_Dictionary> pFontDict,
                                FormFactoryIface* pFormFactory)
-    : CPDF_SimpleFont(pDocument, pFontDict), m_pFormFactory(pFormFactory) {
+    : CPDF_SimpleFont(pDocument, std::move(pFontDict)),
+      m_pFormFactory(pFormFactory) {
   DCHECK(GetDocument());
 }
 
diff --git a/core/fpdfapi/font/cpdf_type3font.h b/core/fpdfapi/font/cpdf_type3font.h
index 2e883ec..067d9dc 100644
--- a/core/fpdfapi/font/cpdf_type3font.h
+++ b/core/fpdfapi/font/cpdf_type3font.h
@@ -44,7 +44,7 @@
 
  private:
   CPDF_Type3Font(CPDF_Document* pDocument,
-                 CPDF_Dictionary* pFontDict,
+                 RetainPtr<CPDF_Dictionary> pFontDict,
                  FormFactoryIface* pFormFactory);
 
   // CPDF_Font:
diff --git a/core/fpdfapi/page/cpdf_docpagedata.cpp b/core/fpdfapi/page/cpdf_docpagedata.cpp
index f36bc7b..d27101e 100644
--- a/core/fpdfapi/page/cpdf_docpagedata.cpp
+++ b/core/fpdfapi/page/cpdf_docpagedata.cpp
@@ -119,7 +119,7 @@
   return flags;
 }
 
-void ProcessNonbCJK(CPDF_Dictionary* pBaseDict,
+void ProcessNonbCJK(const RetainPtr<CPDF_Dictionary>& pBaseDict,
                     bool bold,
                     bool italic,
                     ByteString basefont,
@@ -177,11 +177,12 @@
   CPDF_PageModule::GetInstance()->ClearStockFont(GetDocument());
 }
 
-RetainPtr<CPDF_Font> CPDF_DocPageData::GetFont(CPDF_Dictionary* pFontDict) {
+RetainPtr<CPDF_Font> CPDF_DocPageData::GetFont(
+    RetainPtr<CPDF_Dictionary> pFontDict) {
   if (!pFontDict)
     return nullptr;
 
-  auto it = m_FontMap.find(pFontDict);
+  auto it = m_FontMap.find(pFontDict.Get());
   if (it != m_FontMap.end() && it->second)
     return pdfium::WrapRetain(it->second.Get());
 
@@ -190,7 +191,7 @@
   if (!pFont)
     return nullptr;
 
-  m_FontMap[pFontDict].Reset(pFont.Get());
+  m_FontMap[pFontDict.Get()].Reset(pFont.Get());
   return pFont;
 }
 
@@ -220,7 +221,8 @@
     return pdfium::WrapRetain(pFont);
   }
 
-  CPDF_Dictionary* pDict = GetDocument()->NewIndirect<CPDF_Dictionary>();
+  RetainPtr<CPDF_Dictionary> pDict(
+      GetDocument()->NewIndirect<CPDF_Dictionary>());
   pDict->SetNewFor<CPDF_Name>("Type", "Font");
   pDict->SetNewFor<CPDF_Name>("Subtype", "Type1");
   pDict->SetNewFor<CPDF_Name>("BaseFont", fontName);
@@ -234,7 +236,7 @@
   if (!pFont)
     return nullptr;
 
-  m_FontMap[pDict].Reset(pFont.Get());
+  m_FontMap[pDict.Get()].Reset(pFont.Get());
   return pFont;
 }
 
@@ -479,10 +481,11 @@
       CalculateFlags(pFont->IsBold(), pFont->IsItalic(), pFont->IsFixedWidth(),
                      false, false, charset == FX_Charset::kSymbol);
 
-  CPDF_Dictionary* pBaseDict = GetDocument()->NewIndirect<CPDF_Dictionary>();
+  RetainPtr<CPDF_Dictionary> pBaseDict(
+      GetDocument()->NewIndirect<CPDF_Dictionary>());
   pBaseDict->SetNewFor<CPDF_Name>("Type", "Font");
   auto pEncoding = std::make_unique<CFX_UnicodeEncoding>(pFont.get());
-  CPDF_Dictionary* pFontDict = pBaseDict;
+  RetainPtr<CPDF_Dictionary> pFontDict = pBaseDict;
   if (!bCJK) {
     auto pWidths = pdfium::MakeRetain<CPDF_Array>();
     for (int charcode = 32; charcode < 128; charcode++) {
@@ -500,7 +503,7 @@
         pWidths->AppendNew<CPDF_Number>(char_width);
       }
     } else {
-      size_t i = CalculateEncodingDict(charset, pBaseDict);
+      size_t i = CalculateEncodingDict(charset, pBaseDict.Get());
       if (i < std::size(kFX_CharsetUnicodes)) {
         const uint16_t* pUnicodes = kFX_CharsetUnicodes[i].m_pUnicodes;
         for (int j = 0; j < 128; j++) {
@@ -592,16 +595,17 @@
                  ptm->otmrcFontBox.right, ptm->otmrcFontBox.top};
   FX_Free(tm_buf);
   basefont.Replace(" ", "");
-  CPDF_Dictionary* pBaseDict = GetDocument()->NewIndirect<CPDF_Dictionary>();
+  RetainPtr<CPDF_Dictionary> pBaseDict(
+      GetDocument()->NewIndirect<CPDF_Dictionary>());
   pBaseDict->SetNewFor<CPDF_Name>("Type", "Font");
-  CPDF_Dictionary* pFontDict = pBaseDict;
+  RetainPtr<CPDF_Dictionary> pFontDict = pBaseDict;
   if (!bCJK) {
     if (eCharset == FX_Charset::kANSI || eCharset == FX_Charset::kDefault ||
         eCharset == FX_Charset::kSymbol) {
       pBaseDict->SetNewFor<CPDF_Name>("Encoding",
                                       pdfium::font_encodings::kWinAnsiEncoding);
     } else {
-      CalculateEncodingDict(eCharset, pBaseDict);
+      CalculateEncodingDict(eCharset, pBaseDict.Get());
     }
     int char_widths[224];
     GetCharWidth(hDC, 32, 255, char_widths);
@@ -631,7 +635,7 @@
   hFont = SelectObject(hDC, hFont);
   DeleteObject(hFont);
   DeleteDC(hDC);
-  return GetFont(pBaseDict);
+  return GetFont(std::move(pBaseDict));
 }
 #endif  //  BUILDFLAG(IS_WIN)
 
@@ -664,7 +668,7 @@
 }
 
 CPDF_Dictionary* CPDF_DocPageData::ProcessbCJK(
-    CPDF_Dictionary* pBaseDict,
+    const RetainPtr<CPDF_Dictionary>& pBaseDict,
     FX_Charset charset,
     ByteString basefont,
     std::function<void(wchar_t, wchar_t, CPDF_Array*)> Insert) {
diff --git a/core/fpdfapi/page/cpdf_docpagedata.h b/core/fpdfapi/page/cpdf_docpagedata.h
index bd521a4..30ba851 100644
--- a/core/fpdfapi/page/cpdf_docpagedata.h
+++ b/core/fpdfapi/page/cpdf_docpagedata.h
@@ -54,7 +54,7 @@
 
   RetainPtr<CPDF_Font> AddFont(std::unique_ptr<CFX_Font> pFont,
                                FX_Charset charset);
-  RetainPtr<CPDF_Font> GetFont(CPDF_Dictionary* pFontDict);
+  RetainPtr<CPDF_Font> GetFont(RetainPtr<CPDF_Dictionary> pFontDict);
   RetainPtr<CPDF_Font> AddStandardFont(const ByteString& fontName,
                                        const CPDF_FontEncoding* pEncoding);
   RetainPtr<CPDF_Font> GetStandardFont(const ByteString& fontName,
@@ -99,7 +99,7 @@
 
   size_t CalculateEncodingDict(FX_Charset charset, CPDF_Dictionary* pBaseDict);
   CPDF_Dictionary* ProcessbCJK(
-      CPDF_Dictionary* pBaseDict,
+      const RetainPtr<CPDF_Dictionary>& pBaseDict,
       FX_Charset charset,
       ByteString basefont,
       std::function<void(wchar_t, wchar_t, CPDF_Array*)> Insert);
diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
index d407bc1..12ce7e0 100644
--- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp
+++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
@@ -1137,13 +1137,14 @@
 
 RetainPtr<CPDF_Font> CPDF_StreamContentParser::FindFont(
     const ByteString& name) {
-  CPDF_Dictionary* pFontDict = ToDictionary(FindResourceObj("Font", name));
+  RetainPtr<CPDF_Dictionary> pFontDict(
+      ToDictionary(FindResourceObj("Font", name)));
   if (!pFontDict) {
     return CPDF_Font::GetStockFont(m_pDocument.Get(),
                                    CFX_Font::kDefaultAnsiFontName);
   }
-  RetainPtr<CPDF_Font> pFont =
-      CPDF_DocPageData::FromDocument(m_pDocument.Get())->GetFont(pFontDict);
+  RetainPtr<CPDF_Font> pFont = CPDF_DocPageData::FromDocument(m_pDocument.Get())
+                                   ->GetFont(std::move(pFontDict));
   if (pFont && pFont->IsType3Font()) {
     pFont->AsType3Font()->SetPageResources(m_pResources.Get());
     pFont->AsType3Font()->CheckType3FontMetrics();
diff --git a/core/fpdfapi/page/cpdf_textstate.cpp b/core/fpdfapi/page/cpdf_textstate.cpp
index ce0846c..ec72321 100644
--- a/core/fpdfapi/page/cpdf_textstate.cpp
+++ b/core/fpdfapi/page/cpdf_textstate.cpp
@@ -96,7 +96,7 @@
 
   if (m_pDocument && m_pFont) {
     auto* pPageData = CPDF_DocPageData::FromDocument(m_pDocument.Get());
-    m_pFont = pPageData->GetFont(m_pFont->GetFontDict());
+    m_pFont = pPageData->GetFont(m_pFont->GetMutableFontDict());
   }
 }
 
diff --git a/core/fpdfdoc/cpdf_bafontmap.cpp b/core/fpdfdoc/cpdf_bafontmap.cpp
index 8180687..0b363d1 100644
--- a/core/fpdfdoc/cpdf_bafontmap.cpp
+++ b/core/fpdfdoc/cpdf_bafontmap.cpp
@@ -205,12 +205,12 @@
   CPDF_DictionaryLocker locker(pFonts);
   for (const auto& it : locker) {
     const ByteString& csKey = it.first;
-    CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
-    if (!ValidateDictType(pElement, "Font"))
+    RetainPtr<CPDF_Dictionary> pElement(ToDictionary(it.second->GetDirect()));
+    if (!ValidateDictType(pElement.Get(), "Font"))
       continue;
 
     auto* pData = CPDF_DocPageData::FromDocument(m_pDocument.Get());
-    RetainPtr<CPDF_Font> pFont = pData->GetFont(pElement);
+    RetainPtr<CPDF_Font> pFont = pData->GetFont(std::move(pElement));
     if (!pFont)
       continue;
 
@@ -256,7 +256,7 @@
   absl::optional<ByteString> font = appearance.GetFont(&font_size);
   *sAlias = font.value_or(ByteString());
 
-  CPDF_Dictionary* pFontDict = nullptr;
+  RetainPtr<CPDF_Dictionary> pFontDict;
   if (CPDF_Dictionary* pAPDict =
           m_pAnnotDict->GetDictFor(pdfium::annotation::kAP)) {
     if (CPDF_Dictionary* pNormalDict = pAPDict->GetDictFor("N")) {
@@ -313,7 +313,7 @@
                                               pStreamResFontList->GetObjNum());
   }
   if (!pStreamResFontList->KeyExist(sAlias)) {
-    CPDF_Dictionary* pFontDict = pFont->GetFontDict();
+    const CPDF_Dictionary* pFontDict = pFont->GetFontDict();
     RetainPtr<CPDF_Object> pObject =
         pFontDict->IsInline() ? pFontDict->Clone()
                               : pFontDict->MakeReference(m_pDocument.Get());
diff --git a/core/fpdfdoc/cpdf_formcontrol.cpp b/core/fpdfdoc/cpdf_formcontrol.cpp
index fa598d7..53f01d7 100644
--- a/core/fpdfdoc/cpdf_formcontrol.cpp
+++ b/core/fpdfdoc/cpdf_formcontrol.cpp
@@ -208,7 +208,8 @@
   if (CPDF_Dictionary* pDict = ToDictionary(pObj)) {
     CPDF_Dictionary* pFonts = pDict->GetDictFor("Font");
     if (ValidateFontResourceDict(pFonts)) {
-      CPDF_Dictionary* pElement = pFonts->GetDictFor(csFontNameTag.value());
+      RetainPtr<CPDF_Dictionary> pElement =
+          pFonts->GetMutableDictFor(csFontNameTag.value());
       if (pElement) {
         auto* pData = CPDF_DocPageData::FromDocument(m_pForm->GetDocument());
         RetainPtr<CPDF_Font> pFont = pData->GetFont(pElement);
@@ -231,7 +232,8 @@
   if (!ValidateFontResourceDict(pFonts))
     return nullptr;
 
-  CPDF_Dictionary* pElement = pFonts->GetDictFor(csFontNameTag.value());
+  RetainPtr<CPDF_Dictionary> pElement =
+      pFonts->GetMutableDictFor(csFontNameTag.value());
   if (!pElement)
     return nullptr;
 
diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp
index 35449e2..1395c4b 100644
--- a/core/fpdfdoc/cpdf_formfield.cpp
+++ b/core/fpdfdoc/cpdf_formfield.cpp
@@ -865,12 +865,13 @@
   if (!font_name.has_value())
     return;
 
-  CPDF_Dictionary* pFontDict = pFont->GetDictFor(font_name.value());
+  RetainPtr<CPDF_Dictionary> pFontDict =
+      pFont->GetMutableDictFor(font_name.value());
   if (!pFontDict)
     return;
 
   auto* pData = CPDF_DocPageData::FromDocument(m_pForm->GetDocument());
-  m_pFont = pData->GetFont(pFontDict);
+  m_pFont = pData->GetFont(std::move(pFontDict));
 }
 
 bool CPDF_FormField::NotifyBeforeSelectionChange(const WideString& value) {
diff --git a/core/fpdfdoc/cpdf_generateap.cpp b/core/fpdfdoc/cpdf_generateap.cpp
index 21a17d2..6ec9471 100644
--- a/core/fpdfdoc/cpdf_generateap.cpp
+++ b/core/fpdfdoc/cpdf_generateap.cpp
@@ -766,7 +766,7 @@
       GenerateResourceFontDict(pDoc, sFontName);
 
   auto* pData = CPDF_DocPageData::FromDocument(pDoc);
-  RetainPtr<CPDF_Font> pDefFont = pData->GetFont(pResourceFontDict.Get());
+  RetainPtr<CPDF_Font> pDefFont = pData->GetFont(pResourceFontDict);
   if (!pDefFont)
     return false;
 
@@ -950,7 +950,8 @@
   if (!ValidateFontResourceDict(pDRFontDict))
     return;
 
-  CPDF_Dictionary* pFontDict = pDRFontDict->GetDictFor(font_name);
+  RetainPtr<CPDF_Dictionary> pFontDict =
+      pDRFontDict->GetMutableDictFor(font_name);
   if (!pFontDict) {
     pFontDict = pDoc->NewIndirect<CPDF_Dictionary>();
     pFontDict->SetNewFor<CPDF_Name>("Type", "Font");
diff --git a/core/fpdfdoc/cpdf_interactiveform.cpp b/core/fpdfdoc/cpdf_interactiveform.cpp
index e111687..99f4a4c 100644
--- a/core/fpdfdoc/cpdf_interactiveform.cpp
+++ b/core/fpdfdoc/cpdf_interactiveform.cpp
@@ -216,11 +216,12 @@
   CPDF_DictionaryLocker locker(pFonts);
   for (const auto& it : locker) {
     const ByteString& csKey = it.first;
-    CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
-    if (!ValidateDictType(pElement, "Font"))
+    RetainPtr<CPDF_Dictionary> pElement(ToDictionary(it.second->GetDirect()));
+    if (!ValidateDictType(pElement.Get(), "Font"))
       continue;
 
-    pFont = CPDF_DocPageData::FromDocument(pDocument)->GetFont(pElement);
+    auto* pData = CPDF_DocPageData::FromDocument(pDocument);
+    pFont = pData->GetFont(std::move(pElement));
     if (!pFont)
       continue;
 
@@ -307,12 +308,12 @@
   CPDF_DictionaryLocker locker(pFonts);
   for (const auto& it : locker) {
     const ByteString& csKey = it.first;
-    CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
-    if (!ValidateDictType(pElement, "Font"))
+    RetainPtr<CPDF_Dictionary> pElement(ToDictionary(it.second->GetDirect()));
+    if (!ValidateDictType(pElement.Get(), "Font"))
       continue;
 
     auto* pData = CPDF_DocPageData::FromDocument(pDocument);
-    RetainPtr<CPDF_Font> pFind = pData->GetFont(pElement);
+    RetainPtr<CPDF_Font> pFind = pData->GetFont(std::move(pElement));
     if (!pFind)
       continue;
 
@@ -705,11 +706,12 @@
   if (!ValidateFontResourceDict(pFonts))
     return nullptr;
 
-  CPDF_Dictionary* pElement = pFonts->GetDictFor(csAlias);
-  if (!ValidateDictType(pElement, "Font"))
+  RetainPtr<CPDF_Dictionary> pElement = pFonts->GetMutableDictFor(csAlias);
+  if (!ValidateDictType(pElement.Get(), "Font"))
     return nullptr;
 
-  return CPDF_DocPageData::FromDocument(m_pDocument)->GetFont(pElement);
+  auto* pData = CPDF_DocPageData::FromDocument(m_pDocument);
+  return pData->GetFont(std::move(pElement));
 }
 
 CPDF_DefaultAppearance CPDF_InteractiveForm::GetDefaultAppearance() const {
diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp
index f8adf3c..f9c7696 100644
--- a/fpdfsdk/fpdf_edittext.cpp
+++ b/fpdfsdk/fpdf_edittext.cpp
@@ -291,7 +291,7 @@
                                     std::unique_ptr<CFX_Font> pFont,
                                     pdfium::span<const uint8_t> span,
                                     int font_type) {
-  CPDF_Dictionary* pFontDict = pDoc->NewIndirect<CPDF_Dictionary>();
+  RetainPtr<CPDF_Dictionary> pFontDict(pDoc->NewIndirect<CPDF_Dictionary>());
   pFontDict->SetNewFor<CPDF_Name>("Type", "Font");
   pFontDict->SetNewFor<CPDF_Name>(
       "Subtype", font_type == FPDF_FONT_TYPE1 ? "Type1" : "TrueType");
@@ -327,14 +327,14 @@
 
   pFontDict->SetNewFor<CPDF_Reference>("FontDescriptor", pDoc,
                                        pFontDesc->GetObjNum());
-  return CPDF_DocPageData::FromDocument(pDoc)->GetFont(pFontDict);
+  return CPDF_DocPageData::FromDocument(pDoc)->GetFont(std::move(pFontDict));
 }
 
 RetainPtr<CPDF_Font> LoadCompositeFont(CPDF_Document* pDoc,
                                        std::unique_ptr<CFX_Font> pFont,
                                        pdfium::span<const uint8_t> span,
                                        int font_type) {
-  CPDF_Dictionary* pFontDict = pDoc->NewIndirect<CPDF_Dictionary>();
+  RetainPtr<CPDF_Dictionary> pFontDict(pDoc->NewIndirect<CPDF_Dictionary>());
   pFontDict->SetNewFor<CPDF_Name>("Type", "Font");
   pFontDict->SetNewFor<CPDF_Name>("Subtype", "Type0");
   // TODO(npm): Get the correct encoding, if it's not identity.
@@ -668,8 +668,8 @@
     return nullptr;
 
   auto pTextObj = std::make_unique<CPDF_TextObject>();
-  pTextObj->m_TextState.SetFont(
-      CPDF_DocPageData::FromDocument(pDoc)->GetFont(pFont->GetFontDict()));
+  pTextObj->m_TextState.SetFont(CPDF_DocPageData::FromDocument(pDoc)->GetFont(
+      pFont->GetMutableFontDict()));
   pTextObj->m_TextState.SetFontSize(font_size);
   pTextObj->DefaultStates();
   return FPDFPageObjectFromCPDFPageObject(pTextObj.release());
diff --git a/xfa/fgas/font/cfgas_pdffontmgr.cpp b/xfa/fgas/font/cfgas_pdffontmgr.cpp
index 835401d..20e011a 100644
--- a/xfa/fgas/font/cfgas_pdffontmgr.cpp
+++ b/xfa/fgas/font/cfgas_pdffontmgr.cpp
@@ -61,8 +61,8 @@
                                bStrictMatch)) {
       continue;
     }
-    CPDF_Dictionary* pFontDict = ToDictionary(pObj->GetDirect());
-    if (!ValidateDictType(pFontDict, "Font"))
+    RetainPtr<CPDF_Dictionary> pFontDict(ToDictionary(pObj->GetDirect()));
+    if (!ValidateDictType(pFontDict.Get(), "Font"))
       return nullptr;
 
     RetainPtr<CPDF_Font> pPDFFont = pData->GetFont(pFontDict);