Reduce code duplication in CPDF_Type1Font::LoadGlyphMap

Added methods SetExtGID and CalcExtGID for this purpose.
Changed protected to private: no one inherits CPDF_Type1Font.

Review-Url: https://codereview.chromium.org/2412483006
diff --git a/core/fpdfapi/font/cpdf_type1font.cpp b/core/fpdfapi/font/cpdf_type1font.cpp
index 127ec28..9ef4510 100644
--- a/core/fpdfapi/font/cpdf_type1font.cpp
+++ b/core/fpdfapi/font/cpdf_type1font.cpp
@@ -140,18 +140,7 @@
           m_GlyphIndex[charcode] =
               FXFT_Get_Char_Index(m_Font.GetFace(), unicode);
 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
-          FX_CHAR name_glyph[256];
-          FXFT_Get_Glyph_Name(m_Font.GetFace(), m_GlyphIndex[charcode],
-                              name_glyph, 256);
-          name_glyph[255] = 0;
-          CFStringRef name_ct = CFStringCreateWithCStringNoCopy(
-              kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII,
-              kCFAllocatorNull);
-          m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName(
-              (CGFontRef)m_Font.GetPlatformFont(), name_ct);
-          if (name_ct) {
-            CFRelease(name_ct);
-          }
+          CalcExtGID(charcode);
 #endif
           if (m_GlyphIndex[charcode]) {
             bGotOne = true;
@@ -181,35 +170,13 @@
       m_GlyphIndex[charcode] = FXFT_Get_Char_Index(
           m_Font.GetFace(), m_Encoding.m_Unicodes[charcode]);
 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
-      FX_CHAR name_glyph[256];
-      FXFT_Get_Glyph_Name(m_Font.GetFace(), m_GlyphIndex[charcode], name_glyph,
-                          256);
-      name_glyph[255] = 0;
-      CFStringRef name_ct = CFStringCreateWithCStringNoCopy(
-          kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII,
-          kCFAllocatorNull);
-      m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName(
-          (CGFontRef)m_Font.GetPlatformFont(), name_ct);
-      if (name_ct) {
-        CFRelease(name_ct);
-      }
+      CalcExtGID(charcode);
 #endif
       if (m_GlyphIndex[charcode] == 0 && FXSYS_strcmp(name, ".notdef") == 0) {
         m_Encoding.m_Unicodes[charcode] = 0x20;
         m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.GetFace(), 0x20);
 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
-        FX_CHAR name_glyph[256];
-        FXFT_Get_Glyph_Name(m_Font.GetFace(), m_GlyphIndex[charcode],
-                            name_glyph, 256);
-        name_glyph[255] = 0;
-        CFStringRef name_ct = CFStringCreateWithCStringNoCopy(
-            kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII,
-            kCFAllocatorNull);
-        m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName(
-            (CGFontRef)m_Font.GetPlatformFont(), name_ct);
-        if (name_ct) {
-          CFRelease(name_ct);
-        }
+        CalcExtGID(charcode);
 #endif
       }
     }
@@ -230,14 +197,7 @@
           m_Encoding.m_Unicodes[charcode] = PDF_UnicodeFromAdobeName(name);
           m_GlyphIndex[charcode] =
               FXFT_Get_Name_Index(m_Font.GetFace(), (char*)name);
-          CFStringRef name_ct = CFStringCreateWithCStringNoCopy(
-              kCFAllocatorDefault, name, kCFStringEncodingASCII,
-              kCFAllocatorNull);
-          m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName(
-              (CGFontRef)m_Font.GetPlatformFont(), name_ct);
-          if (name_ct) {
-            CFRelease(name_ct);
-          }
+          SetExtGID(name, charcode);
         } else {
           m_GlyphIndex[charcode] =
               FXFT_Get_Char_Index(m_Font.GetFace(), charcode);
@@ -255,14 +215,7 @@
             unicode = PDF_UnicodeFromAdobeName(name_glyph);
           }
           m_Encoding.m_Unicodes[charcode] = unicode;
-          CFStringRef name_ct = CFStringCreateWithCStringNoCopy(
-              kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII,
-              kCFAllocatorNull);
-          m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName(
-              (CGFontRef)m_Font.GetPlatformFont(), name_ct);
-          if (name_ct) {
-            CFRelease(name_ct);
-          }
+          SetExtGID(name_glyph, charcode);
         }
       }
       return;
