/src/qpdf/libqpdf/QPDFFormFieldObjectHelper.cc
Line | Count | Source |
1 | | #include <qpdf/QPDFFormFieldObjectHelper.hh> |
2 | | |
3 | | #include <qpdf/AcroForm.hh> |
4 | | |
5 | | #include <qpdf/Pipeline_private.hh> |
6 | | #include <qpdf/Pl_QPDFTokenizer.hh> |
7 | | #include <qpdf/QIntC.hh> |
8 | | #include <qpdf/QPDFAcroFormDocumentHelper.hh> |
9 | | #include <qpdf/QPDFAnnotationObjectHelper.hh> |
10 | | #include <qpdf/QPDFObjectHandle_private.hh> |
11 | | #include <qpdf/QPDF_private.hh> |
12 | | #include <qpdf/QTC.hh> |
13 | | #include <qpdf/QUtil.hh> |
14 | | #include <cstdlib> |
15 | | |
16 | | #include <memory> |
17 | | |
18 | | using namespace qpdf; |
19 | | |
20 | | using FormNode = qpdf::impl::FormNode; |
21 | | |
22 | | const QPDFObjectHandle FormNode::null_oh; |
23 | | |
24 | | class QPDFFormFieldObjectHelper::Members: public FormNode |
25 | | { |
26 | | public: |
27 | | Members(QPDFObjectHandle const& oh) : |
28 | 0 | FormNode(oh) |
29 | 0 | { |
30 | 0 | } |
31 | | }; |
32 | | |
33 | | QPDFFormFieldObjectHelper::QPDFFormFieldObjectHelper(QPDFObjectHandle o) : |
34 | 0 | QPDFObjectHelper(o), |
35 | 0 | m(std::make_shared<Members>(oh())) |
36 | 0 | { |
37 | 0 | } |
38 | | |
39 | | QPDFFormFieldObjectHelper::QPDFFormFieldObjectHelper() : |
40 | 0 | QPDFObjectHelper(Null::temp()), |
41 | 0 | m(std::make_shared<Members>(QPDFObjectHandle())) |
42 | 0 | { |
43 | 0 | } |
44 | | |
45 | | bool |
46 | | QPDFFormFieldObjectHelper::isNull() |
47 | 0 | { |
48 | 0 | return m->null(); |
49 | 0 | } |
50 | | |
51 | | QPDFFormFieldObjectHelper |
52 | | QPDFFormFieldObjectHelper::getParent() |
53 | 0 | { |
54 | 0 | return {Null::if_null(m->Parent().oh())}; |
55 | 0 | } |
56 | | |
57 | | QPDFFormFieldObjectHelper |
58 | | QPDFFormFieldObjectHelper::getTopLevelField(bool* is_different) |
59 | 0 | { |
60 | 0 | return Null::if_null(m->root_field(is_different).oh()); |
61 | 0 | } |
62 | | |
63 | | FormNode |
64 | | FormNode::root_field(bool* is_different) |
65 | 0 | { |
66 | 0 | if (is_different) { |
67 | 0 | *is_different = false; |
68 | 0 | } |
69 | 0 | if (!obj) { |
70 | 0 | return {}; |
71 | 0 | } |
72 | 0 | auto rf = *this; |
73 | 0 | size_t depth = 0; // Don't bother with loop detection until depth becomes suspicious |
74 | 0 | QPDFObjGen::set seen; |
75 | 0 | while (rf.Parent() && (++depth < 10 || seen.add(rf))) { |
76 | 0 | rf = rf.Parent(); |
77 | 0 | if (is_different) { |
78 | 0 | *is_different = true; |
79 | 0 | } |
80 | 0 | } |
81 | 0 | return rf; |
82 | 0 | } |
83 | | |
84 | | QPDFObjectHandle |
85 | | QPDFFormFieldObjectHelper::getInheritableFieldValue(std::string const& name) |
86 | 0 | { |
87 | 0 | return Null::if_null(m->inheritable_value<QPDFObjectHandle>(name)); |
88 | 0 | } |
89 | | |
90 | | QPDFObjectHandle const& |
91 | | FormNode::inherited(std::string const& name, bool acroform) const |
92 | 0 | { |
93 | 0 | if (!obj) { |
94 | 0 | return null_oh; |
95 | 0 | } |
96 | 0 | auto node = *this; |
97 | 0 | QPDFObjGen::set seen; |
98 | 0 | size_t depth = 0; // Don't bother with loop detection until depth becomes suspicious |
99 | 0 | while (node.Parent() && (++depth < 10 || seen.add(node))) { |
100 | 0 | node = node.Parent(); |
101 | 0 | if (auto const& result = node[name]) { |
102 | 0 | return {result}; |
103 | 0 | } |
104 | 0 | } |
105 | 0 | return acroform ? from_AcroForm(name) : null_oh; |
106 | 0 | } |
107 | | |
108 | | std::string |
109 | | QPDFFormFieldObjectHelper::getInheritableFieldValueAsString(std::string const& name) |
110 | 0 | { |
111 | 0 | return m->inheritable_string(name); |
112 | 0 | } |
113 | | |
114 | | std::string |
115 | | FormNode::inheritable_string(std::string const& name) const |
116 | 0 | { |
117 | 0 | if (auto fv = inheritable_value<String>(name)) { |
118 | 0 | return fv.utf8_value(); |
119 | 0 | } |
120 | 0 | return {}; |
121 | 0 | } |
122 | | |
123 | | std::string |
124 | | QPDFFormFieldObjectHelper::getInheritableFieldValueAsName(std::string const& name) |
125 | 0 | { |
126 | 0 | if (auto fv = m->inheritable_value<Name>(name)) { |
127 | 0 | return fv; |
128 | 0 | } |
129 | 0 | return {}; |
130 | 0 | } |
131 | | |
132 | | std::string |
133 | | QPDFFormFieldObjectHelper::getFieldType() |
134 | 0 | { |
135 | 0 | if (auto ft = m->FT()) { |
136 | 0 | return ft; |
137 | 0 | } |
138 | 0 | return {}; |
139 | 0 | } |
140 | | |
141 | | std::string |
142 | | QPDFFormFieldObjectHelper::getFullyQualifiedName() |
143 | 0 | { |
144 | 0 | return m->fully_qualified_name(); |
145 | 0 | } |
146 | | |
147 | | std::string |
148 | | FormNode::fully_qualified_name() const |
149 | 0 | { |
150 | 0 | std::string result; |
151 | 0 | auto node = *this; |
152 | 0 | QPDFObjGen::set seen; |
153 | 0 | size_t depth = 0; // Don't bother with loop detection until depth becomes suspicious |
154 | 0 | while (node && (++depth < 10 || seen.add(node))) { |
155 | 0 | if (auto T = node.T()) { |
156 | 0 | if (!result.empty()) { |
157 | 0 | result.insert(0, 1, '.'); |
158 | 0 | } |
159 | 0 | result.insert(0, T.utf8_value()); |
160 | 0 | } |
161 | 0 | node = node.Parent(); |
162 | 0 | } |
163 | 0 | return result; |
164 | 0 | } |
165 | | |
166 | | std::string |
167 | | QPDFFormFieldObjectHelper::getPartialName() |
168 | 0 | { |
169 | 0 | return m->partial_name(); |
170 | 0 | } |
171 | | |
172 | | std::string |
173 | | FormNode::partial_name() const |
174 | 0 | { |
175 | 0 | if (auto pn = T()) { |
176 | 0 | return pn.utf8_value(); |
177 | 0 | } |
178 | 0 | return {}; |
179 | 0 | } |
180 | | |
181 | | std::string |
182 | | QPDFFormFieldObjectHelper::getAlternativeName() |
183 | 0 | { |
184 | 0 | return m->alternative_name(); |
185 | 0 | } |
186 | | |
187 | | std::string |
188 | | FormNode::alternative_name() const |
189 | 0 | { |
190 | 0 | if (auto an = TU()) { |
191 | 0 | return an.utf8_value(); |
192 | 0 | } |
193 | 0 | return fully_qualified_name(); |
194 | 0 | } |
195 | | |
196 | | std::string |
197 | | QPDFFormFieldObjectHelper::getMappingName() |
198 | 0 | { |
199 | 0 | return m->mapping_name(); |
200 | 0 | } |
201 | | |
202 | | std::string |
203 | | FormNode::mapping_name() const |
204 | 0 | { |
205 | 0 | if (auto mn = TM()) { |
206 | 0 | return mn.utf8_value(); |
207 | 0 | } |
208 | 0 | return alternative_name(); |
209 | 0 | } |
210 | | |
211 | | QPDFObjectHandle |
212 | | QPDFFormFieldObjectHelper::getValue() |
213 | 0 | { |
214 | 0 | return Null::if_null(m->V<QPDFObjectHandle>()); |
215 | 0 | } |
216 | | |
217 | | std::string |
218 | | QPDFFormFieldObjectHelper::getValueAsString() |
219 | 0 | { |
220 | 0 | return m->value(); |
221 | 0 | } |
222 | | |
223 | | std::string |
224 | | FormNode::value() const |
225 | 0 | { |
226 | 0 | return inheritable_string("/V"); |
227 | 0 | } |
228 | | |
229 | | QPDFObjectHandle |
230 | | QPDFFormFieldObjectHelper::getDefaultValue() |
231 | 0 | { |
232 | 0 | return Null::if_null(m->DV()); |
233 | 0 | } |
234 | | |
235 | | std::string |
236 | | QPDFFormFieldObjectHelper::getDefaultValueAsString() |
237 | 0 | { |
238 | 0 | return m->default_value(); |
239 | 0 | } |
240 | | |
241 | | std::string |
242 | | FormNode::default_value() const |
243 | 0 | { |
244 | 0 | return inheritable_string("/DV"); |
245 | 0 | } |
246 | | |
247 | | QPDFObjectHandle |
248 | | QPDFFormFieldObjectHelper::getDefaultResources() |
249 | 0 | { |
250 | 0 | return Null::if_null(m->getDefaultResources()); |
251 | 0 | } |
252 | | |
253 | | QPDFObjectHandle |
254 | | FormNode::getDefaultResources() |
255 | 0 | { |
256 | 0 | return from_AcroForm("/DR"); |
257 | 0 | } |
258 | | |
259 | | std::string |
260 | | QPDFFormFieldObjectHelper::getDefaultAppearance() |
261 | 0 | { |
262 | 0 | return m->default_appearance(); |
263 | 0 | } |
264 | | |
265 | | std::string |
266 | | FormNode::default_appearance() const |
267 | 0 | { |
268 | 0 | if (auto DA = inheritable_value<String>("/DA")) { |
269 | 0 | return DA.utf8_value(); |
270 | 0 | } |
271 | 0 | if (String DA = from_AcroForm("/DA")) { |
272 | 0 | return DA.utf8_value(); |
273 | 0 | } |
274 | 0 | return {}; |
275 | 0 | } |
276 | | |
277 | | int |
278 | | QPDFFormFieldObjectHelper::getQuadding() |
279 | 0 | { |
280 | 0 | return m->getQuadding(); |
281 | 0 | } |
282 | | |
283 | | int |
284 | | FormNode::getQuadding() |
285 | 0 | { |
286 | 0 | auto fv = inheritable_value<QPDFObjectHandle>("/Q"); |
287 | 0 | bool looked_in_acroform = false; |
288 | 0 | if (!fv.isInteger()) { |
289 | 0 | fv = from_AcroForm("/Q"); |
290 | 0 | looked_in_acroform = true; |
291 | 0 | } |
292 | 0 | if (fv.isInteger()) { |
293 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper Q present", looked_in_acroform ? 0 : 1); |
294 | 0 | return QIntC::to_int(fv.getIntValue()); |
295 | 0 | } |
296 | 0 | return 0; |
297 | 0 | } |
298 | | |
299 | | int |
300 | | QPDFFormFieldObjectHelper::getFlags() |
301 | 0 | { |
302 | 0 | return m->getFlags(); |
303 | 0 | } |
304 | | |
305 | | int |
306 | | FormNode::getFlags() |
307 | 0 | { |
308 | 0 | auto f = inheritable_value<QPDFObjectHandle>("/Ff"); |
309 | 0 | return f.isInteger() ? f.getIntValueAsInt() : 0; |
310 | 0 | } |
311 | | |
312 | | bool |
313 | | QPDFFormFieldObjectHelper::isText() |
314 | 0 | { |
315 | 0 | return m->isText(); |
316 | 0 | } |
317 | | |
318 | | bool |
319 | | FormNode::isText() |
320 | 0 | { |
321 | 0 | return FT() == "/Tx"; |
322 | 0 | } |
323 | | |
324 | | bool |
325 | | QPDFFormFieldObjectHelper::isCheckbox() |
326 | 0 | { |
327 | 0 | return m->isCheckbox(); |
328 | 0 | } |
329 | | |
330 | | bool |
331 | | FormNode::isCheckbox() |
332 | 0 | { |
333 | 0 | return FT() == "/Btn" && (getFlags() & (ff_btn_radio | ff_btn_pushbutton)) == 0; |
334 | 0 | } |
335 | | |
336 | | bool |
337 | | QPDFFormFieldObjectHelper::isChecked() |
338 | 0 | { |
339 | 0 | return m->isChecked(); |
340 | 0 | } |
341 | | |
342 | | bool |
343 | | FormNode::isChecked() |
344 | 0 | { |
345 | 0 | return isCheckbox() && V<Name>() != "/Off"; |
346 | 0 | } |
347 | | |
348 | | bool |
349 | | QPDFFormFieldObjectHelper::isRadioButton() |
350 | 0 | { |
351 | 0 | return m->isRadioButton(); |
352 | 0 | } |
353 | | |
354 | | bool |
355 | | FormNode::isRadioButton() |
356 | 0 | { |
357 | 0 | return FT() == "/Btn" && (getFlags() & ff_btn_radio) == ff_btn_radio; |
358 | 0 | } |
359 | | |
360 | | bool |
361 | | QPDFFormFieldObjectHelper::isPushbutton() |
362 | 0 | { |
363 | 0 | return m->isPushbutton(); |
364 | 0 | } |
365 | | |
366 | | bool |
367 | | FormNode::isPushbutton() |
368 | 0 | { |
369 | 0 | return FT() == "/Btn" && (getFlags() & ff_btn_pushbutton) == ff_btn_pushbutton; |
370 | 0 | } |
371 | | |
372 | | bool |
373 | | QPDFFormFieldObjectHelper::isChoice() |
374 | 0 | { |
375 | 0 | return m->isChoice(); |
376 | 0 | } |
377 | | |
378 | | bool |
379 | | FormNode::isChoice() |
380 | 0 | { |
381 | 0 | return FT() == "/Ch"; |
382 | 0 | } |
383 | | |
384 | | std::vector<std::string> |
385 | | QPDFFormFieldObjectHelper::getChoices() |
386 | 0 | { |
387 | 0 | return m->getChoices(); |
388 | 0 | } |
389 | | |
390 | | std::vector<std::string> |
391 | | FormNode::getChoices() |
392 | 0 | { |
393 | 0 | if (!isChoice()) { |
394 | 0 | return {}; |
395 | 0 | } |
396 | 0 | std::vector<std::string> result; |
397 | 0 | for (auto const& item: inheritable_value<Array>("/Opt")) { |
398 | 0 | if (item.isString()) { |
399 | 0 | result.emplace_back(item.getUTF8Value()); |
400 | 0 | } else if (item.size() == 2) { |
401 | 0 | auto display = item.getArrayItem(1); |
402 | 0 | if (display.isString()) { |
403 | 0 | result.emplace_back(display.getUTF8Value()); |
404 | 0 | } |
405 | 0 | } |
406 | 0 | } |
407 | 0 | return result; |
408 | 0 | } |
409 | | |
410 | | void |
411 | | QPDFFormFieldObjectHelper::setFieldAttribute(std::string const& key, QPDFObjectHandle value) |
412 | 0 | { |
413 | 0 | m->setFieldAttribute(key, value); |
414 | 0 | } |
415 | | |
416 | | void |
417 | | FormNode::setFieldAttribute(std::string const& key, QPDFObjectHandle value) |
418 | 0 | { |
419 | 0 | replace(key, value); |
420 | 0 | } |
421 | | |
422 | | void |
423 | | FormNode::setFieldAttribute(std::string const& key, Name const& value) |
424 | 0 | { |
425 | 0 | replace(key, value); |
426 | 0 | } |
427 | | |
428 | | void |
429 | | QPDFFormFieldObjectHelper::setFieldAttribute(std::string const& key, std::string const& utf8_value) |
430 | 0 | { |
431 | 0 | m->setFieldAttribute(key, utf8_value); |
432 | 0 | } |
433 | | |
434 | | void |
435 | | FormNode::setFieldAttribute(std::string const& key, std::string const& utf8_value) |
436 | 0 | { |
437 | 0 | replace(key, String::utf16(utf8_value)); |
438 | 0 | } |
439 | | |
440 | | void |
441 | | QPDFFormFieldObjectHelper::setV(QPDFObjectHandle value, bool need_appearances) |
442 | 0 | { |
443 | 0 | m->setV(value, need_appearances); |
444 | 0 | } |
445 | | |
446 | | void |
447 | | FormNode::setV(QPDFObjectHandle value, bool need_appearances) |
448 | 0 | { |
449 | 0 | if (FT() == "/Btn") { |
450 | 0 | Name name = value; |
451 | 0 | if (isCheckbox()) { |
452 | 0 | if (!name) { |
453 | 0 | warn("ignoring attempt to set a checkbox field to a value whose type is not name"); |
454 | 0 | return; |
455 | 0 | } |
456 | | // Accept any value other than /Off to mean checked. Files have been seen that use |
457 | | // /1 or other values. |
458 | 0 | setCheckBoxValue(name != "/Off"); |
459 | 0 | return; |
460 | 0 | } |
461 | 0 | if (isRadioButton()) { |
462 | 0 | if (!name) { |
463 | 0 | warn( |
464 | 0 | "ignoring attempt to set a radio button field to an object that is not a name"); |
465 | 0 | return; |
466 | 0 | } |
467 | 0 | setRadioButtonValue(name); |
468 | 0 | return; |
469 | 0 | } |
470 | 0 | if (isPushbutton()) { |
471 | 0 | warn("ignoring attempt set the value of a pushbutton field"); |
472 | 0 | } |
473 | 0 | return; |
474 | 0 | } |
475 | 0 | if (value.isString()) { |
476 | 0 | setFieldAttribute("/V", QPDFObjectHandle::newUnicodeString(value.getUTF8Value())); |
477 | 0 | } else { |
478 | 0 | setFieldAttribute("/V", value); |
479 | 0 | } |
480 | 0 | if (need_appearances) { |
481 | 0 | QPDF& qpdf = oh().getQPDF( |
482 | 0 | "QPDFFormFieldObjectHelper::setV called with need_appearances = " |
483 | 0 | "true on an object that is not associated with an owning QPDF"); |
484 | 0 | qpdf.doc().acroform().setNeedAppearances(true); |
485 | 0 | } |
486 | 0 | } |
487 | | |
488 | | void |
489 | | QPDFFormFieldObjectHelper::setV(std::string const& utf8_value, bool need_appearances) |
490 | 0 | { |
491 | 0 | m->setV(utf8_value, need_appearances); |
492 | 0 | } |
493 | | |
494 | | void |
495 | | FormNode::setV(std::string const& utf8_value, bool need_appearances) |
496 | 0 | { |
497 | 0 | setV(QPDFObjectHandle::newUnicodeString(utf8_value), need_appearances); |
498 | 0 | } |
499 | | |
500 | | void |
501 | | FormNode::setRadioButtonValue(Name const& name) |
502 | 0 | { |
503 | 0 | qpdf_expect(name); |
504 | | // Set the value of a radio button field. This has the following specific behavior: |
505 | | // * If this is a node without /Kids, assume this is a individual radio button widget and call |
506 | | // itself on the parent |
507 | | // * If this is a radio button field with children, set /V to the given value. Then, for each |
508 | | // child, if the child has the specified value as one of its keys in the /N subdictionary of |
509 | | // its /AP (i.e. its normal appearance stream dictionary), set /AS to name; otherwise, if /Off |
510 | | // is a member, set /AS to /Off. |
511 | 0 | auto kids = Kids(); |
512 | 0 | if (!kids) { |
513 | | // This is most likely one of the individual buttons. Try calling on the parent. |
514 | 0 | auto parent = Parent(); |
515 | 0 | if (parent.Kids()) { |
516 | 0 | parent.setRadioButtonValue(name); |
517 | 0 | return; |
518 | 0 | } |
519 | 0 | } |
520 | 0 | if (!isRadioButton() || !kids) { |
521 | 0 | warn("don't know how to set the value of this field as a radio button"); |
522 | 0 | return; |
523 | 0 | } |
524 | 0 | replace("/V", name); |
525 | 0 | for (FormNode kid: kids) { |
526 | 0 | auto ap = kid.AP(); |
527 | 0 | QPDFObjectHandle annot; |
528 | 0 | if (!ap) { |
529 | | // The widget may be below. If there is more than one, just find the first one. |
530 | 0 | for (FormNode grandkid: kid.Kids()) { |
531 | 0 | ap = grandkid.AP(); |
532 | 0 | if (ap) { |
533 | 0 | annot = grandkid; |
534 | 0 | break; |
535 | 0 | } |
536 | 0 | } |
537 | 0 | } else { |
538 | 0 | annot = kid; |
539 | 0 | } |
540 | 0 | if (!annot) { |
541 | 0 | warn("unable to set the value of this radio button"); |
542 | 0 | continue; |
543 | 0 | } |
544 | 0 | if (ap["/N"].contains(name.value())) { |
545 | 0 | annot.replace("/AS", name); |
546 | 0 | } else { |
547 | 0 | annot.replace("/AS", Name("/Off")); |
548 | 0 | } |
549 | 0 | } |
550 | 0 | } |
551 | | |
552 | | void |
553 | | FormNode::setCheckBoxValue(bool value) |
554 | 0 | { |
555 | 0 | auto ap = AP(); |
556 | 0 | QPDFObjectHandle annot; |
557 | 0 | if (ap) { |
558 | 0 | annot = oh(); |
559 | 0 | } else { |
560 | | // The widget may be below. If there is more than one, just find the first one. |
561 | 0 | for (FormNode kid: Kids()) { |
562 | 0 | ap = kid.AP(); |
563 | 0 | if (ap) { |
564 | 0 | annot = kid; |
565 | 0 | break; |
566 | 0 | } |
567 | 0 | } |
568 | 0 | } |
569 | 0 | std::string on_value; |
570 | 0 | if (value) { |
571 | | // Set the "on" value to the first value in the appearance stream's normal state dictionary |
572 | | // that isn't /Off. If not found, fall back to /Yes. |
573 | 0 | if (ap) { |
574 | 0 | for (auto const& item: Dictionary(ap["/N"])) { |
575 | 0 | if (item.first != "/Off") { |
576 | 0 | on_value = item.first; |
577 | 0 | break; |
578 | 0 | } |
579 | 0 | } |
580 | 0 | } |
581 | 0 | if (on_value.empty()) { |
582 | 0 | on_value = "/Yes"; |
583 | 0 | } |
584 | 0 | } |
585 | | |
586 | | // Set /AS to the on value or /Off in addition to setting /V. |
587 | 0 | auto name = Name(value ? on_value : "/Off"); |
588 | 0 | setFieldAttribute("/V", name); |
589 | 0 | if (!annot) { |
590 | 0 | warn("unable to set the value of this checkbox"); |
591 | 0 | return; |
592 | 0 | } |
593 | 0 | annot.replace("/AS", name); |
594 | 0 | } |
595 | | |
596 | | void |
597 | | QPDFFormFieldObjectHelper::generateAppearance(QPDFAnnotationObjectHelper& aoh) |
598 | 0 | { |
599 | 0 | m->generateAppearance(aoh); |
600 | 0 | } |
601 | | |
602 | | void |
603 | | FormNode::generateAppearance(QPDFAnnotationObjectHelper& aoh) |
604 | 0 | { |
605 | | // Ignore field types we don't know how to generate appearances for. Button fields don't really |
606 | | // need them -- see code in QPDFAcroFormDocumentHelper::generateAppearancesIfNeeded. |
607 | 0 | auto ft = FT(); |
608 | 0 | if (ft == "/Tx" || ft == "/Ch") { |
609 | 0 | generateTextAppearance(aoh); |
610 | 0 | } |
611 | 0 | } |
612 | | |
613 | | namespace |
614 | | { |
615 | | class ValueSetter: public QPDFObjectHandle::TokenFilter |
616 | | { |
617 | | public: |
618 | | ValueSetter( |
619 | | std::string const& DA, |
620 | | std::string const& V, |
621 | | std::vector<std::string> const& opt, |
622 | | double tf, |
623 | | QPDFObjectHandle::Rectangle const& bbox); |
624 | 0 | ~ValueSetter() override = default; |
625 | | void handleToken(QPDFTokenizer::Token const&) override; |
626 | | void handleEOF() override; |
627 | | void writeAppearance(); |
628 | | |
629 | | private: |
630 | | std::string DA; |
631 | | std::string V; |
632 | | std::vector<std::string> opt; |
633 | | double tf; |
634 | | QPDFObjectHandle::Rectangle bbox; |
635 | | enum { st_top, st_bmc, st_emc, st_end } state{st_top}; |
636 | | bool replaced{false}; |
637 | | }; |
638 | | } // namespace |
639 | | |
640 | | ValueSetter::ValueSetter( |
641 | | std::string const& DA, |
642 | | std::string const& V, |
643 | | std::vector<std::string> const& opt, |
644 | | double tf, |
645 | | QPDFObjectHandle::Rectangle const& bbox) : |
646 | 0 | DA(DA), |
647 | 0 | V(V), |
648 | 0 | opt(opt), |
649 | 0 | tf(tf), |
650 | 0 | bbox(bbox) |
651 | 0 | { |
652 | 0 | } |
653 | | |
654 | | void |
655 | | ValueSetter::handleToken(QPDFTokenizer::Token const& token) |
656 | 0 | { |
657 | 0 | QPDFTokenizer::token_type_e ttype = token.getType(); |
658 | 0 | std::string value = token.getValue(); |
659 | 0 | bool do_replace = false; |
660 | 0 | switch (state) { |
661 | 0 | case st_top: |
662 | 0 | writeToken(token); |
663 | 0 | if (token.isWord("BMC")) { |
664 | 0 | state = st_bmc; |
665 | 0 | } |
666 | 0 | break; |
667 | | |
668 | 0 | case st_bmc: |
669 | 0 | if ((ttype == QPDFTokenizer::tt_space) || (ttype == QPDFTokenizer::tt_comment)) { |
670 | 0 | writeToken(token); |
671 | 0 | } else { |
672 | 0 | state = st_emc; |
673 | 0 | } |
674 | | // fall through to emc |
675 | |
|
676 | 0 | case st_emc: |
677 | 0 | if (token.isWord("EMC")) { |
678 | 0 | do_replace = true; |
679 | 0 | state = st_end; |
680 | 0 | } |
681 | 0 | break; |
682 | | |
683 | 0 | case st_end: |
684 | 0 | writeToken(token); |
685 | 0 | break; |
686 | 0 | } |
687 | 0 | if (do_replace) { |
688 | 0 | writeAppearance(); |
689 | 0 | } |
690 | 0 | } |
691 | | |
692 | | void |
693 | | ValueSetter::handleEOF() |
694 | 0 | { |
695 | 0 | if (!replaced) { |
696 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper replaced BMC at EOF"); |
697 | 0 | write("/Tx BMC\n"); |
698 | 0 | writeAppearance(); |
699 | 0 | } |
700 | 0 | } |
701 | | |
702 | | void |
703 | | ValueSetter::writeAppearance() |
704 | 0 | { |
705 | 0 | replaced = true; |
706 | | |
707 | | // This code does not take quadding into consideration because doing so requires font metric |
708 | | // information, which we don't have in many cases. |
709 | |
|
710 | 0 | double tfh = 1.2 * tf; |
711 | 0 | int dx = 1; |
712 | | |
713 | | // Write one or more lines, centered vertically, possibly with one row highlighted. |
714 | |
|
715 | 0 | auto max_rows = static_cast<size_t>((bbox.ury - bbox.lly) / tfh); |
716 | 0 | bool highlight = false; |
717 | 0 | size_t highlight_idx = 0; |
718 | |
|
719 | 0 | std::vector<std::string> lines; |
720 | 0 | if (opt.empty() || (max_rows < 2)) { |
721 | 0 | lines.push_back(V); |
722 | 0 | } else { |
723 | | // Figure out what rows to write |
724 | 0 | size_t nopt = opt.size(); |
725 | 0 | size_t found_idx = 0; |
726 | 0 | bool found = false; |
727 | 0 | for (found_idx = 0; found_idx < nopt; ++found_idx) { |
728 | 0 | if (opt.at(found_idx) == V) { |
729 | 0 | found = true; |
730 | 0 | break; |
731 | 0 | } |
732 | 0 | } |
733 | 0 | if (found) { |
734 | | // Try to make the found item the second one, but adjust for under/overflow. |
735 | 0 | int wanted_first = QIntC::to_int(found_idx) - 1; |
736 | 0 | int wanted_last = QIntC::to_int(found_idx + max_rows) - 2; |
737 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper list found"); |
738 | 0 | if (wanted_first < 0) { |
739 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper list first too low"); |
740 | 0 | wanted_last -= wanted_first; |
741 | 0 | wanted_first = 0; |
742 | 0 | } |
743 | 0 | if (wanted_last >= QIntC::to_int(nopt)) { |
744 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper list last too high"); |
745 | 0 | auto diff = wanted_last - QIntC::to_int(nopt) + 1; |
746 | 0 | wanted_first = std::max(0, wanted_first - diff); |
747 | 0 | wanted_last -= diff; |
748 | 0 | } |
749 | 0 | highlight = true; |
750 | 0 | highlight_idx = found_idx - QIntC::to_size(wanted_first); |
751 | 0 | for (size_t i = QIntC::to_size(wanted_first); i <= QIntC::to_size(wanted_last); ++i) { |
752 | 0 | lines.push_back(opt.at(i)); |
753 | 0 | } |
754 | 0 | } else { |
755 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper list not found"); |
756 | | // include our value and the first n-1 rows |
757 | 0 | highlight_idx = 0; |
758 | 0 | highlight = true; |
759 | 0 | lines.push_back(V); |
760 | 0 | for (size_t i = 0; ((i < nopt) && (i < (max_rows - 1))); ++i) { |
761 | 0 | lines.push_back(opt.at(i)); |
762 | 0 | } |
763 | 0 | } |
764 | 0 | } |
765 | | |
766 | | // Write the lines centered vertically, highlighting if needed |
767 | 0 | size_t nlines = lines.size(); |
768 | 0 | double dy = bbox.ury - ((bbox.ury - bbox.lly - (static_cast<double>(nlines) * tfh)) / 2.0); |
769 | 0 | if (highlight) { |
770 | 0 | write( |
771 | 0 | "q\n0.85 0.85 0.85 rg\n" + QUtil::double_to_string(bbox.llx) + " " + |
772 | 0 | QUtil::double_to_string( |
773 | 0 | bbox.lly + dy - (tfh * (static_cast<double>(highlight_idx + 1)))) + |
774 | 0 | " " + QUtil::double_to_string(bbox.urx - bbox.llx) + " " + |
775 | 0 | QUtil::double_to_string(tfh) + " re f\nQ\n"); |
776 | 0 | } |
777 | 0 | dy -= tf; |
778 | 0 | write("q\nBT\n" + DA + "\n"); |
779 | 0 | for (size_t i = 0; i < nlines; ++i) { |
780 | | // We could adjust Tm to translate to the beginning the first line, set TL to tfh, and use |
781 | | // T* for each subsequent line, but doing this would require extracting any Tm from DA, |
782 | | // which doesn't seem really worth the effort. |
783 | 0 | if (i == 0) { |
784 | 0 | write( |
785 | 0 | QUtil::double_to_string(bbox.llx + static_cast<double>(dx)) + " " + |
786 | 0 | QUtil::double_to_string(bbox.lly + static_cast<double>(dy)) + " Td\n"); |
787 | 0 | } else { |
788 | 0 | write("0 " + QUtil::double_to_string(-tfh) + " Td\n"); |
789 | 0 | } |
790 | 0 | write(QPDFObjectHandle::newString(lines.at(i)).unparse() + " Tj\n"); |
791 | 0 | } |
792 | 0 | write("ET\nQ\nEMC"); |
793 | 0 | } |
794 | | |
795 | | namespace |
796 | | { |
797 | | class TfFinder final: public QPDFObjectHandle::TokenFilter |
798 | | { |
799 | | public: |
800 | 0 | TfFinder() = default; |
801 | 0 | ~TfFinder() final = default; |
802 | | |
803 | | void |
804 | | handleToken(QPDFTokenizer::Token const& token) final |
805 | 0 | { |
806 | 0 | auto ttype = token.getType(); |
807 | 0 | auto const& value = token.getValue(); |
808 | 0 | DA.emplace_back(token.getRawValue()); |
809 | 0 | switch (ttype) { |
810 | 0 | case QPDFTokenizer::tt_integer: |
811 | 0 | case QPDFTokenizer::tt_real: |
812 | 0 | last_num = strtod(value.c_str(), nullptr); |
813 | 0 | last_num_idx = QIntC::to_int(DA.size() - 1); |
814 | 0 | break; |
815 | | |
816 | 0 | case QPDFTokenizer::tt_name: |
817 | 0 | last_name = value; |
818 | 0 | break; |
819 | | |
820 | 0 | case QPDFTokenizer::tt_word: |
821 | 0 | if (token.isWord("Tf")) { |
822 | 0 | if ((last_num > 1.0) && (last_num < 1000.0)) { |
823 | | // These ranges are arbitrary but keep us from doing insane things or |
824 | | // suffering from over/underflow |
825 | 0 | tf = last_num; |
826 | 0 | } |
827 | 0 | tf_idx = last_num_idx; |
828 | 0 | font_name = last_name; |
829 | 0 | } |
830 | 0 | break; |
831 | | |
832 | 0 | default: |
833 | 0 | break; |
834 | 0 | } |
835 | 0 | } |
836 | | |
837 | | double |
838 | | getTf() const |
839 | 0 | { |
840 | 0 | return tf; |
841 | 0 | } |
842 | | std::string |
843 | | getFontName() const |
844 | 0 | { |
845 | 0 | return font_name; |
846 | 0 | } |
847 | | |
848 | | std::string |
849 | | getDA() |
850 | 0 | { |
851 | 0 | std::string result; |
852 | 0 | int i = -1; |
853 | 0 | for (auto const& cur: DA) { |
854 | 0 | if (++i == tf_idx) { |
855 | 0 | double delta = strtod(cur.c_str(), nullptr) - tf; |
856 | 0 | if (delta > 0.001 || delta < -0.001) { |
857 | | // tf doesn't match the font size passed to Tf, so substitute. |
858 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper fallback Tf"); |
859 | 0 | result += QUtil::double_to_string(tf); |
860 | 0 | continue; |
861 | 0 | } |
862 | 0 | } |
863 | 0 | result += cur; |
864 | 0 | } |
865 | 0 | return result; |
866 | 0 | } |
867 | | |
868 | | private: |
869 | | double tf{11.0}; |
870 | | int tf_idx{-1}; |
871 | | std::string font_name; |
872 | | double last_num{0.0}; |
873 | | int last_num_idx{-1}; |
874 | | std::string last_name; |
875 | | std::vector<std::string> DA; |
876 | | }; |
877 | | } // namespace |
878 | | |
879 | | void |
880 | | FormNode::generateTextAppearance(QPDFAnnotationObjectHelper& aoh) |
881 | 0 | { |
882 | 0 | no_ci_warn_if( |
883 | 0 | !Dictionary(aoh), // There is no guarantee that aoh is a dictionary |
884 | 0 | "cannot generate appearance for non-dictionary annotation" // |
885 | 0 | ); |
886 | 0 | Stream AS = aoh.getAppearanceStream("/N"); // getAppearanceStream returns a stream or null. |
887 | 0 | if (!AS) { |
888 | 0 | QPDFObjectHandle::Rectangle rect = aoh.getRect(); // may silently be invalid / all zeros |
889 | 0 | QPDFObjectHandle::Rectangle bbox(0, 0, rect.urx - rect.llx, rect.ury - rect.lly); |
890 | 0 | auto* pdf = qpdf(); |
891 | 0 | no_ci_stop_damaged_if(!pdf, "unable to get owning QPDF for appearance generation"); |
892 | 0 | AS = pdf->newStream("/Tx BMC\nEMC\n"); |
893 | 0 | AS.replaceDict(Dictionary( |
894 | 0 | {{"/BBox", QPDFObjectHandle::newFromRectangle(bbox)}, |
895 | 0 | {"/Resources", Dictionary({{"/ProcSet", Array({Name("/PDF"), Name("/Text")})}})}, |
896 | 0 | {"/Type", Name("/XObject")}, |
897 | 0 | {"/Subtype", Name("/Form")}})); |
898 | 0 | if (auto ap = AP()) { |
899 | 0 | ap.replace("/N", AS); |
900 | 0 | } else { |
901 | 0 | aoh.replace("/AP", Dictionary({{"/N", AS}})); |
902 | 0 | } |
903 | 0 | } |
904 | |
|
905 | 0 | if (AS.obj_sp().use_count() > 3) { |
906 | | // Ensures that the appearance stream is not shared by copying it if the threshold of 3 is |
907 | | // exceeded. The threshold is based on the current implementation details: |
908 | | // - One reference from the local variable AS |
909 | | // - One reference from the appearance dictionary (/AP) |
910 | | // - One reference from the object table |
911 | | // If use_count() is greater than 3, it means the appearance stream is shared elsewhere, |
912 | | // and updating it could have unintended side effects. This threshold may need to be updated |
913 | | // if the internal reference counting changes in the future. |
914 | | // |
915 | | // There is currently no explicit CI test for this code. It has been manually tested by |
916 | | // running it through CI with a threshold of 0, unconditionally copying streams. |
917 | 0 | auto data = AS.getStreamData(qpdf_dl_all); |
918 | 0 | AS = AS.copy(); |
919 | 0 | AS.replaceStreamData(std::move(data), Null::temp(), Null::temp()); |
920 | 0 | if (Dictionary AP = aoh.getAppearanceDictionary()) { |
921 | 0 | AP.replace("/N", AS); |
922 | 0 | } else { |
923 | 0 | aoh.replace("/AP", Dictionary({{"/N", AS}})); |
924 | | // aoh is a dictionary, so insertion will succeed. No need to check by retrieving it. |
925 | 0 | } |
926 | 0 | } |
927 | 0 | QPDFObjectHandle bbox_obj = AS.getDict()["/BBox"]; |
928 | 0 | if (!bbox_obj.isRectangle()) { |
929 | 0 | aoh.warn("unable to get appearance stream bounding box"); |
930 | 0 | return; |
931 | 0 | } |
932 | 0 | QPDFObjectHandle::Rectangle bbox = bbox_obj.getArrayAsRectangle(); |
933 | 0 | std::string DA = default_appearance(); |
934 | 0 | std::string V = value(); |
935 | |
|
936 | 0 | TfFinder tff; |
937 | 0 | Pl_QPDFTokenizer tok("tf", &tff); |
938 | 0 | tok.writeString(DA); |
939 | 0 | tok.finish(); |
940 | 0 | double tf = tff.getTf(); |
941 | 0 | DA = tff.getDA(); |
942 | |
|
943 | 0 | std::string (*encoder)(std::string const&, char) = &QUtil::utf8_to_ascii; |
944 | 0 | std::string font_name = tff.getFontName(); |
945 | 0 | if (!font_name.empty()) { |
946 | | // See if the font is encoded with something we know about. |
947 | 0 | Dictionary resources = AS.getDict()["/Resources"]; |
948 | 0 | Dictionary font = resources["/Font"][font_name]; |
949 | 0 | if (!font) { |
950 | 0 | font = getDefaultResources()["/Font"][font_name]; |
951 | 0 | if (resources) { |
952 | 0 | if (resources.indirect()) { |
953 | 0 | resources = resources.qpdf()->makeIndirectObject(resources.copy()); |
954 | 0 | AS.getDict().replace("/Resources", resources); |
955 | 0 | } |
956 | | // Use mergeResources to force /Font to be local |
957 | 0 | QPDFObjectHandle res = resources; |
958 | 0 | res.mergeResources(Dictionary({{"/Font", Dictionary::empty()}})); |
959 | 0 | res.getKey("/Font").replace(font_name, font); |
960 | 0 | } |
961 | 0 | } |
962 | |
|
963 | 0 | if (Name Encoding = font["/Encoding"]) { |
964 | 0 | if (Encoding == "/WinAnsiEncoding") { |
965 | 0 | encoder = &QUtil::utf8_to_win_ansi; |
966 | 0 | } else if (Encoding == "/MacRomanEncoding") { |
967 | 0 | encoder = &QUtil::utf8_to_mac_roman; |
968 | 0 | } |
969 | 0 | } |
970 | 0 | } |
971 | |
|
972 | 0 | V = (*encoder)(V, '?'); |
973 | |
|
974 | 0 | std::vector<std::string> opt; |
975 | 0 | if (isChoice() && (getFlags() & ff_ch_combo) == 0) { |
976 | 0 | opt = getChoices(); |
977 | 0 | for (auto& o: opt) { |
978 | 0 | o = (*encoder)(o, '?'); |
979 | 0 | } |
980 | 0 | } |
981 | |
|
982 | 0 | std::string result; |
983 | 0 | pl::String pl(result); |
984 | 0 | ValueSetter vs(DA, V, opt, tf, bbox); |
985 | 0 | Pl_QPDFTokenizer vs_tok("", &vs, &pl); |
986 | 0 | vs_tok.writeString(AS.getStreamData(qpdf_dl_all)); |
987 | 0 | vs_tok.finish(); |
988 | 0 | AS.replaceStreamData(std::move(result), Null::temp(), Null::temp()); |
989 | 0 | } |