Write out the font encoding in CPDF_PageContentGenerator::ProcessText().

Test this by updating FPDFEditEmbedderTest.AddStandardFontText to write
changes out to a new PDF, and make sure the newly created PDF renders
just like the original.

BUG=pdfium:982

Change-Id: I8a42eb004872a899beb04b6aec7168a471d98d7a
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/51891
Reviewed-by: Nicolás Peña Moreno <npm@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index 07347a6..c5f9568 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -14,7 +14,8 @@
 
 #include "core/fpdfapi/edit/cpdf_pagecontentmanager.h"
 #include "core/fpdfapi/edit/cpdf_stringarchivestream.h"
-#include "core/fpdfapi/font/cpdf_font.h"
+#include "core/fpdfapi/font/cpdf_truetypefont.h"
+#include "core/fpdfapi/font/cpdf_type1font.h"
 #include "core/fpdfapi/page/cpdf_contentmarks.h"
 #include "core/fpdfapi/page/cpdf_docpagedata.h"
 #include "core/fpdfapi/page/cpdf_image.h"
@@ -508,17 +509,22 @@
   CPDF_Font* pFont = pTextObj->GetFont();
   if (!pFont)
     pFont = CPDF_Font::GetStockFont(m_pDocument.Get(), "Helvetica");
-  FontData fontD;
-  if (pFont->IsType1Font())
-    fontD.type = "Type1";
-  else if (pFont->IsTrueTypeFont())
-    fontD.type = "TrueType";
-  else if (pFont->IsCIDFont())
-    fontD.type = "Type0";
-  else
+
+  FontData data;
+  CPDF_FontEncoding* pEncoding = nullptr;
+  if (pFont->IsType1Font()) {
+    data.type = "Type1";
+    pEncoding = pFont->AsType1Font()->GetEncoding();
+  } else if (pFont->IsTrueTypeFont()) {
+    data.type = "TrueType";
+    pEncoding = pFont->AsTrueTypeFont()->GetEncoding();
+  } else if (pFont->IsCIDFont()) {
+    data.type = "Type0";
+  } else {
     return;
-  fontD.baseFont = pFont->GetBaseFont();
-  auto it = m_pObjHolder->m_FontsMap.find(fontD);
+  }
+  data.baseFont = pFont->GetBaseFont();
+  auto it = m_pObjHolder->m_FontsMap.find(data);
   ByteString dictName;
   if (it != m_pObjHolder->m_FontsMap.end()) {
     dictName = it->second;
@@ -526,14 +532,18 @@
     CPDF_Object* pIndirectFont = pFont->GetFontDict();
     if (pIndirectFont->IsInline()) {
       // In this case we assume it must be a standard font
-      auto fontDict = pdfium::MakeUnique<CPDF_Dictionary>();
-      fontDict->SetNewFor<CPDF_Name>("Type", "Font");
-      fontDict->SetNewFor<CPDF_Name>("Subtype", fontD.type);
-      fontDict->SetNewFor<CPDF_Name>("BaseFont", fontD.baseFont);
-      pIndirectFont = m_pDocument->AddIndirectObject(std::move(fontDict));
+      auto pFontDict = pdfium::MakeUnique<CPDF_Dictionary>();
+      pFontDict->SetNewFor<CPDF_Name>("Type", "Font");
+      pFontDict->SetNewFor<CPDF_Name>("Subtype", data.type);
+      pFontDict->SetNewFor<CPDF_Name>("BaseFont", data.baseFont);
+      if (pEncoding) {
+        pFontDict->SetFor("Encoding",
+                          pEncoding->Realize(m_pDocument->GetByteStringPool()));
+      }
+      pIndirectFont = m_pDocument->AddIndirectObject(std::move(pFontDict));
     }
     dictName = RealizeResource(pIndirectFont, "Font");
-    m_pObjHolder->m_FontsMap[fontD] = dictName;
+    m_pObjHolder->m_FontsMap[data] = dictName;
   }
   *buf << "/" << PDF_NameEncode(dictName) << " " << pTextObj->GetFontSize()
        << " Tf ";
