@sythos/js_barcode_universal 1.1.0 → 1.3.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 (68) hide show
  1. package/LICENSE +24 -18
  2. package/NOTICE.md +27 -22
  3. package/README.md +596 -460
  4. package/bundle/sythos-barcode.esm.js +4606 -228
  5. package/bundle/sythos-barcode.js +4586 -228
  6. package/examples/create.html +1011 -731
  7. package/licenses/README.md +23 -18
  8. package/licenses/aztec-code.license +9 -7
  9. package/licenses/codabar.license +16 -13
  10. package/licenses/code-11.license +11 -9
  11. package/licenses/code-128.license +8 -6
  12. package/licenses/code-39.license +8 -6
  13. package/licenses/code-93.license +13 -11
  14. package/licenses/data-matrix.license +11 -9
  15. package/licenses/ean-13.license +7 -5
  16. package/licenses/ean-8.license +7 -5
  17. package/licenses/frameqr.license +84 -0
  18. package/licenses/gs1-128.license +7 -5
  19. package/licenses/isbn.license +8 -6
  20. package/licenses/itf-14.license +7 -5
  21. package/licenses/itf.license +7 -5
  22. package/licenses/micro-qr.license +79 -0
  23. package/licenses/micropdf417.license +81 -0
  24. package/licenses/msi-plessey.license +12 -10
  25. package/licenses/pdf417.license +78 -0
  26. package/licenses/pharmacode.license +13 -11
  27. package/licenses/qr-code.license +7 -5
  28. package/licenses/rmqr.license +79 -0
  29. package/licenses/upc-a.license +9 -7
  30. package/licenses/upc-e.license +7 -5
  31. package/package.json +104 -88
  32. package/src/core/reed-solomon.js +326 -312
  33. package/src/datamatrix/decoder.js +262 -262
  34. package/src/datamatrix/detector.js +225 -225
  35. package/src/datamatrix/encoder.js +191 -191
  36. package/src/datamatrix/index.js +42 -42
  37. package/src/datamatrix/tables.js +123 -123
  38. package/src/frameqr/decoder.js +239 -0
  39. package/src/frameqr/detector.js +192 -0
  40. package/src/frameqr/encoder.js +156 -0
  41. package/src/frameqr/index.js +42 -0
  42. package/src/frameqr/tables.js +270 -0
  43. package/src/index.js +166 -3
  44. package/src/micropdf417/compaction.js +116 -0
  45. package/src/micropdf417/decoder.js +183 -0
  46. package/src/micropdf417/detector.js +149 -0
  47. package/src/micropdf417/encoder.js +209 -0
  48. package/src/micropdf417/error-correction.js +55 -0
  49. package/src/micropdf417/index.js +49 -0
  50. package/src/micropdf417/tables.js +184 -0
  51. package/src/microqr/decoder.js +245 -0
  52. package/src/microqr/detector.js +355 -0
  53. package/src/microqr/encoder.js +269 -0
  54. package/src/microqr/index.js +36 -0
  55. package/src/microqr/tables.js +316 -0
  56. package/src/oned/index.js +59 -59
  57. package/src/pdf417/compaction.js +298 -0
  58. package/src/pdf417/decoder.js +75 -0
  59. package/src/pdf417/detector.js +468 -0
  60. package/src/pdf417/encoder.js +91 -0
  61. package/src/pdf417/error-correction.js +47 -0
  62. package/src/pdf417/index.js +6 -0
  63. package/src/pdf417/tables.js +317 -0
  64. package/src/rmqr/decoder.js +101 -0
  65. package/src/rmqr/detector.js +90 -0
  66. package/src/rmqr/encoder.js +172 -0
  67. package/src/rmqr/index.js +37 -0
  68. package/src/rmqr/tables.js +154 -0
