Merge CPWL_FontMap into CBA_FontMap.

CBA_FontMap is the only derived class. This puts all the relevant code
into one class and reduces circular dependencies between
fpdfsdk/formfiller and fpdfsdk/pwl.

Change-Id: I0cd32e19d2fe1b583eafa56a3caad9e30d56d764
Reviewed-on: https://pdfium-review.googlesource.com/c/47156
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 990733e..4834578 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -171,7 +171,6 @@
     "core/fxge",
     "fpdfsdk",
     "fpdfsdk/formfiller",
-    "fpdfsdk/pwl",
     "fxjs",
     "third_party:pdfium_base",
     "third_party:skia_shared",
diff --git a/fpdfsdk/formfiller/BUILD.gn b/fpdfsdk/formfiller/BUILD.gn
index a16ee51..c84922f 100644
--- a/fpdfsdk/formfiller/BUILD.gn
+++ b/fpdfsdk/formfiller/BUILD.gn
@@ -33,6 +33,7 @@
   configs += [ "../../:pdfium_core_config" ]
   deps = [
     "../../:pdfium_public_headers",
+    "../../core/fpdfapi",
     "../../core/fpdfapi/font",
     "../../core/fpdfapi/page",
     "../../core/fpdfapi/parser",
diff --git a/fpdfsdk/formfiller/cba_fontmap.cpp b/fpdfsdk/formfiller/cba_fontmap.cpp
index 2d84d39..841468e 100644
--- a/fpdfsdk/formfiller/cba_fontmap.cpp
+++ b/fpdfsdk/formfiller/cba_fontmap.cpp
@@ -9,27 +9,133 @@
 #include <memory>
 #include <utility>
 
+#include "core/fpdfapi/cpdf_modulemgr.h"
 #include "core/fpdfapi/font/cpdf_font.h"
+#include "core/fpdfapi/font/cpdf_fontencoding.h"
 #include "core/fpdfapi/page/cpdf_page.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
+#include "core/fpdfapi/parser/cpdf_parser.h"
 #include "core/fpdfapi/parser/cpdf_reference.h"
 #include "core/fpdfapi/parser/cpdf_stream.h"
 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
 #include "core/fpdfdoc/cpdf_defaultappearance.h"
 #include "core/fpdfdoc/cpdf_formfield.h"
+#include "core/fpdfdoc/ipvt_fontmap.h"
+#include "core/fxcrt/fx_codepage.h"
 #include "core/fxge/cfx_substfont.h"
+#include "fpdfsdk/cfx_systemhandler.h"
 #include "fpdfsdk/cpdfsdk_annot.h"
+#include "third_party/base/ptr_util.h"
+#include "third_party/base/stl_util.h"
 
 CBA_FontMap::CBA_FontMap(CPDFSDK_Annot* pAnnot,
                          CFX_SystemHandler* pSystemHandler)
-    : CPWL_FontMap(pSystemHandler),
+    : m_pSystemHandler(pSystemHandler),
       m_pDocument(pAnnot->GetPDFPage()->GetDocument()),
       m_pAnnotDict(pAnnot->GetPDFAnnot()->GetAnnotDict()) {
   Initialize();
 }
 
-CBA_FontMap::~CBA_FontMap() {}
+CBA_FontMap::~CBA_FontMap() {
+  Empty();
+}
+
+CPDF_Font* CBA_FontMap::GetPDFFont(int32_t nFontIndex) {
+  if (pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex])
+    return m_Data[nFontIndex]->pFont;
+
+  return nullptr;
+}
+
+ByteString CBA_FontMap::GetPDFFontAlias(int32_t nFontIndex) {
+  if (pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex])
+    return m_Data[nFontIndex]->sFontName;
+
+  return ByteString();
+}
+
+int32_t CBA_FontMap::GetWordFontIndex(uint16_t word,
+                                      int32_t nCharset,
+                                      int32_t nFontIndex) {
+  if (nFontIndex > 0) {
+    if (KnowWord(nFontIndex, word))
+      return nFontIndex;
+  } else {
+    if (const CBA_FontMap_Data* pData = GetFontMapData(0)) {
+      if (nCharset == FX_CHARSET_Default ||
+          pData->nCharset == FX_CHARSET_Symbol || nCharset == pData->nCharset) {
+        if (KnowWord(0, word))
+          return 0;
+      }
+    }
+  }
+
+  int32_t nNewFontIndex =
+      GetFontIndex(GetNativeFontName(nCharset), nCharset, true);
+  if (nNewFontIndex >= 0) {
+    if (KnowWord(nNewFontIndex, word))
+      return nNewFontIndex;
+  }
+  nNewFontIndex = GetFontIndex(CFX_Font::kUniversalDefaultFontName,
+                               FX_CHARSET_Default, false);
+  if (nNewFontIndex >= 0) {
+    if (KnowWord(nNewFontIndex, word))
+      return nNewFontIndex;
+  }
+  return -1;
+}
+
+int32_t CBA_FontMap::CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) {
+  if (!pdfium::IndexInBounds(m_Data, nFontIndex))
+    return -1;
+
+  CBA_FontMap_Data* pData = m_Data[nFontIndex].get();
+  if (!pData || !pData->pFont)
+    return -1;
+
+  if (pData->pFont->IsUnicodeCompatible())
+    return pData->pFont->CharCodeFromUnicode(word);
+
+  return word < 0xFF ? word : -1;
+}
+
+int32_t CBA_FontMap::CharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
+  // to avoid CJK Font to show ASCII
+  if (word < 0x7F)
+    return FX_CHARSET_ANSI;
+
+  // follow the old charset
+  if (nOldCharset != FX_CHARSET_Default)
+    return nOldCharset;
+
+  return CFX_Font::GetCharSetFromUnicode(word);
+}
+
+const CBA_FontMap_Data* CBA_FontMap::GetFontMapData(int32_t nIndex) const {
+  return pdfium::IndexInBounds(m_Data, nIndex) ? m_Data[nIndex].get() : nullptr;
+}
+
+int32_t CBA_FontMap::GetNativeCharset() {
+  return FX_GetCharsetFromCodePage(FXSYS_GetACP());
+}
+
+ByteString CBA_FontMap::GetNativeFontName(int32_t nCharset) {
+  for (const auto& pData : m_NativeFont) {
+    if (pData && pData->nCharset == nCharset)
+      return pData->sFontName;
+  }
+
+  ByteString sNew = GetNativeFont(nCharset);
+  if (sNew.IsEmpty())
+    return ByteString();
+
+  auto pNewData = pdfium::MakeUnique<CBA_FontMap_Native>();
+  pNewData->nCharset = nCharset;
+  pNewData->sFontName = sNew;
+  m_NativeFont.push_back(std::move(pNewData));
+  return sNew;
+}
 
 void CBA_FontMap::Reset() {
   Empty();
@@ -83,11 +189,7 @@
   }
 
   if (nCharset != FX_CHARSET_ANSI)
