1#include <QtTest/QTest>
2
3#include <poppler-qt6.h>
4
5#include <memory>
6
7class TestFontsData : public QObject
8{
9 Q_OBJECT
10public:
11 explicit TestFontsData(QObject *parent = nullptr) : QObject(parent) { }
12private slots:
13 void checkNoFonts();
14 void checkType1();
15 void checkType3();
16 void checkTrueType();
17 void checkFontIterator();
18 void checkSecondDocumentQuery();
19 void checkMultipleIterations();
20 void checkIteratorFonts();
21};
22
23static QList<Poppler::FontInfo> loadFontsViaIterator(Poppler::Document *doc, int from = 0, int count = -1)
24{
25 int num = count == -1 ? doc->numPages() - from : count;
26 QList<Poppler::FontInfo> list;
27 std::unique_ptr<Poppler::FontIterator> it(doc->newFontIterator(startPage: from));
28 while (it->hasNext() && num) {
29 list += it->next();
30 --num;
31 }
32 return list;
33}
34
35namespace Poppler {
36static bool operator==(const FontInfo &f1, const FontInfo &f2)
37{
38 if (f1.name() != f2.name()) {
39 return false;
40 }
41 if (f1.file() != f2.file()) {
42 return false;
43 }
44 if (f1.isEmbedded() != f2.isEmbedded()) {
45 return false;
46 }
47 if (f1.isSubset() != f2.isSubset()) {
48 return false;
49 }
50 if (f1.type() != f2.type()) {
51 return false;
52 }
53 if (f1.typeName() != f2.typeName()) {
54 return false;
55 }
56 return true;
57}
58}
59
60void TestFontsData::checkNoFonts()
61{
62 std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/tests/image.pdf");
63 QVERIFY(doc);
64
65 QList<Poppler::FontInfo> listOfFonts = doc->fonts();
66 QCOMPARE(listOfFonts.size(), 0);
67}
68
69void TestFontsData::checkType1()
70{
71 std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/tests/text.pdf");
72 QVERIFY(doc);
73
74 QList<Poppler::FontInfo> listOfFonts = doc->fonts();
75 QCOMPARE(listOfFonts.size(), 1);
76 QCOMPARE(listOfFonts.at(0).name(), QLatin1String("Helvetica"));
77 QCOMPARE(listOfFonts.at(0).type(), Poppler::FontInfo::Type1);
78 QCOMPARE(listOfFonts.at(0).typeName(), QLatin1String("Type 1"));
79
80 QCOMPARE(listOfFonts.at(0).isEmbedded(), false);
81 QCOMPARE(listOfFonts.at(0).isSubset(), false);
82}
83
84void TestFontsData::checkType3()
85{
86 std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/tests/type3.pdf");
87 QVERIFY(doc);
88
89 QList<Poppler::FontInfo> listOfFonts = doc->fonts();
90 QCOMPARE(listOfFonts.size(), 2);
91 QCOMPARE(listOfFonts.at(0).name(), QLatin1String("Helvetica"));
92 QCOMPARE(listOfFonts.at(0).type(), Poppler::FontInfo::Type1);
93 QCOMPARE(listOfFonts.at(0).typeName(), QLatin1String("Type 1"));
94
95 QCOMPARE(listOfFonts.at(0).isEmbedded(), false);
96 QCOMPARE(listOfFonts.at(0).isSubset(), false);
97
98 QCOMPARE(listOfFonts.at(1).name(), QString());
99 QCOMPARE(listOfFonts.at(1).type(), Poppler::FontInfo::Type3);
100 QCOMPARE(listOfFonts.at(1).typeName(), QLatin1String("Type 3"));
101
102 QCOMPARE(listOfFonts.at(1).isEmbedded(), true);
103 QCOMPARE(listOfFonts.at(1).isSubset(), false);
104}
105
106void TestFontsData::checkTrueType()
107{
108 std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf");
109 QVERIFY(doc);
110
111 QList<Poppler::FontInfo> listOfFonts = doc->fonts();
112 QCOMPARE(listOfFonts.size(), 2);
113 QCOMPARE(listOfFonts.at(0).name(), QLatin1String("Arial-BoldMT"));
114 QCOMPARE(listOfFonts.at(0).type(), Poppler::FontInfo::TrueType);
115 QCOMPARE(listOfFonts.at(0).typeName(), QLatin1String("TrueType"));
116
117 QCOMPARE(listOfFonts.at(0).isEmbedded(), false);
118 QCOMPARE(listOfFonts.at(0).isSubset(), false);
119
120 QCOMPARE(listOfFonts.at(1).name(), QLatin1String("ArialMT"));
121 QCOMPARE(listOfFonts.at(1).type(), Poppler::FontInfo::TrueType);
122 QCOMPARE(listOfFonts.at(1).typeName(), QLatin1String("TrueType"));
123
124 QCOMPARE(listOfFonts.at(1).isEmbedded(), false);
125 QCOMPARE(listOfFonts.at(1).isSubset(), false);
126}
127
128void TestFontsData::checkFontIterator()
129{
130 // loading a 1-page document
131 std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/tests/type3.pdf");
132 QVERIFY(doc);
133 // loading a 6-pages document
134 std::unique_ptr<Poppler::Document> doc6 = Poppler::Document::load(TESTDATADIR "/tests/cropbox.pdf");
135 QVERIFY(doc6);
136
137 std::unique_ptr<Poppler::FontIterator> it;
138
139 // some tests with the 1-page document:
140 // - check a default iterator
141 it = doc->newFontIterator();
142 QVERIFY(it->hasNext());
143 // - check an iterator for negative pages to behave as 0
144 it = doc->newFontIterator(startPage: -1);
145 QVERIFY(it->hasNext());
146 // - check an iterator for pages out of the page limit
147 it = doc->newFontIterator(startPage: 1);
148 QVERIFY(!it->hasNext());
149 // - check that it reaches the end after 1 iteration
150 it = doc->newFontIterator();
151 QVERIFY(it->hasNext());
152 it->next();
153 QVERIFY(!it->hasNext());
154
155 // some tests with the 6-page document:
156 // - check a default iterator
157 it = doc6->newFontIterator();
158 QVERIFY(it->hasNext());
159 // - check an iterator for pages out of the page limit
160 it = doc6->newFontIterator(startPage: 6);
161 QVERIFY(!it->hasNext());
162 // - check that it reaches the end after 6 iterations
163 it = doc6->newFontIterator();
164 QVERIFY(it->hasNext());
165 it->next();
166 QVERIFY(it->hasNext());
167 it->next();
168 QVERIFY(it->hasNext());
169 it->next();
170 QVERIFY(it->hasNext());
171 it->next();
172 QVERIFY(it->hasNext());
173 it->next();
174 QVERIFY(it->hasNext());
175 it->next();
176 QVERIFY(!it->hasNext());
177}
178
179void TestFontsData::checkSecondDocumentQuery()
180{
181 std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/tests/type3.pdf");
182 QVERIFY(doc);
183
184 QList<Poppler::FontInfo> listOfFonts = doc->fonts();
185 QCOMPARE(listOfFonts.size(), 2);
186 // check we get the very same result when calling fonts() again (#19405)
187 QList<Poppler::FontInfo> listOfFonts2 = doc->fonts();
188 QCOMPARE(listOfFonts, listOfFonts2);
189}
190
191void TestFontsData::checkMultipleIterations()
192{
193 std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/tests/type3.pdf");
194 QVERIFY(doc);
195
196 QList<Poppler::FontInfo> listOfFonts = loadFontsViaIterator(doc: doc.get());
197 QCOMPARE(listOfFonts.size(), 2);
198 QList<Poppler::FontInfo> listOfFonts2 = loadFontsViaIterator(doc: doc.get());
199 QCOMPARE(listOfFonts, listOfFonts2);
200}
201
202void TestFontsData::checkIteratorFonts()
203{
204 std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/tests/fonts.pdf");
205 QVERIFY(doc);
206
207 QList<Poppler::FontInfo> listOfFonts = doc->fonts();
208 QCOMPARE(listOfFonts.size(), 3);
209
210 // check we get the very same result when gatering fonts using the iterator
211 QList<Poppler::FontInfo> listOfFonts2 = loadFontsViaIterator(doc: doc.get());
212 QCOMPARE(listOfFonts, listOfFonts2);
213}
214
215QTEST_GUILESS_MAIN(TestFontsData)
216#include "check_fonts.moc"
217

source code of poppler/qt6/tests/check_fonts.cpp