// QR code de l'appariement (ADR 0005, point 4), dessiné en SVG par le DOM : // ni innerHTML, ni image data:, compatible avec la CSP stricte. // // Le calcul des modules est confié à qrcode-generator (Kazuhiko Arase, MIT), // vendorisé tel quel dans app/vendor/qrcode-generator/. Ce module n'est chargé // qu'à l'ouverture de l'écran Synchro (import dynamique) : il ne pèse rien au // premier chargement. import qrcode from "../vendor/qrcode-generator/qrcode.mjs"; /** Marge blanche autour du code, en modules : 4 selon la norme. */ export const QUIET_ZONE = 4; /** * Modules d'un QR code (true = noir). Correction d'erreur M (15 %), version * choisie au plus petit. Fonction pure, testée sous Node. * @param {string} text * @returns {boolean[][]} lignes de modules, carré */ export function qrMatrix(text) { const qr = qrcode(0, "M"); qr.addData(text, "Byte"); qr.make(); const n = qr.getModuleCount(); return Array.from({ length: n }, (_, r) => Array.from({ length: n }, (_, c) => !!qr.isDark(r, c))); } /** * Chemin SVG des modules noirs, décalés de la marge : un carré 1 × 1 par * module, regroupés par plages horizontales. * @param {boolean[][]} matrix * @param {number} [margin] */ export function qrPath(matrix, margin = QUIET_ZONE) { let d = ""; matrix.forEach((row, r) => { for (let c = 0; c < row.length; c++) { if (!row[c]) continue; let end = c; while (end + 1 < row.length && row[end + 1]) end++; d += `M${c + margin} ${r + margin}h${end - c + 1}v1h-${end - c + 1}z`; c = end; } }); return d; } const SVG = "http://www.w3.org/2000/svg"; /** * Le QR code en SVG : noir sur blanc quel que soit le thème (les lecteurs * lisent mal un code inversé), marge comprise. * @param {string} text * @param {string} label description pour les lecteurs d'écran * @returns {SVGSVGElement} */ export function qrSvg(text, label) { const matrix = qrMatrix(text); const size = matrix.length + 2 * QUIET_ZONE; const svg = document.createElementNS(SVG, "svg"); svg.setAttribute("viewBox", `0 0 ${size} ${size}`); svg.setAttribute("role", "img"); svg.setAttribute("aria-label", label); svg.setAttribute("shape-rendering", "crispEdges"); svg.setAttribute("class", "qr"); const bg = document.createElementNS(SVG, "rect"); bg.setAttribute("width", String(size)); bg.setAttribute("height", String(size)); bg.setAttribute("fill", "#FFFFFF"); const dark = document.createElementNS(SVG, "path"); dark.setAttribute("d", qrPath(matrix)); dark.setAttribute("fill", "#000000"); svg.append(bg, dark); return svg; }