@sythos/js_barcode_universal 1.2.5 → 1.4.1

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.
Files changed (124) hide show
  1. package/AI_USAGE.md +33 -0
  2. package/LICENSE +11 -2
  3. package/NOTICE.md +10 -4
  4. package/README.md +601 -465
  5. package/bundle/sythos-barcode.esm.js +4669 -74
  6. package/bundle/sythos-barcode.js +4575 -72
  7. package/examples/create.html +1010 -730
  8. package/examples/read.html +1 -0
  9. package/licenses/README.md +40 -58
  10. package/licenses/aztec-code.license +15 -13
  11. package/licenses/aztec-rune.license +76 -0
  12. package/licenses/codabar.license +18 -15
  13. package/licenses/code-11.license +11 -9
  14. package/licenses/code-128.license +10 -8
  15. package/licenses/code-39.license +10 -8
  16. package/licenses/code-93.license +16 -14
  17. package/licenses/compact-pdf417.license +76 -0
  18. package/licenses/data-matrix.license +15 -13
  19. package/licenses/ean-13.license +10 -8
  20. package/licenses/ean-2.license +74 -0
  21. package/licenses/ean-5.license +74 -0
  22. package/licenses/ean-8.license +10 -8
  23. package/licenses/frameqr.license +84 -0
  24. package/licenses/gs1-128.license +10 -8
  25. package/licenses/gs1-databar.license +81 -0
  26. package/licenses/isbn.license +10 -8
  27. package/licenses/itf-14.license +10 -8
  28. package/licenses/itf.license +9 -7
  29. package/licenses/micro-qr.license +79 -0
  30. package/licenses/micropdf417.license +52 -67
  31. package/licenses/msi-plessey.license +13 -11
  32. package/licenses/pdf417.license +78 -37
  33. package/licenses/pharmacode.license +14 -12
  34. package/licenses/qr-code.license +9 -7
  35. package/licenses/rmqr.license +79 -0
  36. package/licenses/upc-a.license +12 -10
  37. package/licenses/upc-e.license +10 -8
  38. package/package.json +109 -88
  39. package/src/aztec/decoder.js +1 -0
  40. package/src/aztec/detector.js +1 -0
  41. package/src/aztec/encoder.js +1 -0
  42. package/src/aztec/high-level.js +1 -0
  43. package/src/aztec/index.js +1 -0
  44. package/src/aztec/tables.js +1 -0
  45. package/src/aztecrune/decoder.js +156 -0
  46. package/src/aztecrune/detector.js +167 -0
  47. package/src/aztecrune/encoder.js +122 -0
  48. package/src/aztecrune/index.js +50 -0
  49. package/src/aztecrune/tables.js +138 -0
  50. package/src/compactpdf417/decoder.js +129 -0
  51. package/src/compactpdf417/detector.js +140 -0
  52. package/src/compactpdf417/encoder.js +141 -0
  53. package/src/compactpdf417/index.js +22 -0
  54. package/src/compactpdf417/tables.js +174 -0
  55. package/src/core/bit-buffer.js +1 -0
  56. package/src/core/bit-matrix.js +1 -0
  57. package/src/core/errors.js +1 -0
  58. package/src/core/galois-field.js +1 -0
  59. package/src/core/index.js +1 -0
  60. package/src/core/reed-solomon.js +1 -0
  61. package/src/databar/codec.js +182 -0
  62. package/src/databar/decoder.js +115 -0
  63. package/src/databar/encoder.js +97 -0
  64. package/src/databar/gs1.js +178 -0
  65. package/src/databar/index.js +69 -0
  66. package/src/databar/patterns.js +144 -0
  67. package/src/databar/tables.js +125 -0
  68. package/src/datamatrix/decoder.js +263 -262
  69. package/src/datamatrix/detector.js +226 -225
  70. package/src/datamatrix/encoder.js +192 -191
  71. package/src/datamatrix/index.js +43 -42
  72. package/src/datamatrix/tables.js +124 -123
  73. package/src/frameqr/decoder.js +240 -0
  74. package/src/frameqr/detector.js +193 -0
  75. package/src/frameqr/encoder.js +157 -0
  76. package/src/frameqr/index.js +43 -0
  77. package/src/frameqr/tables.js +271 -0
  78. package/src/image/binarizer.js +1 -0
  79. package/src/image/grid-sampler.js +1 -0
  80. package/src/image/index.js +1 -0
  81. package/src/image/luminance.js +1 -0
  82. package/src/image/perspective.js +1 -0
  83. package/src/index.js +155 -2
  84. package/src/micropdf417/compaction.js +1 -0
  85. package/src/micropdf417/decoder.js +1 -0
  86. package/src/micropdf417/detector.js +1 -0
  87. package/src/micropdf417/encoder.js +1 -0
  88. package/src/micropdf417/error-correction.js +1 -0
  89. package/src/micropdf417/index.js +1 -0
  90. package/src/micropdf417/tables.js +1 -0
  91. package/src/microqr/decoder.js +246 -0
  92. package/src/microqr/detector.js +356 -0
  93. package/src/microqr/encoder.js +270 -0
  94. package/src/microqr/index.js +37 -0
  95. package/src/microqr/tables.js +317 -0
  96. package/src/oned/addons.js +421 -0
  97. package/src/oned/index.js +77 -59
  98. package/src/oned/patterns.js +1 -0
  99. package/src/oned/reader.js +1 -0
  100. package/src/oned/writers.js +1 -0
  101. package/src/pdf417/compaction.js +1 -0
  102. package/src/pdf417/decoder.js +1 -0
  103. package/src/pdf417/detector.js +1 -0
  104. package/src/pdf417/encoder.js +1 -0
  105. package/src/pdf417/error-correction.js +1 -0
  106. package/src/pdf417/index.js +5 -0
  107. package/src/pdf417/tables.js +1 -0
  108. package/src/qr/decoder.js +1 -0
  109. package/src/qr/detector.js +1 -0
  110. package/src/qr/encoder.js +1 -0
  111. package/src/qr/index.js +1 -0
  112. package/src/qr/tables.js +1 -0
  113. package/src/render/image-data.js +1 -0
  114. package/src/render/index.js +1 -0
  115. package/src/render/options.js +1 -0
  116. package/src/render/png.js +1 -0
  117. package/src/render/svg.js +1 -0
  118. package/src/render/webgl.js +1 -0
  119. package/src/render/webgpu.js +1 -0
  120. package/src/rmqr/decoder.js +102 -0
  121. package/src/rmqr/detector.js +91 -0
  122. package/src/rmqr/encoder.js +173 -0
  123. package/src/rmqr/index.js +38 -0
  124. package/src/rmqr/tables.js +155 -0
