Open source CapCut alternative.
TypeScript
fix(cut): type stays inside the frame [rebuild]
David Le2aab316d6546
diff --git a/docs/guides/cut/README.md b/docs/guides/cut/README.mdindex 5954739d..5894f7ac 100644--- a/docs/guides/cut/README.md+++ b/docs/guides/cut/README.md@@ -28,7 +28,7 @@ browser (the editor)The client probes the engine's health endpoint (dedicated port first, dev server second) and remembers the winner. Browsers permission-gate a hosted page's calls to the local machine, so the page probes only when that raises no prompt — it is served by the engine, or the permission is already granted — and starts in the cloud otherwise. The engine grants the hosted origin cross-origin access, and only that origin. Nothing walls the app, because cloud editing always works. Closing the Donkey app does not hide the work made in it either: a browser that has reached this Mac's engine before keeps listing that shelf from the listing it stored, so those projects stay on the home beside the cloud ones, each badged with where it lives. They are read-only while the app is away — no renames, moves, or deletes — and opening one says that it lives on this Mac rather than guessing at a cloud copy that was never there. That is where the warning belongs, so that is the only place it appears: a banner over the thing that will not open, which the user cannot dismiss and which opens the recovery steps in a dialog with the button that fires the browser's permission prompt. Probing continues behind it, so starting the app clears the banner and loads the project. Engine updates ride the Donkey app's own auto-updater, so the client never prompts to update.-**A cut needs no footage.** The frame starts as a color the project owns, and everything else — clips, titles, shapes, stickers — draws over it. So a video made entirely of text and graphics is an ordinary project: it plays, exports, shares, and renders on the worker like any other, and the same color letterboxes a fitted clip and fills a gap on track 0. Elements stack by row: two on one row slide clear of each other in time, so a title that sits over a card lives a row above it.+**A cut needs no footage.** The frame starts as a color the project owns, and everything else — clips, titles, shapes, stickers — draws over it. So a video made entirely of text and graphics is an ordinary project: it plays, exports, shares, and renders on the worker like any other, and the same color letterboxes a fitted clip and fills a gap on track 0. Elements stack by row: two on one row slide clear of each other in time, so a title that sits over a card lives a row above it. Type stays inside the picture on its own: a line wider than the room its anchor leaves is measured against the face it will actually be drawn in and broken onto another line, in the preview and in the export alike, so whoever writes the words — a person, the assistant — picks the size the design wants and lets the frame do the breaking.**Framing is per clip.** Every video clip meets its box — the whole frame, or the region it was given — through one geometry: fitted with margins or covering with a crop, zoomed in further, panned to choose what the crop keeps, turned, faded, cornered, masked, and shadowed. One function computes where the picture lands, the preview draws that rectangle, and the export graph scales and crops to the same numbers, so a reframed clip looks the same on every surface, freeze frames and headless renders included. On the stage the selected clip wears two outlines: a blue box for its layout, with grips to move, resize, and rotate it, and a grey dashed one standing where the source really lands, so the part a crop discards stays visible — its corners zoom the picture and its interior pans it. The shadow is cast by the shape the clip actually shows, mask included, which is what makes a circle-masked camera bubble throw a circular shadow.diff --git a/site/packages/effects-kit/src/render.ts b/site/packages/effects-kit/src/render.tsindex 83d64c19..30f6ad48 100644--- a/site/packages/effects-kit/src/render.ts+++ b/site/packages/effects-kit/src/render.ts@@ -340,11 +340,69 @@ export function textCssFont(overlay: TextOverlay, fpx: number, env: RenderEnv):return `${overlay.italic ? "italic " : ""}${overlay.weight} ${fpx}px ${env.fontStack(overlay.font)}`;}+/** Share of the frame width a line of type may take. The rest is the margin+ * that keeps words off the edge of the picture. */+export const TEXT_SAFE_WIDTH = 0.88;++/** The room an element anchored at `x` has for a centered box: the safe width,+ * narrowed when the anchor sits off-center so the box cannot reach past the+ * near edge. An anchor pinned to an edge has no room at all, and text there is+ * left whole — nothing a break could do would bring it back into the picture. */+export function textRoom(x: number, frameWidth: number): number {+ const anchored = 2 * Math.min(Math.max(0, x), Math.max(0, 1 - x));+ return Math.min(TEXT_SAFE_WIDTH, anchored) * frameWidth;+}++/**+ * Break text into lines that each fit `room`, keeping the author's own breaks.+ *+ * Every surface draws text from explicit lines and none of them reflows, so a+ * line longer than the frame simply runs off it. This is where that is+ * prevented, and the measurer is the caller's: the painter+ * measures with the canvas it is about to draw on, the preview with the face+ * the browser has loaded, and both break in the same places.+ *+ * A word too wide for the room keeps its own line whole.+ *+ * A line already inside the room is handed back exactly as written, so the+ * spacing an author typed survives everywhere it fits.+ */+export function wrapTextToRoom(+ text: string,+ room: number,+ measure: (line: string) => number+): string {+ if (!(room > 0)) return text;+ const out = text.split("\n").map((source) => {+ if (measure(source) <= room) return source;+ const words = source.split(/\s+/).filter(Boolean);+ if (words.length === 0) return "";+ const lines: string[] = [];+ let line = "";+ for (const w of words) {+ const cand = line ? `${line} ${w}` : w;+ if (line && measure(cand) > room) {+ lines.push(line);+ line = w;+ } else {+ line = cand;+ }+ }+ if (line) lines.push(line);+ return lines.join("\n");+ });+ return out.join("\n");+}+async function paintText(ctx: CanvasRenderingContext2D,overlay: TextOverlay,frame: PaintFrame,- env: RenderEnv+ env: RenderEnv,+ /** Width a line may measure, in the space this call draws in. The stretch+ * pass hands its own down, because everything measured under `ctx.scale` is+ * drawn wider than it measures. */+ room?: number) {// Glyph stretch scales everything the element paints — plate, stroke,// per-glyph motion — about its center, the same space the DOM pair@@ -358,7 +416,13 @@ async function paintText(ctx.scale(sx, sy);ctx.translate(-cx, -cy);try {- await paintText(ctx, { ...overlay, stretchX: undefined, stretchY: undefined }, frame, env);+ await paintText(+ ctx,+ { ...overlay, stretchX: undefined, stretchY: undefined },+ frame,+ env,+ textRoom(overlay.x, frame.width) / sx+ );} finally {ctx.restore();}@@ -377,7 +441,11 @@ async function paintText(ctx.textAlign = "center";ctx.textBaseline = "middle";- const lines = overlay.text.split("\n");+ // 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.+ const lines = wrapTextToRoom(overlay.text, room ?? textRoom(overlay.x, width), (line) =>+ ctx.measureText(line).width+ ).split("\n");const lineH = fpx * (overlay.lineHeight ?? LINE_HEIGHT);const totalH = lines.length * lineH;const cx = overlay.x * width;@@ -772,7 +840,10 @@ export async function measureElementBounds(const ctx = scratch.getContext("2d")!;ctx.font = cssFont;if ("letterSpacing" in ctx) ctx.letterSpacing = `${(o.letterSpacing ?? 0) * fpx}px`;- const lines = o.text.split("\n");+ const { sx, sy } = textStretch(o);+ const lines = wrapTextToRoom(o.text, textRoom(o.x, frame.width) / sx, (line) =>+ ctx.measureText(line).width+ ).split("\n");const maxW = Math.max(...lines.map((l) => ctx.measureText(l).width), 1);const lineH = fpx * (o.lineHeight ?? LINE_HEIGHT);let w = maxW;@@ -781,7 +852,6 @@ export async function measureElementBounds(w += PLATE_PAD_X * fpx * 2;h += PLATE_PAD_Y * fpx * 2;}- const { sx, sy } = textStretch(o);if (opts?.pad === false) return { cx, cy, w: w * sx, h: h * sy };const strokePad = (o.stroke?.width ?? 0) * fpx * 2;const shadow = resolveShadow(o.shadow);diff --git a/site/packages/effects-kit/src/textFit.test.ts b/site/packages/effects-kit/src/textFit.test.tsnew file mode 100644index 00000000..d86449e2--- /dev/null+++ b/site/packages/effects-kit/src/textFit.test.ts@@ -0,0 +1,59 @@+import { describe, expect, test } from "bun:test";+import { TEXT_SAFE_WIDTH, textRoom, wrapTextToRoom } from "./render";++// A stand-in face: every glyph ten units wide, spaces included. Enough to pin+// down where the breaks land without a canvas.+const measure = (line: string) => line.length * 10;++describe("textRoom", () => {+ test("a centered anchor gets the whole safe width", () => {+ expect(textRoom(0.5, 1080)).toBeCloseTo(TEXT_SAFE_WIDTH * 1080);+ });++ test("an anchor near an edge only gets as far as that edge", () => {+ // 0.2 from the left: a centered box may reach 0.2 either way.+ expect(textRoom(0.2, 1000)).toBeCloseTo(400);+ expect(textRoom(0.8, 1000)).toBeCloseTo(400);+ });++ test("an anchor pinned to the edge keeps the promise the narrowing makes", () => {+ // Whatever room is handed back, a box centered on the anchor has to sit+ // inside the frame — there is no floor that quietly reaches past it.+ for (const x of [0, 0.02, 0.05, 0.09, 0.95, 1]) {+ const room = textRoom(x, 1000);+ expect(x * 1000 - room / 2).toBeGreaterThanOrEqual(-1e-9);+ expect(x * 1000 + room / 2).toBeLessThanOrEqual(1000 + 1e-9);+ }+ });+});++describe("wrapTextToRoom", () => {+ test("a line that fits stays on one line", () => {+ expect(wrapTextToRoom("before going to", 400, measure)).toBe("before going to");+ });++ test("a line that does not fit breaks between words", () => {+ expect(wrapTextToRoom("before going to bed", 100, measure)).toBe(+ "before\ngoing to\nbed"+ );+ });++ test("the author's own breaks survive", () => {+ expect(wrapTextToRoom("one\ntwo", 1000, measure)).toBe("one\ntwo");+ });++ test("a word wider than the room stands alone rather than being cut", () => {+ expect(wrapTextToRoom("a supercalifragilistic b", 60, measure)).toBe(+ "a\nsupercalifragilistic\nb"+ );+ });++ test("no room to speak of leaves the text alone", () => {+ expect(wrapTextToRoom("keep me", 0, measure)).toBe("keep me");+ });++ test("a line that fits keeps the spacing it was written with", () => {+ expect(wrapTextToRoom("HELLO WORLD", 1000, measure)).toBe("HELLO WORLD");+ expect(wrapTextToRoom(" indented", 1000, measure)).toBe(" indented");+ });+});diff --git a/site/src/cut/components/OverlayLayer.tsx b/site/src/cut/components/OverlayLayer.tsxindex 17577a26..a66a3120 100644--- a/site/src/cut/components/OverlayLayer.tsx+++ b/site/src/cut/components/OverlayLayer.tsx@@ -25,6 +25,7 @@ import {SHADOW,wordDrawCss,} from "@/cut/lib/textRender";+import { fitTextToFrame } from "@/cut/lib/textFit";import {behindSubjectOverlay,clampOverlayPos,@@ -524,6 +525,11 @@ function OverlayItem({const scale = stageWidth / frame.w;const stageHeight = (stageWidth * frame.h) / frame.w;const isText = isTextOverlay(o);+ // What the element actually reads as: its written lines, broken wherever a+ // line is wider than the room the frame leaves it. The export's painter+ // breaks the same lines in the same places; editing shows the raw text, so+ // the box holds what the author typed.+ const drawnText = !isTextOverlay(o) ? "" : editing ? o.text : fitTextToFrame(o, frame.w);// Publish this element's box so a sibling drag can align to it.useEffect(() => {@@ -588,7 +594,7 @@ function OverlayItem({const wordDraw =words && !glyphs && isTextOverlay(o)? wordDrawsAt(- o.text,+ drawnText,words,o.color ?? WORD_ACCENT_DEFAULT,Math.max(0.1, o.end - o.start),@@ -599,9 +605,9 @@ function OverlayItem({// the box is being edited).const shownText =isText && live?.textProgress !== undefined && !editing- ? o.text.slice(0, Math.ceil(live.textProgress * o.text.length))+ ? drawnText.slice(0, Math.ceil(live.textProgress * drawnText.length)): isText- ? o.text+ ? drawnText: "";// Behind-the-speaker elements draw inside the canvas compositor; the DOM// keeps an invisible hit target (and the selection chrome) so editingdiff --git a/site/src/cut/components/Timeline.tools.ts b/site/src/cut/components/Timeline.tools.tsindex 8bd7589d..f7586554 100644--- a/site/src/cut/components/Timeline.tools.ts+++ b/site/src/cut/components/Timeline.tools.ts@@ -188,7 +188,7 @@ export const TIMELINE_TOOLS = [{name: "add_title",description:- "Add a text title overlay. Position is the text center as a fraction of the frame (x,y in 0..1; y=0.42 is the default band). Size is px at 1080-wide. Font ids come from the graphics skill (system set + the bundled Google families).",+ "Add a text title overlay. Position is the text center as a fraction of the frame (x,y in 0..1; y=0.42 is the default band). Size is px at 1080-wide. Font ids come from the graphics skill (system set + the bundled Google families). A line wider than the room its anchor leaves is broken onto more lines when it draws, so a title always sits inside the frame — write the words and pick the size the design wants.",inputSchema: obj({text: str("The title text (\\n for line breaks)"),start: num("Start time s (default: playhead)"),diff --git a/site/src/cut/lib/fontAssets.ts b/site/src/cut/lib/fontAssets.tsindex ed2ffc01..a8a90f84 100644--- a/site/src/cut/lib/fontAssets.ts+++ b/site/src/cut/lib/fontAssets.ts@@ -9,6 +9,7 @@* here keeps the ones already saved rendering.*/+import { forgetTextWidths } from "./textFit";import {registerFonts,unregisterFonts,@@ -43,7 +44,13 @@ export function setFontInstaller(install: FontInstaller): void {* folder or the account's Library shelf. Throws on bytes that are not a font,* which is what makes it double as the upload's validity check. */export function installFontFace(family: string, bytes: ArrayBuffer): Promise<void> {- return installFace(family, bytes);+ return installFace(family, bytes).then(() => {+ // Anything measured while this face was still on its way was measured+ // against a fallback. A headless process has no `document.fonts` to watch,+ // so the install is the only sign the real face arrived, and a wrap pinned+ // on fallback widths breaks the line somewhere the drawn text does not.+ forgetTextWidths();+ });}/** A display label from the uploaded file's name, extension dropped. */@@ -80,6 +87,7 @@ export function syncFontAssets(assets: MediaAsset[]): Promise<void> {if (stale.length) {for (const id of stale) loaded.delete(id);unregisterFonts(stale.map(uploadedFontId));+ forgetTextWidths();}return Promise.all(fonts.map(registerFontAsset)).then(() => {});}diff --git a/site/src/cut/lib/headless/nodeFonts.ts b/site/src/cut/lib/headless/nodeFonts.tsindex ac26f174..aa7596bf 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 { forgetTextWidths } from "../textFit";import { registerFonts } from "../types";/**@@ -68,6 +69,9 @@ export async function installHeadlessFonts(): Promise<number> {]);loaded++;}+ // Faces installed after a line was measured would leave that line broken+ // against the fallback for the life of the process.+ if (loaded > 0) forgetTextWidths();return loaded;} catch {return 0;diff --git a/site/src/cut/lib/linkedLibrary/fonts.ts b/site/src/cut/lib/linkedLibrary/fonts.tsindex 4bedafe6..d57b73bd 100644--- a/site/src/cut/lib/linkedLibrary/fonts.ts+++ b/site/src/cut/lib/linkedLibrary/fonts.ts@@ -15,6 +15,7 @@*/import { installFontFace } from "../fontAssets";+import { forgetTextWidths } from "../textFit";import { isFontFile } from "../media";import { fontsFromArchive, isFontArchive } from "../fontArchive";import {@@ -57,7 +58,10 @@ registerLinkedKind({await installFontFace(familyOf(key), bytes);registerFonts([{ id: libraryFontId(key), label, stack: `"${familyOf(key)}"` }]);},- drop: (keys) => unregisterFonts(keys.map(libraryFontId)),+ drop: (keys) => {+ unregisterFonts(keys.map(libraryFontId));+ forgetTextWidths();+ },});/** The account's fonts, one entry per typeface however many shelves hold it. */diff --git a/site/src/cut/lib/subtitles.ts b/site/src/cut/lib/subtitles.tsindex e8ce7748..ae9fa82f 100644--- a/site/src/cut/lib/subtitles.ts+++ b/site/src/cut/lib/subtitles.ts@@ -1,11 +1,14 @@import {contrastText as kitContrastText,+ textRoom,+ wrapTextToRoom,wordDrawsAt,WORD_ACCENT_DEFAULT,wordEffect,wordSampleWindows,type OverlayWords,} from "@donkeycut/effects-kit";+import { measureLine, textFontOf } from "./textFit";import { formatTime } from "./time";import type {CaptionStyleId,@@ -310,29 +313,27 @@ export function wrapCaption(text: string): string {/*** Wrap a caption into as many lines as it takes to keep every line inside the- * frame width at font `size`. The export burn-in renders each `\n`-separated- * line centered without wrapping, so this is what guarantees captions stay in- * bounds — bigger styles (and the punchy opener) wrap to fewer words per line.+ * frame width at font `size` in `font`. The export burn-in renders each+ * `\n`-separated line centered without wrapping, so this is what decides how+ * a cue sits in the frame.+ *+ * The line is measured on the face it will be drawn in. A character count has+ * to assume the widest glyph that face could hold, which broke short cues onto+ * two lines with most of the frame still empty beside them; a measured line+ * puts a cue on one line whenever one line fits.*/-export function wrapCaptionForSize(text: string, size: number, frameW: number): string {- const words = text.trim().replace(/\s+/g, " ").split(" ").filter(Boolean);- if (words.length === 0) return "";- // ~88% of the frame width, with a conservative bold-glyph advance- // (~0.58·size) so real fonts stay comfortably inside the safe area.- const maxChars = Math.max(8, Math.floor((0.88 * frameW) / (0.58 * size)));- const lines: string[] = [];- let line = "";- for (const w of words) {- const cand = line ? `${line} ${w}` : w;- if (line && cand.length > maxChars) {- lines.push(line);- line = w;- } else {- line = cand;- }- }- if (line) lines.push(line);- return lines.join("\n");+export function wrapCaptionForSize(+ text: string,+ size: number,+ frameW: number,+ font: FontId = "sf",+ weight: 400 | 700 = 700,+ x = 0.5+): string {+ const flat = text.trim().replace(/\s+/g, " ");+ if (!flat) return "";+ const css = textFontOf({ font, weight, italic: undefined }, size);+ return wrapTextToRoom(flat, textRoom(x, frameW), (line) => measureLine(line, css, size));}/** A cue as a synthetic overlay, so captions ride the title pipeline. The style@@ -352,13 +353,21 @@ export function cueOverlay(const size = Math.round((pos?.size ?? style.size) * (isOpener && style.openerScale ? style.openerScale : 1));- const text = wrapCaptionForSize(cue.text, size, frameW);+ const x = pos?.x ?? style.x;+ const text = wrapCaptionForSize(+ cue.text,+ size,+ frameW,+ pos?.font ?? style.font,+ style.weight,+ x+ );return {id: `sub-${cue.id}`,text,start: cue.start,end: cue.end,- x: pos?.x ?? style.x,+ x,y: pos?.y ?? style.y,size,font: pos?.font ?? style.font,diff --git a/site/src/cut/lib/textCompose.ts b/site/src/cut/lib/textCompose.tsindex 714136ce..5d6ab92f 100644--- a/site/src/cut/lib/textCompose.ts+++ b/site/src/cut/lib/textCompose.ts@@ -1,4 +1,6 @@+import { textRoom, wrapTextToRoom } from "@donkeycut/effects-kit";import type { OverlayAnim, OverlayAnimStyle } from "@donkeycut/effects-kit";+import { measureLine, textFontOf } from "./textFit";import type { TextLook } from "./textLooks";import type { TextMoveId } from "./textMotion";import type { FontId } from "./types";@@ -126,34 +128,6 @@ const EMPHASIS_SCALE: Record<TextEmphasis, number> = {hero: 1.34,};-/** Average glyph width as a fraction of the type size, per face. A run that- * changes typeface has to re-break every line against the face carrying it —- * the same string in Bebas and Archivo Black are nowhere near the same width. */-export const FACE_WIDTH_RATIO: Record<string, number> = {- sf: 0.52,- serif: 0.5,- rounded: 0.55,- mono: 0.6,- impact: 0.46,- inter: 0.53,- montserrat: 0.56,- poppins: 0.58,- oswald: 0.45,- "space-grotesk": 0.55,- playfair: 0.5,- caveat: 0.42,- bebas: 0.42,- anton: 0.44,- "archivo-black": 0.62,- bangers: 0.48,- lobster: 0.46,- pacifico: 0.52,- "permanent-marker": 0.5,- "dm-serif": 0.5,-};--export const faceWidth = (font: string): number => FACE_WIDTH_RATIO[font] ?? 0.55;-/** One face in a look's ensemble. */export interface TextFace {font: FontId;@@ -232,50 +206,52 @@ const normText = (t: string): string =>.replace(/[^\p{L}\p{N}]/gu, "");/**- * A line broken and sized to the room it actually has. The look's size is a- * ceiling written for a wide frame at center; a vertical cut, a long line, a- * wider face, or a line placed off to one side all get breaks at word- * boundaries first and a smaller size only when breaking is not enough.+ * A line broken and sized to the room it actually has.+ *+ * The look's size is a ceiling written for a wide frame at center; a vertical+ * cut, a long line, a wider face, or a line placed off to one side all get+ * breaks at word boundaries first and a smaller size only when breaking is not+ * enough. Every width here is measured on the face the line will be drawn in,+ * through the same seam the painter and the preview measure with, so a run+ * lands where the frame says it lands.*/-export function fitTextToFrame(+export function fitLineToFrame(text: string,frame: { w: number; h: number },size: number,- opts: { widthRatio: number; lineHeight?: number; x?: number; maxLines?: number } = {- widthRatio: 0.55,+ opts: {+ font: FontId;+ weight: 400 | 700;+ italic?: boolean;+ lineHeight?: number;+ x?: number;+ maxLines?: number;}): { text: string; size: number } {if (text.includes("\n")) return { text, size }; // the caller broke it themselves- const ratio = opts.widthRatio;- // An element centered off-axis only has the narrower half of the frame to- // spread into before it runs off the edge.- const x = opts.x ?? 0.5;- const room = Math.max(0.3, 2 * Math.min(x, 1 - x));- const usable = frame.w * 0.86 * room;+ const room = textRoom(opts.x ?? 0.5, frame.w);const words = text.split(/\s+/).filter((w) => w.length > 0);- if (words.length === 0) return { text, size };+ if (words.length === 0 || !(room > 0)) return { text, size };const maxLines = opts.maxLines ?? 3;- const budget = Math.max(6, Math.floor(usable / (size * ratio)));- const lines: string[] = [];- let cur = "";- for (const w of words) {- const next = cur ? `${cur} ${w}` : w;- if (next.length <= budget || cur === "") cur = next;- else {- lines.push(cur);- cur = w;- }- }- if (cur) lines.push(cur);- const longest = lines.reduce((n, l) => Math.max(n, l.length), 0);+ const lineH = opts.lineHeight ?? 1.25;+ const widthAt = (line: string, px: number) =>+ measureLine(line, textFontOf({ font: opts.font, weight: opts.weight, italic: opts.italic }, px), px);++ const layout = (px: number) => wrapTextToRoom(text, room, (line) => widthAt(line, px)).split("\n");+ let lines = layout(size);let fitted = size;- if (longest > budget) fitted = Math.floor(usable / (longest * ratio));+ // A single word wider than the room is the one break that cannot help, so+ // the size comes down until it fits.+ const widest = Math.max(...lines.map((l) => widthAt(l, size)), 1);+ if (widest > room) fitted = Math.floor((size * room) / widest);+ // Too many lines is a design failure the same way overflow is: the run+ // shrinks until the stack is the height the look asked for.if (lines.length > maxLines) fitted = Math.min(fitted, Math.floor(size * (maxLines / lines.length)));- const rows = lines.length;- const lineH = opts.lineHeight ?? 1.25;- const tall = rows * fitted * lineH;- if (tall > frame.h * 0.8) fitted = Math.floor((frame.h * 0.8) / (rows * lineH));- return { text: lines.join("\n"), size: Math.max(16, Math.min(size, fitted)) };+ if (fitted < size) lines = layout(Math.max(16, fitted));+ const tall = lines.length * fitted * lineH;+ if (tall > frame.h * 0.8) fitted = Math.floor((frame.h * 0.8) / (lines.length * lineH));+ const px = Math.max(16, Math.min(size, fitted));+ return { text: layout(px).join("\n"), size: px };}/**@@ -351,12 +327,13 @@ export function composeTextRun(const base = (line.size ?? look.text.size) * (line.size ? 1 : face.scale ?? 1);const wanted = Math.round(base * EMPHASIS_SCALE[emphasis] * repeatBoost);- // The look's own ratio is measured for its own face; the moment a line- // is carried by a different one, the line has to be broken against that- // face instead or it fits the frame on paper only.- const ownFace = font === look.text.font;- const fit = fitTextToFrame(line.text, frame, wanted, {- widthRatio: ownFace ? look.text.widthRatio ?? faceWidth(font) : faceWidth(font),+ // Measured on the face this line is actually carried by, so a line that+ // takes the ensemble's wider typeface breaks where that face runs out of+ // room.+ const fit = fitLineToFrame(line.text, frame, wanted, {+ font,+ weight: face.weight,+ italic: face.italic ?? false,lineHeight: look.text.lineHeight,x,});diff --git a/site/src/cut/lib/textFit.ts b/site/src/cut/lib/textFit.tsnew file mode 100644index 00000000..43243cfe--- /dev/null+++ b/site/src/cut/lib/textFit.ts@@ -0,0 +1,111 @@+"use client";++import { textRoom, wrapTextToRoom } from "@donkeycut/effects-kit";+import { createRasterCanvas } from "./raster";+import { fontStack, type TextOverlay } from "./types";++/**+ * Keeping written words inside the frame.+ *+ * Text is laid out from explicit lines everywhere it is drawn — the preview's+ * DOM, the export's canvas painter — so nothing reflows on its own and a line+ * that is too long simply runs off the edge. This measures a line the way the+ * painter will draw it and breaks it where it has to break, so a title the+ * assistant writes and a caption a transcript produces both land inside the+ * picture whatever face and size they are wearing.+ *+ * The measurement goes through the raster seam, so a headless render measures+ * with the same canvas it draws with.+ */++let measured: CanvasRenderingContext2D | null | undefined;++function measureCtx(): CanvasRenderingContext2D | null {+ if (measured === undefined) {+ try {+ measured = (createRasterCanvas(8, 8).getContext("2d") ??+ null) as CanvasRenderingContext2D | null;+ } catch {+ measured = null;+ }+ }+ return measured;+}++/** Whether the page's own faces have finished loading. A line measured against+ * a fallback face is measured again once the real one arrives, so an early+ * paint never pins a wrap that the loaded font would not have produced. */+function facesReady(): boolean {+ const fonts = typeof document === "undefined" ? undefined : document.fonts;+ return !fonts || fonts.status === "loaded";+}++const widths = new Map<string, number>();++/**+ * Drop every measurement taken so far.+ *+ * A headless run has no `document.fonts` to watch, so the only sign that a+ * face arrived is the install itself: `fontAssets` calls this the moment one+ * is registered or dropped. A caption measured against the fallback while the+ * project's own face was still being fetched would otherwise keep that width+ * for the life of the process and break its lines in places the drawn text+ * does not.+ */+export function forgetTextWidths(): void {+ widths.clear();+}++/** How wide one line draws, in the same pixels `frameW` is in. Falls back to a+ * conservative bold-glyph advance where there is no canvas to ask. */+export function measureLine(+ text: string,+ cssFont: string,+ sizePx: number,+ tracking = 0+): number {+ if (!text) return 0;+ const key = `${facesReady() ? 1 : 0}|${cssFont}|${tracking}|${text}`;+ const hit = widths.get(key);+ if (hit !== undefined) return hit;+ const ctx = measureCtx();+ let w: number;+ if (ctx) {+ ctx.font = cssFont;+ if ("letterSpacing" in ctx) {+ (ctx as { letterSpacing: string }).letterSpacing = `${tracking * sizePx}px`;+ }+ w = ctx.measureText(text).width;+ } else {+ w = text.length * (0.58 + tracking) * sizePx;+ }+ // Bounded: a long lyric video walks through a lot of one-off lines.+ if (widths.size > 4000) widths.clear();+ widths.set(key, w);+ return w;+}++/** The css font a text element draws in, at a given pixel size. Mirrors the+ * kit painter's own `textCssFont`, so what is measured is what is drawn. */+export function textFontOf(+ o: Pick<TextOverlay, "italic" | "weight" | "font">,+ sizePx: number+): string {+ return `${o.italic ? "italic " : ""}${o.weight} ${sizePx}px ${fontStack(o.font)}`;+}++/** The element's text with every over-long line broken to fit the room its+ * anchor leaves in the frame. Returns the text unchanged when it already fits,+ * so nothing re-renders for an element that was never in trouble. */+export function fitTextToFrame(o: TextOverlay, frameW: number): string {+ // A stretched element is measured at its own size and drawn wider, so the+ // room shrinks by the stretch — the same arithmetic the kit painter does+ // under `ctx.scale`, which keeps the two breaking in the same places.+ const size = o.size;+ const font = textFontOf(o, size);+ const room = textRoom(o.x, frameW) / Math.max(0.01, o.stretchX ?? 1);+ const width = (line: string) => measureLine(line, font, size, o.letterSpacing ?? 0);+ if (o.text.split("\n").every((line) => width(line) <= room)) return o.text;+ return wrapTextToRoom(o.text, room, width);+}+diff --git a/site/src/cut/lib/textLooks.ts b/site/src/cut/lib/textLooks.tsindex 976eca34..9b8700ab 100644--- a/site/src/cut/lib/textLooks.ts+++ b/site/src/cut/lib/textLooks.ts@@ -49,11 +49,6 @@ export interface TextLook {shadow?: boolean;x: number;y: number;- /** Average glyph width as a fraction of the type size, for this face.- * A sequence uses it to break and size a line so it fits the frame it is- * actually in — the same 132px that fills a 16:9 frame runs off a 9:16- * one. Absent = 0.55, an ordinary sans. */- widthRatio?: number;};/** The color that marks the word that matters: the karaoke highlight, an* accent line, a marker scribble. */@@ -99,7 +94,6 @@ export const TEXT_LOOKS: Record<string, TextLook> = {shadow: true,x: 0.5,y: 0.78,- widthRatio: 0.52,},accent: "#FFFFFF",motion: { in: { style: "fade", seconds: 0.25 }, out: { style: "fade", seconds: 0.2 } },@@ -127,7 +121,6 @@ export const TEXT_LOOKS: Record<string, TextLook> = {shadow: false,x: 0.5,y: 0.5,- widthRatio: 0.62,},accent: "#FF5500",motion: { in: { style: "pop", seconds: 0.35 } },@@ -161,7 +154,6 @@ export const TEXT_LOOKS: Record<string, TextLook> = {shadow: true,x: 0.5,y: 0.5,- widthRatio: 0.56,},accent: "#FFE94A",motion: { in: { style: "fade", seconds: 0.25 }, out: { style: "fade", seconds: 0.25 } },@@ -192,7 +184,6 @@ export const TEXT_LOOKS: Record<string, TextLook> = {shadow: true,x: 0.5,y: 0.5,- widthRatio: 0.42,},accent: "#F0ABFC",motion: { in: { style: "streak", seconds: 0.4 }, loop: { style: "flicker", speed: 0.6 } },@@ -226,7 +217,6 @@ export const TEXT_LOOKS: Record<string, TextLook> = {shadow: false,x: 0.5,y: 0.48,- widthRatio: 0.42,},accent: "#FF5500",motion: { in: { style: "typewriter", seconds: 0.5 } },@@ -259,7 +249,6 @@ export const TEXT_LOOKS: Record<string, TextLook> = {shadow: false,x: 0.5,y: 0.5,- widthRatio: 0.5,},accent: "#C9A227",motion: { in: { style: "fade", seconds: 0.8 }, out: { style: "fade", seconds: 0.8 } },@@ -293,7 +282,6 @@ export const TEXT_LOOKS: Record<string, TextLook> = {shadow: false,x: 0.5,y: 0.5,- widthRatio: 0.6,},accent: "#F2F2F2",motion: { in: { style: "typewriter", seconds: 0.6 } },@@ -324,7 +312,6 @@ export const TEXT_LOOKS: Record<string, TextLook> = {shadow: true,x: 0.5,y: 0.72,- widthRatio: 0.46,},accent: "#FFE94A",motion: { in: { style: "rise", seconds: 0.35 }, out: { style: "fade", seconds: 0.2 } },@@ -358,7 +345,6 @@ export const TEXT_LOOKS: Record<string, TextLook> = {shadow: false,x: 0.5,y: 0.5,- widthRatio: 0.58,},accent: "#FDE047",motion: { in: { style: "scatter", seconds: 0.5 }, out: { style: "converge", seconds: 0.3 } },@@ -392,7 +378,6 @@ export const TEXT_LOOKS: Record<string, TextLook> = {shadow: false,x: 0.5,y: 0.5,- widthRatio: 0.62,},accent: "#E11D48",motion: { in: { style: "pop", seconds: 0.18 } },@@ -429,7 +414,6 @@ export const TEXT_LOOKS: Record<string, TextLook> = {shadow: false,x: 0.5,y: 0.5,- widthRatio: 0.44,},accent: "#FF3D00",motion: { in: { style: "rise", seconds: 0.3 } },@@ -463,7 +447,6 @@ export const TEXT_LOOKS: Record<string, TextLook> = {shadow: false,x: 0.5,y: 0.5,- widthRatio: 0.58,},accent: "#FB7185",motion: { in: { style: "bounce", seconds: 0.45 } },diff --git a/site/src/cut/lib/textVideo.test.ts b/site/src/cut/lib/textVideo.test.tsindex cb44cb3c..72b8ab72 100644--- a/site/src/cut/lib/textVideo.test.ts+++ b/site/src/cut/lib/textVideo.test.ts@@ -3,7 +3,8 @@ import { runAiTool } from "./aiTools";import { useEditor } from "./store";import { composeTextRun, TEXT_LAYOUT_IDS, TEXT_LAYOUT_NOTES } from "./textCompose";import { TEXT_LOOKS, textLookCatalog } from "./textLooks";-import { evalOverlayFrame } from "@donkeycut/effects-kit";+import { evalOverlayFrame, textRoom } from "@donkeycut/effects-kit";+import { measureLine, textFontOf } from "./textFit";import { liftMoveTracks, matchTextMove, TEXT_MOVE_IDS, TEXT_MOVE_NOTES, textMoveKeys } from "./textMotion";import { emptySubtitles, frameOf, isTextOverlay, isShapeOverlay, type SubtitleCue } from "./types";@@ -110,9 +111,15 @@ describe("add_text_sequence", () => {const word = overlays().filter(isTextOverlay)[0];expect(word.text).toContain("\n");const frame = frameOf("9:16");- const ratio = TEXT_LOOKS["neon-club"].text.widthRatio!;+ const look = TEXT_LOOKS["neon-club"];+ // Measured the way the painter measures it: every line inside the room the+ // element's anchor leaves in this frame.+ const room = textRoom(word.x, frame.w);for (const line of word.text.split("\n"))- expect(line.length * word.size * ratio).toBeLessThanOrEqual(frame.w * 0.9);+ expect(+ measureLine(line, textFontOf({ font: word.font, weight: word.weight }, word.size), word.size)+ ).toBeLessThanOrEqual(room);+ expect(word.size).toBeLessThanOrEqual(look.text.size);});test("a per-line color overrides the look", async () => {diff --git a/site/src/cut/server/ai/catalog.ts b/site/src/cut/server/ai/catalog.tsindex 9e882784..cd6b17eb 100644--- a/site/src/cut/server/ai/catalog.ts+++ b/site/src/cut/server/ai/catalog.ts@@ -170,7 +170,7 @@ The title lanes hold three overlay element kinds, all sharing timing (start/end)Rows are how elements stack (\`lane\`, 0 = the front row; a higher row draws further back). Two elements on ONE row never overlap in time — placing a title where a shape already sits slides it clear instead of laying it on top — so anything meant to sit over something else names a LOWER row: title on lane 0, the background shape behind it on lane 1. Every add tool and update_overlay take \`lane\`.The frame's own color is set_background, not a full-frame rectangle: it fills the frame behind every clip and element, holds for the whole cut, letterboxes fitted footage, and shows through gaps on track 0. "Make the background white", a colored card behind a title sequence, a text-and-graphics video with no footage at all — all of them start with set_background. A rectangle element is for a panel or a band inside the frame, not for the frame itself.A whole run of text — lines timed to speech, a lyric video, a kinetic-typography passage, a quote sequence — is add_text_sequence, one call. Design is opt-in: called with no look it lays a plain run — one readable face, one place in the frame, a short fade, no cards, no moves, the frame color untouched — which is what a bare "add the text" asks for. Name a look when the user asked for a designed run, and then the look is the design of the whole run, not one line: the frame color, the cards, and the ensemble the run walks across its length — faces, entrances, where successive lines land, and what each holds. Read the text-videos skill for the looks and the flow, and text-creativity for the fifty devices that keep a long run from reading flat. A designed run is composed in the call, never laid identical and then repaired element by element. A video needs no footage. Titles, shapes and stickers over a background color are a complete cut on their own: it plays, exports, and shares like any other. Build one by setting the background, then placing elements on the rows the stack needs — reach for footage only when the user asks for it.-Titles (add_title / update_overlay): text, size (frame px; the design short side is 1080), font (${FONTS.map((f) => `${f.id}=${f.label}`).join(", ")}), weight (400/700), color, shadow, plate (translucent dark backdrop).+Titles (add_title / update_overlay): text, size (frame px; the design short side is 1080), font (${FONTS.map((f) => `${f.id}=${f.label}`).join(", ")}), weight (400/700), color, shadow, plate (translucent dark backdrop). Type never leaves the picture: a line too wide for the room its anchor leaves is broken onto more lines as it draws, in the preview and in the export alike, so write the words at the size the design wants and leave the fitting to the renderer.Shapes (add_shape): rect, ellipse, line, arrow. w/h are frame fractions; rect/ellipse take fill + fill_opacity, corner radius (rect), and an outline (stroke_color/stroke_width); line/arrow draw in \`fill\` with h as their thickness, and rotation gives them their direction (0° points right, 90° points down).Stickers (add_sticker): a project image asset (generated cutouts in the Elements panel carry origin "sticker"); w is a frame-width fraction and height follows the source's aspect. Lottie JSON uploads become animated stickers that loop for the element's duration.Custom stickers (create_sticker): one call generates the image, removes the background, adds the die-cut outline, and places it — use it when the user asks for a sticker of something they don't have (it spends credits like image generation).