@sythos/js_barcode_universal 1.5.8 → 1.5.10

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 (179) hide show
  1. package/LICENSE +5 -4
  2. package/NOTICE.md +2 -2
  3. package/README.md +61 -13
  4. package/bundle/sythos-barcode.esm.js +11658 -11244
  5. package/bundle/sythos-barcode.js +11658 -11244
  6. package/package.json +10 -3
  7. package/src/index.d.ts +1 -1
  8. package/src/index.js +508 -516
  9. package/src/js/aztec/decoder.js +208 -199
  10. package/src/js/aztec/detector.js +153 -126
  11. package/src/js/aztec/encoder.js +172 -153
  12. package/src/js/aztec/high-level.js +106 -101
  13. package/src/js/aztec/index.js +1 -12
  14. package/src/js/aztec/tables.js +106 -120
  15. package/src/js/aztecrune/decoder.js +76 -85
  16. package/src/js/aztecrune/detector.js +107 -93
  17. package/src/js/aztecrune/encoder.js +50 -68
  18. package/src/js/aztecrune/index.js +1 -16
  19. package/src/js/aztecrune/tables.js +59 -54
  20. package/src/js/compactpdf417/decoder.js +69 -74
  21. package/src/js/compactpdf417/detector.js +74 -75
  22. package/src/js/compactpdf417/encoder.js +68 -81
  23. package/src/js/compactpdf417/index.js +1 -13
  24. package/src/js/compactpdf417/tables.js +71 -80
  25. package/src/js/core/bit-buffer.js +106 -122
  26. package/src/js/core/bit-matrix.js +196 -194
  27. package/src/js/core/errors.js +13 -15
  28. package/src/js/core/galois-field.js +119 -133
  29. package/src/js/core/index.js +2 -19
  30. package/src/js/core/reed-solomon.js +195 -204
  31. package/src/js/databar/codec.js +93 -108
  32. package/src/js/databar/decoder.js +149 -140
  33. package/src/js/databar/encoder.js +47 -55
  34. package/src/js/databar/gs1.js +122 -123
  35. package/src/js/databar/index.js +4 -36
  36. package/src/js/databar/patterns.js +81 -85
  37. package/src/js/databar/tables.js +51 -52
  38. package/src/js/datamatrix/decoder.js +216 -182
  39. package/src/js/datamatrix/detector.js +161 -132
  40. package/src/js/datamatrix/encoder.js +192 -133
  41. package/src/js/datamatrix/index.js +1 -9
  42. package/src/js/datamatrix/tables.js +68 -69
  43. package/src/js/frameqr/decoder.js +102 -125
  44. package/src/js/frameqr/detector.js +101 -94
  45. package/src/js/frameqr/encoder.js +67 -80
  46. package/src/js/frameqr/index.js +1 -10
  47. package/src/js/frameqr/tables.js +145 -163
  48. package/src/js/image/binarizer.js +146 -162
  49. package/src/js/image/grid-sampler.js +63 -79
  50. package/src/js/image/index.js +0 -2
  51. package/src/js/image/luminance.js +129 -140
  52. package/src/js/image/perspective.js +112 -149
  53. package/src/js/micropdf417/compaction.js +64 -65
  54. package/src/js/micropdf417/decoder.js +106 -109
  55. package/src/js/micropdf417/detector.js +73 -70
  56. package/src/js/micropdf417/encoder.js +129 -154
  57. package/src/js/micropdf417/error-correction.js +6 -11
  58. package/src/js/micropdf417/index.js +2 -16
  59. package/src/js/micropdf417/tables.js +112 -99
  60. package/src/js/microqr/decoder.js +177 -182
  61. package/src/js/microqr/detector.js +240 -256
  62. package/src/js/microqr/encoder.js +239 -194
  63. package/src/js/microqr/index.js +0 -2
  64. package/src/js/microqr/tables.js +208 -200
  65. package/src/js/oned/addons.js +167 -191
  66. package/src/js/oned/index.js +27 -58
  67. package/src/js/oned/patterns.js +194 -213
  68. package/src/js/oned/reader.js +1003 -984
  69. package/src/js/oned/writers.js +411 -466
  70. package/src/js/pdf417/compaction.js +315 -222
  71. package/src/js/pdf417/decoder.js +71 -36
  72. package/src/js/pdf417/detector.js +377 -361
  73. package/src/js/pdf417/encoder.js +57 -46
  74. package/src/js/pdf417/error-correction.js +5 -8
  75. package/src/js/pdf417/tables.js +248 -250
  76. package/src/js/qr/decoder.js +311 -347
  77. package/src/js/qr/detector.js +378 -395
  78. package/src/js/qr/encoder.js +514 -599
  79. package/src/js/qr/index.js +0 -2
  80. package/src/js/qr/tables.js +375 -422
  81. package/src/js/render/image-data.js +57 -62
  82. package/src/js/render/index.js +21 -23
  83. package/src/js/render/options.js +82 -90
  84. package/src/js/render/png.js +144 -169
  85. package/src/js/render/svg.js +41 -44
  86. package/src/js/render/webgl.js +103 -109
  87. package/src/js/render/webgpu.js +205 -217
  88. package/src/js/rmqr/decoder.js +161 -53
  89. package/src/js/rmqr/detector.js +73 -41
  90. package/src/js/rmqr/encoder.js +253 -104
  91. package/src/js/rmqr/index.js +1 -5
  92. package/src/js/rmqr/tables.js +108 -85
  93. package/src/ts/aztec/decoder.ts +317 -0
  94. package/src/ts/aztec/detector.ts +224 -0
  95. package/src/ts/aztec/encoder.ts +257 -0
  96. package/src/ts/aztec/high-level.ts +211 -0
  97. package/src/ts/aztec/index.ts +45 -0
  98. package/src/ts/aztec/tables.ts +210 -0
  99. package/src/ts/aztecrune/decoder.ts +155 -0
  100. package/src/ts/aztecrune/detector.ts +166 -0
  101. package/src/ts/aztecrune/encoder.ts +121 -0
  102. package/src/ts/aztecrune/index.ts +49 -0
  103. package/src/ts/aztecrune/tables.ts +137 -0
  104. package/src/ts/compactpdf417/decoder.ts +128 -0
  105. package/src/ts/compactpdf417/detector.ts +139 -0
  106. package/src/ts/compactpdf417/encoder.ts +140 -0
  107. package/src/ts/compactpdf417/index.ts +17 -0
  108. package/src/ts/compactpdf417/tables.ts +173 -0
  109. package/src/ts/core/bit-buffer.ts +174 -0
  110. package/src/ts/core/bit-matrix.ts +241 -0
  111. package/src/ts/core/errors.ts +61 -0
  112. package/src/ts/core/galois-field.ts +207 -0
  113. package/src/ts/core/index.ts +56 -0
  114. package/src/ts/core/reed-solomon.ts +327 -0
  115. package/src/ts/databar/codec.ts +181 -0
  116. package/src/ts/databar/decoder.ts +215 -0
  117. package/src/ts/databar/encoder.ts +96 -0
  118. package/src/ts/databar/gs1.ts +177 -0
  119. package/src/ts/databar/index.ts +68 -0
  120. package/src/ts/databar/patterns.ts +143 -0
  121. package/src/ts/databar/tables.ts +124 -0
  122. package/src/ts/datamatrix/decoder.ts +262 -0
  123. package/src/ts/datamatrix/detector.ts +225 -0
  124. package/src/ts/datamatrix/encoder.ts +191 -0
  125. package/src/ts/datamatrix/index.ts +42 -0
  126. package/src/ts/datamatrix/tables.ts +123 -0
  127. package/src/ts/frameqr/decoder.ts +239 -0
  128. package/src/ts/frameqr/detector.ts +192 -0
  129. package/src/ts/frameqr/encoder.ts +156 -0
  130. package/src/ts/frameqr/index.ts +42 -0
  131. package/src/ts/frameqr/tables.ts +270 -0
  132. package/src/ts/image/binarizer.ts +270 -0
  133. package/src/ts/image/grid-sampler.ts +164 -0
  134. package/src/ts/image/index.ts +40 -0
  135. package/src/ts/image/luminance.ts +196 -0
  136. package/src/ts/image/perspective.ts +195 -0
  137. package/src/ts/index.d.ts +1 -1
  138. package/src/ts/index.ts +790 -0
  139. package/src/ts/micropdf417/compaction.ts +116 -0
  140. package/src/ts/micropdf417/decoder.ts +183 -0
  141. package/src/ts/micropdf417/detector.ts +149 -0
  142. package/src/ts/micropdf417/encoder.ts +209 -0
  143. package/src/ts/micropdf417/error-correction.ts +55 -0
  144. package/src/ts/micropdf417/index.ts +49 -0
  145. package/src/ts/micropdf417/tables.ts +184 -0
  146. package/src/ts/microqr/decoder.ts +245 -0
  147. package/src/ts/microqr/detector.ts +355 -0
  148. package/src/ts/microqr/encoder.ts +269 -0
  149. package/src/ts/microqr/index.ts +36 -0
  150. package/src/ts/microqr/tables.ts +316 -0
  151. package/src/ts/oned/addons.ts +420 -0
  152. package/src/ts/oned/index.ts +106 -0
  153. package/src/ts/oned/patterns.ts +384 -0
  154. package/src/ts/oned/reader.ts +1364 -0
  155. package/src/ts/oned/writers.ts +746 -0
  156. package/src/ts/pdf417/compaction.ts +298 -0
  157. package/src/ts/pdf417/decoder.ts +75 -0
  158. package/src/ts/pdf417/detector.ts +468 -0
  159. package/src/ts/pdf417/encoder.ts +91 -0
  160. package/src/ts/pdf417/error-correction.ts +47 -0
  161. package/src/ts/pdf417/index.ts +6 -0
  162. package/src/ts/pdf417/tables.ts +317 -0
  163. package/src/ts/qr/decoder.ts +575 -0
  164. package/src/ts/qr/detector.ts +630 -0
  165. package/src/ts/qr/encoder.ts +958 -0
  166. package/src/ts/qr/index.ts +44 -0
  167. package/src/ts/qr/tables.ts +737 -0
  168. package/src/ts/render/image-data.ts +125 -0
  169. package/src/ts/render/index.ts +130 -0
  170. package/src/ts/render/options.ts +160 -0
  171. package/src/ts/render/png.ts +295 -0
  172. package/src/ts/render/svg.ts +120 -0
  173. package/src/ts/render/webgl.ts +206 -0
  174. package/src/ts/render/webgpu.ts +369 -0
  175. package/src/ts/rmqr/decoder.ts +101 -0
  176. package/src/ts/rmqr/detector.ts +90 -0
  177. package/src/ts/rmqr/encoder.ts +172 -0
  178. package/src/ts/rmqr/index.ts +37 -0
  179. package/src/ts/rmqr/tables.ts +154 -0
