/src/poppler/poppler/Page.cc
Line | Count | Source |
1 | | //======================================================================== |
2 | | // |
3 | | // Page.cc |
4 | | // |
5 | | // Copyright 1996-2007 Glyph & Cog, LLC |
6 | | // |
7 | | //======================================================================== |
8 | | |
9 | | //======================================================================== |
10 | | // |
11 | | // Modified under the Poppler project - http://poppler.freedesktop.org |
12 | | // |
13 | | // All changes made under the Poppler project to this file are licensed |
14 | | // under GPL version 2 or later |
15 | | // |
16 | | // Copyright (C) 2005 Kristian Høgsberg <krh@redhat.com> |
17 | | // Copyright (C) 2005 Jeff Muizelaar <jeff@infidigm.net> |
18 | | // Copyright (C) 2005-2013, 2016-2026 Albert Astals Cid <aacid@kde.org> |
19 | | // Copyright (C) 2006-2008 Pino Toscano <pino@kde.org> |
20 | | // Copyright (C) 2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru> |
21 | | // Copyright (C) 2006 Scott Turner <scotty1024@mac.com> |
22 | | // Copyright (C) 2006-2011, 2015 Carlos Garcia Campos <carlosgc@gnome.org> |
23 | | // Copyright (C) 2007 Julien Rebetez <julienr@svn.gnome.org> |
24 | | // Copyright (C) 2008 Iñigo Martínez <inigomartinez@gmail.com> |
25 | | // Copyright (C) 2008 Brad Hards <bradh@kde.org> |
26 | | // Copyright (C) 2008 Ilya Gorenbein <igorenbein@finjan.com> |
27 | | // Copyright (C) 2012, 2013 Fabio D'Urso <fabiodurso@hotmail.it> |
28 | | // Copyright (C) 2013, 2014 Thomas Freitag <Thomas.Freitag@alfa.de> |
29 | | // Copyright (C) 2013 Jason Crain <jason@aquaticape.us> |
30 | | // Copyright (C) 2013, 2017, 2023 Adrian Johnson <ajohnson@redneon.com> |
31 | | // Copyright (C) 2015 Philipp Reinkemeier <philipp.reinkemeier@offis.de> |
32 | | // Copyright (C) 2018, 2019 Adam Reichold <adam.reichold@t-online.de> |
33 | | // Copyright (C) 2020 Oliver Sander <oliver.sander@tu-dresden.de> |
34 | | // Copyright (C) 2020, 2021, 2025, 2026 Nelson Benítez León <nbenitezl@gmail.com> |
35 | | // Copyright (C) 2020 Philipp Knechtges <philipp-dev@knechtges.com> |
36 | | // Copyright (C) 2024 Pablo Correa Gómez <ablocorrea@hotmail.com> |
37 | | // Copyright (C) 2024-2026 g10 Code GmbH, Author: Sune Stolborg Vuorela <sune@vuorela.dk> |
38 | | // Copyright (C) 2025 Stefan Brüns <stefan.bruens@rwth-aachen.de> |
39 | | // Copyright (C) 2025 Arnav V <arnav0872@gmail.com> |
40 | | // Copyright (C) 2026 Adam Sampson <ats@offog.org> |
41 | | // Copyright (C) 2026 Lucas Baudin <lbaudin@gnome.org> |
42 | | // |
43 | | // To see a description of the changes please see the Changelog file that |
44 | | // came with your tarball or type make ChangeLog if you are building from git |
45 | | // |
46 | | //======================================================================== |
47 | | |
48 | | #include <config.h> |
49 | | |
50 | | #include <climits> |
51 | | #include "GlobalParams.h" |
52 | | #include "Object.h" |
53 | | #include "Array.h" |
54 | | #include "Dict.h" |
55 | | #include "PDFDoc.h" |
56 | | #include "XRef.h" |
57 | | #include "Link.h" |
58 | | #include "OutputDev.h" |
59 | | #include "Gfx.h" |
60 | | #include "GfxState.h" |
61 | | #include "Annot.h" |
62 | | #include "TextOutputDev.h" |
63 | | #include "Form.h" |
64 | | #include "Error.h" |
65 | | #include "Page.h" |
66 | | #include "Catalog.h" |
67 | | #include "goo/gmem.h" |
68 | | |
69 | | //------------------------------------------------------------------------ |
70 | | // PDFRectangle |
71 | | //------------------------------------------------------------------------ |
72 | | |
73 | | namespace { |
74 | | namespace testing { |
75 | | static_assert(PDFRectangle {} == PDFRectangle {}); |
76 | | static_assert(PDFRectangle { 0, 0, 1, 1 } == PDFRectangle { 0, 0, 1, 1 }); |
77 | | static_assert(PDFRectangle { 0, 0, 1, 1 }.isValid()); |
78 | | static_assert(!PDFRectangle { 0, 0, 0, 0 }.isValid()); |
79 | | static_assert(PDFRectangle { 0, 0, 2, 2 }.contains(1, 1)); |
80 | | static_assert(!PDFRectangle { 0, 0, 1, 2 }.contains(2, 1)); |
81 | | |
82 | | constexpr PDFRectangle r1 { 0, 0, 5, 5 }; |
83 | | constexpr PDFRectangle r2 { 2, 2, 4, 4 }; |
84 | | constexpr PDFRectangle r3 { 0, 3, 3, 3 }; |
85 | | constexpr PDFRectangle r4 { 2, 3, 3, 3 }; |
86 | | static_assert(r1.isValid()); |
87 | | static_assert([]() { |
88 | | auto r = r1; |
89 | | r.clipTo(r1); |
90 | | return r == r1; |
91 | | }()); |
92 | | static_assert([]() { |
93 | | auto r = r1; |
94 | | r.clipTo(r2); |
95 | | return r == r2; |
96 | | }()); |
97 | | static_assert([]() { |
98 | | auto r = r2; |
99 | | r.clipTo(r1); |
100 | | return r == r2; |
101 | | }()); |
102 | | static_assert([]() { |
103 | | auto r = r2; |
104 | | r.clipTo(r3); |
105 | | return r == r4; |
106 | | }()); |
107 | | } // namespace testing |
108 | | } // namespace |
109 | | |
110 | | //------------------------------------------------------------------------ |
111 | | // PageAttrs |
112 | | //------------------------------------------------------------------------ |
113 | | |
114 | | PageAttrs::PageAttrs(const PageAttrs *attrs, Dict *dict) |
115 | 8.78k | { |
116 | 8.78k | Object obj1; |
117 | 8.78k | PDFRectangle mBox; |
118 | 8.78k | const bool isPage = dict->is("Page"); |
119 | | |
120 | | // get old/default values |
121 | 8.78k | if (attrs) { |
122 | 4.20k | mediaBox = attrs->mediaBox; |
123 | 4.20k | cropBox = attrs->cropBox; |
124 | 4.20k | haveCropBox = attrs->haveCropBox; |
125 | 4.20k | rotate = attrs->rotate; |
126 | 4.20k | resources = attrs->resources.copy(); |
127 | 4.58k | } else { |
128 | | // set default MediaBox to 8.5" x 11" -- this shouldn't be necessary |
129 | | // but some (non-compliant) PDF files don't specify a MediaBox |
130 | 4.58k | mediaBox.x1 = 0; |
131 | 4.58k | mediaBox.y1 = 0; |
132 | 4.58k | mediaBox.x2 = 612; |
133 | 4.58k | mediaBox.y2 = 792; |
134 | 4.58k | cropBox.x1 = cropBox.y1 = cropBox.x2 = cropBox.y2 = 0; |
135 | 4.58k | haveCropBox = false; |
136 | 4.58k | rotate = 0; |
137 | 4.58k | resources.setToNull(); |
138 | 4.58k | } |
139 | | |
140 | | // media box |
141 | 8.78k | if (readBox(dict, "MediaBox", &mBox)) { |
142 | 7.90k | mediaBox = mBox; |
143 | 7.90k | } |
144 | | |
145 | | // crop box |
146 | 8.78k | if (readBox(dict, "CropBox", &cropBox)) { |
147 | 0 | haveCropBox = true; |
148 | 0 | } |
149 | 8.78k | if (!haveCropBox) { |
150 | 8.78k | cropBox = mediaBox; |
151 | 8.78k | } |
152 | | |
153 | 8.78k | if (isPage) { |
154 | | // cropBox can not be bigger than mediaBox |
155 | 4.27k | if (cropBox.x2 - cropBox.x1 > mediaBox.x2 - mediaBox.x1) { |
156 | 0 | cropBox.x1 = mediaBox.x1; |
157 | 0 | cropBox.x2 = mediaBox.x2; |
158 | 0 | } |
159 | 4.27k | if (cropBox.y2 - cropBox.y1 > mediaBox.y2 - mediaBox.y1) { |
160 | 0 | cropBox.y1 = mediaBox.y1; |
161 | 0 | cropBox.y2 = mediaBox.y2; |
162 | 0 | } |
163 | 4.27k | } |
164 | | |
165 | | // other boxes |
166 | 8.78k | bleedBox = cropBox; |
167 | 8.78k | readBox(dict, "BleedBox", &bleedBox); |
168 | 8.78k | trimBox = cropBox; |
169 | 8.78k | readBox(dict, "TrimBox", &trimBox); |
170 | 8.78k | artBox = cropBox; |
171 | 8.78k | readBox(dict, "ArtBox", &artBox); |
172 | | |
173 | | // rotate |
174 | 8.78k | obj1 = dict->lookup("Rotate"); |
175 | 8.78k | if (obj1.isInt()) { |
176 | 0 | rotate = obj1.getInt(); |
177 | 0 | } |
178 | 8.78k | while (rotate < 0) { |
179 | 0 | rotate += 360; |
180 | 0 | } |
181 | 8.78k | while (rotate >= 360) { |
182 | 0 | rotate -= 360; |
183 | 0 | } |
184 | | |
185 | | // misc attributes |
186 | 8.78k | lastModified = dict->lookup("LastModified"); |
187 | 8.78k | boxColorInfo = dict->lookup("BoxColorInfo"); |
188 | 8.78k | group = dict->lookup("Group"); |
189 | 8.78k | metadata = dict->lookup("Metadata"); |
190 | 8.78k | pieceInfo = dict->lookup("PieceInfo"); |
191 | 8.78k | separationInfo = dict->lookup("SeparationInfo"); |
192 | | |
193 | | // resource dictionary |
194 | 8.78k | Object objResources = dict->lookup("Resources"); |
195 | 8.78k | if (objResources.isDict()) { |
196 | 7.54k | resources = std::move(objResources); |
197 | 7.54k | } |
198 | 8.78k | } |
199 | | |
200 | 8.78k | PageAttrs::~PageAttrs() = default; |
201 | | |
202 | | void PageAttrs::clipBoxes() |
203 | 4.27k | { |
204 | 4.27k | cropBox.clipTo(mediaBox); |
205 | 4.27k | bleedBox.clipTo(mediaBox); |
206 | 4.27k | trimBox.clipTo(mediaBox); |
207 | 4.27k | artBox.clipTo(mediaBox); |
208 | 4.27k | } |
209 | | |
210 | | bool PageAttrs::readBox(Dict *dict, const char *key, PDFRectangle *box) |
211 | 43.9k | { |
212 | 43.9k | PDFRectangle tmp; |
213 | 43.9k | double t; |
214 | 43.9k | Object obj1, obj2; |
215 | 43.9k | bool ok; |
216 | | |
217 | 43.9k | obj1 = dict->lookup(key); |
218 | 43.9k | if (obj1.isArrayOfLength(4)) { |
219 | 8.06k | ok = true; |
220 | 8.06k | obj2 = obj1.arrayGet(0); |
221 | 8.06k | if (obj2.isNum()) { |
222 | 8.00k | tmp.x1 = obj2.getNum(); |
223 | 8.00k | } else { |
224 | 58 | ok = false; |
225 | 58 | } |
226 | 8.06k | obj2 = obj1.arrayGet(1); |
227 | 8.06k | if (obj2.isNum()) { |
228 | 7.99k | tmp.y1 = obj2.getNum(); |
229 | 7.99k | } else { |
230 | 69 | ok = false; |
231 | 69 | } |
232 | 8.06k | obj2 = obj1.arrayGet(2); |
233 | 8.06k | if (obj2.isNum()) { |
234 | 8.03k | tmp.x2 = obj2.getNum(); |
235 | 8.03k | } else { |
236 | 22 | ok = false; |
237 | 22 | } |
238 | 8.06k | obj2 = obj1.arrayGet(3); |
239 | 8.06k | if (obj2.isNum()) { |
240 | 8.04k | tmp.y2 = obj2.getNum(); |
241 | 8.04k | } else { |
242 | 20 | ok = false; |
243 | 20 | } |
244 | 8.06k | if (tmp.x1 == 0 && tmp.x2 == 0 && tmp.y1 == 0 && tmp.y2 == 0) { |
245 | 4 | ok = false; |
246 | 4 | } |
247 | 8.06k | if (ok) { |
248 | 7.90k | if (tmp.x1 > tmp.x2) { |
249 | 6 | t = tmp.x1; |
250 | 6 | tmp.x1 = tmp.x2; |
251 | 6 | tmp.x2 = t; |
252 | 6 | } |
253 | 7.90k | if (tmp.y1 > tmp.y2) { |
254 | 29 | t = tmp.y1; |
255 | 29 | tmp.y1 = tmp.y2; |
256 | 29 | tmp.y2 = t; |
257 | 29 | } |
258 | 7.90k | *box = tmp; |
259 | 7.90k | } |
260 | 35.8k | } else { |
261 | 35.8k | ok = false; |
262 | 35.8k | } |
263 | 43.9k | return ok; |
264 | 43.9k | } |
265 | | |
266 | | //------------------------------------------------------------------------ |
267 | | // Page |
268 | | //------------------------------------------------------------------------ |
269 | | |
270 | 4.25k | #define pageLocker() const std::scoped_lock locker(mutex) |
271 | | |
272 | 4.27k | Page::Page(PDFDoc *docA, int numA, Object &&pageDict, Ref pageRefA, std::unique_ptr<PageAttrs> attrsA) : pageRef(pageRefA), attrs(std::move(attrsA)) |
273 | 4.27k | { |
274 | 4.27k | ok = true; |
275 | 4.27k | doc = docA; |
276 | 4.27k | xref = doc->getXRef(); |
277 | 4.27k | num = numA; |
278 | 4.27k | duration = -1; |
279 | 4.27k | annots = nullptr; |
280 | 4.27k | structParents = -1; |
281 | | |
282 | 4.27k | pageObj = std::move(pageDict); |
283 | | |
284 | | // get attributes |
285 | 4.27k | attrs->clipBoxes(); |
286 | | |
287 | | // transtion |
288 | 4.27k | trans = pageObj.dictLookupNF("Trans").copy(); |
289 | 4.27k | if (!(trans.isRef() || trans.isDict() || trans.isNull())) { |
290 | 1 | error(errSyntaxError, -1, "Page transition object (page {0:d}) is wrong type ({1:s})", num, trans.getTypeName()); |
291 | 1 | trans = Object(); |
292 | 1 | } |
293 | | |
294 | | // duration |
295 | 4.27k | const Object &tmp = pageObj.dictLookupNF("Dur"); |
296 | 4.27k | if (!(tmp.isNum() || tmp.isNull())) { |
297 | 0 | error(errSyntaxError, -1, "Page duration object (page {0:d}) is wrong type ({1:s})", num, tmp.getTypeName()); |
298 | 4.27k | } else if (tmp.isNum()) { |
299 | 0 | duration = tmp.getNum(); |
300 | 0 | } |
301 | | |
302 | | // structParents |
303 | 4.27k | const Object &tmp2 = pageObj.dictLookup("StructParents"); |
304 | 4.27k | if (!(tmp2.isInt() || tmp2.isNull())) { |
305 | 6 | error(errSyntaxError, -1, "Page StructParents object (page {0:d}) is wrong type ({1:s})", num, tmp2.getTypeName()); |
306 | 4.26k | } else if (tmp2.isInt()) { |
307 | 2.74k | structParents = tmp2.getInt(); |
308 | 2.74k | } |
309 | | |
310 | | // annotations |
311 | 4.27k | annotsObj = pageObj.dictLookupNF("Annots").copy(); |
312 | 4.27k | if (!(annotsObj.isRef() || annotsObj.isArray() || annotsObj.isNull())) { |
313 | 0 | error(errSyntaxError, -1, "Page annotations object (page {0:d}) is wrong type ({1:s})", num, annotsObj.getTypeName()); |
314 | 0 | annotsObj.setToNull(); |
315 | 0 | } |
316 | | |
317 | 4.27k | if (annotsObj.isArrayOfLengthAtLeast(10000)) { |
318 | 0 | error(errSyntaxError, -1, "Page annotations object (page {0:d}) is likely malformed. Too big: ({1:d})", num, annotsObj.arrayGetLength()); |
319 | 0 | goto err2; |
320 | 0 | } |
321 | 4.27k | if (annotsObj.isRef()) { |
322 | 3 | auto resolvedObj = getAnnotsObject(); |
323 | 3 | if (resolvedObj.isArrayOfLengthAtLeast(10000)) { |
324 | 0 | error(errSyntaxError, -1, "Page annotations object (page {0:d}) is likely malformed. Too big: ({1:d})", num, resolvedObj.arrayGetLength()); |
325 | 0 | goto err2; |
326 | 0 | } |
327 | 3 | if (!resolvedObj.isArray()) { |
328 | 3 | if (!resolvedObj.isNull()) { |
329 | 3 | error(errSyntaxError, -1, "Page annotations object (page {0:d}) is wrong type ({1:s})", num, resolvedObj.getTypeName()); |
330 | 3 | } |
331 | 3 | annotsObj.setToNull(); |
332 | 3 | } |
333 | 3 | } |
334 | | |
335 | | // contents |
336 | 4.27k | contents = pageObj.dictLookupNF("Contents").copy(); |
337 | 4.27k | if (!(contents.isRef() || contents.isArray() || contents.isNull())) { |
338 | 18 | error(errSyntaxError, -1, "Page contents object (page {0:d}) is wrong type ({1:s})", num, contents.getTypeName()); |
339 | 18 | goto err1; |
340 | 18 | } |
341 | | |
342 | | // thumb |
343 | 4.25k | thumb = pageObj.dictLookupNF("Thumb").copy(); |
344 | 4.25k | if (!(thumb.isStream() || thumb.isNull() || thumb.isRef())) { |
345 | 4 | error(errSyntaxError, -1, "Page thumb object (page {0:d}) is wrong type ({1:s})", num, thumb.getTypeName()); |
346 | 4 | thumb.setToNull(); |
347 | 4 | } |
348 | | |
349 | | // actions |
350 | 4.25k | actions = pageObj.dictLookupNF("AA").copy(); |
351 | 4.25k | if (!(actions.isDict() || actions.isNull())) { |
352 | 0 | error(errSyntaxError, -1, "Page additional action object (page {0:d}) is wrong type ({1:s})", num, actions.getTypeName()); |
353 | 0 | actions.setToNull(); |
354 | 0 | } |
355 | | |
356 | 4.25k | return; |
357 | | |
358 | 0 | err2: |
359 | 0 | annotsObj.setToNull(); |
360 | 18 | err1: |
361 | 18 | contents.setToNull(); |
362 | 18 | ok = false; |
363 | 18 | } |
364 | | |
365 | 4.27k | Page::~Page() = default; |
366 | | |
367 | | Dict *Page::getResourceDict() |
368 | 0 | { |
369 | 0 | return attrs->getResourceDict(); |
370 | 0 | } |
371 | | |
372 | | Object *Page::getResourceDictObject() |
373 | 0 | { |
374 | 0 | return attrs->getResourceDictObject(); |
375 | 0 | } |
376 | | |
377 | | std::unique_ptr<Dict> Page::getResourceDictCopy(XRef *xrefA) |
378 | 0 | { |
379 | 0 | pageLocker(); |
380 | 0 | Dict *dict = attrs->getResourceDict(); |
381 | 0 | return dict ? dict->copy(xrefA) : nullptr; |
382 | 0 | } |
383 | | |
384 | | void Page::replaceXRef(XRef *xrefA) |
385 | 8.51k | { |
386 | 8.51k | std::unique_ptr<Dict> pageDict = pageObj.getDict()->copy(xrefA); |
387 | 8.51k | xref = xrefA; |
388 | 8.51k | trans = pageDict->lookupNF("Trans").copy(); |
389 | 8.51k | annotsObj = pageDict->lookupNF("Annots").copy(); |
390 | 8.51k | contents = pageDict->lookupNF("Contents").copy(); |
391 | 8.51k | if (contents.isArray()) { |
392 | 28 | contents = Object(contents.getArray()->copy(xrefA)); |
393 | 28 | } |
394 | 8.51k | thumb = pageDict->lookupNF("Thumb").copy(); |
395 | 8.51k | actions = pageDict->lookupNF("AA").copy(); |
396 | 8.51k | Object resources = pageDict->lookup("Resources"); |
397 | 8.51k | if (resources.isDict()) { |
398 | 7.86k | attrs->replaceResource(std::move(resources)); |
399 | 7.86k | } |
400 | 8.51k | } |
401 | | |
402 | | /* Loads standalone fields into Page, should be called once per page only */ |
403 | | void Page::loadStandaloneFields(Form *form) |
404 | 4.25k | { |
405 | | /* Look for standalone annots, identified by being: 1) of type Widget |
406 | | * 2) not referenced from the Catalog's Form Field array */ |
407 | 4.25k | for (const std::shared_ptr<Annot> &annot : annots->getAnnots()) { |
408 | |
|
409 | 0 | if (annot->getType() != Annot::typeWidget || !annot->getHasRef()) { |
410 | 0 | continue; |
411 | 0 | } |
412 | | |
413 | 0 | const Ref r = annot->getRef(); |
414 | 0 | if (form && form->findWidgetByRef(r)) { |
415 | 0 | continue; // this annot is referenced inside Form, skip it |
416 | 0 | } |
417 | | |
418 | 0 | std::set<int> parents; |
419 | 0 | std::unique_ptr<FormField> field = Form::createFieldFromDict(annot->getAnnotObj().copy(), annot->getDoc(), r, nullptr, &parents); |
420 | |
|
421 | 0 | if (field && field->getNumWidgets() == 1) { |
422 | |
|
423 | 0 | auto aw = std::static_pointer_cast<AnnotWidget>(annot); |
424 | 0 | aw->setField(field.get()); |
425 | |
|
426 | 0 | field->setStandAlone(true); |
427 | 0 | FormWidget *formWidget = field->getWidget(0); |
428 | |
|
429 | 0 | if (!formWidget->getWidgetAnnotation()) { |
430 | 0 | formWidget->setWidgetAnnotation(aw); |
431 | 0 | } |
432 | |
|
433 | 0 | standaloneFields.push_back(std::move(field)); |
434 | 0 | } |
435 | 0 | } |
436 | 4.25k | } |
437 | | |
438 | | void Page::initializeAnnots() |
439 | 4.25k | { |
440 | 4.25k | Object obj = getAnnotsObject(); |
441 | 4.25k | annots = std::make_unique<Annots>(doc, num, &obj); |
442 | | // Load standalone fields once for the page |
443 | 4.25k | loadStandaloneFields(doc->getCatalog()->getForm()); |
444 | 4.25k | } |
445 | | |
446 | | Annots *Page::getAnnots() |
447 | 4.25k | { |
448 | 4.25k | std::call_once(annotsInitializationFlag, std::mem_fn(&Page::initializeAnnots), this); |
449 | | |
450 | 4.25k | return annots.get(); |
451 | 4.25k | } |
452 | | |
453 | | bool Page::addAnnot(const std::shared_ptr<Annot> &annot) |
454 | 0 | { |
455 | 0 | if (unlikely(xref->getEntry(pageRef.num)->type == xrefEntryFree)) { |
456 | | // something very wrong happened if we're here |
457 | 0 | error(errInternal, -1, "Can not addAnnot to page with an invalid ref"); |
458 | 0 | return false; |
459 | 0 | } |
460 | | |
461 | 0 | const Ref annotRef = annot->getRef(); |
462 | | |
463 | | // Make sure we have annots before adding the new one |
464 | | // even if it's an empty list so that we can safely |
465 | | // call annots->appendAnnot(annot) |
466 | 0 | pageLocker(); |
467 | 0 | getAnnots(); |
468 | |
|
469 | 0 | if (annotsObj.isNull()) { |
470 | 0 | Ref annotsRef; |
471 | | // page doesn't have annots array, |
472 | | // we have to create it |
473 | |
|
474 | 0 | auto annotsArray = std::make_unique<Array>(xref); |
475 | 0 | annotsArray->add(Object(annotRef)); |
476 | |
|
477 | 0 | annotsRef = xref->addIndirectObject(Object(std::move(annotsArray))); |
478 | 0 | annotsObj = Object(annotsRef); |
479 | 0 | pageObj.dictSet("Annots", Object(annotsRef)); |
480 | 0 | xref->setModifiedObject(&pageObj, pageRef); |
481 | 0 | } else { |
482 | 0 | Object obj1 = getAnnotsObject(); |
483 | 0 | if (obj1.isArray()) { |
484 | 0 | obj1.getArray()->add(Object(annotRef)); |
485 | 0 | if (annotsObj.isRef()) { |
486 | 0 | xref->setModifiedObject(&obj1, annotsObj.getRef()); |
487 | 0 | } else { |
488 | 0 | xref->setModifiedObject(&pageObj, pageRef); |
489 | 0 | } |
490 | 0 | } |
491 | 0 | } |
492 | | |
493 | | // Popup annots are already handled by markup annots, |
494 | | // so add to the list only Popup annots without a |
495 | | // markup annotation associated. |
496 | 0 | if (annot->getType() != Annot::typePopup || !static_cast<AnnotPopup *>(annot.get())->hasParent()) { |
497 | 0 | annots->appendAnnot(annot); |
498 | 0 | } |
499 | 0 | annot->setPage(num, true); |
500 | |
|
501 | 0 | auto *annotMarkup = dynamic_cast<AnnotMarkup *>(annot.get()); |
502 | 0 | if (annotMarkup) { |
503 | 0 | std::shared_ptr<AnnotPopup> annotPopup = annotMarkup->getPopup(); |
504 | 0 | if (annotPopup) { |
505 | 0 | addAnnot(annotPopup); |
506 | 0 | } |
507 | 0 | } |
508 | |
|
509 | 0 | return true; |
510 | 0 | } |
511 | | |
512 | | void Page::removeAnnot(const std::shared_ptr<Annot> &annot) |
513 | 0 | { |
514 | 0 | Ref annotRef = annot->getRef(); |
515 | |
|
516 | 0 | pageLocker(); |
517 | 0 | Object annArrayObj = getAnnotsObject(); |
518 | 0 | if (annArrayObj.isArray()) { |
519 | 0 | Array *annArray = annArrayObj.getArray(); |
520 | 0 | int idx = -1; |
521 | | // Get annotation position |
522 | 0 | for (int i = 0; idx == -1 && i < annArray->getLength(); ++i) { |
523 | 0 | const Object &tmp = annArray->getNF(i); |
524 | 0 | if (tmp.isRef()) { |
525 | 0 | const Ref currAnnot = tmp.getRef(); |
526 | 0 | if (currAnnot == annotRef) { |
527 | 0 | idx = i; |
528 | 0 | } |
529 | 0 | } |
530 | 0 | } |
531 | |
|
532 | 0 | if (idx == -1) { |
533 | 0 | error(errInternal, -1, "Annotation doesn't belong to this page"); |
534 | 0 | return; |
535 | 0 | } |
536 | 0 | annots->removeAnnot(annot); // Gracefully fails on popup windows |
537 | 0 | annArray->remove(idx); |
538 | |
|
539 | 0 | if (annotsObj.isRef()) { |
540 | 0 | xref->setModifiedObject(&annArrayObj, annotsObj.getRef()); |
541 | 0 | } else { |
542 | 0 | xref->setModifiedObject(&pageObj, pageRef); |
543 | 0 | } |
544 | 0 | } |
545 | 0 | annot->removeReferencedObjects(); // Note: Might recurse in removeAnnot again |
546 | 0 | if (annArrayObj.isArray()) { |
547 | 0 | xref->removeIndirectObject(annotRef); |
548 | 0 | } |
549 | 0 | annot->setPage(0, false); |
550 | 0 | } |
551 | | |
552 | | std::unique_ptr<Links> Page::getLinks() |
553 | 0 | { |
554 | 0 | return std::make_unique<Links>(getAnnots()); |
555 | 0 | } |
556 | | |
557 | | std::unique_ptr<FormPageWidgets> Page::getFormWidgets() |
558 | 0 | { |
559 | 0 | auto frmPageWidgets = std::make_unique<FormPageWidgets>(getAnnots(), num, doc->getCatalog()->getForm()); |
560 | 0 | frmPageWidgets->addWidgets(standaloneFields, num); |
561 | |
|
562 | 0 | return frmPageWidgets; |
563 | 0 | } |
564 | | |
565 | | void Page::display(OutputDev *out, double hDPI, double vDPI, int rotate, bool useMediaBox, bool crop, bool printing, bool (*abortCheckCbk)(void *data), void *abortCheckCbkData, bool (*annotDisplayDecideCbk)(Annot *annot, void *user_data), |
566 | | void *annotDisplayDecideCbkData, bool copyXRef) |
567 | 0 | { |
568 | 0 | displaySlice(out, hDPI, vDPI, rotate, useMediaBox, crop, -1, -1, -1, -1, printing, abortCheckCbk, abortCheckCbkData, annotDisplayDecideCbk, annotDisplayDecideCbkData, copyXRef); |
569 | 0 | } |
570 | | |
571 | | std::unique_ptr<Gfx> Page::createGfx(OutputDev *out, double hDPI, double vDPI, int rotate, bool useMediaBox, bool crop, int sliceX, int sliceY, int sliceW, int sliceH, bool (*abortCheckCbk)(void *data), void *abortCheckCbkData, XRef *xrefA) |
572 | 4.25k | { |
573 | 4.25k | const PDFRectangle &mediaBox = getMediaBox(); |
574 | 4.25k | const PDFRectangle &cropBox = getCropBox(); |
575 | 4.25k | PDFRectangle box; |
576 | | |
577 | 4.25k | rotate += getRotate(); |
578 | 4.25k | if (rotate >= 360) { |
579 | 0 | rotate -= 360; |
580 | 4.25k | } else if (rotate < 0) { |
581 | 0 | rotate += 360; |
582 | 0 | } |
583 | | |
584 | 4.25k | makeBox(hDPI, vDPI, rotate, useMediaBox, out->upsideDown(), sliceX, sliceY, sliceW, sliceH, &box, &crop); |
585 | 4.25k | if (globalParams->getPrintCommands()) { |
586 | 0 | printf("***** MediaBox = ll:%g,%g ur:%g,%g\n", mediaBox.x1, mediaBox.y1, mediaBox.x2, mediaBox.y2); |
587 | 0 | printf("***** CropBox = ll:%g,%g ur:%g,%g\n", cropBox.x1, cropBox.y1, cropBox.x2, cropBox.y2); |
588 | 0 | printf("***** Rotate = %d\n", attrs->getRotate()); |
589 | 0 | } |
590 | | |
591 | 4.25k | if (!crop) { |
592 | 4.25k | crop = (box == cropBox) && out->needClipToCropBox(); |
593 | 4.25k | } |
594 | 4.25k | return std::make_unique<Gfx>(doc, out, num, attrs->getResourceDict(), hDPI, vDPI, box, crop ? &cropBox : nullptr, rotate, abortCheckCbk, abortCheckCbkData, xrefA); |
595 | 4.25k | } |
596 | | |
597 | | void Page::displaySlice(OutputDev *out, double hDPI, double vDPI, int rotate, bool useMediaBox, bool crop, int sliceX, int sliceY, int sliceW, int sliceH, bool printing, bool (*abortCheckCbk)(void *data), void *abortCheckCbkData, |
598 | | bool (*annotDisplayDecideCbk)(Annot *annot, void *user_data), void *annotDisplayDecideCbkData, bool copyXRef) |
599 | 4.25k | { |
600 | 4.25k | Annots *annotList; |
601 | | |
602 | 4.25k | if (!out->checkPageSlice(this, hDPI, vDPI, rotate, useMediaBox, crop, sliceX, sliceY, sliceW, sliceH, printing, abortCheckCbk, abortCheckCbkData, annotDisplayDecideCbk, annotDisplayDecideCbkData)) { |
603 | 0 | return; |
604 | 0 | } |
605 | 4.25k | pageLocker(); |
606 | 4.25k | std::unique_ptr<XRef> copiedXRefIfNeeded; |
607 | 4.25k | if (copyXRef) { |
608 | 4.25k | copiedXRefIfNeeded = xref->copy(); |
609 | 4.25k | replaceXRef(copiedXRefIfNeeded.get()); |
610 | 4.25k | } |
611 | 4.25k | XRef *localXRef = copyXRef ? copiedXRefIfNeeded.get() : xref; |
612 | | |
613 | 4.25k | std::unique_ptr<Gfx> gfx = createGfx(out, hDPI, vDPI, rotate, useMediaBox, crop, sliceX, sliceY, sliceW, sliceH, abortCheckCbk, abortCheckCbkData, localXRef); |
614 | | |
615 | 4.25k | Object obj = contents.fetch(localXRef); |
616 | 4.25k | if (!obj.isNull()) { |
617 | 4.11k | gfx->saveState(); |
618 | 4.11k | gfx->display(&obj); |
619 | 4.11k | gfx->restoreState(); |
620 | 4.11k | } else { |
621 | | // empty pages need to call dump to do any setup required by the |
622 | | // OutputDev |
623 | 141 | out->dump(); |
624 | 141 | } |
625 | | |
626 | | // draw annotations |
627 | 4.25k | annotList = getAnnots(); |
628 | | |
629 | 4.25k | if (!annotList->getAnnots().empty()) { |
630 | 0 | if (globalParams->getPrintCommands()) { |
631 | 0 | printf("***** Annotations\n"); |
632 | 0 | } |
633 | 0 | for (const std::shared_ptr<Annot> &annot : annots->getAnnots()) { |
634 | 0 | if ((annotDisplayDecideCbk && (*annotDisplayDecideCbk)(annot.get(), annotDisplayDecideCbkData)) || !annotDisplayDecideCbk) { |
635 | 0 | annot->draw(gfx.get(), printing); |
636 | 0 | } |
637 | 0 | } |
638 | 0 | out->dump(); |
639 | 0 | } |
640 | | |
641 | 4.25k | if (copyXRef) { |
642 | 4.25k | replaceXRef(doc->getXRef()); |
643 | 4.25k | } |
644 | 4.25k | } |
645 | | |
646 | | void Page::display(Gfx *gfx) |
647 | 0 | { |
648 | 0 | Object obj = contents.fetch(xref); |
649 | 0 | if (!obj.isNull()) { |
650 | 0 | gfx->saveState(); |
651 | 0 | gfx->display(&obj); |
652 | 0 | gfx->restoreState(); |
653 | 0 | } |
654 | 0 | } |
655 | | |
656 | | bool Page::loadThumb(unsigned char **data_out, int *width_out, int *height_out, int *rowstride_out) |
657 | 0 | { |
658 | 0 | unsigned int pixbufdatasize; |
659 | 0 | int width, height, bits; |
660 | 0 | Object obj1; |
661 | 0 | Dict *dict; |
662 | 0 | Stream *str; |
663 | | |
664 | | /* Get stream dict */ |
665 | 0 | pageLocker(); |
666 | 0 | Object fetched_thumb = thumb.fetch(xref); |
667 | 0 | if (!fetched_thumb.isStream()) { |
668 | 0 | return false; |
669 | 0 | } |
670 | | |
671 | 0 | str = fetched_thumb.getStream(); |
672 | 0 | dict = str->getDict(); |
673 | |
|
674 | 0 | if (!dict->lookupInt("Width", "W", &width)) { |
675 | 0 | return false; |
676 | 0 | } |
677 | 0 | if (!dict->lookupInt("Height", "H", &height)) { |
678 | 0 | return false; |
679 | 0 | } |
680 | 0 | if (!dict->lookupInt("BitsPerComponent", "BPC", &bits)) { |
681 | 0 | return false; |
682 | 0 | } |
683 | | |
684 | | /* Check for invalid dimensions and integer overflow. */ |
685 | 0 | if (width <= 0 || height <= 0) { |
686 | 0 | return false; |
687 | 0 | } |
688 | 0 | if (width > INT_MAX / 3 / height) { |
689 | 0 | return false; |
690 | 0 | } |
691 | 0 | pixbufdatasize = width * height * 3; |
692 | | |
693 | | /* Get color space */ |
694 | 0 | obj1 = dict->lookup("ColorSpace"); |
695 | 0 | if (obj1.isNull()) { |
696 | 0 | obj1 = dict->lookup("CS"); |
697 | 0 | } |
698 | | // Just initialize some dummy GfxState for GfxColorSpace::parse. |
699 | | // This will set a sRGB profile for ICC-based colorspaces. |
700 | 0 | auto pdfrectangle = std::make_shared<PDFRectangle>(); |
701 | 0 | auto state = std::make_shared<GfxState>(72.0, 72.0, *pdfrectangle, 0, false); |
702 | 0 | std::unique_ptr<GfxColorSpace> colorSpace = GfxColorSpace::parse(nullptr, &obj1, nullptr, state.get()); |
703 | 0 | if (!colorSpace) { |
704 | 0 | fprintf(stderr, "Error: Cannot parse color space\n"); |
705 | 0 | return false; |
706 | 0 | } |
707 | | |
708 | 0 | obj1 = dict->lookup("Decode"); |
709 | 0 | if (obj1.isNull()) { |
710 | 0 | obj1 = dict->lookup("D"); |
711 | 0 | } |
712 | 0 | GfxImageColorMap colorMap(bits, &obj1, std::move(colorSpace)); |
713 | 0 | if (!colorMap.isOk()) { |
714 | 0 | fprintf(stderr, "Error: invalid colormap\n"); |
715 | 0 | return false; |
716 | 0 | } |
717 | | |
718 | 0 | if (data_out) { |
719 | 0 | ImageStream imgstr { str, width, colorMap.getNumPixelComps(), colorMap.getBits() }; |
720 | 0 | if (!imgstr.rewind()) { |
721 | 0 | return false; |
722 | 0 | } |
723 | 0 | auto *pixbufdata = static_cast<unsigned char *>(gmalloc(pixbufdatasize)); |
724 | 0 | unsigned char *p = pixbufdata; |
725 | 0 | for (int row = 0; row < height; ++row) { |
726 | 0 | for (int col = 0; col < width; ++col) { |
727 | 0 | unsigned char pix[gfxColorMaxComps]; |
728 | 0 | GfxRGB rgb; |
729 | |
|
730 | 0 | imgstr.getPixel(pix); |
731 | 0 | colorMap.getRGB(pix, &rgb); |
732 | |
|
733 | 0 | *p++ = colToByte(rgb.r); |
734 | 0 | *p++ = colToByte(rgb.g); |
735 | 0 | *p++ = colToByte(rgb.b); |
736 | 0 | } |
737 | 0 | } |
738 | 0 | *data_out = pixbufdata; |
739 | 0 | imgstr.close(); |
740 | 0 | } |
741 | | |
742 | 0 | if (width_out) { |
743 | 0 | *width_out = width; |
744 | 0 | } |
745 | 0 | if (height_out) { |
746 | 0 | *height_out = height; |
747 | 0 | } |
748 | 0 | if (rowstride_out) { |
749 | 0 | *rowstride_out = width * 3; |
750 | 0 | } |
751 | 0 | return true; |
752 | 0 | } |
753 | | |
754 | | void Page::makeBox(double hDPI, double vDPI, int rotate, bool useMediaBox, bool upsideDown, double sliceX, double sliceY, double sliceW, double sliceH, PDFRectangle *box, bool *crop) const |
755 | 4.25k | { |
756 | 4.25k | const PDFRectangle &mediaBox = getMediaBox(); |
757 | 4.25k | const PDFRectangle &cropBox = getCropBox(); |
758 | 4.25k | const PDFRectangle *baseBox; |
759 | 4.25k | double kx, ky; |
760 | 4.25k | if (sliceW >= 0 && sliceH >= 0) { |
761 | 0 | baseBox = useMediaBox ? &mediaBox : &cropBox; |
762 | 0 | kx = 72.0 / hDPI; |
763 | 0 | ky = 72.0 / vDPI; |
764 | 0 | if (rotate == 90) { |
765 | 0 | if (upsideDown) { |
766 | 0 | box->x1 = baseBox->x1 + ky * sliceY; |
767 | 0 | box->x2 = baseBox->x1 + ky * (sliceY + sliceH); |
768 | 0 | } else { |
769 | 0 | box->x1 = baseBox->x2 - ky * (sliceY + sliceH); |
770 | 0 | box->x2 = baseBox->x2 - ky * sliceY; |
771 | 0 | } |
772 | 0 | box->y1 = baseBox->y1 + kx * sliceX; |
773 | 0 | box->y2 = baseBox->y1 + kx * (sliceX + sliceW); |
774 | 0 | } else if (rotate == 180) { |
775 | 0 | box->x1 = baseBox->x2 - kx * (sliceX + sliceW); |
776 | 0 | box->x2 = baseBox->x2 - kx * sliceX; |
777 | 0 | if (upsideDown) { |
778 | 0 | box->y1 = baseBox->y1 + ky * sliceY; |
779 | 0 | box->y2 = baseBox->y1 + ky * (sliceY + sliceH); |
780 | 0 | } else { |
781 | 0 | box->y1 = baseBox->y2 - ky * (sliceY + sliceH); |
782 | 0 | box->y2 = baseBox->y2 - ky * sliceY; |
783 | 0 | } |
784 | 0 | } else if (rotate == 270) { |
785 | 0 | if (upsideDown) { |
786 | 0 | box->x1 = baseBox->x2 - ky * (sliceY + sliceH); |
787 | 0 | box->x2 = baseBox->x2 - ky * sliceY; |
788 | 0 | } else { |
789 | 0 | box->x1 = baseBox->x1 + ky * sliceY; |
790 | 0 | box->x2 = baseBox->x1 + ky * (sliceY + sliceH); |
791 | 0 | } |
792 | 0 | box->y1 = baseBox->y2 - kx * (sliceX + sliceW); |
793 | 0 | box->y2 = baseBox->y2 - kx * sliceX; |
794 | 0 | } else { |
795 | 0 | box->x1 = baseBox->x1 + kx * sliceX; |
796 | 0 | box->x2 = baseBox->x1 + kx * (sliceX + sliceW); |
797 | 0 | if (upsideDown) { |
798 | 0 | box->y1 = baseBox->y2 - ky * (sliceY + sliceH); |
799 | 0 | box->y2 = baseBox->y2 - ky * sliceY; |
800 | 0 | } else { |
801 | 0 | box->y1 = baseBox->y1 + ky * sliceY; |
802 | 0 | box->y2 = baseBox->y1 + ky * (sliceY + sliceH); |
803 | 0 | } |
804 | 0 | } |
805 | 4.25k | } else if (useMediaBox) { |
806 | 0 | *box = mediaBox; |
807 | 4.25k | } else { |
808 | 4.25k | *box = cropBox; |
809 | 4.25k | *crop = false; |
810 | 4.25k | } |
811 | 4.25k | } |
812 | | |
813 | | void Page::processLinks(OutputDev *out) |
814 | 0 | { |
815 | 0 | std::unique_ptr<Links> links = getLinks(); |
816 | 0 | for (const std::shared_ptr<AnnotLink> &link : links->getLinks()) { |
817 | 0 | out->processLink(link.get()); |
818 | 0 | } |
819 | 0 | } |
820 | | |
821 | | void Page::getDefaultCTM(double *ctm, double hDPI, double vDPI, int rotate, bool useMediaBox, bool upsideDown) const |
822 | 0 | { |
823 | 0 | GfxState *state; |
824 | 0 | int i; |
825 | 0 | rotate += getRotate(); |
826 | 0 | if (rotate >= 360) { |
827 | 0 | rotate -= 360; |
828 | 0 | } else if (rotate < 0) { |
829 | 0 | rotate += 360; |
830 | 0 | } |
831 | 0 | state = new GfxState(hDPI, vDPI, useMediaBox ? getMediaBox() : getCropBox(), rotate, upsideDown); |
832 | 0 | for (i = 0; i < 6; ++i) { |
833 | 0 | ctm[i] = state->getCTM()[i]; |
834 | 0 | } |
835 | 0 | delete state; |
836 | 0 | } |
837 | | |
838 | | std::unique_ptr<LinkAction> Page::getAdditionalAction(PageAdditionalActionsType type) |
839 | 0 | { |
840 | 0 | Object additionalActionsObject = actions.fetch(doc->getXRef()); |
841 | 0 | if (additionalActionsObject.isDict()) { |
842 | 0 | const char *key = (type == actionOpenPage ? "O" : type == actionClosePage ? "C" : nullptr); |
843 | |
|
844 | 0 | Object actionObject = additionalActionsObject.dictLookup(key); |
845 | 0 | if (actionObject.isDict()) { |
846 | 0 | return LinkAction::parseAction(&actionObject, doc->getCatalog()->getBaseURI()); |
847 | 0 | } |
848 | 0 | } |
849 | | |
850 | 0 | return nullptr; |
851 | 0 | } |