Fix FT_UnicodeFromCharCode() encoding mismatch.

Now that enum class FontEncoding exists, it is obvious that
FT_UnicodeFromCharCode() is not taking in the right encoding value in
some cases. Looking at the existing callers to FT_UnicodeFromCharCode()
and its behavior, the call only works as intended when the encoding is
`FT_ENCODING_APPLE_ROMAN`. Thus simplify FT_UnicodeFromCharCode() to
UnicodeFromAppleRomanCharCode(). Remove the FT_UnicodeFromCharCode()
calls that do not make sense, where FT_UnicodeFromCharCode() always
returns 0, and simplify the caller code.

Change-Id: I948886d2ff90fc7ffd4ac14eb9309cf34ba29a49
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/91942
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_fontencoding.cpp b/core/fpdfapi/font/cpdf_fontencoding.cpp
index 4805fcb..f1079fe 100644
--- a/core/fpdfapi/font/cpdf_fontencoding.cpp
+++ b/core/fpdfapi/font/cpdf_fontencoding.cpp
@@ -1749,6 +1749,10 @@
   return 0;
 }
 
+wchar_t UnicodeFromAppleRomanCharCode(uint8_t charcode) {
+  return kMacRomanEncoding[charcode];
+}
+
 const uint16_t* PDF_UnicodesForPredefinedCharSet(FontEncoding encoding) {
   switch (encoding) {
     case FontEncoding::kBuiltin:
@@ -1804,21 +1808,3 @@
       return nullptr;
   }
 }
-
-wchar_t FT_UnicodeFromCharCode(int encoding, uint32_t charcode) {
-  switch (encoding) {
-    case FT_ENCODING_UNICODE:
-      return (uint16_t)charcode;
-    case FT_ENCODING_ADOBE_STANDARD:
-      return kStandardEncoding[(uint8_t)charcode];
-    case FT_ENCODING_ADOBE_EXPERT:
-      return kMacExpertEncoding[(uint8_t)charcode];
-    case FT_ENCODING_ADOBE_LATIN_1:
-      return kAdobeWinAnsiEncoding[(uint8_t)charcode];
-    case FT_ENCODING_APPLE_ROMAN:
-      return kMacRomanEncoding[(uint8_t)charcode];
-    case static_cast<int>(FontEncoding::kPdfDoc):
-      return kPDFDocEncoding[(uint8_t)charcode];
-  }
-  return 0;
-}
diff --git a/core/fpdfapi/font/cpdf_fontencoding.h b/core/fpdfapi/font/cpdf_fontencoding.h
index a23096a..bad7f1d 100644
--- a/core/fpdfapi/font/cpdf_fontencoding.h
+++ b/core/fpdfapi/font/cpdf_fontencoding.h
@@ -25,7 +25,7 @@
 };
 
 uint32_t FT_CharCodeFromUnicode(int encoding, wchar_t unicode);
-wchar_t FT_UnicodeFromCharCode(int encoding, uint32_t charcode);
+wchar_t UnicodeFromAppleRomanCharCode(uint8_t charcode);
 
 const uint16_t* PDF_UnicodesForPredefinedCharSet(FontEncoding encoding);
 const char* PDF_CharNameFromPredefinedCharSet(FontEncoding encoding,
diff --git a/core/fpdfapi/font/cpdf_truetypefont.cpp b/core/fpdfapi/font/cpdf_truetypefont.cpp
index 9c0f494..3ec388c 100644
--- a/core/fpdfapi/font/cpdf_truetypefont.cpp
+++ b/core/fpdfapi/font/cpdf_truetypefont.cpp
@@ -128,10 +128,9 @@
             m_Encoding.SetUnicode(charcode, PDF_UnicodeFromAdobeName(name));
         }
       } else if (UseTTCharmapMacRoman(face)) {
-        for (int charcode = 0; charcode < 256; charcode++) {
-          m_Encoding.SetUnicode(
-              charcode,
-              FT_UnicodeFromCharCode(FT_ENCODING_APPLE_ROMAN, charcode));
+        for (uint32_t charcode = 0; charcode < 256; charcode++) {
+          m_Encoding.SetUnicode(charcode,
+                                UnicodeFromAppleRomanCharCode(charcode));
         }
       }
       return;
@@ -140,8 +139,7 @@
   if (UseTTCharmapMacRoman(face)) {
     for (uint32_t charcode = 0; charcode < 256; charcode++) {
       m_GlyphIndex[charcode] = FT_Get_Char_Index(face, charcode);
-      m_Encoding.SetUnicode(
-          charcode, FT_UnicodeFromCharCode(FT_ENCODING_APPLE_ROMAN, charcode));
+      m_Encoding.SetUnicode(charcode, UnicodeFromAppleRomanCharCode(charcode));
     }
     if (m_pFontFile || HasAnyGlyphIndex())
       return;
diff --git a/core/fpdfapi/font/cpdf_type1font.cpp b/core/fpdfapi/font/cpdf_type1font.cpp
index af01b6d..2799f89 100644
--- a/core/fpdfapi/font/cpdf_type1font.cpp
+++ b/core/fpdfapi/font/cpdf_type1font.cpp
@@ -208,18 +208,12 @@
         } else {
           m_GlyphIndex[charcode] =
               FT_Get_Char_Index(m_Font.GetFaceRec(), charcode);
-          wchar_t unicode = 0;
-          if (m_GlyphIndex[charcode]) {
-            unicode = FT_UnicodeFromCharCode(
-                static_cast<int>(FontEncoding::kStandard), charcode);
-          }
           char name_glyph[kInternalTableSize] = {};
           FT_Get_Glyph_Name(m_Font.GetFaceRec(), m_GlyphIndex[charcode],
                             name_glyph, sizeof(name_glyph));
           name_glyph[kInternalTableSize - 1] = 0;
-          if (unicode == 0 && name_glyph[0] != 0)
-            unicode = PDF_UnicodeFromAdobeName(name_glyph);
-
+          const wchar_t unicode =
+              name_glyph[0] != 0 ? PDF_UnicodeFromAdobeName(name_glyph) : 0;
           m_Encoding.SetUnicode(charcode, unicode);
           SetExtGID(name_glyph, charcode);
         }
@@ -271,17 +265,12 @@
         m_GlyphIndex[charcode] = FT_Get_Char_Index(
             m_Font.GetFaceRec(), static_cast<uint32_t>(charcode));
         if (m_GlyphIndex[charcode]) {
-          wchar_t unicode =
-              FT_UnicodeFromCharCode(static_cast<int>(FontEncoding::kStandard),
-                                     static_cast<uint32_t>(charcode));
-          if (unicode == 0) {
-            char name_glyph[kInternalTableSize] = {};
-            FT_Get_Glyph_Name(m_Font.GetFaceRec(), m_GlyphIndex[charcode],
-                              name_glyph, sizeof(name_glyph));
-            name_glyph[kInternalTableSize - 1] = 0;
-            if (name_glyph[0] != 0)
-              unicode = PDF_UnicodeFromAdobeName(name_glyph);
-          }
+          char name_glyph[kInternalTableSize] = {};
+          FT_Get_Glyph_Name(m_Font.GetFaceRec(), m_GlyphIndex[charcode],
+                            name_glyph, sizeof(name_glyph));
+          name_glyph[kInternalTableSize - 1] = 0;
+          const wchar_t unicode =
+              name_glyph[0] != 0 ? PDF_UnicodeFromAdobeName(name_glyph) : 0;
           m_Encoding.SetUnicode(charcode, unicode);
         }
       }