• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "testing/test_fonts.h"
6 
7 #include <memory>
8 #include <set>
9 #include <utility>
10 
11 #include "core/fxge/cfx_fontmapper.h"
12 #include "core/fxge/cfx_fontmgr.h"
13 #include "core/fxge/cfx_gemodule.h"
14 #include "core/fxge/systemfontinfo_iface.h"
15 #include "testing/utils/path_service.h"
16 
17 namespace {
18 
RenameFontForTesting(const ByteString & face)19 ByteString RenameFontForTesting(const ByteString& face) {
20   ByteString result;
21   if (face.Contains("Arial") || face.Contains("Calibri") ||
22       face.Contains("Helvetica")) {
23     // Sans
24     result = "Arimo";
25   } else if (face.IsEmpty() || face.Contains("Times")) {
26     // Serif
27     result = "Tinos";
28   } else if (face.Contains("Courier")) {
29     // Mono
30     result = "Cousine";
31   } else {
32     // Some tests expect the fallback font.
33     return face;
34   }
35 
36   if (face.Contains("Bold"))
37     result += " Bold";
38 
39   if (face.Contains("Italic") || face.Contains("Oblique"))
40     result += " Italic";
41 
42   return result;
43 }
44 
45 // Intercepts font requests and renames font faces to those in test_fonts.
46 class SystemFontInfoWrapper : public SystemFontInfoIface {
47  public:
SystemFontInfoWrapper(std::unique_ptr<SystemFontInfoIface> impl)48   explicit SystemFontInfoWrapper(std::unique_ptr<SystemFontInfoIface> impl)
49       : impl_(std::move(impl)) {}
~SystemFontInfoWrapper()50   ~SystemFontInfoWrapper() override { CHECK(active_fonts_.empty()); }
51 
EnumFontList(CFX_FontMapper * pMapper)52   bool EnumFontList(CFX_FontMapper* pMapper) override {
53     return impl_->EnumFontList(pMapper);
54   }
MapFont(int weight,bool bItalic,FX_Charset charset,int pitch_family,const ByteString & face)55   void* MapFont(int weight,
56                 bool bItalic,
57                 FX_Charset charset,
58                 int pitch_family,
59                 const ByteString& face) override {
60     void* font = impl_->MapFont(weight, bItalic, charset, pitch_family,
61                                 RenameFontForTesting(face));
62     if (font) {
63       bool inserted = active_fonts_.insert(font).second;
64       CHECK(inserted);
65     }
66     return font;
67   }
GetFont(const ByteString & face)68   void* GetFont(const ByteString& face) override {
69     return impl_->GetFont(RenameFontForTesting(face));
70   }
GetFontData(void * hFont,uint32_t table,pdfium::span<uint8_t> buffer)71   size_t GetFontData(void* hFont,
72                      uint32_t table,
73                      pdfium::span<uint8_t> buffer) override {
74     return impl_->GetFontData(hFont, table, buffer);
75   }
GetFaceName(void * hFont,ByteString * name)76   bool GetFaceName(void* hFont, ByteString* name) override {
77     auto face = RenameFontForTesting(*name);
78     return impl_->GetFaceName(hFont, &face);
79   }
GetFontCharset(void * hFont,FX_Charset * charset)80   bool GetFontCharset(void* hFont, FX_Charset* charset) override {
81     return impl_->GetFontCharset(hFont, charset);
82   }
DeleteFont(void * hFont)83   void DeleteFont(void* hFont) override {
84     CHECK(active_fonts_.erase(hFont));
85     impl_->DeleteFont(hFont);
86   }
87 
88  private:
89   std::unique_ptr<SystemFontInfoIface> impl_;
90   std::set<void*> active_fonts_;
91 };
92 
93 }  // namespace
94 
TestFonts()95 TestFonts::TestFonts() {
96   if (!PathService::GetExecutableDir(&font_path_))
97     return;
98   font_path_.push_back(PATH_SEPARATOR);
99   font_path_.append("test_fonts");
100   font_paths_ = std::vector<const char*>{font_path_.c_str(), nullptr};
101 }
102 
103 TestFonts::~TestFonts() = default;
104 
InstallFontMapper()105 void TestFonts::InstallFontMapper() {
106   auto* font_mapper = CFX_GEModule::Get()->GetFontMgr()->GetBuiltinMapper();
107   font_mapper->SetSystemFontInfo(std::make_unique<SystemFontInfoWrapper>(
108       font_mapper->TakeSystemFontInfo()));
109 }
110 
111 // static
RenameFont(const char * face)112 std::string TestFonts::RenameFont(const char* face) {
113   ByteString renamed_face = RenameFontForTesting(face);
114   return std::string(renamed_face.c_str());
115 }
116