Check font type before writing operators in ProcessText()

CPDF_PageContentGenerator::ProcessText() currently calls
ProcessGraphics() and writes "BT" / "Tm" operators before checking
whether the font type is supported. When encountering an unsupported
font (such as a Type 3 font), the early return leaves unclosed "q" and
"BT" operators in the stream and can leave behind an unused ExtGState
resource.

Move the font lookup and font-type check ahead of ProcessGraphics() so
returning early writes nothing to the buffer and creates no resources.
Update CPDFPageContentGeneratorTest.ProcessTextWithUnsupportedType3Font
expectations accordingly.

This CL is AI generated.

Change-Id: If251620e85c90cfdcbefcda6a32c37cfc38b9de5
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/164971
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Andy Phan <andyphan@chromium.org>
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index 207468f..fb97506 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -943,14 +943,6 @@
 // TJ sets the actual text, <####...> is used when specifying charcodes.
 void CPDF_PageContentGenerator::ProcessText(fxcrt::ostringstream* buf,
                                             CPDF_TextObject* pTextObj) {
-  ProcessGraphics(buf, pTextObj);
-  *buf << "BT ";
-
-  const CFX_Matrix& matrix = pTextObj->GetTextMatrix();
-  if (!matrix.IsIdentity()) {
-    WriteMatrix(*buf, matrix) << " Tm ";
-  }
-
   RetainPtr<CPDF_Font> font(pTextObj->GetFont());
   if (!font) {
     font = CPDF_Font::GetStockFont(document_, "Helvetica");
@@ -971,6 +963,14 @@
   }
   data.baseFont = font->GetBaseFontName();
 
+  ProcessGraphics(buf, pTextObj);
+  *buf << "BT ";
+
+  const CFX_Matrix& matrix = pTextObj->GetTextMatrix();
+  if (!matrix.IsIdentity()) {
+    WriteMatrix(*buf, matrix) << " Tm ";
+  }
+
   ByteString dict_name;
   std::optional<ByteString> maybe_name = obj_holder_->FontsMapSearch(data);
   if (maybe_name.has_value()) {
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
index 722d662..3f7a730 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
@@ -485,10 +485,8 @@
   fxcrt::ostringstream buf;
   TestProcessText(&generator, &buf, text_obj.get());
   // CPDF_PageContentGenerator does not support Type 3 fonts.
-  // TODO(thestig): Should be empty string.
-  EXPECT_EQ("q /FXE1 gs BT 1 0 0 1 100 100 Tm ", ByteString(buf));
-  // TODO(thestig): Should be false.
-  EXPECT_TRUE(test_page->GetResources());
+  EXPECT_EQ("", ByteString(buf));
+  EXPECT_FALSE(test_page->GetResources());
 }
 
 TEST_F(CPDFPageContentGeneratorTest, ProcessEmptyForm) {