@pellux/goodvibes-sdk 0.18.29 → 0.18.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. package/dist/_internal/platform/config/schema-domain-core.d.ts +1 -0
  2. package/dist/_internal/platform/config/schema-domain-core.d.ts.map +1 -1
  3. package/dist/_internal/platform/config/schema-domain-core.js +7 -0
  4. package/dist/_internal/platform/config/schema-types.d.ts +3 -2
  5. package/dist/_internal/platform/config/schema-types.d.ts.map +1 -1
  6. package/dist/_internal/platform/config/tool-llm.d.ts.map +1 -1
  7. package/dist/_internal/platform/config/tool-llm.js +3 -0
  8. package/dist/_internal/platform/pairing/companion-token.d.ts +45 -0
  9. package/dist/_internal/platform/pairing/companion-token.d.ts.map +1 -0
  10. package/dist/_internal/platform/pairing/companion-token.js +74 -0
  11. package/dist/_internal/platform/pairing/connection-info.d.ts +10 -0
  12. package/dist/_internal/platform/pairing/connection-info.d.ts.map +1 -0
  13. package/dist/_internal/platform/pairing/connection-info.js +27 -0
  14. package/dist/_internal/platform/pairing/index.d.ts +6 -0
  15. package/dist/_internal/platform/pairing/index.d.ts.map +1 -0
  16. package/dist/_internal/platform/pairing/index.js +3 -0
  17. package/dist/_internal/platform/pairing/qr-generator.d.ts +15 -0
  18. package/dist/_internal/platform/pairing/qr-generator.d.ts.map +1 -0
  19. package/dist/_internal/platform/pairing/qr-generator.js +52 -0
  20. package/dist/_internal/platform/pairing/vendor/qrcodegen.d.ts +66 -0
  21. package/dist/_internal/platform/pairing/vendor/qrcodegen.d.ts.map +1 -0
  22. package/dist/_internal/platform/pairing/vendor/qrcodegen.js +854 -0
  23. package/dist/_internal/platform/version.js +1 -1
  24. package/package.json +40 -40
