Tidy CFX_CTTGSUBTable

-- remove in-out parameters.
-- initialize in header.
-- improve constructors to make some members const.

Change-Id: I083d5e3c3c3dba60684157e6883474b8694fca3d
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/77110
Reviewed-by: Hui Yingst <nigi@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/font/cfx_cttgsubtable.cpp b/core/fpdfapi/font/cfx_cttgsubtable.cpp
index 7932e7a..4631582 100644
--- a/core/fpdfapi/font/cfx_cttgsubtable.cpp
+++ b/core/fpdfapi/font/cfx_cttgsubtable.cpp
@@ -239,129 +239,118 @@
     return;
 
   for (auto& subTable : rec->SubTables)
-    ParseSingleSubst(&raw[GetUInt16(sp)], &subTable);
+    subTable = ParseSingleSubst(&raw[GetUInt16(sp)]);
 }
 
 std::unique_ptr<CFX_CTTGSUBTable::TCoverageFormatBase>
 CFX_CTTGSUBTable::ParseCoverage(FT_Bytes raw) {
   FT_Bytes sp = raw;
   uint16_t format = GetUInt16(sp);
-  if (format == 1) {
-    auto rec = std::make_unique<TCoverageFormat1>();
-    ParseCoverageFormat1(raw, rec.get());
-    return std::move(rec);
-  }
-  if (format == 2) {
-    auto rec = std::make_unique<TCoverageFormat2>();
-    ParseCoverageFormat2(raw, rec.get());
-    return std::move(rec);
-  }
+  if (format == 1)
+    return ParseCoverageFormat1(raw);
+  if (format == 2)
+    return ParseCoverageFormat2(raw);
   return nullptr;
 }
 