-    CPWL_FontMap::Initialize();
-}
-
-CPDF_Document* CBA_FontMap::GetDocument() {
-  return m_pDocument.Get();
+    GetFontIndex(CFX_Font::kDefaultAnsiFontName, FX_CHARSET_ANSI, false);
 }
 
 CPDF_Font* CBA_FontMap::FindFontSameCharset(ByteString* sFontAlias,
@@ -95,8 +197,7 @@
   if (m_pAnnotDict->GetStringFor("Subtype") != "Widget")
     return nullptr;
 
-  CPDF_Document* pDocument = GetDocument();
-  const CPDF_Dictionary* pRootDict = pDocument->GetRoot();
+  const CPDF_Dictionary* pRootDict = m_pDocument->GetRoot();
   if (!pRootDict)
     return nullptr;
 
@@ -111,10 +212,6 @@
   return FindResFontSameCharset(pDRDict, sFontAlias, nCharset);
 }
 
-void CBA_FontMap::AddedFont(CPDF_Font* pFont, const ByteString& sFontAlias) {
-  AddFontToAnnotDict(pFont, sFontAlias);
-}
-
 CPDF_Font* CBA_FontMap::FindResFontSameCharset(const CPDF_Dictionary* pResDict,
                                                ByteString* sFontAlias,
                                                int32_t nCharset) {
@@ -125,7 +222,6 @@
   if (!pFonts)
     return nullptr;
 
-  CPDF_Document* pDocument = GetDocument();
   CPDF_Font* pFind = nullptr;
   CPDF_DictionaryLocker locker(pFonts);
   for (const auto& it : locker) {
@@ -139,7 +235,7 @@
     if (pElement->GetStringFor("Type") != "Font")
       continue;
 
-    CPDF_Font* pFont = pDocument->LoadFont(pElement);
+    CPDF_Font* pFont = m_pDocument->LoadFont(pElement);
     if (!pFont)
       continue;
     const CFX_SubstFont* pSubst = pFont->GetSubstFont();
@@ -243,3 +339,141 @@
     pStreamResFontList->SetFor(sAlias, std::move(pObject));
   }
 }
