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