UNPKG

@forward-software/reveal.js-qrcodes

Version:

Custom plugin for Reveal.js to show QR Codes by adding a single class to HTML elements

967 lines (950 loc) 42.3 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.RevealQRCodes = factory()); })(this, (function () { 'use strict'; var QRMode; (function (QRMode) { QRMode[QRMode["MODE_NUMBER"] = 1] = "MODE_NUMBER"; QRMode[QRMode["MODE_ALPHA_NUM"] = 2] = "MODE_ALPHA_NUM"; QRMode[QRMode["MODE_8BIT_BYTE"] = 4] = "MODE_8BIT_BYTE"; QRMode[QRMode["MODE_KANJI"] = 8] = "MODE_KANJI"; })(QRMode || (QRMode = {})); var QRMaskPattern; (function (QRMaskPattern) { QRMaskPattern[QRMaskPattern["PATTERN000"] = 0] = "PATTERN000"; QRMaskPattern[QRMaskPattern["PATTERN001"] = 1] = "PATTERN001"; QRMaskPattern[QRMaskPattern["PATTERN010"] = 2] = "PATTERN010"; QRMaskPattern[QRMaskPattern["PATTERN011"] = 3] = "PATTERN011"; QRMaskPattern[QRMaskPattern["PATTERN100"] = 4] = "PATTERN100"; QRMaskPattern[QRMaskPattern["PATTERN101"] = 5] = "PATTERN101"; QRMaskPattern[QRMaskPattern["PATTERN110"] = 6] = "PATTERN110"; QRMaskPattern[QRMaskPattern["PATTERN111"] = 7] = "PATTERN111"; })(QRMaskPattern || (QRMaskPattern = {})); const PATTERN_POSITION_TABLE = [[], [6, 18], [6, 22], [6, 26], [6, 30], [6, 34], [6, 22, 38], [6, 24, 42], [6, 26, 46], [6, 28, 50], [6, 30, 54], [6, 32, 58], [6, 34, 62], [6, 26, 46, 66], [6, 26, 48, 70], [6, 26, 50, 74], [6, 30, 54, 78], [6, 30, 56, 82], [6, 30, 58, 86], [6, 34, 62, 90], [6, 28, 50, 72, 94], [6, 26, 50, 74, 98], [6, 30, 54, 78, 102], [6, 28, 54, 80, 106], [6, 32, 58, 84, 110], [6, 30, 58, 86, 114], [6, 34, 62, 90, 118], [6, 26, 50, 74, 98, 122], [6, 30, 54, 78, 102, 126], [6, 26, 52, 78, 104, 130], [6, 30, 56, 82, 108, 134], [6, 34, 60, 86, 112, 138], [6, 30, 58, 86, 114, 142], [6, 34, 62, 90, 118, 146], [6, 30, 54, 78, 102, 126, 150], [6, 24, 50, 76, 102, 128, 154], [6, 28, 54, 80, 106, 132, 158], [6, 32, 58, 84, 110, 136, 162], [6, 26, 54, 82, 110, 138, 166], [6, 30, 58, 86, 114, 142, 170]]; const G15 = 1 << 10 | 1 << 8 | 1 << 5 | 1 << 4 | 1 << 2 | 1 << 1 | 1 << 0; const G18 = 1 << 12 | 1 << 11 | 1 << 10 | 1 << 9 | 1 << 8 | 1 << 5 | 1 << 2 | 1 << 0; const G15_MASK = 1 << 14 | 1 << 12 | 1 << 10 | 1 << 4 | 1 << 1; function galoisFieldLog(n) { var _a; if (typeof n !== "number" || n < 1) { console.warn(`galoisFieldLog(${n}): input is not a number or is less than 1`); return 0; } if (LOG_TABLE[n] === null || typeof LOG_TABLE[n] !== "number") { console.warn(`galoisFieldLog(${n}): value retrieved from LOG_TABLE (${LOG_TABLE[n]}) is null or not a number`); return 0; } return (_a = LOG_TABLE[n]) !== null && _a !== void 0 ? _a : 0; } function galoisFieldExp(n) { if (typeof n !== "number") { console.warn(`galoisFieldExp(${n}): input is not a number`); return 0; } while (n < 0) { n += 255; } while (n >= 256) { n -= 255; } return EXP_TABLE[n]; } const EXP_TABLE = [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, 1]; const LOG_TABLE = [null, 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]; class QRPolynomial { constructor(num, shift) { if (num.length === undefined) { throw new Error(num.length + "/" + shift); } let offset = 0; while (offset < num.length && num[offset] === 0) { offset++; } this.num = new Array(num.length - offset + shift); for (let i = 0; i < num.length - offset; i++) { this.num[i] = num[i + offset]; } } get(index) { return this.num[index]; } getLength() { return this.num.length; } multiply(e) { const num = new Array(this.getLength() + e.getLength() - 1); for (let i = 0; i < this.getLength(); i++) { for (let j = 0; j < e.getLength(); j++) { num[i + j] ^= galoisFieldExp(galoisFieldLog(this.get(i)) + galoisFieldLog(e.get(j))); } } return new QRPolynomial(num, 0); } mod(e) { if (this.getLength() - e.getLength() < 0) { return this; } const ratio = galoisFieldLog(this.get(0)) - galoisFieldLog(e.get(0)); const num = new Array(this.getLength()); for (let i = 0; i < this.getLength(); i++) { num[i] = this.get(i); } for (let i = 0; i < e.getLength(); i++) { num[i] ^= galoisFieldExp(galoisFieldLog(e.get(i)) + ratio); } return new QRPolynomial(num, 0).mod(e); } } function getBCHTypeInfo(data) { let d = data << 10; while (getBCHDigit(d) - getBCHDigit(G15) >= 0) { d ^= G15 << getBCHDigit(d) - getBCHDigit(G15); } return (data << 10 | d) ^ G15_MASK; } function getBCHTypeNumber(data) { let d = data << 12; while (getBCHDigit(d) - getBCHDigit(G18) >= 0) { d ^= G18 << getBCHDigit(d) - getBCHDigit(G18); } return data << 12 | d; } function getBCHDigit(data) { let digit = 0; while (data !== 0) { digit++; data >>>= 1; } return digit; } function getPatternPosition(typeNumber) { return PATTERN_POSITION_TABLE[typeNumber - 1]; } function getMask(maskPattern, i, j) { switch (maskPattern) { case QRMaskPattern.PATTERN000: return (i + j) % 2 === 0; case QRMaskPattern.PATTERN001: return i % 2 === 0; case QRMaskPattern.PATTERN010: return j % 3 === 0; case QRMaskPattern.PATTERN011: return (i + j) % 3 === 0; case QRMaskPattern.PATTERN100: return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 === 0; case QRMaskPattern.PATTERN101: return i * j % 2 + i * j % 3 === 0; case QRMaskPattern.PATTERN110: return (i * j % 2 + i * j % 3) % 2 === 0; case QRMaskPattern.PATTERN111: return (i * j % 3 + (i + j) % 2) % 2 === 0; default: throw new Error(`bad maskPattern: ${maskPattern}`); } } function getErrorCorrectPolynomial(errorCorrectLength) { let a = new QRPolynomial([1], 0); for (let i = 0; i < errorCorrectLength; i++) { a = a.multiply(new QRPolynomial([1, galoisFieldExp(i)], 0)); } return a; } function getLostPoint(qrCode) { const moduleCount = qrCode.getModuleCount(); let lostPoint = 0; for (let row = 0; row < moduleCount; row++) { for (let col = 0; col < moduleCount; col++) { let sameCount = 0; const dark = qrCode.isDark(row, col); for (let r = -1; r <= 1; r++) { if (row + r < 0 || moduleCount <= row + r) { continue; } for (let c = -1; c <= 1; c++) { if (col + c < 0 || moduleCount <= col + c) { continue; } if (r === 0 && c === 0) { continue; } if (dark === qrCode.isDark(row + r, col + c)) { sameCount++; } } } if (sameCount > 5) { lostPoint += 3 + sameCount - 5; } } } for (let row = 0; row < moduleCount - 1; row++) { for (let col = 0; col < moduleCount - 1; col++) { let count = 0; if (qrCode.isDark(row, col)) count++; if (qrCode.isDark(row + 1, col)) count++; if (qrCode.isDark(row, col + 1)) count++; if (qrCode.isDark(row + 1, col + 1)) count++; if (count === 0 || count === 4) { lostPoint += 3; } } } for (let row = 0; row < moduleCount; row++) { for (let col = 0; col < moduleCount - 6; col++) { if (qrCode.isDark(row, col) && !qrCode.isDark(row, col + 1) && qrCode.isDark(row, col + 2) && qrCode.isDark(row, col + 3) && qrCode.isDark(row, col + 4) && !qrCode.isDark(row, col + 5) && qrCode.isDark(row, col + 6)) { lostPoint += 40; } } } for (let col = 0; col < moduleCount; col++) { for (let row = 0; row < moduleCount - 6; row++) { if (qrCode.isDark(row, col) && !qrCode.isDark(row + 1, col) && qrCode.isDark(row + 2, col) && qrCode.isDark(row + 3, col) && qrCode.isDark(row + 4, col) && !qrCode.isDark(row + 5, col) && qrCode.isDark(row + 6, col)) { lostPoint += 40; } } } let darkCount = 0; for (let col = 0; col < moduleCount; col++) { for (let row = 0; row < moduleCount; row++) { if (qrCode.isDark(row, col)) { darkCount++; } } } const ratio = Math.abs(100 * darkCount / moduleCount / moduleCount - 50) / 5; lostPoint += ratio * 10; return lostPoint; } function encodeUTF8(str) { const bytes = []; for (let i = 0; i < str.length; i++) { const code = str.charCodeAt(i); const byteArray = getUTF8Bytes(code); bytes.push(...byteArray); } if (bytes.length !== str.length) { bytes.unshift(0xEF, 0xBB, 0xBF); } return bytes; } function getUTF8Bytes(code) { if (code > 0x10000) { return [0xF0 | (code & 0x1C0000) >>> 18, 0x80 | (code & 0x3F000) >>> 12, 0x80 | (code & 0xFC0) >>> 6, 0x80 | code & 0x3F]; } if (code > 0x800) { return [0xE0 | (code & 0xF000) >>> 12, 0x80 | (code & 0xFC0) >>> 6, 0x80 | code & 0x3F]; } if (code > 0x80) { return [0xC0 | (code & 0x7C0) >>> 6, 0x80 | code & 0x3F]; } return [code]; } function createQR8bitByte(data) { const encodedData = encodeUTF8(data); return { data: encodedData, length: encodedData.length, mode: QRMode.MODE_8BIT_BYTE }; } function getLengthInBits(qr8bitByte, type) { if (type > 40) { throw new Error(`type: ${type}`); } if (1 <= type && type < 10) { switch (qr8bitByte.mode) { case QRMode.MODE_NUMBER: return 10; case QRMode.MODE_ALPHA_NUM: return 9; case QRMode.MODE_8BIT_BYTE: return 8; case QRMode.MODE_KANJI: return 8; default: throw new Error(`mode: ${qr8bitByte.mode}`); } } if (type < 27) { switch (qr8bitByte.mode) { case QRMode.MODE_NUMBER: return 12; case QRMode.MODE_ALPHA_NUM: return 11; case QRMode.MODE_8BIT_BYTE: return 16; case QRMode.MODE_KANJI: return 10; default: throw new Error(`mode: ${qr8bitByte.mode}`); } } switch (qr8bitByte.mode) { case QRMode.MODE_NUMBER: return 14; case QRMode.MODE_ALPHA_NUM: return 13; case QRMode.MODE_8BIT_BYTE: return 16; case QRMode.MODE_KANJI: return 12; default: throw new Error(`mode: ${qr8bitByte.mode}`); } } function createEmptyQRBitBuffer() { return { data: [], length: 0 }; } function createQRBitBuffer(typeNumber, dataList) { const buffer = createEmptyQRBitBuffer(); return dataList.reduce((prevBuffer, data) => { let newBuffer = putDataInto(prevBuffer, data.mode, 4); newBuffer = putDataInto(newBuffer, data.length, getLengthInBits(data, typeNumber)); return writeQR8bitByteTo(newBuffer, data); }, buffer); } function cloneQRBitBuffer(originalBuffer) { return { data: [...originalBuffer.data], length: originalBuffer.length }; } function putDataInto(buffer, num, length) { if (num < 0 || num >= 1 << length) { throw new Error(`Number ${num} is too large for ${length} bits`); } let newBuffer = Object.assign({}, buffer); for (let i = 0; i < length; i++) { newBuffer = appendBitTo(newBuffer, (num >>> length - i - 1 & 1) === 1); } return newBuffer; } function appendBitTo(buffer, bit) { const newBuffer = Object.assign({}, buffer); const bufIndex = Math.floor(newBuffer.length / 8); if (newBuffer.data.length <= bufIndex) { newBuffer.data = [...newBuffer.data, 0]; } if (bit) { newBuffer.data[bufIndex] |= 0x80 >>> newBuffer.length % 8; } newBuffer.length++; return newBuffer; } function writeQR8bitByteTo(buffer, qr8bitByte) { return qr8bitByte.data.reduce((buff, byte) => putDataInto(buff, byte, 8), buffer); } function getQRBitBufferBit(buffer, index) { if (index < 0 || index >= buffer.length) { throw new Error("Index out of bounds"); } return buffer.data[index]; } const PAD0 = 0xec; const PAD1 = 0x11; function padQRBitBuffer(buffer, totalSize) { let newState = cloneQRBitBuffer(buffer); if (newState.length + 4 <= totalSize * 8) { newState = putDataInto(newState, 0, 4); } while (newState.length % 8 !== 0) { newState = appendBitTo(newState, false); } while (newState.length < totalSize * 8) { newState = putDataInto(newState, PAD0, 8); if (newState.length >= totalSize * 8) break; newState = putDataInto(newState, PAD1, 8); } return newState; } function validateQRBitBufferSize(buffer, totalSize) { return buffer.length <= totalSize * 8; } function calculateRSBlocksTotalDataCount(rsBlocks) { return rsBlocks.reduce((sum, block) => sum + block.dataCount, 0); } function getRSBlocks(type, errorCorrectLevel) { const rsBlock = getRSBlockTable(type, errorCorrectLevel); if (!rsBlock) { throw new Error("bad RS block @ type:" + type + "/errorCorrectLevel:" + errorCorrectLevel); } const length = rsBlock.length / 3; const rsBlockList = []; for (let i = 0; i < length; i++) { const count = rsBlock[i * 3 + 0]; const totalCount = rsBlock[i * 3 + 1]; const dataCount = rsBlock[i * 3 + 2]; for (let j = 0; j < count; j++) { rsBlockList.push({ dataCount, totalCount }); } } return rsBlockList; } function getRSBlockTable(type, errorCorrectLevel) { switch (errorCorrectLevel) { case 1: return RS_BLOCK_TABLE[(type - 1) * 4 + 0]; case 0: return RS_BLOCK_TABLE[(type - 1) * 4 + 1]; case 3: return RS_BLOCK_TABLE[(type - 1) * 4 + 2]; case 2: return RS_BLOCK_TABLE[(type - 1) * 4 + 3]; default: return undefined; } } const RS_BLOCK_TABLE = [[1, 26, 19], [1, 26, 16], [1, 26, 13], [1, 26, 9], [1, 44, 34], [1, 44, 28], [1, 44, 22], [1, 44, 16], [1, 70, 55], [1, 70, 44], [2, 35, 17], [2, 35, 13], [1, 100, 80], [2, 50, 32], [2, 50, 24], [4, 25, 9], [1, 134, 108], [2, 67, 43], [2, 33, 15, 2, 34, 16], [2, 33, 11, 2, 34, 12], [2, 86, 68], [4, 43, 27], [4, 43, 19], [4, 43, 15], [2, 98, 78], [4, 49, 31], [2, 32, 14, 4, 33, 15], [4, 39, 13, 1, 40, 14], [2, 121, 97], [2, 60, 38, 2, 61, 39], [4, 40, 18, 2, 41, 19], [4, 40, 14, 2, 41, 15], [2, 146, 116], [3, 58, 36, 2, 59, 37], [4, 36, 16, 4, 37, 17], [4, 36, 12, 4, 37, 13], [2, 86, 68, 2, 87, 69], [4, 69, 43, 1, 70, 44], [6, 43, 19, 2, 44, 20], [6, 43, 15, 2, 44, 16], [4, 101, 81], [1, 80, 50, 4, 81, 51], [4, 50, 22, 4, 51, 23], [3, 36, 12, 8, 37, 13], [2, 116, 92, 2, 117, 93], [6, 58, 36, 2, 59, 37], [4, 46, 20, 6, 47, 21], [7, 42, 14, 4, 43, 15], [4, 133, 107], [8, 59, 37, 1, 60, 38], [8, 44, 20, 4, 45, 21], [12, 33, 11, 4, 34, 12], [3, 145, 115, 1, 146, 116], [4, 64, 40, 5, 65, 41], [11, 36, 16, 5, 37, 17], [11, 36, 12, 5, 37, 13], [5, 109, 87, 1, 110, 88], [5, 65, 41, 5, 66, 42], [5, 54, 24, 7, 55, 25], [11, 36, 12], [5, 122, 98, 1, 123, 99], [7, 73, 45, 3, 74, 46], [15, 43, 19, 2, 44, 20], [3, 45, 15, 13, 46, 16], [1, 135, 107, 5, 136, 108], [10, 74, 46, 1, 75, 47], [1, 50, 22, 15, 51, 23], [2, 42, 14, 17, 43, 15], [5, 150, 120, 1, 151, 121], [9, 69, 43, 4, 70, 44], [17, 50, 22, 1, 51, 23], [2, 42, 14, 19, 43, 15], [3, 141, 113, 4, 142, 114], [3, 70, 44, 11, 71, 45], [17, 47, 21, 4, 48, 22], [9, 39, 13, 16, 40, 14], [3, 135, 107, 5, 136, 108], [3, 67, 41, 13, 68, 42], [15, 54, 24, 5, 55, 25], [15, 43, 15, 10, 44, 16], [4, 144, 116, 4, 145, 117], [17, 68, 42], [17, 50, 22, 6, 51, 23], [19, 46, 16, 6, 47, 17], [2, 139, 111, 7, 140, 112], [17, 74, 46], [7, 54, 24, 16, 55, 25], [34, 37, 13], [4, 151, 121, 5, 152, 122], [4, 75, 47, 14, 76, 48], [11, 54, 24, 14, 55, 25], [16, 45, 15, 14, 46, 16], [6, 147, 117, 4, 148, 118], [6, 73, 45, 14, 74, 46], [11, 54, 24, 16, 55, 25], [30, 46, 16, 2, 47, 17], [8, 132, 106, 4, 133, 107], [8, 75, 47, 13, 76, 48], [7, 54, 24, 22, 55, 25], [22, 45, 15, 13, 46, 16], [10, 142, 114, 2, 143, 115], [19, 74, 46, 4, 75, 47], [28, 50, 22, 6, 51, 23], [33, 46, 16, 4, 47, 17], [8, 152, 122, 4, 153, 123], [22, 73, 45, 3, 74, 46], [8, 53, 23, 26, 54, 24], [12, 45, 15, 28, 46, 16], [3, 147, 117, 10, 148, 118], [3, 73, 45, 23, 74, 46], [4, 54, 24, 31, 55, 25], [11, 45, 15, 31, 46, 16], [7, 146, 116, 7, 147, 117], [21, 73, 45, 7, 74, 46], [1, 53, 23, 37, 54, 24], [19, 45, 15, 26, 46, 16], [5, 145, 115, 10, 146, 116], [19, 75, 47, 10, 76, 48], [15, 54, 24, 25, 55, 25], [23, 45, 15, 25, 46, 16], [13, 145, 115, 3, 146, 116], [2, 74, 46, 29, 75, 47], [42, 54, 24, 1, 55, 25], [23, 45, 15, 28, 46, 16], [17, 145, 115], [10, 74, 46, 23, 75, 47], [10, 54, 24, 35, 55, 25], [19, 45, 15, 35, 46, 16], [17, 145, 115, 1, 146, 116], [14, 74, 46, 21, 75, 47], [29, 54, 24, 19, 55, 25], [11, 45, 15, 46, 46, 16], [13, 145, 115, 6, 146, 116], [14, 74, 46, 23, 75, 47], [44, 54, 24, 7, 55, 25], [59, 46, 16, 1, 47, 17], [12, 151, 121, 7, 152, 122], [12, 75, 47, 26, 76, 48], [39, 54, 24, 14, 55, 25], [22, 45, 15, 41, 46, 16], [6, 151, 121, 14, 152, 122], [6, 75, 47, 34, 76, 48], [46, 54, 24, 10, 55, 25], [2, 45, 15, 64, 46, 16], [17, 152, 122, 4, 153, 123], [29, 74, 46, 14, 75, 47], [49, 54, 24, 10, 55, 25], [24, 45, 15, 46, 46, 16], [4, 152, 122, 18, 153, 123], [13, 74, 46, 32, 75, 47], [48, 54, 24, 14, 55, 25], [42, 45, 15, 32, 46, 16], [20, 147, 117, 4, 148, 118], [40, 75, 47, 7, 76, 48], [43, 54, 24, 22, 55, 25], [10, 45, 15, 67, 46, 16], [19, 148, 118, 6, 149, 119], [18, 75, 47, 31, 76, 48], [34, 54, 24, 34, 55, 25], [20, 45, 15, 61, 46, 16]]; class QRCodeModelImpl { constructor(typeNumber, errorCorrectLevel) { this.typeNumber = typeNumber; this.errorCorrectLevel = errorCorrectLevel; this.modules = []; this.moduleCount = 0; this.dataCache = undefined; this.dataList = []; } static createData(typeNumber, errorCorrectLevel, dataList) { const rsBlocks = getRSBlocks(typeNumber, errorCorrectLevel); const totalDataCount = calculateRSBlocksTotalDataCount(rsBlocks); let buffer = createQRBitBuffer(typeNumber, dataList); if (!validateQRBitBufferSize(buffer, totalDataCount)) { throw new Error(`code length overflow. (${buffer.length})`); } buffer = padQRBitBuffer(buffer, totalDataCount); return this.createBytes(buffer, rsBlocks); } static createBytes(buffer, rsBlocks) { const { dcdata, ecdata, maxDcCount, maxEcCount } = this.prepareDataBlocks(buffer, rsBlocks); return this.interleaveData(dcdata, ecdata, maxDcCount, maxEcCount, rsBlocks); } static prepareDataBlocks(buffer, rsBlocks) { let offset = 0; let maxDcCount = 0; let maxEcCount = 0; const dcdata = new Array(rsBlocks.length); const ecdata = new Array(rsBlocks.length); rsBlocks.forEach((block, r) => { const { dcCount, ecCount } = this.processBlock(block, buffer, offset, r, dcdata, ecdata); maxDcCount = Math.max(maxDcCount, dcCount); maxEcCount = Math.max(maxEcCount, ecCount); offset += dcCount; }); return { dcdata, ecdata, maxDcCount, maxEcCount }; } static processBlock(block, buffer, offset, r, dcdata, ecdata) { const dcCount = block.dataCount; const ecCount = block.totalCount - dcCount; dcdata[r] = this.extractData(buffer, offset, dcCount); ecdata[r] = this.generateErrorCorrection(dcdata[r], ecCount); return { dcCount, ecCount }; } static extractData(buffer, offset, count) { return Array.from({ length: count }, (_, i) => 0xff & getQRBitBufferBit(buffer, i + offset)); } static generateErrorCorrection(data, ecCount) { const rsPoly = getErrorCorrectPolynomial(ecCount); const modPoly = new QRPolynomial(data, rsPoly.getLength() - 1).mod(rsPoly); return Array.from({ length: rsPoly.getLength() - 1 }, (_, i) => { const modIndex = i + modPoly.getLength() - (rsPoly.getLength() - 1); return modIndex >= 0 ? modPoly.get(modIndex) : 0; }); } static interleaveData(dcdata, ecdata, maxDcCount, maxEcCount, rsBlocks) { const totalCodeCount = rsBlocks.reduce((sum, block) => sum + block.totalCount, 0); const data = new Array(totalCodeCount); let index = 0; for (let i = 0; i < maxDcCount; i++) { for (let r = 0; r < rsBlocks.length; r++) { if (i < dcdata[r].length) { data[index++] = dcdata[r][i]; } } } for (let i = 0; i < maxEcCount; i++) { for (let r = 0; r < rsBlocks.length; r++) { if (i < ecdata[r].length) { data[index++] = ecdata[r][i]; } } } return data; } addData(data) { this.dataList.push(createQR8bitByte(data)); this.dataCache = undefined; } isDark(row, col) { if (row < 0 || this.moduleCount <= row || col < 0 || this.moduleCount <= col) { throw new Error(`${row},${col}`); } return this.modules[row][col] || false; } getModuleCount() { return this.moduleCount; } make() { this.makeImpl(false, this.getBestMaskPattern()); } makeImpl(test, maskPattern) { this.initializeModules(); this.setupPatterns(test, maskPattern); this.mapData(this.getDataCache(), maskPattern); } initializeModules() { this.moduleCount = this.typeNumber * 4 + 17; this.modules = Array(this.moduleCount).fill(null).map(() => Array(this.moduleCount).fill(null)); } setupPatterns(test, maskPattern) { this.setupPositionProbePattern(0, 0); this.setupPositionProbePattern(this.moduleCount - 7, 0); this.setupPositionProbePattern(0, this.moduleCount - 7); this.setupPositionAdjustPattern(); this.setupTimingPattern(); this.setupTypeInfo(test, maskPattern); if (this.typeNumber >= 7) { this.setupTypeNumber(test); } } getDataCache() { if (!this.dataCache) { this.dataCache = QRCodeModelImpl.createData(this.typeNumber, this.errorCorrectLevel, this.dataList); } return this.dataCache; } setupPositionProbePattern(row, col) { for (let r = -1; r <= 7; r++) { if (row + r <= -1 || this.moduleCount <= row + r) continue; for (let c = -1; c <= 7; c++) { if (col + c <= -1 || this.moduleCount <= col + c) continue; const isPattern = 0 <= r && r <= 6 && (c === 0 || c === 6) || 0 <= c && c <= 6 && (r === 0 || r === 6) || 2 <= r && r <= 4 && 2 <= c && c <= 4; this.modules[row + r][col + c] = isPattern; } } } getBestMaskPattern() { let minLostPoint = 0; let bestPattern = 0; for (let i = 0; i < 8; i++) { this.makeImpl(true, i); const lostPoint = getLostPoint(this); if (i === 0 || minLostPoint > lostPoint) { minLostPoint = lostPoint; bestPattern = i; } } return bestPattern; } setupTimingPattern() { for (let r = 8; r < this.moduleCount - 8; r++) { if (this.modules[r][6] != null) continue; this.modules[r][6] = r % 2 === 0; } for (let c = 8; c < this.moduleCount - 8; c++) { if (this.modules[6][c] != null) continue; this.modules[6][c] = c % 2 === 0; } } setupPositionAdjustPattern() { const pos = getPatternPosition(this.typeNumber); pos.forEach(row => { pos.forEach(col => { if (this.modules[row][col] != null) return; for (let r = -2; r <= 2; r++) { for (let c = -2; c <= 2; c++) { this.modules[row + r][col + c] = r === -2 || r === 2 || c === -2 || c === 2 || r === 0 && c === 0; } } }); }); } setupTypeNumber(test) { const bits = getBCHTypeNumber(this.typeNumber); for (let i = 0; i < 18; i++) { const mod = !test && (bits >> i & 1) === 1; this.modules[Math.floor(i / 3)][i % 3 + this.moduleCount - 8 - 3] = mod; } for (let i = 0; i < 18; i++) { const mod = !test && (bits >> i & 1) === 1; this.modules[i % 3 + this.moduleCount - 8 - 3][Math.floor(i / 3)] = mod; } } setupTypeInfo(test, maskPattern) { const data = this.errorCorrectLevel << 3 | maskPattern; const bits = getBCHTypeInfo(data); for (let i = 0; i < 15; i++) { const mod = !test && (bits >> i & 1) === 1; if (i < 6) { this.modules[i][8] = mod; } else if (i < 8) { this.modules[i + 1][8] = mod; } else { this.modules[this.moduleCount - 15 + i][8] = mod; } } for (let i = 0; i < 15; i++) { const mod = !test && (bits >> i & 1) === 1; if (i < 8) { this.modules[8][this.moduleCount - i - 1] = mod; } else if (i < 9) { this.modules[8][15 - i - 1 + 1] = mod; } else { this.modules[8][15 - i - 1] = mod; } } this.modules[this.moduleCount - 8][8] = !test; } mapData(data, maskPattern) { let inc = -1; let row = this.moduleCount - 1; let bitIndex = 7; let byteIndex = 0; for (let col = this.moduleCount - 1; col > 0; col -= 2) { if (col === 6) col--; while (true) { for (let c = 0; c < 2; c++) { if (this.modules[row][col - c] == null) { const dark = this.getNextBit(data, byteIndex, bitIndex); const mask = getMask(maskPattern, row, col - c); this.modules[row][col - c] = mask ? !dark : dark; bitIndex--; if (bitIndex === -1) { byteIndex++; bitIndex = 7; } } } row += inc; if (row < 0 || this.moduleCount <= row) { row -= inc; inc = -inc; break; } } } } getNextBit(data, byteIndex, bitIndex) { return byteIndex < data.length && (data[byteIndex] >>> bitIndex & 1) === 1; } } function generateQRCodeModel(type, errorCorrection, content) { const qrCodeModel = new QRCodeModelImpl(type, getErrorCorrectionLevelNumber(errorCorrection)); qrCodeModel.addData(content); qrCodeModel.make(); return qrCodeModel; } function getErrorCorrectionLevelNumber(errorCorrectionLevel) { switch (errorCorrectionLevel) { case "L": return 1; case "M": return 0; case "Q": return 3; case "H": return 2; default: return 2; } } function getTypeNumber(text, errorCorrectionLevel) { const length = getUTF8Length(text); let nType = 1; for (let i = 0, len = QR_CODE_LIMIT_LENGTH.length; i <= len; i++) { let nLimit = 0; switch (errorCorrectionLevel) { case "L": nLimit = QR_CODE_LIMIT_LENGTH[i][0]; break; case "M": nLimit = QR_CODE_LIMIT_LENGTH[i][1]; break; case "Q": nLimit = QR_CODE_LIMIT_LENGTH[i][2]; break; case "H": nLimit = QR_CODE_LIMIT_LENGTH[i][3]; break; } if (length <= nLimit) { break; } else { nType++; } } if (nType > QR_CODE_LIMIT_LENGTH.length) { throw new Error("Too long data"); } return nType; } function getUTF8Length(sText) { let utf8Length = 0; for (let i = 0; i < sText.length; i++) { const charCode = sText.charCodeAt(i); if (charCode < 0x80) { utf8Length += 1; } else if (charCode < 0x800) { utf8Length += 2; } else if (charCode < 0x10000) { utf8Length += 3; } else if (charCode < 0x200000) { utf8Length += 4; } } return utf8Length; } const QR_CODE_LIMIT_LENGTH = [[17, 14, 11, 7], [32, 26, 20, 14], [53, 42, 32, 24], [78, 62, 46, 34], [106, 84, 60, 44], [134, 106, 74, 58], [154, 122, 86, 64], [192, 152, 108, 84], [230, 180, 130, 98], [271, 213, 151, 119], [321, 251, 177, 137], [367, 287, 203, 155], [425, 331, 241, 177], [458, 362, 258, 194], [520, 412, 292, 220], [586, 450, 322, 250], [644, 504, 364, 280], [718, 560, 394, 310], [792, 624, 442, 338], [858, 666, 482, 382], [929, 711, 509, 403], [1003, 779, 565, 439], [1091, 857, 611, 461], [1171, 911, 661, 511], [1273, 997, 715, 535], [1367, 1059, 751, 593], [1465, 1125, 805, 625], [1528, 1190, 868, 658], [1628, 1264, 908, 698], [1732, 1370, 982, 742], [1840, 1452, 1030, 790], [1952, 1538, 1112, 842], [2068, 1628, 1168, 898], [2188, 1722, 1228, 958], [2303, 1809, 1283, 983], [2431, 1911, 1351, 1051], [2563, 1989, 1423, 1093], [2699, 2099, 1499, 1139], [2809, 2213, 1579, 1219], [2953, 2331, 1663, 1273]]; const DEFAULT_QRCODE_OPTIONS = { size: 256, colorDark: "#000000", colorLight: "#ffffff", correctionLevel: "H" }; class QRCode { constructor(content, options) { this.content = content; this.options = Object.assign(Object.assign({}, DEFAULT_QRCODE_OPTIONS), options || {}); } static from(text) { return new QRCode(text); } withOptions(options) { this.options = Object.assign(Object.assign({}, DEFAULT_QRCODE_OPTIONS), options || {}); return this; } renderTo(renderer) { const typeNumber = this.options.type || getTypeNumber(this.content, this.options.correctionLevel); const qrCodeModel = generateQRCodeModel(typeNumber, this.options.correctionLevel, this.content); return renderer.draw(qrCodeModel, this.options); } clearFrom(renderer) { return renderer.clear(); } } function HTMLRenderer(element) { return isCanvasSupported() ? CanvasRenderer(element) : DOMRenderer(element); } function CanvasRenderer(element) { const IMAGE_B64 = "data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; let canvasElement = null; let canvasContext = null; let imageElement = null; let isDataURISupported = null; function _safeSetDataURI(successCallback, errorCallback) { if (isDataURISupported === null) { const el = document.createElement("img"); const fOnError = () => { isDataURISupported = false; }; const fOnSuccess = () => { isDataURISupported = true; if (successCallback) { successCallback(); } }; el.onabort = fOnError; el.onerror = fOnError; el.onload = fOnSuccess; el.src = IMAGE_B64; } else if (isDataURISupported === true && successCallback) { successCallback(); } else ; } function _onMakeImage() { if (!canvasElement || !imageElement) { console.warn("[HTMLRenderer][_onMakeImage] Canvas elements have not been initialized properly"); return; } imageElement.src = canvasElement.toDataURL("image/png"); imageElement.style.display = "block"; canvasElement.style.display = "none"; } function clearCanvas() { if (!canvasElement || !canvasContext) { console.warn("[HTMLRenderer][clearCanvas] Canvas context has not been initialized properly"); return false; } canvasContext.clearRect(0, 0, canvasElement.width, canvasElement.height); return true; } function drawWithCanvas(model, options) { var _a, _b, _c, _d; if (!canvasElement) { canvasElement = document.createElement("canvas"); canvasElement.width = (_a = options.size) !== null && _a !== void 0 ? _a : 0; canvasElement.height = (_b = options.size) !== null && _b !== void 0 ? _b : 0; element.appendChild(canvasElement); } if (!canvasContext) { canvasContext = canvasElement.getContext("2d"); } if (!imageElement) { imageElement = document.createElement("img"); imageElement.alt = "Scan me!"; imageElement.style.display = "none"; element.appendChild(imageElement); } const nCount = model.getModuleCount(); const nWidth = ((_c = options.size) !== null && _c !== void 0 ? _c : 0) / nCount; const nHeight = ((_d = options.size) !== null && _d !== void 0 ? _d : 0) / nCount; const nRoundedWidth = Math.round(nWidth); const nRoundedHeight = Math.round(nHeight); clearCanvas(); for (let row = 0; row < nCount; row++) { for (let col = 0; col < nCount; col++) { const bIsDark = model.isDark(row, col); const nLeft = col * nWidth; const nTop = row * nHeight; canvasContext.strokeStyle = bIsDark ? options.colorDark : options.colorLight; canvasContext.lineWidth = 1; canvasContext.fillStyle = bIsDark ? options.colorDark : options.colorLight; canvasContext.fillRect(nLeft, nTop, nWidth, nHeight); canvasContext.strokeRect(Math.floor(nLeft) + 0.5, Math.floor(nTop) + 0.5, nRoundedWidth, nRoundedHeight); canvasContext.strokeRect(Math.ceil(nLeft) - 0.5, Math.ceil(nTop) - 0.5, nRoundedWidth, nRoundedHeight); } } const [isAndroid, androidVersion] = getAndroidPlatformDetails(); if (!isAndroid || androidVersion >= 3) { _safeSetDataURI(_onMakeImage); } return true; } return { draw: (model, options) => { return drawWithCanvas(model, options); }, clear() { return clearCanvas(); } }; } function DOMRenderer(element) { function drawWithDOMElements(model, options) { const nCount = model.getModuleCount(); const nWidth = Math.floor(options.size / nCount); const nHeight = Math.floor(options.size / nCount); const aHTML = ['<table style="border:0;border-collapse:collapse;">']; for (let row = 0; row < nCount; row++) { aHTML.push("<tr>"); for (let col = 0; col < nCount; col++) { aHTML.push('<td style="border:0;border-collapse:collapse;padding:0;margin:0;width:' + nWidth + "px;height:" + nHeight + "px;background-color:" + (model.isDark(row, col) ? options.colorDark : options.colorLight) + ';"></td>'); } aHTML.push("</tr>"); } aHTML.push("</table>"); element.innerHTML = aHTML.join(""); const elTable = element.childNodes[0]; const nLeftMarginTable = (options.size - elTable.offsetWidth) / 2; const nTopMarginTable = (options.size - elTable.offsetHeight) / 2; if (nLeftMarginTable > 0 && nTopMarginTable > 0) { elTable.style.margin = nTopMarginTable + "px " + nLeftMarginTable + "px"; } return true; } function clearDOMElements() { element.innerHTML = ""; return true; } return { draw: drawWithDOMElements, clear: clearDOMElements }; } function isCanvasSupported() { return typeof CanvasRenderingContext2D != "undefined"; } function getAndroidPlatformDetails() { const userAgent = navigator.userAgent; if (/android/i.test(userAgent)) { const aMat = userAgent.toString().match(/android ([0-9]\.[0-9])/i); if (aMat && aMat[1]) { const version = parseFloat(aMat[1]); return [!!version, version]; } return [true, 0]; } return [false, -1]; } var QRErrorCorrectLevel; (function (QRErrorCorrectLevel) { QRErrorCorrectLevel[QRErrorCorrectLevel["L"] = 1] = "L"; QRErrorCorrectLevel[QRErrorCorrectLevel["M"] = 0] = "M"; QRErrorCorrectLevel[QRErrorCorrectLevel["Q"] = 3] = "Q"; QRErrorCorrectLevel[QRErrorCorrectLevel["H"] = 2] = "H"; })(QRErrorCorrectLevel || (QRErrorCorrectLevel = {})); ({ correctLevel: QRErrorCorrectLevel.H }); const DEFAULT_CONFIGURATION = { selector: ".qrcode", size: 256, colorDark: "#000000", colorLight: "#ffffff", correctionLevel: "H", configurations: {} }; const Plugin = () => { return { id: "qrcodes", init: function (deck) { const deckConfig = deck.getConfig(); const config = Object.assign({}, DEFAULT_CONFIGURATION, deckConfig.qrcodes); console.info("🔳 QR Codes plugin by ForWarD Software loaded", config); function renderQRCodes() { document.querySelectorAll(config.selector).forEach(function (el) { console.info("🔳 QR Code found as '%s' element:", el.tagName, el); let elementConfiguration = { text: el.dataset.text || el.href, size: el.dataset.size || config.size, colorDark: el.dataset.colorDark || config.colorDark, colorLight: el.dataset.colorLight || config.colorLight, correctionLevel: el.dataset.correctionLevel || config.correctionLevel }; if (config.configurations[el.id]) { elementConfiguration = { ...elementConfiguration, ...config.configurations[el.id] }; } console.debug("🔳 QR Code generating using configuration:", elementConfiguration); QRCode.from(elementConfiguration.text).withOptions({ size: elementConfiguration.size, colorDark: elementConfiguration.colorDark, colorLight: elementConfiguration.colorLight, correctionLevel: elementConfiguration.correctionLevel }).renderTo(HTMLRenderer(el)); }); } window.addEventListener("DOMContentLoaded", renderQRCodes); } }; }; return Plugin; }));