@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
@@ -27,7 +27,6 @@
27
27
  *
28
28
  * Original work. No code from any other barcode implementation.
29
29
  */
30
-
31
30
  /**
32
31
  * Aztec high-level stream writer.
33
32
  *
@@ -44,22 +43,18 @@
44
43
  *
45
44
  * @module aztec/high-level
46
45
  */
47
-
48
46
  import { BitWriter } from '../core/bit-buffer.js';
49
47
  import { EncodeError } from '../core/errors.js';
50
-
51
48
  /** Aztec high-level table identifiers, exposed for decoder/API symmetry. */
52
49
  export const HIGH_LEVEL_MODE = Object.freeze({
53
- UPPER: 0,
54
- LOWER: 1,
55
- DIGIT: 2,
56
- MIXED: 3,
57
- PUNCT: 4,
50
+ UPPER: 0,
51
+ LOWER: 1,
52
+ DIGIT: 2,
53
+ MIXED: 3,
54
+ PUNCT: 4,
58
55
  });
59
-
60
56
  /** Maximum number of bytes represented by one B/S escape. */
61
57
  export const MAX_BINARY_SHIFT = 2078;
62
-
63
58
  /**
64
59
  * Convert accepted public input to its encoded octets.
65
60
  *
@@ -68,78 +63,81 @@ export const MAX_BINARY_SHIFT = 2078;
68
63
  * @returns {Uint8Array}
69
64
  */
70
65
  export function aztecBytes(value, charset = 'utf-8') {
71
- if (charset !== 'utf-8') throw new EncodeError(`Aztec: unsupported charset "${charset}"`);
72
- if (typeof value === 'string') return new TextEncoder().encode(value);
73
- if (value instanceof ArrayBuffer) return new Uint8Array(value);
74
- if (ArrayBuffer.isView(value)) {
75
- return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
76
- }
77
- throw new EncodeError('Aztec: value must be a string, ArrayBuffer, or byte view');
66
+ if (charset !== 'utf-8')
67
+ throw new EncodeError(`Aztec: unsupported charset "${charset}"`);
68
+ if (typeof value === 'string')
69
+ return new TextEncoder().encode(value);
70
+ if (value instanceof ArrayBuffer)
71
+ return new Uint8Array(value);
72
+ if (ArrayBuffer.isView(value)) {
73
+ return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
74
+ }
75
+ throw new EncodeError('Aztec: value must be a string, ArrayBuffer, or byte view');
78
76
  }
79
-
80
77
  /** @param {number} byte @returns {number} UPPER-table value, or -1. */
81
78
  function upperValue(byte) {
82
- if (byte === 0x20) return 1;
83
- if (byte >= 0x41 && byte <= 0x5a) return byte - 0x41 + 2;
84
- return -1;
79
+ if (byte === 0x20)
80
+ return 1;
81
+ if (byte >= 0x41 && byte <= 0x5a)
82
+ return byte - 0x41 + 2;
83
+ return -1;
85
84
  }
86
-
87
85
  /** Aztec's latch table, packed as `(bitCount << 16) | bits`. */
88
86
  const LATCH = Object.freeze([
89
- [0, 327708, 327710, 327709, 656318],
90
- [590318, 0, 327710, 327709, 656318],
91
- [262158, 590300, 0, 590301, 932798],
92
- [327709, 327708, 656322, 0, 327710],
93
- [327711, 656380, 656382, 656381, 0],
87
+ [0, 327708, 327710, 327709, 656318],
88
+ [590318, 0, 327710, 327709, 656318],
89
+ [262158, 590300, 0, 590301, 932798],
90
+ [327709, 327708, 656322, 0, 327710],
91
+ [327711, 656380, 656382, 656381, 0],
94
92
  ]);
95
-
96
93
  /** @param {number} byte @returns {number} */
97
94
  function lowerValue(byte) {
98
- if (byte === 0x20) return 1;
99
- if (byte >= 0x61 && byte <= 0x7a) return byte - 0x61 + 2;
100
- return -1;
95
+ if (byte === 0x20)
96
+ return 1;
97
+ if (byte >= 0x61 && byte <= 0x7a)
98
+ return byte - 0x61 + 2;
99
+ return -1;
101
100
  }
102
-
103
101
  /** @param {number} byte @returns {number} */