@@ -0,0 +1,854 @@
1
+ // @ts-nocheck
2
+ /*
3
+ * QR Code generator library (TypeScript)
4
+ *
5
+ * Copyright (c) Project Nayuki. (MIT License)
6
+ * https://www.nayuki.io/page/qr-code-generator-library
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
9
+ * this software and associated documentation files (the "Software"), to deal in
10
+ * the Software without restriction, including without limitation the rights to
11
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12
+ * the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ * - The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+ * - The Software is provided "as is", without warranty of any kind, express or
17
+ * implied, including but not limited to the warranties of merchantability,
18
+ * fitness for a particular purpose and noninfringement. In no event shall the
19
+ * authors or copyright holders be liable for any claim, damages or other
20
+ * liability, whether in an action of contract, tort or otherwise, arising from,
21
+ * out of or in connection with the Software or the use or other dealings in the
22
+ * Software.
23
+ */
24
+ "use strict";
25
+ export var qrcodegen;
26
+ (function (qrcodegen) {
27
+ /*---- QR Code symbol class ----*/
28
+ /*
29
+ * A QR Code symbol, which is a type of two-dimension barcode.
30
+ * Invented by Denso Wave and described in the ISO/IEC 18004 standard.
31
+ * Instances of this class represent an immutable square grid of dark and light cells.
32
+ * The class provides static factory functions to create a QR Code from text or binary data.
33
+ * The class covers the QR Code Model 2 specification, supporting all versions (sizes)
34
+ * from 1 to 40, all 4 error correction levels, and 4 character encoding modes.
35
+ *
36
+ * Ways to create a QR Code object:
37
+ * - High level: Take the payload data and call QrCode.encodeText() or QrCode.encodeBinary().
38
+ * - Mid level: Custom-make the list of segments and call QrCode.encodeSegments().
39
+ * - Low level: Custom-make the array of data codeword bytes (including
40
+ * segment headers and final padding, excluding error correction codewords),
41
+ * supply the appropriate version number, and call the QrCode() constructor.
42
+ * (Note that all ways require supplying the desired error correction level.)
43
+ */
44
+ class QrCode {
45
+ version;
46
+ errorCorrectionLevel;
47
+ /*-- Static factory functions (high level) --*/
48
+ // Returns a QR Code representing the given Unicode text string at the given error correction level.
49
+ // As a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer
50
+ // Unicode code points (not UTF-16 code units) if the low error correction level is used. The smallest possible
51
+ // QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the
52
+ // ecl argument if it can be done without increasing the version.
53
+ static encodeText(text, ecl) {
54
+ const segs = qrcodegen.QrSegment.makeSegments(text);
55
+ return QrCode.encodeSegments(segs, ecl);
56
+ }
57
+ // Returns a QR Code representing the given binary data at the given error correction level.
58
+ // This function always encodes using the binary segment mode, not any text mode. The maximum number of
59
+ // bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
60
+ // The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
61
+ static encodeBinary(data, ecl) {
62
+ const seg = qrcodegen.QrSegment.makeBytes(data);
63
+ return QrCode.encodeSegments([seg], ecl);
64
+ }
65
+ /*-- Static factory functions (mid level) --*/
66
+ // Returns a QR Code representing the given segments with the given encoding parameters.
67
+ // The smallest possible QR Code version within the given range is automatically
68
+ // chosen for the output. Iff boostEcl is true, then the ECC level of the result
69
+ // may be higher than the ecl argument if it can be done without increasing the
70
+ // version. The mask number is either between 0 to 7 (inclusive) to force that
71
+ // mask, or -1 to automatically choose an appropriate mask (which may be slow).
72
+ // This function allows the user to create a custom sequence of segments that switches
73
+ // between modes (such as alphanumeric and byte) to encode text in less space.
74
+ // This is a mid-level API; the high-level API is encodeText() and encodeBinary().
75
+ static encodeSegments(segs, ecl, minVersion = 1, maxVersion = 40, mask = -1, boostEcl = true) {
76
+ if (!(QrCode.MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= QrCode.MAX_VERSION)
77
+ || mask < -1 || mask > 7)
78
+ throw new RangeError("Invalid value");
79
+ // Find the minimal version number to use
80
+ let version;
81
+ let dataUsedBits;
82
+ for (version = minVersion;; version++) {
83
+ const dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8; // Number of data bits available
84
+ const usedBits = QrSegment.getTotalBits(segs, version);
85
+ if (usedBits <= dataCapacityBits) {
86
+ dataUsedBits = usedBits;
87
+ break; // This version number is found to be suitable
88
+ }
89
+ if (version >= maxVersion) // All versions in the range could not fit the given data
90
+ throw new RangeError("Data too long");
91
+ }
92
+ // Increase the error correction level while the data still fits in the current version number
93
+ for (const newEcl of [QrCode.Ecc.MEDIUM, QrCode.Ecc.QUARTILE, QrCode.Ecc.HIGH]) { // From low to high
94
+ if (boostEcl && dataUsedBits <= QrCode.getNumDataCodewords(version, newEcl) * 8)
95
+ ecl = newEcl;
96
+ }
97
+ // Concatenate all segments to create the data bit string
98
+ let bb = [];
99
+ for (const seg of segs) {
100
+ appendBits(seg.mode.modeBits, 4, bb);
101
+ appendBits(seg.numChars, seg.mode.numCharCountBits(version), bb);
102
+ for (const b of seg.getData())
103
+ bb.push(b);
104
+ }
105
+ assert(bb.length == dataUsedBits);
106
+ // Add terminator and pad up to a byte if applicable
107
+ const dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8;
108
+ assert(bb.length <= dataCapacityBits);
109
+ appendBits(0, Math.min(4, dataCapacityBits - bb.length), bb);
110
+ appendBits(0, (8 - bb.length % 8) % 8, bb);
111
+ assert(bb.length % 8 == 0);
112
+ // Pad with alternating bytes until data capacity is reached
113
+ for (let padByte = 0xEC; bb.length < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
114
+ appendBits(padByte, 8, bb);
115
+ // Pack bits into bytes in big endian
116
+ let dataCodewords = [];
117
+ while (dataCodewords.length * 8 < bb.length)
118
+ dataCodewords.push(0);
119
+ bb.forEach((b, i) => dataCodewords[i >>> 3] |= b << (7 - (i & 7)));
120
+ // Create the QR Code object
121
+ return new QrCode(version, ecl, dataCodewords, mask);
122
+ }
123
+ /*-- Fields --*/
124
+ // The width and height of this QR Code, measured in modules, between
125
+ // 21 and 177 (inclusive). This is equal to version * 4 + 17.
126
+ size;
127
+ // The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).
128
+ // Even if a QR Code is created with automatic masking requested (mask = -1),
129
+ // the resulting object still has a mask value between 0 and 7.
130
+ mask;
131
+ // The modules of this QR Code (false = light, true = dark).
132
+ // Immutable after constructor finishes. Accessed through getModule().
133
+ modules = [];
134
+ // Indicates function modules that are not subjected to masking. Discarded when constructor finishes.
135
+ isFunction = [];
136
+ /*-- Constructor (low level) and fields --*/
137
+ // Creates a new QR Code with the given version number,
138
+ // error correction level, data codeword bytes, and mask number.
139
+ // This is a low-level API that most users should not use directly.
140
+ // A mid-level API is the encodeSegments() function.
141
+ constructor(
142
+ // The version number of this QR Code, which is between 1 and 40 (inclusive).
143
+ // This determines the size of this barcode.
144
+ version,
145
+ // The error correction level used in this QR Code.
146
+ errorCorrectionLevel, dataCodewords, msk) {
147
+ this.version = version;
148
+ this.errorCorrectionLevel = errorCorrectionLevel;
149
+ // Check scalar arguments
150
+ if (version < QrCode.MIN_VERSION || version > QrCode.MAX_VERSION)
151
+ throw new RangeError("Version value out of range");
152
+ if (msk < -1 || msk > 7)
153
+ throw new RangeError("Mask value out of range");
154
+ this.size = version * 4 + 17;
155
+ // Initialize both grids to be size*size arrays of Boolean false
156
+ let row = [];
157
+ for (let i = 0; i < this.size; i++)
158
+ row.push(false);
159
+ for (let i = 0; i < this.size; i++) {
160
+ this.modules.push(row.slice()); // Initially all light
161
+ this.isFunction.push(row.slice());
162
+ }
163
+ // Compute ECC, draw modules
164
+ this.drawFunctionPatterns();
165
+ const allCodewords = this.addEccAndInterleave(dataCodewords);
166
+ this.drawCodewords(allCodewords);
167
+ // Do masking
168
+ if (msk == -1) { // Automatically choose best mask
169
+ let minPenalty = 1000000000;
170
+ for (let i = 0; i < 8; i++) {
171
+ this.applyMask(i);
172
+ this.drawFormatBits(i);
173
+ const penalty = this.getPenaltyScore();
174
+ if (penalty < minPenalty) {
175
+ msk = i;
176
+ minPenalty = penalty;
177
+ }
178
+ this.applyMask(i); // Undoes the mask due to XOR
179
+ }
180
+ }
181
+ assert(0 <= msk && msk <= 7);
182
+ this.mask = msk;
183
+ this.applyMask(msk); // Apply the final choice of mask
184
+ this.drawFormatBits(msk); // Overwrite old format bits
185
+ this.isFunction = [];
186
+ }
187
+ /*-- Accessor methods --*/
188
+ // Returns the color of the module (pixel) at the given coordinates, which is false
189
+ // for light or true for dark. The top left corner has the coordinates (x=0, y=0).
190
+ // If the given coordinates are out of bounds, then false (light) is returned.
191
+ getModule(x, y) {
192
+ return 0 <= x && x < this.size && 0 <= y && y < this.size && this.modules[y][x];
193
+ }
194
+ /*-- Private helper methods for constructor: Drawing function modules --*/
195
+ // Reads this object's version field, and draws and marks all function modules.
196
+ drawFunctionPatterns() {
197
+ // Draw horizontal and vertical timing patterns
198
+ for (let i = 0; i < this.size; i++) {
199
+ this.setFunctionModule(6, i, i % 2 == 0);
200
+ this.setFunctionModule(i, 6, i % 2 == 0);
201
+ }
202
+ // Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
203
+ this.drawFinderPattern(3, 3);
204
+ this.drawFinderPattern(this.size - 4, 3);
205
+ this.drawFinderPattern(3, this.size - 4);
206
+ // Draw numerous alignment patterns
207
+ const alignPatPos = this.getAlignmentPatternPositions();
208
+ const numAlign = alignPatPos.length;
209
+ for (let i = 0; i < numAlign; i++) {
210
+ for (let j = 0; j < numAlign; j++) {
211
+ // Don't draw on the three finder corners
212
+ if (!(i == 0 && j == 0 || i == 0 && j == numAlign - 1 || i == numAlign - 1 && j == 0))
213
+ this.drawAlignmentPattern(alignPatPos[i], alignPatPos[j]);
214
+ }
215
+ }
216
+ // Draw configuration data
217
+ this.drawFormatBits(0); // Dummy mask value; overwritten later in the constructor
218
+ this.drawVersion();
219
+ }
220
+ // Draws two copies of the format bits (with its own error correction code)
221
+ // based on the given mask and this object's error correction level field.
222
+ drawFormatBits(mask) {
223
+ // Calculate error correction code and pack bits
224
+ const data = this.errorCorrectionLevel.formatBits << 3 | mask; // errCorrLvl is uint2, mask is uint3
225
+ let rem = data;
226
+ for (let i = 0; i < 10; i++)
227
+ rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
228
+ const bits = (data << 10 | rem) ^ 0x5412; // uint15
229
+ assert(bits >>> 15 == 0);
230
+ // Draw first copy
231
+ for (let i = 0; i <= 5; i++)
232
+ this.setFunctionModule(8, i, getBit(bits, i));
233
+ this.setFunctionModule(8, 7, getBit(bits, 6));
234
+ this.setFunctionModule(8, 8, getBit(bits, 7));
235
+ this.setFunctionModule(7, 8, getBit(bits, 8));
236
+ for (let i = 9; i < 15; i++)
237
+ this.setFunctionModule(14 - i, 8, getBit(bits, i));
238
+ // Draw second copy
239
+ for (let i = 0; i < 8; i++)
240
+ this.setFunctionModule(this.size - 1 - i, 8, getBit(bits, i));
241
+ for (let i = 8; i < 15; i++)
242
+ this.setFunctionModule(8, this.size - 15 + i, getBit(bits, i));
243
+ this.setFunctionModule(8, this.size - 8, true); // Always dark
244
+ }
245
+ // Draws two copies of the version bits (with its own error correction code),
246
+ // based on this object's version field, iff 7 <= version <= 40.
247
+ drawVersion() {
248
+ if (this.version < 7)
249
+ return;
250
+ // Calculate error correction code and pack bits
251
+ let rem = this.version; // version is uint6, in the range [7, 40]
252
+ for (let i = 0; i < 12; i++)
253
+ rem = (rem << 1) ^ ((rem >>> 11) * 0x1F25);
254
+ const bits = this.version << 12 | rem; // uint18
255
+ assert(bits >>> 18 == 0);
256
+ // Draw two copies
257
+ for (let i = 0; i < 18; i++) {
258
+ const color = getBit(bits, i);
259
+ const a = this.size - 11 + i % 3;
260
+ const b = Math.floor(i / 3);
261
+ this.setFunctionModule(a, b, color);
262
+ this.setFunctionModule(b, a, color);
263
+ }
264
+ }
265
+ // Draws a 9*9 finder pattern including the border separator,
266
+ // with the center module at (x, y). Modules can be out of bounds.
267
+ drawFinderPattern(x, y) {
268
+ for (let dy = -4; dy <= 4; dy++) {
269
+ for (let dx = -4; dx <= 4; dx++) {
270
+ const dist = Math.max(Math.abs(dx), Math.abs(dy)); // Chebyshev/infinity norm
271
+ const xx = x + dx;
272
+ const yy = y + dy;
273
+ if (0 <= xx && xx < this.size && 0 <= yy && yy < this.size)
274
+ this.setFunctionModule(xx, yy, dist != 2 && dist != 4);
275
+ }
276
+ }
277
+ }
278
+ // Draws a 5*5 alignment pattern, with the center module
279
+ // at (x, y). All modules must be in bounds.
280
+ drawAlignmentPattern(x, y) {
281
+ for (let dy = -2; dy <= 2; dy++) {
282
+ for (let dx = -2; dx <= 2; dx++)
283
+ this.setFunctionModule(x + dx, y + dy, Math.max(Math.abs(dx), Math.abs(dy)) != 1);
284
+ }
285
+ }
286
+ // Sets the color of a module and marks it as a function module.
287
+ // Only used by the constructor. Coordinates must be in bounds.
288
+ setFunctionModule(x, y, isDark) {
289
+ this.modules[y][x] = isDark;
290
+ this.isFunction[y][x] = true;
291
+ }
292
+ /*-- Private helper methods for constructor: Codewords and masking --*/
293
+ // Returns a new byte string representing the given data with the appropriate error correction
294
+ // codewords appended to it, based on this object's version and error correction level.
295
+ addEccAndInterleave(data) {
296
+ const ver = this.version;
297
+ const ecl = this.errorCorrectionLevel;
298
+ if (data.length != QrCode.getNumDataCodewords(ver, ecl))
299
+ throw new RangeError("Invalid argument");
300
+ // Calculate parameter numbers
301
+ const numBlocks = QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];
302
+ const blockEccLen = QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver];
303
+ const rawCodewords = Math.floor(QrCode.getNumRawDataModules(ver) / 8);
304
+ const numShortBlocks = numBlocks - rawCodewords % numBlocks;
305
+ const shortBlockLen = Math.floor(rawCodewords / numBlocks);
306
+ // Split data into blocks and append ECC to each block
307
+ let blocks = [];
308
+ const rsDiv = QrCode.reedSolomonComputeDivisor(blockEccLen);
309
+ for (let i = 0, k = 0; i < numBlocks; i++) {
310
+ let dat = data.slice(k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1));
311
+ k += dat.length;
312
+ const ecc = QrCode.reedSolomonComputeRemainder(dat, rsDiv);
313
+ if (i < numShortBlocks)
314
+ dat.push(0);
315
+ blocks.push(dat.concat(ecc));
316
+ }
317
+ // Interleave (not concatenate) the bytes from every block into a single sequence
318
+ let result = [];
319
+ for (let i = 0; i < blocks[0].length; i++) {
320
+ blocks.forEach((block, j) => {
321
+ // Skip the padding byte in short blocks
322
+ if (i != shortBlockLen - blockEccLen || j >= numShortBlocks)
323
+ result.push(block[i]);
324
+ });
325
+ }
326
+ assert(result.length == rawCodewords);
327
+ return result;
328
+ }
329
+ // Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
330
+ // data area of this QR Code. Function modules need to be marked off before this is called.
331
+ drawCodewords(data) {
332
+ if (data.length != Math.floor(QrCode.getNumRawDataModules(this.version) / 8))
333
+ throw new RangeError("Invalid argument");
334
+ let i = 0; // Bit index into the data
335
+ // Do the funny zigzag scan
336
+ for (let right = this.size - 1; right >= 1; right -= 2) { // Index of right column in each column pair
337
+ if (right == 6)
338
+ right = 5;
339
+ for (let vert = 0; vert < this.size; vert++) { // Vertical counter
340
+ for (let j = 0; j < 2; j++) {
341
+ const x = right - j; // Actual x coordinate
342
+ const upward = ((right + 1) & 2) == 0;
343
+ const y = upward ? this.size - 1 - vert : vert; // Actual y coordinate
344
+ if (!this.isFunction[y][x] && i < data.length * 8) {
345
+ this.modules[y][x] = getBit(data[i >>> 3], 7 - (i & 7));
346
+ i++;
347
+ }
348
+ // If this QR Code has any remainder bits (0 to 7), they were assigned as
349
+ // 0/false/light by the constructor and are left unchanged by this method
350
+ }
351
+ }
352
+ }
353
+ assert(i == data.length * 8);
354
+ }
355
+ // XORs the codeword modules in this QR Code with the given mask pattern.
356
+ // The function modules must be marked and the codeword bits must be drawn
357
+ // before masking. Due to the arithmetic of XOR, calling applyMask() with
358
+ // the same mask value a second time will undo the mask. A final well-formed
359
+ // QR Code needs exactly one (not zero, two, etc.) mask applied.
360
+ applyMask(mask) {
361
+ if (mask < 0 || mask > 7)
362
+ throw new RangeError("Mask value out of range");
363
+ for (let y = 0; y < this.size; y++) {
364
+ for (let x = 0; x < this.size; x++) {
365
+ let invert;
366
+ switch (mask) {
367
+ case 0:
368
+ invert = (x + y) % 2 == 0;
369
+ break;
370
+ case 1:
371
+ invert = y % 2 == 0;
372
+ break;
373
+ case 2:
374
+ invert = x % 3 == 0;
375
+ break;
376
+ case 3:
377
+ invert = (x + y) % 3 == 0;
378
+ break;
379
+ case 4:
380
+ invert = (Math.floor(x / 3) + Math.floor(y / 2)) % 2 == 0;
381
+ break;
382
+ case 5:
383
+ invert = x * y % 2 + x * y % 3 == 0;
384
+ break;
385
+ case 6:
386
+ invert = (x * y % 2 + x * y % 3) % 2 == 0;
387
+ break;
388
+ case 7:
389
+ invert = ((x + y) % 2 + x * y % 3) % 2 == 0;
390
+ break;
391
+ default: throw new Error("Unreachable");
392
+ }
393
+ if (!this.isFunction[y][x] && invert)
394
+ this.modules[y][x] = !this.modules[y][x];
395
+ }
396
+ }
397
+ }
398
+ // Calculates and returns the penalty score based on state of this QR Code's current modules.
399
+ // This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
400
+ getPenaltyScore() {
401
+ let result = 0;
402
+ // Adjacent modules in row having same color, and finder-like patterns
403
+ for (let y = 0; y < this.size; y++) {
404
+ let runColor = false;
405
+ let runX = 0;
406
+ let runHistory = [0, 0, 0, 0, 0, 0, 0];
407
+ for (let x = 0; x < this.size; x++) {
408
+ if (this.modules[y][x] == runColor) {
409
+ runX++;
410
+ if (runX == 5)
411
+ result += QrCode.PENALTY_N1;
412
+ else if (runX > 5)
413
+ result++;
414
+ }
415
+ else {
416
+ this.finderPenaltyAddHistory(runX, runHistory);
417
+ if (!runColor)
418
+ result += this.finderPenaltyCountPatterns(runHistory) * QrCode.PENALTY_N3;
419
+ runColor = this.modules[y][x];
420
+ runX = 1;
421
+ }
422
+ }
423
+ result += this.finderPenaltyTerminateAndCount(runColor, runX, runHistory) * QrCode.PENALTY_N3;
424
+ }
425
+ // Adjacent modules in column having same color, and finder-like patterns
426
+ for (let x = 0; x < this.size; x++) {
427
+ let runColor = false;
428
+ let runY = 0;
429
+ let runHistory = [0, 0, 0, 0, 0, 0, 0];
430
+ for (let y = 0; y < this.size; y++) {
431
+ if (this.modules[y][x] == runColor) {
432
+ runY++;
433
+ if (runY == 5)
434
+ result += QrCode.PENALTY_N1;
435
+ else if (runY > 5)
436
+ result++;
437
+ }
438
+ else {
439
+ this.finderPenaltyAddHistory(runY, runHistory);
440
+ if (!runColor)
441
+ result += this.finderPenaltyCountPatterns(runHistory) * QrCode.PENALTY_N3;
442
+ runColor = this.modules[y][x];
443
+ runY = 1;
444
+ }
445
+ }
446
+ result += this.finderPenaltyTerminateAndCount(runColor, runY, runHistory) * QrCode.PENALTY_N3;
447
+ }
448
+ // 2*2 blocks of modules having same color
449
+ for (let y = 0; y < this.size - 1; y++) {
450
+ for (let x = 0; x < this.size - 1; x++) {
451
+ const color = this.modules[y][x];
452
+ if (color == this.modules[y][x + 1] &&
453
+ color == this.modules[y + 1][x] &&
454
+ color == this.modules[y + 1][x + 1])
455
+ result += QrCode.PENALTY_N2;
456
+ }
457
+ }
458
+ // Balance of dark and light modules
459
+ let dark = 0;
460
+ for (const row of this.modules)
461
+ dark = row.reduce((sum, color) => sum + (color ? 1 : 0), dark);
462
+ const total = this.size * this.size; // Note that size is odd, so dark/total != 1/2
463
+ // Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%
464
+ const k = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1;
465
+ assert(0 <= k && k <= 9);
466
+ result += k * QrCode.PENALTY_N4;
467
+ assert(0 <= result && result <= 2568888); // Non-tight upper bound based on default values of PENALTY_N1, ..., N4
468
+ return result;
469
+ }
470
+ /*-- Private helper functions --*/
471
+ // Returns an ascending list of positions of alignment patterns for this version number.
472
+ // Each position is in the range [0,177), and are used on both the x and y axes.
473
+ // This could be implemented as lookup table of 40 variable-length lists of integers.
474
+ getAlignmentPatternPositions() {
475
+ if (this.version == 1)
476
+ return [];
477
+ else {
478
+ const numAlign = Math.floor(this.version / 7) + 2;
479
+ const step = Math.floor((this.version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4)) * 2;
480
+ let result = [6];
481
+ for (let pos = this.size - 7; result.length < numAlign; pos -= step)
482
+ result.splice(1, 0, pos);
483
+ return result;
484
+ }
485
+ }
486
+ // Returns the number of data bits that can be stored in a QR Code of the given version number, after
487
+ // all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
488
+ // The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
489
+ static getNumRawDataModules(ver) {
490
+ if (ver < QrCode.MIN_VERSION || ver > QrCode.MAX_VERSION)
491
+ throw new RangeError("Version number out of range");
492
+ let result = (16 * ver + 128) * ver + 64;
493
+ if (ver >= 2) {
494
+ const numAlign = Math.floor(ver / 7) + 2;
495
+ result -= (25 * numAlign - 10) * numAlign - 55;
496
+ if (ver >= 7)
497
+ result -= 36;
498
+ }
499
+ assert(208 <= result && result <= 29648);
500
+ return result;
501
+ }
502
+ // Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
503
+ // QR Code of the given version number and error correction level, with remainder bits discarded.
504
+ // This stateless pure function could be implemented as a (40*4)-cell lookup table.
505
+ static getNumDataCodewords(ver, ecl) {
506
+ return Math.floor(QrCode.getNumRawDataModules(ver) / 8) -
507
+ QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver] *
508
+ QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];
509
+ }
510
+ // Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
511
+ // implemented as a lookup table over all possible parameter values, instead of as an algorithm.
512
+ static reedSolomonComputeDivisor(degree) {
513
+ if (degree < 1 || degree > 255)
514
+ throw new RangeError("Degree out of range");
515
+ // Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.
516
+ // For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array [255, 8, 93].
517
+ let result = [];
518
+ for (let i = 0; i < degree - 1; i++)
519
+ result.push(0);
520
+ result.push(1); // Start off with the monomial x^0
521
+ // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
522
+ // and drop the highest monomial term which is always 1x^degree.
523
+ // Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
524
+ let root = 1;
525
+ for (let i = 0; i < degree; i++) {
526
+ // Multiply the current product by (x - r^i)
527
+ for (let j = 0; j < result.length; j++) {
528
+ result[j] = QrCode.reedSolomonMultiply(result[j], root);
529
+ if (j + 1 < result.length)
530
+ result[j] ^= result[j + 1];
531
+ }
532
+ root = QrCode.reedSolomonMultiply(root, 0x02);
533
+ }
534
+ return result;
535
+ }
536
+ // Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
537
+ static reedSolomonComputeRemainder(data, divisor) {
538
+ let result = divisor.map(_ => 0);
539
+ for (const b of data) { // Polynomial division
540
+ const factor = b ^ result.shift();
541
+ result.push(0);
542
+ divisor.forEach((coef, i) => result[i] ^= QrCode.reedSolomonMultiply(coef, factor));
543
+ }
544
+ return result;
545
+ }
546
+ // Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
547
+ // are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.
548
+ static reedSolomonMultiply(x, y) {
549
+ if (x >>> 8 != 0 || y >>> 8 != 0)
550
+ throw new RangeError("Byte out of range");
551
+ // Russian peasant multiplication
552
+ let z = 0;
553
+ for (let i = 7; i >= 0; i--) {
554
+ z = (z << 1) ^ ((z >>> 7) * 0x11D);
555
+ z ^= ((y >>> i) & 1) * x;
556
+ }
557
+ assert(z >>> 8 == 0);
558
+ return z;
559
+ }
560
+ // Can only be called immediately after a light run is added, and
561
+ // returns either 0, 1, or 2. A helper function for getPenaltyScore().
562
+ finderPenaltyCountPatterns(runHistory) {
563
+ const n = runHistory[1];
564
+ assert(n <= this.size * 3);
565
+ const core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;
566
+ return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0)
567
+ + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
568
+ }
569
+ // Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
570
+ finderPenaltyTerminateAndCount(currentRunColor, currentRunLength, runHistory) {
571
+ if (currentRunColor) { // Terminate dark run
572
+ this.finderPenaltyAddHistory(currentRunLength, runHistory);
573
+ currentRunLength = 0;
574
+ }
575
+ currentRunLength += this.size; // Add light border to final run
576
+ this.finderPenaltyAddHistory(currentRunLength, runHistory);
577
+ return this.finderPenaltyCountPatterns(runHistory);
578
+ }
579
+ // Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
580
+ finderPenaltyAddHistory(currentRunLength, runHistory) {
581
+ if (runHistory[0] == 0)
582
+ currentRunLength += this.size; // Add light border to initial run
583
+ runHistory.pop();
584
+ runHistory.unshift(currentRunLength);
585
+ }
586
+ /*-- Constants and tables --*/
587
+ // The minimum version number supported in the QR Code Model 2 standard.
588
+ static MIN_VERSION = 1;
589
+ // The maximum version number supported in the QR Code Model 2 standard.
590
+ static MAX_VERSION = 40;
591
+ // For use in getPenaltyScore(), when evaluating which mask is best.
592
+ static PENALTY_N1 = 3;
593
+ static PENALTY_N2 = 3;
594
+ static PENALTY_N3 = 40;
595
+ static PENALTY_N4 = 10;
596
+ static ECC_CODEWORDS_PER_BLOCK = [
597
+ // Version: (note that index 0 is for padding, and is set to an illegal value)
598
+ //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
599
+ [-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], // Low
600
+ [-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], // Medium
601
+ [-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], // Quartile
602
+ [-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], // High
603
+ ];
604
+ static NUM_ERROR_CORRECTION_BLOCKS = [
605
+ // Version: (note that index 0 is for padding, and is set to an illegal value)
606
+ //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
607
+ [-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], // Low
608
+ [-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], // Medium
609
+ [-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], // Quartile
610
+ [-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], // High
611
+ ];
612
+ }
613
+ qrcodegen.QrCode = QrCode;
614
+ // Appends the given number of low-order bits of the given value
615
+ // to the given buffer. Requires 0 <= len <= 31 and 0 <= val < 2^len.
616
+ function appendBits(val, len, bb) {
617
+ if (len < 0 || len > 31 || val >>> len != 0)
618
+ throw new RangeError("Value out of range");
619
+ for (let i = len - 1; i >= 0; i--) // Append bit by bit
620
+ bb.push((val >>> i) & 1);
621
+ }
622
+ // Returns true iff the i'th bit of x is set to 1.
623
+ function getBit(x, i) {
624
+ return ((x >>> i) & 1) != 0;
625
+ }
626
+ // Throws an exception if the given condition is false.
627
+ function assert(cond) {
628
+ if (!cond)
629
+ throw new Error("Assertion error");
630
+ }
631
+ /*---- Data segment class ----*/
632
+ /*
633
+ * A segment of character/binary/control data in a QR Code symbol.
634
+ * Instances of this class are immutable.
635
+ * The mid-level way to create a segment is to take the payload data
636
+ * and call a static factory function such as QrSegment.makeNumeric().
637
+ * The low-level way to create a segment is to custom-make the bit buffer
638
+ * and call the QrSegment() constructor with appropriate values.
639
+ * This segment class imposes no length restrictions, but QR Codes have restrictions.
640
+ * Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.
641
+ * Any segment longer than this is meaningless for the purpose of generating QR Codes.
642
+ */
643
+ class QrSegment {
644
+ mode;
645
+ numChars;
646
+ bitData;
647
+ /*-- Static factory functions (mid level) --*/
648
+ // Returns a segment representing the given binary data encoded in
649
+ // byte mode. All input byte arrays are acceptable. Any text string
650
+ // can be converted to UTF-8 bytes and encoded as a byte mode segment.
651
+ static makeBytes(data) {
652
+ let bb = [];
653
+ for (const b of data)
654
+ appendBits(b, 8, bb);
655
+ return new QrSegment(QrSegment.Mode.BYTE, data.length, bb);
656
+ }
657
+ // Returns a segment representing the given string of decimal digits encoded in numeric mode.
658
+ static makeNumeric(digits) {
659
+ if (!QrSegment.isNumeric(digits))
660
+ throw new RangeError("String contains non-numeric characters");
661
+ let bb = [];
662
+ for (let i = 0; i < digits.length;) { // Consume up to 3 digits per iteration
663
+ const n = Math.min(digits.length - i, 3);
664
+ appendBits(parseInt(digits.substring(i, i + n), 10), n * 3 + 1, bb);
665
+ i += n;
666
+ }
667
+ return new QrSegment(QrSegment.Mode.NUMERIC, digits.length, bb);
668
+ }
669
+ // Returns a segment representing the given text string encoded in alphanumeric mode.
670
+ // The characters allowed are: 0 to 9, A to Z (uppercase only), space,
671
+ // dollar, percent, asterisk, plus, hyphen, period, slash, colon.
672
+ static makeAlphanumeric(text) {
673
+ if (!QrSegment.isAlphanumeric(text))
674
+ throw new RangeError("String contains unencodable characters in alphanumeric mode");
675
+ let bb = [];
676
+ let i;
677
+ for (i = 0; i + 2 <= text.length; i += 2) { // Process groups of 2
678
+ let temp = QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45;
679
+ temp += QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1));
680
+ appendBits(temp, 11, bb);
681
+ }
682
+ if (i < text.length) // 1 character remaining
683
+ appendBits(QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6, bb);
684
+ return new QrSegment(QrSegment.Mode.ALPHANUMERIC, text.length, bb);
685
+ }
686
+ // Returns a new mutable list of zero or more segments to represent the given Unicode text string.
687
+ // The result may use various segment modes and switch modes to optimize the length of the bit stream.
688
+ static makeSegments(text) {
689
+ // Select the most efficient segment encoding automatically
690
+ if (text == "")
691
+ return [];
692
+ else if (QrSegment.isNumeric(text))
693
+ return [QrSegment.makeNumeric(text)];
694
+ else if (QrSegment.isAlphanumeric(text))
695
+ return [QrSegment.makeAlphanumeric(text)];
696
+ else
697
+ return [QrSegment.makeBytes(QrSegment.toUtf8ByteArray(text))];
698
+ }
699
+ // Returns a segment representing an Extended Channel Interpretation
700
+ // (ECI) designator with the given assignment value.
701
+ static makeEci(assignVal) {
702
+ let bb = [];
703
+ if (assignVal < 0)
704
+ throw new RangeError("ECI assignment value out of range");
705
+ else if (assignVal < (1 << 7))
706
+ appendBits(assignVal, 8, bb);
707
+ else if (assignVal < (1 << 14)) {
708
+ appendBits(0b10, 2, bb);
709
+ appendBits(assignVal, 14, bb);
710
+ }
711
+ else if (assignVal < 1000000) {
712
+ appendBits(0b110, 3, bb);
713
+ appendBits(assignVal, 21, bb);
714
+ }
715
+ else
716
+ throw new RangeError("ECI assignment value out of range");
717
+ return new QrSegment(QrSegment.Mode.ECI, 0, bb);
718
+ }
719
+ // Tests whether the given string can be encoded as a segment in numeric mode.
720
+ // A string is encodable iff each character is in the range 0 to 9.
721
+ static isNumeric(text) {
722
+ return QrSegment.NUMERIC_REGEX.test(text);
723
+ }
724
+ // Tests whether the given string can be encoded as a segment in alphanumeric mode.
725
+ // A string is encodable iff each character is in the following set: 0 to 9, A to Z
726
+ // (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
727
+ static isAlphanumeric(text) {
728
+ return QrSegment.ALPHANUMERIC_REGEX.test(text);
729
+ }
730
+ /*-- Constructor (low level) and fields --*/
731
+ // Creates a new QR Code segment with the given attributes and data.
732
+ // The character count (numChars) must agree with the mode and the bit buffer length,
733
+ // but the constraint isn't checked. The given bit buffer is cloned and stored.
734
+ constructor(
735
+ // The mode indicator of this segment.
736
+ mode,
737
+ // The length of this segment's unencoded data. Measured in characters for
738
+ // numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.
739
+ // Always zero or positive. Not the same as the data's bit length.
740
+ numChars,
741
+ // The data bits of this segment. Accessed through getData().
742
+ bitData) {
743
+ this.mode = mode;
744
+ this.numChars = numChars;
745
+ this.bitData = bitData;
746
+ if (numChars < 0)
747
+ throw new RangeError("Invalid argument");
748
+ this.bitData = bitData.slice(); // Make defensive copy
749
+ }
750
+ /*-- Methods --*/
751
+ // Returns a new copy of the data bits of this segment.
752
+ getData() {
753
+ return this.bitData.slice(); // Make defensive copy
754
+ }
755
+ // (Package-private) Calculates and returns the number of bits needed to encode the given segments at
756
+ // the given version. The result is infinity if a segment has too many characters to fit its length field.
757
+ static getTotalBits(segs, version) {
758
+ let result = 0;
759
+ for (const seg of segs) {
760
+ const ccbits = seg.mode.numCharCountBits(version);
761
+ if (seg.numChars >= (1 << ccbits))
762
+ return Infinity; // The segment's length doesn't fit the field's bit width
763
+ result += 4 + ccbits + seg.bitData.length;
764
+ }
765
+ return result;
766
+ }
767
+ // Returns a new array of bytes representing the given string encoded in UTF-8.
768
+ static toUtf8ByteArray(str) {
769
+ str = encodeURI(str);
770
+ let result = [];
771
+ for (let i = 0; i < str.length; i++) {
772
+ if (str.charAt(i) != "%")
773
+ result.push(str.charCodeAt(i));
774
+ else {
775
+ result.push(parseInt(str.substring(i + 1, i + 3), 16));
776
+ i += 2;
777
+ }
778
+ }
779
+ return result;
780
+ }
781
+ /*-- Constants --*/
782
+ // Describes precisely all strings that are encodable in numeric mode.
783
+ static NUMERIC_REGEX = /^[0-9]*$/;
784
+ // Describes precisely all strings that are encodable in alphanumeric mode.
785
+ static ALPHANUMERIC_REGEX = /^[A-Z0-9 $%*+.\/:-]*$/;
786
+ // The set of all legal characters in alphanumeric mode,
787
+ // where each character value maps to the index in the string.
788
+ static ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
789
+ }
790
+ qrcodegen.QrSegment = QrSegment;
791
+ })(qrcodegen || (qrcodegen = {}));
792
+ /*---- Public helper enumeration ----*/
793
+ (function (qrcodegen) {
794
+ var QrCode;
795
+ (function (QrCode) {
796
+ /*
797
+ * The error correction level in a QR Code symbol. Immutable.
798
+ */
799
+ class Ecc {
800
+ ordinal;
801
+ formatBits;
802
+ /*-- Constants --*/
803
+ static LOW = new Ecc(0, 1); // The QR Code can tolerate about 7% erroneous codewords
804
+ static MEDIUM = new Ecc(1, 0); // The QR Code can tolerate about 15% erroneous codewords
805
+ static QUARTILE = new Ecc(2, 3); // The QR Code can tolerate about 25% erroneous codewords
806
+ static HIGH = new Ecc(3, 2); // The QR Code can tolerate about 30% erroneous codewords
807
+ /*-- Constructor and fields --*/
808
+ constructor(
809
+ // In the range 0 to 3 (unsigned 2-bit integer).
810
+ ordinal,
811
+ // (Package-private) In the range 0 to 3 (unsigned 2-bit integer).
812
+ formatBits) {
813
+ this.ordinal = ordinal;
814
+ this.formatBits = formatBits;
815
+ }
816
+ }
817
+ QrCode.Ecc = Ecc;
818
+ })(QrCode = qrcodegen.QrCode || (qrcodegen.QrCode = {}));
819
+ })(qrcodegen || (qrcodegen = {}));
820
+ /*---- Public helper enumeration ----*/
821
+ (function (qrcodegen) {
822
+ var QrSegment;
823
+ (function (QrSegment) {
824
+ /*
825
+ * Describes how a segment's data bits are interpreted. Immutable.
826
+ */
827
+ class Mode {
828
+ modeBits;
829
+ numBitsCharCount;
830
+ /*-- Constants --*/
831
+ static NUMERIC = new Mode(0x1, [10, 12, 14]);
832
+ static ALPHANUMERIC = new Mode(0x2, [9, 11, 13]);
833
+ static BYTE = new Mode(0x4, [8, 16, 16]);
834
+ static KANJI = new Mode(0x8, [8, 10, 12]);
835
+ static ECI = new Mode(0x7, [0, 0, 0]);
836
+ /*-- Constructor and fields --*/
837
+ constructor(
838
+ // The mode indicator bits, which is a uint4 value (range 0 to 15).
839
+ modeBits,
840
+ // Number of character count bits for three different version ranges.
841
+ numBitsCharCount) {
842
+ this.modeBits = modeBits;
843
+ this.numBitsCharCount = numBitsCharCount;
844
+ }
845
+ /*-- Method --*/
846
+ // (Package-private) Returns the bit width of the character count field for a segment in
847
+ // this mode in a QR Code at the given version number. The result is in the range [0, 16].
848
+ numCharCountBits(ver) {
849
+ return this.numBitsCharCount[Math.floor((ver + 7) / 17)];
850
+ }
851
+ }
852
+ QrSegment.Mode = Mode;
853
+ })(QrSegment = qrcodegen.QrSegment || (qrcodegen.QrSegment = {}));
854
+ })(qrcodegen || (qrcodegen = {}));