diff --git a/fpdfsdk/fpdf_annot_embeddertest.cpp b/fpdfsdk/fpdf_annot_embeddertest.cpp
index df1a25a..59dcd99 100644
--- a/fpdfsdk/fpdf_annot_embeddertest.cpp
+++ b/fpdfsdk/fpdf_annot_embeddertest.cpp
@@ -976,15 +976,15 @@
 TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
 #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
   const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
-  const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
+  const char md5_new_text[] = "60031c1b0330cf1e1575f7d46687d429";
   const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
 #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
   const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
-  const char md5_new_text[] = "554d625b52144816aaabb0dd66962c55";
+  const char md5_new_text[] = "87d55e09f9096de7e6552f5ae79afd3b";
   const char md5_modified_text[] = "26e94fbd3af4b1e65479327507600114";
 #else
   const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
-  const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
+  const char md5_new_text[] = "30f3f5b989612ca03827d95f184f0979";
   const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
 #endif
 
diff --git a/fpdfsdk/fpdf_edit_embeddertest.cpp b/fpdfsdk/fpdf_edit_embeddertest.cpp
index 163becb..6e03644 100644
--- a/fpdfsdk/fpdf_edit_embeddertest.cpp
+++ b/fpdfsdk/fpdf_edit_embeddertest.cpp
@@ -1780,6 +1780,7 @@
   EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
   FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
   FPDFPage_InsertObject(page, text_object1);
+  EXPECT_TRUE(FPDFPage_GenerateContent(page));
   {
     ScopedFPDFBitmap page_bitmap = RenderPage(page);
 #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
@@ -1790,6 +1791,9 @@
     const char md5[] = "eacaa24573b8ce997b3882595f096f00";
 #endif
     CompareBitmap(page_bitmap.get(), 612, 792, md5);
+
+    EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
+    VerifySavedDocument(612, 792, md5);
   }
 
   // Try another font
@@ -1801,16 +1805,20 @@
   EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
   FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
   FPDFPage_InsertObject(page, text_object2);
+  EXPECT_TRUE(FPDFPage_GenerateContent(page));
   {
     ScopedFPDFBitmap page_bitmap = RenderPage(page);
 #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
-    const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
+    const char md5[] = "a5c4ace4c6f27644094813fe1441a21c";
 #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
-    const char md5_2[] = "3755dd35abd4c605755369401ee85b2d";
+    const char md5[] = "3755dd35abd4c605755369401ee85b2d";
 #else
-    const char md5_2[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b";
+    const char md5[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b";
 #endif
-    CompareBitmap(page_bitmap.get(), 612, 792, md5_2);
+    CompareBitmap(page_bitmap.get(), 612, 792, md5);
+
+    EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
+    VerifySavedDocument(612, 792, md5);
   }
 
   // And some randomly transformed text
@@ -1822,16 +1830,20 @@
   EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
   FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
   FPDFPage_InsertObject(page, text_object3);
+  EXPECT_TRUE(FPDFPage_GenerateContent(page));
   {
     ScopedFPDFBitmap page_bitmap = RenderPage(page);
 #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
-    const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
+    const char md5[] = "40b3ef04f915ff4c4208948001763544";
 #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
-    const char md5_3[] = "aba523a8110d01ed9bd7b7781ff74045";
+    const char md5[] = "aba523a8110d01ed9bd7b7781ff74045";
 #else
-    const char md5_3[] = "b8a21668f1dab625af7c072e07fcefc4";
+    const char md5[] = "b8a21668f1dab625af7c072e07fcefc4";
 #endif
-    CompareBitmap(page_bitmap.get(), 612, 792, md5_3);
+    CompareBitmap(page_bitmap.get(), 612, 792, md5);
+
+    EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
+    VerifySavedDocument(612, 792, md5);
   }
 
   double matrix_a = 0;