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
cfx_folderfontinfo_unittest.cpp
Go to the documentation of this file.
1// Copyright 2019 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 "core/fxge/cfx_folderfontinfo.h"
6
7#include <utility>
8
9#include "core/fxcrt/fx_codepage.h"
10#include "core/fxge/fx_font.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace {
14
15constexpr char kArial[] = "Arial";
16constexpr char kCourierNew[] = "Courier New";
17constexpr char kTimesNewRoman[] = "TimesNewRoman";
18constexpr char kSymbol[] = "Symbol";
19constexpr char kBookshelfSymbol7[] = "Bookshelf Symbol 7";
20constexpr char kCalibri[] = "Calibri";
21constexpr char kBookshelf[] = "Bookshelf";
22constexpr char kBook[] = "Book";
23constexpr char kTofuBold[] = "Tofu, Bold Italic";
24constexpr char kTofu[] = "Tofu";
25constexpr char kLatoUltraBold[] = "Lato Ultra-Bold";
26constexpr char kLato[] = "Lato";
27constexpr char kOxygenSansSansBold[] = "Oxygen-Sans Sans-Bold";
28constexpr char kOxygenSans[] = "Oxygen-Sans";
29constexpr char kOxygen[] = "Oxygen";
30constexpr char kComicSansMS[] = "Comic Sans MS";
31
32} // namespace
33
35 public:
37 AddDummyFont(kArial, CHARSET_FLAG_ANSI);
38 AddDummyFont(kCourierNew, CHARSET_FLAG_ANSI);
39 AddDummyFont(kTimesNewRoman, 0);
40 AddDummyFont(kBookshelfSymbol7, CHARSET_FLAG_SYMBOL);
41 AddDummyFont(kSymbol, CHARSET_FLAG_SYMBOL);
42 AddDummyFont(kTofuBold, CHARSET_FLAG_SYMBOL);
43 AddDummyFont(kLatoUltraBold, CHARSET_FLAG_ANSI);
44 AddDummyFont(kOxygenSansSansBold, CHARSET_FLAG_ANSI);
45 AddDummyFont(kComicSansMS, CHARSET_FLAG_ANSI);
46 }
47
48 void* FindFont(int weight,
49 bool bItalic,
50 FX_Charset charset,
51 int pitch_family,
52 const char* family,
53 bool bMatchName) {
54 return font_info_.FindFont(weight, bItalic, charset, pitch_family, family,
55 bMatchName);
56 }
57
58 ByteString GetFaceName(void* font) {
59 return static_cast<CFX_FolderFontInfo::FontFaceInfo*>(font)->m_FaceName;
60 }
61
62 private:
63 void AddDummyFont(const char* font_name, uint32_t charsets) {
64 auto info = std::make_unique<CFX_FolderFontInfo::FontFaceInfo>(
65 /*filePath=*/"", font_name, /*fontTables=*/"",
66 /*fontOffset=*/0, /*fileSize=*/0);
67 info->m_Charsets = charsets;
68 font_info_.m_FontList[font_name] = std::move(info);
69 }
70
71 CFX_FolderFontInfo font_info_;
72};
73
75 // Find "Symbol" font
76 void* font = FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kSymbol,
77 FXFONT_FF_ROMAN, kSymbol, /*bMatchName=*/true);
78 ASSERT_TRUE(font);
79 EXPECT_EQ(GetFaceName(font), kSymbol);
80
81 // Find "Calibri" font that is not present in the installed fonts
82 EXPECT_FALSE(FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kSymbol,
83 FXFONT_FF_ROMAN, kCalibri, /*bMatchName=*/true));
84
85 // Find the closest matching font to "Bookshelf" font that is present in the
86 // installed fonts
87 font = FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kSymbol,
88 FXFONT_FF_ROMAN, kBookshelf, /*bMatchName=*/true);
89 ASSERT_TRUE(font);
90 EXPECT_EQ(GetFaceName(font), kBookshelfSymbol7);
91
92 // Find "Book" font is expected to fail, because none of the installed fonts
93 // is in the same font family.
94 EXPECT_FALSE(FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kSymbol,
95 FXFONT_FF_ROMAN, kBook, /*bMatchName=*/true));
96
97 // Find the closest matching font for "Tofu" in the installed fonts, which
98 // has "," following the string "Tofu".
99 font = FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kSymbol,
100 FXFONT_FF_ROMAN, kTofu, /*bMatchName=*/true);
101 ASSERT_TRUE(font);
102 EXPECT_EQ(GetFaceName(font), kTofuBold);
103
104 // Find the closest matching font for "Lato" in the installed fonts, which
105 // has a space character following the string "Lato".
106 font = FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kANSI,
107 FXFONT_FF_ROMAN, kLato, /*bMatchName=*/true);
108 ASSERT_TRUE(font);
109 EXPECT_EQ(GetFaceName(font), kLatoUltraBold);
110
111 // Find the closest matching font for "Oxygen" in the installed fonts,
112 // which has "-" following the string "Oxygen".
113 font = FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kANSI,
114 FXFONT_FF_ROMAN, kOxygen, /*bMatchName=*/true);
115 ASSERT_TRUE(font);
116 EXPECT_EQ(GetFaceName(font), kOxygenSansSansBold);
117
118 // Find the closest matching font for "Oxygen-Sans" in the installed fonts,
119 // to test matching a family name with "-".
120 font = FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kANSI,
121 FXFONT_FF_ROMAN, kOxygenSans, /*bMatchName=*/true);
122 ASSERT_TRUE(font);
123 EXPECT_EQ(GetFaceName(font), kOxygenSansSansBold);
124
125 // Find "Symbol" font when name matching is false
126 font = FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kSymbol,
127 FXFONT_FF_ROMAN, kSymbol, /*bMatchName=*/false);
128 ASSERT_TRUE(font);
129 EXPECT_EQ(GetFaceName(font), kBookshelfSymbol7);
130
131 font = FindFont(700, false, FX_Charset::kANSI, FXFONT_FF_FIXEDPITCH,
132 kComicSansMS, true);
133 ASSERT_TRUE(font);
134 EXPECT_EQ(GetFaceName(font), kComicSansMS);
135}
#define CHARSET_FLAG_ANSI
#define CHARSET_FLAG_SYMBOL
TEST_F(CFX_FolderFontInfoTest, TestFindFont)
void * FindFont(int weight, bool bItalic, FX_Charset charset, int pitch_family, const char *family, bool bMatchName)
FX_Charset
Definition fx_codepage.h:70
#define FXFONT_FF_ROMAN
Definition fx_font.h:18
#define FXFONT_FF_FIXEDPITCH
Definition fx_font.h:17