@flowselections/layout-maker 1.69.34 → 1.69.37

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 (52) hide show
  1. package/dist-lib/components/GeladenEtiketTab.d.ts.map +1 -1
  2. package/dist-lib/components/GeladenEtiketTab.js +2 -1
  3. package/dist-lib/components/PrintertaalKeuze.d.ts +6 -0
  4. package/dist-lib/components/PrintertaalKeuze.d.ts.map +1 -0
  5. package/dist-lib/components/PrintertaalKeuze.js +131 -0
  6. package/dist-lib/index.d.ts +3 -0
  7. package/dist-lib/index.d.ts.map +1 -1
  8. package/dist-lib/index.js +4 -0
  9. package/dist-lib/integrations/supabase/types.d.ts +490 -0
  10. package/dist-lib/integrations/supabase/types.d.ts.map +1 -1
  11. package/dist-lib/lib/htmlNaarPdf.server.d.ts +2 -0
  12. package/dist-lib/lib/htmlNaarPdf.server.d.ts.map +1 -1
  13. package/dist-lib/lib/htmlNaarPdf.server.js +1 -0
  14. package/dist-lib/lib/htmlPdf.server.d.ts +5 -0
  15. package/dist-lib/lib/htmlPdf.server.d.ts.map +1 -1
  16. package/dist-lib/lib/htmlPdf.server.js +16 -1
  17. package/dist-lib/lib/printQueue.server.d.ts +1 -1
  18. package/dist-lib/lib/printQueue.server.d.ts.map +1 -1
  19. package/dist-lib/lib/printQueue.server.js +71 -11
  20. package/dist-lib/lib/printService.server.d.ts +20 -0
  21. package/dist-lib/lib/printService.server.d.ts.map +1 -1
  22. package/dist-lib/lib/printService.server.js +27 -0
  23. package/dist-lib/lib/printertaal.d.ts +49 -0
  24. package/dist-lib/lib/printertaal.d.ts.map +1 -0
  25. package/dist-lib/lib/printertaal.js +58 -0
  26. package/dist-lib/lib/stickerOntwerp.js +2 -2
  27. package/dist-lib/lib/testpagina.d.ts +2 -0
  28. package/dist-lib/lib/testpagina.d.ts.map +1 -1
  29. package/dist-lib/lib/testpagina.js +41 -16
  30. package/dist-lib/lib/tspl.d.ts +50 -0
  31. package/dist-lib/lib/tspl.d.ts.map +1 -0
  32. package/dist-lib/lib/tspl.js +109 -0
  33. package/dist-lib/lib/tsplDiagnose.functions.d.ts +58 -0
  34. package/dist-lib/lib/tsplDiagnose.functions.d.ts.map +1 -0
  35. package/dist-lib/lib/tsplDiagnose.functions.js +118 -0
  36. package/dist-lib/lib/tsplFont.generated.d.ts +3 -0
  37. package/dist-lib/lib/tsplFont.generated.d.ts.map +1 -0
  38. package/dist-lib/lib/tsplFont.generated.js +5 -0
  39. package/dist-lib/lib/tsplRaster.d.ts +33 -0
  40. package/dist-lib/lib/tsplRaster.d.ts.map +1 -0
  41. package/dist-lib/lib/tsplRaster.js +853 -0
  42. package/dist-lib/lib/tsplRoute.d.ts +19 -0
  43. package/dist-lib/lib/tsplRoute.d.ts.map +1 -0
  44. package/dist-lib/lib/tsplRoute.js +30 -0
  45. package/dist-lib/lib/versie.generated.d.ts +1 -1
  46. package/dist-lib/lib/versie.generated.js +1 -1
  47. package/dist-lib/lib/werkplekken.d.ts +3 -1
  48. package/dist-lib/lib/werkplekken.d.ts.map +1 -1
  49. package/dist-lib/lib/werkplekken.js +5 -4
  50. package/package.json +8 -4
  51. package/sql/print-schema.sql +49 -1
  52. package/sql/print-schema.verslag.md +3 -2
