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