Debian Codegraph @

podofo/test/unit/PageTest.cpp

raw
1 // SPDX-FileCopyrightText: 2009 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("TestEmptyContentsStream")
11 {
12 PdfMemDocument doc;
13 auto& page1 = doc.GetPages().CreatePage(PdfPageSize::A4);
14 REQUIRE(page1.GetDictionary().MustGetKey("Parent").GetReference() == doc.GetPages().GetObject().GetIndirectReference());
15 auto& annot1 = page1.GetAnnotations().CreateAnnot<PdfAnnotationPopup>(Rect(300.0, 20.0, 250.0, 50.0));
16 string_view title = "Author: Dominik Seichter";
17 annot1.SetContents(PdfString(title));
18 annot1.SetOpen(true);
19
20 string filename = TestUtils::GetTestOutputFilePath("testEmptyContentsStream.pdf");
21 doc.Save(filename);
22
23 // Read annotation again
24 PdfMemDocument doc2;
25 doc2.Load(filename);
26 REQUIRE(doc2.GetPages().GetCount() == 1);
27 auto& page2 = doc2.GetPages().GetPageAt(0);
28 REQUIRE(page2.GetAnnotations().GetCount() == 1);
29 auto& annot2 = page2.GetAnnotations().GetAnnotAt(0);
30 REQUIRE(*annot2.GetContents() == title);
31
32 auto& pageObj = page2.GetObject();
33 REQUIRE(!pageObj.GetDictionary().HasKey("Contents"));
34 }
35
36 TEST_CASE("TestRotations")
37 {
38 // The two documents are rotated but still portrait
39 PdfMemDocument doc;
40 {
41 doc.Load(TestUtils::GetTestInputFilePath("blank-rotated-90.pdf"));
42 auto& page = doc.GetPages().GetPageAt(0);
43 REQUIRE(page.GetRect() == Rect(0, 0, 595, 842));
44 REQUIRE(page.GetRectRaw() == Rect(0, 0, 842, 595));
45 auto& annot = page.GetAnnotations().CreateAnnot<PdfAnnotationWatermark>(Rect(100, 600, 80, 20));
46 REQUIRE(annot.GetRect() == Rect(100, 600, 80, 20));
47 REQUIRE(annot.GetRectRaw() == Rect(222, 99.999999999999986, 20, 79.999999999999986));
48 page.SetRect(Rect(0, 0, 500, 800));
49 REQUIRE(page.GetRect() == Rect(0, 0, 500, 800));
50 REQUIRE(page.GetRectRaw() == Rect(0, 0, 800, 500));
51 }
52
53 {
54 doc.Load(TestUtils::GetTestInputFilePath("blank-rotated-270.pdf"));
55 auto& page = doc.GetPages().GetPageAt(0);
56 REQUIRE(page.GetRect() == Rect(0, 0, 595, 842));
57 REQUIRE(page.GetRectRaw() == Rect(0, 0, 842, 595));
58 auto& annot = page.GetAnnotations().CreateAnnot<PdfAnnotationWatermark>(Rect(100, 600, 80, 20));
59 REQUIRE(annot.GetRect() == Rect(100, 600, 80, 20));
60 REQUIRE(annot.GetRectRaw() == Rect(600.00000000000011, 415, 20, 80));
61 annot.SetRect(Rect(100, 500, 100, 30));
62 REQUIRE(annot.GetRect() == Rect(100, 500.00000000000006, 100, 29.999999999999943));
63 REQUIRE(annot.GetRectRaw() == Rect(500.00000000000011, 395, 30, 100));
64 }
65 }
66
67 // FillFromPage must write BBox in form space (content-stream coordinates, pre-rotation
68 // MediaBox) and compute Matrix translation pivots from those content-space dimensions.
69 // Before the fix, BBox was written in post-rotation visual dimensions and the Matrix
70 // pivot values (e, f) were derived from the wrong dimension, causing displaced and
71 // clipped content when importing rotated source pages as Form XObjects.
72 struct FillFromPageExpected
73 {
74 double bboxX1, bboxY1, bboxX2, bboxY2;
75 double matA, matB, matC, matD, matE, matF;
76 };
77
78 static void testFillFromPage(const Rect& mediaBox, int rotation,
79 const FillFromPageExpected& expected)
80 {
81 PdfMemDocument srcDoc;
82 auto& srcPage = srcDoc.GetPages().CreatePage(mediaBox);
83 if (rotation != 0)
84 srcPage.SetRotation(rotation);
85
86 PdfMemDocument dstDoc;
87 auto xobj = dstDoc.CreateXObjectForm(Rect());
88 xobj->FillFromPage(srcPage);
89
90 auto& bboxArr = xobj->GetDictionary().FindKeyAs<PdfArray>("BBox");
91 REQUIRE(bboxArr[0].GetReal() == Catch::Detail::Approx(expected.bboxX1));
92 REQUIRE(bboxArr[1].GetReal() == Catch::Detail::Approx(expected.bboxY1));
93 REQUIRE(bboxArr[2].GetReal() == Catch::Detail::Approx(expected.bboxX2));
94 REQUIRE(bboxArr[3].GetReal() == Catch::Detail::Approx(expected.bboxY2));
95
96 auto& matrixArr = xobj->GetDictionary().FindKeyAs<PdfArray>("Matrix");
97 REQUIRE(matrixArr[0].GetReal() == Catch::Detail::Approx(expected.matA).margin(1e-10));
98 REQUIRE(matrixArr[1].GetReal() == Catch::Detail::Approx(expected.matB).margin(1e-10));
99 REQUIRE(matrixArr[2].GetReal() == Catch::Detail::Approx(expected.matC).margin(1e-10));
100 REQUIRE(matrixArr[3].GetReal() == Catch::Detail::Approx(expected.matD).margin(1e-10));
101 REQUIRE(matrixArr[4].GetReal() == Catch::Detail::Approx(expected.matE).margin(1e-6));
102 REQUIRE(matrixArr[5].GetReal() == Catch::Detail::Approx(expected.matF).margin(1e-6));
103 }
104
105 TEST_CASE("FillFromPage rotation at origin", "[PdfXObjectForm]")
106 {
107 Rect mediaBox(0, 0, 175, 72);
108
109 SECTION("0 degrees") {
110 testFillFromPage(mediaBox, 0,
111 { 0, 0, 175, 72, 1, 0, 0, 1, 0, 0 });
112 }
113 SECTION("90 degrees") {
114 testFillFromPage(mediaBox, 90,
115 { 0, 0, 175, 72, 0, -1, 1, 0, 0, 175 });
116 }
117 SECTION("180 degrees") {
118 testFillFromPage(mediaBox, 180,
119 { 0, 0, 175, 72, -1, 0, 0, -1, 175, 72 });
120 }
121 SECTION("270 degrees") {
122 testFillFromPage(mediaBox, 270,
123 { 0, 0, 175, 72, 0, 1, -1, 0, 72, 0 });
124 }
125 }
126
127 TEST_CASE("FillFromPage rotation with non-zero origin", "[PdfXObjectForm]")
128 {
129 // Non-zero origins appear after cropping or merge operations
130 Rect mediaBox(10, 20, 175, 72);
131
132 SECTION("0 degrees") {
133 testFillFromPage(mediaBox, 0,
134 { 10, 20, 185, 92, 1, 0, 0, 1, -10, -20 });
135 }
136 SECTION("90 degrees") {
137 testFillFromPage(mediaBox, 90,
138 { 10, 20, 185, 92, 0, -1, 1, 0, -20, 185 });
139 }
140 SECTION("180 degrees") {
141 testFillFromPage(mediaBox, 180,
142 { 10, 20, 185, 92, -1, 0, 0, -1, 185, 92 });
143 }
144 SECTION("270 degrees") {
145 testFillFromPage(mediaBox, 270,
146 { 10, 20, 185, 92, 0, 1, -1, 0, 92, -10 });
147 }
148 }
149
150 TEST_CASE("FillFromPage rotation with negative origin", "[PdfXObjectForm]")
151 {
152 // Negative origins appear in some PDF generators and after certain transformations
153 Rect mediaBox(-5, -10, 175, 72);
154
155 SECTION("0 degrees") {
156 testFillFromPage(mediaBox, 0,
157 { -5, -10, 170, 62, 1, 0, 0, 1, 5, 10 });
158 }
159 SECTION("90 degrees") {
160 testFillFromPage(mediaBox, 90,
161 { -5, -10, 170, 62, 0, -1, 1, 0, 10, 170 });
162 }
163 SECTION("180 degrees") {
164 testFillFromPage(mediaBox, 180,
165 { -5, -10, 170, 62, -1, 0, 0, -1, 170, 62 });
166 }
167 SECTION("270 degrees") {
168 testFillFromPage(mediaBox, 270,
169 { -5, -10, 170, 62, 0, 1, -1, 0, 62, 5 });
170 }
171 }
172
173 TEST_CASE("FillFromPage rotation with square page", "[PdfXObjectForm]")
174 {
175 // Square pages are a degenerate case where the W/H swap is a no-op
176 Rect mediaBox(0, 0, 200, 200);
177
178 SECTION("0 degrees") {
179 testFillFromPage(mediaBox, 0,
180 { 0, 0, 200, 200, 1, 0, 0, 1, 0, 0 });
181 }
182 SECTION("90 degrees") {
183 testFillFromPage(mediaBox, 90,
184 { 0, 0, 200, 200, 0, -1, 1, 0, 0, 200 });
185 }
186 SECTION("180 degrees") {
187 testFillFromPage(mediaBox, 180,
188 { 0, 0, 200, 200, -1, 0, 0, -1, 200, 200 });
189 }
190 SECTION("270 degrees") {
191 testFillFromPage(mediaBox, 270,
192 { 0, 0, 200, 200, 0, 1, -1, 0, 200, 0 });
193 }
194 }
195
196 TEST_CASE("FillFromPage rotation with offset square page", "[PdfXObjectForm]")
197 {
198 // Catches bugs only visible when both the square W==H no-op swap and origin normalization interact
199 Rect mediaBox(15, 25, 200, 200);
200
201 SECTION("0 degrees") {
202 testFillFromPage(mediaBox, 0,
203 { 15, 25, 215, 225, 1, 0, 0, 1, -15, -25 });
204 }
205 SECTION("90 degrees") {
206 testFillFromPage(mediaBox, 90,
207 { 15, 25, 215, 225, 0, -1, 1, 0, -25, 215 });
208 }
209 SECTION("180 degrees") {
210 testFillFromPage(mediaBox, 180,
211 { 15, 25, 215, 225, -1, 0, 0, -1, 215, 225 });
212 }
213 SECTION("270 degrees") {
214 testFillFromPage(mediaBox, 270,
215 { 15, 25, 215, 225, 0, 1, -1, 0, 225, -15 });
216 }
217 }
218
219 TEST_CASE("FillFromPage rotation with standard page sizes", "[PdfXObjectForm]")
220 {
221 SECTION("A4 portrait at 90 degrees") {
222 testFillFromPage(Rect(0, 0, 595, 842), 90,
223 { 0, 0, 595, 842, 0, -1, 1, 0, 0, 595 });
224 }
225 SECTION("US Letter portrait at 270 degrees") {
226 testFillFromPage(Rect(0, 0, 612, 792), 270,
227 { 0, 0, 612, 792, 0, 1, -1, 0, 792, 0 });
228 }
229 }
230
231 TEST_CASE("FillFromPage round-trip preserves content", "[PdfXObjectForm]")
232 {
233 // Guards against serialization silently dropping or corrupting BBox/Matrix
234 PdfMemDocument srcDoc;
235 auto& srcPage = srcDoc.GetPages().CreatePage(Rect(0, 0, 175, 72));
236 srcPage.SetRotation(270);
237
238 {
239 PdfPainter painter;
240 painter.SetCanvas(srcPage);
241 painter.GraphicsState.SetStrokingColor(PdfColor(1.0, 0.0, 0.0));
242 painter.DrawLine(0, 0, 175, 72);
243 painter.FinishDrawing();
244 }
245
246 PdfMemDocument dstDoc;
247 auto& dstPage = dstDoc.GetPages().CreatePage(Rect(0, 0, 400, 400));
248 auto xobj = dstDoc.CreateXObjectForm(Rect());
249 xobj->FillFromPage(srcPage);
250
251 string outputPath = TestUtils::GetTestOutputFilePath("fillFromPageRoundTrip.pdf");
252 {
253 PdfPainter painter;
254 painter.SetCanvas(dstPage);
255 painter.DrawXObject(*xobj, 50, 50);
256 painter.FinishDrawing();
257 }
258 dstDoc.Save(outputPath);
259
260 PdfMemDocument reloaded;
261 reloaded.Load(outputPath);
262 REQUIRE(reloaded.GetPages().GetCount() == 1);
263 }
264
265 TEST_CASE("TestFlattening")
266 {
267 PdfMemDocument doc;
268 doc.Load(TestUtils::GetTestInputFilePath("TechDocs", "pdf_implementation.pdf"));
269 doc.GetPages().FlattenStructure();
270 auto pageRootRef = doc.GetPages().GetObject().GetIndirectReference();
271 auto& dict = doc.GetPages().GetDictionary();
272 REQUIRE(dict.GetKey("Count")->GetNumber() == 11);
273 auto& kidsArr = dict.MustFindKey("Kids").GetArray();
274 REQUIRE(kidsArr.GetSize() == 11);
275 for (unsigned i = 0; i < dict.GetSize(); i++)
276 {
277 auto& child = kidsArr.MustFindAt(i);
278 REQUIRE(child.GetDictionary().MustGetKey("Type").GetName() == "Page");
279 REQUIRE(child.GetDictionary().MustGetKey("Parent").GetReference() == pageRootRef);
280 }
281 }
282
283 TEST_CASE("TestFillFromPageCopiesTransparencyGroup")
284 {
285 // Without the fix, FillFromPage dropped /Group, causing viewers to reset
286 // compositing to defaults and ignore isolation/knockout flags (issue #337)
287 PdfMemDocument sourceDoc;
288 auto& sourcePage = sourceDoc.GetPages().CreatePage(PdfPageSize::A4);
289
290 PdfDictionary groupDict;
291 groupDict.AddKey("Type"_n, PdfName("Group"));
292 groupDict.AddKey("S"_n, PdfName("Transparency"));
293 groupDict.AddKey("I"_n, PdfObject(true));
294 groupDict.AddKey("K"_n, PdfObject(false));
295 groupDict.AddKey("CS"_n, PdfName("DeviceRGB"));
296 sourcePage.GetDictionary().AddKey("Group"_n, groupDict);
297
298 PdfMemDocument destDoc;
299 auto xobj = destDoc.CreateXObjectForm(sourcePage.GetMediaBox());
300 xobj->FillFromPage(sourcePage);
301
302 auto* copiedGroup = xobj->GetDictionary().FindKey("Group");
303 REQUIRE(copiedGroup != nullptr);
304 REQUIRE(copiedGroup->GetDictionary().GetKey("S")->GetName() == "Transparency");
305 REQUIRE(copiedGroup->GetDictionary().GetKey("I")->GetBool() == true);
306 REQUIRE(copiedGroup->GetDictionary().GetKey("K")->GetBool() == false);
307 REQUIRE(copiedGroup->GetDictionary().GetKey("CS")->GetName() == "DeviceRGB");
308 }
309
310 TEST_CASE("TestFillFromPageCopiesGroupSameDocument")
311 {
312 // The same-document path skips object remapping, so it exercises a
313 // different branch in FillXObjectFromPage than the cross-document test
314 PdfMemDocument doc;
315 auto& page = doc.GetPages().CreatePage(PdfPageSize::A4);
316
317 PdfDictionary groupDict;
318 groupDict.AddKey("Type"_n, PdfName("Group"));
319 groupDict.AddKey("S"_n, PdfName("Transparency"));
320 groupDict.AddKey("CS"_n, PdfName("DeviceCMYK"));
321 page.GetDictionary().AddKey("Group"_n, groupDict);
322
323 auto xobj = doc.CreateXObjectForm(page.GetMediaBox());
324 xobj->FillFromPage(page);
325
326 auto* copiedGroup = xobj->GetDictionary().FindKey("Group");
327 REQUIRE(copiedGroup != nullptr);
328 REQUIRE(copiedGroup->GetDictionary().GetKey("S")->GetName() == "Transparency");
329 REQUIRE(copiedGroup->GetDictionary().GetKey("CS")->GetName() == "DeviceCMYK");
330 }
331
332 TEST_CASE("TestFillFromPageWithoutGroupDoesNotCreateOne")
333 {
334 PdfMemDocument sourceDoc;
335 auto& sourcePage = sourceDoc.GetPages().CreatePage(PdfPageSize::A4);
336
337 PdfMemDocument destDoc;
338 auto xobj = destDoc.CreateXObjectForm(sourcePage.GetMediaBox());
339 xobj->FillFromPage(sourcePage);
340
341 REQUIRE(xobj->GetDictionary().FindKey("Group") == nullptr);
342 }
343
344 TEST_CASE("TestFillFromPageWithoutGroupSameDocument")
345 {
346 PdfMemDocument doc;
347 auto& page = doc.GetPages().CreatePage(PdfPageSize::A4);
348
349 auto xobj = doc.CreateXObjectForm(page.GetMediaBox());
350 xobj->FillFromPage(page);
351
352 REQUIRE(xobj->GetDictionary().FindKey("Group") == nullptr);
353 }