Fix wrong matrix outputs in CPDF_PageContentGenerator for text objects

When CPDF_PageContentGenerator generates content stream data for a text
page object, it currently does not take the object's container's current
transformation matrix into account. Fix this and update the test that
now renders correctly.

Bug: pdfium:1893,pdfium:2132
Change-Id: I52a6538fbf852bc9ac7b9ee3fb5f8d378902b08d
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/117054
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index c2f83dc..428dc33 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -682,9 +682,11 @@
   ProcessGraphics(buf, pTextObj);
   *buf << "BT ";
 
-  // TODO(crbug.com/pdfium/2132): Does this need to take the current
-  // transformation matrix in `m_pObjHolder` into account?
-  const CFX_Matrix& matrix = pTextObj->GetTextMatrix();
+  CFX_Matrix matrix = pTextObj->GetTextMatrix();
+  const CFX_Matrix& ctm = m_pObjHolder->GetLastCTM();
+  if (!ctm.IsIdentity()) {
+    matrix.Concat(ctm.GetInverse());
+  }
   if (!matrix.IsIdentity()) {
     WriteMatrix(*buf, matrix) << " Tm ";
   }
diff --git a/fpdfsdk/fpdf_edit_embeddertest.cpp b/fpdfsdk/fpdf_edit_embeddertest.cpp
index 22ce975..4583735 100644
--- a/fpdfsdk/fpdf_edit_embeddertest.cpp
+++ b/fpdfsdk/fpdf_edit_embeddertest.cpp
@@ -1003,7 +1003,7 @@
   CloseSavedDocument();
 }
 
-TEST_F(FPDFEditEmbedderTest, BUG_1893) {
+TEST_F(FPDFEditEmbedderTest, Bug1893) {
   ASSERT_TRUE(OpenDocument("bug_1893.pdf"));
   FPDF_PAGE page = LoadPage(0);
   {
@@ -1061,31 +1061,10 @@
   UnloadPage(page);
 
   {
-    // TODO(crbug.com/pdfium/1893): The saved result should match
-    // `removed_checksum`. But in the actual saved result, the remaining text
-    // objects were upside down. Remove `wrong_checksum` after fixing this
-    // issue.
-    const char* wrong_checksum = []() {
-      if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
-#if BUILDFLAG(IS_WIN)
-        return "441cada6218d4fd79dbe0ba95093524e";
-#elif BUILDFLAG(IS_APPLE)
-        return "627290533339e0ae493dc9385fac53e2";
-#else
-        return "57da26dcb24503403cadb27ed8bb46c6";
-#endif
-      }
-#if BUILDFLAG(IS_APPLE)
-      return "c3b6a8ecd863914044f5f79137c606b5";
-#else
-      return "cb19480a846e4efd36418cbd7412118e";
-#endif
-    }();
-
     ASSERT_TRUE(OpenSavedDocument());
     FPDF_PAGE saved_page = LoadSavedPage(0);
     ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(saved_page, FPDF_ANNOT);
-    CompareBitmap(bitmap.get(), 200, 300, wrong_checksum);
+    CompareBitmap(bitmap.get(), 200, 300, removed_checksum);
     CloseSavedPage(saved_page);
     CloseSavedDocument();
   }