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