Better separate CFX_Font from CFX_Face

The checks in PS#1 show that the items that the face retrieves from
the font are already present, except for a bIsVertical flag. Achieve
better layering between the two classes.

-- Fix missing forward in cfx_fontmgr.h.
-- Comment purpose of CFX_SubstFont.

Change-Id: I3bd730a6a8d3a75ee3cea5c800df61e915ae2c4d
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/144410
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxge/cfx_face.cpp b/core/fxge/cfx_face.cpp
index 30efff7..a1b0020 100644
--- a/core/fxge/cfx_face.cpp
+++ b/core/fxge/cfx_face.cpp
@@ -19,7 +19,6 @@
 #include "core/fxcrt/numerics/safe_conversions.h"
 #include "core/fxcrt/numerics/safe_math.h"
 #include "core/fxcrt/unowned_ptr.h"
-#include "core/fxge/cfx_font.h"
 #include "core/fxge/cfx_fontmgr.h"
 #include "core/fxge/cfx_gemodule.h"
 #include "core/fxge/cfx_glyphbitmap.h"
@@ -483,38 +482,37 @@
 }
 
 std::unique_ptr<CFX_GlyphBitmap> CFX_Face::RenderGlyph(
-    const CFX_Font* font,
     uint32_t glyph_index,
-    bool bFontStyle,
+    bool font_style,
+    bool is_vertical,
     const CFX_Matrix& matrix,
     int dest_width,
-    FontAntiAliasingMode anti_alias) {
+    FontAntiAliasingMode anti_alias,
+    const CFX_SubstFont* subst_font) {
   FT_Matrix ft_matrix;
   ft_matrix.xx = matrix.a / 64 * 65536;
   ft_matrix.xy = matrix.c / 64 * 65536;
   ft_matrix.yx = matrix.b / 64 * 65536;
   ft_matrix.yy = matrix.d / 64 * 65536;
   bool bUseCJKSubFont = false;
-  const CFX_SubstFont* pSubstFont = font->GetSubstFont();
-  if (pSubstFont) {
-    bUseCJKSubFont = pSubstFont->subst_cjk_ && bFontStyle;
+  if (subst_font) {
+    bUseCJKSubFont = subst_font->subst_cjk_ && font_style;
     int angle;
     if (bUseCJKSubFont) {
-      angle = pSubstFont->italic_cjk_ ? -15 : 0;
+      angle = subst_font->italic_cjk_ ? -15 : 0;
     } else {
-      angle = pSubstFont->italic_angle_;
+      angle = subst_font->italic_angle_;
     }
     if (angle) {
       int skew = GetSkewFromAngle(angle);
-      if (font->IsVertical()) {
+      if (is_vertical) {
         ft_matrix.yx += ft_matrix.yy * skew / 100;
       } else {
         ft_matrix.xy -= ft_matrix.xx * skew / 100;
       }
     }
-    if (pSubstFont->IsBuiltInGenericFont()) {
-      font->GetFace()->AdjustVariationParams(glyph_index, dest_width,
-                                             font->GetSubstFont()->weight_);
+    if (subst_font->IsBuiltInGenericFont()) {
+      AdjustVariationParams(glyph_index, dest_width, subst_font->weight_);
     }
   }
 
@@ -542,14 +540,14 @@
   auto* glyph = rec->glyph;
   int weight;
   if (bUseCJKSubFont) {
-    weight = pSubstFont->weight_cjk_;
+    weight = subst_font->weight_cjk_;
   } else {
-    weight = pSubstFont ? pSubstFont->weight_ : 0;
+    weight = subst_font ? subst_font->weight_ : 0;
   }
-  if (pSubstFont && !pSubstFont->IsBuiltInGenericFont() && weight > 400) {
+  if (subst_font && !subst_font->IsBuiltInGenericFont() && weight > 400) {
     uint32_t index = (weight - 400) / 10;
     pdfium::CheckedNumeric<signed long> level =
-        GetWeightLevel(pSubstFont->charset_, index);
+        GetWeightLevel(subst_font->charset_, index);
     if (level.ValueOrDefault(-1) < 0) {
       return nullptr;
     }
diff --git a/core/fxge/cfx_face.h b/core/fxge/cfx_face.h
index 277b6b1..88aa08c 100644
--- a/core/fxge/cfx_face.h
+++ b/core/fxge/cfx_face.h
@@ -30,7 +30,6 @@
 enum class FontEncoding : uint32_t;
 }
 
-class CFX_Font;
 class CFX_GlyphBitmap;
 class CFX_Path;
 class CFX_SubstFont;
@@ -85,12 +84,13 @@
   // TODO(crbug.com/42271048): Can this method be private?
   FX_RECT GetGlyphBBox() const;
   std::optional<FX_RECT> GetFontGlyphBBox(uint32_t glyph_index);
-  std::unique_ptr<CFX_GlyphBitmap> RenderGlyph(const CFX_Font* font,
-                                               uint32_t glyph_index,
-                                               bool bFontStyle,
+  std::unique_ptr<CFX_GlyphBitmap> RenderGlyph(uint32_t glyph_index,
+                                               bool font_style,
+                                               bool is_vertical,
                                                const CFX_Matrix& matrix,
                                                int dest_width,
-                                               FontAntiAliasingMode anti_alias);
+                                               FontAntiAliasingMode anti_alias,
+                                               const CFX_SubstFont* subst_font);
   std::unique_ptr<CFX_Path> LoadGlyphPath(uint32_t glyph_index,
                                           int dest_width,
                                           bool is_vertical,
diff --git a/core/fxge/cfx_fontmgr.h b/core/fxge/cfx_fontmgr.h
index 900c9ed..93586c8 100644
--- a/core/fxge/cfx_fontmgr.h
+++ b/core/fxge/cfx_fontmgr.h
@@ -28,6 +28,7 @@
 #include "third_party/skia/include/core/SkRefCnt.h"  // nogncheck
 #endif
 
+class CFX_Font;
 class CFX_FontMapper;
 class CFX_GlyphCache;
 
diff --git a/core/fxge/cfx_glyphcache.cpp b/core/fxge/cfx_glyphcache.cpp
index 6e62915..24bb2bb 100644
--- a/core/fxge/cfx_glyphcache.cpp
+++ b/core/fxge/cfx_glyphcache.cpp
@@ -103,18 +103,18 @@
 CFX_GlyphCache::~CFX_GlyphCache() = default;
 
 std::unique_ptr<CFX_GlyphBitmap> CFX_GlyphCache::RenderGlyph(
-    const CFX_Font* font,
     uint32_t glyph_index,
-    bool bFontStyle,
+    bool font_style,
+    bool is_vertical,
     const CFX_Matrix& matrix,
     int dest_width,
-    FontAntiAliasingMode anti_alias) {
+    FontAntiAliasingMode anti_alias,
+    const CFX_SubstFont* subst_font) {
   if (!face_) {
     return nullptr;
   }
-
-  return face_->RenderGlyph(font, glyph_index, bFontStyle, matrix, dest_width,
-                            anti_alias);
+  return face_->RenderGlyph(glyph_index, font_style, is_vertical, matrix,
+                            dest_width, anti_alias, subst_font);
 }
 
 const CFX_Path* CFX_GlyphCache::LoadGlyphPath(const CFX_Font* font,
@@ -230,8 +230,9 @@
     return it2->second.get();
   }
 
-  std::unique_ptr<CFX_GlyphBitmap> pGlyphBitmap = RenderGlyph(
-      font, glyph_index, bFontStyle, matrix, dest_width, anti_alias);
+  std::unique_ptr<CFX_GlyphBitmap> pGlyphBitmap =
+      RenderGlyph(glyph_index, bFontStyle, font->IsVertical(), matrix,
+                  dest_width, anti_alias, font->GetSubstFont());
   CFX_GlyphBitmap* pResult = pGlyphBitmap.get();
   (*pSizeCache)[glyph_index] = std::move(pGlyphBitmap);
   return pResult;
diff --git a/core/fxge/cfx_glyphcache.h b/core/fxge/cfx_glyphcache.h
index 174c99e..158d202 100644
--- a/core/fxge/cfx_glyphcache.h
+++ b/core/fxge/cfx_glyphcache.h
@@ -53,12 +53,13 @@
   // <glyph_index, dest_width, weight>
   using WidthMapKey = std::tuple<uint32_t, int, int>;
 
-  std::unique_ptr<CFX_GlyphBitmap> RenderGlyph(const CFX_Font* font,
-                                               uint32_t glyph_index,
-                                               bool bFontStyle,
+  std::unique_ptr<CFX_GlyphBitmap> RenderGlyph(uint32_t glyph_index,
+                                               bool font_style,
+                                               bool is_vertical,
                                                const CFX_Matrix& matrix,
                                                int dest_width,
-                                               FontAntiAliasingMode anti_alias);
+                                               FontAntiAliasingMode anti_alias,
+                                               const CFX_SubstFont* subst_font);
   CFX_GlyphBitmap* LookUpGlyphBitmap(const CFX_Font* font,
                                      const CFX_Matrix& matrix,
                                      const ByteString& FaceGlyphsKey,
diff --git a/core/fxge/cfx_substfont.h b/core/fxge/cfx_substfont.h
index 24692b8..bda5a38 100644
--- a/core/fxge/cfx_substfont.h
+++ b/core/fxge/cfx_substfont.h
@@ -10,6 +10,8 @@
 #include "core/fxcrt/bytestring.h"
 #include "core/fxcrt/fx_codepage.h"
 
+// Represents variations to apply on top of an existing font/face to
+// convert it to render as if it were an alternative font.
 class CFX_SubstFont {
  public:
   CFX_SubstFont();