@patrickdk77/xfa-flatten 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/LICENSE-APACHE-2.0 +202 -0
- package/NOTICE +14 -0
- package/README.md +112 -2
- package/dist/barcode-paint.d.ts +11 -0
- package/dist/barcode-paint.js +135 -0
- package/dist/barcode.d.ts +45 -0
- package/dist/barcode.js +750 -0
- package/dist/canvas.d.ts +59 -0
- package/dist/canvas.js +179 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +34 -0
- package/dist/css.d.ts +48 -0
- package/dist/css.js +288 -0
- package/dist/deadline.d.ts +6 -0
- package/dist/deadline.js +18 -0
- package/dist/filters.d.ts +21 -0
- package/dist/filters.js +296 -0
- package/dist/flatten.d.ts +25 -0
- package/dist/flatten.js +319 -0
- package/dist/font-metrics.d.ts +9 -0
- package/dist/font-metrics.js +215 -0
- package/dist/fonts.d.ts +36 -0
- package/dist/fonts.js +144 -0
- package/dist/images.d.ts +23 -0
- package/dist/images.js +519 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/index.mjs +3 -0
- package/dist/layout.d.ts +64 -0
- package/dist/layout.js +779 -0
- package/dist/limits.d.ts +2 -0
- package/dist/limits.js +6 -0
- package/dist/locale.d.ts +29 -0
- package/dist/locale.js +97 -0
- package/dist/paint.d.ts +15 -0
- package/dist/paint.js +533 -0
- package/dist/pdf-crypt.d.ts +35 -0
- package/dist/pdf-crypt.js +261 -0
- package/dist/pdf-read.d.ts +140 -0
- package/dist/pdf-read.js +1026 -0
- package/dist/pdf417-table.d.ts +2 -0
- package/dist/pdf417-table.js +146 -0
- package/dist/pdfjs-repair.d.ts +13 -0
- package/dist/pdfjs-repair.js +257 -0
- package/dist/pdfjs.d.ts +16 -0
- package/dist/pdfjs.js +62 -0
- package/dist/picture.d.ts +40 -0
- package/dist/picture.js +678 -0
- package/dist/saslprep.d.ts +11 -0
- package/dist/saslprep.js +178 -0
- package/dist/style.d.ts +29 -0
- package/dist/style.js +178 -0
- package/dist/text.d.ts +44 -0
- package/dist/text.js +199 -0
- package/dist/tiff.d.ts +3 -0
- package/dist/tiff.js +270 -0
- package/dist/tree.d.ts +23 -0
- package/dist/tree.js +54 -0
- package/dist/types.d.ts +31 -0
- package/dist/types.js +2 -0
- package/dist/writer.d.ts +38 -0
- package/dist/writer.js +145 -0
- package/dist/xfa-apply.d.ts +49 -0
- package/dist/xfa-apply.js +617 -0
- package/dist/xfa-model.d.ts +42 -0
- package/dist/xfa-model.js +227 -0
- package/dist/xfa-paint.d.ts +24 -0
- package/dist/xfa-paint.js +275 -0
- package/dist/xfa-spec.d.ts +47 -0
- package/dist/xfa-spec.js +3 -0
- package/dist/xml.d.ts +30 -0
- package/dist/xml.js +211 -0
- package/package.json +83 -4
|
@@ -0,0 +1,617 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Matches pdf.js's HTML nodes back to template elements and adds what pdf.js leaves out: field values in their
|
|
3
|
+
// display picture, choice list text for values outside the list, check button looks and states, template borders,
|
|
4
|
+
// and barcodes. Also lists the scripts that run when a form opens, which pdf.js never runs.
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.measure = measure;
|
|
7
|
+
exports.borderSpec = borderSpec;
|
|
8
|
+
exports.stripNumber = stripNumber;
|
|
9
|
+
exports.applyTemplate = applyTemplate;
|
|
10
|
+
exports.hrefImages = hrefImages;
|
|
11
|
+
exports.openScripts = openScripts;
|
|
12
|
+
const deadline_1 = require("./deadline");
|
|
13
|
+
const locale_1 = require("./locale");
|
|
14
|
+
const picture_1 = require("./picture");
|
|
15
|
+
const tree_1 = require("./tree");
|
|
16
|
+
const xfa_model_1 = require("./xfa-model");
|
|
17
|
+
const xml_1 = require("./xml");
|
|
18
|
+
const ID_RE = /^([A-Za-z]+?)(\d+)$/;
|
|
19
|
+
function measure(s, fallback) {
|
|
20
|
+
if (!s)
|
|
21
|
+
return fallback;
|
|
22
|
+
const m = /^\s*([+-]?\d*\.?\d+)\s*([a-z]*)/i.exec(s);
|
|
23
|
+
if (!m)
|
|
24
|
+
return fallback;
|
|
25
|
+
const v = Number(m[1]);
|
|
26
|
+
if (!Number.isFinite(v))
|
|
27
|
+
return fallback;
|
|
28
|
+
switch (m[2].toLowerCase()) {
|
|
29
|
+
case 'in':
|
|
30
|
+
return v * 72;
|
|
31
|
+
case 'cm':
|
|
32
|
+
return (v / 2.54) * 72;
|
|
33
|
+
case 'mm':
|
|
34
|
+
return (v / 25.4) * 72;
|
|
35
|
+
case 'mp':
|
|
36
|
+
return v / 1000;
|
|
37
|
+
default:
|
|
38
|
+
return v;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function colorOf(model, node) {
|
|
42
|
+
const c = (0, xfa_model_1.prop)(model, node, 'color');
|
|
43
|
+
const v = c?.attrs.value;
|
|
44
|
+
if (!v)
|
|
45
|
+
return [0, 0, 0];
|
|
46
|
+
const parts = v.split(',').map(x => Number.parseInt(x.trim(), 10));
|
|
47
|
+
const ch = (n) => (n === undefined || !Number.isFinite(n) ? 0 : Math.max(0, Math.min(255, n)) / 255);
|
|
48
|
+
return [ch(parts[0]), ch(parts[1]), ch(parts[2])];
|
|
49
|
+
}
|
|
50
|
+
function edgeOf(model, e) {
|
|
51
|
+
const presence = (0, xfa_model_1.attr)(model, e, 'presence') ?? 'visible';
|
|
52
|
+
return {
|
|
53
|
+
visible: presence === 'visible',
|
|
54
|
+
stroke: (0, xfa_model_1.attr)(model, e, 'stroke') ?? 'solid',
|
|
55
|
+
thickness: measure((0, xfa_model_1.attr)(model, e, 'thickness'), 0.5),
|
|
56
|
+
color: colorOf(model, e),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/** Builds the four edges and four corners of a border element, repeating the last given as XFA does. */
|
|
60
|
+
function borderSpec(model, border, mode) {
|
|
61
|
+
const presence = (0, xfa_model_1.attr)(model, border, 'presence') ?? 'visible';
|
|
62
|
+
if (presence !== 'visible')
|
|
63
|
+
return null;
|
|
64
|
+
const edgeNodes = (0, xfa_model_1.props)(model, border, 'edge');
|
|
65
|
+
const edges = [0, 1, 2, 3].map(i => edgeOf(model, edgeNodes[Math.min(i, edgeNodes.length - 1)] ?? null));
|
|
66
|
+
const cornerNodes = (0, xfa_model_1.props)(model, border, 'corner');
|
|
67
|
+
const corners = [0, 1, 2, 3].map(i => {
|
|
68
|
+
const c = cornerNodes[Math.min(i, cornerNodes.length - 1)] ?? null;
|
|
69
|
+
const base = edgeOf(model, c);
|
|
70
|
+
const spec = {
|
|
71
|
+
...base,
|
|
72
|
+
radius: measure((0, xfa_model_1.attr)(model, c, 'radius'), 0),
|
|
73
|
+
inverted: (0, xfa_model_1.attr)(model, c, 'inverted') === '1',
|
|
74
|
+
join: (0, xfa_model_1.attr)(model, c, 'join') === 'round' ? 'round' : 'square',
|
|
75
|
+
};
|
|
76
|
+
return spec;
|
|
77
|
+
});
|
|
78
|
+
const hand = (0, xfa_model_1.attr)(model, border, 'hand');
|
|
79
|
+
return { edges, corners, hand: hand === 'left' || hand === 'right' ? hand : 'even', mode };
|
|
80
|
+
}
|
|
81
|
+
function hasCssBorder(node) {
|
|
82
|
+
const w = node.style.borderWidth;
|
|
83
|
+
if (w === undefined)
|
|
84
|
+
return false;
|
|
85
|
+
return String(w)
|
|
86
|
+
.split(/\s+/)
|
|
87
|
+
.some(p => Number.parseFloat(p) > 0);
|
|
88
|
+
}
|
|
89
|
+
/** Maps HTML nodes with an id to template elements: same kind and name, inside the mapped parent, nearest in order. */
|
|
90
|
+
function mapNodes(model, root) {
|
|
91
|
+
let offset = null;
|
|
92
|
+
let mapped = 0;
|
|
93
|
+
let unmapped = 0;
|
|
94
|
+
const isInside = (el, anc) => {
|
|
95
|
+
for (let p = el.parent; p; p = p.parent)
|
|
96
|
+
if (p === anc)
|
|
97
|
+
return true;
|
|
98
|
+
return false;
|
|
99
|
+
};
|
|
100
|
+
const visit = (node, parentTpl) => {
|
|
101
|
+
let scope = parentTpl;
|
|
102
|
+
const id = (0, tree_1.attrString)(node, 'id');
|
|
103
|
+
const m = id ? ID_RE.exec(id) : null;
|
|
104
|
+
if (m && xfa_model_1.CONTAINER_KINDS.has(m[1])) {
|
|
105
|
+
const k = Number(m[2]);
|
|
106
|
+
const name = (0, tree_1.attrString)(node, 'xfaName');
|
|
107
|
+
let cands = model.byKindName.get((0, xfa_model_1.kindNameKey)(m[1], name)) ?? [];
|
|
108
|
+
if (parentTpl && parentTpl.local !== 'contentArea') {
|
|
109
|
+
const inside = cands.filter(c => isInside(c, parentTpl));
|
|
110
|
+
if (inside.length)
|
|
111
|
+
cands = inside;
|
|
112
|
+
}
|
|
113
|
+
let best = null;
|
|
114
|
+
let bestD = Number.POSITIVE_INFINITY;
|
|
115
|
+
for (const c of cands) {
|
|
116
|
+
const o = model.order.get(c) ?? 0;
|
|
117
|
+
const d = offset === null ? o : Math.abs(k - offset - o);
|
|
118
|
+
if (d < bestD) {
|
|
119
|
+
bestD = d;
|
|
120
|
+
best = c;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (best) {
|
|
124
|
+
node.tpl = best;
|
|
125
|
+
offset = k - (model.order.get(best) ?? 0);
|
|
126
|
+
scope = best;
|
|
127
|
+
mapped++;
|
|
128
|
+
}
|
|
129
|
+
else
|
|
130
|
+
unmapped++;
|
|
131
|
+
}
|
|
132
|
+
for (const c of node.children)
|
|
133
|
+
visit(c, scope);
|
|
134
|
+
};
|
|
135
|
+
visit(root, null);
|
|
136
|
+
return { mapped, unmapped };
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Finds the data element pdf.js bound, from its id: the data element's name followed by a number. Names may end in
|
|
140
|
+
* digits themselves, so every split whose name exists is tried. Among the elements that accept the value, the one
|
|
141
|
+
* under the most data elements named like the field's template ancestors wins (two copies of a section bind two
|
|
142
|
+
* copies of the data), then the one nearest the order the ids so far suggest.
|
|
143
|
+
*/
|
|
144
|
+
function dataNodeOf(model, field, dataId, accept, hint) {
|
|
145
|
+
if (!dataId)
|
|
146
|
+
return null;
|
|
147
|
+
const tplAnc = new Set();
|
|
148
|
+
for (let p = field.parent; p; p = p.parent)
|
|
149
|
+
if (p.attrs.name)
|
|
150
|
+
tplAnc.add(p.attrs.name);
|
|
151
|
+
const scoreOf = (c) => {
|
|
152
|
+
let score = 0;
|
|
153
|
+
for (let p = c.parent; p; p = p.parent)
|
|
154
|
+
if (tplAnc.has(p.local))
|
|
155
|
+
score++;
|
|
156
|
+
return score;
|
|
157
|
+
};
|
|
158
|
+
let best = null;
|
|
159
|
+
let bestScore = -1;
|
|
160
|
+
let bestD = Number.POSITIVE_INFINITY;
|
|
161
|
+
let bestK = 0;
|
|
162
|
+
let unique = false;
|
|
163
|
+
for (let cut = dataId.length - 1; cut > 0; cut--) {
|
|
164
|
+
const digits = dataId.slice(cut);
|
|
165
|
+
if (!/^\d+$/.test(digits))
|
|
166
|
+
break;
|
|
167
|
+
const list = model.dataByName.get(dataId.slice(0, cut));
|
|
168
|
+
if (!list)
|
|
169
|
+
continue;
|
|
170
|
+
const k = Number(digits);
|
|
171
|
+
const ok = list.filter(c => accept((0, xml_1.textOf)(c)));
|
|
172
|
+
for (const c of ok) {
|
|
173
|
+
const o = model.dataOrder.get(c) ?? 0;
|
|
174
|
+
const d = hint.offset === null ? Math.abs(k - o) : Math.abs(k - hint.offset - o);
|
|
175
|
+
const score = scoreOf(c);
|
|
176
|
+
if (score > bestScore || (score === bestScore && d < bestD)) {
|
|
177
|
+
bestScore = score;
|
|
178
|
+
bestD = d;
|
|
179
|
+
best = c;
|
|
180
|
+
bestK = k;
|
|
181
|
+
unique = ok.length === 1;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (best && unique)
|
|
186
|
+
hint.offset = bestK - (model.dataOrder.get(best) ?? 0);
|
|
187
|
+
return best;
|
|
188
|
+
}
|
|
189
|
+
function pictureKind(vk, ui) {
|
|
190
|
+
if (vk === 'date')
|
|
191
|
+
return 'date';
|
|
192
|
+
if (vk === 'time')
|
|
193
|
+
return 'time';
|
|
194
|
+
if (vk === 'dateTime')
|
|
195
|
+
return 'datetime';
|
|
196
|
+
if (vk === 'decimal' || vk === 'float' || vk === 'integer')
|
|
197
|
+
return 'num';
|
|
198
|
+
if (ui === 'numericEdit')
|
|
199
|
+
return 'num';
|
|
200
|
+
return 'text';
|
|
201
|
+
}
|
|
202
|
+
function findDescendant(node, test) {
|
|
203
|
+
for (const c of node.children) {
|
|
204
|
+
if (test(c))
|
|
205
|
+
return c;
|
|
206
|
+
const d = findDescendant(c, test);
|
|
207
|
+
if (d)
|
|
208
|
+
return d;
|
|
209
|
+
}
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
/** The items of a field: display texts and save values (the list with save="1" holds the values). */
|
|
213
|
+
function itemLists(model, field) {
|
|
214
|
+
const lists = (0, xfa_model_1.props)(model, field, 'items');
|
|
215
|
+
if (lists.length === 0)
|
|
216
|
+
return { display: [], save: [] };
|
|
217
|
+
const texts = (l) => l.kids.filter(k => k.name !== '#text').map(k => (0, xml_1.textOf)(k));
|
|
218
|
+
if (lists.length === 1) {
|
|
219
|
+
const t = texts(lists[0]);
|
|
220
|
+
return { display: t, save: t };
|
|
221
|
+
}
|
|
222
|
+
const saveIdx = lists.findIndex(l => l.attrs.save === '1');
|
|
223
|
+
const s = saveIdx >= 0 ? saveIdx : 1;
|
|
224
|
+
return { display: texts(lists[s === 0 ? 1 : 0]), save: texts(lists[s]) };
|
|
225
|
+
}
|
|
226
|
+
function checkSpec(model, field, ui, value) {
|
|
227
|
+
const items = (0, xfa_model_1.props)(model, field, 'items')[0];
|
|
228
|
+
const vals = items ? items.kids.filter(k => k.name !== '#text').map(k => (0, xml_1.textOf)(k)) : [];
|
|
229
|
+
const on = vals[0] ?? '1';
|
|
230
|
+
const off = vals[1] ?? '0';
|
|
231
|
+
const neutral = vals[2];
|
|
232
|
+
const state = value === on ? 'on' : neutral !== undefined && value === neutral && value !== off ? 'neutral' : 'off';
|
|
233
|
+
const shape = (0, xfa_model_1.attr)(model, ui, 'shape') === 'round' ? 'round' : 'square';
|
|
234
|
+
const markAttr = (0, xfa_model_1.attr)(model, ui, 'mark') ?? 'default';
|
|
235
|
+
const marks = ['check', 'circle', 'cross', 'diamond', 'square', 'star'];
|
|
236
|
+
const named = marks.find(x => x === markAttr);
|
|
237
|
+
const mark = named ?? (shape === 'round' ? 'circle' : 'check');
|
|
238
|
+
const b = (0, xfa_model_1.prop)(model, ui, 'border');
|
|
239
|
+
return { shape, mark, state, border: b ? borderSpec(model, b, 'nominal') : null };
|
|
240
|
+
}
|
|
241
|
+
function barcodeSpec(model, ui, data) {
|
|
242
|
+
const a = (k) => (0, xfa_model_1.attr)(model, ui, k);
|
|
243
|
+
const num = (k, d) => {
|
|
244
|
+
const v = Number.parseFloat(a(k) ?? '');
|
|
245
|
+
return Number.isFinite(v) ? v : d;
|
|
246
|
+
};
|
|
247
|
+
const mw = a('moduleWidth');
|
|
248
|
+
const mh = a('moduleHeight');
|
|
249
|
+
return {
|
|
250
|
+
type: (a('type') ?? '').toLowerCase(),
|
|
251
|
+
data,
|
|
252
|
+
moduleWidth: mw ? measure(mw, 0) : null,
|
|
253
|
+
moduleHeight: mh ? measure(mh, 0) : null,
|
|
254
|
+
textLocation: a('textLocation') ?? 'below',
|
|
255
|
+
wideNarrowRatio: num('wideNarrowRatio', 3),
|
|
256
|
+
checksum: a('checksum') ?? 'none',
|
|
257
|
+
errorCorrectionLevel: num('errorCorrectionLevel', 0),
|
|
258
|
+
dataColumnCount: num('dataColumnCount', 0),
|
|
259
|
+
dataRowCount: num('dataRowCount', 0),
|
|
260
|
+
rowColumnRatio: num('rowColumnRatio', 0),
|
|
261
|
+
printCheckDigit: a('printCheckDigit') === '1',
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
/** The value a field's own template gives, when it has no data. */
|
|
265
|
+
function templateValue(model, field) {
|
|
266
|
+
const v = (0, xfa_model_1.prop)(model, field, 'value');
|
|
267
|
+
if (!v)
|
|
268
|
+
return '';
|
|
269
|
+
for (const k of v.kids)
|
|
270
|
+
if (k.name !== '#text')
|
|
271
|
+
return (0, xml_1.textOf)(k).trim();
|
|
272
|
+
return '';
|
|
273
|
+
}
|
|
274
|
+
/** A data value for a field pdf.js gave no input node (barcodes): matched by the field's name and ancestor names. */
|
|
275
|
+
function dataByName(model, field) {
|
|
276
|
+
const name = field.attrs.name;
|
|
277
|
+
if (!name)
|
|
278
|
+
return null;
|
|
279
|
+
const list = model.dataByName.get(name) ?? [];
|
|
280
|
+
if (list.length <= 1)
|
|
281
|
+
return list[0] ?? null;
|
|
282
|
+
const tplAnc = new Set();
|
|
283
|
+
for (let p = field.parent; p; p = p.parent)
|
|
284
|
+
if (p.attrs.name)
|
|
285
|
+
tplAnc.add(p.attrs.name);
|
|
286
|
+
let best = null;
|
|
287
|
+
let bestScore = -1;
|
|
288
|
+
for (const c of list) {
|
|
289
|
+
let score = 0;
|
|
290
|
+
for (let p = c.parent; p; p = p.parent)
|
|
291
|
+
if (tplAnc.has(p.local))
|
|
292
|
+
score++;
|
|
293
|
+
if (score > bestScore) {
|
|
294
|
+
bestScore = score;
|
|
295
|
+
best = c;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return best;
|
|
299
|
+
}
|
|
300
|
+
/** A data element named by an absolute dataRef, "$record.a.b" or "$data.a.b"; null for anything relative. */
|
|
301
|
+
function dataRef(model, ref) {
|
|
302
|
+
const steps = ref.split('.').map(s => s.replace(/\[\d+\]$/, ''));
|
|
303
|
+
const head = steps.shift();
|
|
304
|
+
let cur = model.data;
|
|
305
|
+
if (head === '$record')
|
|
306
|
+
cur = cur ? (cur.kids.find(k => k.name !== '#text') ?? null) : null;
|
|
307
|
+
else if (head !== '$data')
|
|
308
|
+
return null;
|
|
309
|
+
for (const step of steps) {
|
|
310
|
+
if (!cur)
|
|
311
|
+
return null;
|
|
312
|
+
cur = cur.kids.find(k => k.local === step) ?? null;
|
|
313
|
+
}
|
|
314
|
+
return cur;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* The data value of an exclusion group: from an absolute dataRef, or under normal binding, a data element with the group's name whose named
|
|
318
|
+
* ancestors match the group's named ancestors, or else the one data element of that name when the template has one
|
|
319
|
+
* group of that name. Repeated instances take the matching elements in order. Null for a group bound some other
|
|
320
|
+
* way (match="none" or a dataRef) and when nothing matches.
|
|
321
|
+
*/
|
|
322
|
+
function groupData(model, group, seen) {
|
|
323
|
+
const name = group.attrs.name;
|
|
324
|
+
if (!name)
|
|
325
|
+
return null;
|
|
326
|
+
const bind = (0, xfa_model_1.props)(model, group, 'bind')[0];
|
|
327
|
+
const match = bind?.attrs.match ?? 'once';
|
|
328
|
+
if (match === 'dataRef')
|
|
329
|
+
return dataRef(model, bind?.attrs.ref ?? '');
|
|
330
|
+
if (match !== 'once')
|
|
331
|
+
return null;
|
|
332
|
+
const ancestry = [];
|
|
333
|
+
for (let p = group.parent; p && p.local !== 'template'; p = p.parent)
|
|
334
|
+
if (p.attrs.name && p.local === 'subform')
|
|
335
|
+
ancestry.push(p.attrs.name);
|
|
336
|
+
ancestry.pop();
|
|
337
|
+
const key = [name, ...ancestry].join('.');
|
|
338
|
+
const hits = (model.dataByName.get(name) ?? []).filter(d => {
|
|
339
|
+
let x = d.parent;
|
|
340
|
+
for (const step of ancestry) {
|
|
341
|
+
if (!x || x.local !== step)
|
|
342
|
+
return false;
|
|
343
|
+
x = x.parent;
|
|
344
|
+
}
|
|
345
|
+
return true;
|
|
346
|
+
});
|
|
347
|
+
const n = seen.get(key) ?? 0;
|
|
348
|
+
seen.set(key, n + 1);
|
|
349
|
+
if (hits.length > 0)
|
|
350
|
+
return hits[n] ?? null;
|
|
351
|
+
// Containers bound by dataRef break the path; a name used once in the data and once in the template still pairs.
|
|
352
|
+
const all = model.dataByName.get(name) ?? [];
|
|
353
|
+
const groups = model.byKindName.get((0, xfa_model_1.kindNameKey)('exclGroup', name)) ?? [];
|
|
354
|
+
return all.length === 1 && groups.length === 1 ? all[0] : null;
|
|
355
|
+
}
|
|
356
|
+
function isVisible(node, ancestors) {
|
|
357
|
+
for (const n of [...ancestors, node]) {
|
|
358
|
+
if (n.style.visibility === 'hidden' || n.style.display === 'none')
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
function formatted(model, locales, field, raw, ui) {
|
|
364
|
+
const pic = (0, xfa_model_1.displayPicture)(model, field);
|
|
365
|
+
if (!pic)
|
|
366
|
+
return raw;
|
|
367
|
+
const kind = pictureKind((0, xfa_model_1.valueKind)(model, field), ui);
|
|
368
|
+
const out = (0, picture_1.formatPicture)({ picture: pic, value: raw, kind, locale: (0, xfa_model_1.localeOf)(model, field), locales });
|
|
369
|
+
return out ?? raw;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Keeps the digits, minus signs and the locale's decimal symbol of a number as written, with the symbol turned into
|
|
373
|
+
* '.'. The symbol comes from the form's localeSet, so it is escaped before it goes into the character class.
|
|
374
|
+
*/
|
|
375
|
+
function stripNumber(raw, decimal) {
|
|
376
|
+
const symbol = decimal.replace(/[\\\]^\-[]/g, '\\$&');
|
|
377
|
+
return raw.replace(new RegExp(`[^0-9${symbol}-]`, 'g'), '').replace(decimal, '.');
|
|
378
|
+
}
|
|
379
|
+
/** Converts a data value stored in the bind picture's format to canonical form, for dates. */
|
|
380
|
+
function canonical(model, field, raw, loc) {
|
|
381
|
+
const bind = (0, xfa_model_1.prop)(model, field, 'bind');
|
|
382
|
+
const bp = bind ? (0, xfa_model_1.prop)(model, bind, 'picture') : null;
|
|
383
|
+
const pic = bp ? (0, xml_1.textOf)(bp).trim() : '';
|
|
384
|
+
if (!pic)
|
|
385
|
+
return raw;
|
|
386
|
+
const vk = (0, xfa_model_1.valueKind)(model, field);
|
|
387
|
+
if (vk === 'date')
|
|
388
|
+
return (0, picture_1.parseWithDatePicture)(pic, raw, loc) ?? raw;
|
|
389
|
+
if (vk === 'decimal' || vk === 'float' || vk === 'integer') {
|
|
390
|
+
const stripped = stripNumber(raw, loc.decimal);
|
|
391
|
+
return (0, picture_1.parseDecimal)(stripped) ? stripped : raw;
|
|
392
|
+
}
|
|
393
|
+
return raw;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Applies the template to pdf.js's tree in place. A field whose template or data makes applying it throw keeps what
|
|
397
|
+
* pdf.js drew, and is noted; only the deadline stops the whole pass.
|
|
398
|
+
*/
|
|
399
|
+
function applyTemplate(model, root, options = {}) {
|
|
400
|
+
const locales = new locale_1.Locales(model.localeSet);
|
|
401
|
+
const { mapped, unmapped } = mapNodes(model, root);
|
|
402
|
+
const fields = [];
|
|
403
|
+
const hint = { offset: null };
|
|
404
|
+
const visit = (node, ancestors, owner) => {
|
|
405
|
+
(0, deadline_1.checkDeadline)(options.deadline);
|
|
406
|
+
const tpl = node.tpl ?? null;
|
|
407
|
+
const cur = tpl ?? owner;
|
|
408
|
+
if (tpl && tpl.local === 'field')
|
|
409
|
+
guarded(tpl, () => applyField(node, tpl, ancestors));
|
|
410
|
+
if (cur && hasCssBorder(node) && !node.xborder) {
|
|
411
|
+
const isBorderDiv = node.classes.includes('xfaBorder');
|
|
412
|
+
let border = null;
|
|
413
|
+
let mode = 'css';
|
|
414
|
+
if (isBorderDiv || node.tpl || cur.local !== 'field')
|
|
415
|
+
border = (0, xfa_model_1.prop)(model, cur, 'border');
|
|
416
|
+
else {
|
|
417
|
+
const ui = (0, xfa_model_1.uiKind)(model, cur);
|
|
418
|
+
if (ui.kind === 'button')
|
|
419
|
+
border = (0, xfa_model_1.prop)(model, cur, 'border');
|
|
420
|
+
else if (ui.node) {
|
|
421
|
+
border = (0, xfa_model_1.prop)(model, ui.node, 'border');
|
|
422
|
+
mode = 'nominal';
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
const spec = border ? borderSpec(model, border, mode) : null;
|
|
426
|
+
if (spec)
|
|
427
|
+
node.xborder = spec;
|
|
428
|
+
}
|
|
429
|
+
const next = [...ancestors, node];
|
|
430
|
+
// A wrapper holds the border div and the container's own node; the border belongs to that container.
|
|
431
|
+
const wrapped = node.classes.includes('xfaWrapper') ? node.children.find(c => c.tpl)?.tpl : undefined;
|
|
432
|
+
for (const c of node.children)
|
|
433
|
+
visit(c, next, wrapped ?? cur);
|
|
434
|
+
if (tpl && tpl.local === 'exclGroup')
|
|
435
|
+
guarded(tpl, () => groupStat(node, tpl, ancestors));
|
|
436
|
+
};
|
|
437
|
+
const guarded = (tpl, run) => {
|
|
438
|
+
try {
|
|
439
|
+
run();
|
|
440
|
+
}
|
|
441
|
+
catch (e) {
|
|
442
|
+
if (e instanceof deadline_1.DeadlineError)
|
|
443
|
+
throw e;
|
|
444
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
445
|
+
options.note?.('field-error', (0, xfa_model_1.somOf)(tpl), `A ${tpl.local} was drawn as pdf.js laid it out; applying its template failed: ${msg}`);
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
// pdf.js binds an exclusion group's value to the group and gives its buttons no data id, so the value is counted
|
|
449
|
+
// here: which button, if any, is on for it.
|
|
450
|
+
const groupSeen = new Map();
|
|
451
|
+
const groupStat = (node, group, ancestors) => {
|
|
452
|
+
const d = groupData(model, group, groupSeen);
|
|
453
|
+
const raw = d ? (0, xml_1.textOf)(d).trim() : '';
|
|
454
|
+
if (!d || raw === '')
|
|
455
|
+
return;
|
|
456
|
+
let shown = 'none';
|
|
457
|
+
const buttons = (n) => {
|
|
458
|
+
for (const c of n.children) {
|
|
459
|
+
if (c.tpl && c.tpl.local === 'field' && c.tpl.parent === group) {
|
|
460
|
+
const items = (0, xfa_model_1.props)(model, c.tpl, 'items')[0];
|
|
461
|
+
const on = items ? (items.kids.find(k => k.name !== '#text') ?? null) : null;
|
|
462
|
+
const input = findDescendant(c, x => x.check !== undefined);
|
|
463
|
+
if (on && (0, xml_1.textOf)(on).trim() === raw && input?.check)
|
|
464
|
+
shown = input.check.state;
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
buttons(c);
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
buttons(node);
|
|
471
|
+
fields.push({ dataIndex: model.dataOrder.get(d) ?? null, raw, shown, kind: 'radio', visible: isVisible(node, ancestors) });
|
|
472
|
+
};
|
|
473
|
+
const applyField = (node, field, ancestors) => {
|
|
474
|
+
const ui = (0, xfa_model_1.uiKind)(model, field);
|
|
475
|
+
const loc = locales.get((0, xfa_model_1.localeOf)(model, field));
|
|
476
|
+
const visible = isVisible(node, ancestors);
|
|
477
|
+
if (ui.kind === 'barcode' && ui.node) {
|
|
478
|
+
const d = dataByName(model, field);
|
|
479
|
+
const raw = d ? (0, xml_1.textOf)(d).trim() : templateValue(model, field);
|
|
480
|
+
node.barcode = barcodeSpec(model, ui.node, raw);
|
|
481
|
+
if (raw)
|
|
482
|
+
fields.push({ dataIndex: d ? (model.dataOrder.get(d) ?? null) : null, raw, shown: raw, kind: 'barcode', visible });
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
const input = findDescendant(node, n => n.name === 'input' || n.name === 'textarea' || n.name === 'select');
|
|
486
|
+
if (!input)
|
|
487
|
+
return;
|
|
488
|
+
const dataId = (0, tree_1.attrString)(input, 'dataId');
|
|
489
|
+
if (ui.kind === 'checkButton') {
|
|
490
|
+
const raw = (0, tree_1.attrString)(input, 'value') ?? '';
|
|
491
|
+
const spec = checkSpec(model, field, ui.node, raw);
|
|
492
|
+
input.check = spec;
|
|
493
|
+
const d = dataNodeOf(model, field, dataId, t => t.trim() === raw, hint);
|
|
494
|
+
if (raw !== '')
|
|
495
|
+
fields.push({ dataIndex: d ? (model.dataOrder.get(d) ?? null) : null, raw, shown: spec.state, kind: 'check', visible });
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
if (input.name === 'select') {
|
|
499
|
+
const opts = input.children.filter(o => o.name === 'option');
|
|
500
|
+
const chosen = opts.find(o => (o.attrs.selected === true || o.attrs.selected === 'true') && o.attrs.hidden !== true && o.attrs.hidden !== 'true');
|
|
501
|
+
const items = itemLists(model, field);
|
|
502
|
+
const d = dataNodeOf(model, field, dataId, () => true, hint);
|
|
503
|
+
let raw = d ? (0, xml_1.textOf)(d).trim() : '';
|
|
504
|
+
if (!raw)
|
|
505
|
+
raw = templateValue(model, field);
|
|
506
|
+
if (!chosen && raw) {
|
|
507
|
+
// pdf.js shows a blank when the value is not one of the items; Adobe shows the value itself.
|
|
508
|
+
const i = items.save.indexOf(raw);
|
|
509
|
+
const text = i >= 0 ? (items.display[i] ?? raw) : formatted(model, locales, field, raw, ui.kind);
|
|
510
|
+
input.attrs.xfaShown = text;
|
|
511
|
+
}
|
|
512
|
+
const shown = chosen ? (chosen.value ?? '') : ((0, tree_1.attrString)(input, 'xfaShown') ?? '');
|
|
513
|
+
if (raw)
|
|
514
|
+
fields.push({ dataIndex: d ? (model.dataOrder.get(d) ?? null) : null, raw, shown, kind: 'choice', visible });
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
const isArea = input.name === 'textarea';
|
|
518
|
+
const htmlValue = (isArea ? (0, tree_1.attrString)(input, 'textContent') : (0, tree_1.attrString)(input, 'value')) ?? '';
|
|
519
|
+
const vk = (0, xfa_model_1.valueKind)(model, field);
|
|
520
|
+
const numeric = ui.kind === 'numericEdit' || vk === 'decimal' || vk === 'float' || vk === 'integer';
|
|
521
|
+
const d = dataNodeOf(model, field, dataId, t => {
|
|
522
|
+
if (!numeric)
|
|
523
|
+
return t === htmlValue || t.trim() === htmlValue.trim();
|
|
524
|
+
// pdf.js shows nothing for a value that is not a number ('$0'); the data element still holds it.
|
|
525
|
+
if (htmlValue === '')
|
|
526
|
+
return t.trim() !== '';
|
|
527
|
+
const a = Number.parseFloat(t);
|
|
528
|
+
return Number.isFinite(a) && String(a) === htmlValue;
|
|
529
|
+
}, hint);
|
|
530
|
+
// pdf.js turns numeric values into a JavaScript number string; the data element has the value as written.
|
|
531
|
+
let raw = numeric && d ? (0, xml_1.textOf)(d).trim() : htmlValue;
|
|
532
|
+
if (raw === '' && !d)
|
|
533
|
+
raw = htmlValue;
|
|
534
|
+
if (raw !== '')
|
|
535
|
+
raw = canonical(model, field, raw, loc);
|
|
536
|
+
const shown = raw === '' ? '' : formatted(model, locales, field, raw, ui.kind);
|
|
537
|
+
if (shown !== htmlValue) {
|
|
538
|
+
if (isArea)
|
|
539
|
+
input.attrs.textContent = shown;
|
|
540
|
+
else
|
|
541
|
+
input.attrs.value = shown;
|
|
542
|
+
}
|
|
543
|
+
if (raw !== '')
|
|
544
|
+
fields.push({ dataIndex: d ? (model.dataOrder.get(d) ?? null) : null, raw, shown, kind: 'text', visible });
|
|
545
|
+
};
|
|
546
|
+
visit(root, [], null);
|
|
547
|
+
return { mapped, unmapped, fields };
|
|
548
|
+
}
|
|
549
|
+
/** Script events that run when a form opens or lays out, which pdf.js does not run. */
|
|
550
|
+
const OPEN_EVENTS = {
|
|
551
|
+
initialize: 'initialize',
|
|
552
|
+
docReady: 'docReady',
|
|
553
|
+
layoutReady: 'layoutReady',
|
|
554
|
+
ready: 'ready',
|
|
555
|
+
};
|
|
556
|
+
/** Images the template names only by a path to a file that the PDF does not carry; pdf.js leaves them out. */
|
|
557
|
+
function hrefImages(model) {
|
|
558
|
+
const out = [];
|
|
559
|
+
const visit = (n, container) => {
|
|
560
|
+
for (const k of n.kids) {
|
|
561
|
+
if (k.name === '#text' || k.local === 'proto')
|
|
562
|
+
continue;
|
|
563
|
+
const c = xfa_model_1.CONTAINER_KINDS.has(k.local) ? k : container;
|
|
564
|
+
if (k.local === 'image') {
|
|
565
|
+
const href = k.attrs.href ?? '';
|
|
566
|
+
if (href && (0, xml_1.textOf)(k).trim() === '' && !model.imageNames.has(href) && c)
|
|
567
|
+
out.push({ location: (0, xfa_model_1.somOf)(c), href });
|
|
568
|
+
continue;
|
|
569
|
+
}
|
|
570
|
+
visit(k, c);
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
visit(model.template, null);
|
|
574
|
+
return out;
|
|
575
|
+
}
|
|
576
|
+
function hasScript(n) {
|
|
577
|
+
return (0, xml_1.children)(n, 'script').some(s => (0, xml_1.textOf)(s).trim() !== '');
|
|
578
|
+
}
|
|
579
|
+
/** Lists template scripts that would run at open, by event kind and the SOM location of their container. */
|
|
580
|
+
function openScripts(model) {
|
|
581
|
+
const out = [];
|
|
582
|
+
const seen = new Set();
|
|
583
|
+
const add = (kind, container) => {
|
|
584
|
+
const location = (0, xfa_model_1.somOf)(container);
|
|
585
|
+
const key = `${kind}\u0000${location}`;
|
|
586
|
+
if (seen.has(key))
|
|
587
|
+
return;
|
|
588
|
+
seen.add(key);
|
|
589
|
+
out.push({ kind, location });
|
|
590
|
+
};
|
|
591
|
+
const visit = (n, container) => {
|
|
592
|
+
for (const k of n.kids) {
|
|
593
|
+
if (k.name === '#text' || k.local === 'proto' || k.local === 'exData')
|
|
594
|
+
continue;
|
|
595
|
+
const c = xfa_model_1.CONTAINER_KINDS.has(k.local) ? k : container;
|
|
596
|
+
if (k.local === 'event' && c && hasScript(k)) {
|
|
597
|
+
const act = k.attrs.activity ?? 'click';
|
|
598
|
+
const ref = k.attrs.ref ?? '';
|
|
599
|
+
let kind = OPEN_EVENTS[act];
|
|
600
|
+
if (act === 'ready' && ref.includes('$form'))
|
|
601
|
+
kind = 'form:ready';
|
|
602
|
+
else if (act === 'ready' && ref.includes('$layout'))
|
|
603
|
+
kind = 'layout:ready';
|
|
604
|
+
if (kind)
|
|
605
|
+
add(kind, c);
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
if (k.local === 'calculate' && c && hasScript(k)) {
|
|
609
|
+
add('calculate', c);
|
|
610
|
+
continue;
|
|
611
|
+
}
|
|
612
|
+
visit(k, c);
|
|
613
|
+
}
|
|
614
|
+
};
|
|
615
|
+
visit(model.template, null);
|
|
616
|
+
return out;
|
|
617
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type XmlNode } from './xml';
|
|
2
|
+
/** Element kinds pdf.js gives an id in its HTML, which the renderer maps back to the template. */
|
|
3
|
+
export declare const CONTAINER_KINDS: Set<string>;
|
|
4
|
+
export interface XfaModel {
|
|
5
|
+
template: XmlNode;
|
|
6
|
+
/** The xfa:data element, when there is one. */
|
|
7
|
+
data: XmlNode | null;
|
|
8
|
+
localeSet: XmlNode | null;
|
|
9
|
+
config: XmlNode | null;
|
|
10
|
+
/** Template elements in the order pdf.js clones them, which is the order of its HTML ids. */
|
|
11
|
+
order: Map<XmlNode, number>;
|
|
12
|
+
/** Template elements by kind and name, in that order. */
|
|
13
|
+
byKindName: Map<string, XmlNode[]>;
|
|
14
|
+
/** Data elements by local name, in document order. */
|
|
15
|
+
dataByName: Map<string, XmlNode[]>;
|
|
16
|
+
dataOrder: Map<XmlNode, number>;
|
|
17
|
+
ids: Map<string, XmlNode>;
|
|
18
|
+
/** Images the PDF carries for hrefs in the template. */
|
|
19
|
+
imageNames: Set<string>;
|
|
20
|
+
}
|
|
21
|
+
export declare function kindNameKey(kind: string, name: string | undefined): string;
|
|
22
|
+
/** Reads and indexes the XFA packets. Null when they cannot be read or have no template. */
|
|
23
|
+
export declare function loadModel(bytes: Uint8Array, passwords: readonly string[], deadline?: number): XfaModel | null;
|
|
24
|
+
/** A child property, from the node or along its prototype chain. */
|
|
25
|
+
export declare function prop(model: XfaModel, node: XmlNode | null, local: string): XmlNode | null;
|
|
26
|
+
/** An attribute, from the node or along its prototype chain. */
|
|
27
|
+
export declare function attr(model: XfaModel, node: XmlNode | null, key: string): string | undefined;
|
|
28
|
+
/** All child properties of one kind (edges, corners, items), from the first node in the chain that has any. */
|
|
29
|
+
export declare function props(model: XfaModel, node: XmlNode | null, local: string): XmlNode[];
|
|
30
|
+
/** The one child of a ui (textEdit, checkButton, barcode...), with its local name. */
|
|
31
|
+
export declare function uiKind(model: XfaModel, field: XmlNode): {
|
|
32
|
+
kind: string;
|
|
33
|
+
node: XmlNode | null;
|
|
34
|
+
};
|
|
35
|
+
/** The display picture: format/picture, else the ui's picture. */
|
|
36
|
+
export declare function displayPicture(model: XfaModel, field: XmlNode): string | null;
|
|
37
|
+
/** The value element's content kind: text, date, time, dateTime, decimal, float, integer, boolean, exData, image. */
|
|
38
|
+
export declare function valueKind(model: XfaModel, field: XmlNode): string;
|
|
39
|
+
/** The ambient locale: the nearest locale attribute, then the config's, then en_US. */
|
|
40
|
+
export declare function localeOf(model: XfaModel, node: XmlNode): string;
|
|
41
|
+
/** A SOM expression for a template container, for notes: form1[0].page1[0].name[0]. Unnamed nodes show as #kind. */
|
|
42
|
+
export declare function somOf(node: XmlNode): string;
|