UNPKG

wj-elements

Version:

WebJET Elements is a modern set of user interface tools harnessing the power of web components designed to simplify web application development.

2,191 lines 76.2 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import WJElement from "./wje-element.js"; const styles = ".container {\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n position: relative;\n gap: 0.5rem;\n padding: 0.5rem;\n}\n"; function getDefaultExportFromCjs(x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x; } var qrious$1 = { exports: {} }; var qrious = qrious$1.exports; var hasRequiredQrious; function requireQrious() { if (hasRequiredQrious) return qrious$1.exports; hasRequiredQrious = 1; (function(module, exports) { (function(global, factory) { module.exports = factory(); })(qrious, function() { var Constructor = ( /* istanbul ignore next */ function() { } ); var hasOwnProperty = Object.prototype.hasOwnProperty; var slice = Array.prototype.slice; function createObject(prototype, properties) { var result; if (typeof Object.create === "function") { result = Object.create(prototype); } else { Constructor.prototype = prototype; result = new Constructor(); Constructor.prototype = null; } if (properties) { extendObject(true, result, properties); } return result; } function extend(name, constructor, prototype, statics) { var superConstructor = this; if (typeof name !== "string") { statics = prototype; prototype = constructor; constructor = name; name = null; } if (typeof constructor !== "function") { statics = prototype; prototype = constructor; constructor = function() { return superConstructor.apply(this, arguments); }; } extendObject(false, constructor, superConstructor, statics); constructor.prototype = createObject(superConstructor.prototype, prototype); constructor.prototype.constructor = constructor; constructor.class_ = name || superConstructor.class_; constructor.super_ = superConstructor; return constructor; } function extendObject(own, target, sources) { sources = slice.call(arguments, 2); var property; var source; for (var i = 0, length = sources.length; i < length; i++) { source = sources[i]; for (property in source) { if (!own || hasOwnProperty.call(source, property)) { target[property] = source[property]; } } } } var extend_1 = extend; function Nevis() { } Nevis.class_ = "Nevis"; Nevis.super_ = Object; Nevis.extend = extend_1; var nevis = Nevis; var lite = nevis; var Renderer = lite.extend(function(qrious2, element, enabled) { this.qrious = qrious2; this.element = element; this.element.qrious = qrious2; this.enabled = Boolean(enabled); }, { /** * Draws the specified QR code <code>frame</code> on the underlying element. * * Implementations of {@link Renderer} <b>must</b> override this method with their own specific logic. * * @param {Frame} frame - the {@link Frame} to be drawn * @return {void} * @protected * @abstract * @memberof Renderer# */ draw: function(frame) { }, /** * Returns the element onto which this {@link Renderer} is rendering the QR code. * * If this method is called while this {@link Renderer} is disabled, it will be immediately enabled and rendered * before the element is returned. * * @return {*} The element. * @public * @memberof Renderer# */ getElement: function() { if (!this.enabled) { this.enabled = true; this.render(); } return this.element; }, /** * Calculates the size (in pixel units) to represent an individual module within the QR code based on the * <code>frame</code> provided. * * Any configured padding will be excluded from the returned size. * * The returned value will be at least one, even in cases where the size of the QR code does not fit its contents. * This is done so that the inevitable clipping is handled more gracefully since this way at least something is * displayed instead of just a blank space filled by the background color. * * @param {Frame} frame - the {@link Frame} from which the module size is to be derived * @return {number} The pixel size for each module in the QR code which will be no less than one. * @protected * @memberof Renderer# */ getModuleSize: function(frame) { var qrious2 = this.qrious; var padding = qrious2.padding || 0; var pixels = Math.floor((qrious2.size - padding * 2) / frame.width); return Math.max(1, pixels); }, /** * Calculates the offset/padding (in pixel units) to be inserted before the QR code based on the <code>frame</code> * provided. * * The returned value will be zero if there is no available offset or if the size of the QR code does not fit its * contents. It will never be a negative value. This is done so that the inevitable clipping appears more naturally * and it is not clipped from all directions. * * @param {Frame} frame - the {@link Frame} from which the offset is to be derived * @return {number} The pixel offset for the QR code which will be no less than zero. * @protected * @memberof Renderer# */ getOffset: function(frame) { var qrious2 = this.qrious; var padding = qrious2.padding; if (padding != null) { return padding; } var moduleSize = this.getModuleSize(frame); var offset = Math.floor((qrious2.size - moduleSize * frame.width) / 2); return Math.max(0, offset); }, /** * Renders a QR code on the underlying element based on the <code>frame</code> provided. * * @param {Frame} frame - the {@link Frame} to be rendered * @return {void} * @public * @memberof Renderer# */ render: function(frame) { if (this.enabled) { this.resize(); this.reset(); this.draw(frame); } }, /** * Resets the underlying element, effectively clearing any previously rendered QR code. * * Implementations of {@link Renderer} <b>must</b> override this method with their own specific logic. * * @return {void} * @protected * @abstract * @memberof Renderer# */ reset: function() { }, /** * Ensures that the size of the underlying element matches that defined on the associated {@link QRious} instance. * * Implementations of {@link Renderer} <b>must</b> override this method with their own specific logic. * * @return {void} * @protected * @abstract * @memberof Renderer# */ resize: function() { } }); var Renderer_1 = Renderer; var CanvasRenderer = Renderer_1.extend({ /** * @override */ draw: function(frame) { var i, j; var qrious2 = this.qrious; var moduleSize = this.getModuleSize(frame); var offset = this.getOffset(frame); var context = this.element.getContext("2d"); context.fillStyle = qrious2.foreground; context.globalAlpha = qrious2.foregroundAlpha; for (i = 0; i < frame.width; i++) { for (j = 0; j < frame.width; j++) { if (frame.buffer[j * frame.width + i]) { context.fillRect(moduleSize * i + offset, moduleSize * j + offset, moduleSize, moduleSize); } } } }, /** * @override */ reset: function() { var qrious2 = this.qrious; var context = this.element.getContext("2d"); var size = qrious2.size; context.lineWidth = 1; context.clearRect(0, 0, size, size); context.fillStyle = qrious2.background; context.globalAlpha = qrious2.backgroundAlpha; context.fillRect(0, 0, size, size); }, /** * @override */ resize: function() { var element = this.element; element.width = element.height = this.qrious.size; } }); var CanvasRenderer_1 = CanvasRenderer; var Alignment = lite.extend(null, { /** * The alignment pattern block. * * @public * @static * @type {number[]} * @memberof Alignment */ BLOCK: [ 0, 11, 15, 19, 23, 27, 31, 16, 18, 20, 22, 24, 26, 28, 20, 22, 24, 24, 26, 28, 28, 22, 24, 24, 26, 26, 28, 28, 24, 24, 26, 26, 26, 28, 28, 24, 26, 26, 26, 28, 28 ] }); var Alignment_1 = Alignment; var ErrorCorrection = lite.extend(null, { /** * The error correction blocks. * * There are four elements per version. The first two indicate the number of blocks, then the data width, and finally * the ECC width. * * @public * @static * @type {number[]} * @memberof ErrorCorrection */ BLOCKS: [ 1, 0, 19, 7, 1, 0, 16, 10, 1, 0, 13, 13, 1, 0, 9, 17, 1, 0, 34, 10, 1, 0, 28, 16, 1, 0, 22, 22, 1, 0, 16, 28, 1, 0, 55, 15, 1, 0, 44, 26, 2, 0, 17, 18, 2, 0, 13, 22, 1, 0, 80, 20, 2, 0, 32, 18, 2, 0, 24, 26, 4, 0, 9, 16, 1, 0, 108, 26, 2, 0, 43, 24, 2, 2, 15, 18, 2, 2, 11, 22, 2, 0, 68, 18, 4, 0, 27, 16, 4, 0, 19, 24, 4, 0, 15, 28, 2, 0, 78, 20, 4, 0, 31, 18, 2, 4, 14, 18, 4, 1, 13, 26, 2, 0, 97, 24, 2, 2, 38, 22, 4, 2, 18, 22, 4, 2, 14, 26, 2, 0, 116, 30, 3, 2, 36, 22, 4, 4, 16, 20, 4, 4, 12, 24, 2, 2, 68, 18, 4, 1, 43, 26, 6, 2, 19, 24, 6, 2, 15, 28, 4, 0, 81, 20, 1, 4, 50, 30, 4, 4, 22, 28, 3, 8, 12, 24, 2, 2, 92, 24, 6, 2, 36, 22, 4, 6, 20, 26, 7, 4, 14, 28, 4, 0, 107, 26, 8, 1, 37, 22, 8, 4, 20, 24, 12, 4, 11, 22, 3, 1, 115, 30, 4, 5, 40, 24, 11, 5, 16, 20, 11, 5, 12, 24, 5, 1, 87, 22, 5, 5, 41, 24, 5, 7, 24, 30, 11, 7, 12, 24, 5, 1, 98, 24, 7, 3, 45, 28, 15, 2, 19, 24, 3, 13, 15, 30, 1, 5, 107, 28, 10, 1, 46, 28, 1, 15, 22, 28, 2, 17, 14, 28, 5, 1, 120, 30, 9, 4, 43, 26, 17, 1, 22, 28, 2, 19, 14, 28, 3, 4, 113, 28, 3, 11, 44, 26, 17, 4, 21, 26, 9, 16, 13, 26, 3, 5, 107, 28, 3, 13, 41, 26, 15, 5, 24, 30, 15, 10, 15, 28, 4, 4, 116, 28, 17, 0, 42, 26, 17, 6, 22, 28, 19, 6, 16, 30, 2, 7, 111, 28, 17, 0, 46, 28, 7, 16, 24, 30, 34, 0, 13, 24, 4, 5, 121, 30, 4, 14, 47, 28, 11, 14, 24, 30, 16, 14, 15, 30, 6, 4, 117, 30, 6, 14, 45, 28, 11, 16, 24, 30, 30, 2, 16, 30, 8, 4, 106, 26, 8, 13, 47, 28, 7, 22, 24, 30, 22, 13, 15, 30, 10, 2, 114, 28, 19, 4, 46, 28, 28, 6, 22, 28, 33, 4, 16, 30, 8, 4, 122, 30, 22, 3, 45, 28, 8, 26, 23, 30, 12, 28, 15, 30, 3, 10, 117, 30, 3, 23, 45, 28, 4, 31, 24, 30, 11, 31, 15, 30, 7, 7, 116, 30, 21, 7, 45, 28, 1, 37, 23, 30, 19, 26, 15, 30, 5, 10, 115, 30, 19, 10, 47, 28, 15, 25, 24, 30, 23, 25, 15, 30, 13, 3, 115, 30, 2, 29, 46, 28, 42, 1, 24, 30, 23, 28, 15, 30, 17, 0, 115, 30, 10, 23, 46, 28, 10, 35, 24, 30, 19, 35, 15, 30, 17, 1, 115, 30, 14, 21, 46, 28, 29, 19, 24, 30, 11, 46, 15, 30, 13, 6, 115, 30, 14, 23, 46, 28, 44, 7, 24, 30, 59, 1, 16, 30, 12, 7, 121, 30, 12, 26, 47, 28, 39, 14, 24, 30, 22, 41, 15, 30, 6, 14, 121, 30, 6, 34, 47, 28, 46, 10, 24, 30, 2, 64, 15, 30, 17, 4, 122, 30, 29, 14, 46, 28, 49, 10, 24, 30, 24, 46, 15, 30, 4, 18, 122, 30, 13, 32, 46, 28, 48, 14, 24, 30, 42, 32, 15, 30, 20, 4, 117, 30, 40, 7, 47, 28, 43, 22, 24, 30, 10, 67, 15, 30, 19, 6, 118, 30, 18, 31, 47, 28, 34, 34, 24, 30, 20, 61, 15, 30 ], /** * The final format bits with mask (level << 3 | mask). * * @public * @static * @type {number[]} * @memberof ErrorCorrection */ FINAL_FORMAT: [ // L 30660, 29427, 32170, 30877, 26159, 25368, 27713, 26998, // M 21522, 20773, 24188, 23371, 17913, 16590, 20375, 19104, // Q 13663, 12392, 16177, 14854, 9396, 8579, 11994, 11245, // H 5769, 5054, 7399, 6608, 1890, 597, 3340, 2107 ], /** * A map of human-readable ECC levels. * * @public * @static * @type {Object.<string, number>} * @memberof ErrorCorrection */ LEVELS: { L: 1, M: 2, Q: 3, H: 4 } }); var ErrorCorrection_1 = ErrorCorrection; var Galois = lite.extend(null, { /** * The Galois field exponent table. * * @public * @static * @type {number[]} * @memberof Galois */ EXPONENT: [ 1, 2, 4, 8, 16, 32, 64, 128, 29, 58, 116, 232, 205, 135, 19, 38, 76, 152, 45, 90, 180, 117, 234, 201, 143, 3, 6, 12, 24, 48, 96, 192, 157, 39, 78, 156, 37, 74, 148, 53, 106, 212, 181, 119, 238, 193, 159, 35, 70, 140, 5, 10, 20, 40, 80, 160, 93, 186, 105, 210, 185, 111, 222, 161, 95, 190, 97, 194, 153, 47, 94, 188, 101, 202, 137, 15, 30, 60, 120, 240, 253, 231, 211, 187, 107, 214, 177, 127, 254, 225, 223, 163, 91, 182, 113, 226, 217, 175, 67, 134, 17, 34, 68, 136, 13, 26, 52, 104, 208, 189, 103, 206, 129, 31, 62, 124, 248, 237, 199, 147, 59, 118, 236, 197, 151, 51, 102, 204, 133, 23, 46, 92, 184, 109, 218, 169, 79, 158, 33, 66, 132, 21, 42, 84, 168, 77, 154, 41, 82, 164, 85, 170, 73, 146, 57, 114, 228, 213, 183, 115, 230, 209, 191, 99, 198, 145, 63, 126, 252, 229, 215, 179, 123, 246, 241, 255, 227, 219, 171, 75, 150, 49, 98, 196, 149, 55, 110, 220, 165, 87, 174, 65, 130, 25, 50, 100, 200, 141, 7, 14, 28, 56, 112, 224, 221, 167, 83, 166, 81, 162, 89, 178, 121, 242, 249, 239, 195, 155, 43, 86, 172, 69, 138, 9, 18, 36, 72, 144, 61, 122, 244, 245, 247, 243, 251, 235, 203, 139, 11, 22, 44, 88, 176, 125, 250, 233, 207, 131, 27, 54, 108, 216, 173, 71, 142, 0 ], /** * The Galois field log table. * * @public * @static * @type {number[]} * @memberof Galois */ LOG: [ 255, 0, 1, 25, 2, 50, 26, 198, 3, 223, 51, 238, 27, 104, 199, 75, 4, 100, 224, 14, 52, 141, 239, 129, 28, 193, 105, 248, 200, 8, 76, 113, 5, 138, 101, 47, 225, 36, 15, 33, 53, 147, 142, 218, 240, 18, 130, 69, 29, 181, 194, 125, 106, 39, 249, 185, 201, 154, 9, 120, 77, 228, 114, 166, 6, 191, 139, 98, 102, 221, 48, 253, 226, 152, 37, 179, 16, 145, 34, 136, 54, 208, 148, 206, 143, 150, 219, 189, 241, 210, 19, 92, 131, 56, 70, 64, 30, 66, 182, 163, 195, 72, 126, 110, 107, 58, 40, 84, 250, 133, 186, 61, 202, 94, 155, 159, 10, 21, 121, 43, 78, 212, 229, 172, 115, 243, 167, 87, 7, 112, 192, 247, 140, 128, 99, 13, 103, 74, 222, 237, 49, 197, 254, 24, 227, 165, 153, 119, 38, 184, 180, 124, 17, 68, 146, 217, 35, 32, 137, 46, 55, 63, 209, 91, 149, 188, 207, 205, 144, 135, 151, 178, 220, 252, 190, 97, 242, 86, 211, 171, 20, 42, 93, 158, 132, 60, 57, 83, 71, 109, 65, 162, 31, 45, 67, 216, 183, 123, 164, 118, 196, 23, 73, 236, 127, 12, 111, 246, 108, 161, 59, 82, 41, 157, 85, 170, 251, 96, 134, 177, 187, 204, 62, 90, 203, 89, 95, 176, 156, 169, 160, 81, 11, 245, 22, 235, 122, 117, 44, 215, 79, 174, 213, 233, 230, 231, 173, 232, 116, 214, 244, 234, 168, 80, 88, 175 ] }); var Galois_1 = Galois; var Version = lite.extend(null, { /** * The version pattern block. * * @public * @static * @type {number[]} * @memberof Version */ BLOCK: [ 3220, 1468, 2713, 1235, 3062, 1890, 2119, 1549, 2344, 2936, 1117, 2583, 1330, 2470, 1667, 2249, 2028, 3780, 481, 4011, 142, 3098, 831, 3445, 592, 2517, 1776, 2234, 1951, 2827, 1070, 2660, 1345, 3177 ] }); var Version_1 = Version; var Frame = lite.extend(function(options) { var dataBlock, eccBlock, index2, neccBlock1, neccBlock2; var valueLength = options.value.length; this._badness = []; this._level = ErrorCorrection_1.LEVELS[options.level]; this._polynomial = []; this._value = options.value; this._version = 0; this._stringBuffer = []; while (this._version < 40) { this._version++; index2 = (this._level - 1) * 4 + (this._version - 1) * 16; neccBlock1 = ErrorCorrection_1.BLOCKS[index2++]; neccBlock2 = ErrorCorrection_1.BLOCKS[index2++]; dataBlock = ErrorCorrection_1.BLOCKS[index2++]; eccBlock = ErrorCorrection_1.BLOCKS[index2]; index2 = dataBlock * (neccBlock1 + neccBlock2) + neccBlock2 - 3 + (this._version <= 9); if (valueLength <= index2) { break; } } this._dataBlock = dataBlock; this._eccBlock = eccBlock; this._neccBlock1 = neccBlock1; this._neccBlock2 = neccBlock2; var width = this.width = 17 + 4 * this._version; this.buffer = Frame._createArray(width * width); this._ecc = Frame._createArray(dataBlock + (dataBlock + eccBlock) * (neccBlock1 + neccBlock2) + neccBlock2); this._mask = Frame._createArray((width * (width + 1) + 1) / 2); this._insertFinders(); this._insertAlignments(); this.buffer[8 + width * (width - 8)] = 1; this._insertTimingGap(); this._reverseMask(); this._insertTimingRowAndColumn(); this._insertVersion(); this._syncMask(); this._convertBitStream(valueLength); this._calculatePolynomial(); this._appendEccToData(); this._interleaveBlocks(); this._pack(); this._finish(); }, { _addAlignment: function(x, y) { var i; var buffer = this.buffer; var width = this.width; buffer[x + width * y] = 1; for (i = -2; i < 2; i++) { buffer[x + i + width * (y - 2)] = 1; buffer[x - 2 + width * (y + i + 1)] = 1; buffer[x + 2 + width * (y + i)] = 1; buffer[x + i + 1 + width * (y + 2)] = 1; } for (i = 0; i < 2; i++) { this._setMask(x - 1, y + i); this._setMask(x + 1, y - i); this._setMask(x - i, y - 1); this._setMask(x + i, y + 1); } }, _appendData: function(data, dataLength, ecc, eccLength) { var bit, i, j; var polynomial = this._polynomial; var stringBuffer = this._stringBuffer; for (i = 0; i < eccLength; i++) { stringBuffer[ecc + i] = 0; } for (i = 0; i < dataLength; i++) { bit = Galois_1.LOG[stringBuffer[data + i] ^ stringBuffer[ecc]]; if (bit !== 255) { for (j = 1; j < eccLength; j++) { stringBuffer[ecc + j - 1] = stringBuffer[ecc + j] ^ Galois_1.EXPONENT[Frame._modN(bit + polynomial[eccLength - j])]; } } else { for (j = ecc; j < ecc + eccLength; j++) { stringBuffer[j] = stringBuffer[j + 1]; } } stringBuffer[ecc + eccLength - 1] = bit === 255 ? 0 : Galois_1.EXPONENT[Frame._modN(bit + polynomial[0])]; } }, _appendEccToData: function() { var i; var data = 0; var dataBlock = this._dataBlock; var ecc = this._calculateMaxLength(); var eccBlock = this._eccBlock; for (i = 0; i < this._neccBlock1; i++) { this._appendData(data, dataBlock, ecc, eccBlock); data += dataBlock; ecc += eccBlock; } for (i = 0; i < this._neccBlock2; i++) { this._appendData(data, dataBlock + 1, ecc, eccBlock); data += dataBlock + 1; ecc += eccBlock; } }, _applyMask: function(mask) { var r3x, r3y, x, y; var buffer = this.buffer; var width = this.width; switch (mask) { case 0: for (y = 0; y < width; y++) { for (x = 0; x < width; x++) { if (!(x + y & 1) && !this._isMasked(x, y)) { buffer[x + y * width] ^= 1; } } } break; case 1: for (y = 0; y < width; y++) { for (x = 0; x < width; x++) { if (!(y & 1) && !this._isMasked(x, y)) { buffer[x + y * width] ^= 1; } } } break; case 2: for (y = 0; y < width; y++) { for (r3x = 0, x = 0; x < width; x++, r3x++) { if (r3x === 3) { r3x = 0; } if (!r3x && !this._isMasked(x, y)) { buffer[x + y * width] ^= 1; } } } break; case 3: for (r3y = 0, y = 0; y < width; y++, r3y++) { if (r3y === 3) { r3y = 0; } for (r3x = r3y, x = 0; x < width; x++, r3x++) { if (r3x === 3) { r3x = 0; } if (!r3x && !this._isMasked(x, y)) { buffer[x + y * width] ^= 1; } } } break; case 4: for (y = 0; y < width; y++) { for (r3x = 0, r3y = y >> 1 & 1, x = 0; x < width; x++, r3x++) { if (r3x === 3) { r3x = 0; r3y = !r3y; } if (!r3y && !this._isMasked(x, y)) { buffer[x + y * width] ^= 1; } } } break; case 5: for (r3y = 0, y = 0; y < width; y++, r3y++) { if (r3y === 3) { r3y = 0; } for (r3x = 0, x = 0; x < width; x++, r3x++) { if (r3x === 3) { r3x = 0; } if (!((x & y & 1) + !(!r3x | !r3y)) && !this._isMasked(x, y)) { buffer[x + y * width] ^= 1; } } } break; case 6: for (r3y = 0, y = 0; y < width; y++, r3y++) { if (r3y === 3) { r3y = 0; } for (r3x = 0, x = 0; x < width; x++, r3x++) { if (r3x === 3) { r3x = 0; } if (!((x & y & 1) + (r3x && r3x === r3y) & 1) && !this._isMasked(x, y)) { buffer[x + y * width] ^= 1; } } } break; case 7: for (r3y = 0, y = 0; y < width; y++, r3y++) { if (r3y === 3) { r3y = 0; } for (r3x = 0, x = 0; x < width; x++, r3x++) { if (r3x === 3) { r3x = 0; } if (!((r3x && r3x === r3y) + (x + y & 1) & 1) && !this._isMasked(x, y)) { buffer[x + y * width] ^= 1; } } } break; } }, _calculateMaxLength: function() { return this._dataBlock * (this._neccBlock1 + this._neccBlock2) + this._neccBlock2; }, _calculatePolynomial: function() { var i, j; var eccBlock = this._eccBlock; var polynomial = this._polynomial; polynomial[0] = 1; for (i = 0; i < eccBlock; i++) { polynomial[i + 1] = 1; for (j = i; j > 0; j--) { polynomial[j] = polynomial[j] ? polynomial[j - 1] ^ Galois_1.EXPONENT[Frame._modN(Galois_1.LOG[polynomial[j]] + i)] : polynomial[j - 1]; } polynomial[0] = Galois_1.EXPONENT[Frame._modN(Galois_1.LOG[polynomial[0]] + i)]; } for (i = 0; i <= eccBlock; i++) { polynomial[i] = Galois_1.LOG[polynomial[i]]; } }, _checkBadness: function() { var b, b1, h, x, y; var bad = 0; var badness = this._badness; var buffer = this.buffer; var width = this.width; for (y = 0; y < width - 1; y++) { for (x = 0; x < width - 1; x++) { if (buffer[x + width * y] && buffer[x + 1 + width * y] && buffer[x + width * (y + 1)] && buffer[x + 1 + width * (y + 1)] || // All background colour. !(buffer[x + width * y] || buffer[x + 1 + width * y] || buffer[x + width * (y + 1)] || buffer[x + 1 + width * (y + 1)])) { bad += Frame.N2; } } } var bw = 0; for (y = 0; y < width; y++) { h = 0; badness[0] = 0; for (b = 0, x = 0; x < width; x++) { b1 = buffer[x + width * y]; if (b === b1) { badness[h]++; } else { badness[++h] = 1; } b = b1; bw += b ? 1 : -1; } bad += this._getBadness(h); } if (bw < 0) { bw = -bw; } var count = 0; var big = bw; big += big << 2; big <<= 1; while (big > width * width) { big -= width * width; count++; } bad += count * Frame.N4; for (x = 0; x < width; x++) { h = 0; badness[0] = 0; for (b = 0, y = 0; y < width; y++) { b1 = buffer[x + width * y]; if (b === b1) { badness[h]++; } else { badness[++h] = 1; } b = b1; } bad += this._getBadness(h); } return bad; }, _convertBitStream: function(length) { var bit, i; var ecc = this._ecc; var version = this._version; for (i = 0; i < length; i++) { ecc[i] = this._value.charCodeAt(i); } var stringBuffer = this._stringBuffer = ecc.slice(); var maxLength = this._calculateMaxLength(); if (length >= maxLength - 2) { length = maxLength - 2; if (version > 9) { length--; } } var index2 = length; if (version > 9) { stringBuffer[index2 + 2] = 0; stringBuffer[index2 + 3] = 0; while (index2--) { bit = stringBuffer[index2]; stringBuffer[index2 + 3] |= 255 & bit << 4; stringBuffer[index2 + 2] = bit >> 4; } stringBuffer[2] |= 255 & length << 4; stringBuffer[1] = length >> 4; stringBuffer[0] = 64 | length >> 12; } else { stringBuffer[index2 + 1] = 0; stringBuffer[index2 + 2] = 0; while (index2--) { bit = stringBuffer[index2]; stringBuffer[index2 + 2] |= 255 & bit << 4; stringBuffer[index2 + 1] = bit >> 4; } stringBuffer[1] |= 255 & length << 4; stringBuffer[0] = 64 | length >> 4; } index2 = length + 3 - (version < 10); while (index2 < maxLength) { stringBuffer[index2++] = 236; stringBuffer[index2++] = 17; } }, _getBadness: function(length) { var i; var badRuns = 0; var badness = this._badness; for (i = 0; i <= length; i++) { if (badness[i] >= 5) { badRuns += Frame.N1 + badness[i] - 5; } } for (i = 3; i < length - 1; i += 2) { if (badness[i - 2] === badness[i + 2] && badness[i + 2] === badness[i - 1] && badness[i - 1] === badness[i + 1] && badness[i - 1] * 3 === badness[i] && // Background around the foreground pattern? Not part of the specs. (badness[i - 3] === 0 || i + 3 > length || badness[i - 3] * 3 >= badness[i] * 4 || badness[i + 3] * 3 >= badness[i] * 4)) { badRuns += Frame.N3; } } return badRuns; }, _finish: function() { this._stringBuffer = this.buffer.slice(); var currentMask, i; var bit = 0; var mask = 3e4; for (i = 0; i < 8; i++) { this._applyMask(i); currentMask = this._checkBadness(); if (currentMask < mask) { mask = currentMask; bit = i; } if (bit === 7) { break; } this.buffer = this._stringBuffer.slice(); } if (bit !== i) { this._applyMask(bit); } mask = ErrorCorrection_1.FINAL_FORMAT[bit + (this._level - 1 << 3)]; var buffer = this.buffer; var width = this.width; for (i = 0; i < 8; i++, mask >>= 1) { if (mask & 1) { buffer[width - 1 - i + width * 8] = 1; if (i < 6) { buffer[8 + width * i] = 1; } else { buffer[8 + width * (i + 1)] = 1; } } } for (i = 0; i < 7; i++, mask >>= 1) { if (mask & 1) { buffer[8 + width * (width - 7 + i)] = 1; if (i) { buffer[6 - i + width * 8] = 1; } else { buffer[7 + width * 8] = 1; } } } }, _interleaveBlocks: function() { var i, j; var dataBlock = this._dataBlock; var ecc = this._ecc; var eccBlock = this._eccBlock; var k = 0; var maxLength = this._calculateMaxLength(); var neccBlock1 = this._neccBlock1; var neccBlock2 = this._neccBlock2; var stringBuffer = this._stringBuffer; for (i = 0; i < dataBlock; i++) { for (j = 0; j < neccBlock1; j++) { ecc[k++] = stringBuffer[i + j * dataBlock]; } for (j = 0; j < neccBlock2; j++) { ecc[k++] = stringBuffer[neccBlock1 * dataBlock + i + j * (dataBlock + 1)]; } } for (j = 0; j < neccBlock2; j++) { ecc[k++] = stringBuffer[neccBlock1 * dataBlock + i + j * (dataBlock + 1)]; } for (i = 0; i < eccBlock; i++) { for (j = 0; j < neccBlock1 + neccBlock2; j++) { ecc[k++] = stringBuffer[maxLength + i + j * eccBlock]; } } this._stringBuffer = ecc; }, _insertAlignments: function() { var i, x, y; var version = this._version; var width = this.width; if (version > 1) { i = Alignment_1.BLOCK[version]; y = width - 7; for (; ; ) { x = width - 7; while (x > i - 3) { this._addAlignment(x, y); if (x < i) { break; } x -= i; } if (y <= i + 9) { break; } y -= i; this._addAlignment(6, y); this._addAlignment(y, 6); } } }, _insertFinders: function() { var i, j, x, y; var buffer = this.buffer; var width = this.width; for (i = 0; i < 3; i++) { j = 0; y = 0; if (i === 1) { j = width - 7; } if (i === 2) { y = width - 7; } buffer[y + 3 + width * (j + 3)] = 1; for (x = 0; x < 6; x++) { buffer[y + x + width * j] = 1; buffer[y + width * (j + x + 1)] = 1; buffer[y + 6 + width * (j + x)] = 1; buffer[y + x + 1 + width * (j + 6)] = 1; } for (x = 1; x < 5; x++) { this._setMask(y + x, j + 1); this._setMask(y + 1, j + x + 1); this._setMask(y + 5, j + x); this._setMask(y + x + 1, j + 5); } for (x = 2; x < 4; x++) { buffer[y + x + width * (j + 2)] = 1; buffer[y + 2 + width * (j + x + 1)] = 1; buffer[y + 4 + width * (j + x)] = 1; buffer[y + x + 1 + width * (j + 4)] = 1; } } }, _insertTimingGap: function() { var x, y; var width = this.width; for (y = 0; y < 7; y++) { this._setMask(7, y); this._setMask(width - 8, y); this._setMask(7, y + width - 7); } for (x = 0; x < 8; x++) { this._setMask(x, 7); this._setMask(x + width - 8, 7); this._setMask(x, width - 8); } }, _insertTimingRowAndColumn: function() { var x; var buffer = this.buffer; var width = this.width; for (x = 0; x < width - 14; x++) { if (x & 1) { this._setMask(8 + x, 6); this._setMask(6, 8 + x); } else { buffer[8 + x + width * 6] = 1; buffer[6 + width * (8 + x)] = 1; } } }, _insertVersion: function() { var i, j, x, y; var buffer = this.buffer; var version = this._version; var width = this.width; if (version > 6) { i = Version_1.BLOCK[version - 7]; j = 17; for (x = 0; x < 6; x++) { for (y = 0; y < 3; y++, j--) { if (1 & (j > 11 ? version >> j - 12 : i >> j)) { buffer[5 - x + width * (2 - y + width - 11)] = 1; buffer[2 - y + width - 11 + width * (5 - x)] = 1; } else { this._setMask(5 - x, 2 - y + width - 11); this._setMask(2 - y + width - 11, 5 - x); } } } } }, _isMasked: function(x, y) { var bit = Frame._getMaskBit(x, y); return this._mask[bit] === 1; }, _pack: function() { var bit, i, j; var k = 1; var v = 1; var width = this.width; var x = width - 1; var y = width - 1; var length = (this._dataBlock + this._eccBlock) * (this._neccBlock1 + this._neccBlock2) + this._neccBlock2; for (i = 0; i < length; i++) { bit = this._stringBuffer[i]; for (j = 0; j < 8; j++, bit <<= 1) { if (128 & bit) { this.buffer[x + width * y] = 1; } do { if (v) { x--; } else { x++; if (k) { if (y !== 0) { y--; } else { x -= 2; k = !k; if (x === 6) { x--; y = 9; } } } else if (y !== width - 1) { y++; } else { x -= 2; k = !k; if (x === 6) { x--; y -= 8; } } } v = !v; } while (this._isMasked(x, y)); } } }, _reverseMask: function() { var x, y; var width = this.width; for (x = 0; x < 9; x++) { this._setMask(x, 8); } for (x = 0; x < 8; x++) { this._setMask(x + width - 8, 8); this._setMask(8, x); } for (y = 0; y < 7; y++) { this._setMask(8, y + width - 7); } }, _setMask: function(x, y) { var bit