Make CBA_FontMap::m_sAPType a const member.

This makes the font map less mutable, and requires introducing
a couple of new local font maps in one place, but the cost is
small because updating the existing one doesn't get to re-use
anything.

Change-Id: Ie30b7b0a6140227566a3809b7889511fea2316a1
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/79516
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfdoc/cba_fontmap.cpp b/core/fpdfdoc/cba_fontmap.cpp
index 8c27ecb..9e3ddb6 100644
--- a/core/fpdfdoc/cba_fontmap.cpp
+++ b/core/fpdfdoc/cba_fontmap.cpp
@@ -68,14 +68,14 @@
 
 CBA_FontMap::Data::~Data() = default;
 
-CBA_FontMap::CBA_FontMap(CPDF_Document* pDocument, CPDF_Dictionary* pAnnotDict)
-    : m_pDocument(pDocument), m_pAnnotDict(pAnnotDict) {
+CBA_FontMap::CBA_FontMap(CPDF_Document* pDocument,
+                         CPDF_Dictionary* pAnnotDict,
+                         const ByteString& sAPType)
+    : m_pDocument(pDocument), m_pAnnotDict(pAnnotDict), m_sAPType(sAPType) {
   Initialize();
 }
 
-CBA_FontMap::~CBA_FontMap() {
-  Clear();
-}
+CBA_FontMap::~CBA_FontMap() = default;
 
 RetainPtr<CPDF_Font> CBA_FontMap::GetPDFFont(int32_t nFontIndex) {
   if (pdfium::IndexInBounds(m_Data, nFontIndex))
@@ -151,19 +151,6 @@
   return FX_GetCharsetFromCodePage(FXSYS_GetACP());
 }
 
-void CBA_FontMap::Reset() {
-  Clear();
-  m_pDefaultFont = nullptr;
-  m_sDefaultFontName.clear();
-}
-
-void CBA_FontMap::SetAPType(const ByteString& sAPType) {
-  m_sAPType = sAPType;
-
-  Reset();
-  Initialize();
-}
-
 void CBA_FontMap::Initialize() {
   int32_t nCharset = FX_CHARSET_Default;
 
@@ -352,11 +339,6 @@
          CharCodeFromUnicode(nFontIndex, word) >= 0;
 }
 
-void CBA_FontMap::Clear() {
-  m_Data.clear();
-  m_NativeFont.clear();
-}
-
 int32_t CBA_FontMap::GetFontIndex(const ByteString& sFontName,
                                   int32_t nCharset,
                                   bool bFind) {
diff --git a/core/fpdfdoc/cba_fontmap.h b/core/fpdfdoc/cba_fontmap.h
index 7130064..af9255d 100644
--- a/core/fpdfdoc/cba_fontmap.h
+++ b/core/fpdfdoc/cba_fontmap.h
@@ -22,7 +22,9 @@
  public:
   static int32_t GetNativeCharset();
 
-  CBA_FontMap(CPDF_Document* pDocument, CPDF_Dictionary* pAnnotDict);
+  CBA_FontMap(CPDF_Document* pDocument,
+              CPDF_Dictionary* pAnnotDict,
+              const ByteString& sAPType);
   ~CBA_FontMap() override;
 
   // IPVT_FontMap
@@ -34,9 +36,6 @@
   int32_t CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) override;
   int32_t CharSetFromUnicode(uint16_t word, int32_t nOldCharset) override;
 
-  void Reset();
-  void SetAPType(const ByteString& sAPType);
-
  private:
   struct Data {
     Data();
@@ -64,7 +63,6 @@
 
   bool KnowWord(int32_t nFontIndex, uint16_t word);
 
-  void Clear();
   int32_t GetFontIndex(const ByteString& sFontName,
                        int32_t nCharset,
                        bool bFind);
@@ -90,7 +88,7 @@
   RetainPtr<CPDF_Dictionary> const m_pAnnotDict;
   RetainPtr<CPDF_Font> m_pDefaultFont;
   ByteString m_sDefaultFontName;
-  ByteString m_sAPType = "N";
+  const ByteString m_sAPType;
 };
 
 #endif  // CORE_FPDFDOC_CBA_FONTMAP_H_
diff --git a/fpdfsdk/cpdfsdk_appstream.cpp b/fpdfsdk/cpdfsdk_appstream.cpp
index 1284439..e596ff1 100644
--- a/fpdfsdk/cpdfsdk_appstream.cpp
+++ b/fpdfsdk/cpdfsdk_appstream.cpp
@@ -1255,33 +1255,38 @@
   SetDefaultIconName(pRolloverIcon, "ImgB");
   SetDefaultIconName(pDownIcon, "ImgC");
 
-  CBA_FontMap font_map(widget_->GetPDFPage()->GetDocument(),
-                       widget_->GetPDFAnnot()->GetAnnotDict());
-  font_map.SetAPType("N");
-
   CPDF_IconFit iconFit = pControl->GetIconFit();
-  ByteString csAP =
-      GetRectFillAppStream(rcWindow, crBackground) +
-      GetBorderAppStreamInternal(rcWindow, fBorderWidth, crBorder, crLeftTop,
-                                 crRightBottom, nBorderStyle, dsBorder) +
-      GetPushButtonAppStream(iconFit.GetFittingBounds() ? rcWindow : rcClient,
-                             &font_map, pNormalIcon, iconFit, csNormalCaption,
-                             crText, fFontSize, nLayout);
+  {
+    CBA_FontMap font_map(widget_->GetPDFPage()->GetDocument(),
+                         widget_->GetPDFAnnot()->GetAnnotDict(), "N");
+    ByteString csAP =
+        GetRectFillAppStream(rcWindow, crBackground) +
+        GetBorderAppStreamInternal(rcWindow, fBorderWidth, crBorder, crLeftTop,
+                                   crRightBottom, nBorderStyle, dsBorder) +
+        GetPushButtonAppStream(iconFit.GetFittingBounds() ? rcWindow : rcClient,
+                               &font_map, pNormalIcon, iconFit, csNormalCaption,
+                               crText, fFontSize, nLayout);
 
-  Write("N", csAP, ByteString());
-  if (pNormalIcon)
-    AddImage("N", pNormalIcon);
+    Write("N", csAP, ByteString());
+    if (pNormalIcon)
+      AddImage("N", pNormalIcon);
 
-  CPDF_FormControl::HighlightingMode eHLM = pControl->GetHighlightingMode();
-  if (eHLM == CPDF_FormControl::Push || eHLM == CPDF_FormControl::Toggle) {
+    CPDF_FormControl::HighlightingMode eHLM = pControl->GetHighlightingMode();
+    if (eHLM != CPDF_FormControl::Push && eHLM != CPDF_FormControl::Toggle) {
+      Remove("D");
+      Remove("R");
+      return;
+    }
+
     if (csRolloverCaption.IsEmpty() && !pRolloverIcon) {
       csRolloverCaption = csNormalCaption;
       pRolloverIcon = pNormalIcon;
     }
-
-    font_map.SetAPType("R");
-
-    csAP =
+  }
+  {
+    CBA_FontMap font_map(widget_->GetPDFPage()->GetDocument(),
+                         widget_->GetPDFAnnot()->GetAnnotDict(), "R");
+    ByteString csAP =
         GetRectFillAppStream(rcWindow, crBackground) +
         GetBorderAppStreamInternal(rcWindow, fBorderWidth, crBorder, crLeftTop,
                                    crRightBottom, nBorderStyle, dsBorder) +
@@ -1313,10 +1318,11 @@
       default:
         break;
     }
-
-    font_map.SetAPType("D");
-
-    csAP =
+  }
+  {
+    CBA_FontMap font_map(widget_->GetPDFPage()->GetDocument(),
+                         widget_->GetPDFAnnot()->GetAnnotDict(), "D");
+    ByteString csAP =
         GetRectFillAppStream(rcWindow, crBackground - 0.25f) +
         GetBorderAppStreamInternal(rcWindow, fBorderWidth, crBorder, crLeftTop,
                                    crRightBottom, nBorderStyle, dsBorder) +
@@ -1327,9 +1333,6 @@
     Write("D", csAP, ByteString());
     if (pDownIcon)
       AddImage("D", pDownIcon);
-  } else {
-    Remove("D");
-    Remove("R");
   }
 }
 
