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