@fivepixels-js/react 0.2.8 → 0.2.9

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