@@ -1569,7 +1572,7 @@
 
   // Font map must outlive |pEdit|.
   CBA_FontMap font_map(widget_->GetPDFPage()->GetDocument(),
-                       widget_->GetPDFAnnot()->GetAnnotDict());
+                       widget_->GetPDFAnnot()->GetAnnotDict(), "N");
 
   auto pEdit = std::make_unique<CPWL_EditImpl>();
   pEdit->EnableRefresh(false);
@@ -1635,7 +1638,7 @@
 
   // Font map must outlive |pEdit|.
   CBA_FontMap font_map(widget_->GetPDFPage()->GetDocument(),
-                       widget_->GetPDFAnnot()->GetAnnotDict());
+                       widget_->GetPDFAnnot()->GetAnnotDict(), "N");
 
   auto pEdit = std::make_unique<CPWL_EditImpl>();
   pEdit->EnableRefresh(false);
@@ -1719,7 +1722,7 @@
 
   // Font map must outlive |pEdit|.
   CBA_FontMap font_map(widget_->GetPDFPage()->GetDocument(),
-                       widget_->GetPDFAnnot()->GetAnnotDict());
+                       widget_->GetPDFAnnot()->GetAnnotDict(), "N");
 
   auto pEdit = std::make_unique<CPWL_EditImpl>();
   pEdit->EnableRefresh(false);
diff --git a/fpdfsdk/formfiller/cffl_textobject.cpp b/fpdfsdk/formfiller/cffl_textobject.cpp
index 161b1da..a055670 100644
--- a/fpdfsdk/formfiller/cffl_textobject.cpp
+++ b/fpdfsdk/formfiller/cffl_textobject.cpp
@@ -35,9 +35,9 @@
 
 CBA_FontMap* CFFL_TextObject::MaybeCreateFontMap() {
   if (!m_pFontMap) {
-    m_pFontMap =
-        std::make_unique<CBA_FontMap>(m_pWidget->GetPDFPage()->GetDocument(),
-                                      m_pWidget->GetPDFAnnot()->GetAnnotDict());
+    m_pFontMap = std::make_unique<CBA_FontMap>(
+        m_pWidget->GetPDFPage()->GetDocument(),
+        m_pWidget->GetPDFAnnot()->GetAnnotDict(), "N");
   }
   return m_pFontMap.get();
 }