File size: 6,877 Bytes
5cee033
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <QtTest/QTest>

#include <poppler-qt6.h>

#include <memory>

class TestFontsData : public QObject
{
    Q_OBJECT
public:
    explicit TestFontsData(QObject *parent = nullptr) : QObject(parent) { }
private slots:
    void checkNoFonts();
    void checkType1();
    void checkType3();
    void checkTrueType();
    void checkFontIterator();
    void checkSecondDocumentQuery();
    void checkMultipleIterations();
    void checkIteratorFonts();
};

static QList<Poppler::FontInfo> loadFontsViaIterator(Poppler::Document *doc, int from = 0, int count = -1)
{
    int num = count == -1 ? doc->numPages() - from : count;
    QList<Poppler::FontInfo> list;
    std::unique_ptr<Poppler::FontIterator> it(doc->newFontIterator(from));
    while (it->hasNext() && num) {
        list += it->next();
        --num;
    }
    return list;
}

namespace Poppler {
static bool operator==(const FontInfo &f1, const FontInfo &f2)
{
    if (f1.name() != f2.name()) {
        return false;
    }
    if (f1.file() != f2.file()) {
        return false;
    }
    if (f1.isEmbedded() != f2.isEmbedded()) {
        return false;
    }
    if (f1.isSubset() != f2.isSubset()) {
        return false;
    }
    if (f1.type() != f2.type()) {
        return false;
    }
    if (f1.typeName() != f2.typeName()) {
        return false;
    }
    return true;
}
}

void TestFontsData::checkNoFonts()
{
    std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/tests/image.pdf");
    QVERIFY(doc);

    QList<Poppler::FontInfo> listOfFonts = doc->fonts();
    QCOMPARE(listOfFonts.size(), 0);
}

void TestFontsData::checkType1()
{
    std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/tests/text.pdf");
    QVERIFY(doc);

    QList<Poppler::FontInfo> listOfFonts = doc->fonts();
    QCOMPARE(listOfFonts.size(), 1);
    QCOMPARE(listOfFonts.at(0).name(), QLatin1String("Helvetica"));
    QCOMPARE(listOfFonts.at(0).type(), Poppler::FontInfo::Type1);
    QCOMPARE(listOfFonts.at(0).typeName(), QLatin1String("Type 1"));

    QCOMPARE(listOfFonts.at(0).isEmbedded(), false);
    QCOMPARE(listOfFonts.at(0).isSubset(), false);
}

void TestFontsData::checkType3()
{
    std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/tests/type3.pdf");
    QVERIFY(doc);

    QList<Poppler::FontInfo> listOfFonts = doc->fonts();
    QCOMPARE(listOfFonts.size(), 2);
    QCOMPARE(listOfFonts.at(0).name(), QLatin1String("Helvetica"));
    QCOMPARE(listOfFonts.at(0).type(), Poppler::FontInfo::Type1);
    QCOMPARE(listOfFonts.at(0).typeName(), QLatin1String("Type 1"));

    QCOMPARE(listOfFonts.at(0).isEmbedded(), false);
    QCOMPARE(listOfFonts.at(0).isSubset(), false);

    QCOMPARE(listOfFonts.at(1).name(), QString());
    QCOMPARE(listOfFonts.at(1).type(), Poppler::FontInfo::Type3);
    QCOMPARE(listOfFonts.at(1).typeName(), QLatin1String("Type 3"));

    QCOMPARE(listOfFonts.at(1).isEmbedded(), true);
    QCOMPARE(listOfFonts.at(1).isSubset(), false);
}

