@sythos/js_barcode_universal 1.5.8 → 1.5.9
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 +17 -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
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
*
|
|
28
28
|
* Original work. No code from any other barcode implementation.
|
|
29
29
|
*/
|
|
30
|
-
|
|
31
30
|
/**
|
|
32
31
|
* A 2D bit grid — the common currency of this library.
|
|
33
32
|
*
|
|
@@ -43,199 +42,202 @@
|
|
|
43
42
|
*
|
|
44
43
|
* @module core/bit-matrix
|
|
45
44
|
*/
|
|
46
|
-
|
|
47
45
|
export class BitMatrix {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
this.width = width;
|
|
57
|
-
this.height = height;
|
|
58
|
-
this.rowWords = Math.ceil(width / 32);
|
|
59
|
-
this.bits = new Uint32Array(this.rowWords * height);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* @param {number} x @param {number} y
|
|
64
|
-
* @returns {boolean} True if the module is dark.
|
|
65
|
-
*/
|
|
66
|
-
get(x, y) {
|
|
67
|
-
if (x < 0 || y < 0 || x >= this.width || y >= this.height) return false;
|
|
68
|
-
return ((this.bits[y * this.rowWords + (x >>> 5)] >>> (x & 31)) & 1) === 1;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** @param {number} x @param {number} y */
|
|
72
|
-
set(x, y) {
|
|
73
|
-
if (x < 0 || y < 0 || x >= this.width || y >= this.height) return;
|
|
74
|
-
this.bits[y * this.rowWords + (x >>> 5)] |= 1 << (x & 31);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** @param {number} x @param {number} y */
|
|
78
|
-
unset(x, y) {
|
|
79
|
-
if (x < 0 || y < 0 || x >= this.width || y >= this.height) return;
|
|
80
|
-
this.bits[y * this.rowWords + (x >>> 5)] &= ~(1 << (x & 31));
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** @param {number} x @param {number} y */
|
|
84
|
-
flip(x, y) {
|
|
85
|
-
if (x < 0 || y < 0 || x >= this.width || y >= this.height) return;
|
|
86
|
-
this.bits[y * this.rowWords + (x >>> 5)] ^= 1 << (x & 31);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* @param {number} x @param {number} y @param {boolean} value
|
|
91
|
-
*/
|
|
92
|
-
setValue(x, y, value) {
|
|
93
|
-
if (value) this.set(x, y);
|
|
94
|
-
else this.unset(x, y);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/** Fill a rectangle. @param {number} x @param {number} y @param {number} w @param {number} h */
|
|
98
|
-
setRegion(x, y, w, h) {
|
|
99
|
-
for (let j = y; j < y + h; j++) {
|
|
100
|
-
for (let i = x; i < x + w; i++) this.set(i, j);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
clear() {
|
|
105
|
-
this.bits.fill(0);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** @returns {BitMatrix} */
|
|
109
|
-
clone() {
|
|
110
|
-
const m = new BitMatrix(this.width, this.height);
|
|
111
|
-
m.bits.set(this.bits);
|
|
112
|
-
return m;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Copy row `y` into a reusable array, avoiding an allocation per row in the
|
|
117
|
-
* 1D scanning loops which run this thousands of times per image.
|
|
118
|
-
*
|
|
119
|
-
* @param {number} y
|
|
120
|
-
* @param {Uint8Array} [out]
|
|
121
|
-
* @returns {Uint8Array}
|
|
122
|
-
*/
|
|
123
|
-
getRow(y, out) {
|
|
124
|
-
const row = out && out.length >= this.width ? out : new Uint8Array(this.width);
|
|
125
|
-
const base = y * this.rowWords;
|
|
126
|
-
for (let x = 0; x < this.width; x++) {
|
|
127
|
-
row[x] = (this.bits[base + (x >>> 5)] >>> (x & 31)) & 1;
|
|
128
|
-
}
|
|
129
|
-
return row;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Add a uniform light border. Symbols need a quiet zone to be scannable at
|
|
134
|
-
* all, so this is applied by default when rendering.
|
|
135
|
-
*
|
|
136
|
-
* @param {number} size Modules of margin on every side.
|
|
137
|
-
* @returns {BitMatrix}
|
|
138
|
-
*/
|
|
139
|
-
withMargin(size) {
|
|
140
|
-
if (size <= 0) return this.clone();
|
|
141
|
-
const m = new BitMatrix(this.width + size * 2, this.height + size * 2);
|
|
142
|
-
for (let y = 0; y < this.height; y++) {
|
|
143
|
-
for (let x = 0; x < this.width; x++) {
|
|
144
|
-
if (this.get(x, y)) m.set(x + size, y + size);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return m;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Nearest-neighbour upscale. Integer factors only — a barcode resampled with
|
|
152
|
-
* interpolation stops being readable.
|
|
153
|
-
*
|
|
154
|
-
* @param {number} factor
|
|
155
|
-
* @returns {BitMatrix}
|
|
156
|
-
*/
|
|
157
|
-
scale(factor) {
|
|
158
|
-
const f = Math.max(1, Math.floor(factor));
|
|
159
|
-
if (f === 1) return this.clone();
|
|
160
|
-
const m = new BitMatrix(this.width * f, this.height * f);
|
|
161
|
-
for (let y = 0; y < this.height; y++) {
|
|
162
|
-
for (let x = 0; x < this.width; x++) {
|
|
163
|
-
if (this.get(x, y)) m.setRegion(x * f, y * f, f, f);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return m;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Bounding box of the dark modules, or null if the matrix is empty.
|
|
171
|
-
* @returns {{x: number, y: number, width: number, height: number} | null}
|
|
172
|
-
*/
|
|
173
|
-
getBounds() {
|
|
174
|
-
let minX = this.width, minY = this.height, maxX = -1, maxY = -1;
|
|
175
|
-
for (let y = 0; y < this.height; y++) {
|
|
176
|
-
for (let x = 0; x < this.width; x++) {
|
|
177
|
-
if (this.get(x, y)) {
|
|
178
|
-
if (x < minX) minX = x;
|
|
179
|
-
if (x > maxX) maxX = x;
|
|
180
|
-
if (y < minY) minY = y;
|
|
181
|
-
if (y > maxY) maxY = y;
|
|
46
|
+
/**
|
|
47
|
+
* @param {number} width
|
|
48
|
+
* @param {number} height
|
|
49
|
+
*/
|
|
50
|
+
constructor(width, height = width) {
|
|
51
|
+
if (width < 1 || height < 1) {
|
|
52
|
+
throw new Error(`BitMatrix: dimensions must be positive, got ${width}x${height}`);
|
|
182
53
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
this.
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
54
|
+
this.width = width;
|
|
55
|
+
this.height = height;
|
|
56
|
+
this.rowWords = Math.ceil(width / 32);
|
|
57
|
+
this.bits = new Uint32Array(this.rowWords * height);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @param {number} x @param {number} y
|
|
61
|
+
* @returns {boolean} True if the module is dark.
|
|
62
|
+
*/
|
|
63
|
+
get(x, y) {
|
|
64
|
+
if (x < 0 || y < 0 || x >= this.width || y >= this.height)
|
|
65
|
+
return false;
|
|
66
|
+
return ((this.bits[y * this.rowWords + (x >>> 5)] >>> (x & 31)) & 1) === 1;
|
|
67
|
+
}
|
|
68
|
+
/** @param {number} x @param {number} y */
|
|
69
|
+
set(x, y) {
|
|
70
|
+
if (x < 0 || y < 0 || x >= this.width || y >= this.height)
|
|
71
|
+
return;
|
|
72
|
+
this.bits[y * this.rowWords + (x >>> 5)] |= 1 << (x & 31);
|
|
73
|
+
}
|
|
74
|
+
/** @param {number} x @param {number} y */
|
|
75
|
+
unset(x, y) {
|
|
76
|
+
if (x < 0 || y < 0 || x >= this.width || y >= this.height)
|
|
77
|
+
return;
|
|
78
|
+
this.bits[y * this.rowWords + (x >>> 5)] &= ~(1 << (x & 31));
|
|
79
|
+
}
|
|
80
|
+
/** @param {number} x @param {number} y */
|
|
81
|
+
flip(x, y) {
|
|
82
|
+
if (x < 0 || y < 0 || x >= this.width || y >= this.height)
|
|
83
|
+
return;
|
|
84
|
+
this.bits[y * this.rowWords + (x >>> 5)] ^= 1 << (x & 31);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @param {number} x @param {number} y @param {boolean} value
|
|
88
|
+
*/
|
|
89
|
+
setValue(x, y, value) {
|
|
90
|
+
if (value)
|
|
91
|
+
this.set(x, y);
|
|
92
|
+
else
|
|
93
|
+
this.unset(x, y);
|
|
94
|
+
}
|
|
95
|
+
/** Fill a rectangle. @param {number} x @param {number} y @param {number} w @param {number} h */
|
|
96
|
+
setRegion(x, y, w, h) {
|
|
97
|
+
for (let j = y; j < y + h; j++) {
|
|
98
|
+
for (let i = x; i < x + w; i++)
|
|
99
|
+
this.set(i, j);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
clear() {
|
|
103
|
+
this.bits.fill(0);
|
|
104
|
+
}
|
|
105
|
+
/** @returns {BitMatrix} */
|
|
106
|
+
clone() {
|
|
107
|
+
const m = new BitMatrix(this.width, this.height);
|
|
108
|
+
m.bits.set(this.bits);
|
|
109
|
+
return m;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Copy row `y` into a reusable array, avoiding an allocation per row in the
|
|
113
|
+
* 1D scanning loops which run this thousands of times per image.
|
|
114
|
+
*
|
|
115
|
+
* @param {number} y
|
|
116
|
+
* @param {Uint8Array} [out]
|
|
117
|
+
* @returns {Uint8Array}
|
|
118
|
+
*/
|
|
119
|
+
getRow(y, out) {
|
|
120
|
+
const row = out && out.length >= this.width ? out : new Uint8Array(this.width);
|
|
121
|
+
const base = y * this.rowWords;
|
|
122
|
+
for (let x = 0; x < this.width; x++) {
|
|
123
|
+
row[x] = (this.bits[base + (x >>> 5)] >>> (x & 31)) & 1;
|
|
124
|
+
}
|
|
125
|
+
return row;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Add a uniform light border. Symbols need a quiet zone to be scannable at
|
|
129
|
+
* all, so this is applied by default when rendering.
|
|
130
|
+
*
|
|
131
|
+
* @param {number} size Modules of margin on every side.
|
|
132
|
+
* @returns {BitMatrix}
|
|
133
|
+
*/
|
|
134
|
+
withMargin(size) {
|
|
135
|
+
if (size <= 0)
|
|
136
|
+
return this.clone();
|
|
137
|
+
const m = new BitMatrix(this.width + size * 2, this.height + size * 2);
|
|
138
|
+
for (let y = 0; y < this.height; y++) {
|
|
139
|
+
for (let x = 0; x < this.width; x++) {
|
|
140
|
+
if (this.get(x, y))
|
|
141
|
+
m.set(x + size, y + size);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return m;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Nearest-neighbour upscale. Integer factors only — a barcode resampled with
|
|
148
|
+
* interpolation stops being readable.
|
|
149
|
+
*
|
|
150
|
+
* @param {number} factor
|
|
151
|
+
* @returns {BitMatrix}
|
|
152
|
+
*/
|
|
153
|
+
scale(factor) {
|
|
154
|
+
const f = Math.max(1, Math.floor(factor));
|
|
155
|
+
if (f === 1)
|
|
156
|
+
return this.clone();
|
|
157
|
+
const m = new BitMatrix(this.width * f, this.height * f);
|
|
158
|
+
for (let y = 0; y < this.height; y++) {
|
|
159
|
+
for (let x = 0; x < this.width; x++) {
|
|
160
|
+
if (this.get(x, y))
|
|
161
|
+
m.setRegion(x * f, y * f, f, f);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return m;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Bounding box of the dark modules, or null if the matrix is empty.
|
|
168
|
+
* @returns {{x: number, y: number, width: number, height: number} | null}
|
|
169
|
+
*/
|
|
170
|
+
getBounds() {
|
|
171
|
+
let minX = this.width, minY = this.height, maxX = -1, maxY = -1;
|
|
172
|
+
for (let y = 0; y < this.height; y++) {
|
|
173
|
+
for (let x = 0; x < this.width; x++) {
|
|
174
|
+
if (this.get(x, y)) {
|
|
175
|
+
if (x < minX)
|
|
176
|
+
minX = x;
|
|
177
|
+
if (x > maxX)
|
|
178
|
+
maxX = x;
|
|
179
|
+
if (y < minY)
|
|
180
|
+
minY = y;
|
|
181
|
+
if (y > maxY)
|
|
182
|
+
maxY = y;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (maxX < 0)
|
|
187
|
+
return null;
|
|
188
|
+
return { x: minX, y: minY, width: maxX - minX + 1, height: maxY - minY + 1 };
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Rotate 180 degrees, in place. Cheaper than re-detecting when a reader
|
|
192
|
+
* discovers a symbol is upside down.
|
|
193
|
+
*/
|
|
194
|
+
rotate180() {
|
|
195
|
+
const w = this.width, h = this.height;
|
|
196
|
+
for (let y = 0; y < Math.ceil(h / 2); y++) {
|
|
197
|
+
for (let x = 0; x < w; x++) {
|
|
198
|
+
const oy = h - 1 - y, ox = w - 1 - x;
|
|
199
|
+
if (y === oy && x >= ox)
|
|
200
|
+
break;
|
|
201
|
+
const a = this.get(x, y);
|
|
202
|
+
const b = this.get(ox, oy);
|
|
203
|
+
this.setValue(x, y, b);
|
|
204
|
+
this.setValue(ox, oy, a);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Build from a string of '1'/'X'/'#' (dark) and anything else (light),
|
|
210
|
+
* newline-separated. Test fixtures are far more legible this way.
|
|
211
|
+
*
|
|
212
|
+
* @param {string} text
|
|
213
|
+
* @returns {BitMatrix}
|
|
214
|
+
*/
|
|
215
|
+
static parse(text) {
|
|
216
|
+
const lines = text.trim().split('\n').map((l) => l.trim()).filter((l) => l.length);
|
|
217
|
+
const height = lines.length;
|
|
218
|
+
const width = Math.max(...lines.map((l) => l.length));
|
|
219
|
+
const m = new BitMatrix(width, height);
|
|
220
|
+
for (let y = 0; y < height; y++) {
|
|
221
|
+
for (let x = 0; x < lines[y].length; x++) {
|
|
222
|
+
const c = lines[y][x];
|
|
223
|
+
if (c === '1' || c === 'X' || c === 'x' || c === '#')
|
|
224
|
+
m.set(x, y);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return m;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* @param {string} [dark] @param {string} [light]
|
|
231
|
+
* @returns {string}
|
|
232
|
+
*/
|
|
233
|
+
toString(dark = '##', light = ' ') {
|
|
234
|
+
const rows = [];
|
|
235
|
+
for (let y = 0; y < this.height; y++) {
|
|
236
|
+
let s = '';
|
|
237
|
+
for (let x = 0; x < this.width; x++)
|
|
238
|
+
s += this.get(x, y) ? dark : light;
|
|
239
|
+
rows.push(s);
|
|
240
|
+
}
|
|
241
|
+
return rows.join('\n');
|
|
242
|
+
}
|
|
241
243
|
}
|
package/src/js/core/errors.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
|
* Error types.
|
|
33
32
|
*
|
|
@@ -38,24 +37,23 @@
|
|
|
38
37
|
*
|
|
39
38
|
* @module core/errors
|
|
40
39
|
*/
|
|
41
|
-
|
|
42
40
|
/** Base class so consumers can `catch (e) { if (e instanceof BarcodeError) ... }`. */
|
|
43
41
|
export class BarcodeError extends Error {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
42
|
+
/** @param {string} message */
|
|
43
|
+
constructor(message) {
|
|
44
|
+
super(message);
|
|
45
|
+
this.name = new.target.name;
|
|
46
|
+
}
|
|
49
47
|
}
|
|
50
|
-
|
|
51
48
|
/** Input could not be encoded — bad payload, or it does not fit the symbology. */
|
|
52
|
-
export class EncodeError extends BarcodeError {
|
|
53
|
-
|
|
49
|
+
export class EncodeError extends BarcodeError {
|
|
50
|
+
}
|
|
54
51
|
/** No symbol was found in the image. Not an error condition for `decode()`. */
|
|
55
|
-
export class NotFoundError extends BarcodeError {
|
|
56
|
-
|
|
52
|
+
export class NotFoundError extends BarcodeError {
|
|
53
|
+
}
|
|
57
54
|
/** A symbol was found, but its geometry or content is malformed. */
|
|
58
|
-
export class FormatError extends BarcodeError {
|
|
59
|
-
|
|
55
|
+
export class FormatError extends BarcodeError {
|
|
56
|
+
}
|
|
60
57
|
/** A symbol was found and read, but error correction could not repair it. */
|
|
61
|
-
export class ChecksumError extends BarcodeError {
|
|
58
|
+
export class ChecksumError extends BarcodeError {
|
|
59
|
+
}
|