+
+bool CBA_FontMap::KnowWord(int32_t nFontIndex, uint16_t word) {
+  return pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex] &&
+         CharCodeFromUnicode(nFontIndex, word) >= 0;
+}
+
+void CBA_FontMap::Empty() {
+  m_Data.clear();
+  m_NativeFont.clear();
+}
+
+int32_t CBA_FontMap::GetFontIndex(const ByteString& sFontName,
+                                  int32_t nCharset,
+                                  bool bFind) {
+  int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset);
+  if (nFontIndex >= 0)
+    return nFontIndex;
+
+  ByteString sAlias;
+  CPDF_Font* pFont = bFind ? FindFontSameCharset(&sAlias, nCharset) : nullptr;
+  if (!pFont) {
+    ByteString sTemp = sFontName;
+    pFont = AddFontToDocument(m_pDocument.Get(), sTemp, nCharset);
+    sAlias = EncodeFontAlias(sTemp, nCharset);
+  }
+  AddFontToAnnotDict(pFont, sAlias);
+  return AddFontData(pFont, sAlias, nCharset);
+}
+
+int32_t CBA_FontMap::AddFontData(CPDF_Font* pFont,
+                                 const ByteString& sFontAlias,
+                                 int32_t nCharset) {
+  auto pNewData = pdfium::MakeUnique<CBA_FontMap_Data>();
+  pNewData->pFont = pFont;
+  pNewData->sFontName = sFontAlias;
+  pNewData->nCharset = nCharset;
+  m_Data.push_back(std::move(pNewData));
+  return pdfium::CollectionSize<int32_t>(m_Data) - 1;
+}
+
+ByteString CBA_FontMap::EncodeFontAlias(const ByteString& sFontName,
+                                        int32_t nCharset) {
+  return EncodeFontAlias(sFontName) + ByteString::Format("_%02X", nCharset);
+}
+
+ByteString CBA_FontMap::EncodeFontAlias(const ByteString& sFontName) {
+  ByteString sRet = sFontName;
+  sRet.Remove(' ');
+  return sRet;
+}
+
+int32_t CBA_FontMap::FindFont(const ByteString& sFontName, int32_t nCharset) {
+  int32_t i = 0;
+  for (const auto& pData : m_Data) {
+    if (pData &&
+        (nCharset == FX_CHARSET_Default || nCharset == pData->nCharset) &&
+        (sFontName.IsEmpty() || pData->sFontName == sFontName)) {
+      return i;
+    }
+    ++i;
+  }
+  return -1;
+}
+
+ByteString CBA_FontMap::GetNativeFont(int32_t nCharset) {
+  if (nCharset == FX_CHARSET_Default)
+    nCharset = GetNativeCharset();
+
+  ByteString sFontName = CFX_Font::GetDefaultFontNameByCharset(nCharset);
+  if (!m_pSystemHandler->FindNativeTrueTypeFont(sFontName))
+    return ByteString();
+
+  return sFontName;
+}
+
+CPDF_Font* CBA_FontMap::AddFontToDocument(CPDF_Document* pDoc,
+                                          ByteString& sFontName,
+                                          uint8_t nCharset) {
+  if (IsStandardFont(sFontName))
+    return AddStandardFont(pDoc, sFontName);
+
+  return AddSystemFont(pDoc, sFontName, nCharset);
+}
+
+bool CBA_FontMap::IsStandardFont(const ByteString& sFontName) {
+  static const char* const kStandardFontNames[] = {"Courier",
+                                                   "Courier-Bold",
+                                                   "Courier-BoldOblique",
+                                                   "Courier-Oblique",
+                                                   "Helvetica",
+                                                   "Helvetica-Bold",
+                                                   "Helvetica-BoldOblique",
+                                                   "Helvetica-Oblique",
+                                                   "Times-Roman",
+                                                   "Times-Bold",
+                                                   "Times-Italic",
+                                                   "Times-BoldItalic",
+                                                   "Symbol",
+                                                   "ZapfDingbats"};
+  for (const char* name : kStandardFontNames) {
+    if (sFontName == name)
+      return true;
+  }
+
+  return false;
+}
+
+CPDF_Font* CBA_FontMap::AddStandardFont(CPDF_Document* pDoc,
+                                        ByteString& sFontName) {
+  if (!pDoc)
+    return nullptr;
+
+  CPDF_Font* pFont = nullptr;
+
+  if (sFontName == "ZapfDingbats") {
+    pFont = pDoc->AddStandardFont(sFontName.c_str(), nullptr);
+  } else {
+    CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI);
+    pFont = pDoc->AddStandardFont(sFontName.c_str(), &fe);
+  }
+
+  return pFont;
+}
+
+CPDF_Font* CBA_FontMap::AddSystemFont(CPDF_Document* pDoc,
+                                      ByteString& sFontName,
+                                      uint8_t nCharset) {
+  if (!pDoc)
+    return nullptr;
+
+  if (sFontName.IsEmpty())
+    sFontName = GetNativeFont(nCharset);
+  if (nCharset == FX_CHARSET_Default)
+    nCharset = GetNativeCharset();
+
+  return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName,
+                                                      nCharset);
+}
diff --git a/fpdfsdk/formfiller/cba_fontmap.h b/fpdfsdk/formfiller/cba_fontmap.h
index aa41254..bd14c31 100644
--- a/fpdfsdk/formfiller/cba_fontmap.h
+++ b/fpdfsdk/formfiller/cba_fontmap.h
@@ -7,28 +7,55 @@
 #ifndef FPDFSDK_FORMFILLER_CBA_FONTMAP_H_
 #define FPDFSDK_FORMFILLER_CBA_FONTMAP_H_
 
