@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.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/LICENSE-APACHE-2.0 +202 -0
  3. package/NOTICE +14 -0
  4. package/README.md +112 -2
  5. package/dist/barcode-paint.d.ts +11 -0
  6. package/dist/barcode-paint.js +135 -0
  7. package/dist/barcode.d.ts +45 -0
  8. package/dist/barcode.js +750 -0
  9. package/dist/canvas.d.ts +59 -0
  10. package/dist/canvas.js +179 -0
  11. package/dist/cli.d.ts +2 -0
  12. package/dist/cli.js +34 -0
  13. package/dist/css.d.ts +48 -0
  14. package/dist/css.js +288 -0
  15. package/dist/deadline.d.ts +6 -0
  16. package/dist/deadline.js +18 -0
  17. package/dist/filters.d.ts +21 -0
  18. package/dist/filters.js +296 -0
  19. package/dist/flatten.d.ts +25 -0
  20. package/dist/flatten.js +319 -0
  21. package/dist/font-metrics.d.ts +9 -0
  22. package/dist/font-metrics.js +215 -0
  23. package/dist/fonts.d.ts +36 -0
  24. package/dist/fonts.js +144 -0
  25. package/dist/images.d.ts +23 -0
  26. package/dist/images.js +519 -0
  27. package/dist/index.d.ts +2 -0
  28. package/dist/index.js +5 -0
  29. package/dist/index.mjs +3 -0
  30. package/dist/layout.d.ts +64 -0
  31. package/dist/layout.js +779 -0
  32. package/dist/limits.d.ts +2 -0
  33. package/dist/limits.js +6 -0
  34. package/dist/locale.d.ts +29 -0
  35. package/dist/locale.js +97 -0
  36. package/dist/paint.d.ts +15 -0
  37. package/dist/paint.js +533 -0
  38. package/dist/pdf-crypt.d.ts +35 -0
  39. package/dist/pdf-crypt.js +261 -0
  40. package/dist/pdf-read.d.ts +140 -0
  41. package/dist/pdf-read.js +1026 -0
  42. package/dist/pdf417-table.d.ts +2 -0
  43. package/dist/pdf417-table.js +146 -0
  44. package/dist/pdfjs-repair.d.ts +13 -0
  45. package/dist/pdfjs-repair.js +257 -0
  46. package/dist/pdfjs.d.ts +16 -0
  47. package/dist/pdfjs.js +62 -0
  48. package/dist/picture.d.ts +40 -0
  49. package/dist/picture.js +678 -0
  50. package/dist/saslprep.d.ts +11 -0
  51. package/dist/saslprep.js +178 -0
  52. package/dist/style.d.ts +29 -0
  53. package/dist/style.js +178 -0
  54. package/dist/text.d.ts +44 -0
  55. package/dist/text.js +199 -0
  56. package/dist/tiff.d.ts +3 -0
  57. package/dist/tiff.js +270 -0
  58. package/dist/tree.d.ts +23 -0
  59. package/dist/tree.js +54 -0
  60. package/dist/types.d.ts +31 -0
  61. package/dist/types.js +2 -0
  62. package/dist/writer.d.ts +38 -0
  63. package/dist/writer.js +145 -0
  64. package/dist/xfa-apply.d.ts +49 -0
  65. package/dist/xfa-apply.js +617 -0
  66. package/dist/xfa-model.d.ts +42 -0
  67. package/dist/xfa-model.js +227 -0
  68. package/dist/xfa-paint.d.ts +24 -0
  69. package/dist/xfa-paint.js +275 -0
  70. package/dist/xfa-spec.d.ts +47 -0
  71. package/dist/xfa-spec.js +3 -0
  72. package/dist/xml.d.ts +30 -0
  73. package/dist/xml.js +211 -0
  74. package/package.json +83 -4
