@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.
- package/LICENSE +5 -4
- package/NOTICE.md +2 -2
- package/README.md +61 -13
- package/bundle/sythos-barcode.esm.js +11658 -11244
- package/bundle/sythos-barcode.js +11658 -11244
- package/package.json +10 -3
- package/src/index.d.ts +1 -1
- package/src/index.js +508 -516
- package/src/js/aztec/decoder.js +208 -199
- package/src/js/aztec/detector.js +153 -126
- package/src/js/aztec/encoder.js +172 -153
- package/src/js/aztec/high-level.js +106 -101
- package/src/js/aztec/index.js +1 -12
- package/src/js/aztec/tables.js +106 -120
- package/src/js/aztecrune/decoder.js +76 -85
- package/src/js/aztecrune/detector.js +107 -93
- package/src/js/aztecrune/encoder.js +50 -68
- package/src/js/aztecrune/index.js +1 -16
- package/src/js/aztecrune/tables.js +59 -54
- package/src/js/compactpdf417/decoder.js +69 -74
- package/src/js/compactpdf417/detector.js +74 -75
- package/src/js/compactpdf417/encoder.js +68 -81
- package/src/js/compactpdf417/index.js +1 -13
- package/src/js/compactpdf417/tables.js +71 -80
- package/src/js/core/bit-buffer.js +106 -122
- package/src/js/core/bit-matrix.js +196 -194
- package/src/js/core/errors.js +13 -15
- package/src/js/core/galois-field.js +119 -133
- package/src/js/core/index.js +2 -19
- package/src/js/core/reed-solomon.js +195 -204
- package/src/js/databar/codec.js +93 -108
- package/src/js/databar/decoder.js +149 -140
- package/src/js/databar/encoder.js +47 -55
- package/src/js/databar/gs1.js +122 -123
- package/src/js/databar/index.js +4 -36
- package/src/js/databar/patterns.js +81 -85
- package/src/js/databar/tables.js +51 -52
- package/src/js/datamatrix/decoder.js +216 -182
- package/src/js/datamatrix/detector.js +161 -132
- package/src/js/datamatrix/encoder.js +192 -133
- package/src/js/datamatrix/index.js +1 -9
- package/src/js/datamatrix/tables.js +68 -69
- package/src/js/frameqr/decoder.js +102 -125
- package/src/js/frameqr/detector.js +101 -94
- package/src/js/frameqr/encoder.js +67 -80
- package/src/js/frameqr/index.js +1 -10
- package/src/js/frameqr/tables.js +145 -163
- package/src/js/image/binarizer.js +146 -162
- package/src/js/image/grid-sampler.js +63 -79
- package/src/js/image/index.js +0 -2
- package/src/js/image/luminance.js +129 -140
- package/src/js/image/perspective.js +112 -149
- package/src/js/micropdf417/compaction.js +64 -65
- package/src/js/micropdf417/decoder.js +106 -109
- package/src/js/micropdf417/detector.js +73 -70
- package/src/js/micropdf417/encoder.js +129 -154
- package/src/js/micropdf417/error-correction.js +6 -11
- package/src/js/micropdf417/index.js +2 -16
- package/src/js/micropdf417/tables.js +112 -99
- package/src/js/microqr/decoder.js +177 -182
- package/src/js/microqr/detector.js +240 -256
- package/src/js/microqr/encoder.js +239 -194
- package/src/js/microqr/index.js +0 -2
- package/src/js/microqr/tables.js +208 -200
- package/src/js/oned/addons.js +167 -191
- package/src/js/oned/index.js +27 -58
- package/src/js/oned/patterns.js +194 -213
- package/src/js/oned/reader.js +1003 -984
- package/src/js/oned/writers.js +411 -466
- package/src/js/pdf417/compaction.js +315 -222
- package/src/js/pdf417/decoder.js +71 -36
- package/src/js/pdf417/detector.js +377 -361
- package/src/js/pdf417/encoder.js +57 -46
- package/src/js/pdf417/error-correction.js +5 -8
- package/src/js/pdf417/tables.js +248 -250
- package/src/js/qr/decoder.js +311 -347
- package/src/js/qr/detector.js +378 -395
- package/src/js/qr/encoder.js +514 -599
- package/src/js/qr/index.js +0 -2
- package/src/js/qr/tables.js +375 -422
- package/src/js/render/image-data.js +57 -62
- package/src/js/render/index.js +21 -23
- package/src/js/render/options.js +82 -90
- package/src/js/render/png.js +144 -169
- package/src/js/render/svg.js +41 -44
- package/src/js/render/webgl.js +103 -109
- package/src/js/render/webgpu.js +205 -217
- package/src/js/rmqr/decoder.js +161 -53
- package/src/js/rmqr/detector.js +73 -41
- package/src/js/rmqr/encoder.js +253 -104
- package/src/js/rmqr/index.js +1 -5
- package/src/js/rmqr/tables.js +108 -85
- package/src/ts/aztec/decoder.ts +317 -0
- package/src/ts/aztec/detector.ts +224 -0
- package/src/ts/aztec/encoder.ts +257 -0
- package/src/ts/aztec/high-level.ts +211 -0
- package/src/ts/aztec/index.ts +45 -0
- package/src/ts/aztec/tables.ts +210 -0
- package/src/ts/aztecrune/decoder.ts +155 -0
- package/src/ts/aztecrune/detector.ts +166 -0
- package/src/ts/aztecrune/encoder.ts +121 -0
- package/src/ts/aztecrune/index.ts +49 -0
- package/src/ts/aztecrune/tables.ts +137 -0
- package/src/ts/compactpdf417/decoder.ts +128 -0
- package/src/ts/compactpdf417/detector.ts +139 -0
- package/src/ts/compactpdf417/encoder.ts +140 -0
- package/src/ts/compactpdf417/index.ts +17 -0
- package/src/ts/compactpdf417/tables.ts +173 -0
- package/src/ts/core/bit-buffer.ts +174 -0
- package/src/ts/core/bit-matrix.ts +241 -0
- package/src/ts/core/errors.ts +61 -0
- package/src/ts/core/galois-field.ts +207 -0
- package/src/ts/core/index.ts +56 -0
- package/src/ts/core/reed-solomon.ts +327 -0
- package/src/ts/databar/codec.ts +181 -0
- package/src/ts/databar/decoder.ts +215 -0
- package/src/ts/databar/encoder.ts +96 -0
- package/src/ts/databar/gs1.ts +177 -0
- package/src/ts/databar/index.ts +68 -0
- package/src/ts/databar/patterns.ts +143 -0
- package/src/ts/databar/tables.ts +124 -0
- package/src/ts/datamatrix/decoder.ts +262 -0
- package/src/ts/datamatrix/detector.ts +225 -0
- package/src/ts/datamatrix/encoder.ts +191 -0
- package/src/ts/datamatrix/index.ts +42 -0
- package/src/ts/datamatrix/tables.ts +123 -0
- package/src/ts/frameqr/decoder.ts +239 -0
- package/src/ts/frameqr/detector.ts +192 -0
- package/src/ts/frameqr/encoder.ts +156 -0
- package/src/ts/frameqr/index.ts +42 -0
- package/src/ts/frameqr/tables.ts +270 -0
- package/src/ts/image/binarizer.ts +270 -0
- package/src/ts/image/grid-sampler.ts +164 -0
- package/src/ts/image/index.ts +40 -0
- package/src/ts/image/luminance.ts +196 -0
- package/src/ts/image/perspective.ts +195 -0
- package/src/ts/index.d.ts +1 -1
- package/src/ts/index.ts +790 -0
- package/src/ts/micropdf417/compaction.ts +116 -0
- package/src/ts/micropdf417/decoder.ts +183 -0
- package/src/ts/micropdf417/detector.ts +149 -0
- package/src/ts/micropdf417/encoder.ts +209 -0
- package/src/ts/micropdf417/error-correction.ts +55 -0
- package/src/ts/micropdf417/index.ts +49 -0
- package/src/ts/micropdf417/tables.ts +184 -0
- package/src/ts/microqr/decoder.ts +245 -0
- package/src/ts/microqr/detector.ts +355 -0
- package/src/ts/microqr/encoder.ts +269 -0
- package/src/ts/microqr/index.ts +36 -0
- package/src/ts/microqr/tables.ts +316 -0
- package/src/ts/oned/addons.ts +420 -0
- package/src/ts/oned/index.ts +106 -0
- package/src/ts/oned/patterns.ts +384 -0
- package/src/ts/oned/reader.ts +1364 -0
- package/src/ts/oned/writers.ts +746 -0
- package/src/ts/pdf417/compaction.ts +298 -0
- package/src/ts/pdf417/decoder.ts +75 -0
- package/src/ts/pdf417/detector.ts +468 -0
- package/src/ts/pdf417/encoder.ts +91 -0
- package/src/ts/pdf417/error-correction.ts +47 -0
- package/src/ts/pdf417/index.ts +6 -0
- package/src/ts/pdf417/tables.ts +317 -0
- package/src/ts/qr/decoder.ts +575 -0
- package/src/ts/qr/detector.ts +630 -0
- package/src/ts/qr/encoder.ts +958 -0
- package/src/ts/qr/index.ts +44 -0
- package/src/ts/qr/tables.ts +737 -0
- package/src/ts/render/image-data.ts +125 -0
- package/src/ts/render/index.ts +130 -0
- package/src/ts/render/options.ts +160 -0
- package/src/ts/render/png.ts +295 -0
- package/src/ts/render/svg.ts +120 -0
- package/src/ts/render/webgl.ts +206 -0
- package/src/ts/render/webgpu.ts +369 -0
- package/src/ts/rmqr/decoder.ts +101 -0
- package/src/ts/rmqr/detector.ts +90 -0
- package/src/ts/rmqr/encoder.ts +172 -0
- package/src/ts/rmqr/index.ts +37 -0
- package/src/ts/rmqr/tables.ts +154 -0
package/src/js/aztec/encoder.js
CHANGED
|
@@ -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 encoder: high-level bits, bit stuffing, Reed-Solomon and matrix layout.
|
|
33
32
|
*
|
|
@@ -39,26 +38,23 @@
|
|
|
39
38
|
*
|
|
40
39
|
* @module aztec/encoder
|
|
41
40
|
*/
|
|
42
|
-
|
|
43
41
|
import { BitWriter } from '../core/bit-buffer.js';
|
|
44
42
|
import { BitMatrix } from '../core/bit-matrix.js';
|
|
45
43
|
import { EncodeError } from '../core/errors.js';
|
|
46
44
|
import { rsEncode } from '../core/reed-solomon.js';
|
|
47
45
|
import { encodeHighLevel } from './high-level.js';
|
|
48
46
|
import { AZTEC_COMPACT_LAYERS, AZTEC_FULL_LAYERS, aztecLayer, eccCodewordsFor, fieldForLayers, fieldForWordSize, wordSizeForLayers } from './tables.js';
|
|
49
|
-
|
|
50
47
|
/** @param {BitWriter} bits @param {number} at @returns {boolean} */
|
|
51
48
|
function bitAt(bits, at) {
|
|
52
|
-
|
|
49
|
+
return at >= 0 && at < bits.length && ((bits.bytes[at >>> 3] >>> (7 - (at & 7))) & 1) !== 0;
|
|
53
50
|
}
|
|
54
|
-
|
|
55
51
|
/** @param {BitWriter} bits @param {number} from @param {number} count @returns {number} */
|
|
56
52
|
function readBits(bits, from, count) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
53
|
+
let value = 0;
|
|
54
|
+
for (let i = 0; i < count; i++)
|
|
55
|
+
value = (value << 1) | (bitAt(bits, from + i) ? 1 : 0);
|
|
56
|
+
return value;
|
|
60
57
|
}
|
|
61
|
-
|
|
62
58
|
/**
|
|
63
59
|
* Prevent all-zero and all-one codewords except their final bit. The final
|
|
64
60
|
* bit is intentionally re-consumed after a stuffed word; it is the mechanism
|
|
@@ -67,23 +63,24 @@ function readBits(bits, from, count) {
|
|
|
67
63
|
* @param {BitWriter} bits @param {number} wordSize @returns {BitWriter}
|
|
68
64
|
*/
|
|
69
65
|
export function stuffBits(bits, wordSize) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
66
|
+
const out = new BitWriter();
|
|
67
|
+
const reserved = (1 << wordSize) - 2;
|
|
68
|
+
for (let at = 0; at < bits.length; at += wordSize) {
|
|
69
|
+
const word = readBits(bits, at, wordSize);
|
|
70
|
+
if ((word & reserved) === reserved) {
|
|
71
|
+
out.put(word & reserved, wordSize);
|
|
72
|
+
at--;
|
|
73
|
+
}
|
|
74
|
+
else if ((word & reserved) === 0) {
|
|
75
|
+
out.put(word | 1, wordSize);
|
|
76
|
+
at--;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
out.put(word, wordSize);
|
|
80
|
+
}
|
|
82
81
|
}
|
|
83
|
-
|
|
84
|
-
return out;
|
|
82
|
+
return out;
|
|
85
83
|
}
|
|
86
|
-
|
|
87
84
|
/**
|
|
88
85
|
* Add systematic Aztec Reed-Solomon parity and the leading alignment bits.
|
|
89
86
|
* @param {BitWriter} data @param {number} totalBits @param {number} wordSize
|
|
@@ -91,137 +88,155 @@ export function stuffBits(bits, wordSize) {
|
|
|
91
88
|
* @returns {{bits: BitWriter, dataWords: number, eccWords: number}}
|
|
92
89
|
*/
|
|
93
90
|
export function addCheckWords(data, totalBits, wordSize, field) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
91
|
+
const totalWords = Math.floor(totalBits / wordSize);
|
|
92
|
+
const dataWords = Math.ceil(data.length / wordSize);
|
|
93
|
+
if (dataWords > totalWords)
|
|
94
|
+
throw new EncodeError('Aztec: data codewords exceed layer capacity');
|
|
95
|
+
const eccWords = totalWords - dataWords;
|
|
96
|
+
const words = new Array(dataWords);
|
|
97
|
+
for (let i = 0; i < dataWords; i++)
|
|
98
|
+
words[i] = readBits(data, i * wordSize, wordSize);
|
|
99
|
+
const ecc = rsEncode(words, eccWords, field, 1);
|
|
100
|
+
const out = new BitWriter();
|
|
101
|
+
out.put(0, totalBits % wordSize);
|
|
102
|
+
for (const word of words)
|
|
103
|
+
out.put(word, wordSize);
|
|
104
|
+
for (const word of ecc)
|
|
105
|
+
out.put(word, wordSize);
|
|
106
|
+
return { bits: out, dataWords, eccWords };
|
|
106
107
|
}
|
|
107
|
-
|
|
108
108
|
/** @param {number} layers @param {number} dataWords @param {boolean} compact @returns {BitWriter} */
|
|
109
109
|
export function modeMessage(layers, dataWords, compact) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
110
|
+
const raw = new BitWriter();
|
|
111
|
+
if (compact) {
|
|
112
|
+
raw.put(layers - 1, 2);
|
|
113
|
+
raw.put(dataWords - 1, 6);
|
|
114
|
+
return addCheckWords(raw, 28, 4, fieldForWordSize(4)).bits;
|
|
115
|
+
}
|
|
116
|
+
raw.put(layers - 1, 5);
|
|
117
|
+
raw.put(dataWords - 1, 11);
|
|
118
|
+
return addCheckWords(raw, 40, 4, fieldForWordSize(4)).bits;
|
|
119
119
|
}
|
|
120
|
-
|
|
121
120
|
/** @param {BitMatrix} matrix @param {number} center @param {number} size */
|
|
122
121
|
function drawBullsEye(matrix, center, size) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
122
|
+
for (let ring = 0; ring < size; ring += 2) {
|
|
123
|
+
for (let p = center - ring; p <= center + ring; p++) {
|
|
124
|
+
matrix.set(p, center - ring);
|
|
125
|
+
matrix.set(p, center + ring);
|
|
126
|
+
matrix.set(center - ring, p);
|
|
127
|
+
matrix.set(center + ring, p);
|
|
128
|
+
}
|
|
127
129
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
matrix.set(center + size, center + size - 1);
|
|
130
|
+
matrix.set(center - size, center - size);
|
|
131
|
+
matrix.set(center - size + 1, center - size);
|
|
132
|
+
matrix.set(center - size, center - size + 1);
|
|
133
|
+
matrix.set(center + size, center - size);
|
|
134
|
+
matrix.set(center + size, center - size + 1);
|
|
135
|
+
matrix.set(center + size, center + size - 1);
|
|
135
136
|
}
|
|
136
|
-
|
|
137
137
|
/** @param {BitMatrix} matrix @param {BitWriter} message @param {boolean} compact @param {number} center */
|
|
138
138
|
function drawModeMessage(matrix, message, compact, center) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
139
|
+
if (compact) {
|
|
140
|
+
for (let i = 0; i < 7; i++) {
|
|
141
|
+
const offset = center - 3 + i;
|
|
142
|
+
if (bitAt(message, i))
|
|
143
|
+
matrix.set(offset, center - 5);
|
|
144
|
+
if (bitAt(message, i + 7))
|
|
145
|
+
matrix.set(center + 5, offset);
|
|
146
|
+
if (bitAt(message, 20 - i))
|
|
147
|
+
matrix.set(offset, center + 5);
|
|
148
|
+
if (bitAt(message, 27 - i))
|
|
149
|
+
matrix.set(center - 5, offset);
|
|
150
|
+
}
|
|
146
151
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
152
|
+
else {
|
|
153
|
+
for (let i = 0; i < 10; i++) {
|
|
154
|
+
const offset = center - 5 + i + Math.floor(i / 5);
|
|
155
|
+
if (bitAt(message, i))
|
|
156
|
+
matrix.set(offset, center - 7);
|
|
157
|
+
if (bitAt(message, i + 10))
|
|
158
|
+
matrix.set(center + 7, offset);
|
|
159
|
+
if (bitAt(message, 29 - i))
|
|
160
|
+
matrix.set(offset, center + 7);
|
|
161
|
+
if (bitAt(message, 39 - i))
|
|
162
|
+
matrix.set(center - 7, offset);
|
|
163
|
+
}
|
|
154
164
|
}
|
|
155
|
-
}
|
|
156
165
|
}
|
|
157
|
-
|
|
158
166
|
/**
|
|
159
167
|
* Lay low-level bits in the four-sided, inward Aztec spiral.
|
|
160
168
|
* @param {BitWriter} bits @param {{layers:number,compact:boolean,baseMatrixSize:number,symbolSize:number}} symbol
|
|
161
169
|
* @returns {BitMatrix}
|
|
162
170
|
*/
|
|
163
171
|
export function buildAztecMatrix(bits, symbol) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
172
|
+
const { layers, compact, baseMatrixSize, symbolSize } = symbol;
|
|
173
|
+
const matrix = new BitMatrix(symbolSize);
|
|
174
|
+
const alignment = new Int32Array(baseMatrixSize);
|
|
175
|
+
const center = Math.floor(symbolSize / 2);
|
|
176
|
+
if (compact) {
|
|
177
|
+
for (let i = 0; i < baseMatrixSize; i++)
|
|
178
|
+
alignment[i] = i;
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
const originalCenter = Math.floor(baseMatrixSize / 2);
|
|
182
|
+
for (let i = 0; i < originalCenter; i++) {
|
|
183
|
+
const offset = i + Math.floor(i / 15);
|
|
184
|
+
alignment[originalCenter - i - 1] = center - offset - 1;
|
|
185
|
+
alignment[originalCenter + i] = center + offset + 1;
|
|
186
|
+
}
|
|
177
187
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
188
|
+
let bit = 0;
|
|
189
|
+
for (let layer = 0; layer < layers; layer++) {
|
|
190
|
+
const rowSize = (layers - layer) * 4 + (compact ? 9 : 12);
|
|
191
|
+
const low = layer * 2;
|
|
192
|
+
const high = baseMatrixSize - 1 - low;
|
|
193
|
+
for (let j = 0; j < rowSize; j++) {
|
|
194
|
+
const offset = j * 2;
|
|
195
|
+
for (let k = 0; k < 2; k++) {
|
|
196
|
+
if (bitAt(bits, bit + offset + k))
|
|
197
|
+
matrix.set(alignment[low + k], alignment[low + j]);
|
|
198
|
+
if (bitAt(bits, bit + rowSize * 2 + offset + k))
|
|
199
|
+
matrix.set(alignment[low + j], alignment[high - k]);
|
|
200
|
+
if (bitAt(bits, bit + rowSize * 4 + offset + k))
|
|
201
|
+
matrix.set(alignment[high - k], alignment[high - j]);
|
|
202
|
+
if (bitAt(bits, bit + rowSize * 6 + offset + k))
|
|
203
|
+
matrix.set(alignment[high - j], alignment[low + k]);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
bit += rowSize * 8;
|
|
193
207
|
}
|
|
194
|
-
bit
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
+
if (bit !== bits.length)
|
|
209
|
+
throw new EncodeError(`Aztec: layout consumed ${bit} of ${bits.length} bits`);
|
|
210
|
+
const mode = modeMessage(layers, symbol.dataWords, compact);
|
|
211
|
+
drawModeMessage(matrix, mode, compact, center);
|
|
212
|
+
drawBullsEye(matrix, center, compact ? 5 : 7);
|
|
213
|
+
if (!compact) {
|
|
214
|
+
for (let i = 0, offset = 0; i < Math.floor(baseMatrixSize / 2) - 1; i += 15, offset += 16) {
|
|
215
|
+
for (let p = center & 1; p < symbolSize; p += 2) {
|
|
216
|
+
matrix.set(center - offset, p);
|
|
217
|
+
matrix.set(center + offset, p);
|
|
218
|
+
matrix.set(p, center - offset);
|
|
219
|
+
matrix.set(p, center + offset);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
208
222
|
}
|
|
209
|
-
|
|
210
|
-
return matrix;
|
|
223
|
+
return matrix;
|
|
211
224
|
}
|
|
212
|
-
|
|
213
225
|
/** @param {number | undefined} layers @param {boolean | undefined} compact */
|
|
214
226
|
function candidates(layers, compact) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
227
|
+
if (layers !== undefined) {
|
|
228
|
+
if (!Number.isInteger(layers) || layers < 1 || layers > 32)
|
|
229
|
+
throw new EncodeError('Aztec: layers must be an integer 1..32');
|
|
230
|
+
if (compact === true && layers > 4)
|
|
231
|
+
throw new EncodeError('Aztec: compact symbols support layers 1..4');
|
|
232
|
+
return [aztecLayer(layers, compact === true)];
|
|
233
|
+
}
|
|
234
|
+
if (compact === true)
|
|
235
|
+
return AZTEC_COMPACT_LAYERS;
|
|
236
|
+
if (compact === false)
|
|
237
|
+
return AZTEC_FULL_LAYERS;
|
|
238
|
+
return [...AZTEC_COMPACT_LAYERS, ...AZTEC_FULL_LAYERS];
|
|
223
239
|
}
|
|
224
|
-
|
|
225
240
|
/**
|
|
226
241
|
* Encode a UTF-8 string or bytes into an Aztec Code matrix.
|
|
227
242
|
*
|
|
@@ -230,28 +245,32 @@ function candidates(layers, compact) {
|
|
|
230
245
|
* @returns {BitMatrix & {format?:string,layers?:number,compact?:boolean,eccPercent?:number,dataCodewords?:number}}
|
|
231
246
|
*/
|
|
232
247
|
export function encodeAztec(value, options = {}) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
248
|
+
const eccPercent = options.eccPercent ?? 23;
|
|
249
|
+
if (!Number.isFinite(eccPercent) || eccPercent < 5 || eccPercent > 95) {
|
|
250
|
+
throw new EncodeError('Aztec: eccPercent must be between 5 and 95');
|
|
251
|
+
}
|
|
252
|
+
const high = encodeHighLevel(value, { charset: options.charset ?? 'utf-8' });
|
|
253
|
+
for (const candidate of candidates(options.layers, options.compact)) {
|
|
254
|
+
if (!candidate)
|
|
255
|
+
continue;
|
|
256
|
+
const wordSize = wordSizeForLayers(candidate.layers);
|
|
257
|
+
const stuffed = stuffBits(high, wordSize);
|
|
258
|
+
const dataWords = Math.ceil(stuffed.length / wordSize);
|
|
259
|
+
const eccWords = eccCodewordsFor(dataWords, eccPercent);
|
|
260
|
+
if (dataWords > candidate.maxDataCodewords || dataWords + eccWords > candidate.totalCodewords)
|
|
261
|
+
continue;
|
|
262
|
+
const checked = addCheckWords(stuffed, candidate.totalBits, wordSize, fieldForLayers(candidate.layers));
|
|
263
|
+
// `addCheckWords` uses every remaining word as parity. This is stronger
|
|
264
|
+
// than the requested percentage, never weaker, and canonical for a chosen
|
|
265
|
+
// layer/data-word combination.
|
|
266
|
+
const symbol = { ...candidate, dataWords: checked.dataWords };
|
|
267
|
+
const matrix = buildAztecMatrix(checked.bits, symbol);
|
|
268
|
+
matrix.format = 'aztec';
|
|
269
|
+
matrix.layers = candidate.layers;
|
|
270
|
+
matrix.compact = candidate.compact;
|
|
271
|
+
matrix.eccPercent = Math.round(checked.eccWords * wordSize * 100 / Math.max(1, stuffed.length));
|
|
272
|
+
matrix.dataCodewords = checked.dataWords;
|
|
273
|
+
return matrix;
|
|
274
|
+
}
|
|
275
|
+
throw new EncodeError('Aztec: payload does not fit the requested layers and error correction');
|
|
257
276
|
}
|