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

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 CHANGED
@@ -23,6 +23,13 @@ See the ZPL designer sample application.
23
23
  Parts of the barcode mappings, preview geometry, resizing behavior, font metrics,
24
24
  and bundled printer-preview fonts were adapted from
25
25
  [ZPLab](https://github.com/u8array/ZPLab).
26
+ We sincerely thank [u8array](https://github.com/u8array) and the ZPLab project
27
+ for making this excellent work available to the community.
28
+
29
+ If you are looking for a complete standalone ZPL label designer,
30
+ [ZPLab](https://zplab.org/) is an excellent choice. This package serves a
31
+ different purpose: it provides embeddable components and services for integrating
32
+ ZPL label design into another application.
26
33
 
27
34
  See [THIRD-PARTY-LICENSES.md](./THIRD-PARTY-LICENSES.md) for the complete ZPLab,
28
35
  BWIP-JS, and font notices. The add-on's original source remains MIT licensed;
@@ -1,4 +1,4 @@
1
- import { BarcodeProps, LinearBarcodeProps, Tlc39BarcodeProps } from './barcodeRegistry.js';
1
+ import { BarcodeProps, BarcodeType, LinearBarcodeProps, Tlc39BarcodeProps } from './barcodeRegistry.js';
2
2
  export interface BarcodeRenderResult {
3
3
  canvas: HTMLCanvasElement | null;
4
4
  error: string | null;
@@ -16,6 +16,13 @@ export interface BarcodeBarRectangle {
16
16
  readonly width: number;
17
17
  readonly height: number;
18
18
  }
19
+ export type EanUpcType = 'ean13' | 'ean8' | 'upca' | 'upce';
20
+ export interface EanUpcHriFragment {
21
+ readonly char: string;
22
+ /** Horizontal digit centre in unscaled barcode modules. */
23
+ readonly xModule: number;
24
+ }
25
+ export declare const eanUpcTypes: ReadonlySet<BarcodeType>;
19
26
  /** The add-on preview and its Labelary demo use the same printer density. */
20
27
  export declare const zplPreviewDpmm = 24;
21
28
  /** Firmware-visible HRI/guard zone in dots, used by both widget and tests. */
@@ -23,6 +30,10 @@ export declare function barcodeTextZoneDots(props: BarcodeProps): number;
23
30
  export declare function barcodeTextAbove(props: BarcodeProps): boolean;
24
31
  /** LOGMARS always prints its interpretation; its boolean selects above/below. */
25
32
  export declare function barcodeShowsInterpretation(props: BarcodeProps): boolean;
33
+ /** Text printed beneath/above the bars by Zebra firmware. Code 39 start and
34
+ * stop characters are generated by the printer and are visible in its HRI,
35
+ * but must not be added to ^FD or encoded as data. */
36
+ export declare function barcodeInterpretationText(props: BarcodeProps): string;
26
37
  /** Amount by which visible HRI extends above the ZPL ^FO anchor. */
27
38
  export declare function barcodeFieldOriginAboveOffset(props: BarcodeProps): number;
28
39
  /** Visible EAN/UPC digits extend beyond the guard bars on Zebra output. */
@@ -30,6 +41,15 @@ export declare function barcodeHorizontalInsets(props: BarcodeProps): Readonly<{
30
41
  left: number;
31
42
  right: number;
32
43
  }>;
44
+ /** Zebra-compatible system/data/check digit positions. BWIP exposes these as
45
+ * raw module coordinates, including the floated UPC/EAN digits. */
46
+ export declare function getEanUpcHriFragments(props: BarcodeProps): readonly EanUpcHriFragment[];
47
+ /** EAN/UPC HRI uses the same discrete firmware font sizes as ZPLab/Labelary. */
48
+ export declare function getEanUpcHriStyle(moduleWidth: number): {
49
+ readonly fontSize: 28 | 18 | 8 | 56;
50
+ readonly gap: 4 | 5 | 7;
51
+ readonly fontFamily: "ZplVeraMono" | "ZplOCRB";
52
+ };
33
53
  export declare function buildBwipOptions(props: BarcodeProps): Record<string, unknown>;
34
54
  /** Returns the encoder's unscaled module bounds without a canvas or network. */
35
55
  export declare function measureBarcodeModules(props: BarcodeProps): BarcodeModuleGeometry;
@@ -1,5 +1,6 @@
1
1
  import bwipjs from '@bwip-js/browser';
2
2
  import { getBarcodeDefinition, getDataMatrixVersion, isDataMatrixRectangular } from './barcodeRegistry.js';
3
+ export const eanUpcTypes = new Set(['ean13', 'ean8', 'upca', 'upce']);
3
4
  /** The add-on preview and its Labelary demo use the same printer density. */
4
5
  export const zplPreviewDpmm = 24;
5
6
  const maxicodeWidthMm = 202 / 8;
@@ -67,6 +68,19 @@ export function barcodeShowsInterpretation(props) {
67
68
  return props.type === 'logmars' ||
68
69
  ('printInterpretation' in props && props.printInterpretation);
69
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
+ }
70
84
  /** Amount by which visible HRI extends above the ZPL ^FO anchor. */
71
85
  export function barcodeFieldOriginAboveOffset(props) {
72
86
  return barcodeTextAbove(props) && barcodeShowsInterpretation(props) ? barcodeTextZoneDots(props) : 0;
@@ -82,6 +96,39 @@ export function barcodeHorizontalInsets(props) {
82
96
  return { left: 11 * moduleWidth, right: Math.round(6.5 * moduleWidth) };
83
97
  return { left: 0, right: 0 };
84
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
+ }
85
132
  const gs1DataBarBcid = {
86
133
  1: 'databaromni', 2: 'databartruncated', 3: 'databarstacked',
87
134
  4: 'databarstackedomni', 5: 'databarlimited', 6: 'databarexpanded',
@@ -506,6 +553,36 @@ function renderZebraWidthBars(props) {
506
553
  context.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
507
554
  return canvas;
508
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
+ }
509
586
  function cropMaxicodeCanvas(source) {
510
587
  if (source.width < 2 || source.height < 3)
511
588
  return source;
@@ -523,8 +600,9 @@ export function renderBarcode(props) {
523
600
  const validationError = getBarcodeDefinition(props.type).validate(props);
524
601
  if (validationError)
525
602
  return { canvas: null, error: validationError, width: 0, height: 0 };
526
- let canvas = props.type === 'tlc39' ? renderTlc39Canvas(props)
527
- : 'moduleWidth' in props ? renderZebraWidthBars(props) : null;
603
+ let canvas = eanUpcTypes.has(props.type) ? renderEanUpcBars(props)
604
+ : props.type === 'tlc39' ? renderTlc39Canvas(props)
605
+ : 'moduleWidth' in props ? renderZebraWidthBars(props) : null;
528
606
  if (!canvas) {
529
607
  canvas = document.createElement('canvas');
530
608
  const engine = bwipjs;
@@ -1,7 +1,7 @@
1
1
  import { BaseCustomWebComponentConstructorAppend, css, html } from '@node-projects/base-custom-webcomponent';
2
2
  import { zplPreviewDpmm } from '../../barcodes/bwipRenderer.js';
3
3
  export class ZplDemoView extends BaseCustomWebComponentConstructorAppend {
4
- static template = html `<h2>Label generated via http://api.labelary.com/</h2><br><img id="image">`;
4
+ static template = html `<div id="side"><span>Label generated via http://api.labelary.com/</span></div><img id="image">`;
5
5
  static style = css `
6
6
  :host {
7
7
  display: block;
@@ -11,8 +11,32 @@ export class ZplDemoView extends BaseCustomWebComponentConstructorAppend {
11
11
  width: 100%;
12
12
  position: relative;
13
13
  }
14
+ #side {
15
+ position: absolute;
16
+ left: 0;
17
+ top: 0;
18
+ height: 100%;
19
+ width: 24px;
20
+ background: var(--wcd-demo-view-toolbar-background, black);
21
+ }
22
+ #side > span {
23
+ color: var(--wcd-color-text, white);
24
+ rotate: 270deg;
25
+ display: block;
26
+ position: absolute;
27
+ top: 304px;
28
+ left: -305px;
29
+ font-weight: 600;
30
+ font-family: monospace;
31
+ font-size: 24px;
32
+ white-space: nowrap;
33
+ }
14
34
  #image {
15
35
  border: solid 1px black;
36
+ position: absolute;
37
+ top: 0;
38
+ left: 24px;
39
+ border: 0;
16
40
  }`;
17
41
  constructor() {
18
42
  super();
@@ -1,7 +1,7 @@
1
1
  import { BaseCustomWebComponentConstructorAppend, css, html } from '@node-projects/base-custom-webcomponent';
2
2
  import { getZplCoordinates } from '../zplHelper.js';
3
3
  import { barcodeObservedAttributes, getBarcodeDefinition, getBarcodeFieldOriginOffset, readBarcodeProps } from '../barcodes/barcodeRegistry.js';
4
- import { barcodeFieldOriginAboveOffset, barcodeHorizontalInsets, barcodeShowsInterpretation, barcodeTextAbove, barcodeTextZoneDots, renderBarcode } from '../barcodes/bwipRenderer.js';
4
+ import { barcodeFieldOriginAboveOffset, barcodeHorizontalInsets, barcodeInterpretationText, barcodeShowsInterpretation, barcodeTextAbove, barcodeTextZoneDots, eanUpcTypes, getEanUpcHriFragments, getEanUpcHriStyle, renderBarcode } from '../barcodes/bwipRenderer.js';
5
5
  export class ZplBarcode extends BaseCustomWebComponentConstructorAppend {
6
6
  static style = css `
7
7
  :host { display: block; min-width: 1px; min-height: 1px; }
@@ -10,6 +10,8 @@ export class ZplBarcode extends BaseCustomWebComponentConstructorAppend {
10
10
  canvas { display: block; image-rendering: pixelated; }
11
11
  #hri { position: absolute; left: 0; width: 100%; text-align: center; color: black;
12
12
  font-family: "ZplVeraMono", monospace; white-space: nowrap; }
13
+ #hri.ean-upc { text-align: left; overflow: visible; }
14
+ #hri.ean-upc > span { position: absolute; top: 0; text-align: center; }
13
15
  #error { width: 160px; min-height: 48px; padding: 6px; border: 1px dashed #c62828;
14
16
  color: #8e0000; background: #fff4f4; font: 11px sans-serif; overflow: hidden; }
15
17
  `;
@@ -90,7 +92,13 @@ export class ZplBarcode extends BaseCustomWebComponentConstructorAppend {
90
92
  const definition = getBarcodeDefinition(props.type);
91
93
  const result = renderBarcode(props);
92
94
  this._surface.replaceChildren();
95
+ this._hri.replaceChildren();
93
96
  this._hri.hidden = true;
97
+ this._hri.className = '';
98
+ this._hri.style.left = '';
99
+ this._hri.style.width = '';
100
+ this._hri.style.fontFamily = '';
101
+ this._hri.style.textAlign = '';
94
102
  this._error.hidden = true;
95
103
  this._frame.style.transform = '';
96
104
  this._frame.style.width = '';
@@ -110,8 +118,9 @@ export class ZplBarcode extends BaseCustomWebComponentConstructorAppend {
110
118
  let uprightHeight = result.height;
111
119
  if (definition.resizeKind === 'linear') {
112
120
  const linear = props;
121
+ const isEanUpc = eanUpcTypes.has(props.type);
113
122
  canvas.style.width = `${uprightWidth - horizontalInsets.left - horizontalInsets.right}px`;
114
- canvas.style.height = `${linear.barHeight}px`;
123
+ canvas.style.height = `${linear.barHeight + (isEanUpc ? 13 : 0)}px`;
115
124
  uprightHeight = linear.barHeight;
116
125
  const showHri = barcodeShowsInterpretation(props);
117
126
  const textZone = barcodeTextZoneDots(props);
@@ -129,18 +138,36 @@ export class ZplBarcode extends BaseCustomWebComponentConstructorAppend {
129
138
  if (showHri) {
130
139
  const genericHri = ['code128', 'code39', 'code93', 'code11', 'interleaved2of5',
131
140
  'standard2of5', 'industrial2of5', 'codabar', 'msi'].includes(props.type);
132
- const hriFontSize = genericHri ? 9.7 * Math.max(1, Math.round(linear.moduleWidth)) : 12;
133
- this._hri.textContent = linear.content;
141
+ const eanStyle = isEanUpc ? getEanUpcHriStyle(linear.moduleWidth) : null;
142
+ const hriFontSize = eanStyle?.fontSize ?? (genericHri ? 9.7 * Math.max(1, Math.round(linear.moduleWidth)) : 12);
134
143
  this._hri.style.height = `${textZone}px`;
135
144
  this._hri.style.fontSize = `${hriFontSize}px`;
136
145
  this._hri.style.lineHeight = `${hriFontSize}px`;
146
+ this._hri.style.fontFamily = eanStyle ? eanStyle.fontFamily : '';
137
147
  this._hri.style.letterSpacing = genericHri ? '.6px' : 'normal';
138
148
  this._hri.style.transform = genericHri ? 'translateX(-3px)' : '';
139
- // Zebra keeps a roughly five-dot bar-to-cap gap. Vera Mono's
140
- // cap top sits about .14em below its line-box top. The DOM
141
- // rasterizer needs three extra dots to match the printer cap.
142
- const belowTop = uprightHeight + 8 - hriFontSize * .14;
143
- this._hri.style.top = textAbove ? '0' : `${belowTop}px`;
149
+ if (isEanUpc && !textAbove && eanStyle) {
150
+ const barWidth = uprightWidth - horizontalInsets.left - horizontalInsets.right;
151
+ this._hri.className = 'ean-upc';
152
+ this._hri.style.left = `${horizontalInsets.left}px`;
153
+ this._hri.style.width = `${barWidth}px`;
154
+ this._hri.style.top = `${uprightHeight + eanStyle.gap}px`;
155
+ for (const fragment of getEanUpcHriFragments(props)) {
156
+ const digit = document.createElement('span');
157
+ digit.textContent = fragment.char;
158
+ digit.style.left = `${(fragment.xModule - 1) * linear.moduleWidth - hriFontSize / 2}px`;
159
+ digit.style.width = `${hriFontSize}px`;
160
+ this._hri.appendChild(digit);
161
+ }
162
+ }
163
+ else {
164
+ this._hri.textContent = barcodeInterpretationText(props);
165
+ // Zebra keeps a roughly five-dot bar-to-cap gap. Vera Mono's
166
+ // cap top sits about .14em below its line-box top. The DOM
167
+ // rasterizer needs three extra dots to match the printer cap.
168
+ const belowTop = uprightHeight + 8 - hriFontSize * .14;
169
+ this._hri.style.top = textAbove ? '0' : `${belowTop}px`;
170
+ }
144
171
  this._hri.hidden = false;
145
172
  }
146
173
  uprightHeight += textZone;
@@ -67,12 +67,8 @@ export class ZplGraphicBox extends BaseCustomWebComponentConstructorAppend {
67
67
  const elementWidth = parseInt(this.style.width.replace("px", "")) || this.clientWidth || 1;
68
68
  const elementHeight = parseInt(this.style.height.replace("px", "")) || this.clientHeight || 1;
69
69
  const x = filled ? 0 : this.strokeWidth / 2;
70
- let width = filled ? elementWidth : elementWidth - this.strokeWidth;
71
- if (!filled && width < this.strokeWidth)
72
- width = this.strokeWidth;
73
- let height = filled ? elementHeight : elementHeight - this.strokeWidth;
74
- if (!filled && height < this.strokeWidth)
75
- height = this.strokeWidth;
70
+ const width = filled ? elementWidth : Math.max(0, elementWidth - this.strokeWidth);
71
+ const height = filled ? elementHeight : Math.max(0, elementHeight - this.strokeWidth);
76
72
  let smallerLength = width;
77
73
  if (smallerLength > height)
78
74
  smallerLength = height;
@@ -80,11 +76,12 @@ export class ZplGraphicBox extends BaseCustomWebComponentConstructorAppend {
80
76
  let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
81
77
  let rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
82
78
  const paint = reverse || this.strokeColor == StrokeColor.white ? 'white' : 'black';
83
- // Very thin filled ^GB fields (for example 700x3 with thickness 3)
84
- // can lose their SVG paint when the browser clips the inline SVG line
85
- // box. Paint the backing div too so horizontal/vertical rules remain
86
- // visible at their exact ZPL dimensions.
87
- this._box.style.backgroundColor = filled ? paint : 'transparent';
79
+ // Opposing strokes overlap when twice the thickness reaches the short
80
+ // side (for example ^GB300,7,5). Zebra therefore paints a solid rule.
81
+ // Back it with the same paint so a sub-line-height SVG cannot vanish
82
+ // when clipped by the designer, without changing the stored thickness.
83
+ const overlappingStroke = 2 * this.strokeWidth >= Math.min(elementWidth, elementHeight);
84
+ this._box.style.backgroundColor = filled || overlappingStroke ? paint : 'transparent';
88
85
  rect.setAttribute("stroke", filled ? "none" : paint);
89
86
  rect.setAttribute("fill", filled ? paint : "none");
90
87
  rect.setAttribute("stroke-width", filled ? "0" : this.strokeWidth.toString());
@@ -96,6 +93,7 @@ export class ZplGraphicBox extends BaseCustomWebComponentConstructorAppend {
96
93
  rect.setAttribute("height", height.toString());
97
94
  svg.setAttribute("width", "100%");
98
95
  svg.setAttribute("height", "100%");
96
+ svg.style.display = "block";
99
97
  svg.style.overflow = "visible";
100
98
  if (this._box.childElementCount > 0)
101
99
  this._box.removeChild(this._box.children[0]);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "description": "web-component-designer addon: Widgets and Services for creating a ZPL Designer",
3
3
  "name": "@node-projects/web-component-designer-zpl",
4
- "version": "0.3.1",
4
+ "version": "0.3.3",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "author": "jochen.kuehner@gmx.de",