@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
|
@@ -27,165 +27,224 @@
|
|
|
27
27
|
*
|
|
28
28
|
* Original work. No code from any other barcode implementation.
|
|
29
29
|
*/
|
|
30
|
-
|
|
31
30
|
/** Data Matrix ECC 200 encoder: ASCII/Base256, RS interleaving and Annex F placement. */
|
|
32
31
|
import { BitMatrix } from '../core/bit-matrix.js';
|
|
33
32
|
import { EncodeError } from '../core/errors.js';
|
|
34
33
|
import { GF256_DM } from '../core/galois-field.js';
|
|
35
34
|
import { rsEncode } from '../core/reed-solomon.js';
|
|
36
35
|
import { symbolForDataCodewords } from './tables.js';
|
|
37
|
-
|
|
38
36
|
function asciiCodewords(text) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
37
|
+
const out = [];
|
|
38
|
+
for (let i = 0; i < text.length;) {
|
|
39
|
+
const a = text.charCodeAt(i);
|
|
40
|
+
if (a > 255)
|
|
41
|
+
throw new EncodeError('Data Matrix ASCII: characters must fit ISO-8859-1; use Base256 for UTF-8');
|
|
42
|
+
if (i + 1 < text.length) {
|
|
43
|
+
const b = text.charCodeAt(i + 1);
|
|
44
|
+
if (a >= 48 && a <= 57 && b >= 48 && b <= 57) {
|
|
45
|
+
out.push(130 + (a - 48) * 10 + b - 48);
|
|
46
|
+
i += 2;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (a <= 127)
|
|
51
|
+
out.push(a + 1);
|
|
52
|
+
else
|
|
53
|
+
out.push(235, a - 127);
|
|
54
|
+
i++;
|
|
46
55
|
}
|
|
47
|
-
|
|
48
|
-
else out.push(235, a - 127);
|
|
49
|
-
i++;
|
|
50
|
-
}
|
|
51
|
-
return out;
|
|
56
|
+
return out;
|
|
52
57
|
}
|
|
53
|
-
|
|
54
58
|
function bytesFor(value) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
+
if (value instanceof Uint8Array)
|
|
60
|
+
return value;
|
|
61
|
+
if (ArrayBuffer.isView(value))
|
|
62
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
63
|
+
if (typeof value !== 'string')
|
|
64
|
+
throw new EncodeError('Data Matrix: value must be a string or byte array');
|
|
65
|
+
return new TextEncoder().encode(value);
|
|
59
66
|
}
|
|
60
|
-
|
|
61
67
|
function randomize255(value, position) {
|
|
62
|
-
|
|
63
|
-
|
|
68
|
+
const pseudo = (149 * position) % 255 + 1;
|
|
69
|
+
return value + pseudo <= 255 ? value + pseudo : value + pseudo - 256;
|
|
64
70
|
}
|
|
65
|
-
|
|
66
71
|
function base256Codewords(value, prefixLength = 0) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
72
|
+
const bytes = bytesFor(value);
|
|
73
|
+
if (bytes.length > 1555)
|
|
74
|
+
throw new EncodeError('Data Matrix Base256: payload exceeds ECC 200 capacity');
|
|
75
|
+
const out = [231];
|
|
76
|
+
if (bytes.length <= 249)
|
|
77
|
+
out.push(bytes.length);
|
|
78
|
+
else
|
|
79
|
+
out.push(Math.floor(bytes.length / 250) + 249, bytes.length % 250);
|
|
80
|
+
// Base256 randomization uses the absolute 1-based codeword position in the
|
|
81
|
+
// symbol. A leading GS1 FNC1 therefore shifts every randomized codeword.
|
|
82
|
+
for (let i = 1; i < out.length; i++)
|
|
83
|
+
out[i] = randomize255(out[i], prefixLength + i + 1);
|
|
84
|
+
for (const b of bytes)
|
|
85
|
+
out.push(randomize255(b, prefixLength + out.length + 1));
|
|
86
|
+
return out;
|
|
77
87
|
}
|
|
78
|
-
|
|
79
88
|
function pad(data, capacity) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
+
const out = data.slice();
|
|
90
|
+
if (out.length < capacity)
|
|
91
|
+
out.push(129);
|
|
92
|
+
while (out.length < capacity) {
|
|
93
|
+
const position = out.length + 1;
|
|
94
|
+
const pseudo = (149 * position) % 253 + 1;
|
|
95
|
+
const v = 129 + pseudo;
|
|
96
|
+
out.push(v <= 254 ? v : v - 254);
|
|
97
|
+
}
|
|
98
|
+
return out;
|
|
89
99
|
}
|
|
90
|
-
|
|
91
100
|
function interleave(data, symbol) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
101
|
+
const blocks = symbol.dataBlockLengths.map((length) => ({ data: new Array(length), ecc: null }));
|
|
102
|
+
let at = 0;
|
|
103
|
+
const longest = Math.max(...symbol.dataBlockLengths);
|
|
104
|
+
// Data codewords are dealt across the RS blocks by column. Splitting the
|
|
105
|
+
// stream into consecutive chunks and then interleaving those chunks looks
|
|
106
|
+
// self-consistent to a decoder doing the same inverse operation, but it is
|
|
107
|
+
// not ECC 200's wire order once a symbol has multiple blocks.
|
|
108
|
+
for (let i = 0; i < longest; i++) {
|
|
109
|
+
for (const block of blocks)
|
|
110
|
+
if (i < block.data.length)
|
|
111
|
+
block.data[i] = data[at++];
|
|
112
|
+
}
|
|
113
|
+
for (const block of blocks) {
|
|
114
|
+
// ECC 200 starts its generator roots at alpha^1 (generator base 1).
|
|
115
|
+
block.ecc = rsEncode(block.data, symbol.eccPerBlock, GF256_DM, 1);
|
|
116
|
+
}
|
|
117
|
+
const out = [];
|
|
118
|
+
for (let i = 0; i < longest; i++)
|
|
119
|
+
for (const b of blocks)
|
|
120
|
+
if (i < b.data.length)
|
|
121
|
+
out.push(b.data[i]);
|
|
122
|
+
// The 144x144 symbol has eight long and two short data blocks. Its parity
|
|
123
|
+
// interleave begins with the first short block; deriving the rotation from
|
|
124
|
+
// the declarative lengths keeps that exception out of a size-specific test.
|
|
125
|
+
const eccOffset = blocks.findIndex((block) => block.data.length < longest);
|
|
126
|
+
const rotation = eccOffset < 0 ? 0 : eccOffset;
|
|
127
|
+
for (let i = 0; i < symbol.eccPerBlock; i++) {
|
|
128
|
+
for (let b = 0; b < blocks.length; b++)
|
|
129
|
+
out.push(blocks[(b + rotation) % blocks.length].ecc[i]);
|
|
130
|
+
}
|
|
131
|
+
return out;
|
|
118
132
|
}
|
|
119
|
-
|
|
120
133
|
function place(codewords, rows, cols) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
134
|
+
const cells = new Int8Array(rows * cols).fill(-1);
|
|
135
|
+
const bit = (row, col, pos, n) => {
|
|
136
|
+
if (row < 0) {
|
|
137
|
+
row += rows;
|
|
138
|
+
col += 4 - ((rows + 4) % 8);
|
|
139
|
+
}
|
|
140
|
+
if (col < 0) {
|
|
141
|
+
col += cols;
|
|
142
|
+
row += 4 - ((cols + 4) % 8);
|
|
143
|
+
}
|
|
144
|
+
cells[row * cols + col] = (codewords[pos] >>> (8 - n)) & 1;
|
|
145
|
+
};
|
|
146
|
+
const utah = (r, c, p) => { bit(r - 2, c - 2, p, 1); bit(r - 2, c - 1, p, 2); bit(r - 1, c - 2, p, 3); bit(r - 1, c - 1, p, 4); bit(r - 1, c, p, 5); bit(r, c - 2, p, 6); bit(r, c - 1, p, 7); bit(r, c, p, 8); };
|
|
147
|
+
const corner1 = (p) => { bit(rows - 1, 0, p, 1); bit(rows - 1, 1, p, 2); bit(rows - 1, 2, p, 3); bit(0, cols - 2, p, 4); bit(0, cols - 1, p, 5); bit(1, cols - 1, p, 6); bit(2, cols - 1, p, 7); bit(3, cols - 1, p, 8); };
|
|
148
|
+
const corner2 = (p) => { bit(rows - 3, 0, p, 1); bit(rows - 2, 0, p, 2); bit(rows - 1, 0, p, 3); bit(0, cols - 4, p, 4); bit(0, cols - 3, p, 5); bit(0, cols - 2, p, 6); bit(0, cols - 1, p, 7); bit(1, cols - 1, p, 8); };
|
|
149
|
+
const corner3 = (p) => { bit(rows - 3, 0, p, 1); bit(rows - 2, 0, p, 2); bit(rows - 1, 0, p, 3); bit(0, cols - 2, p, 4); bit(0, cols - 1, p, 5); bit(1, cols - 1, p, 6); bit(2, cols - 1, p, 7); bit(3, cols - 1, p, 8); };
|
|
150
|
+
const corner4 = (p) => { bit(rows - 1, 0, p, 1); bit(rows - 1, cols - 1, p, 2); bit(0, cols - 3, p, 3); bit(0, cols - 2, p, 4); bit(0, cols - 1, p, 5); bit(1, cols - 3, p, 6); bit(1, cols - 2, p, 7); bit(1, cols - 1, p, 8); };
|
|
151
|
+
let row = 4, col = 0, pos = 0;
|
|
152
|
+
do {
|
|
153
|
+
if (row === rows && col === 0)
|
|
154
|
+
corner1(pos++);
|
|
155
|
+
if (row === rows - 2 && col === 0 && cols % 4 !== 0)
|
|
156
|
+
corner2(pos++);
|
|
157
|
+
if (row === rows - 2 && col === 0 && cols % 8 === 4)
|
|
158
|
+
corner3(pos++);
|
|
159
|
+
if (row === rows + 4 && col === 2 && cols % 8 === 0)
|
|
160
|
+
corner4(pos++);
|
|
161
|
+
do {
|
|
162
|
+
if (row < rows && col >= 0 && cells[row * cols + col] < 0)
|
|
163
|
+
utah(row, col, pos++);
|
|
164
|
+
row -= 2;
|
|
165
|
+
col += 2;
|
|
166
|
+
} while (row >= 0 && col < cols);
|
|
167
|
+
row += 1;
|
|
168
|
+
col += 3;
|
|
169
|
+
do {
|
|
170
|
+
if (row >= 0 && col < cols && cells[row * cols + col] < 0)
|
|
171
|
+
utah(row, col, pos++);
|
|
172
|
+
row += 2;
|
|
173
|
+
col -= 2;
|
|
174
|
+
} while (row < rows && col >= 0);
|
|
175
|
+
row += 3;
|
|
176
|
+
col += 1;
|
|
177
|
+
} while (row < rows || col < cols);
|
|
178
|
+
if (cells[cells.length - 1] < 0) {
|
|
179
|
+
cells[cells.length - 1] = 1;
|
|
180
|
+
cells[cells.length - cols - 2] = 1;
|
|
181
|
+
}
|
|
182
|
+
if (pos !== codewords.length)
|
|
183
|
+
throw new EncodeError(`Data Matrix: placement consumed ${pos} of ${codewords.length} codewords`);
|
|
184
|
+
return cells;
|
|
146
185
|
}
|
|
147
|
-
|
|
148
186
|
function buildMatrix(codewords, symbol) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
187
|
+
const regionCols = symbol.width / symbol.regionWidth;
|
|
188
|
+
const regionRows = symbol.height / symbol.regionHeight;
|
|
189
|
+
const dataWidth = symbol.dataRegionColumns;
|
|
190
|
+
const dataHeight = symbol.dataRegionRows;
|
|
191
|
+
const data = place(codewords, regionRows * dataHeight, regionCols * dataWidth);
|
|
192
|
+
const matrix = new BitMatrix(symbol.width, symbol.height);
|
|
193
|
+
for (let ry = 0; ry < regionRows; ry++)
|
|
194
|
+
for (let rx = 0; rx < regionCols; rx++) {
|
|
195
|
+
const x0 = rx * symbol.regionWidth, y0 = ry * symbol.regionHeight;
|
|
196
|
+
for (let x = 0; x < symbol.regionWidth; x++) {
|
|
197
|
+
if ((x & 1) === 0)
|
|
198
|
+
matrix.set(x0 + x, y0);
|
|
199
|
+
matrix.set(x0 + x, y0 + dataHeight + 1);
|
|
200
|
+
}
|
|
201
|
+
// The top and right timing borders are complementary: top-left is dark,
|
|
202
|
+
// top-right is light, and the solid bottom-right corner remains dark.
|
|
203
|
+
for (let y = 0; y < symbol.regionHeight; y++) {
|
|
204
|
+
matrix.set(x0, y0 + y);
|
|
205
|
+
if ((y & 1) === 1)
|
|
206
|
+
matrix.set(x0 + dataWidth + 1, y0 + y);
|
|
207
|
+
}
|
|
208
|
+
for (let y = 0; y < dataHeight; y++)
|
|
209
|
+
for (let x = 0; x < dataWidth; x++)
|
|
210
|
+
if (data[(ry * dataHeight + y) * (regionCols * dataWidth) + rx * dataWidth + x])
|
|
211
|
+
matrix.set(x0 + 1 + x, y0 + 1 + y);
|
|
212
|
+
}
|
|
213
|
+
return matrix;
|
|
164
214
|
}
|
|
165
|
-
|
|
166
215
|
/** Encode a string (ASCII mode) or byte payload (Base256) into Data Matrix ECC 200. */
|
|
167
216
|
export function encodeDataMatrix(value, options = {}) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
217
|
+
const encoding = options.encoding ?? (value instanceof Uint8Array ? 'base256' : 'ascii');
|
|
218
|
+
let raw;
|
|
219
|
+
if (encoding === 'ascii') {
|
|
220
|
+
if (typeof value !== 'string')
|
|
221
|
+
throw new EncodeError('Data Matrix ASCII: value must be a string');
|
|
222
|
+
raw = asciiCodewords(value);
|
|
223
|
+
}
|
|
224
|
+
else if (encoding === 'base256')
|
|
225
|
+
raw = base256Codewords(value, options.gs1 === true ? 1 : 0);
|
|
226
|
+
else
|
|
227
|
+
throw new EncodeError(`Data Matrix: unsupported encoding "${encoding}"`);
|
|
228
|
+
// GS1 DataMatrix is ECC 200 with FNC1 in the first codeword position.
|
|
229
|
+
if (options.gs1 === true)
|
|
230
|
+
raw.unshift(232);
|
|
231
|
+
const shape = options.shape ?? 'any';
|
|
232
|
+
if (shape !== 'any' && shape !== 'square' && shape !== 'rectangular')
|
|
233
|
+
throw new EncodeError(`Data Matrix: invalid shape "${shape}"`);
|
|
234
|
+
const symbol = symbolForDataCodewords(raw.length, shape);
|
|
235
|
+
if (!symbol)
|
|
236
|
+
throw new EncodeError(`Data Matrix: ${raw.length} data codewords do not fit an ECC 200 ${shape} symbol`);
|
|
237
|
+
return buildMatrix(interleave(pad(raw, symbol.dataCodewords), symbol), symbol);
|
|
182
238
|
}
|
|
183
|
-
|
|
184
239
|
/** Encode already compacted ASCII/Base256 codewords, primarily for conformance tests. */
|
|
185
240
|
export function encodeDataMatrixCodewords(codewords, options = {}) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
241
|
+
if (!Array.isArray(codewords) && !(codewords instanceof Uint8Array))
|
|
242
|
+
throw new EncodeError('Data Matrix: codewords must be an array');
|
|
243
|
+
for (const c of codewords)
|
|
244
|
+
if (!Number.isInteger(c) || c < 0 || c > 255)
|
|
245
|
+
throw new EncodeError('Data Matrix: codewords must be bytes');
|
|
246
|
+
const symbol = symbolForDataCodewords(codewords.length, options.shape ?? 'any');
|
|
247
|
+
if (!symbol)
|
|
248
|
+
throw new EncodeError('Data Matrix: codewords do not fit ECC 200');
|
|
249
|
+
return buildMatrix(interleave(pad(Array.from(codewords), symbol.dataCodewords), symbol), symbol);
|
|
191
250
|
}
|
|
@@ -27,16 +27,8 @@
|
|
|
27
27
|
*
|
|
28
28
|
* Original work. No code from any other barcode implementation.
|
|
29
29
|
*/
|
|
30
|
-
|
|
31
30
|
/** Data Matrix ECC 200 entry points. @module datamatrix */
|
|
32
|
-
|
|
33
31
|
export { encodeDataMatrix, encodeDataMatrixCodewords } from './encoder.js';
|
|
34
32
|
export { decodeDataMatrix } from './decoder.js';
|
|
35
33
|
export { detectDataMatrix, detectAndDecodeDataMatrix } from './detector.js';
|
|
36
|
-
export {
|
|
37
|
-
DATAMATRIX_SYMBOLS,
|
|
38
|
-
SYMBOLS,
|
|
39
|
-
symbolForDataCodewords,
|
|
40
|
-
validateDataMatrixTables,
|
|
41
|
-
validateTables,
|
|
42
|
-
} from './tables.js';
|
|
34
|
+
export { DATAMATRIX_SYMBOLS, SYMBOLS, symbolForDataCodewords, validateDataMatrixTables, validateTables, } from './tables.js';
|
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
*
|
|
28
28
|
* Original work. No code from any other barcode implementation.
|
|
29
29
|
*/
|
|
30
|
-
|
|
31
30
|
/**
|
|
32
31
|
* Data Matrix ECC 200 symbol parameters.
|
|
33
32
|
*
|
|
@@ -38,86 +37,86 @@
|
|
|
38
37
|
*
|
|
39
38
|
* @module datamatrix/tables
|
|
40
39
|
*/
|
|
41
|
-
|
|
42
40
|
function symbol(width, height, dataRegionWidth, dataRegionHeight, dataCodewords, errorCodewords, dataBlockLengths) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
41
|
+
const blockCount = dataBlockLengths.length;
|
|
42
|
+
return Object.freeze({
|
|
43
|
+
width, height, rows: height, columns: width,
|
|
44
|
+
// Region dimensions include their one-module finder border on each side;
|
|
45
|
+
// dataRegion* expose the inner placement lattice explicitly.
|
|
46
|
+
regionWidth: dataRegionWidth + 2, regionHeight: dataRegionHeight + 2,
|
|
47
|
+
dataRegionWidth, dataRegionHeight,
|
|
48
|
+
dataRegionRows: dataRegionHeight, dataRegionColumns: dataRegionWidth,
|
|
49
|
+
dataCodewords, errorCodewords, blockCount,
|
|
50
|
+
eccPerBlock: errorCodewords / blockCount,
|
|
51
|
+
dataBlockLengths: Object.freeze(dataBlockLengths),
|
|
52
|
+
});
|
|
55
53
|
}
|
|
56
|
-
|
|
57
54
|
/** Classic ISO/IEC 16022 ECC 200 symbols; DMRE is deliberately excluded. */
|
|
58
55
|
export const DATAMATRIX_SYMBOLS = Object.freeze([
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
56
|
+
symbol(10, 10, 8, 8, 3, 5, [3]),
|
|
57
|
+
symbol(12, 12, 10, 10, 5, 7, [5]),
|
|
58
|
+
symbol(14, 14, 12, 12, 8, 10, [8]),
|
|
59
|
+
symbol(16, 16, 14, 14, 12, 12, [12]),
|
|
60
|
+
symbol(18, 18, 16, 16, 18, 14, [18]),
|
|
61
|
+
symbol(20, 20, 18, 18, 22, 18, [22]),
|
|
62
|
+
symbol(22, 22, 20, 20, 30, 20, [30]),
|
|
63
|
+
symbol(24, 24, 22, 22, 36, 24, [36]),
|
|
64
|
+
symbol(26, 26, 24, 24, 44, 28, [44]),
|
|
65
|
+
symbol(32, 32, 14, 14, 62, 36, [62]),
|
|
66
|
+
symbol(36, 36, 16, 16, 86, 42, [86]),
|
|
67
|
+
symbol(40, 40, 18, 18, 114, 48, [114]),
|
|
68
|
+
symbol(44, 44, 20, 20, 144, 56, [144]),
|
|
69
|
+
symbol(48, 48, 22, 22, 174, 68, [174]),
|
|
70
|
+
symbol(52, 52, 24, 24, 204, 84, [102, 102]),
|
|
71
|
+
symbol(64, 64, 14, 14, 280, 112, [140, 140]),
|
|
72
|
+
symbol(72, 72, 16, 16, 368, 144, [92, 92, 92, 92]),
|
|
73
|
+
symbol(80, 80, 18, 18, 456, 192, [114, 114, 114, 114]),
|
|
74
|
+
symbol(88, 88, 20, 20, 576, 224, [144, 144, 144, 144]),
|
|
75
|
+
symbol(96, 96, 22, 22, 696, 272, [174, 174, 174, 174]),
|
|
76
|
+
symbol(104, 104, 24, 24, 816, 336, [136, 136, 136, 136, 136, 136]),
|
|
77
|
+
symbol(120, 120, 18, 18, 1050, 408, [175, 175, 175, 175, 175, 175]),
|
|
78
|
+
symbol(132, 132, 20, 20, 1304, 496, [163, 163, 163, 163, 163, 163, 163, 163]),
|
|
79
|
+
symbol(144, 144, 22, 22, 1558, 620, [156, 156, 156, 156, 156, 156, 156, 156, 155, 155]),
|
|
80
|
+
symbol(18, 8, 16, 6, 5, 7, [5]),
|
|
81
|
+
symbol(32, 8, 14, 6, 10, 11, [10]),
|
|
82
|
+
symbol(26, 12, 24, 10, 16, 14, [16]),
|
|
83
|
+
symbol(36, 12, 16, 10, 22, 18, [22]),
|
|
84
|
+
symbol(36, 16, 16, 14, 32, 24, [32]),
|
|
85
|
+
symbol(48, 16, 22, 14, 49, 28, [49]),
|
|
89
86
|
]);
|
|
90
|
-
|
|
91
87
|
/** Compatibility alias. */
|
|
92
88
|
export const SYMBOLS = DATAMATRIX_SYMBOLS;
|
|
93
|
-
|
|
94
89
|
/** Return the smallest permitted symbol that holds `count` data codewords. */
|
|
95
90
|
export function symbolForDataCodewords(count, shape = 'any') {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
91
|
+
for (const s of DATAMATRIX_SYMBOLS) {
|
|
92
|
+
const rectangular = s.width !== s.height;
|
|
93
|
+
if ((shape === 'square' && rectangular) || (shape === 'rectangular' && !rectangular))
|
|
94
|
+
continue;
|
|
95
|
+
if (count <= s.dataCodewords)
|
|
96
|
+
return s;
|
|
97
|
+
}
|
|
98
|
+
throw new RangeError(`Data Matrix: ${count} data codewords do not fit an ECC 200 ${shape} symbol`);
|
|
102
99
|
}
|
|
103
|
-
|
|
104
100
|
/** Check redundant geometry and block identities in the static table. */
|
|
105
101
|
export function validateDataMatrixTables() {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
102
|
+
const issues = [];
|
|
103
|
+
for (const s of DATAMATRIX_SYMBOLS) {
|
|
104
|
+
const regionsX = s.width / s.regionWidth;
|
|
105
|
+
const regionsY = s.height / s.regionHeight;
|
|
106
|
+
if (!Number.isInteger(regionsX) || !Number.isInteger(regionsY))
|
|
107
|
+
issues.push(`${s.width}x${s.height}: non-integral regions`);
|
|
108
|
+
const modules = regionsX * regionsY * s.dataRegionWidth * s.dataRegionHeight;
|
|
109
|
+
// Annex F reserves four terminal modules on a few lattice dimensions.
|
|
110
|
+
// They are set to dark after codeword placement and do not carry data.
|
|
111
|
+
const unused = modules - (s.dataCodewords + s.errorCodewords) * 8;
|
|
112
|
+
if (unused !== 0 && unused !== 4)
|
|
113
|
+
issues.push(`${s.width}x${s.height}: geometry/codeword mismatch`);
|
|
114
|
+
if (s.dataBlockLengths.reduce((a, b) => a + b, 0) !== s.dataCodewords)
|
|
115
|
+
issues.push(`${s.width}x${s.height}: data block mismatch`);
|
|
116
|
+
if (s.eccPerBlock * s.blockCount !== s.errorCodewords)
|
|
117
|
+
issues.push(`${s.width}x${s.height}: ecc block mismatch`);
|
|
118
|
+
}
|
|
119
|
+
return issues;
|
|
120
120
|
}
|
|
121
|
-
|
|
122
121
|
/** Compatibility alias. */
|
|
123
122
|
export const validateTables = validateDataMatrixTables;
|