/src/qpdf/libqpdf/QPDFFormFieldObjectHelper.cc
Line | Count | Source |
1 | | #include <qpdf/QPDFFormFieldObjectHelper.hh> |
2 | | |
3 | | #include <qpdf/Pl_QPDFTokenizer.hh> |
4 | | #include <qpdf/QIntC.hh> |
5 | | #include <qpdf/QPDFAcroFormDocumentHelper.hh> |
6 | | #include <qpdf/QPDFAnnotationObjectHelper.hh> |
7 | | #include <qpdf/QPDFObjectHandle_private.hh> |
8 | | #include <qpdf/QPDF_private.hh> |
9 | | #include <qpdf/QTC.hh> |
10 | | #include <qpdf/QUtil.hh> |
11 | | #include <cstdlib> |
12 | | |
13 | | using namespace qpdf; |
14 | | |
15 | | QPDFFormFieldObjectHelper::QPDFFormFieldObjectHelper(QPDFObjectHandle oh) : |
16 | 31.4k | QPDFObjectHelper(oh), |
17 | 31.4k | m(new Members()) |
18 | 31.4k | { |
19 | 31.4k | } |
20 | | |
21 | | QPDFFormFieldObjectHelper::QPDFFormFieldObjectHelper() : |
22 | 18.7k | QPDFObjectHelper(QPDFObjectHandle::newNull()), |
23 | 18.7k | m(new Members()) |
24 | 18.7k | { |
25 | 18.7k | } |
26 | | |
27 | | bool |
28 | | QPDFFormFieldObjectHelper::isNull() |
29 | 0 | { |
30 | 0 | return oh().null(); |
31 | 0 | } |
32 | | |
33 | | QPDFFormFieldObjectHelper |
34 | | QPDFFormFieldObjectHelper::getParent() |
35 | 0 | { |
36 | 0 | return oh().getKey("/Parent"); // may be null |
37 | 0 | } |
38 | | |
39 | | QPDFFormFieldObjectHelper |
40 | | QPDFFormFieldObjectHelper::getTopLevelField(bool* is_different) |
41 | 0 | { |
42 | 0 | auto top_field = oh(); |
43 | 0 | QPDFObjGen::set seen; |
44 | 0 | while (seen.add(top_field) && !top_field.getKeyIfDict("/Parent").null()) { |
45 | 0 | top_field = top_field.getKey("/Parent"); |
46 | 0 | if (is_different) { |
47 | 0 | *is_different = true; |
48 | 0 | } |
49 | 0 | } |
50 | 0 | return {top_field}; |
51 | 0 | } |
52 | | |
53 | | QPDFObjectHandle |
54 | | QPDFFormFieldObjectHelper::getFieldFromAcroForm(std::string const& name) |
55 | 13.1k | { |
56 | 13.1k | QPDFObjectHandle result = QPDFObjectHandle::newNull(); |
57 | | // Fields are supposed to be indirect, so this should work. |
58 | 13.1k | QPDF* q = oh().getOwningQPDF(); |
59 | 13.1k | if (!q) { |
60 | 2.63k | return result; |
61 | 2.63k | } |
62 | 10.5k | auto acroform = q->getRoot().getKey("/AcroForm"); |
63 | 10.5k | if (!acroform.isDictionary()) { |
64 | 0 | return result; |
65 | 0 | } |
66 | 10.5k | return acroform.getKey(name); |
67 | 10.5k | } |
68 | | |
69 | | QPDFObjectHandle |
70 | | QPDFFormFieldObjectHelper::getInheritableFieldValue(std::string const& name) |
71 | 43.8k | { |
72 | 43.8k | QPDFObjectHandle node = oh(); |
73 | 43.8k | if (!node.isDictionary()) { |
74 | 316 | return QPDFObjectHandle::newNull(); |
75 | 316 | } |
76 | 43.5k | QPDFObjectHandle result(node.getKey(name)); |
77 | 43.5k | if (result.null()) { |
78 | 11.3k | QPDFObjGen::set seen; |
79 | 13.7k | while (seen.add(node) && node.hasKey("/Parent")) { |
80 | 4.71k | node = node.getKey("/Parent"); |
81 | 4.71k | result = node.getKey(name); |
82 | 4.71k | if (!result.null()) { |
83 | 2.30k | return result; |
84 | 2.30k | } |
85 | 4.71k | } |
86 | 11.3k | } |
87 | 41.2k | return result; |
88 | 43.5k | } |
89 | | |
90 | | std::string |
91 | | QPDFFormFieldObjectHelper::getInheritableFieldValueAsString(std::string const& name) |
92 | 5.47k | { |
93 | 5.47k | auto fv = getInheritableFieldValue(name); |
94 | 5.47k | if (fv.isString()) { |
95 | 1.27k | return fv.getUTF8Value(); |
96 | 1.27k | } |
97 | 4.20k | return {}; |
98 | 5.47k | } |
99 | | |
100 | | std::string |
101 | | QPDFFormFieldObjectHelper::getInheritableFieldValueAsName(std::string const& name) |
102 | 25.4k | { |
103 | 25.4k | if (Name fv = getInheritableFieldValue(name)) { |
104 | 24.6k | return fv; |
105 | 24.6k | } |
106 | 754 | return {}; |
107 | 25.4k | } |
108 | | |
109 | | std::string |
110 | | QPDFFormFieldObjectHelper::getFieldType() |
111 | 25.4k | { |
112 | 25.4k | return getInheritableFieldValueAsName("/FT"); |
113 | 25.4k | } |
114 | | |
115 | | std::string |
116 | | QPDFFormFieldObjectHelper::getFullyQualifiedName() |
117 | 9.10k | { |
118 | 9.10k | std::string result; |
119 | 9.10k | QPDFObjectHandle node = oh(); |
120 | 9.10k | QPDFObjGen::set seen; |
121 | 28.3k | while (!node.null() && seen.add(node)) { |
122 | 19.2k | if (node.getKey("/T").isString()) { |
123 | 18.4k | if (!result.empty()) { |
124 | 9.41k | result = "." + result; |
125 | 9.41k | } |
126 | 18.4k | result = node.getKey("/T").getUTF8Value() + result; |
127 | 18.4k | } |
128 | 19.2k | node = node.getKey("/Parent"); |
129 | 19.2k | } |
130 | 9.10k | return result; |
131 | 9.10k | } |
132 | | |
133 | | std::string |
134 | | QPDFFormFieldObjectHelper::getPartialName() |
135 | 0 | { |
136 | 0 | std::string result; |
137 | 0 | if (oh().getKey("/T").isString()) { |
138 | 0 | result = oh().getKey("/T").getUTF8Value(); |
139 | 0 | } |
140 | 0 | return result; |
141 | 0 | } |
142 | | |
143 | | std::string |
144 | | QPDFFormFieldObjectHelper::getAlternativeName() |
145 | 0 | { |
146 | 0 | if (oh().getKey("/TU").isString()) { |
147 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper TU present"); |
148 | 0 | return oh().getKey("/TU").getUTF8Value(); |
149 | 0 | } |
150 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper TU absent"); |
151 | 0 | return getFullyQualifiedName(); |
152 | 0 | } |
153 | | |
154 | | std::string |
155 | | QPDFFormFieldObjectHelper::getMappingName() |
156 | 0 | { |
157 | 0 | if (oh().getKey("/TM").isString()) { |
158 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper TM present"); |
159 | 0 | return oh().getKey("/TM").getUTF8Value(); |
160 | 0 | } |
161 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper TM absent"); |
162 | 0 | return getAlternativeName(); |
163 | 0 | } |
164 | | |
165 | | QPDFObjectHandle |
166 | | QPDFFormFieldObjectHelper::getValue() |
167 | 1.14k | { |
168 | 1.14k | return getInheritableFieldValue("/V"); |
169 | 1.14k | } |
170 | | |
171 | | std::string |
172 | | QPDFFormFieldObjectHelper::getValueAsString() |
173 | 5.47k | { |
174 | 5.47k | return getInheritableFieldValueAsString("/V"); |
175 | 5.47k | } |
176 | | |
177 | | QPDFObjectHandle |
178 | | QPDFFormFieldObjectHelper::getDefaultValue() |
179 | 0 | { |
180 | 0 | return getInheritableFieldValue("/DV"); |
181 | 0 | } |
182 | | |
183 | | std::string |
184 | | QPDFFormFieldObjectHelper::getDefaultValueAsString() |
185 | 0 | { |
186 | 0 | return getInheritableFieldValueAsString("/DV"); |
187 | 0 | } |
188 | | |
189 | | QPDFObjectHandle |
190 | | QPDFFormFieldObjectHelper::getDefaultResources() |
191 | 11.6k | { |
192 | 11.6k | return getFieldFromAcroForm("/DR"); |
193 | 11.6k | } |
194 | | |
195 | | std::string |
196 | | QPDFFormFieldObjectHelper::getDefaultAppearance() |
197 | 5.48k | { |
198 | 5.48k | auto value = getInheritableFieldValue("/DA"); |
199 | 5.48k | bool looked_in_acroform = false; |
200 | 5.48k | if (!value.isString()) { |
201 | 1.49k | value = getFieldFromAcroForm("/DA"); |
202 | 1.49k | looked_in_acroform = true; |
203 | 1.49k | } |
204 | 5.48k | if (value.isString()) { |
205 | 3.99k | QTC::TC("qpdf", "QPDFFormFieldObjectHelper DA present", looked_in_acroform ? 0 : 1); |
206 | 3.99k | return value.getUTF8Value(); |
207 | 3.99k | } |
208 | 1.48k | return {}; |
209 | 5.48k | } |
210 | | |
211 | | int |
212 | | QPDFFormFieldObjectHelper::getQuadding() |
213 | 0 | { |
214 | 0 | QPDFObjectHandle fv = getInheritableFieldValue("/Q"); |
215 | 0 | bool looked_in_acroform = false; |
216 | 0 | if (!fv.isInteger()) { |
217 | 0 | fv = getFieldFromAcroForm("/Q"); |
218 | 0 | looked_in_acroform = true; |
219 | 0 | } |
220 | 0 | if (fv.isInteger()) { |
221 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper Q present", looked_in_acroform ? 0 : 1); |
222 | 0 | return QIntC::to_int(fv.getIntValue()); |
223 | 0 | } |
224 | 0 | return 0; |
225 | 0 | } |
226 | | |
227 | | int |
228 | | QPDFFormFieldObjectHelper::getFlags() |
229 | 5.42k | { |
230 | 5.42k | QPDFObjectHandle f = getInheritableFieldValue("/Ff"); |
231 | 5.42k | return f.isInteger() ? f.getIntValueAsInt() : 0; |
232 | 5.42k | } |
233 | | |
234 | | bool |
235 | | QPDFFormFieldObjectHelper::isText() |
236 | 0 | { |
237 | 0 | return getFieldType() == "/Tx"; |
238 | 0 | } |
239 | | |
240 | | bool |
241 | | QPDFFormFieldObjectHelper::isCheckbox() |
242 | 1.75k | { |
243 | 1.75k | return getFieldType() == "/Btn" && (getFlags() & (ff_btn_radio | ff_btn_pushbutton)) == 0; |
244 | 1.75k | } |
245 | | |
246 | | bool |
247 | | QPDFFormFieldObjectHelper::isChecked() |
248 | 0 | { |
249 | 0 | return isCheckbox() && Name(getValue()) != "/Off"; |
250 | 0 | } |
251 | | |
252 | | bool |
253 | | QPDFFormFieldObjectHelper::isRadioButton() |
254 | 2.55k | { |
255 | 2.55k | return getFieldType() == "/Btn" && (getFlags() & ff_btn_radio) == ff_btn_radio; |
256 | 2.55k | } |
257 | | |
258 | | bool |
259 | | QPDFFormFieldObjectHelper::isPushbutton() |
260 | 0 | { |
261 | 0 | return getFieldType() == "/Btn" && (getFlags() & ff_btn_pushbutton) == ff_btn_pushbutton; |
262 | 0 | } |
263 | | |
264 | | bool |
265 | | QPDFFormFieldObjectHelper::isChoice() |
266 | 6.39k | { |
267 | 6.39k | return getFieldType() == "/Ch"; |
268 | 6.39k | } |
269 | | |
270 | | std::vector<std::string> |
271 | | QPDFFormFieldObjectHelper::getChoices() |
272 | 919 | { |
273 | 919 | if (!isChoice()) { |
274 | 0 | return {}; |
275 | 0 | } |
276 | 919 | std::vector<std::string> result; |
277 | 25.7k | for (auto const& item: getInheritableFieldValue("/Opt").as_array()) { |
278 | 25.7k | if (item.isString()) { |
279 | 18.0k | result.emplace_back(item.getUTF8Value()); |
280 | 18.0k | } else if (item.size() == 2) { |
281 | 120 | auto display = item.getArrayItem(1); |
282 | 120 | if (display.isString()) { |
283 | 42 | result.emplace_back(display.getUTF8Value()); |
284 | 42 | } |
285 | 120 | } |
286 | 25.7k | } |
287 | 919 | return result; |
288 | 919 | } |
289 | | |
290 | | void |
291 | | QPDFFormFieldObjectHelper::setFieldAttribute(std::string const& key, QPDFObjectHandle value) |
292 | 745 | { |
293 | 745 | oh().replaceKey(key, value); |
294 | 745 | } |
295 | | |
296 | | void |
297 | | QPDFFormFieldObjectHelper::setFieldAttribute(std::string const& key, std::string const& utf8_value) |
298 | 0 | { |
299 | 0 | oh().replaceKey(key, QPDFObjectHandle::newUnicodeString(utf8_value)); |
300 | 0 | } |
301 | | |
302 | | void |
303 | | QPDFFormFieldObjectHelper::setV(QPDFObjectHandle value, bool need_appearances) |
304 | 1.13k | { |
305 | 1.13k | Name name = value; |
306 | 1.13k | if (getFieldType() == "/Btn") { |
307 | 1.13k | if (isCheckbox()) { |
308 | 591 | if (!name) { |
309 | 212 | warn("ignoring attempt to set a checkbox field to a value whose type is not name"); |
310 | 212 | return; |
311 | 212 | } |
312 | | // Accept any value other than /Off to mean checked. Files have been seen that use |
313 | | // /1 or other values. |
314 | 379 | setCheckBoxValue(name != "/Off"); |
315 | 379 | return; |
316 | 591 | } |
317 | 548 | if (isRadioButton()) { |
318 | 546 | if (!name) { |
319 | 47 | warn( |
320 | 47 | "ignoring attempt to set a radio button field to an object that is not a name"); |
321 | 47 | return; |
322 | 47 | } |
323 | 499 | setRadioButtonValue(name); |
324 | 499 | return; |
325 | 546 | } |
326 | 2 | if (isPushbutton()) { |
327 | 0 | warn("ignoring attempt set the value of a pushbutton field"); |
328 | 0 | } |
329 | 2 | return; |
330 | 548 | } |
331 | 0 | if (value.isString()) { |
332 | 0 | setFieldAttribute("/V", QPDFObjectHandle::newUnicodeString(value.getUTF8Value())); |
333 | 0 | } else { |
334 | 0 | setFieldAttribute("/V", value); |
335 | 0 | } |
336 | 0 | if (need_appearances) { |
337 | 0 | QPDF& qpdf = oh().getQPDF( |
338 | 0 | "QPDFFormFieldObjectHelper::setV called with need_appearances = " |
339 | 0 | "true on an object that is not associated with an owning QPDF"); |
340 | 0 | qpdf.doc().acroform().setNeedAppearances(true); |
341 | 0 | } |
342 | 0 | } |
343 | | |
344 | | void |
345 | | QPDFFormFieldObjectHelper::setV(std::string const& utf8_value, bool need_appearances) |
346 | 0 | { |
347 | 0 | setV(QPDFObjectHandle::newUnicodeString(utf8_value), need_appearances); |
348 | 0 | } |
349 | | |
350 | | void |
351 | | QPDFFormFieldObjectHelper::setRadioButtonValue(QPDFObjectHandle name) |
352 | 788 | { |
353 | | // Set the value of a radio button field. This has the following specific behavior: |
354 | | // * If this is a radio button field that has a parent that is also a radio button field and has |
355 | | // no explicit /V, call itself on the parent |
356 | | // * If this is a radio button field with children, set /V to the given value. Then, for each |
357 | | // child, if the child has the specified value as one of its keys in the /N subdictionary of |
358 | | // its /AP (i.e. its normal appearance stream dictionary), set /AS to name; otherwise, if /Off |
359 | | // is a member, set /AS to /Off. |
360 | | // Note that we never turn on /NeedAppearances when setting a radio button field. |
361 | 788 | QPDFObjectHandle parent = oh().getKey("/Parent"); |
362 | 788 | if (parent.isDictionary() && parent.getKey("/Parent").null()) { |
363 | 348 | QPDFFormFieldObjectHelper ph(parent); |
364 | 348 | if (ph.isRadioButton()) { |
365 | | // This is most likely one of the individual buttons. Try calling on the parent. |
366 | 289 | ph.setRadioButtonValue(name); |
367 | 289 | return; |
368 | 289 | } |
369 | 348 | } |
370 | | |
371 | 499 | QPDFObjectHandle kids = oh().getKey("/Kids"); |
372 | 499 | if (!(isRadioButton() && parent.null() && kids.isArray())) { |
373 | 103 | warn("don't know how to set the value of this field as a radio button"); |
374 | 103 | return; |
375 | 103 | } |
376 | 396 | setFieldAttribute("/V", name); |
377 | 1.24k | for (auto const& kid: kids.as_array()) { |
378 | 1.24k | QPDFObjectHandle AP = kid.getKey("/AP"); |
379 | 1.24k | QPDFObjectHandle annot; |
380 | 1.24k | if (AP.null()) { |
381 | | // The widget may be below. If there is more than one, just find the first one. |
382 | 788 | for (auto const& grandkid: kid.getKey("/Kids").as_array()) { |
383 | 788 | AP = grandkid.getKey("/AP"); |
384 | 788 | if (!AP.null()) { |
385 | 35 | annot = grandkid; |
386 | 35 | break; |
387 | 35 | } |
388 | 788 | } |
389 | 724 | } else { |
390 | 724 | annot = kid; |
391 | 724 | } |
392 | 1.24k | if (!annot) { |
393 | 476 | warn("unable to set the value of this radio button"); |
394 | 476 | continue; |
395 | 476 | } |
396 | 773 | if (AP.isDictionary() && AP.getKey("/N").isDictionary() && |
397 | 659 | AP.getKey("/N").hasKey(name.getName())) { |
398 | 105 | annot.replaceKey("/AS", name); |
399 | 668 | } else { |
400 | 668 | annot.replaceKey("/AS", QPDFObjectHandle::newName("/Off")); |
401 | 668 | } |
402 | 773 | } |
403 | 396 | } |
404 | | |
405 | | void |
406 | | QPDFFormFieldObjectHelper::setCheckBoxValue(bool value) |
407 | 379 | { |
408 | 379 | QPDFObjectHandle AP = oh().getKey("/AP"); |
409 | 379 | QPDFObjectHandle annot; |
410 | 379 | if (AP.null()) { |
411 | | // The widget may be below. If there is more than one, just |
412 | | // find the first one. |
413 | 100 | QPDFObjectHandle kids = oh().getKey("/Kids"); |
414 | 245 | for (auto const& kid: oh().getKey("/Kids").as_array(qpdf::strict)) { |
415 | 245 | AP = kid.getKey("/AP"); |
416 | 245 | if (!AP.null()) { |
417 | 31 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper checkbox kid widget"); |
418 | 31 | annot = kid; |
419 | 31 | break; |
420 | 31 | } |
421 | 245 | } |
422 | 279 | } else { |
423 | 279 | annot = oh(); |
424 | 279 | } |
425 | 379 | std::string on_value; |
426 | 379 | if (value) { |
427 | | // Set the "on" value to the first value in the appearance stream's normal state dictionary |
428 | | // that isn't /Off. If not found, fall back to /Yes. |
429 | 246 | if (AP.isDictionary()) { |
430 | 307 | for (auto const& item: AP.getKey("/N").as_dictionary()) { |
431 | 307 | if (item.first != "/Off") { |
432 | 170 | on_value = item.first; |
433 | 170 | break; |
434 | 170 | } |
435 | 307 | } |
436 | 212 | } |
437 | 246 | if (on_value.empty()) { |
438 | 76 | on_value = "/Yes"; |
439 | 76 | } |
440 | 246 | } |
441 | | |
442 | | // Set /AS to the on value or /Off in addition to setting /V. |
443 | 379 | QPDFObjectHandle name = QPDFObjectHandle::newName(value ? on_value : "/Off"); |
444 | 379 | setFieldAttribute("/V", name); |
445 | 379 | if (!annot) { |
446 | 43 | QTC::TC("qpdf", "QPDFObjectHandle broken checkbox"); |
447 | 43 | warn("unable to set the value of this checkbox"); |
448 | 43 | return; |
449 | 43 | } |
450 | 336 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper set checkbox AS"); |
451 | 336 | annot.replaceKey("/AS", name); |
452 | 336 | } |
453 | | |
454 | | void |
455 | | QPDFFormFieldObjectHelper::generateAppearance(QPDFAnnotationObjectHelper& aoh) |
456 | 6.19k | { |
457 | 6.19k | std::string ft = getFieldType(); |
458 | | // Ignore field types we don't know how to generate appearances for. Button fields don't really |
459 | | // need them -- see code in QPDFAcroFormDocumentHelper::generateAppearancesIfNeeded. |
460 | 6.19k | if ((ft == "/Tx") || (ft == "/Ch")) { |
461 | 5.69k | generateTextAppearance(aoh); |
462 | 5.69k | } |
463 | 6.19k | } |
464 | | |
465 | | namespace |
466 | | { |
467 | | class ValueSetter: public QPDFObjectHandle::TokenFilter |
468 | | { |
469 | | public: |
470 | | ValueSetter( |
471 | | std::string const& DA, |
472 | | std::string const& V, |
473 | | std::vector<std::string> const& opt, |
474 | | double tf, |
475 | | QPDFObjectHandle::Rectangle const& bbox); |
476 | 5.46k | ~ValueSetter() override = default; |
477 | | void handleToken(QPDFTokenizer::Token const&) override; |
478 | | void handleEOF() override; |
479 | | void writeAppearance(); |
480 | | |
481 | | private: |
482 | | std::string DA; |
483 | | std::string V; |
484 | | std::vector<std::string> opt; |
485 | | double tf; |
486 | | QPDFObjectHandle::Rectangle bbox; |
487 | | enum { st_top, st_bmc, st_emc, st_end } state{st_top}; |
488 | | bool replaced{false}; |
489 | | }; |
490 | | } // namespace |
491 | | |
492 | | ValueSetter::ValueSetter( |
493 | | std::string const& DA, |
494 | | std::string const& V, |
495 | | std::vector<std::string> const& opt, |
496 | | double tf, |
497 | | QPDFObjectHandle::Rectangle const& bbox) : |
498 | 5.46k | DA(DA), |
499 | 5.46k | V(V), |
500 | 5.46k | opt(opt), |
501 | 5.46k | tf(tf), |
502 | 5.46k | bbox(bbox) |
503 | 5.46k | { |
504 | 5.46k | } |
505 | | |
506 | | void |
507 | | ValueSetter::handleToken(QPDFTokenizer::Token const& token) |
508 | 0 | { |
509 | 0 | QPDFTokenizer::token_type_e ttype = token.getType(); |
510 | 0 | std::string value = token.getValue(); |
511 | 0 | bool do_replace = false; |
512 | 0 | switch (state) { |
513 | 0 | case st_top: |
514 | 0 | writeToken(token); |
515 | 0 | if (token.isWord("BMC")) { |
516 | 0 | state = st_bmc; |
517 | 0 | } |
518 | 0 | break; |
519 | | |
520 | 0 | case st_bmc: |
521 | 0 | if ((ttype == QPDFTokenizer::tt_space) || (ttype == QPDFTokenizer::tt_comment)) { |
522 | 0 | writeToken(token); |
523 | 0 | } else { |
524 | 0 | state = st_emc; |
525 | 0 | } |
526 | | // fall through to emc |
527 | |
|
528 | 0 | case st_emc: |
529 | 0 | if (token.isWord("EMC")) { |
530 | 0 | do_replace = true; |
531 | 0 | state = st_end; |
532 | 0 | } |
533 | 0 | break; |
534 | | |
535 | 0 | case st_end: |
536 | 0 | writeToken(token); |
537 | 0 | break; |
538 | 0 | } |
539 | 0 | if (do_replace) { |
540 | 0 | writeAppearance(); |
541 | 0 | } |
542 | 0 | } |
543 | | |
544 | | void |
545 | | ValueSetter::handleEOF() |
546 | 0 | { |
547 | 0 | if (!replaced) { |
548 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper replaced BMC at EOF"); |
549 | 0 | write("/Tx BMC\n"); |
550 | 0 | writeAppearance(); |
551 | 0 | } |
552 | 0 | } |
553 | | |
554 | | void |
555 | | ValueSetter::writeAppearance() |
556 | 0 | { |
557 | 0 | replaced = true; |
558 | | |
559 | | // This code does not take quadding into consideration because doing so requires font metric |
560 | | // information, which we don't have in many cases. |
561 | |
|
562 | 0 | double tfh = 1.2 * tf; |
563 | 0 | int dx = 1; |
564 | | |
565 | | // Write one or more lines, centered vertically, possibly with one row highlighted. |
566 | |
|
567 | 0 | auto max_rows = static_cast<size_t>((bbox.ury - bbox.lly) / tfh); |
568 | 0 | bool highlight = false; |
569 | 0 | size_t highlight_idx = 0; |
570 | |
|
571 | 0 | std::vector<std::string> lines; |
572 | 0 | if (opt.empty() || (max_rows < 2)) { |
573 | 0 | lines.push_back(V); |
574 | 0 | } else { |
575 | | // Figure out what rows to write |
576 | 0 | size_t nopt = opt.size(); |
577 | 0 | size_t found_idx = 0; |
578 | 0 | bool found = false; |
579 | 0 | for (found_idx = 0; found_idx < nopt; ++found_idx) { |
580 | 0 | if (opt.at(found_idx) == V) { |
581 | 0 | found = true; |
582 | 0 | break; |
583 | 0 | } |
584 | 0 | } |
585 | 0 | if (found) { |
586 | | // Try to make the found item the second one, but adjust for under/overflow. |
587 | 0 | int wanted_first = QIntC::to_int(found_idx) - 1; |
588 | 0 | int wanted_last = QIntC::to_int(found_idx + max_rows) - 2; |
589 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper list found"); |
590 | 0 | if (wanted_first < 0) { |
591 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper list first too low"); |
592 | 0 | wanted_last -= wanted_first; |
593 | 0 | wanted_first = 0; |
594 | 0 | } |
595 | 0 | if (wanted_last >= QIntC::to_int(nopt)) { |
596 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper list last too high"); |
597 | 0 | auto diff = wanted_last - QIntC::to_int(nopt) + 1; |
598 | 0 | wanted_first = std::max(0, wanted_first - diff); |
599 | 0 | wanted_last -= diff; |
600 | 0 | } |
601 | 0 | highlight = true; |
602 | 0 | highlight_idx = found_idx - QIntC::to_size(wanted_first); |
603 | 0 | for (size_t i = QIntC::to_size(wanted_first); i <= QIntC::to_size(wanted_last); ++i) { |
604 | 0 | lines.push_back(opt.at(i)); |
605 | 0 | } |
606 | 0 | } else { |
607 | 0 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper list not found"); |
608 | | // include our value and the first n-1 rows |
609 | 0 | highlight_idx = 0; |
610 | 0 | highlight = true; |
611 | 0 | lines.push_back(V); |
612 | 0 | for (size_t i = 0; ((i < nopt) && (i < (max_rows - 1))); ++i) { |
613 | 0 | lines.push_back(opt.at(i)); |
614 | 0 | } |
615 | 0 | } |
616 | 0 | } |
617 | | |
618 | | // Write the lines centered vertically, highlighting if needed |
619 | 0 | size_t nlines = lines.size(); |
620 | 0 | double dy = bbox.ury - ((bbox.ury - bbox.lly - (static_cast<double>(nlines) * tfh)) / 2.0); |
621 | 0 | if (highlight) { |
622 | 0 | write( |
623 | 0 | "q\n0.85 0.85 0.85 rg\n" + QUtil::double_to_string(bbox.llx) + " " + |
624 | 0 | QUtil::double_to_string( |
625 | 0 | bbox.lly + dy - (tfh * (static_cast<double>(highlight_idx + 1)))) + |
626 | 0 | " " + QUtil::double_to_string(bbox.urx - bbox.llx) + " " + |
627 | 0 | QUtil::double_to_string(tfh) + " re f\nQ\n"); |
628 | 0 | } |
629 | 0 | dy -= tf; |
630 | 0 | write("q\nBT\n" + DA + "\n"); |
631 | 0 | for (size_t i = 0; i < nlines; ++i) { |
632 | | // We could adjust Tm to translate to the beginning the first line, set TL to tfh, and use |
633 | | // T* for each subsequent line, but doing this would require extracting any Tm from DA, |
634 | | // which doesn't seem really worth the effort. |
635 | 0 | if (i == 0) { |
636 | 0 | write( |
637 | 0 | QUtil::double_to_string(bbox.llx + static_cast<double>(dx)) + " " + |
638 | 0 | QUtil::double_to_string(bbox.lly + static_cast<double>(dy)) + " Td\n"); |
639 | 0 | } else { |
640 | 0 | write("0 " + QUtil::double_to_string(-tfh) + " Td\n"); |
641 | 0 | } |
642 | 0 | write(QPDFObjectHandle::newString(lines.at(i)).unparse() + " Tj\n"); |
643 | 0 | } |
644 | 0 | write("ET\nQ\nEMC"); |
645 | 0 | } |
646 | | |
647 | | namespace |
648 | | { |
649 | | class TfFinder final: public QPDFObjectHandle::TokenFilter |
650 | | { |
651 | | public: |
652 | 5.47k | TfFinder() = default; |
653 | 5.47k | ~TfFinder() final = default; |
654 | | |
655 | | void |
656 | | handleToken(QPDFTokenizer::Token const& token) final |
657 | 1.36M | { |
658 | 1.36M | auto ttype = token.getType(); |
659 | 1.36M | auto const& value = token.getValue(); |
660 | 1.36M | DA.emplace_back(token.getRawValue()); |
661 | 1.36M | switch (ttype) { |
662 | 67.9k | case QPDFTokenizer::tt_integer: |
663 | 80.9k | case QPDFTokenizer::tt_real: |
664 | 80.9k | last_num = strtod(value.c_str(), nullptr); |
665 | 80.9k | last_num_idx = QIntC::to_int(DA.size() - 1); |
666 | 80.9k | break; |
667 | | |
668 | 987k | case QPDFTokenizer::tt_name: |
669 | 987k | last_name = value; |
670 | 987k | break; |
671 | | |
672 | 68.2k | case QPDFTokenizer::tt_word: |
673 | 68.2k | if (token.isWord("Tf")) { |
674 | 4.63k | if ((last_num > 1.0) && (last_num < 1000.0)) { |
675 | | // These ranges are arbitrary but keep us from doing insane things or |
676 | | // suffering from over/underflow |
677 | 3.87k | tf = last_num; |
678 | 3.87k | } |
679 | 4.63k | tf_idx = last_num_idx; |
680 | 4.63k | font_name = last_name; |
681 | 4.63k | } |
682 | 68.2k | break; |
683 | | |
684 | 227k | default: |
685 | 227k | break; |
686 | 1.36M | } |
687 | 1.36M | } |
688 | | |
689 | | double |
690 | | getTf() const |
691 | 5.47k | { |
692 | 5.47k | return tf; |
693 | 5.47k | } |
694 | | std::string |
695 | | getFontName() const |
696 | 5.47k | { |
697 | 5.47k | return font_name; |
698 | 5.47k | } |
699 | | |
700 | | std::string |
701 | | getDA() |
702 | 5.47k | { |
703 | 5.47k | std::string result; |
704 | 5.47k | int i = -1; |
705 | 1.36M | for (auto const& cur: DA) { |
706 | 1.36M | if (++i == tf_idx) { |
707 | 3.60k | double delta = strtod(cur.c_str(), nullptr) - tf; |
708 | 3.60k | if (delta > 0.001 || delta < -0.001) { |
709 | | // tf doesn't match the font size passed to Tf, so substitute. |
710 | 349 | QTC::TC("qpdf", "QPDFFormFieldObjectHelper fallback Tf"); |
711 | 349 | result += QUtil::double_to_string(tf); |
712 | 349 | continue; |
713 | 349 | } |
714 | 3.60k | } |
715 | 1.36M | result += cur; |
716 | 1.36M | } |
717 | 5.47k | return result; |
718 | 5.47k | } |
719 | | |
720 | | private: |
721 | | double tf{11.0}; |
722 | | int tf_idx{-1}; |
723 | | std::string font_name; |
724 | | double last_num{0.0}; |
725 | | int last_num_idx{-1}; |
726 | | std::string last_name; |
727 | | std::vector<std::string> DA; |
728 | | }; |
729 | | } // namespace |
730 | | |
731 | | QPDFObjectHandle |
732 | | QPDFFormFieldObjectHelper::getFontFromResource(QPDFObjectHandle resources, std::string const& name) |
733 | 7.13k | { |
734 | 7.13k | QPDFObjectHandle result; |
735 | 7.13k | if (resources.isDictionary() && resources.getKey("/Font").isDictionary() && |
736 | 1.86k | resources.getKey("/Font").hasKey(name)) { |
737 | 843 | result = resources.getKey("/Font").getKey(name); |
738 | 843 | } |
739 | 7.13k | return result; |
740 | 7.13k | } |
741 | | |
742 | | void |
743 | | QPDFFormFieldObjectHelper::generateTextAppearance(QPDFAnnotationObjectHelper& aoh) |
744 | 5.69k | { |
745 | 5.69k | QPDFObjectHandle AS = aoh.getAppearanceStream("/N"); |
746 | 5.69k | if (AS.null()) { |
747 | 3.76k | QPDFObjectHandle::Rectangle rect = aoh.getRect(); |
748 | 3.76k | QPDFObjectHandle::Rectangle bbox(0, 0, rect.urx - rect.llx, rect.ury - rect.lly); |
749 | 3.76k | auto dict = Dictionary( |
750 | 3.76k | {{"/BBox", QPDFObjectHandle::newFromRectangle(bbox)}, |
751 | 3.76k | {"/Resources", Dictionary({{"/ProcSet", Array({Name("/PDF"), Name("/Text")})}})}, |
752 | 3.76k | {"/Type", Name("/XObject")}, |
753 | 3.76k | {"/Subtype", Name("/Form")}}); |
754 | 3.76k | AS = QPDFObjectHandle::newStream(oh().getOwningQPDF(), "/Tx BMC\nEMC\n"); |
755 | 3.76k | AS.replaceDict(dict); |
756 | 3.76k | Dictionary AP = aoh.getAppearanceDictionary(); |
757 | 3.76k | if (!AP) { |
758 | 2.50k | aoh.getObjectHandle().replaceKey("/AP", Dictionary::empty()); |
759 | 2.50k | AP = aoh.getAppearanceDictionary(); |
760 | 2.50k | } |
761 | 3.76k | AP.replaceKey("/N", AS); |
762 | 3.76k | } |
763 | 5.69k | if (!AS.isStream()) { |
764 | 0 | aoh.warn("unable to get normal appearance stream for update"); |
765 | 0 | return; |
766 | 0 | } |
767 | | |
768 | 5.69k | if (AS.obj_sp().use_count() > 3) { |
769 | 67 | aoh.warn("unable to generate text appearance from shared appearance stream for update"); |
770 | 67 | return; |
771 | 67 | } |
772 | 5.62k | QPDFObjectHandle bbox_obj = AS.getDict().getKey("/BBox"); |
773 | 5.62k | if (!bbox_obj.isRectangle()) { |
774 | 137 | aoh.warn("unable to get appearance stream bounding box"); |
775 | 137 | return; |
776 | 137 | } |
777 | 5.49k | QPDFObjectHandle::Rectangle bbox = bbox_obj.getArrayAsRectangle(); |
778 | 5.49k | std::string DA = getDefaultAppearance(); |
779 | 5.49k | std::string V = getValueAsString(); |
780 | 5.49k | std::vector<std::string> opt; |
781 | 5.49k | if (isChoice() && (getFlags() & ff_ch_combo) == 0) { |
782 | 919 | opt = getChoices(); |
783 | 919 | } |
784 | | |
785 | 5.49k | TfFinder tff; |
786 | 5.49k | Pl_QPDFTokenizer tok("tf", &tff); |
787 | 5.49k | tok.writeString(DA); |
788 | 5.49k | tok.finish(); |
789 | 5.49k | double tf = tff.getTf(); |
790 | 5.49k | DA = tff.getDA(); |
791 | | |
792 | 5.49k | std::string (*encoder)(std::string const&, char) = &QUtil::utf8_to_ascii; |
793 | 5.49k | std::string font_name = tff.getFontName(); |
794 | 5.49k | if (!font_name.empty()) { |
795 | | // See if the font is encoded with something we know about. |
796 | 3.60k | Dictionary resources = AS.getDict()["/Resources"]; |
797 | 3.60k | Dictionary font = getFontFromResource(resources, font_name); |
798 | 3.60k | if (!font) { |
799 | 3.53k | font = getFontFromResource(getDefaultResources(), font_name); |
800 | 3.53k | if (resources) { |
801 | 3.11k | if (resources.indirect()) { |
802 | 241 | resources = resources.qpdf()->makeIndirectObject(resources.copy()); |
803 | 241 | AS.getDict().replaceKey("/Resources", resources); |
804 | 241 | } |
805 | | // Use mergeResources to force /Font to be local |
806 | 3.11k | QPDFObjectHandle res = resources; |
807 | 3.11k | res.mergeResources(Dictionary({{"/Font", Dictionary::empty()}})); |
808 | 3.11k | res.getKey("/Font").replaceKey(font_name, font); |
809 | 3.11k | } |
810 | 3.53k | } |
811 | | |
812 | 3.60k | if (Name Encoding = font["/Encoding"]) { |
813 | 636 | if (Encoding == "/WinAnsiEncoding") { |
814 | 113 | encoder = &QUtil::utf8_to_win_ansi; |
815 | 523 | } else if (Encoding == "/MacRomanEncoding") { |
816 | 43 | encoder = &QUtil::utf8_to_mac_roman; |
817 | 43 | } |
818 | 636 | } |
819 | 3.60k | } |
820 | | |
821 | 5.49k | V = (*encoder)(V, '?'); |
822 | 23.5k | for (size_t i = 0; i < opt.size(); ++i) { |
823 | 18.0k | opt.at(i) = (*encoder)(opt.at(i), '?'); |
824 | 18.0k | } |
825 | 5.49k | AS.addTokenFilter( |
826 | 5.49k | std::shared_ptr<QPDFObjectHandle::TokenFilter>(new ValueSetter(DA, V, opt, tf, bbox))); |
827 | 5.49k | } |