+#include <memory>
+#include <vector>
+
+#include "core/fpdfdoc/ipvt_fontmap.h"
+#include "core/fxcrt/fx_codepage.h"
 #include "core/fxcrt/unowned_ptr.h"
-#include "fpdfsdk/pwl/cpwl_font_map.h"
+#include "public/fpdf_sysfontinfo.h"
 
-class CPDF_Dictionary;
+class CFX_SystemHandler;
 class CPDFSDK_Annot;
+class CPDF_Dictionary;
+class CPDF_Document;
 
-class CBA_FontMap final : public CPWL_FontMap {
+struct CBA_FontMap_Data {
+  CPDF_Font* pFont;
+  int32_t nCharset;
+  ByteString sFontName;
+};
+
+struct CBA_FontMap_Native {
+  int32_t nCharset;
+  ByteString sFontName;
+};
+
+class CBA_FontMap final : public IPVT_FontMap {
  public:
   CBA_FontMap(CPDFSDK_Annot* pAnnot, CFX_SystemHandler* pSystemHandler);
   ~CBA_FontMap() override;
 
+  // IPVT_FontMap
+  CPDF_Font* GetPDFFont(int32_t nFontIndex) override;
+  ByteString GetPDFFontAlias(int32_t nFontIndex) override;
+  int32_t GetWordFontIndex(uint16_t word,
+                           int32_t nCharset,
+                           int32_t nFontIndex) override;
+  int32_t CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) override;
+  int32_t CharSetFromUnicode(uint16_t word, int32_t nOldCharset) override;
+
+  const CBA_FontMap_Data* GetFontMapData(int32_t nIndex) const;
+  static int32_t GetNativeCharset();
+  ByteString GetNativeFontName(int32_t nCharset);
+
   void Reset();
   void SetDefaultFont(CPDF_Font* pFont, const ByteString& sFontName);
   void SetAPType(const ByteString& sAPType);
 
  private:
-  // CPWL_FontMap:
-  void Initialize() override;
-  CPDF_Document* GetDocument() override;
-  CPDF_Font* FindFontSameCharset(ByteString* sFontAlias,
-                                 int32_t nCharset) override;
-  void AddedFont(CPDF_Font* pFont, const ByteString& sFontAlias) override;
+  void Initialize();
+  CPDF_Font* FindFontSameCharset(ByteString* sFontAlias, int32_t nCharset);
 
   CPDF_Font* FindResFontSameCharset(const CPDF_Dictionary* pResDict,
                                     ByteString* sFontAlias,
@@ -36,6 +63,33 @@
   CPDF_Font* GetAnnotDefaultFont(ByteString* csNameTag);
   void AddFontToAnnotDict(CPDF_Font* pFont, const ByteString& sAlias);
 
+  bool KnowWord(int32_t nFontIndex, uint16_t word);
+
+  void Empty();
+  int32_t GetFontIndex(const ByteString& sFontName,
+                       int32_t nCharset,
+                       bool bFind);
+  int32_t AddFontData(CPDF_Font* pFont,
+                      const ByteString& sFontAlias,
+                      int32_t nCharset);
+
+  ByteString EncodeFontAlias(const ByteString& sFontName, int32_t nCharset);
+  ByteString EncodeFontAlias(const ByteString& sFontName);
+
+  int32_t FindFont(const ByteString& sFontName, int32_t nCharset);
+  ByteString GetNativeFont(int32_t nCharset);
+  CPDF_Font* AddFontToDocument(CPDF_Document* pDoc,
+                               ByteString& sFontName,
+                               uint8_t nCharset);
+  bool IsStandardFont(const ByteString& sFontName);
+  CPDF_Font* AddStandardFont(CPDF_Document* pDoc, ByteString& sFontName);
+  CPDF_Font* AddSystemFont(CPDF_Document* pDoc,
+                           ByteString& sFontName,
+                           uint8_t nCharset);
+
+  std::vector<std::unique_ptr<CBA_FontMap_Data>> m_Data;
+  std::vector<std::unique_ptr<CBA_FontMap_Native>> m_NativeFont;
+  UnownedPtr<CFX_SystemHandler> const m_pSystemHandler;
   UnownedPtr<CPDF_Document> const m_pDocument;
   UnownedPtr<CPDF_Dictionary> const m_pAnnotDict;
   UnownedPtr<CPDF_Font> m_pDefaultFont;
diff --git a/fpdfsdk/fpdf_sysfontinfo.cpp b/fpdfsdk/fpdf_sysfontinfo.cpp
index 0b91df8..a0694a5 100644
--- a/fpdfsdk/fpdf_sysfontinfo.cpp
+++ b/fpdfsdk/fpdf_sysfontinfo.cpp
@@ -16,7 +16,6 @@
 #include "core/fxge/fx_font.h"
 #include "core/fxge/systemfontinfo_iface.h"
 #include "fpdfsdk/cpdfsdk_helpers.h"
-#include "fpdfsdk/pwl/cpwl_font_map.h"
 #include "third_party/base/ptr_util.h"
 
 static_assert(FXFONT_ANSI_CHARSET == FX_CHARSET_ANSI, "Charset must match");
diff --git a/fpdfsdk/pwl/BUILD.gn b/fpdfsdk/pwl/BUILD.gn
index 39c3410..5a5019a 100644
--- a/fpdfsdk/pwl/BUILD.gn
+++ b/fpdfsdk/pwl/BUILD.gn
@@ -22,8 +22,6 @@
     "cpwl_edit_ctrl.h",
     "cpwl_edit_impl.cpp",
     "cpwl_edit_impl.h",
-    "cpwl_font_map.cpp",
-    "cpwl_font_map.h",
     "cpwl_icon.cpp",
     "cpwl_icon.h",
     "cpwl_list_box.cpp",
@@ -44,7 +42,6 @@
   configs += [ "../../:pdfium_core_config" ]
   deps = [
     "../../:pdfium_public_headers",
-    "../../core/fpdfapi",
     "../../core/fpdfapi/font",
     "../../core/fpdfapi/page",
     "../../core/fpdfapi/parser",
diff --git a/fpdfsdk/pwl/cpwl_edit.cpp b/fpdfsdk/pwl/cpwl_edit.cpp
index abaac94..f4ce4e3 100644
--- a/fpdfsdk/pwl/cpwl_edit.cpp
+++ b/fpdfsdk/pwl/cpwl_edit.cpp
@@ -14,6 +14,7 @@
 
 #include "core/fpdfapi/font/cpdf_font.h"
 #include "core/fpdfdoc/cpvt_word.h"
+#include "core/fpdfdoc/ipvt_fontmap.h"
 #include "core/fxcrt/fx_safe_types.h"
 #include "core/fxge/cfx_graphstatedata.h"
 #include "core/fxge/cfx_pathdata.h"
@@ -22,7 +23,6 @@
 #include "fpdfsdk/pwl/cpwl_caret.h"
 #include "fpdfsdk/pwl/cpwl_edit_ctrl.h"
 #include "fpdfsdk/pwl/cpwl_edit_impl.h"
-#include "fpdfsdk/pwl/cpwl_font_map.h"
 #include "fpdfsdk/pwl/cpwl_scroll_bar.h"
 #include "fpdfsdk/pwl/cpwl_wnd.h"
 #include "public/fpdf_fwlevent.h"
diff --git a/fpdfsdk/pwl/cpwl_edit_ctrl.cpp b/fpdfsdk/pwl/cpwl_edit_ctrl.cpp
index af74213..abda919 100644
--- a/fpdfsdk/pwl/cpwl_edit_ctrl.cpp
+++ b/fpdfsdk/pwl/cpwl_edit_ctrl.cpp
@@ -12,7 +12,6 @@
 #include "core/fxge/fx_font.h"
 #include "fpdfsdk/pwl/cpwl_caret.h"
 #include "fpdfsdk/pwl/cpwl_edit_impl.h"
-#include "fpdfsdk/pwl/cpwl_font_map.h"
 #include "fpdfsdk/pwl/cpwl_scroll_bar.h"
 #include "fpdfsdk/pwl/cpwl_wnd.h"
 #include "public/fpdf_fwlevent.h"
diff --git a/fpdfsdk/pwl/cpwl_font_map.cpp b/fpdfsdk/pwl/cpwl_font_map.cpp
deleted file mode 100644
index 532d210..0000000
--- a/fpdfsdk/pwl/cpwl_font_map.cpp
+++ /dev/null
@@ -1,272 +0,0 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "fpdfsdk/pwl/cpwl_font_map.h"
-
-#include <utility>
-
-#include "core/fpdfapi/cpdf_modulemgr.h"
-#include "core/fpdfapi/font/cpdf_font.h"
-#include "core/fpdfapi/font/cpdf_fontencoding.h"
-#include "core/fpdfapi/parser/cpdf_document.h"
-#include "core/fpdfapi/parser/cpdf_parser.h"
-#include "core/fpdfdoc/ipvt_fontmap.h"
-#include "core/fxcrt/fx_codepage.h"
-#include "fpdfsdk/pwl/cpwl_wnd.h"
-#include "third_party/base/ptr_util.h"
-#include "third_party/base/stl_util.h"
-
-namespace {
-
-const char* const g_sDEStandardFontName[] = {"Courier",
-                                             "Courier-Bold",
-                                             "Courier-BoldOblique",
-                                             "Courier-Oblique",
-                                             "Helvetica",
-                                             "Helvetica-Bold",
-                                             "Helvetica-BoldOblique",
-                                             "Helvetica-Oblique",
-                                             "Times-Roman",
-                                             "Times-Bold",
-                                             "Times-Italic",
-                                             "Times-BoldItalic",
-                                             "Symbol",
-                                             "ZapfDingbats"};
-
-}  // namespace
-
-CPWL_FontMap::CPWL_FontMap(CFX_SystemHandler* pSystemHandler)
-    : m_pSystemHandler(pSystemHandler) {
-  ASSERT(m_pSystemHandler);
-}
-
-CPWL_FontMap::~CPWL_FontMap() {
-  Empty();
-}
-
-CPDF_Font* CPWL_FontMap::GetPDFFont(int32_t nFontIndex) {
-  if (pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex])
-    return m_Data[nFontIndex]->pFont;
-
-  return nullptr;
-}
-
-ByteString CPWL_FontMap::GetPDFFontAlias(int32_t nFontIndex) {
-  if (pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex])
-    return m_Data[nFontIndex]->sFontName;
-
-  return ByteString();
-}
-
-bool CPWL_FontMap::KnowWord(int32_t nFontIndex, uint16_t word) {
-  return pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex] &&
-         CharCodeFromUnicode(nFontIndex, word) >= 0;
-}
-
-int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word,
-                                       int32_t nCharset,
-                                       int32_t nFontIndex) {
-  if (nFontIndex > 0) {
-    if (KnowWord(nFontIndex, word))
-      return nFontIndex;
-  } else {
-    if (const CPWL_FontMap_Data* pData = GetFontMapData(0)) {
-      if (nCharset == FX_CHARSET_Default ||
-          pData->nCharset == FX_CHARSET_Symbol || nCharset == pData->nCharset) {
-        if (KnowWord(0, word))
-          return 0;
-      }
-    }
-  }
-
-  int32_t nNewFontIndex =
-      GetFontIndex(GetNativeFontName(nCharset), nCharset, true);
-  if (nNewFontIndex >= 0) {
-    if (KnowWord(nNewFontIndex, word))
-      return nNewFontIndex;
-  }
-  nNewFontIndex = GetFontIndex(CFX_Font::kUniversalDefaultFontName,
-                               FX_CHARSET_Default, false);
-  if (nNewFontIndex >= 0) {
-    if (KnowWord(nNewFontIndex, word))
-      return nNewFontIndex;
-  }
-  return -1;
-}
-
-int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) {
-  if (!pdfium::IndexInBounds(m_Data, nFontIndex))
-    return -1;
-
-  CPWL_FontMap_Data* pData = m_Data[nFontIndex].get();
-  if (!pData || !pData->pFont)
-    return -1;
-
-  if (pData->pFont->IsUnicodeCompatible())
-    return pData->pFont->CharCodeFromUnicode(word);
-
-  return word < 0xFF ? word : -1;
-}
-
-ByteString CPWL_FontMap::GetNativeFontName(int32_t nCharset) {
-  for (const auto& pData : m_NativeFont) {
-    if (pData && pData->nCharset == nCharset)
-      return pData->sFontName;
-  }
-
-  ByteString sNew = GetNativeFont(nCharset);
-  if (sNew.IsEmpty())
-    return ByteString();
-
-  auto pNewData = pdfium::MakeUnique<CPWL_FontMap_Native>();
-  pNewData->nCharset = nCharset;
-  pNewData->sFontName = sNew;
-  m_NativeFont.push_back(std::move(pNewData));
-  return sNew;
-}
-
-void CPWL_FontMap::Empty() {
-  m_Data.clear();
-  m_NativeFont.clear();
-}
-
-void CPWL_FontMap::Initialize() {
-  GetFontIndex(CFX_Font::kDefaultAnsiFontName, FX_CHARSET_ANSI, false);
-}
-
-bool CPWL_FontMap::IsStandardFont(const ByteString& sFontName) {
-  for (const char* name : g_sDEStandardFontName) {
-    if (sFontName == name)
-      return true;
-  }
-
-  return false;
-}
-
-int32_t CPWL_FontMap::FindFont(const ByteString& sFontName, int32_t nCharset) {
-  int32_t i = 0;
-  for (const auto& pData : m_Data) {
-    if (pData &&
-        (nCharset == FX_CHARSET_Default || nCharset == pData->nCharset) &&
-        (sFontName.IsEmpty() || pData->sFontName == sFontName)) {
-      return i;
-    }
-    ++i;
-  }
-  return -1;
-}
-
-int32_t CPWL_FontMap::GetFontIndex(const ByteString& sFontName,
-                                   int32_t nCharset,
-                                   bool bFind) {
-  int32_t nFontIndex = FindFont(EncodeFontAlias(sFontName, nCharset), nCharset);
-  if (nFontIndex >= 0)
-    return nFontIndex;
-
-  ByteString sAlias;
-  CPDF_Font* pFont = bFind ? FindFontSameCharset(&sAlias, nCharset) : nullptr;
-  if (!pFont) {
-    ByteString sTemp = sFontName;
-    pFont = AddFontToDocument(GetDocument(), sTemp, nCharset);
-    sAlias = EncodeFontAlias(sTemp, nCharset);
-  }
-  AddedFont(pFont, sAlias);
-  return AddFontData(pFont, sAlias, nCharset);
-}
-
-int32_t CPWL_FontMap::AddFontData(CPDF_Font* pFont,
-                                  const ByteString& sFontAlias,
-                                  int32_t nCharset) {
-  auto pNewData = pdfium::MakeUnique<CPWL_FontMap_Data>();
-  pNewData->pFont = pFont;
-  pNewData->sFontName = sFontAlias;
-  pNewData->nCharset = nCharset;
-  m_Data.push_back(std::move(pNewData));
-  return pdfium::CollectionSize<int32_t>(m_Data) - 1;
-}
-
-ByteString CPWL_FontMap::GetNativeFont(int32_t nCharset) {
-  if (nCharset == FX_CHARSET_Default)
-    nCharset = GetNativeCharset();
-
-  ByteString sFontName = CFX_Font::GetDefaultFontNameByCharset(nCharset);
-  if (!m_pSystemHandler->FindNativeTrueTypeFont(sFontName))
-    return ByteString();
-
-  return sFontName;
-}
-
-CPDF_Font* CPWL_FontMap::AddFontToDocument(CPDF_Document* pDoc,
-                                           ByteString& sFontName,
-                                           uint8_t nCharset) {
-  if (IsStandardFont(sFontName))
-    return AddStandardFont(pDoc, sFontName);
-
-  return AddSystemFont(pDoc, sFontName, nCharset);
-}
-
-CPDF_Font* CPWL_FontMap::AddStandardFont(CPDF_Document* pDoc,
-                                         ByteString& sFontName) {
-  if (!pDoc)
-    return nullptr;
-
-  CPDF_Font* pFont = nullptr;
-
-  if (sFontName == "ZapfDingbats") {
-    pFont = pDoc->AddStandardFont(sFontName.c_str(), nullptr);
-  } else {
-    CPDF_FontEncoding fe(PDFFONT_ENCODING_WINANSI);
-    pFont = pDoc->AddStandardFont(sFontName.c_str(), &fe);
-  }
-
-  return pFont;
-}
-
-CPDF_Font* CPWL_FontMap::AddSystemFont(CPDF_Document* pDoc,
-                                       ByteString& sFontName,
-                                       uint8_t nCharset) {
-  if (!pDoc)
-    return nullptr;
-
-  if (sFontName.IsEmpty())
-    sFontName = GetNativeFont(nCharset);
-  if (nCharset == FX_CHARSET_Default)
-    nCharset = GetNativeCharset();
-
-  return m_pSystemHandler->AddNativeTrueTypeFontToPDF(pDoc, sFontName,
-                                                      nCharset);
-}
-
-ByteString CPWL_FontMap::EncodeFontAlias(const ByteString& sFontName,
-                                         int32_t nCharset) {
-  return EncodeFontAlias(sFontName) + ByteString::Format("_%02X", nCharset);
-}
-
-ByteString CPWL_FontMap::EncodeFontAlias(const ByteString& sFontName) {
-  ByteString sRet = sFontName;
-  sRet.Remove(' ');
-  return sRet;
-}
-
-const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const {
-  return pdfium::IndexInBounds(m_Data, nIndex) ? m_Data[nIndex].get() : nullptr;
-}
-
-int32_t CPWL_FontMap::GetNativeCharset() {
-  return FX_GetCharsetFromCodePage(FXSYS_GetACP());
-}
-
-int32_t CPWL_FontMap::CharSetFromUnicode(uint16_t word, int32_t nOldCharset) {
-  // to avoid CJK Font to show ASCII
-  if (word < 0x7F)
-    return FX_CHARSET_ANSI;
-
-  // follow the old charset
-  if (nOldCharset != FX_CHARSET_Default)
-    return nOldCharset;
-
-  return CFX_Font::GetCharSetFromUnicode(word);
-}
diff --git a/fpdfsdk/pwl/cpwl_font_map.h b/fpdfsdk/pwl/cpwl_font_map.h
deleted file mode 100644
index c9c7fff..0000000
--- a/fpdfsdk/pwl/cpwl_font_map.h
+++ /dev/null
@@ -1,87 +0,0 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef FPDFSDK_PWL_CPWL_FONT_MAP_H_
-#define FPDFSDK_PWL_CPWL_FONT_MAP_H_
-
-#include <memory>
-#include <vector>
-
-#include "core/fpdfdoc/ipvt_fontmap.h"
-#include "core/fxcrt/fx_codepage.h"
-#include "public/fpdf_sysfontinfo.h"
-
-class CPDF_Document;
-class CFX_SystemHandler;
-
-struct CPWL_FontMap_Data {
-  CPDF_Font* pFont;
-  int32_t nCharset;
-  ByteString sFontName;
-};
-
-struct CPWL_FontMap_Native {
-  int32_t nCharset;
-  ByteString sFontName;
-};
-
-class CPWL_FontMap : public IPVT_FontMap {
- public:
-  explicit CPWL_FontMap(CFX_SystemHandler* pSystemHandler);
-  ~CPWL_FontMap() override;
-
-  // IPVT_FontMap
-  CPDF_Font* GetPDFFont(int32_t nFontIndex) override;
-  ByteString GetPDFFontAlias(int32_t nFontIndex) override;
-  int32_t GetWordFontIndex(uint16_t word,
-                           int32_t nCharset,
-                           int32_t nFontIndex) override;
-  int32_t CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) override;
-  int32_t CharSetFromUnicode(uint16_t word, int32_t nOldCharset) override;
-
-  const CPWL_FontMap_Data* GetFontMapData(int32_t nIndex) const;
-  static int32_t GetNativeCharset();
-  ByteString GetNativeFontName(int32_t nCharset);
-
- protected:
-  virtual void Initialize();
-  virtual CPDF_Document* GetDocument() = 0;
-  virtual CPDF_Font* FindFontSameCharset(ByteString* sFontAlias,
-                                         int32_t nCharset) = 0;
-  virtual void AddedFont(CPDF_Font* pFont, const ByteString& sFontAlias) = 0;
-
-  bool KnowWord(int32_t nFontIndex, uint16_t word);
-
-  void Empty();
-  int32_t GetFontIndex(const ByteString& sFontName,
-                       int32_t nCharset,
-                       bool bFind);
-  int32_t AddFontData(CPDF_Font* pFont,
-                      const ByteString& sFontAlias,
-                      int32_t nCharset);
-
-  ByteString EncodeFontAlias(const ByteString& sFontName, int32_t nCharset);
-  ByteString EncodeFontAlias(const ByteString& sFontName);
-
-  std::vector<std::unique_ptr<CPWL_FontMap_Data>> m_Data;
-  std::vector<std::unique_ptr<CPWL_FontMap_Native>> m_NativeFont;
-
- private:
-  int32_t FindFont(const ByteString& sFontName, int32_t nCharset);
-  ByteString GetNativeFont(int32_t nCharset);
-  CPDF_Font* AddFontToDocument(CPDF_Document* pDoc,
-                               ByteString& sFontName,
-                               uint8_t nCharset);
-  bool IsStandardFont(const ByteString& sFontName);
-  CPDF_Font* AddStandardFont(CPDF_Document* pDoc, ByteString& sFontName);
-  CPDF_Font* AddSystemFont(CPDF_Document* pDoc,
-                           ByteString& sFontName,
-                           uint8_t nCharset);
-
-  UnownedPtr<CFX_SystemHandler> const m_pSystemHandler;
-};
-
-#endif  // FPDFSDK_PWL_CPWL_FONT_MAP_H_