@@ -1,191 +1,192 @@
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
- /** Data Matrix ECC 200 encoder: ASCII/Base256, RS interleaving and Annex F placement. */
32
- import { BitMatrix } from '../core/bit-matrix.js';
33
- import { EncodeError } from '../core/errors.js';
34
- import { GF256_DM } from '../core/galois-field.js';
35
- import { rsEncode } from '../core/reed-solomon.js';
36
- import { symbolForDataCodewords } from './tables.js';
37
-
38
- function asciiCodewords(text) {
39
- const out = [];
40
- for (let i = 0; i < text.length;) {
41
- const a = text.charCodeAt(i);
42
- if (a > 255) throw new EncodeError('Data Matrix ASCII: characters must fit ISO-8859-1; use Base256 for UTF-8');
43
- if (i + 1 < text.length) {
44
- const b = text.charCodeAt(i + 1);
45
- if (a >= 48 && a <= 57 && b >= 48 && b <= 57) { out.push(130 + (a - 48) * 10 + b - 48); i += 2; continue; }
46
- }
47
- if (a <= 127) out.push(a + 1);
48
- else out.push(235, a - 127);
49
- i++;
50
- }
51
- return out;
52
- }
53
-
54
- function bytesFor(value) {
55
- if (value instanceof Uint8Array) return value;
56
- if (ArrayBuffer.isView(value)) return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
57
- if (typeof value !== 'string') throw new EncodeError('Data Matrix: value must be a string or byte array');
58
- return new TextEncoder().encode(value);
59
- }
60
-
61
- function randomize255(value, position) {
62
- const pseudo = (149 * position) % 255 + 1;
63
- return value + pseudo <= 255 ? value + pseudo : value + pseudo - 256;
64
- }
65
-
66
- function base256Codewords(value, prefixLength = 0) {
67
- const bytes = bytesFor(value);
68
- if (bytes.length > 1555) throw new EncodeError('Data Matrix Base256: payload exceeds ECC 200 capacity');
69
- const out = [231];
70
- if (bytes.length <= 249) out.push(bytes.length);
71
- else out.push(Math.floor(bytes.length / 250) + 249, bytes.length % 250);
72
- // Base256 randomization uses the absolute 1-based codeword position in the
73
- // symbol. A leading GS1 FNC1 therefore shifts every randomized codeword.
74
- for (let i = 1; i < out.length; i++) out[i] = randomize255(out[i], prefixLength + i + 1);
75
- for (const b of bytes) out.push(randomize255(b, prefixLength + out.length + 1));
76
- return out;
77
- }
78
-
79
- function pad(data, capacity) {
80
- const out = data.slice();
81
- if (out.length < capacity) out.push(129);
82
- while (out.length < capacity) {
83
- const position = out.length + 1;
84
- const pseudo = (149 * position) % 253 + 1;
85
- const v = 129 + pseudo;
86
- out.push(v <= 254 ? v : v - 254);
87
- }
88
- return out;
89
- }
90
-
91
- function interleave(data, symbol) {
92
- const blocks = symbol.dataBlockLengths.map((length) => ({ data: new Array(length), ecc: null }));
93
- let at = 0;
94
- const longest = Math.max(...symbol.dataBlockLengths);
95
- // Data codewords are dealt across the RS blocks by column. Splitting the
96
- // stream into consecutive chunks and then interleaving those chunks looks
97
- // self-consistent to a decoder doing the same inverse operation, but it is
98
- // not ECC 200's wire order once a symbol has multiple blocks.
99
- for (let i = 0; i < longest; i++) {
100
- for (const block of blocks) if (i < block.data.length) block.data[i] = data[at++];
101
- }
102
- for (const block of blocks) {
103
- // ECC 200 starts its generator roots at alpha^1 (generator base 1).
104
- block.ecc = rsEncode(block.data, symbol.eccPerBlock, GF256_DM, 1);
105
- }
106
-
107
- const out = [];
108
- for (let i = 0; i < longest; i++) for (const b of blocks) if (i < b.data.length) out.push(b.data[i]);
109
- // The 144x144 symbol has eight long and two short data blocks. Its parity
110
- // interleave begins with the first short block; deriving the rotation from
111
- // the declarative lengths keeps that exception out of a size-specific test.
112
- const eccOffset = blocks.findIndex((block) => block.data.length < longest);
113
- const rotation = eccOffset < 0 ? 0 : eccOffset;
114
- for (let i = 0; i < symbol.eccPerBlock; i++) {
115
- for (let b = 0; b < blocks.length; b++) out.push(blocks[(b + rotation) % blocks.length].ecc[i]);
116
- }
117
- return out;
118
- }
119
-
120
- function place(codewords, rows, cols) {
121
- const cells = new Int8Array(rows * cols).fill(-1);
122
- const bit = (row, col, pos, n) => {
123
- if (row < 0) { row += rows; col += 4 - ((rows + 4) % 8); }
124
- if (col < 0) { col += cols; row += 4 - ((cols + 4) % 8); }
125
- cells[row * cols + col] = (codewords[pos] >>> (8 - n)) & 1;
126
- };
127
- 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); };
128
- 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); };
129
- 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); };
130
- 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); };
131
- 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); };
132
- let row = 4, col = 0, pos = 0;
133
- do {
134
- if (row === rows && col === 0) corner1(pos++);
135
- if (row === rows - 2 && col === 0 && cols % 4 !== 0) corner2(pos++);
136
- if (row === rows - 2 && col === 0 && cols % 8 === 4) corner3(pos++);
137
- if (row === rows + 4 && col === 2 && cols % 8 === 0) corner4(pos++);
138
- do { if (row < rows && col >= 0 && cells[row * cols + col] < 0) utah(row, col, pos++); row -= 2; col += 2; } while (row >= 0 && col < cols);
139
- row += 1; col += 3;
140
- do { if (row >= 0 && col < cols && cells[row * cols + col] < 0) utah(row, col, pos++); row += 2; col -= 2; } while (row < rows && col >= 0);
141
- row += 3; col += 1;
142
- } while (row < rows || col < cols);
143
- if (cells[cells.length - 1] < 0) { cells[cells.length - 1] = 1; cells[cells.length - cols - 2] = 1; }
144
- if (pos !== codewords.length) throw new EncodeError(`Data Matrix: placement consumed ${pos} of ${codewords.length} codewords`);
145
- return cells;
146
- }
147
-
148
- function buildMatrix(codewords, symbol) {
149
- const regionCols = symbol.width / symbol.regionWidth;
150
- const regionRows = symbol.height / symbol.regionHeight;
151
- const dataWidth = symbol.dataRegionColumns;
152
- const dataHeight = symbol.dataRegionRows;
153
- const data = place(codewords, regionRows * dataHeight, regionCols * dataWidth);
154
- const matrix = new BitMatrix(symbol.width, symbol.height);
155
- for (let ry = 0; ry < regionRows; ry++) for (let rx = 0; rx < regionCols; rx++) {
156
- const x0 = rx * symbol.regionWidth, y0 = ry * symbol.regionHeight;
157
- for (let x = 0; x < symbol.regionWidth; x++) { if ((x & 1) === 0) matrix.set(x0 + x, y0); matrix.set(x0 + x, y0 + dataHeight + 1); }
158
- // The top and right timing borders are complementary: top-left is dark,
159
- // top-right is light, and the solid bottom-right corner remains dark.
160
- for (let y = 0; y < symbol.regionHeight; y++) { matrix.set(x0, y0 + y); if ((y & 1) === 1) matrix.set(x0 + dataWidth + 1, y0 + y); }
161
- for (let y = 0; y < dataHeight; y++) for (let x = 0; x < dataWidth; x++) if (data[(ry * dataHeight + y) * (regionCols * dataWidth) + rx * dataWidth + x]) matrix.set(x0 + 1 + x, y0 + 1 + y);
162
- }
163
- return matrix;
164
- }
165
-
166
- /** Encode a string (ASCII mode) or byte payload (Base256) into Data Matrix ECC 200. */
167
- export function encodeDataMatrix(value, options = {}) {
168
- const encoding = options.encoding ?? (value instanceof Uint8Array ? 'base256' : 'ascii');
169
- let raw;
170
- if (encoding === 'ascii') {
171
- if (typeof value !== 'string') throw new EncodeError('Data Matrix ASCII: value must be a string');
172
- raw = asciiCodewords(value);
173
- } else if (encoding === 'base256') raw = base256Codewords(value, options.gs1 === true ? 1 : 0);
174
- else throw new EncodeError(`Data Matrix: unsupported encoding "${encoding}"`);
175
- // GS1 DataMatrix is ECC 200 with FNC1 in the first codeword position.
176
- if (options.gs1 === true) raw.unshift(232);
177
- const shape = options.shape ?? 'any';
178
- if (shape !== 'any' && shape !== 'square' && shape !== 'rectangular') throw new EncodeError(`Data Matrix: invalid shape "${shape}"`);
179
- const symbol = symbolForDataCodewords(raw.length, shape);
180
- if (!symbol) throw new EncodeError(`Data Matrix: ${raw.length} data codewords do not fit an ECC 200 ${shape} symbol`);
181
- return buildMatrix(interleave(pad(raw, symbol.dataCodewords), symbol), symbol);
182
- }
183
-
184
- /** Encode already compacted ASCII/Base256 codewords, primarily for conformance tests. */
185
- export function encodeDataMatrixCodewords(codewords, options = {}) {
186
- if (!Array.isArray(codewords) && !(codewords instanceof Uint8Array)) throw new EncodeError('Data Matrix: codewords must be an array');
187
- for (const c of codewords) if (!Number.isInteger(c) || c < 0 || c > 255) throw new EncodeError('Data Matrix: codewords must be bytes');
188
- const symbol = symbolForDataCodewords(codewords.length, options.shape ?? 'any');
189
- if (!symbol) throw new EncodeError('Data Matrix: codewords do not fit ECC 200');
190
- return buildMatrix(interleave(pad(Array.from(codewords), symbol.dataCodewords), symbol), symbol);
191
- }
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-FileCopyrightText: 2026 Sythos (https://www.sythos.net)
27
+ * SPDX-License-Identifier: MIT
28
+ *
29
+ * Original work. No code from any other barcode implementation.
30
+ */
31
+
32
+ /** Data Matrix ECC 200 encoder: ASCII/Base256, RS interleaving and Annex F placement. */
33
+ import { BitMatrix } from '../core/bit-matrix.js';
34
+ import { EncodeError } from '../core/errors.js';
35
+ import { GF256_DM } from '../core/galois-field.js';
36
+ import { rsEncode } from '../core/reed-solomon.js';
37
+ import { symbolForDataCodewords } from './tables.js';
38
+
39
+ function asciiCodewords(text) {
40
+ const out = [];
41
+ for (let i = 0; i < text.length;) {
42
+ const a = text.charCodeAt(i);
43
+ if (a > 255) throw new EncodeError('Data Matrix ASCII: characters must fit ISO-8859-1; use Base256 for UTF-8');
44
+ if (i + 1 < text.length) {
45
+ const b = text.charCodeAt(i + 1);
46
+ if (a >= 48 && a <= 57 && b >= 48 && b <= 57) { out.push(130 + (a - 48) * 10 + b - 48); i += 2; continue; }
47
+ }
48
+ if (a <= 127) out.push(a + 1);
49
+ else out.push(235, a - 127);
50
+ i++;
51
+ }
52
+ return out;
53
+ }
54
+
55
+ function bytesFor(value) {
56
+ if (value instanceof Uint8Array) return value;
57
+ if (ArrayBuffer.isView(value)) return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
58
+ if (typeof value !== 'string') throw new EncodeError('Data Matrix: value must be a string or byte array');
59
+ return new TextEncoder().encode(value);
60
+ }
61
+
62
+ function randomize255(value, position) {
63
+ const pseudo = (149 * position) % 255 + 1;
64
+ return value + pseudo <= 255 ? value + pseudo : value + pseudo - 256;
65
+ }
66
+
67
+ function base256Codewords(value, prefixLength = 0) {
68
+ const bytes = bytesFor(value);
69
+ if (bytes.length > 1555) throw new EncodeError('Data Matrix Base256: payload exceeds ECC 200 capacity');
70
+ const out = [231];
71
+ if (bytes.length <= 249) out.push(bytes.length);
72
+ else out.push(Math.floor(bytes.length / 250) + 249, bytes.length % 250);
73
+ // Base256 randomization uses the absolute 1-based codeword position in the
74
+ // symbol. A leading GS1 FNC1 therefore shifts every randomized codeword.
75
+ for (let i = 1; i < out.length; i++) out[i] = randomize255(out[i], prefixLength + i + 1);
76
+ for (const b of bytes) out.push(randomize255(b, prefixLength + out.length + 1));
77
+ return out;
78
+ }
79
+
80
+ function pad(data, capacity) {
81
+ const out = data.slice();
82
+ if (out.length < capacity) out.push(129);
83
+ while (out.length < capacity) {
84
+ const position = out.length + 1;
85
+ const pseudo = (149 * position) % 253 + 1;
86
+ const v = 129 + pseudo;
87
+ out.push(v <= 254 ? v : v - 254);
88
+ }
89
+ return out;
90
+ }
91
+
92
+ function interleave(data, symbol) {
93
+ const blocks = symbol.dataBlockLengths.map((length) => ({ data: new Array(length), ecc: null }));
94
+ let at = 0;
95
+ const longest = Math.max(...symbol.dataBlockLengths);
96
+ // Data codewords are dealt across the RS blocks by column. Splitting the
97
+ // stream into consecutive chunks and then interleaving those chunks looks
98
+ // self-consistent to a decoder doing the same inverse operation, but it is
99
+ // not ECC 200's wire order once a symbol has multiple blocks.
100
+ for (let i = 0; i < longest; i++) {
101
+ for (const block of blocks) if (i < block.data.length) block.data[i] = data[at++];
102
+ }
103
+ for (const block of blocks) {
104
+ // ECC 200 starts its generator roots at alpha^1 (generator base 1).
105
+ block.ecc = rsEncode(block.data, symbol.eccPerBlock, GF256_DM, 1);
106
+ }
107
+
108
+ const out = [];
109
+ for (let i = 0; i < longest; i++) for (const b of blocks) if (i < b.data.length) out.push(b.data[i]);
110
+ // The 144x144 symbol has eight long and two short data blocks. Its parity
111
+ // interleave begins with the first short block; deriving the rotation from
112
+ // the declarative lengths keeps that exception out of a size-specific test.
113
+ const eccOffset = blocks.findIndex((block) => block.data.length < longest);
114
+ const rotation = eccOffset < 0 ? 0 : eccOffset;
115
+ for (let i = 0; i < symbol.eccPerBlock; i++) {
116
+ for (let b = 0; b < blocks.length; b++) out.push(blocks[(b + rotation) % blocks.length].ecc[i]);
117
+ }
118
+ return out;
119
+ }
120
+
121
+ function place(codewords, rows, cols) {
122
+ const cells = new Int8Array(rows * cols).fill(-1);
123
+ const bit = (row, col, pos, n) => {
124
+ if (row < 0) { row += rows; col += 4 - ((rows + 4) % 8); }
125
+ if (col < 0) { col += cols; row += 4 - ((cols + 4) % 8); }
126
+ cells[row * cols + col] = (codewords[pos] >>> (8 - n)) & 1;
127
+ };
128
+ 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); };
129
+ 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); };
130
+ 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); };
131
+ 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); };
132
+ 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); };
133
+ let row = 4, col = 0, pos = 0;
134
+ do {
135
+ if (row === rows && col === 0) corner1(pos++);
136
+ if (row === rows - 2 && col === 0 && cols % 4 !== 0) corner2(pos++);
137
+ if (row === rows - 2 && col === 0 && cols % 8 === 4) corner3(pos++);
138
+ if (row === rows + 4 && col === 2 && cols % 8 === 0) corner4(pos++);
139
+ do { if (row < rows && col >= 0 && cells[row * cols + col] < 0) utah(row, col, pos++); row -= 2; col += 2; } while (row >= 0 && col < cols);
140
+ row += 1; col += 3;
141
+ do { if (row >= 0 && col < cols && cells[row * cols + col] < 0) utah(row, col, pos++); row += 2; col -= 2; } while (row < rows && col >= 0);
142
+ row += 3; col += 1;
143
+ } while (row < rows || col < cols);
144
+ if (cells[cells.length - 1] < 0) { cells[cells.length - 1] = 1; cells[cells.length - cols - 2] = 1; }
145
+ if (pos !== codewords.length) throw new EncodeError(`Data Matrix: placement consumed ${pos} of ${codewords.length} codewords`);
146
+ return cells;
147
+ }
148
+
149
+ function buildMatrix(codewords, symbol) {
150
+ const regionCols = symbol.width / symbol.regionWidth;
151
+ const regionRows = symbol.height / symbol.regionHeight;
152
+ const dataWidth = symbol.dataRegionColumns;
153
+ const dataHeight = symbol.dataRegionRows;
154
+ const data = place(codewords, regionRows * dataHeight, regionCols * dataWidth);
155
+ const matrix = new BitMatrix(symbol.width, symbol.height);
156
+ for (let ry = 0; ry < regionRows; ry++) for (let rx = 0; rx < regionCols; rx++) {
157
+ const x0 = rx * symbol.regionWidth, y0 = ry * symbol.regionHeight;
158
+ for (let x = 0; x < symbol.regionWidth; x++) { if ((x & 1) === 0) matrix.set(x0 + x, y0); matrix.set(x0 + x, y0 + dataHeight + 1); }
159
+ // The top and right timing borders are complementary: top-left is dark,
160
+ // top-right is light, and the solid bottom-right corner remains dark.
161
+ for (let y = 0; y < symbol.regionHeight; y++) { matrix.set(x0, y0 + y); if ((y & 1) === 1) matrix.set(x0 + dataWidth + 1, y0 + y); }
162
+ for (let y = 0; y < dataHeight; y++) for (let x = 0; x < dataWidth; x++) if (data[(ry * dataHeight + y) * (regionCols * dataWidth) + rx * dataWidth + x]) matrix.set(x0 + 1 + x, y0 + 1 + y);
163
+ }
164
+ return matrix;
165
+ }
166
+
167
+ /** Encode a string (ASCII mode) or byte payload (Base256) into Data Matrix ECC 200. */
168
+ export function encodeDataMatrix(value, options = {}) {
169
+ const encoding = options.encoding ?? (value instanceof Uint8Array ? 'base256' : 'ascii');
170
+ let raw;
171
+ if (encoding === 'ascii') {
172
+ if (typeof value !== 'string') throw new EncodeError('Data Matrix ASCII: value must be a string');
173
+ raw = asciiCodewords(value);
174
+ } else if (encoding === 'base256') raw = base256Codewords(value, options.gs1 === true ? 1 : 0);
175
+ else throw new EncodeError(`Data Matrix: unsupported encoding "${encoding}"`);
176
+ // GS1 DataMatrix is ECC 200 with FNC1 in the first codeword position.
177
+ if (options.gs1 === true) raw.unshift(232);
178
+ const shape = options.shape ?? 'any';
179
+ if (shape !== 'any' && shape !== 'square' && shape !== 'rectangular') throw new EncodeError(`Data Matrix: invalid shape "${shape}"`);
180
+ const symbol = symbolForDataCodewords(raw.length, shape);
181
+ if (!symbol) throw new EncodeError(`Data Matrix: ${raw.length} data codewords do not fit an ECC 200 ${shape} symbol`);
182
+ return buildMatrix(interleave(pad(raw, symbol.dataCodewords), symbol), symbol);
183
+ }
184
+
185
+ /** Encode already compacted ASCII/Base256 codewords, primarily for conformance tests. */
186
+ export function encodeDataMatrixCodewords(codewords, options = {}) {
187
+ if (!Array.isArray(codewords) && !(codewords instanceof Uint8Array)) throw new EncodeError('Data Matrix: codewords must be an array');
188
+ for (const c of codewords) if (!Number.isInteger(c) || c < 0 || c > 255) throw new EncodeError('Data Matrix: codewords must be bytes');
189
+ const symbol = symbolForDataCodewords(codewords.length, options.shape ?? 'any');
190
+ if (!symbol) throw new EncodeError('Data Matrix: codewords do not fit ECC 200');
191
+ return buildMatrix(interleave(pad(Array.from(codewords), symbol.dataCodewords), symbol), symbol);
192
+ }
@@ -1,42 +1,43 @@
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
- /** Data Matrix ECC 200 entry points. @module datamatrix */
32
-
33
- export { encodeDataMatrix, encodeDataMatrixCodewords } from './encoder.js';
34
- export { decodeDataMatrix } from './decoder.js';
35
- export { detectDataMatrix, detectAndDecodeDataMatrix } from './detector.js';
36
- export {
37
- DATAMATRIX_SYMBOLS,
38
- SYMBOLS,
39
- symbolForDataCodewords,
40
- validateDataMatrixTables,
41
- validateTables,
42
- } from './tables.js';
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-FileCopyrightText: 2026 Sythos (https://www.sythos.net)
27
+ * SPDX-License-Identifier: MIT
28
+ *
29
+ * Original work. No code from any other barcode implementation.
30
+ */
31
+
32
+ /** Data Matrix ECC 200 entry points. @module datamatrix */
33
+
34
+ export { encodeDataMatrix, encodeDataMatrixCodewords } from './encoder.js';
35
+ export { decodeDataMatrix } from './decoder.js';
36
+ export { detectDataMatrix, detectAndDecodeDataMatrix } from './detector.js';
37
+ export {
38
+ DATAMATRIX_SYMBOLS,
39
+ SYMBOLS,
40
+ symbolForDataCodewords,
41
+ validateDataMatrixTables,
42
+ validateTables,
43
+ } from './tables.js';