diff options
Diffstat (limited to 'src/qr.js')
| -rw-r--r-- | src/qr.js | 18 |
1 files changed, 14 insertions, 4 deletions
@@ -6,10 +6,20 @@ import qrcode from 'qrcode-generator'; // ec: error-correction level. 'M' by default; pass 'L' for long payloads (e.g. // gift links) where a smaller, chunkier grid scans more easily — on-screen QRs // are scanned immediately and don't need the damage tolerance of higher levels. -export function qrSvg(text, { margin = 2, ec = 'M' } = {}) { - const qr = qrcode(0, ec); // type 0 = auto-size - qr.addData(text); - qr.make(); +// mode: pass 'Alphanumeric' for an all-uppercase QR-alphanumeric string (0-9 A-Z +// space $%*+-./:) to pack it at 5.5 bits/char instead of byte mode's 8 — a much +// smaller grid for the same data. Falls back to byte mode if the text doesn't fit. +export function qrSvg(text, { margin = 2, ec = 'M', mode } = {}) { + let qr; + try { + qr = qrcode(0, ec); + qr.addData(text, mode); + qr.make(); + } catch { + qr = qrcode(0, ec); // mode mismatch (e.g. a non-alphanumeric char) — plain byte mode + qr.addData(text); + qr.make(); + } const count = qr.getModuleCount(); const size = count + margin * 2; |
