Add text object version of a more complex test for a PDF saving bug

Add a FPDFEditEmbedderTest case to demonstrate a PDF saving bug, where a
no-op FPDFPageObj_SetMatrix() call causes the saved PDF to have
incorrect transformation matrices. This test case has text objects
inside a form. The basic case where text objects are not inside a form
is already covered by the BUG_1893 test case.

Also add a missing sanity check in the prior image object test.

Bug: pdfium:2132
Change-Id: Ib6bb459f4d1a707f53b02c6705ca9422e1eb273a
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/117051
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
diff --git a/fpdfsdk/fpdf_edit_embeddertest.cpp b/fpdfsdk/fpdf_edit_embeddertest.cpp
index a8e319a..39c205c 100644
--- a/fpdfsdk/fpdf_edit_embeddertest.cpp
+++ b/fpdfsdk/fpdf_edit_embeddertest.cpp
@@ -5203,6 +5203,90 @@
   UnloadPage(page);
 }
 
+TEST_F(FPDFEditEmbedderTest, GetAndSetMatrixForFormWithText) {
+  constexpr int kExpectedWidth = 200;
+  constexpr int kExpectedHeight = 200;
+
+  OpenDocument("form_object_with_text.pdf");
+  FPDF_PAGE page = LoadPage(0);
+  ASSERT_TRUE(page);
+
+  {
+    ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
+    CompareBitmap(bitmap.get(), kExpectedWidth, kExpectedHeight,
+                  HelloWorldChecksum());
+  }
+
+  FPDF_PAGEOBJECT form = FPDFPage_GetObject(page, 0);
+  ASSERT_TRUE(form);
+  ASSERT_EQ(FPDF_PAGEOBJ_FORM, FPDFPageObj_GetType(form));
+
+  FS_MATRIX matrix;
+  ASSERT_TRUE(FPDFPageObj_GetMatrix(form, &matrix));
+  EXPECT_FLOAT_EQ(2.0f, matrix.a);
+  EXPECT_FLOAT_EQ(0.0f, matrix.b);
+  EXPECT_FLOAT_EQ(0.0f, matrix.c);
+  EXPECT_FLOAT_EQ(-1.0f, matrix.d);
+  EXPECT_FLOAT_EQ(0.0f, matrix.e);
+  EXPECT_FLOAT_EQ(200.0f, matrix.f);
+
+  ASSERT_TRUE(FPDFPageObj_SetMatrix(form, &matrix));
+  {
+    ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
+    CompareBitmap(bitmap.get(), kExpectedWidth, kExpectedHeight,
+                  HelloWorldChecksum());
+  }
+
+  FPDF_PAGEOBJECT text = FPDFFormObj_GetObject(form, 0);
+  ASSERT_TRUE(text);
+  ASSERT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text));
+
+  ASSERT_TRUE(FPDFPageObj_GetMatrix(text, &matrix));
+  EXPECT_FLOAT_EQ(0.5f, matrix.a);
+  EXPECT_FLOAT_EQ(0.0f, matrix.b);
+  EXPECT_FLOAT_EQ(0.0f, matrix.c);
+  EXPECT_FLOAT_EQ(-1.0f, matrix.d);
+  EXPECT_FLOAT_EQ(10.0f, matrix.e);
+  EXPECT_FLOAT_EQ(150.0f, matrix.f);
+
+  ASSERT_TRUE(FPDFPageObj_SetMatrix(text, &matrix));
+  {
+    ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
+    CompareBitmap(bitmap.get(), kExpectedWidth, kExpectedHeight,
+                  HelloWorldChecksum());
+  }
+
+  ASSERT_TRUE(FPDFPage_GenerateContent(page));
+  ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
+
+  {
+    ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
+    CompareBitmap(bitmap.get(), kExpectedWidth, kExpectedHeight,
+                  HelloWorldChecksum());
+  }
+
+  UnloadPage(page);
+
+  // TODO(crbug.com/pdfium/2132): This should use HelloWorldChecksum().
+  const char* const kWrongChecksum = []() {
+    if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
+#if BUILDFLAG(IS_WIN)
+      return "0b5370959c7731c846a35066f0463aee";
+#elif BUILDFLAG(IS_MAC)
+      return "8715c645f7dccfb23e25a32da32ef6af";
+#else
+      return "b4a1a4a73668c5dd4b769db86334943b";
+#endif
+    }
+#if BUILDFLAG(IS_MAC)
+    return "6cb1bfe4f8bef5547eab41cdfad79d59";
+#else
+    return "0e6dd5f1332bb4c4b0cd3aa6a0b89943";
+#endif
+  }();
+  VerifySavedDocument(kExpectedWidth, kExpectedHeight, kWrongChecksum);
+}
+
 class FPDFEditMoveEmbedderTest : public EmbedderTest {
  protected:
   std::vector<std::string> HashesForDocument(int page_count) {
diff --git a/fpdfsdk/fpdf_editimg_embeddertest.cpp b/fpdfsdk/fpdf_editimg_embeddertest.cpp
index d16cef3..c7716ee 100644
--- a/fpdfsdk/fpdf_editimg_embeddertest.cpp
+++ b/fpdfsdk/fpdf_editimg_embeddertest.cpp
@@ -191,6 +191,7 @@
 
   FPDF_PAGEOBJECT image = FPDFPage_GetObject(page, 0);
   ASSERT_TRUE(image);
+  ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image));
 
   FS_MATRIX matrix;
   ASSERT_TRUE(FPDFPageObj_GetMatrix(image, &matrix));
