/src/xpdf-4.06/xpdf/XFAScanner.cc
Line | Count | Source |
1 | | //======================================================================== |
2 | | // |
3 | | // XFAScanner.cc |
4 | | // |
5 | | // Copyright 2020 Glyph & Cog, LLC |
6 | | // |
7 | | //======================================================================== |
8 | | |
9 | | #include <aconf.h> |
10 | | |
11 | | #include "GString.h" |
12 | | #include "GHash.h" |
13 | | #include "Object.h" |
14 | | #include "Error.h" |
15 | | #include "Zoox.h" |
16 | | #include "XFAScanner.h" |
17 | | |
18 | | //------------------------------------------------------------------------ |
19 | | |
20 | | // fields have two names: |
21 | | // |
22 | | // name: |
23 | | // - nodes with bind=global set the index to 0 ("foo[0]") regardless |
24 | | // of the number of nodes with the same name |
25 | | // - nodes with bind=none are dropped from the name |
26 | | // - <area> nodes are dropped from the name |
27 | | // - used for field value lookup in <xfa:datasets> |
28 | | // |
29 | | // fullName: |
30 | | // - all named nodes are treated the same, regardless of bind=global |
31 | | // or bind=none |
32 | | // - <area> nodes are included in the name, but don't reset the |
33 | | // numbering (i.e., <area> nodes are "transparent" with respect to |
34 | | // node numbering) |
35 | | // - used for field value lookup in <form> |
36 | | // - used for matching with AcroForm names |
37 | | // |
38 | | // Both names use indexes on all nodes, even if there's only one node |
39 | | // with the name -- this isn't correct for XFA naming, but it matches |
40 | | // the AcroForm behavior. |
41 | | |
42 | | //------------------------------------------------------------------------ |
43 | | |
44 | | XFAFieldLayoutInfo::XFAFieldLayoutInfo(XFAFieldLayoutHAlign hAlignA, |
45 | | XFAFieldLayoutVAlign vAlignA, |
46 | | double marginLeftA, |
47 | 1.32k | double marginRightA) { |
48 | 1.32k | hAlign = hAlignA; |
49 | 1.32k | vAlign = vAlignA; |
50 | 1.32k | marginLeft = marginLeftA; |
51 | 1.32k | marginRight = marginRightA; |
52 | 1.32k | } |
53 | | |
54 | | //------------------------------------------------------------------------ |
55 | | |
56 | | XFAFieldPictureInfo::XFAFieldPictureInfo(XFAFieldPictureSubtype subtypeA, |
57 | 0 | GString *formatA) { |
58 | 0 | subtype = subtypeA; |
59 | 0 | format = formatA; |
60 | 0 | } |
61 | | |
62 | 0 | XFAFieldPictureInfo::~XFAFieldPictureInfo() { |
63 | 0 | delete format; |
64 | 0 | } |
65 | | |
66 | | //------------------------------------------------------------------------ |
67 | | |
68 | | XFAFieldBarcodeInfo::XFAFieldBarcodeInfo(GString *barcodeTypeA, |
69 | | double wideNarrowRatioA, |
70 | | double moduleWidthA, |
71 | | double moduleHeightA, |
72 | | int dataLengthA, |
73 | | int errorCorrectionLevelA, |
74 | 0 | GString *textLocationA) { |
75 | 0 | barcodeType = barcodeTypeA; |
76 | 0 | wideNarrowRatio = wideNarrowRatioA; |
77 | 0 | moduleWidth = moduleWidthA; |
78 | 0 | moduleHeight = moduleHeightA; |
79 | 0 | dataLength = dataLengthA; |
80 | 0 | errorCorrectionLevel = errorCorrectionLevelA; |
81 | 0 | textLocation = textLocationA; |
82 | 0 | } |
83 | | |
84 | 0 | XFAFieldBarcodeInfo::~XFAFieldBarcodeInfo() { |
85 | 0 | delete barcodeType; |
86 | 0 | delete textLocation; |
87 | 0 | } |
88 | | |
89 | | //------------------------------------------------------------------------ |
90 | | |
91 | | XFAField::XFAField(GString *nameA, GString *fullNameA, GString *valueA, |
92 | | XFAFieldLayoutInfo *layoutInfoA, |
93 | | XFAFieldPictureInfo *pictureInfoA, |
94 | | XFAFieldBarcodeInfo *barcodeInfoA) |
95 | 3.23k | : name(nameA) |
96 | 3.23k | , fullName(fullNameA) |
97 | 3.23k | , value(valueA) |
98 | 3.23k | , layoutInfo(layoutInfoA) |
99 | 3.23k | , pictureInfo(pictureInfoA) |
100 | 3.23k | , barcodeInfo(barcodeInfoA) |
101 | 3.23k | { |
102 | 3.23k | } |
103 | | |
104 | 3.23k | XFAField::~XFAField() { |
105 | 3.23k | delete name; |
106 | 3.23k | delete fullName; |
107 | 3.23k | delete value; |
108 | 3.23k | delete layoutInfo; |
109 | 3.23k | delete pictureInfo; |
110 | 3.23k | delete barcodeInfo; |
111 | 3.23k | } |
112 | | |
113 | | //------------------------------------------------------------------------ |
114 | | |
115 | 595 | XFAScanner *XFAScanner::load(Object *xfaObj) { |
116 | 595 | GString *xfaData = readXFAStreams(xfaObj); |
117 | 595 | if (!xfaData) { |
118 | 42 | return NULL; |
119 | 42 | } |
120 | 553 | ZxDoc *xml = ZxDoc::loadMem(xfaData->getCString(), xfaData->getLength()); |
121 | 553 | delete xfaData; |
122 | 553 | if (!xml) { |
123 | 56 | error(errSyntaxError, -1, "Invalid XML in XFA form"); |
124 | 56 | return NULL; |
125 | 56 | } |
126 | | |
127 | 497 | XFAScanner *scanner = new XFAScanner(); |
128 | | |
129 | 497 | if (xml->getRoot()) { |
130 | 497 | GHash *formValues = scanner->scanFormValues(xml->getRoot()); |
131 | 497 | ZxElement *dataElem = NULL; |
132 | 497 | ZxElement *datasets = |
133 | 497 | xml->getRoot()->findFirstChildElement("xfa:datasets"); |
134 | 497 | if (datasets) { |
135 | 45 | dataElem = datasets->findFirstChildElement("xfa:data"); |
136 | 45 | } |
137 | 497 | ZxElement *tmpl = xml->getRoot()->findFirstChildElement("template"); |
138 | 497 | if (tmpl) { |
139 | 200 | scanner->scanNode(tmpl, NULL, NULL, NULL, NULL, NULL, |
140 | 200 | dataElem, formValues); |
141 | 200 | } |
142 | 497 | deleteGHash(formValues, GString); |
143 | 497 | } |
144 | | |
145 | 497 | delete xml; |
146 | | |
147 | 497 | return scanner; |
148 | 553 | } |
149 | | |
150 | 497 | XFAScanner::XFAScanner() { |
151 | 497 | fields = new GHash(); |
152 | 497 | } |
153 | | |
154 | 497 | XFAScanner::~XFAScanner() { |
155 | 497 | deleteGHash(fields, XFAField); |
156 | 497 | } |
157 | | |
158 | 128 | XFAField *XFAScanner::findField(GString *acroFormFieldName) { |
159 | 128 | return (XFAField *)fields->lookup(acroFormFieldName); |
160 | 128 | } |
161 | | |
162 | 595 | GString *XFAScanner::readXFAStreams(Object *xfaObj) { |
163 | 595 | GString *data = new GString(); |
164 | 595 | char buf[4096]; |
165 | 595 | int n; |
166 | 595 | if (xfaObj->isStream()) { |
167 | 1 | xfaObj->streamReset(); |
168 | 3 | while ((n = xfaObj->getStream()->getBlock(buf, sizeof(buf))) > 0) { |
169 | 2 | data->append(buf, n); |
170 | 2 | } |
171 | 594 | } else if (xfaObj->isArray()) { |
172 | 3.63k | for (int i = 1; i < xfaObj->arrayGetLength(); i += 2) { |
173 | 3.08k | Object obj; |
174 | 3.08k | if (!xfaObj->arrayGet(i, &obj)->isStream()) { |
175 | 42 | error(errSyntaxError, -1, "XFA array element is wrong type"); |
176 | 42 | obj.free(); |
177 | 42 | delete data; |
178 | 42 | return NULL; |
179 | 42 | } |
180 | 3.03k | obj.streamReset(); |
181 | 15.8k | while ((n = obj.getStream()->getBlock(buf, sizeof(buf))) > 0) { |
182 | 12.8k | data->append(buf, n); |
183 | 12.8k | } |
184 | 3.03k | obj.free(); |
185 | 3.03k | } |
186 | 594 | } else { |
187 | 0 | error(errSyntaxError, -1, "XFA object is wrong type"); |
188 | 0 | return NULL; |
189 | 0 | } |
190 | 553 | return data; |
191 | 595 | } |
192 | | |
193 | 497 | GHash *XFAScanner::scanFormValues(ZxElement *xmlRoot) { |
194 | 497 | GHash *formValues = new GHash(gTrue); |
195 | 497 | ZxElement *formElem = xmlRoot->findFirstChildElement("form"); |
196 | 497 | if (formElem) { |
197 | 58 | scanFormNode(formElem, NULL, formValues); |
198 | 58 | } |
199 | 497 | return formValues; |
200 | 497 | } |
201 | | |
202 | | void XFAScanner::scanFormNode(ZxElement *elem, GString *fullName, |
203 | 2.18k | GHash *formValues) { |
204 | 2.18k | GHash *fullNameIdx = new GHash(); |
205 | 2.18k | for (ZxNode *node = elem->getFirstChild(); |
206 | 39.0k | node; |
207 | 36.8k | node = node->getNextChild()) { |
208 | 36.8k | if (node->isElement("value")) { |
209 | 2.13k | if (fullName) { |
210 | 1.63k | ZxNode *child1Node = ((ZxElement *)node)->getFirstChild(); |
211 | 1.63k | if (child1Node && child1Node->isElement()) { |
212 | 1.25k | ZxNode *child2Node = ((ZxElement *)child1Node)->getFirstChild(); |
213 | 1.25k | if (child2Node && child2Node->isCharData()) { |
214 | 365 | formValues->add(fullName->copy(), |
215 | 365 | ((ZxCharData *)child2Node)->getData()->copy()); |
216 | 365 | } |
217 | 1.25k | } |
218 | 1.63k | } |
219 | 34.6k | } else if (node->isElement()) { |
220 | 17.8k | ZxAttr *nameAttr = ((ZxElement *)node)->findAttr("name"); |
221 | 17.8k | if (nameAttr && (node->isElement("subform") || |
222 | 1.66k | node->isElement("field"))) { |
223 | 1.66k | GString *nodeName = nameAttr->getValue(); |
224 | 1.66k | GString *childFullName; |
225 | 1.66k | if (fullName) { |
226 | 1.06k | childFullName = GString::format("{0:t}.{1:t}", fullName, nodeName); |
227 | 1.06k | } else { |
228 | 605 | childFullName = nodeName->copy(); |
229 | 605 | } |
230 | 1.66k | int idx = fullNameIdx->lookupInt(nodeName); |
231 | 1.66k | childFullName->appendf("[{0:d}]", idx); |
232 | 1.66k | fullNameIdx->replace(nodeName, idx + 1); |
233 | 1.66k | scanFormNode((ZxElement *)node, childFullName, formValues); |
234 | 1.66k | delete childFullName; |
235 | 16.2k | } else if (node->isElement("subform")) { |
236 | 462 | scanFormNode((ZxElement *)node, fullName, formValues); |
237 | 462 | } |
238 | 17.8k | } |
239 | 36.8k | } |
240 | 2.18k | delete fullNameIdx; |
241 | 2.18k | } |
242 | | |
243 | | void XFAScanner::scanNode(ZxElement *elem, |
244 | | GString *parentName, GString *parentFullName, |
245 | | GHash *nameIdx, GHash *fullNameIdx, |
246 | | GString *exclGroupName, ZxElement *dataElem, |
247 | 96.6k | GHash *formValues) { |
248 | 96.6k | GString *nodeName = getNodeName(elem); |
249 | 96.6k | GHash *childNameIdx; |
250 | 96.6k | if (!nameIdx || nodeName) { |
251 | 18.8k | childNameIdx = new GHash(); |
252 | 77.7k | } else { |
253 | 77.7k | childNameIdx = nameIdx; |
254 | 77.7k | } |
255 | 96.6k | GString *nodeFullName = getNodeFullName(elem); |
256 | 96.6k | GHash *childFullNameIdx; |
257 | 96.6k | if (!fullNameIdx || (nodeFullName && !elem->isElement("area"))) { |
258 | 18.8k | childFullNameIdx = new GHash(); |
259 | 77.7k | } else { |
260 | 77.7k | childFullNameIdx = fullNameIdx; |
261 | 77.7k | } |
262 | | |
263 | 96.6k | GString *childName; |
264 | 96.6k | if (nodeName) { |
265 | 18.6k | if (parentName) { |
266 | 17.8k | childName = GString::format("{0:t}.{1:t}", parentName, nodeName); |
267 | 17.8k | } else { |
268 | 780 | childName = nodeName->copy(); |
269 | 780 | } |
270 | 18.6k | int idx = nameIdx->lookupInt(nodeName); |
271 | 18.6k | nameIdx->replace(nodeName, idx + 1); |
272 | 18.6k | if (nodeIsBindGlobal(elem)) { |
273 | 0 | childName->appendf("[0]"); |
274 | 18.6k | } else { |
275 | 18.6k | childName->appendf("[{0:d}]", idx); |
276 | 18.6k | } |
277 | 77.9k | } else { |
278 | 77.9k | childName = parentName; |
279 | 77.9k | } |
280 | 96.6k | GString *childFullName; |
281 | 96.6k | if (nodeFullName) { |
282 | 18.6k | if (parentFullName) { |
283 | 17.8k | childFullName = GString::format("{0:t}.{1:t}", |
284 | 17.8k | parentFullName, nodeFullName); |
285 | 17.8k | } else { |
286 | 780 | childFullName = nodeFullName->copy(); |
287 | 780 | } |
288 | 18.6k | int idx = fullNameIdx->lookupInt(nodeFullName); |
289 | 18.6k | fullNameIdx->replace(nodeFullName, idx + 1); |
290 | 18.6k | childFullName->appendf("[{0:d}]", idx); |
291 | 77.9k | } else { |
292 | 77.9k | childFullName = parentFullName; |
293 | 77.9k | } |
294 | | |
295 | 96.6k | if (elem->isElement("field")) { |
296 | 3.53k | if (childName && childFullName) { |
297 | 3.23k | scanField(elem, childName, childFullName, exclGroupName, |
298 | 3.23k | dataElem, formValues); |
299 | 3.23k | } |
300 | 93.0k | } else { |
301 | 93.0k | GString *childExclGroupName; |
302 | 93.0k | if (elem->isElement("exclGroup")) { |
303 | 534 | childExclGroupName = childName; |
304 | 92.5k | } else { |
305 | 92.5k | childExclGroupName = NULL; |
306 | 92.5k | } |
307 | 93.0k | for (ZxNode *child = elem->getFirstChild(); |
308 | 263k | child; |
309 | 170k | child = child->getNextChild()) { |
310 | 170k | if (child->isElement()) { |
311 | 96.4k | scanNode((ZxElement *)child, childName, childFullName, |
312 | 96.4k | childNameIdx, childFullNameIdx, childExclGroupName, |
313 | 96.4k | dataElem, formValues); |
314 | 96.4k | } |
315 | 170k | } |
316 | 93.0k | } |
317 | | |
318 | 96.6k | if (childName != parentName) { |
319 | 18.6k | delete childName; |
320 | 18.6k | } |
321 | 96.6k | if (childFullName != parentFullName) { |
322 | 18.6k | delete childFullName; |
323 | 18.6k | } |
324 | 96.6k | if (childNameIdx != nameIdx) { |
325 | 18.8k | delete childNameIdx; |
326 | 18.8k | } |
327 | 96.6k | if (childFullNameIdx != fullNameIdx) { |
328 | 18.8k | delete childFullNameIdx; |
329 | 18.8k | } |
330 | 96.6k | } |
331 | | |
332 | | void XFAScanner::scanField(ZxElement *elem, GString *name, GString *fullName, |
333 | | GString *exclGroupName, ZxElement *dataElem, |
334 | 3.23k | GHash *formValues) { |
335 | 3.23k | GString *value = getFieldValue(elem, name, fullName, exclGroupName, |
336 | 3.23k | dataElem, formValues); |
337 | 3.23k | XFAFieldLayoutInfo *layoutInfo = getFieldLayoutInfo(elem); |
338 | 3.23k | XFAFieldPictureInfo *pictureInfo = getFieldPictureInfo(elem); |
339 | 3.23k | XFAFieldBarcodeInfo *barcodeInfo = getFieldBarcodeInfo(elem); |
340 | 3.23k | XFAField *field = new XFAField(name->copy(), fullName->copy(), value, |
341 | 3.23k | layoutInfo, pictureInfo, barcodeInfo); |
342 | 3.23k | fields->add(field->fullName, field); |
343 | 3.23k | } |
344 | | |
345 | | GString *XFAScanner::getFieldValue(ZxElement *elem, GString *name, |
346 | | GString *fullName, GString *exclGroupName, |
347 | 3.23k | ZxElement *dataElem, GHash *formValues) { |
348 | 3.23k | GString *val = NULL; |
349 | | |
350 | | //--- check the <xfa:datasets> packet |
351 | 3.23k | val = getDatasetsValue(name->getCString(), dataElem); |
352 | 3.23k | if (!val && exclGroupName) { |
353 | 575 | val = (GString *)getDatasetsValue(exclGroupName->getCString(), dataElem); |
354 | 575 | } |
355 | | |
356 | | //--- check the <form> element |
357 | 3.23k | if (!val) { |
358 | 3.20k | val = (GString *)formValues->lookup(fullName); |
359 | 3.20k | } |
360 | | |
361 | | //--- check the <value> element within the field |
362 | 3.23k | if (!val) { |
363 | 3.20k | ZxElement *valueElem = elem->findFirstChildElement("value"); |
364 | 3.20k | if (valueElem) { |
365 | 1.28k | ZxNode *child1Node = valueElem->getFirstChild(); |
366 | 1.28k | if (child1Node && child1Node->isElement()) { |
367 | 1.17k | ZxNode *child2Node = ((ZxElement *)child1Node)->getFirstChild(); |
368 | 1.17k | if (child2Node && child2Node->isCharData()) { |
369 | 633 | val = ((ZxCharData *)child2Node)->getData(); |
370 | 633 | } |
371 | 1.17k | } |
372 | 1.28k | } |
373 | 3.20k | } |
374 | | |
375 | | //--- get the checkbutton item value |
376 | 3.23k | GString *checkbuttonItem = NULL; |
377 | 3.23k | ZxElement *uiElem = elem->findFirstChildElement("ui"); |
378 | 3.23k | if (uiElem) { |
379 | 1.48k | ZxNode *uiChild = uiElem->getFirstChild(); |
380 | 1.48k | if (uiChild && uiChild->isElement("checkButton")) { |
381 | 1.21k | ZxElement *itemsElem = elem->findFirstChildElement("items"); |
382 | 1.21k | if (itemsElem) { |
383 | 946 | ZxNode *node1 = itemsElem->getFirstChild(); |
384 | 946 | if (node1 && node1->isElement()) { |
385 | 789 | ZxNode *node2 = ((ZxElement *)node1)->getFirstChild(); |
386 | 789 | if (node2 && node2->isCharData()) { |
387 | 459 | checkbuttonItem = ((ZxCharData *)node2)->getData(); |
388 | 459 | } |
389 | 789 | } |
390 | 946 | } |
391 | 1.21k | } |
392 | 1.48k | } |
393 | | // convert XFA checkbutton value to AcroForm-style On/Off value |
394 | 3.23k | if (checkbuttonItem && val) { |
395 | 432 | if (val->cmp(checkbuttonItem)) { |
396 | 309 | val = new GString("Off"); |
397 | 309 | } else { |
398 | 123 | val = new GString("On"); |
399 | 123 | } |
400 | 2.79k | } else if (val) { |
401 | 232 | val = val->copy(); |
402 | 232 | } |
403 | | |
404 | 3.23k | return val; |
405 | 3.23k | } |
406 | | |
407 | 6.56k | GString *XFAScanner::getDatasetsValue(char *partName, ZxElement *elem) { |
408 | 6.56k | if (!elem) { |
409 | 3.02k | return NULL; |
410 | 3.02k | } |
411 | | |
412 | | // partName = xxxx[nn].yyyy---- |
413 | 3.54k | char *p = strchr(partName, '['); |
414 | 3.54k | if (!p) { |
415 | 268 | return NULL; |
416 | 268 | } |
417 | 3.27k | int partLen = (int)(p - partName); |
418 | 3.27k | int idx = atoi(p + 1); |
419 | 3.27k | p = strchr(p + 1, '.'); |
420 | 3.27k | if (p) { |
421 | 2.10k | ++p; |
422 | 2.10k | } |
423 | | |
424 | 3.27k | int curIdx = 0; |
425 | 3.27k | for (ZxNode *node = elem->getFirstChild(); |
426 | 15.3k | node; |
427 | 13.2k | node = node->getNextChild()) { |
428 | 13.2k | if (!node->isElement()) { |
429 | 2.48k | continue; |
430 | 2.48k | } |
431 | 10.7k | GString *nodeName = ((ZxElement *)node)->getType(); |
432 | 10.7k | if (nodeName->getLength() != partLen || |
433 | 9.07k | strncmp(nodeName->getCString(), partName, partLen)) { |
434 | 9.07k | continue; |
435 | 9.07k | } |
436 | 1.64k | if (curIdx != idx) { |
437 | 493 | ++curIdx; |
438 | 493 | continue; |
439 | 493 | } |
440 | 1.14k | if (p) { |
441 | 689 | GString *val = getDatasetsValue(p, (ZxElement *)node); |
442 | 689 | if (val) { |
443 | 31 | return val; |
444 | 31 | } |
445 | 658 | break; |
446 | 689 | } else { |
447 | 458 | ZxNode *child = ((ZxElement *)node)->getFirstChild(); |
448 | 458 | if (!child || !child->isCharData()) { |
449 | 427 | return NULL; |
450 | 427 | } |
451 | 31 | return ((ZxCharData *)child)->getData(); |
452 | 458 | } |
453 | 1.14k | } |
454 | | |
455 | | // search for an 'ancestor match' |
456 | 2.78k | if (p) { |
457 | 2.07k | return getDatasetsValue(p, elem); |
458 | 2.07k | } |
459 | | |
460 | 718 | return NULL; |
461 | 2.78k | } |
462 | | |
463 | 3.23k | XFAFieldLayoutInfo *XFAScanner::getFieldLayoutInfo(ZxElement *elem) { |
464 | 3.23k | ZxElement *paraElem = elem->findFirstChildElement("para"); |
465 | 3.23k | if (!paraElem) { |
466 | 1.90k | return NULL; |
467 | 1.90k | } |
468 | 1.32k | XFAFieldLayoutHAlign hAlign = xfaFieldLayoutHAlignLeft; |
469 | 1.32k | ZxAttr *hAlignAttr = paraElem->findAttr("hAlign"); |
470 | 1.32k | if (hAlignAttr) { |
471 | 13 | if (!hAlignAttr->getValue()->cmp("left")) { |
472 | 0 | hAlign = xfaFieldLayoutHAlignLeft; |
473 | 13 | } else if (!hAlignAttr->getValue()->cmp("center")) { |
474 | 0 | hAlign = xfaFieldLayoutHAlignCenter; |
475 | 13 | } else if (!hAlignAttr->getValue()->cmp("right")) { |
476 | 0 | hAlign = xfaFieldLayoutHAlignRight; |
477 | 0 | } |
478 | 13 | } |
479 | 1.32k | XFAFieldLayoutVAlign vAlign = xfaFieldLayoutVAlignTop; |
480 | 1.32k | ZxAttr *vAlignAttr = paraElem->findAttr("vAlign"); |
481 | 1.32k | if (vAlignAttr) { |
482 | 726 | if (!vAlignAttr->getValue()->cmp("top")) { |
483 | 0 | vAlign = xfaFieldLayoutVAlignTop; |
484 | 726 | } else if (!vAlignAttr->getValue()->cmp("middle")) { |
485 | 102 | vAlign = xfaFieldLayoutVAlignMiddle; |
486 | 624 | } else if (!vAlignAttr->getValue()->cmp("bottom")) { |
487 | 0 | vAlign = xfaFieldLayoutVAlignBottom; |
488 | 0 | } |
489 | 726 | } |
490 | 1.32k | double marginLeft = 0; |
491 | 1.32k | double marginRight = 0; |
492 | 1.32k | ZxAttr *marginAttr; |
493 | 1.32k | if ((marginAttr = paraElem->findAttr("marginLeft"))) { |
494 | 0 | marginLeft = getMeasurement(marginAttr->getValue()); |
495 | 0 | } |
496 | 1.32k | if ((marginAttr = paraElem->findAttr("marginRight"))) { |
497 | 0 | marginRight = getMeasurement(marginAttr->getValue()); |
498 | 0 | } |
499 | 1.32k | return new XFAFieldLayoutInfo(hAlign, vAlign, marginLeft, marginRight); |
500 | 3.23k | } |
501 | | |
502 | 3.23k | XFAFieldPictureInfo *XFAScanner::getFieldPictureInfo(ZxElement *elem) { |
503 | 3.23k | ZxElement *uiElem = elem->findFirstChildElement("ui"); |
504 | 3.23k | if (!uiElem) { |
505 | 1.74k | return NULL; |
506 | 1.74k | } |
507 | 1.48k | XFAFieldPictureSubtype subtype; |
508 | 1.48k | if (uiElem->findFirstChildElement("dateTimeEdit")) { |
509 | 0 | subtype = xfaFieldPictureDateTime; |
510 | 1.48k | } else if (uiElem->findFirstChildElement("numericEdit")) { |
511 | 0 | subtype = xfaFieldPictureNumeric; |
512 | 1.48k | } else if (uiElem->findFirstChildElement("textEdit")) { |
513 | 0 | subtype = xfaFieldPictureText; |
514 | 1.48k | } else { |
515 | 1.48k | return NULL; |
516 | 1.48k | } |
517 | | |
518 | 0 | ZxElement *formatElem, *pictureElem; |
519 | 0 | ZxNode *pictureChildNode; |
520 | 0 | if (!(formatElem = elem->findFirstChildElement("format")) || |
521 | 0 | !(pictureElem = formatElem->findFirstChildElement("picture")) || |
522 | 0 | !(pictureChildNode = pictureElem->getFirstChild()) || |
523 | 0 | !pictureChildNode->isCharData()) { |
524 | 0 | return NULL; |
525 | 0 | } |
526 | 0 | GString *format = ((ZxCharData *)pictureChildNode)->getData()->copy(); |
527 | |
|
528 | 0 | return new XFAFieldPictureInfo(subtype, format); |
529 | 0 | } |
530 | | |
531 | 3.23k | XFAFieldBarcodeInfo *XFAScanner::getFieldBarcodeInfo(ZxElement *elem) { |
532 | 3.23k | ZxElement *uiElem, *barcodeElem; |
533 | 3.23k | if (!(uiElem = elem->findFirstChildElement("ui")) || |
534 | 3.23k | !(barcodeElem = uiElem->findFirstChildElement("barcode"))) { |
535 | 3.23k | return NULL; |
536 | 3.23k | } |
537 | | |
538 | 0 | ZxAttr *attr; |
539 | 0 | if (!(attr = barcodeElem->findAttr("type"))) { |
540 | 0 | return NULL; |
541 | 0 | } |
542 | 0 | GString *barcodeType = attr->getValue()->copy(); |
543 | |
|
544 | 0 | double wideNarrowRatio = 3; |
545 | 0 | if ((attr = barcodeElem->findAttr("wideNarrowRatio"))) { |
546 | 0 | char *s = attr->getValue()->getCString(); |
547 | 0 | char *colon = strchr(s, ':'); |
548 | 0 | if (colon) { |
549 | 0 | GString *numStr = new GString(s, (int)(colon - s)); |
550 | 0 | double num = atof(numStr->getCString()); |
551 | 0 | delete numStr; |
552 | 0 | double den = atof(colon + 1); |
553 | 0 | if (den == 0) { |
554 | 0 | wideNarrowRatio = num; |
555 | 0 | } else { |
556 | 0 | wideNarrowRatio = num / den; |
557 | 0 | } |
558 | 0 | } else { |
559 | 0 | wideNarrowRatio = atof(s); |
560 | 0 | } |
561 | 0 | } |
562 | |
|
563 | 0 | double moduleWidth = (0.25 / 25.4) * 72.0; // 0.25mm |
564 | 0 | if ((attr = barcodeElem->findAttr("moduleWidth"))) { |
565 | 0 | moduleWidth = getMeasurement(attr->getValue()); |
566 | 0 | } |
567 | |
|
568 | 0 | double moduleHeight = (5.0 / 25.4) * 72.0; // 5mm |
569 | 0 | if ((attr = barcodeElem->findAttr("moduleHeight"))) { |
570 | 0 | moduleHeight = getMeasurement(attr->getValue()); |
571 | 0 | } |
572 | |
|
573 | 0 | int dataLength = 0; |
574 | 0 | if ((attr = barcodeElem->findAttr("dataLength"))) { |
575 | 0 | dataLength = atoi(attr->getValue()->getCString()); |
576 | 0 | } |
577 | |
|
578 | 0 | int errorCorrectionLevel = 0; |
579 | 0 | if ((attr = barcodeElem->findAttr("errorCorrectionLevel"))) { |
580 | 0 | errorCorrectionLevel = atoi(attr->getValue()->getCString()); |
581 | 0 | } |
582 | |
|
583 | 0 | GString *textLocation; |
584 | 0 | if ((attr = barcodeElem->findAttr("textLocation"))) { |
585 | 0 | textLocation = attr->getValue()->copy(); |
586 | 0 | } else { |
587 | 0 | textLocation = new GString("below"); |
588 | 0 | } |
589 | |
|
590 | 0 | return new XFAFieldBarcodeInfo(barcodeType, wideNarrowRatio, |
591 | 0 | moduleWidth, moduleHeight, dataLength, |
592 | 0 | errorCorrectionLevel, textLocation); |
593 | 0 | } |
594 | | |
595 | 0 | double XFAScanner::getMeasurement(GString *s) { |
596 | 0 | int i = 0; |
597 | 0 | GBool neg = gFalse; |
598 | 0 | if (i < s->getLength() && s->getChar(i) == '+') { |
599 | 0 | ++i; |
600 | 0 | } else if (i < s->getLength() && s->getChar(i) == '-') { |
601 | 0 | neg = gTrue; |
602 | 0 | ++i; |
603 | 0 | } |
604 | 0 | double val = 0; |
605 | 0 | while (i < s->getLength() && s->getChar(i) >= '0' && s->getChar(i) <= '9') { |
606 | 0 | val = val * 10 + s->getChar(i) - '0'; |
607 | 0 | ++i; |
608 | 0 | } |
609 | 0 | if (i < s->getLength() && s->getChar(i) == '.') { |
610 | 0 | ++i; |
611 | 0 | double mul = 0.1; |
612 | 0 | while (i < s->getLength() && |
613 | 0 | s->getChar(i) >= '0' && s->getChar(i) <= '9') { |
614 | 0 | val += mul * (s->getChar(i) - '0'); |
615 | 0 | mul *= 0.1; |
616 | 0 | ++i; |
617 | 0 | } |
618 | 0 | } |
619 | 0 | if (neg) { |
620 | 0 | val = -val; |
621 | 0 | } |
622 | 0 | if (i+1 < s->getLength()) { |
623 | 0 | if (s->getChar(i) == 'i' && s->getChar(i+1) == 'n') { |
624 | 0 | val *= 72; |
625 | 0 | } else if (s->getChar(i) == 'p' && s->getChar(i+1) == 't') { |
626 | | // no change |
627 | 0 | } else if (s->getChar(i) == 'c' && s->getChar(i+1) == 'm') { |
628 | 0 | val *= 72 / 2.54; |
629 | 0 | } else if (s->getChar(i) == 'm' && s->getChar(i+1) == 'm') { |
630 | 0 | val *= 72 / 25.4; |
631 | 0 | } else { |
632 | | // default to inches |
633 | 0 | val *= 72; |
634 | 0 | } |
635 | 0 | } else { |
636 | | // default to inches |
637 | 0 | val *= 72; |
638 | 0 | } |
639 | 0 | return val; |
640 | 0 | } |
641 | | |
642 | 96.6k | GString *XFAScanner::getNodeName(ZxElement *elem) { |
643 | 96.6k | if (elem->isElement("template") || |
644 | 95.8k | elem->isElement("area") || |
645 | 95.7k | elem->isElement("draw")) { |
646 | 1.13k | return NULL; |
647 | 1.13k | } |
648 | 95.4k | if (!elem->isElement("field") && nodeIsBindNone(elem)) { |
649 | 0 | return NULL; |
650 | 0 | } |
651 | 95.4k | ZxAttr *nameAttr = elem->findAttr("name"); |
652 | 95.4k | if (!nameAttr) { |
653 | 76.8k | return NULL; |
654 | 76.8k | } |
655 | 18.6k | return nameAttr->getValue(); |
656 | 95.4k | } |
657 | | |
658 | 96.6k | GString *XFAScanner::getNodeFullName(ZxElement *elem) { |
659 | 96.6k | if (elem->isElement("template") || |
660 | 95.8k | elem->isElement("draw")) { |
661 | 1.05k | return NULL; |
662 | 1.05k | } |
663 | 95.5k | ZxAttr *nameAttr = elem->findAttr("name"); |
664 | 95.5k | if (!nameAttr) { |
665 | 76.8k | return NULL; |
666 | 76.8k | } |
667 | 18.6k | return nameAttr->getValue(); |
668 | 95.5k | } |
669 | | |
670 | 18.6k | GBool XFAScanner::nodeIsBindGlobal(ZxElement *elem) { |
671 | 18.6k | ZxElement *bindElem = elem->findFirstChildElement("bind"); |
672 | 18.6k | if (!bindElem) { |
673 | 18.6k | return gFalse; |
674 | 18.6k | } |
675 | 13 | ZxAttr *attr = bindElem->findAttr("match"); |
676 | 13 | return attr && !attr->getValue()->cmp("global"); |
677 | 18.6k | } |
678 | | |
679 | 91.9k | GBool XFAScanner::nodeIsBindNone(ZxElement *elem) { |
680 | 91.9k | ZxElement *bindElem = elem->findFirstChildElement("bind"); |
681 | 91.9k | if (!bindElem) { |
682 | 91.8k | return gFalse; |
683 | 91.8k | } |
684 | 103 | ZxAttr *attr = bindElem->findAttr("match"); |
685 | 103 | return attr && !attr->getValue()->cmp("none"); |
686 | 91.9k | } |