| 1 |
#include <QtTest/QTest> |
| 2 |
|
| 3 |
#include <poppler-qt5.h> |
| 4 |
|
| 5 |
#include <QtCore/QFile> |
| 6 |
|
| 7 |
class TestActualText : public QObject |
| 8 |
{ |
| 9 |
Q_OBJECT |
| 10 |
public: |
| 11 |
explicit TestActualText(QObject *parent = nullptr) : QObject(parent) { } |
| 12 |
|
| 13 |
void checkActualText(Poppler::Document *doc, const QRectF &area, const QString &text); |
| 14 |
|
| 15 |
private Q_SLOTS: |
| 16 |
void checkActualText1(); |
| 17 |
void checkActualText2(); |
| 18 |
void checkActualText2_data(); |
| 19 |
void checkAllOrientations(); |
| 20 |
void checkAllOrientations_data(); |
| 21 |
void checkFakeboldText(); |
| 22 |
void checkFakeboldText_data(); |
| 23 |
}; |
| 24 |
|
| 25 |
void TestActualText::checkActualText(Poppler::Document *doc, const QRectF &area, const QString &text) |
| 26 |
{ |
| 27 |
Poppler::Page *page = doc->page(0); |
| 28 |
QVERIFY(page); |
| 29 |
|
| 30 |
QCOMPARE(page->text(area), text); |
| 31 |
|
| 32 |
delete page; |
| 33 |
} |
| 34 |
|
| 35 |
void TestActualText::checkActualText1() |
| 36 |
{ |
| 37 |
Poppler::Document *doc; |
| 38 |
doc = Poppler::Document::load(QStringLiteral(TESTDATADIR "/unittestcases/WithActualText.pdf")); |
| 39 |
QVERIFY(doc); |
| 40 |
|
| 41 |
checkActualText(doc, QRectF {}, QStringLiteral("The slow brown fox jumps over the black dog.")); |
| 42 |
|
| 43 |
delete doc; |
| 44 |
} |
| 45 |
|
| 46 |
void TestActualText::checkActualText2() |
| 47 |
{ |
| 48 |
QFETCH(QRectF, area); |
| 49 |
QFETCH(QString, text); |
| 50 |
|
| 51 |
QFile file(QStringLiteral(TESTDATADIR "/unittestcases/WithActualText.pdf")); |
| 52 |
QVERIFY(file.open(QIODevice::ReadOnly)); |
| 53 |
|
| 54 |
Poppler::Document *doc; |
| 55 |
doc = Poppler::Document::load(&file); |
| 56 |
QVERIFY(doc); |
| 57 |
|
| 58 |
checkActualText(doc, area, text); |
| 59 |
|
| 60 |
delete doc; |
| 61 |
} |
| 62 |
|
| 63 |
void TestActualText::checkActualText2_data() |
| 64 |
{ |
| 65 |
QTest::addColumn<QRectF>("area"); |
| 66 |
QTest::addColumn<QString>("text"); |
| 67 |
|
| 68 |
// Line bounding box is [100.000 90.720 331.012110 102.350] |
| 69 |
|
| 70 |
QTest::newRow("full page") << QRectF {} << QStringLiteral("The slow brown fox jumps over the black dog."); |
| 71 |
QTest::newRow("full line") << QRectF { 50.0, 90.0, 290.0, 20.0 } << QStringLiteral("The slow brown fox jumps over the black dog."); |
| 72 |
QTest::newRow("full line [narrow]") << QRectF { 50.0, 95.0, 290.0, 5.0 } << QStringLiteral("The slow brown fox jumps over the black dog."); |
| 73 |
QTest::newRow("above line") << QRectF { 50.0, 85.0, 290.0, 10.0 } << QString {}; |
| 74 |
QTest::newRow("above line mid") << QRectF { 50.0, 90.0, 290.0, 5.0 } << QString {}; |
| 75 |
QTest::newRow("first two words") << QRectF { 50.0, 90.0, 100.0, 20.0 } << QStringLiteral("The slow"); |
| 76 |
QTest::newRow("first two words [narrow]") << QRectF { 50.0, 95.0, 100.0, 5.0 } << QStringLiteral("The slow"); |
| 77 |
QTest::newRow("first character") << QRectF { 103.0, 95.0, 1.0, 5.0 } << QStringLiteral("T"); |
| 78 |
QTest::newRow("last two words") << QRectF { 285.0, 90.0, 100.0, 20.0 } << QStringLiteral("black dog."); |
| 79 |
QTest::newRow("last character") << QRectF { 320.0, 90.0, 8.0, 20.0 } << QStringLiteral("g"); |
| 80 |
QTest::newRow("middle 'fox'") << QRectF { 190.0, 90.0, 15.0, 20.0 } << QStringLiteral("fox"); |
| 81 |
QTest::newRow("middle 'x'") << QRectF { 200.0, 90.0, 5.0, 20.0 } << QStringLiteral("x"); |
| 82 |
} |
| 83 |
|
| 84 |
void TestActualText::checkAllOrientations() |
| 85 |
{ |
| 86 |
QFETCH(int, pageNr); |
| 87 |
QFETCH(QRectF, area); |
| 88 |
QFETCH(QString, text); |
| 89 |
|
| 90 |
QString path { QStringLiteral(TESTDATADIR "/unittestcases/orientation.pdf") }; |
| 91 |
std::unique_ptr<Poppler::Document> doc { Poppler::Document::load(path) }; |
| 92 |
QVERIFY(doc); |
| 93 |
|
| 94 |
std::unique_ptr<Poppler::Page> page { doc->page(pageNr) }; |
| 95 |
QVERIFY(page); |
| 96 |
|
| 97 |
QCOMPARE(page->text(area), text); |
| 98 |
} |
| 99 |
|
| 100 |
void TestActualText::checkAllOrientations_data() |
| 101 |
{ |
| 102 |
QTest::addColumn<int>("pageNr"); |
| 103 |
QTest::addColumn<QRectF>("area"); |
| 104 |
QTest::addColumn<QString>("text"); |
| 105 |
|
| 106 |
QTest::newRow("Portrait") << 0 << QRectF {} << QStringLiteral("Portrait"); |
| 107 |
QTest::newRow("Landscape") << 1 << QRectF {} << QStringLiteral("Landscape"); |
| 108 |
QTest::newRow("Upside down") << 2 << QRectF {} << QStringLiteral("Upside down"); |
| 109 |
QTest::newRow("Seacape") << 3 << QRectF {} << QStringLiteral("Seascape"); |
| 110 |
|
| 111 |
QTest::newRow("Portrait A4 rect") << 0 << QRectF { 0, 0, 595, 842 } << QStringLiteral("Portrait"); |
| 112 |
QTest::newRow("Landscape A4 rect") << 1 << QRectF { 0, 0, 842, 595 } << QStringLiteral("Landscape"); |
| 113 |
QTest::newRow("Upside down A4 rect") << 2 << QRectF { 0, 0, 595, 842 } << QStringLiteral("Upside down"); |
| 114 |
QTest::newRow("Seacape A4 rect") << 3 << QRectF { 0, 0, 842, 595 } << QStringLiteral("Seascape"); |
| 115 |
|
| 116 |
QTest::newRow("Portrait line rect") << 0 << QRectF { 30, 30, 60, 20 } << QStringLiteral("Portrait"); |
| 117 |
QTest::newRow("Landscape line rect") << 1 << QRectF { 790, 30, 20, 80 } << QStringLiteral("Landscape"); |
| 118 |
QTest::newRow("Upside down line rect") << 2 << QRectF { 485, 790, 75, 20 } << QStringLiteral("Upside down"); |
| 119 |
QTest::newRow("Seacape line rect") << 3 << QRectF { 30, 500, 20, 70 } << QStringLiteral("Seascape"); |
| 120 |
|
| 121 |
QTest::newRow("Portrait small rect B") << 0 << QRectF { 30, 35, 10, 10 } << QStringLiteral("P"); |
| 122 |
QTest::newRow("Portrait small rect E") << 0 << QRectF { 80, 35, 10, 10 } << QStringLiteral("t"); |
| 123 |
QTest::newRow("Landscape small rect B") << 1 << QRectF { 800, 30, 10, 10 } << QStringLiteral("L"); |
| 124 |
QTest::newRow("Landscape small rect E") << 1 << QRectF { 800, 90, 10, 10 } << QStringLiteral("e"); |
| 125 |
QTest::newRow("Upside down small rect B") << 2 << QRectF { 550, 800, 10, 10 } << QStringLiteral("U"); |
| 126 |
QTest::newRow("Upside down small rect E") << 2 << QRectF { 485, 800, 10, 10 } << QStringLiteral("n"); |
| 127 |
QTest::newRow("Seacape small rect B") << 3 << QRectF { 40, 550, 10, 10 } << QStringLiteral("S"); |
| 128 |
QTest::newRow("Seacape small rect E") << 3 << QRectF { 40, 510, 10, 10 } << QStringLiteral("p"); |
| 129 |
} |
| 130 |
|
| 131 |
void TestActualText::checkFakeboldText() |
| 132 |
{ |
| 133 |
QFETCH(int, pageNr); |
| 134 |
QFETCH(QRectF, area); |
| 135 |
QFETCH(QString, text); |
| 136 |
|
| 137 |
QString path { QStringLiteral(TESTDATADIR "/unittestcases/fakebold.pdf") }; |
| 138 |
std::unique_ptr<Poppler::Document> doc { Poppler::Document::load(path) }; |
| 139 |
QVERIFY(doc); |
| 140 |
|
| 141 |
std::unique_ptr<Poppler::Page> page { doc->page(pageNr) }; |
| 142 |
QVERIFY(page); |
| 143 |
|
| 144 |
QCOMPARE(page->text(area), text); |
| 145 |
} |
| 146 |
|
| 147 |
void TestActualText::checkFakeboldText_data() |
| 148 |
{ |
| 149 |
QTest::addColumn<int>("pageNr"); |
| 150 |
QTest::addColumn<QRectF>("area"); |
| 151 |
QTest::addColumn<QString>("text"); |
| 152 |
|
| 153 |
QTest::newRow("Upright line 1") << 0 << QRectF { 0, 0, 595, 80 } << QStringLiteral("1 This is fakebold text."); |
| 154 |
QTest::newRow("Upright line 2") << 0 << QRectF { 0, 80, 595, 80 } << QStringLiteral("2 This is a fakebold word."); |
| 155 |
QTest::newRow("Upright line 3") << 0 << QRectF { 0, 140, 595, 80 } << QStringLiteral("3 The last word is in fakebold."); |
| 156 |
QTest::newRow("Upright line 4") << 0 << QRectF { 0, 220, 595, 80 } << QStringLiteral("4 Hyphenated-fakebold word."); |
| 157 |
QTest::newRow("Upright line 5") << 0 << QRectF { 0, 300, 595, 80 } << QStringLiteral("5 Quoted \"fakebold\" word."); |
| 158 |
|
| 159 |
QTest::newRow("Rotated 90' line 1") << 1 << QRectF { 510, 0, 80, 842 } << QStringLiteral("1 This is fakebold text."); |
| 160 |
QTest::newRow("Rotated 90' line 2") << 1 << QRectF { 430, 0, 80, 842 } << QStringLiteral("2 This is a fakebold word."); |
| 161 |
QTest::newRow("Rotated 90' line 3") << 1 << QRectF { 350, 0, 80, 842 } << QStringLiteral("3 The last word is in fakebold."); |
| 162 |
QTest::newRow("Rotated 90' line 4") << 1 << QRectF { 270, 0, 80, 842 } << QStringLiteral("4 Hyphenated-fakebold word."); |
| 163 |
QTest::newRow("Rotated 90' line 5") << 1 << QRectF { 190, 0, 80, 842 } << QStringLiteral("5 Quoted \"fakebold\" word."); |
| 164 |
|
| 165 |
QTest::newRow("Rotated 180' line 1") << 2 << QRectF { 0, 760, 595, 80 } << QStringLiteral("1 This is fakebold text."); |
| 166 |
QTest::newRow("Rotated 180' line 2") << 2 << QRectF { 0, 680, 595, 80 } << QStringLiteral("2 This is a fakebold word."); |
| 167 |
QTest::newRow("Rotated 180' line 3") << 2 << QRectF { 0, 600, 595, 80 } << QStringLiteral("3 The last word is in fakebold."); |
| 168 |
QTest::newRow("Rotated 180' line 4") << 2 << QRectF { 0, 520, 595, 80 } << QStringLiteral("4 Hyphenated-fakebold word."); |
| 169 |
QTest::newRow("Rotated 180' line 5") << 2 << QRectF { 0, 440, 595, 80 } << QStringLiteral("5 Quoted \"fakebold\" word."); |
| 170 |
|
| 171 |
QTest::newRow("Rotated 270' line 1") << 3 << QRectF { 20, 0, 80, 842 } << QStringLiteral("1 This is fakebold text."); |
| 172 |
QTest::newRow("Rotated 270' line 2") << 3 << QRectF { 100, 0, 80, 842 } << QStringLiteral("2 This is a fakebold word."); |
| 173 |
QTest::newRow("Rotated 270' line 3") << 3 << QRectF { 160, 0, 80, 842 } << QStringLiteral("3 The last word is in fakebold."); |
| 174 |
QTest::newRow("Rotated 270' line 4") << 3 << QRectF { 240, 0, 80, 842 } << QStringLiteral("4 Hyphenated-fakebold word."); |
| 175 |
QTest::newRow("Rotated 270' line 5") << 3 << QRectF { 320, 0, 80, 842 } << QStringLiteral("5 Quoted \"fakebold\" word."); |
| 176 |
} |
| 177 |
|
| 178 |
QTEST_GUILESS_MAIN(TestActualText) |
| 179 |
|
| 180 |
#include "check_actualtext.moc" |