Split part of CPDF_TrueTypeFont into SetGlyphIndicesFromFirstChar().

Refactor one chunk of LoadGlyphMap() into SetGlyphIndicesFromFirstChar()
and use STL to simplify the code.

Change-Id: I0db3b6b2d7d2990ea3e81ef36c75b050053faef2
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/91793
Reviewed-by: Nigi <nigi@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_truetypefont.cpp b/core/fpdfapi/font/cpdf_truetypefont.cpp
index 1fbefa3..40222fc 100644
--- a/core/fpdfapi/font/cpdf_truetypefont.cpp
+++ b/core/fpdfapi/font/cpdf_truetypefont.cpp
@@ -6,6 +6,8 @@
 
 #include "core/fpdfapi/font/cpdf_truetypefont.h"
 
+#include <algorithm>
+
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fxge/fx_font.h"
 #include "third_party/base/cxx17_backports.h"
@@ -53,16 +55,7 @@
       FontStyleIsNonSymbolic(m_Flags)) {
     if (!FXFT_Has_Glyph_Names(face) &&
         (!face->num_charmaps || !face->charmaps)) {
-      int nStartChar = m_pFontDict->GetIntegerFor("FirstChar");
-      if (nStartChar < 0 || nStartChar > 255)
-        return;
-
-      int charcode = 0;
-      for (; charcode < nStartChar; charcode++)
-        m_GlyphIndex[charcode] = 0;
-      uint16_t nGlyph = charcode - nStartChar + 3;
-      for (; charcode < 256; charcode++, nGlyph++)
-        m_GlyphIndex[charcode] = nGlyph;
+      SetGlyphIndicesFromFirstChar();
       return;
     }
     bool bMSUnicode = FT_UseTTCharmap(face, 3, 1);
@@ -227,3 +220,15 @@
     return support_win ? PDFFONT_ENCODING_WINANSI : PDFFONT_ENCODING_BUILTIN;
   return m_BaseEncoding;
 }
+
+void CPDF_TrueTypeFont::SetGlyphIndicesFromFirstChar() {
+  int start_char = m_pFontDict->GetIntegerFor("FirstChar");
+  if (start_char < 0 || start_char > 255)
+    return;
+
+  auto* it = std::begin(m_GlyphIndex);
+  std::fill(it, it + start_char, 0);
+  uint16_t glyph = 3;
+  for (int charcode = start_char; charcode < 256; charcode++, glyph++)
+    m_GlyphIndex[charcode] = glyph;
+}
diff --git a/core/fpdfapi/font/cpdf_truetypefont.h b/core/fpdfapi/font/cpdf_truetypefont.h
index a17a7f0..7dd540b 100644
--- a/core/fpdfapi/font/cpdf_truetypefont.h
+++ b/core/fpdfapi/font/cpdf_truetypefont.h
@@ -30,6 +30,7 @@
   void LoadGlyphMap() override;
 
   int DetermineEncoding() const;
+  void SetGlyphIndicesFromFirstChar();
 };
 
 #endif  // CORE_FPDFAPI_FONT_CPDF_TRUETYPEFONT_H_