@@ -0,0 +1,853 @@
1
+ // ============================================================================
2
+ // src/lib/tsplRaster.ts — 1.69.35: PDF → 1-bit beeld voor TSPL (pure TS).
3
+ // Geen native afhankelijkheden: draait in de Worker. Tekent de PDF's van de
4
+ // eigen documentmaker (pdf-lib): paden/rechthoeken, Helvetica-tekst (glyphs
5
+ // van Liberation Sans, metrisch gelijk), PNG-afbeeldingen (Flate) en
6
+ // ingebedde pagina's (Form XObjects, ook gedraaid/geschaald).
7
+ //
8
+ // Vaste drempel, geen dithering. Barcodes (gemarkeerd met /FSBarcode in de
9
+ // PDF) worden op hele dots uitgelijnd: elke module een geheel aantal dots,
10
+ // geen afronding per streep. Tekst kleiner dan 6 pt wordt op 6 pt getekend.
11
+ //
12
+ // Alles wat deze motor niet kent (andere lettertypen, JPEG, onbekende
13
+ // filters) = TsplRasterFout met reden. Losse PDF's (Floriday) komen daardoor
14
+ // nooit half op het etiket; de opdracht wordt geweigerd.
15
+ // ============================================================================
16
+ import pako from "pako";
17
+ import jpeg from "jpeg-js";
18
+ import { PDFArray, PDFDict, PDFDocument, PDFName, PDFRawStream, PDFRef, PDFStream, decodePDFRawStream, } from "pdf-lib";
19
+ import { Encodings, Font, FontNames } from "@pdf-lib/standard-fonts";
20
+ import { TSPL_FONT, TSPL_FONT_EM } from "./tsplFont.generated.js";
21
+ import { breedteDots, mmNaarDots, nieuwRaster } from "./tspl.js";
22
+ export class TsplRasterFout extends Error {
23
+ constructor() {
24
+ super(...arguments);
25
+ this.name = "TsplRasterFout";
26
+ }
27
+ }
28
+ export const TSPL_MIN_PT = 6;
29
+ const PT_MM = 25.4 / 72;
30
+ const ID = [1, 0, 0, 1, 0, 0];
31
+ /** a × b (eerst a, dan b) — PDF-rijvectorconventie. */
32
+ function mul(a, b) {
33
+ return [
34
+ a[0] * b[0] + a[1] * b[2],
35
+ a[0] * b[1] + a[1] * b[3],
36
+ a[2] * b[0] + a[3] * b[2],
37
+ a[2] * b[1] + a[3] * b[3],
38
+ a[4] * b[0] + a[5] * b[2] + b[4],
39
+ a[4] * b[1] + a[5] * b[3] + b[5],
40
+ ];
41
+ }
42
+ function inv(m) {
43
+ const d = m[0] * m[3] - m[1] * m[2];
44
+ if (Math.abs(d) < 1e-12)
45
+ return null;
46
+ return [m[3] / d, -m[1] / d, -m[2] / d, m[0] / d, (m[2] * m[5] - m[3] * m[4]) / d, (m[1] * m[4] - m[0] * m[5]) / d];
47
+ }
48
+ const pt = (m, x, y) => [x * m[0] + y * m[2] + m[4], x * m[1] + y * m[3] + m[5]];
49
+ const WIT = new Set([0, 9, 10, 12, 13, 32]);
50
+ const SCHEID = new Set([40, 41, 60, 62, 91, 93, 123, 125, 47, 37]);
51
+ class Lexer {
52
+ constructor(b) {
53
+ this.b = b;
54
+ this.i = 0;
55
+ }
56
+ skip() {
57
+ const b = this.b;
58
+ for (;;) {
59
+ while (this.i < b.length && WIT.has(b[this.i]))
60
+ this.i++;
61
+ if (b[this.i] === 37) {
62
+ while (this.i < b.length && b[this.i] !== 10 && b[this.i] !== 13)
63
+ this.i++;
64
+ continue;
65
+ }
66
+ return;
67
+ }
68
+ }
69
+ volgende() {
70
+ this.skip();
71
+ const b = this.b;
72
+ if (this.i >= b.length)
73
+ return null;
74
+ const c = b[this.i];
75
+ if (c === 47) {
76
+ let j = ++this.i;
77
+ while (j < b.length && !WIT.has(b[j]) && !SCHEID.has(b[j]))
78
+ j++;
79
+ const v = String.fromCharCode(...b.subarray(this.i, j));
80
+ this.i = j;
81
+ return { t: "name", v: v.replace(/#([0-9a-fA-F]{2})/g, (_, h) => String.fromCharCode(parseInt(h, 16))) };
82
+ }
83
+ if (c === 40)
84
+ return { t: "str", v: this.letterlijk() };
85
+ if (c === 60 && b[this.i + 1] === 60) {
86
+ this.i += 2;
87
+ const d = {};
88
+ for (;;) {
89
+ this.skip();
90
+ if (b[this.i] === 62 && b[this.i + 1] === 62) {
91
+ this.i += 2;
92
+ break;
93
+ }
94
+ const k = this.volgende();
95
+ if (!k)
96
+ break;
97
+ const v = this.volgende();
98
+ if (k.t === "name" && v)
99
+ d[k.v] = v;
100
+ }
101
+ return { t: "dict", v: d };
102
+ }
103
+ if (c === 60) {
104
+ let j = ++this.i;
105
+ let hex = "";
106
+ while (j < b.length && b[j] !== 62) {
107
+ if (!WIT.has(b[j]))
108
+ hex += String.fromCharCode(b[j]);
109
+ j++;
110
+ }
111
+ this.i = j + 1;
112
+ if (hex.length % 2)
113
+ hex += "0";
114
+ const out = new Uint8Array(hex.length / 2);
115
+ for (let k = 0; k < out.length; k++)
116
+ out[k] = parseInt(hex.substr(k * 2, 2), 16);
117
+ return { t: "str", v: out };
118
+ }
119
+ if (c === 91) {
120
+ this.i++;
121
+ const arr = [];
122
+ for (;;) {
123
+ this.skip();
124
+ if (b[this.i] === 93) {
125
+ this.i++;
126
+ break;
127
+ }
128
+ const v = this.volgende();
129
+ if (!v)
130
+ break;
131
+ arr.push(v);
132
+ }
133
+ return { t: "arr", v: arr };
134
+ }
135
+ let j = this.i;
136
+ while (j < b.length && !WIT.has(b[j]) && !SCHEID.has(b[j]))
137
+ j++;
138
+ if (j === this.i)
139
+ j++;
140
+ const w = String.fromCharCode(...b.subarray(this.i, j));
141
+ this.i = j;
142
+ const n = Number(w);
143
+ if (w !== "" && /^[+-]?(\d+\.?\d*|\.\d+)$/.test(w) && Number.isFinite(n))
144
+ return { t: "num", v: n };
145
+ return { t: "op", v: w };
146
+ }
147
+ letterlijk() {
148
+ const b = this.b;
149
+ this.i++;
150
+ const uit = [];
151
+ let diepte = 1;
152
+ while (this.i < b.length) {
153
+ const c = b[this.i++];
154
+ if (c === 92) {
155
+ const n = b[this.i++];
156
+ const map = { 110: 10, 114: 13, 116: 9, 98: 8, 102: 12, 40: 40, 41: 41, 92: 92 };
157
+ if (n >= 48 && n <= 55) {
158
+ let o = n - 48;
159
+ for (let k = 0; k < 2 && b[this.i] >= 48 && b[this.i] <= 55; k++)
160
+ o = o * 8 + (b[this.i++] - 48);
161
+ uit.push(o & 255);
162
+ }
163
+ else if (n === 13 || n === 10) {
164
+ if (n === 13 && b[this.i] === 10)
165
+ this.i++;
166
+ }
167
+ else
168
+ uit.push(map[n] ?? n);
169
+ continue;
170
+ }
171
+ if (c === 40)
172
+ diepte++;
173
+ if (c === 41 && --diepte === 0)
174
+ break;
175
+ uit.push(c);
176
+ }
177
+ return new Uint8Array(uit);
178
+ }
179
+ }
180
+ const glyphCache = { normaal: new Map(), vet: new Map() };
181
+ function glyph(soort, code) {
182
+ const c = glyphCache[soort];
183
+ if (c.has(code))
184
+ return c.get(code);
185
+ const g = TSPL_FONT[soort][code];
186
+ let res = null;
187
+ if (g) {
188
+ const raw = Uint8Array.from(atob(g[4]), (ch) => ch.charCodeAt(0));
189
+ res = { l: g[0], t: g[1], w: g[2], h: g[3], px: pako.inflate(raw) };
190
+ }
191
+ c.set(code, res);
192
+ return res;
193
+ }
194
+ const breedteFont = {};
195
+ const codeNaarNaam = new Map();
196
+ for (const [, [code, naam]] of Object.entries(Encodings.WinAnsi.unicodeMappings)) {
197
+ if (!codeNaarNaam.has(code))
198
+ codeNaarNaam.set(code, naam);
199
+ }
200
+ function voorloopBreedte(vet, code) {
201
+ const naam = vet ? FontNames.HelveticaBold : FontNames.Helvetica;
202
+ const f = (breedteFont[naam] ?? (breedteFont[naam] = Font.load(naam)));
203
+ const g = codeNaarNaam.get(code);
204
+ return (g ? f.getWidthOfGlyph(g) : 0) || (code === 32 ? 278 : 556);
205
+ }
206
+ class Schilder {
207
+ constructor(r, meld) {
208
+ this.r = r;
209
+ this.meld = meld;
210
+ }
211
+ zet(x, y, zwart) {
212
+ if (x < 0 || y < 0 || x >= this.r.breedte || y >= this.r.hoogte)
213
+ return;
214
+ this.r.dots[y * this.r.breedte + x] = zwart ? 1 : 0;
215
+ }
216
+ rechthoek(x0, y0, x1, y1, zwart) {
217
+ let a = Math.round(Math.min(x0, x1)), b = Math.round(Math.max(x0, x1));
218
+ let c = Math.round(Math.min(y0, y1)), d = Math.round(Math.max(y0, y1));
219
+ if (b <= a)
220
+ b = a + 1;
221
+ if (d <= c)
222
+ d = c + 1;
223
+ a = Math.max(0, a);
224
+ c = Math.max(0, c);
225
+ b = Math.min(this.r.breedte, b);
226
+ d = Math.min(this.r.hoogte, d);
227
+ for (let y = c; y < d; y++)
228
+ this.r.dots.fill(zwart ? 1 : 0, y * this.r.breedte + a, y * this.r.breedte + b);
229
+ }
230
+ /** Scanline-vulling (nonzero of evenodd) op pixelmiddens. */
231
+ polygonen(polys, evenodd, zwart) {
232
+ let minY = Infinity, maxY = -Infinity;
233
+ for (const p of polys)
234
+ for (const [, y] of p) {
235
+ minY = Math.min(minY, y);
236
+ maxY = Math.max(maxY, y);
237
+ }
238
+ const y0 = Math.max(0, Math.floor(minY)), y1 = Math.min(this.r.hoogte - 1, Math.ceil(maxY));
239
+ for (let y = y0; y <= y1; y++) {
240
+ const sy = y + 0.5;
241
+ const kruis = [];
242
+ for (const p of polys) {
243
+ for (let i = 0; i < p.length; i++) {
244
+ const [ax, ay] = p[i], [bx, by] = p[(i + 1) % p.length];
245
+ if ((ay <= sy && by > sy) || (by <= sy && ay > sy))
246
+ kruis.push([ax + ((sy - ay) / (by - ay)) * (bx - ax), by > ay ? 1 : -1]);
247
+ }
248
+ }
249
+ kruis.sort((p, q) => p[0] - q[0]);
250
+ let w = 0;
251
+ for (let i = 0; i < kruis.length - 1; i++) {
252
+ w = evenodd ? w ^ 1 : w + kruis[i][1];
253
+ if (w !== 0) {
254
+ const xa = Math.max(0, Math.ceil(kruis[i][0] - 0.5)), xb = Math.min(this.r.breedte - 1, Math.floor(kruis[i + 1][0] - 0.5));
255
+ for (let x = xa; x <= xb; x++)
256
+ this.zet(x, y, zwart);
257
+ }
258
+ }
259
+ }
260
+ }
261
+ }
262
+ const asBreed = (m) => Math.hypot(m[0], m[1]);
263
+ const asRecht = (m) => Math.abs(m[1]) < 1e-9 && Math.abs(m[2]) < 1e-9 || Math.abs(m[0]) < 1e-9 && Math.abs(m[3]) < 1e-9;
264
+ function lum(ops, soort) {
265
+ if (soort === "g")
266
+ return ops[0] ?? 0;
267
+ if (soort === "rg")
268
+ return 0.299 * (ops[0] ?? 0) + 0.587 * (ops[1] ?? 0) + 0.114 * (ops[2] ?? 0);
269
+ const [c = 0, m = 0, y = 0, k = 0] = ops;
270
+ return 0.299 * (1 - c) * (1 - k) + 0.587 * (1 - m) * (1 - k) + 0.114 * (1 - y) * (1 - k);
271
+ }
272
+ function lookup(doc, o) {
273
+ return o instanceof PDFRef ? doc.context.lookup(o) : o;
274
+ }
275
+ function streamBytes(s) {
276
+ if (s instanceof PDFRawStream) {
277
+ const f = s.dict.get(PDFName.of("Filter"));
278
+ const namen = f instanceof PDFArray ? f.asArray().map(String) : f ? [String(f)] : [];
279
+ for (const n of namen)
280
+ if (n !== "/FlateDecode")
281
+ throw new TsplRasterFout(`De PDF gebruikt ${n.slice(1)}; dat kan niet via TSPL.`);
282
+ return decodePDFRawStream(s).decode();
283
+ }
284
+ if (s instanceof PDFStream)
285
+ return s.getContents();
286
+ throw new TsplRasterFout("De PDF-inhoud kon niet worden gelezen.");
287
+ }
288
+ class Tolk {
289
+ constructor(doc, sch, dev) {
290
+ this.doc = doc;
291
+ this.sch = sch;
292
+ this.dev = dev;
293
+ this.stapel = [];
294
+ this.pad = [];
295
+ this.huidig = null;
296
+ this.tm = ID;
297
+ this.tlm = ID;
298
+ this.barcode = null;
299
+ this.s = { ctm: dev, vulZwart: true, lijnZwart: true, lijn: 1, font: null, grootte: 0, tc: 0, tw: 0, tz: 100, tl: 0, rise: 0 };
300
+ }
301
+ voer(inhoud, res, diepte = 0) {
302
+ if (diepte > 8)
303
+ throw new TsplRasterFout("De PDF is te diep genest.");
304
+ const lx = new Lexer(inhoud);
305
+ const ops = [];
306
+ for (let t = lx.volgende(); t; t = lx.volgende()) {
307
+ if (t.t !== "op") {
308
+ ops.push(t);
309
+ continue;
310
+ }
311
+ this.op(t.v, ops, res, diepte);
312
+ ops.length = 0;
313
+ }
314
+ }
315
+ n(ops, i) {
316
+ const t = ops[i];
317
+ return t && t.t === "num" ? t.v : 0;
318
+ }
319
+ nums(ops) {
320
+ return ops.filter((t) => t.t === "num").map((t) => t.v);
321
+ }
322
+ dp(x, y) { return pt(this.s.ctm, x, y); }
323
+ op(op, a, res, diepte) {
324
+ const s = this.s;
325
+ switch (op) {
326
+ case "q":
327
+ this.stapel.push({ ...s });
328
+ return;
329
+ case "Q":
330
+ if (this.stapel.length)
331
+ this.s = this.stapel.pop();
332
+ return;
333
+ case "cm":
334
+ s.ctm = mul([this.n(a, 0), this.n(a, 1), this.n(a, 2), this.n(a, 3), this.n(a, 4), this.n(a, 5)], s.ctm);
335
+ return;
336
+ case "w":
337
+ s.lijn = this.n(a, 0);
338
+ return;
339
+ case "g":
340
+ s.vulZwart = lum(this.nums(a), "g") < 0.5;
341
+ return;
342
+ case "G":
343
+ s.lijnZwart = lum(this.nums(a), "g") < 0.5;
344
+ return;
345
+ case "rg":
346
+ s.vulZwart = lum(this.nums(a), "rg") < 0.5;
347
+ return;
348
+ case "RG":
349
+ s.lijnZwart = lum(this.nums(a), "rg") < 0.5;
350
+ return;
351
+ case "k":
352
+ s.vulZwart = lum(this.nums(a), "k") < 0.5;
353
+ return;
354
+ case "K":
355
+ s.lijnZwart = lum(this.nums(a), "k") < 0.5;
356
+ return;
357
+ case "m":
358
+ this.huidig = [this.dp(this.n(a, 0), this.n(a, 1))];
359
+ this.pad.push({ pts: this.huidig, dicht: false, rect: false });
360
+ return;
361
+ case "l":
362
+ this.huidig?.push(this.dp(this.n(a, 0), this.n(a, 1)));
363
+ return;
364
+ case "c":
365
+ case "v":
366
+ case "y":
367
+ this.bezier(op, a);
368
+ return;
369
+ case "h":
370
+ if (this.pad.length)
371
+ this.pad[this.pad.length - 1].dicht = true;
372
+ return;
373
+ case "re": {
374
+ const [x, y, w, h] = [this.n(a, 0), this.n(a, 1), this.n(a, 2), this.n(a, 3)];
375
+ this.pad.push({ pts: [this.dp(x, y), this.dp(x + w, y), this.dp(x + w, y + h), this.dp(x, y + h)], dicht: true, rect: asRecht(s.ctm) });
376
+ this.huidig = null;
377
+ return;
378
+ }
379
+ case "f":
380
+ case "F":
381
+ case "f*":
382
+ this.vul(op === "f*");
383
+ this.pad = [];
384
+ return;
385
+ case "S":
386
+ this.lijn(false);
387
+ this.pad = [];
388
+ return;
389
+ case "s":
390
+ this.lijn(true);
391
+ this.pad = [];
392
+ return;
393
+ case "B":
394
+ case "B*":
395
+ this.vul(op === "B*");
396
+ this.lijn(false);
397
+ this.pad = [];
398
+ return;
399
+ case "b":
400
+ case "b*":
401
+ this.vul(op === "b*");
402
+ this.lijn(true);
403
+ this.pad = [];
404
+ return;
405
+ case "n":
406
+ this.pad = [];
407
+ return;
408
+ case "BT":
409
+ this.tm = ID;
410
+ this.tlm = ID;
411
+ return;
412
+ case "ET": return;
413
+ case "Tf": {
414
+ const naam = a[0]?.t === "name" ? a[0].v : "";
415
+ s.grootte = this.n(a, 1);
416
+ s.font = this.font(res, naam);
417
+ return;
418
+ }
419
+ case "Tc":
420
+ s.tc = this.n(a, 0);
421
+ return;
422
+ case "Tw":
423
+ s.tw = this.n(a, 0);
424
+ return;
425
+ case "Tz":
426
+ s.tz = this.n(a, 0);
427
+ return;
428
+ case "TL":
429
+ s.tl = this.n(a, 0);
430
+ return;
431
+ case "Ts":
432
+ s.rise = this.n(a, 0);
433
+ return;
434
+ case "Td":
435
+ this.tlm = mul([1, 0, 0, 1, this.n(a, 0), this.n(a, 1)], this.tlm);
436
+ this.tm = this.tlm;
437
+ return;
438
+ case "TD":
439
+ s.tl = -this.n(a, 1);
440
+ this.tlm = mul([1, 0, 0, 1, this.n(a, 0), this.n(a, 1)], this.tlm);
441
+ this.tm = this.tlm;
442
+ return;
443
+ case "Tm":
444
+ this.tlm = [this.n(a, 0), this.n(a, 1), this.n(a, 2), this.n(a, 3), this.n(a, 4), this.n(a, 5)];
445
+ this.tm = this.tlm;
446
+ return;
447
+ case "T*":
448
+ this.tlm = mul([1, 0, 0, 1, 0, -s.tl], this.tlm);
449
+ this.tm = this.tlm;
450
+ return;
451
+ case "Tj":
452
+ if (a[0]?.t === "str")
453
+ this.tekst(a[0].v);
454
+ return;
455
+ case "'":
456
+ this.op("T*", [], res, diepte);
457
+ if (a[0]?.t === "str")
458
+ this.tekst(a[0].v);
459
+ return;
460
+ case "TJ":
461
+ if (a[0]?.t === "arr")
462
+ for (const e of a[0].v) {
463
+ if (e.t === "str")
464
+ this.tekst(e.v);
465
+ else if (e.t === "num")
466
+ this.tm = mul([1, 0, 0, 1, (-e.v / 1000) * s.grootte * (s.tz / 100), 0], this.tm);
467
+ }
468
+ return;
469
+ case "Do":
470
+ if (a[0]?.t === "name")
471
+ this.xobject(a[0].v, res, diepte);
472
+ return;
473
+ case "BDC": {
474
+ if (a[0]?.t === "name" && a[0].v === "FSBarcode" && a[1]?.t === "dict") {
475
+ const d = a[1].v;
476
+ const p = d["P"]?.t === "str" ? String.fromCharCode(...d["P"].v) : "";
477
+ const num = (k) => (d[k]?.t === "num" ? d[k].v : 0);
478
+ if (/^[01]+$/.test(p))
479
+ this.barcode = { patroon: p, doos: [num("X"), num("Y"), num("W"), num("H")], ctm: s.ctm };
480
+ }
481
+ return;
482
+ }
483
+ case "EMC":
484
+ if (this.barcode) {
485
+ this.tekenBarcode(this.barcode);
486
+ this.barcode = null;
487
+ }
488
+ return;
489
+ case "BI": throw new TsplRasterFout("De PDF bevat een inline-afbeelding; dat kan niet via TSPL.");
490
+ default: return; // W, W*, gs, d, j, J, M, ri, i, BMC, MP, DP, sh(…) → genegeerd
491
+ }
492
+ }
493
+ bezier(op, a) {
494
+ if (!this.huidig?.length)
495
+ return;
496
+ const n = this.nums(a);
497
+ const inv0 = inv(this.s.ctm);
498
+ if (!inv0)
499
+ return;
500
+ const [sx, sy] = pt(inv0, ...this.huidig[this.huidig.length - 1]);
501
+ let p1, p2, p3;
502
+ if (op === "c") {
503
+ p1 = [n[0], n[1]];
504
+ p2 = [n[2], n[3]];
505
+ p3 = [n[4], n[5]];
506
+ }
507
+ else if (op === "v") {
508
+ p1 = [sx, sy];
509
+ p2 = [n[0], n[1]];
510
+ p3 = [n[2], n[3]];
511
+ }
512
+ else {
513
+ p1 = [n[0], n[1]];
514
+ p2 = [n[2], n[3]];
515
+ p3 = [n[2], n[3]];
516
+ }
517
+ for (let i = 1; i <= 16; i++) {
518
+ const t = i / 16, u = 1 - t;
519
+ const x = u * u * u * sx + 3 * u * u * t * p1[0] + 3 * u * t * t * p2[0] + t * t * t * p3[0];
520
+ const y = u * u * u * sy + 3 * u * u * t * p1[1] + 3 * u * t * t * p2[1] + t * t * t * p3[1];
521
+ this.huidig.push(this.dp(x, y));
522
+ }
523
+ }
524
+ vul(evenodd) {
525
+ if (this.barcode)
526
+ return; // de staven tekent tekenBarcode zelf
527
+ const zwart = this.s.vulZwart;
528
+ const rest = [];
529
+ for (const sp of this.pad) {
530
+ if (sp.rect) {
531
+ const xs = sp.pts.map((p) => p[0]), ys = sp.pts.map((p) => p[1]);
532
+ this.sch.rechthoek(Math.min(...xs), Math.min(...ys), Math.max(...xs), Math.max(...ys), zwart);
533
+ }
534
+ else if (sp.pts.length > 2)
535
+ rest.push(sp.pts);
536
+ }
537
+ if (rest.length)
538
+ this.sch.polygonen(rest, evenodd, zwart);
539
+ }
540
+ lijn(sluit) {
541
+ if (this.barcode)
542
+ return;
543
+ const zwart = this.s.lijnZwart;
544
+ const dik = Math.max(1, this.s.lijn * asBreed(this.s.ctm));
545
+ for (const sp of this.pad) {
546
+ const p = sp.pts;
547
+ const n = sp.dicht || sluit ? p.length : p.length - 1;
548
+ for (let i = 0; i < n; i++) {
549
+ const [ax, ay] = p[i], [bx, by] = p[(i + 1) % p.length];
550
+ if (Math.abs(ax - bx) < 1e-6 || Math.abs(ay - by) < 1e-6) {
551
+ // Horizontaal/verticaal: rechthoek met vierkante uiteinden.
552
+ const h = dik / 2;
553
+ if (Math.abs(ay - by) < 1e-6)
554
+ this.sch.rechthoek(Math.min(ax, bx) - (sp.dicht ? h : 0), ay - h, Math.max(ax, bx) + (sp.dicht ? h : 0), ay + h, zwart);
555
+ else
556
+ this.sch.rechthoek(ax - h, Math.min(ay, by) - (sp.dicht ? h : 0), ax + h, Math.max(ay, by) + (sp.dicht ? h : 0), zwart);
557
+ }
558
+ else {
559
+ const len = Math.hypot(bx - ax, by - ay);
560
+ const nx = (-(by - ay) / len) * (dik / 2), ny = ((bx - ax) / len) * (dik / 2);
561
+ this.sch.polygonen([[[ax + nx, ay + ny], [bx + nx, by + ny], [bx - nx, by - ny], [ax - nx, ay - ny]]], false, zwart);
562
+ }
563
+ }
564
+ }
565
+ }
566
+ font(res, naam) {
567
+ const fonts = lookup(this.doc, res?.get(PDFName.of("Font")));
568
+ const f = fonts instanceof PDFDict ? lookup(this.doc, fonts.get(PDFName.of(naam))) : undefined;
569
+ const base = f instanceof PDFDict ? String(f.get(PDFName.of("BaseFont")) ?? "") : "";
570
+ if (base === "/Helvetica")
571
+ return { vet: false };
572
+ if (base === "/Helvetica-Bold")
573
+ return { vet: true };
574
+ throw new TsplRasterFout(`De PDF gebruikt lettertype ${base.slice(1) || "onbekend"}; dat kan niet via TSPL.`);
575
+ }
576
+ tekst(bytes) {
577
+ const s = this.s;
578
+ if (!s.font || !s.grootte)
579
+ return;
580
+ const th = s.tz / 100;
581
+ // Effectieve grootte in pt (zonder het dot-schaal van de pagina).
582
+ const eff = s.grootte * Math.sqrt(Math.abs((mul(this.tm, s.ctm)[0] * mul(this.tm, s.ctm)[3] - mul(this.tm, s.ctm)[1] * mul(this.tm, s.ctm)[2]))) / asBreed(this.dev);
583
+ const f = eff > 0 && eff < TSPL_MIN_PT ? TSPL_MIN_PT / eff : 1;
584
+ if (f > 1)
585
+ this.sch.meld.tekst_vergroot++;
586
+ const groot = s.grootte * f;
587
+ if (f > 1)
588
+ bytes = this.pasVergroot(bytes, groot, th);
589
+ if (!bytes.length)
590
+ return;
591
+ for (const code of bytes) {
592
+ const trm = mul(mul([groot * th, 0, 0, groot, 0, s.rise], this.tm), s.ctm);
593
+ const g = glyph(s.font.vet ? "vet" : "normaal", code);
594
+ if (g)
595
+ this.glyphTekenen(g, trm);
596
+ const adv = (voorloopBreedte(s.font.vet, code) / 1000) * groot + s.tc + (code === 32 ? s.tw : 0);
597
+ this.tm = mul([1, 0, 0, 1, adv * th, 0], this.tm);
598
+ }
599
+ }
600
+ /**
601
+ * Na vergroten naar 6 pt: past de regel nog binnen de beschikbare breedte
602
+ * (het etiket)? Zo niet: eerst terugschuiven binnen de rand, dan inkorten
603
+ * met "…", en als er minder dan 3 tekens overblijven de regel weglaten.
604
+ * Nooit over de rand tekenen.
605
+ */
606
+ pasVergroot(bytes, groot, th) {
607
+ const s = this.s;
608
+ const adv = (code) => ((voorloopBreedte(s.font.vet, code) / 1000) * groot + s.tc + (code === 32 ? s.tw : 0)) * th;
609
+ const naarDev = (tx) => pt(mul(mul([1, 0, 0, 1, tx, s.rise], this.tm), s.ctm), 0, 0);
610
+ const B = this.sch.r.breedte, H = this.sch.r.hoogte;
611
+ const binnen = (p) => p[0] >= -0.5 && p[0] <= B + 0.5 && p[1] >= -0.5 && p[1] <= H + 0.5;
612
+ const totaal = (b) => b.reduce((t, c) => t + adv(c), 0);
613
+ const a = naarDev(0);
614
+ const lengteDev = (w) => { const e = naarDev(w); return Math.hypot(e[0] - a[0], e[1] - a[1]); };
615
+ const w = totaal(bytes);
616
+ if (binnen(naarDev(w)))
617
+ return bytes;
618
+ // 1) Terugschuiven langs de regel, zolang het begin binnen het etiket blijft.
619
+ const e = naarDev(w);
620
+ const dirX = (e[0] - a[0]) / (lengteDev(w) || 1), dirY = (e[1] - a[1]) / (lengteDev(w) || 1);
621
+ const over = Math.max(e[0] - B, -e[0], e[1] - H, -e[1], 0) / Math.max(Math.abs(dirX), Math.abs(dirY), 1e-6);
622
+ const ruimteTerug = Math.min(dirX > 0 ? a[0] : dirX < 0 ? B - a[0] : Infinity, dirY > 0 ? a[1] : dirY < 0 ? H - a[1] : Infinity);
623
+ if (over <= ruimteTerug) {
624
+ const tTerug = (over / (lengteDev(w) || 1)) * w;
625
+ this.tm = mul([1, 0, 0, 1, -tTerug, 0], this.tm);
626
+ return bytes;
627
+ }
628
+ // 2) Inkorten met "…" (WinAnsi 133) tot het past.
629
+ const ellips = glyph(s.font.vet ? "vet" : "normaal", 133) ? 133 : 46;
630
+ const staart = ellips === 133 ? [133] : [46, 46, 46];
631
+ for (let n = bytes.length - 1; n >= 3; n--) {
632
+ const kand = new Uint8Array([...bytes.subarray(0, n), ...staart]);
633
+ if (binnen(naarDev(totaal(kand)))) {
634
+ this.sch.meld.tekst_ingekort = (this.sch.meld.tekst_ingekort ?? 0) + 1;
635
+ return kand;
636
+ }
637
+ }
638
+ // 3) Weglaten.
639
+ this.sch.meld.tekst_weggelaten = (this.sch.meld.tekst_weggelaten ?? 0) + 1;
640
+ return new Uint8Array();
641
+ }
642
+ glyphTekenen(g, trm) {
643
+ // Glyph-ruimte in em: x = (l + u)/EM, y = (t - v)/EM.
644
+ const E = TSPL_FONT_EM;
645
+ const naarDev = mul([1 / E, 0, 0, -1 / E, g.l / E, g.t / E], trm);
646
+ const terug = inv(naarDev);
647
+ if (!terug)
648
+ return;
649
+ const hoeken = [pt(naarDev, 0, 0), pt(naarDev, g.w, 0), pt(naarDev, 0, g.h), pt(naarDev, g.w, g.h)];
650
+ const x0 = Math.max(0, Math.floor(Math.min(...hoeken.map((p) => p[0]))));
651
+ const x1 = Math.min(this.sch.r.breedte - 1, Math.ceil(Math.max(...hoeken.map((p) => p[0]))));
652
+ const y0 = Math.max(0, Math.floor(Math.min(...hoeken.map((p) => p[1]))));
653
+ const y1 = Math.min(this.sch.r.hoogte - 1, Math.ceil(Math.max(...hoeken.map((p) => p[1]))));
654
+ for (let y = y0; y <= y1; y++) {
655
+ for (let x = x0; x <= x1; x++) {
656
+ const [u, v] = pt(terug, x + 0.5, y + 0.5);
657
+ if (bilineair(g.px, g.w, g.h, u - 0.5, v - 0.5) >= 128)
658
+ this.sch.zet(x, y, true);
659
+ }
660
+ }
661
+ }
662
+ xobject(naam, res, diepte) {
663
+ const xo = lookup(this.doc, res?.get(PDFName.of("XObject")));
664
+ const x = xo instanceof PDFDict ? lookup(this.doc, xo.get(PDFName.of(naam))) : undefined;
665
+ if (!(x instanceof PDFRawStream || x instanceof PDFStream))
666
+ return;
667
+ const d = x.dict;
668
+ const sub = String(d.get(PDFName.of("Subtype")) ?? "");
669
+ if (sub === "/Form") {
670
+ const mat = lookup(this.doc, d.get(PDFName.of("Matrix")));
671
+ const m = mat instanceof PDFArray ? mat.asArray().map((v) => lookup(this.doc, v).asNumber()) : ID;
672
+ const eigen = lookup(this.doc, d.get(PDFName.of("Resources")));
673
+ this.stapel.push({ ...this.s });
674
+ this.s.ctm = mul(m, this.s.ctm);
675
+ const pad = this.pad, huidig = this.huidig, tm = this.tm, tlm = this.tlm;
676
+ this.pad = [];
677
+ this.voer(streamBytes(x), eigen instanceof PDFDict ? eigen : res, diepte + 1);
678
+ this.pad = pad;
679
+ this.huidig = huidig;
680
+ this.tm = tm;
681
+ this.tlm = tlm;
682
+ this.s = this.stapel.pop();
683
+ return;
684
+ }
685
+ if (sub === "/Image")
686
+ this.afbeelding(x);
687
+ }
688
+ afbeelding(x) {
689
+ const d = x.dict;
690
+ const getal = (k) => lookup(this.doc, d.get(PDFName.of(k)))?.asNumber?.() ?? 0;
691
+ const W = getal("Width"), H = getal("Height"), bpc = getal("BitsPerComponent");
692
+ const cs = String(lookup(this.doc, d.get(PDFName.of("ColorSpace"))) ?? "");
693
+ if (bpc !== 8 || (cs !== "/DeviceRGB" && cs !== "/DeviceGray")) {
694
+ // (CMYK-JPEG en geïndexeerde kleuren komen uit de eigen documentmaker niet voor.)
695
+ throw new TsplRasterFout("De PDF bevat een afbeelding in een vorm die TSPL hier niet kan tekenen.");
696
+ }
697
+ let kan = cs === "/DeviceRGB" ? 3 : 1;
698
+ const filter = String(d.get(PDFName.of("Filter")) ?? "");
699
+ let px;
700
+ if (filter === "/DCTDecode") {
701
+ // JPEG (bijv. een eigen sticker): pure-JS decoder, geen Buffer nodig.
702
+ const j = jpeg.decode(x.contents, { useTArray: true, formatAsRGBA: false, maxMemoryUsageInMB: 256 });
703
+ px = j.data;
704
+ kan = Math.round(px.length / (j.width * j.height)) || 3;
705
+ }
706
+ else
707
+ px = streamBytes(x);
708
+ const sm = lookup(this.doc, d.get(PDFName.of("SMask")));
709
+ const alfa = sm instanceof PDFRawStream ? streamBytes(sm) : null;
710
+ const m = this.s.ctm;
711
+ const terug = inv(m);
712
+ if (!terug)
713
+ return;
714
+ const hoeken = [pt(m, 0, 0), pt(m, 1, 0), pt(m, 0, 1), pt(m, 1, 1)];
715
+ const x0 = Math.max(0, Math.floor(Math.min(...hoeken.map((p) => p[0]))));
716
+ const x1 = Math.min(this.sch.r.breedte - 1, Math.ceil(Math.max(...hoeken.map((p) => p[0]))));
717
+ const y0 = Math.max(0, Math.floor(Math.min(...hoeken.map((p) => p[1]))));
718
+ const y1 = Math.min(this.sch.r.hoogte - 1, Math.ceil(Math.max(...hoeken.map((p) => p[1]))));
719
+ for (let y = y0; y <= y1; y++) {
720
+ for (let xx = x0; xx <= x1; xx++) {
721
+ const [u, v] = pt(terug, xx + 0.5, y + 0.5);
722
+ if (u < 0 || u >= 1 || v < 0 || v >= 1)
723
+ continue;
724
+ const ix = Math.min(W - 1, Math.floor(u * W)), iy = Math.min(H - 1, Math.floor((1 - v) * H));
725
+ const o = (iy * W + ix) * kan;
726
+ const l = kan >= 3 ? (0.299 * px[o] + 0.587 * px[o + 1] + 0.114 * px[o + 2]) / 255 : px[o] / 255;
727
+ const a = alfa ? alfa[iy * W + ix] / 255 : 1;
728
+ const lw = 1 - a * (1 - l);
729
+ if (a > 0.01)
730
+ this.sch.zet(xx, y, lw < 0.5);
731
+ }
732
+ }
733
+ }
734
+ /** Barcode op hele dots: elke module een geheel aantal dots, gecentreerd in het vak. */
735
+ tekenBarcode(b) {
736
+ const [X, Y, W, H] = b.doos;
737
+ const p0 = pt(b.ctm, X, Y), p1 = pt(b.ctm, X + W, Y), p2 = pt(b.ctm, X, Y + H);
738
+ const langsX = Math.abs(p1[0] - p0[0]) >= Math.abs(p1[1] - p0[1]);
739
+ const lengte = langsX ? Math.abs(p1[0] - p0[0]) : Math.abs(p1[1] - p0[1]);
740
+ const M_ = b.patroon.length;
741
+ const mod = Math.max(1, Math.floor(lengte / M_));
742
+ const totaal = mod * M_;
743
+ const begin = Math.round(Math.min(langsX ? p0[0] : p0[1], langsX ? p1[0] : p1[1]) + (lengte - totaal) / 2);
744
+ const omgekeerd = langsX ? p1[0] < p0[0] : p1[1] < p0[1];
745
+ const dwars = langsX ? [p0[1], p2[1]] : [p0[0], p2[0]];
746
+ const d0 = Math.round(Math.min(...dwars)), d1 = Math.max(d0 + 1, Math.round(Math.max(...dwars)));
747
+ for (let i = 0; i < M_; i++) {
748
+ if (b.patroon[omgekeerd ? M_ - 1 - i : i] !== "1")
749
+ continue;
750
+ const a = begin + i * mod;
751
+ if (langsX)
752
+ this.sch.rechthoek(a, d0, a + mod, d1, true);
753
+ else
754
+ this.sch.rechthoek(d0, a, d1, a + mod, true);
755
+ }
756
+ this.sch.meld.barcodes.push({ modules: M_, module_dots: mod, breedte_dots: totaal, as: langsX ? "x" : "y" });
757
+ }
758
+ }
759
+ function bilineair(px, w, h, u, v) {
760
+ if (u < -0.5 || v < -0.5 || u > w - 0.5 || v > h - 0.5)
761
+ return 0;
762
+ const x0 = Math.floor(u), y0 = Math.floor(v), fx = u - x0, fy = v - y0;
763
+ const g = (x, y) => (x < 0 || y < 0 || x >= w || y >= h ? 0 : px[y * w + x]);
764
+ return (g(x0, y0) * (1 - fx) + g(x0 + 1, y0) * fx) * (1 - fy) + (g(x0, y0 + 1) * (1 - fx) + g(x0 + 1, y0 + 1) * fx) * fy;
765
+ }
766
+ /** Zet elke PDF-pagina om naar een 1-bit beeld op `dpi`. */
767
+ export async function pdfNaarRasters(pdf, dpi) {
768
+ const bytes = typeof pdf === "string" ? Uint8Array.from(atob(pdf), (c) => c.charCodeAt(0)) : pdf;
769
+ const doc = await PDFDocument.load(bytes);
770
+ const meldingen = { tekst_vergroot: 0, barcodes: [] };
771
+ const paginas = [];
772
+ const maten = [];
773
+ for (const p of doc.getPages()) {
774
+ if (p.getRotation().angle % 360)
775
+ throw new TsplRasterFout("De PDF-pagina is gedraaid opgeslagen; dat kan niet via TSPL.");
776
+ const { x: mx, y: my, width, height } = p.getMediaBox();
777
+ const bMm = width * PT_MM, hMm = height * PT_MM;
778
+ maten.push({ breedte_mm: bMm, hoogte_mm: hMm });
779
+ const r = nieuwRaster(breedteDots(bMm, dpi), mmNaarDots(hMm, dpi));
780
+ const s = dpi / 72;
781
+ const dev = [s, 0, 0, -s, -mx * s, r.hoogte + my * s];
782
+ const tolk = new Tolk(doc, new Schilder(r, meldingen), dev);
783
+ const inhoud = p.node.Contents();
784
+ const delen = inhoud instanceof PDFArray ? inhoud.asArray().map((o) => lookup(doc, o)) : [inhoud];
785
+ const stukken = delen.filter(Boolean).map((d) => streamBytes(d));
786
+ const totaal = new Uint8Array(stukken.reduce((t, b) => t + b.length + 1, 0));
787
+ let o = 0;
788
+ for (const b of stukken) {
789
+ totaal.set(b, o);
790
+ o += b.length;
791
+ totaal[o++] = 10;
792
+ }
793
+ tolk.voer(totaal, p.node.Resources());
794
+ paginas.push(r);
795
+ }
796
+ return { paginas, maten_mm: maten, meldingen };
797
+ }
798
+ // ---------------------------------------------------------------- PNG
799
+ const CRC = (() => {
800
+ const t = new Uint32Array(256);
801
+ for (let n = 0; n < 256; n++) {
802
+ let c = n;
803
+ for (let k = 0; k < 8; k++)
804
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
805
+ t[n] = c >>> 0;
806
+ }
807
+ return t;
808
+ })();
809
+ function crc32(b) {
810
+ let c = 0xffffffff;
811
+ for (const x of b)
812
+ c = CRC[(c ^ x) & 0xff] ^ (c >>> 8);
813
+ return (c ^ 0xffffffff) >>> 0;
814
+ }
815
+ /** PNG (8-bit grijs) van precies wat er naar de printer gaat; `rand` tekent de etiketrand licht grijs. */
816
+ export function rasterNaarPng(r, opties = {}) {
817
+ const rij = r.breedte + 1;
818
+ const raw = new Uint8Array(rij * r.hoogte);
819
+ for (let y = 0; y < r.hoogte; y++) {
820
+ raw[y * rij] = 0;
821
+ for (let x = 0; x < r.breedte; x++) {
822
+ const rand = opties.rand && (x === 0 || y === 0 || x === r.breedte - 1 || y === r.hoogte - 1);
823
+ raw[y * rij + 1 + x] = r.dots[y * r.breedte + x] ? 0 : rand ? 180 : 255;
824
+ }
825
+ }
826
+ const chunk = (type, data) => {
827
+ const b = new Uint8Array(12 + data.length);
828
+ const dv = new DataView(b.buffer);
829
+ dv.setUint32(0, data.length);
830
+ for (let i = 0; i < 4; i++)
831
+ b[4 + i] = type.charCodeAt(i);
832
+ b.set(data, 8);
833
+ dv.setUint32(8 + data.length, crc32(b.subarray(4, 8 + data.length)));
834
+ return b;
835
+ };
836
+ const ihdr = new Uint8Array(13);
837
+ const dv = new DataView(ihdr.buffer);
838
+ dv.setUint32(0, r.breedte);
839
+ dv.setUint32(4, r.hoogte);
840
+ ihdr[8] = 8;
841
+ ihdr[9] = 0;
842
+ ihdr[10] = 0;
843
+ ihdr[11] = 0;
844
+ ihdr[12] = 0;
845
+ const delen = [new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]), chunk("IHDR", ihdr), chunk("IDAT", pako.deflate(raw)), chunk("IEND", new Uint8Array())];
846
+ const uit = new Uint8Array(delen.reduce((t, d) => t + d.length, 0));
847
+ let o = 0;
848
+ for (const d of delen) {
849
+ uit.set(d, o);
850
+ o += d.length;
851
+ }
852
+ return uit;
853
+ }