@@ -285,47 +238,19 @@
       }
       m_GlyphIndex[charcode] =
           FXFT_Get_Name_Index(m_Font.GetFace(), (char*)name);
-      CFStringRef name_ct = CFStringCreateWithCStringNoCopy(
-          kCFAllocatorDefault, name, kCFStringEncodingASCII, kCFAllocatorNull);
-      m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName(
-          (CGFontRef)m_Font.GetPlatformFont(), name_ct);
-      if (name_ct) {
-        CFRelease(name_ct);
-      }
+      SetExtGID(name, charcode);
       if (m_GlyphIndex[charcode] == 0) {
         if (FXSYS_strcmp(name, ".notdef") != 0 &&
             FXSYS_strcmp(name, "space") != 0) {
           m_GlyphIndex[charcode] = FXFT_Get_Char_Index(
               m_Font.GetFace(),
               bUnicode ? m_Encoding.m_Unicodes[charcode] : charcode);
-          FX_CHAR name_glyph[256];
-          FXFT_Get_Glyph_Name(m_Font.GetFace(), m_GlyphIndex[charcode],
-                              name_glyph, 256);
-          name_glyph[255] = 0;
-          CFStringRef name_ct = CFStringCreateWithCStringNoCopy(
-              kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII,
-              kCFAllocatorNull);
-          m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName(
-              (CGFontRef)m_Font.GetPlatformFont(), name_ct);
-          if (name_ct) {
-            CFRelease(name_ct);
-          }
+          CalcExtGID(charcode);
         } else {
           m_Encoding.m_Unicodes[charcode] = 0x20;
           m_GlyphIndex[charcode] =
               bUnicode ? FXFT_Get_Char_Index(m_Font.GetFace(), 0x20) : 0xffff;
-          FX_CHAR name_glyph[256];
-          FXFT_Get_Glyph_Name(m_Font.GetFace(), m_GlyphIndex[charcode],
-                              name_glyph, 256);
-          name_glyph[255] = 0;
-          CFStringRef name_ct = CFStringCreateWithCStringNoCopy(
-              kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII,
-              kCFAllocatorNull);
-          m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName(
-              (CGFontRef)m_Font.GetPlatformFont(), name_ct);
-          if (name_ct) {
-            CFRelease(name_ct);
-          }
+          CalcExtGID(charcode);
         }
       }
     }
@@ -396,3 +321,22 @@
     FXSYS_memcpy(m_ExtGID, m_GlyphIndex, 256);
 #endif
 }
+
+#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
+void CPDF_Type1Font::SetExtGID(const FX_CHAR* name, int charcode) {
+  CFStringRef name_ct = CFStringCreateWithCStringNoCopy(
+      kCFAllocatorDefault, name, kCFStringEncodingASCII, kCFAllocatorNull);
+  m_ExtGID[charcode] =
+      CGFontGetGlyphWithGlyphName((CGFontRef)m_Font.GetPlatformFont(), name_ct);
+  if (name_ct)
+    CFRelease(name_ct);
+}
+
+void CPDF_Type1Font::CalcExtGID(int charcode) {
+  FX_CHAR name_glyph[256];
+  FXFT_Get_Glyph_Name(m_Font.GetFace(), m_GlyphIndex[charcode], name_glyph,
+                      256);
+  name_glyph[255] = 0;
+  SetExtGID(name_glyph, charcode);
+}
+#endif  // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
diff --git a/core/fpdfapi/font/cpdf_type1font.h b/core/fpdfapi/font/cpdf_type1font.h
index 4dc5ea0..f5f3785 100644
--- a/core/fpdfapi/font/cpdf_type1font.h
+++ b/core/fpdfapi/font/cpdf_type1font.h
@@ -22,13 +22,18 @@
 
   int GetBase14Font() const { return m_Base14Font; }
 
- protected:
+ private:
   // CPDF_Font:
   bool Load() override;
 
   // CPDF_SimpleFont:
   void LoadGlyphMap() override;
 
+#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
+  void SetExtGID(const FX_CHAR* name, int charcode);
+  void CalcExtGID(int charcode);
+#endif
+
   int m_Base14Font;
 };