Encapsulate FT_Get_Char_Index() and FT_Get_Name_Index() calls

Implement CFX_Face::GetCharIndex() and GetNameIndex(), and switch all
existing callers.

Bug: pdfium:2037
Change-Id: Iaff242db6ccc1ca82d1fba6712995e4fbc483db3
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/114793
Reviewed-by: Dominik Röttsches <drott@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_cidfont.cpp b/core/fpdfapi/font/cpdf_cidfont.cpp
index 26caef9..88a5686 100644
--- a/core/fpdfapi/font/cpdf_cidfont.cpp
+++ b/core/fpdfapi/font/cpdf_cidfont.cpp
@@ -642,8 +642,7 @@
   if (pVertGlyph)
     *pVertGlyph = false;
 
-  FXFT_FaceRec* face = m_Font.GetFaceRec();
-  int index = FT_Get_Char_Index(face, unicode);
+  int index = m_Font.GetFace()->GetCharIndex(unicode);
   if (unicode == pdfium::unicode::kBoxDrawingsLightVerical)
     return index;
 
@@ -655,6 +654,7 @@
 
   static constexpr uint32_t kGsubTag =
       CFX_FontMapper::MakeTag('G', 'S', 'U', 'B');
+  FXFT_FaceRec* face = m_Font.GetFaceRec();
   unsigned long length = 0;
   int error = FT_Load_Sfnt_Table(face, kGsubTag, 0, nullptr, &length);
   if (error || !length) {
@@ -740,17 +740,18 @@
       if (!name_unicode)
         return charcode ? static_cast<int>(charcode) : -1;
 
-      if (base_encoding == FontEncoding::kStandard)
-        return FT_Get_Char_Index(face_rec, name_unicode);
+      if (base_encoding == FontEncoding::kStandard) {
+        return face->GetCharIndex(name_unicode);
+      }
 
       if (base_encoding == FontEncoding::kWinAnsi) {
-        index = FT_Get_Char_Index(face_rec, name_unicode);
+        index = face->GetCharIndex(name_unicode);
       } else {
         DCHECK_EQ(base_encoding, FontEncoding::kMacRoman);
         uint32_t maccode = CharCodeFromUnicodeForEncoding(
             fxge::FontEncoding::kAppleRoman, name_unicode);
-        index = maccode ? FT_Get_Char_Index(face_rec, maccode)
-                        : FT_Get_Name_Index(face_rec, name);
+        index =
+            maccode ? face->GetCharIndex(maccode) : face->GetNameIndex(name);
       }
       if (index == 0 || index == 0xffff)
         return charcode ? static_cast<int>(charcode) : -1;
diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp
index 2f88a69..e87aa36 100644
--- a/core/fpdfapi/font/cpdf_font.cpp
+++ b/core/fpdfapi/font/cpdf_font.cpp
@@ -393,8 +393,7 @@
 
   WideString str = UnicodeFromCharCode(charcode);
   uint32_t unicode = !str.IsEmpty() ? str[0] : charcode;
-  int glyph =
-      FT_Get_Char_Index(m_FontFallbacks[fallbackFont]->GetFaceRec(), unicode);
+  int glyph = m_FontFallbacks[fallbackFont]->GetFace()->GetCharIndex(unicode);
   if (glyph == 0)
     return -1;
 
diff --git a/core/fpdfapi/font/cpdf_truetypefont.cpp b/core/fpdfapi/font/cpdf_truetypefont.cpp
index 5c6505f..0c90b09 100644
--- a/core/fpdfapi/font/cpdf_truetypefont.cpp
+++ b/core/fpdfapi/font/cpdf_truetypefont.cpp
@@ -16,10 +16,11 @@
 
 constexpr uint8_t kPrefix[4] = {0x00, 0xf0, 0xf1, 0xf2};
 
-uint16_t GetGlyphIndexForMSSymbol(FXFT_FaceRec* face, uint32_t charcode) {
+uint16_t GetGlyphIndexForMSSymbol(const RetainPtr<CFX_Face>& face,
+                                  uint32_t charcode) {
   for (uint8_t c : kPrefix) {
     uint16_t unicode = c * 256 + charcode;
-    uint16_t val = FT_Get_Char_Index(face, unicode);
+    uint16_t val = face->GetCharIndex(unicode);
     if (val)
       return val;
   }
@@ -56,12 +57,11 @@
 }
 
 void CPDF_TrueTypeFont::LoadGlyphMap() {
-  FXFT_FaceRec* face_rec = m_Font.GetFaceRec();
-  if (!face_rec) {
+  RetainPtr<CFX_Face> face = m_Font.GetFace();
+  if (!face) {
     return;
   }
 
-  RetainPtr<CFX_Face> face = m_Font.GetFace();
   const FontEncoding base_encoding = DetermineEncoding();
   if ((IsWinAnsiOrMacRomanEncoding(base_encoding) && m_CharNames.empty()) ||
       FontStyleIsNonSymbolic(m_Flags)) {
@@ -76,24 +76,24 @@
       const char* name = GetAdobeCharName(base_encoding, m_CharNames, charcode);
       if (!name) {
         m_GlyphIndex[charcode] =
-            m_pFontFile ? FT_Get_Char_Index(face_rec, charcode) : -1;
+            m_pFontFile ? face->GetCharIndex(charcode) : -1;
         continue;
       }
       m_Encoding.SetUnicode(charcode, UnicodeFromAdobeName(name));
       if (charmap_type == CharmapType::kMSSymbol) {
-        m_GlyphIndex[charcode] = GetGlyphIndexForMSSymbol(face_rec, charcode);
+        m_GlyphIndex[charcode] = GetGlyphIndexForMSSymbol(face, charcode);
       } else if (m_Encoding.UnicodeFromCharCode(charcode)) {
         if (charmap_type == CharmapType::kMSUnicode) {
-          m_GlyphIndex[charcode] = FT_Get_Char_Index(
-              face_rec, m_Encoding.UnicodeFromCharCode(charcode));
+          m_GlyphIndex[charcode] =
+              face->GetCharIndex(m_Encoding.UnicodeFromCharCode(charcode));
         } else if (charmap_type == CharmapType::kMacRoman) {
           uint32_t maccode = CharCodeFromUnicodeForEncoding(
               fxge::FontEncoding::kAppleRoman,
               m_Encoding.UnicodeFromCharCode(charcode));
           if (!maccode) {
-            m_GlyphIndex[charcode] = FT_Get_Name_Index(face_rec, name);
+            m_GlyphIndex[charcode] = face->GetNameIndex(name);
           } else {
-            m_GlyphIndex[charcode] = FT_Get_Char_Index(face_rec, maccode);
+            m_GlyphIndex[charcode] = face->GetCharIndex(maccode);
           }
         }
       }
@@ -102,16 +102,16 @@
         continue;
       }
       if (strcmp(name, ".notdef") == 0) {
-        m_GlyphIndex[charcode] = FT_Get_Char_Index(face_rec, 32);
+        m_GlyphIndex[charcode] = face->GetCharIndex(32);
         continue;
       }
-      m_GlyphIndex[charcode] = FT_Get_Name_Index(face_rec, name);
+      m_GlyphIndex[charcode] = face->GetNameIndex(name);
       if (m_GlyphIndex[charcode] != 0 || !bToUnicode)
         continue;
 
       WideString wsUnicode = UnicodeFromCharCode(charcode);
       if (!wsUnicode.IsEmpty()) {
-        m_GlyphIndex[charcode] = FT_Get_Char_Index(face_rec, wsUnicode[0]);
+        m_GlyphIndex[charcode] = face->GetCharIndex(wsUnicode[0]);
         m_Encoding.SetUnicode(charcode, wsUnicode[0]);
       }
     }
@@ -119,7 +119,7 @@
   }
   if (UseTTCharmapMSSymbol(face)) {
     for (uint32_t charcode = 0; charcode < 256; charcode++)
-      m_GlyphIndex[charcode] = GetGlyphIndexForMSSymbol(face_rec, charcode);
+      m_GlyphIndex[charcode] = GetGlyphIndexForMSSymbol(face, charcode);
     if (HasAnyGlyphIndex()) {
       if (base_encoding != FontEncoding::kBuiltin) {
         for (uint32_t charcode = 0; charcode < 256; charcode++) {
@@ -139,7 +139,7 @@
   }
   if (UseTTCharmapMacRoman(face)) {
     for (uint32_t charcode = 0; charcode < 256; charcode++) {
-      m_GlyphIndex[charcode] = FT_Get_Char_Index(face_rec, charcode);
+      m_GlyphIndex[charcode] = face->GetCharIndex(charcode);
       m_Encoding.SetUnicode(charcode, UnicodeFromAppleRomanCharCode(charcode));
     }
     if (m_pFontFile || HasAnyGlyphIndex())
@@ -161,7 +161,7 @@
         }
       }
       m_GlyphIndex[charcode] =
-          FT_Get_Char_Index(face_rec, m_Encoding.UnicodeFromCharCode(charcode));
+          face->GetCharIndex(m_Encoding.UnicodeFromCharCode(charcode));
     }
     if (HasAnyGlyphIndex())
       return;
diff --git a/core/fpdfapi/font/cpdf_type1font.cpp b/core/fpdfapi/font/cpdf_type1font.cpp
index a556c11..cc77cdc 100644
--- a/core/fpdfapi/font/cpdf_type1font.cpp
+++ b/core/fpdfapi/font/cpdf_type1font.cpp
@@ -126,8 +126,10 @@
 #endif
 
 void CPDF_Type1Font::LoadGlyphMap() {
-  if (!m_Font.GetFaceRec())
+  RetainPtr<CFX_Face> face = m_Font.GetFace();
+  if (!face) {
     return;
+  }
 
 #if BUILDFLAG(IS_APPLE)
   bool bCoreText = true;
@@ -143,14 +145,13 @@
   }
 #endif
   if (!IsEmbedded() && !IsSymbolicFont() && m_Font.IsTTFont()) {
-    if (UseTTCharmapMSSymbol(m_Font.GetFace())) {
+    if (UseTTCharmapMSSymbol(face)) {
       bool bGotOne = false;
       for (uint32_t charcode = 0; charcode < kInternalTableSize; charcode++) {
         const uint8_t prefix[4] = {0x00, 0xf0, 0xf1, 0xf2};
         for (int j = 0; j < 4; j++) {
           uint16_t unicode = prefix[j] * 256 + charcode;
-          m_GlyphIndex[charcode] =
-              FT_Get_Char_Index(m_Font.GetFaceRec(), unicode);
+          m_GlyphIndex[charcode] = face->GetCharIndex(unicode);
 #if BUILDFLAG(IS_APPLE)
           CalcExtGID(charcode);
 #endif
@@ -168,7 +169,7 @@
         return;
       }
     }
-    m_Font.GetFace()->SelectCharMap(fxge::FontEncoding::kUnicode);
+    face->SelectCharMap(fxge::FontEncoding::kUnicode);
     if (m_BaseEncoding == FontEncoding::kBuiltin)
       m_BaseEncoding = FontEncoding::kStandard;
 
@@ -179,14 +180,14 @@
         continue;
 
       m_Encoding.SetUnicode(charcode, UnicodeFromAdobeName(name));
-      m_GlyphIndex[charcode] = FT_Get_Char_Index(
-          m_Font.GetFaceRec(), m_Encoding.UnicodeFromCharCode(charcode));
+      m_GlyphIndex[charcode] =
+          face->GetCharIndex(m_Encoding.UnicodeFromCharCode(charcode));
 #if BUILDFLAG(IS_APPLE)
       CalcExtGID(charcode);
 #endif
       if (m_GlyphIndex[charcode] == 0 && strcmp(name, ".notdef") == 0) {
         m_Encoding.SetUnicode(charcode, 0x20);
-        m_GlyphIndex[charcode] = FT_Get_Char_Index(m_Font.GetFaceRec(), 0x20);
+        m_GlyphIndex[charcode] = face->GetCharIndex(0x20);
 #if BUILDFLAG(IS_APPLE)
         CalcExtGID(charcode);
 #endif
@@ -200,7 +201,7 @@
 #endif
     return;
   }
-  UseType1Charmap(m_Font.GetFace());
+  UseType1Charmap(face);
 #if BUILDFLAG(IS_APPLE)
   if (bCoreText) {
     if (FontStyleIsSymbolic(m_Flags)) {
@@ -209,11 +210,10 @@
             GetAdobeCharName(m_BaseEncoding, m_CharNames, charcode);
         if (name) {
           m_Encoding.SetUnicode(charcode, UnicodeFromAdobeName(name));
-          m_GlyphIndex[charcode] = FT_Get_Name_Index(m_Font.GetFaceRec(), name);
+          m_GlyphIndex[charcode] = m_Font.GetFace()->GetNameIndex(name);
           SetExtGID(name, charcode);
         } else {
-          m_GlyphIndex[charcode] =
-              FT_Get_Char_Index(m_Font.GetFaceRec(), charcode);
+          m_GlyphIndex[charcode] = face->GetCharIndex(charcode);
           char name_glyph[kInternalTableSize] = {};
           FT_Get_Glyph_Name(m_Font.GetFaceRec(), m_GlyphIndex[charcode],
                             name_glyph, sizeof(name_glyph));
@@ -227,8 +227,7 @@
       return;
     }
 
-    bool bUnicode =
-        m_Font.GetFace()->SelectCharMap(fxge::FontEncoding::kUnicode);
+    bool bUnicode = face->SelectCharMap(fxge::FontEncoding::kUnicode);
     for (uint32_t charcode = 0; charcode < kInternalTableSize; charcode++) {
       const char* name =
           GetAdobeCharName(m_BaseEncoding, m_CharNames, charcode);
@@ -237,23 +236,22 @@
 
       m_Encoding.SetUnicode(charcode, UnicodeFromAdobeName(name));
       const char* pStrUnicode = GlyphNameRemap(name);
-      if (pStrUnicode && FT_Get_Name_Index(m_Font.GetFaceRec(), name) == 0) {
+      int name_index = m_Font.GetFace()->GetNameIndex(name);
+      if (pStrUnicode && name_index == 0) {
         name = pStrUnicode;
       }
-      m_GlyphIndex[charcode] = FT_Get_Name_Index(m_Font.GetFaceRec(), name);
+      m_GlyphIndex[charcode] = name_index;
       SetExtGID(name, charcode);
       if (m_GlyphIndex[charcode] != 0)
         continue;
 
       if (strcmp(name, ".notdef") != 0 && strcmp(name, "space") != 0) {
-        m_GlyphIndex[charcode] = FT_Get_Char_Index(
-            m_Font.GetFaceRec(),
+        m_GlyphIndex[charcode] = face->GetCharIndex(
             bUnicode ? m_Encoding.UnicodeFromCharCode(charcode) : charcode);
         CalcExtGID(charcode);
       } else {
         m_Encoding.SetUnicode(charcode, 0x20);
-        m_GlyphIndex[charcode] =
-            bUnicode ? FT_Get_Char_Index(m_Font.GetFaceRec(), 0x20) : 0xffff;
+        m_GlyphIndex[charcode] = bUnicode ? face->GetCharIndex(0x20) : 0xffff;
         CalcExtGID(charcode);
       }
     }
@@ -266,10 +264,10 @@
                                           static_cast<uint32_t>(charcode));
       if (name) {
         m_Encoding.SetUnicode(charcode, UnicodeFromAdobeName(name));
-        m_GlyphIndex[charcode] = FT_Get_Name_Index(m_Font.GetFaceRec(), name);
+        m_GlyphIndex[charcode] = m_Font.GetFace()->GetNameIndex(name);
       } else {
-        m_GlyphIndex[charcode] = FT_Get_Char_Index(
-            m_Font.GetFaceRec(), static_cast<uint32_t>(charcode));
+        m_GlyphIndex[charcode] =
+            face->GetCharIndex(static_cast<uint32_t>(charcode));
         if (m_GlyphIndex[charcode]) {
           char name_glyph[kInternalTableSize] = {};
           FT_Get_Glyph_Name(m_Font.GetFaceRec(), m_GlyphIndex[charcode],
@@ -288,7 +286,7 @@
     return;
   }
 
-  bool bUnicode = m_Font.GetFace()->SelectCharMap(fxge::FontEncoding::kUnicode);
+  bool bUnicode = face->SelectCharMap(fxge::FontEncoding::kUnicode);
   for (size_t charcode = 0; charcode < kInternalTableSize; charcode++) {
     const char* name = GetAdobeCharName(m_BaseEncoding, m_CharNames,
                                         static_cast<uint32_t>(charcode));
@@ -296,15 +294,14 @@
       continue;
 
     m_Encoding.SetUnicode(charcode, UnicodeFromAdobeName(name));
-    m_GlyphIndex[charcode] = FT_Get_Name_Index(m_Font.GetFaceRec(), name);
+    m_GlyphIndex[charcode] = m_Font.GetFace()->GetNameIndex(name);
     if (m_GlyphIndex[charcode] != 0)
       continue;
 
     if (strcmp(name, ".notdef") != 0 && strcmp(name, "space") != 0) {
       m_GlyphIndex[charcode] =
-          FT_Get_Char_Index(m_Font.GetFaceRec(),
-                            bUnicode ? m_Encoding.UnicodeFromCharCode(charcode)
-                                     : static_cast<uint32_t>(charcode));
+          face->GetCharIndex(bUnicode ? m_Encoding.UnicodeFromCharCode(charcode)
+                                      : static_cast<uint32_t>(charcode));
     } else {
       m_Encoding.SetUnicode(charcode, 0x20);
       m_GlyphIndex[charcode] = 0xffff;
diff --git a/core/fxge/cfx_face.cpp b/core/fxge/cfx_face.cpp
index ad80937..f335d42 100644
--- a/core/fxge/cfx_face.cpp
+++ b/core/fxge/cfx_face.cpp
@@ -525,6 +525,14 @@
   return static_cast<int>(EM_ADJUST(GetUnitsPerEm(), horizontal_advance));
 }
 
+int CFX_Face::GetCharIndex(uint32_t code) {
+  return FT_Get_Char_Index(GetRec(), code);
+}
+
+int CFX_Face::GetNameIndex(const char* name) {
+  return FT_Get_Name_Index(GetRec(), name);
+}
+
 CFX_Face::CharMap CFX_Face::GetCurrentCharMap() const {
   return GetRec()->charmap;
 }
diff --git a/core/fxge/cfx_face.h b/core/fxge/cfx_face.h
index 3d0f5b7..b9e5aba 100644
--- a/core/fxge/cfx_face.h
+++ b/core/fxge/cfx_face.h
@@ -83,6 +83,9 @@
                     int weight,
                     const CFX_SubstFont* subst_font);
 
+  int GetCharIndex(uint32_t code);
+  int GetNameIndex(const char* name);
+
   CharMap GetCurrentCharMap() const;
   absl::optional<fxge::FontEncoding> GetCurrentCharMapEncoding() const;
   int GetCharMapPlatformIdByIndex(size_t index) const;
diff --git a/core/fxge/cfx_unicodeencoding.cpp b/core/fxge/cfx_unicodeencoding.cpp
index de37a2a..d9c025a 100644
--- a/core/fxge/cfx_unicodeencoding.cpp
+++ b/core/fxge/cfx_unicodeencoding.cpp
@@ -9,7 +9,6 @@
 #include "core/fxcrt/fx_codepage.h"
 #include "core/fxge/cfx_font.h"
 #include "core/fxge/cfx_substfont.h"
-#include "core/fxge/freetype/fx_freetype.h"
 #include "core/fxge/fx_font.h"
 #include "core/fxge/fx_fontencoding.h"
 
@@ -19,23 +18,22 @@
 CFX_UnicodeEncoding::~CFX_UnicodeEncoding() = default;
 
 uint32_t CFX_UnicodeEncoding::GlyphFromCharCode(uint32_t charcode) {
-  FXFT_FaceRec* face = m_pFont->GetFaceRec();
+  RetainPtr<CFX_Face> face = m_pFont->GetFace();
   if (!face)
     return charcode;
 
-  if (m_pFont->GetFace()->SelectCharMap(fxge::FontEncoding::kUnicode)) {
-    return FT_Get_Char_Index(face, charcode);
+  if (face->SelectCharMap(fxge::FontEncoding::kUnicode)) {
+    return face->GetCharIndex(charcode);
   }
 
   if (m_pFont->GetSubstFont() &&
       m_pFont->GetSubstFont()->m_Charset == FX_Charset::kSymbol) {
     uint32_t index = 0;
-    if (m_pFont->GetFace()->SelectCharMap(fxge::FontEncoding::kSymbol)) {
-      index = FT_Get_Char_Index(face, charcode);
+    if (face->SelectCharMap(fxge::FontEncoding::kSymbol)) {
+      index = face->GetCharIndex(charcode);
     }
-    if (!index &&
-        m_pFont->GetFace()->SelectCharMap(fxge::FontEncoding::kAppleRoman)) {
-      return FT_Get_Char_Index(face, charcode);
+    if (!index && face->SelectCharMap(fxge::FontEncoding::kAppleRoman)) {
+      return face->GetCharIndex(charcode);
     }
   }
   return charcode;
diff --git a/core/fxge/cfx_unicodeencodingex.cpp b/core/fxge/cfx_unicodeencodingex.cpp
index 03cdf3b..5fcf5a1 100644
--- a/core/fxge/cfx_unicodeencodingex.cpp
+++ b/core/fxge/cfx_unicodeencodingex.cpp
@@ -43,12 +43,11 @@
 CFX_UnicodeEncodingEx::~CFX_UnicodeEncodingEx() = default;
 
 uint32_t CFX_UnicodeEncodingEx::GlyphFromCharCode(uint32_t charcode) {
-  FXFT_FaceRec* face_rec = m_pFont->GetFaceRec();
-  FT_UInt nIndex = FT_Get_Char_Index(face_rec, charcode);
+  RetainPtr<CFX_Face> face = m_pFont->GetFace();
+  FT_UInt nIndex = face->GetCharIndex(charcode);
   if (nIndex > 0)
     return nIndex;
 
-  RetainPtr<CFX_Face> face = m_pFont->GetFace();
   size_t map_index = 0;
   while (map_index < face->GetCharMapCount()) {
     fxge::FontEncoding encoding_id =
@@ -59,7 +58,7 @@
     if (!face->SelectCharMap(encoding_id)) {
       continue;
     }
-    nIndex = FT_Get_Char_Index(face_rec, charcode);
+    nIndex = face->GetCharIndex(charcode);
     if (nIndex > 0) {
       encoding_id_ = encoding_id;
       return nIndex;
diff --git a/xfa/fgas/font/cfgas_fontmgr.cpp b/xfa/fgas/font/cfgas_fontmgr.cpp
index 7ba38a0..e117b5e 100644
--- a/xfa/fgas/font/cfgas_fontmgr.cpp
+++ b/xfa/fgas/font/cfgas_fontmgr.cpp
@@ -46,8 +46,7 @@
     return false;
   }
 
-  FXFT_FaceRec* pFaceRec = pFace->GetRec();
-  if (FT_Get_Char_Index(pFaceRec, wcUnicode) == 0) {
+  if (pFace->GetCharIndex(wcUnicode) == 0) {
     pFace->SetCharMap(charmap);
     return false;
   }
@@ -552,7 +551,7 @@
 
   bool select_charmap_result =
       pFace->SelectCharMap(fxge::FontEncoding::kUnicode);
-  FT_Error ret_index = FT_Get_Char_Index(pFace->GetRec(), wcUnicode);
+  int ret_index = pFace->GetCharIndex(wcUnicode);
 
   pFace->ClearExternalStream();