| 1 |
// SPDX-FileCopyrightText: 2008 Dominik Seichter <[email protected]> |
| 2 |
// SPDX-FileCopyrightText: 2021 Francesco Pretto <[email protected]> |
| 3 |
// SPDX-License-Identifier: MIT-0 |
| 4 |
|
| 5 |
#include <PdfTest.h> |
| 6 |
|
| 7 |
using namespace std; |
| 8 |
using namespace PoDoFo; |
| 9 |
|
| 10 |
TEST_CASE("TextExtraction1") |
| 11 |
{ |
| 12 |
PdfMemDocument doc; |
| 13 |
doc.Load(TestUtils::GetTestInputFilePath("TextExtraction1.pdf")); |
| 14 |
auto& page = doc.GetPages().GetPageAt(0); |
| 15 |
vector<PdfTextEntry> entries; |
| 16 |
page.ExtractTextTo(entries); |
| 17 |
|
| 18 |
REQUIRE(entries[0].Text == "MATLAB (an abbreviation of \"matrix laboratory\") is a proprietary multi-paradigm programming"); |
| 19 |
ASSERT_EQUAL(entries[0].X, 29.000000232); |
| 20 |
ASSERT_EQUAL(entries[0].Y, 694.943905559551); |
| 21 |
REQUIRE(entries[1].Text == "language and numerical computing environment developed by MathWorks. MATLAB allows matrix"); |
| 22 |
ASSERT_EQUAL(entries[1].X, 29.000000232); |
| 23 |
ASSERT_EQUAL(entries[1].Y, 684.920205479362); |
| 24 |
REQUIRE(entries[2].Text == "manipulations, plotting of functions and data, implementation of algorithms, creation of user"); |
| 25 |
ASSERT_EQUAL(entries[2].X, 28.977805831822455); |
| 26 |
ASSERT_EQUAL(entries[2].Y, 674.89580539916642); |
| 27 |
REQUIRE(entries[3].Text == "interfaces, and interfacing with programs written in other languages."); |
| 28 |
ASSERT_EQUAL(entries[3].X, 29.000000232); |
| 29 |
ASSERT_EQUAL(entries[3].Y, 664.872605318981); |
| 30 |
} |
| 31 |
|
| 32 |
TEST_CASE("TextExtraction2") |
| 33 |
{ |
| 34 |
// Extraction with inline fonts |
| 35 |
PdfMemDocument doc; |
| 36 |
doc.Load(TestUtils::GetTestInputFilePath("TextExtraction2.pdf")); |
| 37 |
auto& page = doc.GetPages().GetPageAt(0); |
| 38 |
vector<PdfTextEntry> entries; |
| 39 |
page.ExtractTextTo(entries); |
| 40 |
REQUIRE(entries[0].Text == "Test text"); |
| 41 |
ASSERT_EQUAL(entries[0].X, 31.199999999999999); |
| 42 |
ASSERT_EQUAL(entries[0].Y, 801.60000000000002); |
| 43 |
} |
| 44 |
|
| 45 |
TEST_CASE("TextExtraction3") |
| 46 |
{ |
| 47 |
PdfMemDocument doc; |
| 48 |
doc.Load(TestUtils::GetTestInputFilePath("TextExtractionPredefinedCmap.pdf")); |
| 49 |
auto& page = doc.GetPages().GetPageAt(0); |
| 50 |
vector<PdfTextEntry> entries; |
| 51 |
page.ExtractTextTo(entries); |
| 52 |
|
| 53 |
REQUIRE(entries[0].Text == "全省环岛天然气管网尚未成型,东部部分建设滞后,管网缺乏统一规划,管道管径、设计压力参差不齐,省内支干"); |
| 54 |
ASSERT_EQUAL(entries[0].X, 44.59); |
| 55 |
ASSERT_EQUAL(entries[0].Y, 406.7); |
| 56 |
REQUIRE(entries[1].Text == "线及支线长度不足、密度过小,难以实现省内资源的调度配置。城市天然气管网密度太小,应急储备设施的储备能力"); |
| 57 |
ASSERT_EQUAL(entries[1].X, 42.52); |
| 58 |
ASSERT_EQUAL(entries[1].Y, 394.2); |
| 59 |
REQUIRE(entries[2].Text == "不足,供气的安全可靠性较差。天然气管网公平接入机制尚未建立和用气序列不合理,使得天然气供应安全难以得到"); |
| 60 |
ASSERT_EQUAL(entries[2].X, 42.52); |
| 61 |
ASSERT_EQUAL(entries[2].Y, 381.7); |
| 62 |
REQUIRE(entries[3].Text == "有效保障。"); |
| 63 |
ASSERT_EQUAL(entries[3].X, 42.52); |
| 64 |
ASSERT_EQUAL(entries[3].Y, 369.2); |
| 65 |
REQUIRE(entries[4].Text == "3.重点耗能行业能耗占比较大,产值占比较低"); |
| 66 |
ASSERT_EQUAL(entries[4].X, 44.59); |
| 67 |
ASSERT_EQUAL(entries[4].Y, 344.2); |
| 68 |
|
| 69 |
auto font = doc.GetFonts().GetCachedFont(PdfReference(7, 0)); |
| 70 |
REQUIRE(font != nullptr); |
| 71 |
auto str = PdfString::FromHexData("00205168770173af5c9b592971366c147ba17f515c1a672a6210578bff0c4e1c90e890e852065efa8bbe6ede540eff0c7ba17f517f3a4e4f7edf4e0089c45212ff0c7ba190537ba15f8430018bbe8ba1538b529b53c25dee4e0d9f50ff0c77015185652f5e72"); |
| 72 |
REQUIRE(font->GetEncoding().ConvertToUtf8(str) == " 全省环岛天然气管网尚未成型,东部部分建设滞后,管网缺乏统一规划,管道管径、设计压力参差不齐,省内支干"); |
| 73 |
} |
| 74 |
|
| 75 |
TEST_CASE("TextExtraction4") |
| 76 |
{ |
| 77 |
PdfMemDocument doc; |
| 78 |
doc.Load(TestUtils::GetTestInputFilePath("TextExtraction1.pdf")); |
| 79 |
|
| 80 |
auto& page = doc.GetPages().GetPageAt(0); |
| 81 |
vector<PdfTextEntry> entries; |
| 82 |
bool abort = false; |
| 83 |
PdfTextExtractParams params = {}; |
| 84 |
params.AbortCheck = [&](const AbortCheckInfo& info) { |
| 85 |
abort = info.ReadCount > 2; |
| 86 |
return abort; |
| 87 |
}; |
| 88 |
page.ExtractTextTo(entries, params); |
| 89 |
|
| 90 |
REQUIRE(abort); |
| 91 |
} |
| 92 |
|
| 93 |
TEST_CASE("TextExtraction5") |
| 94 |
{ |
| 95 |
// Extraction with quote operators |
| 96 |
PdfMemDocument doc; |
| 97 |
doc.Load(TestUtils::GetTestInputFilePath("TextExtraction5.pdf")); |
| 98 |
auto& page = doc.GetPages().GetPageAt(0); |
| 99 |
vector<PdfTextEntry> entries; |
| 100 |
page.ExtractTextTo(entries); |
| 101 |
REQUIRE(entries.size() == 4); |
| 102 |
REQUIRE(entries[0].Text == "Line 1"); |
| 103 |
ASSERT_EQUAL(entries[0].X, 10.0); |
| 104 |
ASSERT_EQUAL(entries[0].Y, 112.0); |
| 105 |
REQUIRE(entries[1].Text == "Line 2"); |
| 106 |
ASSERT_EQUAL(entries[1].X, 10.0); |
| 107 |
ASSERT_EQUAL(entries[1].Y, 94.0); |
| 108 |
|
| 109 |
REQUIRE(entries[2].Text == "Paragraph 2"); |
| 110 |
ASSERT_EQUAL(entries[2].X, 10.0); |
| 111 |
ASSERT_EQUAL(entries[2].Y, 64.0); |
| 112 |
REQUIRE(entries[3].Text == "Para 2, Line 2"); |
| 113 |
ASSERT_EQUAL(entries[3].X, 10.0); |
| 114 |
ASSERT_EQUAL(entries[3].Y, 46.0); |
| 115 |
} |
| 116 |
|
| 117 |
TEST_CASE("TextExtractionAllRotations") |
| 118 |
{ |
| 119 |
PdfMemDocument doc; |
| 120 |
doc.Load(TestUtils::GetTestInputFilePath("TextExtractionAllRotations.pdf")); |
| 121 |
|
| 122 |
struct ExpectedEntry |
| 123 |
{ |
| 124 |
double X; |
| 125 |
double Y; |
| 126 |
Rect BoundingBox; |
| 127 |
}; |
| 128 |
|
| 129 |
// One "HelloWorld" entry per page, each page using a different page rotation |
| 130 |
const ExpectedEntry expected[] = { |
| 131 |
{ 71.832, 772.534, Rect(71.832, 768.214, 58.08, 16.188) }, |
| 132 |
{ 81.8325, 515.2705, Rect(81.8325, 510.9505, 58.08, 16.188) }, |
| 133 |
{ 91.8325, 751.8565, Rect(91.8325, 747.5365, 58.08, 16.188) }, |
| 134 |
{ 101.8325, 495.2705, Rect(101.8325, 490.9505, 58.08, 16.188) }, |
| 135 |
}; |
| 136 |
|
| 137 |
REQUIRE(doc.GetPages().GetCount() == std::size(expected)); |
| 138 |
|
| 139 |
for (unsigned i = 0; i < doc.GetPages().GetCount(); i++) |
| 140 |
{ |
| 141 |
auto& page = doc.GetPages().GetPageAt(i); |
| 142 |
vector<PdfTextEntry> entries; |
| 143 |
PdfTextExtractParams params = {}; |
| 144 |
params.Flags = PdfTextExtractFlags::ComputeBoundingBox; |
| 145 |
page.ExtractTextTo(entries, params); |
| 146 |
|
| 147 |
REQUIRE(entries.size() == 1); |
| 148 |
auto& entry = entries[0]; |
| 149 |
REQUIRE(entry.Text == "HelloWorld"); |
| 150 |
ASSERT_EQUAL(entry.X, expected[i].X); |
| 151 |
ASSERT_EQUAL(entry.Y, expected[i].Y); |
| 152 |
REQUIRE(entry.BoundingBox.has_value()); |
| 153 |
auto& bbox = *entry.BoundingBox; |
| 154 |
ASSERT_EQUAL(bbox.X, expected[i].BoundingBox.X); |
| 155 |
ASSERT_EQUAL(bbox.Y, expected[i].BoundingBox.Y); |
| 156 |
ASSERT_EQUAL(bbox.Width, expected[i].BoundingBox.Width); |
| 157 |
ASSERT_EQUAL(bbox.Height, expected[i].BoundingBox.Height); |
| 158 |
|
| 159 |
// The painter aligns supplied coordinates to the canonical frame by default, |
| 160 |
// so the extracted bounding box can be drawn directly on rotated pages |
| 161 |
PdfPainter painter; |
| 162 |
painter.SetCanvas(page); |
| 163 |
painter.GraphicsState.SetStrokingColor(PdfColor(1.0, 0.0, 0.0)); |
| 164 |
painter.DrawRectangle(bbox, PdfPathDrawMode::Stroke); |
| 165 |
painter.FinishDrawing(); |
| 166 |
} |
| 167 |
|
| 168 |
doc.Save(TestUtils::GetTestOutputFilePath("TextExtractionAllRotations.pdf")); |
| 169 |
|
| 170 |
// Verify full content streams after painting the bounding boxes |
| 171 |
auto getContents = [](const PdfPage& pg) |
| 172 |
{ |
| 173 |
PdfCanvasInputDevice input(pg); |
| 174 |
string ret; |
| 175 |
StringStreamDevice output(ret); |
| 176 |
input.CopyTo(output); |
| 177 |
return ret; |
| 178 |
}; |
| 179 |
|
| 180 |
REQUIRE(getContents(doc.GetPages().GetPageAt(0)) == |
| 181 |
R"(q |
| 182 |
BT |
| 183 |
/C0_0 12 Tf |
| 184 |
71.832 772.534 Td |
| 185 |
<00290046004d004d0050>Tj |
| 186 |
1 0 0 rg |
| 187 |
<003800500053004d0045>Tj |
| 188 |
ET |
| 189 |
|
| 190 |
Q |
| 191 |
q |
| 192 |
1 0 0 RG |
| 193 |
71.832 768.214 58.08 16.188 re |
| 194 |
S |
| 195 |
Q |
| 196 |
)"); |
| 197 |
|
| 198 |
REQUIRE(getContents(doc.GetPages().GetPageAt(1)) == |
| 199 |
R"(q |
| 200 |
BT |
| 201 |
/C0_0 12 Tf |
| 202 |
0 1 -1 0 80.0335 81.8325 Tm |
| 203 |
<00290046004d004d0050>Tj |
| 204 |
1 0 0 rg |
| 205 |
<003800500053004d0045>Tj |
| 206 |
ET |
| 207 |
|
| 208 |
Q |
| 209 |
q |
| 210 |
0 1 -1 0 595.304 -0 cm |
| 211 |
1 0 0 RG |
| 212 |
81.8325 510.9505 58.08 16.188 re |
| 213 |
S |
| 214 |
Q |
| 215 |
)"); |
| 216 |
|
| 217 |
REQUIRE(getContents(doc.GetPages().GetPageAt(2)) == |
| 218 |
R"(q |
| 219 |
BT |
| 220 |
/C0_0 12 Tf |
| 221 |
-1 0 0 -1 503.4715 90.0335 Tm |
| 222 |
<00290046004d004d0050>Tj |
| 223 |
1 0 0 rg |
| 224 |
<003800500053004d0045>Tj |
| 225 |
ET |
| 226 |
|
| 227 |
Q |
| 228 |
q |
| 229 |
-1 0 -0 -1 595.304 841.89 cm |
| 230 |
1 0 0 RG |
| 231 |
91.8325 747.5365 58.08 16.188 re |
| 232 |
S |
| 233 |
Q |
| 234 |
)"); |
| 235 |
|
| 236 |
REQUIRE(getContents(doc.GetPages().GetPageAt(3)) == |
| 237 |
R"(q |
| 238 |
BT |
| 239 |
/C0_0 12 Tf |
| 240 |
0 -1 1 0 495.2705 740.0575 Tm |
| 241 |
<00290046004d004d0050>Tj |
| 242 |
1 0 0 rg |
| 243 |
<003800500053004d0045>Tj |
| 244 |
ET |
| 245 |
|
| 246 |
Q |
| 247 |
q |
| 248 |
-0 -1 1 -0 0 841.89 cm |
| 249 |
1 0 0 RG |
| 250 |
101.8325 490.9505 58.08 16.188 re |
| 251 |
S |
| 252 |
Q |
| 253 |
)"); |
| 254 |
} |
| 255 |
|
| 256 |
TEST_CASE("TestBfRange4CodePoints") |
| 257 |
{ |
| 258 |
// The /ToUnicode CMap has a beginbfrange whose DstCodeLo decodes to |
| 259 |
// exactly 4 codepoints |
| 260 |
PdfMemDocument doc; |
| 261 |
doc.Load(TestUtils::GetTestInputFilePath("TestBfRange4CodePoints.pdf")); |
| 262 |
auto& page = doc.GetPages().GetPageAt(0); |
| 263 |
vector<PdfTextEntry> entries; |
| 264 |
page.ExtractTextTo(entries); |
| 265 |
REQUIRE(entries.size() == 1); |
| 266 |
REQUIRE(entries[0].Text == "Aሴ噸C"); |
| 267 |
} |