/** * Runs MathCAT (mathcat.js + mathcat_bg.wasm, beside this file) for * johnmcm.com's Equation Editor: MathML in, Nemeth or UEB braille out, as * Unicode braille cells. * * Messages in: { id, mathml, code: 'Nemeth' | 'UEB' } * Messages out: { id, braille, version } * { id, problem: 'symbol', chars } a character MathCAT has no braille for * { id, problem: 'mathcat', detail } MathCAT could not braille it * { id, error: 'load' } the module could not be downloaded * (an error, so the request fails) * * MathCAT spells out a character missing from its tables as '\x......' in * braille (as NVDA does). That is no use to a reader of the braille, so it is * reported instead, with the characters that caused it. * * A panic inside MathCAT aborts the WebAssembly instance, which cannot be used * again; the worker then reports the error and stops itself with an uncaught * error, so the page starts a fresh one for the next request. * * Written for johnmcm.com (2026); part of the module published at /source/mathcat/. */ import init, { braille, version } from './mathcat.js'; let ready = null; function load() { ready ??= init({ module_or_path: new URL('./mathcat_bg.wasm', import.meta.url) }).catch((err) => { ready = null; throw err; }); return ready; } // The braille of the quote, backslash and x or y that open MathCAT's '\x......' spelling. const SPELLED_OUT = /⠄⠸⠡[⠭⠽]/; const ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'" }; /** The distinct characters outside ASCII in the MathML's text, entities decoded. */ function textChars(mathml) { const text = mathml .replace(/<[^>]*>/g, ' ') .replace(/&#x([0-9a-f]+);/gi, (_, h) => String.fromCodePoint(parseInt(h, 16))) .replace(/&#(\d+);/g, (_, d) => String.fromCodePoint(+d)) .replace(/&(\w+);/g, (m, n) => ENTITIES[n] ?? m); return [...new Set([...text].filter((c) => c.codePointAt(0) > 0x7f && !/[\u2061-\u2064\u200b]/.test(c)))]; } const escapeXml = (c) => (c === '<' ? '<' : c === '&' ? '&' : c); self.onmessage = async (e) => { const { id, mathml, code } = e.data; try { await load(); } catch { self.postMessage({ id, error: 'load' }); return; } try { const out = braille(mathml, code); if (SPELLED_OUT.test(out)) { const spelled = (m) => { try { return SPELLED_OUT.test(braille(m, code)); } catch { return true; } }; const chars = textChars(mathml).filter((c) => ['mi', 'mo', 'mtext'].some((t) => spelled(`<${t}>${escapeXml(c)}`))); // The same cells can be real braille: in Nemeth, A′∖x is ⠠⠁⠄⠸⠡⠭. When no // character is to blame, each token is tried as it stands (with its // mathvariant), and only if none is spelled out either is the braille kept. const tokens = chars.length ? [] : [...mathml.matchAll(/<(mi|mn|mo|mtext|ms)\b[^>]*>[^<]*<\/\1>/g)].map((m) => m[0]); if (chars.length || tokens.some((t) => spelled(`${t}`))) { self.postMessage({ id, problem: 'symbol', chars }); return; } } self.postMessage({ id, braille: out, version: version() }); } catch (err) { if (err instanceof WebAssembly.RuntimeError) { self.postMessage({ id, problem: 'mathcat', detail: 'crashed' }); setTimeout(() => { throw err; }); return; } self.postMessage({ id, problem: 'mathcat', detail: String(err).slice(0, 2000) }); } };