@@ -1,123 +1,123 @@
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
- /**
32
- * Data Matrix ECC 200 symbol parameters.
33
- *
34
- * Width and height include finder borders. `regionWidth` and `regionHeight`
35
- * describe the usable modules inside one data region. The last three columns
36
- * make the Reed-Solomon block split explicit instead of hiding the 144x144
37
- * exception in encoder control flow.
38
- *
39
- * @module datamatrix/tables
40
- */
41
-
42
- function symbol(width, height, dataRegionWidth, dataRegionHeight, dataCodewords, errorCodewords, dataBlockLengths) {
43
- const blockCount = dataBlockLengths.length;
44
- return Object.freeze({
45
- width, height, rows: height, columns: width,
46
- // Region dimensions include their one-module finder border on each side;
47
- // dataRegion* expose the inner placement lattice explicitly.
48
- regionWidth: dataRegionWidth + 2, regionHeight: dataRegionHeight + 2,
49
- dataRegionWidth, dataRegionHeight,
50
- dataRegionRows: dataRegionHeight, dataRegionColumns: dataRegionWidth,
51
- dataCodewords, errorCodewords, blockCount,
52
- eccPerBlock: errorCodewords / blockCount,
53
- dataBlockLengths: Object.freeze(dataBlockLengths),
54
- });
55
- }
56
-
57
- /** Classic ISO/IEC 16022 ECC 200 symbols; DMRE is deliberately excluded. */
58
- export const DATAMATRIX_SYMBOLS = Object.freeze([
59
- symbol(10, 10, 8, 8, 3, 5, [3]),
60
- symbol(12, 12, 10, 10, 5, 7, [5]),
61
- symbol(14, 14, 12, 12, 8, 10, [8]),
62
- symbol(16, 16, 14, 14, 12, 12, [12]),
63
- symbol(18, 18, 16, 16, 18, 14, [18]),
64
- symbol(20, 20, 18, 18, 22, 18, [22]),
65
- symbol(22, 22, 20, 20, 30, 20, [30]),
66
- symbol(24, 24, 22, 22, 36, 24, [36]),
67
- symbol(26, 26, 24, 24, 44, 28, [44]),
68
- symbol(32, 32, 14, 14, 62, 36, [62]),
69
- symbol(36, 36, 16, 16, 86, 42, [86]),
70
- symbol(40, 40, 18, 18, 114, 48, [114]),
71
- symbol(44, 44, 20, 20, 144, 56, [144]),
72
- symbol(48, 48, 22, 22, 174, 68, [174]),
73
- symbol(52, 52, 24, 24, 204, 84, [102, 102]),
74
- symbol(64, 64, 14, 14, 280, 112, [140, 140]),
75
- symbol(72, 72, 16, 16, 368, 144, [92, 92, 92, 92]),
76
- symbol(80, 80, 18, 18, 456, 192, [114, 114, 114, 114]),
77
- symbol(88, 88, 20, 20, 576, 224, [144, 144, 144, 144]),
78
- symbol(96, 96, 22, 22, 696, 272, [174, 174, 174, 174]),
79
- symbol(104, 104, 24, 24, 816, 336, [136, 136, 136, 136, 136, 136]),
80
- symbol(120, 120, 18, 18, 1050, 408, [175, 175, 175, 175, 175, 175]),
81
- symbol(132, 132, 20, 20, 1304, 496, [163, 163, 163, 163, 163, 163, 163, 163]),
82
- symbol(144, 144, 22, 22, 1558, 620, [156, 156, 156, 156, 156, 156, 156, 156, 155, 155]),
83
- symbol(18, 8, 16, 6, 5, 7, [5]),
84
- symbol(32, 8, 14, 6, 10, 11, [10]),
85
- symbol(26, 12, 24, 10, 16, 14, [16]),
86
- symbol(36, 12, 16, 10, 22, 18, [22]),
87
- symbol(36, 16, 16, 14, 32, 24, [32]),
88
- symbol(48, 16, 22, 14, 49, 28, [49]),
89
- ]);
90
-
91
- /** Compatibility alias. */
92
- export const SYMBOLS = DATAMATRIX_SYMBOLS;
93
-
94
- /** Return the smallest permitted symbol that holds `count` data codewords. */
95
- export function symbolForDataCodewords(count, shape = 'any') {
96
- for (const s of DATAMATRIX_SYMBOLS) {
97
- const rectangular = s.width !== s.height;
98
- if ((shape === 'square' && rectangular) || (shape === 'rectangular' && !rectangular)) continue;
99
- if (count <= s.dataCodewords) return s;
100
- }
101
- throw new RangeError(`Data Matrix: ${count} data codewords do not fit an ECC 200 ${shape} symbol`);
102
- }
103
-
104
- /** Check redundant geometry and block identities in the static table. */
105
- export function validateDataMatrixTables() {
106
- const issues = [];
107
- for (const s of DATAMATRIX_SYMBOLS) {
108
- const regionsX = s.width / s.regionWidth;
109
- const regionsY = s.height / s.regionHeight;
110
- if (!Number.isInteger(regionsX) || !Number.isInteger(regionsY)) issues.push(`${s.width}x${s.height}: non-integral regions`);
111
- const modules = regionsX * regionsY * s.dataRegionWidth * s.dataRegionHeight;
112
- // Annex F reserves four terminal modules on a few lattice dimensions.
113
- // They are set to dark after codeword placement and do not carry data.
114
- const unused = modules - (s.dataCodewords + s.errorCodewords) * 8;
115
- if (unused !== 0 && unused !== 4) issues.push(`${s.width}x${s.height}: geometry/codeword mismatch`);
116
- if (s.dataBlockLengths.reduce((a, b) => a + b, 0) !== s.dataCodewords) issues.push(`${s.width}x${s.height}: data block mismatch`);
117
- if (s.eccPerBlock * s.blockCount !== s.errorCodewords) issues.push(`${s.width}x${s.height}: ecc block mismatch`);
118
- }
119
- return issues;
120
- }
121
-
122
- /** Compatibility alias. */
123
- export const validateTables = validateDataMatrixTables;
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
+ /**
32
+ * Data Matrix ECC 200 symbol parameters.
33
+ *
34
+ * Width and height include finder borders. `regionWidth` and `regionHeight`
35
+ * describe the usable modules inside one data region. The last three columns
36
+ * make the Reed-Solomon block split explicit instead of hiding the 144x144
37
+ * exception in encoder control flow.
38
+ *
39
+ * @module datamatrix/tables
40
+ */
41
+
42
+ function symbol(width, height, dataRegionWidth, dataRegionHeight, dataCodewords, errorCodewords, dataBlockLengths) {
43
+ const blockCount = dataBlockLengths.length;
44
+ return Object.freeze({
45
+ width, height, rows: height, columns: width,
46
+ // Region dimensions include their one-module finder border on each side;
47
+ // dataRegion* expose the inner placement lattice explicitly.
48
+ regionWidth: dataRegionWidth + 2, regionHeight: dataRegionHeight + 2,
49
+ dataRegionWidth, dataRegionHeight,
50
+ dataRegionRows: dataRegionHeight, dataRegionColumns: dataRegionWidth,
51
+ dataCodewords, errorCodewords, blockCount,
52
+ eccPerBlock: errorCodewords / blockCount,
53
+ dataBlockLengths: Object.freeze(dataBlockLengths),
54
+ });
55
+ }
56
+
57
+ /** Classic ISO/IEC 16022 ECC 200 symbols; DMRE is deliberately excluded. */
58
+ export const DATAMATRIX_SYMBOLS = Object.freeze([
59
+ symbol(10, 10, 8, 8, 3, 5, [3]),
60
+ symbol(12, 12, 10, 10, 5, 7, [5]),
61
+ symbol(14, 14, 12, 12, 8, 10, [8]),
62
+ symbol(16, 16, 14, 14, 12, 12, [12]),
63
+ symbol(18, 18, 16, 16, 18, 14, [18]),
64
+ symbol(20, 20, 18, 18, 22, 18, [22]),
65
+ symbol(22, 22, 20, 20, 30, 20, [30]),
66
+ symbol(24, 24, 22, 22, 36, 24, [36]),
67
+ symbol(26, 26, 24, 24, 44, 28, [44]),
68
+ symbol(32, 32, 14, 14, 62, 36, [62]),
69
+ symbol(36, 36, 16, 16, 86, 42, [86]),
70
+ symbol(40, 40, 18, 18, 114, 48, [114]),
71
+ symbol(44, 44, 20, 20, 144, 56, [144]),
72
+ symbol(48, 48, 22, 22, 174, 68, [174]),
73
+ symbol(52, 52, 24, 24, 204, 84, [102, 102]),
74
+ symbol(64, 64, 14, 14, 280, 112, [140, 140]),
75
+ symbol(72, 72, 16, 16, 368, 144, [92, 92, 92, 92]),
76
+ symbol(80, 80, 18, 18, 456, 192, [114, 114, 114, 114]),
77
+ symbol(88, 88, 20, 20, 576, 224, [144, 144, 144, 144]),
78
+ symbol(96, 96, 22, 22, 696, 272, [174, 174, 174, 174]),
79
+ symbol(104, 104, 24, 24, 816, 336, [136, 136, 136, 136, 136, 136]),
80
+ symbol(120, 120, 18, 18, 1050, 408, [175, 175, 175, 175, 175, 175]),
81
+ symbol(132, 132, 20, 20, 1304, 496, [163, 163, 163, 163, 163, 163, 163, 163]),
82
+ symbol(144, 144, 22, 22, 1558, 620, [156, 156, 156, 156, 156, 156, 156, 156, 155, 155]),
83
+ symbol(18, 8, 16, 6, 5, 7, [5]),
84
+ symbol(32, 8, 14, 6, 10, 11, [10]),
85
+ symbol(26, 12, 24, 10, 16, 14, [16]),
86
+ symbol(36, 12, 16, 10, 22, 18, [22]),
87
+ symbol(36, 16, 16, 14, 32, 24, [32]),
88
+ symbol(48, 16, 22, 14, 49, 28, [49]),
89
+ ]);
90
+
91
+ /** Compatibility alias. */
92
+ export const SYMBOLS = DATAMATRIX_SYMBOLS;
93
+
94
+ /** Return the smallest permitted symbol that holds `count` data codewords. */
95
+ export function symbolForDataCodewords(count, shape = 'any') {
96
+ for (const s of DATAMATRIX_SYMBOLS) {
97
+ const rectangular = s.width !== s.height;
98
+ if ((shape === 'square' && rectangular) || (shape === 'rectangular' && !rectangular)) continue;
99
+ if (count <= s.dataCodewords) return s;
100
+ }
101
+ throw new RangeError(`Data Matrix: ${count} data codewords do not fit an ECC 200 ${shape} symbol`);
102
+ }
103
+
104
+ /** Check redundant geometry and block identities in the static table. */
105
+ export function validateDataMatrixTables() {
106
+ const issues = [];
107
+ for (const s of DATAMATRIX_SYMBOLS) {
108
+ const regionsX = s.width / s.regionWidth;
109
+ const regionsY = s.height / s.regionHeight;
110
+ if (!Number.isInteger(regionsX) || !Number.isInteger(regionsY)) issues.push(`${s.width}x${s.height}: non-integral regions`);
111
+ const modules = regionsX * regionsY * s.dataRegionWidth * s.dataRegionHeight;
112
+ // Annex F reserves four terminal modules on a few lattice dimensions.
113
+ // They are set to dark after codeword placement and do not carry data.
114
+ const unused = modules - (s.dataCodewords + s.errorCodewords) * 8;
115
+ if (unused !== 0 && unused !== 4) issues.push(`${s.width}x${s.height}: geometry/codeword mismatch`);
116
+ if (s.dataBlockLengths.reduce((a, b) => a + b, 0) !== s.dataCodewords) issues.push(`${s.width}x${s.height}: data block mismatch`);
117
+ if (s.eccPerBlock * s.blockCount !== s.errorCodewords) issues.push(`${s.width}x${s.height}: ecc block mismatch`);
118
+ }
119
+ return issues;
120
+ }
121
+
122
+ /** Compatibility alias. */
123
+ export const validateTables = validateDataMatrixTables;
@@ -0,0 +1,239 @@
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
+ /**
32
+ * Decoder for the Sythos Canvas QR profile.
33
+ *
34
+ * This is intentionally not a DENSO FrameQR decoder. The profile is a QR
35
+ * symbol with a bounded artwork reservation and an explicit `frameqr` marker.
36
+ * A normal QR matrix is rejected unless a detector (or another trusted
37
+ * caller) supplies the profile and explicitly opts in to an unmarked matrix.
38
+ *
39
+ * The QR decoder is deliberately kept as the single payload decoder. It can
40
+ * repair the bounded modules removed for the canvas because the encoder fixes
41
+ * error correction to H and validates the reservation before clearing it.
42
+ *
43
+ * @module frameqr/decoder
44
+ */
45
+
46
+ import { FormatError } from '../core/errors.js';
47
+ import { decodeQR } from '../qr/decoder.js';
48
+ import {
49
+ FRAMEQR_PROFILE,
50
+ normalizeCanvasSpec,
51
+ validateCanvasSpec,
52
+ analyzeCanvasDamage,
53
+ } from './tables.js';
54
+
55
+ /** @param {unknown} value @returns {boolean} */
56
+ function isObject(value) {
57
+ return value !== null && typeof value === 'object';
58
+ }
59
+
60
+ /**
61
+ * Return the profile marker attached by the encoder. Older callers sometimes
62
+ * use camel case, so accepting it here is harmless while the emitted marker
63
+ * remains the canonical `frameqr` property.
64
+ *
65
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
66
+ * @returns {object|null}
67
+ */
68
+ function markerOf(matrix) {
69
+ const marker = matrix && (matrix.frameqr ?? matrix.frameQR);
70
+ return isObject(marker) ? marker : null;
71
+ }
72
+
73
+ /** @param {unknown} profile @returns {boolean} */
74
+ function isExpectedProfile(profile) {
75
+ if (profile === FRAMEQR_PROFILE.id) return true;
76
+ return isObject(profile) && profile.id === FRAMEQR_PROFILE.id;
77
+ }
78
+
79
+ /**
80
+ * Normalize and validate the canvas metadata. Validation functions in the
81
+ * table module throw on invalid input; a few consumers also return `false` or
82
+ * a problem list, so those forms are handled defensively here.
83
+ *
84
+ * @param {number} symbolSize
85
+ * @param {number} version
86
+ * @param {object} canvas
87
+ * @returns {{canvas: object, damage: object}}
88
+ */
89
+ function validateCanvas(symbolSize, version, canvas) {
90
+ if (!isObject(canvas)) throw new FormatError('Sythos Canvas QR: canvas metadata is missing');
91
+
92
+ let normalized;
93
+ try {
94
+ normalized = normalizeCanvasSpec(symbolSize, canvas);
95
+ const validation = validateCanvasSpec(version, normalized);
96
+ if (
97
+ validation === false ||
98
+ (Array.isArray(validation) && validation.length > 0) ||
99
+ (isObject(validation) && validation.valid === false) ||
100
+ (isObject(validation) && validation.safe === false)
101
+ ) {
102
+ throw new Error('canvas geometry is outside the profile limits');
103
+ }
104
+ } catch (error) {
105
+ if (error instanceof FormatError) throw error;
106
+ throw new FormatError(`Sythos Canvas QR: invalid canvas metadata: ${error.message}`);
107
+ }
108
+
109
+ let damage;
110
+ try {
111
+ damage = analyzeCanvasDamage(version, normalized);
112
+ } catch (error) {
113
+ throw new FormatError(`Sythos Canvas QR: cannot analyse canvas damage: ${error.message}`);
114
+ }
115
+ return { canvas: normalized, damage };
116
+ }
117
+
118
+ /**
119
+ * Resolve the profile marker and canvas from the matrix/options pair.
120
+ *
121
+ * `allowUnmarked` is deliberately opt-in. It exists for a detector that has
122
+ * already verified the canvas signature after sampling a photograph; it is
123
+ * not enabled by the public default and therefore cannot silently relabel an
124
+ * ordinary QR Code as FrameQR.
125
+ *
126
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
127
+ * @param {object} options
128
+ * @returns {{profile: string, canvas: object, damage: object, certified: false}}
129
+ */
130
+ function resolveProfile(matrix, options) {
131
+ const marker = markerOf(matrix);
132
+ const suppliedProfile = options.profile;
133
+ const markerProfile = marker ? marker.profile : undefined;
134
+ if (markerProfile !== undefined && suppliedProfile !== undefined) {
135
+ const markerId = isObject(markerProfile) ? markerProfile.id : markerProfile;
136
+ const suppliedId = isObject(suppliedProfile) ? suppliedProfile.id : suppliedProfile;
137
+ if (markerId !== suppliedId) {
138
+ throw new FormatError('Sythos Canvas QR: matrix and requested profile markers disagree');
139
+ }
140
+ }
141
+ const profile = markerProfile ?? suppliedProfile;
142
+
143
+ if (!isExpectedProfile(profile)) {
144
+ throw new FormatError(
145
+ 'Sythos Canvas QR: profile marker is missing or is not the Sythos Canvas QR profile'
146
+ );
147
+ }
148
+ if (marker && marker.certified === true) {
149
+ throw new FormatError(
150
+ 'Sythos Canvas QR: certified FrameQR input is not supported by this profile decoder'
151
+ );
152
+ }
153
+ if (!marker && !options.allowUnmarked) {
154
+ throw new FormatError(
155
+ 'Sythos Canvas QR: unmarked QR matrix rejected; provide a verified profile marker'
156
+ );
157
+ }
158
+
159
+ const markerCanvas = marker ? marker.canvas : undefined;
160
+ const canvas = options.canvas ?? markerCanvas;
161
+ const version = (matrix.width - 17) / 4;
162
+ if (!Number.isInteger(version) || version < 1 || version > 40) {
163
+ throw new FormatError(`Sythos Canvas QR: invalid QR symbol size ${matrix.width}`);
164
+ }
165
+ const checked = validateCanvas(matrix.width, version, canvas);
166
+ if (markerCanvas && options.canvas) {
167
+ let marked;
168
+ try {
169
+ marked = normalizeCanvasSpec(matrix.width, markerCanvas);
170
+ } catch (error) {
171
+ throw new FormatError(`Sythos Canvas QR: invalid marker canvas: ${error.message}`);
172
+ }
173
+ const fields = ['shape', 'centerX', 'centerY', 'width', 'height', 'angle'];
174
+ if (fields.some((field) => marked[field] !== checked.canvas[field])) {
175
+ throw new FormatError('Sythos Canvas QR: matrix and requested canvas metadata disagree');
176
+ }
177
+ }
178
+ return {
179
+ profile: FRAMEQR_PROFILE.id,
180
+ canvas: checked.canvas,
181
+ damage: checked.damage,
182
+ certified: false,
183
+ };
184
+ }
185
+
186
+ /**
187
+ * Decode a Sythos Canvas QR matrix.
188
+ *
189
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
190
+ * Square QR modules, normally returned by `encodeFrameQR` or a FrameQR
191
+ * detector. The encoder's `frameqr` metadata is required by default.
192
+ * @param {object} [options]
193
+ * @param {object} [options.canvas] Explicit canvas metadata for a sampled
194
+ * matrix when the source marker was not preserved.
195
+ * @param {string|object} [options.profile] Expected profile identifier.
196
+ * @param {boolean} [options.allowUnmarked=false] Explicit detector opt-in for
197
+ * a matrix whose marker was lost during image sampling.
198
+ * @returns {import('../qr/decoder.js').DecodeResult & {
199
+ * format: 'frameqr', profile: string, certified: false,
200
+ * frame: object, canvas: object, canvasDamage: object
201
+ * }}
202
+ * @throws {FormatError} If the profile marker/canvas is invalid or the input
203
+ * is an ordinary QR Code.
204
+ */
205
+ export function decodeFrameQR(matrix, options = {}) {
206
+ if (!matrix || !Number.isInteger(matrix.width) || !Number.isInteger(matrix.height)) {
207
+ throw new FormatError('Sythos Canvas QR: no matrix supplied');
208
+ }
209
+ if (matrix.width !== matrix.height) {
210
+ throw new FormatError(
211
+ `Sythos Canvas QR: symbol must be square, got ${matrix.width}x${matrix.height}`
212
+ );
213
+ }
214
+
215
+ const profile = resolveProfile(matrix, options);
216
+ let decoded;
217
+ try {
218
+ decoded = decodeQR(matrix);
219
+ } catch (error) {
220
+ // Preserve the original QR/RS error when possible, but give callers a
221
+ // profile-specific context without exposing implementation details.
222
+ if (error instanceof FormatError) {
223
+ throw new FormatError(`Sythos Canvas QR: payload is not recoverable: ${error.message}`);
224
+ }
225
+ throw error;
226
+ }
227
+
228
+ return {
229
+ ...decoded,
230
+ format: 'frameqr',
231
+ profile: profile.profile,
232
+ certified: profile.certified,
233
+ frame: profile.canvas,
234
+ canvas: profile.canvas,
235
+ canvasDamage: profile.damage,
236
+ };
237
+ }
238
+
239
+ export { isExpectedProfile };
@@ -0,0 +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
+ /**
32
+ * Detector for the non-certified Sythos Canvas QR profile.
33
+ *
34
+ * The profile deliberately reuses QR Model 2 geometry. Finder localisation and
35
+ * projective sampling therefore use the QR detector; the additional profile
36
+ * check is the light canvas signature. A sampled image cannot carry the
37
+ * encoder's in-memory marker, so the detector reconstructs the expected
38
+ * metadata, verifies that every reserved canvas module is light, and only then
39
+ * opts into the profile decoder. This rejects ordinary QR symbols whose centre
40
+ * merely happens to contain a plausible payload.
41
+ *
42
+ * Detection is verified for clean binarized rasters, integer scaling, quiet
43
+ * zones, and in-plane quarter turns. Arbitrary photographic perspective is not
44
+ * claimed by this module.
45
+ *
46
+ * @module frameqr/detector
47
+ */
48
+
49
+ import { NotFoundError } from '../core/errors.js';
50
+ import { sampleQuad } from '../image/grid-sampler.js';
51
+ import { detectQR } from '../qr/detector.js';
52
+ import { decodeFrameQR } from './decoder.js';
53
+ import {
54
+ FRAMEQR_PROFILE,
55
+ canvasModules,
56
+ normalizeCanvasSpec,
57
+ } from './tables.js';
58
+
59
+ /** @typedef {{x:number, y:number}} Point */
60
+
61
+ /** @typedef {object} FrameQRDetection
62
+ * @property {Point[]} corners Outer corners in reading order.
63
+ * @property {number} dimension QR modules per side.
64
+ * @property {number} version QR Model 2 version.
65
+ * @property {number} moduleSize Estimated pixels per module.
66
+ * @property {number} rotation Clockwise in-plane orientation in degrees.
67
+ * @property {BitMatrix} matrix Rectified profile matrix.
68
+ * @property {object} canvas Normalized canvas specification.
69
+ * @property {string} profile Profile identifier.
70
+ * @property {false} certified Always false for this implementation.
71
+ */
72
+
73
+ function orientationDegrees(corners) {
74
+ const [tl, tr] = corners;
75
+ const angle = Math.atan2(tr.y - tl.y, tr.x - tl.x) * 180 / Math.PI;
76
+ return ((Math.round(angle / 90) * 90) % 360 + 360) % 360;
77
+ }
78
+ /**
79
+ * Return the number of canvas modules that are dark in a sampled matrix.
80
+ *
81
+ * Encoded profile symbols clear the whole reserved canvas. A non-zero count
82
+ * means either a normal QR symbol or a raster whose canvas was overwritten by
83
+ * artwork; both are rejected because decoding that image would be speculative.
84
+ */
85
+ function canvasDarkCount(matrix, canvas) {
86
+ let dark = 0;
87
+ for (const [x, y] of canvasModules(matrix.width, canvas)) {
88
+ if (matrix.get(x, y)) dark++;
89
+ }
90
+ return dark;
91
+ }
92
+
93
+ function sameCandidate(left, right) {
94
+ if (left.dimension !== right.dimension) return false;
95
+ const a = left.corners[0];
96
+ const b = right.corners[0];
97
+ return Math.hypot(a.x - b.x, a.y - b.y) <= Math.max(left.moduleSize, right.moduleSize) * 2;
98
+ }
99
+
100
+ /**
101
+ * Detect Sythos Canvas QR symbols in a binarized raster.
102
+ *
103
+ * @param {import('../core/bit-matrix.js').BitMatrix} binaryImage Set bit = dark.
104
+ * @param {object} [options]
105
+ * @param {object} [options.canvas] Explicit canvas metadata for non-default
106
+ * shapes/size. Without it, the profile's canonical centered square is used.
107
+ * @param {boolean} [options.voting=false] Use majority sampling per module.
108
+ * @returns {FrameQRDetection[]} Best candidate first; empty when no verified
109
+ * profile signature is found.
110
+ */
111
+ export function detectFrameQR(binaryImage, options = {}) {
112
+ if (!binaryImage || !binaryImage.width || !binaryImage.height) {
113
+ throw new NotFoundError('detectFrameQR: no image supplied');
114
+ }
115
+
116
+ let candidates;
117
+ try { candidates = detectQR(binaryImage); } catch { return []; }
118
+ const detections = [];
119
+
120
+ for (const candidate of candidates) {
121
+ // QR detector dimensions are already constrained to legal Model 2 sizes.
122
+ let canvas;
123
+ try { canvas = normalizeCanvasSpec(candidate.dimension, options.canvas); }
124
+ catch { continue; }
125
+
126
+ for (const voting of [Boolean(options.voting), !Boolean(options.voting)]) {
127
+ let matrix;
128
+ try { matrix = sampleQuad(binaryImage, candidate.dimension, candidate.corners, voting); }
129
+ catch { continue; }
130
+
131
+ // The signature check is intentionally strict. It prevents an ordinary
132
+ // QR symbol from being relabelled as Canvas QR by the decoder's explicit
133
+ // allowUnmarked escape hatch.
134
+ if (canvasDarkCount(matrix, canvas) !== 0) continue;
135
+
136
+ const marked = matrix;
137
+ marked.frameqr = {
138
+ profile: FRAMEQR_PROFILE.id,
139
+ certified: false,
140
+ canvas,
141
+ };
142
+ let decoded;
143
+ try {
144
+ decoded = decodeFrameQR(marked, {
145
+ profile: FRAMEQR_PROFILE.id,
146
+ canvas,
147
+ allowUnmarked: true,
148
+ });
149
+ } catch { continue; }
150
+
151
+ const detection = {
152
+ corners: candidate.corners,
153
+ dimension: candidate.dimension,
154
+ version: candidate.version,
155
+ moduleSize: candidate.moduleSize,
156
+ rotation: orientationDegrees(candidate.corners),
157
+ matrix: marked,
158
+ canvas,
159
+ profile: FRAMEQR_PROFILE.id,
160
+ certified: false,
161
+ result: decoded,
162
+ };
163
+ if (!detections.some((entry) => sameCandidate(entry, detection))) detections.push(detection);
164
+ break;
165
+ }
166
+ }
167
+
168
+ detections.sort((a, b) => b.moduleSize - a.moduleSize);
169
+ return detections;
170
+ }
171
+
172
+ /**
173
+ * Detect and decode all verified Canvas QR symbols in one call.
174
+ *
175
+ * @param {import('../core/bit-matrix.js').BitMatrix} binaryImage
176
+ * @param {object} [options]
177
+ * @returns {Array<object>}
178
+ */
179
+ export function detectAndDecodeFrameQR(binaryImage, options = {}) {
180
+ let detections;
181
+ try { detections = detectFrameQR(binaryImage, options); } catch { return []; }
182
+ const results = [];
183
+ const seen = new Set();
184
+ for (const detection of detections) {
185
+ const result = detection.result;
186
+ const key = `${result.version}|${result.text}`;
187
+ if (seen.has(key)) continue;
188
+ seen.add(key);
189
+ results.push({ ...result, corners: detection.corners, rotation: detection.rotation });
190
+ }
191
+ return results;
192
+ }