@@ -0,0 +1,227 @@
1
+ "use strict";
2
+ // The XFA template, datasets, config and localeSet packets, parsed, with what the renderer asks of them:
3
+ // properties with prototypes (use, usehref) resolved, the ambient locale, and SOM locations for notes.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.CONTAINER_KINDS = void 0;
6
+ exports.kindNameKey = kindNameKey;
7
+ exports.loadModel = loadModel;
8
+ exports.prop = prop;
9
+ exports.attr = attr;
10
+ exports.props = props;
11
+ exports.uiKind = uiKind;
12
+ exports.displayPicture = displayPicture;
13
+ exports.valueKind = valueKind;
14
+ exports.localeOf = localeOf;
15
+ exports.somOf = somOf;
16
+ const pdf_read_1 = require("./pdf-read");
17
+ const xml_1 = require("./xml");
18
+ /** Element kinds pdf.js gives an id in its HTML, which the renderer maps back to the template. */
19
+ exports.CONTAINER_KINDS = new Set(['subform', 'subformSet', 'field', 'draw', 'exclGroup', 'area', 'pageArea', 'contentArea', 'pageSet']);
20
+ function findPacket(doc, local) {
21
+ let found = null;
22
+ const visit = (n, depth) => {
23
+ if (found || depth > 3)
24
+ return;
25
+ for (const k of n.kids) {
26
+ if (k.name === '#text')
27
+ continue;
28
+ if (k.local === local) {
29
+ found = k;
30
+ return;
31
+ }
32
+ visit(k, depth + 1);
33
+ }
34
+ };
35
+ visit(doc, 0);
36
+ return found;
37
+ }
38
+ function kindNameKey(kind, name) {
39
+ return `${kind}\u0000${name ?? ''}`;
40
+ }
41
+ /** Reads and indexes the XFA packets. Null when they cannot be read or have no template. */
42
+ function loadModel(bytes, passwords, deadline) {
43
+ const source = (0, pdf_read_1.readXfa)(bytes, passwords, deadline);
44
+ if (!source)
45
+ return null;
46
+ const doc = (0, xml_1.parseXml)((0, pdf_read_1.joinPackets)(source.packets), deadline);
47
+ const template = findPacket(doc, 'template');
48
+ if (!template)
49
+ return null;
50
+ const datasets = findPacket(doc, 'datasets');
51
+ const data = datasets ? (datasets.kids.find(k => k.local === 'data') ?? null) : null;
52
+ const order = new Map();
53
+ const byKindName = new Map();
54
+ const ids = new Map();
55
+ let n = 0;
56
+ // pdf.js clones template children but not the rich text an exData holds, so those do not take an id.
57
+ const visit = (node) => {
58
+ for (const k of node.kids) {
59
+ if (k.name === '#text')
60
+ continue;
61
+ order.set(k, n++);
62
+ if (k.attrs.id)
63
+ ids.set(k.attrs.id, k);
64
+ if (exports.CONTAINER_KINDS.has(k.local)) {
65
+ const key = kindNameKey(k.local, k.attrs.name || undefined);
66
+ const list = byKindName.get(key);
67
+ if (list)
68
+ list.push(k);
69
+ else
70
+ byKindName.set(key, [k]);
71
+ }
72
+ if (k.local !== 'exData')
73
+ visit(k);
74
+ }
75
+ };
76
+ visit(template);
77
+ const dataByName = new Map();
78
+ const dataOrder = new Map();
79
+ let d = 0;
80
+ const visitData = (node) => {
81
+ for (const k of node.kids) {
82
+ if (k.name === '#text')
83
+ continue;
84
+ dataOrder.set(k, d++);
85
+ const list = dataByName.get(k.local);
86
+ if (list)
87
+ list.push(k);
88
+ else
89
+ dataByName.set(k.local, [k]);
90
+ visitData(k);
91
+ }
92
+ };
93
+ if (data)
94
+ visitData(data);
95
+ return { template, data, localeSet: findPacket(doc, 'localeSet'), config: findPacket(doc, 'config'), order, byKindName, dataByName, dataOrder, ids, imageNames: source.imageNames };
96
+ }
97
+ /** The prototype a node names with use="#id" or usehref="#id" (internal references only). */
98
+ function protoOf(model, node) {
99
+ const ref = node.attrs.use || node.attrs.usehref || '';
100
+ const hash = ref.indexOf('#');
101
+ if (hash < 0)
102
+ return null;
103
+ const id = ref.slice(hash + 1);
104
+ if (!id || ref.slice(0, hash).includes('.xdp'))
105
+ return null;
106
+ const p = model.ids.get(id);
107
+ return p && p !== node ? p : null;
108
+ }
109
+ /** A child property, from the node or along its prototype chain. */
110
+ function prop(model, node, local) {
111
+ let cur = node;
112
+ for (let i = 0; cur && i < 8; i++) {
113
+ const c = (0, xml_1.child)(cur, local);
114
+ if (c)
115
+ return c;
116
+ cur = protoOf(model, cur);
117
+ }
118
+ return null;
119
+ }
120
+ /** An attribute, from the node or along its prototype chain. */
121
+ function attr(model, node, key) {
122
+ let cur = node;
123
+ for (let i = 0; cur && i < 8; i++) {
124
+ const v = cur.attrs[key];
125
+ if (v !== undefined)
126
+ return v;
127
+ cur = protoOf(model, cur);
128
+ }
129
+ return undefined;
130
+ }
131
+ /** All child properties of one kind (edges, corners, items), from the first node in the chain that has any. */
132
+ function props(model, node, local) {
133
+ let cur = node;
134
+ for (let i = 0; cur && i < 8; i++) {
135
+ const c = (0, xml_1.children)(cur, local);
136
+ if (c.length)
137
+ return c;
138
+ cur = protoOf(model, cur);
139
+ }
140
+ return [];
141
+ }
142
+ /** The one child of a ui (textEdit, checkButton, barcode...), with its local name. */
143
+ function uiKind(model, field) {
144
+ const ui = prop(model, field, 'ui');
145
+ if (ui) {
146
+ let cur = ui;
147
+ for (let i = 0; cur && i < 8; i++) {
148
+ for (const k of cur.kids) {
149
+ if (k.name !== '#text' && k.local !== 'picture' && k.local !== 'extras')
150
+ return { kind: k.local, node: k };
151
+ }
152
+ cur = protoOf(model, cur);
153
+ }
154
+ }
155
+ return { kind: 'textEdit', node: null };
156
+ }
157
+ /** The display picture: format/picture, else the ui's picture. */
158
+ function displayPicture(model, field) {
159
+ const fmt = prop(model, field, 'format');
160
+ const pic = fmt ? prop(model, fmt, 'picture') : null;
161
+ const t = pic ? (0, xml_1.textOf)(pic).trim() : '';
162
+ if (t)
163
+ return t;
164
+ const ui = prop(model, field, 'ui');
165
+ const up = ui ? prop(model, ui, 'picture') : null;
166
+ const u = up ? (0, xml_1.textOf)(up).trim() : '';
167
+ return u || null;
168
+ }
169
+ /** The value element's content kind: text, date, time, dateTime, decimal, float, integer, boolean, exData, image. */
170
+ function valueKind(model, field) {
171
+ const v = prop(model, field, 'value');
172
+ if (!v)
173
+ return 'text';
174
+ for (const k of v.kids)
175
+ if (k.name !== '#text')
176
+ return k.local;
177
+ return 'text';
178
+ }
179
+ /** The ambient locale: the nearest locale attribute, then the config's, then en_US. */
180
+ function localeOf(model, node) {
181
+ for (let cur = node; cur; cur = cur.parent) {
182
+ const l = cur.attrs.locale;
183
+ if (l && l !== 'ambient')
184
+ return l;
185
+ }
186
+ if (model.config) {
187
+ let found = '';
188
+ const visit = (n) => {
189
+ for (const k of n.kids) {
190
+ if (found)
191
+ return;
192
+ if (k.local === 'locale' && k.parent?.local === 'common')
193
+ found = (0, xml_1.textOf)(k).trim();
194
+ else if (k.name !== '#text')
195
+ visit(k);
196
+ }
197
+ };
198
+ visit(model.config);
199
+ if (found)
200
+ return found;
201
+ }
202
+ return 'en_US';
203
+ }
204
+ const SOM_KINDS = new Set(['subform', 'subformSet', 'field', 'draw', 'exclGroup', 'area', 'pageSet', 'pageArea', 'contentArea']);
205
+ /** A SOM expression for a template container, for notes: form1[0].page1[0].name[0]. Unnamed nodes show as #kind. */
206
+ function somOf(node) {
207
+ const parts = [];
208
+ for (let cur = node; cur && cur.local !== 'template'; cur = cur.parent) {
209
+ if (!SOM_KINDS.has(cur.local))
210
+ continue;
211
+ const name = cur.attrs.name;
212
+ const parent = cur.parent;
213
+ let index = 0;
214
+ if (parent) {
215
+ for (const sib of parent.kids) {
216
+ if (sib === cur)
217
+ break;
218
+ if (sib.local === cur.local && (sib.attrs.name ?? '') === (name ?? ''))
219
+ index++;
220
+ else if (name && sib.attrs.name === name && SOM_KINDS.has(sib.local))
221
+ index++;
222
+ }
223
+ }
224
+ parts.push(`${name || `#${cur.local}`}[${index}]`);
225
+ }
226
+ return parts.reverse().join('.');
227
+ }
@@ -0,0 +1,24 @@
1
+ import type { Canvas } from './canvas';
2
+ import type { Rgb } from './css';
3
+ import type { BorderSpec, CheckSpec } from './xfa-spec';
4
+ /** Dash array and line cap for an edge stroke. Lengths scale with the thickness, at least 1pt per unit. */
5
+ export declare function strokeDash(stroke: string, thickness: number): {
6
+ dash: number[] | null;
7
+ cap: number;
8
+ };
9
+ /** The darker and lighter shades of a 3D edge. A black edge shades with grays, as a black cannot get darker. */
10
+ export declare function shades(c: readonly [number, number, number]): {
11
+ dark: Rgb;
12
+ light: Rgb;
13
+ };
14
+ /** The closed outline path, for fills and for borders whose sides all look alike. */
15
+ export declare function outlinePath(spec: BorderSpec, x: number, y: number, w: number, h: number): {
16
+ ops: string;
17
+ points: number[];
18
+ };
19
+ /** Paints an XFA border on a box. */
20
+ export declare function paintXfaBorder(canvas: Canvas, spec: BorderSpec, x: number, y: number, w: number, h: number): void;
21
+ /** The ZapfDingbats codes the marks use and the characters they stand for, for the font's ToUnicode map. */
22
+ export declare const MARK_UNICODE: [number, number][];
23
+ /** Draws a check button: its box or circle and, unless it is off, its mark. */
24
+ export declare function paintCheck(canvas: Canvas, spec: CheckSpec, x: number, y: number, w: number, h: number, markColor: Rgb): void;
@@ -0,0 +1,275 @@
1
+ "use strict";
2
+ // Draws what the template describes and CSS cannot: XFA edge strokes (dash patterns and the 3D styles), border
3
+ // hand, corner shapes (round, inverted, notched), and check button shapes and marks.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.MARK_UNICODE = void 0;
6
+ exports.strokeDash = strokeDash;
7
+ exports.shades = shades;
8
+ exports.outlinePath = outlinePath;
9
+ exports.paintXfaBorder = paintXfaBorder;
10
+ exports.paintCheck = paintCheck;
11
+ const KAPPA = 0.5522847498;
12
+ function f(n) {
13
+ if (!Number.isFinite(n))
14
+ return '0';
15
+ const s = n.toFixed(3).replace(/0+$/, '').replace(/\.$/, '');
16
+ return s === '-0' ? '0' : s;
17
+ }
18
+ const DIRS = [
19
+ [1, 0],
20
+ [0, 1],
21
+ [-1, 0],
22
+ [0, -1],
23
+ ];
24
+ /** Dash array and line cap for an edge stroke. Lengths scale with the thickness, at least 1pt per unit. */
25
+ function strokeDash(stroke, thickness) {
26
+ const u = Math.max(thickness, 1);
27
+ switch (stroke) {
28
+ case 'dashed':
29
+ return { dash: [3 * u, 3 * u], cap: 0 };
30
+ case 'dotted':
31
+ return { dash: [0, 2 * u], cap: 1 };
32
+ case 'dashDot':
33
+ return { dash: [3 * u, 2 * u, 0, 2 * u], cap: 1 };
34
+ case 'dashDotDot':
35
+ return { dash: [3 * u, 2 * u, 0, 2 * u, 0, 2 * u], cap: 1 };
36
+ default:
37
+ return { dash: null, cap: 0 };
38
+ }
39
+ }
40
+ /** The darker and lighter shades of a 3D edge. A black edge shades with grays, as a black cannot get darker. */
41
+ function shades(c) {
42
+ if (c[0] + c[1] + c[2] < 0.15)
43
+ return { dark: [0.5, 0.5, 0.5], light: [0.83, 0.83, 0.83] };
44
+ return { dark: [c[0] * 0.5, c[1] * 0.5, c[2] * 0.5], light: [c[0] + (1 - c[0]) * 0.6, c[1] + (1 - c[1]) * 0.6, c[2] + (1 - c[2]) * 0.6] };
45
+ }
46
+ const THREE_D = new Set(['lowered', 'raised', 'etched', 'embossed']);
47
+ /** Whether a side reads as the dark one for a 3D stroke: top and left for lowered and etched. */
48
+ function sideIsDark(stroke, side) {
49
+ const topLeft = side === 0 || side === 3;
50
+ return stroke === 'lowered' || stroke === 'etched' ? topLeft : !topLeft;
51
+ }
52
+ function outlineOf(spec, x, y, w, h) {
53
+ const t = spec.edges.map(e => (e.visible ? e.thickness : 0));
54
+ let off;
55
+ if (spec.mode === 'css')
56
+ off = t.map(v => v / 2);
57
+ else if (spec.hand === 'left')
58
+ off = t.map(v => -v / 2);
59
+ else if (spec.hand === 'right')
60
+ off = t.map(v => v / 2);
61
+ else
62
+ off = [0, 0, 0, 0];
63
+ const x0 = x + off[3];
64
+ const y0 = y + off[0];
65
+ const x1 = x + w - off[1];
66
+ const y1 = y + h - off[2];
67
+ const maxR = Math.max(0, Math.min(x1 - x0, y1 - y0) / 2);
68
+ const r = spec.corners.map(c => (c.radius > 0 && (c.join === 'round' || c.inverted) ? Math.min(c.radius, maxR) : 0));
69
+ return {
70
+ p: [
71
+ [x0, y0],
72
+ [x1, y0],
73
+ [x1, y1],
74
+ [x0, y1],
75
+ ],
76
+ r,
77
+ };
78
+ }
79
+ /** Path for corner i, from the end of the side before it to the start of the side after it. */
80
+ function cornerOps(o, c, i, move, pts) {
81
+ const P = o.p[i];
82
+ const r = o.r[i];
83
+ const din = DIRS[(i + 3) % 4];
84
+ const dout = DIRS[i];
85
+ const A = [P[0] - r * din[0], P[1] - r * din[1]];
86
+ const B = [P[0] + r * dout[0], P[1] + r * dout[1]];
87
+ pts.push(P[0], P[1], A[0], A[1], B[0], B[1]);
88
+ // Moving starts a path at the corner; otherwise the side before it runs to the corner's first point.
89
+ let ops = `${f(A[0])} ${f(A[1])} ${move ? 'm' : 'l'} `;
90
+ if (r <= 0)
91
+ return move ? ops.trim() : `${f(B[0])} ${f(B[1])} l`;
92
+ if (!c.inverted) {
93
+ ops += `${f(A[0] + KAPPA * (P[0] - A[0]))} ${f(A[1] + KAPPA * (P[1] - A[1]))} ${f(B[0] + KAPPA * (P[0] - B[0]))} ${f(B[1] + KAPPA * (P[1] - B[1]))} ${f(B[0])} ${f(B[1])} c`;
94
+ }
95
+ else if (c.join === 'round') {
96
+ ops += `${f(A[0] + KAPPA * (B[0] - P[0]))} ${f(A[1] + KAPPA * (B[1] - P[1]))} ${f(B[0] + KAPPA * (A[0] - P[0]))} ${f(B[1] + KAPPA * (A[1] - P[1]))} ${f(B[0])} ${f(B[1])} c`;
97
+ }
98
+ else {
99
+ const Q = [A[0] + B[0] - P[0], A[1] + B[1] - P[1]];
100
+ ops += `${f(Q[0])} ${f(Q[1])} l ${f(B[0])} ${f(B[1])} l`;
101
+ }
102
+ return ops;
103
+ }
104
+ /** The closed outline path, for fills and for borders whose sides all look alike. */
105
+ function outlinePath(spec, x, y, w, h) {
106
+ const o = outlineOf(spec, x, y, w, h);
107
+ const pts = [];
108
+ const parts = [];
109
+ for (let i = 0; i < 4; i++)
110
+ parts.push(cornerOps(o, spec.corners[i], i, i === 0, pts));
111
+ return { ops: `${parts.join(' ')} h`, points: pts };
112
+ }
113
+ function sameEdge(a, b) {
114
+ return a.visible === b.visible && a.stroke === b.stroke && a.thickness === b.thickness && a.color.join() === b.color.join();
115
+ }
116
+ function strokeOne(canvas, ops, pts, color, width, stroke) {
117
+ const { dash, cap } = strokeDash(stroke, width);
118
+ canvas.strokePath(ops, pts, color, width, dash, dash ? cap : 0);
119
+ }
120
+ /** Paints an XFA border on a box. */
121
+ function paintXfaBorder(canvas, spec, x, y, w, h) {
122
+ const e = spec.edges;
123
+ const o = outlineOf(spec, x, y, w, h);
124
+ const uniform = e.every(v => sameEdge(v, e[0])) && e[0].visible && !THREE_D.has(e[0].stroke);
125
+ const cornersPlain = spec.corners.every((c, i) => o.r[i] === 0 || (c.visible && c.stroke === e[0].stroke && c.thickness === e[0].thickness));
126
+ if (uniform && cornersPlain) {
127
+ const { ops, points } = outlinePath(spec, x, y, w, h);
128
+ strokeOne(canvas, ops, points, e[0].color, e[0].thickness, e[0].stroke);
129
+ return;
130
+ }
131
+ // Side by side: each side between its corners' tangent points, then the shaped corners.
132
+ for (let i = 0; i < 4; i++) {
133
+ const edge = e[i];
134
+ if (!edge.visible || edge.thickness <= 0)
135
+ continue;
136
+ const d = DIRS[i];
137
+ const P0 = o.p[i];
138
+ const P1 = o.p[(i + 1) % 4];
139
+ const a = [P0[0] + o.r[i] * d[0], P0[1] + o.r[i] * d[1]];
140
+ const b = [P1[0] - o.r[(i + 1) % 4] * d[0], P1[1] - o.r[(i + 1) % 4] * d[1]];
141
+ if (THREE_D.has(edge.stroke)) {
142
+ paint3dSide(canvas, edge, i, a, b, o);
143
+ continue;
144
+ }
145
+ const ops = `${f(a[0])} ${f(a[1])} m ${f(b[0])} ${f(b[1])} l`;
146
+ const { dash, cap } = strokeDash(edge.stroke, edge.thickness);
147
+ // Solid sides meeting at a sharp corner use projecting caps so the corner is filled.
148
+ const sharp = o.r[i] === 0 && o.r[(i + 1) % 4] === 0;
149
+ canvas.strokePath(ops, [a[0], a[1], b[0], b[1]], edge.color, edge.thickness, dash, dash ? cap : sharp ? 2 : 0);
150
+ }
151
+ for (let i = 0; i < 4; i++) {
152
+ const c = spec.corners[i];
153
+ if (o.r[i] <= 0)
154
+ continue;
155
+ const edge = c.visible ? c : null;
156
+ if (!edge)
157
+ continue;
158
+ const pts = [];
159
+ const ops = cornerOps(o, c, i, true, pts);
160
+ let color = c.color;
161
+ if (THREE_D.has(c.stroke)) {
162
+ const sh = shades(c.color);
163
+ color = sideIsDark(c.stroke, i) ? sh.dark : sh.light;
164
+ }
165
+ strokeOne(canvas, ops, pts, color, c.thickness, THREE_D.has(c.stroke) ? 'solid' : c.stroke);
166
+ }
167
+ }
168
+ /** A 3D side: one shade for lowered and raised, two half-width lines for etched and embossed. */
169
+ function paint3dSide(canvas, edge, side, a, b, o) {
170
+ const sh = shades(edge.color);
171
+ const t = edge.thickness;
172
+ const dark = sideIsDark(edge.stroke, side);
173
+ // Outward normal of this side.
174
+ const n = [DIRS[(side + 3) % 4][0], DIRS[(side + 3) % 4][1]];
175
+ const sharp = o.r[side] === 0 && o.r[(side + 1) % 4] === 0;
176
+ if (edge.stroke === 'lowered' || edge.stroke === 'raised') {
177
+ canvas.strokePath(`${f(a[0])} ${f(a[1])} m ${f(b[0])} ${f(b[1])} l`, [a[0], a[1], b[0], b[1]], dark ? sh.dark : sh.light, t, null, sharp ? 2 : 0);
178
+ return;
179
+ }
180
+ const q = t / 4;
181
+ const outer = dark ? sh.dark : sh.light;
182
+ const inner = dark ? sh.light : sh.dark;
183
+ for (const [k, color] of [
184
+ [q, outer],
185
+ [-q, inner],
186
+ ]) {
187
+ const a2 = [a[0] + n[0] * k, a[1] + n[1] * k];
188
+ const b2 = [b[0] + n[0] * k, b[1] + n[1] * k];
189
+ canvas.strokePath(`${f(a2[0])} ${f(a2[1])} m ${f(b2[0])} ${f(b2[1])} l`, [a2[0], a2[1], b2[0], b2[1]], color, t / 2, null, sharp ? 2 : 0);
190
+ }
191
+ }
192
+ /** ZapfDingbats code, width and glyph box for each check mark, with the share of the box the mark fills. */
193
+ const MARKS = {
194
+ check: { code: 0x34, bbox: [36, -14, 811, 705], fill: 0.75 },
195
+ cross: { code: 0x38, bbox: [36, -13, 642, 705], fill: 0.7 },
196
+ star: { code: 0x48, bbox: [35, -14, 782, 705], fill: 0.75 },
197
+ circle: { code: 0x6c, bbox: [35, -14, 757, 705], fill: 0.5 },
198
+ square: { code: 0x6e, bbox: [35, 0, 726, 692], fill: 0.5 },
199
+ diamond: { code: 0x75, bbox: [35, -14, 754, 705], fill: 0.55 },
200
+ };
201
+ /** The ZapfDingbats codes the marks use and the characters they stand for, for the font's ToUnicode map. */
202
+ exports.MARK_UNICODE = [
203
+ [0x34, 0x2714],
204
+ [0x38, 0x2716],
205
+ [0x48, 0x2605],
206
+ [0x6c, 0x25cf],
207
+ [0x6e, 0x25a0],
208
+ [0x75, 0x25c6],
209
+ ];
210
+ /** Draws a check button: its box or circle and, unless it is off, its mark. */
211
+ function paintCheck(canvas, spec, x, y, w, h, markColor) {
212
+ const size = Math.min(w, h);
213
+ if (size <= 0)
214
+ return;
215
+ const bx = x + (w - size) / 2;
216
+ const by = y + (h - size) / 2;
217
+ const edge = spec.border?.edges[0];
218
+ if (spec.shape === 'round') {
219
+ const cx = bx + size / 2;
220
+ const cy = by + size / 2;
221
+ const t = edge ? (edge.visible ? edge.thickness : 0) : Math.max(0.5, size / 20);
222
+ const r = size / 2 - t / 2;
223
+ if (t > 0 && r > 0) {
224
+ const circle = (from, to) => arcOps(cx, cy, r, from, to);
225
+ const pts = [bx, by, bx + size, by + size];
226
+ if (edge && THREE_D.has(edge.stroke)) {
227
+ const sh = shades(edge.color);
228
+ const darkFirst = edge.stroke === 'lowered' || edge.stroke === 'etched';
229
+ canvas.strokePath(circle(135, 315), pts, darkFirst ? sh.dark : sh.light, t, null);
230
+ canvas.strokePath(circle(315, 495), pts, darkFirst ? sh.light : sh.dark, t, null);
231
+ }
232
+ else {
233
+ const { dash, cap } = strokeDash(edge?.stroke ?? 'solid', t);
234
+ canvas.strokePath(`${circle(0, 360)} h`, pts, edge?.color ?? [0, 0, 0], t, dash, dash ? cap : 0);
235
+ }
236
+ }
237
+ }
238
+ else if (spec.border) {
239
+ paintXfaBorder(canvas, { ...spec.border, mode: 'css' }, bx, by, size, size);
240
+ }
241
+ else {
242
+ const t = Math.max(0.5, size / 20);
243
+ canvas.strokePath(`${f(bx + t / 2)} ${f(by + t / 2)} ${f(size - t)} ${f(size - t)} re`, [bx, by, bx + size, by + size], [0, 0, 0], t, null);
244
+ }
245
+ if (spec.state === 'off')
246
+ return;
247
+ const m = MARKS[spec.mark];
248
+ const [gx0, gy0, gx1, gy1] = m.bbox;
249
+ const glyphSize = (size * m.fill * 1000) / Math.max(gx1 - gx0, gy1 - gy0);
250
+ const gw = ((gx1 - gx0) * glyphSize) / 1000;
251
+ const gh = ((gy1 - gy0) * glyphSize) / 1000;
252
+ const tx = bx + (size - gw) / 2 - (gx0 * glyphSize) / 1000;
253
+ // Canvas y grows downward; the text baseline sits below the glyph's top by its height above the baseline.
254
+ const baseline = by + (size - gh) / 2 + (gy1 * glyphSize) / 1000;
255
+ const color = spec.state === 'neutral' ? [0.5, 0.5, 0.5] : markColor;
256
+ canvas.text('ZapfDingbats', glyphSize, tx, baseline, [m.code], color, { width: gw });
257
+ }
258
+ /** A circular arc as Bezier segments, angles in degrees, clockwise on the page from the positive x axis. */
259
+ function arcOps(cx, cy, r, fromDeg, toDeg) {
260
+ const segs = Math.max(1, Math.ceil(Math.abs(toDeg - fromDeg) / 90));
261
+ const step = ((toDeg - fromDeg) / segs) * (Math.PI / 180);
262
+ const k = (4 / 3) * Math.tan(step / 4);
263
+ let a = fromDeg * (Math.PI / 180);
264
+ const out = [`${f(cx + r * Math.cos(a))} ${f(cy + r * Math.sin(a))} m`];
265
+ for (let s = 0; s < segs; s++) {
266
+ const b = a + step;
267
+ const p0 = [cx + r * Math.cos(a), cy + r * Math.sin(a)];
268
+ const p3 = [cx + r * Math.cos(b), cy + r * Math.sin(b)];
269
+ const c1 = [p0[0] - k * r * Math.sin(a), p0[1] + k * r * Math.cos(a)];
270
+ const c2 = [p3[0] + k * r * Math.sin(b), p3[1] - k * r * Math.cos(b)];
271
+ out.push(`${f(c1[0])} ${f(c1[1])} ${f(c2[0])} ${f(c2[1])} ${f(p3[0])} ${f(p3[1])} c`);
272
+ a = b;
273
+ }
274
+ return out.join(' ');
275
+ }
@@ -0,0 +1,47 @@
1
+ export type Rgb3 = readonly [number, number, number];
2
+ export interface EdgeSpec {
3
+ visible: boolean;
4
+ stroke: string;
5
+ thickness: number;
6
+ color: Rgb3;
7
+ }
8
+ export interface CornerSpec extends EdgeSpec {
9
+ radius: number;
10
+ inverted: boolean;
11
+ join: 'square' | 'round';
12
+ }
13
+ export interface BorderSpec {
14
+ /** Top, right, bottom, left. */
15
+ edges: [EdgeSpec, EdgeSpec, EdgeSpec, EdgeSpec];
16
+ /** Top left, top right, bottom right, bottom left. */
17
+ corners: [CornerSpec, CornerSpec, CornerSpec, CornerSpec];
18
+ hand: 'even' | 'left' | 'right';
19
+ /**
20
+ * 'css': pdf.js already placed the box for the hand (an xfaBorder div), so strokes go inside the box.
21
+ * 'nominal': the box is the nominal edge (a ui's own border), and the hand decides where strokes go.
22
+ */
23
+ mode: 'css' | 'nominal';
24
+ }
25
+ export type CheckState = 'on' | 'off' | 'neutral';
26
+ export interface CheckSpec {
27
+ shape: 'square' | 'round';
28
+ mark: 'check' | 'circle' | 'cross' | 'diamond' | 'square' | 'star';
29
+ state: CheckState;
30
+ border: BorderSpec | null;
31
+ }
32
+ export interface BarcodeSpec {
33
+ type: string;
34
+ data: string;
35
+ /** Narrow bar or module width in points, when the template gives one. */
36
+ moduleWidth: number | null;
37
+ moduleHeight: number | null;
38
+ /** Where the human-readable text goes: none, below, above, belowEmbedded, aboveEmbedded. */
39
+ textLocation: string;
40
+ wideNarrowRatio: number;
41
+ checksum: string;
42
+ errorCorrectionLevel: number;
43
+ dataColumnCount: number;
44
+ dataRowCount: number;
45
+ rowColumnRatio: number;
46
+ printCheckDigit: boolean;
47
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // What the template adds to pdf.js's HTML nodes for the painter: XFA borders, check button looks and barcodes.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/xml.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ export interface XmlNode {
2
+ /** Qualified name, for example 'field' or 'xfa:data'. '#text' for a text node. */
3
+ name: string;
4
+ /** The name without its prefix. */
5
+ local: string;
6
+ attrs: Record<string, string>;
7
+ kids: XmlNode[];
8
+ parent: XmlNode | null;
9
+ /** Text of a '#text' node. */
10
+ text: string;
11
+ /** Position among all elements of the document, in document order, starting at 0. */
12
+ index: number;
13
+ /** Offsets in the source of the start tag's '<' and of the character after its '>'; -1 for text and the root. */
14
+ tagStart: number;
15
+ tagEnd: number;
16
+ }
17
+ export declare function decodeEntities(s: string): string;
18
+ /** At most this many elements are open at once; a start tag inside the deepest is read as an empty element. */
19
+ export declare const MAX_XML_DEPTH = 1000;
20
+ /**
21
+ * Parses a document; returns a synthetic root named '#document' holding the top-level elements. Checks `deadline`
22
+ * (a Date.now() value) every few thousand tags.
23
+ */
24
+ export declare function parseXml(src: string, deadline?: number): XmlNode;
25
+ export declare function elements(node: XmlNode): XmlNode[];
26
+ export declare function child(node: XmlNode | null | undefined, local: string): XmlNode | null;
27
+ export declare function children(node: XmlNode | null | undefined, local: string): XmlNode[];
28
+ /** All text in the subtree, in document order. */
29
+ export declare function textOf(node: XmlNode | null | undefined): string;
30
+ export declare function walk(node: XmlNode, visit: (n: XmlNode) => boolean | undefined): void;