@sythos/js_barcode_universal 1.5.8 → 1.5.10
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 +61 -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
package/src/js/oned/addons.js
CHANGED
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
*
|
|
28
28
|
* Original work. No code from any other barcode implementation.
|
|
29
29
|
*/
|
|
30
|
-
|
|
31
30
|
/**
|
|
32
31
|
* UPC/EAN two- and five-digit supplements.
|
|
33
32
|
*
|
|
@@ -43,40 +42,25 @@
|
|
|
43
42
|
*
|
|
44
43
|
* @module oned/addons
|
|
45
44
|
*/
|
|
46
|
-
|
|
47
45
|
import { BitMatrix } from '../core/bit-matrix.js';
|
|
48
46
|
import { EncodeError, FormatError } from '../core/errors.js';
|
|
49
|
-
import {
|
|
50
|
-
|
|
51
|
-
EAN_G,
|
|
52
|
-
} from './patterns.js';
|
|
53
|
-
import {
|
|
54
|
-
encodeEAN13,
|
|
55
|
-
encodeEAN8,
|
|
56
|
-
encodeUPCA,
|
|
57
|
-
encodeUPCE,
|
|
58
|
-
} from './writers.js';
|
|
59
|
-
|
|
47
|
+
import { EAN_L, EAN_G, } from './patterns.js';
|
|
48
|
+
import { encodeEAN13, encodeEAN8, encodeUPCA, encodeUPCE, } from './writers.js';
|
|
60
49
|
/** EAN-2 parity rows, indexed by the two-digit value modulo four. */
|
|
61
50
|
export const EAN2_PARITY = Object.freeze([
|
|
62
|
-
|
|
51
|
+
'AA', 'AB', 'BA', 'BB',
|
|
63
52
|
]);
|
|
64
|
-
|
|
65
53
|
/** EAN-5 parity rows, indexed by the supplemental checksum. */
|
|
66
54
|
export const EAN5_PARITY = Object.freeze([
|
|
67
|
-
|
|
68
|
-
|
|
55
|
+
'BBAAA', 'BABAA', 'BAABA', 'BAAAB', 'ABBAA',
|
|
56
|
+
'AABBA', 'AAABB', 'ABABA', 'ABAAB', 'AABAB',
|
|
69
57
|
]);
|
|
70
|
-
|
|
71
58
|
/** Start guard used by both UPC/EAN supplemental symbols. */
|
|
72
59
|
export const EAN_ADDON_START = '1011';
|
|
73
|
-
|
|
74
60
|
/** Separator inserted between adjacent supplemental digits. */
|
|
75
61
|
export const EAN_ADDON_SEPARATOR = '01';
|
|
76
|
-
|
|
77
62
|
export const EAN2_WIDTH = EAN_ADDON_START.length + 2 * 7 + EAN_ADDON_SEPARATOR.length;
|
|
78
63
|
export const EAN5_WIDTH = EAN_ADDON_START.length + 5 * 7 + EAN_ADDON_SEPARATOR.length * 4;
|
|
79
|
-
|
|
80
64
|
/**
|
|
81
65
|
* @param {unknown} value
|
|
82
66
|
* @param {number} length
|
|
@@ -84,13 +68,12 @@ export const EAN5_WIDTH = EAN_ADDON_START.length + 5 * 7 + EAN_ADDON_SEPARATOR.l
|
|
|
84
68
|
* @returns {string}
|
|
85
69
|
*/
|
|
86
70
|
function normalizeDigits(value, length, format) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
71
|
+
const digits = String(value);
|
|
72
|
+
if (!new RegExp(`^[0-9]{${length}}$`).test(digits)) {
|
|
73
|
+
throw new EncodeError(`${format}: payload must contain exactly ${length} digits`);
|
|
74
|
+
}
|
|
75
|
+
return digits;
|
|
92
76
|
}
|
|
93
|
-
|
|
94
77
|
/**
|
|
95
78
|
* EAN-5 supplemental checksum.
|
|
96
79
|
*
|
|
@@ -102,16 +85,13 @@ function normalizeDigits(value, length, format) {
|
|
|
102
85
|
* @returns {number} Checksum value, 0-9.
|
|
103
86
|
*/
|
|
104
87
|
export function ean5Checksum(value) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
return weighted % 10;
|
|
88
|
+
const digits = normalizeDigits(value, 5, 'EAN-5');
|
|
89
|
+
const weighted = 3 * (Number(digits[0]) + Number(digits[2]) + Number(digits[4])) +
|
|
90
|
+
9 * (Number(digits[1]) + Number(digits[3]));
|
|
91
|
+
return weighted % 10;
|
|
110
92
|
}
|
|
111
|
-
|
|
112
93
|
/** Alias for callers that use the EAN family check-digit terminology. */
|
|
113
94
|
export const ean5CheckDigit = ean5Checksum;
|
|
114
|
-
|
|
115
95
|
/**
|
|
116
96
|
* Resolve the EAN-2 parity row for a payload.
|
|
117
97
|
*
|
|
@@ -119,10 +99,9 @@ export const ean5CheckDigit = ean5Checksum;
|
|
|
119
99
|
* @returns {string} Two-character A/B parity row.
|
|
120
100
|
*/
|
|
121
101
|
export function ean2Parity(value) {
|
|
122
|
-
|
|
123
|
-
|
|
102
|
+
const digits = normalizeDigits(value, 2, 'EAN-2');
|
|
103
|
+
return EAN2_PARITY[Number(digits) % 4];
|
|
124
104
|
}
|
|
125
|
-
|
|
126
105
|
/**
|
|
127
106
|
* Resolve the EAN-5 parity row for a payload.
|
|
128
107
|
*
|
|
@@ -130,9 +109,8 @@ export function ean2Parity(value) {
|
|
|
130
109
|
* @returns {string} Five-character A/B parity row.
|
|
131
110
|
*/
|
|
132
111
|
export function ean5Parity(value) {
|
|
133
|
-
|
|
112
|
+
return EAN5_PARITY[ean5Checksum(value)];
|
|
134
113
|
}
|
|
135
|
-
|
|
136
114
|
/**
|
|
137
115
|
* Expand an A/B parity row into EAN digit patterns.
|
|
138
116
|
*
|
|
@@ -141,15 +119,15 @@ export function ean5Parity(value) {
|
|
|
141
119
|
* @returns {string}
|
|
142
120
|
*/
|
|
143
121
|
function encodeDigitPatterns(digits, parity) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
122
|
+
let modules = EAN_ADDON_START;
|
|
123
|
+
for (let i = 0; i < digits.length; i++) {
|
|
124
|
+
if (i > 0)
|
|
125
|
+
modules += EAN_ADDON_SEPARATOR;
|
|
126
|
+
const table = parity[i] === 'A' ? EAN_L : EAN_G;
|
|
127
|
+
modules += table[Number(digits[i])];
|
|
128
|
+
}
|
|
129
|
+
return modules;
|
|
151
130
|
}
|
|
152
|
-
|
|
153
131
|
/**
|
|
154
132
|
* Turn a module string into a one-row matrix and attach immutable source
|
|
155
133
|
* metadata for composition/rendering callers.
|
|
@@ -159,14 +137,14 @@ function encodeDigitPatterns(digits, parity) {
|
|
|
159
137
|
* @returns {BitMatrix}
|
|
160
138
|
*/
|
|
161
139
|
function toAddonMatrix(modules, metadata) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
140
|
+
const matrix = new BitMatrix(modules.length, 1);
|
|
141
|
+
for (let x = 0; x < modules.length; x++) {
|
|
142
|
+
if (modules[x] === '1')
|
|
143
|
+
matrix.set(x, 0);
|
|
144
|
+
}
|
|
145
|
+
matrix.eanAddon = Object.freeze({ ...metadata });
|
|
146
|
+
return matrix;
|
|
168
147
|
}
|
|
169
|
-
|
|
170
148
|
/**
|
|
171
149
|
* Encode an EAN-2 supplement.
|
|
172
150
|
*
|
|
@@ -174,19 +152,18 @@ function toAddonMatrix(modules, metadata) {
|
|
|
174
152
|
* @returns {BitMatrix} A 20-module, one-row supplement.
|
|
175
153
|
*/
|
|
176
154
|
export function encodeEAN2(value) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
155
|
+
const digits = normalizeDigits(value, 2, 'EAN-2');
|
|
156
|
+
const parity = ean2Parity(digits);
|
|
157
|
+
const matrix = toAddonMatrix(encodeDigitPatterns(digits, parity), {
|
|
158
|
+
format: 'ean2',
|
|
159
|
+
text: digits,
|
|
160
|
+
parity,
|
|
161
|
+
});
|
|
162
|
+
if (matrix.width !== EAN2_WIDTH) {
|
|
163
|
+
throw new EncodeError(`EAN-2: internal width is ${matrix.width}, expected ${EAN2_WIDTH}`);
|
|
164
|
+
}
|
|
165
|
+
return matrix;
|
|
188
166
|
}
|
|
189
|
-
|
|
190
167
|
/**
|
|
191
168
|
* Encode an EAN-5 supplement.
|
|
192
169
|
*
|
|
@@ -194,21 +171,20 @@ export function encodeEAN2(value) {
|
|
|
194
171
|
* @returns {BitMatrix} A 47-module, one-row supplement.
|
|
195
172
|
*/
|
|
196
173
|
export function encodeEAN5(value) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
174
|
+
const digits = normalizeDigits(value, 5, 'EAN-5');
|
|
175
|
+
const checksum = ean5Checksum(digits);
|
|
176
|
+
const parity = EAN5_PARITY[checksum];
|
|
177
|
+
const matrix = toAddonMatrix(encodeDigitPatterns(digits, parity), {
|
|
178
|
+
format: 'ean5',
|
|
179
|
+
text: digits,
|
|
180
|
+
parity,
|
|
181
|
+
checksum,
|
|
182
|
+
});
|
|
183
|
+
if (matrix.width !== EAN5_WIDTH) {
|
|
184
|
+
throw new EncodeError(`EAN-5: internal width is ${matrix.width}, expected ${EAN5_WIDTH}`);
|
|
185
|
+
}
|
|
186
|
+
return matrix;
|
|
210
187
|
}
|
|
211
|
-
|
|
212
188
|
/**
|
|
213
189
|
* Encode either supported supplement length.
|
|
214
190
|
*
|
|
@@ -216,15 +192,15 @@ export function encodeEAN5(value) {
|
|
|
216
192
|
* @returns {BitMatrix}
|
|
217
193
|
*/
|
|
218
194
|
export function encodeEANAddon(value) {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
195
|
+
const digits = String(value);
|
|
196
|
+
if (digits.length === 2)
|
|
197
|
+
return encodeEAN2(digits);
|
|
198
|
+
if (digits.length === 5)
|
|
199
|
+
return encodeEAN5(digits);
|
|
200
|
+
throw new EncodeError('EAN add-on: payload must contain exactly two or five digits');
|
|
223
201
|
}
|
|
224
|
-
|
|
225
202
|
/** Alias using the spelling used by some EAN documentation. */
|
|
226
203
|
export const encodeEANAddOn = encodeEANAddon;
|
|
227
|
-
|
|
228
204
|
/**
|
|
229
205
|
* @param {BitMatrix} matrix
|
|
230
206
|
* @param {number} offset
|
|
@@ -232,13 +208,14 @@ export const encodeEANAddOn = encodeEANAddon;
|
|
|
232
208
|
* @returns {boolean}
|
|
233
209
|
*/
|
|
234
210
|
function matches(matrix, offset, pattern) {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
211
|
+
if (offset < 0 || offset + pattern.length > matrix.width)
|
|
212
|
+
return false;
|
|
213
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
214
|
+
if ((matrix.get(offset + i, 0) ? '1' : '0') !== pattern[i])
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
return true;
|
|
240
218
|
}
|
|
241
|
-
|
|
242
219
|
/**
|
|
243
220
|
* @param {BitMatrix} matrix
|
|
244
221
|
* @param {number} start
|
|
@@ -246,46 +223,49 @@ function matches(matrix, offset, pattern) {
|
|
|
246
223
|
* @returns {{format:string,text:string,parity:string,checksum?:number,end:number}|null}
|
|
247
224
|
*/
|
|
248
225
|
function decodeAt(matrix, start, digitCount) {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
226
|
+
const width = digitCount === 2 ? EAN2_WIDTH : EAN5_WIDTH;
|
|
227
|
+
if (!matches(matrix, start, EAN_ADDON_START))
|
|
228
|
+
return null;
|
|
229
|
+
if (start > 0 && matrix.get(start - 1, 0))
|
|
230
|
+
return null;
|
|
231
|
+
let offset = start + EAN_ADDON_START.length;
|
|
232
|
+
let text = '';
|
|
233
|
+
let parity = '';
|
|
234
|
+
for (let i = 0; i < digitCount; i++) {
|
|
235
|
+
let found = null;
|
|
236
|
+
for (let digit = 0; digit < 10; digit++) {
|
|
237
|
+
for (const [letter, table] of [['A', EAN_L], ['B', EAN_G]]) {
|
|
238
|
+
if (matches(matrix, offset, table[digit]) &&
|
|
239
|
+
(!found || found.letter !== letter || found.digit !== digit)) {
|
|
240
|
+
if (found)
|
|
241
|
+
return null;
|
|
242
|
+
found = { digit, letter };
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (!found)
|
|
247
|
+
return null;
|
|
248
|
+
text += String(found.digit);
|
|
249
|
+
parity += found.letter;
|
|
250
|
+
offset += 7;
|
|
251
|
+
if (i < digitCount - 1) {
|
|
252
|
+
if (!matches(matrix, offset, EAN_ADDON_SEPARATOR))
|
|
253
|
+
return null;
|
|
254
|
+
offset += EAN_ADDON_SEPARATOR.length;
|
|
265
255
|
}
|
|
266
|
-
}
|
|
267
256
|
}
|
|
268
|
-
if (
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
offset += EAN_ADDON_SEPARATOR.length;
|
|
257
|
+
if (offset - start !== width)
|
|
258
|
+
return null;
|
|
259
|
+
if (digitCount === 2) {
|
|
260
|
+
if (EAN2_PARITY[Number(text) % 4] !== parity)
|
|
261
|
+
return null;
|
|
262
|
+
return { format: 'ean2', text, parity, end: offset };
|
|
275
263
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
if (EAN2_PARITY[Number(text) % 4] !== parity) return null;
|
|
281
|
-
return { format: 'ean2', text, parity, end: offset };
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
const checksum = ean5Checksum(text);
|
|
285
|
-
if (EAN5_PARITY[checksum] !== parity) return null;
|
|
286
|
-
return { format: 'ean5', text, parity, checksum, end: offset };
|
|
264
|
+
const checksum = ean5Checksum(text);
|
|
265
|
+
if (EAN5_PARITY[checksum] !== parity)
|
|
266
|
+
return null;
|
|
267
|
+
return { format: 'ean5', text, parity, checksum, end: offset };
|
|
287
268
|
}
|
|
288
|
-
|
|
289
269
|
/**
|
|
290
270
|
* Validate and decode an EAN-2 supplement. A leading quiet zone is accepted;
|
|
291
271
|
* the decoder searches the row for a valid start guard so a composed base
|
|
@@ -296,20 +276,19 @@ function decodeAt(matrix, start, digitCount) {
|
|
|
296
276
|
* @throws {FormatError} When no valid supplement is found.
|
|
297
277
|
*/
|
|
298
278
|
export function decodeEAN2(matrix) {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
279
|
+
if (!matrix || !Number.isInteger(matrix.width) || matrix.height !== 1) {
|
|
280
|
+
throw new FormatError('EAN-2: expected a one-row module matrix');
|
|
281
|
+
}
|
|
282
|
+
for (let start = 0; start + EAN2_WIDTH <= matrix.width; start++) {
|
|
283
|
+
const result = decodeAt(matrix, start, 2);
|
|
284
|
+
if (result) {
|
|
285
|
+
const { end, ...publicResult } = result;
|
|
286
|
+
void end;
|
|
287
|
+
return publicResult;
|
|
288
|
+
}
|
|
308
289
|
}
|
|
309
|
-
|
|
310
|
-
throw new FormatError('EAN-2: start, parity or digit pattern is invalid');
|
|
290
|
+
throw new FormatError('EAN-2: start, parity or digit pattern is invalid');
|
|
311
291
|
}
|
|
312
|
-
|
|
313
292
|
/**
|
|
314
293
|
* Validate and decode an EAN-5 supplement.
|
|
315
294
|
*
|
|
@@ -318,20 +297,19 @@ export function decodeEAN2(matrix) {
|
|
|
318
297
|
* @throws {FormatError} When no valid supplement is found.
|
|
319
298
|
*/
|
|
320
299
|
export function decodeEAN5(matrix) {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
}
|
|
324
|
-
for (let start = 0; start + EAN5_WIDTH <= matrix.width; start++) {
|
|
325
|
-
const result = decodeAt(matrix, start, 5);
|
|
326
|
-
if (result) {
|
|
327
|
-
const { end, ...publicResult } = result;
|
|
328
|
-
void end;
|
|
329
|
-
return publicResult;
|
|
300
|
+
if (!matrix || !Number.isInteger(matrix.width) || matrix.height !== 1) {
|
|
301
|
+
throw new FormatError('EAN-5: expected a one-row module matrix');
|
|
330
302
|
}
|
|
331
|
-
|
|
332
|
-
|
|
303
|
+
for (let start = 0; start + EAN5_WIDTH <= matrix.width; start++) {
|
|
304
|
+
const result = decodeAt(matrix, start, 5);
|
|
305
|
+
if (result) {
|
|
306
|
+
const { end, ...publicResult } = result;
|
|
307
|
+
void end;
|
|
308
|
+
return publicResult;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
throw new FormatError('EAN-5: start, parity, checksum or digit pattern is invalid');
|
|
333
312
|
}
|
|
334
|
-
|
|
335
313
|
/**
|
|
336
314
|
* Decode either supported supplement length.
|
|
337
315
|
*
|
|
@@ -340,20 +318,20 @@ export function decodeEAN5(matrix) {
|
|
|
340
318
|
* @throws {FormatError} When neither supplement grammar matches.
|
|
341
319
|
*/
|
|
342
320
|
export function decodeEANAddon(matrix) {
|
|
343
|
-
try {
|
|
344
|
-
return decodeEAN5(matrix);
|
|
345
|
-
} catch (fiveError) {
|
|
346
321
|
try {
|
|
347
|
-
|
|
348
|
-
}
|
|
349
|
-
|
|
322
|
+
return decodeEAN5(matrix);
|
|
323
|
+
}
|
|
324
|
+
catch (fiveError) {
|
|
325
|
+
try {
|
|
326
|
+
return decodeEAN2(matrix);
|
|
327
|
+
}
|
|
328
|
+
catch (twoError) {
|
|
329
|
+
throw new FormatError(`EAN add-on: ${fiveError.message}; ${twoError.message}`);
|
|
330
|
+
}
|
|
350
331
|
}
|
|
351
|
-
}
|
|
352
332
|
}
|
|
353
|
-
|
|
354
333
|
/** Alias using the spelling used by some EAN documentation. */
|
|
355
334
|
export const decodeEANAddOn = decodeEANAddon;
|
|
356
|
-
|
|
357
335
|
/**
|
|
358
336
|
* Join a base EAN/UPC matrix and a supplement with the standard quiet gap.
|
|
359
337
|
*
|
|
@@ -368,30 +346,31 @@ export const decodeEANAddOn = decodeEANAddon;
|
|
|
368
346
|
* @returns {BitMatrix}
|
|
369
347
|
*/
|
|
370
348
|
export function composeEANAddon(base, addon, options = {}) {
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
349
|
+
if (!base || !Number.isInteger(base.width) || base.height !== 1) {
|
|
350
|
+
throw new TypeError('EAN add-on: base must be a one-row module matrix');
|
|
351
|
+
}
|
|
352
|
+
const addonMatrix = typeof addon === 'string' || typeof addon === 'number'
|
|
353
|
+
? encodeEANAddon(addon)
|
|
354
|
+
: addon;
|
|
355
|
+
if (!addonMatrix || !Number.isInteger(addonMatrix.width) || addonMatrix.height !== 1) {
|
|
356
|
+
throw new TypeError('EAN add-on: supplement must be a one-row module matrix or digits');
|
|
357
|
+
}
|
|
358
|
+
const gap = options.gap === undefined ? 9 : Number(options.gap);
|
|
359
|
+
if (!Number.isInteger(gap) || gap < 1) {
|
|
360
|
+
throw new EncodeError(`EAN add-on: gap must be a positive integer, got ${options.gap}`);
|
|
361
|
+
}
|
|
362
|
+
const result = new BitMatrix(base.width + gap + addonMatrix.width, 1);
|
|
363
|
+
for (let x = 0; x < base.width; x++)
|
|
364
|
+
if (base.get(x, 0))
|
|
365
|
+
result.set(x, 0);
|
|
366
|
+
for (let x = 0; x < addonMatrix.width; x++) {
|
|
367
|
+
if (addonMatrix.get(x, 0))
|
|
368
|
+
result.set(base.width + gap + x, 0);
|
|
369
|
+
}
|
|
370
|
+
if (addonMatrix.eanAddon)
|
|
371
|
+
result.eanAddon = { ...addonMatrix.eanAddon, gap };
|
|
372
|
+
return result;
|
|
393
373
|
}
|
|
394
|
-
|
|
395
374
|
/**
|
|
396
375
|
* Compose an EAN-13 symbol with an EAN-2 or EAN-5 supplement.
|
|
397
376
|
*
|
|
@@ -401,20 +380,17 @@ export function composeEANAddon(base, addon, options = {}) {
|
|
|
401
380
|
* @returns {BitMatrix}
|
|
402
381
|
*/
|
|
403
382
|
export function encodeEAN13WithAddon(value, addon, options = {}) {
|
|
404
|
-
|
|
383
|
+
return composeEANAddon(encodeEAN13(value), addon, options);
|
|
405
384
|
}
|
|
406
|
-
|
|
407
385
|
/** @see encodeEAN13WithAddon */
|
|
408
386
|
export function encodeEAN8WithAddon(value, addon, options = {}) {
|
|
409
|
-
|
|
387
|
+
return composeEANAddon(encodeEAN8(value), addon, options);
|
|
410
388
|
}
|
|
411
|
-
|
|
412
389
|
/** @see encodeEAN13WithAddon */
|
|
413
390
|
export function encodeUPCAWithAddon(value, addon, options = {}) {
|
|
414
|
-
|
|
391
|
+
return composeEANAddon(encodeUPCA(value), addon, options);
|
|
415
392
|
}
|
|
416
|
-
|
|
417
393
|
/** @see encodeEAN13WithAddon */
|
|
418
394
|
export function encodeUPCEWithAddon(value, addon, options = {}) {
|
|
419
|
-
|
|
395
|
+
return composeEANAddon(encodeUPCE(value), addon, options);
|
|
420
396
|
}
|
package/src/js/oned/index.js
CHANGED
|
@@ -27,48 +27,17 @@
|
|
|
27
27
|
*
|
|
28
28
|
* Original work. No code from any other barcode implementation.
|
|
29
29
|
*/
|
|
30
|
-
|
|
31
30
|
/**
|
|
32
31
|
* Linear symbologies.
|
|
33
32
|
*
|
|
34
33
|
* @module oned
|
|
35
34
|
*/
|
|
36
|
-
|
|
37
|
-
export {
|
|
38
|
-
|
|
39
|
-
encodeCode39, encodeCode93, encodeCode128,
|
|
40
|
-
encodeITF, encodeITF14, encodeCodabar, encodeCode11,
|
|
41
|
-
encodeMSI, encodePharmacode,
|
|
42
|
-
ean13CheckDigit,
|
|
43
|
-
} from './writers.js';
|
|
44
|
-
|
|
45
|
-
export {
|
|
46
|
-
EAN2_PARITY, EAN5_PARITY, EAN2_WIDTH, EAN5_WIDTH,
|
|
47
|
-
EAN_ADDON_START, EAN_ADDON_SEPARATOR,
|
|
48
|
-
ean2Parity, ean5Checksum, ean5CheckDigit, ean5Parity,
|
|
49
|
-
encodeEAN2, encodeEAN5, encodeEANAddon, encodeEANAddOn,
|
|
50
|
-
decodeEAN2, decodeEAN5, decodeEANAddon, decodeEANAddOn,
|
|
51
|
-
composeEANAddon,
|
|
52
|
-
encodeEAN13WithAddon, encodeEAN8WithAddon,
|
|
53
|
-
encodeUPCAWithAddon, encodeUPCEWithAddon,
|
|
54
|
-
} from './addons.js';
|
|
55
|
-
|
|
56
|
-
export {
|
|
57
|
-
decodeOneD, decodeOneDStrict,
|
|
58
|
-
decodeCode11, decodeMSI,
|
|
59
|
-
patternVariance, recordPattern, toNarrowWidePattern,
|
|
60
|
-
} from './reader.js';
|
|
61
|
-
|
|
35
|
+
export { encodeEAN13, encodeEAN8, encodeUPCA, encodeUPCE, encodeISBN, encodeCode39, encodeCode93, encodeCode128, encodeITF, encodeITF14, encodeCodabar, encodeCode11, encodeMSI, encodePharmacode, ean13CheckDigit, } from './writers.js';
|
|
36
|
+
export { EAN2_PARITY, EAN5_PARITY, EAN2_WIDTH, EAN5_WIDTH, EAN_ADDON_START, EAN_ADDON_SEPARATOR, ean2Parity, ean5Checksum, ean5CheckDigit, ean5Parity, encodeEAN2, encodeEAN5, encodeEANAddon, encodeEANAddOn, decodeEAN2, decodeEAN5, decodeEANAddon, decodeEANAddOn, composeEANAddon, encodeEAN13WithAddon, encodeEAN8WithAddon, encodeUPCAWithAddon, encodeUPCEWithAddon, } from './addons.js';
|
|
37
|
+
export { decodeOneD, decodeOneDStrict, decodeCode11, decodeMSI, patternVariance, recordPattern, toNarrowWidePattern, } from './reader.js';
|
|
62
38
|
export { validateTables } from './patterns.js';
|
|
63
|
-
|
|
64
|
-
import {
|
|
65
|
-
encodeEAN13, encodeEAN8, encodeUPCA, encodeUPCE, encodeISBN,
|
|
66
|
-
encodeCode39, encodeCode93, encodeCode128,
|
|
67
|
-
encodeITF, encodeITF14, encodeCodabar, encodeCode11,
|
|
68
|
-
encodeMSI, encodePharmacode,
|
|
69
|
-
} from './writers.js';
|
|
39
|
+
import { encodeEAN13, encodeEAN8, encodeUPCA, encodeUPCE, encodeISBN, encodeCode39, encodeCode93, encodeCode128, encodeITF, encodeITF14, encodeCodabar, encodeCode11, encodeMSI, encodePharmacode, } from './writers.js';
|
|
70
40
|
import { encodeEAN2, encodeEAN5 } from './addons.js';
|
|
71
|
-
|
|
72
41
|
/**
|
|
73
42
|
* Writers by format id, for the top-level `encode()` dispatcher.
|
|
74
43
|
*
|
|
@@ -80,27 +49,27 @@ import { encodeEAN2, encodeEAN5 } from './addons.js';
|
|
|
80
49
|
* @type {Record<string, {encode: Function, readable: boolean, label: string, role?: string}>}
|
|
81
50
|
*/
|
|
82
51
|
export const ONED_FORMATS = {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
52
|
+
ean13: { encode: encodeEAN13, readable: true, label: 'EAN-13' },
|
|
53
|
+
ean8: { encode: encodeEAN8, readable: true, label: 'EAN-8' },
|
|
54
|
+
upca: { encode: encodeUPCA, readable: true, label: 'UPC-A' },
|
|
55
|
+
isbn: { encode: encodeISBN, readable: true, label: 'ISBN (Bookland EAN-13)' },
|
|
56
|
+
upce: { encode: encodeUPCE, readable: true, label: 'UPC-E' },
|
|
57
|
+
code128: { encode: encodeCode128, readable: true, label: 'Code 128' },
|
|
58
|
+
gs1128: {
|
|
59
|
+
encode: (v, o) => encodeCode128(v, { ...o, gs1: true }),
|
|
60
|
+
readable: true,
|
|
61
|
+
label: 'GS1-128',
|
|
62
|
+
},
|
|
63
|
+
code39: { encode: encodeCode39, readable: true, label: 'Code 39' },
|
|
64
|
+
code93: { encode: encodeCode93, readable: true, label: 'Code 93' },
|
|
65
|
+
itf: { encode: encodeITF, readable: true, label: 'ITF (Interleaved 2 of 5)' },
|
|
66
|
+
itf14: { encode: encodeITF14, readable: true, label: 'ITF-14' },
|
|
67
|
+
codabar: { encode: encodeCodabar, readable: true, label: 'Codabar' },
|
|
68
|
+
code11: { encode: encodeCode11, readable: true, label: 'Code 11' },
|
|
69
|
+
msi: { encode: encodeMSI, readable: true, label: 'MSI Plessey' },
|
|
70
|
+
pharmacode: { encode: encodePharmacode, readable: false, label: 'Pharmacode' },
|
|
71
|
+
// Supplements are reported as readable capabilities, but the image reader
|
|
72
|
+
// only accepts them when attached to a validated EAN/UPC parent symbol.
|
|
73
|
+
ean2: { encode: encodeEAN2, readable: true, role: 'supplement', label: 'EAN-2 supplement' },
|
|
74
|
+
ean5: { encode: encodeEAN5, readable: true, role: 'supplement', label: 'EAN-5 supplement' },
|
|
106
75
|
};
|