Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
test_fonts.cpp
Go to the documentation of this file.
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
17namespace {
18
19ByteString 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.
46class SystemFontInfoWrapper : public SystemFontInfoIface {
47 public:
48 explicit SystemFontInfoWrapper(std::unique_ptr<SystemFontInfoIface> impl)
49 : impl_(std::move(impl)) {}
50 ~SystemFontInfoWrapper() override { CHECK(active_fonts_.empty()); }
51
52 bool EnumFontList(CFX_FontMapper* pMapper) override {
53 return impl_->EnumFontList(pMapper);
54 }
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 }
68 void* GetFont(const ByteString& face) override {
69 return impl_->GetFont(RenameFontForTesting(face));
70 }
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 }
76 bool GetFaceName(void* hFont, ByteString* name) override {
77 auto face = RenameFontForTesting(*name);
78 return impl_->GetFaceName(hFont, &face);
79 }
80 bool GetFontCharset(void* hFont, FX_Charset* charset) override {
81 return impl_->GetFontCharset(hFont, charset);
82 }
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
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
103TestFonts::~TestFonts() = default;
104
107 font_mapper->SetSystemFontInfo(std::make_unique<SystemFontInfoWrapper>(
108 font_mapper->TakeSystemFontInfo()));
109}
110
111// static
112std::string TestFonts::RenameFont(const char* face) {
113 ByteString renamed_face = RenameFontForTesting(face);
114 return std::string(renamed_face.c_str());
115}
CFX_FontMapper * GetBuiltinMapper() const
Definition cfx_fontmgr.h:71
static CFX_GEModule * Get()
CFX_FontMgr * GetFontMgr() const
static std::string RenameFont(const char *face)
void InstallFontMapper()
ByteString & operator+=(const char *str)
ByteString & operator=(const char *str)
const char * c_str() const
Definition bytestring.h:76
bool IsEmpty() const
Definition bytestring.h:119
FX_Charset
Definition fx_codepage.h:70
#define PATH_SEPARATOR
#define CHECK(cvref)