Open source CapCut alternative.
TypeScript
fix(site): set titles from the face's em box on every canvas [rebuild]
David Le308084cbb85e
diff --git a/evals/cut-perf.latest-report.json b/evals/cut-perf.latest-report.jsonindex 4cba0c60..a8f1de8b 100644--- a/evals/cut-perf.latest-report.json+++ b/evals/cut-perf.latest-report.json@@ -51,10 +51,10 @@},"frameMs": 16.7,"scrub": {- "p50": 15.9,- "p95": 32.6,- "mean": 18.4,- "max": 32.8,+ "p50": 15.3,+ "p95": 31.8,+ "mean": 17.99,+ "max": 33.7,"n": 41},"longTasks": {@@ -64,7 +64,7 @@"max": 0,"n": 0},- "liveSamples": 56,+ "liveSamples": 55,"machine": "desktop"},{@@ -81,10 +81,10 @@},"frameMs": 16.7,"scrub": {- "p50": 15.7,- "p95": 32.2,- "mean": 17.47,- "max": 32.6,+ "p50": 15.9,+ "p95": 32.5,+ "mean": 17.68,+ "max": 33,"n": 41},"longTasks": {@@ -105,16 +105,16 @@"late": {"p50": 0,"p95": 0,- "mean": 0.05,+ "mean": 0.02,"max": 1,"n": 41},"frameMs": 16.7,"scrub": {- "p50": 14.2,- "p95": 18.7,- "mean": 13.39,- "max": 32.3,+ "p50": 15.6,+ "p95": 17.3,+ "mean": 15.83,+ "max": 30.9,"n": 41},"longTasks": {@@ -124,7 +124,7 @@"max": 0,"n": 0},- "liveSamples": 57,+ "liveSamples": 56,"machine": "desktop"},{@@ -135,16 +135,16 @@"late": {"p50": 0,"p95": 0,- "mean": 0.01,+ "mean": 0.02,"max": 1,"n": 81},"frameMs": 16.7,"scrub": {- "p50": 15.7,- "p95": 16.7,- "mean": 15.69,- "max": 32.3,+ "p50": 15.4,+ "p95": 17.3,+ "mean": 15.73,+ "max": 32.9,"n": 81},"longTasks": {@@ -165,16 +165,16 @@"late": {"p50": 0,"p95": 1,- "mean": 0.17,- "max": 1,+ "mean": 0.2,+ "max": 2,"n": 41},"frameMs": 16.7,"scrub": {- "p50": 15.7,- "p95": 32.5,- "mean": 18.43,- "max": 32.8,+ "p50": 16,+ "p95": 32.7,+ "mean": 18.98,+ "max": 52,"n": 41},"longTasks": {@@ -184,7 +184,7 @@"max": 0,"n": 0},- "liveSamples": 55,+ "liveSamples": 46,"machine": "desktop"},{@@ -202,9 +202,9 @@"frameMs": 16.7,"scrub": {"p50": 15.7,- "p95": 32.4,- "mean": 18.42,- "max": 32.5,+ "p95": 32.9,+ "mean": 18.45,+ "max": 33.9,"n": 41},"longTasks": {@@ -214,7 +214,7 @@"max": 0,"n": 0},- "liveSamples": 55,+ "liveSamples": 44,"machine": "desktop"},{@@ -231,10 +231,10 @@},"frameMs": 16.7,"scrub": {- "p50": 32.3,- "p95": 33,- "mean": 30.47,- "max": 33.2,+ "p50": 31.9,+ "p95": 33.7,+ "mean": 30.29,+ "max": 33.9,"n": 41},"longTasks": {@@ -326,13 +326,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 3243,+ "presented": 3239,"lag": {"p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 3243+ "n": 3239},"stallShare": 0,"decay": {@@ -437,13 +437,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 3443,+ "presented": 3444,"lag": {"p50": 0.01,"p95": 0.02,"mean": 0.01,"max": 0.02,- "n": 3443+ "n": 3444},"stallShare": 0,"decay": {@@ -474,13 +474,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 3430,+ "presented": 3444,"lag": {"p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 3430+ "n": 3444},"stallShare": 0,"decay": {@@ -492,11 +492,11 @@"reported": 0.04},"longTasks": {- "p50": 190,- "p95": 190,- "mean": 134.5,- "max": 190,- "n": 2+ "p50": 0,+ "p95": 0,+ "mean": 0,+ "max": 0,+ "n": 0},"liveSamples": 10,"liveSources": 1,@@ -511,13 +511,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 3446,+ "presented": 3444,"lag": {- "p50": 0.02,+ "p50": 0.01,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 3446+ "n": 3444},"stallShare": 0,"decay": {@@ -529,11 +529,11 @@"reported": 0.04},"longTasks": {- "p50": 53,- "p95": 53,- "mean": 53,- "max": 53,- "n": 1+ "p50": 0,+ "p95": 0,+ "mean": 0,+ "max": 0,+ "n": 0},"liveSamples": 10,"liveSources": 1,@@ -589,7 +589,7 @@"lag": {"p50": 0.02,"p95": 0.03,- "mean": 0.01,+ "mean": 0.02,"max": 0.03,"n": 3444},@@ -622,13 +622,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 1621,+ "presented": 1620,"lag": {"p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 1621+ "n": 1620},"stallShare": 0,"decay": {@@ -691,10 +691,10 @@"bucket": "playback","pass": true,"notes": [],- "drops": 2,- "presented": 1094,+ "drops": 3,+ "presented": 1095,"lag": {- "p50": 0.01,+ "p50": 0.02,"p95": 0.03,"mean": 0.01,"max": 0.03,@@ -702,7 +702,7 @@},"stallShare": 0,"decay": {- "first": 0.005,+ "first": 0.008,"last": 0},"clock": {@@ -710,10 +710,10 @@"reported": 0.04},"longTasks": {- "p50": 128,- "p95": 128,- "mean": 97.5,- "max": 128,+ "p50": 153,+ "p95": 153,+ "mean": 113.5,+ "max": 153,"n": 2},"liveSamples": 10,diff --git a/site/packages/effects-kit/src/render.ts b/site/packages/effects-kit/src/render.tsindex 87642403..b559b26c 100644--- a/site/packages/effects-kit/src/render.ts+++ b/site/packages/effects-kit/src/render.ts@@ -137,6 +137,37 @@ export interface RenderEnv {/** Seekable player for a Lottie sticker asset id; null when the asset is* gone or isn't a Lottie document. */resolveLottie?(assetId: string): Promise<LottieHandle | null>;+ /** The share of a face's em box above its baseline, read off the face's own+ * tables, for a css font; undefined for a face the host cannot read. Lines+ * are set from the em box's middle (`baselineDrop`), so a host that knows+ * its faces sets them alike on every canvas. */+ emAscent?(cssFont: string): number | undefined;+}++/**+ * How far under a line's middle the line's baseline sits, px, for the font+ * set on `ctx` at `px`.+ *+ * A line is set from the middle of its face's em box, as Chrome draws+ * textBaseline "middle": its OS/2 typo ascent and descent, scaled to fill the+ * em. Other canvases read "middle" off other boxes — skia off the line box a+ * CSS line centres, which on Jost at 110 px is 12 px lower — so the painter+ * draws from the alphabetic baseline and drops each line by this much itself.+ * Given the face's `emAscent` the drop is the em box's own; a face nobody can+ * read falls back on this canvas's "middle", read as the gap between the two+ * alignments' ascents.+ */+export function baselineDrop(ctx: CanvasRenderingContext2D, px: number, emAscent?: number): number {+ if (emAscent !== undefined && Number.isFinite(emAscent)) return px * (emAscent - 0.5);+ const was = ctx.textBaseline;+ ctx.textBaseline = "alphabetic";+ const alphabetic = ctx.measureText("Hg").actualBoundingBoxAscent;+ ctx.textBaseline = "middle";+ const middle = ctx.measureText("Hg").actualBoundingBoxAscent;+ ctx.textBaseline = was;+ return Number.isFinite(alphabetic) && Number.isFinite(middle) && alphabetic > middle+ ? alphabetic - middle+ : px * WORD_BASELINE_DROP;}/** The frame geometry handed to painters. `scale` converts design px (1080@@ -579,7 +610,9 @@ async function paintText(ctx.letterSpacing = `${(overlay.letterSpacing ?? 0) * fpx}px`;}ctx.textAlign = "center";- ctx.textBaseline = "middle";+ ctx.textBaseline = "alphabetic";+ // Each line is drawn on its baseline, this far under the line's middle.+ const drop = baselineDrop(ctx, fpx, env.emAscent?.(cssFont));// Words stay in the picture: a line wider than the room its anchor leaves// is broken here, measured on the very context that is about to draw it.@@ -787,11 +820,6 @@ async function paintText(// as the preview's bar.const caretColor = phase?.caret ? (typeCaret(overlay.anim)?.color ?? overlay.color) : null;const alphaOf = ctx.globalAlpha;- // How far the type's top sits over a baseline, in the current font.- const ascentAt = (baseline: CanvasTextBaseline) => {- ctx.textBaseline = baseline;- return ctx.measureText("Hg").actualBoundingBoxAscent;- };// A bar after a typed letter hugs its ink: the tracking a browser counts// after a run's last letter (skia leaves it out) comes back off `right`.const drawCaret = (right: number, line = lines.length - 1, hug = false) => {@@ -803,11 +831,8 @@ async function paintText(const track = (overlay.letterSpacing ?? 0) * fpx;right -= track - (ctx.measureText("II").width - 2 * ctx.measureText("I").width);}- const base = ascentAt("alphabetic");const xh = ctx.measureText("x").actualBoundingBoxAscent;- const mid = ascentAt("middle");- const off =- Number.isFinite(base) && Number.isFinite(mid) && Number.isFinite(xh) ? base - mid - xh / 2 : 0;+ const off = Number.isFinite(xh) ? drop - xh / 2 : 0;ctx.shadowColor = "transparent";ctx.globalAlpha = alphaOf;ctx.fillStyle = caretColor;@@ -859,8 +884,8 @@ async function paintText(}if (g.split) {const off = g.split * scale;- drawTinted(ch, -off, 0, SPLIT_RED);- drawTinted(ch, off, 0, SPLIT_CYAN);+ drawTinted(ch, -off, drop, SPLIT_RED);+ drawTinted(ch, off, drop, SPLIT_CYAN);}if (g.roll) {// A rolling reel shows through its own line band only; sideways@@ -871,10 +896,10 @@ async function paintText(const a = ctx.globalAlpha;for (const r of slotReel(ch, gi - 1, g.roll, slotSeed(overlay.text))) {ctx.globalAlpha = a * r.alpha;- drawText(r.ch, 0, r.y * lineH);+ drawText(r.ch, 0, r.y * lineH + drop);}} else {- drawText(ch, 0, 0);+ drawText(ch, 0, drop);}if (texture) {cutTexture(ctx, texture, surface, -w / 2, -lineH / 2, w, lineH);@@ -901,7 +926,7 @@ async function paintText(const chars = [...line];const n = shownIn(i);if (n >= chars.length) {- drawText(line, lineX(i), y);+ drawText(line, lineX(i), y + drop);return;}@@ -916,27 +941,16 @@ async function paintText(return;}ctx.textAlign = "left";- drawText(head, left, y);+ drawText(head, left, y + drop);ctx.textAlign = "center";});drawCaret(caretRight, typingLine, shownIn(typingLine) > 0);return;}- // A word drawn at another size shares the baseline with the words beside it,- // so where that baseline sits under the middle this painter draws from has to- // be read off the face. Measuring the same string against both alignments- // gives the gap exactly, in whatever engine is drawing: the ascent is- // reported from the alignment point, so the difference between the two is the- // distance between them.- const alphabetic = ascentAt("alphabetic");- const middle = ascentAt("middle");- const drop =- Number.isFinite(alphabetic) && Number.isFinite(middle) && alphabetic > middle- ? alphabetic - middle- : fpx * WORD_BASELINE_DROP;- // A word in its own face sits on the line's baseline too, and that face- // puts its baseline its own distance below its middle: read once per face,- // at the line's size, and scaled with the word.+ // A word drawn at another size or in its own face stands on the line's+ // baseline, so its middle — where its marks and its turn are centred — sits+ // its own face's drop above that baseline: read once per face, at the+ // line's size, and scaled with the word.const drops = new Map<string, number>();const dropOf = (d: WordDraw) => {const face = faceKey(d);@@ -944,15 +958,12 @@ async function paintText(let v = drops.get(face);if (v === undefined) {setSize(1, d);- const a = ascentAt("alphabetic");- const m = ascentAt("middle");- v = Number.isFinite(a) && Number.isFinite(m) && a > m ? a - m : drop;+ v = baselineDrop(ctx, fpx, env.emAscent?.(fontFor(1, d)));drops.set(face, v);setSize(1);}return v;};- ctx.textBaseline = "middle";const alpha0 = ctx.globalAlpha;let k = 0;let caretRight = cx - (runs[typingLine]?.lineW ?? 0) / 2;@@ -989,12 +1000,11 @@ async function paintText(// A word its effect has not brought in yet has no pixels; it still holds// its place, so the words beside it stay where they will be.if (alpha > 0.001 && shown) {- // Every word draws from its own middle; a resized one has that middle- // lifted or dropped by exactly as much as sharing the line's baseline- // asks for.+ // Every word draws on the line's baseline; a resized one has its+ // middle lifted or dropped by exactly as much as sharing it asks for.const size = fpx * mult;- const mid = y + drop - dropOf(d) * mult;- const drawY = mid;+ const drawY = y + drop;+ const mid = drawY - dropOf(d) * mult;// Everything below rides one saved state, so nothing set for this word// reaches the next.ctx.save();diff --git a/site/packages/effects-kit/src/textBaseline.test.ts b/site/packages/effects-kit/src/textBaseline.test.tsnew file mode 100644index 00000000..90e983f6--- /dev/null+++ b/site/packages/effects-kit/src/textBaseline.test.ts@@ -0,0 +1,84 @@+import { expect, test } from "bun:test";+import { Canvas, FontLibrary } from "skia-canvas";+import { paintElement, type PaintPhase, type RenderEnv } from "./render";+import type { TextOverlay } from "./types";+import { wordDrawsAt } from "./words";++/**+ * Every line is set from its face's em box, whatever the canvas makes of+ * textBaseline "middle".+ *+ * Chrome draws "middle" at the em box's middle; skia draws it at the middle of+ * the line box a CSS line centres. On Jost, whose em box sits low in a tall+ * line box, the two part by 12 px at 110 px. Jost's OS/2 typo ascender and+ * descender, 1070 and -375, put 1070/1445 of the em above the baseline, so+ * Chrome's baseline sits 110 × (0.7405 − 0.5) = 26.45 px under a line's+ * middle (its canvas reads 26.453125).+ */++/** skia's font registry, under a name the hook lint leaves alone. */+const fontLibrary = FontLibrary;+fontLibrary.use("Jost", [+ new URL("../../../node_modules/@fontsource/jost/files/jost-latin-700-normal.woff2", import.meta.url).pathname,+]);+const JOST_EM = 1070 / 1445;+const CHROME_DROP = 26.453125;++const frame = { width: 1080, height: 1920, scale: 1 };+const env: RenderEnv = {+ fontStack: () => '"Jost"',+ createCanvas: (w, h) => new Canvas(w, h) as unknown as HTMLCanvasElement,+ emAscent: (css) => (css.includes("Jost") ? JOST_EM : undefined),+};+const title: TextOverlay = {+ id: "t",+ text: "HIH",+ start: 0,+ end: 2,+ x: 0.5,+ y: 0.5,+ size: 110,+ font: "jost",+ weight: 700,+ color: "#FFFFFF",+ plate: false,+ shadow: false,+};++/** The row under a title's last row of ink: its baseline, set in capitals. */+async function baseline(o: TextOverlay, phase?: PaintPhase, host = env): Promise<number> {+ const canvas = new Canvas(frame.width, frame.height);+ const ctx = canvas.getContext("2d") as unknown as CanvasRenderingContext2D;+ await paintElement(ctx, o, { ...frame, phase }, host);+ const data = ctx.getImageData(0, 0, frame.width, frame.height).data;+ for (let y = frame.height - 1; y >= 0; y--) {+ for (let x = 0; x < frame.width; x++) if (data[(y * frame.width + x) * 4 + 3] > 127) return y + 1;+ }+ return -1;+}++const middle = frame.height / 2;++test("a line stands on the em box's baseline, not the canvas's middle", async () => {+ expect(Math.abs((await baseline(title)) - (middle + CHROME_DROP))).toBeLessThanOrEqual(1);+});++test("a word effect and a letter-by-letter ramp stand on the same baseline", async () => {+ const words = wordDrawsAt(title.text, { style: "color", color: "#FF0000" }, title.color, 2, 0.5);+ expect(Math.abs((await baseline({ ...title, wordDraw: words })) - (middle + CHROME_DROP))).toBeLessThanOrEqual(1);+ const ramp: PaintPhase = { glyphs: { style: "fade", p: 1, exiting: false } };+ expect(Math.abs((await baseline(title, ramp)) - (middle + CHROME_DROP))).toBeLessThanOrEqual(1);+});++test("a face the host cannot read is set from the canvas's own middle", async () => {+ // Without an em box the painter falls back on what this canvas reads for+ // "middle": the gap between the two alignments' ascents.+ const ctx = new Canvas(8, 8).getContext("2d");+ ctx.font = '700 110px "Jost"';+ ctx.textBaseline = "alphabetic";+ const alphabetic = ctx.measureText("Hg").actualBoundingBoxAscent;+ ctx.textBaseline = "middle";+ const own = alphabetic - ctx.measureText("Hg").actualBoundingBoxAscent;+ const unread = await baseline(title, undefined, { ...env, emAscent: () => undefined });+ expect(Math.abs(unread - (middle + own))).toBeLessThanOrEqual(1);+});diff --git a/site/scripts/stage-cut-fonts.mjs b/site/scripts/stage-cut-fonts.mjsindex 621148ce..328a6321 100644--- a/site/scripts/stage-cut-fonts.mjs+++ b/site/scripts/stage-cut-fonts.mjs@@ -3,7 +3,8 @@// each weight is copied here from its @fontsource package, and the manifest// records each face's subset and unicode-range: the page adds one lazy// FontFace per face (googleFonts.ts), and a render worker hands skia the latin-// files (lib/headless/nodeFonts.ts).+// files (lib/headless/nodeFonts.ts). It also records each face's em box, which+// is where every canvas sets the face's lines (lib/fontEm.ts).//// The alias-only families cover the base system set (SF Pro, New York,// Impact…). Those live on a Mac and nowhere else, so a Linux container would@@ -16,6 +17,7 @@ import { copyFileSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "noimport { createRequire } from "node:module";import path from "node:path";import { fileURLToPath } from "node:url";+import { brotliDecompressSync } from "node:zlib";const root = path.dirname(path.dirname(fileURLToPath(import.meta.url)));const dest = path.join(root, "public", "cut-fonts");@@ -48,6 +50,67 @@ function readFaces(pkgDir, pkg, weight, style) {return faces;}+/** The WOFF2 known-table tags read here, by the index a table directory+ * entry names; glyf and loca are 10 and 11. */+const WOFF2_TAGS = ["cmap", "head", "hhea", "hmtx", "maxp", "name", "OS/2", "post"];++/** A WOFF2 file's untransformed tables by tag. The vertical metrics tables+ * are never transformed, and they come before glyf in the stream. */+function woff2Tables(buf) {+ const count = buf.readUInt16BE(12);+ const packed = buf.readUInt32BE(20);+ let p = 48;+ const base128 = () => {+ let v = 0;+ for (let i = 0; i < 5; i++) {+ const b = buf[p++];+ v = v * 128 + (b & 0x7f);+ if (!(b & 0x80)) return v;+ }+ return v;+ };+ const dir = [];+ for (let i = 0; i < count; i++) {+ const flags = buf[p++];+ const known = flags & 0x3f;+ let tag = WOFF2_TAGS[known];+ if (known === 63) {+ tag = buf.toString("latin1", p, p + 4);+ p += 4;+ }+ const version = flags >> 6;+ let length = base128();+ // glyf and loca are transformed at version 0, everything else at not-0.+ if ((known === 10 || known === 11) === (version === 0)) length = base128();+ dir.push({ tag, length });+ }+ const data = brotliDecompressSync(buf.subarray(p, p + packed));+ const tables = {};+ let at = 0;+ for (const { tag, length } of dir) {+ if (tag) tables[tag] = data.subarray(at, at + length);+ at += length;+ }+ return tables;+}++/** A face's em box: the share of the em above the baseline. A canvas sets a+ * line's baseline under the line's middle by the em box's own offset, and+ * Chrome takes the box from OS/2's typo ascender and descender, scaled to+ * fill the em; a face without them falls back to its line ascent and+ * descent. Example: Jost's 1070 and -375 put 0.7405 of the em above. */+function emAscent(file) {+ const t = woff2Tables(readFileSync(file));+ const os2 = t["OS/2"];+ let up = os2 && os2.length >= 72 ? os2.readInt16BE(68) : 0;+ let down = os2 && os2.length >= 72 ? -os2.readInt16BE(70) : 0;+ if (!up && !down) {+ up = t.hhea.readInt16BE(4);+ down = -t.hhea.readInt16BE(6);+ }+ return up + down > 0 ? Math.round((up / (up + down)) * 1e5) / 1e5 : 0.8;+}+rmSync(dest, { recursive: true, force: true });mkdirSync(dest, { recursive: true });const manifest = [];@@ -66,7 +129,7 @@ for (const entry of FAMILIES) {for (const { weight, style } of styled) {for (const { file, subset, range } of readFaces(pkgDir, pkg, weight, style)) {copyFileSync(path.join(pkgDir, "files", file), path.join(dest, file));- faces.push({ file, weight, style, subset });+ faces.push({ file, weight, style, subset, em: emAscent(path.join(dest, file)) });if (range) {ranges[subset] = range;}diff --git a/site/src/cut/lib/fontEm.ts b/site/src/cut/lib/fontEm.tsnew file mode 100644index 00000000..e2699bcc--- /dev/null+++ b/site/src/cut/lib/fontEm.ts@@ -0,0 +1,71 @@+import manifest from "../../../public/cut-fonts/manifest.json";++/**+ * Each staged face's em box: the share of the em above its baseline, read off+ * the face's own OS/2 table when the fonts were staged+ * (scripts/stage-cut-fonts.mjs).+ *+ * A title's lines are set from the em box's middle (the kit's+ * `baselineDrop`). Chrome reads that box itself when it draws textBaseline+ * "middle"; skia and Safari read other boxes, so the box comes from here and+ * every canvas sets a face's lines where Chrome does. A face that is not+ * staged — a system face in the page, an uploaded font — is not known here,+ * and the painter reads its canvas instead.+ */++interface FaceEm {+ weight: number;+ italic: boolean;+ em: number;+}++/** One staged family's faces as the manifest lists them. */+interface StagedFamily {+ family: string;+ faces: { weight: number; style: string; em?: number }[];+}++const byFamily = new Map<string, FaceEm[]>();+for (const entry of manifest as StagedFamily[]) {+ const faces = entry.faces.flatMap((f) =>+ f.em === undefined ? [] : [{ weight: f.weight, italic: f.style === "italic", em: f.em }]+ );+ if (faces.length) byFamily.set(entry.family.toLowerCase(), faces);+}++/** Each css font's reading, by css font. */+const read = new Map<string, number | undefined>();++/** Answer for `alias` with a staged family's faces: a headless render draws+ * the system names the font stacks ask for in a stand-in, so the stand-in's+ * em box is the one it sets. */+export function aliasFaceEm(alias: string, family: string): void {+ const faces = byFamily.get(family.toLowerCase());+ if (!faces) return;+ byFamily.set(alias.toLowerCase(), faces);+ read.clear();+}++/**+ * The share of the em above the baseline for the face a css font draws in,+ * as the kit painter's `emAscent` takes it; undefined when its first family+ * is not a staged one. A weight the family does not stage reads the nearest+ * one it does, as a browser matches it. Example: '700 110px "Jost"' → 0.74048.+ */+export function faceEmAscent(cssFont: string): number | undefined {+ if (read.has(cssFont)) return read.get(cssFont);+ const m = /^(?:(italic|oblique)\s+)?(?:normal\s+)?(?:(\d+)\s+)?[\d.]+px\s+(.+)$/.exec(cssFont.trim());+ let em: number | undefined;+ const faces = m ? byFamily.get(m[3].split(",")[0].trim().replace(/^["']|["']$/g, "").toLowerCase()) : undefined;+ if (m && faces) {+ const italic = !!m[1];+ const weight = Number(m[2] ?? 400);+ const slant = faces.filter((f) => f.italic === italic);+ const pool = slant.length ? slant : faces;+ em = pool.reduce((a, b) => (Math.abs(b.weight - weight) < Math.abs(a.weight - weight) ? b : a)).em;+ }+ // Bounded: every stage size is a font string of its own.+ if (read.size > 500) read.clear();+ read.set(cssFont, em);+ return em;+}diff --git a/site/src/cut/lib/fontMatch.ts b/site/src/cut/lib/fontMatch.tsindex 3a01a752..6767d72b 100644--- a/site/src/cut/lib/fontMatch.ts+++ b/site/src/cut/lib/fontMatch.ts@@ -11,8 +11,9 @@* rank alike.*/-import { ensureFontLoaded, LETTER_SPACING_MAX, LETTER_SPACING_MIN, LINE_HEIGHT, TEXT_WEIGHTS, type TextWeight } from "@donkeycut/effects-kit";+import { baselineDrop, ensureFontLoaded, LETTER_SPACING_MAX, LETTER_SPACING_MIN, LINE_HEIGHT, TEXT_WEIGHTS, type TextWeight } from "@donkeycut/effects-kit";import { GOOGLE_FONTS } from "./fontCatalog";+import { faceEmAscent } from "./fontEm";import { createRasterCanvas, type RasterSurface } from "./raster";import { allFonts, FONTS, hasFont } from "./types";@@ -165,10 +166,11 @@ export async function rankFaces(}/** How far a title's ink centre sits from the point it is set at, px, the- * type upright: each line centred on x and stacked about y on the middle- * baseline, as the kit draws it. Caps stand above that baseline, and a- * browser counts tracking after the last letter, so the ink is off the- * point; a title set at the reference ink's centre less this lands on it. */+ * type upright: each line centred on x and stacked about y, its baseline+ * under the line's middle by the face's em box, as the kit draws it. Caps+ * stand above that baseline, and a browser counts tracking after the last+ * letter, so the ink is off the point; a title set at the reference ink's+ * centre less this lands on it. */export async function inkOffset(face: TitleFace,px: number,@@ -190,8 +192,9 @@ export async function inkOffset(if ("letterSpacing" in ctx) ctx.letterSpacing = `${tracking * px}px`;ctx.fillStyle = "#ffffff";ctx.textAlign = "center";- ctx.textBaseline = "middle";- lines.forEach((line, i) => ctx.fillText(line, w / 2, h / 2 - (lineH * lines.length) / 2 + lineH * (i + 0.5)));+ ctx.textBaseline = "alphabetic";+ const drop = baselineDrop(ctx, px, faceEmAscent(css));+ lines.forEach((line, i) => ctx.fillText(line, w / 2, h / 2 - (lineH * lines.length) / 2 + lineH * (i + 0.5) + drop));const data = ctx.getImageData(0, 0, w, h).data;const ink = new Uint8Array(w * h);for (let i = 0; i < ink.length; i++) ink[i] = data[i * 4 + 3] > 127 ? 1 : 0;diff --git a/site/src/cut/lib/headless/nodeFonts.ts b/site/src/cut/lib/headless/nodeFonts.tsindex e80df8f0..fbb8008e 100644--- a/site/src/cut/lib/headless/nodeFonts.ts+++ b/site/src/cut/lib/headless/nodeFonts.ts@@ -1,4 +1,5 @@import { setFontInstaller } from "../fontAssets";+import { aliasFaceEm } from "../fontEm";import { forgetTextWidths } from "../textFit";import { registerFonts } from "../types";@@ -58,7 +59,10 @@ export async function installHeadlessFonts(): Promise<number> {const files = entry.files.map((f) => path.join(dir, f));try {skia.FontLibrary.use(entry.family, files);- for (const alias of entry.aliases ?? []) skia.FontLibrary.use(alias, files);+ for (const alias of entry.aliases ?? []) {+ skia.FontLibrary.use(alias, files);+ aliasFaceEm(alias, entry.family);+ }} catch {continue; // one unreadable face costs that font alone}diff --git a/site/src/cut/lib/suggestFontTool.test.ts b/site/src/cut/lib/suggestFontTool.test.tsindex 290146ff..7aa64c71 100644--- a/site/src/cut/lib/suggestFontTool.test.ts+++ b/site/src/cut/lib/suggestFontTool.test.ts@@ -3,6 +3,8 @@ import { Canvas, FontLibrary } from "skia-canvas";/** skia's font registry, under a name the hook lint leaves alone. */const fontLibrary = FontLibrary;+import { baselineDrop } from "@donkeycut/effects-kit";+import { faceEmAscent } from "./fontEm";import { CUT_FONT_MATCH } from "./fontMatchSettings";import { stubModule } from "@/lib/testing/stubModule";import { setRasterFactory, type RasterFactory, type RasterSurface } from "./raster";@@ -28,11 +30,14 @@ async function frameAt(_src: string | Blob, time: number) {ctx.font = shows.css;ctx.fillStyle = "#ffffff";ctx.textAlign = "center";- ctx.textBaseline = "middle";+ // Set as a title is set at `at`: on the baseline its em box puts under the+ // line's middle.+ ctx.textBaseline = "alphabetic";+ const drop = baselineDrop(ctx as unknown as CanvasRenderingContext2D, Number(/(\d+)px/.exec(shows.css)![1]), faceEmAscent(shows.css));const [ax, ay] = shows.at ?? [960, 540];ctx.translate(ax, ay);ctx.rotate(((shows.rotate ?? 0) * Math.PI) / 180);- ctx.fillText(shows.text, 0, 0);+ ctx.fillText(shows.text, 0, drop);return { canvas: c, timestamp: time, duration: 1 / 30 } as never;}await stubModule<typeof import("./mediaRead")>("./mediaRead", import.meta.url, { frameAt });diff --git a/site/src/cut/lib/textFit.ts b/site/src/cut/lib/textFit.tsindex 90177705..731bb738 100644--- a/site/src/cut/lib/textFit.ts+++ b/site/src/cut/lib/textFit.ts@@ -1,6 +1,7 @@"use client";-import { cycleFaceAt as faceShowingAt, forgetSettledFaces, overlayFontCycle, settleCycleFace, textWrapRoom, withFontCycle, wrapTextToRoom, LINE_HEIGHT, PLATE_PAD_X, PLATE_PAD_Y, type CycleFace } from "@donkeycut/effects-kit";+import { baselineDrop, cycleFaceAt as faceShowingAt, forgetSettledFaces, overlayFontCycle, settleCycleFace, textWrapRoom, withFontCycle, wrapTextToRoom, LINE_HEIGHT, PLATE_PAD_X, PLATE_PAD_Y, type CycleFace } from "@donkeycut/effects-kit";+import { faceEmAscent } from "./fontEm";import { createRasterCanvas } from "./raster";import { fontStack, type Overlay, type TextOverlay } from "./types";@@ -99,12 +100,12 @@ export function measureLine(** A CSS line box centres the face's ascent and descent in its height, so the* baseline sits half their difference under the line's middle. The painter- * draws each line from textBaseline "middle", and how far its baseline sits- * under that is the canvas's own reading of the face. On a face with a tall- * ascent the two part by a tenth of an em: Jost at 110 px sits 12 px lower in- * the DOM than the export draws it. The preview lifts its type by this much.- * Both gaps are read off the canvas, so the lift is whatever this browser- * draws; a canvas that cannot report the face's line metrics lifts nothing.+ * sets each line's baseline under its middle by the face's em box+ * (`baselineDrop`), the same on every canvas. On a face with a tall ascent the+ * two part by a tenth of an em: Jost at 110 px sits 12 px lower in the DOM+ * than the export draws it. The preview lifts its type by this much. The line+ * box is read off this browser's canvas, the one the DOM's lines are set by;+ * a canvas that cannot report the face's line metrics lifts nothing.*/export function textLineLift(cssFont: string): number {const hit = lifts.get(cssFont);@@ -116,11 +117,9 @@ export function textLineLift(cssFont: string): number {const was = ctx.textBaseline;ctx.textBaseline = "alphabetic";const line = ctx.measureText("Hg");- ctx.textBaseline = "middle";- const middle = ctx.measureText("Hg").actualBoundingBoxAscent;- ctx.textBaseline = was;const cssDrop = (line.fontBoundingBoxAscent - line.fontBoundingBoxDescent) / 2;- const painterDrop = line.actualBoundingBoxAscent - middle;+ const painterDrop = baselineDrop(ctx, Number(/([\d.]+)px/.exec(cssFont)?.[1]), faceEmAscent(cssFont));+ ctx.textBaseline = was;if (Number.isFinite(cssDrop) && Number.isFinite(painterDrop)) lift = cssDrop - painterDrop;}// Bounded: every stage size is a size of its own.diff --git a/site/src/cut/lib/textLift.test.ts b/site/src/cut/lib/textLift.test.tsindex 201af66b..79fe3da2 100644--- a/site/src/cut/lib/textLift.test.ts+++ b/site/src/cut/lib/textLift.test.ts@@ -3,9 +3,10 @@ import { setRasterFactory, type RasterFactory } from "./raster";import { textLineLift } from "./textFit";// The preview sets a title's lines in CSS line boxes; the export's painter-// sets them from textBaseline "middle". The two put the baseline in different-// places under a line's middle, so a Jost title turned -90 in five columns-// showed in the preview 12 px to the right of where the export drew it.+// sets them from the face's em box, where Chrome draws textBaseline "middle".+// The two put the baseline in different places under a line's middle, so a+// Jost title turned -90 in five columns showed in the preview 12 px to the+// right of where the export drew it.//// Chrome's own numbers for Jost 700 at 110 px, read off its canvas: the// face's line ascent and descent (what a CSS line box centres), and the@@ -36,15 +37,21 @@ test("a title's lines sit where the painter sets them", () => {// A CSS line box centres the face's ascent and descent, which leaves the// baseline half their difference under the line's middle.const cssDrop = (JOST_110.ascent - JOST_110.descent) / 2;- // The painter's baseline sits under its middle by the gap between the two- // alignments' ascents.- const painterDrop = JOST_110.alphabetic - JOST_110.middle;+ // The painter sets Jost from its em box, which is where Chrome's "middle"+ // puts it: under its middle by the gap between the two alignments' ascents.+ const chromeDrop = JOST_110.alphabetic - JOST_110.middle;const lift = textLineLift('700 110px "Jost"');- expect(cssDrop - lift).toBeCloseTo(painterDrop, 6);+ expect(cssDrop - lift).toBeCloseTo(chromeDrop, 2);// The offset the turned columns showed on a 1920 frame.expect(lift).toBeCloseTo(12.05, 2);});+test("a face with no staged em box is lifted by the canvas's own middle", () => {+ // A system face in the page: the painter reads Chrome's "middle" for it.+ const lift = textLineLift('700 110px -apple-system, "SF Pro Display", sans-serif');+ expect((JOST_110.ascent - JOST_110.descent) / 2 - lift).toBeCloseTo(JOST_110.alphabetic - JOST_110.middle, 6);+});+test("a canvas that cannot read a face's line metrics lifts nothing", () => {reading.lineMetrics = false;expect(textLineLift('700 109px "Jost"')).toBe(0);diff --git a/site/src/cut/lib/textRender.ts b/site/src/cut/lib/textRender.tsindex 5a60364a..36c6970f 100644--- a/site/src/cut/lib/textRender.ts+++ b/site/src/cut/lib/textRender.ts@@ -15,6 +15,7 @@ import {type WordDraw,} from "@donkeycut/effects-kit";import type { CSSProperties } from "react";+import { faceEmAscent } from "./fontEm";import { allowance, holdMemory } from "./memoryBudget";import { createRasterCanvas, decodeRasterImage, rasterCanvasToPng } from "./raster";import { fontStack, type MediaAsset, type Overlay } from "./types";@@ -148,12 +149,13 @@ async function decodeSticker(asset: MediaAsset): Promise<StickerImage | null> {}}-/** The kit render env bound to this app: Cut's font stacks, and sticker- * assets resolved from the given project media (their URLs already point at- * the active backend — local engine or signed R2). */+/** The kit render env bound to this app: Cut's font stacks and their em+ * boxes, and sticker assets resolved from the given project media (their URLs+ * already point at the active backend — local engine or signed R2). */export function cutRenderEnv(assets: MediaAsset[]): RenderEnv {return {fontStack,+ emAscent: faceEmAscent,createCanvas: (w, h) => createRasterCanvas(w, h) as HTMLCanvasElement,canvasToPngBlob: (canvas) => rasterCanvasToPng(canvas),resolveLottie: (assetId) =>diff --git a/site/src/cut/lib/titleBaseline.test.ts b/site/src/cut/lib/titleBaseline.test.tsnew file mode 100644index 00000000..e1b195d2--- /dev/null+++ b/site/src/cut/lib/titleBaseline.test.ts@@ -0,0 +1,104 @@+import { beforeAll, expect, test } from "bun:test";+import { FontLibrary } from "skia-canvas";+import { installSkiaRaster } from "./headless/skiaRaster";+import { renderProjectFrame } from "./exportRender";+import { inkOffset } from "./fontMatch";+import type { ExportDoc } from "./renderSnapshot";+import { fontStack, registerFonts, type TextOverlay } from "./types";++/** skia's font registry, under a name the hook lint leaves alone. */+const fontLibrary = FontLibrary;++/**+ * A headless export sets a title's lines where the in-tab export does.+ *+ * Each line is drawn about its middle, and where a face's baseline sits under+ * that middle used to be the canvas's own reading of textBaseline "middle".+ * Chrome reads it off the face's em box; skia reads it off the line box a CSS+ * line centres, so the same Jost title stood 12 px apart in the two exports.+ *+ * Chrome's numbers at 110 px, read off its canvas: how far under the line's+ * middle it draws each face's baseline.+ */+const CHROME_110: [font: string, weight: number, drop: number][] = [+ ["jost", 700, 26.453125],+ // A face whose em box is not its line box: Chrome sets Prompt by the em+ // box, which its line box overhangs at both ends.+ ["prompt", 900, 27.5],+ // skia reads Poppins' line box off its Windows metrics.+ ["poppins", 700, 27.5],+];++const size = { width: 1080, height: 1920 };++// The staged faces, as a headless render loads them. Only these: skia's+// registry outlives the file, and the stand-ins would answer for Arial.+const FACES: [id: string, family: string, file: string][] = [+ ["jost", "Jost", "jost-latin-700-normal.woff2"],+ ["prompt", "Prompt", "prompt-latin-900-normal.woff2"],+ ["poppins", "Poppins", "poppins-latin-700-normal.woff2"],+];++beforeAll(async () => {+ expect(await installSkiaRaster()).toBe(true);+ for (const [id, family, file] of FACES) {+ fontLibrary.use(family, [new URL(`../../../public/cut-fonts/${file}`, import.meta.url).pathname]);+ registerFonts([{ id, label: family, stack: `"${family}"` }]);+ }+});++const title = (font: string, weight: number): TextOverlay => ({+ id: "t",+ text: "HIH",+ start: 0,+ end: 3,+ x: 0.5,+ y: 0.5,+ size: 110,+ font,+ weight: weight as TextOverlay["weight"],+ color: "#ffffff",+ plate: false,+ shadow: false,+});++/** The title's ink rows through the export frame: its top and the row under+ * its last, which is the baseline for capitals. */+async function inkRows(o: TextOverlay): Promise<{ top: number; base: number }> {+ const doc: ExportDoc = {+ aspect: "9:16", assets: [], clips: [], audioClips: [], overlays: [o],+ subtitles: { cues: [], showOnVideo: false, showOnTimeline: false },+ };+ const frame = await renderProjectFrame(doc, 1, size, () => "");+ const data = (frame.getContext("2d") as CanvasRenderingContext2D).getImageData(0, 0, size.width, size.height).data;+ frame.width = frame.height = 1;+ let top = -1;+ let base = -1;+ for (let y = 0; y < size.height; y++) {+ for (let x = 400; x < 680; x++) {+ const i = (y * size.width + x) * 4;+ if (data[i] + data[i + 1] + data[i + 2] > 3 * 200) {+ if (top < 0) top = y;+ base = y + 1;+ break;+ }+ }+ }+ return { top, base };+}++for (const [font, weight, drop] of CHROME_110) {+ test(`a ${font} title sits on the baseline Chrome draws it on`, async () => {+ const { base } = await inkRows(title(font, weight));+ expect(Math.abs(base - (size.height / 2 + drop))).toBeLessThanOrEqual(1);+ });+}++test("suggest_font reads a face's ink where the export draws it", async () => {+ // A title set at the reference's ink centre less this offset lands its ink+ // on the reference's, so it has to be the offset the export draws.+ const { top, base } = await inkRows(title("jost", 700));+ const off = await inkOffset({ font: "jost", weight: 700 }, 110, 0, "HIH", fontStack);+ expect(off).not.toBeNull();+ expect(Math.abs(off!.dy - ((top + base) / 2 - size.height / 2))).toBeLessThanOrEqual(1);+});