@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
package/dist/tiff.js
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Baseline TIFF decoding for XFA images: uncompressed, PackBits, LZW and Deflate; strips and tiles; chunky and
|
|
3
|
+
// planar samples of 1, 2, 4, 8 or 16 bits; gray, RGB, palette and CMYK, with extra alpha. The first image only.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.readTiff = readTiff;
|
|
6
|
+
const filters_1 = require("./filters");
|
|
7
|
+
const limits_1 = require("./limits");
|
|
8
|
+
/** The most samples per pixel taken: CMYK with alpha and a few extra channels. */
|
|
9
|
+
const MAX_SAMPLES_PER_PIXEL = 8;
|
|
10
|
+
class Reader {
|
|
11
|
+
b;
|
|
12
|
+
le;
|
|
13
|
+
constructor(b, le) {
|
|
14
|
+
this.b = b;
|
|
15
|
+
this.le = le;
|
|
16
|
+
}
|
|
17
|
+
u16(o) {
|
|
18
|
+
if (o + 2 > this.b.length)
|
|
19
|
+
throw new Error('TIFF data ends early');
|
|
20
|
+
return this.le ? this.b[o] | (this.b[o + 1] << 8) : (this.b[o] << 8) | this.b[o + 1];
|
|
21
|
+
}
|
|
22
|
+
u32(o) {
|
|
23
|
+
if (o + 4 > this.b.length)
|
|
24
|
+
throw new Error('TIFF data ends early');
|
|
25
|
+
return this.le ? (this.b[o] | (this.b[o + 1] << 8) | (this.b[o + 2] << 16)) + this.b[o + 3] * 0x1000000 : this.b[o] * 0x1000000 + ((this.b[o + 1] << 16) | (this.b[o + 2] << 8) | this.b[o + 3]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const TYPE_SIZE = { 1: 1, 2: 1, 3: 2, 4: 4, 5: 8, 6: 1, 7: 1, 8: 2, 9: 4, 10: 8, 11: 4, 12: 8, 16: 8 };
|
|
29
|
+
function values(r, e) {
|
|
30
|
+
if (!e)
|
|
31
|
+
return [];
|
|
32
|
+
const out = [];
|
|
33
|
+
const size = TYPE_SIZE[e.type] ?? 1;
|
|
34
|
+
const n = Math.min(e.count, 1 << 20);
|
|
35
|
+
for (let i = 0; i < n; i++) {
|
|
36
|
+
const o = e.at + i * size;
|
|
37
|
+
if (e.type === 3 || e.type === 8)
|
|
38
|
+
out.push(r.u16(o));
|
|
39
|
+
else if (e.type === 4 || e.type === 9)
|
|
40
|
+
out.push(r.u32(o));
|
|
41
|
+
else if (e.type === 5)
|
|
42
|
+
out.push(r.u32(o) / Math.max(1, r.u32(o + 4)));
|
|
43
|
+
else
|
|
44
|
+
out.push(r.b[o]);
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
function fail(msg) {
|
|
49
|
+
throw new Error(msg);
|
|
50
|
+
}
|
|
51
|
+
/** Decodes the first image of a TIFF file. */
|
|
52
|
+
function readTiff(b) {
|
|
53
|
+
const le = b[0] === 0x49;
|
|
54
|
+
const r = new Reader(b, le);
|
|
55
|
+
if (r.u16(2) !== 42)
|
|
56
|
+
fail('not a classic TIFF');
|
|
57
|
+
const ifd = r.u32(4);
|
|
58
|
+
const n = r.u16(ifd);
|
|
59
|
+
const tags = new Map();
|
|
60
|
+
for (let i = 0; i < n; i++) {
|
|
61
|
+
const o = ifd + 2 + i * 12;
|
|
62
|
+
const type = r.u16(o + 2);
|
|
63
|
+
const count = r.u32(o + 4);
|
|
64
|
+
const bytes = (TYPE_SIZE[type] ?? 1) * count;
|
|
65
|
+
tags.set(r.u16(o), { type, count, at: bytes <= 4 ? o + 8 : r.u32(o + 8) });
|
|
66
|
+
}
|
|
67
|
+
const one = (tag, d) => values(r, tags.get(tag))[0] ?? d;
|
|
68
|
+
const width = one(256, 0);
|
|
69
|
+
const height = one(257, 0);
|
|
70
|
+
if (width <= 0 || height <= 0 || width * height > limits_1.MAX_PIXELS)
|
|
71
|
+
fail('bad TIFF image size');
|
|
72
|
+
const spp = one(277, 1);
|
|
73
|
+
if (!Number.isInteger(spp) || spp < 1 || spp > MAX_SAMPLES_PER_PIXEL)
|
|
74
|
+
fail(`TIFF with ${spp} samples per pixel`);
|
|
75
|
+
if (width * height * spp > 4 * limits_1.MAX_PIXELS)
|
|
76
|
+
fail('bad TIFF image size');
|
|
77
|
+
const bpsList = values(r, tags.get(258));
|
|
78
|
+
const bps = bpsList[0] ?? 1;
|
|
79
|
+
if (bpsList.some(v => v !== bps))
|
|
80
|
+
fail('mixed TIFF sample sizes');
|
|
81
|
+
if (![1, 2, 4, 8, 16].includes(bps))
|
|
82
|
+
fail(`TIFF samples of ${bps} bits`);
|
|
83
|
+
const compression = one(259, 1);
|
|
84
|
+
const photometric = one(262, spp >= 3 ? 2 : 1);
|
|
85
|
+
const planar = one(284, 1);
|
|
86
|
+
const predictor = one(317, 1);
|
|
87
|
+
const sampleFormat = one(339, 1);
|
|
88
|
+
if (sampleFormat === 3)
|
|
89
|
+
fail('floating point TIFF samples');
|
|
90
|
+
if (![0, 1, 2, 3, 5].includes(photometric))
|
|
91
|
+
fail(`TIFF photometric interpretation ${photometric}`);
|
|
92
|
+
// `limit` is the size of a full strip or tile; data that decodes past it is refused before it is allocated.
|
|
93
|
+
const decompress = (data, limit) => {
|
|
94
|
+
try {
|
|
95
|
+
switch (compression) {
|
|
96
|
+
case 1:
|
|
97
|
+
return data;
|
|
98
|
+
case 5:
|
|
99
|
+
if (data[0] === 0 && (data[1] & 1) === 1)
|
|
100
|
+
fail('old-style TIFF LZW');
|
|
101
|
+
return (0, filters_1.lzwDecode)(data, 1, limit);
|
|
102
|
+
case 8:
|
|
103
|
+
case 32946:
|
|
104
|
+
return (0, filters_1.inflate)(data, limit);
|
|
105
|
+
case 32773:
|
|
106
|
+
return (0, filters_1.runLengthDecode)(data, limit);
|
|
107
|
+
default:
|
|
108
|
+
return fail(`TIFF compression ${compression}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
if (e instanceof filters_1.OutputLimitError)
|
|
113
|
+
fail('TIFF strip or tile is larger than its declared size');
|
|
114
|
+
throw e;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const tiled = tags.has(322);
|
|
118
|
+
const tw = tiled ? one(322, width) : width;
|
|
119
|
+
const th = tiled ? one(323, height) : Math.min(one(278, height), height);
|
|
120
|
+
if (!(tw >= 1 && th >= 1) || tw * th > limits_1.MAX_PIXELS)
|
|
121
|
+
fail('bad TIFF strip or tile size');
|
|
122
|
+
const offsets = values(r, tags.get(tiled ? 324 : 273));
|
|
123
|
+
const counts = values(r, tags.get(tiled ? 325 : 279));
|
|
124
|
+
if (offsets.length === 0)
|
|
125
|
+
fail('TIFF without image data');
|
|
126
|
+
const planes = planar === 2 ? spp : 1;
|
|
127
|
+
const sppPerPlane = planar === 2 ? 1 : spp;
|
|
128
|
+
const rowBytes = (cols) => Math.ceil((cols * sppPerPlane * bps) / 8);
|
|
129
|
+
const across = Math.ceil(width / tw);
|
|
130
|
+
const down = Math.ceil(height / th);
|
|
131
|
+
// Full planes at the file's bit depth, row-padded to bytes.
|
|
132
|
+
const planeRow = rowBytes(width);
|
|
133
|
+
const full = Array.from({ length: planes }, () => new Uint8Array(planeRow * height));
|
|
134
|
+
const perPlane = across * down;
|
|
135
|
+
for (let p = 0; p < planes; p++) {
|
|
136
|
+
for (let t = 0; t < perPlane; t++) {
|
|
137
|
+
const k = p * perPlane + t;
|
|
138
|
+
const off = offsets[k];
|
|
139
|
+
if (off === undefined)
|
|
140
|
+
continue;
|
|
141
|
+
const cnt = counts[k] ?? b.length - off;
|
|
142
|
+
const tileRow = rowBytes(tw);
|
|
143
|
+
let data = decompress(b.subarray(off, Math.min(b.length, off + cnt)), tileRow * th);
|
|
144
|
+
const tx = (t % across) * tw;
|
|
145
|
+
const ty = Math.floor(t / across) * th;
|
|
146
|
+
const rows = tiled ? th : Math.min(th, height - ty);
|
|
147
|
+
if (predictor === 2)
|
|
148
|
+
data = undoPredictor(new Uint8Array(data), tw, sppPerPlane, bps, le, rows);
|
|
149
|
+
for (let y = 0; y < rows && ty + y < height; y++) {
|
|
150
|
+
const src = y * tileRow;
|
|
151
|
+
if (src >= data.length)
|
|
152
|
+
break;
|
|
153
|
+
if (!tiled) {
|
|
154
|
+
full[p].set(data.subarray(src, Math.min(data.length, src + planeRow)), (ty + y) * planeRow);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const cols = Math.min(tw, width - tx);
|
|
158
|
+
copyBits(data, src, full[p], (ty + y) * planeRow, (tx * sppPerPlane * bps) | 0, cols * sppPerPlane * bps);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// Expand to 8 bits per sample, chunky.
|
|
163
|
+
const samples = new Uint8Array(width * height * spp);
|
|
164
|
+
const maxV = (1 << Math.min(bps, 8)) - 1;
|
|
165
|
+
for (let p = 0; p < planes; p++) {
|
|
166
|
+
const src = full[p];
|
|
167
|
+
for (let y = 0; y < height; y++) {
|
|
168
|
+
for (let x = 0; x < width; x++) {
|
|
169
|
+
for (let s = 0; s < sppPerPlane; s++) {
|
|
170
|
+
const idx = x * sppPerPlane + s;
|
|
171
|
+
let v;
|
|
172
|
+
if (bps === 16) {
|
|
173
|
+
const o = y * planeRow + idx * 2;
|
|
174
|
+
v = le ? src[o + 1] : src[o];
|
|
175
|
+
}
|
|
176
|
+
else if (bps === 8)
|
|
177
|
+
v = src[y * planeRow + idx];
|
|
178
|
+
else {
|
|
179
|
+
const bit = idx * bps;
|
|
180
|
+
const byte = src[y * planeRow + (bit >> 3)];
|
|
181
|
+
const raw = (byte >> (8 - bps - (bit & 7))) & maxV;
|
|
182
|
+
v = photometric === 3 ? raw : Math.round((raw * 255) / maxV);
|
|
183
|
+
}
|
|
184
|
+
samples[(y * width + x) * spp + (planar === 2 ? p : s)] = v;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const extra = values(r, tags.get(338));
|
|
190
|
+
const colorCount = photometric === 2 ? 3 : photometric === 5 ? 4 : 1;
|
|
191
|
+
const hasAlpha = spp > colorCount && extra.length > 0 && (extra[0] === 1 || extra[0] === 2);
|
|
192
|
+
const px = width * height;
|
|
193
|
+
let alpha;
|
|
194
|
+
if (hasAlpha) {
|
|
195
|
+
alpha = new Uint8Array(px);
|
|
196
|
+
for (let i = 0; i < px; i++)
|
|
197
|
+
alpha[i] = samples[i * spp + colorCount];
|
|
198
|
+
}
|
|
199
|
+
if (photometric === 3) {
|
|
200
|
+
const map = values(r, tags.get(320));
|
|
201
|
+
const entries = 1 << bps;
|
|
202
|
+
if (map.length < entries * 3)
|
|
203
|
+
fail('TIFF palette is short');
|
|
204
|
+
const out = new Uint8Array(px * 3);
|
|
205
|
+
for (let i = 0; i < px; i++) {
|
|
206
|
+
const k = bps === 16 ? 0 : samples[i * spp];
|
|
207
|
+
out[i * 3] = map[k] >> 8;
|
|
208
|
+
out[i * 3 + 1] = map[entries + k] >> 8;
|
|
209
|
+
out[i * 3 + 2] = map[2 * entries + k] >> 8;
|
|
210
|
+
}
|
|
211
|
+
return { width, height, colorSpace: 'DeviceRGB', data: out, dct: false, alpha };
|
|
212
|
+
}
|
|
213
|
+
if (spp < colorCount)
|
|
214
|
+
fail('TIFF has fewer samples than its colors need');
|
|
215
|
+
const out = new Uint8Array(px * colorCount);
|
|
216
|
+
for (let i = 0; i < px; i++) {
|
|
217
|
+
for (let c = 0; c < colorCount; c++) {
|
|
218
|
+
const v = samples[i * spp + c];
|
|
219
|
+
out[i * colorCount + c] = photometric === 0 ? 255 - v : v;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const colorSpace = colorCount === 4 ? 'DeviceCMYK' : colorCount === 3 ? 'DeviceRGB' : 'DeviceGray';
|
|
223
|
+
return { width, height, colorSpace, data: out, dct: false, alpha };
|
|
224
|
+
}
|
|
225
|
+
function undoPredictor(data, cols, spp, bps, le, rows) {
|
|
226
|
+
if (bps !== 8 && bps !== 16)
|
|
227
|
+
return data;
|
|
228
|
+
const rowLen = cols * spp * (bps / 8);
|
|
229
|
+
for (let y = 0; y < rows; y++) {
|
|
230
|
+
const o = y * rowLen;
|
|
231
|
+
if (o + rowLen > data.length)
|
|
232
|
+
break;
|
|
233
|
+
if (bps === 8) {
|
|
234
|
+
for (let i = spp; i < rowLen; i++)
|
|
235
|
+
data[o + i] = (data[o + i] + data[o + i - spp]) & 255;
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
const rd = (k) => (le ? data[k] | (data[k + 1] << 8) : (data[k] << 8) | data[k + 1]);
|
|
239
|
+
for (let i = spp * 2; i < rowLen; i += 2) {
|
|
240
|
+
const v = (rd(o + i) + rd(o + i - spp * 2)) & 0xffff;
|
|
241
|
+
if (le) {
|
|
242
|
+
data[o + i] = v & 255;
|
|
243
|
+
data[o + i + 1] = v >> 8;
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
data[o + i] = v >> 8;
|
|
247
|
+
data[o + i + 1] = v & 255;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return data;
|
|
253
|
+
}
|
|
254
|
+
/** Copies a run of bits from one row to another at a bit offset. */
|
|
255
|
+
function copyBits(src, srcByte, dst, dstByte, dstBit, bits) {
|
|
256
|
+
if (dstBit % 8 === 0 && bits % 8 === 0) {
|
|
257
|
+
dst.set(src.subarray(srcByte, srcByte + bits / 8), dstByte + dstBit / 8);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
for (let i = 0; i < bits; i++) {
|
|
261
|
+
const sb = src[srcByte + (i >> 3)];
|
|
262
|
+
const v = (sb >> (7 - (i & 7))) & 1;
|
|
263
|
+
const d = dstBit + i;
|
|
264
|
+
const k = dstByte + (d >> 3);
|
|
265
|
+
if (v)
|
|
266
|
+
dst[k] |= 0x80 >> (d & 7);
|
|
267
|
+
else
|
|
268
|
+
dst[k] &= ~(0x80 >> (d & 7));
|
|
269
|
+
}
|
|
270
|
+
}
|
package/dist/tree.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { BarcodeSpec, BorderSpec, CheckSpec } from './xfa-spec';
|
|
2
|
+
import type { XmlNode } from './xml';
|
|
3
|
+
export type StyleValue = string | number;
|
|
4
|
+
export interface XNode {
|
|
5
|
+
name: string;
|
|
6
|
+
classes: string[];
|
|
7
|
+
style: Record<string, StyleValue>;
|
|
8
|
+
attrs: Record<string, unknown>;
|
|
9
|
+
children: XNode[];
|
|
10
|
+
value?: string;
|
|
11
|
+
/** The template element this node was laid out from, when the template could be read and matched. */
|
|
12
|
+
tpl?: XmlNode;
|
|
13
|
+
/** The template border to draw instead of the CSS border. */
|
|
14
|
+
xborder?: BorderSpec;
|
|
15
|
+
/** The check button's shape, mark and state. */
|
|
16
|
+
check?: CheckSpec;
|
|
17
|
+
/** A barcode to draw in this box. */
|
|
18
|
+
barcode?: BarcodeSpec;
|
|
19
|
+
}
|
|
20
|
+
/** Converts pdf.js's untyped tree into XNode, dropping anything that does not have the expected shape. */
|
|
21
|
+
export declare function toTree(raw: unknown): XNode | null;
|
|
22
|
+
export declare function attrString(node: XNode, key: string): string | undefined;
|
|
23
|
+
export declare function attrFlag(node: XNode, key: string): boolean;
|
package/dist/tree.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// The node tree pdf.js returns from allXfaHtml, checked and typed.
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.toTree = toTree;
|
|
5
|
+
exports.attrString = attrString;
|
|
6
|
+
exports.attrFlag = attrFlag;
|
|
7
|
+
function isRecord(v) {
|
|
8
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
9
|
+
}
|
|
10
|
+
/** Converts pdf.js's untyped tree into XNode, dropping anything that does not have the expected shape. */
|
|
11
|
+
function toTree(raw) {
|
|
12
|
+
if (!isRecord(raw) || typeof raw.name !== 'string')
|
|
13
|
+
return null;
|
|
14
|
+
const attrsIn = isRecord(raw.attributes) ? raw.attributes : {};
|
|
15
|
+
const classes = Array.isArray(attrsIn.class) ? attrsIn.class.filter((c) => typeof c === 'string') : [];
|
|
16
|
+
const style = {};
|
|
17
|
+
if (isRecord(attrsIn.style)) {
|
|
18
|
+
for (const [k, v] of Object.entries(attrsIn.style)) {
|
|
19
|
+
if (typeof v === 'string' || typeof v === 'number')
|
|
20
|
+
style[k] = v;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const attrs = {};
|
|
24
|
+
for (const [k, v] of Object.entries(attrsIn)) {
|
|
25
|
+
if (k !== 'class' && k !== 'style')
|
|
26
|
+
attrs[k] = v;
|
|
27
|
+
}
|
|
28
|
+
const children = [];
|
|
29
|
+
if (Array.isArray(raw.children)) {
|
|
30
|
+
for (const c of raw.children) {
|
|
31
|
+
const t = toTree(c);
|
|
32
|
+
if (t)
|
|
33
|
+
children.push(t);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const node = { name: raw.name, classes, style, attrs, children };
|
|
37
|
+
if (typeof raw.value === 'string')
|
|
38
|
+
node.value = raw.value;
|
|
39
|
+
else if (typeof raw.value === 'number')
|
|
40
|
+
node.value = String(raw.value);
|
|
41
|
+
return node;
|
|
42
|
+
}
|
|
43
|
+
function attrString(node, key) {
|
|
44
|
+
const v = node.attrs[key];
|
|
45
|
+
if (typeof v === 'string')
|
|
46
|
+
return v;
|
|
47
|
+
if (typeof v === 'number')
|
|
48
|
+
return String(v);
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
function attrFlag(node, key) {
|
|
52
|
+
const v = node.attrs[key];
|
|
53
|
+
return v === true || v === 'true' || v === '';
|
|
54
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Options for flattenXfa. */
|
|
2
|
+
export interface FlattenOptions {
|
|
3
|
+
/** Password to open an encrypted PDF, or several to try in order. */
|
|
4
|
+
password?: string | string[];
|
|
5
|
+
/**
|
|
6
|
+
* A Date.now() value; there is no default. The reader, the XML parser, image decoding, template application, layout
|
|
7
|
+
* and painting check it, and the run returns status 'failed' with a 'deadline' note soon after it passes. pdf.js's
|
|
8
|
+
* own parsing and layout cannot be interrupted, so a hard bound needs a worker thread with resourceLimits.
|
|
9
|
+
*/
|
|
10
|
+
deadline?: number;
|
|
11
|
+
}
|
|
12
|
+
/** Something that was not drawn or not done, and why. */
|
|
13
|
+
export interface FlattenNote {
|
|
14
|
+
/** What kind of gap this is, for example 'unencodable-text' or 'image-format'. */
|
|
15
|
+
kind: string;
|
|
16
|
+
/** Where it is, for example 'page 2' or 'document'. */
|
|
17
|
+
location: string;
|
|
18
|
+
/** A sentence describing it. */
|
|
19
|
+
detail: string;
|
|
20
|
+
}
|
|
21
|
+
/** The result of flattenXfa. */
|
|
22
|
+
export interface FlattenResult {
|
|
23
|
+
/** 'flattened' when bytes holds a new PDF, 'unchanged' when there was nothing to do, 'failed' when the form could not be laid out. */
|
|
24
|
+
status: 'flattened' | 'unchanged' | 'failed';
|
|
25
|
+
/** The flattened PDF, set when status is 'flattened'. */
|
|
26
|
+
bytes?: Uint8Array;
|
|
27
|
+
/** The number of pages in the flattened PDF. */
|
|
28
|
+
pages?: number;
|
|
29
|
+
/** What was not drawn or done, and why. */
|
|
30
|
+
notes: FlattenNote[];
|
|
31
|
+
}
|
package/dist/types.js
ADDED
package/dist/writer.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export declare class Ref {
|
|
2
|
+
readonly num: number;
|
|
3
|
+
constructor(num: number);
|
|
4
|
+
}
|
|
5
|
+
export declare class Name {
|
|
6
|
+
readonly name: string;
|
|
7
|
+
constructor(name: string);
|
|
8
|
+
}
|
|
9
|
+
export declare class Raw {
|
|
10
|
+
readonly text: string;
|
|
11
|
+
constructor(text: string);
|
|
12
|
+
}
|
|
13
|
+
export type PdfValue = number | boolean | string | null | Ref | Name | Raw | PdfValue[] | {
|
|
14
|
+
[key: string]: PdfValue;
|
|
15
|
+
};
|
|
16
|
+
export interface StreamObject {
|
|
17
|
+
dict: {
|
|
18
|
+
[key: string]: PdfValue;
|
|
19
|
+
};
|
|
20
|
+
data: Uint8Array;
|
|
21
|
+
}
|
|
22
|
+
declare function formatNumber(n: number): string;
|
|
23
|
+
declare function formatName(name: string): string;
|
|
24
|
+
export declare function formatValue(v: PdfValue): string;
|
|
25
|
+
export { formatName, formatNumber };
|
|
26
|
+
export declare class PdfWriter {
|
|
27
|
+
private readonly objects;
|
|
28
|
+
/** Reserves an object number to fill in later. */
|
|
29
|
+
reserve(): Ref;
|
|
30
|
+
set(ref: Ref, value: PdfValue | StreamObject): void;
|
|
31
|
+
add(value: PdfValue | StreamObject): Ref;
|
|
32
|
+
/** Adds a stream, compressing it with Flate unless the dictionary already names a filter. */
|
|
33
|
+
addStream(dict: {
|
|
34
|
+
[key: string]: PdfValue;
|
|
35
|
+
}, data: Uint8Array, compress?: boolean): Ref;
|
|
36
|
+
/** Serializes every object with the given catalog as the root. */
|
|
37
|
+
finish(root: Ref, info?: Ref): Uint8Array;
|
|
38
|
+
}
|
package/dist/writer.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// A small writer for a new single-revision PDF: indirect objects, streams compressed with Flate, a cross-reference
|
|
3
|
+
// table and a trailer. It writes only what the flattener needs.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.PdfWriter = exports.Raw = exports.Name = exports.Ref = void 0;
|
|
6
|
+
exports.formatValue = formatValue;
|
|
7
|
+
exports.formatName = formatName;
|
|
8
|
+
exports.formatNumber = formatNumber;
|
|
9
|
+
const node_crypto_1 = require("node:crypto");
|
|
10
|
+
const node_zlib_1 = require("node:zlib");
|
|
11
|
+
class Ref {
|
|
12
|
+
num;
|
|
13
|
+
constructor(num) {
|
|
14
|
+
this.num = num;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.Ref = Ref;
|
|
18
|
+
class Name {
|
|
19
|
+
name;
|
|
20
|
+
constructor(name) {
|
|
21
|
+
this.name = name;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.Name = Name;
|
|
25
|
+
class Raw {
|
|
26
|
+
text;
|
|
27
|
+
constructor(text) {
|
|
28
|
+
this.text = text;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.Raw = Raw;
|
|
32
|
+
function formatNumber(n) {
|
|
33
|
+
if (!Number.isFinite(n))
|
|
34
|
+
return '0';
|
|
35
|
+
if (Number.isInteger(n))
|
|
36
|
+
return String(n);
|
|
37
|
+
const s = n.toFixed(4).replace(/0+$/, '').replace(/\.$/, '');
|
|
38
|
+
return s === '-0' ? '0' : s;
|
|
39
|
+
}
|
|
40
|
+
function formatName(name) {
|
|
41
|
+
let out = '/';
|
|
42
|
+
for (const ch of name) {
|
|
43
|
+
const c = ch.charCodeAt(0);
|
|
44
|
+
if (c < 0x21 || c > 0x7e || '#()<>[]{}/%'.includes(ch))
|
|
45
|
+
out += `#${c.toString(16).padStart(2, '0')}`;
|
|
46
|
+
else
|
|
47
|
+
out += ch;
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
/** Writes a text string as a PDF literal string, with ASCII kept and everything else as UTF-16BE hex. */
|
|
52
|
+
function formatString(s) {
|
|
53
|
+
if (/^[\x20-\x7e]*$/.test(s))
|
|
54
|
+
return `(${s.replace(/[\\()]/g, m => `\\${m}`)})`;
|
|
55
|
+
let hex = 'FEFF';
|
|
56
|
+
for (let i = 0; i < s.length; i++)
|
|
57
|
+
hex += s.charCodeAt(i).toString(16).padStart(4, '0');
|
|
58
|
+
return `<${hex}>`;
|
|
59
|
+
}
|
|
60
|
+
function formatValue(v) {
|
|
61
|
+
if (v === null)
|
|
62
|
+
return 'null';
|
|
63
|
+
if (typeof v === 'number')
|
|
64
|
+
return formatNumber(v);
|
|
65
|
+
if (typeof v === 'boolean')
|
|
66
|
+
return v ? 'true' : 'false';
|
|
67
|
+
if (typeof v === 'string')
|
|
68
|
+
return formatString(v);
|
|
69
|
+
if (v instanceof Ref)
|
|
70
|
+
return `${v.num} 0 R`;
|
|
71
|
+
if (v instanceof Name)
|
|
72
|
+
return formatName(v.name);
|
|
73
|
+
if (v instanceof Raw)
|
|
74
|
+
return v.text;
|
|
75
|
+
if (Array.isArray(v))
|
|
76
|
+
return `[${v.map(formatValue).join(' ')}]`;
|
|
77
|
+
const parts = Object.entries(v).map(([k, val]) => `${formatName(k)} ${formatValue(val)}`);
|
|
78
|
+
return `<<${parts.join(' ')}>>`;
|
|
79
|
+
}
|
|
80
|
+
class PdfWriter {
|
|
81
|
+
objects = [];
|
|
82
|
+
/** Reserves an object number to fill in later. */
|
|
83
|
+
reserve() {
|
|
84
|
+
this.objects.push(undefined);
|
|
85
|
+
return new Ref(this.objects.length);
|
|
86
|
+
}
|
|
87
|
+
set(ref, value) {
|
|
88
|
+
this.objects[ref.num - 1] = value;
|
|
89
|
+
}
|
|
90
|
+
add(value) {
|
|
91
|
+
this.objects.push(value);
|
|
92
|
+
return new Ref(this.objects.length);
|
|
93
|
+
}
|
|
94
|
+
/** Adds a stream, compressing it with Flate unless the dictionary already names a filter. */
|
|
95
|
+
addStream(dict, data, compress = true) {
|
|
96
|
+
if (compress && dict.Filter === undefined) {
|
|
97
|
+
return this.add({ dict: { ...dict, Filter: new Name('FlateDecode') }, data: (0, node_zlib_1.deflateSync)(data) });
|
|
98
|
+
}
|
|
99
|
+
return this.add({ dict, data });
|
|
100
|
+
}
|
|
101
|
+
/** Serializes every object with the given catalog as the root. */
|
|
102
|
+
finish(root, info) {
|
|
103
|
+
const chunks = [];
|
|
104
|
+
let offset = 0;
|
|
105
|
+
const push = (b) => {
|
|
106
|
+
chunks.push(b);
|
|
107
|
+
offset += b.length;
|
|
108
|
+
};
|
|
109
|
+
push(Buffer.from('%PDF-1.7\n%\xe2\xe3\xcf\xd3\n', 'latin1'));
|
|
110
|
+
const offsets = [];
|
|
111
|
+
this.objects.forEach((obj, i) => {
|
|
112
|
+
offsets.push(offset);
|
|
113
|
+
const num = i + 1;
|
|
114
|
+
if (obj === undefined) {
|
|
115
|
+
push(Buffer.from(`${num} 0 obj\nnull\nendobj\n`, 'latin1'));
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (isStream(obj)) {
|
|
119
|
+
const dict = { ...obj.dict, Length: obj.data.length };
|
|
120
|
+
push(Buffer.from(`${num} 0 obj\n${formatValue(dict)}\nstream\n`, 'latin1'));
|
|
121
|
+
push(Buffer.from(obj.data));
|
|
122
|
+
push(Buffer.from('\nendstream\nendobj\n', 'latin1'));
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
push(Buffer.from(`${num} 0 obj\n${formatValue(obj)}\nendobj\n`, 'latin1'));
|
|
126
|
+
});
|
|
127
|
+
const xrefAt = offset;
|
|
128
|
+
let xref = `xref\n0 ${this.objects.length + 1}\n0000000000 65535 f \n`;
|
|
129
|
+
for (const o of offsets)
|
|
130
|
+
xref += `${String(o).padStart(10, '0')} 00000 n \n`;
|
|
131
|
+
const hash = (0, node_crypto_1.createHash)('md5');
|
|
132
|
+
for (const c of chunks)
|
|
133
|
+
hash.update(c);
|
|
134
|
+
const id = hash.digest('hex');
|
|
135
|
+
const trailer = { Size: this.objects.length + 1, Root: root, ID: [new Raw(`<${id}>`), new Raw(`<${id}>`)] };
|
|
136
|
+
if (info)
|
|
137
|
+
trailer.Info = info;
|
|
138
|
+
push(Buffer.from(`${xref}trailer\n${formatValue(trailer)}\nstartxref\n${xrefAt}\n%%EOF\n`, 'latin1'));
|
|
139
|
+
return new Uint8Array(Buffer.concat(chunks));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.PdfWriter = PdfWriter;
|
|
143
|
+
function isStream(v) {
|
|
144
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v) && !(v instanceof Ref) && !(v instanceof Name) && !(v instanceof Raw) && 'data' in v && v.data instanceof Uint8Array;
|
|
145
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type XNode } from './tree';
|
|
2
|
+
import { type XfaModel } from './xfa-model';
|
|
3
|
+
import type { BorderSpec } from './xfa-spec';
|
|
4
|
+
import { type XmlNode } from './xml';
|
|
5
|
+
/** One bound field value, for the corpus coverage measure. */
|
|
6
|
+
export interface FieldValueStat {
|
|
7
|
+
/** The data element's position in the datasets, when it was found. */
|
|
8
|
+
dataIndex: number | null;
|
|
9
|
+
raw: string;
|
|
10
|
+
/** The text drawn, or the check state for a check button. */
|
|
11
|
+
shown: string;
|
|
12
|
+
/** 'radio' is an exclusion group's value: shown is the state of the button whose on value it is, or 'none'. */
|
|
13
|
+
kind: 'text' | 'check' | 'choice' | 'barcode' | 'radio';
|
|
14
|
+
visible: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface ApplyResult {
|
|
17
|
+
mapped: number;
|
|
18
|
+
unmapped: number;
|
|
19
|
+
fields: FieldValueStat[];
|
|
20
|
+
}
|
|
21
|
+
export declare function measure(s: string | undefined, fallback: number): number;
|
|
22
|
+
/** Builds the four edges and four corners of a border element, repeating the last given as XFA does. */
|
|
23
|
+
export declare function borderSpec(model: XfaModel, border: XmlNode, mode: 'css' | 'nominal'): BorderSpec | null;
|
|
24
|
+
/**
|
|
25
|
+
* Keeps the digits, minus signs and the locale's decimal symbol of a number as written, with the symbol turned into
|
|
26
|
+
* '.'. The symbol comes from the form's localeSet, so it is escaped before it goes into the character class.
|
|
27
|
+
*/
|
|
28
|
+
export declare function stripNumber(raw: string, decimal: string): string;
|
|
29
|
+
/** Options for applyTemplate: the caller's deadline, and where a field that could not be applied is noted. */
|
|
30
|
+
export interface ApplyOptions {
|
|
31
|
+
deadline?: number;
|
|
32
|
+
note?(kind: string, location: string, detail: string): void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Applies the template to pdf.js's tree in place. A field whose template or data makes applying it throw keeps what
|
|
36
|
+
* pdf.js drew, and is noted; only the deadline stops the whole pass.
|
|
37
|
+
*/
|
|
38
|
+
export declare function applyTemplate(model: XfaModel, root: XNode, options?: ApplyOptions): ApplyResult;
|
|
39
|
+
/** Images the template names only by a path to a file that the PDF does not carry; pdf.js leaves them out. */
|
|
40
|
+
export declare function hrefImages(model: XfaModel): {
|
|
41
|
+
location: string;
|
|
42
|
+
href: string;
|
|
43
|
+
}[];
|
|
44
|
+
export interface ScriptNote {
|
|
45
|
+
kind: string;
|
|
46
|
+
location: string;
|
|
47
|
+
}
|
|
48
|
+
/** Lists template scripts that would run at open, by event kind and the SOM location of their container. */
|
|
49
|
+
export declare function openScripts(model: XfaModel): ScriptNote[];
|