diff --git a/testing/resources/form_object_with_text.in b/testing/resources/form_object_with_text.in
new file mode 100644
index 0000000..9201686
--- /dev/null
+++ b/testing/resources/form_object_with_text.in
@@ -0,0 +1,75 @@
+{{header}}
+{{object 1 0}} <<
+  /Type /Catalog
+  /Pages 2 0 R
+>>
+endobj
+{{object 2 0}} <<
+  /Type /Pages
+  /MediaBox [0 0 200 200]
+  /Count 1
+  /Kids [3 0 R]
+>>
+endobj
+{{object 3 0}} <<
+  /Type /Page
+  /Parent 2 0 R
+  /Contents 4 0 R
+  /Resources <<
+    /XObject <<
+      /F1 5 0 R
+    >>
+  >>
+>>
+endobj
+{{object 4 0}} <<
+  {{streamlen}}
+>>
+stream
+2 0 0 -1 0 200 cm
+q
+/F1 Do
+Q
+endstream
+endobj
+{{object 5 0}} <<
+  /Type /XObject
+  /Subtype /Form
+  /BBox [0 0 200 200]
+  /Resources <<
+    /Font <<
+      /F1 6 0 R
+      /F2 7 0 R
+    >>
+  >>
+>>
+stream
+0.5 0 0 -1 0 200 cm
+BT
+20 50 Td
+/F1 12 Tf
+(Hello, world!) Tj
+0 50 Td
+/F2 16 Tf
+(Goodbye, world!) Tj
+ET
+endstream
+endobj
+endstream
+endobj
+{{object 6 0}} <<
+  /Type /Font
+  /Subtype /Type1
+  /BaseFont /Times-Roman
+>>
+endobj
+{{object 7 0}} <<
+  /Type /Font
+  /Subtype /Type1
+  /BaseFont /Helvetica
+>>
+endobj
+{{xref}}
+{{trailer}}
+{{startxref}}
+%%EOF
diff --git a/testing/resources/form_object_with_text.pdf b/testing/resources/form_object_with_text.pdf
new file mode 100644
index 0000000..0759112
--- /dev/null
+++ b/testing/resources/form_object_with_text.pdf
@@ -0,0 +1,89 @@
+%PDF-1.7
+% ò¤ô
+1 0 obj <<
+  /Type /Catalog
+  /Pages 2 0 R
+>>
+endobj
+2 0 obj <<
+  /Type /Pages
+  /MediaBox [0 0 200 200]
+  /Count 1
+  /Kids [3 0 R]
+>>
+endobj
+3 0 obj <<
+  /Type /Page
+  /Parent 2 0 R
+  /Contents 4 0 R
+  /Resources <<
+    /XObject <<
+      /F1 5 0 R
+    >>
+  >>
+>>
+endobj
+4 0 obj <<
+  /Length 29
+>>
+stream
+2 0 0 -1 0 200 cm
+q
+/F1 Do
+Q
+endstream
+endobj
+5 0 obj <<
+  /Type /XObject
+  /Subtype /Form
+  /BBox [0 0 200 200]
+  /Resources <<
+    /Font <<
+      /F1 6 0 R
+      /F2 7 0 R
+    >>
+  >>
+>>
+stream
+0.5 0 0 -1 0 200 cm
+BT
+20 50 Td
+/F1 12 Tf
+(Hello, world!) Tj
+0 50 Td
+/F2 16 Tf
+(Goodbye, world!) Tj
+ET
+endstream
+endobj
+endstream
+endobj
+6 0 obj <<
+  /Type /Font
+  /Subtype /Type1
+  /BaseFont /Times-Roman
+>>
+endobj
+7 0 obj <<
+  /Type /Font
+  /Subtype /Type1
+  /BaseFont /Helvetica
+>>
+endobj
+xref
+0 8
+0000000000 65535 f 
+0000000015 00000 n 
+0000000068 00000 n 
+0000000157 00000 n 
+0000000286 00000 n 
+0000000366 00000 n 
+0000000653 00000 n 
+0000000731 00000 n 
+trailer <<
+  /Root 1 0 R
+  /Size 8
+>>
+startxref
+807
+%%EOF