mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-26 23:28:43 +00:00
fix(data): draw link-card characters from OpenMono's rest file (#64798)
This commit is contained in:
parent
377026dd03
commit
5dfb6dfccd
9 changed files with 88 additions and 20 deletions
|
|
@ -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"
|
||||
|
|
|
|||
46
packages/data/app/script/openmono.ts
Normal file
46
packages/data/app/script/openmono.ts
Normal file
|
|
@ -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 <services/site/src/asset/fonts>")
|
||||
|
||||
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"))
|
||||
|
|
@ -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<OgAssets> {
|
|||
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),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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 () => {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue