xref: /aosp_15_r20/external/pdfium/core/fxge/cfx_folderfontinfo_unittest.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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 
13 namespace {
14 
15 constexpr char kArial[] = "Arial";
16 constexpr char kCourierNew[] = "Courier New";
17 constexpr char kTimesNewRoman[] = "TimesNewRoman";
18 constexpr char kSymbol[] = "Symbol";
19 constexpr char kBookshelfSymbol7[] = "Bookshelf Symbol 7";
20 constexpr char kCalibri[] = "Calibri";
21 constexpr char kBookshelf[] = "Bookshelf";
22 constexpr char kBook[] = "Book";
23 constexpr char kTofuBold[] = "Tofu, Bold Italic";
24 constexpr char kTofu[] = "Tofu";
25 constexpr char kLatoUltraBold[] = "Lato Ultra-Bold";
26 constexpr char kLato[] = "Lato";
27 constexpr char kOxygenSansSansBold[] = "Oxygen-Sans Sans-Bold";
28 constexpr char kOxygenSans[] = "Oxygen-Sans";
29 constexpr char kOxygen[] = "Oxygen";
30 constexpr char kComicSansMS[] = "Comic Sans MS";
31 
32 }  // namespace
33 
34 class CFX_FolderFontInfoTest : public ::testing::Test {
35  public:
CFX_FolderFontInfoTest()36   CFX_FolderFontInfoTest() {
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 
FindFont(int weight,bool bItalic,FX_Charset charset,int pitch_family,const char * family,bool bMatchName)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 
GetFaceName(void * font)58   ByteString GetFaceName(void* font) {
59     return static_cast<CFX_FolderFontInfo::FontFaceInfo*>(font)->m_FaceName;
60   }
61 
62  private:
AddDummyFont(const char * font_name,uint32_t charsets)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 
TEST_F(CFX_FolderFontInfoTest,TestFindFont)74 TEST_F(CFX_FolderFontInfoTest, TestFindFont) {
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 }
136