Debian Codegraph @

qt5/tests/check_fonts.cpp

raw
1 #include <QtTest/QTest>
2
3 #include <poppler-qt5.h>
4
5 #include <memory>
6
7 class TestFontsData : public QObject
8 {
9 Q_OBJECT
10 public:
11 explicit TestFontsData(QObject *parent = nullptr) : QObject(parent) { }
12 private Q_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
23 static 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(from));
28 while (it->hasNext() && num) {
29 list += it->next();
30 --num;
31 }
32 return list;
33 }
34
35 namespace Poppler {
36 static 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
60 void TestFontsData::checkNoFonts()
61 {
62 Poppler::Document *doc;
63 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/tests/image.pdf"));
64 QVERIFY(doc);
65
66 QList<Poppler::FontInfo> listOfFonts = doc->fonts();
67 QCOMPARE(listOfFonts.size(), 0);
68
69 delete doc;
70 }
71
72 void TestFontsData::checkType1()
73 {
74 Poppler::Document *doc;
75 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/tests/text.pdf"));
76 QVERIFY(doc);
77
78 QList<Poppler::FontInfo> listOfFonts = doc->fonts();
79 QCOMPARE(listOfFonts.size(), 1);
80 QCOMPARE(listOfFonts.at(0).name(), QLatin1String("Helvetica"));
81 QCOMPARE(listOfFonts.at(0).type(), Poppler::FontInfo::Type1);
82 QCOMPARE(listOfFonts.at(0).typeName(), QLatin1String("Type 1"));
83
84 QCOMPARE(listOfFonts.at(0).isEmbedded(), false);
85 QCOMPARE(listOfFonts.at(0).isSubset(), false);
86
87 delete doc;
88 }
89
90 void TestFontsData::checkType3()
91 {
92 Poppler::Document *doc;
93 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/tests/type3.pdf"));
94 QVERIFY(doc);
95
96 QList<Poppler::FontInfo> listOfFonts = doc->fonts();
97 QCOMPARE(listOfFonts.size(), 2);
98 QCOMPARE(listOfFonts.at(0).name(), QLatin1String("Helvetica"));
99 QCOMPARE(listOfFonts.at(0).type(), Poppler::FontInfo::Type1);
100 QCOMPARE(listOfFonts.at(0).typeName(), QLatin1String("Type 1"));
101
102 QCOMPARE(listOfFonts.at(0).isEmbedded(), false);
103 QCOMPARE(listOfFonts.at(0).isSubset(), false);
104
105 QCOMPARE(listOfFonts.at(1).name(), QString());
106 QCOMPARE(listOfFonts.at(1).type(), Poppler::FontInfo::Type3);
107 QCOMPARE(listOfFonts.at(1).typeName(), QLatin1String("Type 3"));
108
109 QCOMPARE(listOfFonts.at(1).isEmbedded(), true);
110 QCOMPARE(listOfFonts.at(1).isSubset(), false);
111
112 delete doc;
113 }
114
115 void TestFontsData::checkTrueType()
116 {
117 Poppler::Document *doc;
118 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/unittestcases/truetype.pdf"));
119 QVERIFY(doc);
120
121 QList<Poppler::FontInfo> listOfFonts = doc->fonts();
122 QCOMPARE(listOfFonts.size(), 2);
123 QCOMPARE(listOfFonts.at(0).name(), QLatin1String("Arial-BoldMT"));
124 QCOMPARE(listOfFonts.at(0).type(), Poppler::FontInfo::TrueType);
125 QCOMPARE(listOfFonts.at(0).typeName(), QLatin1String("TrueType"));
126
127 QCOMPARE(listOfFonts.at(0).isEmbedded(), false);
128 QCOMPARE(listOfFonts.at(0).isSubset(), false);
129
130 QCOMPARE(listOfFonts.at(1).name(), QLatin1String("ArialMT"));
131 QCOMPARE(listOfFonts.at(1).type(), Poppler::FontInfo::TrueType);
132 QCOMPARE(listOfFonts.at(1).typeName(), QLatin1String("TrueType"));
133
134 QCOMPARE(listOfFonts.at(1).isEmbedded(), false);
135 QCOMPARE(listOfFonts.at(1).isSubset(), false);
136
137 delete doc;
138 }
139
140 void TestFontsData::checkFontIterator()
141 {
142 // loading a 1-page document
143 Poppler::Document *doc;
144 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/tests/type3.pdf"));
145 QVERIFY(doc);
146 // loading a 6-pages document
147 Poppler::Document *doc6 = Poppler::Document::load(QStringLiteral(TESTDATADIR "/tests/cropbox.pdf"));
148 QVERIFY(doc6);
149
150 std::unique_ptr<Poppler::FontIterator> it;
151
152 // some tests with the 1-page document:
153 // - check a default iterator
154 it.reset(doc->newFontIterator());
155 QVERIFY(it->hasNext());
156 // - check an iterator for negative pages to behave as 0
157 it.reset(doc->newFontIterator(-1));
158 QVERIFY(it->hasNext());
159 // - check an iterator for pages out of the page limit
160 it.reset(doc->newFontIterator(1));
161 QVERIFY(!it->hasNext());
162 // - check that it reaches the end after 1 iteration
163 it.reset(doc->newFontIterator());
164 QVERIFY(it->hasNext());
165 it->next();
166 QVERIFY(!it->hasNext());
167
168 // some tests with the 6-page document:
169 // - check a default iterator
170 it.reset(doc6->newFontIterator());
171 QVERIFY(it->hasNext());
172 // - check an iterator for pages out of the page limit
173 it.reset(doc6->newFontIterator(6));
174 QVERIFY(!it->hasNext());
175 // - check that it reaches the end after 6 iterations
176 it.reset(doc6->newFontIterator());
177 QVERIFY(it->hasNext());
178 it->next();
179 QVERIFY(it->hasNext());
180 it->next();
181 QVERIFY(it->hasNext());
182 it->next();
183 QVERIFY(it->hasNext());
184 it->next();
185 QVERIFY(it->hasNext());
186 it->next();
187 QVERIFY(it->hasNext());
188 it->next();
189 QVERIFY(!it->hasNext());
190
191 delete doc;
192 delete doc6;
193 }
194
195 void TestFontsData::checkSecondDocumentQuery()
196 {
197 Poppler::Document *doc;
198 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/tests/type3.pdf"));
199 QVERIFY(doc);
200
201 QList<Poppler::FontInfo> listOfFonts = doc->fonts();
202 QCOMPARE(listOfFonts.size(), 2);
203 // check we get the very same result when calling fonts() again (#19405)
204 QList<Poppler::FontInfo> listOfFonts2 = doc->fonts();
205 QCOMPARE(listOfFonts, listOfFonts2);
206
207 delete doc;
208 }
209
210 void TestFontsData::checkMultipleIterations()
211 {
212 Poppler::Document *doc;
213 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/tests/type3.pdf"));
214 QVERIFY(doc);
215
216 QList<Poppler::FontInfo> listOfFonts = loadFontsViaIterator(doc);
217 QCOMPARE(listOfFonts.size(), 2);
218 QList<Poppler::FontInfo> listOfFonts2 = loadFontsViaIterator(doc);
219 QCOMPARE(listOfFonts, listOfFonts2);
220
221 delete doc;
222 }
223
224 void TestFontsData::checkIteratorFonts()
225 {
226 Poppler::Document *doc;
227 doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/tests/fonts.pdf"));
228 QVERIFY(doc);
229
230 QList<Poppler::FontInfo> listOfFonts = doc->fonts();
231 QCOMPARE(listOfFonts.size(), 3);
232
233 // check we get the very same result when gatering fonts using the iterator
234 QList<Poppler::FontInfo> listOfFonts2 = loadFontsViaIterator(doc);
235 QCOMPARE(listOfFonts, listOfFonts2);
236
237 delete doc;
238 }
239
240 QTEST_GUILESS_MAIN(TestFontsData)
241 #include "check_fonts.moc"