@sythos/js_barcode_universal 1.3.1 → 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.
- package/AI_USAGE.md +33 -0
- package/LICENSE +4 -2
- package/NOTICE.md +8 -5
- package/README.md +74 -23
- package/bundle/sythos-barcode.esm.js +2313 -92
- package/bundle/sythos-barcode.js +2229 -88
- package/examples/create.html +2 -1
- package/examples/read.html +1 -0
- package/licenses/README.md +6 -1
- package/licenses/aztec-rune.license +76 -0
- package/licenses/compact-pdf417.license +76 -0
- package/licenses/ean-2.license +74 -0
- package/licenses/ean-5.license +74 -0
- package/licenses/frameqr.license +6 -6
- package/licenses/gs1-databar.license +81 -0
- package/package.json +16 -4
- package/src/aztec/decoder.js +1 -0
- package/src/aztec/detector.js +1 -0
- package/src/aztec/encoder.js +1 -0
- package/src/aztec/high-level.js +1 -0
- package/src/aztec/index.js +1 -0
- package/src/aztec/tables.js +1 -0
- package/src/aztecrune/decoder.js +156 -0
- package/src/aztecrune/detector.js +167 -0
- package/src/aztecrune/encoder.js +122 -0
- package/src/aztecrune/index.js +50 -0
- package/src/aztecrune/tables.js +138 -0
- package/src/compactpdf417/decoder.js +129 -0
- package/src/compactpdf417/detector.js +140 -0
- package/src/compactpdf417/encoder.js +141 -0
- package/src/compactpdf417/index.js +22 -0
- package/src/compactpdf417/tables.js +174 -0
- package/src/core/bit-buffer.js +1 -0
- package/src/core/bit-matrix.js +1 -0
- package/src/core/errors.js +1 -0
- package/src/core/galois-field.js +1 -0
- package/src/core/index.js +1 -0
- package/src/core/reed-solomon.js +1 -0
- package/src/databar/codec.js +182 -0
- package/src/databar/decoder.js +115 -0
- package/src/databar/encoder.js +97 -0
- package/src/databar/gs1.js +178 -0
- package/src/databar/index.js +69 -0
- package/src/databar/patterns.js +144 -0
- package/src/databar/tables.js +125 -0
- package/src/datamatrix/decoder.js +1 -0
- package/src/datamatrix/detector.js +1 -0
- package/src/datamatrix/encoder.js +1 -0
- package/src/datamatrix/index.js +1 -0
- package/src/datamatrix/tables.js +1 -0
- package/src/frameqr/decoder.js +16 -15
- package/src/frameqr/detector.js +3 -2
- package/src/frameqr/encoder.js +8 -7
- package/src/frameqr/index.js +1 -0
- package/src/frameqr/tables.js +11 -10
- package/src/image/binarizer.js +1 -0
- package/src/image/grid-sampler.js +1 -0
- package/src/image/index.js +1 -0
- package/src/image/luminance.js +1 -0
- package/src/image/perspective.js +1 -0
- package/src/index.js +72 -8
- package/src/micropdf417/compaction.js +1 -0
- package/src/micropdf417/decoder.js +1 -0
- package/src/micropdf417/detector.js +1 -0
- package/src/micropdf417/encoder.js +1 -0
- package/src/micropdf417/error-correction.js +1 -0
- package/src/micropdf417/index.js +1 -0
- package/src/micropdf417/tables.js +1 -0
- package/src/microqr/decoder.js +1 -0
- package/src/microqr/detector.js +1 -0
- package/src/microqr/encoder.js +1 -0
- package/src/microqr/index.js +1 -0
- package/src/microqr/tables.js +1 -0
- package/src/oned/addons.js +421 -0
- package/src/oned/index.js +18 -0
- package/src/oned/patterns.js +1 -0
- package/src/oned/reader.js +1 -0
- package/src/oned/writers.js +1 -0
- package/src/pdf417/compaction.js +1 -0
- package/src/pdf417/decoder.js +1 -0
- package/src/pdf417/detector.js +1 -0
- package/src/pdf417/encoder.js +1 -0
- package/src/pdf417/error-correction.js +1 -0
- package/src/pdf417/index.js +5 -0
- package/src/pdf417/tables.js +1 -0
- package/src/qr/decoder.js +1 -0
- package/src/qr/detector.js +1 -0
- package/src/qr/encoder.js +1 -0
- package/src/qr/index.js +1 -0
- package/src/qr/tables.js +1 -0
- package/src/render/image-data.js +1 -0
- package/src/render/index.js +1 -0
- package/src/render/options.js +1 -0
- package/src/render/png.js +1 -0
- package/src/render/svg.js +1 -0
- package/src/render/webgl.js +1 -0
- package/src/render/webgpu.js +1 -0
- package/src/rmqr/decoder.js +1 -0
- package/src/rmqr/detector.js +1 -0
- package/src/rmqr/encoder.js +1 -0
- package/src/rmqr/index.js +1 -0
- package/src/rmqr/tables.js +1 -0
|
@@ -0,0 +1,156 @@
|
|
|
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
|
+
/**
|
|
33
|
+
* Aztec Rune decoder.
|
|
34
|
+
*
|
|
35
|
+
* The input is a square 11x11 module matrix. Structural modules are checked
|
|
36
|
+
* before the seven masked GF(16) codewords are Reed-Solomon corrected. The
|
|
37
|
+
* decoder tries the four in-plane quarter turns and both polarities, allowing
|
|
38
|
+
* it to consume an oriented sampled image while still rejecting ordinary
|
|
39
|
+
* Aztec/QR matrices.
|
|
40
|
+
*
|
|
41
|
+
* @module aztecrune/decoder
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
import { FormatError } from '../core/errors.js';
|
|
45
|
+
import { rsDecode } from '../core/reed-solomon.js';
|
|
46
|
+
import {
|
|
47
|
+
AZTEC_RUNE_DATA_CODEWORDS,
|
|
48
|
+
AZTEC_RUNE_DATA_POSITIONS,
|
|
49
|
+
AZTEC_RUNE_ECC_CODEWORDS,
|
|
50
|
+
AZTEC_RUNE_MASK,
|
|
51
|
+
AZTEC_RUNE_SIZE,
|
|
52
|
+
aztecRuneField,
|
|
53
|
+
aztecRuneStructuralValue,
|
|
54
|
+
} from './tables.js';
|
|
55
|
+
|
|
56
|
+
/** @param {import('../core/bit-matrix.js').BitMatrix} source @param {number} turns */
|
|
57
|
+
function rotateClockwise(source, turns) {
|
|
58
|
+
let current = source.clone();
|
|
59
|
+
for (let turn = 0; turn < turns; turn++) {
|
|
60
|
+
const out = new source.constructor(current.height, current.width);
|
|
61
|
+
for (let y = 0; y < current.height; y++) for (let x = 0; x < current.width; x++) {
|
|
62
|
+
if (current.get(x, y)) out.set(current.height - 1 - y, x);
|
|
63
|
+
}
|
|
64
|
+
current = out;
|
|
65
|
+
}
|
|
66
|
+
return current;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** @param {import('../core/bit-matrix.js').BitMatrix} matrix */
|
|
70
|
+
function invert(matrix) {
|
|
71
|
+
const out = matrix.clone();
|
|
72
|
+
for (let y = 0; y < out.height; y++) for (let x = 0; x < out.width; x++) out.flip(x, y);
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @param {import('../core/bit-matrix.js').BitMatrix} matrix @param {boolean} inverted */
|
|
77
|
+
function structureMatches(matrix, inverted) {
|
|
78
|
+
for (let y = 0; y < AZTEC_RUNE_SIZE; y++) for (let x = 0; x < AZTEC_RUNE_SIZE; x++) {
|
|
79
|
+
const expected = aztecRuneStructuralValue(x, y);
|
|
80
|
+
if (expected === null) continue;
|
|
81
|
+
if (matrix.get(x, y) !== (inverted ? !expected : expected)) return false;
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** @param {import('../core/bit-matrix.js').BitMatrix} matrix @param {boolean} inverted */
|
|
87
|
+
function readCodewords(matrix, inverted) {
|
|
88
|
+
const words = new Array(2 + AZTEC_RUNE_ECC_CODEWORDS).fill(0);
|
|
89
|
+
for (let at = 0; at < AZTEC_RUNE_DATA_POSITIONS.length; at++) {
|
|
90
|
+
const [x, y] = AZTEC_RUNE_DATA_POSITIONS[at];
|
|
91
|
+
const bit = matrix.get(x, y) !== inverted;
|
|
92
|
+
words[at >>> 2] = (words[at >>> 2] << 1) | (bit ? 1 : 0);
|
|
93
|
+
}
|
|
94
|
+
return words.map((word) => word ^ AZTEC_RUNE_MASK);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** @param {number} value @param {boolean} inverted @param {number} rotation @param {number} corrections */
|
|
98
|
+
function result(value, inverted, rotation, corrections) {
|
|
99
|
+
return {
|
|
100
|
+
format: 'aztecrune',
|
|
101
|
+
value,
|
|
102
|
+
text: String(value).padStart(3, '0'),
|
|
103
|
+
bytes: Uint8Array.of(value),
|
|
104
|
+
dimension: AZTEC_RUNE_SIZE,
|
|
105
|
+
inverted,
|
|
106
|
+
rotation,
|
|
107
|
+
corrections,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Decode a square Aztec Rune matrix.
|
|
113
|
+
*
|
|
114
|
+
* @param {import('../core/bit-matrix.js').BitMatrix} matrix
|
|
115
|
+
* @param {{inverted?: boolean|'auto', rotation?: number|'auto'}} [options]
|
|
116
|
+
* @returns {{format:'aztecrune',value:number,text:string,bytes:Uint8Array,dimension:number,inverted:boolean,rotation:number,corrections:number}}
|
|
117
|
+
* @throws {FormatError} For non-Rune geometry, structure or uncorrectable data.
|
|
118
|
+
*/
|
|
119
|
+
export function decodeAztecRune(matrix, options = {}) {
|
|
120
|
+
if (!matrix || matrix.width !== AZTEC_RUNE_SIZE || matrix.height !== AZTEC_RUNE_SIZE) {
|
|
121
|
+
throw new FormatError(`Aztec Rune: expected an ${AZTEC_RUNE_SIZE}x${AZTEC_RUNE_SIZE} module matrix`);
|
|
122
|
+
}
|
|
123
|
+
const invertedModes = options.inverted === true ? [true] : options.inverted === false ? [false] : [false, true];
|
|
124
|
+
const rotations = Number.isInteger(options.rotation)
|
|
125
|
+
? [((options.rotation % 360) + 360) % 360]
|
|
126
|
+
: [0, 90, 180, 270];
|
|
127
|
+
|
|
128
|
+
let checksumError = null;
|
|
129
|
+
for (const requestedRotation of rotations) {
|
|
130
|
+
if (requestedRotation % 90 !== 0) continue;
|
|
131
|
+
// To canonicalize an input rotated clockwise by k degrees, rotate it
|
|
132
|
+
// counter-clockwise by k. `turns` is the clockwise operation we apply.
|
|
133
|
+
const turns = (4 - requestedRotation / 90) % 4;
|
|
134
|
+
const canonicalRotation = requestedRotation;
|
|
135
|
+
const rotated = turns === 0 ? matrix.clone() : rotateClockwise(matrix, turns);
|
|
136
|
+
for (const inverted of invertedModes) {
|
|
137
|
+
const canonical = inverted ? invert(rotated) : rotated;
|
|
138
|
+
if (!structureMatches(canonical, false)) continue;
|
|
139
|
+
const words = readCodewords(canonical, false);
|
|
140
|
+
try {
|
|
141
|
+
const corrections = rsDecode(words, AZTEC_RUNE_ECC_CODEWORDS, aztecRuneField(), 1);
|
|
142
|
+
const value = (words[0] << 4) | words[1];
|
|
143
|
+
return result(value, inverted, canonicalRotation, corrections);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
checksumError = error;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (checksumError) {
|
|
151
|
+
throw new FormatError(`Aztec Rune: Reed-Solomon check failed: ${checksumError.message}`);
|
|
152
|
+
}
|
|
153
|
+
throw new FormatError('Aztec Rune: structural pattern or orientation is invalid');
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export { readCodewords, structureMatches };
|
|
@@ -0,0 +1,167 @@
|
|
|
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
|
+
/**
|
|
33
|
+
* Detector for Aztec Rune symbols.
|
|
34
|
+
*
|
|
35
|
+
* Rune has a fixed 11x11 geometry and a distinctive five-ring bull's-eye. This
|
|
36
|
+
* detector targets binarized, module-aligned rasters (including an integer
|
|
37
|
+
* scale, quiet zone and quarter turns). Arbitrary perspective and severe blur
|
|
38
|
+
* remain detector-level limitations; callers needing those conditions should
|
|
39
|
+
* pass a rectified 11x11 matrix to decodeAztecRune.
|
|
40
|
+
*
|
|
41
|
+
* @module aztecrune/detector
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
import { NotFoundError } from '../core/errors.js';
|
|
45
|
+
import { BitMatrix } from '../core/bit-matrix.js';
|
|
46
|
+
import { decodeAztecRune } from './decoder.js';
|
|
47
|
+
import { AZTEC_RUNE_SIZE } from './tables.js';
|
|
48
|
+
|
|
49
|
+
/** @typedef {{x:number,y:number}} Point */
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Find connected components of a polarity. Components touching the image
|
|
53
|
+
* border are ignored when they exceed the plausible module area; this avoids
|
|
54
|
+
* returning the light background as a candidate while retaining an inverted
|
|
55
|
+
* Rune's isolated light centre module.
|
|
56
|
+
*/
|
|
57
|
+
function components(image, value) {
|
|
58
|
+
const width = image.width;
|
|
59
|
+
const height = image.height;
|
|
60
|
+
const seen = new Uint8Array(width * height);
|
|
61
|
+
const limit = Math.max(64, Math.floor(width * height * 0.04));
|
|
62
|
+
const found = [];
|
|
63
|
+
|
|
64
|
+
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
|
|
65
|
+
const start = y * width + x;
|
|
66
|
+
if (seen[start] || image.get(x, y) !== value) continue;
|
|
67
|
+
const queue = [[x, y]];
|
|
68
|
+
seen[start] = 1;
|
|
69
|
+
let head = 0;
|
|
70
|
+
let minX = x; let maxX = x; let minY = y; let maxY = y;
|
|
71
|
+
let count = 0;
|
|
72
|
+
let touchesBorder = false;
|
|
73
|
+
|
|
74
|
+
while (head < queue.length) {
|
|
75
|
+
const [px, py] = queue[head++];
|
|
76
|
+
count++;
|
|
77
|
+
minX = Math.min(minX, px); maxX = Math.max(maxX, px);
|
|
78
|
+
minY = Math.min(minY, py); maxY = Math.max(maxY, py);
|
|
79
|
+
if (px === 0 || py === 0 || px === width - 1 || py === height - 1) touchesBorder = true;
|
|
80
|
+
for (const [nx, ny] of [[px - 1, py], [px + 1, py], [px, py - 1], [px, py + 1]]) {
|
|
81
|
+
if (nx < 0 || ny < 0 || nx >= width || ny >= height) continue;
|
|
82
|
+
const at = ny * width + nx;
|
|
83
|
+
if (!seen[at] && image.get(nx, ny) === value) {
|
|
84
|
+
seen[at] = 1;
|
|
85
|
+
queue.push([nx, ny]);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const boxWidth = maxX - minX + 1;
|
|
91
|
+
const boxHeight = maxY - minY + 1;
|
|
92
|
+
if (count <= limit && !touchesBorder &&
|
|
93
|
+
Math.abs(boxWidth - boxHeight) <= Math.max(1, Math.ceil(Math.max(boxWidth, boxHeight) * 0.35)) &&
|
|
94
|
+
count >= boxWidth * boxHeight * 0.45) {
|
|
95
|
+
found.push({ x: (minX + maxX) / 2, y: (minY + maxY) / 2, width: boxWidth, height: boxHeight, count });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return found.sort((a, b) => b.count - a.count);
|
|
99
|
+
}
|
|
100
|
+
/** @param {import('../core/bit-matrix.js').BitMatrix} image @param {Point} center @param {number} pitch */
|
|
101
|
+
function sample(image, center, pitch) {
|
|
102
|
+
const matrix = new BitMatrix(AZTEC_RUNE_SIZE);
|
|
103
|
+
for (let y = 0; y < AZTEC_RUNE_SIZE; y++) for (let x = 0; x < AZTEC_RUNE_SIZE; x++) {
|
|
104
|
+
const px = Math.round(center.x + (x - 5) * pitch);
|
|
105
|
+
const py = Math.round(center.y + (y - 5) * pitch);
|
|
106
|
+
if (px >= 0 && py >= 0 && px < image.width && py < image.height && image.get(px, py)) matrix.set(x, y);
|
|
107
|
+
}
|
|
108
|
+
return matrix;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function corners(center, pitch) {
|
|
112
|
+
const half = AZTEC_RUNE_SIZE * pitch / 2;
|
|
113
|
+
return [
|
|
114
|
+
{ x: center.x - half, y: center.y - half },
|
|
115
|
+
{ x: center.x + half, y: center.y - half },
|
|
116
|
+
{ x: center.x + half, y: center.y + half },
|
|
117
|
+
{ x: center.x - half, y: center.y + half },
|
|
118
|
+
];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function sameCandidate(left, right) {
|
|
122
|
+
return Math.hypot(left.center.x - right.center.x, left.center.y - right.center.y) <= Math.max(left.moduleSize, right.moduleSize) * 2;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Detect the most prominent Aztec Rune in a binarized image.
|
|
127
|
+
*
|
|
128
|
+
* @param {import('../core/bit-matrix.js').BitMatrix} binaryImage
|
|
129
|
+
* @returns {{corners:Point[],dimension:11,moduleSize:number,matrix:BitMatrix,result:object}|null}
|
|
130
|
+
*/
|
|
131
|
+
export function detectAztecRune(binaryImage) {
|
|
132
|
+
if (!binaryImage || !binaryImage.width || !binaryImage.height) {
|
|
133
|
+
throw new NotFoundError('detectAztecRune: no image supplied');
|
|
134
|
+
}
|
|
135
|
+
const candidates = [];
|
|
136
|
+
for (const value of [true, false]) {
|
|
137
|
+
for (const component of components(binaryImage, value)) {
|
|
138
|
+
const pitch = (component.width + component.height) / 2;
|
|
139
|
+
if (pitch < 0.8) continue;
|
|
140
|
+
const matrix = sample(binaryImage, component, pitch);
|
|
141
|
+
let decoded;
|
|
142
|
+
try { decoded = decodeAztecRune(matrix); } catch { continue; }
|
|
143
|
+
const candidate = {
|
|
144
|
+
center: component,
|
|
145
|
+
corners: corners(component, pitch),
|
|
146
|
+
dimension: AZTEC_RUNE_SIZE,
|
|
147
|
+
moduleSize: pitch,
|
|
148
|
+
matrix,
|
|
149
|
+
result: decoded,
|
|
150
|
+
};
|
|
151
|
+
if (!candidates.some((entry) => sameCandidate(entry, candidate))) candidates.push(candidate);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
candidates.sort((a, b) => b.moduleSize - a.moduleSize);
|
|
155
|
+
const best = candidates[0];
|
|
156
|
+
if (!best) return null;
|
|
157
|
+
delete best.center;
|
|
158
|
+
return best;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Detect and decode one Aztec Rune, or return `null` when none is verified. */
|
|
162
|
+
export function detectAndDecodeAztecRune(binaryImage) {
|
|
163
|
+
let detection;
|
|
164
|
+
try { detection = detectAztecRune(binaryImage); } catch { return null; }
|
|
165
|
+
if (!detection) return null;
|
|
166
|
+
return { ...detection.result, corners: detection.corners, moduleSize: detection.moduleSize };
|
|
167
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
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
|
+
/**
|
|
33
|
+
* Aztec Rune encoder.
|
|
34
|
+
*
|
|
35
|
+
* A Rune carries exactly one byte. Numeric strings are accepted for convenient
|
|
36
|
+
* human-readable input and are normalized to the canonical three-digit text;
|
|
37
|
+
* binary callers can pass one byte in an ArrayBuffer or typed-array view.
|
|
38
|
+
*
|
|
39
|
+
* @module aztecrune/encoder
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
import { BitMatrix } from '../core/bit-matrix.js';
|
|
43
|
+
import { EncodeError } from '../core/errors.js';
|
|
44
|
+
import { rsEncode } from '../core/reed-solomon.js';
|
|
45
|
+
import {
|
|
46
|
+
AZTEC_RUNE_DATA_POSITIONS,
|
|
47
|
+
AZTEC_RUNE_ECC_CODEWORDS,
|
|
48
|
+
AZTEC_RUNE_MASK,
|
|
49
|
+
AZTEC_RUNE_SIZE,
|
|
50
|
+
aztecRuneField,
|
|
51
|
+
buildAztecRuneStructure,
|
|
52
|
+
} from './tables.js';
|
|
53
|
+
|
|
54
|
+
/** @param {unknown} value @returns {number} */
|
|
55
|
+
export function normalizeAztecRuneValue(value) {
|
|
56
|
+
if (typeof value === 'number') {
|
|
57
|
+
if (Number.isInteger(value) && value >= 0 && value <= 255) return value;
|
|
58
|
+
throw new EncodeError('Aztec Rune: numeric value must be an integer from 0 to 255');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (typeof value === 'string') {
|
|
62
|
+
if (!/^\d{1,3}$/.test(value)) throw new EncodeError('Aztec Rune: text input must contain one to three decimal digits');
|
|
63
|
+
const numeric = Number(value);
|
|
64
|
+
if (numeric > 255) throw new EncodeError('Aztec Rune: decimal value must be from 000 to 255');
|
|
65
|
+
return numeric;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let bytes = null;
|
|
69
|
+
if (value instanceof ArrayBuffer) bytes = new Uint8Array(value);
|
|
70
|
+
else if (ArrayBuffer.isView(value)) bytes = new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
71
|
+
if (bytes) {
|
|
72
|
+
if (bytes.length !== 1) throw new EncodeError('Aztec Rune: binary input must contain exactly one byte');
|
|
73
|
+
return bytes[0];
|
|
74
|
+
}
|
|
75
|
+
throw new EncodeError('Aztec Rune: expected a number, decimal string or one-byte binary value');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** @param {number} value @returns {number[]} */
|
|
79
|
+
function codewordsForValue(value) {
|
|
80
|
+
const data = [value >>> 4, value & 0x0f];
|
|
81
|
+
const parity = rsEncode(data, AZTEC_RUNE_ECC_CODEWORDS, aztecRuneField(), 1);
|
|
82
|
+
return data.concat(parity).map((word) => word ^ AZTEC_RUNE_MASK);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** @param {BitMatrix} matrix @param {number[]} codewords */
|
|
86
|
+
function writeDataRing(matrix, codewords) {
|
|
87
|
+
for (let word = 0; word < codewords.length; word++) {
|
|
88
|
+
for (let bit = 0; bit < 4; bit++) {
|
|
89
|
+
const at = word * 4 + bit;
|
|
90
|
+
const [x, y] = AZTEC_RUNE_DATA_POSITIONS[at];
|
|
91
|
+
matrix.setValue(x, y, ((codewords[word] >>> (3 - bit)) & 1) !== 0);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Encode one byte as an Aztec Rune.
|
|
98
|
+
*
|
|
99
|
+
* @param {number|string|ArrayBuffer|ArrayBufferView} value
|
|
100
|
+
* @param {{inverted?: boolean}} [options]
|
|
101
|
+
* @returns {import('../core/bit-matrix.js').BitMatrix & {
|
|
102
|
+
* format: 'aztecrune', value: number, inverted: boolean
|
|
103
|
+
* }}
|
|
104
|
+
*/
|
|
105
|
+
export function encodeAztecRune(value, options = {}) {
|
|
106
|
+
const numeric = normalizeAztecRuneValue(value);
|
|
107
|
+
if (options.inverted !== undefined && typeof options.inverted !== 'boolean') {
|
|
108
|
+
throw new EncodeError('Aztec Rune: inverted must be boolean');
|
|
109
|
+
}
|
|
110
|
+
const inverted = options.inverted === true;
|
|
111
|
+
const matrix = buildAztecRuneStructure();
|
|
112
|
+
writeDataRing(matrix, codewordsForValue(numeric));
|
|
113
|
+
if (inverted) {
|
|
114
|
+
for (let y = 0; y < AZTEC_RUNE_SIZE; y++) for (let x = 0; x < AZTEC_RUNE_SIZE; x++) matrix.flip(x, y);
|
|
115
|
+
}
|
|
116
|
+
matrix.format = 'aztecrune';
|
|
117
|
+
matrix.value = numeric;
|
|
118
|
+
matrix.inverted = inverted;
|
|
119
|
+
return matrix;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export { codewordsForValue };
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
/** Aztec Rune entry points. @module aztecrune */
|
|
33
|
+
|
|
34
|
+
export { encodeAztecRune, normalizeAztecRuneValue } from './encoder.js';
|
|
35
|
+
export { decodeAztecRune } from './decoder.js';
|
|
36
|
+
export { detectAztecRune, detectAndDecodeAztecRune } from './detector.js';
|
|
37
|
+
export {
|
|
38
|
+
AZTEC_RUNE_SIZE,
|
|
39
|
+
AZTEC_RUNE_DATA_BITS,
|
|
40
|
+
AZTEC_RUNE_WORD_SIZE,
|
|
41
|
+
AZTEC_RUNE_DATA_CODEWORDS,
|
|
42
|
+
AZTEC_RUNE_ECC_CODEWORDS,
|
|
43
|
+
AZTEC_RUNE_TOTAL_CODEWORDS,
|
|
44
|
+
AZTEC_RUNE_MASK,
|
|
45
|
+
AZTEC_RUNE_DATA_POSITIONS,
|
|
46
|
+
aztecRuneStructuralValue,
|
|
47
|
+
buildAztecRuneStructure,
|
|
48
|
+
aztecRuneField,
|
|
49
|
+
validateAztecRuneTables,
|
|
50
|
+
} from './tables.js';
|
|
@@ -0,0 +1,138 @@
|
|
|
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
|
+
/**
|
|
33
|
+
* Aztec Rune geometry.
|
|
34
|
+
*
|
|
35
|
+
* Aztec Rune is the 11x11, one-byte member of the Aztec family. Two 4-bit
|
|
36
|
+
* data words and five 4-bit Reed-Solomon check words are XORed with the fixed
|
|
37
|
+
* Aztec Rune mask and written clockwise on the outer data ring. The finder and
|
|
38
|
+
* corner orientation marks are independent of the value.
|
|
39
|
+
*
|
|
40
|
+
* The constants below are derived from the public Aztec Code specification and
|
|
41
|
+
* are kept separate from the general Aztec implementation so a normal Aztec
|
|
42
|
+
* decoder can never accidentally treat an 11x11 Rune as a regular symbol.
|
|
43
|
+
*
|
|
44
|
+
* @module aztecrune/tables
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
import { BitMatrix } from '../core/bit-matrix.js';
|
|
48
|
+
import { fieldForWordSize } from '../aztec/tables.js';
|
|
49
|
+
|
|
50
|
+
export const AZTEC_RUNE_SIZE = 11;
|
|
51
|
+
export const AZTEC_RUNE_DATA_BITS = 8;
|
|
52
|
+
export const AZTEC_RUNE_WORD_SIZE = 4;
|
|
53
|
+
export const AZTEC_RUNE_DATA_CODEWORDS = 2;
|
|
54
|
+
export const AZTEC_RUNE_ECC_CODEWORDS = 5;
|
|
55
|
+
export const AZTEC_RUNE_TOTAL_CODEWORDS = AZTEC_RUNE_DATA_CODEWORDS + AZTEC_RUNE_ECC_CODEWORDS;
|
|
56
|
+
export const AZTEC_RUNE_MASK = 0b1010;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Data-module coordinates in wire order: clockwise, starting at the top.
|
|
60
|
+
* Every side contributes seven modules, for 28 bits in total.
|
|
61
|
+
*/
|
|
62
|
+
export const AZTEC_RUNE_DATA_POSITIONS = Object.freeze([
|
|
63
|
+
...Array.from({ length: 7 }, (_, i) => [i + 2, 0]),
|
|
64
|
+
...Array.from({ length: 7 }, (_, i) => [10, i + 2]),
|
|
65
|
+
...Array.from({ length: 7 }, (_, i) => [8 - i, 10]),
|
|
66
|
+
...Array.from({ length: 7 }, (_, i) => [0, 8 - i]),
|
|
67
|
+
].map(([x, y]) => Object.freeze([x, y])));
|
|
68
|
+
|
|
69
|
+
const DATA_KEYS = new Set(AZTEC_RUNE_DATA_POSITIONS.map(([x, y]) => `${x},${y}`));
|
|
70
|
+
|
|
71
|
+
/** @param {number} x @param {number} y @returns {boolean} */
|
|
72
|
+
export function isAztecRuneDataPosition(x, y) {
|
|
73
|
+
return DATA_KEYS.has(`${x},${y}`);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Return the value of a structural module. Data positions return `null`.
|
|
77
|
+
* The bull's-eye has five square rings including its one-module centre; its
|
|
78
|
+
* even-radius rings are dark. The four corner groups are the orientation marks
|
|
79
|
+
* described by the Aztec Rune specification.
|
|
80
|
+
*
|
|
81
|
+
* @param {number} x @param {number} y
|
|
82
|
+
* @returns {boolean|null}
|
|
83
|
+
*/
|
|
84
|
+
export function aztecRuneStructuralValue(x, y) {
|
|
85
|
+
if (!Number.isInteger(x) || !Number.isInteger(y) || x < 0 || y < 0 || x >= AZTEC_RUNE_SIZE || y >= AZTEC_RUNE_SIZE) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
if (isAztecRuneDataPosition(x, y)) return null;
|
|
89
|
+
|
|
90
|
+
const distance = Math.max(Math.abs(x - 5), Math.abs(y - 5));
|
|
91
|
+
if (distance <= 4) return (distance & 1) === 0;
|
|
92
|
+
|
|
93
|
+
// Corner orientation groups, clockwise from the upper-left corner:
|
|
94
|
+
// three dark, white-dark-dark, dark-white-white, three white.
|
|
95
|
+
if ((x === 0 && y <= 1) || (y === 0 && x <= 1)) return true;
|
|
96
|
+
if ((x === 10 && y <= 1) || (y === 0 && x === 10)) return true;
|
|
97
|
+
if (x === 9 && y === 0) return false;
|
|
98
|
+
if (x === 10 && y === 10) return false;
|
|
99
|
+
if (y === 10 && x >= 0 && x <= 1) return false;
|
|
100
|
+
if (x === 0 && y >= 9) return false;
|
|
101
|
+
if (x === 10 && y === 9) return true;
|
|
102
|
+
|
|
103
|
+
// All non-data cells are covered by the bull's-eye or the four corners.
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Build only the fixed finder/orientation structure. */
|
|
108
|
+
export function buildAztecRuneStructure() {
|
|
109
|
+
const matrix = new BitMatrix(AZTEC_RUNE_SIZE);
|
|
110
|
+
for (let y = 0; y < AZTEC_RUNE_SIZE; y++) {
|
|
111
|
+
for (let x = 0; x < AZTEC_RUNE_SIZE; x++) {
|
|
112
|
+
const value = aztecRuneStructuralValue(x, y);
|
|
113
|
+
if (value === true) matrix.set(x, y);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return matrix;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Return the field used by Rune data/check words. */
|
|
120
|
+
export function aztecRuneField() {
|
|
121
|
+
return fieldForWordSize(AZTEC_RUNE_WORD_SIZE);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Validate the fixed Rune contract and coordinate layout. */
|
|
125
|
+
export function validateAztecRuneTables() {
|
|
126
|
+
const problems = [];
|
|
127
|
+
if (AZTEC_RUNE_SIZE !== 11) problems.push('Rune symbol size must be 11');
|
|
128
|
+
if (AZTEC_RUNE_TOTAL_CODEWORDS * AZTEC_RUNE_WORD_SIZE !== 28) problems.push('Rune outer ring must carry 28 bits');
|
|
129
|
+
if (AZTEC_RUNE_DATA_POSITIONS.length !== 28) problems.push('Rune data ring must contain 28 modules');
|
|
130
|
+
if (new Set(AZTEC_RUNE_DATA_POSITIONS.map(([x, y]) => `${x},${y}`)).size !== 28) problems.push('Rune data coordinates must be unique');
|
|
131
|
+
const structure = buildAztecRuneStructure();
|
|
132
|
+
for (let y = 0; y < AZTEC_RUNE_SIZE; y++) for (let x = 0; x < AZTEC_RUNE_SIZE; x++) {
|
|
133
|
+
if (isAztecRuneDataPosition(x, y)) continue;
|
|
134
|
+
const expected = aztecRuneStructuralValue(x, y) === true;
|
|
135
|
+
if (structure.get(x, y) !== expected) problems.push(`Rune structure mismatch at ${x},${y}`);
|
|
136
|
+
}
|
|
137
|
+
return problems;
|
|
138
|
+
}
|