Update the "BaseFont" value of Type0, Type1, and TrueType.

The change is based on:

- PDF Spec 1.7, section 5.5.2, page 418, the last two paragraphs, for
  the name of the TrueType fonts:
  - "If the name contains any spaces, the spaces are removed."
  - "..., a comma and the style name (one of Bold, Italic, or
    BoldItalic) are appended to the font name."

- PDF Spec 1.7, section 5.5.1, page 413, table 5.8: for Type1 font, the
  "BaseFont"'s value should be the PostScript Name of the font.

- Use "Untitled" for empty names (before this change, "Unnamed" is used
  in fpdfsdk/fpdf_edittext.cpp while "Untitled" is used in
  core/fxge/cfx_font.cpp).

Change-Id: I954d63a7e47367c982a3ba4abe28ec34c2be8bd2
Reviewed-on: https://pdfium-review.googlesource.com/c/48810
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Nicolás Peña Moreno <npm@chromium.org>
diff --git a/core/fxge/cfx_font.cpp b/core/fxge/cfx_font.cpp
index b686d26..64423eb 100644
--- a/core/fxge/cfx_font.cpp
+++ b/core/fxge/cfx_font.cpp
@@ -532,6 +532,26 @@
   return m_pSubstFont->m_Family;
 }
 
+ByteString CFX_Font::GetBaseFontName(bool restrict_to_psname) const {
+  ByteString psname = GetPsName();
+  if (restrict_to_psname || (!psname.IsEmpty() && psname != "Untitled"))
+    return psname;
+  if (!m_Face && !m_pSubstFont)
+    return ByteString();
+  if (m_Face) {
+    ByteString style = ByteString(FXFT_Get_Face_Style_Name(m_Face.Get()));
+    ByteString facename = GetFamilyName();
+    if (facename.IsEmpty())
+      facename = "Untitled";
+    if (IsTTFont())
+      facename.Remove(' ');
+    if (!style.IsEmpty() && style != "Regular")
+      facename += (IsTTFont() ? "," : " ") + style;
+    return facename;
+  }
+  return m_pSubstFont->m_Family;
+}
+
 bool CFX_Font::GetBBox(FX_RECT* pBBox) {
   if (!m_Face)
     return false;
diff --git a/core/fxge/cfx_font.h b/core/fxge/cfx_font.h
index 9293fd7..7e78110 100644
--- a/core/fxge/cfx_font.h
+++ b/core/fxge/cfx_font.h
@@ -81,6 +81,7 @@
   ByteString GetPsName() const;
   ByteString GetFamilyName() const;
   ByteString GetFaceName() const;
+  ByteString GetBaseFontName(bool restrict_to_psname) const;
   bool IsTTFont() const;
   bool GetBBox(FX_RECT* pBBox);
   bool IsEmbedded() const { return m_bEmbedded; }
diff --git a/fpdfsdk/fpdf_edit_embeddertest.cpp b/fpdfsdk/fpdf_edit_embeddertest.cpp
index 03fabd7..d911c54 100644
--- a/fpdfsdk/fpdf_edit_embeddertest.cpp
+++ b/fpdfsdk/fpdf_edit_embeddertest.cpp
@@ -2171,7 +2171,7 @@
   const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
   EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
   EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
-  EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
+  EXPECT_EQ("TimesNewRomanPS-BoldMT", font_dict->GetStringFor("BaseFont"));
   ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
   ASSERT_TRUE(font_dict->KeyExist("LastChar"));
   EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
@@ -2199,7 +2199,7 @@
   const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
   EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
   EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
-  EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
+  EXPECT_EQ("CourierNewPSMT", font_dict->GetStringFor("BaseFont"));
   ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
   ASSERT_TRUE(font_dict->KeyExist("LastChar"));
   EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
@@ -2229,7 +2229,7 @@
   const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
   EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
   EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
-  EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
+  EXPECT_EQ("TimesNewRomanPSMT-Identity-H", font_dict->GetStringFor("BaseFont"));
   EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
   const CPDF_Array* descendant_array =
       font_dict->GetArrayFor("DescendantFonts");
@@ -2240,7 +2240,7 @@
   const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
   EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
   EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
-  EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
+  EXPECT_EQ("TimesNewRomanPSMT", cidfont_dict->GetStringFor("BaseFont"));
   const CPDF_Dictionary* cidinfo_dict =
       cidfont_dict->GetDictFor("CIDSystemInfo");
   ASSERT_TRUE(cidinfo_dict);
@@ -2277,7 +2277,7 @@
   const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
   EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
   EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
-  EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
+  EXPECT_EQ("Arial-ItalicMT", font_dict->GetStringFor("BaseFont"));
   EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
   const CPDF_Array* descendant_array =
       font_dict->GetArrayFor("DescendantFonts");
@@ -2288,7 +2288,7 @@
   const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
   EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
   EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
-  EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
+  EXPECT_EQ("Arial-ItalicMT", cidfont_dict->GetStringFor("BaseFont"));
   const CPDF_Dictionary* cidinfo_dict =
       cidfont_dict->GetDictFor("CIDSystemInfo");
   ASSERT_TRUE(cidinfo_dict);
diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp
index 361c117..45749f0 100644
--- a/fpdfsdk/fpdf_edittext.cpp
+++ b/fpdfsdk/fpdf_edittext.cpp
@@ -269,9 +269,9 @@
   fontDict->SetNewFor<CPDF_Name>("Type", "Font");
   fontDict->SetNewFor<CPDF_Name>(
       "Subtype", font_type == FPDF_FONT_TYPE1 ? "Type1" : "TrueType");
-  ByteString name = pFont->GetFaceName();
+  ByteString name = pFont->GetBaseFontName(font_type == FPDF_FONT_TYPE1);
   if (name.IsEmpty())
-    name = "Unnamed";
+    name = "Untitled";
   fontDict->SetNewFor<CPDF_Name>("BaseFont", name);
 
   uint32_t glyphIndex;
@@ -314,9 +314,9 @@
   // TODO(npm): Get the correct encoding, if it's not identity.
   ByteString encoding = "Identity-H";
   fontDict->SetNewFor<CPDF_Name>("Encoding", encoding);
-  ByteString name = pFont->GetFaceName();
+  ByteString name = pFont->GetBaseFontName(font_type == FPDF_FONT_TYPE1);
   if (name.IsEmpty())
-    name = "Unnamed";
+    name = "Untitled";
   fontDict->SetNewFor<CPDF_Name>(
       "BaseFont", font_type == FPDF_FONT_TYPE1 ? name + "-" + encoding : name);