@node-projects/web-component-designer-zpl 0.2.1 → 0.3.1

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 (75) hide show
  1. package/README.md +16 -2
  2. package/THIRD-PARTY-LICENSES.md +432 -0
  3. package/dist/assets/fonts/LICENSE-APACHE-2.0.txt +202 -0
  4. package/dist/assets/fonts/NOTICE.md +27 -0
  5. package/dist/assets/fonts/OCRA-NOTICE.md +27 -0
  6. package/dist/assets/fonts/OCRA.ttf +0 -0
  7. package/dist/assets/fonts/OCRB-NOTICE.md +21 -0
  8. package/dist/assets/fonts/OCRB.ttf +0 -0
  9. package/dist/assets/fonts/PrintLabMono-NOTICE.md +24 -0
  10. package/dist/assets/fonts/PrintLabMono.ttf +0 -0
  11. package/dist/assets/fonts/PrintLabZPL-Bold.ttf +0 -0
  12. package/dist/assets/fonts/PrintLabZPL-Bold.woff2 +0 -0
  13. package/dist/assets/fonts/VeraMono-Bold.ttf +0 -0
  14. package/dist/assets/fonts/VeraMono-NOTICE.md +57 -0
  15. package/dist/assets/fonts/VeraMono.ttf +0 -0
  16. package/dist/assets/licenses/BWIP-JS-LICENSE.txt +23 -0
  17. package/dist/assets/licenses/ZPLab-LICENSE.txt +21 -0
  18. package/dist/barcodes/barcodeRegistry.d.ts +124 -0
  19. package/dist/barcodes/barcodeRegistry.js +357 -0
  20. package/dist/barcodes/bwipRenderer.d.ts +70 -0
  21. package/dist/barcodes/bwipRenderer.js +552 -0
  22. package/dist/fonts/zplFonts.d.ts +27 -0
  23. package/dist/fonts/zplFonts.js +85 -0
  24. package/dist/index.d.ts +10 -3
  25. package/dist/index.js +10 -3
  26. package/dist/services/ZplBarcodeElementsService.d.ts +5 -0
  27. package/dist/services/ZplBarcodeElementsService.js +16 -0
  28. package/dist/services/ZplBarcodePropertiesService.d.ts +10 -0
  29. package/dist/services/ZplBarcodePropertiesService.js +59 -0
  30. package/dist/services/ZplDiagonalLineExtension.d.ts +46 -0
  31. package/dist/services/ZplDiagonalLineExtension.js +255 -0
  32. package/dist/services/ZplElementResizeStrategy.d.ts +20 -0
  33. package/dist/services/ZplElementResizeStrategy.js +182 -0
  34. package/dist/services/ZplGraphicResizeStrategy.d.ts +18 -0
  35. package/dist/services/ZplGraphicResizeStrategy.js +81 -0
  36. package/dist/services/ZplIncludeInOutputPropertiesService.d.ts +12 -0
  37. package/dist/services/ZplIncludeInOutputPropertiesService.js +50 -0
  38. package/dist/services/ZplParserService.d.ts +7 -4
  39. package/dist/services/ZplParserService.js +214 -249
  40. package/dist/services/ZplSelectionExtensionProvider.d.ts +9 -0
  41. package/dist/services/ZplSelectionExtensionProvider.js +15 -0
  42. package/dist/services/ZplTextEditExtension.d.ts +20 -0
  43. package/dist/services/ZplTextEditExtension.js +80 -0
  44. package/dist/services/ZplTextPropertiesService.d.ts +8 -0
  45. package/dist/services/ZplTextPropertiesService.js +24 -0
  46. package/dist/services/hiddenMetadata.d.ts +17 -0
  47. package/dist/services/hiddenMetadata.js +95 -0
  48. package/dist/services/zplResizeGeometry.d.ts +6 -0
  49. package/dist/services/zplResizeGeometry.js +8 -0
  50. package/dist/setupZplServiceContainer.js +22 -4
  51. package/dist/widgets/elements.json +0 -24
  52. package/dist/widgets/views/zpl-demo-view.js +2 -1
  53. package/dist/widgets/zpl-barcode.d.ts +68 -14
  54. package/dist/widgets/zpl-barcode.js +162 -74
  55. package/dist/widgets/zpl-comment.d.ts +2 -0
  56. package/dist/widgets/zpl-comment.js +6 -3
  57. package/dist/widgets/zpl-graphic-box.d.ts +12 -0
  58. package/dist/widgets/zpl-graphic-box.js +49 -13
  59. package/dist/widgets/zpl-graphic-circle.d.ts +2 -0
  60. package/dist/widgets/zpl-graphic-circle.js +7 -0
  61. package/dist/widgets/zpl-graphic-diagonal-line.d.ts +2 -0
  62. package/dist/widgets/zpl-graphic-diagonal-line.js +7 -0
  63. package/dist/widgets/zpl-image.d.ts +4 -1
  64. package/dist/widgets/zpl-image.js +17 -14
  65. package/dist/widgets/zpl-text.d.ts +38 -8
  66. package/dist/widgets/zpl-text.js +183 -49
  67. package/dist/zplHelper.d.ts +1 -1
  68. package/dist/zplHelper.js +5 -1
  69. package/package.json +9 -4
  70. package/dist/extensions/ZplLayoutResizeExtensionProvider.d.ts +0 -4
  71. package/dist/extensions/ZplLayoutResizeExtensionProvider.js +0 -16
  72. package/dist/jsBarcodeOptions.d.ts +0 -27
  73. package/dist/jsBarcodeOptions.js +0 -14
  74. package/dist/qr.d.ts +0 -11
  75. package/dist/qr.js +0 -759
