Fix rendering of text annotations with custom fonts.

When https://pdfium-review.googlesource.com/91290 fixed a bug with
annotations, it introduced another. Now CPDFSDK_AppStream creates a new
stream, but the new stream is missing resources. Fix this by copying the
resources from the original stream.

Add a pixel test to illustrate this issue.

Bug: chromium:1308739
Change-Id: I756d72e96e79025f78e52a6f2cf2b1142bce6ea1
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/91670
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_appstream.cpp b/fpdfsdk/cpdfsdk_appstream.cpp
index 2edbc21..e349205 100644
--- a/fpdfsdk/cpdfsdk_appstream.cpp
+++ b/fpdfsdk/cpdfsdk_appstream.cpp
@@ -1855,8 +1855,11 @@
   // If `pStream` is created by CreateModifiedAPStream(), then it is safe to
   // edit, as it is not shared.
   CPDF_Stream* pStream = pParentDict->GetStreamFor(key);
+  CPDF_Dictionary* pOrigStreamDict = nullptr;
   CPDF_Document* doc = widget_->GetPageView()->GetPDFDocument();
   if (!doc->IsModifiedAPStream(pStream)) {
+    if (pStream)
+      pOrigStreamDict = pStream->GetDict();
     pStream = doc->CreateModifiedAPStream();
     pParentDict->SetNewFor<CPDF_Reference>(key, doc, pStream->GetObjNum());
   }
@@ -1869,6 +1872,13 @@
     pStreamDict->SetNewFor<CPDF_Name>("Type", "XObject");
     pStreamDict->SetNewFor<CPDF_Name>("Subtype", "Form");
     pStreamDict->SetNewFor<CPDF_Number>("FormType", 1);
+
+    if (pOrigStreamDict) {
+      CPDF_Dictionary* pResources = pOrigStreamDict->GetDictFor("Resources");
+      if (pResources)
+        pStreamDict->SetFor("Resources", pResources->Clone());
+    }
+
     pStream->InitStream({}, std::move(pNewDict));
   }
   pStreamDict->SetMatrixFor("Matrix", widget_->GetMatrix());
diff --git a/testing/resources/pixel/text_form_custom_font.in b/testing/resources/pixel/text_form_custom_font.in
new file mode 100644
index 0000000..af404e4
--- /dev/null
+++ b/testing/resources/pixel/text_form_custom_font.in
@@ -0,0 +1,81 @@
+{{header}}
+{{object 1 0}} <<
+  /Type /Catalog
+  /Pages 2 0 R
+  /AcroForm <<
+    /DR <<
+      /Font <<
+        /TT0 5 0 R
+      >>
+    >>
+    /Fields [4 0 R]
+  >>
+>>
+endobj
+{{object 2 0}} <<
+  /Type /Pages
+  /Count 1
+  /Kids [3 0 R]
+>>
+endobj
+{{object 3 0}} <<
+  /Type /Page
+  /Parent 2 0 R
+  /Annots [4 0 R]
+  /MediaBox [0 0 200 200]
+>>
+endobj
+{{object 4 0}} <<
+  /Type /Annot
+  /Subtype /Widget
+  /FT /Tx
+  /DA (1 0 0 rg /TT0 16 Tf)
+  /Rect [50 100 150 130]
+  /T (Text Box)
+  /V (      )
+>>
+endobj
+{{object 5 0}} <<
+  /Type /Font
+  /Subtype /TrueType
+  /FirstChar 32
+  /BaseFont /AAAAAD+Test
+  /FontDescriptor 6 0 R
+  /ToUnicode 7 0 R
+  /LastChar 32
+  /Widths [1055]
+>>
+endobj
+{{object 6 0}} <<
+  /Type /FontDescriptor
+  /Descent -68
+  /MissingWidth 1000
+  /CapHeight 1149
+  /StemV 0
+  /FontFile2 8 0 R
+  /Flags 4
+  /FontBBox [0 -215 1000 932]
+  /FontName /AAAAAD+Test
+  /ItalicAngle 0
+  /Ascent 933
+>>
+endobj
+{{object 7 0}} <<
+  {{streamlen}}
+>>
+stream
+{{include ../bug_1388_cmap.fragment}}
+endstream
+endobj
+{{object 8 0}} <<
+  /Filter [/ASCIIHexDecode /FlateDecode]
+  {{streamlen}}
+>>
+stream
+{{include ../bug_1388_truetype_font.fragment}}
+endstream
+endobj
+{{xref}}
+{{trailer}}
+{{startxref}}
+%%EOF
diff --git a/testing/resources/pixel/text_form_custom_font_expected.pdf.0.png b/testing/resources/pixel/text_form_custom_font_expected.pdf.0.png
new file mode 100644
index 0000000..3894ad5
--- /dev/null
+++ b/testing/resources/pixel/text_form_custom_font_expected.pdf.0.png
Binary files differ
diff --git a/testing/resources/pixel/text_form_custom_font_expected_mac.pdf.0.png b/testing/resources/pixel/text_form_custom_font_expected_mac.pdf.0.png
new file mode 100644
index 0000000..01b3dbf
--- /dev/null
+++ b/testing/resources/pixel/text_form_custom_font_expected_mac.pdf.0.png
Binary files differ
diff --git a/testing/resources/pixel/text_form_custom_font_expected_skia.pdf.0.png b/testing/resources/pixel/text_form_custom_font_expected_skia.pdf.0.png
new file mode 100644
index 0000000..5f898f8
--- /dev/null
+++ b/testing/resources/pixel/text_form_custom_font_expected_skia.pdf.0.png
Binary files differ