GeezerVison is showing github.com — fix: bars, line and print check follow the data the printer encodes · u8array/ZPLab@37ce700
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/lib/barcodeDims.ts +9-27Lines changed: 9 additions & 27 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,7 +7,7 @@ import type { LabelObject } from "../types/Group"; | 7 | 7 | import { errorMessage } from "./errorMessage"; | 8 | 8 | import { clampCodablockColumns, CODABLOCK_PREVIEW_COLUMNS_MIN } from "../registry/codablock"; | 9 | 9 | import { EC_PERCENT_MIN, EC_PERCENT_MAX } from "../registry/aztec"; | 10 | | -import { upceData6FromFd } from "../registry/hriFormatters"; | | 10 | +import { firmwareData } from "./firmwareData"; | 11 | 11 | import type { Gs1DatabarProps } from "../registry/gs1databar"; | 12 | 12 | import { isAxisSwapped, objectRotation } from "../registry/rotation"; | 13 | 13 | import { dotsToPx, mmToDots, pxToDots } from "./coordinates"; | @@ -221,14 +221,9 @@ export interface EanUpcHriFragment { | 221 | 221 | xModule: number; | 222 | 222 | } | 223 | 223 |
| 224 | | -/** bwip raw geometry (bars + HRI text) for an EAN/UPC symbol. UPC-E | 225 | | - * accepts 6-digit content but bwip rejects it, so pre-pad with the | 226 | | - * number-system digit as the firmware does. Shared by the bars and HRI | 227 | | - * paths so both encode the identical symbol. */ | | 224 | +/** bwip raw geometry of the EAN/UPC symbol, shared by the bars and the HRI line. */ | 228 | 225 | export function rawEanUpc(bwip: BwipEngine, type: EanUpcType, text: string): BwipRawLinear | null { | 229 | | - // UPC-E: always feed bwip the canonical 7-digit form (NS 0 + 6 data), | 230 | | - // NS-aware so a content that already carries the prefix is not doubled. | 231 | | - const encoded = type === "upce" ? `0${upceData6FromFd(text)}` : text; | | 226 | + const encoded = firmwareData(type, text); | 232 | 227 | try { | 233 | 228 | const stack = bwip.raw({ bcid: type, text: encoded, includetext: true }) as BwipRawLinear[]; | 234 | 229 | return stack?.[0] ?? null; | @@ -268,14 +263,7 @@ export function buildBwipOptions( | 268 | 263 | case "upce": { | 269 | 264 | const p = obj.props; | 270 | 265 | const scale = bwipScale1D(p.moduleWidth, renderScale, renderDpmm); | 271 | | - let text: string; | 272 | | - if (obj.type === "upce") { | 273 | | - const r = p.content || "000000"; | 274 | | - text = r.length === 6 ? `0${r}` : r; | 275 | | - } else { | 276 | | - text = p.content || "0"; | 277 | | - } | 278 | | - opts = { bcid, text, scale, height: 10 }; | | 266 | + opts = { bcid, text: firmwareData(obj.type, p.content) || "0", scale, height: 10 }; | 279 | 267 | break; | 280 | 268 | } | 281 | 269 | case "code49": { | @@ -304,7 +292,7 @@ export function buildBwipOptions( | 304 | 292 | // bwip splits ^BS into ean2/ean5 by length; non-2 falls back to ean5. | 305 | 293 | // HRI rendered as separate Konva overlay (Zebra puts it above bars) | 306 | 294 | // so rotation lands at the firmware anchor. | 307 | | - const text = p.content || "00000"; | | 295 | + const text = firmwareData(obj.type, p.content) || "00000"; | 308 | 296 | const variantBcid = text.length === 2 ? "ean2" : "ean5"; | 309 | 297 | opts = { | 310 | 298 | bcid: variantBcid, | @@ -343,10 +331,7 @@ export function buildBwipOptions( | 343 | 331 | case "codabar": { | 344 | 332 | const p = obj.props; | 345 | 333 | const scale = bwipScale1D(p.moduleWidth, renderScale, renderDpmm); | 346 | | - // Zebra silently uppercases for these symbologies; bwip-js throws. | 347 | | - const needsUpper = obj.type === "code39" || obj.type === "codabar"; | 348 | | - const raw = p.content || "0"; | 349 | | - const text = needsUpper ? raw.toUpperCase() : raw; | | 334 | + const text = firmwareData(obj.type, p.content || "0"); | 350 | 335 | // ^B3/^B2 e encode Mod 43 / Mod 10 into the bars (ZD230-measured: +1 | 351 | 336 | // char / +1 digit pair). Codabar's e-slot is a spec-fixed N and the | 352 | 337 | // 2of5 pair has no check param. | @@ -360,7 +345,7 @@ export function buildBwipOptions( | 360 | 345 | const scale = bwipScale1D(p.moduleWidth, renderScale, renderDpmm); | 361 | 346 | // Zebra ^BA always encodes the C+K check chars; the ZPL e param only | 362 | 347 | // gates the HRI. Without includecheck bwip's symbol is 18 modules short. | 363 | | - opts = { bcid, text: p.content || "0", scale, height: 10, includecheck: true }; | | 348 | + opts = { bcid, text: firmwareData(obj.type, p.content || "0"), scale, height: 10, includecheck: true }; | 364 | 349 | break; | 365 | 350 | } | 366 | 351 | case "code11": { | @@ -393,10 +378,9 @@ export function buildBwipOptions( | 393 | 378 | case "logmars": { | 394 | 379 | const p = obj.props; | 395 | 380 | const scale = bwipScale1D(p.moduleWidth, renderScale, renderDpmm); | 396 | | - // LOGMARS is a Code39 subset; same uppercase rule applies. | 397 | 381 | opts = { | 398 | 382 | bcid, | 399 | | - text: (p.content || "0").toUpperCase(), | | 383 | + text: firmwareData(obj.type, p.content || "0"), | 400 | 384 | scale, | 401 | 385 | height: 10, | 402 | 386 | includecheck: true, | @@ -1124,9 +1108,7 @@ export function barcodeEncodeIssueWith( | 1124 | 1108 | function encodeFailureReason(bwip: BwipEngine, obj: LeafObject, dpmm: number): string { | 1125 | 1109 | try { | 1126 | 1110 | if (EAN_UPC_TYPES.has(obj.type)) { | 1127 | | - const text = getObjectStringContent(obj) ?? ""; | 1128 | | - const encoded = obj.type === "upce" ? `0${upceData6FromFd(text)}` : text; | 1129 | | - bwip.raw({ bcid: obj.type, text: encoded, includetext: true }); | | 1111 | + bwip.raw({ bcid: obj.type, text: firmwareData(obj.type, getObjectStringContent(obj) ?? ""), includetext: true }); | 1130 | 1112 | return "the encoder produced no symbol for this payload"; | 1131 | 1113 | } | 1132 | 1114 | // barcodeDimsPx routes these past buildBwipOptions, so their missing BCID | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/lib/barcodeHri.ts +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | // Pure HRI text-zone resolution shared by the barcode renderer (getDisplaySize) | 2 | 2 | // and the group-rotation bbox probe, so zone height and side never drift apart. | 3 | 3 |
| 4 | | -import { isGs1Active, ObjectRegistry, type LeafObject } from "../registry"; | | 4 | +import { getEntry, isGs1Active, ObjectRegistry, type LeafObject } from "../registry"; | | 5 | +import type { HriFormatProps } from "../types/ZplEmit"; | | 6 | +import { firmwareData } from "./firmwareData"; | 5 | 7 | import { | 6 | 8 | EAN_TEXT_ZONE_DOTS, | 7 | 9 | LOGMARS_TEXT_ZONE_DOTS, | @@ -61,6 +63,11 @@ export function hriZoneDots(moduleWidth: number): number { | 61 | 63 | return 7 * (Math.max(1, Math.round(moduleWidth)) + 1); | 62 | 64 | } | 63 | 65 |
| | 66 | +/** The line text: the type's own format, else the data the firmware encodes. */ | | 67 | +export function hriText(type: string, content: string, props?: HriFormatProps): string { | | 68 | + return getEntry(type)?.hri?.formatHri?.(content, props) ?? firmwareData(type, content); | | 69 | +} | | 70 | + | 64 | 71 | /** Firmware-reserved HRI text-zone height in dots. ^BS reserves it only when | 65 | 72 | * printInterpretation is on; other EAN/UPC reserve the fixed guard zone always; | 66 | 73 | * the rest reserve a module-scaled line, but only with the line turned on. */ | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/lib/barcodeRawGeometry.test.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,7 +14,7 @@ describe("zebraWidthBarText", () => { | 14 | 14 | expect(zebraWidthBarText("plessey", "")).toBe("0"); | 15 | 15 | }); | 16 | 16 |
| 17 | | - it("pads PLANET like firmware (11/13 digits) and strips non-digits", () => { | | 17 | + it("fills PLANET to 11 or 13 digits and strips non-digits", () => { | 18 | 18 | expect(zebraWidthBarText("planet", "12345678")).toBe("00012345678"); | 19 | 19 | expect(zebraWidthBarText("planet", "123456789012")).toBe("0123456789012"); | 20 | 20 | expect(zebraWidthBarText("planet", "1a2b3")).toBe("00000000123"); | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/lib/barcodeRawGeometry.ts +3-10Lines changed: 3 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | */ | 10 | 10 |
| 11 | 11 | import { PLESSEY_RATIO } from "../registry/plessey"; | | 12 | +import { firmwareData } from "./firmwareData"; | 12 | 13 |
| 13 | 14 | export interface RawBarRect { | 14 | 15 | x: number; | @@ -30,17 +31,9 @@ export const ZEBRA_WIDTH_BAR_TYPES: ReadonlySet<string> = new Set( | 30 | 31 | Object.keys(ZEBRA_WIDTH_BCID), | 31 | 32 | ); | 32 | 33 |
| 33 | | -/** Encode text per type: Plessey uppercases like firmware, PLANET strips to | 34 | | - * digits and pads to 11/13, POSTNET stays verbatim (bwip throws on | 35 | | - * non-digits, which is the encode error the preflight surfaces; stripping | 36 | | - * would silently encode a shorter symbol). */ | | 34 | +/** The text the bars encode. Empty content becomes "0". */ | 37 | 35 | export function zebraWidthBarText(type: ZebraWidthBarType, content: string): string { | 38 | | - if (type === "plessey") return (content || "0").toUpperCase(); | 39 | | - if (type === "postal") return content || "0"; | 40 | | - let text = (content || "0").replace(/\D/g, "") || "0"; | 41 | | - if (text.length < 11) text = text.padStart(11, "0"); | 42 | | - else if (text.length === 12) text = text.padStart(13, "0"); | 43 | | - return text; | | 36 | + return firmwareData(type, content || "0"); | 44 | 37 | } | 45 | 38 |
| 46 | 39 | /** First entry of a bwipjs.raw() stack; absorbs the per-site double cast | |
Collapse file Copy file name to clipboard +73Lines changed: 73 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { describe, expect, it } from "vitest"; | | 2 | +import bwipjs from "bwip-js/generic"; | | 3 | +import { getEntry, type LeafObject } from "../registry"; | | 4 | +import { barcodeEncodeIssueWith, type BwipEngine } from "./barcodeDims"; | | 5 | +import { hriText } from "./barcodeHri"; | | 6 | +import { firmwareData } from "./firmwareData"; | | 7 | + | | 8 | +describe("firmwareData", () => { | | 9 | + // Each row is a ZD230 preview of the right-hand data. | | 10 | + it.each<[string, string, string]>([ | | 11 | + ["ean13", "12345", "000000012345"], | | 12 | + ["ean13", "5901234123450", "590123412345"], | | 13 | + ["ean8", "123", "0000123"], | | 14 | + ["ean8", "12345679", "1234567"], | | 15 | + ["upca", "123", "00000000123"], | | 16 | + ["upca", "123456789019", "12345678901"], | | 17 | + ["upcEanExtension", "1", "01"], | | 18 | + ["upcEanExtension", "123", "00123"], | | 19 | + ["upcEanExtension", "1234", "01234"], | | 20 | + ["upcEanExtension", "123456", "12345"], | | 21 | + ["upcEanExtension", "1234567", "12345"], | | 22 | + ["code39", "abc", "ABC"], | | 23 | + ["code93", "abc", "ABC"], | | 24 | + ["logmars", "abc", "ABC"], | | 25 | + ["codabar", "a123b", "A123B"], | | 26 | + ["plessey", "ab12", "AB12"], | | 27 | + ])("reads %s data %s as %s", (type, typed, encoded) => { | | 28 | + expect(firmwareData(type, typed)).toBe(encoded); | | 29 | + }); | | 30 | + | | 31 | + it("uppercases a to z and leaves other letters", () => { | | 32 | + expect(firmwareData("code93", "stra" + String.fromCharCode(223) + "e")).toBe("STRA" + String.fromCharCode(223) + "E"); | | 33 | + }); | | 34 | + | | 35 | + it.each(["code93", "codabar", "plessey"])("shows the encoded data in the %s line", (type) => { | | 36 | + expect(hriText(type, "ab12")).toBe("AB12"); | | 37 | + }); | | 38 | + | | 39 | + it.each(["123", "12a3"])("shows PLANET data %s without the fill in the line", (typed) => { | | 40 | + expect(hriText("planet", typed)).toBe("123"); | | 41 | + }); | | 42 | + | | 43 | + it.each<[string, string]>([ | | 44 | + ["ean13", "12345678901234"], | | 45 | + ["ean8", "123456789"], | | 46 | + ["upca", "1234567890123"], | | 47 | + ["ean13", "12A45"], | | 48 | + ["ean13", ""], | | 49 | + ])("leaves %s data %s as typed, where the printer prints something else or nothing", (type, typed) => { | | 50 | + expect(firmwareData(type, typed)).toBe(typed); | | 51 | + }); | | 52 | +}); | | 53 | + | | 54 | +describe("the print check on data the firmware adjusts", () => { | | 55 | + const leaf = (type: string, content: string) => | | 56 | + ({ id: "x", type, x: 0, y: 0, rotation: 0, props: { ...(getEntry(type)?.defaultProps as object), content } }) as unknown as LeafObject; | | 57 | + | | 58 | + it.each<[string, string]>([ | | 59 | + ["ean13", "12345"], | | 60 | + ["ean13", "5901234123450"], | | 61 | + ["ean8", "123"], | | 62 | + ["upca", "123"], | | 63 | + ["upcEanExtension", "1"], | | 64 | + ["upcEanExtension", "123"], | | 65 | + ["code93", "abc"], | | 66 | + ])("takes %s data %s, which the printer prints", (type, content) => { | | 67 | + expect(barcodeEncodeIssueWith(bwipjs as unknown as BwipEngine, leaf(type, content), 8)).toBeNull(); | | 68 | + }); | | 69 | + | | 70 | + it("refuses an EAN-13 that is two digits too long", () => { | | 71 | + expect(barcodeEncodeIssueWith(bwipjs as unknown as BwipEngine, leaf("ean13", "12345678901234"), 8)).not.toBeNull(); | | 72 | + }); | | 73 | +}); |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { getEntry } from "../registry"; | | 2 | + | | 3 | +/** The data the firmware encodes from a field, per the registry rule. */ | | 4 | +export function firmwareData(type: string, content: string): string { | | 5 | + return getEntry(type)?.firmwareData?.(content) ?? content; | | 6 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/lib/zplParser/flushField.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -43,7 +43,7 @@ import type { MaxicodeProps } from "../../registry/maxicode"; | 43 | 43 | import type { MicroPdf417Props } from "../../registry/micropdf417"; | 44 | 44 | import type { CodablockProps } from "../../registry/codablock"; | 45 | 45 | import type { Tlc39Props } from "../../registry/tlc39"; | 46 | | -import { upceData6FromFd } from "../../registry/hriFormatters"; | | 46 | +import { upceData6FromFd } from "../../registry/firmwareRules"; | 47 | 47 | import { decodeFH, makeObj, variableNameFromComment } from "./helpers"; | 48 | 48 | import { notePartial, | 49 | 49 | getPosType, | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/registry/barcode1d.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,6 +56,8 @@ export interface Barcode1DCoreConfig<P extends Barcode1DProps = Barcode1DProps> | 56 | 56 | interpretationLocked?: boolean; | 57 | 57 | /** See {@link HriBehavior}. */ | 58 | 58 | hri?: HriBehavior; | | 59 | + /** See {@link ObjectTypeCore.firmwareData}. */ | | 60 | + firmwareData?: (content: string) => string; | 59 | 61 | /** Transform stored content into the ^FD payload (e.g. UPC-E prepends | 60 | 62 | * the number-system digit the spec requires). Default: identity. */ | 61 | 63 | fdContent?: (content: string) => string; | @@ -153,6 +155,7 @@ export function createBarcode1DCore<P extends Barcode1DProps = Barcode1DProps>( | 153 | 155 | heightLocked: config.heightLocked, | 154 | 156 | interpretationLocked: config.interpretationLocked, | 155 | 157 | hri: config.hri, | | 158 | + firmwareData: config.firmwareData, | 156 | 159 | // GS1 mode swaps in the shared GS1 element-string spec; the static half | 157 | 160 | // stays the symbology's own rule. | 158 | 161 | contentSpec: config.gs1Capable | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/registry/codabar.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { BARCODE_1D_PROP_SPECS, createBarcode1DCore, type Barcode1DCoreConfig } from './barcode1d'; | | 2 | +import { upperAZ } from './firmwareRules'; | 2 | 3 | export type { Barcode1DProps as CodabarProps } from "./barcode1d"; | 3 | 4 |
| 4 | 5 | export const codabarCoreConfig: Barcode1DCoreConfig = { | 5 | 6 | label: "Codabar", | 6 | 7 | icon: "CBA", | 7 | 8 | placeholderContent: 'A12345A', | | 9 | + firmwareData: upperAZ, | 8 | 10 | group: 'legacy', | 9 | 11 | propSpecs: BARCODE_1D_PROP_SPECS, | 10 | 12 | zplCommand: (p) => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/registry/code39.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { BARCODE_1D_PROP_SPECS, createBarcode1DCore, type Barcode1DCoreConfig } from './barcode1d'; | | 2 | +import { upperAZ } from './firmwareRules'; | 2 | 3 | import { formatCode39Hri } from './hriFormatters'; | 3 | 4 | export type { Barcode1DProps as Code39Props } from './barcode1d'; | 4 | 5 |
| 5 | 6 | export const code39CoreConfig: Barcode1DCoreConfig = { | 6 | 7 | label: 'Code 39', | 7 | 8 | icon: '|·|', | 8 | 9 | placeholderContent: 'CODE39', | | 10 | + firmwareData: upperAZ, | 9 | 11 | group: 'code-1d', | 10 | 12 | propSpecs: BARCODE_1D_PROP_SPECS, | 11 | 13 | zplCommand: (p) => { | |
|