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