aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / docs / design / tools / extract-font.mjs
| 1 | // extract-font.mjs -- copies Allen's stroke typeface out of the kernel (native/kernel/ui/allen_font.c) into |
| 2 | // docs/design/screens/allen-font.js, so the screen mockups draw their words with the same strokes the kernel |
| 3 | // draws, at the kernel's cap heights and stroke weights. Dev tool only. |
| 4 | // |
| 5 | // node docs/design/tools/extract-font.mjs path/to/allen_font.c "source label" > docs/design/screens/allen-font.js |
| 6 | |
| 7 | import { readFileSync } from "node:fs"; |
| 8 | |
| 9 | const [src, label = "native/kernel/ui/allen_font.c"] = process.argv.slice(2); |
| 10 | if (!src) { |
| 11 | console.error("usage: extract-font.mjs allen_font.c [label]"); |
| 12 | process.exit(2); |
| 13 | } |
| 14 | const c = readFileSync(src, "utf8"); |
| 15 | const start = c.indexOf("glyphs[CK_AF_COUNT] = {"); |
| 16 | const end = c.indexOf("};", start); |
| 17 | if (start < 0 || end < 0) { |
| 18 | console.error("extract-font: glyph table not found"); |
| 19 | process.exit(1); |
| 20 | } |
| 21 | const glyphs = [...c.slice(start, end).matchAll(/^\s*\/\*.*?\*\/\s*"([^"]*)"/gm)].map((m) => m[1]); |
| 22 | if (glyphs.length !== 95) { |
| 23 | console.error(`extract-font: expected 95 glyphs (ASCII 32..126), found ${glyphs.length}`); |
| 24 | process.exit(1); |
| 25 | } |
| 26 | |
| 27 | process.stdout.write(`// allen-font.js -- Allen's stroke typeface for the screen mockups. |
| 28 | // Generated from ${label} by docs/design/tools/extract-font.mjs. Do not edit. |
| 29 | // Glyph grid: cap height at y = 0, baseline at y = 20; one design unit = cap / 20 px. Commands: wN advance, |
| 30 | // M move, L line, A cx,cy,rx,ry,a0,a1 arc in degrees (0 right, 90 down), D dot. Round caps and joins. |
| 31 | // Half the stroke width is weight / 1000 design units (kernel ck_af_face_init). |
| 32 | "use strict"; |
| 33 | const AF_GLYPHS = ${JSON.stringify(glyphs, null, 0).replace(/","/g, '",\n "').replace("[", "[\n ").replace(/]$/, "\n]")}; |
| 34 | |
| 35 | // The faces Allen's window uses (allen_ui.c 823: cap heights at 1080p; 860: weights). |
| 36 | const AF_FACES = { voice: { cap: 19, weight: 1000, line: 40 }, body: { cap: 15, weight: 1150, line: 30 }, |
| 37 | small: { cap: 11, weight: 1250, line: 22 } }; |
| 38 | |
| 39 | function afGlyph(ch) { |
| 40 | const code = ch.charCodeAt(0); |
| 41 | return AF_GLYPHS[code >= 32 && code <= 126 ? code - 32 : "?".charCodeAt(0) - 32]; |
| 42 | } |
| 43 | |
| 44 | function afAdvance(ch) { return parseInt(afGlyph(ch).slice(1), 10); } |
| 45 | |
| 46 | // SVG path data for one line of text, in px, pen starting at x0 with the cap top at y0. |
| 47 | function afPath(text, cap, x0 = 0, y0 = 0) { |
| 48 | const u = cap / 20, out = []; |
| 49 | let pen = x0; |
| 50 | for (const ch of text) { |
| 51 | const g = afGlyph(ch), toks = g.split(" ").slice(1); |
| 52 | const P = (x, y) => \`\${(pen + x * u).toFixed(2)} \${(y0 + y * u).toFixed(2)}\`; |
| 53 | for (const t of toks) { |
| 54 | const n = t.slice(1).split(",").map(Number); |
| 55 | if (t[0] === "M") out.push("M" + P(n[0], n[1])); |
| 56 | else if (t[0] === "L") out.push("L" + P(n[0], n[1])); |
| 57 | else if (t[0] === "D") out.push("M" + P(n[0], n[1]) + "l0.01 0"); |
| 58 | else if (t[0] === "A") { |
| 59 | const [cx, cy, rx, ry, a0, a1] = n, steps = Math.max(2, Math.ceil(Math.abs(a1 - a0) / 8)); |
| 60 | for (let i = 0; i <= steps; i++) { |
| 61 | const a = ((a0 + (a1 - a0) * i / steps) * Math.PI) / 180; |
| 62 | out.push((i ? "L" : "M") + P(cx + rx * Math.cos(a), cy + ry * Math.sin(a))); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | pen += parseInt(g.slice(1), 10) * u; |
| 67 | } |
| 68 | return { d: out.join(""), width: pen - x0 }; |
| 69 | } |
| 70 | |
| 71 | function afMeasure(text, cap) { let w = 0; for (const ch of text) w += afAdvance(ch); return (w * cap) / 20; } |
| 72 | |
| 73 | // Word wrap to a width in px (the kernel wraps greedily, allen_ui.c 150), then balanced: the narrowest width |
| 74 | // that keeps the same number of lines, so no line ends with one stranded word. |
| 75 | function afWrapAt(text, cap, width) { |
| 76 | const lines = []; |
| 77 | for (const para of text.split("\\n")) { |
| 78 | let line = ""; |
| 79 | for (const word of para.split(" ")) { |
| 80 | const next = line ? line + " " + word : word; |
| 81 | if (line && afMeasure(next, cap) > width) { lines.push(line); line = word; } else line = next; |
| 82 | } |
| 83 | lines.push(line); |
| 84 | } |
| 85 | return lines; |
| 86 | } |
| 87 | function afWrap(text, cap, width) { |
| 88 | const greedy = afWrapAt(text, cap, width); |
| 89 | if (greedy.length < 2) return greedy; |
| 90 | let lo = 0, hi = width; |
| 91 | while (hi - lo > 2) { const mid = (lo + hi) / 2; if (afWrapAt(text, cap, mid).length <= greedy.length) hi = mid; else lo = mid; } |
| 92 | return afWrapAt(text, cap, hi); |
| 93 | } |
| 94 | |
| 95 | // Draws every [data-af] element's text with the stroke font. The real text stays in the page for screen |
| 96 | // readers and search; the drawing is aria-hidden. data-af = voice | body | small; <p> wraps to its width (or data-wrap px), anything else stays on one line. |
| 97 | function afRender(root = document) { |
| 98 | for (const el of root.querySelectorAll("[data-af]")) { |
| 99 | if (el.dataset.afDone) continue; |
| 100 | const face = AF_FACES[el.dataset.af] || AF_FACES.body, text = el.textContent.replace(/\\s+/g, " ").trim(); |
| 101 | const wrap = Number(el.dataset.wrap || 0) || (el.tagName === "P" ? el.clientWidth || 900 : 1e9); // only paragraphs wrap |
| 102 | const lines = afWrap(text, face.cap, wrap), sw = (2 * face.weight / 1000) * (face.cap / 20); |
| 103 | const w = Math.max(...lines.map((l) => afMeasure(l, face.cap))) + sw * 2; |
| 104 | const h = (lines.length - 1) * face.line + face.cap * 27 / 20 + sw * 2; |
| 105 | const align = getComputedStyle(el).textAlign; |
| 106 | const d = lines.map((l, i) => { |
| 107 | const lw = afMeasure(l, face.cap), x = align === "center" ? (w - lw) / 2 : align === "right" ? w - lw - sw : sw; |
| 108 | return afPath(l, face.cap, x, sw + i * face.line).d; |
| 109 | }).join(""); |
| 110 | const ns = "http://www.w3.org/2000/svg", svg = document.createElementNS(ns, "svg"); |
| 111 | svg.setAttribute("width", w.toFixed(1)); svg.setAttribute("height", h.toFixed(1)); |
| 112 | svg.setAttribute("viewBox", \`0 0 \${w.toFixed(1)} \${h.toFixed(1)}\`); svg.setAttribute("aria-hidden", "true"); |
| 113 | svg.classList.add("af-svg"); |
| 114 | const path = document.createElementNS(ns, "path"); |
| 115 | path.setAttribute("d", d); path.setAttribute("fill", "none"); path.setAttribute("stroke", "currentColor"); |
| 116 | path.setAttribute("stroke-width", sw.toFixed(2)); path.setAttribute("stroke-linecap", "round"); |
| 117 | path.setAttribute("stroke-linejoin", "round"); |
| 118 | svg.appendChild(path); |
| 119 | const span = document.createElement("span"); |
| 120 | span.className = "af-text"; span.textContent = text; |
| 121 | el.textContent = ""; el.append(span, svg); el.dataset.afDone = "1"; |
| 122 | } |
| 123 | } |
| 124 | `); |