104
102
  function digitValue(byte) {
105
- if (byte === 0x20) return 1;
106
- if (byte >= 0x30 && byte <= 0x39) return byte - 0x30 + 2;
107
- if (byte === 0x2c) return 12;
108
- if (byte === 0x2e) return 13;
109
- return -1;
103
+ if (byte === 0x20)
104
+ return 1;
105
+ if (byte >= 0x30 && byte <= 0x39)
106
+ return byte - 0x30 + 2;
107
+ if (byte === 0x2c)
108
+ return 12;
109
+ if (byte === 0x2e)
110
+ return 13;
111
+ return -1;
110
112
  }
111
-
112
113
  const PUNCT = new Map([
113
- [0x0d, 1], [0x21, 6], [0x22, 7], [0x23, 8], [0x24, 9], [0x25, 10],
114
- [0x26, 11], [0x27, 12], [0x28, 13], [0x29, 14], [0x2a, 15], [0x2b, 16],
115
- [0x2c, 17], [0x2d, 18], [0x2e, 19], [0x2f, 20], [0x3a, 21], [0x3b, 22],
116
- [0x3c, 23], [0x3d, 24], [0x3e, 25], [0x3f, 26], [0x5b, 27], [0x5d, 28],
117
- [0x7b, 29], [0x7d, 30],
114
+ [0x0d, 1], [0x21, 6], [0x22, 7], [0x23, 8], [0x24, 9], [0x25, 10],
115
+ [0x26, 11], [0x27, 12], [0x28, 13], [0x29, 14], [0x2a, 15], [0x2b, 16],
116
+ [0x2c, 17], [0x2d, 18], [0x2e, 19], [0x2f, 20], [0x3a, 21], [0x3b, 22],
117
+ [0x3c, 23], [0x3d, 24], [0x3e, 25], [0x3f, 26], [0x5b, 27], [0x5d, 28],
118
+ [0x7b, 29], [0x7d, 30],
118
119
  ]);
119
-
120
120
  /** @param {number} byte @param {number} mode @returns {number} */
121
121
  function textValue(byte, mode) {
122
- switch (mode) {
123
- case HIGH_LEVEL_MODE.UPPER: return upperValue(byte);
124
- case HIGH_LEVEL_MODE.LOWER: return lowerValue(byte);
125
- case HIGH_LEVEL_MODE.DIGIT: return digitValue(byte);
126
- case HIGH_LEVEL_MODE.PUNCT: return PUNCT.get(byte) ?? -1;
127
- default: return -1;
128
- }
122
+ switch (mode) {
123
+ case HIGH_LEVEL_MODE.UPPER: return upperValue(byte);
124
+ case HIGH_LEVEL_MODE.LOWER: return lowerValue(byte);
125
+ case HIGH_LEVEL_MODE.DIGIT: return digitValue(byte);
126
+ case HIGH_LEVEL_MODE.PUNCT: return PUNCT.get(byte) ?? -1;
127
+ default: return -1;
128
+ }
129
129
  }
130
-
131
130
  /** @param {BitWriter} writer @param {number} from @param {number} to */
132
131
  function latch(writer, from, to) {
133
- if (from === to) return;
134
- const packed = LATCH[from][to];
135
- writer.put(packed & 0xffff, packed >>> 16);
132
+ if (from === to)
133
+ return;
134
+ const packed = LATCH[from][to];
135
+ writer.put(packed & 0xffff, packed >>> 16);
136
136
  }
137
-
138
137
  /** @param {number} mode @returns {number} */
139
138
  function characterWidth(mode) {
140
- return mode === HIGH_LEVEL_MODE.DIGIT ? 4 : 5;
139
+ return mode === HIGH_LEVEL_MODE.DIGIT ? 4 : 5;
141
140
  }
142
-
143
141
  /**
144
142
  * Write an Aztec binary-shift segment while in UPPER mode.
145
143
  *
@@ -154,22 +152,23 @@ function characterWidth(mode) {
154
152
  * @param {number} length
155
153
  */
