Coverage Report

Created: 2026-07-12 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
403
               double marginRightA) {
48
403
  hAlign = hAlignA;
49
403
  vAlign = vAlignA;
50
403
  marginLeft = marginLeftA;
51
403
  marginRight = marginRightA;
52
403
}
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
1.30k
  : name(nameA)
96
1.30k
  , fullName(fullNameA)
97
1.30k
  , value(valueA)
98
1.30k
  , layoutInfo(layoutInfoA)
99
1.30k
  , pictureInfo(pictureInfoA)
100
1.30k
  , barcodeInfo(barcodeInfoA)
101
1.30k
{
102
1.30k
}
103
104
1.30k
XFAField::~XFAField() {
105
1.30k
  delete name;
106
1.30k
  delete fullName;
107
1.30k
  delete value;
108
1.30k
  delete layoutInfo;
109
1.30k
  delete pictureInfo;
110
1.30k
  delete barcodeInfo;
111
1.30k
}
112
113
//------------------------------------------------------------------------
114
115
542
XFAScanner *XFAScanner::load(Object *xfaObj) {
116
542
  GString *xfaData = readXFAStreams(xfaObj);
117
542
  if (!xfaData) {
118
20
    return NULL;
119
20
  }
120
522
  ZxDoc *xml = ZxDoc::loadMem(xfaData->getCString(), xfaData->getLength());
121
522
  delete xfaData;
122
522
  if (!xml) {
123
81
    error(errSyntaxError, -1, "Invalid XML in XFA form");
124
81
    return NULL;
125
81
  }
126
127
441
  XFAScanner *scanner = new XFAScanner();
128
129
441
  if (xml->getRoot()) {
130
441
    GHash *formValues = scanner->scanFormValues(xml->getRoot());
131
441
    ZxElement *dataElem = NULL;
132
441
    ZxElement *datasets =
133
441
        xml->getRoot()->findFirstChildElement("xfa:datasets");
134
441
    if (datasets) {
135
73
      dataElem = datasets->findFirstChildElement("xfa:data");
136
73
    }
137
441
    ZxElement *tmpl = xml->getRoot()->findFirstChildElement("template");
138
441
    if (tmpl) {
139
256
      scanner->scanNode(tmpl, NULL, NULL, NULL, NULL, NULL,
140
256
      dataElem, formValues);
141
256
    }
142
441
    deleteGHash(formValues, GString);
143
441
  }
144
145
441
  delete xml;
146
147
441
  return scanner;
148
522
}
149
150
441
XFAScanner::XFAScanner() {
151
441
  fields = new GHash();
152
441
}
153
154
441
XFAScanner::~XFAScanner() {
155
441
  deleteGHash(fields, XFAField);
156
441
}
157
158
823
XFAField *XFAScanner::findField(GString *acroFormFieldName) {
159
823
  return (XFAField *)fields->lookup(acroFormFieldName);
160
823
}
161
162
542
GString *XFAScanner::readXFAStreams(Object *xfaObj) {
163
542
  GString *data = new GString();
164
542
  char buf[4096];
165
542
  int n;
166
542
  if (xfaObj->isStream()) {
167
0
    xfaObj->streamReset();
168
0
    while ((n = xfaObj->getStream()->getBlock(buf, sizeof(buf))) > 0) {
169
0
      data->append(buf, n);
170
0
    }
171
542
  } else if (xfaObj->isArray()) {
172
3.30k
    for (int i = 1; i < xfaObj->arrayGetLength(); i += 2) {
173
2.77k
      Object obj;
174
2.77k
      if (!xfaObj->arrayGet(i, &obj)->isStream()) {
175
19
  error(errSyntaxError, -1, "XFA array element is wrong type");
176
19
  obj.free();
177
19
  delete data;
178
19
  return NULL;
179
19
      }
180
2.75k
      obj.streamReset();
181
8.04k
      while ((n = obj.getStream()->getBlock(buf, sizeof(buf))) > 0) {
182
5.28k
  data->append(buf, n);
183
5.28k
      }
184
2.75k
      obj.free();
185
2.75k
    }
186
541
  } else {
187
1
    error(errSyntaxError, -1, "XFA object is wrong type");
188
1
    return NULL;
189
1
  }
190
522
  return data;
191
542
}
192
193
441
GHash *XFAScanner::scanFormValues(ZxElement *xmlRoot) {
194
441
  GHash *formValues = new GHash(gTrue);
195
441
  ZxElement *formElem = xmlRoot->findFirstChildElement("form");
196
441
  if (formElem) {
197
23
    scanFormNode(formElem, NULL, formValues);
198
23
  }
199
441
  return formValues;
200
441
}
201
202
void XFAScanner::scanFormNode(ZxElement *elem, GString *fullName,
203
86
            GHash *formValues) {
204
86
  GHash *fullNameIdx = new GHash();
205
86
  for (ZxNode *node = elem->getFirstChild();
206
527
       node;
207
441
       node = node->getNextChild()) {
208
441
    if (node->isElement("value")) {
209
33
      if (fullName) {
210
10
  ZxNode *child1Node = ((ZxElement *)node)->getFirstChild();
211
10
  if (child1Node && child1Node->isElement()) {
212
1
    ZxNode *child2Node = ((ZxElement *)child1Node)->getFirstChild();
213
1
    if (child2Node && child2Node->isCharData()) {
214
0
      formValues->add(fullName->copy(),
215
0
          ((ZxCharData *)child2Node)->getData()->copy());
216
0
    }
217
1
  }
218
10
      }
219
408
    } else if (node->isElement()) {
220
244
      ZxAttr *nameAttr = ((ZxElement *)node)->findAttr("name");
221
244
      if (nameAttr && (node->isElement("subform") ||
222
40
           node->isElement("field"))) {
223
40
  GString *nodeName = nameAttr->getValue();
224
40
  GString *childFullName;
225
40
  if (fullName) {
226
25
    childFullName = GString::format("{0:t}.{1:t}", fullName, nodeName);
227
25
  } else {
228
15
    childFullName = nodeName->copy();
229
15
  }
230
40
  int idx = fullNameIdx->lookupInt(nodeName);
231
40
  childFullName->appendf("[{0:d}]", idx);
232
40
  fullNameIdx->replace(nodeName, idx + 1);
233
40
  scanFormNode((ZxElement *)node, childFullName, formValues);
234
40
  delete childFullName;
235
204
      } else if (node->isElement("subform")) {
236
23
  scanFormNode((ZxElement *)node, fullName, formValues);
237
23
      }
238
244
    }
239
441
  }
240
86
  delete fullNameIdx;
241
86
}
242
243
void XFAScanner::scanNode(ZxElement *elem,
244
        GString *parentName, GString *parentFullName,
245
        GHash *nameIdx, GHash *fullNameIdx,
246
        GString *exclGroupName, ZxElement *dataElem,
247
47.7k
        GHash *formValues) {
248
47.7k
  GString *nodeName = getNodeName(elem);
249
47.7k
  GHash *childNameIdx;
250
47.7k
  if (!nameIdx || nodeName) {
251
8.96k
    childNameIdx = new GHash();
252
38.7k
  } else {
253
38.7k
    childNameIdx = nameIdx;
254
38.7k
  }
255
47.7k
  GString *nodeFullName = getNodeFullName(elem);
256
47.7k
  GHash *childFullNameIdx;
257
47.7k
  if (!fullNameIdx || (nodeFullName && !elem->isElement("area"))) {
258
8.96k
    childFullNameIdx = new GHash();
259
38.7k
  } else {
260
38.7k
    childFullNameIdx = fullNameIdx;
261
38.7k
  }
262
263
47.7k
  GString *childName;
264
47.7k
  if (nodeName) {
265
8.70k
    if (parentName) {
266
5.45k
      childName = GString::format("{0:t}.{1:t}", parentName, nodeName);
267
5.45k
    } else {
268
3.24k
      childName = nodeName->copy();
269
3.24k
    }
270
8.70k
    int idx = nameIdx->lookupInt(nodeName);
271
8.70k
    nameIdx->replace(nodeName, idx + 1);
272
8.70k
    if (nodeIsBindGlobal(elem)) {
273
0
      childName->appendf("[0]");
274
8.70k
    } else {
275
8.70k
      childName->appendf("[{0:d}]", idx);
276
8.70k
    }
277
39.0k
  } else {
278
39.0k
    childName = parentName;
279
39.0k
  }
280
47.7k
  GString *childFullName;
281
47.7k
  if (nodeFullName) {
282
8.70k
    if (parentFullName) {
283
5.45k
      childFullName = GString::format("{0:t}.{1:t}",
284
5.45k
              parentFullName, nodeFullName);
285
5.45k
    } else {
286
3.24k
      childFullName = nodeFullName->copy();
287
3.24k
    }
288
8.70k
    int idx = fullNameIdx->lookupInt(nodeFullName);
289
8.70k
    fullNameIdx->replace(nodeFullName, idx + 1);
290
8.70k
    childFullName->appendf("[{0:d}]", idx);
291
39.0k
  } else {
292
39.0k
    childFullName = parentFullName;
293
39.0k
  }
294
295
47.7k
  if (elem->isElement("field")) {
296
1.47k
    if (childName && childFullName) {
297
1.30k
      scanField(elem, childName, childFullName, exclGroupName,
298
1.30k
    dataElem, formValues);
299
1.30k
    }
300
46.2k
  } else {
301
46.2k
    GString *childExclGroupName;
302
46.2k
    if (elem->isElement("exclGroup")) {
303
78
      childExclGroupName = childName;
304
46.2k
    } else {
305
46.2k
      childExclGroupName = NULL;
306
46.2k
    }
307
46.2k
    for (ZxNode *child = elem->getFirstChild();
308
134k
   child;
309
87.8k
   child = child->getNextChild()) {
310
87.8k
      if (child->isElement()) {
311
47.4k
  scanNode((ZxElement *)child, childName, childFullName,
312
47.4k
     childNameIdx, childFullNameIdx, childExclGroupName,
313
47.4k
     dataElem, formValues);
314
47.4k
      }
315
87.8k
    }
316
46.2k
  }
317
318
47.7k
  if (childName != parentName) {
319
8.70k
    delete childName;
320
8.70k
  }
321
47.7k
  if (childFullName != parentFullName) {
322
8.70k
    delete childFullName;
323
8.70k
  }
324
47.7k
  if (childNameIdx != nameIdx) {
325
8.96k
    delete childNameIdx;
326
8.96k
  }
327
47.7k
  if (childFullNameIdx != fullNameIdx) {
328
8.96k
    delete childFullNameIdx;
329
8.96k
  }
330
47.7k
}
331
332
void XFAScanner::scanField(ZxElement *elem, GString *name, GString *fullName,
333
         GString *exclGroupName, ZxElement *dataElem,
334
1.30k
         GHash *formValues) {
335
1.30k
  GString *value = getFieldValue(elem, name, fullName, exclGroupName,
336
1.30k
         dataElem, formValues);
337
1.30k
  XFAFieldLayoutInfo *layoutInfo = getFieldLayoutInfo(elem);
338
1.30k
  XFAFieldPictureInfo *pictureInfo = getFieldPictureInfo(elem);
339
1.30k
  XFAFieldBarcodeInfo *barcodeInfo = getFieldBarcodeInfo(elem);
340
1.30k
  XFAField *field = new XFAField(name->copy(), fullName->copy(), value,
341
1.30k
         layoutInfo, pictureInfo, barcodeInfo);
342
1.30k
  fields->add(field->fullName, field);
343
1.30k
}
344
345
GString *XFAScanner::getFieldValue(ZxElement *elem, GString *name,
346
           GString *fullName, GString *exclGroupName,
347
1.30k
           ZxElement *dataElem, GHash *formValues) {
348
1.30k
  GString *val = NULL;
349
350
  //--- check the <xfa:datasets> packet
351
1.30k
  val = getDatasetsValue(name->getCString(), dataElem);
352
1.30k
  if (!val && exclGroupName) {
353
197
    val = (GString *)getDatasetsValue(exclGroupName->getCString(), dataElem);
354
197
  }
355
356
  //--- check the <form> element
357
1.30k
  if (!val) {
358
1.30k
    val = (GString *)formValues->lookup(fullName);
359
1.30k
  }
360
361
  //--- check the <value> element within the field
362
1.30k
  if (!val) {
363
1.30k
    ZxElement *valueElem = elem->findFirstChildElement("value");
364
1.30k
    if (valueElem) {
365
606
      ZxNode *child1Node = valueElem->getFirstChild();
366
606
      if (child1Node && child1Node->isElement()) {
367
385
  ZxNode *child2Node = ((ZxElement *)child1Node)->getFirstChild();
368
385
  if (child2Node && child2Node->isCharData()) {
369
346
    val = ((ZxCharData *)child2Node)->getData();
370
346
  }
371
385
      }
372
606
    }
373
1.30k
  }
374
375
  //--- get the checkbutton item value
376
1.30k
  GString *checkbuttonItem = NULL;
377
1.30k
  ZxElement *uiElem = elem->findFirstChildElement("ui");
378
1.30k
  if (uiElem) {
379
404
    ZxNode *uiChild = uiElem->getFirstChild();
380
404
    if (uiChild && uiChild->isElement("checkButton")) {
381
52
      ZxElement *itemsElem = elem->findFirstChildElement("items");
382
52
      if (itemsElem) {
383
39
  ZxNode *node1 = itemsElem->getFirstChild();
384
39
  if (node1 && node1->isElement()) {
385
37
    ZxNode *node2 = ((ZxElement *)node1)->getFirstChild();
386
37
    if (node2 && node2->isCharData()) {
387
26
      checkbuttonItem = ((ZxCharData *)node2)->getData();
388
26
    }
389
37
  }
390
39
      }
391
52
    }
392
404
  }
393
  // convert XFA checkbutton value to AcroForm-style On/Off value
394
1.30k
  if (checkbuttonItem && val) {
395
25
    if (val->cmp(checkbuttonItem)) {
396
16
      val = new GString("Off");
397
16
    } else {
398
9
      val = new GString("On");
399
9
    }
400
1.28k
  } else if (val) {
401
321
    val = val->copy();
402
321
  }
403
404
1.30k
  return val;
405
1.30k
}
406
407
2.11k
GString *XFAScanner::getDatasetsValue(char *partName, ZxElement *elem) {
408
2.11k
  if (!elem) {
409
1.17k
    return NULL;
410
1.17k
  }
411
412
  // partName = xxxx[nn].yyyy----
413
933
  char *p = strchr(partName, '[');
414
933
  if (!p) {
415
61
    return NULL;
416
61
  }
417
872
  int partLen = (int)(p - partName);
418
872
  int idx = atoi(p + 1);
419
872
  p = strchr(p + 1, '.');
420
872
  if (p) {
421
488
    ++p;
422
488
  }
423
424
872
  int curIdx = 0;
425
872
  for (ZxNode *node = elem->getFirstChild();
426
3.98k
       node;
427
3.49k
       node = node->getNextChild()) {
428
3.49k
    if (!node->isElement()) {
429
319
      continue;
430
319
    }
431
3.17k
    GString *nodeName = ((ZxElement *)node)->getType();
432
3.17k
    if (nodeName->getLength() != partLen ||
433
2.76k
  strncmp(nodeName->getCString(), partName, partLen)) {
434
2.76k
      continue;
435
2.76k
    }
436
412
    if (curIdx != idx) {
437
34
      ++curIdx;
438
34
      continue;
439
34
    }
440
378
    if (p) {
441
119
      GString *val = getDatasetsValue(p, (ZxElement *)node);
442
119
      if (val) {
443
0
  return val;
444
0
      }
445
119
      break;
446
259
    } else {
447
259
      ZxNode *child = ((ZxElement *)node)->getFirstChild();
448
259
      if (!child || !child->isCharData()) {
449
259
  return NULL;
450
259
      }
451
0
      return ((ZxCharData *)child)->getData();
452
259
    }
453
378
  }
454
455
  // search for an 'ancestor match'
456
613
  if (p) {
457
488
    return getDatasetsValue(p, elem);
458
488
  }
459
460
125
  return NULL;
461
613
}
462
463
1.30k
XFAFieldLayoutInfo *XFAScanner::getFieldLayoutInfo(ZxElement *elem) {
464
1.30k
  ZxElement *paraElem = elem->findFirstChildElement("para");
465
1.30k
  if (!paraElem) {
466
905
    return NULL;
467
905
  }
468
403
  XFAFieldLayoutHAlign hAlign = xfaFieldLayoutHAlignLeft;
469
403
  ZxAttr *hAlignAttr = paraElem->findAttr("hAlign");
470
403
  if (hAlignAttr) {
471
137
    if (!hAlignAttr->getValue()->cmp("left")) {
472
0
      hAlign = xfaFieldLayoutHAlignLeft;
473
137
    } else if (!hAlignAttr->getValue()->cmp("center")) {
474
42
      hAlign = xfaFieldLayoutHAlignCenter;
475
95
    } else if (!hAlignAttr->getValue()->cmp("right")) {
476
40
      hAlign = xfaFieldLayoutHAlignRight;
477
40
    }
478
137
  }
479
403
  XFAFieldLayoutVAlign vAlign = xfaFieldLayoutVAlignTop;
480
403
  ZxAttr *vAlignAttr = paraElem->findAttr("vAlign");
481
403
  if (vAlignAttr) {
482
222
    if (!vAlignAttr->getValue()->cmp("top")) {
483
0
      vAlign = xfaFieldLayoutVAlignTop;
484
222
    } else if (!vAlignAttr->getValue()->cmp("middle")) {
485
204
      vAlign = xfaFieldLayoutVAlignMiddle;
486
204
    } else if (!vAlignAttr->getValue()->cmp("bottom")) {
487
0
      vAlign = xfaFieldLayoutVAlignBottom;
488
0
    }
489
222
  }
490
403
  double marginLeft = 0;
491
403
  double marginRight = 0;
492
403
  ZxAttr *marginAttr;
493
403
  if ((marginAttr = paraElem->findAttr("marginLeft"))) {
494
0
    marginLeft = getMeasurement(marginAttr->getValue());
495
0
  }
496
403
  if ((marginAttr = paraElem->findAttr("marginRight"))) {
497
0
    marginRight = getMeasurement(marginAttr->getValue());
498
0
  }
499
403
  return new XFAFieldLayoutInfo(hAlign, vAlign, marginLeft, marginRight);
500
1.30k
}
501
502
1.30k
XFAFieldPictureInfo *XFAScanner::getFieldPictureInfo(ZxElement *elem) {
503
1.30k
  ZxElement *uiElem = elem->findFirstChildElement("ui");
504
1.30k
  if (!uiElem) {
505
904
    return NULL;
506
904
  }
507
404
  XFAFieldPictureSubtype subtype;
508
404
  if (uiElem->findFirstChildElement("dateTimeEdit")) {
509
0
    subtype = xfaFieldPictureDateTime;
510
404
  } else if (uiElem->findFirstChildElement("numericEdit")) {
511
0
    subtype = xfaFieldPictureNumeric;
512
404
  } else if (uiElem->findFirstChildElement("textEdit")) {
513
322
    subtype = xfaFieldPictureText;
514
322
  } else {
515
82
    return NULL;
516
82
  }
517
518
322
  ZxElement *formatElem, *pictureElem;
519
322
  ZxNode *pictureChildNode;
520
322
  if (!(formatElem = elem->findFirstChildElement("format")) ||
521
0
      !(pictureElem = formatElem->findFirstChildElement("picture")) ||
522
0
      !(pictureChildNode = pictureElem->getFirstChild()) ||
523
322
      !pictureChildNode->isCharData()) {
524
322
    return NULL;
525
322
  }
526
0
  GString *format = ((ZxCharData *)pictureChildNode)->getData()->copy();
527
528
0
  return new XFAFieldPictureInfo(subtype, format);
529
322
}
530
531
1.30k
XFAFieldBarcodeInfo *XFAScanner::getFieldBarcodeInfo(ZxElement *elem) {
532
1.30k
  ZxElement *uiElem, *barcodeElem;
533
1.30k
  if (!(uiElem = elem->findFirstChildElement("ui")) ||
534
1.30k
      !(barcodeElem = uiElem->findFirstChildElement("barcode"))) {
535
1.30k
    return NULL;
536
1.30k
  }
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
47.7k
GString *XFAScanner::getNodeName(ZxElement *elem) {
643
47.7k
  if (elem->isElement("template") ||
644
46.3k
      elem->isElement("area") ||
645
46.3k
      elem->isElement("draw")) {
646
1.38k
    return NULL;
647
1.38k
  }
648
46.3k
  if (!elem->isElement("field") && nodeIsBindNone(elem)) {
649
0
    return NULL;
650
0
  }
651
46.3k
  ZxAttr *nameAttr = elem->findAttr("name");
652
46.3k
  if (!nameAttr) {
653
37.6k
    return NULL;
654
37.6k
  }
655
8.70k
  return nameAttr->getValue();
656
46.3k
}
657
658
47.7k
GString *XFAScanner::getNodeFullName(ZxElement *elem) {
659
47.7k
  if (elem->isElement("template") ||
660
46.3k
      elem->isElement("draw")) {
661
1.38k
    return NULL;
662
1.38k
  }
663
46.3k
  ZxAttr *nameAttr = elem->findAttr("name");
664
46.3k
  if (!nameAttr) {
665
37.6k
    return NULL;
666
37.6k
  }
667
8.70k
  return nameAttr->getValue();
668
46.3k
}
669
670
8.70k
GBool XFAScanner::nodeIsBindGlobal(ZxElement *elem) {
671
8.70k
  ZxElement *bindElem = elem->findFirstChildElement("bind");
672
8.70k
  if (!bindElem) {
673
8.70k
    return gFalse;
674
8.70k
  }
675
0
  ZxAttr *attr = bindElem->findAttr("match");
676
0
  return attr && !attr->getValue()->cmp("global");
677
8.70k
}
678
679
44.9k
GBool XFAScanner::nodeIsBindNone(ZxElement *elem) {
680
44.9k
  ZxElement *bindElem = elem->findFirstChildElement("bind");
681
44.9k
  if (!bindElem) {
682
44.9k
    return gFalse;
683
44.9k
  }
684
0
  ZxAttr *attr = bindElem->findAttr("match");
685
0
  return attr && !attr->getValue()->cmp("none");
686
44.9k
}