@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,123 +1,124 @@
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-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
+ * Data Matrix ECC 200 symbol parameters.
34
+ *
35
+ * Width and height include finder borders. `regionWidth` and `regionHeight`
36
+ * describe the usable modules inside one data region. The last three columns
37
+ * make the Reed-Solomon block split explicit instead of hiding the 144x144
38
+ * exception in encoder control flow.
39
+ *
40
+ * @module datamatrix/tables
41
+ */
42
+
43
+ function symbol(width, height, dataRegionWidth, dataRegionHeight, dataCodewords, errorCodewords, dataBlockLengths) {
44
+ const blockCount = dataBlockLengths.length;
45
+ return Object.freeze({
46
+ width, height, rows: height, columns: width,
47
+ // Region dimensions include their one-module finder border on each side;
48
+ // dataRegion* expose the inner placement lattice explicitly.
49
+ regionWidth: dataRegionWidth + 2, regionHeight: dataRegionHeight + 2,
50
+ dataRegionWidth, dataRegionHeight,
51
+ dataRegionRows: dataRegionHeight, dataRegionColumns: dataRegionWidth,
52
+ dataCodewords, errorCodewords, blockCount,
53
+ eccPerBlock: errorCodewords / blockCount,
54
+ dataBlockLengths: Object.freeze(dataBlockLengths),
55
+ });
56
+ }
57
+
58
+ /** Classic ISO/IEC 16022 ECC 200 symbols; DMRE is deliberately excluded. */
59
+ export const DATAMATRIX_SYMBOLS = Object.freeze([
60
+ symbol(10, 10, 8, 8, 3, 5, [3]),
61
+ symbol(12, 12, 10, 10, 5, 7, [5]),
62
+ symbol(14, 14, 12, 12, 8, 10, [8]),
63
+ symbol(16, 16, 14, 14, 12, 12, [12]),
64
+ symbol(18, 18, 16, 16, 18, 14, [18]),
65
+ symbol(20, 20, 18, 18, 22, 18, [22]),
66
+ symbol(22, 22, 20, 20, 30, 20, [30]),
67
+ symbol(24, 24, 22, 22, 36, 24, [36]),
68
+ symbol(26, 26, 24, 24, 44, 28, [44]),
69
+ symbol(32, 32, 14, 14, 62, 36, [62]),
70
+ symbol(36, 36, 16, 16, 86, 42, [86]),
71
+ symbol(40, 40, 18, 18, 114, 48, [114]),
72
+ symbol(44, 44, 20, 20, 144, 56, [144]),
73
+ symbol(48, 48, 22, 22, 174, 68, [174]),
74
+ symbol(52, 52, 24, 24, 204, 84, [102, 102]),
75
+ symbol(64, 64, 14, 14, 280, 112, [140, 140]),
76
+ symbol(72, 72, 16, 16, 368, 144, [92, 92, 92, 92]),
77
+ symbol(80, 80, 18, 18, 456, 192, [114, 114, 114, 114]),
78
+ symbol(88, 88, 20, 20, 576, 224, [144, 144, 144, 144]),
79
+ symbol(96, 96, 22, 22, 696, 272, [174, 174, 174, 174]),
80
+ symbol(104, 104, 24, 24, 816, 336, [136, 136, 136, 136, 136, 136]),
81
+ symbol(120, 120, 18, 18, 1050, 408, [175, 175, 175, 175, 175, 175]),
82
+ symbol(132, 132, 20, 20, 1304, 496, [163, 163, 163, 163, 163, 163, 163, 163]),
83
+ symbol(144, 144, 22, 22, 1558, 620, [156, 156, 156, 156, 156, 156, 156, 156, 155, 155]),
84
+ symbol(18, 8, 16, 6, 5, 7, [5]),
85
+ symbol(32, 8, 14, 6, 10, 11, [10]),
86
+ symbol(26, 12, 24, 10, 16, 14, [16]),
87
+ symbol(36, 12, 16, 10, 22, 18, [22]),
88
+ symbol(36, 16, 16, 14, 32, 24, [32]),
89
+ symbol(48, 16, 22, 14, 49, 28, [49]),
90
+ ]);
91
+
92
+ /** Compatibility alias. */
93
+ export const SYMBOLS = DATAMATRIX_SYMBOLS;
94
+
95
+ /** Return the smallest permitted symbol that holds `count` data codewords. */
96
+ export function symbolForDataCodewords(count, shape = 'any') {
97
+ for (const s of DATAMATRIX_SYMBOLS) {
98
+ const rectangular = s.width !== s.height;
99
+ if ((shape === 'square' && rectangular) || (shape === 'rectangular' && !rectangular)) continue;
100
+ if (count <= s.dataCodewords) return s;
101
+ }
102
+ throw new RangeError(`Data Matrix: ${count} data codewords do not fit an ECC 200 ${shape} symbol`);
103
+ }
104
+
105
+ /** Check redundant geometry and block identities in the static table. */
106
+ export function validateDataMatrixTables() {
107
+ const issues = [];
108
+ for (const s of DATAMATRIX_SYMBOLS) {
109
+ const regionsX = s.width / s.regionWidth;
110
+ const regionsY = s.height / s.regionHeight;
111
+ if (!Number.isInteger(regionsX) || !Number.isInteger(regionsY)) issues.push(`${s.width}x${s.height}: non-integral regions`);
112
+ const modules = regionsX * regionsY * s.dataRegionWidth * s.dataRegionHeight;
113
+ // Annex F reserves four terminal modules on a few lattice dimensions.
114
+ // They are set to dark after codeword placement and do not carry data.
115
+ const unused = modules - (s.dataCodewords + s.errorCodewords) * 8;
116
+ if (unused !== 0 && unused !== 4) issues.push(`${s.width}x${s.height}: geometry/codeword mismatch`);
117
+ if (s.dataBlockLengths.reduce((a, b) => a + b, 0) !== s.dataCodewords) issues.push(`${s.width}x${s.height}: data block mismatch`);
118
+ if (s.eccPerBlock * s.blockCount !== s.errorCodewords) issues.push(`${s.width}x${s.height}: ecc block mismatch`);
119
+ }
120
+ return issues;
121
+ }
122
+
123
+ /** Compatibility alias. */
124
+ export const validateTables = validateDataMatrixTables;
@@ -0,0 +1,240 @@
1
+ /*!
2
+ * Sythos Barcode Suite
3
+ *
4
+ * MIT License
5
+ *
6
+ * Copyright (c) 2026 Sythos
7
+ * SPDX-FileCopyrightText: 2026 Sythos (https://www.sythos.net)
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ * of this software and associated documentation files (the "Software"), to deal
11
+ * in the Software without restriction, including without limitation the rights
12
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ * copies of the Software, and to permit persons to whom the Software is
14
+ * furnished to do so, subject to the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be included in all
17
+ * copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ * SOFTWARE.
26
+ *
27
+ * SPDX-License-Identifier: MIT
28
+ *
29
+ * Original work. No code from any other barcode implementation.
30
+ */
31
+
32
+ /**
33
+ * Decoder for FrameQR Code.
34
+ *
35
+ * This is intentionally not a DENSO FrameQR decoder. The profile is a QR
36
+ * symbol with a bounded artwork reservation and an explicit `frameqr` marker.
37
+ * A normal QR matrix is rejected unless a detector (or another trusted
38
+ * caller) supplies the profile and explicitly opts in to an unmarked matrix.
39
+ *
40
+ * The QR decoder is deliberately kept as the single payload decoder. It can
41
+ * repair the bounded modules removed for the canvas because the encoder fixes
42
+ * error correction to H and validates the reservation before clearing it.
43
+ *
44
+ * @module frameqr/decoder
45
+ */
46
+
47
+ import { FormatError } from '../core/errors.js';
48
+ import { decodeQR } from '../qr/decoder.js';
49
+ import {
50
+ FRAMEQR_PROFILE,
51
+ normalizeCanvasSpec,
52
+ validateCanvasSpec,
53
+ analyzeCanvasDamage,
54
+ } from './tables.js';
55
+
56
+ /** @param {unknown} value @returns {boolean} */
57
+ function isObject(value) {
58
+ return value !== null && typeof value === 'object';
59
+ }
60
+
61
+ /**
62
+ * Return the profile marker attached by the encoder. Older callers sometimes
63
+ * use camel case, so accepting it here is harmless while the emitted marker
64
+ * remains the canonical `frameqr` property.
65
+ *
66
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
67
+ * @returns {object|null}
68
+ */
69
+ function markerOf(matrix) {
70
+ const marker = matrix && (matrix.frameqr ?? matrix.frameQR);
71
+ return isObject(marker) ? marker : null;
72
+ }
73
+
74
+ /** @param {unknown} profile @returns {boolean} */
75
+ function isExpectedProfile(profile) {
76
+ if (profile === FRAMEQR_PROFILE.id) return true;
77
+ return isObject(profile) && profile.id === FRAMEQR_PROFILE.id;
78
+ }
79
+
80
+ /**
81
+ * Normalize and validate the canvas metadata. Validation functions in the
82
+ * table module throw on invalid input; a few consumers also return `false` or
83
+ * a problem list, so those forms are handled defensively here.
84
+ *
85
+ * @param {number} symbolSize
86
+ * @param {number} version
87
+ * @param {object} canvas
88
+ * @returns {{canvas: object, damage: object}}
89
+ */
90
+ function validateCanvas(symbolSize, version, canvas) {
91
+ if (!isObject(canvas)) throw new FormatError('FrameQR Code: canvas metadata is missing');
92
+
93
+ let normalized;
94
+ try {
95
+ normalized = normalizeCanvasSpec(symbolSize, canvas);
96
+ const validation = validateCanvasSpec(version, normalized);
97
+ if (
98
+ validation === false ||
99
+ (Array.isArray(validation) && validation.length > 0) ||
100
+ (isObject(validation) && validation.valid === false) ||
101
+ (isObject(validation) && validation.safe === false)
102
+ ) {
103
+ throw new Error('canvas geometry is outside the profile limits');
104
+ }
105
+ } catch (error) {
106
+ if (error instanceof FormatError) throw error;
107
+ throw new FormatError(`FrameQR Code: invalid canvas metadata: ${error.message}`);
108
+ }
109
+
110
+ let damage;
111
+ try {
112
+ damage = analyzeCanvasDamage(version, normalized);
113
+ } catch (error) {
114
+ throw new FormatError(`FrameQR Code: cannot analyse canvas damage: ${error.message}`);
115
+ }
116
+ return { canvas: normalized, damage };
117
+ }
118
+
119
+ /**
120
+ * Resolve the profile marker and canvas from the matrix/options pair.
121
+ *
122
+ * `allowUnmarked` is deliberately opt-in. It exists for a detector that has
123
+ * already verified the canvas signature after sampling a photograph; it is
124
+ * not enabled by the public default and therefore cannot silently relabel an
125
+ * ordinary QR Code as FrameQR.
126
+ *
127
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
128
+ * @param {object} options
129
+ * @returns {{profile: string, canvas: object, damage: object, certified: false}}
130
+ */
131
+ function resolveProfile(matrix, options) {
132
+ const marker = markerOf(matrix);
133
+ const suppliedProfile = options.profile;
134
+ const markerProfile = marker ? marker.profile : undefined;
135
+ if (markerProfile !== undefined && suppliedProfile !== undefined) {
136
+ const markerId = isObject(markerProfile) ? markerProfile.id : markerProfile;
137
+ const suppliedId = isObject(suppliedProfile) ? suppliedProfile.id : suppliedProfile;
138
+ if (markerId !== suppliedId) {
139
+ throw new FormatError('FrameQR Code: matrix and requested profile markers disagree');
140
+ }
141
+ }
142
+ const profile = markerProfile ?? suppliedProfile;
143
+
144
+ if (!isExpectedProfile(profile)) {
145
+ throw new FormatError(
146
+ 'FrameQR Code: profile marker is missing or is not a FrameQR Code symbol'
147
+ );
148
+ }
149
+ if (marker && marker.certified === true) {
150
+ throw new FormatError(
151
+ 'FrameQR Code: certified FrameQR input is not supported by this profile decoder'
152
+ );
153
+ }
154
+ if (!marker && !options.allowUnmarked) {
155
+ throw new FormatError(
156
+ 'FrameQR Code: unmarked QR matrix rejected; provide a verified profile marker'
157
+ );
158
+ }
159
+
160
+ const markerCanvas = marker ? marker.canvas : undefined;
161
+ const canvas = options.canvas ?? markerCanvas;
162
+ const version = (matrix.width - 17) / 4;
163
+ if (!Number.isInteger(version) || version < 1 || version > 40) {
164
+ throw new FormatError(`FrameQR Code: invalid QR symbol size ${matrix.width}`);
165
+ }
166
+ const checked = validateCanvas(matrix.width, version, canvas);
167
+ if (markerCanvas && options.canvas) {
168
+ let marked;
169
+ try {
170
+ marked = normalizeCanvasSpec(matrix.width, markerCanvas);
171
+ } catch (error) {
172
+ throw new FormatError(`FrameQR Code: invalid marker canvas: ${error.message}`);
173
+ }
174
+ const fields = ['shape', 'centerX', 'centerY', 'width', 'height', 'angle'];
175
+ if (fields.some((field) => marked[field] !== checked.canvas[field])) {
176
+ throw new FormatError('FrameQR Code: matrix and requested canvas metadata disagree');
177
+ }
178
+ }
179
+ return {
180
+ profile: FRAMEQR_PROFILE.id,
181
+ canvas: checked.canvas,
182
+ damage: checked.damage,
183
+ certified: false,
184
+ };
185
+ }
186
+
187
+ /**
188
+ * Decode a FrameQR Code matrix.
189
+ *
190
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
191
+ * Square QR modules, normally returned by `encodeFrameQR` or a FrameQR
192
+ * detector. The encoder's `frameqr` metadata is required by default.
193
+ * @param {object} [options]
194
+ * @param {object} [options.canvas] Explicit canvas metadata for a sampled
195
+ * matrix when the source marker was not preserved.
196
+ * @param {string|object} [options.profile] Expected profile identifier.
197
+ * @param {boolean} [options.allowUnmarked=false] Explicit detector opt-in for
198
+ * a matrix whose marker was lost during image sampling.
199
+ * @returns {import('../qr/decoder.js').DecodeResult & {
200
+ * format: 'frameqr', profile: string, certified: false,
201
+ * frame: object, canvas: object, canvasDamage: object
202
+ * }}
203
+ * @throws {FormatError} If the profile marker/canvas is invalid or the input
204
+ * is an ordinary QR Code.
205
+ */
206
+ export function decodeFrameQR(matrix, options = {}) {
207
+ if (!matrix || !Number.isInteger(matrix.width) || !Number.isInteger(matrix.height)) {
208
+ throw new FormatError('FrameQR Code: no matrix supplied');
209
+ }
210
+ if (matrix.width !== matrix.height) {
211
+ throw new FormatError(
212
+ `FrameQR Code: symbol must be square, got ${matrix.width}x${matrix.height}`
213
+ );
214
+ }
215
+
216
+ const profile = resolveProfile(matrix, options);
217
+ let decoded;
218
+ try {
219
+ decoded = decodeQR(matrix);
220
+ } catch (error) {
221
+ // Preserve the original QR/RS error when possible, but give callers a
222
+ // profile-specific context without exposing implementation details.
223
+ if (error instanceof FormatError) {
224
+ throw new FormatError(`FrameQR Code: payload is not recoverable: ${error.message}`);
225
+ }
226
+ throw error;
227
+ }
228
+
229
+ return {
230
+ ...decoded,
231
+ format: 'frameqr',
232
+ profile: profile.profile,
233
+ certified: profile.certified,
234
+ frame: profile.canvas,
235
+ canvas: profile.canvas,
236
+ canvasDamage: profile.damage,
237
+ };
238
+ }
239
+
240
+ export { isExpectedProfile };