156
154
  export function writeBinaryShift(writer, bytes, start, length) {
157
- let at = start;
158
- let left = length;
159
- while (left > 0) {
160
- const count = Math.min(left, MAX_BINARY_SHIFT);
161
- writer.put(31, 5); // UPPER B/S
162
- if (count <= 31) writer.put(count, 5);
163
- else {
164
- writer.put(0, 5);
165
- writer.put(count - 31, 11);
155
+ let at = start;
156
+ let left = length;
157
+ while (left > 0) {
158
+ const count = Math.min(left, MAX_BINARY_SHIFT);
159
+ writer.put(31, 5); // UPPER B/S
160
+ if (count <= 31)
161
+ writer.put(count, 5);
162
+ else {
163
+ writer.put(0, 5);
164
+ writer.put(count - 31, 11);
165
+ }
166
+ for (let i = 0; i < count; i++)
167
+ writer.put(bytes[at + i], 8);
168
+ at += count;
169
+ left -= count;
166
170
  }
167
- for (let i = 0; i < count; i++) writer.put(bytes[at + i], 8);
168
- at += count;
169
- left -= count;
170
- }
171
171
  }
172
-
173
172
  /**
174
173
  * Build a valid Aztec high-level bitstream.
175
174
  *
@@ -178,34 +177,40 @@ export function writeBinaryShift(writer, bytes, start, length) {
178
177
  * @returns {BitWriter}
179
178
  */
180
179
  export function encodeHighLevel(value, options = {}) {
181
- const bytes = aztecBytes(value, options.charset ?? 'utf-8');
182
- const writer = new BitWriter();
183
- let mode = HIGH_LEVEL_MODE.UPPER;
184
-
185
- for (let at = 0; at < bytes.length;) {
186
- let bestMode = -1;
187
- let bestValue = -1;
188
- let bestCost = Number.POSITIVE_INFINITY;
189
- for (const candidate of [HIGH_LEVEL_MODE.UPPER, HIGH_LEVEL_MODE.LOWER, HIGH_LEVEL_MODE.DIGIT, HIGH_LEVEL_MODE.PUNCT]) {
190
- const value = textValue(bytes[at], candidate);
191
- if (value < 0) continue;
192
- const latchCost = candidate === mode ? 0 : LATCH[mode][candidate] >>> 16;
193
- const cost = latchCost + characterWidth(candidate);
194
- if (cost < bestCost) { bestCost = cost; bestMode = candidate; bestValue = value; }
195
- }
196
- if (bestMode >= 0) {
197
- latch(writer, mode, bestMode);
198
- writer.put(bestValue, characterWidth(bestMode));
199
- mode = bestMode;
200
- at++;
201
- } else {
202
- // B/S is defined from UPPER; the latch is retained after the shift.
203
- latch(writer, mode, HIGH_LEVEL_MODE.UPPER);
204
- mode = HIGH_LEVEL_MODE.UPPER;
205
- const start = at;
206
- while (at < bytes.length && ![HIGH_LEVEL_MODE.UPPER, HIGH_LEVEL_MODE.LOWER, HIGH_LEVEL_MODE.DIGIT, HIGH_LEVEL_MODE.PUNCT].some((m) => textValue(bytes[at], m) >= 0)) at++;
207
- writeBinaryShift(writer, bytes, start, at - start);
180
+ const bytes = aztecBytes(value, options.charset ?? 'utf-8');
181
+ const writer = new BitWriter();
182
+ let mode = HIGH_LEVEL_MODE.UPPER;
183
+ for (let at = 0; at < bytes.length;) {
184
+ let bestMode = -1;
185
+ let bestValue = -1;
186
+ let bestCost = Number.POSITIVE_INFINITY;
187
+ for (const candidate of [HIGH_LEVEL_MODE.UPPER, HIGH_LEVEL_MODE.LOWER, HIGH_LEVEL_MODE.DIGIT, HIGH_LEVEL_MODE.PUNCT]) {
188
+ const value = textValue(bytes[at], candidate);
189
+ if (value < 0)
190
+ continue;
191
+ const latchCost = candidate === mode ? 0 : LATCH[mode][candidate] >>> 16;
192
+ const cost = latchCost + characterWidth(candidate);
193
+ if (cost < bestCost) {
194
+ bestCost = cost;
195
+ bestMode = candidate;
196
+ bestValue = value;
197
+ }
198
+ }
199
+ if (bestMode >= 0) {
200
+ latch(writer, mode, bestMode);
201
+ writer.put(bestValue, characterWidth(bestMode));
202
+ mode = bestMode;
203
+ at++;
204
+ }
205
+ else {
206
+ // B/S is defined from UPPER; the latch is retained after the shift.
207
+ latch(writer, mode, HIGH_LEVEL_MODE.UPPER);
208
+ mode = HIGH_LEVEL_MODE.UPPER;
209
+ const start = at;
210
+ while (at < bytes.length && ![HIGH_LEVEL_MODE.UPPER, HIGH_LEVEL_MODE.LOWER, HIGH_LEVEL_MODE.DIGIT, HIGH_LEVEL_MODE.PUNCT].some((m) => textValue(bytes[at], m) >= 0))
211
+ at++;
212
+ writeBinaryShift(writer, bytes, start, at - start);
213
+ }
208
214
  }
209
- }
210
- return writer;
215
+ return writer;
211
216
  }
@@ -27,19 +27,8 @@
27
27
  *
28
28
  * Original work. No code from any other barcode implementation.
29
29
  */
30
-
31
30
  /** Aztec Code entry points. @module aztec */
32
-
33
31
  export { encodeAztec } from './encoder.js';
34
32
  export { decodeAztec } from './decoder.js';
35
33
  export { detectAztec, detectAndDecodeAztec } from './detector.js';
36
- export {
37
- AZTEC_COMPACT_LAYERS,
38
- AZTEC_FULL_LAYERS,
39
- AZTEC_LAYERS,
40
- AZTEC_DEFAULT_ECC_PERCENT,
41
- AZTEC_RS_GENERATOR_BASE,
42
- aztecLayer,
43
- aztecSymbolSize,
44
- validateAztecTables,
45
- } from './tables.js';
34
+ export { AZTEC_COMPACT_LAYERS, AZTEC_FULL_LAYERS, AZTEC_LAYERS, AZTEC_DEFAULT_ECC_PERCENT, AZTEC_RS_GENERATOR_BASE, aztecLayer, aztecSymbolSize, validateAztecTables, } from './tables.js';
@@ -27,7 +27,6 @@
27
27
  *
28
28
  * Original work. No code from any other barcode implementation.
29
29
  */
30
-
31
30
  /**
32
31
  * Aztec Code layer geometry and Reed-Solomon parameters.
33
32
  *
@@ -41,103 +40,90 @@
41
40
  *
42
41
  * @module aztec/tables
43
42
  */
44
-
45
43
  import { GF16, GF64, GF256_AZTEC, GF1024, GF4096 } from '../core/galois-field.js';
46
-
47
44
  /** Reed-Solomon generator base defined for Aztec parameter and data fields. */
48
45
  export const AZTEC_RS_GENERATOR_BASE = 1;
49
-
50
46
  /** Minimum recommended error correction: 23 percent plus three codewords. */
51
47
  export const AZTEC_DEFAULT_ECC_PERCENT = 23;
52
48
  export const AZTEC_MIN_ECC_WORDS = 3;
53
-
54
49
  /** Word size selected solely by the number of layers. */
55
50
  export function wordSizeForLayers(layers) {
56
- if (!Number.isInteger(layers) || layers < 1 || layers > 32) {
57
- throw new RangeError(`Aztec: layers must be an integer from 1 to 32 (got ${layers})`);
58
- }
59
- if (layers <= 2) return 6;
60
- if (layers <= 8) return 8;
61
- if (layers <= 22) return 10;
62
- return 12;
51
+ if (!Number.isInteger(layers) || layers < 1 || layers > 32) {
52
+ throw new RangeError(`Aztec: layers must be an integer from 1 to 32 (got ${layers})`);
53
+ }
54
+ if (layers <= 2)
55
+ return 6;
56
+ if (layers <= 8)
57
+ return 8;
58
+ if (layers <= 22)
59
+ return 10;
60
+ return 12;
63
61
  }
64
-
65
62
  /** Return the field used by Aztec codewords of `wordSize` bits. */
66
63
  export function fieldForWordSize(wordSize) {
67
- switch (wordSize) {
68
- case 4: return GF16; // Mode message only.
69
- case 6: return GF64;
70
- case 8: return GF256_AZTEC;
71
- case 10: return GF1024;
72
- case 12: return GF4096;
73
- default: throw new RangeError(`Aztec: unsupported codeword size ${wordSize}`);
74
- }
64
+ switch (wordSize) {
65
+ case 4: return GF16; // Mode message only.
66
+ case 6: return GF64;
67
+ case 8: return GF256_AZTEC;
68
+ case 10: return GF1024;
69
+ case 12: return GF4096;
70
+ default: throw new RangeError(`Aztec: unsupported codeword size ${wordSize}`);
71
+ }
75
72
  }
76
-
77
73
  /** Return the data field selected for a symbol with `layers` layers. */
78
74
  export function fieldForLayers(layers) {
79
- return fieldForWordSize(wordSizeForLayers(layers));
75
+ return fieldForWordSize(wordSizeForLayers(layers));
80
76
  }
81
-
82
77
  /** Matrix side length, including Full-mode reference grid lines. */
83
78
  export function aztecSymbolSize(layers, compact = false) {
84
- if (!Number.isInteger(layers) || layers < 1 || layers > (compact ? 4 : 32)) {
85
- throw new RangeError(`Aztec: ${compact ? 'Compact' : 'Full'} layers out of range: ${layers}`);
86
- }
87
- if (compact) return 11 + 4 * layers;
88
- const baseMatrixSize = 14 + 4 * layers;
89
- return baseMatrixSize + 1 + 2 * Math.floor((baseMatrixSize / 2 - 1) / 15);
79
+ if (!Number.isInteger(layers) || layers < 1 || layers > (compact ? 4 : 32)) {
80
+ throw new RangeError(`Aztec: ${compact ? 'Compact' : 'Full'} layers out of range: ${layers}`);
81
+ }
82
+ if (compact)
83
+ return 11 + 4 * layers;
84
+ const baseMatrixSize = 14 + 4 * layers;
85
+ return baseMatrixSize + 1 + 2 * Math.floor((baseMatrixSize / 2 - 1) / 15);
90
86
  }
91
-
92
87
  function layer(layers, compact) {
93
- const wordSize = wordSizeForLayers(layers);
94
- const totalBits = ((compact ? 88 : 112) + 16 * layers) * layers;
95
- const usableBits = totalBits - totalBits % wordSize;
96
- const totalCodewords = usableBits / wordSize;
97
- const baseMatrixSize = (compact ? 11 : 14) + 4 * layers;
98
- return Object.freeze({
99
- compact,
100
- layers,
101
- wordSize,
102
- totalBits,
103
- usableBits,
104
- totalCodewords,
105
- // Compact mode encodes the count in six bits and can therefore hold no
106
- // more than 64 data codewords even where the ring itself is larger.
107
- maxDataCodewords: compact ? Math.min(totalCodewords, 64) : totalCodewords,
108
- baseMatrixSize,
109
- symbolSize: aztecSymbolSize(layers, compact),
110
- modeMessageDataWords: compact ? 2 : 4,
111
- modeMessageWords: compact ? 7 : 10,
112
- modeMessageBits: compact ? 28 : 40,
113
- rsGeneratorBase: AZTEC_RS_GENERATOR_BASE,
114
- });
88
+ const wordSize = wordSizeForLayers(layers);
89
+ const totalBits = ((compact ? 88 : 112) + 16 * layers) * layers;
90
+ const usableBits = totalBits - totalBits % wordSize;
91
+ const totalCodewords = usableBits / wordSize;
92
+ const baseMatrixSize = (compact ? 11 : 14) + 4 * layers;
93
+ return Object.freeze({
94
+ compact,
95
+ layers,
96
+ wordSize,
97
+ totalBits,
98
+ usableBits,
99
+ totalCodewords,
100
+ // Compact mode encodes the count in six bits and can therefore hold no
101
+ // more than 64 data codewords even where the ring itself is larger.
102
+ maxDataCodewords: compact ? Math.min(totalCodewords, 64) : totalCodewords,
103
+ baseMatrixSize,
104
+ symbolSize: aztecSymbolSize(layers, compact),
105
+ modeMessageDataWords: compact ? 2 : 4,
106
+ modeMessageWords: compact ? 7 : 10,
107
+ modeMessageBits: compact ? 28 : 40,
108
+ rsGeneratorBase: AZTEC_RS_GENERATOR_BASE,
109
+ });
115
110
  }
116
-
117
111
  /** Compact Aztec layers 1 through 4, in encoding preference order. */
118
- export const AZTEC_COMPACT_LAYERS = Object.freeze(
119
- Array.from({ length: 4 }, (_, i) => layer(i + 1, true)),
120
- );
121
-
112
+ export const AZTEC_COMPACT_LAYERS = Object.freeze(Array.from({ length: 4 }, (_, i) => layer(i + 1, true)));
122
113
  /** Full Aztec layers 1 through 32, in ascending layer order. */
123
- export const AZTEC_FULL_LAYERS = Object.freeze(
124
- Array.from({ length: 32 }, (_, i) => layer(i + 1, false)),
125
- );
126
-
114
+ export const AZTEC_FULL_LAYERS = Object.freeze(Array.from({ length: 32 }, (_, i) => layer(i + 1, false)));
127
115
  /** All allowed symbols. Compact entries precede Full entries for automatic selection. */
128
116
  export const AZTEC_LAYERS = Object.freeze([
129
- ...AZTEC_COMPACT_LAYERS,
130
- ...AZTEC_FULL_LAYERS,
117
+ ...AZTEC_COMPACT_LAYERS,
118
+ ...AZTEC_FULL_LAYERS,
131
119
  ]);
132
-
133
120
  /** Return one immutable layer record. */
134
121
  export function aztecLayer(layers, compact = false) {
135
- if (!Number.isInteger(layers) || layers < 1 || layers > (compact ? 4 : 32)) {
136
- throw new RangeError(`Aztec: ${compact ? 'Compact' : 'Full'} layers out of range: ${layers}`);
137
- }
138
- return (compact ? AZTEC_COMPACT_LAYERS : AZTEC_FULL_LAYERS)[layers - 1];
122
+ if (!Number.isInteger(layers) || layers < 1 || layers > (compact ? 4 : 32)) {
123
+ throw new RangeError(`Aztec: ${compact ? 'Compact' : 'Full'} layers out of range: ${layers}`);
124
+ }
125
+ return (compact ? AZTEC_COMPACT_LAYERS : AZTEC_FULL_LAYERS)[layers - 1];
139
126
  }
140
-
141
127
  /**
142
128
  * Calculate the minimum parity count for a data word count.
143
129
  *
@@ -146,65 +132,65 @@ export function aztecLayer(layers, compact = false) {
146
132
  * percentage would otherwise round to zero.
147
133
  */
148
134
  export function eccCodewordsFor(dataCodewords, eccPercent = AZTEC_DEFAULT_ECC_PERCENT) {
149
- if (!Number.isInteger(dataCodewords) || dataCodewords < 0) {
150
- throw new RangeError(`Aztec: data codewords must be a non-negative integer (got ${dataCodewords})`);
151
- }
152
- if (!Number.isFinite(eccPercent) || eccPercent < 0 || eccPercent > 100) {
153
- throw new RangeError(`Aztec: ECC percent must be between 0 and 100 (got ${eccPercent})`);
154
- }
155
- return Math.ceil(dataCodewords * eccPercent / 100) + AZTEC_MIN_ECC_WORDS;
135
+ if (!Number.isInteger(dataCodewords) || dataCodewords < 0) {
136
+ throw new RangeError(`Aztec: data codewords must be a non-negative integer (got ${dataCodewords})`);
137
+ }
138
+ if (!Number.isFinite(eccPercent) || eccPercent < 0 || eccPercent > 100) {
139
+ throw new RangeError(`Aztec: ECC percent must be between 0 and 100 (got ${eccPercent})`);
140
+ }
141
+ return Math.ceil(dataCodewords * eccPercent / 100) + AZTEC_MIN_ECC_WORDS;
156
142
  }
157
-
158
143
  /**
159
144
  * Choose the first symbol which holds an already stuffed payload.
160
145
  *
161
146
  * `dataBits` must be a multiple of the candidate word size; callers which
162
147
  * start from high-level bits must stuff separately per candidate word size.
163
148
  */
164
- export function selectAztecLayer(dataBits, {
165
- eccPercent = AZTEC_DEFAULT_ECC_PERCENT,
166
- layers = null,
167
- compact = null,
168
- } = {}) {
169
- if (!Number.isInteger(dataBits) || dataBits < 0) {
170
- throw new RangeError(`Aztec: data bits must be a non-negative integer (got ${dataBits})`);
171
- }
172
- if (compact !== null && typeof compact !== 'boolean') {
173
- throw new TypeError('Aztec: compact must be true, false or null');
174
- }
175
-
176
- let candidates;
177
- if (layers !== null) {
178
- if (compact === null) throw new TypeError('Aztec: compact must be specified when layers is specified');
179
- candidates = [aztecLayer(layers, compact)];
180
- } else if (compact === null) {
181
- candidates = AZTEC_LAYERS;
182
- } else {
183
- candidates = compact ? AZTEC_COMPACT_LAYERS : AZTEC_FULL_LAYERS;
184
- }
185
-
186
- for (const candidate of candidates) {
187
- if (dataBits % candidate.wordSize !== 0) continue;
188
- const dataCodewords = dataBits / candidate.wordSize;
189
- const eccCodewords = eccCodewordsFor(dataCodewords, eccPercent);
190
- if (dataCodewords <= candidate.maxDataCodewords &&
191
- dataCodewords + eccCodewords <= candidate.totalCodewords) {
192
- return Object.freeze({ ...candidate, dataCodewords, eccCodewords });
149
+ export function selectAztecLayer(dataBits, { eccPercent = AZTEC_DEFAULT_ECC_PERCENT, layers = null, compact = null, } = {}) {
150
+ if (!Number.isInteger(dataBits) || dataBits < 0) {
151
+ throw new RangeError(`Aztec: data bits must be a non-negative integer (got ${dataBits})`);
193
152
  }
194
- }
195
-
196
- throw new RangeError('Aztec: payload and requested error correction do not fit an available symbol');
153
+ if (compact !== null && typeof compact !== 'boolean') {
154
+ throw new TypeError('Aztec: compact must be true, false or null');
155
+ }
156
+ let candidates;
157
+ if (layers !== null) {
158
+ if (compact === null)
159
+ throw new TypeError('Aztec: compact must be specified when layers is specified');
160
+ candidates = [aztecLayer(layers, compact)];
161
+ }
162
+ else if (compact === null) {
163
+ candidates = AZTEC_LAYERS;
164
+ }
165
+ else {
166
+ candidates = compact ? AZTEC_COMPACT_LAYERS : AZTEC_FULL_LAYERS;
167
+ }
168
+ for (const candidate of candidates) {
169
+ if (dataBits % candidate.wordSize !== 0)
170
+ continue;
171
+ const dataCodewords = dataBits / candidate.wordSize;
172
+ const eccCodewords = eccCodewordsFor(dataCodewords, eccPercent);
173
+ if (dataCodewords <= candidate.maxDataCodewords &&
174
+ dataCodewords + eccCodewords <= candidate.totalCodewords) {
175
+ return Object.freeze({ ...candidate, dataCodewords, eccCodewords });
176
+ }
177
+ }
178
+ throw new RangeError('Aztec: payload and requested error correction do not fit an available symbol');
197
179
  }
198
-
199
180
  /** Check static identities so table corruption fails explicitly in tests. */
200
181
  export function validateAztecTables() {
201
- const issues = [];
202
- for (const entry of AZTEC_LAYERS) {
203
- if (entry.usableBits % entry.wordSize !== 0) issues.push(`${entry.compact ? 'C' : 'F'}${entry.layers}: unaligned usable bits`);
204
- if (entry.totalCodewords !== entry.usableBits / entry.wordSize) issues.push(`${entry.compact ? 'C' : 'F'}${entry.layers}: codeword mismatch`);
205
- if (entry.symbolSize !== aztecSymbolSize(entry.layers, entry.compact)) issues.push(`${entry.compact ? 'C' : 'F'}${entry.layers}: matrix size mismatch`);
206
- if (entry.rsGeneratorBase !== AZTEC_RS_GENERATOR_BASE) issues.push(`${entry.compact ? 'C' : 'F'}${entry.layers}: generator base mismatch`);
207
- if (entry.compact && entry.maxDataCodewords > 64) issues.push(`C${entry.layers}: Compact data-word limit exceeded`);
208
- }
209
- return issues;
182
+ const issues = [];
183
+ for (const entry of AZTEC_LAYERS) {
184
+ if (entry.usableBits % entry.wordSize !== 0)
185
+ issues.push(`${entry.compact ? 'C' : 'F'}${entry.layers}: unaligned usable bits`);
186
+ if (entry.totalCodewords !== entry.usableBits / entry.wordSize)
187
+ issues.push(`${entry.compact ? 'C' : 'F'}${entry.layers}: codeword mismatch`);
188
+ if (entry.symbolSize !== aztecSymbolSize(entry.layers, entry.compact))
189
+ issues.push(`${entry.compact ? 'C' : 'F'}${entry.layers}: matrix size mismatch`);
190
+ if (entry.rsGeneratorBase !== AZTEC_RS_GENERATOR_BASE)
191
+ issues.push(`${entry.compact ? 'C' : 'F'}${entry.layers}: generator base mismatch`);
192
+ if (entry.compact && entry.maxDataCodewords > 64)
193
+ issues.push(`C${entry.layers}: Compact data-word limit exceeded`);
194
+ }
195
+ return issues;
210
196
  }