void TestFontsData::checkTrueType()
{
    std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/truetype.pdf");
    QVERIFY(doc);

    QList<Poppler::FontInfo> listOfFonts = doc->fonts();
    QCOMPARE(listOfFonts.size(), 2);
    QCOMPARE(listOfFonts.at(0).name(), QLatin1String("Arial-BoldMT"));
    QCOMPARE(listOfFonts.at(0).type(), Poppler::FontInfo::TrueType);
    QCOMPARE(listOfFonts.at(0).typeName(), QLatin1String("TrueType"));

    QCOMPARE(listOfFonts.at(0).isEmbedded(), false);
    QCOMPARE(listOfFonts.at(0).isSubset(), false);

    QCOMPARE(listOfFonts.at(1).name(), QLatin1String("ArialMT"));
    QCOMPARE(listOfFonts.at(1).type(), Poppler::FontInfo::TrueType);
    QCOMPARE(listOfFonts.at(1).typeName(), QLatin1String("TrueType"));

    QCOMPARE(listOfFonts.at(1).isEmbedded(), false);
    QCOMPARE(listOfFonts.at(1).isSubset(), false);
}

void TestFontsData::checkFontIterator()
{
    // loading a 1-page document
    std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/tests/type3.pdf");
    QVERIFY(doc);
    // loading a 6-pages document
    std::unique_ptr<Poppler::Document> doc6 = Poppler::Document::load(TESTDATADIR "/tests/cropbox.pdf");
    QVERIFY(doc6);

    std::unique_ptr<Poppler::FontIterator> it;

    // some tests with the 1-page document:
    // - check a default iterator
    it = doc->newFontIterator();
    QVERIFY(it->hasNext());
    // - check an iterator for negative pages to behave as 0
    it = doc->newFontIterator(-1);
    QVERIFY(it->hasNext());
    // - check an iterator for pages out of the page limit
    it = doc->newFontIterator(1);
    QVERIFY(!it->hasNext());
    // - check that it reaches the end after 1 iteration
    it = doc->newFontIterator();
    QVERIFY(it->hasNext());
    it->next();
    QVERIFY(!it->hasNext());

    // some tests with the 6-page document:
    // - check a default iterator
    it = doc6->newFontIterator();
    QVERIFY(it->hasNext());
    // - check an iterator for pages out of the page limit
    it = doc6->newFontIterator(6);
    QVERIFY(!it->hasNext());
    // - check that it reaches the end after 6 iterations
    it = doc6->newFontIterator();
    QVERIFY(it->hasNext());
    it->next();
    QVERIFY(it->hasNext());
    it->next();
    QVERIFY(it->hasNext());
    it->next();
    QVERIFY(it->hasNext());
    it->next();
    QVERIFY(it->hasNext());
    it->next();
    QVERIFY(it->hasNext());
    it->next();
    QVERIFY(!it->hasNext());
}

void TestFontsData::checkSecondDocumentQuery()
{
    std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/tests/type3.pdf");
    QVERIFY(doc);

    QList<Poppler::FontInfo> listOfFonts = doc->fonts();
    QCOMPARE(listOfFonts.size(), 2);
    // check we get the very same result when calling fonts() again (#19405)
    QList<Poppler::FontInfo> listOfFonts2 = doc->fonts();
    QCOMPARE(listOfFonts, listOfFonts2);
}

void TestFontsData::checkMultipleIterations()
{
    std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/tests/type3.pdf");
    QVERIFY(doc);

    QList<Poppler::FontInfo> listOfFonts = loadFontsViaIterator(doc.get());
    QCOMPARE(listOfFonts.size(), 2);
    QList<Poppler::FontInfo> listOfFonts2 = loadFontsViaIterator(doc.get());
    QCOMPARE(listOfFonts, listOfFonts2);
}

void TestFontsData::checkIteratorFonts()
{
    std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/tests/fonts.pdf");
    QVERIFY(doc);

    QList<Poppler::FontInfo> listOfFonts = doc->fonts();
    QCOMPARE(listOfFonts.size(), 3);

    // check we get the very same result when gatering fonts using the iterator
    QList<Poppler::FontInfo> listOfFonts2 = loadFontsViaIterator(doc.get());
    QCOMPARE(listOfFonts, listOfFonts2);
}

QTEST_GUILESS_MAIN(TestFontsData)
#include "check_fonts.moc"