Get rid of CFPF_SkiaFont::InitFont().

Initialize via the ctor instead. Also fix refcounting for the class, so
the refcount does not go negative for invalid fonts.

Change-Id: I5cf51ae9051f5fc5e0204193d18f8eb37b34c244
Reviewed-on: https://pdfium-review.googlesource.com/40590
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxge/android/cfpf_skiafont.cpp b/core/fxge/android/cfpf_skiafont.cpp
index 242ddfd..2d9f274 100644
--- a/core/fxge/android/cfpf_skiafont.cpp
+++ b/core/fxge/android/cfpf_skiafont.cpp
@@ -15,7 +15,15 @@
 
 #define FPF_EM_ADJUST(em, a) (em == 0 ? (a) : (a)*1000 / em)
 
-CFPF_SkiaFont::CFPF_SkiaFont() = default;
+CFPF_SkiaFont::CFPF_SkiaFont(CFPF_SkiaFontMgr* pFontMgr,
+                             const CFPF_SkiaPathFont* pFont,
+                             uint32_t dwStyle,
+                             uint8_t uCharset)
+    : m_pFontMgr(pFontMgr),
+      m_pFont(pFont),
+      m_Face(m_pFontMgr->GetFontFace(m_pFont->path(), m_pFont->face_index())),
+      m_dwStyle(dwStyle),
+      m_uCharset(uCharset) {}
 
 CFPF_SkiaFont::~CFPF_SkiaFont() {
   if (m_Face)
@@ -167,23 +175,3 @@
     return 0;
   return pdfium::base::checked_cast<uint32_t>(ulSize);
 }
-
-bool CFPF_SkiaFont::InitFont(CFPF_SkiaFontMgr* pFontMgr,
-                             const CFPF_SkiaPathFont* pFont,
-                             const ByteStringView& bsFamily,
-                             uint32_t dwStyle,
-                             uint8_t uCharset) {
-  if (!pFontMgr || !pFont)
-    return false;
-
-  m_Face = pFontMgr->GetFontFace(pFont->path(), pFont->face_index());
-  if (!m_Face)
-    return false;
-
-  m_dwStyle = dwStyle;
-  m_uCharset = uCharset;
-  m_pFontMgr = pFontMgr;
-  m_pFont = pFont;
-  m_dwRefCount = 1;
-  return true;
-}
diff --git a/core/fxge/android/cfpf_skiafont.h b/core/fxge/android/cfpf_skiafont.h
index d80378e..4e3b02d 100644
--- a/core/fxge/android/cfpf_skiafont.h
+++ b/core/fxge/android/cfpf_skiafont.h
@@ -16,12 +16,16 @@
 
 class CFPF_SkiaFont {
  public:
-  CFPF_SkiaFont();
-  ~CFPF_SkiaFont();
+  CFPF_SkiaFont(CFPF_SkiaFontMgr* pFontMgr,
+                const CFPF_SkiaPathFont* pFont,
+                uint32_t dwStyle,
+                uint8_t uCharset);
 
   void Release();
   CFPF_SkiaFont* Retain();
 
+  bool IsValid() const { return !!m_Face; }
+
   ByteString GetFamilyName();
   ByteString GetPsName();
   uint32_t GetFontStyle() const { return m_dwStyle; }
@@ -36,18 +40,14 @@
   int32_t GetItalicAngle() const;
   uint32_t GetFontData(uint32_t dwTable, uint8_t* pBuffer, uint32_t dwSize);
 
-  bool InitFont(CFPF_SkiaFontMgr* pFontMgr,
-                const CFPF_SkiaPathFont* pFont,
-                const ByteStringView& bsFamily,
-                uint32_t dwStyle,
-                uint8_t uCharset);
-
  private:
-  UnownedPtr<CFPF_SkiaFontMgr> m_pFontMgr;
-  UnownedPtr<const CFPF_SkiaPathFont> m_pFont;
-  FXFT_Face m_Face = nullptr;
-  uint32_t m_dwStyle = 0;
-  uint8_t m_uCharset = 0;
+  ~CFPF_SkiaFont();
+
+  UnownedPtr<CFPF_SkiaFontMgr> const m_pFontMgr;
+  UnownedPtr<const CFPF_SkiaPathFont> const m_pFont;
+  const FXFT_Face m_Face;
+  const uint32_t m_dwStyle;
+  const uint8_t m_uCharset;
   uint32_t m_dwRefCount = 0;
 };
 
diff --git a/core/fxge/android/cfpf_skiafontmgr.cpp b/core/fxge/android/cfpf_skiafontmgr.cpp
index 08b6fc5..37cd451 100644
--- a/core/fxge/android/cfpf_skiafontmgr.cpp
+++ b/core/fxge/android/cfpf_skiafontmgr.cpp
@@ -228,10 +228,9 @@
 CFPF_SkiaFontMgr::CFPF_SkiaFontMgr() = default;
 
 CFPF_SkiaFontMgr::~CFPF_SkiaFontMgr() {
-  for (const auto& pair : m_FamilyFonts) {
-    if (pair.second)
-      pair.second->Release();
-  }
+  for (const auto& pair : m_FamilyFonts)
+    pair.second->Release();
+
   m_FamilyFonts.clear();
   m_FontFaces.clear();
   if (m_FTLibrary)
@@ -257,7 +256,7 @@
                                             uint32_t dwMatch) {
   uint32_t dwHash = FPF_SKIAGetFamilyHash(bsFamilyname, dwStyle, uCharset);
   auto it = m_FamilyFonts.find(dwHash);
-  if (it != m_FamilyFonts.end() && it->second)
+  if (it != m_FamilyFonts.end())
     return it->second->Retain();
 
   uint32_t dwFaceName = FPF_SKIANormalizeFontName(bsFamilyname);
@@ -323,10 +322,12 @@
     }
   }
   if (pBestFont) {
-    CFPF_SkiaFont* pFont = new CFPF_SkiaFont;
-    if (pFont->InitFont(this, pBestFont, bsFamilyname, dwStyle, uCharset)) {
+    CFPF_SkiaFont* pFont =
+        new CFPF_SkiaFont(this, pBestFont, dwStyle, uCharset);
+    pFont->Retain();
+    if (pFont->IsValid()) {
       m_FamilyFonts[dwHash] = pFont;
-      return pFont->Retain();
+      return pFont;
     }
     pFont->Release();
   }