@handijitcom/barcode 0.0.0-stage → 1.0.0

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../lib/helpers/xml.helper.ts","../lib/helpers/code128-codewords.helper.ts","../lib/helpers/code128-patterns.helper.ts","../lib/modules/encode-code128.ts","../lib/modules/code128-svg.ts","../lib/modules/label-sizes.ts","../lib/modules/label-print-css.ts"],"sourcesContent":["const XML_ENTITIES: ReadonlyMap<string, string> = new Map([\n [\"&\", \"&amp;\"],\n [\"<\", \"&lt;\"],\n [\">\", \"&gt;\"],\n ['\"', \"&quot;\"],\n [\"'\", \"&apos;\"],\n]);\n\n// XML 1.0 \"Char\": anything else (NUL, most C0 controls, lone surrogates, U+FFFE/FFFF) makes the\n// whole document unparseable, even as a character reference.\nconst isXmlChar = (codePoint: number): boolean =>\n codePoint === 0x09 ||\n codePoint === 0x0a ||\n codePoint === 0x0d ||\n (codePoint >= 0x20 && codePoint <= 0xd7ff) ||\n (codePoint >= 0xe000 && codePoint <= 0xfffd) ||\n codePoint >= 0x10000;\n\n/** Escapes a value for XML text and attributes, dropping characters XML cannot carry. */\nconst escapeXml = (value: string): string => {\n let escaped = \"\";\n for (const character of value) {\n if (!isXmlChar(character.codePointAt(0) as number)) continue;\n escaped += XML_ENTITIES.get(character) ?? character;\n }\n return escaped;\n};\n\nexport { escapeXml };\n","type CodeSet = 0 | 1 | 2;\n\nconst SET_A = 0;\nconst SET_B = 1;\nconst SET_C = 2;\n\n// Tie-break order: when two plans are equally short, C (denser) wins, then B (text) before A.\nconst SETS_BY_PREFERENCE: readonly CodeSet[] = [SET_C, SET_B, SET_A];\nconst SET_COUNT = 3;\n\n// Indexed by code set: Start A/B/C, and the CODE A/B/C symbol that switches into that set.\nconst START_CODEWORDS = [103, 104, 105] as const;\nconst SWITCH_CODEWORDS = [101, 100, 99] as const;\nconst SHIFT_CODEWORD = 98;\nconst STOP_CODEWORD = 106;\nconst CHECKSUM_MODULUS = 103;\n\nconst MAX_ASCII = 127;\n\n// Planner decisions per (position, code set); a switch is stored as ACTION_SWITCH + target set.\nconst ACTION_ENCODE = 0;\nconst ACTION_SHIFT = 1;\nconst ACTION_SWITCH = 2;\n\nconst isDigit = (code: number): boolean => code >= 48 && code <= 57;\n\nconst otherTextSet = (set: CodeSet): CodeSet => (set === SET_A ? SET_B : SET_A);\n\n/** Characters one codeword of `set` consumes at `index`, or 0 when the set cannot encode them. */\nconst spanOf = (text: string, index: number, set: CodeSet): number => {\n const code = text.charCodeAt(index);\n // charCodeAt past the end is NaN, so a trailing single digit is never taken as a pair.\n if (set === SET_C) {\n return isDigit(code) && isDigit(text.charCodeAt(index + 1)) ? 2 : 0;\n }\n if (set === SET_A) return code < 96 ? 1 : 0;\n return code >= 32 ? 1 : 0;\n};\n\n/** Symbol value of the characters at `index` in `set`; the caller has checked `spanOf`. */\nconst valueOf = (text: string, index: number, set: CodeSet): number => {\n const code = text.charCodeAt(index);\n if (set === SET_C) return (code - 48) * 10 + text.charCodeAt(index + 1) - 48;\n // Set A puts the control characters (0–31) after the printable ones: NUL is value 64.\n if (set === SET_A && code < 32) return code + 64;\n return code - 32;\n};\n\nconst assertEncodable = (text: unknown, caller: string): string => {\n if (typeof text !== \"string\") {\n throw new TypeError(\n `${caller}: expected a string, received ${text === null ? \"null\" : typeof text}`\n );\n }\n if (text.length === 0) {\n throw new RangeError(`${caller}: text must contain at least one character`);\n }\n for (let index = 0; index < text.length; index++) {\n if (text.charCodeAt(index) > MAX_ASCII) {\n const character = String.fromCodePoint(text.codePointAt(index) as number);\n throw new TypeError(\n `${caller}: \"${character}\" at index ${index} is not ASCII; Code 128 encodes characters 0–127 only`\n );\n }\n }\n return text;\n};\n\n/**\n * Plans the shortest symbol sequence by dynamic programming over (position, current code set),\n * so CODE C runs, shifts and switches are placed optimally rather than by look-ahead heuristics.\n */\nconst planDataCodewords = (text: string): number[] => {\n const { length } = text;\n // cost[i * 3 + set]: fewest symbols that encode text[i..] when `set` is active; the row for\n // i = length stays 0.\n const cost = new Float64Array((length + 1) * SET_COUNT);\n const action = new Uint8Array(length * SET_COUNT);\n const costAt = (index: number, set: CodeSet): number =>\n cost[index * SET_COUNT + set] as number;\n const direct: [number, number, number] = [0, 0, 0];\n\n for (let index = length - 1; index >= 0; index--) {\n for (const set of SETS_BY_PREFERENCE) {\n let best = Infinity;\n let choice = ACTION_ENCODE;\n const span = spanOf(text, index, set);\n if (span > 0) best = 1 + costAt(index + span, set);\n if (set !== SET_C && spanOf(text, index, otherTextSet(set)) > 0) {\n const shifted = 2 + costAt(index + 1, set);\n if (shifted < best) {\n best = shifted;\n choice = ACTION_SHIFT;\n }\n }\n direct[set] = best;\n action[index * SET_COUNT + set] = choice;\n }\n // Every ASCII character fits set A or B, so a switch always leads to a finite plan; switching\n // twice in a row is never shorter than switching once to the final set.\n for (const set of SETS_BY_PREFERENCE) {\n let best = direct[set];\n for (const target of SETS_BY_PREFERENCE) {\n if (target !== set && 1 + direct[target] < best) {\n best = 1 + direct[target];\n action[index * SET_COUNT + set] = ACTION_SWITCH + target;\n }\n }\n cost[index * SET_COUNT + set] = best;\n }\n }\n\n let set: CodeSet = SET_C;\n for (const candidate of SETS_BY_PREFERENCE) {\n if (costAt(0, candidate) < costAt(0, set)) set = candidate;\n }\n\n const codewords: number[] = [START_CODEWORDS[set]];\n let index = 0;\n while (index < length) {\n const step = action[index * SET_COUNT + set] as number;\n if (step >= ACTION_SWITCH) {\n set = (step - ACTION_SWITCH) as CodeSet;\n codewords.push(SWITCH_CODEWORDS[set]);\n } else if (step === ACTION_SHIFT) {\n codewords.push(SHIFT_CODEWORD, valueOf(text, index, otherTextSet(set)));\n index += 1;\n } else {\n codewords.push(valueOf(text, index, set));\n index += spanOf(text, index, set);\n }\n }\n return codewords;\n};\n\n/**\n * Full Code 128 symbol sequence for `text`: start, data, mod-103 check symbol and stop.\n * Throws TypeError for a non-string or non-ASCII text and RangeError for an empty one.\n */\nconst toCode128Codewords = (text: string, caller: string): number[] => {\n const codewords = planDataCodewords(assertEncodable(text, caller));\n // The start symbol has weight 1, like the first data symbol.\n let checksum = codewords[0] as number;\n for (let position = 1; position < codewords.length; position++) {\n checksum += (codewords[position] as number) * position;\n }\n codewords.push(checksum % CHECKSUM_MODULUS, STOP_CODEWORD);\n return codewords;\n};\n\nexport { toCode128Codewords };\n","// Bar/space module widths of every Code 128 symbol (ISO/IEC 15417), indexed by symbol value.\n// Each string alternates bar, space, bar...; 0–105 span 11 modules, the stop symbol (106) spans 13\n// because it carries the final 2-module termination bar.\n// prettier-ignore\nconst PATTERN_SOURCE = [\n \"212222\", \"222122\", \"222221\", \"121223\", \"121322\", \"131222\", \"122213\", \"122312\",\n \"132212\", \"221213\", \"221312\", \"231212\", \"112232\", \"122132\", \"122231\", \"113222\",\n \"123122\", \"123221\", \"223211\", \"221132\", \"221231\", \"213212\", \"223112\", \"312131\",\n \"311222\", \"321122\", \"321221\", \"312212\", \"322112\", \"322211\", \"212123\", \"212321\",\n \"232121\", \"111323\", \"131123\", \"131321\", \"112313\", \"132113\", \"132311\", \"211313\",\n \"231113\", \"231311\", \"112133\", \"112331\", \"132131\", \"113123\", \"113321\", \"133121\",\n \"313121\", \"211331\", \"231131\", \"213113\", \"213311\", \"213131\", \"311123\", \"311321\",\n \"331121\", \"312113\", \"312311\", \"332111\", \"314111\", \"221411\", \"431111\", \"111224\",\n \"111422\", \"121124\", \"121421\", \"141122\", \"141221\", \"112214\", \"112412\", \"122114\",\n \"122411\", \"142112\", \"142211\", \"241211\", \"221114\", \"413111\", \"241112\", \"134111\",\n \"111242\", \"121142\", \"121241\", \"114212\", \"124112\", \"124211\", \"411212\", \"421112\",\n \"421211\", \"212141\", \"214121\", \"412121\", \"111143\", \"111341\", \"131141\", \"114113\",\n \"114311\", \"411113\", \"411311\", \"113141\", \"114131\", \"311141\", \"411131\", \"211412\",\n \"211214\", \"211232\", \"2331112\",\n];\n\n/** Module widths per symbol value (0–106). */\nconst CODE128_PATTERNS: readonly (readonly number[])[] = PATTERN_SOURCE.map(\n (pattern) => Array.from(pattern, Number)\n);\n\nexport { CODE128_PATTERNS };\n","import { toCode128Codewords } from \"../helpers/code128-codewords.helper\";\nimport { CODE128_PATTERNS } from \"../helpers/code128-patterns.helper\";\n\n/**\n * Encodes ASCII text as a Code 128 symbol and returns its module widths: bar, space, bar...,\n * from the start symbol to the stop symbol (quiet zones excluded).\n */\nconst encodeCode128 = (text: string): number[] => {\n const widths: number[] = [];\n for (const codeword of toCode128Codewords(text, \"encodeCode128\")) {\n // Codewords come from the encoder and are always 0–106, so the lookup cannot miss.\n widths.push(...(CODE128_PATTERNS[codeword] as readonly number[]));\n }\n return widths;\n};\n\nexport default encodeCode128;\n","import { escapeXml } from \"../helpers/xml.helper\";\nimport encodeCode128 from \"./encode-code128\";\n\ntype LengthUnit = \"px\" | \"mm\";\n\ntype Code128SvgOptions = {\n /** Width of the narrowest bar, in `unit` (default 2 px or 0.33 mm). */\n moduleWidth?: number;\n /** Unit of every length option and of the SVG size (default \"px\"). */\n unit?: LengthUnit;\n /** Bar height excluding the text line, in `unit` (default 80 px or 15 mm). */\n height?: number;\n /** Blank margin on the left and right, in modules (default 10, the Code 128 minimum). */\n quietZone?: number;\n /** Print the encoded text under the bars (default true). */\n showText?: boolean;\n /** Font size of the text line, in `unit` (default 14 px or 3 mm). */\n fontSize?: number;\n /** Colour of the bars and the text (default \"#000000\"). */\n foreground?: string;\n /** Colour of the background, quiet zones included (default \"#ffffff\"). */\n background?: string;\n /** Accessible name, emitted as `<title>` and `aria-label` (default: `aria-label` is the text). */\n title?: string;\n};\n\nconst UNIT_DEFAULTS = {\n px: { moduleWidth: 2, height: 80, fontSize: 14 },\n mm: { moduleWidth: 0.33, height: 15, fontSize: 3 },\n} as const;\nconst DEFAULT_QUIET_ZONE = 10;\nconst DEFAULT_FOREGROUND = \"#000000\";\nconst DEFAULT_BACKGROUND = \"#ffffff\";\n// Text line below the bars, in ems: the baseline sits 1.1 em under the bars, the line ends 1.4 em down.\nconst TEXT_BASELINE_EM = 1.1;\nconst TEXT_LINE_EM = 1.4;\n// Enough for 0.001 mm / 0.001 px precision while cutting floating-point noise (0.9900000000000001).\nconst DECIMALS = 4;\n\nconst formatNumber = (value: number): string =>\n String(Number(value.toFixed(DECIMALS)));\n\nconst positiveOr = (value: number | undefined, fallback: number): number =>\n typeof value === \"number\" && Number.isFinite(value) && value > 0\n ? value\n : fallback;\n\n/** Renders `text` as a Code 128 barcode SVG string; needs no DOM. */\nconst code128Svg = (\n text: string,\n {\n unit,\n moduleWidth,\n height,\n quietZone,\n showText = true,\n fontSize,\n foreground = DEFAULT_FOREGROUND,\n background = DEFAULT_BACKGROUND,\n title,\n }: Code128SvgOptions = {}\n): string => {\n const widths = encodeCode128(text);\n const lengthUnit: LengthUnit = unit === \"mm\" ? \"mm\" : \"px\";\n const defaults = UNIT_DEFAULTS[lengthUnit];\n const moduleSize = positiveOr(moduleWidth, defaults.moduleWidth);\n const barHeight = positiveOr(height, defaults.height);\n const textSize = positiveOr(fontSize, defaults.fontSize);\n const margin =\n typeof quietZone === \"number\" &&\n Number.isFinite(quietZone) &&\n quietZone >= 0\n ? Math.floor(quietZone)\n : DEFAULT_QUIET_ZONE;\n const hasText = showText !== false;\n\n // Positions are counted in whole modules and scaled once, so no rounding error accumulates\n // from bar to bar.\n const bars: string[] = [];\n let cursor = margin;\n widths.forEach((width, index) => {\n if (index % 2 === 0) {\n const barWidth = formatNumber(width * moduleSize);\n bars.push(\n `M${formatNumber(cursor * moduleSize)} 0h${barWidth}v${formatNumber(barHeight)}h-${barWidth}z`\n );\n }\n cursor += width;\n });\n\n const totalWidth = formatNumber((cursor + margin) * moduleSize);\n const totalHeight = formatNumber(\n barHeight + (hasText ? textSize * TEXT_LINE_EM : 0)\n );\n const hasTitle = typeof title === \"string\" && title.length > 0;\n const label = escapeXml(hasTitle ? title : text);\n const fill = escapeXml(foreground);\n\n const parts = [\n `<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"${totalWidth}${lengthUnit}\" height=\"${totalHeight}${lengthUnit}\" viewBox=\"0 0 ${totalWidth} ${totalHeight}\" role=\"img\" aria-label=\"${label}\">`,\n hasTitle ? `<title>${label}</title>` : \"\",\n `<rect width=\"${totalWidth}\" height=\"${totalHeight}\" fill=\"${escapeXml(background)}\"/>`,\n `<path fill=\"${fill}\" shape-rendering=\"crispEdges\" d=\"${bars.join(\"\")}\"/>`,\n hasText\n ? `<text x=\"${formatNumber(((cursor + margin) * moduleSize) / 2)}\" y=\"${formatNumber(barHeight + textSize * TEXT_BASELINE_EM)}\" fill=\"${fill}\" font-family=\"monospace\" font-size=\"${formatNumber(textSize)}\" text-anchor=\"middle\">${escapeXml(text)}</text>`\n : \"\",\n \"</svg>\",\n ];\n return parts.join(\"\");\n};\n\nexport type { Code128SvgOptions, LengthUnit };\nexport default code128Svg;\n","type LabelSize = \"100x100\" | \"100x150\" | \"a4\";\n\ntype LabelDimensions = {\n /** Page width in millimetres. */\n readonly width: number;\n /** Page height in millimetres. */\n readonly height: number;\n};\n\n/** Page sizes of common shipping labels, in millimetres: 100×100 and 100×150 thermal, A4. */\nconst LABEL_SIZES: Readonly<Record<LabelSize, LabelDimensions>> = Object.freeze(\n {\n \"100x100\": Object.freeze({ width: 100, height: 100 }),\n \"100x150\": Object.freeze({ width: 100, height: 150 }),\n a4: Object.freeze({ width: 210, height: 297 }),\n }\n);\n\nexport type { LabelDimensions, LabelSize };\nexport default LABEL_SIZES;\n","import LABEL_SIZES from \"./label-sizes\";\nimport type { LabelSize } from \"./label-sizes\";\n\n/** Returns the `@page` rule that prints on a `size` label edge to edge (no page margin). */\nconst labelPrintCss = (size: LabelSize): string => {\n // Own keys only: \"toString\" or \"__proto__\" must not resolve through the prototype.\n if (typeof size !== \"string\" || !Object.hasOwn(LABEL_SIZES, size)) {\n const expected = Object.keys(LABEL_SIZES)\n .map((key) => `\"${key}\"`)\n .join(\", \");\n throw new TypeError(\n `labelPrintCss: unknown label size ${String(size)}; expected one of ${expected}`\n );\n }\n const { width, height } = LABEL_SIZES[size];\n return `@page { size: ${width}mm ${height}mm; margin: 0; }`;\n};\n\nexport default labelPrintCss;\n"],"names":[],"mappings":";;AAAA,MAAM,YAAY,GAAgC,IAAI,GAAG,CAAC;IACxD,CAAC,GAAG,EAAE,OAAO,CAAC;IACd,CAAC,GAAG,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,QAAQ,CAAC;IACf,CAAC,GAAG,EAAE,QAAQ,CAAC;AAChB,CAAA,CAAC;AAEF;AACA;AACA,MAAM,SAAS,GAAG,CAAC,SAAiB,KAClC,SAAS,KAAK,IAAI;AAClB,IAAA,SAAS,KAAK,IAAI;AAClB,IAAA,SAAS,KAAK,IAAI;AAClB,KAAC,SAAS,IAAI,IAAI,IAAI,SAAS,IAAI,MAAM,CAAC;AAC1C,KAAC,SAAS,IAAI,MAAM,IAAI,SAAS,IAAI,MAAM,CAAC;IAC5C,SAAS,IAAI,OAAO;AAEtB;AACA,MAAM,SAAS,GAAG,CAAC,KAAa,KAAY;IAC1C,IAAI,OAAO,GAAG,EAAE;AAChB,IAAA,KAAK,MAAM,SAAS,IAAI,KAAK,EAAE;QAC7B,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,CAAW,CAAC;YAAE;QACpD,OAAO,IAAI,YAAY,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,SAAS;IACrD;AACA,IAAA,OAAO,OAAO;AAChB,CAAC;;ACxBD,MAAM,KAAK,GAAG,CAAC;AACf,MAAM,KAAK,GAAG,CAAC;AACf,MAAM,KAAK,GAAG,CAAC;AAEf;AACA,MAAM,kBAAkB,GAAuB,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC;AACpE,MAAM,SAAS,GAAG,CAAC;AAEnB;AACA,MAAM,eAAe,GAAG,CAAC,GAAG,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,MAAM,EAAE,GAAG,EAAE,CAAC;AAC/C,CAAA;;ACZH;AACA,MAAM,aAAa,GAAG,CAAC,IAAe,KAAY;;AAEhD,IAAA,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,EAAE,IAAI,CAAC,EAAE;AACjE,QAAA,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW;aACrC,GAAG,CAAC,CAAC,GAAG,KAAK,CAAA,CAAA,EAAI,GAAG,CAAA,CAAA,CAAG;aACvB,IAAI,CAAC,IAAI,CAAC;AACb,QAAA,MAAM,IAAI,SAAS,CACjB,CAAA,kCAAA,EAAqC,MAAM,CAAC,IAAI,CAAC,CAAA,kBAAA,EAAqB,QAAQ,CAAA,CAAE,CACjF;IACH;IACA,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,WAAW,CAAC,IAAI,CAAC;AAC3C,IAAA,OAAO,CAAA,cAAA,EAAiB,KAAK,CAAA,GAAA,EAAM,MAAM,kBAAkB;AAC7D;;;;;"}
@@ -0,0 +1,45 @@
1
+ type LengthUnit = "px" | "mm";
2
+ type Code128SvgOptions = {
3
+ /** Width of the narrowest bar, in `unit` (default 2 px or 0.33 mm). */
4
+ moduleWidth?: number;
5
+ /** Unit of every length option and of the SVG size (default "px"). */
6
+ unit?: LengthUnit;
7
+ /** Bar height excluding the text line, in `unit` (default 80 px or 15 mm). */
8
+ height?: number;
9
+ /** Blank margin on the left and right, in modules (default 10, the Code 128 minimum). */
10
+ quietZone?: number;
11
+ /** Print the encoded text under the bars (default true). */
12
+ showText?: boolean;
13
+ /** Font size of the text line, in `unit` (default 14 px or 3 mm). */
14
+ fontSize?: number;
15
+ /** Colour of the bars and the text (default "#000000"). */
16
+ foreground?: string;
17
+ /** Colour of the background, quiet zones included (default "#ffffff"). */
18
+ background?: string;
19
+ /** Accessible name, emitted as `<title>` and `aria-label` (default: `aria-label` is the text). */
20
+ title?: string;
21
+ };
22
+ /** Renders `text` as a Code 128 barcode SVG string; needs no DOM. */
23
+ declare const code128Svg: (text: string, { unit, moduleWidth, height, quietZone, showText, fontSize, foreground, background, title, }?: Code128SvgOptions) => string;
24
+
25
+ /**
26
+ * Encodes ASCII text as a Code 128 symbol and returns its module widths: bar, space, bar...,
27
+ * from the start symbol to the stop symbol (quiet zones excluded).
28
+ */
29
+ declare const encodeCode128: (text: string) => number[];
30
+
31
+ type LabelSize = "100x100" | "100x150" | "a4";
32
+ type LabelDimensions = {
33
+ /** Page width in millimetres. */
34
+ readonly width: number;
35
+ /** Page height in millimetres. */
36
+ readonly height: number;
37
+ };
38
+ /** Page sizes of common shipping labels, in millimetres: 100×100 and 100×150 thermal, A4. */
39
+ declare const LABEL_SIZES: Readonly<Record<LabelSize, LabelDimensions>>;
40
+
41
+ /** Returns the `@page` rule that prints on a `size` label edge to edge (no page margin). */
42
+ declare const labelPrintCss: (size: LabelSize) => string;
43
+
44
+ export { LABEL_SIZES, code128Svg, encodeCode128, labelPrintCss };
45
+ export type { Code128SvgOptions, LabelDimensions, LabelSize, LengthUnit };
@@ -0,0 +1,45 @@
1
+ type LengthUnit = "px" | "mm";
2
+ type Code128SvgOptions = {
3
+ /** Width of the narrowest bar, in `unit` (default 2 px or 0.33 mm). */
4
+ moduleWidth?: number;
5
+ /** Unit of every length option and of the SVG size (default "px"). */
6
+ unit?: LengthUnit;
7
+ /** Bar height excluding the text line, in `unit` (default 80 px or 15 mm). */
8
+ height?: number;
9
+ /** Blank margin on the left and right, in modules (default 10, the Code 128 minimum). */
10
+ quietZone?: number;
11
+ /** Print the encoded text under the bars (default true). */
12
+ showText?: boolean;
13
+ /** Font size of the text line, in `unit` (default 14 px or 3 mm). */
14
+ fontSize?: number;
15
+ /** Colour of the bars and the text (default "#000000"). */
16
+ foreground?: string;
17
+ /** Colour of the background, quiet zones included (default "#ffffff"). */
18
+ background?: string;
19
+ /** Accessible name, emitted as `<title>` and `aria-label` (default: `aria-label` is the text). */
20
+ title?: string;
21
+ };
22
+ /** Renders `text` as a Code 128 barcode SVG string; needs no DOM. */
23
+ declare const code128Svg: (text: string, { unit, moduleWidth, height, quietZone, showText, fontSize, foreground, background, title, }?: Code128SvgOptions) => string;
24
+
25
+ /**
26
+ * Encodes ASCII text as a Code 128 symbol and returns its module widths: bar, space, bar...,
27
+ * from the start symbol to the stop symbol (quiet zones excluded).
28
+ */
29
+ declare const encodeCode128: (text: string) => number[];
30
+
31
+ type LabelSize = "100x100" | "100x150" | "a4";
32
+ type LabelDimensions = {
33
+ /** Page width in millimetres. */
34
+ readonly width: number;
35
+ /** Page height in millimetres. */
36
+ readonly height: number;
37
+ };
38
+ /** Page sizes of common shipping labels, in millimetres: 100×100 and 100×150 thermal, A4. */
39
+ declare const LABEL_SIZES: Readonly<Record<LabelSize, LabelDimensions>>;
40
+
41
+ /** Returns the `@page` rule that prints on a `size` label edge to edge (no page margin). */
42
+ declare const labelPrintCss: (size: LabelSize) => string;
43
+
44
+ export { LABEL_SIZES, code128Svg, encodeCode128, labelPrintCss };
45
+ export type { Code128SvgOptions, LabelDimensions, LabelSize, LengthUnit };
@@ -0,0 +1,279 @@
1
+ const XML_ENTITIES = new Map([
2
+ ["&", "&amp;"],
3
+ ["<", "&lt;"],
4
+ [">", "&gt;"],
5
+ ['"', "&quot;"],
6
+ ["'", "&apos;"],
7
+ ]);
8
+ // XML 1.0 "Char": anything else (NUL, most C0 controls, lone surrogates, U+FFFE/FFFF) makes the
9
+ // whole document unparseable, even as a character reference.
10
+ const isXmlChar = (codePoint) => codePoint === 0x09 ||
11
+ codePoint === 0x0a ||
12
+ codePoint === 0x0d ||
13
+ (codePoint >= 0x20 && codePoint <= 0xd7ff) ||
14
+ (codePoint >= 0xe000 && codePoint <= 0xfffd) ||
15
+ codePoint >= 0x10000;
16
+ /** Escapes a value for XML text and attributes, dropping characters XML cannot carry. */
17
+ const escapeXml = (value) => {
18
+ let escaped = "";
19
+ for (const character of value) {
20
+ if (!isXmlChar(character.codePointAt(0)))
21
+ continue;
22
+ escaped += XML_ENTITIES.get(character) ?? character;
23
+ }
24
+ return escaped;
25
+ };
26
+
27
+ const SET_A = 0;
28
+ const SET_B = 1;
29
+ const SET_C = 2;
30
+ // Tie-break order: when two plans are equally short, C (denser) wins, then B (text) before A.
31
+ const SETS_BY_PREFERENCE = [SET_C, SET_B, SET_A];
32
+ const SET_COUNT = 3;
33
+ // Indexed by code set: Start A/B/C, and the CODE A/B/C symbol that switches into that set.
34
+ const START_CODEWORDS = [103, 104, 105];
35
+ const SWITCH_CODEWORDS = [101, 100, 99];
36
+ const SHIFT_CODEWORD = 98;
37
+ const STOP_CODEWORD = 106;
38
+ const CHECKSUM_MODULUS = 103;
39
+ const MAX_ASCII = 127;
40
+ // Planner decisions per (position, code set); a switch is stored as ACTION_SWITCH + target set.
41
+ const ACTION_ENCODE = 0;
42
+ const ACTION_SHIFT = 1;
43
+ const ACTION_SWITCH = 2;
44
+ const isDigit = (code) => code >= 48 && code <= 57;
45
+ const otherTextSet = (set) => (set === SET_A ? SET_B : SET_A);
46
+ /** Characters one codeword of `set` consumes at `index`, or 0 when the set cannot encode them. */
47
+ const spanOf = (text, index, set) => {
48
+ const code = text.charCodeAt(index);
49
+ // charCodeAt past the end is NaN, so a trailing single digit is never taken as a pair.
50
+ if (set === SET_C) {
51
+ return isDigit(code) && isDigit(text.charCodeAt(index + 1)) ? 2 : 0;
52
+ }
53
+ if (set === SET_A)
54
+ return code < 96 ? 1 : 0;
55
+ return code >= 32 ? 1 : 0;
56
+ };
57
+ /** Symbol value of the characters at `index` in `set`; the caller has checked `spanOf`. */
58
+ const valueOf = (text, index, set) => {
59
+ const code = text.charCodeAt(index);
60
+ if (set === SET_C)
61
+ return (code - 48) * 10 + text.charCodeAt(index + 1) - 48;
62
+ // Set A puts the control characters (0–31) after the printable ones: NUL is value 64.
63
+ if (set === SET_A && code < 32)
64
+ return code + 64;
65
+ return code - 32;
66
+ };
67
+ const assertEncodable = (text, caller) => {
68
+ if (typeof text !== "string") {
69
+ throw new TypeError(`${caller}: expected a string, received ${text === null ? "null" : typeof text}`);
70
+ }
71
+ if (text.length === 0) {
72
+ throw new RangeError(`${caller}: text must contain at least one character`);
73
+ }
74
+ for (let index = 0; index < text.length; index++) {
75
+ if (text.charCodeAt(index) > MAX_ASCII) {
76
+ const character = String.fromCodePoint(text.codePointAt(index));
77
+ throw new TypeError(`${caller}: "${character}" at index ${index} is not ASCII; Code 128 encodes characters 0–127 only`);
78
+ }
79
+ }
80
+ return text;
81
+ };
82
+ /**
83
+ * Plans the shortest symbol sequence by dynamic programming over (position, current code set),
84
+ * so CODE C runs, shifts and switches are placed optimally rather than by look-ahead heuristics.
85
+ */
86
+ const planDataCodewords = (text) => {
87
+ const { length } = text;
88
+ // cost[i * 3 + set]: fewest symbols that encode text[i..] when `set` is active; the row for
89
+ // i = length stays 0.
90
+ const cost = new Float64Array((length + 1) * SET_COUNT);
91
+ const action = new Uint8Array(length * SET_COUNT);
92
+ const costAt = (index, set) => cost[index * SET_COUNT + set];
93
+ const direct = [0, 0, 0];
94
+ for (let index = length - 1; index >= 0; index--) {
95
+ for (const set of SETS_BY_PREFERENCE) {
96
+ let best = Infinity;
97
+ let choice = ACTION_ENCODE;
98
+ const span = spanOf(text, index, set);
99
+ if (span > 0)
100
+ best = 1 + costAt(index + span, set);
101
+ if (set !== SET_C && spanOf(text, index, otherTextSet(set)) > 0) {
102
+ const shifted = 2 + costAt(index + 1, set);
103
+ if (shifted < best) {
104
+ best = shifted;
105
+ choice = ACTION_SHIFT;
106
+ }
107
+ }
108
+ direct[set] = best;
109
+ action[index * SET_COUNT + set] = choice;
110
+ }
111
+ // Every ASCII character fits set A or B, so a switch always leads to a finite plan; switching
112
+ // twice in a row is never shorter than switching once to the final set.
113
+ for (const set of SETS_BY_PREFERENCE) {
114
+ let best = direct[set];
115
+ for (const target of SETS_BY_PREFERENCE) {
116
+ if (target !== set && 1 + direct[target] < best) {
117
+ best = 1 + direct[target];
118
+ action[index * SET_COUNT + set] = ACTION_SWITCH + target;
119
+ }
120
+ }
121
+ cost[index * SET_COUNT + set] = best;
122
+ }
123
+ }
124
+ let set = SET_C;
125
+ for (const candidate of SETS_BY_PREFERENCE) {
126
+ if (costAt(0, candidate) < costAt(0, set))
127
+ set = candidate;
128
+ }
129
+ const codewords = [START_CODEWORDS[set]];
130
+ let index = 0;
131
+ while (index < length) {
132
+ const step = action[index * SET_COUNT + set];
133
+ if (step >= ACTION_SWITCH) {
134
+ set = (step - ACTION_SWITCH);
135
+ codewords.push(SWITCH_CODEWORDS[set]);
136
+ }
137
+ else if (step === ACTION_SHIFT) {
138
+ codewords.push(SHIFT_CODEWORD, valueOf(text, index, otherTextSet(set)));
139
+ index += 1;
140
+ }
141
+ else {
142
+ codewords.push(valueOf(text, index, set));
143
+ index += spanOf(text, index, set);
144
+ }
145
+ }
146
+ return codewords;
147
+ };
148
+ /**
149
+ * Full Code 128 symbol sequence for `text`: start, data, mod-103 check symbol and stop.
150
+ * Throws TypeError for a non-string or non-ASCII text and RangeError for an empty one.
151
+ */
152
+ const toCode128Codewords = (text, caller) => {
153
+ const codewords = planDataCodewords(assertEncodable(text, caller));
154
+ // The start symbol has weight 1, like the first data symbol.
155
+ let checksum = codewords[0];
156
+ for (let position = 1; position < codewords.length; position++) {
157
+ checksum += codewords[position] * position;
158
+ }
159
+ codewords.push(checksum % CHECKSUM_MODULUS, STOP_CODEWORD);
160
+ return codewords;
161
+ };
162
+
163
+ // Bar/space module widths of every Code 128 symbol (ISO/IEC 15417), indexed by symbol value.
164
+ // Each string alternates bar, space, bar...; 0–105 span 11 modules, the stop symbol (106) spans 13
165
+ // because it carries the final 2-module termination bar.
166
+ // prettier-ignore
167
+ const PATTERN_SOURCE = [
168
+ "212222", "222122", "222221", "121223", "121322", "131222", "122213", "122312",
169
+ "132212", "221213", "221312", "231212", "112232", "122132", "122231", "113222",
170
+ "123122", "123221", "223211", "221132", "221231", "213212", "223112", "312131",
171
+ "311222", "321122", "321221", "312212", "322112", "322211", "212123", "212321",
172
+ "232121", "111323", "131123", "131321", "112313", "132113", "132311", "211313",
173
+ "231113", "231311", "112133", "112331", "132131", "113123", "113321", "133121",
174
+ "313121", "211331", "231131", "213113", "213311", "213131", "311123", "311321",
175
+ "331121", "312113", "312311", "332111", "314111", "221411", "431111", "111224",
176
+ "111422", "121124", "121421", "141122", "141221", "112214", "112412", "122114",
177
+ "122411", "142112", "142211", "241211", "221114", "413111", "241112", "134111",
178
+ "111242", "121142", "121241", "114212", "124112", "124211", "411212", "421112",
179
+ "421211", "212141", "214121", "412121", "111143", "111341", "131141", "114113",
180
+ "114311", "411113", "411311", "113141", "114131", "311141", "411131", "211412",
181
+ "211214", "211232", "2331112",
182
+ ];
183
+ /** Module widths per symbol value (0–106). */
184
+ const CODE128_PATTERNS = PATTERN_SOURCE.map((pattern) => Array.from(pattern, Number));
185
+
186
+ /**
187
+ * Encodes ASCII text as a Code 128 symbol and returns its module widths: bar, space, bar...,
188
+ * from the start symbol to the stop symbol (quiet zones excluded).
189
+ */
190
+ const encodeCode128 = (text) => {
191
+ const widths = [];
192
+ for (const codeword of toCode128Codewords(text, "encodeCode128")) {
193
+ // Codewords come from the encoder and are always 0–106, so the lookup cannot miss.
194
+ widths.push(...CODE128_PATTERNS[codeword]);
195
+ }
196
+ return widths;
197
+ };
198
+
199
+ const UNIT_DEFAULTS = {
200
+ px: { moduleWidth: 2, height: 80, fontSize: 14 },
201
+ mm: { moduleWidth: 0.33, height: 15, fontSize: 3 },
202
+ };
203
+ const DEFAULT_QUIET_ZONE = 10;
204
+ const DEFAULT_FOREGROUND = "#000000";
205
+ const DEFAULT_BACKGROUND = "#ffffff";
206
+ // Text line below the bars, in ems: the baseline sits 1.1 em under the bars, the line ends 1.4 em down.
207
+ const TEXT_BASELINE_EM = 1.1;
208
+ const TEXT_LINE_EM = 1.4;
209
+ // Enough for 0.001 mm / 0.001 px precision while cutting floating-point noise (0.9900000000000001).
210
+ const DECIMALS = 4;
211
+ const formatNumber = (value) => String(Number(value.toFixed(DECIMALS)));
212
+ const positiveOr = (value, fallback) => typeof value === "number" && Number.isFinite(value) && value > 0
213
+ ? value
214
+ : fallback;
215
+ /** Renders `text` as a Code 128 barcode SVG string; needs no DOM. */
216
+ const code128Svg = (text, { unit, moduleWidth, height, quietZone, showText = true, fontSize, foreground = DEFAULT_FOREGROUND, background = DEFAULT_BACKGROUND, title, } = {}) => {
217
+ const widths = encodeCode128(text);
218
+ const lengthUnit = unit === "mm" ? "mm" : "px";
219
+ const defaults = UNIT_DEFAULTS[lengthUnit];
220
+ const moduleSize = positiveOr(moduleWidth, defaults.moduleWidth);
221
+ const barHeight = positiveOr(height, defaults.height);
222
+ const textSize = positiveOr(fontSize, defaults.fontSize);
223
+ const margin = typeof quietZone === "number" &&
224
+ Number.isFinite(quietZone) &&
225
+ quietZone >= 0
226
+ ? Math.floor(quietZone)
227
+ : DEFAULT_QUIET_ZONE;
228
+ const hasText = showText !== false;
229
+ // Positions are counted in whole modules and scaled once, so no rounding error accumulates
230
+ // from bar to bar.
231
+ const bars = [];
232
+ let cursor = margin;
233
+ widths.forEach((width, index) => {
234
+ if (index % 2 === 0) {
235
+ const barWidth = formatNumber(width * moduleSize);
236
+ bars.push(`M${formatNumber(cursor * moduleSize)} 0h${barWidth}v${formatNumber(barHeight)}h-${barWidth}z`);
237
+ }
238
+ cursor += width;
239
+ });
240
+ const totalWidth = formatNumber((cursor + margin) * moduleSize);
241
+ const totalHeight = formatNumber(barHeight + (hasText ? textSize * TEXT_LINE_EM : 0));
242
+ const hasTitle = typeof title === "string" && title.length > 0;
243
+ const label = escapeXml(hasTitle ? title : text);
244
+ const fill = escapeXml(foreground);
245
+ const parts = [
246
+ `<svg xmlns="http://www.w3.org/2000/svg" width="${totalWidth}${lengthUnit}" height="${totalHeight}${lengthUnit}" viewBox="0 0 ${totalWidth} ${totalHeight}" role="img" aria-label="${label}">`,
247
+ hasTitle ? `<title>${label}</title>` : "",
248
+ `<rect width="${totalWidth}" height="${totalHeight}" fill="${escapeXml(background)}"/>`,
249
+ `<path fill="${fill}" shape-rendering="crispEdges" d="${bars.join("")}"/>`,
250
+ hasText
251
+ ? `<text x="${formatNumber(((cursor + margin) * moduleSize) / 2)}" y="${formatNumber(barHeight + textSize * TEXT_BASELINE_EM)}" fill="${fill}" font-family="monospace" font-size="${formatNumber(textSize)}" text-anchor="middle">${escapeXml(text)}</text>`
252
+ : "",
253
+ "</svg>",
254
+ ];
255
+ return parts.join("");
256
+ };
257
+
258
+ /** Page sizes of common shipping labels, in millimetres: 100×100 and 100×150 thermal, A4. */
259
+ const LABEL_SIZES = Object.freeze({
260
+ "100x100": Object.freeze({ width: 100, height: 100 }),
261
+ "100x150": Object.freeze({ width: 100, height: 150 }),
262
+ a4: Object.freeze({ width: 210, height: 297 }),
263
+ });
264
+
265
+ /** Returns the `@page` rule that prints on a `size` label edge to edge (no page margin). */
266
+ const labelPrintCss = (size) => {
267
+ // Own keys only: "toString" or "__proto__" must not resolve through the prototype.
268
+ if (typeof size !== "string" || !Object.hasOwn(LABEL_SIZES, size)) {
269
+ const expected = Object.keys(LABEL_SIZES)
270
+ .map((key) => `"${key}"`)
271
+ .join(", ");
272
+ throw new TypeError(`labelPrintCss: unknown label size ${String(size)}; expected one of ${expected}`);
273
+ }
274
+ const { width, height } = LABEL_SIZES[size];
275
+ return `@page { size: ${width}mm ${height}mm; margin: 0; }`;
276
+ };
277
+
278
+ export { LABEL_SIZES, code128Svg, encodeCode128, labelPrintCss };
279
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../lib/helpers/xml.helper.ts","../lib/helpers/code128-codewords.helper.ts","../lib/helpers/code128-patterns.helper.ts","../lib/modules/encode-code128.ts","../lib/modules/code128-svg.ts","../lib/modules/label-sizes.ts","../lib/modules/label-print-css.ts"],"sourcesContent":["const XML_ENTITIES: ReadonlyMap<string, string> = new Map([\n [\"&\", \"&amp;\"],\n [\"<\", \"&lt;\"],\n [\">\", \"&gt;\"],\n ['\"', \"&quot;\"],\n [\"'\", \"&apos;\"],\n]);\n\n// XML 1.0 \"Char\": anything else (NUL, most C0 controls, lone surrogates, U+FFFE/FFFF) makes the\n// whole document unparseable, even as a character reference.\nconst isXmlChar = (codePoint: number): boolean =>\n codePoint === 0x09 ||\n codePoint === 0x0a ||\n codePoint === 0x0d ||\n (codePoint >= 0x20 && codePoint <= 0xd7ff) ||\n (codePoint >= 0xe000 && codePoint <= 0xfffd) ||\n codePoint >= 0x10000;\n\n/** Escapes a value for XML text and attributes, dropping characters XML cannot carry. */\nconst escapeXml = (value: string): string => {\n let escaped = \"\";\n for (const character of value) {\n if (!isXmlChar(character.codePointAt(0) as number)) continue;\n escaped += XML_ENTITIES.get(character) ?? character;\n }\n return escaped;\n};\n\nexport { escapeXml };\n","type CodeSet = 0 | 1 | 2;\n\nconst SET_A = 0;\nconst SET_B = 1;\nconst SET_C = 2;\n\n// Tie-break order: when two plans are equally short, C (denser) wins, then B (text) before A.\nconst SETS_BY_PREFERENCE: readonly CodeSet[] = [SET_C, SET_B, SET_A];\nconst SET_COUNT = 3;\n\n// Indexed by code set: Start A/B/C, and the CODE A/B/C symbol that switches into that set.\nconst START_CODEWORDS = [103, 104, 105] as const;\nconst SWITCH_CODEWORDS = [101, 100, 99] as const;\nconst SHIFT_CODEWORD = 98;\nconst STOP_CODEWORD = 106;\nconst CHECKSUM_MODULUS = 103;\n\nconst MAX_ASCII = 127;\n\n// Planner decisions per (position, code set); a switch is stored as ACTION_SWITCH + target set.\nconst ACTION_ENCODE = 0;\nconst ACTION_SHIFT = 1;\nconst ACTION_SWITCH = 2;\n\nconst isDigit = (code: number): boolean => code >= 48 && code <= 57;\n\nconst otherTextSet = (set: CodeSet): CodeSet => (set === SET_A ? SET_B : SET_A);\n\n/** Characters one codeword of `set` consumes at `index`, or 0 when the set cannot encode them. */\nconst spanOf = (text: string, index: number, set: CodeSet): number => {\n const code = text.charCodeAt(index);\n // charCodeAt past the end is NaN, so a trailing single digit is never taken as a pair.\n if (set === SET_C) {\n return isDigit(code) && isDigit(text.charCodeAt(index + 1)) ? 2 : 0;\n }\n if (set === SET_A) return code < 96 ? 1 : 0;\n return code >= 32 ? 1 : 0;\n};\n\n/** Symbol value of the characters at `index` in `set`; the caller has checked `spanOf`. */\nconst valueOf = (text: string, index: number, set: CodeSet): number => {\n const code = text.charCodeAt(index);\n if (set === SET_C) return (code - 48) * 10 + text.charCodeAt(index + 1) - 48;\n // Set A puts the control characters (0–31) after the printable ones: NUL is value 64.\n if (set === SET_A && code < 32) return code + 64;\n return code - 32;\n};\n\nconst assertEncodable = (text: unknown, caller: string): string => {\n if (typeof text !== \"string\") {\n throw new TypeError(\n `${caller}: expected a string, received ${text === null ? \"null\" : typeof text}`\n );\n }\n if (text.length === 0) {\n throw new RangeError(`${caller}: text must contain at least one character`);\n }\n for (let index = 0; index < text.length; index++) {\n if (text.charCodeAt(index) > MAX_ASCII) {\n const character = String.fromCodePoint(text.codePointAt(index) as number);\n throw new TypeError(\n `${caller}: \"${character}\" at index ${index} is not ASCII; Code 128 encodes characters 0–127 only`\n );\n }\n }\n return text;\n};\n\n/**\n * Plans the shortest symbol sequence by dynamic programming over (position, current code set),\n * so CODE C runs, shifts and switches are placed optimally rather than by look-ahead heuristics.\n */\nconst planDataCodewords = (text: string): number[] => {\n const { length } = text;\n // cost[i * 3 + set]: fewest symbols that encode text[i..] when `set` is active; the row for\n // i = length stays 0.\n const cost = new Float64Array((length + 1) * SET_COUNT);\n const action = new Uint8Array(length * SET_COUNT);\n const costAt = (index: number, set: CodeSet): number =>\n cost[index * SET_COUNT + set] as number;\n const direct: [number, number, number] = [0, 0, 0];\n\n for (let index = length - 1; index >= 0; index--) {\n for (const set of SETS_BY_PREFERENCE) {\n let best = Infinity;\n let choice = ACTION_ENCODE;\n const span = spanOf(text, index, set);\n if (span > 0) best = 1 + costAt(index + span, set);\n if (set !== SET_C && spanOf(text, index, otherTextSet(set)) > 0) {\n const shifted = 2 + costAt(index + 1, set);\n if (shifted < best) {\n best = shifted;\n choice = ACTION_SHIFT;\n }\n }\n direct[set] = best;\n action[index * SET_COUNT + set] = choice;\n }\n // Every ASCII character fits set A or B, so a switch always leads to a finite plan; switching\n // twice in a row is never shorter than switching once to the final set.\n for (const set of SETS_BY_PREFERENCE) {\n let best = direct[set];\n for (const target of SETS_BY_PREFERENCE) {\n if (target !== set && 1 + direct[target] < best) {\n best = 1 + direct[target];\n action[index * SET_COUNT + set] = ACTION_SWITCH + target;\n }\n }\n cost[index * SET_COUNT + set] = best;\n }\n }\n\n let set: CodeSet = SET_C;\n for (const candidate of SETS_BY_PREFERENCE) {\n if (costAt(0, candidate) < costAt(0, set)) set = candidate;\n }\n\n const codewords: number[] = [START_CODEWORDS[set]];\n let index = 0;\n while (index < length) {\n const step = action[index * SET_COUNT + set] as number;\n if (step >= ACTION_SWITCH) {\n set = (step - ACTION_SWITCH) as CodeSet;\n codewords.push(SWITCH_CODEWORDS[set]);\n } else if (step === ACTION_SHIFT) {\n codewords.push(SHIFT_CODEWORD, valueOf(text, index, otherTextSet(set)));\n index += 1;\n } else {\n codewords.push(valueOf(text, index, set));\n index += spanOf(text, index, set);\n }\n }\n return codewords;\n};\n\n/**\n * Full Code 128 symbol sequence for `text`: start, data, mod-103 check symbol and stop.\n * Throws TypeError for a non-string or non-ASCII text and RangeError for an empty one.\n */\nconst toCode128Codewords = (text: string, caller: string): number[] => {\n const codewords = planDataCodewords(assertEncodable(text, caller));\n // The start symbol has weight 1, like the first data symbol.\n let checksum = codewords[0] as number;\n for (let position = 1; position < codewords.length; position++) {\n checksum += (codewords[position] as number) * position;\n }\n codewords.push(checksum % CHECKSUM_MODULUS, STOP_CODEWORD);\n return codewords;\n};\n\nexport { toCode128Codewords };\n","// Bar/space module widths of every Code 128 symbol (ISO/IEC 15417), indexed by symbol value.\n// Each string alternates bar, space, bar...; 0–105 span 11 modules, the stop symbol (106) spans 13\n// because it carries the final 2-module termination bar.\n// prettier-ignore\nconst PATTERN_SOURCE = [\n \"212222\", \"222122\", \"222221\", \"121223\", \"121322\", \"131222\", \"122213\", \"122312\",\n \"132212\", \"221213\", \"221312\", \"231212\", \"112232\", \"122132\", \"122231\", \"113222\",\n \"123122\", \"123221\", \"223211\", \"221132\", \"221231\", \"213212\", \"223112\", \"312131\",\n \"311222\", \"321122\", \"321221\", \"312212\", \"322112\", \"322211\", \"212123\", \"212321\",\n \"232121\", \"111323\", \"131123\", \"131321\", \"112313\", \"132113\", \"132311\", \"211313\",\n \"231113\", \"231311\", \"112133\", \"112331\", \"132131\", \"113123\", \"113321\", \"133121\",\n \"313121\", \"211331\", \"231131\", \"213113\", \"213311\", \"213131\", \"311123\", \"311321\",\n \"331121\", \"312113\", \"312311\", \"332111\", \"314111\", \"221411\", \"431111\", \"111224\",\n \"111422\", \"121124\", \"121421\", \"141122\", \"141221\", \"112214\", \"112412\", \"122114\",\n \"122411\", \"142112\", \"142211\", \"241211\", \"221114\", \"413111\", \"241112\", \"134111\",\n \"111242\", \"121142\", \"121241\", \"114212\", \"124112\", \"124211\", \"411212\", \"421112\",\n \"421211\", \"212141\", \"214121\", \"412121\", \"111143\", \"111341\", \"131141\", \"114113\",\n \"114311\", \"411113\", \"411311\", \"113141\", \"114131\", \"311141\", \"411131\", \"211412\",\n \"211214\", \"211232\", \"2331112\",\n];\n\n/** Module widths per symbol value (0–106). */\nconst CODE128_PATTERNS: readonly (readonly number[])[] = PATTERN_SOURCE.map(\n (pattern) => Array.from(pattern, Number)\n);\n\nexport { CODE128_PATTERNS };\n","import { toCode128Codewords } from \"../helpers/code128-codewords.helper\";\nimport { CODE128_PATTERNS } from \"../helpers/code128-patterns.helper\";\n\n/**\n * Encodes ASCII text as a Code 128 symbol and returns its module widths: bar, space, bar...,\n * from the start symbol to the stop symbol (quiet zones excluded).\n */\nconst encodeCode128 = (text: string): number[] => {\n const widths: number[] = [];\n for (const codeword of toCode128Codewords(text, \"encodeCode128\")) {\n // Codewords come from the encoder and are always 0–106, so the lookup cannot miss.\n widths.push(...(CODE128_PATTERNS[codeword] as readonly number[]));\n }\n return widths;\n};\n\nexport default encodeCode128;\n","import { escapeXml } from \"../helpers/xml.helper\";\nimport encodeCode128 from \"./encode-code128\";\n\ntype LengthUnit = \"px\" | \"mm\";\n\ntype Code128SvgOptions = {\n /** Width of the narrowest bar, in `unit` (default 2 px or 0.33 mm). */\n moduleWidth?: number;\n /** Unit of every length option and of the SVG size (default \"px\"). */\n unit?: LengthUnit;\n /** Bar height excluding the text line, in `unit` (default 80 px or 15 mm). */\n height?: number;\n /** Blank margin on the left and right, in modules (default 10, the Code 128 minimum). */\n quietZone?: number;\n /** Print the encoded text under the bars (default true). */\n showText?: boolean;\n /** Font size of the text line, in `unit` (default 14 px or 3 mm). */\n fontSize?: number;\n /** Colour of the bars and the text (default \"#000000\"). */\n foreground?: string;\n /** Colour of the background, quiet zones included (default \"#ffffff\"). */\n background?: string;\n /** Accessible name, emitted as `<title>` and `aria-label` (default: `aria-label` is the text). */\n title?: string;\n};\n\nconst UNIT_DEFAULTS = {\n px: { moduleWidth: 2, height: 80, fontSize: 14 },\n mm: { moduleWidth: 0.33, height: 15, fontSize: 3 },\n} as const;\nconst DEFAULT_QUIET_ZONE = 10;\nconst DEFAULT_FOREGROUND = \"#000000\";\nconst DEFAULT_BACKGROUND = \"#ffffff\";\n// Text line below the bars, in ems: the baseline sits 1.1 em under the bars, the line ends 1.4 em down.\nconst TEXT_BASELINE_EM = 1.1;\nconst TEXT_LINE_EM = 1.4;\n// Enough for 0.001 mm / 0.001 px precision while cutting floating-point noise (0.9900000000000001).\nconst DECIMALS = 4;\n\nconst formatNumber = (value: number): string =>\n String(Number(value.toFixed(DECIMALS)));\n\nconst positiveOr = (value: number | undefined, fallback: number): number =>\n typeof value === \"number\" && Number.isFinite(value) && value > 0\n ? value\n : fallback;\n\n/** Renders `text` as a Code 128 barcode SVG string; needs no DOM. */\nconst code128Svg = (\n text: string,\n {\n unit,\n moduleWidth,\n height,\n quietZone,\n showText = true,\n fontSize,\n foreground = DEFAULT_FOREGROUND,\n background = DEFAULT_BACKGROUND,\n title,\n }: Code128SvgOptions = {}\n): string => {\n const widths = encodeCode128(text);\n const lengthUnit: LengthUnit = unit === \"mm\" ? \"mm\" : \"px\";\n const defaults = UNIT_DEFAULTS[lengthUnit];\n const moduleSize = positiveOr(moduleWidth, defaults.moduleWidth);\n const barHeight = positiveOr(height, defaults.height);\n const textSize = positiveOr(fontSize, defaults.fontSize);\n const margin =\n typeof quietZone === \"number\" &&\n Number.isFinite(quietZone) &&\n quietZone >= 0\n ? Math.floor(quietZone)\n : DEFAULT_QUIET_ZONE;\n const hasText = showText !== false;\n\n // Positions are counted in whole modules and scaled once, so no rounding error accumulates\n // from bar to bar.\n const bars: string[] = [];\n let cursor = margin;\n widths.forEach((width, index) => {\n if (index % 2 === 0) {\n const barWidth = formatNumber(width * moduleSize);\n bars.push(\n `M${formatNumber(cursor * moduleSize)} 0h${barWidth}v${formatNumber(barHeight)}h-${barWidth}z`\n );\n }\n cursor += width;\n });\n\n const totalWidth = formatNumber((cursor + margin) * moduleSize);\n const totalHeight = formatNumber(\n barHeight + (hasText ? textSize * TEXT_LINE_EM : 0)\n );\n const hasTitle = typeof title === \"string\" && title.length > 0;\n const label = escapeXml(hasTitle ? title : text);\n const fill = escapeXml(foreground);\n\n const parts = [\n `<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"${totalWidth}${lengthUnit}\" height=\"${totalHeight}${lengthUnit}\" viewBox=\"0 0 ${totalWidth} ${totalHeight}\" role=\"img\" aria-label=\"${label}\">`,\n hasTitle ? `<title>${label}</title>` : \"\",\n `<rect width=\"${totalWidth}\" height=\"${totalHeight}\" fill=\"${escapeXml(background)}\"/>`,\n `<path fill=\"${fill}\" shape-rendering=\"crispEdges\" d=\"${bars.join(\"\")}\"/>`,\n hasText\n ? `<text x=\"${formatNumber(((cursor + margin) * moduleSize) / 2)}\" y=\"${formatNumber(barHeight + textSize * TEXT_BASELINE_EM)}\" fill=\"${fill}\" font-family=\"monospace\" font-size=\"${formatNumber(textSize)}\" text-anchor=\"middle\">${escapeXml(text)}</text>`\n : \"\",\n \"</svg>\",\n ];\n return parts.join(\"\");\n};\n\nexport type { Code128SvgOptions, LengthUnit };\nexport default code128Svg;\n","type LabelSize = \"100x100\" | \"100x150\" | \"a4\";\n\ntype LabelDimensions = {\n /** Page width in millimetres. */\n readonly width: number;\n /** Page height in millimetres. */\n readonly height: number;\n};\n\n/** Page sizes of common shipping labels, in millimetres: 100×100 and 100×150 thermal, A4. */\nconst LABEL_SIZES: Readonly<Record<LabelSize, LabelDimensions>> = Object.freeze(\n {\n \"100x100\": Object.freeze({ width: 100, height: 100 }),\n \"100x150\": Object.freeze({ width: 100, height: 150 }),\n a4: Object.freeze({ width: 210, height: 297 }),\n }\n);\n\nexport type { LabelDimensions, LabelSize };\nexport default LABEL_SIZES;\n","import LABEL_SIZES from \"./label-sizes\";\nimport type { LabelSize } from \"./label-sizes\";\n\n/** Returns the `@page` rule that prints on a `size` label edge to edge (no page margin). */\nconst labelPrintCss = (size: LabelSize): string => {\n // Own keys only: \"toString\" or \"__proto__\" must not resolve through the prototype.\n if (typeof size !== \"string\" || !Object.hasOwn(LABEL_SIZES, size)) {\n const expected = Object.keys(LABEL_SIZES)\n .map((key) => `\"${key}\"`)\n .join(\", \");\n throw new TypeError(\n `labelPrintCss: unknown label size ${String(size)}; expected one of ${expected}`\n );\n }\n const { width, height } = LABEL_SIZES[size];\n return `@page { size: ${width}mm ${height}mm; margin: 0; }`;\n};\n\nexport default labelPrintCss;\n"],"names":[],"mappings":"AAAA,MAAM,YAAY,GAAgC,IAAI,GAAG,CAAC;IACxD,CAAC,GAAG,EAAE,OAAO,CAAC;IACd,CAAC,GAAG,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,QAAQ,CAAC;IACf,CAAC,GAAG,EAAE,QAAQ,CAAC;AAChB,C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AAI,SAAS,CACjB,CAAA,kCAAA,EAAqC,MAAM,CAAC,IAAI,CAAC,CAAA,kBAAA,EAAqB,QAAQ,CAAA,CAAE,CACjF;IACH;IACA,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,WAAW,CAAC,IAAI,CAAC;AAC3C,IAAA,OAAO,CAAA,cAAA,EAAiB,KAAK,CAAA,GAAA,EAAM,MAAM,kBAAkB;AAC7D;;"}
@@ -0,0 +1,2 @@
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).handijitBarcode={})}(this,function(e){"use strict";const t=new Map([["&","&amp;"],["<","&lt;"],[">","&gt;"],['"',"&quot;"],["'","&apos;"]]),o=e=>9===e||10===e||13===e||e>=32&&e<=55295||e>=57344&&e<=65533||e>=65536,n=e=>{let n="";for(const r of e)o(r.codePointAt(0))&&(n+=t.get(r)??r);return n},r=[2,1,0],i=[103,104,105],f=[101,100,99],s=e=>e>=48&&e<=57,h=e=>0===e?1:0,c=(e,t,o)=>{const n=e.charCodeAt(t);return 2===o?s(n)&&s(e.charCodeAt(t+1))?2:0:0===o?n<96?1:0:n>=32?1:0},l=(e,t,o)=>{const n=e.charCodeAt(t);return 2===o?10*(n-48)+e.charCodeAt(t+1)-48:0===o&&n<32?n+64:n-32},d=(e,t)=>{const o=(e=>{const{length:t}=e,o=new Float64Array(3*(t+1)),n=new Uint8Array(3*t),s=(e,t)=>o[3*e+t],d=[0,0,0];for(let i=t-1;i>=0;i--){for(const t of r){let o=1/0,r=0;const f=c(e,i,t);if(f>0&&(o=1+s(i+f,t)),2!==t&&c(e,i,h(t))>0){const e=2+s(i+1,t);e<o&&(o=e,r=1)}d[t]=o,n[3*i+t]=r}for(const e of r){let t=d[e];for(const o of r)o!==e&&1+d[o]<t&&(t=1+d[o],n[3*i+e]=2+o);o[3*i+e]=t}}let a=2;for(const e of r)s(0,e)<s(0,a)&&(a=e);const u=[i[a]];let g=0;for(;g<t;){const t=n[3*g+a];t>=2?(a=t-2,u.push(f[a])):1===t?(u.push(98,l(e,g,h(a))),g+=1):(u.push(l(e,g,a)),g+=c(e,g,a))}return u})(((e,t)=>{if("string"!=typeof e)throw new TypeError(`${t}: expected a string, received ${null===e?"null":typeof e}`);if(0===e.length)throw new RangeError(`${t}: text must contain at least one character`);for(let o=0;o<e.length;o++)if(e.charCodeAt(o)>127){const n=String.fromCodePoint(e.codePointAt(o));throw new TypeError(`${t}: "${n}" at index ${o} is not ASCII; Code 128 encodes characters 0–127 only`)}return e})(e,t));let n=o[0];for(let e=1;e<o.length;e++)n+=o[e]*e;return o.push(n%103,106),o},a=["212222","222122","222221","121223","121322","131222","122213","122312","132212","221213","221312","231212","112232","122132","122231","113222","123122","123221","223211","221132","221231","213212","223112","312131","311222","321122","321221","312212","322112","322211","212123","212321","232121","111323","131123","131321","112313","132113","132311","211313","231113","231311","112133","112331","132131","113123","113321","133121","313121","211331","231131","213113","213311","213131","311123","311321","331121","312113","312311","332111","314111","221411","431111","111224","111422","121124","121421","141122","141221","112214","112412","122114","122411","142112","142211","241211","221114","413111","241112","134111","111242","121142","121241","114212","124112","124211","411212","421112","421211","212141","214121","412121","111143","111341","131141","114113","114311","411113","411311","113141","114131","311141","411131","211412","211214","211232","2331112"].map(e=>Array.from(e,Number)),u=e=>{const t=[];for(const o of d(e,"encodeCode128"))t.push(...a[o]);return t},g={px:{moduleWidth:2,height:80,fontSize:14},mm:{moduleWidth:.33,height:15,fontSize:3}},p=e=>String(Number(e.toFixed(4))),m=(e,t)=>"number"==typeof e&&Number.isFinite(e)&&e>0?e:t,$=Object.freeze({"100x100":Object.freeze({width:100,height:100}),"100x150":Object.freeze({width:100,height:150}),a4:Object.freeze({width:210,height:297})});e.LABEL_SIZES=$,e.code128Svg=(e,{unit:t,moduleWidth:o,height:r,quietZone:i,showText:f=!0,fontSize:s,foreground:h="#000000",background:c="#ffffff",title:l}={})=>{const d=u(e),a="mm"===t?"mm":"px",$=g[a],w=m(o,$.moduleWidth),b=m(r,$.height),y=m(s,$.fontSize),x="number"==typeof i&&Number.isFinite(i)&&i>=0?Math.floor(i):10,z=!1!==f,A=[];let C=x;d.forEach((e,t)=>{if(t%2==0){const t=p(e*w);A.push(`M${p(C*w)} 0h${t}v${p(b)}h-${t}z`)}C+=e});const j=p((C+x)*w),S=p(b+(z?1.4*y:0)),E="string"==typeof l&&l.length>0,v=n(E?l:e),O=n(h);return[`<svg xmlns="http://www.w3.org/2000/svg" width="${j}${a}" height="${S}${a}" viewBox="0 0 ${j} ${S}" role="img" aria-label="${v}">`,E?`<title>${v}</title>`:"",`<rect width="${j}" height="${S}" fill="${n(c)}"/>`,`<path fill="${O}" shape-rendering="crispEdges" d="${A.join("")}"/>`,z?`<text x="${p((C+x)*w/2)}" y="${p(b+1.1*y)}" fill="${O}" font-family="monospace" font-size="${p(y)}" text-anchor="middle">${n(e)}</text>`:"","</svg>"].join("")},e.encodeCode128=u,e.labelPrintCss=e=>{if("string"!=typeof e||!Object.hasOwn($,e)){const t=Object.keys($).map(e=>`"${e}"`).join(", ");throw new TypeError(`labelPrintCss: unknown label size ${String(e)}; expected one of ${t}`)}const{width:t,height:o}=$[e];return`@page { size: ${t}mm ${o}mm; margin: 0; }`}});
2
+ //# sourceMappingURL=index.umd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.umd.js","sources":["../lib/helpers/xml.helper.ts","../lib/helpers/code128-codewords.helper.ts","../lib/helpers/code128-patterns.helper.ts","../lib/modules/encode-code128.ts","../lib/modules/code128-svg.ts","../lib/modules/label-sizes.ts","../lib/modules/label-print-css.ts"],"sourcesContent":["const XML_ENTITIES: ReadonlyMap<string, string> = new Map([\n [\"&\", \"&amp;\"],\n [\"<\", \"&lt;\"],\n [\">\", \"&gt;\"],\n ['\"', \"&quot;\"],\n [\"'\", \"&apos;\"],\n]);\n\n// XML 1.0 \"Char\": anything else (NUL, most C0 controls, lone surrogates, U+FFFE/FFFF) makes the\n// whole document unparseable, even as a character reference.\nconst isXmlChar = (codePoint: number): boolean =>\n codePoint === 0x09 ||\n codePoint === 0x0a ||\n codePoint === 0x0d ||\n (codePoint >= 0x20 && codePoint <= 0xd7ff) ||\n (codePoint >= 0xe000 && codePoint <= 0xfffd) ||\n codePoint >= 0x10000;\n\n/** Escapes a value for XML text and attributes, dropping characters XML cannot carry. */\nconst escapeXml = (value: string): string => {\n let escaped = \"\";\n for (const character of value) {\n if (!isXmlChar(character.codePointAt(0) as number)) continue;\n escaped += XML_ENTITIES.get(character) ?? character;\n }\n return escaped;\n};\n\nexport { escapeXml };\n","type CodeSet = 0 | 1 | 2;\n\nconst SET_A = 0;\nconst SET_B = 1;\nconst SET_C = 2;\n\n// Tie-break order: when two plans are equally short, C (denser) wins, then B (text) before A.\nconst SETS_BY_PREFERENCE: readonly CodeSet[] = [SET_C, SET_B, SET_A];\nconst SET_COUNT = 3;\n\n// Indexed by code set: Start A/B/C, and the CODE A/B/C symbol that switches into that set.\nconst START_CODEWORDS = [103, 104, 105] as const;\nconst SWITCH_CODEWORDS = [101, 100, 99] as const;\nconst SHIFT_CODEWORD = 98;\nconst STOP_CODEWORD = 106;\nconst CHECKSUM_MODULUS = 103;\n\nconst MAX_ASCII = 127;\n\n// Planner decisions per (position, code set); a switch is stored as ACTION_SWITCH + target set.\nconst ACTION_ENCODE = 0;\nconst ACTION_SHIFT = 1;\nconst ACTION_SWITCH = 2;\n\nconst isDigit = (code: number): boolean => code >= 48 && code <= 57;\n\nconst otherTextSet = (set: CodeSet): CodeSet => (set === SET_A ? SET_B : SET_A);\n\n/** Characters one codeword of `set` consumes at `index`, or 0 when the set cannot encode them. */\nconst spanOf = (text: string, index: number, set: CodeSet): number => {\n const code = text.charCodeAt(index);\n // charCodeAt past the end is NaN, so a trailing single digit is never taken as a pair.\n if (set === SET_C) {\n return isDigit(code) && isDigit(text.charCodeAt(index + 1)) ? 2 : 0;\n }\n if (set === SET_A) return code < 96 ? 1 : 0;\n return code >= 32 ? 1 : 0;\n};\n\n/** Symbol value of the characters at `index` in `set`; the caller has checked `spanOf`. */\nconst valueOf = (text: string, index: number, set: CodeSet): number => {\n const code = text.charCodeAt(index);\n if (set === SET_C) return (code - 48) * 10 + text.charCodeAt(index + 1) - 48;\n // Set A puts the control characters (0–31) after the printable ones: NUL is value 64.\n if (set === SET_A && code < 32) return code + 64;\n return code - 32;\n};\n\nconst assertEncodable = (text: unknown, caller: string): string => {\n if (typeof text !== \"string\") {\n throw new TypeError(\n `${caller}: expected a string, received ${text === null ? \"null\" : typeof text}`\n );\n }\n if (text.length === 0) {\n throw new RangeError(`${caller}: text must contain at least one character`);\n }\n for (let index = 0; index < text.length; index++) {\n if (text.charCodeAt(index) > MAX_ASCII) {\n const character = String.fromCodePoint(text.codePointAt(index) as number);\n throw new TypeError(\n `${caller}: \"${character}\" at index ${index} is not ASCII; Code 128 encodes characters 0–127 only`\n );\n }\n }\n return text;\n};\n\n/**\n * Plans the shortest symbol sequence by dynamic programming over (position, current code set),\n * so CODE C runs, shifts and switches are placed optimally rather than by look-ahead heuristics.\n */\nconst planDataCodewords = (text: string): number[] => {\n const { length } = text;\n // cost[i * 3 + set]: fewest symbols that encode text[i..] when `set` is active; the row for\n // i = length stays 0.\n const cost = new Float64Array((length + 1) * SET_COUNT);\n const action = new Uint8Array(length * SET_COUNT);\n const costAt = (index: number, set: CodeSet): number =>\n cost[index * SET_COUNT + set] as number;\n const direct: [number, number, number] = [0, 0, 0];\n\n for (let index = length - 1; index >= 0; index--) {\n for (const set of SETS_BY_PREFERENCE) {\n let best = Infinity;\n let choice = ACTION_ENCODE;\n const span = spanOf(text, index, set);\n if (span > 0) best = 1 + costAt(index + span, set);\n if (set !== SET_C && spanOf(text, index, otherTextSet(set)) > 0) {\n const shifted = 2 + costAt(index + 1, set);\n if (shifted < best) {\n best = shifted;\n choice = ACTION_SHIFT;\n }\n }\n direct[set] = best;\n action[index * SET_COUNT + set] = choice;\n }\n // Every ASCII character fits set A or B, so a switch always leads to a finite plan; switching\n // twice in a row is never shorter than switching once to the final set.\n for (const set of SETS_BY_PREFERENCE) {\n let best = direct[set];\n for (const target of SETS_BY_PREFERENCE) {\n if (target !== set && 1 + direct[target] < best) {\n best = 1 + direct[target];\n action[index * SET_COUNT + set] = ACTION_SWITCH + target;\n }\n }\n cost[index * SET_COUNT + set] = best;\n }\n }\n\n let set: CodeSet = SET_C;\n for (const candidate of SETS_BY_PREFERENCE) {\n if (costAt(0, candidate) < costAt(0, set)) set = candidate;\n }\n\n const codewords: number[] = [START_CODEWORDS[set]];\n let index = 0;\n while (index < length) {\n const step = action[index * SET_COUNT + set] as number;\n if (step >= ACTION_SWITCH) {\n set = (step - ACTION_SWITCH) as CodeSet;\n codewords.push(SWITCH_CODEWORDS[set]);\n } else if (step === ACTION_SHIFT) {\n codewords.push(SHIFT_CODEWORD, valueOf(text, index, otherTextSet(set)));\n index += 1;\n } else {\n codewords.push(valueOf(text, index, set));\n index += spanOf(text, index, set);\n }\n }\n return codewords;\n};\n\n/**\n * Full Code 128 symbol sequence for `text`: start, data, mod-103 check symbol and stop.\n * Throws TypeError for a non-string or non-ASCII text and RangeError for an empty one.\n */\nconst toCode128Codewords = (text: string, caller: string): number[] => {\n const codewords = planDataCodewords(assertEncodable(text, caller));\n // The start symbol has weight 1, like the first data symbol.\n let checksum = codewords[0] as number;\n for (let position = 1; position < codewords.length; position++) {\n checksum += (codewords[position] as number) * position;\n }\n codewords.push(checksum % CHECKSUM_MODULUS, STOP_CODEWORD);\n return codewords;\n};\n\nexport { toCode128Codewords };\n","// Bar/space module widths of every Code 128 symbol (ISO/IEC 15417), indexed by symbol value.\n// Each string alternates bar, space, bar...; 0–105 span 11 modules, the stop symbol (106) spans 13\n// because it carries the final 2-module termination bar.\n// prettier-ignore\nconst PATTERN_SOURCE = [\n \"212222\", \"222122\", \"222221\", \"121223\", \"121322\", \"131222\", \"122213\", \"122312\",\n \"132212\", \"221213\", \"221312\", \"231212\", \"112232\", \"122132\", \"122231\", \"113222\",\n \"123122\", \"123221\", \"223211\", \"221132\", \"221231\", \"213212\", \"223112\", \"312131\",\n \"311222\", \"321122\", \"321221\", \"312212\", \"322112\", \"322211\", \"212123\", \"212321\",\n \"232121\", \"111323\", \"131123\", \"131321\", \"112313\", \"132113\", \"132311\", \"211313\",\n \"231113\", \"231311\", \"112133\", \"112331\", \"132131\", \"113123\", \"113321\", \"133121\",\n \"313121\", \"211331\", \"231131\", \"213113\", \"213311\", \"213131\", \"311123\", \"311321\",\n \"331121\", \"312113\", \"312311\", \"332111\", \"314111\", \"221411\", \"431111\", \"111224\",\n \"111422\", \"121124\", \"121421\", \"141122\", \"141221\", \"112214\", \"112412\", \"122114\",\n \"122411\", \"142112\", \"142211\", \"241211\", \"221114\", \"413111\", \"241112\", \"134111\",\n \"111242\", \"121142\", \"121241\", \"114212\", \"124112\", \"124211\", \"411212\", \"421112\",\n \"421211\", \"212141\", \"214121\", \"412121\", \"111143\", \"111341\", \"131141\", \"114113\",\n \"114311\", \"411113\", \"411311\", \"113141\", \"114131\", \"311141\", \"411131\", \"211412\",\n \"211214\", \"211232\", \"2331112\",\n];\n\n/** Module widths per symbol value (0–106). */\nconst CODE128_PATTERNS: readonly (readonly number[])[] = PATTERN_SOURCE.map(\n (pattern) => Array.from(pattern, Number)\n);\n\nexport { CODE128_PATTERNS };\n","import { toCode128Codewords } from \"../helpers/code128-codewords.helper\";\nimport { CODE128_PATTERNS } from \"../helpers/code128-patterns.helper\";\n\n/**\n * Encodes ASCII text as a Code 128 symbol and returns its module widths: bar, space, bar...,\n * from the start symbol to the stop symbol (quiet zones excluded).\n */\nconst encodeCode128 = (text: string): number[] => {\n const widths: number[] = [];\n for (const codeword of toCode128Codewords(text, \"encodeCode128\")) {\n // Codewords come from the encoder and are always 0–106, so the lookup cannot miss.\n widths.push(...(CODE128_PATTERNS[codeword] as readonly number[]));\n }\n return widths;\n};\n\nexport default encodeCode128;\n","import { escapeXml } from \"../helpers/xml.helper\";\nimport encodeCode128 from \"./encode-code128\";\n\ntype LengthUnit = \"px\" | \"mm\";\n\ntype Code128SvgOptions = {\n /** Width of the narrowest bar, in `unit` (default 2 px or 0.33 mm). */\n moduleWidth?: number;\n /** Unit of every length option and of the SVG size (default \"px\"). */\n unit?: LengthUnit;\n /** Bar height excluding the text line, in `unit` (default 80 px or 15 mm). */\n height?: number;\n /** Blank margin on the left and right, in modules (default 10, the Code 128 minimum). */\n quietZone?: number;\n /** Print the encoded text under the bars (default true). */\n showText?: boolean;\n /** Font size of the text line, in `unit` (default 14 px or 3 mm). */\n fontSize?: number;\n /** Colour of the bars and the text (default \"#000000\"). */\n foreground?: string;\n /** Colour of the background, quiet zones included (default \"#ffffff\"). */\n background?: string;\n /** Accessible name, emitted as `<title>` and `aria-label` (default: `aria-label` is the text). */\n title?: string;\n};\n\nconst UNIT_DEFAULTS = {\n px: { moduleWidth: 2, height: 80, fontSize: 14 },\n mm: { moduleWidth: 0.33, height: 15, fontSize: 3 },\n} as const;\nconst DEFAULT_QUIET_ZONE = 10;\nconst DEFAULT_FOREGROUND = \"#000000\";\nconst DEFAULT_BACKGROUND = \"#ffffff\";\n// Text line below the bars, in ems: the baseline sits 1.1 em under the bars, the line ends 1.4 em down.\nconst TEXT_BASELINE_EM = 1.1;\nconst TEXT_LINE_EM = 1.4;\n// Enough for 0.001 mm / 0.001 px precision while cutting floating-point noise (0.9900000000000001).\nconst DECIMALS = 4;\n\nconst formatNumber = (value: number): string =>\n String(Number(value.toFixed(DECIMALS)));\n\nconst positiveOr = (value: number | undefined, fallback: number): number =>\n typeof value === \"number\" && Number.isFinite(value) && value > 0\n ? value\n : fallback;\n\n/** Renders `text` as a Code 128 barcode SVG string; needs no DOM. */\nconst code128Svg = (\n text: string,\n {\n unit,\n moduleWidth,\n height,\n quietZone,\n showText = true,\n fontSize,\n foreground = DEFAULT_FOREGROUND,\n background = DEFAULT_BACKGROUND,\n title,\n }: Code128SvgOptions = {}\n): string => {\n const widths = encodeCode128(text);\n const lengthUnit: LengthUnit = unit === \"mm\" ? \"mm\" : \"px\";\n const defaults = UNIT_DEFAULTS[lengthUnit];\n const moduleSize = positiveOr(moduleWidth, defaults.moduleWidth);\n const barHeight = positiveOr(height, defaults.height);\n const textSize = positiveOr(fontSize, defaults.fontSize);\n const margin =\n typeof quietZone === \"number\" &&\n Number.isFinite(quietZone) &&\n quietZone >= 0\n ? Math.floor(quietZone)\n : DEFAULT_QUIET_ZONE;\n const hasText = showText !== false;\n\n // Positions are counted in whole modules and scaled once, so no rounding error accumulates\n // from bar to bar.\n const bars: string[] = [];\n let cursor = margin;\n widths.forEach((width, index) => {\n if (index % 2 === 0) {\n const barWidth = formatNumber(width * moduleSize);\n bars.push(\n `M${formatNumber(cursor * moduleSize)} 0h${barWidth}v${formatNumber(barHeight)}h-${barWidth}z`\n );\n }\n cursor += width;\n });\n\n const totalWidth = formatNumber((cursor + margin) * moduleSize);\n const totalHeight = formatNumber(\n barHeight + (hasText ? textSize * TEXT_LINE_EM : 0)\n );\n const hasTitle = typeof title === \"string\" && title.length > 0;\n const label = escapeXml(hasTitle ? title : text);\n const fill = escapeXml(foreground);\n\n const parts = [\n `<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"${totalWidth}${lengthUnit}\" height=\"${totalHeight}${lengthUnit}\" viewBox=\"0 0 ${totalWidth} ${totalHeight}\" role=\"img\" aria-label=\"${label}\">`,\n hasTitle ? `<title>${label}</title>` : \"\",\n `<rect width=\"${totalWidth}\" height=\"${totalHeight}\" fill=\"${escapeXml(background)}\"/>`,\n `<path fill=\"${fill}\" shape-rendering=\"crispEdges\" d=\"${bars.join(\"\")}\"/>`,\n hasText\n ? `<text x=\"${formatNumber(((cursor + margin) * moduleSize) / 2)}\" y=\"${formatNumber(barHeight + textSize * TEXT_BASELINE_EM)}\" fill=\"${fill}\" font-family=\"monospace\" font-size=\"${formatNumber(textSize)}\" text-anchor=\"middle\">${escapeXml(text)}</text>`\n : \"\",\n \"</svg>\",\n ];\n return parts.join(\"\");\n};\n\nexport type { Code128SvgOptions, LengthUnit };\nexport default code128Svg;\n","type LabelSize = \"100x100\" | \"100x150\" | \"a4\";\n\ntype LabelDimensions = {\n /** Page width in millimetres. */\n readonly width: number;\n /** Page height in millimetres. */\n readonly height: number;\n};\n\n/** Page sizes of common shipping labels, in millimetres: 100×100 and 100×150 thermal, A4. */\nconst LABEL_SIZES: Readonly<Record<LabelSize, LabelDimensions>> = Object.freeze(\n {\n \"100x100\": Object.freeze({ width: 100, height: 100 }),\n \"100x150\": Object.freeze({ width: 100, height: 150 }),\n a4: Object.freeze({ width: 210, height: 297 }),\n }\n);\n\nexport type { LabelDimensions, LabelSize };\nexport default LABEL_SIZES;\n","import LABEL_SIZES from \"./label-sizes\";\nimport type { LabelSize } from \"./label-sizes\";\n\n/** Returns the `@page` rule that prints on a `size` label edge to edge (no page margin). */\nconst labelPrintCss = (size: LabelSize): string => {\n // Own keys only: \"toString\" or \"__proto__\" must not resolve through the prototype.\n if (typeof size !== \"string\" || !Object.hasOwn(LABEL_SIZES, size)) {\n const expected = Object.keys(LABEL_SIZES)\n .map((key) => `\"${key}\"`)\n .join(\", \");\n throw new TypeError(\n `labelPrintCss: unknown label size ${String(size)}; expected one of ${expected}`\n );\n }\n const { width, height } = LABEL_SIZES[size];\n return `@page { size: ${width}mm ${height}mm; margin: 0; }`;\n};\n\nexport default labelPrintCss;\n"],"names":["XML_ENTITIES","Map","isXmlChar","codePoint","escapeXml","value","escaped","character","codePointAt","get","SETS_BY_PREFERENCE","START_CODEWORDS","SWITCH_CODEWORDS","isDigit","code","otherTextSet","set","spanOf","text","index","charCodeAt","valueOf","toCode128Codewords","caller","codewords","length","cost","Float64Array","action","Uint8Array","costAt","direct","best","Infinity","choice","span","shifted","target","candidate","step","push","planDataCodewords","TypeError","RangeError","String","fromCodePoint","assertEncodable","checksum","position","CODE128_PATTERNS","map","pattern","Array","from","Number","encodeCode128","widths","codeword","UNIT_DEFAULTS","px","moduleWidth","height","fontSize","mm","formatNumber","toFixed","positiveOr","fallback","isFinite","LABEL_SIZES","Object","freeze","width","a4","unit","quietZone","showText","foreground","background","title","lengthUnit","defaults","moduleSize","barHeight","textSize","margin","Math","floor","hasText","bars","cursor","forEach","barWidth","totalWidth","totalHeight","hasTitle","label","fill","join","size","hasOwn","expected","keys","key"],"mappings":"sPAAA,MAAMA,EAA4C,IAAIC,IAAI,CACxD,CAAC,IAAK,SACN,CAAC,IAAK,QACN,CAAC,IAAK,QACN,CAAC,IAAK,UACN,CAAC,IAAK,YAKFC,EAAaC,GACH,IAAdA,GACc,KAAdA,GACc,KAAdA,GACCA,GAAa,IAAQA,GAAa,OAClCA,GAAa,OAAUA,GAAa,OACrCA,GAAa,MAGTC,EAAaC,IACjB,IAAIC,EAAU,GACd,IAAK,MAAMC,KAAaF,EACjBH,EAAUK,EAAUC,YAAY,MACrCF,GAAWN,EAAaS,IAAIF,IAAcA,GAE5C,OAAOD,GClBHI,EAAyC,CAHjC,EADA,EADA,GASRC,EAAkB,CAAC,IAAK,IAAK,KAC7BC,EAAmB,CAAC,IAAK,IAAK,IAY9BC,EAAWC,GAA0BA,GAAQ,IAAMA,GAAQ,GAE3DC,EAAgBC,GAxBR,IAwBmCA,EAvBnC,EADA,EA2BRC,EAAS,CAACC,EAAcC,EAAeH,KAC3C,MAAMF,EAAOI,EAAKE,WAAWD,GAE7B,OA5BY,IA4BRH,EACKH,EAAQC,IAASD,EAAQK,EAAKE,WAAWD,EAAQ,IAAM,EAAI,EA/BxD,IAiCRH,EAAsBF,EAAO,GAAK,EAAI,EACnCA,GAAQ,GAAK,EAAI,GAIpBO,EAAU,CAACH,EAAcC,EAAeH,KAC5C,MAAMF,EAAOI,EAAKE,WAAWD,GAC7B,OAtCY,IAsCRH,EAAoC,IAAbF,EAAO,IAAWI,EAAKE,WAAWD,EAAQ,GAAK,GAxC9D,IA0CRH,GAAiBF,EAAO,GAAWA,EAAO,GACvCA,EAAO,IA8FVQ,EAAqB,CAACJ,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