|
| 1 | +/** |
| 2 | + * MicroPDF417 encoder (ISO/IEC 24728) |
| 3 | + * Compact variant of PDF417 for small items |
| 4 | + * |
| 5 | + * Features: |
| 6 | + * - 1-4 data columns (vs 1-30 in PDF417) |
| 7 | + * - 4-44 rows |
| 8 | + * - No start/stop patterns (uses Row Address Patterns instead) |
| 9 | + * - Smaller than standard PDF417 for short data |
| 10 | + */ |
| 11 | + |
| 12 | +import { InvalidInputError, CapacityError } from "../errors"; |
| 13 | +import { getCodewordPattern, getRowCluster } from "./pdf417/tables"; |
| 14 | +import { encodeData } from "./pdf417/encoder"; |
| 15 | +import { generateECCodewords } from "./pdf417/ec"; |
| 16 | + |
| 17 | +// MicroPDF417 symbol sizes: [columns, rows, dataCW, ecCW] |
| 18 | +const SYMBOL_SIZES: [number, number, number, number][] = [ |
| 19 | + [1, 11, 1, 6], // smallest |
| 20 | + [1, 14, 4, 6], |
| 21 | + [1, 17, 7, 6], |
| 22 | + [1, 20, 10, 6], |
| 23 | + [1, 24, 14, 7], |
| 24 | + [1, 28, 18, 7], |
| 25 | + [2, 8, 4, 8], |
| 26 | + [2, 11, 10, 8], |
| 27 | + [2, 14, 16, 8], |
| 28 | + [2, 17, 22, 10], |
| 29 | + [2, 20, 28, 11], |
| 30 | + [2, 23, 34, 12], |
| 31 | + [2, 26, 40, 14], |
| 32 | + [3, 6, 4, 10], |
| 33 | + [3, 8, 10, 10], |
| 34 | + [3, 10, 16, 12], |
| 35 | + [3, 12, 22, 14], |
| 36 | + [3, 15, 31, 16], |
| 37 | + [3, 20, 46, 18], |
| 38 | + [3, 26, 64, 20], |
| 39 | + [3, 32, 82, 24], |
| 40 | + [3, 38, 100, 28], |
| 41 | + [3, 44, 118, 32], |
| 42 | + [4, 4, 4, 8], |
| 43 | + [4, 6, 12, 8], |
| 44 | + [4, 8, 20, 10], |
| 45 | + [4, 10, 28, 12], |
| 46 | + [4, 12, 36, 14], |
| 47 | + [4, 15, 48, 16], |
| 48 | + [4, 20, 68, 22], |
| 49 | + [4, 26, 88, 28], |
| 50 | + [4, 32, 108, 32], |
| 51 | + [4, 38, 128, 36], |
| 52 | + [4, 44, 148, 40], |
| 53 | +]; |
| 54 | + |
| 55 | +export interface MicroPDF417Options { |
| 56 | + columns?: 1 | 2 | 3 | 4; |
| 57 | +} |
| 58 | + |
| 59 | +export interface MicroPDF417Result { |
| 60 | + matrix: boolean[][]; |
| 61 | + rows: number; |
| 62 | + cols: number; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Encode text as MicroPDF417 |
| 67 | + */ |
| 68 | +export function encodeMicroPDF417( |
| 69 | + text: string, |
| 70 | + options: MicroPDF417Options = {}, |
| 71 | +): MicroPDF417Result { |
| 72 | + if (text.length === 0) { |
| 73 | + throw new InvalidInputError("MicroPDF417 input must not be empty"); |
| 74 | + } |
| 75 | + |
| 76 | + // Encode data to codewords |
| 77 | + const dataCW = encodeData(text); |
| 78 | + |
| 79 | + // Select symbol size |
| 80 | + const symbol = selectSize(dataCW.length, options.columns); |
| 81 | + if (!symbol) { |
| 82 | + throw new CapacityError(`Data too long for MicroPDF417: ${dataCW.length} codewords needed`); |
| 83 | + } |
| 84 | + |
| 85 | + const [cols, rows, maxDataCW, ecCW] = symbol; |
| 86 | + |
| 87 | + // Pad data codewords |
| 88 | + while (dataCW.length < maxDataCW) { |
| 89 | + dataCW.push(900); // text compaction latch as pad |
| 90 | + } |
| 91 | + |
| 92 | + // Generate EC codewords |
| 93 | + const ecLevel = Math.ceil(Math.log2(ecCW)) - 1; |
| 94 | + const ec = generateECCodewords(dataCW, Math.max(0, Math.min(ecLevel, 8))); |
| 95 | + |
| 96 | + // Combine |
| 97 | + const allCW = [...dataCW, ...ec.slice(0, ecCW)]; |
| 98 | + |
| 99 | + // Build matrix |
| 100 | + const moduleWidth = cols * 17 + 17 + 17; // data + left RAP + right RAP |
| 101 | + const matrix: boolean[][] = []; |
| 102 | + |
| 103 | + for (let row = 0; row < rows; row++) { |
| 104 | + const cluster = getRowCluster(row); |
| 105 | + const rowModules: boolean[] = []; |
| 106 | + |
| 107 | + // Left Row Address Pattern (simplified: use cluster 0 codeword for row indicator) |
| 108 | + const leftRAP = getCodewordPattern(row % 929, cluster); |
| 109 | + for (const w of leftRAP) { |
| 110 | + for (let i = 0; i < w; i++) { |
| 111 | + rowModules.push(rowModules.length % 2 === 0); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // Data codewords for this row |
| 116 | + for (let col = 0; col < cols; col++) { |
| 117 | + const cwIndex = row * cols + col; |
| 118 | + const cw = cwIndex < allCW.length ? allCW[cwIndex]! : 0; |
| 119 | + const pattern = getCodewordPattern(cw % 929, cluster); |
| 120 | + let isBar = true; |
| 121 | + for (const w of pattern) { |
| 122 | + for (let i = 0; i < w; i++) { |
| 123 | + rowModules.push(isBar); |
| 124 | + } |
| 125 | + isBar = !isBar; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // Right Row Address Pattern |
| 130 | + const rightRAP = getCodewordPattern((row + 1) % 929, cluster); |
| 131 | + let isBar = true; |
| 132 | + for (const w of rightRAP) { |
| 133 | + for (let i = 0; i < w; i++) { |
| 134 | + rowModules.push(isBar); |
| 135 | + } |
| 136 | + isBar = !isBar; |
| 137 | + } |
| 138 | + |
| 139 | + // Termination bar |
| 140 | + rowModules.push(true); |
| 141 | + |
| 142 | + matrix.push(rowModules); |
| 143 | + } |
| 144 | + |
| 145 | + return { matrix, rows, cols: matrix[0]?.length ?? 0 }; |
| 146 | +} |
| 147 | + |
| 148 | +function selectSize( |
| 149 | + dataCWCount: number, |
| 150 | + requestedCols?: number, |
| 151 | +): [number, number, number, number] | undefined { |
| 152 | + for (const size of SYMBOL_SIZES) { |
| 153 | + const [cols, _rows, maxData, _ec] = size; |
| 154 | + if (requestedCols && cols !== requestedCols) continue; |
| 155 | + if (dataCWCount <= maxData) return size; |
| 156 | + } |
| 157 | + return undefined; |
| 158 | +} |
0 commit comments