Open source CapCut alternative.
TypeScript
feat(site): read turned and small type in suggest_font [rebuild]
David Le0afb3eb5a54b
diff --git a/site/package-lock.json b/site/package-lock.jsonindex 5b01182a..54d01f5c 100644--- a/site/package-lock.json+++ b/site/package-lock.json@@ -45,6 +45,7 @@"@fontsource/inter": "^5.3.0","@fontsource/jost": "^5.3.0","@fontsource/kaushan-script": "^5.3.0",+ "@fontsource/libre-caslon-condensed": "^5.3.1","@fontsource/lobster": "^5.3.0","@fontsource/montserrat": "^5.3.0","@fontsource/noto-serif": "^5.3.0",@@ -2317,6 +2318,15 @@"url": "https://github.com/sponsors/ayuhito"}},+ "node_modules/@fontsource/libre-caslon-condensed": {+ "version": "5.3.1",+ "resolved": "https://registry.npmjs.org/@fontsource/libre-caslon-condensed/-/libre-caslon-condensed-5.3.1.tgz",+ "integrity": "sha512-8caze8xreYsU9Odsm2klyRvCkApZNXSHOsEHROuB/34z4ug31gjmATTamzfDWzqGxEbU0Mq+s5qx5/Yc8OxvRw==",+ "license": "OFL-1.1",+ "funding": {+ "url": "https://github.com/sponsors/ayuhito"+ }+ },"node_modules/@fontsource/lobster": {"version": "5.3.0","resolved": "https://registry.npmjs.org/@fontsource/lobster/-/lobster-5.3.0.tgz",diff --git a/site/package.json b/site/package.jsonindex 2177eb5d..2f6eed43 100644--- a/site/package.json+++ b/site/package.json@@ -86,6 +86,7 @@"@fontsource/inter": "^5.3.0","@fontsource/jost": "^5.3.0","@fontsource/kaushan-script": "^5.3.0",+ "@fontsource/libre-caslon-condensed": "^5.3.1","@fontsource/lobster": "^5.3.0","@fontsource/montserrat": "^5.3.0","@fontsource/noto-serif": "^5.3.0",diff --git a/site/src/cut/components/AiPanel.tools.ts b/site/src/cut/components/AiPanel.tools.tsindex 79ffa82d..177ceeab 100644--- a/site/src/cut/components/AiPanel.tools.ts+++ b/site/src/cut/components/AiPanel.tools.ts@@ -304,7 +304,7 @@ export const AI_PANEL_TOOLS = [{name: "suggest_font",description:- "Name the face a reference's type is set in: reads the source's frame at t, lifts the ink inside region off its ground, and ranks every face Donkey can set a title in — each bundled family at each bundled weight, the system set and the account's own fonts — by how closely its glyphs for the same words match, tracking solved so each runs as wide as the reference. Call it for each distinct face while rebuilding a reference's titles, so the font, weight, size and tracking come from the frame. Pass the words exactly as they read, case and line breaks included, and a region close around them read off watch_video at detail original. Returns the best faces with score (0..1), size (px at the 1080 short side, for the same ink height) and tracking (em), plus the ink's box as centre and size in frame fractions — its x/y are add_title's. verdict no_match means nothing bundled is close: the reference uses a face Donkey does not bundle, so say so and name the stand-in you use.",+ "Name the face a reference's type is set in: reads the source's frame at t, lifts the ink inside region off its ground, and ranks every face Donkey can set a title in — each bundled family at each bundled weight, the system set and the account's own fonts — by how closely its glyphs for the same words match, tracking solved so each runs as wide as the reference. Call it for each distinct face while rebuilding a reference's titles, so the font, weight, size and tracking come from the frame. Pass the words exactly as they read, case and line breaks included, and a region close around them read off watch_video at detail original. Returns the best faces with score (0..1), size (px at the 1080 short side, for the same ink height) and tracking (em), plus the ink's box as centre and size in frame fractions — its x/y are add_title's. Type set at an angle — a column word reading bottom to top — is read upright: pass `rotation`, the angle add_title would set it at (-90 reads bottom to top, 90 top to bottom), with a region around the word as it stands; one line of words in a region taller than wide is tried both ways round without it, and `rotation` comes back with the result to set on the title. verdict no_match means nothing bundled is close: the reference uses a face Donkey does not bundle, so say so and name the stand-in you use, of its kind — `kinds` lists every face by kind (serif, sans, script, display, mono), each as its id and a few words on its look. `inkPx` is how tall a line of the ink runs in the source; under 24 the result is `small`: the glyphs have blurred into their proportions, the ranking cannot tell a serif from a condensed sans, so name the face by eye at detail original and take the closest of its kind from `kinds`.",inputSchema: obj({asset_id: str("Project asset id of the reference (video or image)"),@@ -319,6 +319,7 @@ export const AI_PANEL_TOOLS = [["x", "y", "w", "h"]),text: str("The words the region shows, as they read, with a line break where the type breaks"),+ rotation: num("Degrees the type is turned, as add_title's rotation: -90 reads bottom to top, 90 top to bottom (default: upright, or both ways for one line in a tall region)"),count: num("How many faces to return, 1..10 (default 5)"),picture: pictureArg,},diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.tsindex 0fe278f5..67f5a750 100644--- a/site/src/cut/lib/aiTools.ts+++ b/site/src/cut/lib/aiTools.ts@@ -239,6 +239,7 @@ import { renderMix } from "./audioMix";import { measureLoudnessSliced, roundedLoudness, type LoudnessMeasure } from "./loudness";import { frameAt, framesAt, readMediaFileSize } from "./mediaRead";import { bundledFaces, inkMaskOf, rankFaces } from "./fontMatch";+import { fontKind, fontLook } from "./fontCatalog";import { CUT_FONT_MATCH } from "./fontMatchSettings";import { COMPARE_MODES, MAX_COMPARE, SWEEP_TARGET } from "./frameDiff";import { compareMoments, sweepSource } from "./sourceCompare";@@ -742,6 +743,10 @@ const FONT_SUGGESTIONS_MAX = 10;/** The longest side a suggest_font crop is read at, px: type this tall is* already far finer than the grid it is scored on. */const FONT_CROP_MAX = 1600;+/** Ink a line of type runs tall in the source, px, under which its glyphs+ * blur into their proportions: a serif and a condensed sans of the same+ * shape score alike, so the ranking no longer names the face. */+const FONT_SMALL_INK_PX = 24;/** Design px on a frame's short side, the unit a title's size is in. */const DESIGN_SHORT_SIDE = 1080;@@ -2291,15 +2296,58 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const ctx = crop.getContext("2d") as CanvasRenderingContext2D | null;if (!ctx) throw new ToolError("No 2D context to read the frame on.");ctx.drawImage(source, x0, y0, sw, sh, 0, 0, cw, ch);- const ref = inkMaskOf(ctx.getImageData(0, 0, cw, ch).data, cw, ch);++ // Type set at an angle is read upright: the crop turned back by the+ // title's rotation. One line of words in a region taller than wide is+ // a column word, read both ways round when no rotation is given.+ const lines = text.split("\n").filter((l) => l.trim()).length;+ const rotations = isNum(input.rotation)+ ? [input.rotation]+ : lines === 1 && text.trim().length > 1 && ch > cw * 1.3+ ? [-90, 90]+ : [0];+ const reads = [];+ for (const rotation of rotations) {+ const a = (rotation * Math.PI) / 180;+ const [cos, sin] = [Math.abs(Math.cos(a)), Math.abs(Math.sin(a))];+ const uw = Math.max(1, Math.round(cw * cos + ch * sin));+ const uh = Math.max(1, Math.round(cw * sin + ch * cos));+ const upright = rotation === 0 ? crop : createRasterCanvas(uw, uh);+ const uctx = upright.getContext("2d") as CanvasRenderingContext2D | null;+ if (!uctx) throw new ToolError("No 2D context to read the frame on.");+ if (upright !== crop) {+ uctx.translate(uw / 2, uh / 2);+ uctx.rotate(-a);+ uctx.drawImage(crop as CanvasImageSource, -cw / 2, -ch / 2);+ }+ const ref = inkMaskOf(uctx.getImageData(0, 0, upright.width, upright.height).data, upright.width, upright.height);+ const size = { w: upright.width, h: upright.height };+ if (upright !== crop) upright.width = 0;+ if (!ref.box) continue;+ // Every face ranked; the size each would need to fill the ink's height.+ const ranked = await rankFaces(ref, text, bundledFaces(), fontStack).then(+ (r) => r,+ (e) => {+ // A guess at the angle that reads wrong is one of the guesses.+ if (rotations.length > 1) return null;+ throw new ToolError(e instanceof Error ? e.message : "Could not set the faces.", { cause: e });+ }+ );+ if (ranked) reads.push({ rotation, ref, ranked, size });+ }crop.width = 0;- if (!ref.box)+ if (reads.length === 0)throw new ToolError("No type stands out from its ground in that region. Check where the words sit with watch_video at detail original, then pass a region around them.");-- // Every face ranked; the size each would need to fill the ink's height.- const ranked = await rankFaces(ref, text, bundledFaces(), fontStack).catch((e) => {- throw new ToolError(e instanceof Error ? e.message : "Could not set the faces.", { cause: e });- });+ const { rotation, ref, ranked, size: upSize } = reads.reduce((a, b) => ((b.ranked[0]?.score ?? 0) > (a.ranked[0]?.score ?? 0) ? b : a));+ const inkBox = ref.box!;+ // The ink's box, turned back onto the crop: its centre, and the frame-+ // aligned box it fills there.+ const a = (rotation * Math.PI) / 180;+ const [ux, uy] = [inkBox.x + inkBox.w / 2 - upSize.w / 2, inkBox.y + inkBox.h / 2 - upSize.h / 2];+ const inkCx = ux * Math.cos(a) - uy * Math.sin(a) + cw / 2;+ const inkCy = ux * Math.sin(a) + uy * Math.cos(a) + ch / 2;+ const inkW = Math.abs(inkBox.w * Math.cos(a)) + Math.abs(inkBox.h * Math.sin(a));+ const inkH = Math.abs(inkBox.w * Math.sin(a)) + Math.abs(inkBox.h * Math.cos(a));const count = Math.round(clamp(isNum(input.count) ? input.count : FONT_SUGGESTIONS, 1, FONT_SUGGESTIONS_MAX));const labels = new Map(allFonts().map((f) => [f.id, f.label]));const matches = ranked.slice(0, count).map((m) => ({@@ -2314,22 +2362,35 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const good = CUT_FONT_MATCH.goodScore;const best = matches[0];const round3 = (v: number) => Math.round(v * 1000) / 1000;+ // How tall a line of the ink runs in the source's own pixels.+ const inkPx = Math.round(inkBox.h / k / lines);+ const small = inkPx < FONT_SMALL_INK_PX;+ const kinds: Record<string, string[]> = {};+ for (const f of allFonts()) {+ const kind = fontKind(f.id);+ if (kind) (kinds[kind] ??= []).push(`${f.id} — ${fontLook(f.id)}`);+ }return {t: round2(t),text,ink: {- x: round3(((x0 + (ref.box.x + ref.box.w / 2) / k) / fw - left) / area.w),- y: round3(((y0 + (ref.box.y + ref.box.h / 2) / k) / fh - top) / area.h),- w: round3(ref.box.w / k / fw / area.w),- h: round3(ref.box.h / k / fh / area.h),+ x: round3(((x0 + inkCx / k) / fw - left) / area.w),+ y: round3(((y0 + inkCy / k) / fh - top) / area.h),+ w: round3(inkW / k / fw / area.w),+ h: round3(inkH / k / fh / area.h),},+ ...(rotation !== 0 ? { rotation } : {}),...pictureReport(area),- verdict: best && best.score >= good ? "match" : "no_match",+ verdict: best && best.score >= good && !small ? "match" : "no_match",matches,- note:- best && best.score >= good- ? `${best.label} ${best.weight}${best.italic ? " italic" : ""} matches (a match scores ${good}+). Set it with its weight, size (px at the 1080 short side, for the same ink height) and tracking (em); faces scoring near it are close cuts worth a look with compare_to_source.`- : `Nothing bundled matches closely (best ${best?.score ?? 0}; a match scores ${good}+), so the reference is likely set in a face Donkey does not bundle. Tell the user so, name the closest face here as the stand-in you use, and say the Library takes their own font files.`,+ inkPx,+ ...(small ? { small: true } : {}),+ ...(small || !(best && best.score >= good) ? { kinds } : {}),+ note: small+ ? `The type is ${inkPx}px a line in the source, too small for its glyphs to tell faces apart: the ranking above follows its proportions, not its face, and can put a condensed sans over a serif. Name the face from the frame at detail original — serif or sans, its weight, how narrow — and set the closest face of that kind from \`kinds\`, at the size above.`+ : best && best.score >= good+ ? `${best.label} ${best.weight}${best.italic ? " italic" : ""} matches (a match scores ${good}+). Set it with its weight, size (px at the 1080 short side, for the same ink height) and tracking (em)${rotation !== 0 ? `, at rotation ${rotation} as the type stands` : ""}; faces scoring near it are close cuts worth a look with compare_to_source.`+ : `Nothing bundled matches closely (best ${best?.score ?? 0}; a match scores ${good}+), so the reference is likely set in a face Donkey does not bundle. Tell the user so, name the closest face of its kind (\`kinds\`) as the stand-in you use, and say the Library takes their own font files.`,};},diff --git a/site/src/cut/lib/fontCatalog.test.ts b/site/src/cut/lib/fontCatalog.test.tsindex c0c6d8ae..4c02cf93 100644--- a/site/src/cut/lib/fontCatalog.test.ts+++ b/site/src/cut/lib/fontCatalog.test.ts@@ -1,6 +1,6 @@import { expect, test } from "bun:test";import manifest from "../../../public/cut-fonts/manifest.json";-import { GOOGLE_FONTS } from "./fontCatalog";+import { GOOGLE_FONTS, fontKind } from "./fontCatalog";// The catalog's weights are the faces staged for the page, so a font// suggestion never offers a weight the page would draw synthesized.@@ -18,3 +18,14 @@ test("every bundled family lists the weights the page declares", () => {const italics = new Set(GOOGLE_FONTS.flatMap((f) => (f.italics ?? []).map((w) => `${f.id}/${w}`)));expect(italics).toEqual(faces("italic"));});++test("every bundled family says what kind of face it is", () => {+ for (const f of GOOGLE_FONTS) {+ expect(["serif", "sans", "script", "display", "mono"]).toContain(f.kind);+ expect(f.look.length).toBeGreaterThan(3);+ }+ expect(fontKind("libre-caslon-condensed")).toBe("serif");+ // The system set is classed too.+ expect(fontKind("serif")).toBe("serif");+ expect(fontKind("sf")).toBe("sans");+});diff --git a/site/src/cut/lib/fontCatalog.ts b/site/src/cut/lib/fontCatalog.tsindex 698f364f..6af8e6b2 100644--- a/site/src/cut/lib/fontCatalog.ts+++ b/site/src/cut/lib/fontCatalog.ts@@ -11,9 +11,15 @@import fonts from "./fonts.json";+/** What kind of face a family is, for picking a stand-in by eye. */+export type FontKind = "serif" | "sans" | "script" | "display" | "mono";+export interface BundledFont {id: string;label: string;+ kind: FontKind;+ /** A few words on how the face looks, for picking a stand-in by eye. */+ look: string;/** The upright weights bundled. */weights: number[];/** The weights bundled in italic too. */@@ -22,7 +28,28 @@ export interface BundledFont {export const GOOGLE_FONTS: BundledFont[] = fonts.flatMap((f) =>// A family with no id is a headless stand-in, off the menu.- f.id ? [{ id: f.id, label: f.family, weights: f.weights, ...(f.italics ? { italics: f.italics } : {}) }] : []+ f.id+ ? [{ id: f.id, label: f.family, kind: f.kind as FontKind, look: f.look ?? "", weights: f.weights, ...(f.italics ? { italics: f.italics } : {}) }]+ : []);export const GOOGLE_FONT_IDS = GOOGLE_FONTS.map((f) => f.id);++/** The system set's kinds and looks (types.ts FONTS). */+const SYSTEM_FACES: Record<string, { kind: FontKind; look: string }> = {+ sf: { kind: "sans", look: "neutral system grotesque" },+ serif: { kind: "serif", look: "book serif (New York)" },+ rounded: { kind: "sans", look: "rounded system" },+ mono: { kind: "mono", look: "monospace" },+ impact: { kind: "sans", look: "heavy condensed (Impact)" },+};++/** The kind of a bundled or system face; undefined for the account's own. */+export function fontKind(id: string): FontKind | undefined {+ return SYSTEM_FACES[id]?.kind ?? GOOGLE_FONTS.find((f) => f.id === id)?.kind;+}++/** How a bundled or system face looks, in a few words. */+export function fontLook(id: string): string | undefined {+ return SYSTEM_FACES[id]?.look ?? GOOGLE_FONTS.find((f) => f.id === id)?.look;+}diff --git a/site/src/cut/lib/fonts.json b/site/src/cut/lib/fonts.jsonindex e9e11a82..63e7a306 100644--- a/site/src/cut/lib/fonts.json+++ b/site/src/cut/lib/fonts.json@@ -1,54 +1,70 @@[- { "id": "inter", "family": "Inter", "weights": [400, 500, 600, 700] },- { "id": "montserrat", "family": "Montserrat", "weights": [400, 500, 600, 700, 800, 900] },- { "id": "poppins", "family": "Poppins", "weights": [400, 500, 600, 700, 800, 900] },- { "id": "oswald", "family": "Oswald", "weights": [400, 500, 600, 700] },- { "id": "space-grotesk", "family": "Space Grotesk", "weights": [400, 500, 600, 700] },+ { "id": "inter", "kind": "sans", "look": "neutral grotesque", "family": "Inter", "weights": [400, 500, 600, 700] },+ { "id": "montserrat", "kind": "sans", "look": "wide geometric", "family": "Montserrat", "weights": [400, 500, 600, 700, 800, 900] },+ { "id": "poppins", "kind": "sans", "look": "round geometric", "family": "Poppins", "weights": [400, 500, 600, 700, 800, 900] },+ { "id": "oswald", "kind": "sans", "look": "condensed grotesque", "family": "Oswald", "weights": [400, 500, 600, 700] },+ { "id": "space-grotesk", "kind": "sans", "look": "quirky grotesque", "family": "Space Grotesk", "weights": [400, 500, 600, 700] },{"id": "playfair",+ "kind": "serif",+ "look": "wide high-contrast display serif","family": "Playfair Display","weights": [400, 500, 600, 700, 900],"italics": [400, 700, 900],"note": "The italics are the caption emphasis default's face (subtitles.ts)."},- { "id": "caveat", "family": "Caveat", "weights": [400, 500, 600, 700] },- { "id": "bebas", "family": "Bebas Neue", "weights": [400] },- { "id": "anton", "family": "Anton", "weights": [400], "aliases": ["Impact", "Arial Black"] },- { "id": "archivo-black", "family": "Archivo Black", "weights": [400] },- { "id": "bangers", "family": "Bangers", "weights": [400] },- { "id": "lobster", "family": "Lobster", "weights": [400] },- { "id": "pacifico", "family": "Pacifico", "weights": [400] },- { "id": "permanent-marker", "family": "Permanent Marker", "weights": [400] },- { "id": "dm-serif", "family": "DM Serif Display", "weights": [400] },- { "id": "amatic", "family": "Amatic SC", "weights": [400, 700] },+ { "id": "caveat", "kind": "script", "look": "casual handwriting", "family": "Caveat", "weights": [400, 500, 600, 700] },+ { "id": "bebas", "kind": "sans", "look": "tall condensed capitals", "family": "Bebas Neue", "weights": [400] },+ { "id": "anton", "kind": "sans", "look": "heavy condensed", "family": "Anton", "weights": [400], "aliases": ["Impact", "Arial Black"] },+ { "id": "archivo-black", "kind": "sans", "look": "heavy wide grotesque", "family": "Archivo Black", "weights": [400] },+ { "id": "bangers", "kind": "display", "look": "comic-book capitals", "family": "Bangers", "weights": [400] },+ { "id": "lobster", "kind": "script", "look": "bold retro script", "family": "Lobster", "weights": [400] },+ { "id": "pacifico", "kind": "script", "look": "round brush script", "family": "Pacifico", "weights": [400] },+ { "id": "permanent-marker", "kind": "script", "look": "marker lettering", "family": "Permanent Marker", "weights": [400] },+ { "id": "dm-serif", "kind": "serif", "look": "heavy high-contrast display serif", "family": "DM Serif Display", "weights": [400] },+ { "id": "amatic", "kind": "display", "look": "thin hand-drawn condensed capitals", "family": "Amatic SC", "weights": [400, 700] },{"id": "dseg14",+ "kind": "display",+ "look": "segmented LCD digits","family": "DSEG14 Classic","weights": [300, 700],"italics": [300, 700],"note": "Light and Bold only: font matching draws a 400 title in the thin Light LCD face, and 700-900 in Bold."},- { "id": "outfit", "family": "Outfit", "weights": [400, 500, 600, 700] },- { "id": "cinzel", "family": "Cinzel", "weights": [400, 700] },- { "id": "freckle-face", "family": "Freckle Face", "weights": [400] },- { "id": "orbitron", "family": "Orbitron", "weights": [400, 900] },- { "id": "chewy", "family": "Chewy", "weights": [400] },- { "id": "great-vibes", "family": "Great Vibes", "weights": [400] },- { "id": "rubik-dirt", "family": "Rubik Dirt", "weights": [400] },- { "id": "fredoka", "family": "Fredoka", "weights": [500, 600, 700] },- { "id": "oranienbaum", "family": "Oranienbaum", "weights": [400] },- { "id": "kaushan", "family": "Kaushan Script", "weights": [400] },- { "id": "rubik", "family": "Rubik", "weights": [400, 500, 700, 900] },- { "id": "jost", "family": "Jost", "weights": [400, 500, 600, 700] },- { "id": "gloria-hallelujah", "family": "Gloria Hallelujah", "weights": [400] },+ { "id": "outfit", "kind": "sans", "look": "clean geometric", "family": "Outfit", "weights": [400, 500, 600, 700] },+ { "id": "cinzel", "kind": "serif", "look": "classical roman capitals", "family": "Cinzel", "weights": [400, 700] },+ { "id": "freckle-face", "kind": "display", "look": "bouncy playful", "family": "Freckle Face", "weights": [400] },+ { "id": "orbitron", "kind": "display", "look": "square techno", "family": "Orbitron", "weights": [400, 900] },+ { "id": "chewy", "kind": "display", "look": "soft cartoon", "family": "Chewy", "weights": [400] },+ { "id": "great-vibes", "kind": "script", "look": "formal calligraphic script", "family": "Great Vibes", "weights": [400] },+ { "id": "rubik-dirt", "kind": "display", "look": "distressed rounded", "family": "Rubik Dirt", "weights": [400] },+ { "id": "fredoka", "kind": "sans", "look": "rounded friendly", "family": "Fredoka", "weights": [500, 600, 700] },+ { "id": "oranienbaum", "kind": "serif", "look": "condensed high-contrast serif", "family": "Oranienbaum", "weights": [400] },+ { "id": "kaushan", "kind": "script", "look": "brushy casual script", "family": "Kaushan Script", "weights": [400] },+ { "id": "rubik", "kind": "sans", "look": "softly rounded", "family": "Rubik", "weights": [400, 500, 700, 900] },+ { "id": "jost", "kind": "sans", "look": "Futura-style geometric", "family": "Jost", "weights": [400, 500, 600, 700] },+ { "id": "gloria-hallelujah", "kind": "script", "look": "childlike handwriting", "family": "Gloria Hallelujah", "weights": [400] },{"id": "prompt",+ "kind": "sans",+ "look": "black geometric","family": "Prompt","weights": [900],"note": "Black only: the heavy geometric face of a reference's font cycle."},+ {+ "id": "libre-caslon-condensed",+ "kind": "serif",+ "look": "narrow book serif, Times-like",+ "family": "Libre Caslon Condensed",+ "weights": [400],+ "note": "Regular only: the narrow book serif of a lyric template's small centred lines."+ },{"id": "antonio",+ "kind": "sans",+ "look": "tall condensed bold","family": "Antonio","weights": [700],"note": "Bold only: the tall condensed face of a lyric template's big words."diff --git a/site/src/cut/lib/suggestFontTool.test.ts b/site/src/cut/lib/suggestFontTool.test.tsindex 9247307e..40ab6589 100644--- a/site/src/cut/lib/suggestFontTool.test.ts+++ b/site/src/cut/lib/suggestFontTool.test.ts@@ -17,7 +17,7 @@ fontLibrary.use("Anton", [`${FILES}anton/files/anton-latin-400-normal.woff2`]);fontLibrary.use("Great Vibes", [`${FILES}great-vibes/files/great-vibes-latin-400-normal.woff2`]);/** What the reference shows: a 1920×1080 frame with one line of type. */-let shows = { text: "TRAVEL DESTINATION", css: '700 72px "Oswald"' };+let shows: { text: string; css: string; rotate?: number; at?: [number, number] } = { text: "TRAVEL DESTINATION", css: '700 72px "Oswald"' };const reads: number[] = [];async function frameAt(_src: string | Blob, time: number) {reads.push(time);@@ -29,7 +29,10 @@ async function frameAt(_src: string | Blob, time: number) {ctx.fillStyle = "#ffffff";ctx.textAlign = "center";ctx.textBaseline = "middle";- ctx.fillText(shows.text, 960, 540);+ 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);return { canvas: c, timestamp: time, duration: 1 / 30 } as never;}await stubModule<typeof import("./mediaRead")>("./mediaRead", import.meta.url, { frameAt });@@ -125,3 +128,42 @@ test("inside a picture box, the region, the ink and the size are the box's", asyexpect(Math.abs(out.ink.y - 0.5)).toBeLessThan(0.02);expect(out.picture).toBeDefined();});++test("a word set sideways is read upright, and the rotation that sets it comes back", async () => {+ // A column word reading bottom to top: a title at rotation -90.+ shows = { text: "PLEASE", css: '700 120px "Oswald"', rotate: -90, at: [192, 540] };+ const column = { x: 0.1, y: 0.5, w: 0.12, h: 0.7 };+ const out = (await runAiTool("suggest_font", { asset_id: "ref", t: 3, region: column, text: "PLEASE" })) as Out & { rotation: number };+ expect(out.rotation).toBe(-90);+ expect(out.matches[0]).toMatchObject({ font: "oswald", weight: 700 });+ expect(Math.abs(out.matches[0].size - 120)).toBeLessThanOrEqual(5);+ expect(Math.abs(out.ink.x - 0.1)).toBeLessThan(0.01);+ expect(Math.abs(out.ink.y - 0.5)).toBeLessThan(0.02);+ // Read upright the word is long; on the frame it stands tall.+ expect(out.ink.h).toBeGreaterThan(out.ink.w);+ // Told the rotation, it reads the same.+ const told = (await runAiTool("suggest_font", { asset_id: "ref", t: 3, region: column, text: "PLEASE", rotation: -90 })) as Out & { rotation: number };+ expect(told.rotation).toBe(-90);+ expect(told.matches[0]).toMatchObject({ font: "oswald", weight: 700 });+});++test("type too small to tell faces apart says so and lists the faces by kind", async () => {+ // 18px on a 1080 frame: a few pixels of x-height, where glyph overlap+ // sees proportions and not the face.+ shows = { text: "of my life", css: '400 18px "Oswald"' };+ const out = (await runAiTool("suggest_font", { asset_id: "ref", t: 3, region: { x: 0.5, y: 0.5, w: 0.2, h: 0.08 }, text: "of my life" })) as Out & {+ small?: boolean;+ inkPx: number;+ kinds?: Record<string, string[]>;+ };+ expect(out.small).toBe(true);+ expect(out.inkPx).toBeLessThan(24);+ expect(out.note).toContain("too small");+ // Each face by its id and how it looks, to pick a stand-in by eye.+ expect(out.kinds?.serif).toContain("serif — book serif (New York)");+ expect(out.kinds?.sans).toContain("oswald — condensed grotesque");+ // Type big enough to read is not flagged.+ shows = { text: "TRAVEL DESTINATION", css: '700 72px "Oswald"' };+ const big = (await runAiTool("suggest_font", { asset_id: "ref", t: 3, region: REGION, text: "TRAVEL DESTINATION" })) as Out & { small?: boolean };+ expect(big.small).toBeUndefined();+});