/src/qpdf/libqpdf/QPDFAcroFormDocumentHelper.cc
Line | Count | Source |
1 | | #include <qpdf/QPDFAcroFormDocumentHelper.hh> |
2 | | |
3 | | #include <qpdf/AcroForm.hh> |
4 | | |
5 | | #include <qpdf/Pl_Buffer.hh> |
6 | | #include <qpdf/QPDFObjectHandle_private.hh> |
7 | | #include <qpdf/QPDFPageDocumentHelper.hh> |
8 | | #include <qpdf/QPDF_private.hh> |
9 | | #include <qpdf/QTC.hh> |
10 | | #include <qpdf/QUtil.hh> |
11 | | #include <qpdf/ResourceFinder.hh> |
12 | | #include <qpdf/Util.hh> |
13 | | |
14 | | #include <deque> |
15 | | #include <utility> |
16 | | |
17 | | using namespace qpdf; |
18 | | using namespace qpdf::impl; |
19 | | using namespace std::literals; |
20 | | |
21 | | using AcroForm = impl::AcroForm; |
22 | | |
23 | | QPDFAcroFormDocumentHelper::QPDFAcroFormDocumentHelper(QPDF& qpdf) : |
24 | 15.0k | QPDFDocumentHelper(qpdf), |
25 | 15.0k | m(std::make_shared<Members>(qpdf)) |
26 | 15.0k | { |
27 | 15.0k | } |
28 | | |
29 | | QPDFAcroFormDocumentHelper& |
30 | | QPDFAcroFormDocumentHelper::get(QPDF& qpdf) |
31 | 0 | { |
32 | 0 | return qpdf.doc().acroform_dh(); |
33 | 0 | } |
34 | | |
35 | | void |
36 | | QPDFAcroFormDocumentHelper::validate(bool repair) |
37 | 0 | { |
38 | 0 | m->validate(repair); |
39 | 0 | } |
40 | | |
41 | | void |
42 | | AcroForm::validate(bool repair) |
43 | 15.0k | { |
44 | 15.0k | invalidateCache(); |
45 | 15.0k | analyze(); |
46 | 15.0k | } |
47 | | |
48 | | void |
49 | | QPDFAcroFormDocumentHelper::invalidateCache() |
50 | 0 | { |
51 | 0 | m->invalidateCache(); |
52 | 0 | } |
53 | | |
54 | | void |
55 | | AcroForm::invalidateCache() |
56 | 15.0k | { |
57 | 15.0k | cache_valid_ = false; |
58 | 15.0k | fields_.clear(); |
59 | 15.0k | annotation_to_field_.clear(); |
60 | 15.0k | bad_fields_.clear(); |
61 | 15.0k | unnamed_fields_.clear(); |
62 | 15.0k | name_to_fields_.clear(); |
63 | 15.0k | } |
64 | | |
65 | | bool |
66 | | QPDFAcroFormDocumentHelper::hasAcroForm() |
67 | 0 | { |
68 | 0 | return m->hasAcroForm(); |
69 | 0 | } |
70 | | |
71 | | bool |
72 | | AcroForm::hasAcroForm() |
73 | 0 | { |
74 | 0 | return qpdf.getRoot().hasKey("/AcroForm"); |
75 | 0 | } |
76 | | |
77 | | QPDFObjectHandle |
78 | | AcroForm::getOrCreateAcroForm() |
79 | 0 | { |
80 | 0 | auto acroform = qpdf.getRoot().getKey("/AcroForm"); |
81 | 0 | if (!acroform.isDictionary()) { |
82 | 0 | acroform = qpdf.getRoot().replaceKeyAndGetNew( |
83 | 0 | "/AcroForm", qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary())); |
84 | 0 | } |
85 | 0 | return acroform; |
86 | 0 | } |
87 | | |
88 | | void |
89 | | QPDFAcroFormDocumentHelper::addFormField(QPDFFormFieldObjectHelper ff) |
90 | 0 | { |
91 | 0 | m->addFormField(ff); |
92 | 0 | } |
93 | | |
94 | | void |
95 | | AcroForm::addFormField(QPDFFormFieldObjectHelper ff) |
96 | 0 | { |
97 | 0 | auto acroform = getOrCreateAcroForm(); |
98 | 0 | auto fields = acroform.getKey("/Fields"); |
99 | 0 | if (!fields.isArray()) { |
100 | 0 | fields = acroform.replaceKeyAndGetNew("/Fields", QPDFObjectHandle::newArray()); |
101 | 0 | } |
102 | 0 | fields.appendItem(ff.getObjectHandle()); |
103 | 0 | traverseField(ff.getObjectHandle(), {}, 0); |
104 | 0 | } |
105 | | |
106 | | void |
107 | | QPDFAcroFormDocumentHelper::addAndRenameFormFields(std::vector<QPDFObjectHandle> fields) |
108 | 0 | { |
109 | 0 | m->addAndRenameFormFields(fields); |
110 | 0 | } |
111 | | |
112 | | void |
113 | | AcroForm::addAndRenameFormFields(std::vector<QPDFObjectHandle> fields) |
114 | 0 | { |
115 | 0 | analyze(); |
116 | 0 | std::map<std::string, std::string> renames; |
117 | 0 | QPDFObjGen::set seen; |
118 | 0 | for (std::list<QPDFObjectHandle> queue{fields.begin(), fields.end()}; !queue.empty(); |
119 | 0 | queue.pop_front()) { |
120 | 0 | auto& obj = queue.front(); |
121 | 0 | if (seen.add(obj)) { |
122 | 0 | auto kids = obj.getKey("/Kids"); |
123 | 0 | if (kids.isArray()) { |
124 | 0 | for (auto const& kid: kids.aitems()) { |
125 | 0 | queue.push_back(kid); |
126 | 0 | } |
127 | 0 | } |
128 | |
|
129 | 0 | if (obj.hasKey("/T")) { |
130 | | // Find something we can append to the partial name that makes the fully qualified |
131 | | // name unique. When we find something, reuse the same suffix for all fields in this |
132 | | // group with the same name. We can only change the name of fields that have /T, and |
133 | | // this field's /T is always at the end of the fully qualified name, appending to /T |
134 | | // has the effect of appending the same thing to the fully qualified name. |
135 | 0 | std::string old_name = QPDFFormFieldObjectHelper(obj).getFullyQualifiedName(); |
136 | 0 | if (!renames.contains(old_name)) { |
137 | 0 | std::string new_name = old_name; |
138 | 0 | int suffix = 0; |
139 | 0 | std::string append; |
140 | 0 | while (!getFieldsWithQualifiedName(new_name).empty()) { |
141 | 0 | ++suffix; |
142 | 0 | append = "+" + std::to_string(suffix); |
143 | 0 | new_name = old_name + append; |
144 | 0 | } |
145 | 0 | renames[old_name] = append; |
146 | 0 | } |
147 | 0 | std::string append = renames[old_name]; |
148 | 0 | if (!append.empty()) { |
149 | 0 | obj.replaceKey( |
150 | 0 | "/T", |
151 | 0 | QPDFObjectHandle::newUnicodeString( |
152 | 0 | obj.getKey("/T").getUTF8Value() + append)); |
153 | 0 | } |
154 | 0 | } |
155 | 0 | } |
156 | 0 | } |
157 | |
|
158 | 0 | for (auto const& i: fields) { |
159 | 0 | addFormField(i); |
160 | 0 | } |
161 | 0 | } |
162 | | |
163 | | void |
164 | | QPDFAcroFormDocumentHelper::removeFormFields(std::set<QPDFObjGen> const& to_remove) |
165 | 0 | { |
166 | 0 | m->removeFormFields(to_remove); |
167 | 0 | } |
168 | | |
169 | | void |
170 | | AcroForm::removeFormFields(std::set<QPDFObjGen> const& to_remove) |
171 | 0 | { |
172 | 0 | auto acroform = qpdf.getRoot().getKey("/AcroForm"); |
173 | 0 | if (!acroform.isDictionary()) { |
174 | 0 | return; |
175 | 0 | } |
176 | 0 | auto fields = acroform.getKey("/Fields"); |
177 | 0 | if (!fields.isArray()) { |
178 | 0 | return; |
179 | 0 | } |
180 | | |
181 | 0 | for (auto const& og: to_remove) { |
182 | 0 | auto it = fields_.find(og); |
183 | 0 | if (it != fields_.end()) { |
184 | 0 | for (auto aoh: it->second.annotations) { |
185 | 0 | annotation_to_field_.erase(aoh.getObjectHandle().getObjGen()); |
186 | 0 | } |
187 | 0 | auto const& name = it->second.name; |
188 | 0 | if (!name.empty()) { |
189 | 0 | name_to_fields_[name].erase(og); |
190 | 0 | if (name_to_fields_[name].empty()) { |
191 | 0 | name_to_fields_.erase(name); |
192 | 0 | } |
193 | 0 | } |
194 | 0 | fields_.erase(og); |
195 | 0 | } |
196 | 0 | } |
197 | |
|
198 | 0 | int i = 0; |
199 | 0 | while (std::cmp_less(i, fields.size())) { |
200 | 0 | auto field = fields.getArrayItem(i); |
201 | 0 | if (to_remove.contains(field.getObjGen())) { |
202 | 0 | fields.eraseItem(i); |
203 | 0 | } else { |
204 | 0 | ++i; |
205 | 0 | } |
206 | 0 | } |
207 | 0 | } |
208 | | |
209 | | void |
210 | | QPDFAcroFormDocumentHelper::setFormFieldName(QPDFFormFieldObjectHelper ff, std::string const& name) |
211 | 0 | { |
212 | 0 | m->setFormFieldName(ff, name); |
213 | 0 | } |
214 | | |
215 | | void |
216 | | AcroForm::setFormFieldName(QPDFFormFieldObjectHelper ff, std::string const& name) |
217 | 0 | { |
218 | 0 | ff.setFieldAttribute("/T", name); |
219 | 0 | traverseField(ff, ff["/Parent"], 0); |
220 | 0 | } |
221 | | |
222 | | std::vector<QPDFFormFieldObjectHelper> |
223 | | QPDFAcroFormDocumentHelper::getFormFields() |
224 | 0 | { |
225 | 0 | return m->getFormFields(); |
226 | 0 | } |
227 | | |
228 | | std::vector<QPDFFormFieldObjectHelper> |
229 | | AcroForm::getFormFields() |
230 | 0 | { |
231 | 0 | analyze(); |
232 | 0 | std::vector<QPDFFormFieldObjectHelper> result; |
233 | 0 | for (auto const& [og, data]: fields_) { |
234 | 0 | if (!data.annotations.empty()) { |
235 | 0 | result.emplace_back(qpdf.getObject(og)); |
236 | 0 | } |
237 | 0 | } |
238 | 0 | return result; |
239 | 0 | } |
240 | | |
241 | | std::set<QPDFObjGen> |
242 | | QPDFAcroFormDocumentHelper::getFieldsWithQualifiedName(std::string const& name) |
243 | 0 | { |
244 | 0 | return m->getFieldsWithQualifiedName(name); |
245 | 0 | } |
246 | | |
247 | | std::set<QPDFObjGen> |
248 | | AcroForm::getFieldsWithQualifiedName(std::string const& name) |
249 | 0 | { |
250 | 0 | analyze(); |
251 | | // Keep from creating an empty entry |
252 | 0 | auto iter = name_to_fields_.find(name); |
253 | 0 | if (iter != name_to_fields_.end()) { |
254 | 0 | return iter->second; |
255 | 0 | } |
256 | 0 | return {}; |
257 | 0 | } |
258 | | |
259 | | std::vector<QPDFAnnotationObjectHelper> |
260 | | QPDFAcroFormDocumentHelper::getAnnotationsForField(QPDFFormFieldObjectHelper h) |
261 | 0 | { |
262 | 0 | return m->getAnnotationsForField(h); |
263 | 0 | } |
264 | | |
265 | | std::vector<QPDFAnnotationObjectHelper> |
266 | | AcroForm::getAnnotationsForField(QPDFFormFieldObjectHelper h) |
267 | 0 | { |
268 | 0 | analyze(); |
269 | 0 | std::vector<QPDFAnnotationObjectHelper> result; |
270 | 0 | QPDFObjGen og(h.getObjectHandle().getObjGen()); |
271 | 0 | if (fields_.contains(og)) { |
272 | 0 | result = fields_[og].annotations; |
273 | 0 | } |
274 | 0 | return result; |
275 | 0 | } |
276 | | |
277 | | std::vector<QPDFAnnotationObjectHelper> |
278 | | QPDFAcroFormDocumentHelper::getWidgetAnnotationsForPage(QPDFPageObjectHelper h) |
279 | 12.7k | { |
280 | 12.7k | return m->getWidgetAnnotationsForPage(h); |
281 | 12.7k | } |
282 | | |
283 | | std::vector<QPDFAnnotationObjectHelper> |
284 | | AcroForm::getWidgetAnnotationsForPage(QPDFPageObjectHelper h) |
285 | 24.4k | { |
286 | 24.4k | return h.getAnnotations("/Widget"); |
287 | 24.4k | } |
288 | | |
289 | | std::vector<QPDFFormFieldObjectHelper> |
290 | | QPDFAcroFormDocumentHelper::getFormFieldsForPage(QPDFPageObjectHelper ph) |
291 | 0 | { |
292 | 0 | return m->getFormFieldsForPage(ph); |
293 | 0 | } |
294 | | |
295 | | std::vector<QPDFFormFieldObjectHelper> |
296 | | AcroForm::getFormFieldsForPage(QPDFPageObjectHelper ph) |
297 | 0 | { |
298 | 0 | analyze(); |
299 | 0 | QPDFObjGen::set todo; |
300 | 0 | std::vector<QPDFFormFieldObjectHelper> result; |
301 | 0 | for (auto& annot: getWidgetAnnotationsForPage(ph)) { |
302 | 0 | auto field = getFieldForAnnotation(annot).getTopLevelField(); |
303 | 0 | if (todo.add(field) && field.getObjectHandle().isDictionary()) { |
304 | 0 | result.push_back(field); |
305 | 0 | } |
306 | 0 | } |
307 | 0 | return result; |
308 | 0 | } |
309 | | |
310 | | QPDFFormFieldObjectHelper |
311 | | QPDFAcroFormDocumentHelper::getFieldForAnnotation(QPDFAnnotationObjectHelper h) |
312 | 1.58k | { |
313 | 1.58k | return m->getFieldForAnnotation(h); |
314 | 1.58k | } |
315 | | |
316 | | QPDFFormFieldObjectHelper |
317 | | AcroForm::getFieldForAnnotation(QPDFAnnotationObjectHelper h) |
318 | 30.2k | { |
319 | 30.2k | QPDFObjectHandle oh = h.getObjectHandle(); |
320 | 30.2k | if (!oh.isDictionaryOfType("", "/Widget")) { |
321 | 0 | return Null::temp(); |
322 | 0 | } |
323 | 30.2k | analyze(); |
324 | 30.2k | QPDFObjGen og(oh.getObjGen()); |
325 | 30.2k | if (annotation_to_field_.contains(og)) { |
326 | 28.0k | return annotation_to_field_[og]; |
327 | 28.0k | } |
328 | 2.17k | return Null::temp(); |
329 | 30.2k | } |
330 | | |
331 | | void |
332 | | AcroForm::analyze() |
333 | 45.2k | { |
334 | 45.2k | if (cache_valid_) { |
335 | 30.2k | return; |
336 | 30.2k | } |
337 | 15.0k | cache_valid_ = true; |
338 | 15.0k | QPDFObjectHandle acroform = qpdf.getRoot().getKey("/AcroForm"); |
339 | 15.0k | if (!(acroform.isDictionary() && acroform.hasKey("/Fields"))) { |
340 | 8.81k | return; |
341 | 8.81k | } |
342 | 6.24k | QPDFObjectHandle fields = acroform.getKey("/Fields"); |
343 | 6.24k | if (Array fa = fields) { |
344 | | // Traverse /AcroForm to find annotations and map them bidirectionally to fields. |
345 | 53.2k | for (auto const& field: fa) { |
346 | 53.2k | traverseField(field, {}, 0); |
347 | 53.2k | } |
348 | 6.18k | } else { |
349 | 64 | acroform.warn("/Fields key of /AcroForm dictionary is not an array; ignoring"); |
350 | 64 | fields = Array::empty(); |
351 | 64 | } |
352 | | |
353 | | // All Widget annotations should have been encountered by traversing /AcroForm, but in case any |
354 | | // weren't, find them by walking through pages, and treat any widget annotation that is not |
355 | | // associated with a field as its own field. This just ensures that requesting the field for any |
356 | | // annotation we find through a page's /Annots list will have some associated field. Note that |
357 | | // a file that contains this kind of error will probably not |
358 | | // actually work with most viewers. |
359 | | |
360 | 7.58k | for (QPDFPageObjectHelper ph: pages) { |
361 | 30.9k | for (auto const& iter: getWidgetAnnotationsForPage(ph)) { |
362 | 30.9k | QPDFObjectHandle annot(iter.getObjectHandle()); |
363 | 30.9k | QPDFObjGen og(annot.getObjGen()); |
364 | 30.9k | if (!annotation_to_field_.contains(og)) { |
365 | | // This is not supposed to happen, but it's easy enough for us to handle this case. |
366 | | // Treat the annotation as its own field. This could allow qpdf to sensibly handle a |
367 | | // case such as a PDF creator adding a self-contained annotation (merged with the |
368 | | // field dictionary) to the page's /Annots array and forgetting to also put it in |
369 | | // /AcroForm. |
370 | 19.7k | annot.warn( |
371 | 19.7k | "this widget annotation is not reachable from /AcroForm in the document " |
372 | 19.7k | "catalog"); |
373 | 19.7k | annotation_to_field_[og] = QPDFFormFieldObjectHelper(annot); |
374 | 19.7k | fields_[og].annotations.emplace_back(annot); |
375 | 19.7k | } |
376 | 30.9k | } |
377 | 7.58k | } |
378 | 6.24k | } |
379 | | |
380 | | bool |
381 | | AcroForm::traverseField(QPDFObjectHandle field, QPDFObjectHandle const& parent, int depth) |
382 | 79.9k | { |
383 | 79.9k | if (depth > 100) { |
384 | | // Arbitrarily cut off recursion at a fixed depth to avoid specially crafted files that |
385 | | // could cause stack overflow. |
386 | 0 | return false; |
387 | 0 | } |
388 | 79.9k | if (!field.indirect()) { |
389 | 27.3k | field.warn( |
390 | 27.3k | "encountered a direct object as a field or annotation while traversing /AcroForm; " |
391 | 27.3k | "ignoring field or annotation"); |
392 | 27.3k | return false; |
393 | 27.3k | } |
394 | 52.5k | if (field == parent) { |
395 | 89 | field.warn("loop detected while traversing /AcroForm"); |
396 | 89 | return false; |
397 | 89 | } |
398 | 52.4k | if (!field.isDictionary()) { |
399 | 33.3k | field.warn( |
400 | 33.3k | "encountered a non-dictionary as a field or annotation while traversing /AcroForm; " |
401 | 33.3k | "ignoring field or annotation"); |
402 | 33.3k | return false; |
403 | 33.3k | } |
404 | 19.1k | QPDFObjGen og(field.getObjGen()); |
405 | 19.1k | if (unnamed_fields_.contains(og)) { |
406 | 416 | bad_fields_.insert(og); |
407 | 416 | } |
408 | 19.1k | if (fields_.contains(og) || annotation_to_field_.contains(og) || bad_fields_.contains(og)) { |
409 | 1.67k | field.warn("loop detected while traversing /AcroForm"); |
410 | 1.67k | return false; |
411 | 1.67k | } |
412 | | |
413 | | // A dictionary encountered while traversing the /AcroForm field may be a form field, an |
414 | | // annotation, or the merger of the two. A field that has no fields below it is a terminal. If a |
415 | | // terminal field looks like an annotation, it is an annotation because annotation dictionary |
416 | | // fields can be merged with terminal field dictionaries. Otherwise, the annotation fields might |
417 | | // be there to be inherited by annotations below it. |
418 | | |
419 | 17.4k | FormNode node = field; |
420 | 17.4k | const bool is_field = node.field(); |
421 | 17.4k | const bool is_annotation = node.widget(); |
422 | | |
423 | 17.4k | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper field found", (depth == 0) ? 0 : 1); |
424 | 17.4k | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper annotation found", (is_field ? 0 : 1)); |
425 | | |
426 | 17.4k | if (!is_field && !is_annotation) { |
427 | 4.59k | field.warn( |
428 | 4.59k | "encountered an object that is neither field nor annotation while traversing " |
429 | 4.59k | "/AcroForm"); |
430 | 4.59k | return false; |
431 | 4.59k | } |
432 | | |
433 | 12.8k | if (is_annotation) { |
434 | 8.01k | auto our_field = (is_field ? field : parent); |
435 | 8.01k | fields_[our_field].annotations.emplace_back(field); |
436 | 8.01k | annotation_to_field_[og] = QPDFFormFieldObjectHelper(our_field); |
437 | 8.01k | } |
438 | | |
439 | 12.8k | if (!is_field) { |
440 | 556 | return true; |
441 | 556 | } |
442 | | |
443 | 12.3k | if (depth != 0 && field["/Parent"] != parent) { |
444 | 31.9k | for (auto const& kid: Array(field["/Parent"]["/Kids"])) { |
445 | 31.9k | if (kid == field) { |
446 | 367 | field.warn("while traversing /AcroForm found field with two parents"); |
447 | 367 | return true; |
448 | 367 | } |
449 | 31.9k | } |
450 | 7.21k | for (auto const& kid: Array(field["/Parent"]["/Kids"])) { |
451 | 7.21k | if (kid == parent) { |
452 | 120 | field.warn("loop detected while traversing /AcroForm"); |
453 | 120 | return false; |
454 | 120 | } |
455 | 7.21k | } |
456 | 949 | field.warn("encountered invalid /Parent entry while traversing /AcroForm; correcting"); |
457 | 949 | field.replaceKey("/Parent", parent); |
458 | 949 | } |
459 | | |
460 | 11.8k | if (node.T()) { |
461 | 8.52k | auto old = fields_.find(og); |
462 | 8.52k | if (old != fields_.end() && !old->second.name.empty()) { |
463 | | // We might be updating after a name change, so remove any old information |
464 | 0 | name_to_fields_[old->second.name].erase(og); |
465 | 0 | } |
466 | 8.52k | std::string name = node.fully_qualified_name(); |
467 | 8.52k | fields_[og].name = name; |
468 | 8.52k | name_to_fields_[name].insert(og); |
469 | 8.52k | } else if (!is_annotation) { |
470 | 2.42k | unnamed_fields_.insert(og); |
471 | 2.42k | } |
472 | | |
473 | 52.5k | for (auto const& kid: node.Kids()) { |
474 | 52.5k | if (bad_fields_.contains(kid)) { |
475 | 25.8k | continue; |
476 | 25.8k | } |
477 | | |
478 | 26.6k | if (!traverseField(kid, field, 1 + depth)) { |
479 | 19.0k | bad_fields_.insert(kid); |
480 | 19.0k | } |
481 | 26.6k | } |
482 | 11.8k | return true; |
483 | 12.3k | } |
484 | | |
485 | | bool |
486 | | QPDFAcroFormDocumentHelper::getNeedAppearances() |
487 | 0 | { |
488 | 0 | return m->getNeedAppearances(); |
489 | 0 | } |
490 | | |
491 | | bool |
492 | | AcroForm::getNeedAppearances() |
493 | 37.4k | { |
494 | 37.4k | bool result = false; |
495 | 37.4k | QPDFObjectHandle acroform = qpdf.getRoot().getKey("/AcroForm"); |
496 | 37.4k | if (acroform.isDictionary() && acroform.getKey("/NeedAppearances").isBool()) { |
497 | 3.44k | result = acroform.getKey("/NeedAppearances").getBoolValue(); |
498 | 3.44k | } |
499 | 37.4k | return result; |
500 | 37.4k | } |
501 | | |
502 | | void |
503 | | QPDFAcroFormDocumentHelper::setNeedAppearances(bool val) |
504 | 0 | { |
505 | 0 | m->setNeedAppearances(val); |
506 | 0 | } |
507 | | |
508 | | void |
509 | | AcroForm::setNeedAppearances(bool val) |
510 | 2.09k | { |
511 | 2.09k | QPDFObjectHandle acroform = qpdf.getRoot().getKey("/AcroForm"); |
512 | 2.09k | if (!acroform.isDictionary()) { |
513 | 0 | qpdf.getRoot().warn( |
514 | 0 | "ignoring call to QPDFAcroFormDocumentHelper::setNeedAppearances" |
515 | 0 | " on a file that lacks an /AcroForm dictionary"); |
516 | 0 | return; |
517 | 0 | } |
518 | 2.09k | if (val) { |
519 | 0 | acroform.replaceKey("/NeedAppearances", QPDFObjectHandle::newBool(true)); |
520 | 2.09k | } else { |
521 | 2.09k | acroform.removeKey("/NeedAppearances"); |
522 | 2.09k | } |
523 | 2.09k | } |
524 | | |
525 | | void |
526 | | QPDFAcroFormDocumentHelper::generateAppearancesIfNeeded() |
527 | 8.19k | { |
528 | 8.19k | m->generateAppearancesIfNeeded(); |
529 | 8.19k | } |
530 | | |
531 | | void |
532 | | AcroForm::generateAppearancesIfNeeded() |
533 | 8.19k | { |
534 | 8.19k | if (!getNeedAppearances()) { |
535 | 4.77k | return; |
536 | 4.77k | } |
537 | | |
538 | 4.16k | for (auto const& page: QPDFPageDocumentHelper(qpdf).getAllPages()) { |
539 | 15.6k | for (auto& aoh: getWidgetAnnotationsForPage(page)) { |
540 | 15.6k | QPDFFormFieldObjectHelper ffh = getFieldForAnnotation(aoh); |
541 | 15.6k | if (ffh.getFieldType() == "/Btn") { |
542 | | // Rather than generating appearances for button fields, rely on what's already |
543 | | // there. Just make sure /AS is consistent with /V, which we can do by resetting the |
544 | | // value of the field back to itself. This code is referenced in a comment in |
545 | | // QPDFFormFieldObjectHelper::generateAppearance. |
546 | 1.15k | if (ffh.isRadioButton() || ffh.isCheckbox()) { |
547 | 1.12k | ffh.setV(ffh.getValue()); |
548 | 1.12k | } |
549 | 14.4k | } else { |
550 | 14.4k | ffh.generateAppearance(aoh); |
551 | 14.4k | } |
552 | 15.6k | } |
553 | 4.16k | } |
554 | 3.41k | setNeedAppearances(false); |
555 | 3.41k | } |
556 | | |
557 | | void |
558 | | QPDFAcroFormDocumentHelper::disableDigitalSignatures() |
559 | 0 | { |
560 | 0 | m->disableDigitalSignatures(); |
561 | 0 | } |
562 | | |
563 | | void |
564 | | AcroForm::disableDigitalSignatures() |
565 | 0 | { |
566 | 0 | qpdf.removeSecurityRestrictions(); |
567 | 0 | std::set<QPDFObjGen> to_remove; |
568 | 0 | auto fields = getFormFields(); |
569 | 0 | for (auto& f: fields) { |
570 | 0 | auto ft = f.getFieldType(); |
571 | 0 | if (ft == "/Sig") { |
572 | 0 | auto oh = f.getObjectHandle(); |
573 | 0 | to_remove.insert(oh.getObjGen()); |
574 | | // Make this no longer a form field. If it's also an annotation, the annotation will |
575 | | // survive. If it's only a field and is no longer referenced, it will disappear. |
576 | 0 | oh.removeKey("/FT"); |
577 | | // Remove fields that are specific to signature fields. |
578 | 0 | oh.removeKey("/V"); |
579 | 0 | oh.removeKey("/SV"); |
580 | 0 | oh.removeKey("/Lock"); |
581 | 0 | } |
582 | 0 | } |
583 | 0 | removeFormFields(to_remove); |
584 | 0 | } |
585 | | |
586 | | void |
587 | | AcroForm::adjustInheritedFields( |
588 | | QPDFObjectHandle obj, |
589 | | bool override_da, |
590 | | std::string const& from_default_da, |
591 | | bool override_q, |
592 | | int from_default_q) |
593 | 0 | { |
594 | 0 | if (!(override_da || override_q)) { |
595 | 0 | return; |
596 | 0 | } |
597 | | // Override /Q or /DA if needed. If this object has a field type, directly or inherited, it is a |
598 | | // field and not just an annotation. In that case, we need to override if we are getting a value |
599 | | // from the document that is different from the value we would have gotten from the old |
600 | | // document. We must take care not to override an explicit value. It's possible that /FT may be |
601 | | // inherited by lower fields that may explicitly set /DA or /Q or that this is a field whose |
602 | | // type does not require /DA or /Q and we may be put a value on the field that is unused. This |
603 | | // is harmless, so it's not worth trying to work around. |
604 | | |
605 | 0 | auto has_explicit = [](QPDFFormFieldObjectHelper& field, std::string const& key) { |
606 | 0 | if (field.getObjectHandle().hasKey(key)) { |
607 | 0 | return true; |
608 | 0 | } |
609 | 0 | return !field.getInheritableFieldValue(key).null(); |
610 | 0 | }; |
611 | |
|
612 | 0 | if (override_da || override_q) { |
613 | 0 | QPDFFormFieldObjectHelper cur_field(obj); |
614 | 0 | if (override_da && (!has_explicit(cur_field, "/DA"))) { |
615 | 0 | std::string da = cur_field.getDefaultAppearance(); |
616 | 0 | if (da != from_default_da) { |
617 | 0 | obj.replaceKey("/DA", QPDFObjectHandle::newUnicodeString(from_default_da)); |
618 | 0 | } |
619 | 0 | } |
620 | 0 | if (override_q && (!has_explicit(cur_field, "/Q"))) { |
621 | 0 | int q = cur_field.getQuadding(); |
622 | 0 | if (q != from_default_q) { |
623 | 0 | obj.replaceKey("/Q", QPDFObjectHandle::newInteger(from_default_q)); |
624 | 0 | } |
625 | 0 | } |
626 | 0 | } |
627 | 0 | } |
628 | | |
629 | | namespace |
630 | | { |
631 | | class ResourceReplacer: public QPDFObjectHandle::TokenFilter |
632 | | { |
633 | | public: |
634 | | ResourceReplacer( |
635 | | std::map<std::string, std::map<std::string, std::string>> const& dr_map, |
636 | | std::map<std::string, std::map<std::string, std::vector<size_t>>> const& rnames); |
637 | 0 | ~ResourceReplacer() override = default; |
638 | | void handleToken(QPDFTokenizer::Token const&) override; |
639 | | |
640 | | private: |
641 | | size_t offset{0}; |
642 | | std::map<std::string, std::map<size_t, std::string>> to_replace; |
643 | | }; |
644 | | } // namespace |
645 | | |
646 | | ResourceReplacer::ResourceReplacer( |
647 | | std::map<std::string, std::map<std::string, std::string>> const& dr_map, |
648 | | std::map<std::string, std::map<std::string, std::vector<size_t>>> const& rnames) |
649 | 0 | { |
650 | | // We have: |
651 | | // * dr_map[resource_type][key] == new_key |
652 | | // * rnames[resource_type][key] == set of offsets |
653 | | // |
654 | | // We want: |
655 | | // * to_replace[key][offset] = new_key |
656 | |
|
657 | 0 | for (auto const& [rtype, key_offsets]: rnames) { |
658 | 0 | auto dr_map_rtype = dr_map.find(rtype); |
659 | 0 | if (dr_map_rtype == dr_map.end()) { |
660 | 0 | continue; |
661 | 0 | } |
662 | 0 | for (auto const& [old_key, offsets]: key_offsets) { |
663 | 0 | auto dr_map_rtype_old = dr_map_rtype->second.find(old_key); |
664 | 0 | if (dr_map_rtype_old == dr_map_rtype->second.end()) { |
665 | 0 | continue; |
666 | 0 | } |
667 | 0 | for (auto const& offs: offsets) { |
668 | 0 | to_replace[old_key][offs] = dr_map_rtype_old->second; |
669 | 0 | } |
670 | 0 | } |
671 | 0 | } |
672 | 0 | } |
673 | | |
674 | | void |
675 | | ResourceReplacer::handleToken(QPDFTokenizer::Token const& token) |
676 | 0 | { |
677 | 0 | if (token.getType() == QPDFTokenizer::tt_name) { |
678 | 0 | auto it1 = to_replace.find(token.getValue()); |
679 | 0 | if (it1 != to_replace.end()) { |
680 | 0 | auto it2 = it1->second.find(offset); |
681 | 0 | if (it2 != it1->second.end()) { |
682 | 0 | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper replaced DA token"); |
683 | 0 | write(it2->second); |
684 | 0 | offset += token.getRawValue().length(); |
685 | 0 | return; |
686 | 0 | } |
687 | 0 | } |
688 | 0 | } |
689 | 0 | offset += token.getRawValue().length(); |
690 | 0 | writeToken(token); |
691 | 0 | } |
692 | | |
693 | | void |
694 | | AcroForm::adjustDefaultAppearances( |
695 | | QPDFObjectHandle obj, std::map<std::string, std::map<std::string, std::string>> const& dr_map) |
696 | 0 | { |
697 | | // This method is called on a field that has been copied from another file but whose /DA still |
698 | | // refers to resources in the original file's /DR. |
699 | | |
700 | | // When appearance streams are generated for variable text fields (see ISO 32000 PDF spec |
701 | | // section 12.7.3.3), the field's /DA is used to generate content of the appearance stream. /DA |
702 | | // contains references to resources that may be resolved in the document's /DR dictionary, which |
703 | | // appears in the document's /AcroForm dictionary. For fields that we copied from other |
704 | | // documents, we need to ensure that resources are mapped correctly in the case of conflicting |
705 | | // names. For example, if a.pdf's /DR has /F1 pointing to one font and b.pdf's /DR also has /F1 |
706 | | // but it points elsewhere, we need to make sure appearance streams of fields copied from b.pdf |
707 | | // into a.pdf use whatever font /F1 meant in b.pdf, not whatever it means in a.pdf. This method |
708 | | // takes care of that. It is only called on fields copied from foreign files. |
709 | | |
710 | | // A few notes: |
711 | | // |
712 | | // * If the from document's /DR and the current document's /DR have conflicting keys, we have |
713 | | // already resolved the conflicts before calling this method. The dr_map parameter contains |
714 | | // the mapping from old keys to new keys. |
715 | | // |
716 | | // * /DA may be inherited from the document's /AcroForm dictionary. By the time this method has |
717 | | // been called, we have already copied any document-level values into the fields to avoid |
718 | | // having them inherit from the new document. This was done in adjustInheritedFields. |
719 | |
|
720 | 0 | auto DA = obj.getKey("/DA"); |
721 | 0 | if (!DA.isString()) { |
722 | 0 | return; |
723 | 0 | } |
724 | | |
725 | | // Find names in /DA. /DA is a string that contains content stream-like code, so we create a |
726 | | // stream out of the string and then filter it. We don't attach the stream to anything, so it |
727 | | // will get discarded. |
728 | 0 | ResourceFinder rf; |
729 | 0 | auto da_stream = QPDFObjectHandle::newStream(&qpdf, DA.getUTF8Value()); |
730 | 0 | try { |
731 | 0 | auto nwarnings = qpdf.numWarnings(); |
732 | 0 | da_stream.parseAsContents(&rf); |
733 | 0 | if (qpdf.numWarnings() > nwarnings) { |
734 | 0 | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper /DA parse error"); |
735 | 0 | } |
736 | 0 | } catch (std::exception& e) { |
737 | | // No way to reproduce in test suite right now since error conditions are converted to |
738 | | // warnings. |
739 | 0 | obj.warn("Unable to parse /DA: "s + e.what() + "; this form field may not update properly"); |
740 | 0 | return; |
741 | 0 | } |
742 | | |
743 | | // Regenerate /DA by filtering its tokens. |
744 | 0 | ResourceReplacer rr(dr_map, rf.getNamesByResourceType()); |
745 | 0 | Pl_Buffer buf_pl("filtered DA"); |
746 | 0 | da_stream.filterAsContents(&rr, &buf_pl); |
747 | 0 | std::string new_da = buf_pl.getString(); |
748 | 0 | obj.replaceKey("/DA", QPDFObjectHandle::newString(new_da)); |
749 | 0 | } |
750 | | |
751 | | void |
752 | | AcroForm::adjustAppearanceStream( |
753 | | QPDFObjectHandle stream, std::map<std::string, std::map<std::string, std::string>> dr_map) |
754 | 0 | { |
755 | | // We don't have to modify appearance streams or their resource dictionaries for them to display |
756 | | // properly, but we need to do so to make them save to regenerate. Suppose an appearance stream |
757 | | // as a font /F1 that is different from /F1 in /DR, and that when we copy the field, /F1 is |
758 | | // remapped to /F1_1. When the field is regenerated, /F1_1 won't appear in the stream's resource |
759 | | // dictionary, so the regenerated appearance stream will revert to the /F1_1 in /DR. If we |
760 | | // adjust existing appearance streams, we are protected from this problem. |
761 | |
|
762 | 0 | auto dict = stream.getDict(); |
763 | 0 | auto resources = dict.getKey("/Resources"); |
764 | | |
765 | | // Make sure this stream has its own private resource dictionary. |
766 | 0 | bool was_indirect = resources.isIndirect(); |
767 | 0 | resources = resources.shallowCopy(); |
768 | 0 | if (was_indirect) { |
769 | 0 | resources = qpdf.makeIndirectObject(resources); |
770 | 0 | } |
771 | 0 | dict.replaceKey("/Resources", resources); |
772 | | // Create a dictionary with top-level keys so we can use mergeResources to force them to be |
773 | | // unshared. We will also use this to resolve conflicts that may already be in the resource |
774 | | // dictionary. |
775 | 0 | auto merge_with = QPDFObjectHandle::newDictionary(); |
776 | 0 | for (auto const& top_key: dr_map) { |
777 | 0 | merge_with.replaceKey(top_key.first, QPDFObjectHandle::newDictionary()); |
778 | 0 | } |
779 | 0 | resources.mergeResources(merge_with); |
780 | | // Rename any keys in the resource dictionary that we remapped. |
781 | 0 | for (auto const& i1: dr_map) { |
782 | 0 | std::string const& top_key = i1.first; |
783 | 0 | auto subdict = resources.getKey(top_key); |
784 | 0 | if (!subdict.isDictionary()) { |
785 | 0 | continue; |
786 | 0 | } |
787 | 0 | for (auto const& i2: i1.second) { |
788 | 0 | std::string const& old_key = i2.first; |
789 | 0 | std::string const& new_key = i2.second; |
790 | 0 | auto existing_new = subdict.getKey(new_key); |
791 | 0 | if (!existing_new.null()) { |
792 | | // The resource dictionary already has a key in it matching what we remapped an old |
793 | | // key to, so we'll have to move it out of the way. Stick it in merge_with, which we |
794 | | // will re-merge with the dictionary when we're done. We know merge_with already has |
795 | | // dictionaries for all the top keys. |
796 | 0 | merge_with.getKey(top_key).replaceKey(new_key, existing_new); |
797 | 0 | } |
798 | 0 | auto existing_old = subdict.getKey(old_key); |
799 | 0 | if (!existing_old.null()) { |
800 | 0 | subdict.replaceKey(new_key, existing_old); |
801 | 0 | subdict.removeKey(old_key); |
802 | 0 | } |
803 | 0 | } |
804 | 0 | } |
805 | | // Deal with any any conflicts by re-merging with merge_with and updating our local copy of |
806 | | // dr_map, which we will use to modify the stream contents. |
807 | 0 | resources.mergeResources(merge_with, &dr_map); |
808 | | // Remove empty subdictionaries |
809 | 0 | for (auto iter: resources.ditems()) { |
810 | 0 | if (iter.second.isDictionary() && iter.second.getKeys().empty()) { |
811 | 0 | resources.removeKey(iter.first); |
812 | 0 | } |
813 | 0 | } |
814 | | |
815 | | // Now attach a token filter to replace the actual resources. |
816 | 0 | ResourceFinder rf; |
817 | 0 | try { |
818 | 0 | auto nwarnings = qpdf.numWarnings(); |
819 | 0 | stream.parseAsContents(&rf); |
820 | 0 | QTC::TC( |
821 | 0 | "qpdf", |
822 | 0 | "QPDFAcroFormDocumentHelper AP parse error", |
823 | 0 | qpdf.numWarnings() > nwarnings ? 0 : 1); |
824 | 0 | auto rr = new ResourceReplacer(dr_map, rf.getNamesByResourceType()); |
825 | 0 | auto tf = std::shared_ptr<QPDFObjectHandle::TokenFilter>(rr); |
826 | 0 | stream.addTokenFilter(tf); |
827 | 0 | } catch (std::exception& e) { |
828 | | // No way to reproduce in test suite right now since error conditions are converted to |
829 | | // warnings. |
830 | 0 | stream.warn("Unable to parse appearance stream: "s + e.what()); |
831 | 0 | } |
832 | 0 | } |
833 | | |
834 | | void |
835 | | QPDFAcroFormDocumentHelper::transformAnnotations( |
836 | | QPDFObjectHandle a_old_annots, |
837 | | std::vector<QPDFObjectHandle>& new_annots, |
838 | | std::vector<QPDFObjectHandle>& new_fields, |
839 | | std::set<QPDFObjGen>& old_fields, |
840 | | QPDFMatrix const& cm, |
841 | | QPDF* from_qpdf, |
842 | | QPDFAcroFormDocumentHelper* from_afdh) |
843 | 0 | { |
844 | 0 | if (!from_qpdf) { |
845 | | // Assume these are from the same QPDF. |
846 | 0 | from_qpdf = &qpdf; |
847 | 0 | from_afdh = this; |
848 | 0 | } else if (from_qpdf != &qpdf && !from_afdh) { |
849 | 0 | from_afdh = &QPDFAcroFormDocumentHelper::get(*from_qpdf); |
850 | 0 | } |
851 | 0 | m->transformAnnotations( |
852 | 0 | a_old_annots, |
853 | 0 | new_annots, |
854 | 0 | new_fields, |
855 | 0 | old_fields, |
856 | 0 | cm, |
857 | 0 | from_qpdf, |
858 | 0 | from_afdh->m.get(), |
859 | 0 | nullptr); |
860 | 0 | } |
861 | | |
862 | | void |
863 | | AcroForm::transformAnnotations( |
864 | | QPDFObjectHandle a_old_annots, |
865 | | std::vector<QPDFObjectHandle>& new_annots, |
866 | | std::vector<QPDFObjectHandle>& new_fields, |
867 | | std::set<QPDFObjGen>& old_fields, |
868 | | QPDFMatrix const& cm, |
869 | | QPDF* from_qpdf, |
870 | | AcroForm* from_afdh, |
871 | | QPDFObjectHandle* new_page) |
872 | 0 | { |
873 | 0 | qpdf_expect(from_qpdf); |
874 | 0 | qpdf_expect(from_afdh); |
875 | 0 | Array old_annots = std::move(a_old_annots); |
876 | 0 | const bool foreign = from_qpdf != &qpdf; |
877 | | |
878 | | // It's possible that we will transform annotations that don't include any form fields. This |
879 | | // code takes care not to muck around with /AcroForm unless we have to. |
880 | |
|
881 | 0 | Dictionary acroform = qpdf.getRoot()["/AcroForm"]; |
882 | 0 | Dictionary from_acroform = from_qpdf->getRoot()["/AcroForm"]; |
883 | | |
884 | | // /DA and /Q may be inherited from the document-level /AcroForm dictionary. If we are copying a |
885 | | // foreign stream and the stream is getting one of these values from its document's /AcroForm, |
886 | | // we will need to copy the value explicitly so that it doesn't start getting its default from |
887 | | // the destination document. |
888 | 0 | std::string from_default_da; |
889 | 0 | int from_default_q = 0; |
890 | | // If we copy any form fields, we will need to merge the source document's /DR into this |
891 | | // document's /DR. |
892 | 0 | Dictionary from_dr; |
893 | 0 | std::string default_da; |
894 | 0 | int default_q = 0; |
895 | 0 | if (foreign) { |
896 | 0 | if (acroform) { |
897 | 0 | if (acroform["/DA"].isString()) { |
898 | 0 | default_da = acroform["/DA"].getUTF8Value(); |
899 | 0 | } |
900 | 0 | if (Integer Q = acroform["/Q"]) { |
901 | 0 | default_q = Q; |
902 | 0 | } |
903 | 0 | } |
904 | 0 | if (from_acroform) { |
905 | 0 | from_dr = from_acroform["/DR"]; |
906 | 0 | if (from_dr) { |
907 | 0 | if (!from_dr.indirect()) { |
908 | 0 | from_dr = from_qpdf->makeIndirectObject(from_dr); |
909 | 0 | } |
910 | 0 | from_dr = qpdf.copyForeignObject(from_dr); |
911 | 0 | } |
912 | 0 | if (from_acroform["/DA"].isString()) { |
913 | 0 | from_default_da = from_acroform["/DA"].getUTF8Value(); |
914 | 0 | } |
915 | 0 | if (Integer Q = from_acroform["/Q"]) { |
916 | 0 | from_default_q = Q; |
917 | 0 | } |
918 | 0 | } |
919 | 0 | } |
920 | 0 | const bool override_da = from_acroform ? from_default_da != default_da : false; |
921 | 0 | const bool override_q = from_acroform ? from_default_q != default_q : false; |
922 | | |
923 | | // If we have to merge /DR, we will need a mapping of conflicting keys for rewriting /DA. Set |
924 | | // this up for lazy initialization in case we encounter any form fields. |
925 | 0 | std::map<std::string, std::map<std::string, std::string>> dr_map; |
926 | 0 | Dictionary dr; |
927 | |
|
928 | 0 | auto init_dr_map = [&]() { |
929 | 0 | if (!dr) { |
930 | | // Ensure that we have a /DR that is an indirect |
931 | | // dictionary object. |
932 | 0 | if (!acroform) { |
933 | 0 | acroform = getOrCreateAcroForm(); |
934 | 0 | } |
935 | 0 | dr = acroform["/DR"]; |
936 | 0 | if (!dr) { |
937 | 0 | dr = Dictionary::empty(); |
938 | 0 | } |
939 | 0 | QPDFObjectHandle(dr).makeResourcesIndirect(qpdf); |
940 | 0 | if (!dr.indirect()) { |
941 | 0 | acroform.replace("/DR", qpdf.makeIndirectObject(dr)); |
942 | 0 | dr = acroform["/DR"]; |
943 | 0 | } |
944 | | // Merge the other document's /DR, creating a conflict map. mergeResources checks to |
945 | | // make sure both objects are dictionaries. By this point, if this is foreign, from_dr |
946 | | // has been copied, so we use the target qpdf as the owning qpdf. |
947 | 0 | QPDFObjectHandle(from_dr).makeResourcesIndirect(qpdf); |
948 | 0 | QPDFObjectHandle(dr).mergeResources(from_dr, &dr_map); |
949 | |
|
950 | 0 | if (from_afdh->getNeedAppearances()) { |
951 | 0 | setNeedAppearances(true); |
952 | 0 | } |
953 | 0 | } |
954 | 0 | }; |
955 | | |
956 | | // This helper prevents us from copying the same object multiple times. |
957 | 0 | std::map<QPDFObjGen, QPDFObjectHandle> orig_to_copy; |
958 | 0 | auto maybe_copy_object = [&](QPDFObjectHandle& to_copy) { |
959 | 0 | auto og = to_copy.getObjGen(); |
960 | 0 | if (orig_to_copy.contains(og)) { |
961 | 0 | to_copy = orig_to_copy[og]; |
962 | 0 | return false; |
963 | 0 | } else { |
964 | 0 | to_copy = qpdf.makeIndirectObject(to_copy.shallowCopy()); |
965 | 0 | orig_to_copy[og] = to_copy; |
966 | 0 | return true; |
967 | 0 | } |
968 | 0 | }; |
969 | | |
970 | | // Traverse the field, copying kids, and preserving integrity. |
971 | 0 | auto traverse_field = [&](QPDFObjectHandle& top_field) -> void { |
972 | 0 | std::deque<Dictionary> queue({top_field}); |
973 | 0 | QPDFObjGen::set seen; |
974 | 0 | for (; !queue.empty(); queue.pop_front()) { |
975 | 0 | auto& obj = queue.front(); |
976 | 0 | if (seen.add(obj)) { |
977 | 0 | Dictionary parent = obj["/Parent"]; |
978 | 0 | if (parent.indirect()) { |
979 | 0 | auto parent_og = parent.id_gen(); |
980 | 0 | if (orig_to_copy.contains(parent_og)) { |
981 | 0 | obj.replace("/Parent", orig_to_copy[parent_og]); |
982 | 0 | } else { |
983 | 0 | parent.warn( |
984 | 0 | "while traversing field " + obj.id_gen().unparse(',') + |
985 | 0 | ", found parent (" + parent_og.unparse(',') + |
986 | 0 | ") that had not been seen, indicating likely invalid field structure"); |
987 | 0 | } |
988 | 0 | } |
989 | 0 | size_t i = 0; |
990 | 0 | Array Kids = obj["/Kids"]; |
991 | 0 | for (auto& kid: Kids) { |
992 | 0 | if (maybe_copy_object(kid)) { |
993 | 0 | Kids.set(i, kid); |
994 | 0 | queue.emplace_back(kid); |
995 | 0 | } |
996 | 0 | ++i; |
997 | 0 | } |
998 | 0 | adjustInheritedFields( |
999 | 0 | obj, override_da, from_default_da, override_q, from_default_q); |
1000 | 0 | if (foreign) { |
1001 | | // Lazily initialize our /DR and the conflict map. |
1002 | 0 | init_dr_map(); |
1003 | | // The spec doesn't say anything about /DR on the field, but lots of writers |
1004 | | // put one there, and it is frequently the same as the document-level /DR. |
1005 | | // To avoid having the field's /DR point to information that we are not |
1006 | | // maintaining, just reset it to that if it exists. Empirical evidence |
1007 | | // suggests that many readers, including Acrobat, Adobe Acrobat Reader, |
1008 | | // chrome, firefox, the mac Preview application, and several of the free |
1009 | | // readers on Linux all ignore /DR at the field level. |
1010 | 0 | if (obj.contains("/DR")) { |
1011 | 0 | obj.replace("/DR", dr); |
1012 | 0 | } |
1013 | 0 | if (obj["/DA"].isString() && !dr_map.empty()) { |
1014 | 0 | adjustDefaultAppearances(obj, dr_map); |
1015 | 0 | } |
1016 | 0 | } |
1017 | 0 | } |
1018 | 0 | } |
1019 | 0 | }; |
1020 | |
|
1021 | 0 | auto transform_annotation = |
1022 | 0 | [&](QPDFObjectHandle& annot) -> std::tuple<QPDFObjectHandle, bool, bool> { |
1023 | | // Make copies of annotations and fields down to the appearance streams, preserving all |
1024 | | // internal referential integrity. When the incoming annotations are from a different file, |
1025 | | // we first copy them locally. Then, whether local or foreign, we copy them again so that if |
1026 | | // we bring the same annotation in multiple times (e.g. overlaying a foreign page onto |
1027 | | // multiple local pages or a local page onto multiple other local pages), we don't create |
1028 | | // annotations that are referenced in more than one place. If we did that, the effect of |
1029 | | // applying transformations would be cumulative, which is definitely not what we want. |
1030 | | // Besides, annotations and fields are not intended to be referenced in multiple places. |
1031 | | |
1032 | | // Determine if this annotation is attached to a form field. If so, the annotation may be |
1033 | | // the same object as the form field, or the form field may have the annotation as a kid. In |
1034 | | // either case, we have to walk up the field structure to find the top-level field. Within |
1035 | | // one iteration through a set of annotations, we don't want to copy the same item more than |
1036 | | // once. For example, suppose we have field A with kids B, C, and D, each of which has |
1037 | | // annotations BA, CA, and DA. When we get to BA, we will find that BA is a kid of B which |
1038 | | // is under A. When we do a copyForeignObject of A, it will also copy everything else |
1039 | | // because of the indirect references. When we clone BA, we will want to clone A and then |
1040 | | // update A's clone's kid to point B's clone and B's clone's parent to point to A's clone. |
1041 | | // The same thing holds for annotations. Next, when we get to CA, we will again discover |
1042 | | // that A is the top, but we don't want to re-copy A. We want CA's clone to be linked to the |
1043 | | // same clone as BA's. Failure to do this will break up things like radio button groups, |
1044 | | // which all have to kids of the same parent. |
1045 | |
|
1046 | 0 | auto ffield = from_afdh->getFieldForAnnotation(annot); |
1047 | 0 | auto ffield_oh = ffield.getObjectHandle(); |
1048 | 0 | if (ffield.null()) { |
1049 | 0 | return {{}, false, false}; |
1050 | 0 | } |
1051 | 0 | if (ffield_oh.isStream()) { |
1052 | 0 | ffield.warn("ignoring form field that's a stream"); |
1053 | 0 | return {{}, false, false}; |
1054 | 0 | } |
1055 | 0 | if (!ffield_oh.isIndirect()) { |
1056 | 0 | ffield.warn("ignoring form field not indirect"); |
1057 | 0 | return {{}, false, false}; |
1058 | 0 | } |
1059 | | // A field and its associated annotation can be the same object. This matters because we |
1060 | | // don't want to clone the annotation and field separately in this case. |
1061 | | // Find the top-level field. It may be the field itself. |
1062 | 0 | bool have_parent = false; |
1063 | 0 | QPDFObjectHandle top_field = ffield.getTopLevelField(&have_parent).getObjectHandle(); |
1064 | 0 | if (foreign) { |
1065 | | // copyForeignObject returns the same value if called multiple times with the same |
1066 | | // field. Create/retrieve the local copy of the original field. This pulls over |
1067 | | // everything the field references including annotations and appearance streams, but |
1068 | | // it's harmless to call copyForeignObject on them too. They will already be copied, |
1069 | | // so we'll get the right object back. |
1070 | | |
1071 | | // top_field and ffield_oh are known to be indirect. |
1072 | 0 | top_field = qpdf.copyForeignObject(top_field); |
1073 | 0 | ffield_oh = qpdf.copyForeignObject(ffield_oh); |
1074 | 0 | } else { |
1075 | | // We don't need to add top_field to old_fields if it's foreign because the new copy |
1076 | | // of the foreign field won't be referenced anywhere. It's just the starting point |
1077 | | // for us to make an additional local copy of. |
1078 | 0 | old_fields.insert(top_field.getObjGen()); |
1079 | 0 | } |
1080 | |
|
1081 | 0 | if (maybe_copy_object(top_field)) { |
1082 | 0 | traverse_field(top_field); |
1083 | 0 | } |
1084 | | |
1085 | | // Now switch to copies. We already switched for top_field |
1086 | 0 | maybe_copy_object(ffield_oh); |
1087 | 0 | return {top_field, true, have_parent}; |
1088 | 0 | }; |
1089 | | |
1090 | | // Now do the actual copies. |
1091 | |
|
1092 | 0 | QPDFObjGen::set added_new_fields; |
1093 | 0 | for (auto annot: old_annots) { |
1094 | 0 | if (!annot.isDictionary()) { |
1095 | 0 | annot.warn("ignoring annotation that is not a dictionary"); |
1096 | 0 | continue; |
1097 | 0 | } |
1098 | 0 | auto [top_field, have_field, have_parent] = transform_annotation(annot); |
1099 | |
|
1100 | 0 | QTC::TC( |
1101 | 0 | "qpdf", |
1102 | 0 | "QPDFAcroFormDocumentHelper copy annotation", |
1103 | 0 | (have_field ? 1 : 0) | (foreign ? 2 : 0)); |
1104 | 0 | if (have_field) { |
1105 | 0 | QTC::TC( |
1106 | 0 | "qpdf", |
1107 | 0 | "QPDFAcroFormDocumentHelper field with parent", |
1108 | 0 | (have_parent ? 1 : 0) | (foreign ? 2 : 0)); |
1109 | 0 | } |
1110 | 0 | if (foreign) { |
1111 | 0 | if (!annot.isIndirect()) { |
1112 | 0 | annot = from_qpdf->makeIndirectObject(annot); |
1113 | 0 | } |
1114 | 0 | annot = qpdf.copyForeignObject(annot); |
1115 | 0 | } |
1116 | 0 | maybe_copy_object(annot); |
1117 | | |
1118 | | // Now we have copies, so we can safely mutate. |
1119 | 0 | if (have_field && added_new_fields.add(top_field)) { |
1120 | 0 | new_fields.emplace_back(top_field); |
1121 | 0 | } |
1122 | 0 | new_annots.emplace_back(annot); |
1123 | | |
1124 | | // Identify and copy any appearance streams |
1125 | |
|
1126 | 0 | auto ah = QPDFAnnotationObjectHelper(annot); |
1127 | 0 | Dictionary apdict = ah.getAppearanceDictionary(); |
1128 | 0 | std::vector<QPDFObjectHandle> streams; |
1129 | 0 | auto replace_stream = [](auto& dict, auto& key, auto& old) { |
1130 | 0 | dict.replace(key, old.copyStream()); |
1131 | 0 | return dict[key]; |
1132 | 0 | }; Unexecuted instantiation: QPDFAcroFormDocumentHelper.cc:auto qpdf::impl::AcroForm::transformAnnotations(QPDFObjectHandle, std::__1::vector<QPDFObjectHandle, std::__1::allocator<QPDFObjectHandle> >&, std::__1::vector<QPDFObjectHandle, std::__1::allocator<QPDFObjectHandle> >&, std::__1::set<QPDFObjGen, std::__1::less<QPDFObjGen>, std::__1::allocator<QPDFObjGen> >&, QPDFMatrix const&, QPDF*, qpdf::impl::AcroForm*, QPDFObjectHandle*)::$_0::operator()<qpdf::Dictionary, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, QPDFObjectHandle>(qpdf::Dictionary&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, QPDFObjectHandle&) const Unexecuted instantiation: QPDFAcroFormDocumentHelper.cc:auto qpdf::impl::AcroForm::transformAnnotations(QPDFObjectHandle, std::__1::vector<QPDFObjectHandle, std::__1::allocator<QPDFObjectHandle> >&, std::__1::vector<QPDFObjectHandle, std::__1::allocator<QPDFObjectHandle> >&, std::__1::set<QPDFObjGen, std::__1::less<QPDFObjGen>, std::__1::allocator<QPDFObjGen> >&, QPDFMatrix const&, QPDF*, qpdf::impl::AcroForm*, QPDFObjectHandle*)::$_0::operator()<QPDFObjectHandle, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, QPDFObjectHandle>(QPDFObjectHandle&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, QPDFObjectHandle&) const |
1133 | |
|
1134 | 0 | for (auto& [key1, value1]: apdict) { |
1135 | 0 | if (value1.isStream()) { |
1136 | 0 | streams.emplace_back(replace_stream(apdict, key1, value1)); |
1137 | 0 | } else { |
1138 | 0 | for (auto& [key2, value2]: Dictionary(value1)) { |
1139 | 0 | if (value2.isStream()) { |
1140 | 0 | streams.emplace_back(replace_stream(value1, key2, value2)); |
1141 | 0 | } |
1142 | 0 | } |
1143 | 0 | } |
1144 | 0 | } |
1145 | | |
1146 | | // Now we can safely mutate the annotation and its appearance streams. |
1147 | 0 | for (auto& stream: streams) { |
1148 | 0 | Dictionary dict = stream.getDict(); |
1149 | |
|
1150 | 0 | QPDFMatrix apcm; |
1151 | 0 | Array omatrix = dict["/Matrix"]; |
1152 | 0 | if (omatrix) { |
1153 | 0 | apcm = QPDFMatrix(QPDFObjectHandle(omatrix).getArrayAsMatrix()); |
1154 | 0 | } |
1155 | 0 | apcm.concat(cm); |
1156 | 0 | if (omatrix || apcm != QPDFMatrix()) { |
1157 | 0 | dict.replace("/Matrix", QPDFObjectHandle::newFromMatrix(apcm)); |
1158 | 0 | } |
1159 | 0 | Dictionary resources = dict["/Resources"]; |
1160 | 0 | if (!dr_map.empty() && resources) { |
1161 | 0 | adjustAppearanceStream(stream, dr_map); |
1162 | 0 | } |
1163 | 0 | } |
1164 | 0 | auto rect = cm.transformRectangle(annot["/Rect"].getArrayAsRectangle()); |
1165 | 0 | annot.replaceKey("/Rect", QPDFObjectHandle::newFromRectangle(rect)); |
1166 | 0 | if (new_page && annot.contains("/P")) { |
1167 | 0 | annot.replace("/P", *new_page); |
1168 | 0 | } |
1169 | 0 | } |
1170 | 0 | } |
1171 | | |
1172 | | void |
1173 | | QPDFAcroFormDocumentHelper::fixCopiedAnnotations( |
1174 | | QPDFObjectHandle to_page, |
1175 | | QPDFObjectHandle from_page, |
1176 | | QPDFAcroFormDocumentHelper& from_afdh, |
1177 | | std::set<QPDFObjGen>* added_fields) |
1178 | 0 | { |
1179 | 0 | m->fixCopiedAnnotations(to_page, from_page, *from_afdh.m, added_fields); |
1180 | 0 | } |
1181 | | |
1182 | | void |
1183 | | AcroForm::fixCopiedAnnotations( |
1184 | | QPDFObjectHandle to_page, |
1185 | | QPDFObjectHandle from_page, |
1186 | | AcroForm& from_afdh, |
1187 | | std::set<QPDFObjGen>* added_fields) |
1188 | 0 | { |
1189 | 0 | auto const& old_annots = from_page["/Annots"]; |
1190 | 0 | if (!old_annots) { |
1191 | 0 | return; |
1192 | 0 | } |
1193 | 0 | if (!old_annots.isArray()) { |
1194 | 0 | from_page.warn("removing non-array /Annots"); |
1195 | 0 | from_page.erase("/Annots"); |
1196 | 0 | return; |
1197 | 0 | } |
1198 | 0 | if (old_annots.empty()) { |
1199 | 0 | from_page.erase("/Annots"); |
1200 | 0 | return; |
1201 | 0 | } |
1202 | | |
1203 | 0 | std::vector<QPDFObjectHandle> new_annots; |
1204 | 0 | std::vector<QPDFObjectHandle> new_fields; |
1205 | 0 | std::set<QPDFObjGen> old_fields; |
1206 | 0 | transformAnnotations( |
1207 | 0 | old_annots, |
1208 | 0 | new_annots, |
1209 | 0 | new_fields, |
1210 | 0 | old_fields, |
1211 | 0 | QPDFMatrix(), |
1212 | 0 | &(from_afdh.qpdf), |
1213 | 0 | &from_afdh, |
1214 | 0 | &to_page); |
1215 | |
|
1216 | 0 | to_page.replace("/Annots", Array(std::move(new_annots))); |
1217 | 0 | addAndRenameFormFields(new_fields); |
1218 | 0 | if (added_fields) { |
1219 | 0 | for (auto const& f: new_fields) { |
1220 | 0 | added_fields->insert(f.getObjGen()); |
1221 | 0 | } |
1222 | 0 | } |
1223 | 0 | } |