@vkontakte/vk-qr 2.0.13 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js DELETED
@@ -1,1184 +0,0 @@
1
- 'use strict';
2
-
3
- /*! *****************************************************************************
4
- Copyright (c) Microsoft Corporation. All rights reserved.
5
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
6
- this file except in compliance with the License. You may obtain a copy of the
7
- License at http://www.apache.org/licenses/LICENSE-2.0
8
-
9
- THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10
- KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
11
- WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
12
- MERCHANTABLITY OR NON-INFRINGEMENT.
13
-
14
- See the Apache Version 2.0 License for specific language governing permissions
15
- and limitations under the License.
16
- ***************************************************************************** */
17
-
18
- var __assign = function() {
19
- __assign = Object.assign || function __assign(t) {
20
- for (var s, i = 1, n = arguments.length; i < n; i++) {
21
- s = arguments[i];
22
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
23
- }
24
- return t;
25
- };
26
- return __assign.apply(this, arguments);
27
- };
28
-
29
- var MULTI = 1;
30
- var QR_BORDER = 7;
31
- var TILE_SIZE = 96 * MULTI;
32
- var INC_TILE_SIZE = 96 * MULTI;
33
- var SMALL_QR_SIZE = 25;
34
- /**
35
- * Returns the color of the module (pixel) at the given coordinates, which is false
36
- * for white or true for black. The top left corner has the coordinates (x=0, y=0).
37
- * If the given coordinates are out of bounds, then false (white) is returned.
38
- */
39
- var getPixel = function (x, y, size, modules, isLogoShowed) {
40
- if (x < QR_BORDER && y < QR_BORDER) {
41
- return false;
42
- }
43
- if (x >= size - QR_BORDER && y < QR_BORDER) {
44
- return false;
45
- }
46
- if (x < QR_BORDER && y >= size - QR_BORDER) {
47
- return false;
48
- }
49
- if (isLogoShowed) {
50
- var imageTiles = QR_BORDER + 2;
51
- if (size <= SMALL_QR_SIZE) {
52
- imageTiles--;
53
- }
54
- var paddingTiles = (size - QR_BORDER * 2 - imageTiles) / 2 - 1;
55
- if (size <= SMALL_QR_SIZE) {
56
- paddingTiles++;
57
- }
58
- if (x > QR_BORDER + paddingTiles &&
59
- x < size - QR_BORDER - paddingTiles - 1 &&
60
- y > QR_BORDER + paddingTiles &&
61
- y < size - QR_BORDER - paddingTiles - 1) {
62
- return false;
63
- }
64
- return 0 <= x && x < size && 0 <= y && y < size && modules[y][x];
65
- }
66
- return modules[y] && modules[y][x];
67
- };
68
- var getNeighbors = function (x, y, size, modules, isLogoShowed) {
69
- if (isLogoShowed === void 0) { isLogoShowed = true; }
70
- return ({
71
- l: getPixel(x - 1, y, size, modules, isLogoShowed),
72
- r: getPixel(x + 1, y, size, modules, isLogoShowed),
73
- t: getPixel(x, y - 1, size, modules, isLogoShowed),
74
- b: getPixel(x, y + 1, size, modules, isLogoShowed),
75
- current: getPixel(x, y, size, modules, isLogoShowed)
76
- });
77
- };
78
- /**
79
- * Converts QrCode instance to SVG code
80
- * @param qrCode QrCode instance
81
- * @param options Convertation options
82
- */
83
- var convertSegmentsToSvgString = function (qrCode, options) {
84
- if (typeof options.qrSize !== 'number') {
85
- throw new Error('Size should be a number');
86
- }
87
- if (typeof options.className !== 'string') {
88
- throw new Error('Classname should be a string');
89
- }
90
- var _2 = 2 * MULTI;
91
- var _12_7 = 12.7 * MULTI;
92
- var _12_8 = 12.8 * MULTI;
93
- var _14_7 = 14.7 * MULTI;
94
- var _14_8 = 14.8 * MULTI;
95
- var _15_9 = 15.9 * MULTI;
96
- var _28_6 = 28.6 * MULTI;
97
- var _30_5 = 30.5 * MULTI;
98
- var _84_7 = 84.7776815 * MULTI;
99
- var _87_3 = 87.3 * MULTI;
100
- var _71_4 = 71.4 * MULTI;
101
- var _42_9 = 42.9 * MULTI;
102
- var _87_2 = 87.2 * MULTI;
103
- var _85_2 = 85.2 * MULTI;
104
- var _85_3 = 85.3 * MULTI;
105
- var _69_5 = 69.5 * MULTI;
106
- var _98 = 98 * MULTI;
107
- var _100 = 100 * MULTI;
108
- var parts = [];
109
- var leftPadding = 0;
110
- var topPadding = 0;
111
- var xCoord = 0;
112
- var yCoord = 0;
113
- for (var y = 0; y < qrCode.size; y++) {
114
- leftPadding = 0;
115
- for (var x = 0; x < qrCode.size; x++) {
116
- xCoord = x + leftPadding;
117
- leftPadding += TILE_SIZE;
118
- yCoord = y + topPadding;
119
- var neighbors = getNeighbors(x, y, qrCode.size, qrCode.modules, options.isShowLogo);
120
- var path = '';
121
- var selector = '';
122
- if (neighbors.current) {
123
- selector = !selector && !neighbors.l && !neighbors.r && !neighbors.t && !neighbors.b ? 'empty' : '';
124
- selector = (!selector && neighbors.l && neighbors.r) || (neighbors.t && neighbors.b) ? 'rect' : '';
125
- if (!selector) {
126
- selector += neighbors.l ? 'l' : neighbors.r ? 'r' : '';
127
- selector += neighbors.t ? 't' : neighbors.b ? 'b' : '';
128
- if (!selector) {
129
- selector = 'empty';
130
- }
131
- }
132
- }
133
- else {
134
- selector =
135
- !selector &&
136
- neighbors.l &&
137
- neighbors.t &&
138
- getPixel(x - 1, y - 1, qrCode.size, qrCode.modules, options.isShowLogo)
139
- ? 'n_lt'
140
- : '';
141
- selector =
142
- !selector &&
143
- neighbors.l &&
144
- neighbors.b &&
145
- getPixel(x - 1, y + 1, qrCode.size, qrCode.modules, options.isShowLogo)
146
- ? 'n_lb'
147
- : '';
148
- selector =
149
- !selector &&
150
- neighbors.r &&
151
- neighbors.t &&
152
- getPixel(x + 1, y - 1, qrCode.size, qrCode.modules, options.isShowLogo)
153
- ? 'n_rt'
154
- : '';
155
- selector =
156
- !selector &&
157
- neighbors.r &&
158
- neighbors.b &&
159
- getPixel(x + 1, y + 1, qrCode.size, qrCode.modules, options.isShowLogo)
160
- ? 'n_rb'
161
- : '';
162
- }
163
- if (!selector) {
164
- continue;
165
- }
166
- path = "<use xlink:href=\"#" + selector + "-" + options.suffix + "\"/>";
167
- parts.push("<g transform=\"translate(" + xCoord + "," + yCoord + ")\">" + path + "</g>");
168
- }
169
- topPadding += TILE_SIZE;
170
- }
171
- var scale = '';
172
- var position = ((qrCode.size - QR_BORDER * 3) / 2) * TILE_SIZE + TILE_SIZE * QR_BORDER - 10;
173
- if (qrCode.size <= SMALL_QR_SIZE) {
174
- scale = 'scale(0.85)';
175
- position += 50;
176
- }
177
- var pointPosition = (qrCode.size - QR_BORDER) * INC_TILE_SIZE;
178
- parts.push("<use fill-rule=\"evenodd\" transform=\"translate(0,0)\" xlink:href=\"#point-" + options.suffix + "\"/>");
179
- parts.push("<use fill-rule=\"evenodd\" transform=\"translate(" + pointPosition + ",0)\" xlink:href=\"#point-" + options.suffix + "\"/>");
180
- parts.push("<use fill-rule=\"evenodd\" transform=\"translate(0," + pointPosition + ")\" xlink:href=\"#point-" + options.suffix + "\"/>");
181
- if (options.isShowLogo) {
182
- if (!options.logoData) {
183
- parts.push("\n <use style=\"width: 750px; height: 750px;\" width=\"750\" height=\"750\"\n fill=\"none\"\n fill-rule=\"evenodd\"\n transform=\"translate(" + position + "," + position + ") " + scale + "\" xlink:href=\"#vk_logo-" + options.suffix + "\"\n />\n ");
184
- }
185
- else {
186
- parts.push("\n <image\n preserveAspectRatio=\"xMidYMid slice\" clip-path=\"url(#logo-mask-" + options.suffix + ")\"\n style=\"width: 750px; height: 750px;\" width=\"750\" height=\"750\"\n transform=\"translate(" + position + "," + position + ") " + scale + "\"\n xlink:href=\"" + options.logoData + "\"\n />\n ");
187
- }
188
- }
189
- var backgroundSize = 99 * qrCode.size;
190
- var qrBackground = '';
191
- var qrTransform = 'translate(0,0)';
192
- if (options.isShowBackground) {
193
- var qrScale = (options.qrSize - 20 * 2) / options.qrSize; // 0.756972112
194
- var padding = (backgroundSize / options.qrSize) * 21;
195
- var radius = Math.ceil(backgroundSize / (options.qrSize / 36)); // 7.11111111 = 256px/36px radius
196
- qrBackground = "\n <rect\n x=\"0\"\n width=\"" + backgroundSize + "\"\n height=\"" + backgroundSize + "\"\n rx=\"" + radius + "\"\n fill=\"" + options.backgroundColor + "\"\n />";
197
- qrTransform = "translate(" + padding + ", " + padding + ") scale(" + qrScale + ")";
198
- }
199
- // eslint-disable-next-line
200
- var oneSide = "M0,0 L66,0 C" + _84_7 + ",-3.44940413e-15 " + _100 + ",15.2223185 " + _100 + ",34 L" + _100 + ",66 C" + _100 + "," + _84_7 + " " + _84_7 + "," + _100 + " 66," + _100 + " L0," + _100 + " L0,0 Z";
201
- var twoSides = "M0,0 L" + _100 + ",0 L" + _100 + ",66 C" + _100 + "," + _84_7 + " " + _84_7 + "," + _100 + " 66," + _100 + " L0," + _100 + " L0,0 Z";
202
- return "\n <svg\n version=\"1.1\"\n viewBox=\"0 0 " + backgroundSize + " " + backgroundSize + "\"\n width=\"" + options.qrSize + "px\"\n height=\"" + options.qrSize + "px\"\n " + (options.className ? "class=\"" + options.className + "\"" : '') + "\n xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n >\n <defs>\n <rect id=\"rect-" + options.suffix + "\" width=\"100\" height=\"100\" fill=\"" + options.foregroundColor + "\"/>\n <path\n id=\"empty-" + options.suffix + "\"\n d=\"M0," + _28_6 + "v" + _42_9 + "C0," + _87_3 + "," + _12_8 + "," + _100 + "," + _28_6 + "," + _100 + "h" + _42_9 + "c" + _15_9 + ",0," + _28_6 + "-" + _12_8 + "," + _28_6 + "-" + _28_6 + "V" + _28_6 + "C" + _100 + "," + _12_7 + "," + _87_2 + ",0," + _71_4 + ",0H" + _28_6 + " C" + _12_8 + ",0,0," + _12_8 + ",0," + _28_6 + "z\"\n fill=\"" + options.foregroundColor + "\"\n />\n <path id=\"b-" + options.suffix + "\" d=\"" + oneSide + "\" transform=\"rotate(-90 50 50)\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"r-" + options.suffix + "\" d=\"" + oneSide + "\" transform=\"rotate(-180 50 50)\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"l-" + options.suffix + "\" d=\"" + oneSide + "\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"t-" + options.suffix + "\" d=\"" + oneSide + "\" transform=\"rotate(90 50 50)\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"l-" + options.suffix + "\" d=\"" + twoSides + "\" transform=\"rotate(-90 50 50)\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"lt-" + options.suffix + "\" d=\"" + twoSides + "\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"lb-" + options.suffix + "\" d=\"" + twoSides + "\" transform=\"rotate(-90 50 50)\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"rb-" + options.suffix + "\" d=\"" + twoSides + "\" transform=\"rotate(-180 50 50)\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"rt-" + options.suffix + "\" d=\"" + twoSides + "\" transform=\"rotate(90 50 50)\" fill=\"" + options.foregroundColor + "\"/>\n <path\n id=\"n_lt-" + options.suffix + "\"\n d=\"M" + _30_5 + "," + _2 + "V0H0v" + _30_5 + "h" + _2 + "C" + _2 + "," + _14_7 + "," + _14_8 + "," + _2 + "," + _30_5 + "," + _2 + "z\"\n fill=\"" + options.foregroundColor + "\"\n />\n <path\n id=\"n_lb-" + options.suffix + "\"\n d=\"M" + _2 + "," + _69_5 + "H0V" + _100 + "h" + _30_5 + "v-" + _2 + "C" + _14_7 + "," + _98 + "," + _2 + "," + _85_2 + "," + _2 + "," + _69_5 + "z\"\n fill=\"" + options.foregroundColor + "\"\n />\n <path\n id=\"n_rt-" + options.suffix + "\"\n d=\"M" + _98 + "," + _30_5 + "h" + _2 + "V0H" + _69_5 + "v" + _2 + "C" + _85_3 + "," + _2 + "," + _98 + "," + _14_8 + "," + _98 + "," + _30_5 + "z\"\n fill=\"" + options.foregroundColor + "\"\n />\n <path id=\"n_rb-" + options.suffix + "\"\n d=\"M" + _69_5 + "," + _98 + "v" + _2 + "H" + _100 + "V" + _69_5 + "h-" + _2 + "C" + _98 + "," + _85_3 + "," + _85_2 + "," + _98 + "," + _69_5 + "," + _98 + "z\"\n fill=\"" + options.foregroundColor + "\"\n />\n <path\n id=\"point-" + options.suffix + "\"\n fill=\"" + options.foregroundColor + "\"\n d=\"M600.001786,457.329333 L600.001786,242.658167 C600.001786,147.372368 587.039517,124.122784 581.464617,118.535383 C575.877216,112.960483 552.627632,99.9982143 457.329333,99.9982143 L242.670667,99.9982143 C147.372368,99.9982143 124.122784,112.960483 118.547883,118.535383 C112.972983,124.122784 99.9982143,147.372368 99.9982143,242.658167 L99.9982143,457.329333 C99.9982143,552.627632 112.972983,575.877216 118.547883,581.464617 C124.122784,587.027017 147.372368,600.001786 242.670667,600.001786 L457.329333,600.001786 C552.627632,600.001786 575.877216,587.027017 581.464617,581.464617 C587.039517,575.877216 600.001786,552.627632 600.001786,457.329333 Z M457.329333,0 C653.338333,0 700,46.6616668 700,242.658167 C700,438.667167 700,261.332833 700,457.329333 C700,653.338333 653.338333,700 457.329333,700 C261.332833,700 438.667167,700 242.670667,700 C46.6616668,700 0,653.338333 0,457.329333 C0,261.332833 0,352.118712 0,242.658167 C0,46.6616668 46.6616668,0 242.670667,0 C438.667167,0 261.332833,0 457.329333,0 Z M395.996667,200 C480.004166,200 500,220.008332 500,303.990835 C500,387.998334 500,312.001666 500,395.996667 C500,479.991668 480.004166,500 395.996667,500 C312.001666,500 387.998334,500 304.003333,500 C220.008332,500 200,479.991668 200,395.996667 C200,312.001666 200,350.906061 200,303.990835 C200,220.008332 220.008332,200 304.003333,200 C387.998334,200 312.001666,200 395.996667,200 Z\"\n />\n <g id=\"vk_logo-" + options.suffix + "\">\n <path\n fill=\"" + options.logoColor + "\"\n d=\"M83.3334 363.333C83.3334 231.34 83.3334 165.343 124.338 124.338C165.343 83.3333 231.34 83.3333 363.333 83.3333H386.667C518.66 83.3333 584.657 83.3333 625.662 124.338C666.667 165.343 666.667 231.34 666.667 363.333V386.667C666.667 518.66 666.667 584.657 625.662 625.662C584.657 666.667 518.66 666.667 386.667 666.667H363.333C231.34 666.667 165.343 666.667 124.338 625.662C83.3334 584.657 83.3334 518.66 83.3334 386.667V363.333Z\"\n />\n <path\n fill=\"#FFF\"\n d=\"M394.907 508.681C263.293 508.681 183.442 417.365 180.313 265.625H246.972C249.05 377.09 299.763 424.323 338.638 434.036V265.625H402.519V361.82C440.016 357.664 479.264 313.909 492.456 265.625H555.333C545.27 325 502.543 368.754 472.353 386.803C502.566 401.395 551.177 439.593 569.926 508.681H500.815C486.222 462.498 450.476 426.728 402.542 421.872V508.681H394.907Z\"\n />\n </g>\n <clipPath id=\"logo-mask-" + options.suffix + "\">\n <rect x=\"0\" y=\"0\" width=\"750\" height=\"750\" />\n </clipPath>\n </defs>\n\n " + qrBackground + "\n\n <g transform=\"" + qrTransform + "\">\n " + parts.join('\n') + "\n </g>\n </svg>";
203
- };
204
-
205
- /*
206
- * QR Code generator library (TypeScript)
207
- *
208
- * Copyright (c) Project Nayuki. (MIT License)
209
- * https://www.nayuki.io/page/qr-code-generator-library
210
- *
211
- * Permission is hereby granted, free of charge, to any person obtaining a copy of
212
- * this software and associated documentation files (the "Software"), to deal in
213
- * the Software without restriction, including without limitation the rights to
214
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
215
- * the Software, and to permit persons to whom the Software is furnished to do so,
216
- * subject to the following conditions:
217
- * - The above copyright notice and this permission notice shall be included in
218
- * all copies or substantial portions of the Software.
219
- * - The Software is provided "as is", without warranty of any kind, express or
220
- * implied, including but not limited to the warranties of merchantability,
221
- * fitness for a particular purpose and noninfringement. In no event shall the
222
- * authors or copyright holders be liable for any claim, damages or other
223
- * liability, whether in an action of contract, tort or otherwise, arising from,
224
- * out of or in connection with the Software or the use or other dealings in the
225
- * Software.
226
- */
227
- /*---- QR Code symbol class ----*/
228
- /*
229
- * A QR Code symbol, which is a type of two-dimension barcode.
230
- * Invented by Denso Wave and described in the ISO/IEC 18004 standard.
231
- * Instances of this class represent an immutable square grid of black and white cells.
232
- * The class provides static factory functions to create a QR Code from text or binary data.
233
- * The class covers the QR Code Model 2 specification, supporting all versions (sizes)
234
- * from 1 to 40, all 4 error correction levels, and 4 character encoding modes.
235
- *
236
- * Ways to create a QR Code object:
237
- * - High level: Take the payload data and call QrCode.encodeText() or QrCode.encodeBinary().
238
- * - Mid level: Custom-make the list of segments and call QrCode.encodeSegments().
239
- * - Low level: Custom-make the array of data codeword bytes (including
240
- * segment headers and final padding, excluding error correction codewords),
241
- * supply the appropriate version number, and call the QrCode() constructor.
242
- * (Note that all ways require supplying the desired error correction level.)
243
- */
244
- var QrCode = /** @class */ (function () {
245
- /*-- Constructor (low level) and fields --*/
246
- // Creates a new QR Code with the given version number,
247
- // error correction level, data codeword bytes, and mask number.
248
- // This is a low-level API that most users should not use directly.
249
- // A mid-level API is the encodeSegments() function.
250
- function QrCode(
251
- // The version number of this QR Code, which is between 1 and 40 (inclusive).
252
- // This determines the size of this barcode.
253
- version,
254
- // The error correction level used in this QR Code.
255
- errorCorrectionLevel, dataCodewords,
256
- // The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).
257
- // Even if a QR Code is created with automatic masking requested (mask = -1),
258
- // the resulting object still has a mask value between 0 and 7.
259
- mask) {
260
- this.version = version;
261
- this.errorCorrectionLevel = errorCorrectionLevel;
262
- this.mask = mask;
263
- // The modules of this QR Code (false = white, true = black).
264
- // Immutable after constructor finishes. Accessed through getModule().
265
- this.modules = [];
266
- // Indicates function modules that are not subjected to masking. Discarded when constructor finishes.
267
- this.isFunction = [];
268
- // Check scalar arguments
269
- if (version < QrCode.MIN_VERSION || version > QrCode.MAX_VERSION)
270
- throw 'Version value out of range';
271
- if (mask < -1 || mask > 7)
272
- throw 'Mask value out of range';
273
- this.size = version * 4 + 17;
274
- // Initialize both grids to be size*size arrays of Boolean false
275
- var row = [];
276
- for (var i = 0; i < this.size; i++)
277
- row.push(false);
278
- for (var i = 0; i < this.size; i++) {
279
- this.modules.push(row.slice()); // Initially all white
280
- this.isFunction.push(row.slice());
281
- }
282
- // Compute ECC, draw modules
283
- this.drawFunctionPatterns();
284
- var allCodewords = this.addEccAndInterleave(dataCodewords);
285
- this.drawCodewords(allCodewords);
286
- // Do masking
287
- if (mask == -1) {
288
- // Automatically choose best mask
289
- var minPenalty = 1000000000;
290
- for (var i = 0; i < 8; i++) {
291
- this.applyMask(i);
292
- this.drawFormatBits(i);
293
- var penalty = this.getPenaltyScore();
294
- if (penalty < minPenalty) {
295
- mask = i;
296
- minPenalty = penalty;
297
- }
298
- this.applyMask(i); // Undoes the mask due to XOR
299
- }
300
- }
301
- if (mask < 0 || mask > 7)
302
- throw 'Assertion error';
303
- this.mask = mask;
304
- this.applyMask(mask); // Apply the final choice of mask
305
- this.drawFormatBits(mask); // Overwrite old format bits
306
- this.isFunction = [];
307
- }
308
- /*-- Static factory functions (high level) --*/
309
- // Returns a QR Code representing the given Unicode text string at the given error correction level.
310
- // As a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer
311
- // Unicode code points (not UTF-16 code units) if the low error correction level is used. The smallest possible
312
- // QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the
313
- // ecl argument if it can be done without increasing the version.
314
- QrCode.encodeText = function (text, ecl) {
315
- var segs = QrSegment.makeSegments(text);
316
- return QrCode.encodeSegments(segs, ecl);
317
- };
318
- // Returns a QR Code representing the given binary data at the given error correction level.
319
- // This function always encodes using the binary segment mode, not any text mode. The maximum number of
320
- // bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
321
- // The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
322
- QrCode.encodeBinary = function (data, ecl) {
323
- var seg = QrSegment.makeBytes(data);
324
- return QrCode.encodeSegments([seg], ecl);
325
- };
326
- /*-- Static factory functions (mid level) --*/
327
- // Returns a QR Code representing the given segments with the given encoding parameters.
328
- // The smallest possible QR Code version within the given range is automatically
329
- // chosen for the output. Iff boostEcl is true, then the ECC level of the result
330
- // may be higher than the ecl argument if it can be done without increasing the
331
- // version. The mask number is either between 0 to 7 (inclusive) to force that
332
- // mask, or -1 to automatically choose an appropriate mask (which may be slow).
333
- // This function allows the user to create a custom sequence of segments that switches
334
- // between modes (such as alphanumeric and byte) to encode text in less space.
335
- // This is a mid-level API; the high-level API is encodeText() and encodeBinary().
336
- QrCode.encodeSegments = function (segs, ecl, minVersion, maxVersion, mask, boostEcl) {
337
- if (minVersion === void 0) { minVersion = 1; }
338
- if (maxVersion === void 0) { maxVersion = 40; }
339
- if (mask === void 0) { mask = -1; }
340
- if (boostEcl === void 0) { boostEcl = true; }
341
- if (!(QrCode.MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= QrCode.MAX_VERSION) ||
342
- mask < -1 ||
343
- mask > 7)
344
- throw 'Invalid value';
345
- // Find the minimal version number to use
346
- var version;
347
- var dataUsedBits;
348
- for (version = minVersion;; version++) {
349
- var dataCapacityBits_1 = QrCode.getNumDataCodewords(version, ecl) * 8; // Number of data bits available
350
- var usedBits = QrSegment.getTotalBits(segs, version);
351
- if (usedBits <= dataCapacityBits_1) {
352
- dataUsedBits = usedBits;
353
- break; // This version number is found to be suitable
354
- }
355
- if (version >= maxVersion)
356
- // All versions in the range could not fit the given data
357
- throw 'Data too long';
358
- }
359
- // Increase the error correction level while the data still fits in the current version number
360
- for (var _i = 0, _a = [QrCode.Ecc.MEDIUM, QrCode.Ecc.QUARTILE, QrCode.Ecc.HIGH]; _i < _a.length; _i++) {
361
- var newEcl = _a[_i];
362
- // From low to high
363
- if (boostEcl && dataUsedBits <= QrCode.getNumDataCodewords(version, newEcl) * 8)
364
- ecl = newEcl;
365
- }
366
- // Concatenate all segments to create the data bit string
367
- var bb = new BitBuffer();
368
- for (var _b = 0, segs_1 = segs; _b < segs_1.length; _b++) {
369
- var seg = segs_1[_b];
370
- bb.appendBits(seg.mode.modeBits, 4);
371
- bb.appendBits(seg.numChars, seg.mode.numCharCountBits(version));
372
- for (var _c = 0, _d = seg.getData(); _c < _d.length; _c++) {
373
- var b = _d[_c];
374
- bb.array.push(b);
375
- }
376
- }
377
- if (bb.array.length != dataUsedBits)
378
- throw 'Assertion error';
379
- // Add terminator and pad up to a byte if applicable
380
- var dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8;
381
- if (bb.array.length > dataCapacityBits)
382
- throw 'Assertion error';
383
- bb.appendBits(0, Math.min(4, dataCapacityBits - bb.array.length));
384
- bb.appendBits(0, (8 - (bb.array.length % 8)) % 8);
385
- if (bb.array.length % 8 != 0)
386
- throw 'Assertion error';
387
- // Pad with alternating bytes until data capacity is reached
388
- for (var padByte = 0xec; bb.array.length < dataCapacityBits; padByte ^= 0xec ^ 0x11)
389
- bb.appendBits(padByte, 8);
390
- // Pack bits into bytes in big endian
391
- var dataCodewords = [];
392
- while (dataCodewords.length * 8 < bb.array.length)
393
- dataCodewords.push(0);
394
- bb.array.forEach(function (b, i) { return (dataCodewords[i >>> 3] |= b << (7 - (i & 7))); });
395
- // Create the QR Code object
396
- return new QrCode(version, ecl, dataCodewords, mask);
397
- };
398
- /*-- Accessor methods --*/
399
- // Returns the color of the module (pixel) at the given coordinates, which is false
400
- // for white or true for black. The top left corner has the coordinates (x=0, y=0).
401
- // If the given coordinates are out of bounds, then false (white) is returned.
402
- QrCode.prototype.getModule = function (x, y) {
403
- return 0 <= x && x < this.size && 0 <= y && y < this.size && this.modules[y][x];
404
- };
405
- /*-- Public instance methods --*/
406
- // Draws this QR Code, with the given module scale and border modules, onto the given HTML
407
- // canvas element. The canvas's width and height is resized to (this.size + border * 2) * scale.
408
- // The drawn image is be purely black and white, and fully opaque.
409
- // The scale must be a positive integer and the border must be a non-negative integer.
410
- QrCode.prototype.drawCanvas = function (scale, border, canvas) {
411
- if (scale <= 0 || border < 0)
412
- throw 'Value out of range';
413
- var width = (this.size + border * 2) * scale;
414
- canvas.width = width;
415
- canvas.height = width;
416
- var ctx = canvas.getContext('2d');
417
- for (var y = -border; y < this.size + border; y++) {
418
- for (var x = -border; x < this.size + border; x++) {
419
- ctx.fillStyle = this.getModule(x, y) ? '#000000' : '#FFFFFF';
420
- ctx.fillRect((x + border) * scale, (y + border) * scale, scale, scale);
421
- }
422
- }
423
- };
424
- // Returns a string of SVG code for an image depicting this QR Code, with the given number
425
- // of border modules. The string always uses Unix newlines (\n), regardless of the platform.
426
- QrCode.prototype.toSvgString = function (border) {
427
- if (border < 0)
428
- throw 'Border must be non-negative';
429
- var parts = [];
430
- for (var y = 0; y < this.size; y++) {
431
- for (var x = 0; x < this.size; x++) {
432
- if (this.getModule(x, y))
433
- parts.push("M" + (x + border) + "," + (y + border) + "h1v1h-1z");
434
- }
435
- }
436
- return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 " + (this.size + border * 2) + " " + (this.size +
437
- border * 2) + "\" stroke=\"none\">\n\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n\t<path d=\"" + parts.join(' ') + "\" fill=\"#000000\"/>\n</svg>\n";
438
- };
439
- /*-- Private helper methods for constructor: Drawing function modules --*/
440
- // Reads this object's version field, and draws and marks all function modules.
441
- QrCode.prototype.drawFunctionPatterns = function () {
442
- // Draw horizontal and vertical timing patterns
443
- for (var i = 0; i < this.size; i++) {
444
- this.setFunctionModule(6, i, i % 2 == 0);
445
- this.setFunctionModule(i, 6, i % 2 == 0);
446
- }
447
- // Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
448
- this.drawFinderPattern(3, 3);
449
- this.drawFinderPattern(this.size - 4, 3);
450
- this.drawFinderPattern(3, this.size - 4);
451
- // Draw numerous alignment patterns
452
- var alignPatPos = this.getAlignmentPatternPositions();
453
- var numAlign = alignPatPos.length;
454
- for (var i = 0; i < numAlign; i++) {
455
- for (var j = 0; j < numAlign; j++) {
456
- // Don't draw on the three finder corners
457
- if (!((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0)))
458
- this.drawAlignmentPattern(alignPatPos[i], alignPatPos[j]);
459
- }
460
- }
461
- // Draw configuration data
462
- this.drawFormatBits(0); // Dummy mask value; overwritten later in the constructor
463
- this.drawVersion();
464
- };
465
- // Draws two copies of the format bits (with its own error correction code)
466
- // based on the given mask and this object's error correction level field.
467
- QrCode.prototype.drawFormatBits = function (mask) {
468
- // Calculate error correction code and pack bits
469
- var data = (this.errorCorrectionLevel.formatBits << 3) | mask; // errCorrLvl is uint2, mask is uint3
470
- var rem = data;
471
- for (var i = 0; i < 10; i++)
472
- rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
473
- var bits = ((data << 10) | rem) ^ 0x5412; // uint15
474
- if (bits >>> 15 != 0)
475
- throw 'Assertion error';
476
- // Draw first copy
477
- for (var i = 0; i <= 5; i++)
478
- this.setFunctionModule(8, i, getBit(bits, i));
479
- this.setFunctionModule(8, 7, getBit(bits, 6));
480
- this.setFunctionModule(8, 8, getBit(bits, 7));
481
- this.setFunctionModule(7, 8, getBit(bits, 8));
482
- for (var i = 9; i < 15; i++)
483
- this.setFunctionModule(14 - i, 8, getBit(bits, i));
484
- // Draw second copy
485
- for (var i = 0; i < 8; i++)
486
- this.setFunctionModule(this.size - 1 - i, 8, getBit(bits, i));
487
- for (var i = 8; i < 15; i++)
488
- this.setFunctionModule(8, this.size - 15 + i, getBit(bits, i));
489
- this.setFunctionModule(8, this.size - 8, true); // Always black
490
- };
491
- // Draws two copies of the version bits (with its own error correction code),
492
- // based on this object's version field, iff 7 <= version <= 40.
493
- QrCode.prototype.drawVersion = function () {
494
- if (this.version < 7)
495
- return;
496
- // Calculate error correction code and pack bits
497
- var rem = this.version; // version is uint6, in the range [7, 40]
498
- for (var i = 0; i < 12; i++)
499
- rem = (rem << 1) ^ ((rem >>> 11) * 0x1f25);
500
- var bits = (this.version << 12) | rem; // uint18
501
- if (bits >>> 18 != 0)
502
- throw 'Assertion error';
503
- // Draw two copies
504
- for (var i = 0; i < 18; i++) {
505
- var color = getBit(bits, i);
506
- var a = this.size - 11 + (i % 3);
507
- var b = Math.floor(i / 3);
508
- this.setFunctionModule(a, b, color);
509
- this.setFunctionModule(b, a, color);
510
- }
511
- };
512
- // Draws a 9*9 finder pattern including the border separator,
513
- // with the center module at (x, y). Modules can be out of bounds.
514
- QrCode.prototype.drawFinderPattern = function (x, y) {
515
- for (var dy = -4; dy <= 4; dy++) {
516
- for (var dx = -4; dx <= 4; dx++) {
517
- var dist = Math.max(Math.abs(dx), Math.abs(dy)); // Chebyshev/infinity norm
518
- var xx = x + dx;
519
- var yy = y + dy;
520
- if (0 <= xx && xx < this.size && 0 <= yy && yy < this.size)
521
- this.setFunctionModule(xx, yy, dist != 2 && dist != 4);
522
- }
523
- }
524
- };
525
- // Draws a 5*5 alignment pattern, with the center module
526
- // at (x, y). All modules must be in bounds.
527
- QrCode.prototype.drawAlignmentPattern = function (x, y) {
528
- for (var dy = -2; dy <= 2; dy++) {
529
- for (var dx = -2; dx <= 2; dx++)
530
- this.setFunctionModule(x + dx, y + dy, Math.max(Math.abs(dx), Math.abs(dy)) != 1);
531
- }
532
- };
533
- // Sets the color of a module and marks it as a function module.
534
- // Only used by the constructor. Coordinates must be in bounds.
535
- QrCode.prototype.setFunctionModule = function (x, y, isBlack) {
536
- this.modules[y][x] = isBlack;
537
- this.isFunction[y][x] = true;
538
- };
539
- /*-- Private helper methods for constructor: Codewords and masking --*/
540
- // Returns a new byte string representing the given data with the appropriate error correction
541
- // codewords appended to it, based on this object's version and error correction level.
542
- QrCode.prototype.addEccAndInterleave = function (data) {
543
- var ver = this.version;
544
- var ecl = this.errorCorrectionLevel;
545
- if (data.length != QrCode.getNumDataCodewords(ver, ecl))
546
- throw 'Invalid argument';
547
- // Calculate parameter numbers
548
- var numBlocks = QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];
549
- var blockEccLen = QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver];
550
- var rawCodewords = Math.floor(QrCode.getNumRawDataModules(ver) / 8);
551
- var numShortBlocks = numBlocks - (rawCodewords % numBlocks);
552
- var shortBlockLen = Math.floor(rawCodewords / numBlocks);
553
- // Split data into blocks and append ECC to each block
554
- var blocks = [];
555
- var rs = new ReedSolomonGenerator(blockEccLen);
556
- for (var i = 0, k = 0; i < numBlocks; i++) {
557
- var dat = data.slice(k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1));
558
- k += dat.length;
559
- var ecc = rs.getRemainder(dat);
560
- if (i < numShortBlocks)
561
- dat.push(0);
562
- blocks.push(dat.concat(ecc));
563
- }
564
- // Interleave (not concatenate) the bytes from every block into a single sequence
565
- var result = [];
566
- for (var i = 0; i < blocks[0].length; i++) {
567
- for (var j = 0; j < blocks.length; j++) {
568
- // Skip the padding byte in short blocks
569
- if (i != shortBlockLen - blockEccLen || j >= numShortBlocks)
570
- result.push(blocks[j][i]);
571
- }
572
- }
573
- if (result.length != rawCodewords)
574
- throw 'Assertion error';
575
- return result;
576
- };
577
- // Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
578
- // data area of this QR Code. Function modules need to be marked off before this is called.
579
- QrCode.prototype.drawCodewords = function (data) {
580
- if (data.length != Math.floor(QrCode.getNumRawDataModules(this.version) / 8))
581
- throw 'Invalid argument';
582
- var i = 0; // Bit index into the data
583
- // Do the funny zigzag scan
584
- for (var right = this.size - 1; right >= 1; right -= 2) {
585
- // Index of right column in each column pair
586
- if (right == 6)
587
- right = 5;
588
- for (var vert = 0; vert < this.size; vert++) {
589
- // Vertical counter
590
- for (var j = 0; j < 2; j++) {
591
- var x = right - j; // Actual x coordinate
592
- var upward = ((right + 1) & 2) == 0;
593
- var y = upward ? this.size - 1 - vert : vert; // Actual y coordinate
594
- if (!this.isFunction[y][x] && i < data.length * 8) {
595
- this.modules[y][x] = getBit(data[i >>> 3], 7 - (i & 7));
596
- i++;
597
- }
598
- // If this QR Code has any remainder bits (0 to 7), they were assigned as
599
- // 0/false/white by the constructor and are left unchanged by this method
600
- }
601
- }
602
- }
603
- if (i != data.length * 8)
604
- throw 'Assertion error';
605
- };
606
- // XORs the codeword modules in this QR Code with the given mask pattern.
607
- // The function modules must be marked and the codeword bits must be drawn
608
- // before masking. Due to the arithmetic of XOR, calling applyMask() with
609
- // the same mask value a second time will undo the mask. A final well-formed
610
- // QR Code needs exactly one (not zero, two, etc.) mask applied.
611
- QrCode.prototype.applyMask = function (mask) {
612
- if (mask < 0 || mask > 7)
613
- throw 'Mask value out of range';
614
- for (var y = 0; y < this.size; y++) {
615
- for (var x = 0; x < this.size; x++) {
616
- var invert = void 0;
617
- switch (mask) {
618
- case 0:
619
- invert = (x + y) % 2 == 0;
620
- break;
621
- case 1:
622
- invert = y % 2 == 0;
623
- break;
624
- case 2:
625
- invert = x % 3 == 0;
626
- break;
627
- case 3:
628
- invert = (x + y) % 3 == 0;
629
- break;
630
- case 4:
631
- invert = (Math.floor(x / 3) + Math.floor(y / 2)) % 2 == 0;
632
- break;
633
- case 5:
634
- invert = ((x * y) % 2) + ((x * y) % 3) == 0;
635
- break;
636
- case 6:
637
- invert = (((x * y) % 2) + ((x * y) % 3)) % 2 == 0;
638
- break;
639
- case 7:
640
- invert = (((x + y) % 2) + ((x * y) % 3)) % 2 == 0;
641
- break;
642
- default:
643
- throw 'Assertion error';
644
- }
645
- if (!this.isFunction[y][x] && invert)
646
- this.modules[y][x] = !this.modules[y][x];
647
- }
648
- }
649
- };
650
- // Calculates and returns the penalty score based on state of this QR Code's current modules.
651
- // This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
652
- QrCode.prototype.getPenaltyScore = function () {
653
- var result = 0;
654
- // Adjacent modules in row having same color, and finder-like patterns
655
- for (var y = 0; y < this.size; y++) {
656
- var runHistory = [0, 0, 0, 0, 0, 0, 0];
657
- var color = false;
658
- var runX = 0;
659
- for (var x = 0; x < this.size; x++) {
660
- if (this.modules[y][x] == color) {
661
- runX++;
662
- if (runX == 5)
663
- result += QrCode.PENALTY_N1;
664
- else if (runX > 5)
665
- result++;
666
- }
667
- else {
668
- QrCode.addRunToHistory(runX, runHistory);
669
- if (!color && QrCode.hasFinderLikePattern(runHistory))
670
- result += QrCode.PENALTY_N3;
671
- color = this.modules[y][x];
672
- runX = 1;
673
- }
674
- }
675
- QrCode.addRunToHistory(runX, runHistory);
676
- if (color)
677
- QrCode.addRunToHistory(0, runHistory); // Dummy run of white
678
- if (QrCode.hasFinderLikePattern(runHistory))
679
- result += QrCode.PENALTY_N3;
680
- }
681
- // Adjacent modules in column having same color, and finder-like patterns
682
- for (var x = 0; x < this.size; x++) {
683
- var runHistory = [0, 0, 0, 0, 0, 0, 0];
684
- var color = false;
685
- var runY = 0;
686
- for (var y = 0; y < this.size; y++) {
687
- if (this.modules[y][x] == color) {
688
- runY++;
689
- if (runY == 5)
690
- result += QrCode.PENALTY_N1;
691
- else if (runY > 5)
692
- result++;
693
- }
694
- else {
695
- QrCode.addRunToHistory(runY, runHistory);
696
- if (!color && QrCode.hasFinderLikePattern(runHistory))
697
- result += QrCode.PENALTY_N3;
698
- color = this.modules[y][x];
699
- runY = 1;
700
- }
701
- }
702
- QrCode.addRunToHistory(runY, runHistory);
703
- if (color)
704
- QrCode.addRunToHistory(0, runHistory); // Dummy run of white
705
- if (QrCode.hasFinderLikePattern(runHistory))
706
- result += QrCode.PENALTY_N3;
707
- }
708
- // 2*2 blocks of modules having same color
709
- for (var y = 0; y < this.size - 1; y++) {
710
- for (var x = 0; x < this.size - 1; x++) {
711
- var color = this.modules[y][x];
712
- if (color == this.modules[y][x + 1] && color == this.modules[y + 1][x] && color == this.modules[y + 1][x + 1])
713
- result += QrCode.PENALTY_N2;
714
- }
715
- }
716
- // Balance of black and white modules
717
- var black = 0;
718
- for (var _i = 0, _a = this.modules; _i < _a.length; _i++) {
719
- var row = _a[_i];
720
- for (var _b = 0, row_1 = row; _b < row_1.length; _b++) {
721
- var color = row_1[_b];
722
- if (color)
723
- black++;
724
- }
725
- }
726
- var total = this.size * this.size; // Note that size is odd, so black/total != 1/2
727
- // Compute the smallest integer k >= 0 such that (45-5k)% <= black/total <= (55+5k)%
728
- var k = Math.ceil(Math.abs(black * 20 - total * 10) / total) - 1;
729
- result += k * QrCode.PENALTY_N4;
730
- return result;
731
- };
732
- /*-- Private helper functions --*/
733
- // Returns an ascending list of positions of alignment patterns for this version number.
734
- // Each position is in the range [0,177), and are used on both the x and y axes.
735
- // This could be implemented as lookup table of 40 variable-length lists of integers.
736
- QrCode.prototype.getAlignmentPatternPositions = function () {
737
- if (this.version == 1)
738
- return [];
739
- else {
740
- var numAlign = Math.floor(this.version / 7) + 2;
741
- var step = this.version == 32 ? 26 : Math.ceil((this.size - 13) / (numAlign * 2 - 2)) * 2;
742
- var result = [6];
743
- for (var pos = this.size - 7; result.length < numAlign; pos -= step)
744
- result.splice(1, 0, pos);
745
- return result;
746
- }
747
- };
748
- // Returns the number of data bits that can be stored in a QR Code of the given version number, after
749
- // all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
750
- // The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
751
- QrCode.getNumRawDataModules = function (ver) {
752
- if (ver < QrCode.MIN_VERSION || ver > QrCode.MAX_VERSION)
753
- throw 'Version number out of range';
754
- var result = (16 * ver + 128) * ver + 64;
755
- if (ver >= 2) {
756
- var numAlign = Math.floor(ver / 7) + 2;
757
- result -= (25 * numAlign - 10) * numAlign - 55;
758
- if (ver >= 7)
759
- result -= 36;
760
- }
761
- return result;
762
- };
763
- // Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
764
- // QR Code of the given version number and error correction level, with remainder bits discarded.
765
- // This stateless pure function could be implemented as a (40*4)-cell lookup table.
766
- QrCode.getNumDataCodewords = function (ver, ecl) {
767
- return (Math.floor(QrCode.getNumRawDataModules(ver) / 8) -
768
- QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver] * QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver]);
769
- };
770
- // Inserts the given value to the front of the given array, which shifts over the
771
- // existing values and deletes the last value. A helper function for getPenaltyScore().
772
- QrCode.addRunToHistory = function (run, history) {
773
- history.pop();
774
- history.unshift(run);
775
- };
776
- // Tests whether the given run history has the pattern of ratio 1:1:3:1:1 in the middle, and
777
- // surrounded by at least 4 on either or both ends. A helper function for getPenaltyScore().
778
- // Must only be called immediately after a run of white modules has ended.
779
- QrCode.hasFinderLikePattern = function (runHistory) {
780
- var n = runHistory[1];
781
- return (n > 0 &&
782
- runHistory[2] == n &&
783
- runHistory[4] == n &&
784
- runHistory[5] == n &&
785
- runHistory[3] == n * 3 &&
786
- Math.max(runHistory[0], runHistory[6]) >= n * 4);
787
- };
788
- /*-- Constants and tables --*/
789
- // The minimum version number supported in the QR Code Model 2 standard.
790
- QrCode.MIN_VERSION = 1;
791
- // The maximum version number supported in the QR Code Model 2 standard.
792
- QrCode.MAX_VERSION = 40;
793
- // For use in getPenaltyScore(), when evaluating which mask is best.
794
- QrCode.PENALTY_N1 = 3;
795
- QrCode.PENALTY_N2 = 3;
796
- QrCode.PENALTY_N3 = 40;
797
- QrCode.PENALTY_N4 = 10;
798
- QrCode.ECC_CODEWORDS_PER_BLOCK = [
799
- /* eslint-disable */
800
- // Version: (note that index 0 is for padding, and is set to an illegal value)
801
- //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
802
- // prettier-ignore
803
- [-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],
804
- // prettier-ignore
805
- [-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],
806
- // prettier-ignore
807
- [-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],
808
- // prettier-ignore
809
- [-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
810
- /* eslint-enable */
811
- ];
812
- QrCode.NUM_ERROR_CORRECTION_BLOCKS = [
813
- /* eslint-disable */
814
- // Version: (note that index 0 is for padding, and is set to an illegal value)
815
- //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
816
- // prettier-ignore
817
- [-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],
818
- // prettier-ignore
819
- [-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],
820
- // prettier-ignore
821
- [-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],
822
- // prettier-ignore
823
- [-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
824
- /* eslint-enable */
825
- ];
826
- return QrCode;
827
- }());
828
- // Returns true iff the i'th bit of x is set to 1.
829
- function getBit(x, i) {
830
- return ((x >>> i) & 1) != 0;
831
- }
832
- /*---- Data segment class ----*/
833
- /*
834
- * A segment of character/binary/control data in a QR Code symbol.
835
- * Instances of this class are immutable.
836
- * The mid-level way to create a segment is to take the payload data
837
- * and call a static factory function such as QrSegment.makeNumeric().
838
- * The low-level way to create a segment is to custom-make the bit buffer
839
- * and call the QrSegment() constructor with appropriate values.
840
- * This segment class imposes no length restrictions, but QR Codes have restrictions.
841
- * Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.
842
- * Any segment longer than this is meaningless for the purpose of generating QR Codes.
843
- */
844
- var QrSegment = /** @class */ (function () {
845
- /*-- Constructor (low level) and fields --*/
846
- // Creates a new QR Code segment with the given attributes and data.
847
- // The character count (numChars) must agree with the mode and the bit buffer length,
848
- // but the constraint isn't checked. The given bit buffer is cloned and stored.
849
- function QrSegment(
850
- // The mode indicator of this segment.
851
- mode,
852
- // The length of this segment's unencoded data. Measured in characters for
853
- // numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.
854
- // Always zero or positive. Not the same as the data's bit length.
855
- numChars,
856
- // The data bits of this segment. Accessed through getData().
857
- bitData) {
858
- this.mode = mode;
859
- this.numChars = numChars;
860
- this.bitData = bitData;
861
- if (numChars < 0)
862
- throw 'Invalid argument';
863
- this.bitData = bitData.slice(); // Make defensive copy
864
- }
865
- /*-- Static factory functions (mid level) --*/
866
- // Returns a segment representing the given binary data encoded in
867
- // byte mode. All input byte arrays are acceptable. Any text string
868
- // can be converted to UTF-8 bytes and encoded as a byte mode segment.
869
- QrSegment.makeBytes = function (data) {
870
- var bb = new BitBuffer();
871
- for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
872
- var b = data_1[_i];
873
- bb.appendBits(b, 8);
874
- }
875
- return new QrSegment(QrSegment.Mode.BYTE, data.length, bb.array);
876
- };
877
- // Returns a segment representing the given string of decimal digits encoded in numeric mode.
878
- QrSegment.makeNumeric = function (digits) {
879
- if (!this.NUMERIC_REGEX.test(digits))
880
- throw 'String contains non-numeric characters';
881
- var bb = new BitBuffer();
882
- for (var i = 0; i < digits.length;) {
883
- // Consume up to 3 digits per iteration
884
- var n = Math.min(digits.length - i, 3);
885
- bb.appendBits(parseInt(digits.substr(i, n), 10), n * 3 + 1);
886
- i += n;
887
- }
888
- return new QrSegment(QrSegment.Mode.NUMERIC, digits.length, bb.array);
889
- };
890
- // Returns a segment representing the given text string encoded in alphanumeric mode.
891
- // The characters allowed are: 0 to 9, A to Z (uppercase only), space,
892
- // dollar, percent, asterisk, plus, hyphen, period, slash, colon.
893
- QrSegment.makeAlphanumeric = function (text) {
894
- if (!this.ALPHANUMERIC_REGEX.test(text))
895
- throw 'String contains unencodable characters in alphanumeric mode';
896
- var bb = new BitBuffer();
897
- var i;
898
- for (i = 0; i + 2 <= text.length; i += 2) {
899
- // Process groups of 2
900
- var temp = QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45;
901
- temp += QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1));
902
- bb.appendBits(temp, 11);
903
- }
904
- if (i < text.length)
905
- // 1 character remaining
906
- bb.appendBits(QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6);
907
- return new QrSegment(QrSegment.Mode.ALPHANUMERIC, text.length, bb.array);
908
- };
909
- // Returns a new mutable list of zero or more segments to represent the given Unicode text string.
910
- // The result may use various segment modes and switch modes to optimize the length of the bit stream.
911
- QrSegment.makeSegments = function (text) {
912
- // Select the most efficient segment encoding automatically
913
- if (text == '')
914
- return [];
915
- else if (this.NUMERIC_REGEX.test(text))
916
- return [QrSegment.makeNumeric(text)];
917
- else if (this.ALPHANUMERIC_REGEX.test(text))
918
- return [QrSegment.makeAlphanumeric(text)];
919
- else
920
- return [QrSegment.makeBytes(QrSegment.toUtf8ByteArray(text))];
921
- };
922
- // Returns a segment representing an Extended Channel Interpretation
923
- // (ECI) designator with the given assignment value.
924
- QrSegment.makeEci = function (assignVal) {
925
- var bb = new BitBuffer();
926
- if (assignVal < 0)
927
- throw 'ECI assignment value out of range';
928
- else if (assignVal < 1 << 7)
929
- bb.appendBits(assignVal, 8);
930
- else if (assignVal < 1 << 14) {
931
- bb.appendBits(2, 2);
932
- bb.appendBits(assignVal, 14);
933
- }
934
- else if (assignVal < 1000000) {
935
- bb.appendBits(6, 3);
936
- bb.appendBits(assignVal, 21);
937
- }
938
- else
939
- throw 'ECI assignment value out of range';
940
- return new QrSegment(QrSegment.Mode.ECI, 0, bb.array);
941
- };
942
- /*-- Methods --*/
943
- // Returns a new copy of the data bits of this segment.
944
- QrSegment.prototype.getData = function () {
945
- return this.bitData.slice(); // Make defensive copy
946
- };
947
- // (Package-private) Calculates and returns the number of bits needed to encode the given segments at
948
- // the given version. The result is infinity if a segment has too many characters to fit its length field.
949
- QrSegment.getTotalBits = function (segs, version) {
950
- var result = 0;
951
- for (var _i = 0, segs_2 = segs; _i < segs_2.length; _i++) {
952
- var seg = segs_2[_i];
953
- var ccbits = seg.mode.numCharCountBits(version);
954
- if (seg.numChars >= 1 << ccbits)
955
- return Infinity; // The segment's length doesn't fit the field's bit width
956
- result += 4 + ccbits + seg.bitData.length;
957
- }
958
- return result;
959
- };
960
- // Returns a new array of bytes representing the given string encoded in UTF-8.
961
- QrSegment.toUtf8ByteArray = function (str) {
962
- str = encodeURI(str);
963
- var result = [];
964
- for (var i = 0; i < str.length; i++) {
965
- if (str.charAt(i) != '%')
966
- result.push(str.charCodeAt(i));
967
- else {
968
- result.push(parseInt(str.substr(i + 1, 2), 16));
969
- i += 2;
970
- }
971
- }
972
- return result;
973
- };
974
- /*-- Constants --*/
975
- // Describes precisely all strings that are encodable in numeric mode. To test
976
- // whether a string s is encodable: let ok: boolean = NUMERIC_REGEX.test(s);
977
- // A string is encodable iff each character is in the range 0 to 9.
978
- QrSegment.NUMERIC_REGEX = /^[0-9]*$/;
979
- // Describes precisely all strings that are encodable in alphanumeric mode. To test
980
- // whether a string s is encodable: let ok: boolean = ALPHANUMERIC_REGEX.test(s);
981
- // A string is encodable iff each character is in the following set: 0 to 9, A to Z
982
- // (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
983
- QrSegment.ALPHANUMERIC_REGEX = /^[A-Z0-9 $%*+.\/:-]*$/;
984
- // The set of all legal characters in alphanumeric mode,
985
- // where each character value maps to the index in the string.
986
- QrSegment.ALPHANUMERIC_CHARSET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:';
987
- return QrSegment;
988
- }());
989
- /*---- Private helper classes ----*/
990
- /*
991
- * Computes the Reed-Solomon error correction codewords for a sequence of data codewords
992
- * at a given degree. Objects are immutable, and the state only depends on the degree.
993
- * This class exists because each data block in a QR Code shares the same the divisor polynomial.
994
- */
995
- var ReedSolomonGenerator = /** @class */ (function () {
996
- // Creates a Reed-Solomon ECC generator for the given degree. This could be implemented
997
- // as a lookup table over all possible parameter values, instead of as an algorithm.
998
- function ReedSolomonGenerator(degree) {
999
- // Coefficients of the divisor polynomial, stored from highest to lowest power, excluding the leading term which
1000
- // is always 1. For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.
1001
- this.coefficients = [];
1002
- if (degree < 1 || degree > 255)
1003
- throw 'Degree out of range';
1004
- var coefs = this.coefficients;
1005
- // Start with the monomial x^0
1006
- for (var i = 0; i < degree - 1; i++)
1007
- coefs.push(0);
1008
- coefs.push(1);
1009
- // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
1010
- // drop the highest term, and store the rest of the coefficients in order of descending powers.
1011
- // Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
1012
- var root = 1;
1013
- for (var i = 0; i < degree; i++) {
1014
- // Multiply the current product by (x - r^i)
1015
- for (var j = 0; j < coefs.length; j++) {
1016
- coefs[j] = ReedSolomonGenerator.multiply(coefs[j], root);
1017
- if (j + 1 < coefs.length)
1018
- coefs[j] ^= coefs[j + 1];
1019
- }
1020
- root = ReedSolomonGenerator.multiply(root, 0x02);
1021
- }
1022
- }
1023
- // Computes and returns the Reed-Solomon error correction codewords for the given
1024
- // sequence of data codewords. The returned object is always a new byte array.
1025
- // This method does not alter this object's state (because it is immutable).
1026
- ReedSolomonGenerator.prototype.getRemainder = function (data) {
1027
- // Compute the remainder by performing polynomial division
1028
- var result = this.coefficients.map(function (_) { return 0; });
1029
- var _loop_1 = function (b) {
1030
- var factor = b ^ result.shift();
1031
- result.push(0);
1032
- this_1.coefficients.forEach(function (coef, i) { return (result[i] ^= ReedSolomonGenerator.multiply(coef, factor)); });
1033
- };
1034
- var this_1 = this;
1035
- for (var _i = 0, data_2 = data; _i < data_2.length; _i++) {
1036
- var b = data_2[_i];
1037
- _loop_1(b);
1038
- }
1039
- return result;
1040
- };
1041
- // Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
1042
- // are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.
1043
- ReedSolomonGenerator.multiply = function (x, y) {
1044
- if (x >>> 8 != 0 || y >>> 8 != 0)
1045
- throw 'Byte out of range';
1046
- // Russian peasant multiplication
1047
- var z = 0;
1048
- for (var i = 7; i >= 0; i--) {
1049
- z = (z << 1) ^ ((z >>> 7) * 0x11d);
1050
- z ^= ((y >>> i) & 1) * x;
1051
- }
1052
- if (z >>> 8 != 0)
1053
- throw 'Assertion error';
1054
- return z;
1055
- };
1056
- return ReedSolomonGenerator;
1057
- }());
1058
- /*
1059
- * An appendable sequence of bits (0s and 1s). Mainly used by QrSegment.
1060
- * The implicit constructor creates an empty bit buffer (length 0).
1061
- */
1062
- var BitBuffer = /** @class */ (function () {
1063
- function BitBuffer() {
1064
- this.array = [];
1065
- }
1066
- // Appends the given number of low-order bits of the given value
1067
- // to this buffer. Requires 0 <= len <= 31 and 0 <= val < 2^len.
1068
- BitBuffer.prototype.appendBits = function (val, len) {
1069
- if (len < 0 || len > 31 || val >>> len != 0)
1070
- throw 'Value out of range';
1071
- for (var i = len - 1; i >= 0; i-- // Append bit by bit
1072
- )
1073
- this.array.push((val >>> i) & 1);
1074
- };
1075
- return BitBuffer;
1076
- }());
1077
- /*---- Public helper enumeration ----*/
1078
- (function (QrCode) {
1079
- /*
1080
- * The error correction level in a QR Code symbol. Immutable.
1081
- */
1082
- var Ecc = /** @class */ (function () {
1083
- /*-- Constructor and fields --*/
1084
- function Ecc(
1085
- // In the range 0 to 3 (unsigned 2-bit integer).
1086
- ordinal,
1087
- // (Package-private) In the range 0 to 3 (unsigned 2-bit integer).
1088
- formatBits) {
1089
- this.ordinal = ordinal;
1090
- this.formatBits = formatBits;
1091
- }
1092
- /*-- Constants --*/
1093
- Ecc.LOW = new Ecc(0, 1); // The QR Code can tolerate about 7% erroneous codewords
1094
- Ecc.MEDIUM = new Ecc(1, 0); // The QR Code can tolerate about 15% erroneous codewords
1095
- Ecc.QUARTILE = new Ecc(2, 3); // The QR Code can tolerate about 25% erroneous codewords
1096
- Ecc.HIGH = new Ecc(3, 2); // The QR Code can tolerate about 30% erroneous codewords
1097
- return Ecc;
1098
- }());
1099
- QrCode.Ecc = Ecc;
1100
- })(QrCode || (QrCode = {}));
1101
- /*---- Public helper enumeration ----*/
1102
- (function (QrSegment) {
1103
- /*
1104
- * Describes how a segment's data bits are interpreted. Immutable.
1105
- */
1106
- var Mode = /** @class */ (function () {
1107
- /*-- Constructor and fields --*/
1108
- function Mode(
1109
- // The mode indicator bits, which is a uint4 value (range 0 to 15).
1110
- modeBits,
1111
- // Number of character count bits for three different version ranges.
1112
- numBitsCharCount) {
1113
- this.modeBits = modeBits;
1114
- this.numBitsCharCount = numBitsCharCount;
1115
- }
1116
- /*-- Method --*/
1117
- // (Package-private) Returns the bit width of the character count field for a segment in
1118
- // this mode in a QR Code at the given version number. The result is in the range [0, 16].
1119
- Mode.prototype.numCharCountBits = function (ver) {
1120
- return this.numBitsCharCount[Math.floor((ver + 7) / 17)];
1121
- };
1122
- /*-- Constants --*/
1123
- Mode.NUMERIC = new Mode(0x1, [10, 12, 14]);
1124
- Mode.ALPHANUMERIC = new Mode(0x2, [9, 11, 13]);
1125
- Mode.BYTE = new Mode(0x4, [8, 16, 16]);
1126
- Mode.KANJI = new Mode(0x8, [8, 10, 12]);
1127
- Mode.ECI = new Mode(0x7, [0, 0, 0]);
1128
- return Mode;
1129
- }());
1130
- QrSegment.Mode = Mode;
1131
- })(QrSegment || (QrSegment = {}));
1132
-
1133
- /** Default width and height of QR code */
1134
- var DEFAULT_SIZE = 128;
1135
- /** Default background color of QR code*/
1136
- var BACKGROUND_COLOR_DEFAULT = '#ffffff';
1137
- /** Default foreground color */
1138
- var FOREGROUND_COLOR_DEFAULT = '#000000';
1139
- /** Default logo color */
1140
- var LOGO_COLOR_DEFAULT = '#07f';
1141
- /**
1142
- * Implementation
1143
- */
1144
- function createQR(text, qrSizeOrOptions, classNameLegacy, legacyOptions) {
1145
- if (typeof text !== 'string') {
1146
- throw new TypeError('Enter text for encoding');
1147
- }
1148
- // Legacy interface resolve
1149
- var options = __assign(__assign(__assign({}, (typeof qrSizeOrOptions === 'object' && qrSizeOrOptions !== null ? qrSizeOrOptions : {})), (typeof legacyOptions === 'object' && legacyOptions !== null ? legacyOptions : {})), { qrSize: typeof qrSizeOrOptions === 'object' && qrSizeOrOptions !== null && typeof qrSizeOrOptions.qrSize === 'number'
1150
- ? qrSizeOrOptions.qrSize
1151
- : qrSizeOrOptions, className: typeof qrSizeOrOptions === 'object' && qrSizeOrOptions !== null && typeof qrSizeOrOptions.className === 'string'
1152
- ? qrSizeOrOptions.className
1153
- : classNameLegacy });
1154
- // ECC levels mapping
1155
- var eccLevels = [
1156
- QrCode.Ecc.LOW,
1157
- QrCode.Ecc.MEDIUM,
1158
- QrCode.Ecc.QUARTILE,
1159
- QrCode.Ecc.HIGH
1160
- ];
1161
- // Fallback undefined options
1162
- var fallbackOptions = {
1163
- qrSize: typeof options.qrSize === 'number' ? options.qrSize : DEFAULT_SIZE,
1164
- className: typeof options.className === 'string' ? options.className : classNameLegacy || '',
1165
- isShowLogo: !!options.isShowLogo || false,
1166
- isShowBackground: !!options.isShowBackground || false,
1167
- foregroundColor: typeof options.foregroundColor === 'string' ? options.foregroundColor : FOREGROUND_COLOR_DEFAULT,
1168
- backgroundColor: typeof options.backgroundColor === 'string' ? options.backgroundColor : BACKGROUND_COLOR_DEFAULT,
1169
- logoColor: typeof options.logoColor === 'string' ? options.logoColor : LOGO_COLOR_DEFAULT,
1170
- suffix: options.suffix ? options.suffix.toString() : '0',
1171
- logoData: typeof options.logoData === 'string' ? options.logoData : null,
1172
- ecc: (typeof options.ecc === 'number' && eccLevels[options.ecc] ? options.ecc : 3),
1173
- };
1174
- // Code generation
1175
- var segments = QrSegment.makeSegments(text);
1176
- var qrCode = QrCode.encodeSegments(segments, eccLevels[fallbackOptions.ecc], 1, 40, -1, true);
1177
- var svgCode = convertSegmentsToSvgString(qrCode, fallbackOptions);
1178
- return svgCode;
1179
- }
1180
- var index = {
1181
- createQR: createQR
1182
- };
1183
-
1184
- module.exports = index;