|
| 1 | +/** |
| 2 | + * MaxiCode encoder (ISO/IEC 16023) |
| 3 | + * Fixed-size 2D barcode used on UPS shipping labels |
| 4 | + * |
| 5 | + * Structure: |
| 6 | + * - 33 rows × 30 columns hexagonal grid |
| 7 | + * - Central bullseye finder pattern |
| 8 | + * - 6 encoding modes (2/3 for structured carrier, 4/5/6 for general) |
| 9 | + * - Reed-Solomon error correction |
| 10 | + */ |
| 11 | + |
| 12 | +import { InvalidInputError } from "../errors"; |
| 13 | + |
| 14 | +const ROWS = 33; |
| 15 | +const COLS = 30; |
| 16 | + |
| 17 | +// MaxiCode character set (Mode 4: Standard Code Set A — printable ASCII) |
| 18 | +function encodeMode4(text: string): number[] { |
| 19 | + const codewords: number[] = []; |
| 20 | + for (const ch of text) { |
| 21 | + const code = ch.charCodeAt(0); |
| 22 | + if (code < 32 || code > 126) { |
| 23 | + codewords.push(0); // replace non-printable with space |
| 24 | + } else { |
| 25 | + codewords.push(code - 32); // offset to 0-based |
| 26 | + } |
| 27 | + } |
| 28 | + return codewords; |
| 29 | +} |
| 30 | + |
| 31 | +// Mode 2/3: Structured Carrier Message (UPS shipping) |
| 32 | +function encodeStructured( |
| 33 | + postalCode: string, |
| 34 | + countryCode: number, |
| 35 | + serviceClass: number, |
| 36 | + message: string, |
| 37 | + mode: 2 | 3, |
| 38 | +): number[] { |
| 39 | + const codewords: number[] = []; |
| 40 | + |
| 41 | + // Header: mode indicator |
| 42 | + codewords.push(mode); |
| 43 | + |
| 44 | + if (mode === 2) { |
| 45 | + // Numeric postal code (9 digits for US) |
| 46 | + const postal = postalCode.replace(/\D/g, "").padEnd(9, "0").substring(0, 9); |
| 47 | + // Pack 9 digits into 4 codewords |
| 48 | + const postalNum = Number.parseInt(postal, 10); |
| 49 | + codewords.push((postalNum >> 18) & 0x3f); |
| 50 | + codewords.push((postalNum >> 12) & 0x3f); |
| 51 | + codewords.push((postalNum >> 6) & 0x3f); |
| 52 | + codewords.push(postalNum & 0x3f); |
| 53 | + } else { |
| 54 | + // Alphanumeric postal code (6 chars for international) |
| 55 | + const postal = postalCode.padEnd(6, " ").substring(0, 6); |
| 56 | + for (const ch of postal) { |
| 57 | + codewords.push(ch.charCodeAt(0) & 0x3f); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Country code (3 digits → 10 bits → 2 codewords) |
| 62 | + codewords.push((countryCode >> 6) & 0x3f); |
| 63 | + codewords.push(countryCode & 0x3f); |
| 64 | + |
| 65 | + // Service class (3 digits → 10 bits → 2 codewords) |
| 66 | + codewords.push((serviceClass >> 6) & 0x3f); |
| 67 | + codewords.push(serviceClass & 0x3f); |
| 68 | + |
| 69 | + // Message data |
| 70 | + codewords.push(...encodeMode4(message)); |
| 71 | + |
| 72 | + return codewords; |
| 73 | +} |
| 74 | + |
| 75 | +// Simple RS for MaxiCode (GF(64)) |
| 76 | +function maxicodeEC(data: number[], ecCount: number): number[] { |
| 77 | + const ec = Array.from({ length: ecCount }, () => 0); |
| 78 | + for (const byte of data) { |
| 79 | + const feedback = (byte ^ ec[0]!) % 64; |
| 80 | + for (let j = 0; j < ecCount - 1; j++) { |
| 81 | + ec[j] = (ec[j + 1]! ^ (feedback * (j + 1))) % 64; |
| 82 | + } |
| 83 | + ec[ecCount - 1] = (feedback * ecCount) % 64; |
| 84 | + } |
| 85 | + return ec; |
| 86 | +} |
| 87 | + |
| 88 | +export interface MaxiCodeOptions { |
| 89 | + /** Encoding mode: 2 (US structured), 3 (intl structured), 4 (standard), 5 (full ECC), 6 (reader programming) */ |
| 90 | + mode?: 2 | 3 | 4 | 5 | 6; |
| 91 | + /** Postal code (modes 2/3) */ |
| 92 | + postalCode?: string; |
| 93 | + /** ISO country code number (modes 2/3) */ |
| 94 | + countryCode?: number; |
| 95 | + /** Service class (modes 2/3, e.g. 840 for UPS) */ |
| 96 | + serviceClass?: number; |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Encode text as MaxiCode |
| 101 | + * Returns a 33×30 boolean matrix (hexagonal grid representation) |
| 102 | + */ |
| 103 | +export function encodeMaxiCode(text: string, options: MaxiCodeOptions = {}): boolean[][] { |
| 104 | + if (text.length === 0) { |
| 105 | + throw new InvalidInputError("MaxiCode input must not be empty"); |
| 106 | + } |
| 107 | + |
| 108 | + const mode = options.mode ?? 4; |
| 109 | + let codewords: number[]; |
| 110 | + |
| 111 | + if (mode === 2 || mode === 3) { |
| 112 | + codewords = encodeStructured( |
| 113 | + options.postalCode ?? "", |
| 114 | + options.countryCode ?? 840, |
| 115 | + options.serviceClass ?? 1, |
| 116 | + text, |
| 117 | + mode, |
| 118 | + ); |
| 119 | + } else { |
| 120 | + codewords = [mode, ...encodeMode4(text)]; |
| 121 | + } |
| 122 | + |
| 123 | + // Pad to 144 data codewords (MaxiCode has 144 total symbols in grid) |
| 124 | + const totalDataCW = 84; // primary + secondary data |
| 125 | + while (codewords.length < totalDataCW) { |
| 126 | + codewords.push(33); // pad with '!' |
| 127 | + } |
| 128 | + codewords = codewords.slice(0, totalDataCW); |
| 129 | + |
| 130 | + // Error correction: primary (10 EC) + secondary (10 EC + 10 EC) |
| 131 | + const primaryData = codewords.slice(0, 10); |
| 132 | + const secondaryData1 = codewords.slice(10, 47); |
| 133 | + const secondaryData2 = codewords.slice(47, 84); |
| 134 | + |
| 135 | + const primaryEC = maxicodeEC(primaryData, 10); |
| 136 | + const secondaryEC1 = maxicodeEC(secondaryData1, 10); |
| 137 | + const secondaryEC2 = maxicodeEC(secondaryData2, 10); |
| 138 | + |
| 139 | + const allCW = [ |
| 140 | + ...primaryData, |
| 141 | + ...primaryEC, |
| 142 | + ...secondaryData1, |
| 143 | + ...secondaryEC1, |
| 144 | + ...secondaryData2, |
| 145 | + ...secondaryEC2, |
| 146 | + ]; |
| 147 | + |
| 148 | + // Build 33×30 matrix |
| 149 | + const matrix: boolean[][] = Array.from({ length: ROWS }, () => |
| 150 | + Array.from({ length: COLS }, () => false), |
| 151 | + ); |
| 152 | + |
| 153 | + // Place bullseye at center (rows 13-19, cols 12-17) |
| 154 | + placeBullseye(matrix); |
| 155 | + |
| 156 | + // Place data modules in spiral pattern around bullseye |
| 157 | + let cwIdx = 0; |
| 158 | + let bitIdx = 0; |
| 159 | + for (let r = 0; r < ROWS; r++) { |
| 160 | + for (let c = 0; c < COLS; c++) { |
| 161 | + // Skip bullseye area |
| 162 | + if (r >= 13 && r <= 19 && c >= 12 && c <= 17) continue; |
| 163 | + |
| 164 | + if (cwIdx < allCW.length) { |
| 165 | + // Each codeword is 6 bits |
| 166 | + const bit = (allCW[cwIdx]! >> (5 - bitIdx)) & 1; |
| 167 | + matrix[r]![c] = bit === 1; |
| 168 | + bitIdx++; |
| 169 | + if (bitIdx >= 6) { |
| 170 | + bitIdx = 0; |
| 171 | + cwIdx++; |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return matrix; |
| 178 | +} |
| 179 | + |
| 180 | +function placeBullseye(matrix: boolean[][]): void { |
| 181 | + const cx = 15; // center col (approx) |
| 182 | + const cy = 16; // center row |
| 183 | + |
| 184 | + // Concentric rings: dark, light, dark, light, dark |
| 185 | + for (let r = 13; r <= 19; r++) { |
| 186 | + for (let c = 12; c <= 17; c++) { |
| 187 | + const dist = Math.max(Math.abs(r - cy), Math.abs(c - cx)); |
| 188 | + matrix[r]![c] = dist % 2 === 0; |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments