@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
package/dist/images.js ADDED
@@ -0,0 +1,519 @@
1
+ "use strict";
2
+ // Turns the image bytes in an XFA form into PDF image data. JPEG passes through as DCTDecode. PNG, GIF and BMP
3
+ // and TIFF are decoded to 8-bit samples that the writer compresses with Flate; transparency becomes a soft mask.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.sniffImage = sniffImage;
6
+ exports.decodeImage = decodeImage;
7
+ const filters_1 = require("./filters");
8
+ const limits_1 = require("./limits");
9
+ const tiff_1 = require("./tiff");
10
+ function sniffImage(b) {
11
+ if (b.length >= 3 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff)
12
+ return 'jpeg';
13
+ if (b.length >= 8 && b[0] === 0x89 && b[1] === 0x50 && b[2] === 0x4e && b[3] === 0x47)
14
+ return 'png';
15
+ if (b.length >= 6 && b[0] === 0x47 && b[1] === 0x49 && b[2] === 0x46 && b[3] === 0x38)
16
+ return 'gif';
17
+ if (b.length >= 2 && b[0] === 0x42 && b[1] === 0x4d)
18
+ return 'bmp';
19
+ if (b.length >= 4 && ((b[0] === 0x49 && b[1] === 0x49 && b[2] === 0x2a) || (b[0] === 0x4d && b[1] === 0x4d && b[3] === 0x2a)))
20
+ return 'tiff';
21
+ return 'unknown';
22
+ }
23
+ function decodeImage(bytes) {
24
+ const format = sniffImage(bytes);
25
+ try {
26
+ switch (format) {
27
+ case 'jpeg':
28
+ return { ok: true, image: readJpeg(bytes) };
29
+ case 'png':
30
+ return { ok: true, image: readPng(bytes) };
31
+ case 'gif':
32
+ return { ok: true, image: readGif(bytes) };
33
+ case 'bmp':
34
+ return { ok: true, image: readBmp(bytes) };
35
+ case 'tiff':
36
+ return { ok: true, image: (0, tiff_1.readTiff)(bytes) };
37
+ default:
38
+ return { ok: false, format, reason: 'the image format is not recognized' };
39
+ }
40
+ }
41
+ catch (e) {
42
+ return { ok: false, format, reason: `the image could not be decoded: ${e instanceof Error ? e.message : String(e)}` };
43
+ }
44
+ }
45
+ function fail(msg) {
46
+ throw new Error(msg);
47
+ }
48
+ function readJpeg(b) {
49
+ let i = 2;
50
+ let adobe = false;
51
+ while (i + 4 <= b.length) {
52
+ if (b[i] !== 0xff) {
53
+ i++;
54
+ continue;
55
+ }
56
+ const marker = b[i + 1];
57
+ if (marker === 0xff) {
58
+ i++;
59
+ continue;
60
+ }
61
+ if (marker === 0xd8 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) {
62
+ i += 2;
63
+ continue;
64
+ }
65
+ const len = (b[i + 2] << 8) | b[i + 3];
66
+ if (marker === 0xee && len >= 12 && String.fromCharCode(b[i + 4], b[i + 5], b[i + 6], b[i + 7], b[i + 8]) === 'Adobe')
67
+ adobe = true;
68
+ if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) {
69
+ const height = (b[i + 5] << 8) | b[i + 6];
70
+ const width = (b[i + 7] << 8) | b[i + 8];
71
+ const comps = b[i + 9];
72
+ if (!width || !height)
73
+ fail('JPEG has no size');
74
+ const colorSpace = comps === 1 ? 'DeviceGray' : comps === 4 ? 'DeviceCMYK' : 'DeviceRGB';
75
+ const img = { width, height, colorSpace, data: b, dct: true };
76
+ if (comps === 4 && adobe)
77
+ img.decode = [1, 0, 1, 0, 1, 0, 1, 0];
78
+ return img;
79
+ }
80
+ i += 2 + len;
81
+ }
82
+ return fail('JPEG has no frame header');
83
+ }
84
+ function readU32(b, i) {
85
+ return ((b[i] << 24) | (b[i + 1] << 16) | (b[i + 2] << 8) | b[i + 3]) >>> 0;
86
+ }
87
+ function paeth(a, b, c) {
88
+ const p = a + b - c;
89
+ const pa = Math.abs(p - a);
90
+ const pb = Math.abs(p - b);
91
+ const pc = Math.abs(p - c);
92
+ if (pa <= pb && pa <= pc)
93
+ return a;
94
+ return pb <= pc ? b : c;
95
+ }
96
+ /** Undoes PNG row filters in place for one pass; returns the unfiltered rows. */
97
+ function unfilter(data, offset, rows, rowBytes, bpp) {
98
+ const out = new Uint8Array(rows * rowBytes);
99
+ let p = offset;
100
+ for (let y = 0; y < rows; y++) {
101
+ const type = data[p++];
102
+ const row = y * rowBytes;
103
+ const prev = row - rowBytes;
104
+ for (let x = 0; x < rowBytes; x++) {
105
+ const raw = data[p++] ?? 0;
106
+ const a = x >= bpp ? out[row + x - bpp] : 0;
107
+ const up = y > 0 ? out[prev + x] : 0;
108
+ const c = x >= bpp && y > 0 ? out[prev + x - bpp] : 0;
109
+ let v = raw;
110
+ switch (type) {
111
+ case 1:
112
+ v = raw + a;
113
+ break;
114
+ case 2:
115
+ v = raw + up;
116
+ break;
117
+ case 3:
118
+ v = raw + ((a + up) >> 1);
119
+ break;
120
+ case 4:
121
+ v = raw + paeth(a, up, c);
122
+ break;
123
+ default:
124
+ break;
125
+ }
126
+ out[row + x] = v & 0xff;
127
+ }
128
+ }
129
+ return { rows: out, next: p };
130
+ }
131
+ /** Adam7 passes: x0, y0, dx, dy. */
132
+ const ADAM7 = [
133
+ [0, 0, 8, 8],
134
+ [4, 0, 8, 8],
135
+ [0, 4, 4, 8],
136
+ [2, 0, 4, 4],
137
+ [0, 2, 2, 4],
138
+ [1, 0, 2, 2],
139
+ [0, 1, 1, 2],
140
+ ];
141
+ function readPng(b) {
142
+ let i = 8;
143
+ let width = 0;
144
+ let height = 0;
145
+ let depth = 8;
146
+ let colorType = 0;
147
+ let interlace = 0;
148
+ let palette = null;
149
+ let trns = null;
150
+ const idat = [];
151
+ while (i + 8 <= b.length) {
152
+ const len = readU32(b, i);
153
+ const type = String.fromCharCode(b[i + 4], b[i + 5], b[i + 6], b[i + 7]);
154
+ const body = b.subarray(i + 8, i + 8 + len);
155
+ if (type === 'IHDR') {
156
+ width = readU32(body, 0);
157
+ height = readU32(body, 4);
158
+ depth = body[8];
159
+ colorType = body[9];
160
+ interlace = body[12];
161
+ }
162
+ else if (type === 'PLTE')
163
+ palette = body;
164
+ else if (type === 'tRNS')
165
+ trns = body;
166
+ else if (type === 'IDAT')
167
+ idat.push(body);
168
+ else if (type === 'IEND')
169
+ break;
170
+ i += 12 + len;
171
+ }
172
+ if (!width || !height || width * height > limits_1.MAX_PIXELS)
173
+ fail('PNG size is not usable');
174
+ const channels = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 }[colorType];
175
+ if (channels === undefined)
176
+ fail(`PNG color type ${colorType}`);
177
+ if (![1, 2, 4, 8, 16].includes(depth))
178
+ fail(`PNG bit depth ${depth}`);
179
+ const bitsPerPixel = channels * depth;
180
+ // The filtered rows of the declared size are all the image uses; zlib data that inflates past them is refused
181
+ // before it is allocated.
182
+ let expected = 0;
183
+ if (interlace === 1) {
184
+ for (const [x0, y0, dx, dy] of ADAM7) {
185
+ const pw = Math.ceil((width - x0) / dx);
186
+ const ph = Math.ceil((height - y0) / dy);
187
+ if (pw > 0 && ph > 0)
188
+ expected += ph * (Math.ceil((pw * bitsPerPixel) / 8) + 1);
189
+ }
190
+ }
191
+ else
192
+ expected = height * (Math.ceil((width * bitsPerPixel) / 8) + 1);
193
+ let raw;
194
+ try {
195
+ raw = (0, filters_1.inflate)(Buffer.concat(idat), expected);
196
+ }
197
+ catch (e) {
198
+ if (e instanceof filters_1.OutputLimitError)
199
+ fail('PNG data is larger than its declared size');
200
+ throw e;
201
+ }
202
+ const bpp = Math.max(1, bitsPerPixel >> 3);
203
+ // samples[pixel * channels + c], scaled to 8 bits (palette indexes kept as is).
204
+ const samples = new Uint8Array(width * height * channels);
205
+ const putRow = (rows, rowBytes, y, xs, ry) => {
206
+ const base = ry * rowBytes;
207
+ for (let k = 0; k < xs.length; k++) {
208
+ const x = xs[k];
209
+ for (let c = 0; c < channels; c++) {
210
+ const s = k * channels + c;
211
+ let v;
212
+ if (depth === 8)
213
+ v = rows[base + s];
214
+ else if (depth === 16)
215
+ v = rows[base + s * 2];
216
+ else {
217
+ const bit = s * depth;
218
+ const byte = rows[base + (bit >> 3)];
219
+ const shift = 8 - depth - (bit & 7);
220
+ v = (byte >> shift) & ((1 << depth) - 1);
221
+ if (colorType !== 3)
222
+ v = Math.round((v * 255) / ((1 << depth) - 1));
223
+ }
224
+ samples[(y * width + x) * channels + c] = v;
225
+ }
226
+ }
227
+ };
228
+ if (interlace === 1) {
229
+ let p = 0;
230
+ for (const [x0, y0, dx, dy] of ADAM7) {
231
+ const pw = Math.ceil((width - x0) / dx);
232
+ const ph = Math.ceil((height - y0) / dy);
233
+ if (pw <= 0 || ph <= 0)
234
+ continue;
235
+ const rowBytes = Math.ceil((pw * bitsPerPixel) / 8);
236
+ const r = unfilter(raw, p, ph, rowBytes, bpp);
237
+ p = r.next;
238
+ const xs = [];
239
+ for (let k = 0; k < pw; k++)
240
+ xs.push(x0 + k * dx);
241
+ for (let ry = 0; ry < ph; ry++)
242
+ putRow(r.rows, rowBytes, y0 + ry * dy, xs, ry);
243
+ }
244
+ }
245
+ else {
246
+ const rowBytes = Math.ceil((width * bitsPerPixel) / 8);
247
+ const r = unfilter(raw, 0, height, rowBytes, bpp);
248
+ const xs = [];
249
+ for (let k = 0; k < width; k++)
250
+ xs.push(k);
251
+ for (let y = 0; y < height; y++)
252
+ putRow(r.rows, rowBytes, y, xs, y);
253
+ }
254
+ const n = width * height;
255
+ let alpha;
256
+ if (colorType === 3) {
257
+ if (!palette)
258
+ fail('PNG palette missing');
259
+ const rgb = new Uint8Array(n * 3);
260
+ if (trns)
261
+ alpha = new Uint8Array(n);
262
+ for (let k = 0; k < n; k++) {
263
+ const idx = samples[k];
264
+ rgb[k * 3] = palette[idx * 3] ?? 0;
265
+ rgb[k * 3 + 1] = palette[idx * 3 + 1] ?? 0;
266
+ rgb[k * 3 + 2] = palette[idx * 3 + 2] ?? 0;
267
+ if (alpha && trns)
268
+ alpha[k] = idx < trns.length ? trns[idx] : 255;
269
+ }
270
+ return finishAlpha({ width, height, colorSpace: 'DeviceRGB', data: rgb, dct: false, alpha });
271
+ }
272
+ const colorChannels = colorType === 0 || colorType === 4 ? 1 : 3;
273
+ const color = new Uint8Array(n * colorChannels);
274
+ const hasAlpha = colorType === 4 || colorType === 6;
275
+ if (hasAlpha || trns)
276
+ alpha = new Uint8Array(n).fill(255);
277
+ for (let k = 0; k < n; k++) {
278
+ for (let c = 0; c < colorChannels; c++)
279
+ color[k * colorChannels + c] = samples[k * channels + c];
280
+ if (hasAlpha && alpha)
281
+ alpha[k] = samples[k * channels + colorChannels];
282
+ }
283
+ if (trns && alpha && !hasAlpha) {
284
+ // tRNS for gray or RGB names one transparent color, in the image's own bit depth.
285
+ const scale = depth === 16 ? 1 / 257 : depth < 8 ? 255 / ((1 << depth) - 1) : 1;
286
+ const key = [];
287
+ for (let c = 0; c < colorChannels; c++)
288
+ key.push(Math.round(((trns[c * 2] << 8) | trns[c * 2 + 1]) * scale));
289
+ for (let k = 0; k < n; k++) {
290
+ let match = true;
291
+ for (let c = 0; c < colorChannels; c++)
292
+ if (color[k * colorChannels + c] !== key[c])
293
+ match = false;
294
+ if (match)
295
+ alpha[k] = 0;
296
+ }
297
+ }
298
+ return finishAlpha({ width, height, colorSpace: colorChannels === 1 ? 'DeviceGray' : 'DeviceRGB', data: color, dct: false, alpha });
299
+ }
300
+ /** Drops an alpha channel that is fully opaque. */
301
+ function finishAlpha(img) {
302
+ if (img.alpha?.every(v => v === 255))
303
+ img.alpha = undefined;
304
+ return img;
305
+ }
306
+ function readGif(b) {
307
+ const width = b[6] | (b[7] << 8);
308
+ const height = b[8] | (b[9] << 8);
309
+ if (!width || !height || width * height > limits_1.MAX_PIXELS)
310
+ fail('GIF size is not usable');
311
+ const flags = b[10];
312
+ let i = 13;
313
+ let globalTable = null;
314
+ if (flags & 0x80) {
315
+ const size = 3 * (1 << ((flags & 7) + 1));
316
+ globalTable = b.subarray(i, i + size);
317
+ i += size;
318
+ }
319
+ let transparent = -1;
320
+ while (i < b.length) {
321
+ const block = b[i++];
322
+ if (block === 0x21) {
323
+ const label = b[i++];
324
+ if (label === 0xf9 && b[i] >= 4) {
325
+ if (b[i + 1] & 1)
326
+ transparent = b[i + 4];
327
+ }
328
+ while (i < b.length && b[i] !== 0)
329
+ i += b[i] + 1;
330
+ i++;
331
+ continue;
332
+ }
333
+ if (block !== 0x2c)
334
+ break;
335
+ const fx = b[i] | (b[i + 1] << 8);
336
+ const fy = b[i + 2] | (b[i + 3] << 8);
337
+ const fw = b[i + 4] | (b[i + 5] << 8);
338
+ const fh = b[i + 6] | (b[i + 7] << 8);
339
+ const fflags = b[i + 8];
340
+ i += 9;
341
+ let table = globalTable;
342
+ if (fflags & 0x80) {
343
+ const size = 3 * (1 << ((fflags & 7) + 1));
344
+ table = b.subarray(i, i + size);
345
+ i += size;
346
+ }
347
+ if (!table)
348
+ fail('GIF has no color table');
349
+ if (!fw || !fh || fw * fh > limits_1.MAX_PIXELS)
350
+ fail('GIF frame size is not usable');
351
+ const minCode = b[i++];
352
+ // Codes are at most 12 bits, so the initial table holds at most 2^11 entries.
353
+ if (!(minCode >= 1 && minCode <= 11))
354
+ fail(`GIF LZW minimum code size ${minCode}`);
355
+ const parts = [];
356
+ while (i < b.length && b[i] !== 0) {
357
+ parts.push(b.subarray(i + 1, i + 1 + b[i]));
358
+ i += b[i] + 1;
359
+ }
360
+ const indexes = lzwDecode(Buffer.concat(parts), minCode, fw * fh);
361
+ const interlaced = (fflags & 0x40) !== 0;
362
+ const rowOrder = [];
363
+ if (interlaced) {
364
+ for (const [start, step] of [
365
+ [0, 8],
366
+ [4, 8],
367
+ [2, 4],
368
+ [1, 2],
369
+ ]) {
370
+ for (let y = start; y < fh; y += step)
371
+ rowOrder.push(y);
372
+ }
373
+ }
374
+ else
375
+ for (let y = 0; y < fh; y++)
376
+ rowOrder.push(y);
377
+ const rgb = new Uint8Array(width * height * 3).fill(255);
378
+ const alpha = new Uint8Array(width * height);
379
+ rowOrder.forEach((y, r) => {
380
+ for (let x = 0; x < fw; x++) {
381
+ const idx = indexes[r * fw + x] ?? 0;
382
+ const px = fx + x;
383
+ const py = fy + y;
384
+ if (px >= width || py >= height || idx === transparent)
385
+ continue;
386
+ const k = py * width + px;
387
+ rgb[k * 3] = table[idx * 3] ?? 0;
388
+ rgb[k * 3 + 1] = table[idx * 3 + 1] ?? 0;
389
+ rgb[k * 3 + 2] = table[idx * 3 + 2] ?? 0;
390
+ alpha[k] = 255;
391
+ }
392
+ });
393
+ return finishAlpha({ width, height, colorSpace: 'DeviceRGB', data: rgb, dct: false, alpha });
394
+ }
395
+ return fail('GIF has no image');
396
+ }
397
+ function lzwDecode(data, minCode, count) {
398
+ const out = new Uint8Array(count);
399
+ let outPos = 0;
400
+ const clear = 1 << minCode;
401
+ const end = clear + 1;
402
+ let size = minCode + 1;
403
+ const prefix = new Int32Array(4096);
404
+ const suffix = new Uint8Array(4096);
405
+ for (let k = 0; k < clear; k++)
406
+ suffix[k] = k;
407
+ const stackBuf = new Uint8Array(4097);
408
+ let next = end + 1;
409
+ let old = -1;
410
+ let first = 0;
411
+ let bits = 0;
412
+ let acc = 0;
413
+ let pos = 0;
414
+ // Entries at or past `next` are written before they are read, so a clear code only resets the counters.
415
+ const reset = () => {
416
+ size = minCode + 1;
417
+ next = end + 1;
418
+ old = -1;
419
+ };
420
+ reset();
421
+ while (outPos < count) {
422
+ while (bits < size && pos < data.length) {
423
+ acc |= data[pos++] << bits;
424
+ bits += 8;
425
+ }
426
+ if (bits < size)
427
+ break;
428
+ const code = acc & ((1 << size) - 1);
429
+ acc >>>= size;
430
+ bits -= size;
431
+ if (code === clear) {
432
+ reset();
433
+ continue;
434
+ }
435
+ if (code === end)
436
+ break;
437
+ if (old === -1) {
438
+ out[outPos++] = suffix[code];
439
+ old = code;
440
+ first = suffix[code];
441
+ continue;
442
+ }
443
+ let sp = 0;
444
+ let cur = code;
445
+ if (code >= next) {
446
+ stackBuf[sp++] = first;
447
+ cur = old;
448
+ }
449
+ while (cur >= clear && sp < 4096) {
450
+ stackBuf[sp++] = suffix[cur];
451
+ cur = prefix[cur];
452
+ }
453
+ first = suffix[cur];
454
+ stackBuf[sp++] = first;
455
+ while (sp > 0 && outPos < count)
456
+ out[outPos++] = stackBuf[--sp];
457
+ if (next < 4096) {
458
+ prefix[next] = old;
459
+ suffix[next] = first;
460
+ next++;
461
+ if (next === 1 << size && size < 12)
462
+ size++;
463
+ }
464
+ old = code;
465
+ }
466
+ return out;
467
+ }
468
+ function readBmp(b) {
469
+ const dv = new DataView(b.buffer, b.byteOffset, b.byteLength);
470
+ const dataAt = dv.getUint32(10, true);
471
+ const headerSize = dv.getUint32(14, true);
472
+ const width = dv.getInt32(18, true);
473
+ const rawHeight = dv.getInt32(22, true);
474
+ const bitCount = dv.getUint16(28, true);
475
+ const compression = headerSize >= 40 ? dv.getUint32(30, true) : 0;
476
+ if (compression !== 0 && compression !== 3)
477
+ fail(`BMP compression ${compression}`);
478
+ const height = Math.abs(rawHeight);
479
+ const topDown = rawHeight < 0;
480
+ if (width <= 0 || height === 0 || width * height > limits_1.MAX_PIXELS)
481
+ fail('BMP size is not usable');
482
+ const colors = headerSize >= 40 ? dv.getUint32(46, true) : 0;
483
+ const paletteAt = 14 + headerSize;
484
+ const paletteCount = bitCount <= 8 ? colors || 1 << bitCount : 0;
485
+ const rowBytes = Math.ceil((width * bitCount) / 32) * 4;
486
+ const rgb = new Uint8Array(width * height * 3);
487
+ for (let y = 0; y < height; y++) {
488
+ const srcRow = dataAt + (topDown ? y : height - 1 - y) * rowBytes;
489
+ for (let x = 0; x < width; x++) {
490
+ let r = 0;
491
+ let g = 0;
492
+ let bl = 0;
493
+ if (bitCount === 24 || bitCount === 32) {
494
+ const p = srcRow + x * (bitCount / 8);
495
+ bl = b[p];
496
+ g = b[p + 1];
497
+ r = b[p + 2];
498
+ }
499
+ else if (bitCount <= 8) {
500
+ const bit = x * bitCount;
501
+ const byte = b[srcRow + (bit >> 3)];
502
+ const idx = (byte >> (8 - bitCount - (bit & 7))) & ((1 << bitCount) - 1);
503
+ if (idx < paletteCount) {
504
+ const p = paletteAt + idx * 4;
505
+ bl = b[p];
506
+ g = b[p + 1];
507
+ r = b[p + 2];
508
+ }
509
+ }
510
+ else
511
+ fail(`BMP bit count ${bitCount}`);
512
+ const k = (y * width + x) * 3;
513
+ rgb[k] = r;
514
+ rgb[k + 1] = g;
515
+ rgb[k + 2] = bl;
516
+ }
517
+ }
518
+ return { width, height, colorSpace: 'DeviceRGB', data: rgb, dct: false };
519
+ }
@@ -0,0 +1,2 @@
1
+ export { flattenXfa } from './flatten';
2
+ export type { FlattenNote, FlattenOptions, FlattenResult } from './types';
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.flattenXfa = void 0;
4
+ var flatten_1 = require("./flatten");
5
+ Object.defineProperty(exports, "flattenXfa", { enumerable: true, get: function () { return flatten_1.flattenXfa; } });
package/dist/index.mjs ADDED
@@ -0,0 +1,3 @@
1
+ import cjs from './index.js';
2
+ export const { flattenXfa } = cjs;
3
+ export default cjs;
@@ -0,0 +1,64 @@
1
+ import { type Matrix } from './css';
2
+ import { type Inherited, type Style } from './style';
3
+ import { type Line } from './text';
4
+ import type { XNode } from './tree';
5
+ export type Sides = [number, number, number, number];
6
+ export interface Box {
7
+ node: XNode;
8
+ style: Style;
9
+ inh: Inherited;
10
+ /** Border box, relative to the parent's border box. */
11
+ x: number;
12
+ y: number;
13
+ w: number;
14
+ h: number;
15
+ border: Sides;
16
+ padding: Sides;
17
+ kids: Box[];
18
+ /** Inline content, relative to the content box. */
19
+ lines: Line[] | null;
20
+ absolute: boolean;
21
+ positioned: boolean;
22
+ transform: Matrix | null;
23
+ clip: boolean;
24
+ /** For a draw: how far its text runs past the height pdf.js gave it, before reflowPage grows or squeezes it. */
25
+ textNeed?: number;
26
+ }
27
+ export interface ImageSize {
28
+ width: number;
29
+ height: number;
30
+ }
31
+ export interface LayoutEnv {
32
+ imageSize(src: string): ImageSize | null;
33
+ /** Called once per box; throws to stop the layout (the caller's deadline). */
34
+ check?(): void;
35
+ }
36
+ export declare function borderStyles(style: Style): string[];
37
+ interface Ctx {
38
+ env: LayoutEnv;
39
+ }
40
+ interface Request {
41
+ /** Width of the containing block, for percentages and auto widths. */
42
+ cw: number;
43
+ /** Height of the containing block, or null when it is auto. */
44
+ ch: number | null;
45
+ forcedW?: number;
46
+ forcedH?: number;
47
+ /** Size an auto width to its content instead of filling cw. */
48
+ shrink?: boolean;
49
+ }
50
+ /** Lays out one element and everything inside it. */
51
+ export declare function layoutBox(node: XNode, parent: XNode | null, parentInh: Inherited, req: Request, ctx: Ctx): Box;
52
+ /** Max-content width: the width a node takes when nothing wraps. */
53
+ export declare function intrinsicWidth(node: XNode, parent: XNode | null, parentInh: Inherited, ctx: Ctx): number;
54
+ /** The text a select shows: its selected option that is not the hidden placeholder. */
55
+ export declare function selectedText(node: XNode): string;
56
+ /**
57
+ * pdf.js measures some rich text short (lists, paragraphs that hold only a line break), so a draw's text can need
58
+ * more height than its box. Where the draw and the boxes around it have no fixed height in the template, the draw
59
+ * grows and the content after it in the same flow moves down, as far as the space left in the content area (or the
60
+ * first box of fixed height) allows; nothing moves into the box below. Text that still does not fit is drawn with
61
+ * its lines closer together (fitDrawText).
62
+ */
63
+ export declare function reflowPage(page: Box): void;
64
+ export {};