Add FPDFText_GetMatrix() API

This is similar to FPDFPath_GetMatrix(), but works on text, not path
objects.

Change-Id: If268362b7fa4398124b953e0e2225074523f5f65
Reviewed-on: https://pdfium-review.googlesource.com/35434
Reviewed-by: dsinclair <dsinclair@chromium.org>
Reviewed-by: Nicolás Peña Moreno <npm@chromium.org>
Commit-Queue: Nicolás Peña Moreno <npm@chromium.org>
diff --git a/fpdfsdk/fpdf_edit_embeddertest.cpp b/fpdfsdk/fpdf_edit_embeddertest.cpp
index 07879c5..c78700e 100644
--- a/fpdfsdk/fpdf_edit_embeddertest.cpp
+++ b/fpdfsdk/fpdf_edit_embeddertest.cpp
@@ -1122,6 +1122,23 @@
     CompareBitmap(page_bitmap.get(), 612, 792, md5_3);
   }
 
+  double matrix_a = 0;
+  double matrix_b = 0;
+  double matrix_c = 0;
+  double matrix_d = 0;
+  double matrix_e = 0;
+  double matrix_f = 0;
+  EXPECT_FALSE(FPDFText_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
+                                  &matrix_d, &matrix_e, &matrix_f));
+  EXPECT_TRUE(FPDFText_GetMatrix(text_object3, &matrix_a, &matrix_b, &matrix_c,
+                                 &matrix_d, &matrix_e, &matrix_f));
+  EXPECT_EQ(1., matrix_a);
+  EXPECT_EQ(1.5, matrix_b);
+  EXPECT_EQ(2., matrix_c);
+  EXPECT_EQ(0.5, matrix_d);
+  EXPECT_EQ(200., matrix_e);
+  EXPECT_EQ(200., matrix_f);
+
   // TODO(npm): Why are there issues with text rotated by 90 degrees?
   // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
   FPDF_ClosePage(page);
diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp
index 8186d8d..a927e16 100644
--- a/fpdfsdk/fpdf_edittext.cpp
+++ b/fpdfsdk/fpdf_edittext.cpp
@@ -394,6 +394,11 @@
   return pDoc->LoadFont(fontDict);
 }
 
+CPDF_TextObject* CPDFTextObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) {
+  auto* obj = CPDFPageObjectFromFPDFPageObject(page_object);
+  return obj ? obj->AsText() : nullptr;
+}
+
 }  // namespace
 
 FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV
@@ -481,6 +486,31 @@
   return FPDFPageObj_SetFillColor(text_object, R, G, B, A);
 }
 
+FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_GetMatrix(FPDF_PAGEOBJECT text,
+                                                       double* a,
+                                                       double* b,
+                                                       double* c,
+                                                       double* d,
+                                                       double* e,
+                                                       double* f) {
+  if (!text || !a || !b || !c || !d || !e || !f)
+    return false;
+
+  CPDF_TextObject* pTextObj = CPDFTextObjectFromFPDFPageObject(text);
+  if (!pTextObj)
+    return false;
+
+  CFX_Matrix text_matrix = pTextObj->GetTextMatrix();
+  *a = text_matrix.a;
+  *b = text_matrix.b;
+  *c = text_matrix.c;
+  *d = text_matrix.d;
+  *e = text_matrix.e;
+  *f = text_matrix.f;
+
+  return true;
+}
+
 FPDF_EXPORT void FPDF_CALLCONV FPDFFont_Close(FPDF_FONT font) {
   CPDF_Font* pFont = CPDFFontFromFPDFFont(font);
   if (!pFont)
diff --git a/fpdfsdk/fpdf_view_c_api_test.c b/fpdfsdk/fpdf_view_c_api_test.c
index 543a7d1..90eca33 100644
--- a/fpdfsdk/fpdf_view_c_api_test.c
+++ b/fpdfsdk/fpdf_view_c_api_test.c
@@ -195,6 +195,7 @@
     CHK(FPDFPath_SetMatrix);
     CHK(FPDFPath_SetStrokeColor);
     CHK(FPDFPath_SetStrokeWidth);
+    CHK(FPDFText_GetMatrix);
     CHK(FPDFText_LoadFont);
     CHK(FPDFText_LoadStandardFont);
     CHK(FPDFText_SetFillColor);
diff --git a/public/fpdf_edit.h b/public/fpdf_edit.h
index d3d7c4b..6df5e32 100644
--- a/public/fpdf_edit.h
+++ b/public/fpdf_edit.h
@@ -1065,6 +1065,31 @@
                       unsigned int B,
                       unsigned int A);
 
+// Experimental API.
+// Get the transform matrix of a text object.
+//
+//   text - handle to a text.
+//   a    - matrix value.
+//   b    - matrix value.
+//   c    - matrix value.
+//   d    - matrix value.
+//   e    - matrix value.
+//   f    - matrix value.
+//
+// The matrix is composed as:
+//   |a c e|
+//   |b d f|
+// and used to scale, rotate, shear and translate the text.
+//
+// Returns TRUE on success.
+FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_GetMatrix(FPDF_PAGEOBJECT text,
+                                                       double* a,
+                                                       double* b,
+                                                       double* c,
+                                                       double* d,
+                                                       double* e,
+                                                       double* f);
+
 // Close a loaded PDF font.
 //
 // font   - Handle to the loaded font.