-void CFX_CTTGSUBTable::ParseCoverageFormat1(FT_Bytes raw,
-                                            TCoverageFormat1* rec) {
+std::unique_ptr<CFX_CTTGSUBTable::TCoverageFormat1>
+CFX_CTTGSUBTable::ParseCoverageFormat1(FT_Bytes raw) {
   FT_Bytes sp = raw;
   (void)GetUInt16(sp);
-  rec->GlyphArray =
-      std::vector<uint16_t, FxAllocAllocator<uint16_t>>(GetUInt16(sp));
+  auto rec = std::make_unique<TCoverageFormat1>(GetUInt16(sp));
   for (auto& glyph : rec->GlyphArray)
     glyph = GetUInt16(sp);
+  return rec;
 }
 
-void CFX_CTTGSUBTable::ParseCoverageFormat2(FT_Bytes raw,
-                                            TCoverageFormat2* rec) {
+std::unique_ptr<CFX_CTTGSUBTable::TCoverageFormat2>
+CFX_CTTGSUBTable::ParseCoverageFormat2(FT_Bytes raw) {
   FT_Bytes sp = raw;
   (void)GetUInt16(sp);
-  rec->RangeRecords = std::vector<TRangeRecord>(GetUInt16(sp));
+  auto rec = std::make_unique<TCoverageFormat2>(GetUInt16(sp));
   for (auto& rangeRec : rec->RangeRecords) {
     rangeRec.Start = GetUInt16(sp);
     rangeRec.End = GetUInt16(sp);
     rangeRec.StartCoverageIndex = GetUInt16(sp);
   }
+  return rec;
 }
 
-void CFX_CTTGSUBTable::ParseSingleSubst(FT_Bytes raw,
-                                        std::unique_ptr<TSubTableBase>* rec) {
+std::unique_ptr<CFX_CTTGSUBTable::TSubTableBase>
+CFX_CTTGSUBTable::ParseSingleSubst(FT_Bytes raw) {
   FT_Bytes sp = raw;
-  uint16_t Format = GetUInt16(sp);
-  switch (Format) {
-    case 1:
-      *rec = std::make_unique<TSubTable1>();
-      ParseSingleSubstFormat1(raw, static_cast<TSubTable1*>(rec->get()));
-      break;
-    case 2:
-      *rec = std::make_unique<TSubTable2>();
-      ParseSingleSubstFormat2(raw, static_cast<TSubTable2*>(rec->get()));
-      break;
-  }
+  uint16_t format = GetUInt16(sp);
+  if (format == 1)
+    return ParseSingleSubstFormat1(raw);
+  if (format == 2)
+    return ParseSingleSubstFormat2(raw);
+  return nullptr;
 }
 
-void CFX_CTTGSUBTable::ParseSingleSubstFormat1(FT_Bytes raw, TSubTable1* rec) {
+std::unique_ptr<CFX_CTTGSUBTable::TSubTable1>
+CFX_CTTGSUBTable::ParseSingleSubstFormat1(FT_Bytes raw) {
   FT_Bytes sp = raw;
   GetUInt16(sp);
   uint16_t offset = GetUInt16(sp);
+  auto rec = std::make_unique<TSubTable1>();
   rec->Coverage = ParseCoverage(&raw[offset]);
   rec->DeltaGlyphID = GetInt16(sp);
+  return rec;
 }
 
-void CFX_CTTGSUBTable::ParseSingleSubstFormat2(FT_Bytes raw, TSubTable2* rec) {
+std::unique_ptr<CFX_CTTGSUBTable::TSubTable2>
+CFX_CTTGSUBTable::ParseSingleSubstFormat2(FT_Bytes raw) {
   FT_Bytes sp = raw;
   (void)GetUInt16(sp);
   uint16_t offset = GetUInt16(sp);
+  auto rec = std::make_unique<TSubTable2>();
   rec->Coverage = ParseCoverage(&raw[offset]);
   rec->Substitutes =
       std::vector<uint16_t, FxAllocAllocator<uint16_t>>(GetUInt16(sp));
   for (auto& substitute : rec->Substitutes)
     substitute = GetUInt16(sp);
+  return rec;
 }
 
-CFX_CTTGSUBTable::TLangSysRecord::TLangSysRecord()
-    : LangSysTag(0), LookupOrder(0), ReqFeatureIndex(0) {}
+CFX_CTTGSUBTable::TLangSysRecord::TLangSysRecord() = default;
 
 CFX_CTTGSUBTable::TLangSysRecord::~TLangSysRecord() = default;
 
-CFX_CTTGSUBTable::TScriptRecord::TScriptRecord()
-    : ScriptTag(0), DefaultLangSys(0) {}
+CFX_CTTGSUBTable::TScriptRecord::TScriptRecord() = default;
 
 CFX_CTTGSUBTable::TScriptRecord::~TScriptRecord() = default;
 
-CFX_CTTGSUBTable::TFeatureRecord::TFeatureRecord()
-    : FeatureTag(0), FeatureParams(0) {}
+CFX_CTTGSUBTable::TFeatureRecord::TFeatureRecord() = default;
 
 CFX_CTTGSUBTable::TFeatureRecord::~TFeatureRecord() = default;
 
-CFX_CTTGSUBTable::TRangeRecord::TRangeRecord()
-    : Start(0), End(0), StartCoverageIndex(0) {}
+CFX_CTTGSUBTable::TRangeRecord::TRangeRecord() = default;
 
-CFX_CTTGSUBTable::TCoverageFormat1::TCoverageFormat1() {
-  CoverageFormat = 1;
-}
+CFX_CTTGSUBTable::TCoverageFormat1::TCoverageFormat1(size_t initial_size)
+    : TCoverageFormatBase(1), GlyphArray(initial_size) {}
 
 CFX_CTTGSUBTable::TCoverageFormat1::~TCoverageFormat1() = default;
 
-CFX_CTTGSUBTable::TCoverageFormat2::TCoverageFormat2() {
-  CoverageFormat = 2;
-}
+CFX_CTTGSUBTable::TCoverageFormat2::TCoverageFormat2(size_t initial_size)
+    : TCoverageFormatBase(2), RangeRecords(initial_size) {}
 
 CFX_CTTGSUBTable::TCoverageFormat2::~TCoverageFormat2() = default;
 
-CFX_CTTGSUBTable::TSubTableBase::TSubTableBase() = default;
+CFX_CTTGSUBTable::TDevice::TDevice() = default;
+
+CFX_CTTGSUBTable::TSubTableBase::TSubTableBase(uint16_t format)
+    : SubstFormat(format) {}
 
 CFX_CTTGSUBTable::TSubTableBase::~TSubTableBase() = default;
 
-CFX_CTTGSUBTable::TSubTable1::TSubTable1() {
-  SubstFormat = 1;
-}
+CFX_CTTGSUBTable::TSubTable1::TSubTable1() : TSubTableBase(1) {}
 
 CFX_CTTGSUBTable::TSubTable1::~TSubTable1() = default;
 
-CFX_CTTGSUBTable::TSubTable2::TSubTable2() {
-  SubstFormat = 2;
-}
+CFX_CTTGSUBTable::TSubTable2::TSubTable2() : TSubTableBase(2) {}
 
 CFX_CTTGSUBTable::TSubTable2::~TSubTable2() = default;
 
-CFX_CTTGSUBTable::TLookup::TLookup() : LookupType(0), LookupFlag(0) {}
+CFX_CTTGSUBTable::TLookup::TLookup() = default;
 
 CFX_CTTGSUBTable::TLookup::~TLookup() = default;
diff --git a/core/fpdfapi/font/cfx_cttgsubtable.h b/core/fpdfapi/font/cfx_cttgsubtable.h
index 8155b6c..fecd262 100644
--- a/core/fpdfapi/font/cfx_cttgsubtable.h
+++ b/core/fpdfapi/font/cfx_cttgsubtable.h
@@ -28,9 +28,9 @@
     TLangSysRecord();
     ~TLangSysRecord();
 
-    uint32_t LangSysTag;
-    uint16_t LookupOrder;
-    uint16_t ReqFeatureIndex;
+    uint32_t LangSysTag = 0;
+    uint16_t LookupOrder = 0;
+    uint16_t ReqFeatureIndex = 0;
     std::vector<uint16_t, FxAllocAllocator<uint16_t>> FeatureIndices;
   };
 
@@ -38,8 +38,8 @@
     TScriptRecord();
     ~TScriptRecord();
 
-    uint32_t ScriptTag;
-    uint16_t DefaultLangSys;
+    uint32_t ScriptTag = 0;
+    uint16_t DefaultLangSys = 0;
     std::vector<TLangSysRecord> LangSysRecords;
   };
 
@@ -47,52 +47,54 @@
     TFeatureRecord();
     ~TFeatureRecord();
 
-    uint32_t FeatureTag;
-    uint16_t FeatureParams;
+    uint32_t FeatureTag = 0;
+    uint16_t FeatureParams = 0;
     std::vector<uint16_t, FxAllocAllocator<uint16_t>> LookupListIndices;
   };
 
   struct TRangeRecord {
     TRangeRecord();
 
-    uint16_t Start;
-    uint16_t End;
-    uint16_t StartCoverageIndex;
+    uint16_t Start = 0;
+    uint16_t End = 0;
+    uint16_t StartCoverageIndex = 0;
   };
 
   struct TCoverageFormatBase {
+    TCoverageFormatBase(uint16_t format) : CoverageFormat(format) {}
     virtual ~TCoverageFormatBase() = default;
-    uint16_t CoverageFormat;
+
+    const uint16_t CoverageFormat;
   };
 
   struct TCoverageFormat1 final : public TCoverageFormatBase {
-    TCoverageFormat1();
+    TCoverageFormat1(size_t initial_size);
     ~TCoverageFormat1() override;
 
     std::vector<uint16_t, FxAllocAllocator<uint16_t>> GlyphArray;
   };
 
   struct TCoverageFormat2 final : public TCoverageFormatBase {
-    TCoverageFormat2();
+    TCoverageFormat2(size_t initial_size);
     ~TCoverageFormat2() override;
 
     std::vector<TRangeRecord> RangeRecords;
   };
 
   struct TDevice {
-    TDevice() : StartSize(0), EndSize(0), DeltaFormat(0) {}
+    TDevice();
 
-    uint16_t StartSize;
-    uint16_t EndSize;
-    uint16_t DeltaFormat;
+    uint16_t StartSize = 0;
+    uint16_t EndSize = 0;
+    uint16_t DeltaFormat = 0;
   };
 
   struct TSubTableBase {
-    TSubTableBase();
+    TSubTableBase(uint16_t format);
     virtual ~TSubTableBase();
 
+    const uint16_t SubstFormat;
     std::unique_ptr<TCoverageFormatBase> Coverage;
-    uint16_t SubstFormat;
   };
 
   struct TSubTable1 final : public TSubTableBase {
@@ -113,8 +115,8 @@
     TLookup();
     ~TLookup();
 
-    uint16_t LookupType;
-    uint16_t LookupFlag;
+    uint16_t LookupType = 0;
+    uint16_t LookupFlag = 0;
     std::vector<std::unique_ptr<TSubTableBase>> SubTables;
   };
 
@@ -128,11 +130,11 @@
   void ParseLookupList(FT_Bytes raw);
   void ParseLookup(FT_Bytes raw, TLookup* rec);
   std::unique_ptr<TCoverageFormatBase> ParseCoverage(FT_Bytes raw);
-  void ParseCoverageFormat1(FT_Bytes raw, TCoverageFormat1* rec);
-  void ParseCoverageFormat2(FT_Bytes raw, TCoverageFormat2* rec);
-  void ParseSingleSubst(FT_Bytes raw, std::unique_ptr<TSubTableBase>* rec);
-  void ParseSingleSubstFormat1(FT_Bytes raw, TSubTable1* rec);
-  void ParseSingleSubstFormat2(FT_Bytes raw, TSubTable2* rec);
+  std::unique_ptr<TCoverageFormat1> ParseCoverageFormat1(FT_Bytes raw);
+  std::unique_ptr<TCoverageFormat2> ParseCoverageFormat2(FT_Bytes raw);
+  std::unique_ptr<TSubTableBase> ParseSingleSubst(FT_Bytes raw);
+  std::unique_ptr<TSubTable1> ParseSingleSubstFormat1(FT_Bytes raw);
+  std::unique_ptr<TSubTable2> ParseSingleSubstFormat2(FT_Bytes raw);
 
   bool GetVerticalGlyphSub(const TFeatureRecord& feature,
                            uint32_t glyphnum,