The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | /* oxlint-disable eslint/no-useless-escape -- vendored Project Nayuki code, kept as upstream wrote it. */ |
| 2 | /* |
| 3 | * QR Code generator library (TypeScript) |
| 4 | * |
| 5 | * Copyright (c) Project Nayuki. (MIT License) |
| 6 | * https://www.nayuki.io/page/qr-code-generator-library |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 9 | * this software and associated documentation files (the "Software"), to deal in |
| 10 | * the Software without restriction, including without limitation the rights to |
| 11 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 12 | * the Software, and to permit persons to whom the Software is furnished to do so, |
| 13 | * subject to the following conditions: |
| 14 | * - The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * - The Software is provided "as is", without warranty of any kind, express or |
| 17 | * implied, including but not limited to the warranties of merchantability, |
| 18 | * fitness for a particular purpose and noninfringement. In no event shall the |
| 19 | * authors or copyright holders be liable for any claim, damages or other |
| 20 | * liability, whether in an action of contract, tort or otherwise, arising from, |
| 21 | * out of or in connection with the Software or the use or other dealings in the |
| 22 | * Software. |
| 23 | */ |
| 24 | |
| 25 | "use strict"; |
| 26 | |
| 27 | type bit = number; |
| 28 | type byte = number; |
| 29 | type int = number; |
| 30 | |
| 31 | export class QrCode { |
| 32 | public static Ecc: typeof QrCodeEcc; |
| 33 | |
| 34 | public static encodeText(text: string, ecl: QrCodeEcc): QrCode { |
| 35 | const segs: Array<QrSegment> = QrSegment.makeSegments(text); |
| 36 | return QrCode.encodeSegments(segs, ecl); |
| 37 | } |
| 38 | |
| 39 | public static encodeBinary(data: Readonly<Array<byte>>, ecl: QrCodeEcc): QrCode { |
| 40 | const seg: QrSegment = QrSegment.makeBytes(data); |
| 41 | return QrCode.encodeSegments([seg], ecl); |
| 42 | } |
| 43 | |
| 44 | public static encodeSegments( |
| 45 | segs: Readonly<Array<QrSegment>>, |
| 46 | ecl: QrCodeEcc, |
| 47 | minVersion: int = 1, |
| 48 | maxVersion: int = 40, |
| 49 | mask: int = -1, |
| 50 | boostEcl: boolean = true, |
| 51 | ): QrCode { |
| 52 | if ( |
| 53 | !( |
| 54 | QrCode.MIN_VERSION <= minVersion && |
| 55 | minVersion <= maxVersion && |
| 56 | maxVersion <= QrCode.MAX_VERSION |
| 57 | ) || |
| 58 | mask < -1 || |
| 59 | mask > 7 |
| 60 | ) |
| 61 | throw new RangeError("Invalid value"); |
| 62 | |
| 63 | let version: int; |
| 64 | let dataUsedBits: int; |
| 65 | for (version = minVersion; ; version++) { |
| 66 | const dataCapacityBits: int = QrCode.getNumDataCodewords(version, ecl) * 8; |
| 67 | const usedBits: number = QrSegment.getTotalBits(segs, version); |
| 68 | if (usedBits <= dataCapacityBits) { |
| 69 | dataUsedBits = usedBits; |
| 70 | break; |
| 71 | } |
| 72 | if (version >= maxVersion) throw new RangeError("Data too long"); |
| 73 | } |
| 74 | |
| 75 | for (const newEcl of [QrCode.Ecc.MEDIUM, QrCode.Ecc.QUARTILE, QrCode.Ecc.HIGH]) { |
| 76 | if (boostEcl && dataUsedBits <= QrCode.getNumDataCodewords(version, newEcl) * 8) ecl = newEcl; |
| 77 | } |
| 78 | |
| 79 | let bb: Array<bit> = []; |
| 80 | for (const seg of segs) { |
| 81 | appendBits(seg.mode.modeBits, 4, bb); |
| 82 | appendBits(seg.numChars, seg.mode.numCharCountBits(version), bb); |
| 83 | for (const b of seg.getData()) bb.push(b); |
| 84 | } |
| 85 | assert(bb.length == dataUsedBits); |
| 86 | |
| 87 | const dataCapacityBits: int = QrCode.getNumDataCodewords(version, ecl) * 8; |
| 88 | assert(bb.length <= dataCapacityBits); |
| 89 | appendBits(0, Math.min(4, dataCapacityBits - bb.length), bb); |
| 90 | appendBits(0, (8 - (bb.length % 8)) % 8, bb); |
| 91 | assert(bb.length % 8 == 0); |
| 92 | |
| 93 | for (let padByte = 0xec; bb.length < dataCapacityBits; padByte ^= 0xec ^ 0x11) |
| 94 | appendBits(padByte, 8, bb); |
| 95 | |
| 96 | let dataCodewords: Array<byte> = []; |
| 97 | while (dataCodewords.length * 8 < bb.length) dataCodewords.push(0); |
| 98 | bb.forEach((b: bit, i: int) => (dataCodewords[i >>> 3]! |= b << (7 - (i & 7)))); |
| 99 | |
| 100 | return new QrCode(version, ecl, dataCodewords, mask); |
| 101 | } |
| 102 | |
| 103 | public readonly version: int; |
| 104 | public readonly errorCorrectionLevel: QrCodeEcc; |
| 105 | public readonly size: int; |
| 106 | |
| 107 | public readonly mask: int; |
| 108 | |
| 109 | private readonly modules: Array<Array<boolean>> = []; |
| 110 | |
| 111 | private readonly isFunction: Array<Array<boolean>> = []; |
| 112 | |
| 113 | public constructor( |
| 114 | version: int, |
| 115 | |
| 116 | errorCorrectionLevel: QrCodeEcc, |
| 117 | |
| 118 | dataCodewords: Readonly<Array<byte>>, |
| 119 | |
| 120 | msk: int, |
| 121 | ) { |
| 122 | this.version = version; |
| 123 | this.errorCorrectionLevel = errorCorrectionLevel; |
| 124 | |
| 125 | if (version < QrCode.MIN_VERSION || version > QrCode.MAX_VERSION) |
| 126 | throw new RangeError("Version value out of range"); |
| 127 | if (msk < -1 || msk > 7) throw new RangeError("Mask value out of range"); |
| 128 | this.size = version * 4 + 17; |
| 129 | |
| 130 | let row: Array<boolean> = []; |
| 131 | for (let i = 0; i < this.size; i++) row.push(false); |
| 132 | for (let i = 0; i < this.size; i++) { |
| 133 | this.modules.push(row.slice()); |
| 134 | this.isFunction.push(row.slice()); |
| 135 | } |
| 136 | |
| 137 | this.drawFunctionPatterns(); |
| 138 | const allCodewords: Array<byte> = this.addEccAndInterleave(dataCodewords); |
| 139 | this.drawCodewords(allCodewords); |
| 140 | |
| 141 | if (msk == -1) { |
| 142 | let minPenalty: int = 1000000000; |
| 143 | for (let i = 0; i < 8; i++) { |
| 144 | this.applyMask(i); |
| 145 | this.drawFormatBits(i); |
| 146 | const penalty: int = this.getPenaltyScore(); |
| 147 | if (penalty < minPenalty) { |
| 148 | msk = i; |
| 149 | minPenalty = penalty; |
| 150 | } |
| 151 | this.applyMask(i); |
| 152 | } |
| 153 | } |
| 154 | assert(0 <= msk && msk <= 7); |
| 155 | this.mask = msk; |
| 156 | this.applyMask(msk); |
| 157 | this.drawFormatBits(msk); |
| 158 | |
| 159 | this.isFunction = []; |
| 160 | } |
| 161 | |
| 162 | public getModule(x: int, y: int): boolean { |
| 163 | return 0 <= x && x < this.size && 0 <= y && y < this.size && this.modules[y]![x]!; |
| 164 | } |
| 165 | |
| 166 | private drawFunctionPatterns(): void { |
| 167 | for (let i = 0; i < this.size; i++) { |
| 168 | this.setFunctionModule(6, i, i % 2 == 0); |
| 169 | this.setFunctionModule(i, 6, i % 2 == 0); |
| 170 | } |
| 171 | |
| 172 | this.drawFinderPattern(3, 3); |
| 173 | this.drawFinderPattern(this.size - 4, 3); |
| 174 | this.drawFinderPattern(3, this.size - 4); |
| 175 | |
| 176 | const alignPatPos: Array<int> = this.getAlignmentPatternPositions(); |
| 177 | const numAlign: int = alignPatPos.length; |
| 178 | for (let i = 0; i < numAlign; i++) { |
| 179 | for (let j = 0; j < numAlign; j++) { |
| 180 | if (!((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0))) |
| 181 | this.drawAlignmentPattern(alignPatPos[i]!, alignPatPos[j]!); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | this.drawFormatBits(0); |
| 186 | this.drawVersion(); |
| 187 | } |
| 188 | |
| 189 | private drawFormatBits(mask: int): void { |
| 190 | const data: int = (this.errorCorrectionLevel.formatBits << 3) | mask; |
| 191 | let rem: int = data; |
| 192 | for (let i = 0; i < 10; i++) rem = (rem << 1) ^ ((rem >>> 9) * 0x537); |
| 193 | const bits = ((data << 10) | rem) ^ 0x5412; |
| 194 | assert(bits >>> 15 == 0); |
| 195 | |
| 196 | for (let i = 0; i <= 5; i++) this.setFunctionModule(8, i, getBit(bits, i)); |
| 197 | this.setFunctionModule(8, 7, getBit(bits, 6)); |
| 198 | this.setFunctionModule(8, 8, getBit(bits, 7)); |
| 199 | this.setFunctionModule(7, 8, getBit(bits, 8)); |
| 200 | for (let i = 9; i < 15; i++) this.setFunctionModule(14 - i, 8, getBit(bits, i)); |
| 201 | |
| 202 | for (let i = 0; i < 8; i++) this.setFunctionModule(this.size - 1 - i, 8, getBit(bits, i)); |
| 203 | for (let i = 8; i < 15; i++) this.setFunctionModule(8, this.size - 15 + i, getBit(bits, i)); |
| 204 | this.setFunctionModule(8, this.size - 8, true); |
| 205 | } |
| 206 | |
| 207 | private drawVersion(): void { |
| 208 | if (this.version < 7) return; |
| 209 | |
| 210 | let rem: int = this.version; |
| 211 | for (let i = 0; i < 12; i++) rem = (rem << 1) ^ ((rem >>> 11) * 0x1f25); |
| 212 | const bits: int = (this.version << 12) | rem; |
| 213 | assert(bits >>> 18 == 0); |
| 214 | |
| 215 | for (let i = 0; i < 18; i++) { |
| 216 | const color: boolean = getBit(bits, i); |
| 217 | const a: int = this.size - 11 + (i % 3); |
| 218 | const b: int = Math.floor(i / 3); |
| 219 | this.setFunctionModule(a, b, color); |
| 220 | this.setFunctionModule(b, a, color); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | private drawFinderPattern(x: int, y: int): void { |
| 225 | for (let dy = -4; dy <= 4; dy++) { |
| 226 | for (let dx = -4; dx <= 4; dx++) { |
| 227 | const dist: int = Math.max(Math.abs(dx), Math.abs(dy)); |
| 228 | const xx: int = x + dx; |
| 229 | const yy: int = y + dy; |
| 230 | if (0 <= xx && xx < this.size && 0 <= yy && yy < this.size) |
| 231 | this.setFunctionModule(xx, yy, dist != 2 && dist != 4); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | private drawAlignmentPattern(x: int, y: int): void { |
| 237 | for (let dy = -2; dy <= 2; dy++) { |
| 238 | for (let dx = -2; dx <= 2; dx++) |
| 239 | this.setFunctionModule(x + dx, y + dy, Math.max(Math.abs(dx), Math.abs(dy)) != 1); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | private setFunctionModule(x: int, y: int, isDark: boolean): void { |
| 244 | this.modules[y]![x] = isDark; |
| 245 | this.isFunction[y]![x] = true; |
| 246 | } |
| 247 | |
| 248 | private addEccAndInterleave(data: Readonly<Array<byte>>): Array<byte> { |
| 249 | const ver: int = this.version; |
| 250 | const ecl: QrCodeEcc = this.errorCorrectionLevel; |
| 251 | if (data.length != QrCode.getNumDataCodewords(ver, ecl)) |
| 252 | throw new RangeError("Invalid argument"); |
| 253 | |
| 254 | const numBlocks: int = QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal]![ver]!; |
| 255 | const blockEccLen: int = QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal]![ver]!; |
| 256 | const rawCodewords: int = Math.floor(QrCode.getNumRawDataModules(ver) / 8); |
| 257 | const numShortBlocks: int = numBlocks - (rawCodewords % numBlocks); |
| 258 | const shortBlockLen: int = Math.floor(rawCodewords / numBlocks); |
| 259 | |
| 260 | let blocks: Array<Array<byte>> = []; |
| 261 | const rsDiv: Array<byte> = QrCode.reedSolomonComputeDivisor(blockEccLen); |
| 262 | for (let i = 0, k = 0; i < numBlocks; i++) { |
| 263 | let dat: Array<byte> = data.slice( |
| 264 | k, |
| 265 | k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1), |
| 266 | ); |
| 267 | k += dat.length; |
| 268 | const ecc: Array<byte> = QrCode.reedSolomonComputeRemainder(dat, rsDiv); |
| 269 | if (i < numShortBlocks) dat.push(0); |
| 270 | blocks.push(dat.concat(ecc)); |
| 271 | } |
| 272 | |
| 273 | let result: Array<byte> = []; |
| 274 | for (let i = 0; i < blocks[0]!.length; i++) { |
| 275 | blocks.forEach((block, j) => { |
| 276 | if (i != shortBlockLen - blockEccLen || j >= numShortBlocks) result.push(block[i]!); |
| 277 | }); |
| 278 | } |
| 279 | assert(result.length == rawCodewords); |
| 280 | return result; |
| 281 | } |
| 282 | |
| 283 | private drawCodewords(data: Readonly<Array<byte>>): void { |
| 284 | if (data.length != Math.floor(QrCode.getNumRawDataModules(this.version) / 8)) |
| 285 | throw new RangeError("Invalid argument"); |
| 286 | let i: int = 0; |
| 287 | for (let right = this.size - 1; right >= 1; right -= 2) { |
| 288 | if (right == 6) right = 5; |
| 289 | for (let vert = 0; vert < this.size; vert++) { |
| 290 | for (let j = 0; j < 2; j++) { |
| 291 | const x: int = right - j; |
| 292 | const upward: boolean = ((right + 1) & 2) == 0; |
| 293 | const y: int = upward ? this.size - 1 - vert : vert; |
| 294 | if (!this.isFunction[y]![x]! && i < data.length * 8) { |
| 295 | this.modules[y]![x] = getBit(data[i >>> 3]!, 7 - (i & 7)); |
| 296 | i++; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | assert(i == data.length * 8); |
| 302 | } |
| 303 | |
| 304 | private applyMask(mask: int): void { |
| 305 | if (mask < 0 || mask > 7) throw new RangeError("Mask value out of range"); |
| 306 | for (let y = 0; y < this.size; y++) { |
| 307 | for (let x = 0; x < this.size; x++) { |
| 308 | let invert: boolean; |
| 309 | switch (mask) { |
| 310 | case 0: |
| 311 | invert = (x + y) % 2 == 0; |
| 312 | break; |
| 313 | case 1: |
| 314 | invert = y % 2 == 0; |
| 315 | break; |
| 316 | case 2: |
| 317 | invert = x % 3 == 0; |
| 318 | break; |
| 319 | case 3: |
| 320 | invert = (x + y) % 3 == 0; |
| 321 | break; |
| 322 | case 4: |
| 323 | invert = (Math.floor(x / 3) + Math.floor(y / 2)) % 2 == 0; |
| 324 | break; |
| 325 | case 5: |
| 326 | invert = ((x * y) % 2) + ((x * y) % 3) == 0; |
| 327 | break; |
| 328 | case 6: |
| 329 | invert = (((x * y) % 2) + ((x * y) % 3)) % 2 == 0; |
| 330 | break; |
| 331 | case 7: |
| 332 | invert = (((x + y) % 2) + ((x * y) % 3)) % 2 == 0; |
| 333 | break; |
| 334 | default: |
| 335 | throw new Error("Unreachable"); |
| 336 | } |
| 337 | if (!this.isFunction[y]![x]! && invert) this.modules[y]![x] = !this.modules[y]![x]!; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | private getPenaltyScore(): int { |
| 343 | let result: int = 0; |
| 344 | |
| 345 | for (let y = 0; y < this.size; y++) { |
| 346 | let runColor = false; |
| 347 | let runX = 0; |
| 348 | let runHistory = [0, 0, 0, 0, 0, 0, 0]; |
| 349 | for (let x = 0; x < this.size; x++) { |
| 350 | if (this.modules[y]![x] === runColor) { |
| 351 | runX++; |
| 352 | if (runX == 5) result += QrCode.PENALTY_N1; |
| 353 | else if (runX > 5) result++; |
| 354 | } else { |
| 355 | this.finderPenaltyAddHistory(runX, runHistory); |
| 356 | if (!runColor) result += this.finderPenaltyCountPatterns(runHistory) * QrCode.PENALTY_N3; |
| 357 | runColor = this.modules[y]![x]!; |
| 358 | runX = 1; |
| 359 | } |
| 360 | } |
| 361 | result += this.finderPenaltyTerminateAndCount(runColor, runX, runHistory) * QrCode.PENALTY_N3; |
| 362 | } |
| 363 | for (let x = 0; x < this.size; x++) { |
| 364 | let runColor = false; |
| 365 | let runY = 0; |
| 366 | let runHistory = [0, 0, 0, 0, 0, 0, 0]; |
| 367 | for (let y = 0; y < this.size; y++) { |
| 368 | if (this.modules[y]![x] === runColor) { |
| 369 | runY++; |
| 370 | if (runY == 5) result += QrCode.PENALTY_N1; |
| 371 | else if (runY > 5) result++; |
| 372 | } else { |
| 373 | this.finderPenaltyAddHistory(runY, runHistory); |
| 374 | if (!runColor) result += this.finderPenaltyCountPatterns(runHistory) * QrCode.PENALTY_N3; |
| 375 | runColor = this.modules[y]![x]!; |
| 376 | runY = 1; |
| 377 | } |
| 378 | } |
| 379 | result += this.finderPenaltyTerminateAndCount(runColor, runY, runHistory) * QrCode.PENALTY_N3; |
| 380 | } |
| 381 | |
| 382 | for (let y = 0; y < this.size - 1; y++) { |
| 383 | for (let x = 0; x < this.size - 1; x++) { |
| 384 | const color: boolean = this.modules[y]![x]!; |
| 385 | if ( |
| 386 | color == this.modules[y]![x + 1]! && |
| 387 | color == this.modules[y + 1]![x]! && |
| 388 | color == this.modules[y + 1]![x + 1]! |
| 389 | ) |
| 390 | result += QrCode.PENALTY_N2; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | let dark: int = 0; |
| 395 | for (const row of this.modules) dark = row.reduce((sum, color) => sum + (color ? 1 : 0), dark); |
| 396 | const total: int = this.size * this.size; |
| 397 | const k: int = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1; |
| 398 | assert(0 <= k && k <= 9); |
| 399 | result += k * QrCode.PENALTY_N4; |
| 400 | assert(0 <= result && result <= 2568888); |
| 401 | return result; |
| 402 | } |
| 403 | |
| 404 | private getAlignmentPatternPositions(): Array<int> { |
| 405 | if (this.version == 1) return []; |
| 406 | else { |
| 407 | const numAlign: int = Math.floor(this.version / 7) + 2; |
| 408 | const step: int = |
| 409 | this.version == 32 ? 26 : Math.ceil((this.version * 4 + 4) / (numAlign * 2 - 2)) * 2; |
| 410 | let result: Array<int> = [6]; |
| 411 | for (let pos = this.size - 7; result.length < numAlign; pos -= step) result.splice(1, 0, pos); |
| 412 | return result; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | private static getNumRawDataModules(ver: int): int { |
| 417 | if (ver < QrCode.MIN_VERSION || ver > QrCode.MAX_VERSION) |
| 418 | throw new RangeError("Version number out of range"); |
| 419 | let result: int = (16 * ver + 128) * ver + 64; |
| 420 | if (ver >= 2) { |
| 421 | const numAlign: int = Math.floor(ver / 7) + 2; |
| 422 | result -= (25 * numAlign - 10) * numAlign - 55; |
| 423 | if (ver >= 7) result -= 36; |
| 424 | } |
| 425 | assert(208 <= result && result <= 29648); |
| 426 | return result; |
| 427 | } |
| 428 | |
| 429 | private static getNumDataCodewords(ver: int, ecl: QrCodeEcc): int { |
| 430 | return ( |
| 431 | Math.floor(QrCode.getNumRawDataModules(ver) / 8) - |
| 432 | QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal]![ver]! * |
| 433 | QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal]![ver]! |
| 434 | ); |
| 435 | } |
| 436 | |
| 437 | private static reedSolomonComputeDivisor(degree: int): Array<byte> { |
| 438 | if (degree < 1 || degree > 255) throw new RangeError("Degree out of range"); |
| 439 | let result: Array<byte> = []; |
| 440 | for (let i = 0; i < degree - 1; i++) result.push(0); |
| 441 | result.push(1); |
| 442 | |
| 443 | let root = 1; |
| 444 | for (let i = 0; i < degree; i++) { |
| 445 | for (let j = 0; j < result.length; j++) { |
| 446 | result[j] = QrCode.reedSolomonMultiply(result[j]!, root); |
| 447 | if (j + 1 < result.length) result[j]! ^= result[j + 1]!; |
| 448 | } |
| 449 | root = QrCode.reedSolomonMultiply(root, 0x02); |
| 450 | } |
| 451 | return result; |
| 452 | } |
| 453 | |
| 454 | private static reedSolomonComputeRemainder( |
| 455 | data: Readonly<Array<byte>>, |
| 456 | divisor: Readonly<Array<byte>>, |
| 457 | ): Array<byte> { |
| 458 | let result: Array<byte> = divisor.map((_) => 0); |
| 459 | for (const b of data) { |
| 460 | const factor: byte = b ^ (result.shift() as byte); |
| 461 | result.push(0); |
| 462 | divisor.forEach((coef, i) => (result[i]! ^= QrCode.reedSolomonMultiply(coef, factor))); |
| 463 | } |
| 464 | return result; |
| 465 | } |
| 466 | |
| 467 | private static reedSolomonMultiply(x: byte, y: byte): byte { |
| 468 | if (x >>> 8 != 0 || y >>> 8 != 0) throw new RangeError("Byte out of range"); |
| 469 | let z: int = 0; |
| 470 | for (let i = 7; i >= 0; i--) { |
| 471 | z = (z << 1) ^ ((z >>> 7) * 0x11d); |
| 472 | z ^= ((y >>> i) & 1) * x; |
| 473 | } |
| 474 | assert(z >>> 8 == 0); |
| 475 | return z as byte; |
| 476 | } |
| 477 | |
| 478 | private finderPenaltyCountPatterns(runHistory: Readonly<Array<int>>): int { |
| 479 | const n: int = runHistory[1]!; |
| 480 | assert(n <= this.size * 3); |
| 481 | const core: boolean = |
| 482 | n > 0 && |
| 483 | runHistory[2] === n && |
| 484 | runHistory[3] === n * 3 && |
| 485 | runHistory[4] === n && |
| 486 | runHistory[5] === n; |
| 487 | return ( |
| 488 | (core && runHistory[0]! >= n * 4 && runHistory[6]! >= n ? 1 : 0) + |
| 489 | (core && runHistory[6]! >= n * 4 && runHistory[0]! >= n ? 1 : 0) |
| 490 | ); |
| 491 | } |
| 492 | |
| 493 | private finderPenaltyTerminateAndCount( |
| 494 | currentRunColor: boolean, |
| 495 | currentRunLength: int, |
| 496 | runHistory: Array<int>, |
| 497 | ): int { |
| 498 | if (currentRunColor) { |
| 499 | this.finderPenaltyAddHistory(currentRunLength, runHistory); |
| 500 | currentRunLength = 0; |
| 501 | } |
| 502 | currentRunLength += this.size; |
| 503 | this.finderPenaltyAddHistory(currentRunLength, runHistory); |
| 504 | return this.finderPenaltyCountPatterns(runHistory); |
| 505 | } |
| 506 | |
| 507 | private finderPenaltyAddHistory(currentRunLength: int, runHistory: Array<int>): void { |
| 508 | if (runHistory[0] === 0) currentRunLength += this.size; |
| 509 | runHistory.pop(); |
| 510 | runHistory.unshift(currentRunLength); |
| 511 | } |
| 512 | |
| 513 | public static readonly MIN_VERSION: int = 1; |
| 514 | public static readonly MAX_VERSION: int = 40; |
| 515 | |
| 516 | private static readonly PENALTY_N1: int = 3; |
| 517 | private static readonly PENALTY_N2: int = 3; |
| 518 | private static readonly PENALTY_N3: int = 40; |
| 519 | private static readonly PENALTY_N4: int = 10; |
| 520 | |
| 521 | private static readonly ECC_CODEWORDS_PER_BLOCK: Array<Array<int>> = [ |
| 522 | [ |
| 523 | -1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, |
| 524 | 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, |
| 525 | ], |
| 526 | [ |
| 527 | -1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, |
| 528 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
| 529 | ], |
| 530 | [ |
| 531 | -1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, |
| 532 | 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, |
| 533 | ], |
| 534 | [ |
| 535 | -1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, |
| 536 | 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, |
| 537 | ], |
| 538 | ]; |
| 539 | |
| 540 | private static readonly NUM_ERROR_CORRECTION_BLOCKS: Array<Array<int>> = [ |
| 541 | [ |
| 542 | -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, |
| 543 | 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25, |
| 544 | ], |
| 545 | [ |
| 546 | -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, |
| 547 | 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49, |
| 548 | ], |
| 549 | [ |
| 550 | -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, |
| 551 | 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68, |
| 552 | ], |
| 553 | [ |
| 554 | -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, |
| 555 | 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81, |
| 556 | ], |
| 557 | ]; |
| 558 | } |
| 559 | |
| 560 | function appendBits(val: int, len: int, bb: Array<bit>): void { |
| 561 | if (len < 0 || len > 31 || val >>> len != 0) throw new RangeError("Value out of range"); |
| 562 | for (let i = len - 1; i >= 0; i--) bb.push((val >>> i) & 1); |
| 563 | } |
| 564 | |
| 565 | function getBit(x: int, i: int): boolean { |
| 566 | return ((x >>> i) & 1) != 0; |
| 567 | } |
| 568 | |
| 569 | function assert(cond: boolean): void { |
| 570 | if (!cond) throw new Error("Assertion error"); |
| 571 | } |
| 572 | |
| 573 | export class QrSegment { |
| 574 | public static Mode: typeof QrSegmentMode; |
| 575 | |
| 576 | public static makeBytes(data: Readonly<Array<byte>>): QrSegment { |
| 577 | let bb: Array<bit> = []; |
| 578 | for (const b of data) appendBits(b, 8, bb); |
| 579 | return new QrSegment(QrSegment.Mode.BYTE, data.length, bb); |
| 580 | } |
| 581 | |
| 582 | public static makeNumeric(digits: string): QrSegment { |
| 583 | if (!QrSegment.isNumeric(digits)) |
| 584 | throw new RangeError("String contains non-numeric characters"); |
| 585 | let bb: Array<bit> = []; |
| 586 | for (let i = 0; i < digits.length;) { |
| 587 | const n: int = Math.min(digits.length - i, 3); |
| 588 | appendBits(parseInt(digits.substring(i, i + n), 10), n * 3 + 1, bb); |
| 589 | i += n; |
| 590 | } |
| 591 | return new QrSegment(QrSegment.Mode.NUMERIC, digits.length, bb); |
| 592 | } |
| 593 | |
| 594 | public static makeAlphanumeric(text: string): QrSegment { |
| 595 | if (!QrSegment.isAlphanumeric(text)) |
| 596 | throw new RangeError("String contains unencodable characters in alphanumeric mode"); |
| 597 | let bb: Array<bit> = []; |
| 598 | let i: int; |
| 599 | for (i = 0; i + 2 <= text.length; i += 2) { |
| 600 | let temp: int = QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45; |
| 601 | temp += QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1)); |
| 602 | appendBits(temp, 11, bb); |
| 603 | } |
| 604 | if (i < text.length) appendBits(QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6, bb); |
| 605 | return new QrSegment(QrSegment.Mode.ALPHANUMERIC, text.length, bb); |
| 606 | } |
| 607 | |
| 608 | public static makeSegments(text: string): Array<QrSegment> { |
| 609 | if (text == "") return []; |
| 610 | else if (QrSegment.isNumeric(text)) return [QrSegment.makeNumeric(text)]; |
| 611 | else if (QrSegment.isAlphanumeric(text)) return [QrSegment.makeAlphanumeric(text)]; |
| 612 | else return [QrSegment.makeBytes(QrSegment.toUtf8ByteArray(text))]; |
| 613 | } |
| 614 | |
| 615 | public static makeEci(assignVal: int): QrSegment { |
| 616 | let bb: Array<bit> = []; |
| 617 | if (assignVal < 0) throw new RangeError("ECI assignment value out of range"); |
| 618 | else if (assignVal < 1 << 7) appendBits(assignVal, 8, bb); |
| 619 | else if (assignVal < 1 << 14) { |
| 620 | appendBits(0b10, 2, bb); |
| 621 | appendBits(assignVal, 14, bb); |
| 622 | } else if (assignVal < 1000000) { |
| 623 | appendBits(0b110, 3, bb); |
| 624 | appendBits(assignVal, 21, bb); |
| 625 | } else throw new RangeError("ECI assignment value out of range"); |
| 626 | return new QrSegment(QrSegment.Mode.ECI, 0, bb); |
| 627 | } |
| 628 | |
| 629 | public static isNumeric(text: string): boolean { |
| 630 | return QrSegment.NUMERIC_REGEX.test(text); |
| 631 | } |
| 632 | |
| 633 | public static isAlphanumeric(text: string): boolean { |
| 634 | return QrSegment.ALPHANUMERIC_REGEX.test(text); |
| 635 | } |
| 636 | |
| 637 | public readonly mode: QrSegmentMode; |
| 638 | public readonly numChars: int; |
| 639 | private readonly bitData: Array<bit>; |
| 640 | |
| 641 | // numChars must match the mode and bit-buffer length; not checked. |
| 642 | public constructor( |
| 643 | mode: QrSegmentMode, |
| 644 | |
| 645 | numChars: int, |
| 646 | |
| 647 | bitData: Array<bit>, |
| 648 | ) { |
| 649 | this.mode = mode; |
| 650 | this.numChars = numChars; |
| 651 | if (numChars < 0) throw new RangeError("Invalid argument"); |
| 652 | this.bitData = bitData.slice(); |
| 653 | } |
| 654 | |
| 655 | public getData(): Array<bit> { |
| 656 | return this.bitData.slice(); |
| 657 | } |
| 658 | |
| 659 | public static getTotalBits(segs: Readonly<Array<QrSegment>>, version: int): number { |
| 660 | let result: number = 0; |
| 661 | for (const seg of segs) { |
| 662 | const ccbits: int = seg.mode.numCharCountBits(version); |
| 663 | if (seg.numChars >= 1 << ccbits) return Infinity; |
| 664 | result += 4 + ccbits + seg.bitData.length; |
| 665 | } |
| 666 | return result; |
| 667 | } |
| 668 | |
| 669 | private static toUtf8ByteArray(str: string): Array<byte> { |
| 670 | str = encodeURI(str); |
| 671 | let result: Array<byte> = []; |
| 672 | for (let i = 0; i < str.length; i++) { |
| 673 | if (str.charAt(i) != "%") result.push(str.charCodeAt(i)); |
| 674 | else { |
| 675 | result.push(parseInt(str.substring(i + 1, i + 3), 16)); |
| 676 | i += 2; |
| 677 | } |
| 678 | } |
| 679 | return result; |
| 680 | } |
| 681 | |
| 682 | private static readonly NUMERIC_REGEX: RegExp = /^[0-9]*$/; |
| 683 | |
| 684 | private static readonly ALPHANUMERIC_REGEX: RegExp = /^[A-Z0-9 $%*+.\/:-]*$/; |
| 685 | |
| 686 | private static readonly ALPHANUMERIC_CHARSET: string = |
| 687 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; |
| 688 | } |
| 689 | |
| 690 | class QrCodeEcc { |
| 691 | public static readonly LOW = new QrCodeEcc(0, 1); |
| 692 | public static readonly MEDIUM = new QrCodeEcc(1, 0); |
| 693 | public static readonly QUARTILE = new QrCodeEcc(2, 3); |
| 694 | public static readonly HIGH = new QrCodeEcc(3, 2); |
| 695 | |
| 696 | public readonly ordinal: int; |
| 697 | public readonly formatBits: int; |
| 698 | |
| 699 | private constructor(ordinal: int, formatBits: int) { |
| 700 | this.ordinal = ordinal; |
| 701 | this.formatBits = formatBits; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | class QrSegmentMode { |
| 706 | public static readonly NUMERIC = new QrSegmentMode(0x1, [10, 12, 14]); |
| 707 | public static readonly ALPHANUMERIC = new QrSegmentMode(0x2, [9, 11, 13]); |
| 708 | public static readonly BYTE = new QrSegmentMode(0x4, [8, 16, 16]); |
| 709 | public static readonly KANJI = new QrSegmentMode(0x8, [8, 10, 12]); |
| 710 | public static readonly ECI = new QrSegmentMode(0x7, [0, 0, 0]); |
| 711 | |
| 712 | public readonly modeBits: int; |
| 713 | private readonly numBitsCharCount: [int, int, int]; |
| 714 | |
| 715 | private constructor(modeBits: int, numBitsCharCount: [int, int, int]) { |
| 716 | this.modeBits = modeBits; |
| 717 | this.numBitsCharCount = numBitsCharCount; |
| 718 | } |
| 719 | |
| 720 | public numCharCountBits(ver: int): int { |
| 721 | return this.numBitsCharCount[Math.floor((ver + 7) / 17)]!; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | QrCode.Ecc = QrCodeEcc; |
| 726 | QrSegment.Mode = QrSegmentMode; |