qr 0.2.3 → 0.4.0

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/esm/index.js ADDED
@@ -0,0 +1,1093 @@
1
+ /*!
2
+ Copyright (c) 2023 Paul Miller (paulmillr.com)
3
+ The library paulmillr-qr is dual-licensed under the Apache 2.0 OR MIT license.
4
+ You can select a license of your choice.
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */
17
+ /**
18
+ * Methods for encoding (generating) QR code patterns.
19
+ * Check out decode.ts for decoding (reading).
20
+ * @module
21
+ * @example
22
+ ```js
23
+ import encodeQR from '@paulmillr/qr';
24
+ const txt = 'Hello world';
25
+ const ascii = encodeQR(txt, 'ascii'); // Not all fonts are supported
26
+ const terminalFriendly = encodeQR(txt, 'term'); // 2x larger, all fonts are OK
27
+ const gifBytes = encodeQR(txt, 'gif'); // Uncompressed GIF
28
+ const svgElement = encodeQR(txt, 'svg'); // SVG vector image element
29
+ const array = encodeQR(txt, 'raw'); // 2d array for canvas or other libs
30
+ // import decodeQR from '@paulmillr/qr/decode';
31
+ ```
32
+ */
33
+ // We do not use newline escape code directly in strings because it's not parser-friendly
34
+ const chCodes = { newline: 10, reset: 27 };
35
+ function assertNumber(n) {
36
+ if (!Number.isSafeInteger(n))
37
+ throw new Error(`integer expected: ${n}`);
38
+ }
39
+ function validateVersion(ver) {
40
+ if (!Number.isSafeInteger(ver) || ver < 1 || ver > 40)
41
+ throw new Error(`Invalid version=${ver}. Expected number [1..40]`);
42
+ }
43
+ function bin(dec, pad) {
44
+ return dec.toString(2).padStart(pad, '0');
45
+ }
46
+ function mod(a, b) {
47
+ const result = a % b;
48
+ return result >= 0 ? result : b + result;
49
+ }
50
+ function fillArr(length, val) {
51
+ return new Array(length).fill(val);
52
+ }
53
+ /**
54
+ * Interleaves byte blocks.
55
+ * @param blocks [[1, 2, 3], [4, 5, 6]]
56
+ * @returns [1, 4, 2, 5, 3, 6]
57
+ */
58
+ function interleaveBytes(...blocks) {
59
+ let len = 0;
60
+ for (const b of blocks)
61
+ len = Math.max(len, b.length);
62
+ const res = [];
63
+ for (let i = 0; i < len; i++) {
64
+ for (const b of blocks) {
65
+ if (i >= b.length)
66
+ continue; // outside of block, skip
67
+ res.push(b[i]);
68
+ }
69
+ }
70
+ return new Uint8Array(res);
71
+ }
72
+ function includesAt(lst, pattern, index) {
73
+ if (index < 0 || index + pattern.length > lst.length)
74
+ return false;
75
+ for (let i = 0; i < pattern.length; i++)
76
+ if (pattern[i] !== lst[index + i])
77
+ return false;
78
+ return true;
79
+ }
80
+ // Optimize for minimal score/penalty
81
+ function best() {
82
+ let best;
83
+ let bestScore = Infinity;
84
+ return {
85
+ add(score, value) {
86
+ if (score >= bestScore)
87
+ return;
88
+ best = value;
89
+ bestScore = score;
90
+ },
91
+ get: () => best,
92
+ score: () => bestScore,
93
+ };
94
+ }
95
+ // Based on https://github.com/paulmillr/scure-base/blob/main/index.ts
96
+ function alphabet(alphabet) {
97
+ return {
98
+ has: (char) => alphabet.includes(char),
99
+ decode: (input) => {
100
+ if (!Array.isArray(input) || (input.length && typeof input[0] !== 'string'))
101
+ throw new Error('alphabet.decode input should be array of strings');
102
+ return input.map((letter) => {
103
+ if (typeof letter !== 'string')
104
+ throw new Error(`alphabet.decode: not string element=${letter}`);
105
+ const index = alphabet.indexOf(letter);
106
+ if (index === -1)
107
+ throw new Error(`Unknown letter: "${letter}". Allowed: ${alphabet}`);
108
+ return index;
109
+ });
110
+ },
111
+ encode: (digits) => {
112
+ if (!Array.isArray(digits) || (digits.length && typeof digits[0] !== 'number'))
113
+ throw new Error('alphabet.encode input should be an array of numbers');
114
+ return digits.map((i) => {
115
+ assertNumber(i);
116
+ if (i < 0 || i >= alphabet.length)
117
+ throw new Error(`Digit index outside alphabet: ${i} (alphabet: ${alphabet.length})`);
118
+ return alphabet[i];
119
+ });
120
+ },
121
+ };
122
+ }
123
+ export class Bitmap {
124
+ static size(size, limit) {
125
+ if (typeof size === 'number')
126
+ size = { height: size, width: size };
127
+ if (!Number.isSafeInteger(size.height) && size.height !== Infinity)
128
+ throw new Error(`Bitmap: invalid height=${size.height} (${typeof size.height})`);
129
+ if (!Number.isSafeInteger(size.width) && size.width !== Infinity)
130
+ throw new Error(`Bitmap: invalid width=${size.width} (${typeof size.width})`);
131
+ if (limit !== undefined) {
132
+ // Clamp length, so it won't overflow, also allows to use Infinity, so we draw until end
133
+ size = {
134
+ width: Math.min(size.width, limit.width),
135
+ height: Math.min(size.height, limit.height),
136
+ };
137
+ }
138
+ return size;
139
+ }
140
+ static fromString(s) {
141
+ // Remove linebreaks on start and end, so we draw in `` section
142
+ s = s.replace(/^\n+/g, '').replace(/\n+$/g, '');
143
+ const lines = s.split(String.fromCharCode(chCodes.newline));
144
+ const height = lines.length;
145
+ const data = new Array(height);
146
+ let width;
147
+ for (const line of lines) {
148
+ const row = line.split('').map((i) => {
149
+ if (i === 'X')
150
+ return true;
151
+ if (i === ' ')
152
+ return false;
153
+ if (i === '?')
154
+ return undefined;
155
+ throw new Error(`Bitmap.fromString: unknown symbol=${i}`);
156
+ });
157
+ if (width && row.length !== width)
158
+ throw new Error(`Bitmap.fromString different row sizes: width=${width} cur=${row.length}`);
159
+ width = row.length;
160
+ data.push(row);
161
+ }
162
+ if (!width)
163
+ width = 0;
164
+ return new Bitmap({ height, width }, data);
165
+ }
166
+ constructor(size, data) {
167
+ const { height, width } = Bitmap.size(size);
168
+ this.data = data || Array.from({ length: height }, () => fillArr(width, undefined));
169
+ this.height = height;
170
+ this.width = width;
171
+ }
172
+ point(p) {
173
+ return this.data[p.y][p.x];
174
+ }
175
+ isInside(p) {
176
+ return 0 <= p.x && p.x < this.width && 0 <= p.y && p.y < this.height;
177
+ }
178
+ size(offset) {
179
+ if (!offset)
180
+ return { height: this.height, width: this.width };
181
+ const { x, y } = this.xy(offset);
182
+ return { height: this.height - y, width: this.width - x };
183
+ }
184
+ xy(c) {
185
+ if (typeof c === 'number')
186
+ c = { x: c, y: c };
187
+ if (!Number.isSafeInteger(c.x))
188
+ throw new Error(`Bitmap: invalid x=${c.x}`);
189
+ if (!Number.isSafeInteger(c.y))
190
+ throw new Error(`Bitmap: invalid y=${c.y}`);
191
+ // Do modulo, so we can use negative positions
192
+ c.x = mod(c.x, this.width);
193
+ c.y = mod(c.y, this.height);
194
+ return c;
195
+ }
196
+ // Basically every operation can be represented as rect
197
+ rect(c, size, value) {
198
+ const { x, y } = this.xy(c);
199
+ const { height, width } = Bitmap.size(size, this.size({ x, y }));
200
+ for (let yPos = 0; yPos < height; yPos++) {
201
+ for (let xPos = 0; xPos < width; xPos++) {
202
+ // NOTE: we use give function relative coordinates inside box
203
+ this.data[y + yPos][x + xPos] =
204
+ typeof value === 'function'
205
+ ? value({ x: xPos, y: yPos }, this.data[y + yPos][x + xPos])
206
+ : value;
207
+ }
208
+ }
209
+ return this;
210
+ }
211
+ // returns rectangular part of bitmap
212
+ rectRead(c, size, fn) {
213
+ return this.rect(c, size, (c, cur) => {
214
+ fn(c, cur);
215
+ return cur;
216
+ });
217
+ }
218
+ // Horizontal & vertical lines
219
+ hLine(c, len, value) {
220
+ return this.rect(c, { width: len, height: 1 }, value);
221
+ }
222
+ vLine(c, len, value) {
223
+ return this.rect(c, { width: 1, height: len }, value);
224
+ }
225
+ // add border
226
+ border(border = 2, value) {
227
+ const height = this.height + 2 * border;
228
+ const width = this.width + 2 * border;
229
+ const v = fillArr(border, value);
230
+ const h = Array.from({ length: border }, () => fillArr(width, value));
231
+ return new Bitmap({ height, width }, [...h, ...this.data.map((i) => [...v, ...i, ...v]), ...h]);
232
+ }
233
+ // Embed another bitmap on coordinates
234
+ embed(c, bm) {
235
+ return this.rect(c, bm.size(), ({ x, y }) => bm.data[y][x]);
236
+ }
237
+ // returns rectangular part of bitmap
238
+ rectSlice(c, size = this.size()) {
239
+ const rect = new Bitmap(Bitmap.size(size, this.size(this.xy(c))));
240
+ this.rect(c, size, ({ x, y }, cur) => (rect.data[y][x] = cur));
241
+ return rect;
242
+ }
243
+ // Change shape, replace rows with columns (data[y][x] -> data[x][y])
244
+ inverse() {
245
+ const { height, width } = this;
246
+ const res = new Bitmap({ height: width, width: height });
247
+ return res.rect({ x: 0, y: 0 }, Infinity, ({ x, y }) => this.data[x][y]);
248
+ }
249
+ // Each pixel size is multiplied by factor
250
+ scale(factor) {
251
+ if (!Number.isSafeInteger(factor) || factor > 1024)
252
+ throw new Error(`invalid scale factor: ${factor}`);
253
+ const { height, width } = this;
254
+ const res = new Bitmap({ height: factor * height, width: factor * width });
255
+ return res.rect({ x: 0, y: 0 }, Infinity, ({ x, y }) => this.data[Math.floor(y / factor)][Math.floor(x / factor)]);
256
+ }
257
+ clone() {
258
+ const res = new Bitmap(this.size());
259
+ return res.rect({ x: 0, y: 0 }, this.size(), ({ x, y }) => this.data[y][x]);
260
+ }
261
+ // Ensure that there is no undefined values left
262
+ assertDrawn() {
263
+ this.rectRead(0, Infinity, (_, cur) => {
264
+ if (typeof cur !== 'boolean')
265
+ throw new Error(`Invalid color type=${typeof cur}`);
266
+ });
267
+ }
268
+ // Simple string representation for debugging
269
+ toString() {
270
+ return this.data
271
+ .map((i) => i.map((j) => (j === undefined ? '?' : j ? 'X' : ' ')).join(''))
272
+ .join(String.fromCharCode(chCodes.newline));
273
+ }
274
+ toASCII() {
275
+ const { height, width, data } = this;
276
+ let out = '';
277
+ // Terminal character height is x2 of character width, so we process two rows of bitmap
278
+ // to produce one row of ASCII
279
+ for (let y = 0; y < height; y += 2) {
280
+ for (let x = 0; x < width; x++) {
281
+ const first = data[y][x];
282
+ const second = y + 1 >= height ? true : data[y + 1][x]; // if last row outside bitmap, make it black
283
+ if (!first && !second)
284
+ out += '█'; // both rows white (empty)
285
+ else if (!first && second)
286
+ out += '▀'; // top row white
287
+ else if (first && !second)
288
+ out += '▄'; // down row white
289
+ else if (first && second)
290
+ out += ' '; // both rows black
291
+ }
292
+ out += String.fromCharCode(chCodes.newline);
293
+ }
294
+ return out;
295
+ }
296
+ toTerm() {
297
+ const cc = String.fromCharCode(chCodes.reset);
298
+ const reset = cc + '[0m';
299
+ const whiteBG = cc + '[1;47m ' + reset;
300
+ const darkBG = cc + `[40m ` + reset;
301
+ return this.data
302
+ .map((i) => i.map((j) => (j ? darkBG : whiteBG)).join(''))
303
+ .join(String.fromCharCode(chCodes.newline));
304
+ }
305
+ toSVG() {
306
+ let out = `<svg xmlns:svg="http://www.w3.org/2000/svg" viewBox="0 0 ${this.width} ${this.height}" version="1.1" xmlns="http://www.w3.org/2000/svg">`;
307
+ this.rectRead(0, Infinity, ({ x, y }, val) => {
308
+ if (val)
309
+ out += `<rect x="${x}" y="${y}" width="1" height="1" />`;
310
+ });
311
+ out += '</svg>';
312
+ return out;
313
+ }
314
+ toGIF() {
315
+ // NOTE: Small, but inefficient implementation.
316
+ // Uses 1 byte per pixel, but still less bloated than SVG.
317
+ const u16le = (i) => [i & 0xff, (i >>> 8) & 0xff];
318
+ const dims = [...u16le(this.width), ...u16le(this.height)];
319
+ const data = [];
320
+ this.rectRead(0, Infinity, (_, cur) => data.push(+(cur === true)));
321
+ const N = 126; // Block size
322
+ // prettier-ignore
323
+ const bytes = [
324
+ 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, ...dims, 0xf6, 0x00, 0x00, 0xff, 0xff, 0xff,
325
+ ...fillArr(3 * 127, 0x00), 0x2c, 0x00, 0x00, 0x00, 0x00, ...dims, 0x00, 0x07
326
+ ];
327
+ const fullChunks = Math.floor(data.length / N);
328
+ // Full blocks
329
+ for (let i = 0; i < fullChunks; i++)
330
+ bytes.push(N + 1, 0x80, ...data.slice(N * i, N * (i + 1)).map((i) => +i));
331
+ // Remaining bytes
332
+ bytes.push((data.length % N) + 1, 0x80, ...data.slice(fullChunks * N).map((i) => +i));
333
+ bytes.push(0x01, 0x81, 0x00, 0x3b);
334
+ return new Uint8Array(bytes);
335
+ }
336
+ toImage(isRGB = false) {
337
+ const { height, width } = this.size();
338
+ const data = new Uint8Array(height * width * (isRGB ? 3 : 4));
339
+ let i = 0;
340
+ for (let y = 0; y < height; y++) {
341
+ for (let x = 0; x < width; x++) {
342
+ const value = !!this.data[y][x] ? 0 : 255;
343
+ data[i++] = value;
344
+ data[i++] = value;
345
+ data[i++] = value;
346
+ if (!isRGB)
347
+ data[i++] = 255; // alpha channel
348
+ }
349
+ }
350
+ return { height, width, data };
351
+ }
352
+ }
353
+ // End of utils
354
+ // Runtime type-checking
355
+ /** Error correction mode. low: 7%, medium: 15%, quartile: 25%, high: 30% */
356
+ export const ECMode = ['low', 'medium', 'quartile', 'high'];
357
+ /** QR Code encoding */
358
+ export const Encoding = ['numeric', 'alphanumeric', 'byte', 'kanji', 'eci'];
359
+ // Various constants & tables
360
+ // prettier-ignore
361
+ const BYTES = [
362
+ // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
363
+ 26, 44, 70, 100, 134, 172, 196, 242, 292, 346, 404, 466, 532, 581, 655, 733, 815, 901, 991, 1085,
364
+ // 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
365
+ 1156, 1258, 1364, 1474, 1588, 1706, 1828, 1921, 2051, 2185, 2323, 2465, 2611, 2761, 2876, 3034, 3196, 3362, 3532, 3706,
366
+ ];
367
+ // prettier-ignore
368
+ const WORDS_PER_BLOCK = {
369
+ // Version 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
370
+ low: [7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
371
+ medium: [10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28],
372
+ quartile: [13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
373
+ high: [17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
374
+ };
375
+ // prettier-ignore
376
+ const ECC_BLOCKS = {
377
+ // Version 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
378
+ low: [1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25],
379
+ medium: [1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49],
380
+ quartile: [1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68],
381
+ high: [1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81],
382
+ };
383
+ const info = {
384
+ size: {
385
+ encode: (ver) => 21 + 4 * (ver - 1), // ver1 = 21, ver40=177 blocks
386
+ decode: (size) => (size - 17) / 4,
387
+ },
388
+ sizeType: (ver) => Math.floor((ver + 7) / 17),
389
+ // Based on https://codereview.stackexchange.com/questions/74925/algorithm-to-generate-this-alignment-pattern-locations-table-for-qr-codes
390
+ alignmentPatterns(ver) {
391
+ if (ver === 1)
392
+ return [];
393
+ const first = 6;
394
+ const last = info.size.encode(ver) - first - 1;
395
+ const distance = last - first;
396
+ const count = Math.ceil(distance / 28);
397
+ let interval = Math.floor(distance / count);
398
+ if (interval % 2)
399
+ interval += 1;
400
+ else if ((distance % count) * 2 >= count)
401
+ interval += 2;
402
+ const res = [first];
403
+ for (let m = 1; m < count; m++)
404
+ res.push(last - (count - m) * interval);
405
+ res.push(last);
406
+ return res;
407
+ },
408
+ ECCode: {
409
+ low: 0b01,
410
+ medium: 0b00,
411
+ quartile: 0b11,
412
+ high: 0b10,
413
+ },
414
+ formatMask: 0b101010000010010,
415
+ formatBits(ecc, maskIdx) {
416
+ const data = (info.ECCode[ecc] << 3) | maskIdx;
417
+ let d = data;
418
+ for (let i = 0; i < 10; i++)
419
+ d = (d << 1) ^ ((d >> 9) * 0b10100110111);
420
+ return ((data << 10) | d) ^ info.formatMask;
421
+ },
422
+ versionBits(ver) {
423
+ let d = ver;
424
+ for (let i = 0; i < 12; i++)
425
+ d = (d << 1) ^ ((d >> 11) * 0b1111100100101);
426
+ return (ver << 12) | d;
427
+ },
428
+ alphabet: {
429
+ numeric: alphabet('0123456789'),
430
+ alphanumerc: alphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'),
431
+ }, // as Record<EncodingType, ReturnType<typeof alphabet>>,
432
+ lengthBits(ver, type) {
433
+ const table = {
434
+ numeric: [10, 12, 14],
435
+ alphanumeric: [9, 11, 13],
436
+ byte: [8, 16, 16],
437
+ kanji: [8, 10, 12],
438
+ eci: [0, 0, 0],
439
+ };
440
+ return table[type][info.sizeType(ver)];
441
+ },
442
+ modeBits: {
443
+ numeric: '0001',
444
+ alphanumeric: '0010',
445
+ byte: '0100',
446
+ kanji: '1000',
447
+ eci: '0111',
448
+ },
449
+ capacity(ver, ecc) {
450
+ const bytes = BYTES[ver - 1];
451
+ const words = WORDS_PER_BLOCK[ecc][ver - 1];
452
+ const numBlocks = ECC_BLOCKS[ecc][ver - 1];
453
+ const blockLen = Math.floor(bytes / numBlocks) - words;
454
+ const shortBlocks = numBlocks - (bytes % numBlocks);
455
+ return {
456
+ words,
457
+ numBlocks,
458
+ shortBlocks,
459
+ blockLen,
460
+ capacity: (bytes - words * numBlocks) * 8,
461
+ total: (words + blockLen) * numBlocks + numBlocks - shortBlocks,
462
+ };
463
+ },
464
+ };
465
+ const PATTERNS = [
466
+ (x, y) => (x + y) % 2 == 0,
467
+ (_x, y) => y % 2 == 0,
468
+ (x, _y) => x % 3 == 0,
469
+ (x, y) => (x + y) % 3 == 0,
470
+ (x, y) => (Math.floor(y / 2) + Math.floor(x / 3)) % 2 == 0,
471
+ (x, y) => ((x * y) % 2) + ((x * y) % 3) == 0,
472
+ (x, y) => (((x * y) % 2) + ((x * y) % 3)) % 2 == 0,
473
+ (x, y) => (((x + y) % 2) + ((x * y) % 3)) % 2 == 0,
474
+ ];
475
+ // Galois field && reed-solomon encoding
476
+ const GF = {
477
+ tables: ((p_poly) => {
478
+ const exp = fillArr(256, 0);
479
+ const log = fillArr(256, 0);
480
+ for (let i = 0, x = 1; i < 256; i++) {
481
+ exp[i] = x;
482
+ log[x] = i;
483
+ x <<= 1;
484
+ if (x & 0x100)
485
+ x ^= p_poly;
486
+ }
487
+ return { exp, log };
488
+ })(0x11d),
489
+ exp: (x) => GF.tables.exp[x],
490
+ log(x) {
491
+ if (x === 0)
492
+ throw new Error(`GF.log: invalid arg=${x}`);
493
+ return GF.tables.log[x] % 255;
494
+ },
495
+ mul(x, y) {
496
+ if (x === 0 || y === 0)
497
+ return 0;
498
+ return GF.tables.exp[(GF.tables.log[x] + GF.tables.log[y]) % 255];
499
+ },
500
+ add: (x, y) => x ^ y,
501
+ pow: (x, e) => GF.tables.exp[(GF.tables.log[x] * e) % 255],
502
+ inv(x) {
503
+ if (x === 0)
504
+ throw new Error(`GF.inverse: invalid arg=${x}`);
505
+ return GF.tables.exp[255 - GF.tables.log[x]];
506
+ },
507
+ polynomial(poly) {
508
+ if (poly.length == 0)
509
+ throw new Error('GF.polymomial: invalid length');
510
+ if (poly[0] !== 0)
511
+ return poly;
512
+ // Strip leading zeros
513
+ let i = 0;
514
+ for (; i < poly.length - 1 && poly[i] == 0; i++)
515
+ ;
516
+ return poly.slice(i);
517
+ },
518
+ monomial(degree, coefficient) {
519
+ if (degree < 0)
520
+ throw new Error(`GF.monomial: invalid degree=${degree}`);
521
+ if (coefficient == 0)
522
+ return [0];
523
+ let coefficients = fillArr(degree + 1, 0);
524
+ coefficients[0] = coefficient;
525
+ return GF.polynomial(coefficients);
526
+ },
527
+ degree: (a) => a.length - 1,
528
+ coefficient: (a, degree) => a[GF.degree(a) - degree],
529
+ mulPoly(a, b) {
530
+ if (a[0] === 0 || b[0] === 0)
531
+ return [0];
532
+ const res = fillArr(a.length + b.length - 1, 0);
533
+ for (let i = 0; i < a.length; i++) {
534
+ for (let j = 0; j < b.length; j++) {
535
+ res[i + j] = GF.add(res[i + j], GF.mul(a[i], b[j]));
536
+ }
537
+ }
538
+ return GF.polynomial(res);
539
+ },
540
+ mulPolyScalar(a, scalar) {
541
+ if (scalar == 0)
542
+ return [0];
543
+ if (scalar == 1)
544
+ return a;
545
+ const res = fillArr(a.length, 0);
546
+ for (let i = 0; i < a.length; i++)
547
+ res[i] = GF.mul(a[i], scalar);
548
+ return GF.polynomial(res);
549
+ },
550
+ mulPolyMonomial(a, degree, coefficient) {
551
+ if (degree < 0)
552
+ throw new Error('GF.mulPolyMonomial: invalid degree');
553
+ if (coefficient == 0)
554
+ return [0];
555
+ const res = fillArr(a.length + degree, 0);
556
+ for (let i = 0; i < a.length; i++)
557
+ res[i] = GF.mul(a[i], coefficient);
558
+ return GF.polynomial(res);
559
+ },
560
+ addPoly(a, b) {
561
+ if (a[0] === 0)
562
+ return b;
563
+ if (b[0] === 0)
564
+ return a;
565
+ let smaller = a;
566
+ let larger = b;
567
+ if (smaller.length > larger.length)
568
+ [smaller, larger] = [larger, smaller];
569
+ let sumDiff = fillArr(larger.length, 0);
570
+ let lengthDiff = larger.length - smaller.length;
571
+ let s = larger.slice(0, lengthDiff);
572
+ for (let i = 0; i < s.length; i++)
573
+ sumDiff[i] = s[i];
574
+ for (let i = lengthDiff; i < larger.length; i++)
575
+ sumDiff[i] = GF.add(smaller[i - lengthDiff], larger[i]);
576
+ return GF.polynomial(sumDiff);
577
+ },
578
+ remainderPoly(data, divisor) {
579
+ const out = Array.from(data);
580
+ for (let i = 0; i < data.length - divisor.length + 1; i++) {
581
+ const elm = out[i];
582
+ if (elm === 0)
583
+ continue;
584
+ for (let j = 1; j < divisor.length; j++) {
585
+ if (divisor[j] !== 0)
586
+ out[i + j] = GF.add(out[i + j], GF.mul(divisor[j], elm));
587
+ }
588
+ }
589
+ return out.slice(data.length - divisor.length + 1, out.length);
590
+ },
591
+ divisorPoly(degree) {
592
+ let g = [1];
593
+ for (let i = 0; i < degree; i++)
594
+ g = GF.mulPoly(g, [1, GF.pow(2, i)]);
595
+ return g;
596
+ },
597
+ evalPoly(poly, a) {
598
+ if (a == 0)
599
+ return GF.coefficient(poly, 0); // Just return the x^0 coefficient
600
+ let res = poly[0];
601
+ for (let i = 1; i < poly.length; i++)
602
+ res = GF.add(GF.mul(a, res), poly[i]);
603
+ return res;
604
+ },
605
+ // TODO: cleanup
606
+ euclidian(a, b, R) {
607
+ // Force degree(a) >= degree(b)
608
+ if (GF.degree(a) < GF.degree(b))
609
+ [a, b] = [b, a];
610
+ let rLast = a;
611
+ let r = b;
612
+ let tLast = [0];
613
+ let t = [1];
614
+ // while degree of Ri ≥ t/2
615
+ while (2 * GF.degree(r) >= R) {
616
+ let rLastLast = rLast;
617
+ let tLastLast = tLast;
618
+ rLast = r;
619
+ tLast = t;
620
+ if (rLast[0] === 0)
621
+ throw new Error('rLast[0] === 0');
622
+ r = rLastLast;
623
+ let q = [0];
624
+ const dltInverse = GF.inv(rLast[0]);
625
+ while (GF.degree(r) >= GF.degree(rLast) && r[0] !== 0) {
626
+ const degreeDiff = GF.degree(r) - GF.degree(rLast);
627
+ const scale = GF.mul(r[0], dltInverse);
628
+ q = GF.addPoly(q, GF.monomial(degreeDiff, scale));
629
+ r = GF.addPoly(r, GF.mulPolyMonomial(rLast, degreeDiff, scale));
630
+ }
631
+ q = GF.mulPoly(q, tLast);
632
+ t = GF.addPoly(q, tLastLast);
633
+ if (GF.degree(r) >= GF.degree(rLast))
634
+ throw new Error(`Division failed r: ${r}, rLast: ${rLast}`);
635
+ }
636
+ const sigmaTildeAtZero = GF.coefficient(t, 0);
637
+ if (sigmaTildeAtZero == 0)
638
+ throw new Error('sigmaTilde(0) was zero');
639
+ const inverse = GF.inv(sigmaTildeAtZero);
640
+ return [GF.mulPolyScalar(t, inverse), GF.mulPolyScalar(r, inverse)];
641
+ },
642
+ };
643
+ function RS(eccWords) {
644
+ return {
645
+ encode(from) {
646
+ const d = GF.divisorPoly(eccWords);
647
+ const pol = Array.from(from);
648
+ pol.push(...d.slice(0, -1).fill(0));
649
+ return Uint8Array.from(GF.remainderPoly(pol, d));
650
+ },
651
+ decode(to) {
652
+ const res = to.slice();
653
+ const poly = GF.polynomial(Array.from(to));
654
+ // Find errors
655
+ let syndrome = fillArr(eccWords, 0);
656
+ let hasError = false;
657
+ for (let i = 0; i < eccWords; i++) {
658
+ const evl = GF.evalPoly(poly, GF.exp(i));
659
+ syndrome[syndrome.length - 1 - i] = evl;
660
+ if (evl !== 0)
661
+ hasError = true;
662
+ }
663
+ if (!hasError)
664
+ return res;
665
+ syndrome = GF.polynomial(syndrome);
666
+ const monomial = GF.monomial(eccWords, 1);
667
+ const [errorLocator, errorEvaluator] = GF.euclidian(monomial, syndrome, eccWords);
668
+ // Error locations
669
+ const locations = fillArr(GF.degree(errorLocator), 0);
670
+ let e = 0;
671
+ for (let i = 1; i < 256 && e < locations.length; i++) {
672
+ if (GF.evalPoly(errorLocator, i) === 0)
673
+ locations[e++] = GF.inv(i);
674
+ }
675
+ if (e !== locations.length)
676
+ throw new Error('RS.decode: invalid errors number');
677
+ for (let i = 0; i < locations.length; i++) {
678
+ const pos = res.length - 1 - GF.log(locations[i]);
679
+ if (pos < 0)
680
+ throw new Error('RS.decode: invalid error location');
681
+ const xiInverse = GF.inv(locations[i]);
682
+ let denominator = 1;
683
+ for (let j = 0; j < locations.length; j++) {
684
+ if (i === j)
685
+ continue;
686
+ denominator = GF.mul(denominator, GF.add(1, GF.mul(locations[j], xiInverse)));
687
+ }
688
+ res[pos] = GF.add(res[pos], GF.mul(GF.evalPoly(errorEvaluator, xiInverse), GF.inv(denominator)));
689
+ }
690
+ return res;
691
+ },
692
+ };
693
+ }
694
+ // Interleaves blocks
695
+ function interleave(ver, ecc) {
696
+ const { words, shortBlocks, numBlocks, blockLen, total } = info.capacity(ver, ecc);
697
+ const rs = RS(words);
698
+ return {
699
+ encode(bytes) {
700
+ // Add error correction to bytes
701
+ const blocks = [];
702
+ const eccBlocks = [];
703
+ for (let i = 0; i < numBlocks; i++) {
704
+ const isShort = i < shortBlocks;
705
+ const len = blockLen + (isShort ? 0 : 1);
706
+ blocks.push(bytes.subarray(0, len));
707
+ eccBlocks.push(rs.encode(bytes.subarray(0, len)));
708
+ bytes = bytes.subarray(len);
709
+ }
710
+ const resBlocks = interleaveBytes(...blocks);
711
+ const resECC = interleaveBytes(...eccBlocks);
712
+ const res = new Uint8Array(resBlocks.length + resECC.length);
713
+ res.set(resBlocks);
714
+ res.set(resECC, resBlocks.length);
715
+ return res;
716
+ },
717
+ decode(data) {
718
+ if (data.length !== total)
719
+ throw new Error(`interleave.decode: len(data)=${data.length}, total=${total}`);
720
+ const blocks = [];
721
+ for (let i = 0; i < numBlocks; i++) {
722
+ const isShort = i < shortBlocks;
723
+ blocks.push(new Uint8Array(words + blockLen + (isShort ? 0 : 1)));
724
+ }
725
+ // Short blocks
726
+ let pos = 0;
727
+ for (let i = 0; i < blockLen; i++) {
728
+ for (let j = 0; j < numBlocks; j++)
729
+ blocks[j][i] = data[pos++];
730
+ }
731
+ // Long blocks
732
+ for (let j = shortBlocks; j < numBlocks; j++)
733
+ blocks[j][blockLen] = data[pos++];
734
+ // ECC
735
+ for (let i = blockLen; i < blockLen + words; i++) {
736
+ for (let j = 0; j < numBlocks; j++) {
737
+ const isShort = j < shortBlocks;
738
+ blocks[j][i + (isShort ? 0 : 1)] = data[pos++];
739
+ }
740
+ }
741
+ // Decode
742
+ // Error-correct and copy data blocks together into a stream of bytes
743
+ const res = [];
744
+ for (const block of blocks)
745
+ res.push(...Array.from(rs.decode(block)).slice(0, -words));
746
+ return Uint8Array.from(res);
747
+ },
748
+ };
749
+ }
750
+ // Draw
751
+ // Generic template per version+ecc+mask. Can be cached, to speedup calculations.
752
+ function drawTemplate(ver, ecc, maskIdx, test = false) {
753
+ const size = info.size.encode(ver);
754
+ let b = new Bitmap(size + 2);
755
+ // Finder patterns
756
+ // We draw full pattern and later slice, since before addition of borders finder is truncated by one pixel on sides
757
+ const finder = new Bitmap(3).rect(0, 3, true).border(1, false).border(1, true).border(1, false);
758
+ b = b
759
+ .embed(0, finder) // top left
760
+ .embed({ x: -finder.width, y: 0 }, finder) // top right
761
+ .embed({ x: 0, y: -finder.height }, finder); // bottom left
762
+ b = b.rectSlice(1, size);
763
+ // Alignment patterns
764
+ const align = new Bitmap(1).rect(0, 1, true).border(1, false).border(1, true);
765
+ const alignPos = info.alignmentPatterns(ver);
766
+ for (const y of alignPos) {
767
+ for (const x of alignPos) {
768
+ if (b.data[y][x] !== undefined)
769
+ continue;
770
+ b.embed({ x: x - 2, y: y - 2 }, align); // center of pattern should be at position
771
+ }
772
+ }
773
+ // Timing patterns
774
+ b = b
775
+ .hLine({ x: 0, y: 6 }, Infinity, ({ x }, cur) => (cur === undefined ? x % 2 == 0 : cur))
776
+ .vLine({ x: 6, y: 0 }, Infinity, ({ y }, cur) => (cur === undefined ? y % 2 == 0 : cur));
777
+ // Format information
778
+ {
779
+ const bits = info.formatBits(ecc, maskIdx);
780
+ const getBit = (i) => !test && ((bits >> i) & 1) == 1;
781
+ // vertical
782
+ for (let i = 0; i < 6; i++)
783
+ b.data[i][8] = getBit(i); // right of top-left finder
784
+ // TODO: re-write as lines, like:
785
+ // b.vLine({ x: 8, y: 0 }, 6, ({ x, y }) => getBit(y));
786
+ for (let i = 6; i < 8; i++)
787
+ b.data[i + 1][8] = getBit(i); // after timing pattern
788
+ for (let i = 8; i < 15; i++)
789
+ b.data[size - 15 + i][8] = getBit(i); // right of bottom-left finder
790
+ // horizontal
791
+ for (let i = 0; i < 8; i++)
792
+ b.data[8][size - i - 1] = getBit(i); // under top-right finder
793
+ for (let i = 8; i < 9; i++)
794
+ b.data[8][15 - i - 1 + 1] = getBit(i); // VVV, after timing
795
+ for (let i = 9; i < 15; i++)
796
+ b.data[8][15 - i - 1] = getBit(i); // under top-left finder
797
+ b.data[size - 8][8] = !test; // bottom-left finder, right
798
+ }
799
+ // Version information
800
+ if (ver >= 7) {
801
+ const bits = info.versionBits(ver);
802
+ for (let i = 0; i < 18; i += 1) {
803
+ const bit = !test && ((bits >> i) & 1) == 1;
804
+ const x = Math.floor(i / 3);
805
+ const y = (i % 3) + size - 8 - 3;
806
+ // two copies
807
+ b.data[x][y] = bit;
808
+ b.data[y][x] = bit;
809
+ }
810
+ }
811
+ return b;
812
+ }
813
+ // zigzag: bottom->top && top->bottom
814
+ function zigzag(tpl, maskIdx, fn) {
815
+ const size = tpl.height;
816
+ const pattern = PATTERNS[maskIdx];
817
+ // zig-zag pattern
818
+ let dir = -1;
819
+ let y = size - 1;
820
+ // two columns at time
821
+ for (let xOffset = size - 1; xOffset > 0; xOffset -= 2) {
822
+ if (xOffset == 6)
823
+ xOffset = 5; // skip vertical timing pattern
824
+ for (;; y += dir) {
825
+ for (let j = 0; j < 2; j += 1) {
826
+ const x = xOffset - j;
827
+ if (tpl.data[y][x] !== undefined)
828
+ continue; // skip already written elements
829
+ fn(x, y, pattern(x, y));
830
+ }
831
+ if (y + dir < 0 || y + dir >= size)
832
+ break;
833
+ }
834
+ dir = -dir; // change direction
835
+ }
836
+ }
837
+ // NOTE: byte encoding is just representation, QR works with strings only. Most decoders will fail on raw byte array,
838
+ // since they expect unicode or other text encoding inside bytes
839
+ function detectType(str) {
840
+ let type = 'numeric';
841
+ for (let x of str) {
842
+ if (info.alphabet.numeric.has(x))
843
+ continue;
844
+ type = 'alphanumeric';
845
+ if (!info.alphabet.alphanumerc.has(x))
846
+ return 'byte';
847
+ }
848
+ return type;
849
+ }
850
+ /**
851
+ * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
852
+ */
853
+ export function utf8ToBytes(str) {
854
+ if (typeof str !== 'string')
855
+ throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
856
+ return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
857
+ }
858
+ function encode(ver, ecc, data, type) {
859
+ let encoded = '';
860
+ let dataLen = data.length;
861
+ if (type === 'numeric') {
862
+ const t = info.alphabet.numeric.decode(data.split(''));
863
+ const n = t.length;
864
+ for (let i = 0; i < n - 2; i += 3)
865
+ encoded += bin(t[i] * 100 + t[i + 1] * 10 + t[i + 2], 10);
866
+ if (n % 3 === 1) {
867
+ encoded += bin(t[n - 1], 4);
868
+ }
869
+ else if (n % 3 === 2) {
870
+ encoded += bin(t[n - 2] * 10 + t[n - 1], 7);
871
+ }
872
+ }
873
+ else if (type === 'alphanumeric') {
874
+ const t = info.alphabet.alphanumerc.decode(data.split(''));
875
+ const n = t.length;
876
+ for (let i = 0; i < n - 1; i += 2)
877
+ encoded += bin(t[i] * 45 + t[i + 1], 11);
878
+ if (n % 2 == 1)
879
+ encoded += bin(t[n - 1], 6); // pad if odd number of chars
880
+ }
881
+ else if (type === 'byte') {
882
+ const utf8 = utf8ToBytes(data);
883
+ dataLen = utf8.length;
884
+ encoded = Array.from(utf8)
885
+ .map((i) => bin(i, 8))
886
+ .join('');
887
+ }
888
+ else {
889
+ throw new Error('encode: unsupported type');
890
+ }
891
+ const { capacity } = info.capacity(ver, ecc);
892
+ const len = bin(dataLen, info.lengthBits(ver, type));
893
+ let bits = info.modeBits[type] + len + encoded;
894
+ if (bits.length > capacity)
895
+ throw new Error('Capacity overflow');
896
+ // Terminator
897
+ bits += '0'.repeat(Math.min(4, Math.max(0, capacity - bits.length)));
898
+ // Pad bits string untill full byte
899
+ if (bits.length % 8)
900
+ bits += '0'.repeat(8 - (bits.length % 8));
901
+ // Add padding until capacity is full
902
+ const padding = '1110110000010001';
903
+ for (let idx = 0; bits.length !== capacity; idx++)
904
+ bits += padding[idx % padding.length];
905
+ // Convert a bitstring to array of bytes
906
+ const bytes = Uint8Array.from(bits.match(/(.{8})/g).map((i) => Number(`0b${i}`)));
907
+ return interleave(ver, ecc).encode(bytes);
908
+ }
909
+ // DRAW
910
+ function drawQR(ver, ecc, data, maskIdx, test = false) {
911
+ const b = drawTemplate(ver, ecc, maskIdx, test);
912
+ let i = 0;
913
+ const need = 8 * data.length;
914
+ zigzag(b, maskIdx, (x, y, mask) => {
915
+ let value = false;
916
+ if (i < need) {
917
+ value = ((data[i >>> 3] >> ((7 - i) & 7)) & 1) !== 0;
918
+ i++;
919
+ }
920
+ b.data[y][x] = value !== mask; // !== as xor
921
+ });
922
+ if (i !== need)
923
+ throw new Error('QR: bytes left after draw');
924
+ return b;
925
+ }
926
+ function penalty(bm) {
927
+ const inverse = bm.inverse();
928
+ // Adjacent modules in row/column in same | No. of modules = (5 + i) color
929
+ const sameColor = (row) => {
930
+ let res = 0;
931
+ for (let i = 0, same = 1, last = undefined; i < row.length; i++) {
932
+ if (last === row[i]) {
933
+ same++;
934
+ if (i !== row.length - 1)
935
+ continue; // handle last element
936
+ }
937
+ if (same >= 5)
938
+ res += 3 + (same - 5);
939
+ last = row[i];
940
+ same = 1;
941
+ }
942
+ return res;
943
+ };
944
+ let adjacent = 0;
945
+ bm.data.forEach((row) => (adjacent += sameColor(row)));
946
+ inverse.data.forEach((column) => (adjacent += sameColor(column)));
947
+ // Block of modules in same color (Block size = 2x2)
948
+ let box = 0;
949
+ let b = bm.data;
950
+ const lastW = bm.width - 1;
951
+ const lastH = bm.height - 1;
952
+ for (let x = 0; x < lastW; x++) {
953
+ for (let y = 0; y < lastH; y++) {
954
+ const x1 = x + 1;
955
+ const y1 = y + 1;
956
+ if (b[x][y] === b[x1][y] && b[x1][y] === b[x][y1] && b[x1][y] === b[x1][y1]) {
957
+ box += 3;
958
+ }
959
+ }
960
+ }
961
+ // 1:1:3:1:1 ratio (dark:light:dark:light:dark) pattern in row/column, preceded or followed by light area 4 modules wide
962
+ const finderPattern = (row) => {
963
+ const finderPattern = [true, false, true, true, true, false, true]; // dark:light:dark:light:dark
964
+ const lightPattern = [false, false, false, false]; // light area 4 modules wide
965
+ const p1 = [...finderPattern, ...lightPattern];
966
+ const p2 = [...lightPattern, ...finderPattern];
967
+ let res = 0;
968
+ for (let i = 0; i < row.length; i++) {
969
+ if (includesAt(row, p1, i))
970
+ res += 40;
971
+ if (includesAt(row, p2, i))
972
+ res += 40;
973
+ }
974
+ return res;
975
+ };
976
+ let finder = 0;
977
+ for (const row of bm.data)
978
+ finder += finderPattern(row);
979
+ for (const column of inverse.data)
980
+ finder += finderPattern(column);
981
+ // Proportion of dark modules in entire symbol
982
+ // Add 10 points to a deviation of 5% increment or decrement in the proportion
983
+ // ratio of dark module from the referential 50%
984
+ let darkPixels = 0;
985
+ bm.rectRead(0, Infinity, (_c, val) => (darkPixels += val ? 1 : 0));
986
+ const darkPercent = (darkPixels / (bm.height * bm.width)) * 100;
987
+ const dark = 10 * Math.floor(Math.abs(darkPercent - 50) / 5);
988
+ return adjacent + box + finder + dark;
989
+ }
990
+ // Selects best mask according to penalty, if no mask is provided
991
+ function drawQRBest(ver, ecc, data, maskIdx) {
992
+ if (maskIdx === undefined) {
993
+ const bestMask = best();
994
+ for (let mask = 0; mask < PATTERNS.length; mask++)
995
+ bestMask.add(penalty(drawQR(ver, ecc, data, mask, true)), mask);
996
+ maskIdx = bestMask.get();
997
+ }
998
+ if (maskIdx === undefined)
999
+ throw new Error('Cannot find mask'); // Should never happen
1000
+ return drawQR(ver, ecc, data, maskIdx);
1001
+ }
1002
+ function validateECC(ec) {
1003
+ if (!ECMode.includes(ec))
1004
+ throw new Error(`Invalid error correction mode=${ec}. Expected: ${ECMode}`);
1005
+ }
1006
+ function validateEncoding(enc) {
1007
+ if (!Encoding.includes(enc))
1008
+ throw new Error(`Encoding: invalid mode=${enc}. Expected: ${Encoding}`);
1009
+ if (enc === 'kanji' || enc === 'eci')
1010
+ throw new Error(`Encoding: ${enc} is not supported (yet?).`);
1011
+ }
1012
+ function validateMask(mask) {
1013
+ if (![0, 1, 2, 3, 4, 5, 6, 7].includes(mask) || !PATTERNS[mask])
1014
+ throw new Error(`Invalid mask=${mask}. Expected number [0..7]`);
1015
+ }
1016
+ export function encodeQR(text, output = 'raw', opts = {}) {
1017
+ const ecc = opts.ecc !== undefined ? opts.ecc : 'medium';
1018
+ validateECC(ecc);
1019
+ const encoding = opts.encoding !== undefined ? opts.encoding : detectType(text);
1020
+ validateEncoding(encoding);
1021
+ if (opts.mask !== undefined)
1022
+ validateMask(opts.mask);
1023
+ let ver = opts.version;
1024
+ let data, err = new Error('Unknown error');
1025
+ if (ver !== undefined) {
1026
+ validateVersion(ver);
1027
+ data = encode(ver, ecc, text, encoding);
1028
+ }
1029
+ else {
1030
+ // If no version is provided, try to find smallest one which fits
1031
+ // Currently just scans all version, can be significantly speedup if needed
1032
+ for (let i = 1; i <= 40; i++) {
1033
+ try {
1034
+ data = encode(i, ecc, text, encoding);
1035
+ ver = i;
1036
+ break;
1037
+ }
1038
+ catch (e) {
1039
+ err = e;
1040
+ }
1041
+ }
1042
+ }
1043
+ if (!ver || !data)
1044
+ throw err;
1045
+ let res = drawQRBest(ver, ecc, data, opts.mask);
1046
+ res.assertDrawn();
1047
+ const border = opts.border === undefined ? 2 : opts.border;
1048
+ if (!Number.isSafeInteger(border))
1049
+ throw new Error(`invalid border type=${typeof border}`);
1050
+ res = res.border(border, false); // Add border
1051
+ if (opts.scale !== undefined)
1052
+ res = res.scale(opts.scale); // Scale image
1053
+ if (output === 'raw')
1054
+ return res.data;
1055
+ else if (output === 'ascii')
1056
+ return res.toASCII();
1057
+ else if (output === 'svg')
1058
+ return res.toSVG();
1059
+ else if (output === 'gif')
1060
+ return res.toGIF();
1061
+ else if (output === 'term')
1062
+ return res.toTerm();
1063
+ else
1064
+ throw new Error(`Unknown output: ${output}`);
1065
+ }
1066
+ export default encodeQR;
1067
+ export const utils = {
1068
+ best,
1069
+ bin,
1070
+ drawTemplate,
1071
+ fillArr,
1072
+ info,
1073
+ interleave,
1074
+ validateVersion,
1075
+ zigzag,
1076
+ };
1077
+ // Unsafe API utils, exported only for tests
1078
+ export const _tests = {
1079
+ Bitmap,
1080
+ info,
1081
+ detectType,
1082
+ encode,
1083
+ drawQR,
1084
+ penalty,
1085
+ PATTERNS,
1086
+ };
1087
+ // Type tests
1088
+ // const o1 = qr('test', 'ascii');
1089
+ // const o2 = qr('test', 'raw');
1090
+ // const o3 = qr('test', 'gif');
1091
+ // const o4 = qr('test', 'svg');
1092
+ // const o5 = qr('test', 'term');
1093
+ //# sourceMappingURL=index.js.map