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