@sksoftofficial/ocduet 0.2.1

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.
@@ -0,0 +1,741 @@
1
+ // Vendored from uqr v0.1.3 (https://github.com/antfu/uqr) — MIT License.
2
+ // Unmodified except for this header. Provides encode() and renderUnicodeCompact().
3
+ var QrCodeDataType = /* @__PURE__ */ ((QrCodeDataType2) => {
4
+ QrCodeDataType2[QrCodeDataType2["Border"] = -1] = "Border";
5
+ QrCodeDataType2[QrCodeDataType2["Data"] = 0] = "Data";
6
+ QrCodeDataType2[QrCodeDataType2["Function"] = 1] = "Function";
7
+ QrCodeDataType2[QrCodeDataType2["Position"] = 2] = "Position";
8
+ QrCodeDataType2[QrCodeDataType2["Timing"] = 3] = "Timing";
9
+ QrCodeDataType2[QrCodeDataType2["Alignment"] = 4] = "Alignment";
10
+ return QrCodeDataType2;
11
+ })(QrCodeDataType || {});
12
+
13
+ const LOW = [0, 1];
14
+ const MEDIUM = [1, 0];
15
+ const QUARTILE = [2, 3];
16
+ const HIGH = [3, 2];
17
+ const EccMap = {
18
+ L: LOW,
19
+ M: MEDIUM,
20
+ Q: QUARTILE,
21
+ H: HIGH
22
+ };
23
+ const NUMERIC_REGEX = /^\d*$/;
24
+ const ALPHANUMERIC_REGEX = /^[A-Z0-9 $%*+./:-]*$/;
25
+ const ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
26
+ const MIN_VERSION = 1;
27
+ const MAX_VERSION = 40;
28
+ const PENALTY_N1 = 3;
29
+ const PENALTY_N2 = 3;
30
+ const PENALTY_N3 = 40;
31
+ const PENALTY_N4 = 10;
32
+ const ECC_CODEWORDS_PER_BLOCK = [
33
+ // Version: (note that index 0 is for padding, and is set to an illegal value)
34
+ // 0, 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 Error correction level
35
+ [-1, 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],
36
+ // Low
37
+ [-1, 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],
38
+ // Medium
39
+ [-1, 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],
40
+ // Quartile
41
+ [-1, 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]
42
+ // High
43
+ ];
44
+ const NUM_ERROR_CORRECTION_BLOCKS = [
45
+ // Version: (note that index 0 is for padding, and is set to an illegal value)
46
+ // 0, 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 Error correction level
47
+ [-1, 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],
48
+ // Low
49
+ [-1, 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],
50
+ // Medium
51
+ [-1, 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],
52
+ // Quartile
53
+ [-1, 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]
54
+ // High
55
+ ];
56
+ class QrCode {
57
+ /* -- Constructor (low level) and fields -- */
58
+ // Creates a new QR Code with the given version number,
59
+ // error correction level, data codeword bytes, and mask number.
60
+ // This is a low-level API that most users should not use directly.
61
+ // A mid-level API is the encodeSegments() function.
62
+ constructor(version, ecc, dataCodewords, msk) {
63
+ this.version = version;
64
+ this.ecc = ecc;
65
+ if (version < MIN_VERSION || version > MAX_VERSION)
66
+ throw new RangeError("Version value out of range");
67
+ if (msk < -1 || msk > 7)
68
+ throw new RangeError("Mask value out of range");
69
+ this.size = version * 4 + 17;
70
+ const row = Array.from({ length: this.size }).fill(false);
71
+ for (let i = 0; i < this.size; i++) {
72
+ this.modules.push(row.slice());
73
+ this.types.push(row.map(() => 0));
74
+ }
75
+ this.drawFunctionPatterns();
76
+ const allCodewords = this.addEccAndInterleave(dataCodewords);
77
+ this.drawCodewords(allCodewords);
78
+ if (msk === -1) {
79
+ let minPenalty = 1e9;
80
+ for (let i = 0; i < 8; i++) {
81
+ this.applyMask(i);
82
+ this.drawFormatBits(i);
83
+ const penalty = this.getPenaltyScore();
84
+ if (penalty < minPenalty) {
85
+ msk = i;
86
+ minPenalty = penalty;
87
+ }
88
+ this.applyMask(i);
89
+ }
90
+ }
91
+ this.mask = msk;
92
+ this.applyMask(msk);
93
+ this.drawFormatBits(msk);
94
+ }
95
+ /* -- Fields -- */
96
+ // The width and height of this QR Code, measured in modules, between
97
+ // 21 and 177 (inclusive). This is equal to version * 4 + 17.
98
+ size;
99
+ // The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).
100
+ // Even if a QR Code is created with automatic masking requested (mask = -1),
101
+ // the resulting object still has a mask value between 0 and 7.
102
+ mask;
103
+ // The modules of this QR Code (false = light, true = dark).
104
+ // Immutable after constructor finishes. Accessed through getModule().
105
+ modules = [];
106
+ types = [];
107
+ /* -- Accessor methods -- */
108
+ // Returns the color of the module (pixel) at the given coordinates, which is false
109
+ // for light or true for dark. The top left corner has the coordinates (x=0, y=0).
110
+ // If the given coordinates are out of bounds, then false (light) is returned.
111
+ getModule(x, y) {
112
+ return x >= 0 && x < this.size && y >= 0 && y < this.size && this.modules[y][x];
113
+ }
114
+ /* -- Private helper methods for constructor: Drawing function modules -- */
115
+ // Reads this object's version field, and draws and marks all function modules.
116
+ drawFunctionPatterns() {
117
+ for (let i = 0; i < this.size; i++) {
118
+ this.setFunctionModule(6, i, i % 2 === 0, QrCodeDataType.Timing);
119
+ this.setFunctionModule(i, 6, i % 2 === 0, QrCodeDataType.Timing);
120
+ }
121
+ this.drawFinderPattern(3, 3);
122
+ this.drawFinderPattern(this.size - 4, 3);
123
+ this.drawFinderPattern(3, this.size - 4);
124
+ const alignPatPos = this.getAlignmentPatternPositions();
125
+ const numAlign = alignPatPos.length;
126
+ for (let i = 0; i < numAlign; i++) {
127
+ for (let j = 0; j < numAlign; j++) {
128
+ if (!(i === 0 && j === 0 || i === 0 && j === numAlign - 1 || i === numAlign - 1 && j === 0))
129
+ this.drawAlignmentPattern(alignPatPos[i], alignPatPos[j]);
130
+ }
131
+ }
132
+ this.drawFormatBits(0);
133
+ this.drawVersion();
134
+ }
135
+ // Draws two copies of the format bits (with its own error correction code)
136
+ // based on the given mask and this object's error correction level field.
137
+ drawFormatBits(mask) {
138
+ const data = this.ecc[1] << 3 | mask;
139
+ let rem = data;
140
+ for (let i = 0; i < 10; i++)
141
+ rem = rem << 1 ^ (rem >>> 9) * 1335;
142
+ const bits = (data << 10 | rem) ^ 21522;
143
+ for (let i = 0; i <= 5; i++)
144
+ this.setFunctionModule(8, i, getBit(bits, i));
145
+ this.setFunctionModule(8, 7, getBit(bits, 6));
146
+ this.setFunctionModule(8, 8, getBit(bits, 7));
147
+ this.setFunctionModule(7, 8, getBit(bits, 8));
148
+ for (let i = 9; i < 15; i++)
149
+ this.setFunctionModule(14 - i, 8, getBit(bits, i));
150
+ for (let i = 0; i < 8; i++)
151
+ this.setFunctionModule(this.size - 1 - i, 8, getBit(bits, i));
152
+ for (let i = 8; i < 15; i++)
153
+ this.setFunctionModule(8, this.size - 15 + i, getBit(bits, i));
154
+ this.setFunctionModule(8, this.size - 8, true);
155
+ }
156
+ // Draws two copies of the version bits (with its own error correction code),
157
+ // based on this object's version field, iff 7 <= version <= 40.
158
+ drawVersion() {
159
+ if (this.version < 7)
160
+ return;
161
+ let rem = this.version;
162
+ for (let i = 0; i < 12; i++)
163
+ rem = rem << 1 ^ (rem >>> 11) * 7973;
164
+ const bits = this.version << 12 | rem;
165
+ for (let i = 0; i < 18; i++) {
166
+ const color = getBit(bits, i);
167
+ const a = this.size - 11 + i % 3;
168
+ const b = Math.floor(i / 3);
169
+ this.setFunctionModule(a, b, color);
170
+ this.setFunctionModule(b, a, color);
171
+ }
172
+ }
173
+ // Draws a 9*9 finder pattern including the border separator,
174
+ // with the center module at (x, y). Modules can be out of bounds.
175
+ drawFinderPattern(x, y) {
176
+ for (let dy = -4; dy <= 4; dy++) {
177
+ for (let dx = -4; dx <= 4; dx++) {
178
+ const dist = Math.max(Math.abs(dx), Math.abs(dy));
179
+ const xx = x + dx;
180
+ const yy = y + dy;
181
+ if (xx >= 0 && xx < this.size && yy >= 0 && yy < this.size)
182
+ this.setFunctionModule(xx, yy, dist !== 2 && dist !== 4, QrCodeDataType.Position);
183
+ }
184
+ }
185
+ }
186
+ // Draws a 5*5 alignment pattern, with the center module
187
+ // at (x, y). All modules must be in bounds.
188
+ drawAlignmentPattern(x, y) {
189
+ for (let dy = -2; dy <= 2; dy++) {
190
+ for (let dx = -2; dx <= 2; dx++) {
191
+ this.setFunctionModule(
192
+ x + dx,
193
+ y + dy,
194
+ Math.max(Math.abs(dx), Math.abs(dy)) !== 1,
195
+ QrCodeDataType.Alignment
196
+ );
197
+ }
198
+ }
199
+ }
200
+ // Sets the color of a module and marks it as a function module.
201
+ // Only used by the constructor. Coordinates must be in bounds.
202
+ setFunctionModule(x, y, isDark, type = QrCodeDataType.Function) {
203
+ this.modules[y][x] = isDark;
204
+ this.types[y][x] = type;
205
+ }
206
+ /* -- Private helper methods for constructor: Codewords and masking -- */
207
+ // Returns a new byte string representing the given data with the appropriate error correction
208
+ // codewords appended to it, based on this object's version and error correction level.
209
+ addEccAndInterleave(data) {
210
+ const ver = this.version;
211
+ const ecl = this.ecc;
212
+ if (data.length !== getNumDataCodewords(ver, ecl))
213
+ throw new RangeError("Invalid argument");
214
+ const numBlocks = NUM_ERROR_CORRECTION_BLOCKS[ecl[0]][ver];
215
+ const blockEccLen = ECC_CODEWORDS_PER_BLOCK[ecl[0]][ver];
216
+ const rawCodewords = Math.floor(getNumRawDataModules(ver) / 8);
217
+ const numShortBlocks = numBlocks - rawCodewords % numBlocks;
218
+ const shortBlockLen = Math.floor(rawCodewords / numBlocks);
219
+ const blocks = [];
220
+ const rsDiv = reedSolomonComputeDivisor(blockEccLen);
221
+ for (let i = 0, k = 0; i < numBlocks; i++) {
222
+ const dat = data.slice(k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1));
223
+ k += dat.length;
224
+ const ecc = reedSolomonComputeRemainder(dat, rsDiv);
225
+ if (i < numShortBlocks)
226
+ dat.push(0);
227
+ blocks.push(dat.concat(ecc));
228
+ }
229
+ const result = [];
230
+ for (let i = 0; i < blocks[0].length; i++) {
231
+ blocks.forEach((block, j) => {
232
+ if (i !== shortBlockLen - blockEccLen || j >= numShortBlocks)
233
+ result.push(block[i]);
234
+ });
235
+ }
236
+ return result;
237
+ }
238
+ // Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
239
+ // data area of this QR Code. Function modules need to be marked off before this is called.
240
+ drawCodewords(data) {
241
+ if (data.length !== Math.floor(getNumRawDataModules(this.version) / 8))
242
+ throw new RangeError("Invalid argument");
243
+ let i = 0;
244
+ for (let right = this.size - 1; right >= 1; right -= 2) {
245
+ if (right === 6)
246
+ right = 5;
247
+ for (let vert = 0; vert < this.size; vert++) {
248
+ for (let j = 0; j < 2; j++) {
249
+ const x = right - j;
250
+ const upward = (right + 1 & 2) === 0;
251
+ const y = upward ? this.size - 1 - vert : vert;
252
+ if (!this.types[y][x] && i < data.length * 8) {
253
+ this.modules[y][x] = getBit(data[i >>> 3], 7 - (i & 7));
254
+ i++;
255
+ }
256
+ }
257
+ }
258
+ }
259
+ }
260
+ // XORs the codeword modules in this QR Code with the given mask pattern.
261
+ // The function modules must be marked and the codeword bits must be drawn
262
+ // before masking. Due to the arithmetic of XOR, calling applyMask() with
263
+ // the same mask value a second time will undo the mask. A final well-formed
264
+ // QR Code needs exactly one (not zero, two, etc.) mask applied.
265
+ applyMask(mask) {
266
+ if (mask < 0 || mask > 7)
267
+ throw new RangeError("Mask value out of range");
268
+ for (let y = 0; y < this.size; y++) {
269
+ for (let x = 0; x < this.size; x++) {
270
+ let invert;
271
+ switch (mask) {
272
+ case 0:
273
+ invert = (x + y) % 2 === 0;
274
+ break;
275
+ case 1:
276
+ invert = y % 2 === 0;
277
+ break;
278
+ case 2:
279
+ invert = x % 3 === 0;
280
+ break;
281
+ case 3:
282
+ invert = (x + y) % 3 === 0;
283
+ break;
284
+ case 4:
285
+ invert = (Math.floor(x / 3) + Math.floor(y / 2)) % 2 === 0;
286
+ break;
287
+ case 5:
288
+ invert = x * y % 2 + x * y % 3 === 0;
289
+ break;
290
+ case 6:
291
+ invert = (x * y % 2 + x * y % 3) % 2 === 0;
292
+ break;
293
+ case 7:
294
+ invert = ((x + y) % 2 + x * y % 3) % 2 === 0;
295
+ break;
296
+ default:
297
+ throw new Error("Unreachable");
298
+ }
299
+ if (!this.types[y][x] && invert)
300
+ this.modules[y][x] = !this.modules[y][x];
301
+ }
302
+ }
303
+ }
304
+ // Calculates and returns the penalty score based on state of this QR Code's current modules.
305
+ // This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
306
+ getPenaltyScore() {
307
+ let result = 0;
308
+ for (let y = 0; y < this.size; y++) {
309
+ let runColor = false;
310
+ let runX = 0;
311
+ const runHistory = [0, 0, 0, 0, 0, 0, 0];
312
+ for (let x = 0; x < this.size; x++) {
313
+ if (this.modules[y][x] === runColor) {
314
+ runX++;
315
+ if (runX === 5)
316
+ result += PENALTY_N1;
317
+ else if (runX > 5)
318
+ result++;
319
+ } else {
320
+ this.finderPenaltyAddHistory(runX, runHistory);
321
+ if (!runColor)
322
+ result += this.finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
323
+ runColor = this.modules[y][x];
324
+ runX = 1;
325
+ }
326
+ }
327
+ result += this.finderPenaltyTerminateAndCount(runColor, runX, runHistory) * PENALTY_N3;
328
+ }
329
+ for (let x = 0; x < this.size; x++) {
330
+ let runColor = false;
331
+ let runY = 0;
332
+ const runHistory = [0, 0, 0, 0, 0, 0, 0];
333
+ for (let y = 0; y < this.size; y++) {
334
+ if (this.modules[y][x] === runColor) {
335
+ runY++;
336
+ if (runY === 5)
337
+ result += PENALTY_N1;
338
+ else if (runY > 5)
339
+ result++;
340
+ } else {
341
+ this.finderPenaltyAddHistory(runY, runHistory);
342
+ if (!runColor)
343
+ result += this.finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
344
+ runColor = this.modules[y][x];
345
+ runY = 1;
346
+ }
347
+ }
348
+ result += this.finderPenaltyTerminateAndCount(runColor, runY, runHistory) * PENALTY_N3;
349
+ }
350
+ for (let y = 0; y < this.size - 1; y++) {
351
+ for (let x = 0; x < this.size - 1; x++) {
352
+ const color = this.modules[y][x];
353
+ if (color === this.modules[y][x + 1] && color === this.modules[y + 1][x] && color === this.modules[y + 1][x + 1]) {
354
+ result += PENALTY_N2;
355
+ }
356
+ }
357
+ }
358
+ let dark = 0;
359
+ for (const row of this.modules)
360
+ dark = row.reduce((sum, color) => sum + (color ? 1 : 0), dark);
361
+ const total = this.size * this.size;
362
+ const k = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1;
363
+ result += k * PENALTY_N4;
364
+ return result;
365
+ }
366
+ /* -- Private helper functions -- */
367
+ // Returns an ascending list of positions of alignment patterns for this version number.
368
+ // Each position is in the range [0,177), and are used on both the x and y axes.
369
+ // This could be implemented as lookup table of 40 variable-length lists of integers.
370
+ getAlignmentPatternPositions() {
371
+ if (this.version === 1) {
372
+ return [];
373
+ } else {
374
+ const numAlign = Math.floor(this.version / 7) + 2;
375
+ const step = this.version === 32 ? 26 : Math.ceil((this.version * 4 + 4) / (numAlign * 2 - 2)) * 2;
376
+ const result = [6];
377
+ for (let pos = this.size - 7; result.length < numAlign; pos -= step)
378
+ result.splice(1, 0, pos);
379
+ return result;
380
+ }
381
+ }
382
+ // Can only be called immediately after a light run is added, and
383
+ // returns either 0, 1, or 2. A helper function for getPenaltyScore().
384
+ finderPenaltyCountPatterns(runHistory) {
385
+ const n = runHistory[1];
386
+ const core = n > 0 && runHistory[2] === n && runHistory[3] === n * 3 && runHistory[4] === n && runHistory[5] === n;
387
+ return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0) + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
388
+ }
389
+ // Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
390
+ finderPenaltyTerminateAndCount(currentRunColor, currentRunLength, runHistory) {
391
+ if (currentRunColor) {
392
+ this.finderPenaltyAddHistory(currentRunLength, runHistory);
393
+ currentRunLength = 0;
394
+ }
395
+ currentRunLength += this.size;
396
+ this.finderPenaltyAddHistory(currentRunLength, runHistory);
397
+ return this.finderPenaltyCountPatterns(runHistory);
398
+ }
399
+ // Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
400
+ finderPenaltyAddHistory(currentRunLength, runHistory) {
401
+ if (runHistory[0] === 0)
402
+ currentRunLength += this.size;
403
+ runHistory.pop();
404
+ runHistory.unshift(currentRunLength);
405
+ }
406
+ }
407
+ function appendBits(val, len, bb) {
408
+ if (len < 0 || len > 31 || val >>> len !== 0)
409
+ throw new RangeError("Value out of range");
410
+ for (let i = len - 1; i >= 0; i--)
411
+ bb.push(val >>> i & 1);
412
+ }
413
+ function getBit(x, i) {
414
+ return (x >>> i & 1) !== 0;
415
+ }
416
+ class QrSegment {
417
+ // Creates a new QR Code segment with the given attributes and data.
418
+ // The character count (numChars) must agree with the mode and the bit buffer length,
419
+ // but the constraint isn't checked. The given bit buffer is cloned and stored.
420
+ constructor(mode, numChars, bitData) {
421
+ this.mode = mode;
422
+ this.numChars = numChars;
423
+ this.bitData = bitData;
424
+ if (numChars < 0)
425
+ throw new RangeError("Invalid argument");
426
+ this.bitData = bitData.slice();
427
+ }
428
+ /* -- Methods -- */
429
+ // Returns a new copy of the data bits of this segment.
430
+ getData() {
431
+ return this.bitData.slice();
432
+ }
433
+ }
434
+ const MODE_NUMERIC = [1, 10, 12, 14];
435
+ const MODE_ALPHANUMERIC = [2, 9, 11, 13];
436
+ const MODE_BYTE = [4, 8, 16, 16];
437
+ function numCharCountBits(mode, ver) {
438
+ return mode[Math.floor((ver + 7) / 17) + 1];
439
+ }
440
+ function makeBytes(data) {
441
+ const bb = [];
442
+ for (const b of data)
443
+ appendBits(b, 8, bb);
444
+ return new QrSegment(MODE_BYTE, data.length, bb);
445
+ }
446
+ function makeNumeric(digits) {
447
+ if (!isNumeric(digits))
448
+ throw new RangeError("String contains non-numeric characters");
449
+ const bb = [];
450
+ for (let i = 0; i < digits.length; ) {
451
+ const n = Math.min(digits.length - i, 3);
452
+ appendBits(Number.parseInt(digits.substring(i, i + n), 10), n * 3 + 1, bb);
453
+ i += n;
454
+ }
455
+ return new QrSegment(MODE_NUMERIC, digits.length, bb);
456
+ }
457
+ function makeAlphanumeric(text) {
458
+ if (!isAlphanumeric(text))
459
+ throw new RangeError("String contains unencodable characters in alphanumeric mode");
460
+ const bb = [];
461
+ let i;
462
+ for (i = 0; i + 2 <= text.length; i += 2) {
463
+ let temp = ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45;
464
+ temp += ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1));
465
+ appendBits(temp, 11, bb);
466
+ }
467
+ if (i < text.length)
468
+ appendBits(ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6, bb);
469
+ return new QrSegment(MODE_ALPHANUMERIC, text.length, bb);
470
+ }
471
+ function makeSegments(text) {
472
+ if (text === "")
473
+ return [];
474
+ else if (isNumeric(text))
475
+ return [makeNumeric(text)];
476
+ else if (isAlphanumeric(text))
477
+ return [makeAlphanumeric(text)];
478
+ else
479
+ return [makeBytes(toUtf8ByteArray(text))];
480
+ }
481
+ function isNumeric(text) {
482
+ return NUMERIC_REGEX.test(text);
483
+ }
484
+ function isAlphanumeric(text) {
485
+ return ALPHANUMERIC_REGEX.test(text);
486
+ }
487
+ function getTotalBits(segs, version) {
488
+ let result = 0;
489
+ for (const seg of segs) {
490
+ const ccbits = numCharCountBits(seg.mode, version);
491
+ if (seg.numChars >= 1 << ccbits)
492
+ return Number.POSITIVE_INFINITY;
493
+ result += 4 + ccbits + seg.bitData.length;
494
+ }
495
+ return result;
496
+ }
497
+ function toUtf8ByteArray(str) {
498
+ str = encodeURI(str);
499
+ const result = [];
500
+ for (let i = 0; i < str.length; i++) {
501
+ if (str.charAt(i) !== "%") {
502
+ result.push(str.charCodeAt(i));
503
+ } else {
504
+ result.push(Number.parseInt(str.substring(i + 1, i + 3), 16));
505
+ i += 2;
506
+ }
507
+ }
508
+ return result;
509
+ }
510
+ function getNumRawDataModules(ver) {
511
+ if (ver < MIN_VERSION || ver > MAX_VERSION)
512
+ throw new RangeError("Version number out of range");
513
+ let result = (16 * ver + 128) * ver + 64;
514
+ if (ver >= 2) {
515
+ const numAlign = Math.floor(ver / 7) + 2;
516
+ result -= (25 * numAlign - 10) * numAlign - 55;
517
+ if (ver >= 7)
518
+ result -= 36;
519
+ }
520
+ return result;
521
+ }
522
+ function getNumDataCodewords(ver, ecl) {
523
+ return Math.floor(getNumRawDataModules(ver) / 8) - ECC_CODEWORDS_PER_BLOCK[ecl[0]][ver] * NUM_ERROR_CORRECTION_BLOCKS[ecl[0]][ver];
524
+ }
525
+ function reedSolomonComputeDivisor(degree) {
526
+ if (degree < 1 || degree > 255)
527
+ throw new RangeError("Degree out of range");
528
+ const result = [];
529
+ for (let i = 0; i < degree - 1; i++)
530
+ result.push(0);
531
+ result.push(1);
532
+ let root = 1;
533
+ for (let i = 0; i < degree; i++) {
534
+ for (let j = 0; j < result.length; j++) {
535
+ result[j] = reedSolomonMultiply(result[j], root);
536
+ if (j + 1 < result.length)
537
+ result[j] ^= result[j + 1];
538
+ }
539
+ root = reedSolomonMultiply(root, 2);
540
+ }
541
+ return result;
542
+ }
543
+ function reedSolomonComputeRemainder(data, divisor) {
544
+ const result = divisor.map((_) => 0);
545
+ for (const b of data) {
546
+ const factor = b ^ result.shift();
547
+ result.push(0);
548
+ divisor.forEach((coef, i) => result[i] ^= reedSolomonMultiply(coef, factor));
549
+ }
550
+ return result;
551
+ }
552
+ function reedSolomonMultiply(x, y) {
553
+ if (x >>> 8 !== 0 || y >>> 8 !== 0)
554
+ throw new RangeError("Byte out of range");
555
+ let z = 0;
556
+ for (let i = 7; i >= 0; i--) {
557
+ z = z << 1 ^ (z >>> 7) * 285;
558
+ z ^= (y >>> i & 1) * x;
559
+ }
560
+ return z;
561
+ }
562
+ function encodeSegments(segs, ecl, minVersion = 1, maxVersion = 40, mask = -1, boostEcl = true) {
563
+ if (!(MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION) || mask < -1 || mask > 7) {
564
+ throw new RangeError("Invalid value");
565
+ }
566
+ let version;
567
+ let dataUsedBits;
568
+ for (version = minVersion; ; version++) {
569
+ const dataCapacityBits2 = getNumDataCodewords(version, ecl) * 8;
570
+ const usedBits = getTotalBits(segs, version);
571
+ if (usedBits <= dataCapacityBits2) {
572
+ dataUsedBits = usedBits;
573
+ break;
574
+ }
575
+ if (version >= maxVersion)
576
+ throw new RangeError("Data too long");
577
+ }
578
+ for (const newEcl of [MEDIUM, QUARTILE, HIGH]) {
579
+ if (boostEcl && dataUsedBits <= getNumDataCodewords(version, newEcl) * 8)
580
+ ecl = newEcl;
581
+ }
582
+ const bb = [];
583
+ for (const seg of segs) {
584
+ appendBits(seg.mode[0], 4, bb);
585
+ appendBits(seg.numChars, numCharCountBits(seg.mode, version), bb);
586
+ for (const b of seg.getData())
587
+ bb.push(b);
588
+ }
589
+ const dataCapacityBits = getNumDataCodewords(version, ecl) * 8;
590
+ appendBits(0, Math.min(4, dataCapacityBits - bb.length), bb);
591
+ appendBits(0, (8 - bb.length % 8) % 8, bb);
592
+ for (let padByte = 236; bb.length < dataCapacityBits; padByte ^= 236 ^ 17)
593
+ appendBits(padByte, 8, bb);
594
+ const dataCodewords = Array.from({ length: Math.ceil(bb.length / 8) }, () => 0);
595
+ bb.forEach((b, i) => dataCodewords[i >>> 3] |= b << 7 - (i & 7));
596
+ return new QrCode(version, ecl, dataCodewords, mask);
597
+ }
598
+
599
+ function encode(data, options) {
600
+ const {
601
+ ecc = "L",
602
+ boostEcc = false,
603
+ minVersion = 1,
604
+ maxVersion = 40,
605
+ maskPattern = -1,
606
+ border = 1
607
+ } = options || {};
608
+ const segment = typeof data === "string" ? makeSegments(data) : Array.isArray(data) ? [makeBytes(data)] : void 0;
609
+ if (!segment)
610
+ throw new Error(`uqr only supports encoding string and binary data, but got: ${typeof data}`);
611
+ const qr = encodeSegments(
612
+ segment,
613
+ EccMap[ecc],
614
+ minVersion,
615
+ maxVersion,
616
+ maskPattern,
617
+ boostEcc
618
+ );
619
+ const result = addBorder({
620
+ version: qr.version,
621
+ maskPattern: qr.mask,
622
+ size: qr.size,
623
+ data: qr.modules,
624
+ types: qr.types
625
+ }, border);
626
+ if (options?.invert)
627
+ result.data = result.data.map((row) => row.map((mod) => !mod));
628
+ options?.onEncoded?.(result);
629
+ return result;
630
+ }
631
+ function addBorder(input, border = 1) {
632
+ if (!border)
633
+ return input;
634
+ const { size } = input;
635
+ const newSize = size + border * 2;
636
+ input.size = newSize;
637
+ input.data.forEach((row) => {
638
+ for (let i = 0; i < border; i++) {
639
+ row.unshift(false);
640
+ row.push(false);
641
+ }
642
+ });
643
+ for (let i = 0; i < border; i++) {
644
+ input.data.unshift(Array.from({ length: newSize }, (_) => false));
645
+ input.data.push(Array.from({ length: newSize }, (_) => false));
646
+ }
647
+ const b = QrCodeDataType.Border;
648
+ input.types.forEach((row) => {
649
+ for (let i = 0; i < border; i++) {
650
+ row.unshift(b);
651
+ row.push(b);
652
+ }
653
+ });
654
+ for (let i = 0; i < border; i++) {
655
+ input.types.unshift(Array.from({ length: newSize }, (_) => b));
656
+ input.types.push(Array.from({ length: newSize }, (_) => b));
657
+ }
658
+ return input;
659
+ }
660
+ function getDataAt(data, x, y, defaults = false) {
661
+ if (x < 0 || y < 0 || x >= data.length || y >= data.length)
662
+ return defaults;
663
+ return data[y][x];
664
+ }
665
+
666
+ function renderUnicode(data, options = {}) {
667
+ const {
668
+ whiteChar = "\u2588",
669
+ blackChar = "\u2591"
670
+ } = options;
671
+ const result = encode(data, options);
672
+ return result.data.map((row) => {
673
+ return row.map((mod) => mod ? blackChar : whiteChar).join("");
674
+ }).join("\n");
675
+ }
676
+ function renderANSI(data, options = {}) {
677
+ return renderUnicode(data, {
678
+ ...options,
679
+ blackChar: "\x1B[40m\u3000\x1B[0m",
680
+ whiteChar: "\x1B[47m\u3000\x1B[0m"
681
+ });
682
+ }
683
+ function renderUnicodeCompact(data, options = {}) {
684
+ const palette = {
685
+ WHITE_ALL: "\u2588",
686
+ WHITE_BLACK: "\u2580",
687
+ BLACK_WHITE: "\u2584",
688
+ BLACK_ALL: " "
689
+ };
690
+ const result = encode(data, options);
691
+ const WHITE = false;
692
+ const BLACK = true;
693
+ const at = (x, y) => getDataAt(result.data, x, y, true);
694
+ const lines = [];
695
+ let line = "";
696
+ for (let row = 0; row < result.size; row += 2) {
697
+ for (let col = 0; col < result.size; col++) {
698
+ if (at(col, row) === WHITE && at(col, row + 1) === WHITE)
699
+ line += palette.WHITE_ALL;
700
+ else if (at(col, row) === WHITE && at(col, row + 1) === BLACK)
701
+ line += palette.WHITE_BLACK;
702
+ else if (at(col, row) === BLACK && at(col, row + 1) === WHITE)
703
+ line += palette.BLACK_WHITE;
704
+ else
705
+ line += palette.BLACK_ALL;
706
+ }
707
+ lines.push(line);
708
+ line = "";
709
+ }
710
+ return lines.join("\n");
711
+ }
712
+
713
+ function escapeAttr(value) {
714
+ return value.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
715
+ }
716
+ function renderSVG(data, options = {}) {
717
+ const result = encode(data, options);
718
+ const {
719
+ pixelSize = 10,
720
+ whiteColor = "white",
721
+ blackColor = "black"
722
+ } = options;
723
+ const height = result.size * pixelSize;
724
+ const width = result.size * pixelSize;
725
+ let svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}">`;
726
+ const paths = [];
727
+ for (let row = 0; row < result.size; row++) {
728
+ for (let col = 0; col < result.size; col++) {
729
+ const x = col * pixelSize;
730
+ const y = row * pixelSize;
731
+ if (result.data[row][col])
732
+ paths.push(`M${x},${y}h${pixelSize}v${pixelSize}h-${pixelSize}z`);
733
+ }
734
+ }
735
+ svg += `<rect fill="${escapeAttr(whiteColor)}" width="${width}" height="${height}"/>`;
736
+ svg += `<path fill="${escapeAttr(blackColor)}" d="${paths.join("")}"/>`;
737
+ svg += "</svg>";
738
+ return svg;
739
+ }
740
+
741
+ export { QrCodeDataType, encode, renderANSI, renderSVG, renderUnicode, renderUnicodeCompact };