@@ -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
+ }
@@ -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-License-Identifier: MIT
27
+ *
28
+ * Original work. No code from any other barcode implementation.
29
+ */
30
+
31
+ /**
32
+ * Sythos Canvas QR profile encoder.
33
+ *
34
+ * This encoder deliberately builds on this project's QR Code implementation
35
+ * and then removes a conservatively bounded set of data modules for artwork.
36
+ * It is not an implementation of DENSO FrameQR and makes no interoperability
37
+ * claim for that proprietary format.
38
+ *
39
+ * @module frameqr/encoder
40
+ */
41
+
42
+ import { EncodeError } from '../core/errors.js';
43
+ import { encodeQR } from '../qr/encoder.js';
44
+ import {
45
+ FRAMEQR_PROFILE,
46
+ canvasModules,
47
+ normalizeCanvasSpec,
48
+ validateCanvasSpec,
49
+ } from './tables.js';
50
+
51
+ /**
52
+ * @typedef {object} FrameQrEncodeOptions
53
+ * @property {'H'} [ecc] The profile always uses QR error correction H.
54
+ * @property {number} [version] Force a QR version 1-40.
55
+ * @property {number} [mask] Force a QR mask 0-7.
56
+ * @property {'auto'|'utf-8'|'iso-8859-1'} [charset] Byte mode interpretation.
57
+ * @property {boolean} [kanji] Allow QR kanji mode.
58
+ * @property {object} [canvas] Profile artwork reservation.
59
+ */
60
+
61
+ function versionFor(matrix) {
62
+ return (matrix.width - 17) / 4;
63
+ }
64
+
65
+ function clearCanvas(matrix, modules) {
66
+ for (const [x, y] of modules) matrix.unset(x, y);
67
+ }
68
+
69
+ /**
70
+ * Encode a QR Code with a conservative artwork canvas according to the
71
+ * non-certified Sythos Canvas QR profile.
72
+ *
73
+ * The profile forces QR H error correction and rejects a canvas whenever its
74
+ * known codeword damage exceeds the per-block correction budget. When a
75
+ * version is not forced, the smallest QR version that holds both payload and
76
+ * safe canvas is selected. A decoder can reconstruct the reserved modules from
77
+ * the returned profile metadata.
78
+ *
79
+ * @param {string} text
80
+ * @param {FrameQrEncodeOptions} [options]
81
+ * @returns {import('../core/bit-matrix.js').BitMatrix}
82
+ * @throws {EncodeError} When the QR payload/options are invalid or the canvas
83
+ * cannot safely fit the selected QR version.
84
+ */
85
+ export function encodeFrameQR(text, options = {}) {
86
+ if (typeof text !== 'string') {
87
+ throw new EncodeError('Sythos Canvas QR: text must be a string');
88
+ }
89
+ if (options.ecc !== undefined && options.ecc !== 'H') {
90
+ throw new EncodeError('Sythos Canvas QR: ecc is fixed to H for this profile');
91
+ }
92
+
93
+ const qrOptions = {
94
+ mask: options.mask,
95
+ charset: options.charset,
96
+ kanji: options.kanji,
97
+ ecc: 'H',
98
+ };
99
+ for (const key of Object.keys(qrOptions)) {
100
+ if (qrOptions[key] === undefined) delete qrOptions[key];
101
+ }
102
+
103
+ const versions = options.version === undefined
104
+ ? Array.from({ length: 40 }, (_, index) => index + 1)
105
+ : [options.version];
106
+ let capacityError = null;
107
+ let unsafeAnalysis = null;
108
+ let selected = null;
109
+
110
+ for (const version of versions) {
111
+ let matrix;
112
+ try {
113
+ matrix = encodeQR(text, { ...qrOptions, version });
114
+ } catch (error) {
115
+ capacityError = error;
116
+ continue;
117
+ }
118
+
119
+ let canvas;
120
+ let analysis;
121
+ try {
122
+ canvas = normalizeCanvasSpec(matrix.width, options.canvas);
123
+ analysis = validateCanvasSpec(versionFor(matrix), canvas);
124
+ } catch (error) {
125
+ if (error instanceof EncodeError) throw error;
126
+ throw new EncodeError(`Sythos Canvas QR: invalid canvas: ${error.message}`);
127
+ }
128
+ if (!analysis.safe) {
129
+ unsafeAnalysis = analysis;
130
+ continue;
131
+ }
132
+ selected = { matrix, canvas };
133
+ break;
134
+ }
135
+
136
+ if (!selected) {
137
+ if (unsafeAnalysis) {
138
+ throw new EncodeError(
139
+ 'Sythos Canvas QR: canvas is not safe for the selected QR version; ' +
140
+ `it touches ${unsafeAnalysis.touchedCodewordCount} codewords and has ` +
141
+ `a per-block correction budget of ${unsafeAnalysis.correctionBudgetPerBlock}`
142
+ );
143
+ }
144
+ if (capacityError) throw capacityError;
145
+ throw new EncodeError('Sythos Canvas QR: unable to select a QR version');
146
+ }
147
+
148
+ const { matrix, canvas } = selected;
149
+ clearCanvas(matrix, canvasModules(matrix.width, canvas));
150
+ matrix.frameqr = {
151
+ profile: FRAMEQR_PROFILE.id,
152
+ certified: false,
153
+ canvas,
154
+ };
155
+ return matrix;
156
+ }
@@ -0,0 +1,42 @@
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
+ export { encodeFrameQR } from './encoder.js';
32
+ export { decodeFrameQR } from './decoder.js';
33
+ export { detectFrameQR, detectAndDecodeFrameQR } from './detector.js';
34
+ export {
35
+ FRAMEQR_PROFILE,
36
+ FRAMEQR_CANVAS_SHAPES,
37
+ canvasModules,
38
+ normalizeCanvasSpec,
39
+ analyzeCanvasDamage,
40
+ validateCanvasSpec,
41
+ validateFrameQrTables,
42
+ } from './tables.js';