Simplify CPDF_InteractiveForm::AddNativeInteractiveFormFont().

Avoid having a parameter that passes a pointer by reference, which can
be confusing. Do the same for internal methods like AddFont() and
InitDict(). Then it becomes obvious that InitDict() is always dealing
with a newly-created dictionary, and some of the checks it does are
always true or always false. Remove those checks to simplify the code.

Change-Id: Idf81695e97b5b0102a07d1dfb9934a98d8db4b01
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/91530
Reviewed-by: Nigi <nigi@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfdoc/cpdf_interactiveform.cpp b/core/fpdfdoc/cpdf_interactiveform.cpp
index 355b056..56cd622 100644
--- a/core/fpdfdoc/cpdf_interactiveform.cpp
+++ b/core/fpdfdoc/cpdf_interactiveform.cpp
@@ -234,7 +234,7 @@
   return false;
 }
 
-void AddFont(CPDF_Dictionary*& pFormDict,
+void AddFont(CPDF_Dictionary* pFormDict,
              CPDF_Document* pDocument,
              const RetainPtr<CPDF_Font>& pFont,
              ByteString* csNameTag) {
@@ -263,42 +263,33 @@
   return FX_GetCharsetFromCodePage(FX_GetACP());
 }
 
-void InitDict(CPDF_Dictionary*& pFormDict, CPDF_Document* pDocument) {
-  DCHECK(pDocument);
+CPDF_Dictionary* InitDict(CPDF_Document* pDocument) {
+  CPDF_Dictionary* pFormDict = pDocument->NewIndirect<CPDF_Dictionary>();
+  pDocument->GetRoot()->SetNewFor<CPDF_Reference>("AcroForm", pDocument,
+                                                  pFormDict->GetObjNum());
 
-  if (!pFormDict) {
-    pFormDict = pDocument->NewIndirect<CPDF_Dictionary>();
-    pDocument->GetRoot()->SetNewFor<CPDF_Reference>("AcroForm", pDocument,
-                                                    pFormDict->GetObjNum());
-  }
+  ByteString csBaseName;
+  FX_Charset charSet = GetNativeCharSet();
+  RetainPtr<CPDF_Font> pFont = AddStandardFont(pDocument);
+  if (pFont)
+    AddFont(pFormDict, pDocument, pFont, &csBaseName);
 
-  ByteString csDA;
-  if (!pFormDict->KeyExist("DR")) {
-    ByteString csBaseName;
-    FX_Charset charSet = GetNativeCharSet();
-    RetainPtr<CPDF_Font> pFont = AddStandardFont(pDocument);
-    if (pFont)
-      AddFont(pFormDict, pDocument, pFont, &csBaseName);
-
-    if (charSet != FX_Charset::kANSI) {
-      ByteString csFontName = GetNativeFontName(charSet, nullptr);
-      if (!pFont || csFontName != CFX_Font::kDefaultAnsiFontName) {
-        pFont = AddNativeFont(charSet, pDocument);
-        if (pFont) {
-          csBaseName.clear();
-          AddFont(pFormDict, pDocument, pFont, &csBaseName);
-        }
+  if (charSet != FX_Charset::kANSI) {
+    ByteString csFontName = GetNativeFontName(charSet, nullptr);
+    if (!pFont || csFontName != CFX_Font::kDefaultAnsiFontName) {
+      pFont = AddNativeFont(charSet, pDocument);
+      if (pFont) {
+        csBaseName.clear();
+        AddFont(pFormDict, pDocument, pFont, &csBaseName);
       }
     }
-    if (pFont)
-      csDA = "/" + PDF_NameEncode(csBaseName) + " 0 Tf";
   }
-  if (!csDA.IsEmpty())
-    csDA += " ";
-
+  ByteString csDA;
+  if (pFont)
+    csDA = "/" + PDF_NameEncode(csBaseName) + " 0 Tf ";
   csDA += "0 g";
-  if (!pFormDict->KeyExist("DA"))
-    pFormDict->SetNewFor<CPDF_String>("DA", csDA, false);
+  pFormDict->SetNewFor<CPDF_String>("DA", csDA, /*bHex=*/false);
+  return pFormDict;
 }
 
 RetainPtr<CPDF_Font> GetNativeFont(CPDF_Dictionary* pFormDict,
@@ -568,15 +559,14 @@
 
 // static
 RetainPtr<CPDF_Font> CPDF_InteractiveForm::AddNativeInteractiveFormFont(
-    CPDF_Dictionary*& pFormDict,
     CPDF_Document* pDocument,
     ByteString* csNameTag) {
   DCHECK(pDocument);
   DCHECK(csNameTag);
 
+  CPDF_Dictionary* pFormDict = pDocument->GetRoot()->GetDictFor("AcroForm");
   if (!pFormDict)
-    InitDict(pFormDict, pDocument);
-  DCHECK(pFormDict);
+    pFormDict = InitDict(pDocument);
 
   FX_Charset charSet = GetNativeCharSet();
   ByteString csTemp;
diff --git a/core/fpdfdoc/cpdf_interactiveform.h b/core/fpdfdoc/cpdf_interactiveform.h
index e3b1be3..cca8770 100644
--- a/core/fpdfdoc/cpdf_interactiveform.h
+++ b/core/fpdfdoc/cpdf_interactiveform.h
@@ -53,7 +53,6 @@
   static bool IsUpdateAPEnabled();
   static void SetUpdateAP(bool bUpdateAP);
   static RetainPtr<CPDF_Font> AddNativeInteractiveFormFont(
-      CPDF_Dictionary*& pFormDict,
       CPDF_Document* pDocument,
       ByteString* csNameTag);
 
diff --git a/core/fpdfdoc/cpvt_fontmap.cpp b/core/fpdfdoc/cpvt_fontmap.cpp
index f337fa3..74240a0 100644
--- a/core/fpdfdoc/cpvt_fontmap.cpp
+++ b/core/fpdfdoc/cpvt_fontmap.cpp
@@ -33,10 +33,9 @@
   if (!m_pDocument || !m_pResDict)
     return;
 
-  CPDF_Dictionary* pFormDict = m_pDocument->GetRoot()->GetDictFor("AcroForm");
   RetainPtr<CPDF_Font> pPDFFont =
-      CPDF_InteractiveForm::AddNativeInteractiveFormFont(
-          pFormDict, m_pDocument.Get(), &m_sSysFontAlias);
+      CPDF_InteractiveForm::AddNativeInteractiveFormFont(m_pDocument.Get(),
+                                                         &m_sSysFontAlias);
   if (!pPDFFont)
     return;