Split out FontRenamer from pdfium_test.cc

Allow possible re-use in other gtest-driven tests.

Change-Id: I3cfd47cf03e156afc2f31e342208736e84266cc1
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/93850
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc
index 16e2ba7..f6e35fe 100644
--- a/samples/pdfium_test.cc
+++ b/samples/pdfium_test.cc
@@ -32,14 +32,13 @@
 #include "public/fpdf_formfill.h"
 #include "public/fpdf_progressive.h"
 #include "public/fpdf_structtree.h"
-#include "public/fpdf_sysfontinfo.h"
 #include "public/fpdf_text.h"
 #include "public/fpdfview.h"
 #include "samples/pdfium_test_dump_helper.h"
 #include "samples/pdfium_test_event_helper.h"
 #include "samples/pdfium_test_write_helper.h"
+#include "testing/font_renamer.h"
 #include "testing/fx_string_testhelpers.h"
-#include "testing/test_fonts.h"
 #include "testing/test_loader.h"
 #include "testing/utils/file_util.h"
 #include "testing/utils/hash.h"
@@ -213,87 +212,6 @@
   return options.font_directory.c_str();
 }
 
-class FontRenamer final : public FPDF_SYSFONTINFO {
- public:
-  FontRenamer() : impl_(FPDF_GetDefaultSystemFontInfo()) {
-    version = 1;
-    Release = FontRenamer::ReleaseImpl;
-    EnumFonts = FontRenamer::EnumFontsImpl;
-    MapFont = FontRenamer::MapFontImpl;
-    GetFont = FontRenamer::GetFontImpl;
-    GetFontData = FontRenamer::GetFontDataImpl;
-    GetFaceName = FontRenamer::GetFaceNameImpl;
-    GetFontCharset = FontRenamer::GetFontCharsetImpl;
-    DeleteFont = FontRenamer::DeleteFontImpl;
-    FPDF_SetSystemFontInfo(this);
-  }
-
-  ~FontRenamer() { FPDF_FreeDefaultSystemFontInfo(impl_); }
-
- private:
-  static void ReleaseImpl(FPDF_SYSFONTINFO* info) {
-    FPDF_SYSFONTINFO* impl = GetImpl(info);
-    impl->Release(impl);
-  }
-  static void EnumFontsImpl(FPDF_SYSFONTINFO* info, void* mapper) {
-    FPDF_SYSFONTINFO* impl = GetImpl(info);
-    impl->EnumFonts(impl, mapper);
-  }
-
-  static void* MapFontImpl(FPDF_SYSFONTINFO* info,
-                           int weight,
-                           FPDF_BOOL italic,
-                           int charset,
-                           int pitch_family,
-                           const char* face,
-                           FPDF_BOOL* exact) {
-    std::string renamed_face = TestFonts::RenameFont(face);
-    FPDF_SYSFONTINFO* impl = GetImpl(info);
-    return impl->MapFont(impl, weight, italic, charset, pitch_family,
-                         renamed_face.c_str(), exact);
-  }
-
-  static void* GetFontImpl(FPDF_SYSFONTINFO* info, const char* face) {
-    // Any non-null return will do.
-    FPDF_SYSFONTINFO* impl = GetImpl(info);
-    std::string renamed_face = TestFonts::RenameFont(face);
-    return impl->GetFont(impl, renamed_face.c_str());
-  }
-
-  static unsigned long GetFontDataImpl(FPDF_SYSFONTINFO* info,
-                                       void* font,
-                                       unsigned int table,
-                                       unsigned char* buffer,
-                                       unsigned long buf_size) {
-    FPDF_SYSFONTINFO* impl = GetImpl(info);
-    return impl->GetFontData(impl, font, table, buffer, buf_size);
-  }
-
-  static unsigned long GetFaceNameImpl(FPDF_SYSFONTINFO* info,
-                                       void* font,
-                                       char* buffer,
-                                       unsigned long buf_size) {
-    FPDF_SYSFONTINFO* impl = GetImpl(info);
-    return impl->GetFaceName(impl, font, buffer, buf_size);
-  }
-
-  static int GetFontCharsetImpl(FPDF_SYSFONTINFO* info, void* font) {
-    FPDF_SYSFONTINFO* impl = GetImpl(info);
-    return impl->GetFontCharset(impl, font);
-  }
-
-  static void DeleteFontImpl(FPDF_SYSFONTINFO* info, void* font) {
-    FPDF_SYSFONTINFO* impl = GetImpl(info);
-    impl->DeleteFont(impl, font);
-  }
-
-  static FPDF_SYSFONTINFO* GetImpl(FPDF_SYSFONTINFO* info) {
-    return static_cast<FontRenamer*>(info)->impl_;
-  }
-
-  FPDF_SYSFONTINFO* const impl_;
-};
-
 struct FPDF_FORMFILLINFO_PDFiumTest final : public FPDF_FORMFILLINFO {
   // Hold a map of the currently loaded pages in order to avoid them
   // to get loaded twice.
diff --git a/testing/BUILD.gn b/testing/BUILD.gn
index 0a23439..9a01845 100644
--- a/testing/BUILD.gn
+++ b/testing/BUILD.gn
@@ -7,6 +7,8 @@
 source_set("test_support") {
   testonly = true
   sources = [
+    "font_renamer.cpp",
+    "font_renamer.h",
     "fx_string_testhelpers.cpp",
     "fx_string_testhelpers.h",
     "invalid_seekable_read_stream.cpp",
diff --git a/testing/font_renamer.cpp b/testing/font_renamer.cpp
new file mode 100644
index 0000000..181fa16
--- /dev/null
+++ b/testing/font_renamer.cpp
@@ -0,0 +1,91 @@
+// Copyright 2022 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.
+
+#include "testing/font_renamer.h"
+
+#include <string>
+
+#include "testing/test_fonts.h"
+
+namespace {
+
+FPDF_SYSFONTINFO* GetImpl(FPDF_SYSFONTINFO* info) {
+  return static_cast<FontRenamer*>(info)->impl();
+}
+
+void ReleaseImpl(FPDF_SYSFONTINFO* info) {
+  FPDF_SYSFONTINFO* impl = GetImpl(info);
+  impl->Release(impl);
+}
+
+void EnumFontsImpl(FPDF_SYSFONTINFO* info, void* mapper) {
+  FPDF_SYSFONTINFO* impl = GetImpl(info);
+  impl->EnumFonts(impl, mapper);
+}
+
+void* MapFontImpl(FPDF_SYSFONTINFO* info,
+                  int weight,
+                  FPDF_BOOL italic,
+                  int charset,
+                  int pitch_family,
+                  const char* face,
+                  FPDF_BOOL* exact) {
+  std::string renamed_face = TestFonts::RenameFont(face);
+  FPDF_SYSFONTINFO* impl = GetImpl(info);
+  return impl->MapFont(impl, weight, italic, charset, pitch_family,
+                       renamed_face.c_str(), exact);
+}
+
+void* GetFontImpl(FPDF_SYSFONTINFO* info, const char* face) {
+  // Any non-null return will do.
+  FPDF_SYSFONTINFO* impl = GetImpl(info);
+  std::string renamed_face = TestFonts::RenameFont(face);
+  return impl->GetFont(impl, renamed_face.c_str());
+}
+
+unsigned long GetFontDataImpl(FPDF_SYSFONTINFO* info,
+                              void* font,
+                              unsigned int table,
+                              unsigned char* buffer,
+                              unsigned long buf_size) {
+  FPDF_SYSFONTINFO* impl = GetImpl(info);
+  return impl->GetFontData(impl, font, table, buffer, buf_size);
+}
+
+unsigned long GetFaceNameImpl(FPDF_SYSFONTINFO* info,
+                              void* font,
+                              char* buffer,
+                              unsigned long buf_size) {
+  FPDF_SYSFONTINFO* impl = GetImpl(info);
+  return impl->GetFaceName(impl, font, buffer, buf_size);
+}
+
+int GetFontCharsetImpl(FPDF_SYSFONTINFO* info, void* font) {
+  FPDF_SYSFONTINFO* impl = GetImpl(info);
+  return impl->GetFontCharset(impl, font);
+}
+
+void DeleteFontImpl(FPDF_SYSFONTINFO* info, void* font) {
+  FPDF_SYSFONTINFO* impl = GetImpl(info);
+  impl->DeleteFont(impl, font);
+}
+
+}  // namespace
+
+FontRenamer::FontRenamer() : impl_(FPDF_GetDefaultSystemFontInfo()) {
+  version = 1;
+  Release = ReleaseImpl;
+  EnumFonts = EnumFontsImpl;
+  MapFont = MapFontImpl;
+  GetFont = GetFontImpl;
+  GetFontData = GetFontDataImpl;
+  GetFaceName = GetFaceNameImpl;
+  GetFontCharset = GetFontCharsetImpl;
+  DeleteFont = DeleteFontImpl;
+  FPDF_SetSystemFontInfo(this);
+}
+
+FontRenamer::~FontRenamer() {
+  FPDF_FreeDefaultSystemFontInfo(impl_.Release());
+}
diff --git a/testing/font_renamer.h b/testing/font_renamer.h
new file mode 100644
index 0000000..3064858
--- /dev/null
+++ b/testing/font_renamer.h
@@ -0,0 +1,22 @@
+// Copyright 2022 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.
+
+#ifndef TESTING_FONT_RENAMER_H_
+#define TESTING_FONT_RENAMER_H_
+
+#include "core/fxcrt/unowned_ptr.h"
+#include "public/fpdf_sysfontinfo.h"
+
+class FontRenamer final : public FPDF_SYSFONTINFO {
+ public:
+  FontRenamer();
+  ~FontRenamer();
+
+  FPDF_SYSFONTINFO* impl() { return impl_; }
+
+ private:
+  UnownedPtr<FPDF_SYSFONTINFO> impl_;
+};
+
+#endif  // TESTING_FONT_RENAMER_H_