Fix the TrueType font not rendered correctly issue.

This CL removes the extra check on whether no glyph index is found for
a TrueType font character in CPDF_SimpleFont::GlyphFromCharCode(), so
that whenever CPDF_SimpleFont::GlyphFromCharCode() returns -1, it
indicates a guaranteed failure and we can handle the TrueType font
later on in CPDF_CharPosList::CPDF_CharPosList().

If a TrueType font fails to find glyph index, i.e. when
CPDF_SimpleFont::GlyphFromCharCode() returns 0, we should first try to
find the glyph index from the fallback font. If that fails, switch back
to use the original font.

Bug: pdfium:1388
Change-Id: I8b1935a094f76408e4678723d566914c2bbf0f42
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/60091
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_simplefont.cpp b/core/fpdfapi/font/cpdf_simplefont.cpp
index cff773a..9371678 100644
--- a/core/fpdfapi/font/cpdf_simplefont.cpp
+++ b/core/fpdfapi/font/cpdf_simplefont.cpp
@@ -47,7 +47,7 @@
     return -1;
 
   int index = m_GlyphIndex[charcode];
-  if (index == 0xffff || (index == 0 && IsTrueTypeFont()))
+  if (index == 0xffff)
     return -1;
 
   return index;
diff --git a/core/fpdfapi/render/cpdf_charposlist.cpp b/core/fpdfapi/render/cpdf_charposlist.cpp
index 7fc1e24..13ef124 100644
--- a/core/fpdfapi/render/cpdf_charposlist.cpp
+++ b/core/fpdfapi/render/cpdf_charposlist.cpp
@@ -19,6 +19,7 @@
   m_CharPos.reserve(charCodes.size());
   CPDF_CIDFont* pCIDFont = pFont->AsCIDFont();
   bool bVertWriting = pCIDFont && pCIDFont->IsVertWriting();
+  bool bToUnicode = !!pFont->GetFontDict()->GetStreamFor("ToUnicode");
   for (size_t i = 0; i < charCodes.size(); ++i) {
     uint32_t CharCode = charCodes[i];
     if (CharCode == static_cast<uint32_t>(-1))
@@ -39,19 +40,41 @@
                   ? charpos.m_ExtGID
                   : charpos.m_GlyphIndex;
 #endif
-    CFX_Font* pCurrentFont;
-    if (GlyphID != static_cast<uint32_t>(-1)) {
-      charpos.m_FallbackFontPosition = -1;
-      pCurrentFont = pFont->GetFont();
-    } else {
+    bool bIsInvalidGlyph = GlyphID == static_cast<uint32_t>(-1);
+    bool bIsTrueTypeZeroGlyph = GlyphID == 0 && pFont->IsTrueTypeFont();
+    bool bUseFallbackFont = false;
+    if (bIsInvalidGlyph || bIsTrueTypeZeroGlyph) {
       charpos.m_FallbackFontPosition =
           pFont->FallbackFontFromCharcode(CharCode);
       charpos.m_GlyphIndex = pFont->FallbackGlyphFromCharcode(
           charpos.m_FallbackFontPosition, CharCode);
+      if (bIsTrueTypeZeroGlyph &&
+          charpos.m_GlyphIndex == static_cast<uint32_t>(-1)) {
+        // For a TrueType font character, when finding the glyph from the
+        // fallback font fails, switch back to using the original font.
+
+        // When keyword "ToUnicode" exists in the PDF file, it indicates
+        // a "ToUnicode" mapping file is used to convert from CIDs (which
+        // begins at decimal 0) to Unicode code. (See ToUnicode Mapping File
+        // Tutorial - Adobe
+        // https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/5411.ToUnicode.pdf
+        // and
+        // https://www.freetype.org/freetype2/docs/tutorial/step1.html#section-6)
+        if (bToUnicode)
+          charpos.m_GlyphIndex = 0;
+      } else {
+        bUseFallbackFont = true;
+      }
+    }
+    CFX_Font* pCurrentFont;
+    if (bUseFallbackFont) {
       pCurrentFont = pFont->GetFontFallback(charpos.m_FallbackFontPosition);
 #if defined(OS_MACOSX)
       charpos.m_ExtGID = charpos.m_GlyphIndex;
 #endif
+    } else {
+      pCurrentFont = pFont->GetFont();
+      charpos.m_FallbackFontPosition = -1;
     }
 
     if (!pFont->IsEmbedded() && !pFont->IsCIDFont())
diff --git a/testing/SUPPRESSIONS b/testing/SUPPRESSIONS
index beca1be..2edcc57 100644
--- a/testing/SUPPRESSIONS
+++ b/testing/SUPPRESSIONS
@@ -331,9 +331,6 @@
 # TODO(pdfium:1331): Remove after associated bug is fixed
 bug_1331.in * * *
 
-# TODO(pdfium:1388): Remove after associated bug is fixed
-bug_1388.in * * *
-
 # xfa_specific
 
 # TODO(pdfium:1107): Remove after associated bug is fixed