@sythos/js_barcode_universal 1.5.8 → 1.5.9
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/LICENSE +5 -4
- package/NOTICE.md +2 -2
- package/README.md +17 -13
- package/bundle/sythos-barcode.esm.js +11658 -11244
- package/bundle/sythos-barcode.js +11658 -11244
- package/package.json +10 -3
- package/src/index.d.ts +1 -1
- package/src/index.js +508 -516
- package/src/js/aztec/decoder.js +208 -199
- package/src/js/aztec/detector.js +153 -126
- package/src/js/aztec/encoder.js +172 -153
- package/src/js/aztec/high-level.js +106 -101
- package/src/js/aztec/index.js +1 -12
- package/src/js/aztec/tables.js +106 -120
- package/src/js/aztecrune/decoder.js +76 -85
- package/src/js/aztecrune/detector.js +107 -93
- package/src/js/aztecrune/encoder.js +50 -68
- package/src/js/aztecrune/index.js +1 -16
- package/src/js/aztecrune/tables.js +59 -54
- package/src/js/compactpdf417/decoder.js +69 -74
- package/src/js/compactpdf417/detector.js +74 -75
- package/src/js/compactpdf417/encoder.js +68 -81
- package/src/js/compactpdf417/index.js +1 -13
- package/src/js/compactpdf417/tables.js +71 -80
- package/src/js/core/bit-buffer.js +106 -122
- package/src/js/core/bit-matrix.js +196 -194
- package/src/js/core/errors.js +13 -15
- package/src/js/core/galois-field.js +119 -133
- package/src/js/core/index.js +2 -19
- package/src/js/core/reed-solomon.js +195 -204
- package/src/js/databar/codec.js +93 -108
- package/src/js/databar/decoder.js +149 -140
- package/src/js/databar/encoder.js +47 -55
- package/src/js/databar/gs1.js +122 -123
- package/src/js/databar/index.js +4 -36
- package/src/js/databar/patterns.js +81 -85
- package/src/js/databar/tables.js +51 -52
- package/src/js/datamatrix/decoder.js +216 -182
- package/src/js/datamatrix/detector.js +161 -132
- package/src/js/datamatrix/encoder.js +192 -133
- package/src/js/datamatrix/index.js +1 -9
- package/src/js/datamatrix/tables.js +68 -69
- package/src/js/frameqr/decoder.js +102 -125
- package/src/js/frameqr/detector.js +101 -94
- package/src/js/frameqr/encoder.js +67 -80
- package/src/js/frameqr/index.js +1 -10
- package/src/js/frameqr/tables.js +145 -163
- package/src/js/image/binarizer.js +146 -162
- package/src/js/image/grid-sampler.js +63 -79
- package/src/js/image/index.js +0 -2
- package/src/js/image/luminance.js +129 -140
- package/src/js/image/perspective.js +112 -149
- package/src/js/micropdf417/compaction.js +64 -65
- package/src/js/micropdf417/decoder.js +106 -109
- package/src/js/micropdf417/detector.js +73 -70
- package/src/js/micropdf417/encoder.js +129 -154
- package/src/js/micropdf417/error-correction.js +6 -11
- package/src/js/micropdf417/index.js +2 -16
- package/src/js/micropdf417/tables.js +112 -99
- package/src/js/microqr/decoder.js +177 -182
- package/src/js/microqr/detector.js +240 -256
- package/src/js/microqr/encoder.js +239 -194
- package/src/js/microqr/index.js +0 -2
- package/src/js/microqr/tables.js +208 -200
- package/src/js/oned/addons.js +167 -191
- package/src/js/oned/index.js +27 -58
- package/src/js/oned/patterns.js +194 -213
- package/src/js/oned/reader.js +1003 -984
- package/src/js/oned/writers.js +411 -466
- package/src/js/pdf417/compaction.js +315 -222
- package/src/js/pdf417/decoder.js +71 -36
- package/src/js/pdf417/detector.js +377 -361
- package/src/js/pdf417/encoder.js +57 -46
- package/src/js/pdf417/error-correction.js +5 -8
- package/src/js/pdf417/tables.js +248 -250
- package/src/js/qr/decoder.js +311 -347
- package/src/js/qr/detector.js +378 -395
- package/src/js/qr/encoder.js +514 -599
- package/src/js/qr/index.js +0 -2
- package/src/js/qr/tables.js +375 -422
- package/src/js/render/image-data.js +57 -62
- package/src/js/render/index.js +21 -23
- package/src/js/render/options.js +82 -90
- package/src/js/render/png.js +144 -169
- package/src/js/render/svg.js +41 -44
- package/src/js/render/webgl.js +103 -109
- package/src/js/render/webgpu.js +205 -217
- package/src/js/rmqr/decoder.js +161 -53
- package/src/js/rmqr/detector.js +73 -41
- package/src/js/rmqr/encoder.js +253 -104
- package/src/js/rmqr/index.js +1 -5
- package/src/js/rmqr/tables.js +108 -85
- package/src/ts/aztec/decoder.ts +317 -0
- package/src/ts/aztec/detector.ts +224 -0
- package/src/ts/aztec/encoder.ts +257 -0
- package/src/ts/aztec/high-level.ts +211 -0
- package/src/ts/aztec/index.ts +45 -0
- package/src/ts/aztec/tables.ts +210 -0
- package/src/ts/aztecrune/decoder.ts +155 -0
- package/src/ts/aztecrune/detector.ts +166 -0
- package/src/ts/aztecrune/encoder.ts +121 -0
- package/src/ts/aztecrune/index.ts +49 -0
- package/src/ts/aztecrune/tables.ts +137 -0
- package/src/ts/compactpdf417/decoder.ts +128 -0
- package/src/ts/compactpdf417/detector.ts +139 -0
- package/src/ts/compactpdf417/encoder.ts +140 -0
- package/src/ts/compactpdf417/index.ts +17 -0
- package/src/ts/compactpdf417/tables.ts +173 -0
- package/src/ts/core/bit-buffer.ts +174 -0
- package/src/ts/core/bit-matrix.ts +241 -0
- package/src/ts/core/errors.ts +61 -0
- package/src/ts/core/galois-field.ts +207 -0
- package/src/ts/core/index.ts +56 -0
- package/src/ts/core/reed-solomon.ts +327 -0
- package/src/ts/databar/codec.ts +181 -0
- package/src/ts/databar/decoder.ts +215 -0
- package/src/ts/databar/encoder.ts +96 -0
- package/src/ts/databar/gs1.ts +177 -0
- package/src/ts/databar/index.ts +68 -0
- package/src/ts/databar/patterns.ts +143 -0
- package/src/ts/databar/tables.ts +124 -0
- package/src/ts/datamatrix/decoder.ts +262 -0
- package/src/ts/datamatrix/detector.ts +225 -0
- package/src/ts/datamatrix/encoder.ts +191 -0
- package/src/ts/datamatrix/index.ts +42 -0
- package/src/ts/datamatrix/tables.ts +123 -0
- package/src/ts/frameqr/decoder.ts +239 -0
- package/src/ts/frameqr/detector.ts +192 -0
- package/src/ts/frameqr/encoder.ts +156 -0
- package/src/ts/frameqr/index.ts +42 -0
- package/src/ts/frameqr/tables.ts +270 -0
- package/src/ts/image/binarizer.ts +270 -0
- package/src/ts/image/grid-sampler.ts +164 -0
- package/src/ts/image/index.ts +40 -0
- package/src/ts/image/luminance.ts +196 -0
- package/src/ts/image/perspective.ts +195 -0
- package/src/ts/index.d.ts +1 -1
- package/src/ts/index.ts +790 -0
- package/src/ts/micropdf417/compaction.ts +116 -0
- package/src/ts/micropdf417/decoder.ts +183 -0
- package/src/ts/micropdf417/detector.ts +149 -0
- package/src/ts/micropdf417/encoder.ts +209 -0
- package/src/ts/micropdf417/error-correction.ts +55 -0
- package/src/ts/micropdf417/index.ts +49 -0
- package/src/ts/micropdf417/tables.ts +184 -0
- package/src/ts/microqr/decoder.ts +245 -0
- package/src/ts/microqr/detector.ts +355 -0
- package/src/ts/microqr/encoder.ts +269 -0
- package/src/ts/microqr/index.ts +36 -0
- package/src/ts/microqr/tables.ts +316 -0
- package/src/ts/oned/addons.ts +420 -0
- package/src/ts/oned/index.ts +106 -0
- package/src/ts/oned/patterns.ts +384 -0
- package/src/ts/oned/reader.ts +1364 -0
- package/src/ts/oned/writers.ts +746 -0
- package/src/ts/pdf417/compaction.ts +298 -0
- package/src/ts/pdf417/decoder.ts +75 -0
- package/src/ts/pdf417/detector.ts +468 -0
- package/src/ts/pdf417/encoder.ts +91 -0
- package/src/ts/pdf417/error-correction.ts +47 -0
- package/src/ts/pdf417/index.ts +6 -0
- package/src/ts/pdf417/tables.ts +317 -0
- package/src/ts/qr/decoder.ts +575 -0
- package/src/ts/qr/detector.ts +630 -0
- package/src/ts/qr/encoder.ts +958 -0
- package/src/ts/qr/index.ts +44 -0
- package/src/ts/qr/tables.ts +737 -0
- package/src/ts/render/image-data.ts +125 -0
- package/src/ts/render/index.ts +130 -0
- package/src/ts/render/options.ts +160 -0
- package/src/ts/render/png.ts +295 -0
- package/src/ts/render/svg.ts +120 -0
- package/src/ts/render/webgl.ts +206 -0
- package/src/ts/render/webgpu.ts +369 -0
- package/src/ts/rmqr/decoder.ts +101 -0
- package/src/ts/rmqr/detector.ts +90 -0
- package/src/ts/rmqr/encoder.ts +172 -0
- package/src/ts/rmqr/index.ts +37 -0
- package/src/ts/rmqr/tables.ts +154 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Sythos Barcode Suite
|
|
3
|
+
*
|
|
4
|
+
* MIT License
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) 2026 Sythos
|
|
7
|
+
*
|
|
8
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
* in the Software without restriction, including without limitation the rights
|
|
11
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
* furnished to do so, subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
* copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
* SOFTWARE.
|
|
25
|
+
*
|
|
26
|
+
* SPDX-License-Identifier: MIT
|
|
27
|
+
*
|
|
28
|
+
* Original work. No code from any other barcode implementation.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/** Clean-matrix decoder for GS1 DataBar Omnidirectional and Truncated. @module databar/decoder */
|
|
32
|
+
|
|
33
|
+
import { ChecksumError, FormatError } from '../core/errors.js';
|
|
34
|
+
import { BitMatrix } from '../core/bit-matrix.js';
|
|
35
|
+
import { decodeDataBar14GTIN } from './codec.js';
|
|
36
|
+
import {
|
|
37
|
+
DATABAR14_CHECKSUM_WEIGHTS,
|
|
38
|
+
DATABAR14_FINDERS,
|
|
39
|
+
dataBar14ValueForWidths,
|
|
40
|
+
} from './patterns.js';
|
|
41
|
+
|
|
42
|
+
function sampledRuns(matrix) {
|
|
43
|
+
if (!matrix || !Number.isInteger(matrix.width) || !Number.isInteger(matrix.height)) {
|
|
44
|
+
throw new TypeError('GS1 DataBar decoder expects a BitMatrix-like value');
|
|
45
|
+
}
|
|
46
|
+
if (matrix.width % 96 !== 0) throw new FormatError('GS1 DataBar-14 matrix width must be an integer multiple of 96 modules');
|
|
47
|
+
const scale = matrix.width / 96;
|
|
48
|
+
const y = Math.floor(matrix.height / 2);
|
|
49
|
+
const bits = Array.from({ length: 96 }, (_, x) => matrix.get(x * scale + Math.floor(scale / 2), y));
|
|
50
|
+
if (bits[0] || !bits[95]) throw new FormatError('GS1 DataBar-14 guard pattern is invalid');
|
|
51
|
+
const runs = [];
|
|
52
|
+
let current = bits[0];
|
|
53
|
+
let length = 0;
|
|
54
|
+
for (const bit of bits) {
|
|
55
|
+
if (bit === current) length++;
|
|
56
|
+
else {
|
|
57
|
+
runs.push(length);
|
|
58
|
+
current = bit;
|
|
59
|
+
length = 1;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
runs.push(length);
|
|
63
|
+
if (runs.length !== 46 || runs[0] !== 1 || runs[1] !== 1 || runs[44] !== 1 || runs[45] !== 1) {
|
|
64
|
+
throw new FormatError('GS1 DataBar-14 element count or guard widths are invalid');
|
|
65
|
+
}
|
|
66
|
+
return runs;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function finderIndex(widths) {
|
|
70
|
+
const key = widths.join(',');
|
|
71
|
+
return DATABAR14_FINDERS.findIndex((candidate) => candidate.join(',') === key);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function expectedChecksum(widths) {
|
|
75
|
+
const offsets = [0, 8, 24, 16];
|
|
76
|
+
let checksum = 0;
|
|
77
|
+
for (let character = 0; character < 4; character++) {
|
|
78
|
+
for (let element = 0; element < 8; element++) {
|
|
79
|
+
checksum += widths[character][element] * DATABAR14_CHECKSUM_WEIGHTS[offsets[character] + element];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
checksum %= 79;
|
|
83
|
+
if (checksum >= 8) checksum++;
|
|
84
|
+
if (checksum >= 72) checksum++;
|
|
85
|
+
return checksum;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Decode a clean or integer-scaled 96-module DataBar-14 matrix. */
|
|
89
|
+
export function decodeDataBar14(matrix) {
|
|
90
|
+
const runs = sampledRuns(matrix);
|
|
91
|
+
const widths = [
|
|
92
|
+
runs.slice(2, 10),
|
|
93
|
+
runs.slice(15, 23).slice().reverse(),
|
|
94
|
+
runs.slice(23, 31),
|
|
95
|
+
runs.slice(36, 44).slice().reverse(),
|
|
96
|
+
];
|
|
97
|
+
const leftFinder = finderIndex(runs.slice(10, 15));
|
|
98
|
+
const rightFinder = finderIndex(runs.slice(31, 36).slice().reverse());
|
|
99
|
+
if (leftFinder < 0 || rightFinder < 0) throw new FormatError('GS1 DataBar-14 finder pattern is invalid');
|
|
100
|
+
const encodedChecksum = leftFinder * 9 + rightFinder;
|
|
101
|
+
if (encodedChecksum !== expectedChecksum(widths)) throw new ChecksumError('GS1 DataBar-14 checksum mismatch');
|
|
102
|
+
|
|
103
|
+
const outerLeft = dataBar14ValueForWidths(widths[0], 'outside');
|
|
104
|
+
const innerLeft = dataBar14ValueForWidths(widths[1], 'inside');
|
|
105
|
+
const innerRight = dataBar14ValueForWidths(widths[2], 'inside');
|
|
106
|
+
const outerRight = dataBar14ValueForWidths(widths[3], 'outside');
|
|
107
|
+
const decoded = decodeDataBar14GTIN({ outerLeft, innerLeft, outerRight, innerRight });
|
|
108
|
+
return Object.freeze({
|
|
109
|
+
format: 'databar-omnidirectional',
|
|
110
|
+
text: decoded.gtin,
|
|
111
|
+
gtin: decoded.gtin,
|
|
112
|
+
linkage: decoded.linkage,
|
|
113
|
+
symbologyIdentifier: ']e0',
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Extract alternating runs from a binarized scanline.
|
|
119
|
+
* @param {Uint8Array} row
|
|
120
|
+
* @returns {{widths:number[], dark:boolean}[]}
|
|
121
|
+
*/
|
|
122
|
+
function scanlineRuns(row) {
|
|
123
|
+
if (!row || row.length < 96) return [];
|
|
124
|
+
const runs = [];
|
|
125
|
+
let dark = row[0] === 1;
|
|
126
|
+
let start = 0;
|
|
127
|
+
for (let x = 1; x < row.length; x++) {
|
|
128
|
+
const nextDark = row[x] === 1;
|
|
129
|
+
if (nextDark === dark) continue;
|
|
130
|
+
runs.push({ width: x - start, dark });
|
|
131
|
+
start = x;
|
|
132
|
+
dark = nextDark;
|
|
133
|
+
}
|
|
134
|
+
runs.push({ width: row.length - start, dark });
|
|
135
|
+
return runs;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Normalize a candidate's pixel runs to the 96 logical DataBar modules.
|
|
140
|
+
* Rounding is followed by a small conservation pass so mild non-integer
|
|
141
|
+
* scaling does not change the total symbol width.
|
|
142
|
+
* @param {number[]} raw
|
|
143
|
+
* @returns {number[]|null}
|
|
144
|
+
*/
|
|
145
|
+
function normalizeScanlineWidths(raw) {
|
|
146
|
+
const total = raw.reduce((sum, width) => sum + width, 0);
|
|
147
|
+
if (total <= 0) return null;
|
|
148
|
+
const scale = total / 96;
|
|
149
|
+
if (scale < 0.5) return null;
|
|
150
|
+
const widths = raw.map((width) => Math.max(1, Math.round(width / scale)));
|
|
151
|
+
let delta = 96 - widths.reduce((sum, width) => sum + width, 0);
|
|
152
|
+
while (delta !== 0) {
|
|
153
|
+
if (delta > 0) {
|
|
154
|
+
let index = 0;
|
|
155
|
+
for (let i = 1; i < widths.length; i++) if (raw[i] > raw[index]) index = i;
|
|
156
|
+
widths[index]++;
|
|
157
|
+
delta--;
|
|
158
|
+
} else {
|
|
159
|
+
let index = -1;
|
|
160
|
+
for (let i = 0; i < widths.length; i++) {
|
|
161
|
+
if (widths[i] <= 1) continue;
|
|
162
|
+
if (index < 0 || raw[i] / widths[i] > raw[index] / widths[index]) index = i;
|
|
163
|
+
}
|
|
164
|
+
if (index < 0) return null;
|
|
165
|
+
widths[index]--;
|
|
166
|
+
delta++;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (widths[0] !== 1 || widths[widths.length - 1] !== 1) return null;
|
|
170
|
+
return widths;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Build a 96-module row from normalized alternating runs. */
|
|
174
|
+
function matrixFromRuns(widths, darkFirst) {
|
|
175
|
+
const matrix = new BitMatrix(96, 1);
|
|
176
|
+
let x = 0;
|
|
177
|
+
let dark = darkFirst;
|
|
178
|
+
for (const width of widths) {
|
|
179
|
+
if (dark) for (let i = 0; i < width; i++) matrix.set(x + i, 0);
|
|
180
|
+
x += width;
|
|
181
|
+
dark = !dark;
|
|
182
|
+
}
|
|
183
|
+
return matrix;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Decode GS1 DataBar-14 from one raster scanline. This is the image layer over
|
|
188
|
+
* the existing clean 96-module decoder; it recognizes both Omnidirectional
|
|
189
|
+
* and Truncated symbols because their horizontal pattern is identical.
|
|
190
|
+
*
|
|
191
|
+
* @param {Uint8Array} row
|
|
192
|
+
* @returns {{format:'gs1databar14', text:string, gtin:string, gs1:boolean, linkage:boolean, symbologyIdentifier:string, elements:Array}|null}
|
|
193
|
+
*/
|
|
194
|
+
export function decodeDataBar14Scanline(row) {
|
|
195
|
+
const runs = scanlineRuns(row);
|
|
196
|
+
for (let start = 0; start + 46 <= runs.length; start++) {
|
|
197
|
+
if (runs[start].dark) continue;
|
|
198
|
+
const candidate = runs.slice(start, start + 46);
|
|
199
|
+
if (start + 46 < runs.length && runs[start + 46].dark) continue;
|
|
200
|
+
const widths = normalizeScanlineWidths(candidate.map((run) => run.width));
|
|
201
|
+
if (!widths) continue;
|
|
202
|
+
try {
|
|
203
|
+
const decoded = decodeDataBar14(matrixFromRuns(widths, false));
|
|
204
|
+
return {
|
|
205
|
+
...decoded,
|
|
206
|
+
format: 'gs1databar14',
|
|
207
|
+
gs1: true,
|
|
208
|
+
elements: [{ ai: '01', value: decoded.gtin, fixed: true }],
|
|
209
|
+
};
|
|
210
|
+
} catch {
|
|
211
|
+
// Try the next light-run candidate in the scanline.
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Sythos Barcode Suite
|
|
3
|
+
*
|
|
4
|
+
* MIT License
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) 2026 Sythos
|
|
7
|
+
*
|
|
8
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
* in the Software without restriction, including without limitation the rights
|
|
11
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
* furnished to do so, subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
* copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
* SOFTWARE.
|
|
25
|
+
*
|
|
26
|
+
* SPDX-License-Identifier: MIT
|
|
27
|
+
*
|
|
28
|
+
* Original work. No code from any other barcode implementation.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/** GS1 DataBar Omnidirectional and Truncated encoder. @module databar/encoder */
|
|
32
|
+
|
|
33
|
+
import { BitMatrix } from '../core/bit-matrix.js';
|
|
34
|
+
import { EncodeError } from '../core/errors.js';
|
|
35
|
+
import { encodeDataBar14GTIN } from './codec.js';
|
|
36
|
+
import {
|
|
37
|
+
DATABAR14_CHECKSUM_WEIGHTS,
|
|
38
|
+
DATABAR14_FINDERS,
|
|
39
|
+
dataBar14CharacterWidths,
|
|
40
|
+
} from './patterns.js';
|
|
41
|
+
|
|
42
|
+
function finderIndexes(characters) {
|
|
43
|
+
const widths = characters.map((value, index) =>
|
|
44
|
+
dataBar14CharacterWidths(value, index === 0 || index === 3 ? 'outside' : 'inside')
|
|
45
|
+
);
|
|
46
|
+
let checksum = 0;
|
|
47
|
+
const weightOffsets = [0, 8, 24, 16];
|
|
48
|
+
for (let character = 0; character < 4; character++) {
|
|
49
|
+
for (let element = 0; element < 8; element++) {
|
|
50
|
+
checksum += widths[character][element] * DATABAR14_CHECKSUM_WEIGHTS[weightOffsets[character] + element];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
checksum %= 79;
|
|
54
|
+
if (checksum >= 8) checksum++;
|
|
55
|
+
if (checksum >= 72) checksum++;
|
|
56
|
+
return { widths, checksum, left: Math.floor(checksum / 9), right: checksum % 9 };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function widthsToMatrix(widths, height) {
|
|
60
|
+
const width = widths.reduce((sum, value) => sum + value, 0);
|
|
61
|
+
const matrix = new BitMatrix(width, height);
|
|
62
|
+
let dark = false;
|
|
63
|
+
let x = 0;
|
|
64
|
+
for (const run of widths) {
|
|
65
|
+
if (dark) matrix.setRegion(x, 0, run, height);
|
|
66
|
+
x += run;
|
|
67
|
+
dark = !dark;
|
|
68
|
+
}
|
|
69
|
+
return matrix;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Encode a checked GTIN as DataBar Omnidirectional or Truncated. */
|
|
73
|
+
export function encodeDataBar14(value, options = {}) {
|
|
74
|
+
const variant = options.variant ?? 'omnidirectional';
|
|
75
|
+
if (variant !== 'omnidirectional' && variant !== 'truncated') {
|
|
76
|
+
throw new EncodeError('GS1 DataBar-14 physical encoder currently supports omnidirectional and truncated');
|
|
77
|
+
}
|
|
78
|
+
const height = options.height ?? (variant === 'omnidirectional' ? 33 : 13);
|
|
79
|
+
if (!Number.isInteger(height) || height < (variant === 'omnidirectional' ? 33 : 13)) {
|
|
80
|
+
throw new EncodeError(`GS1 DataBar ${variant} height is below its normative minimum`);
|
|
81
|
+
}
|
|
82
|
+
const compacted = encodeDataBar14GTIN(value, { linkage: options.linkage });
|
|
83
|
+
const characters = compacted.physicalCharacters;
|
|
84
|
+
const check = finderIndexes(characters);
|
|
85
|
+
const widths = [1, 1];
|
|
86
|
+
widths.push(...check.widths[0]);
|
|
87
|
+
widths.push(...DATABAR14_FINDERS[check.left]);
|
|
88
|
+
widths.push(...check.widths[1].slice().reverse());
|
|
89
|
+
widths.push(...check.widths[2]);
|
|
90
|
+
widths.push(...DATABAR14_FINDERS[check.right].slice().reverse());
|
|
91
|
+
widths.push(...check.widths[3].slice().reverse());
|
|
92
|
+
widths.push(1, 1);
|
|
93
|
+
const matrix = widthsToMatrix(widths, height);
|
|
94
|
+
matrix.databar = Object.freeze({ variant, gtin: compacted.gtin, linkage: compacted.linkage, checksum: check.checksum });
|
|
95
|
+
return matrix;
|
|
96
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Sythos Barcode Suite
|
|
3
|
+
*
|
|
4
|
+
* MIT License
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) 2026 Sythos
|
|
7
|
+
*
|
|
8
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
* in the Software without restriction, including without limitation the rights
|
|
11
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
* furnished to do so, subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
* copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
* SOFTWARE.
|
|
25
|
+
*
|
|
26
|
+
* SPDX-License-Identifier: MIT
|
|
27
|
+
*
|
|
28
|
+
* Original work. No code from any other barcode implementation.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/** GS1 element-string codec used by GS1 DataBar Expanded. @module databar/gs1 */
|
|
32
|
+
|
|
33
|
+
import { EncodeError, FormatError } from '../core/errors.js';
|
|
34
|
+
|
|
35
|
+
export const GS1_SEPARATOR = '\x1d';
|
|
36
|
+
|
|
37
|
+
const EXACT = new Map([
|
|
38
|
+
['00', [18, true]], ['01', [14, true]], ['02', [14, true]],
|
|
39
|
+
['10', [20, false]], ['11', [6, true]], ['12', [6, true]],
|
|
40
|
+
['13', [6, true]], ['15', [6, true]], ['16', [6, true]],
|
|
41
|
+
['17', [6, true]], ['20', [2, true]], ['21', [20, false]],
|
|
42
|
+
['22', [29, false]], ['30', [8, false]], ['37', [8, false]],
|
|
43
|
+
['240', [30, false]], ['241', [30, false]], ['242', [6, false]],
|
|
44
|
+
['243', [20, false]], ['250', [30, false]], ['251', [30, false]],
|
|
45
|
+
['253', [30, false]], ['254', [20, false]], ['255', [13, true]],
|
|
46
|
+
['400', [30, false]], ['401', [30, false]], ['402', [17, true]],
|
|
47
|
+
['403', [30, false]], ['410', [13, true]], ['411', [13, true]],
|
|
48
|
+
['412', [13, true]], ['413', [13, true]], ['414', [13, true]],
|
|
49
|
+
['415', [13, true]], ['416', [13, true]], ['417', [13, true]],
|
|
50
|
+
['420', [20, false]], ['421', [15, false]], ['422', [3, true]],
|
|
51
|
+
['423', [15, false]], ['424', [3, true]], ['425', [3, true]],
|
|
52
|
+
['426', [3, true]], ['427', [3, false]],
|
|
53
|
+
['7001', [13, true]], ['7002', [30, false]], ['7003', [10, true]],
|
|
54
|
+
['7004', [4, false]], ['7005', [12, false]], ['7006', [6, true]],
|
|
55
|
+
['7007', [12, false]], ['7008', [3, false]], ['7009', [10, false]],
|
|
56
|
+
['7010', [2, false]], ['7020', [20, false]], ['7021', [20, false]],
|
|
57
|
+
['7022', [20, false]], ['710', [20, false]], ['711', [20, false]],
|
|
58
|
+
['712', [20, false]], ['713', [20, false]], ['714', [20, false]],
|
|
59
|
+
['715', [20, false]],
|
|
60
|
+
['8001', [14, true]], ['8002', [20, false]], ['8003', [30, false]],
|
|
61
|
+
['8004', [30, false]], ['8005', [6, true]], ['8006', [18, true]],
|
|
62
|
+
['8007', [34, false]], ['8008', [12, false]], ['8009', [50, false]],
|
|
63
|
+
['8010', [30, false]], ['8011', [12, false]], ['8012', [20, false]],
|
|
64
|
+
['8013', [30, false]], ['8017', [18, true]], ['8018', [18, true]],
|
|
65
|
+
['8019', [10, false]], ['8020', [25, false]], ['8026', [18, true]],
|
|
66
|
+
['8110', [70, false]], ['8111', [4, true]], ['8112', [70, false]],
|
|
67
|
+
['8200', [70, false]],
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
function rangedAI(ai) {
|
|
71
|
+
if (/^31[0-6]\d$/.test(ai) || /^32[0-7]\d$/.test(ai) ||
|
|
72
|
+
/^33[0-7]\d$/.test(ai) || /^34[0-9]\d$/.test(ai) ||
|
|
73
|
+
/^35[0-7]\d$/.test(ai) || /^36[0-9]\d$/.test(ai)) return [6, true];
|
|
74
|
+
if (/^390\d$/.test(ai) || /^392\d$/.test(ai)) return [15, false];
|
|
75
|
+
if (/^391\d$/.test(ai) || /^393\d$/.test(ai)) return [18, false];
|
|
76
|
+
if (/^394\d$/.test(ai)) return [4, true];
|
|
77
|
+
if (/^395\d$/.test(ai)) return [6, true];
|
|
78
|
+
if (/^703\d$/.test(ai) || /^723\d$/.test(ai)) return [30, false];
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function gs1AIInfo(ai) {
|
|
83
|
+
if (typeof ai !== 'string' || !/^\d{2,4}$/.test(ai)) return undefined;
|
|
84
|
+
const spec = EXACT.get(ai) || rangedAI(ai);
|
|
85
|
+
if (!spec) return undefined;
|
|
86
|
+
return { ai, length: spec[0], fixed: spec[1] };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function assertCharacters(value, label, ErrorType) {
|
|
90
|
+
if (/[^\x20-\x7e]/.test(value)) {
|
|
91
|
+
throw new ErrorType(`GS1: ${label} contains a character outside the GS1 element-string character set`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function validateElement(ai, value, ErrorType) {
|
|
96
|
+
const info = gs1AIInfo(ai);
|
|
97
|
+
if (!info) throw new ErrorType(`GS1: unsupported or invalid Application Identifier ${ai}`);
|
|
98
|
+
if (value.length === 0) throw new ErrorType(`GS1: AI ${ai} must not be empty`);
|
|
99
|
+
if (info.fixed ? value.length !== info.length : value.length > info.length) {
|
|
100
|
+
const expectation = info.fixed ? `exactly ${info.length}` : `at most ${info.length}`;
|
|
101
|
+
throw new ErrorType(`GS1: AI ${ai} requires ${expectation} characters`);
|
|
102
|
+
}
|
|
103
|
+
assertCharacters(value, `AI ${ai}`, ErrorType);
|
|
104
|
+
return { ai, value, fixed: info.fixed };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function parseGS1ElementString(input) {
|
|
108
|
+
if (typeof input !== 'string' || input.length === 0) {
|
|
109
|
+
throw new EncodeError('GS1: element string must be a non-empty string');
|
|
110
|
+
}
|
|
111
|
+
const elements = [];
|
|
112
|
+
let offset = 0;
|
|
113
|
+
while (offset < input.length) {
|
|
114
|
+
const match = /^\((\d{2,4})\)/.exec(input.slice(offset));
|
|
115
|
+
if (!match) throw new EncodeError(`GS1: expected an Application Identifier at offset ${offset}`);
|
|
116
|
+
const ai = match[1];
|
|
117
|
+
offset += match[0].length;
|
|
118
|
+
const next = input.indexOf('(', offset);
|
|
119
|
+
const end = next < 0 ? input.length : next;
|
|
120
|
+
elements.push(validateElement(ai, input.slice(offset, end), EncodeError));
|
|
121
|
+
offset = end;
|
|
122
|
+
}
|
|
123
|
+
return elements;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function encodeGS1ElementString(input) {
|
|
127
|
+
const elements = Array.isArray(input)
|
|
128
|
+
? input.map(({ ai, value }) => validateElement(String(ai), String(value), EncodeError))
|
|
129
|
+
: parseGS1ElementString(input);
|
|
130
|
+
return elements.map((element, index) =>
|
|
131
|
+
`${element.ai}${element.value}${!element.fixed && index + 1 < elements.length ? GS1_SEPARATOR : ''}`
|
|
132
|
+
).join('');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function matchAIAt(data, offset) {
|
|
136
|
+
for (const length of [4, 3, 2]) {
|
|
137
|
+
const ai = data.slice(offset, offset + length);
|
|
138
|
+
if (gs1AIInfo(ai)) return ai;
|
|
139
|
+
}
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function decodeGS1ElementString(data) {
|
|
144
|
+
if (typeof data !== 'string' || data.length === 0) {
|
|
145
|
+
throw new FormatError('GS1: encoded data must be a non-empty string');
|
|
146
|
+
}
|
|
147
|
+
const elements = [];
|
|
148
|
+
let offset = 0;
|
|
149
|
+
while (offset < data.length) {
|
|
150
|
+
if (data[offset] === GS1_SEPARATOR) {
|
|
151
|
+
throw new FormatError(`GS1: unexpected separator at offset ${offset}`);
|
|
152
|
+
}
|
|
153
|
+
const ai = matchAIAt(data, offset);
|
|
154
|
+
if (!ai) throw new FormatError(`GS1: unknown Application Identifier at offset ${offset}`);
|
|
155
|
+
offset += ai.length;
|
|
156
|
+
const info = gs1AIInfo(ai);
|
|
157
|
+
let value;
|
|
158
|
+
if (info.fixed) {
|
|
159
|
+
value = data.slice(offset, offset + info.length);
|
|
160
|
+
if (value.length !== info.length || value.includes(GS1_SEPARATOR)) {
|
|
161
|
+
throw new FormatError(`GS1: truncated fixed-length AI ${ai}`);
|
|
162
|
+
}
|
|
163
|
+
offset += info.length;
|
|
164
|
+
} else {
|
|
165
|
+
const separator = data.indexOf(GS1_SEPARATOR, offset);
|
|
166
|
+
const end = separator < 0 ? data.length : separator;
|
|
167
|
+
value = data.slice(offset, end);
|
|
168
|
+
offset = separator < 0 ? data.length : separator + 1;
|
|
169
|
+
}
|
|
170
|
+
elements.push(validateElement(ai, value, FormatError));
|
|
171
|
+
}
|
|
172
|
+
return elements;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function formatGS1Elements(elements) {
|
|
176
|
+
return elements.map(({ ai, value }) => `(${ai})${value}`).join('');
|
|
177
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Sythos Barcode Suite
|
|
3
|
+
*
|
|
4
|
+
* MIT License
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) 2026 Sythos
|
|
7
|
+
*
|
|
8
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
* in the Software without restriction, including without limitation the rights
|
|
11
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
* furnished to do so, subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
* copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
* SOFTWARE.
|
|
25
|
+
*
|
|
26
|
+
* SPDX-License-Identifier: MIT
|
|
27
|
+
*
|
|
28
|
+
* Original work. No code from any other barcode implementation.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/** Verified GS1 DataBar data-layer primitives. @module databar */
|
|
32
|
+
|
|
33
|
+
export {
|
|
34
|
+
dataBarGtinTransmission,
|
|
35
|
+
decodeDataBar14GTIN,
|
|
36
|
+
decodeDataBarLimitedGTIN,
|
|
37
|
+
encodeDataBar14GTIN,
|
|
38
|
+
encodeDataBarLimitedGTIN,
|
|
39
|
+
gtinCheckDigit,
|
|
40
|
+
makeGTIN14,
|
|
41
|
+
normalizeGTIN,
|
|
42
|
+
} from './codec.js';
|
|
43
|
+
|
|
44
|
+
export {
|
|
45
|
+
GS1_SEPARATOR,
|
|
46
|
+
decodeGS1ElementString,
|
|
47
|
+
encodeGS1ElementString,
|
|
48
|
+
formatGS1Elements,
|
|
49
|
+
gs1AIInfo,
|
|
50
|
+
parseGS1ElementString,
|
|
51
|
+
} from './gs1.js';
|
|
52
|
+
|
|
53
|
+
export {
|
|
54
|
+
DATABAR14_VARIANTS,
|
|
55
|
+
DATABAR_LIMITED_VARIANT,
|
|
56
|
+
dataBar14GroupFor,
|
|
57
|
+
validateDataBarTables,
|
|
58
|
+
} from './tables.js';
|
|
59
|
+
|
|
60
|
+
export { encodeDataBar14 } from './encoder.js';
|
|
61
|
+
export { decodeDataBar14, decodeDataBar14Scanline } from './decoder.js';
|
|
62
|
+
export {
|
|
63
|
+
DATABAR14_CHECKSUM_WEIGHTS,
|
|
64
|
+
DATABAR14_FINDERS,
|
|
65
|
+
dataBar14CharacterWidths,
|
|
66
|
+
dataBar14ValueForWidths,
|
|
67
|
+
dataBarWidths,
|
|
68
|
+
} from './patterns.js';
|