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
fpdf_sysfontinfo_embeddertest.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 "public/fpdf_sysfontinfo.h"
6
7#include <string>
8#include <vector>
9
10#include "build/build_config.h"
11#include "core/fxcrt/compiler_specific.h"
12#include "testing/embedder_test.h"
13#include "testing/embedder_test_environment.h"
14#include "testing/gmock/include/gmock/gmock.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17namespace {
18
19extern "C" {
20
21void FakeRelease(FPDF_SYSFONTINFO* pThis) {}
22void FakeEnumFonts(FPDF_SYSFONTINFO* pThis, void* pMapper) {}
23
24void* FakeMapFont(FPDF_SYSFONTINFO* pThis,
25 int weight,
26 FPDF_BOOL bItalic,
27 int charset,
28 int pitch_family,
29 const char* face,
30 FPDF_BOOL* bExact) {
31 // Any non-null return will do.
32 return pThis;
33}
34
35void* FakeGetFont(FPDF_SYSFONTINFO* pThis, const char* face) {
36 // Any non-null return will do.
37 return pThis;
38}
39
40unsigned long FakeGetFontData(FPDF_SYSFONTINFO* pThis,
41 void* hFont,
42 unsigned int table,
43 unsigned char* buffer,
44 unsigned long buf_size) {
45 return 0;
46}
47
48unsigned long FakeGetFaceName(FPDF_SYSFONTINFO* pThis,
49 void* hFont,
50 char* buffer,
51 unsigned long buf_size) {
52 return 0;
53}
54
55int FakeGetFontCharset(FPDF_SYSFONTINFO* pThis, void* hFont) {
56 return 1;
57}
58
59void FakeDeleteFont(FPDF_SYSFONTINFO* pThis, void* hFont) {}
60
61} // extern "C"
62
63class FPDFUnavailableSysFontInfoEmbedderTest : public EmbedderTest {
64 public:
65 FPDFUnavailableSysFontInfoEmbedderTest() = default;
66 ~FPDFUnavailableSysFontInfoEmbedderTest() override = default;
67
68 void SetUp() override {
70 font_info_.version = 1;
71 font_info_.Release = FakeRelease;
72 font_info_.EnumFonts = FakeEnumFonts;
73 font_info_.MapFont = FakeMapFont;
74 font_info_.GetFont = FakeGetFont;
75 font_info_.GetFontData = FakeGetFontData;
76 font_info_.GetFaceName = FakeGetFaceName;
77 font_info_.GetFontCharset = FakeGetFontCharset;
78 font_info_.DeleteFont = FakeDeleteFont;
79 FPDF_SetSystemFontInfo(&font_info_);
80 }
81
82 void TearDown() override {
85
86 // Bouncing the library is the only reliable way to fully undo the initial
87 // FPDF_SetSystemFontInfo() call at the moment.
90 }
91
92 FPDF_SYSFONTINFO font_info_;
93};
94
95class FPDFSysFontInfoEmbedderTest : public EmbedderTest {
96 public:
97 FPDFSysFontInfoEmbedderTest() = default;
98 ~FPDFSysFontInfoEmbedderTest() override = default;
99
100 void SetUp() override {
103 ASSERT_TRUE(font_info_);
104 FPDF_SetSystemFontInfo(font_info_);
105 }
106
107 void TearDown() override {
109
110 // After releasing `font_info_` from PDFium, it is safe to free it.
113
114 // Bouncing the library is the only reliable way to fully undo the initial
115 // FPDF_SetSystemFontInfo() call at the moment.
117
119 }
120
121 FPDF_SYSFONTINFO* font_info_;
122};
123
124} // namespace
125
126TEST_F(FPDFUnavailableSysFontInfoEmbedderTest, Bug972518) {
127 ASSERT_TRUE(OpenDocument("bug_972518.pdf"));
128 ASSERT_EQ(1, FPDF_GetPageCount(document()));
129
130 FPDF_PAGE page = LoadPage(0);
131 ASSERT_TRUE(page);
132 UnloadPage(page);
133}
134
135TEST_F(FPDFSysFontInfoEmbedderTest, DefaultSystemFontInfo) {
136 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
137 ASSERT_EQ(1, FPDF_GetPageCount(document()));
138
139 FPDF_PAGE page = LoadPage(0);
140 ASSERT_TRUE(page);
141
142 {
143 // Not checking the rendering because it will depend on the fonts installed.
144 ScopedFPDFBitmap bitmap = RenderPage(page);
145 ASSERT_EQ(200, FPDFBitmap_GetWidth(bitmap.get()));
146 ASSERT_EQ(200, FPDFBitmap_GetHeight(bitmap.get()));
147 }
148
149 UnloadPage(page);
150}
151
152TEST_F(FPDFSysFontInfoEmbedderTest, DefaultTTFMap) {
153 static constexpr int kExpectedCharsets[] = {
158 };
159 std::vector<int> charsets;
160
161 const FPDF_CharsetFontMap* cfmap = FPDF_GetDefaultTTFMap();
162 ASSERT_TRUE(cfmap);
163
164 // Stop at either end mark.
165 while (cfmap->charset != -1 && cfmap->fontname) {
166 charsets.push_back(cfmap->charset);
167 // SAFETY: requires FPDF_GetDefaultTTFMap() to provide a sentinel.
168 UNSAFE_BUFFERS(++cfmap);
169 }
170
171 // Confirm end marks only occur as a pair.
172 EXPECT_EQ(cfmap->charset, -1);
173 EXPECT_EQ(cfmap->fontname, nullptr);
174
175 EXPECT_THAT(charsets, testing::UnorderedElementsAreArray(kExpectedCharsets));
176}
177
178TEST_F(FPDFSysFontInfoEmbedderTest, DefaultTTFMapCountAndEntries) {
179 static constexpr int kExpectedCharsets[] = {
188 };
189 static const std::string kExpectedFontNames[] = {
190 "Helvetica", "SimSun", "MingLiU", "MS Gothic", "Batang", "Arial",
191#if BUILDFLAG(IS_WIN)
192 "Tahoma",
193#else
194 "Arial",
195#endif
196 "Arial",
197 };
198 std::vector<int> charsets;
199 std::vector<const char*> font_names;
200
201 const size_t count = FPDF_GetDefaultTTFMapCount();
202 for (size_t i = 0; i < count; ++i) {
203 const FPDF_CharsetFontMap* entry = FPDF_GetDefaultTTFMapEntry(i);
204 ASSERT_TRUE(entry);
205 charsets.push_back(entry->charset);
206 font_names.push_back(entry->fontname);
207 }
208
209 EXPECT_THAT(charsets, testing::ElementsAreArray(kExpectedCharsets));
210 EXPECT_THAT(font_names, testing::ElementsAreArray(kExpectedFontNames));
211
212 // Test out of bound indices.
213 EXPECT_FALSE(FPDF_GetDefaultTTFMapEntry(count));
214 EXPECT_FALSE(FPDF_GetDefaultTTFMapEntry(9999));
215}
static EmbedderTestEnvironment * GetInstance()
void SetUp() override
void TearDown() override
#define UNSAFE_BUFFERS(...)
FPDF_EXPORT void FPDF_CALLCONV FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO *pFontInfoExt)
FPDF_EXPORT const FPDF_CharsetFontMap *FPDF_CALLCONV FPDF_GetDefaultTTFMap()
FPDF_EXPORT FPDF_SYSFONTINFO *FPDF_CALLCONV FPDF_GetDefaultSystemFontInfo()
FPDF_EXPORT void FPDF_CALLCONV FPDF_FreeDefaultSystemFontInfo(FPDF_SYSFONTINFO *pFontInfo)
#define FXFONT_HANGEUL_CHARSET
#define FXFONT_EASTERNEUROPEAN_CHARSET
#define FXFONT_SHIFTJIS_CHARSET
#define FXFONT_CYRILLIC_CHARSET
#define FXFONT_ANSI_CHARSET
#define FXFONT_ARABIC_CHARSET
#define FXFONT_GB2312_CHARSET
#define FXFONT_CHINESEBIG5_CHARSET
TEST_F(FPDFUnavailableSysFontInfoEmbedderTest, Bug972518)
TEST_F(FPDFSysFontInfoEmbedderTest, DefaultSystemFontInfo)
void(* Release)(struct _FPDF_SYSFONTINFO *pThis)
void(* DeleteFont)(struct _FPDF_SYSFONTINFO *pThis, void *hFont)
unsigned long(* GetFontData)(struct _FPDF_SYSFONTINFO *pThis, void *hFont, unsigned int table, unsigned char *buffer, unsigned long buf_size)
void *(* GetFont)(struct _FPDF_SYSFONTINFO *pThis, const char *face)
unsigned long(* GetFaceName)(struct _FPDF_SYSFONTINFO *pThis, void *hFont, char *buffer, unsigned long buf_size)
int(* GetFontCharset)(struct _FPDF_SYSFONTINFO *pThis, void *hFont)
void(* EnumFonts)(struct _FPDF_SYSFONTINFO *pThis, void *pMapper)
void *(* MapFont)(struct _FPDF_SYSFONTINFO *pThis, int weight, FPDF_BOOL bItalic, int charset, int pitch_family, const char *face, FPDF_BOOL *bExact)