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