@@ -0,0 +1,552 @@
1
+ import bwipjs from '@bwip-js/browser';
2
+ import { getBarcodeDefinition, getDataMatrixVersion, isDataMatrixRectangular } from './barcodeRegistry.js';
3
+ /** The add-on preview and its Labelary demo use the same printer density. */
4
+ export const zplPreviewDpmm = 24;
5
+ const maxicodeWidthMm = 202 / 8;
6
+ const maxicodeHeightMm = 192 / 8;
7
+ const microPdf417Modes = [
8
+ { columns: 1, rows: 11 }, { columns: 1, rows: 14 }, { columns: 1, rows: 17 },
9
+ { columns: 1, rows: 20 }, { columns: 1, rows: 24 }, { columns: 1, rows: 28 },
10
+ { columns: 2, rows: 8 }, { columns: 2, rows: 11 }, { columns: 2, rows: 14 },
11
+ { columns: 2, rows: 17 }, { columns: 2, rows: 20 }, { columns: 2, rows: 23 },
12
+ { columns: 2, rows: 26 }, { columns: 3, rows: 6 }, { columns: 3, rows: 8 },
13
+ { columns: 3, rows: 10 }, { columns: 3, rows: 12 }, { columns: 3, rows: 15 },
14
+ { columns: 3, rows: 20 }, { columns: 3, rows: 26 }, { columns: 3, rows: 32 },
15
+ { columns: 3, rows: 38 }, { columns: 3, rows: 44 }, { columns: 4, rows: 6 },
16
+ { columns: 4, rows: 8 }, { columns: 4, rows: 10 }, { columns: 4, rows: 12 },
17
+ { columns: 4, rows: 15 }, { columns: 4, rows: 20 }, { columns: 4, rows: 26 },
18
+ { columns: 4, rows: 32 }, { columns: 4, rows: 38 }, { columns: 4, rows: 44 },
19
+ { columns: 4, rows: 4 }
20
+ ];
21
+ const tlc39Rows = [6, 8, 10, 12, 15, 20, 26, 32, 38, 44];
22
+ const tlc39LinkGapModules = 10;
23
+ function microPdf417Mode(mode) {
24
+ return microPdf417Modes[mode] ?? microPdf417Modes[0];
25
+ }
26
+ function estimatePdf417Columns(content, securityLevel) {
27
+ const dataCodewords = Math.ceil((content.length || 1) / 2.3);
28
+ const eccCodewords = Math.pow(2, securityLevel + 1);
29
+ return Math.max(1, Math.min(30, Math.floor(Math.sqrt((dataCodewords + eccCodewords) / 4))));
30
+ }
31
+ function code11Weighted(value, maxWeight) {
32
+ let sum = 0;
33
+ for (let i = 0; i < value.length; i++) {
34
+ const character = value[value.length - 1 - i];
35
+ const digit = character === '-' ? 10 : Number(character);
36
+ if (Number.isFinite(digit))
37
+ sum += digit * ((i % maxWeight) + 1);
38
+ }
39
+ const result = sum % 11;
40
+ return result === 10 ? '-' : String(result);
41
+ }
42
+ function code11CheckDigits(value, two) {
43
+ const c = code11Weighted(value, 10);
44
+ return two ? c + code11Weighted(value + c, 9) : c;
45
+ }
46
+ /** Firmware-visible HRI/guard zone in dots, used by both widget and tests. */
47
+ export function barcodeTextZoneDots(props) {
48
+ if (props.type === 'logmars')
49
+ return props.printInterpretation ? 26 : 20;
50
+ if (props.type === 'postnet' || props.type === 'planet')
51
+ return props.printInterpretation ? 20 : 0;
52
+ if (props.type === 'upceanextension')
53
+ return props.printInterpretation ? 18 : 0;
54
+ if (['ean13', 'ean8', 'upca', 'upce'].includes(props.type))
55
+ return props.printInterpretation ? 18 : 13;
56
+ if ('printInterpretation' in props && props.printInterpretation)
57
+ return 7 * (Math.max(1, Math.round(props.moduleWidth)) + 1);
58
+ return 0;
59
+ }
60
+ export function barcodeTextAbove(props) {
61
+ return ((props.type === 'logmars' || props.type === 'upceanextension') &&
62
+ props.printInterpretation) ||
63
+ ('printInterpretationAbove' in props && props.printInterpretationAbove);
64
+ }
65
+ /** LOGMARS always prints its interpretation; its boolean selects above/below. */
66
+ export function barcodeShowsInterpretation(props) {
67
+ return props.type === 'logmars' ||
68
+ ('printInterpretation' in props && props.printInterpretation);
69
+ }
70
+ /** Amount by which visible HRI extends above the ZPL ^FO anchor. */
71
+ export function barcodeFieldOriginAboveOffset(props) {
72
+ return barcodeTextAbove(props) && barcodeShowsInterpretation(props) ? barcodeTextZoneDots(props) : 0;
73
+ }
74
+ /** Visible EAN/UPC digits extend beyond the guard bars on Zebra output. */
75
+ export function barcodeHorizontalInsets(props) {
76
+ if (!('moduleWidth' in props) || !('printInterpretation' in props) || !props.printInterpretation)
77
+ return { left: 0, right: 0 };
78
+ const moduleWidth = Math.max(1, Math.round(props.moduleWidth));
79
+ if (props.type === 'ean13')
80
+ return { left: Math.round(10.5 * moduleWidth), right: 0 };
81
+ if (props.type === 'upca' || props.type === 'upce')
82
+ return { left: 11 * moduleWidth, right: Math.round(6.5 * moduleWidth) };
83
+ return { left: 0, right: 0 };
84
+ }
85
+ const gs1DataBarBcid = {
86
+ 1: 'databaromni', 2: 'databartruncated', 3: 'databarstacked',
87
+ 4: 'databarstackedomni', 5: 'databarlimited', 6: 'databarexpanded',
88
+ 7: 'databarexpandedstacked'
89
+ };
90
+ function gtin14(value) {
91
+ const digits = value.replace(/\D/g, '').padStart(13, '0').slice(0, 13);
92
+ const sum = [...digits].reduce((total, digit, index) => total + Number(digit) * (index % 2 === 0 ? 3 : 1), 0);
93
+ return `${digits}${(10 - sum % 10) % 10}`;
94
+ }
95
+ function cleanError(error) {
96
+ return String(error instanceof Error ? error.message : error)
97
+ .replace(/^bwip-js:\s*/i, '').replace(/^bwipp\.[^:]+:\s*/i, '');
98
+ }
99
+ /** A bare Zebra ^BC starts in Code Set B. BWIP-JS otherwise optimizes an
100
+ * all-numeric value into Set C, producing a much narrower symbol than the
101
+ * printer. Feed BWIP's raw-symbol syntax to preserve Zebra's default. */
102
+ function toCode128BRaw(text) {
103
+ if (!text)
104
+ return null;
105
+ const symbols = ['^104'];
106
+ for (const character of text) {
107
+ const code = character.charCodeAt(0);
108
+ if (code < 32 || code > 126)
109
+ return null;
110
+ symbols.push(`^${String(code - 32).padStart(3, '0')}`);
111
+ }
112
+ return symbols.join('');
113
+ }
114
+ function bwipRetryOptions(options) {
115
+ if (options.bcid === 'azteccodecompact' && options.layers === undefined)
116
+ return { ...options, bcid: 'azteccode' };
117
+ if (options.bcid === 'micropdf417' && options.rows !== undefined) {
118
+ const { rows: _rows, ...retry } = options;
119
+ return retry;
120
+ }
121
+ return null;
122
+ }
123
+ export function buildBwipOptions(props) {
124
+ const definition = getBarcodeDefinition(props.type);
125
+ if (definition.bwipOptions)
126
+ return definition.bwipOptions(props);
127
+ if (definition.resizeKind === 'linear') {
128
+ const v = props;
129
+ let bcid = definition.bcid;
130
+ let text = v.content || definition.defaultContent || '0';
131
+ if (v.type === 'upceanextension')
132
+ bcid = text.length === 2 ? 'ean2' : 'ean5';
133
+ if (v.type === 'upce' && text.length === 6)
134
+ text = `0${text}`;
135
+ if (v.type === 'logmars' || v.type === 'code39' || v.type === 'codabar' || v.type === 'plessey')
136
+ text = text.toUpperCase();
137
+ if (v.type === 'code11')
138
+ text += code11CheckDigits(text, !v.checkDigit);
139
+ if (v.type === 'planet') {
140
+ text = text.replace(/\D/g, '') || '0';
141
+ if (text.length < 11)
142
+ text = text.padStart(11, '0');
143
+ else if (text.length === 12)
144
+ text = text.padStart(13, '0');
145
+ }
146
+ const options = {
147
+ bcid, text, scale: Math.max(1, Math.round(v.moduleWidth)), height: 10,
148
+ includetext: false
149
+ };
150
+ if (v.type === 'code128' && !v.gs1) {
151
+ const raw = toCode128BRaw(text);
152
+ if (raw) {
153
+ options.text = raw;
154
+ options.raw = true;
155
+ }
156
+ }
157
+ if (!['msi', 'plessey', 'postnet', 'planet', 'ean13', 'ean8', 'upca', 'upce', 'upceanextension', 'code49'].includes(v.type)) {
158
+ options.ratio = v.wideRatio;
159
+ }
160
+ if (v.checkDigit && ['code39', 'interleaved2of5', 'msi'].includes(v.type))
161
+ options.includecheck = true;
162
+ if (v.type === 'code93')
163
+ options.includecheck = true;
164
+ if (v.type === 'logmars')
165
+ options.includecheck = true;
166
+ if (v.type === 'code49') {
167
+ options.rowheight = Math.max(8, Math.min(50, Math.round(v.barHeight / Math.max(v.moduleWidth, 1))));
168
+ if (v.mode !== 'A')
169
+ options.mode = Number(v.mode);
170
+ }
171
+ return options;
172
+ }
173
+ switch (props.type) {
174
+ case 'gs1databar': {
175
+ const v = props;
176
+ const text = v.symbology >= 6
177
+ ? (v.content.includes('(') ? v.content : `(01)${gtin14(v.content)}`)
178
+ : `(01)${gtin14(v.content)}`;
179
+ return {
180
+ bcid: gs1DataBarBcid[v.symbology] ?? 'databaromni', text,
181
+ scale: v.magnification, ...(v.symbology === 7 ? { segments: v.segments } : {})
182
+ };
183
+ }
184
+ case 'qrcode': {
185
+ const v = props;
186
+ return { bcid: 'qrcode', text: v.content || ' ', scale: v.magnification, eclevel: v.errorCorrection };
187
+ }
188
+ case 'datamatrix': {
189
+ const v = props;
190
+ const rectangular = isDataMatrixRectangular(v);
191
+ const options = {
192
+ bcid: v.gs1 ? (rectangular ? 'gs1datamatrixrectangular' : 'gs1datamatrix') : (rectangular ? 'datamatrixrectangular' : 'datamatrix'),
193
+ text: v.content || ' ', scale: v.dimension
194
+ };
195
+ const version = getDataMatrixVersion(v);
196
+ if (version)
197
+ options.version = version;
198
+ return options;
199
+ }
200
+ case 'pdf417': {
201
+ const v = props;
202
+ const columns = v.columns || estimatePdf417Columns(v.content, Number(v.securityLevel));
203
+ return { bcid: 'pdf417', text: v.content || ' ', scale: 2,
204
+ rowheight: Math.max(1, Math.round(v.rowHeight / Math.max(v.moduleWidth, 1))),
205
+ columns, eclevel: String(v.securityLevel) };
206
+ }
207
+ case 'micropdf417': {
208
+ const v = props;
209
+ const version = microPdf417Mode(v.mode);
210
+ return { bcid: 'micropdf417', text: v.content || ' ', scale: 2,
211
+ columns: version.columns, rows: version.rows,
212
+ rowheight: Math.max(1, Math.round(v.rowHeight / Math.max(v.moduleWidth, 1))) };
213
+ }
214
+ case 'codablock': {
215
+ const v = props;
216
+ return { bcid: 'codablockf', text: v.content || ' ', scale: 2,
217
+ columns: Math.max(4, v.columns), rowheight: Math.max(8, Math.round(v.rowHeight / Math.max(v.moduleWidth, 1))) };
218
+ }
219
+ case 'aztec': {
220
+ const v = props;
221
+ const options = { bcid: 'azteccodecompact', text: v.content || ' ', scale: v.magnification };
222
+ if (v.ecLevel === 300)
223
+ Object.assign(options, { bcid: 'azteccode', format: 'rune' });
224
+ else if (v.ecLevel >= 201 && v.ecLevel <= 232)
225
+ Object.assign(options, { bcid: 'azteccode', format: 'full', layers: v.ecLevel - 200 });
226
+ else if (v.ecLevel >= 101 && v.ecLevel <= 104)
227
+ options.layers = v.ecLevel - 100;
228
+ else if (v.ecLevel >= 5 && v.ecLevel <= 95)
229
+ options.eclevel = v.ecLevel;
230
+ return options;
231
+ }
232
+ case 'maxicode': {
233
+ const v = props;
234
+ return { bcid: 'maxicode', text: v.content || ' ', scale: 2, mode: v.mode };
235
+ }
236
+ case 'tlc39': {
237
+ const v = props;
238
+ return { bcid: 'code39', text: v.content.split(',')[0] || '0', scale: v.moduleWidth,
239
+ height: 10, includetext: false, ratio: v.wideRatio };
240
+ }
241
+ }
242
+ return { bcid: definition.bcid, text: ' ', scale: 2 };
243
+ }
244
+ /** Returns the encoder's unscaled module bounds without a canvas or network. */
245
+ export function measureBarcodeModules(props) {
246
+ const validationError = getBarcodeDefinition(props.type).validate(props);
247
+ if (validationError)
248
+ throw new Error(validationError);
249
+ const { scale: _scale, ...options } = buildBwipOptions(props);
250
+ const engine = bwipjs;
251
+ let symbol;
252
+ try {
253
+ symbol = engine.raw(options)[0];
254
+ }
255
+ catch (error) {
256
+ const retry = bwipRetryOptions(options);
257
+ if (!retry)
258
+ throw error;
259
+ symbol = engine.raw(retry)[0];
260
+ }
261
+ if (!symbol)
262
+ throw new Error('BWIP-JS returned no geometry');
263
+ if (symbol.pixx != null && symbol.pixy != null)
264
+ return { width: symbol.pixx, height: symbol.pixy };
265
+ const width = symbol.sbs?.reduce((total, value) => total + value, 0) ?? 0;
266
+ const height = Math.max(0, ...(symbol.bhs ?? []).map((value, index) => value + (symbol.bbs?.[index] ?? 0)));
267
+ return { width, height };
268
+ }
269
+ /**
270
+ * Returns the preview bounds in ZPL dots.
271
+ *
272
+ * BWIP-JS deliberately renders QR, DataMatrix, and Aztec modules as two canvas
273
+ * pixels at scale 1. Keeping that larger bitmap is useful on HiDPI displays,
274
+ * but its backing-store dimensions must not become the designer dimensions.
275
+ */
276
+ export function measureBarcodePreview(props, fallback) {
277
+ switch (props.type) {
278
+ case 'qrcode': {
279
+ const modules = measureBarcodeModules(props);
280
+ const size = props.magnification;
281
+ return { width: modules.width * size, height: modules.height * size };
282
+ }
283
+ case 'datamatrix': {
284
+ const modules = measureBarcodeModules(props);
285
+ const size = props.dimension;
286
+ return { width: modules.width * size, height: modules.height * size };
287
+ }
288
+ case 'aztec': {
289
+ const modules = measureBarcodeModules(props);
290
+ const size = props.magnification;
291
+ return { width: modules.width * size, height: modules.height * size };
292
+ }
293
+ case 'gs1databar': {
294
+ const value = props;
295
+ const heightModules = {
296
+ 1: 33, 2: 13, 3: 14, 4: 72, 5: 10, 6: 34
297
+ };
298
+ const modules = heightModules[value.symbology];
299
+ return { width: fallback.width, height: modules == null ? fallback.height : modules * value.magnification };
300
+ }
301
+ case 'pdf417': {
302
+ const value = props;
303
+ const columns = value.columns || estimatePdf417Columns(value.content, Number(value.securityLevel));
304
+ const rows = Math.max(1, fallback.height / 6);
305
+ return { width: (17 * (columns + 4) + 1) * value.moduleWidth, height: rows * value.rowHeight };
306
+ }
307
+ case 'micropdf417': {
308
+ const value = props;
309
+ const version = microPdf417Mode(value.mode);
310
+ return { width: fallback.width / 2 * value.moduleWidth, height: version.rows * value.rowHeight };
311
+ }
312
+ case 'codablock': {
313
+ const value = props;
314
+ const rowHeightUnits = Math.max(8, Math.round(value.rowHeight / Math.max(value.moduleWidth, 1)));
315
+ return {
316
+ width: (fallback.width / 2 + 6) * value.moduleWidth,
317
+ height: Math.max(1, Math.round(fallback.height / 2 * (value.rowHeight / rowHeightUnits)))
318
+ };
319
+ }
320
+ case 'maxicode':
321
+ return {
322
+ width: Math.round(maxicodeWidthMm * zplPreviewDpmm),
323
+ height: Math.round(maxicodeHeightMm * zplPreviewDpmm)
324
+ };
325
+ case 'ean13':
326
+ case 'upca':
327
+ case 'upce': {
328
+ const insets = barcodeHorizontalInsets(props);
329
+ return { width: fallback.width + insets.left + insets.right, height: fallback.height };
330
+ }
331
+ default:
332
+ return fallback;
333
+ }
334
+ }
335
+ /** Zebra-width bar geometry shared by the renderer and offline regression tests. */
336
+ export function getZebraWidthBarGeometry(props) {
337
+ if (!['plessey', 'planet', 'postnet'].includes(props.type))
338
+ return null;
339
+ const options = buildBwipOptions(props);
340
+ const engine = bwipjs;
341
+ // The raw postal height flags are 0.125/0.05 only when BWIPP owns the
342
+ // nominal height. Passing the display height rewrites them to 10/4 and
343
+ // makes a fixed absolute threshold classify every bar as tall.
344
+ const raw = engine.raw({ bcid: options.bcid, text: options.text })[0];
345
+ const moduleWidth = Math.max(1, Math.round(props.moduleWidth));
346
+ const height = Math.max(1, Math.round(props.barHeight));
347
+ const rects = [];
348
+ let width = 0;
349
+ if (props.type === 'plessey' && raw.sbs) {
350
+ const runs = raw.sbs.map((value, index) => index % 2 === 0
351
+ ? value === 1 ? 1 : value === 3 ? 2 : value === 5 ? 3 : 0
352
+ : value === 2 ? 1 : value === 4 ? 2 : 0);
353
+ if (runs.some(value => value === 0))
354
+ throw new Error('BWIP-JS returned unsupported Plessey geometry');
355
+ runs.forEach((modules, index) => {
356
+ const runWidth = modules * moduleWidth;
357
+ if (index % 2 === 0)
358
+ rects.push({ x: width, y: 0, width: runWidth, height });
359
+ width += runWidth;
360
+ });
361
+ }
362
+ else if (raw.bhs) {
363
+ const pitch = 2.5 * moduleWidth;
364
+ const shortHeight = Math.round(height * .4);
365
+ raw.bhs.forEach((barHeight, index) => {
366
+ const tall = barHeight >= .1;
367
+ rects.push({ x: Math.round(index * pitch), y: tall ? 0 : height - shortHeight,
368
+ width: moduleWidth, height: tall ? height : shortHeight });
369
+ });
370
+ width = raw.bhs.length ? Math.round((raw.bhs.length - 1) * pitch) + moduleWidth : 0;
371
+ }
372
+ return { rects, width, height };
373
+ }
374
+ function tlc39Code39Runs(text, wideRatio) {
375
+ const engine = bwipjs;
376
+ const raw = engine.raw({ bcid: 'code39', text: text || ' ' })[0];
377
+ if (!raw?.sbs?.length)
378
+ throw new Error('BWIP-JS returned no TLC39 Code 39 geometry');
379
+ const runs = raw.sbs.map((value, index) => index % 10 === 9 ? 1 : Number(value) >= 3 ? wideRatio : 1);
380
+ if (runs.length % 10 === 0)
381
+ runs.pop();
382
+ return runs;
383
+ }
384
+ function firstTlc39MicroPdf(serial) {
385
+ const engine = bwipjs;
386
+ for (const rows of tlc39Rows) {
387
+ try {
388
+ const svg = engine.toSVG({ bcid: 'micropdf417', text: serial, scale: 2, columns: 4, rows });
389
+ const viewBox = svg.match(/viewBox="0 0 ([\d.]+) ([\d.]+)"/);
390
+ if (viewBox)
391
+ return { rows, width: Number(viewBox[1]), height: Number(viewBox[2]) };
392
+ }
393
+ catch { /* try the next firmware-supported row count */ }
394
+ }
395
+ return null;
396
+ }
397
+ /** Composite TLC39 footprint adapted from ZPLab's ZD230-measured layout. */
398
+ export function getTlc39Geometry(props) {
399
+ const comma = props.content.indexOf(',');
400
+ const eci = comma < 0 ? props.content : props.content.slice(0, comma);
401
+ const serial = comma < 0 ? '' : props.content.slice(comma + 1);
402
+ const runs = tlc39Code39Runs(eci, props.wideRatio);
403
+ const micro = serial ? firstTlc39MicroPdf(serial) : null;
404
+ let tallFromModule = null;
405
+ if (micro) {
406
+ const linkRuns = tlc39Code39Runs('T', props.wideRatio).slice(10, 19);
407
+ if (linkRuns.length === 9) {
408
+ tallFromModule = runs.reduce((sum, value) => sum + value, 0) + tlc39LinkGapModules;
409
+ runs.push(tlc39LinkGapModules, ...linkRuns);
410
+ }
411
+ }
412
+ const code39Width = Math.round(runs.reduce((sum, value) => sum + value, 0) * props.moduleWidth);
413
+ const code39Height = Math.max(1, Math.round(props.barHeight));
414
+ if (!micro)
415
+ return {
416
+ runs, tallFromModule, width: code39Width, height: code39Height,
417
+ code39Y: 0, stopOverhang: 0, microPdf: null
418
+ };
419
+ const microWidth = Math.round(micro.width / 2 * props.microPdfModuleWidth);
420
+ const microHeight = micro.rows * props.microPdfRowHeight;
421
+ const microX = Math.round(props.moduleWidth);
422
+ const gap = Math.round(props.moduleWidth);
423
+ const stopOverhang = Math.round(code39Height / 5);
424
+ const microY = Math.max(0, stopOverhang - (microHeight + gap));
425
+ const code39Y = microY + microHeight + gap;
426
+ return {
427
+ runs, tallFromModule,
428
+ width: Math.max(code39Width, microX + microWidth),
429
+ height: code39Y + code39Height + stopOverhang,
430
+ code39Y, stopOverhang,
431
+ microPdf: { x: microX, y: microY, width: microWidth, height: microHeight, rows: micro.rows }
432
+ };
433
+ }
434
+ function renderTlc39Canvas(props) {
435
+ const geometry = getTlc39Geometry(props);
436
+ const canvas = document.createElement('canvas');
437
+ canvas.width = geometry.width;
438
+ canvas.height = geometry.height;
439
+ const context = canvas.getContext('2d');
440
+ if (!context)
441
+ return null;
442
+ context.fillStyle = 'black';
443
+ let xModules = 0;
444
+ geometry.runs.forEach((modules, index) => {
445
+ if (index % 2 === 0) {
446
+ const tall = geometry.tallFromModule != null && xModules >= geometry.tallFromModule;
447
+ const x0 = Math.round(xModules * props.moduleWidth);
448
+ const x1 = Math.round((xModules + modules) * props.moduleWidth);
449
+ context.fillRect(x0, tall ? geometry.code39Y - geometry.stopOverhang : geometry.code39Y, Math.max(1, x1 - x0), props.barHeight + (tall ? 2 * geometry.stopOverhang : 0));
450
+ }
451
+ xModules += modules;
452
+ });
453
+ if (geometry.microPdf) {
454
+ const source = document.createElement('canvas');
455
+ const serial = props.content.slice(props.content.indexOf(',') + 1);
456
+ try {
457
+ bwipjs
458
+ .toCanvas(source, { bcid: 'micropdf417', text: serial, scale: 2, columns: 4, rows: geometry.microPdf.rows });
459
+ context.imageSmoothingEnabled = false;
460
+ context.drawImage(source, geometry.microPdf.x, geometry.microPdf.y, geometry.microPdf.width, geometry.microPdf.height);
461
+ }
462
+ catch { /* the Code 39 component remains a stable fallback */ }
463
+ }
464
+ return canvas;
465
+ }
466
+ /** Headless preview footprint used by the widget and regression fixtures. */
467
+ export function measureBarcodeBounds(props) {
468
+ if (props.type === 'tlc39') {
469
+ const geometry = getTlc39Geometry(props);
470
+ return { width: geometry.width, height: geometry.height };
471
+ }
472
+ if ('moduleWidth' in props) {
473
+ const zebra = getZebraWidthBarGeometry(props);
474
+ if (zebra)
475
+ return { width: zebra.width, height: zebra.height };
476
+ }
477
+ const options = buildBwipOptions(props);
478
+ const engine = bwipjs;
479
+ let svg;
480
+ try {
481
+ svg = engine.toSVG(options);
482
+ }
483
+ catch (error) {
484
+ const retry = bwipRetryOptions(options);
485
+ if (!retry)
486
+ throw error;
487
+ svg = engine.toSVG(retry);
488
+ }
489
+ const viewBox = svg.match(/viewBox="0 0 ([\d.]+) ([\d.]+)"/);
490
+ if (!viewBox)
491
+ throw new Error('BWIP-JS returned SVG without viewBox geometry');
492
+ return measureBarcodePreview(props, { width: Number(viewBox[1]), height: Number(viewBox[2]) });
493
+ }
494
+ function renderZebraWidthBars(props) {
495
+ const geometry = getZebraWidthBarGeometry(props);
496
+ if (!geometry)
497
+ return null;
498
+ const canvas = document.createElement('canvas');
499
+ canvas.width = geometry.width;
500
+ canvas.height = geometry.height;
501
+ const context = canvas.getContext('2d');
502
+ if (!context)
503
+ return null;
504
+ context.fillStyle = 'black';
505
+ for (const rectangle of geometry.rects)
506
+ context.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
507
+ return canvas;
508
+ }
509
+ function cropMaxicodeCanvas(source) {
510
+ if (source.width < 2 || source.height < 3)
511
+ return source;
512
+ const canvas = document.createElement('canvas');
513
+ canvas.width = source.width - 1;
514
+ canvas.height = source.height - 2;
515
+ const context = canvas.getContext('2d');
516
+ if (!context)
517
+ return source;
518
+ context.drawImage(source, 0, 1, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height);
519
+ return canvas;
520
+ }
521
+ export function renderBarcode(props) {
522
+ try {
523
+ const validationError = getBarcodeDefinition(props.type).validate(props);
524
+ if (validationError)
525
+ return { canvas: null, error: validationError, width: 0, height: 0 };
526
+ let canvas = props.type === 'tlc39' ? renderTlc39Canvas(props)
527
+ : 'moduleWidth' in props ? renderZebraWidthBars(props) : null;
528
+ if (!canvas) {
529
+ canvas = document.createElement('canvas');
530
+ const engine = bwipjs;
531
+ const options = buildBwipOptions(props);
532
+ try {
533
+ engine.toCanvas(canvas, options);
534
+ }
535
+ catch (error) {
536
+ const retry = bwipRetryOptions(options);
537
+ if (!retry)
538
+ throw error;
539
+ engine.toCanvas(canvas, retry);
540
+ }
541
+ }
542
+ if (props.type === 'maxicode')
543
+ canvas = cropMaxicodeCanvas(canvas);
544
+ const bounds = measureBarcodeBounds(props);
545
+ canvas.style.width = `${bounds.width}px`;
546
+ canvas.style.height = `${bounds.height}px`;
547
+ return { canvas, error: null, ...bounds };
548
+ }
549
+ catch (error) {
550
+ return { canvas: null, error: cleanError(error), width: 0, height: 0 };
551
+ }
552
+ }
@@ -0,0 +1,27 @@
1
+ export type ZplFontName = '0' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H';
2
+ export interface ZplDeviceFontMetrics {
3
+ fontSize: number;
4
+ scaleX: number;
5
+ /** Final Zebra character-cell advance in dots, after magnification. */
6
+ characterAdvance: number;
7
+ xOffset: number;
8
+ yOffset: number;
9
+ letterSpacing: number;
10
+ }
11
+ export interface ZplDeviceFontMagnification {
12
+ height: number;
13
+ width: number;
14
+ heightStep: number;
15
+ widthStep: number;
16
+ }
17
+ export declare const zplFontFamilies: Record<ZplFontName, string>;
18
+ /** Resolve the printer's discrete bitmap-font magnifications. Width 0 means
19
+ * use the height magnification, rather than a zero-width glyph cell. */
20
+ export declare function getDeviceFontMagnification(font: ZplFontName, height: number, width: number): ZplDeviceFontMagnification | null;
21
+ export declare function loadZplFonts(document: Document): Promise<void>;
22
+ export declare function getDeviceFontMetrics(font: ZplFontName, height: number, width: number): ZplDeviceFontMetrics | null;
23
+ /** Numeric ZPL width that represents the natural cell at a given requested
24
+ * height. Used only when an automatic-width field is stretched
25
+ * disproportionately and therefore has to become explicit. */
26
+ export declare function getNaturalZplFontWidth(font: ZplFontName, height: number): number;
27
+ export declare function applyDeviceFontCase(font: ZplFontName, content: string): string;
@@ -0,0 +1,85 @@
1
+ const deviceFonts = {
2
+ A: { magStep: 9, magWidthStep: 5, advancePerMag: 6, capInkPerMag: 7.125, capPerEm: .757, advancePerEm: .602, yOffsetEm: .096, xOffsetEm: -.032, widthCorrection: .964, letterSpacingEm: .021 },
3
+ B: { magStep: 11, magWidthStep: 7, advancePerMag: 9, capInkPerMag: 11.5, capPerEm: .762, advancePerEm: .602, xOffsetEm: -.040 },
4
+ C: { magStep: 18, magWidthStep: 10, advancePerMag: 12, capInkPerMag: 14.75, capPerEm: .757, advancePerEm: .602, yOffsetEm: .026, xOffsetEm: -.043 },
5
+ D: { magStep: 18, magWidthStep: 10, advancePerMag: 12, capInkPerMag: 14.75, capPerEm: .757, advancePerEm: .602, yOffsetEm: .026, xOffsetEm: -.043 },
6
+ E: { magStep: 28, magWidthStep: 15, advancePerMag: 20, capInkPerMag: 21.875, capPerEm: .787, advancePerEm: .723, yOffsetEm: .198, xOffsetEm: -.189 },
7
+ F: { magStep: 26, magWidthStep: 13, advancePerMag: 16, capInkPerMag: 22, capPerEm: .757, advancePerEm: .602, yOffsetEm: .034, xOffsetEm: -.052 },
8
+ G: { magStep: 60, magWidthStep: 40, advancePerMag: 48, capInkPerMag: 48, capPerEm: .757, advancePerEm: .602, yOffsetEm: .055, widthCorrection: .817, letterSpacingEm: .142 },
9
+ H: { magStep: 21, magWidthStep: 13, advancePerMag: 19, capInkPerMag: 21, capPerEm: .780, advancePerEm: .723, yOffsetEm: -.030, xOffsetEm: -.193 }
10
+ };
11
+ export const zplFontFamilies = {
12
+ '0': 'ZplPrintLab', A: 'ZplPrintLabMono', B: 'ZplVeraMonoBold',
13
+ C: 'ZplPrintLabMono', D: 'ZplPrintLabMono', E: 'ZplOCRB',
14
+ F: 'ZplPrintLabMono', G: 'ZplPrintLabMono', H: 'ZplOCRA'
15
+ };
16
+ const documentLoads = new WeakMap();
17
+ const deviceMagnification = (requested, step) => Math.min(10, Math.max(1, Math.round(requested / step)));
18
+ /** Resolve the printer's discrete bitmap-font magnifications. Width 0 means
19
+ * use the height magnification, rather than a zero-width glyph cell. */
20
+ export function getDeviceFontMagnification(font, height, width) {
21
+ if (font === '0' || !(height > 0))
22
+ return null;
23
+ const spec = deviceFonts[font];
24
+ const heightMagnification = deviceMagnification(height, spec.magStep);
25
+ return {
26
+ height: heightMagnification,
27
+ width: width > 0 ? deviceMagnification(width, spec.magWidthStep) : heightMagnification,
28
+ heightStep: spec.magStep,
29
+ widthStep: spec.magWidthStep
30
+ };
31
+ }
32
+ export async function loadZplFonts(document) {
33
+ let loading = documentLoads.get(document);
34
+ if (!loading) {
35
+ const sources = [
36
+ ['ZplPrintLab', 'PrintLabZPL-Bold.woff2'],
37
+ ['ZplPrintLabMono', 'PrintLabMono.ttf'],
38
+ ['ZplVeraMono', 'VeraMono.ttf'],
39
+ ['ZplVeraMonoBold', 'VeraMono-Bold.ttf'],
40
+ ['ZplOCRB', 'OCRB.ttf'],
41
+ ['ZplOCRA', 'OCRA.ttf']
42
+ ];
43
+ loading = Promise.all(sources.map(async ([family, file]) => {
44
+ const url = new URL(`../assets/fonts/${file}`, import.meta.url);
45
+ const face = new FontFace(family, `url(${url})`);
46
+ await face.load();
47
+ document.fonts.add(face);
48
+ })).then(() => undefined);
49
+ documentLoads.set(document, loading);
50
+ }
51
+ await loading;
52
+ }
53
+ export function getDeviceFontMetrics(font, height, width) {
54
+ const magnification = getDeviceFontMagnification(font, height, width);
55
+ if (!magnification)
56
+ return null;
57
+ const spec = deviceFonts[font];
58
+ const fontSize = magnification.height * spec.capInkPerMag / spec.capPerEm;
59
+ const scaleX = magnification.width * spec.advancePerMag / (fontSize * spec.advancePerEm) * (spec.widthCorrection ?? 1);
60
+ return {
61
+ fontSize,
62
+ scaleX,
63
+ characterAdvance: magnification.width * spec.advancePerMag,
64
+ xOffset: (spec.xOffsetEm ?? 0) * fontSize,
65
+ yOffset: (spec.yOffsetEm ?? 0) * fontSize,
66
+ letterSpacing: (spec.letterSpacingEm ?? 0) * fontSize
67
+ };
68
+ }
69
+ /** Numeric ZPL width that represents the natural cell at a given requested
70
+ * height. Used only when an automatic-width field is stretched
71
+ * disproportionately and therefore has to become explicit. */
72
+ export function getNaturalZplFontWidth(font, height) {
73
+ if (font === '0')
74
+ return Math.max(1, Math.round(height));
75
+ const spec = deviceFonts[font];
76
+ const magnificationHeight = deviceMagnification(height, spec.magStep);
77
+ return magnificationHeight * spec.magWidthStep;
78
+ }
79
+ export function applyDeviceFontCase(font, content) {
80
+ if (font === 'B')
81
+ return content.toUpperCase();
82
+ if (font === 'H')
83
+ return content.replace(/[a-z]/g, '');
84
+ return content;
85
+ }