From 5dfb6dfccde4adadafeda02ba06042d0981db2f9 Mon Sep 17 00:00:00 2001 From: David Hill <1879069+iamdavidhill@users.noreply.github.com> Date: Thu, 26 Nov 2026 14:50:36 -0600 Subject: [PATCH] fix(data): draw link-card characters from OpenMono's rest file (#64798) --- packages/data/app/script/build-og-worker.ts | 3 +- packages/data/app/script/openmono.ts | 46 +++++++++++++++++++++ packages/data/app/src/lib/resource.node.ts | 4 +- packages/data/app/src/og/card-data.ts | 2 +- packages/data/app/src/og/card.ts | 6 ++- packages/data/app/src/og/worker.ts | 10 ++--- packages/data/app/src/routes/index.css | 11 ++--- packages/data/app/test/og.test.ts | 16 +++++++ packages/nav-menus/src/nav-menus.css | 10 ++--- 9 files changed, 88 insertions(+), 20 deletions(-) create mode 100644 packages/data/app/script/openmono.ts diff --git a/packages/data/app/script/build-og-worker.ts b/packages/data/app/script/build-og-worker.ts index 00dd761db3b..84180dcaadf 100644 --- a/packages/data/app/script/build-og-worker.ts +++ b/packages/data/app/script/build-og-worker.ts @@ -1,5 +1,6 @@ // Builds the OG Worker (src/og/worker.ts) into .og-worker: the bundle, the Wasm engines, and the site's OpenMono -// fonts as TrueType (Satori can't read WOFF2). services/data/script/build.ts runs this and deploys every file there. It is +// fonts as TrueType (Satori can't read WOFF2): the Latin and rest files, not the Cyrillic one, as the cards set model +// and lab names. services/data/script/build.ts runs this and deploys every file there. It is // outside .output because the site's own build replaces that directory. test/og.test.ts builds it too, so PR CI // catches a broken build before the deploy does. import { mkdir, rm } from "node:fs/promises" diff --git a/packages/data/app/script/openmono.ts b/packages/data/app/script/openmono.ts new file mode 100644 index 00000000000..9a10b0c3366 --- /dev/null +++ b/packages/data/app/script/openmono.ts @@ -0,0 +1,46 @@ +// Copies the site's OpenMono files into this app: each style's "latin", "cyrillic" and "rest" subsets that +// services/site/script/openmono.py builds from OpenMono 1.000, and their licence. Run it from packages/data/app +// after the site's files change, with the folder that holds them: +// +// bun script/openmono.ts ../../../services/site/src/asset/fonts +// +// src/routes/index.css repeats each file's unicode-range in that file's @font-face rule. Every input is read and +// every rule is checked against the site's ranges before anything is written, so a missing file or a stale rule +// leaves this app's files as they were. +import path from "node:path" + +const source = process.argv[2] +if (!source) throw new Error("Usage: bun script/openmono.ts ") + +const app = path.resolve(import.meta.dir, "..") +const groups = ["latin", "cyrillic", "rest"] as const +const fonts = ["regular", "bold", "italic", "bold-italic"].flatMap((style) => + groups.map((group) => ({ group, name: `openmono-${style}-${group}.woff2` })), +) +const names = [...fonts.map((font) => font.name), "OpenMono-OFL.txt", "openmono.json"] +const missing = ( + await Promise.all(names.map(async (name) => ((await Bun.file(path.join(source, name)).exists()) ? [] : [name]))) +).flat() +if (missing.length) throw new Error(`Nothing was written. ${source} has no ${missing.join(", ")}`) + +const ranges: Record<(typeof groups)[number], string> = await Bun.file(path.join(source, "openmono.json")).json() +const compact = (text: string) => text.replaceAll(/\s/g, "") +const rules = [...(await Bun.file(path.join(app, "src/routes/index.css")).text()).matchAll(/@font-face\s*\{[^}]*\}/g)] +const stale = fonts.flatMap((font) => { + const rule = rules.find((match) => match[0].includes(`/${font.name}"`))?.[0] + if (rule && compact(rule).includes(compact(`unicode-range: ${ranges[font.group]};`))) return [] + return [`${font.name}: ${rule ? "its rule needs" : "add a rule with"} unicode-range: ${ranges[font.group]}`] +}) +if (stale.length) throw new Error(`Nothing was written. Update src/routes/index.css first:\n${stale.join("\n")}`) + +const files = await Promise.all( + names + .filter((name) => name !== "openmono.json") + .map(async (name) => ({ name, bytes: await Bun.file(path.join(source, name)).bytes() })), +) +const copied = await Promise.all( + files.map( + async (file) => `${file.name} ${await Bun.write(path.join(app, "src/asset/fonts", file.name), file.bytes)} bytes`, + ), +) +console.log(copied.join("\n")) diff --git a/packages/data/app/src/lib/resource.node.ts b/packages/data/app/src/lib/resource.node.ts index 2d7b5059b0d..ed45c8b6fa7 100644 --- a/packages/data/app/src/lib/resource.node.ts +++ b/packages/data/app/src/lib/resource.node.ts @@ -2,7 +2,7 @@ // the source fonts, there is no edge cache, and waitUntil just awaits. import { readFile } from "node:fs/promises" import { createRequire } from "node:module" -import type { OgCard } from "../og/card" +import { ogFontFamilies, type OgCard } from "../og/card" import type { OgAssets } from "../og/render-png" const resolve = createRequire(import.meta.url).resolve @@ -33,7 +33,7 @@ async function loadAssets(): Promise { await readFile(new URL(`../asset/fonts/openmono-${weight}-${group}.woff2`, import.meta.url)), ) return { - name: "OpenMono", + name: ogFontFamilies[group], data: bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) as ArrayBuffer, weight: weight === "bold" ? (700 as const) : (400 as const), } diff --git a/packages/data/app/src/og/card-data.ts b/packages/data/app/src/og/card-data.ts index 11c59a60c41..8e468ed7dbc 100644 --- a/packages/data/app/src/og/card-data.ts +++ b/packages/data/app/src/og/card-data.ts @@ -7,7 +7,7 @@ import { formatCatalogLabName, isKnownCatalogLab } from "../routes/model-catalog import type { OgCard } from "./card" // Bump to change every card's URL, so unfurlers fetch the new image after a design change. -const cardVersion = "2" +const cardVersion = "3" type Usage = { start: string; tokens: number } diff --git a/packages/data/app/src/og/card.ts b/packages/data/app/src/og/card.ts index 396d7a13e7e..3686f694be3 100644 --- a/packages/data/app/src/og/card.ts +++ b/packages/data/app/src/og/card.ts @@ -10,6 +10,10 @@ import { formatTokens } from "../lib/format" export const width = 1200 export const height = 630 +// Satori keeps one face per family name and weight, so each of the font's two files has a family name of its own +// and the card names both: the Latin file first, the rest for the characters it lacks. +export const ogFontFamilies = { latin: "OpenMono", rest: "OpenMono Rest" } as const + export type OgCard = | { kind: "model" @@ -99,7 +103,7 @@ function frame(...children: SatoriElement[]) { width, height, backgroundColor: colors.background, - fontFamily: "OpenMono", + fontFamily: Object.values(ogFontFamilies).join(", "), color: colors.text, }, dots(), diff --git a/packages/data/app/src/og/worker.ts b/packages/data/app/src/og/worker.ts index 7a8c0c92235..5da742fd6cf 100644 --- a/packages/data/app/src/og/worker.ts +++ b/packages/data/app/src/og/worker.ts @@ -9,7 +9,7 @@ import harfbuzz from "./harfbuzz.wasm" import resvg from "./resvg.wasm" import layout from "./satori-layout.wasm" import sprite from "../../../../ui/src/components/provider-icons/sprite.svg" -import type { OgCard } from "./card" +import { ogFontFamilies, type OgCard } from "./card" import { renderOgPng } from "./render-png" // Satori shapes text with HarfBuzz, whose Wasm it inlines as bytes and compiles at runtime, which Workers forbid. The @@ -25,10 +25,10 @@ WebAssembly.instantiate = ((source: BufferSource | WebAssembly.Module, imports?: }))) as typeof WebAssembly.instantiate const fonts = [ - { name: "OpenMono", data: regularLatin, weight: 400 as const }, - { name: "OpenMono", data: regularRest, weight: 400 as const }, - { name: "OpenMono", data: boldLatin, weight: 700 as const }, - { name: "OpenMono", data: boldRest, weight: 700 as const }, + { name: ogFontFamilies.latin, data: regularLatin, weight: 400 as const }, + { name: ogFontFamilies.rest, data: regularRest, weight: 400 as const }, + { name: ogFontFamilies.latin, data: boldLatin, weight: 700 as const }, + { name: ogFontFamilies.rest, data: boldRest, weight: 700 as const }, ] export default { diff --git a/packages/data/app/src/routes/index.css b/packages/data/app/src/routes/index.css index 00f3d865408..c9e85226cc6 100644 --- a/packages/data/app/src/routes/index.css +++ b/packages/data/app/src/routes/index.css @@ -1,8 +1,9 @@ -/* OpenMono, Anomaly's font (SIL OFL 1.1, ../asset/fonts/OpenMono-OFL.txt), copied from services/site/src/asset/fonts, - with its faces as in services/site style/fonts.css. "latin" covers Latin, punctuation, arrows and box drawing; - "cyrillic" and "rest" load only when a page uses one of their characters. Regular serves weights up to 599 and bold - the rest, so 500 and 600 never get a synthesized face. The Menlo, Consolas and Courier faces stand in while it loads: - the system font scaled to OpenMono's 0.6 em advance and line metrics, so the swap changes letter shapes only. */ +/* OpenMono, Anomaly's font (SIL OFL 1.1, ../asset/fonts/OpenMono-OFL.txt), copied from services/site/src/asset/fonts + by script/openmono.ts, with its faces as in services/site style/fonts.css. "latin" covers Latin, punctuation, arrows + and box drawing; "cyrillic" and "rest" load only when a page uses one of their characters. Regular serves weights up + to 599 and bold the rest, so 500 and 600 never get a synthesized face. The Menlo, Consolas and Courier faces stand in + while it loads: the system font scaled to OpenMono's 0.6 em advance and line metrics, so the swap changes letter + shapes only. */ @font-face { font-family: "OpenMono"; src: url("../asset/fonts/openmono-regular-latin.woff2") format("woff2"); diff --git a/packages/data/app/test/og.test.ts b/packages/data/app/test/og.test.ts index 2226e873f7d..57801a5687b 100644 --- a/packages/data/app/test/og.test.ts +++ b/packages/data/app/test/og.test.ts @@ -246,6 +246,22 @@ describe("og.png response", () => { }) }) +describe("card text", () => { + // Satori draws a character no font file has as the same empty box. The Greek alpha and the arrow ⥈ are in the + // font's "rest" file, not the Latin one, so each must draw as itself: different from that box and from the other. + test("draws characters the Latin file lacks from the rest file", async () => { + const card = modelOgCard(inkling)! + const [alpha, arrow, missing] = await Promise.all( + ["\u03b1", "\u2948", "\ue000"].map(async (character) => + Buffer.from(await resource.renderOgCard({ ...card, name: `Inkling ${character}` })).toString("base64"), + ), + ) + expect(alpha).not.toBe(missing) + expect(arrow).not.toBe(missing) + expect(alpha).not.toBe(arrow) + }, 30_000) +}) + describe("OG Worker build", () => { // The deploy builds and uploads this; building it here fails the PR instead. test("bundles the Worker with the Wasm and font modules it imports", async () => { diff --git a/packages/nav-menus/src/nav-menus.css b/packages/nav-menus/src/nav-menus.css index 03eb6a9b5e2..8723884c84b 100644 --- a/packages/nav-menus/src/nav-menus.css +++ b/packages/nav-menus/src/nav-menus.css @@ -8,7 +8,7 @@ --nav-menu-link the link blue: hover and focus --nav-menu-bar the active bar --nav-menu-icon the Brand, ZIP and Press drawings - --nav-menu-mono OpenTUI Mono (the host loads it) */ + --nav-menu-mono OpenMono (the host loads it) */ [data-nav-menu] { --ink: var(--nav-menu-ink, light-dark(#161616, #fafafa)); --weak: var(--nav-menu-weak, light-dark(#5c5c5c, #aeaeae)); @@ -45,7 +45,7 @@ border: 0; background: none; color: var(--ink); - font: 400 12px/16px var(--nav-menu-mono, "OpenTUI Mono", ui-monospace, monospace); + font: 400 12px/16px var(--nav-menu-mono, "OpenMono", ui-monospace, monospace); letter-spacing: 0; text-align: start; -webkit-font-smoothing: antialiased; @@ -205,7 +205,7 @@ /* The product name's hover fill, opencode.ai's lockup hover: the hover step of the shared grey scale. Fixed, so it is the same on every page background. */ --nav-name-hover: light-dark(#eeeeee, #242424); - /* The product name after the mark (and the docs' DOCS): its size, and the padding under it that puts OpenTUI Mono's + /* The product name after the mark (and the docs' DOCS): its size, and the padding under it that puts OpenMono's caps (0.69 of the size) where they belong in the 25px row. */ --nav-name-size: 15px; --nav-name-pad: 0.65px; @@ -228,7 +228,7 @@ } /* The lockup: the logo (`data-nav-logo`, a 20 × 25 mark at whole pixels in its box, as on opencode.ai), then the - product's name (`data-nav-name`) 12px after the box: OpenTUI Mono Bold 15px caps whose 6px padding is the + product's name (`data-nav-name`) 12px after the box: OpenMono Bold 15px caps whose 6px padding is the highlight, --nav-name-hover while hovered or pressed. Placed at --nav-edge, centred in the bar. */ [data-nav-lockup] { --nav-lockup-ink: light-dark(#161616, #fafafa); @@ -281,7 +281,7 @@ padding: 0 6px var(--nav-name-pad); background: none; color: var(--nav-lockup-ink); - font: 700 var(--nav-name-size) / 1 var(--nav-menu-mono, "OpenTUI Mono", ui-monospace, monospace); + font: 700 var(--nav-name-size) / 1 var(--nav-menu-mono, "OpenMono", ui-monospace, monospace); letter-spacing: 0; text-decoration: none; text-transform: uppercase;