Open source CapCut alternative.
TypeScript
fix(cut): set a font cycle's face on the title's own lines [rebuild]
David Le1e33016a3a5a
diff --git a/site/packages/effects-kit/src/fontCycle.test.ts b/site/packages/effects-kit/src/fontCycle.test.tsindex c2565e73..19adee02 100644--- a/site/packages/effects-kit/src/fontCycle.test.ts+++ b/site/packages/effects-kit/src/fontCycle.test.ts@@ -1,7 +1,7 @@import { describe, expect, test } from "bun:test";-import { Canvas } from "skia-canvas";+import { Canvas, loadImage } from "skia-canvas";import { cutAtFaces, cycleFaceAt, cycleFaceTag, fontCycleCuts, fontCycleVariants, withFontCycle } from "./fontCycle";-import { planAnimatedLayers, renderOverlayFrames, type RenderEnv } from "./render";+import { planAnimatedLayers, renderOverlayFrames, settleCycleFace, textRoom, type RenderEnv } from "./render";import type { TextOverlay } from "./types";const title = (fonts?: NonNullable<TextOverlay["anim"]>["fonts"]): TextOverlay => ({@@ -107,3 +107,67 @@ describe("the font cycle", () => {for (const c of cuts) expect(edges.includes(c)).toBe(true);});});++describe("a face keeps the title's own lines", () => {+ // A one-line title whose cycle shows a wide face at 1.6×: in that face the+ // line is wider than the room the title wraps in, and it must stay one line,+ // set smaller, rather than break in two over whatever sits above it.+ const wide = { faces: [{ font: "wide", scale: 1.6 }], at: 0, seconds: 1, rate: 10 };+ const travel = (): TextOverlay => ({ ...title(wide), text: "TRAVEL DESTINATION", size: 60, letterSpacing: 0, start: 0, end: 2 });+ /** Advance per character: the title's own face 0.6 em, the wide one 0.9 em. */+ const measure = (type: Pick<TextOverlay, "font" | "size" | "letterSpacing">, line: string) =>+ line.length * type.size * ((type.font === "wide" ? 0.9 : 0.6) + (type.letterSpacing ?? 0));+ const room = textRoom(0.5, 1080);++ test("a face too wide for the room shrinks to it and keeps the line whole", () => {+ const own = travel();+ expect(measure(own, own.text)).toBeLessThan(room);+ const face = settleCycleFace(withFontCycle(own, 0.5), room, measure);+ expect(face.font).toBe("wide");+ expect(face.text).toBe("TRAVEL DESTINATION");+ expect(measure(face, face.text)).toBeLessThanOrEqual(room);+ expect(face.size).toBeLessThan(60 * 1.6);+ });++ test("a face that fits keeps its size, and the title's own breaks stay where they were", () => {+ const own = { ...travel(), text: "WELCOME TO\nTRAVEL DESTINATION", size: 30 };+ const face = settleCycleFace(withFontCycle(own, 0.5), room, measure);+ expect(face.text).toBe("WELCOME TO\nTRAVEL DESTINATION");+ expect(face.size).toBe(48);+ // A title broken by its own room keeps those breaks in a narrower face too.+ const long = { ...travel(), text: "A VERY LONG TITLE THAT BREAKS IN ITS OWN FACE", size: 60 };+ const ownLines = settleCycleFace({ ...long, cycleOwn: { font: long.font, weight: long.weight, size: long.size, letterSpacing: 0 } }, room, measure).text;+ const narrow = settleCycleFace(withFontCycle({ ...long, anim: { fonts: { ...wide, faces: [{ font: "narrow", scale: 0.6 }] } } }, 0.5), room, measure);+ expect(narrow.text).toBe(ownLines);+ });++ test("the canvas and ffmpeg exports draw the wide face on one line", async () => {+ const env: RenderEnv = {+ fontStack: () => "Arial",+ createCanvas: (w, h) => new Canvas(w, h) as unknown as HTMLCanvasElement,+ canvasToPngBlob: async (c) => new Blob([new Uint8Array(await (c as unknown as Canvas).toBuffer("png"))]),+ };+ const o: TextOverlay = { ...travel(), font: "own", anim: { fonts: { faces: [{ font: "own", scale: 1.6 }], at: 0, seconds: 1, rate: 10 } } };+ const set = await renderOverlayFrames(o, 270, 480, 30, env);+ // The bands of rows with ink in a picture: one per line of type.+ const bands = async (blob: Blob) => {+ const img = await loadImage(Buffer.from(await blob.arrayBuffer()));+ const c = new Canvas(img.width, img.height);+ const ctx = c.getContext("2d");+ ctx.drawImage(img, 0, 0);+ const data = ctx.getImageData(0, 0, img.width, img.height).data;+ let n = 0;+ let inked = false;+ for (let y = 0; y < img.height; y++) {+ let row = false;+ for (let x = 0; x < img.width && !row; x++) row = data[(y * img.width + x) * 4 + 3] > 40;+ if (row && !inked) n++;+ inked = row;+ }+ return n;+ };+ // The first picture is the cycle's face, the last the title's own.+ expect(await bands(set.images[set.entries[0].image])).toBe(1);+ expect(await bands(set.images[set.entries[set.entries.length - 1].image])).toBe(1);+ });+});diff --git a/site/packages/effects-kit/src/fontCycle.ts b/site/packages/effects-kit/src/fontCycle.tsindex 82dc2cc4..012e5582 100644--- a/site/packages/effects-kit/src/fontCycle.ts+++ b/site/packages/effects-kit/src/fontCycle.ts@@ -6,7 +6,9 @@** It is a slot on the element's animation and changes the type itself, so* every painter reads the element through `withFontCycle` at the moment it- * draws, and every sampler cuts its pictures at `fontCycleCuts`.+ * draws, and every sampler cuts its pictures at `fontCycleCuts`. A face never+ * reflows the title: it is set on the lines the title breaks into in its own+ * type, smaller where one of them would not fit (`settleCycleFace`).*/import type { Overlay, TextOverlay } from "./types";@@ -90,7 +92,8 @@ export function cycleFaceAt(c: OverlayFontCycle, tLocal: number): CycleFace | nu/** The element as it is set `tLocal` seconds in: the cycle's face while it* runs, its own outside it. Hands back the same object when nothing changes,* so a caller can memoize on identity. The face it hands back carries no- * cycle, so resolving it again changes nothing. */+ * cycle, so resolving it again changes nothing, and carries the title's own+ * type in `cycleOwn`, which the painter's measure settles it against. */export function withFontCycle<T extends Overlay>(o: T, tLocal: number): T {const c = overlayFontCycle(o);if (!c) return o;@@ -106,6 +109,7 @@ export function withFontCycle<T extends Overlay>(o: T, tLocal: number): T {letterSpacing: face.tracking ?? t.letterSpacing,italic: face.italic ?? t.italic,anim: { ...o.anim, fonts: undefined },+ cycleOwn: { font: t.font, weight: t.weight, italic: t.italic, size: t.size, letterSpacing: t.letterSpacing },} as T;}diff --git a/site/packages/effects-kit/src/render.ts b/site/packages/effects-kit/src/render.tsindex 913dec8d..5818e70e 100644--- a/site/packages/effects-kit/src/render.ts+++ b/site/packages/effects-kit/src/render.ts@@ -466,12 +466,65 @@ export function wrapTextToRoom(return out.join("\n");}+/** The type a line is measured in: a title's own, or a face of its cycle. */+export type TextType = Pick<TextOverlay, "font" | "weight" | "italic" | "size" | "letterSpacing">;++/**+ * A face of a title's font cycle, set on the title's own lines.+ *+ * A face is wider or narrower than the title's own type, and broken to the+ * room in its own measure it would reflow: a one-line title in a wide face at+ * 1.6× drops its last word onto a second line, over whatever sits above or+ * below it. So the lines are the ones the title breaks into in its own type+ * (`cycleOwn`), and where one of them is wider than the room in the face, the+ * face is set smaller until the widest fits. Every painter calls this with+ * its own measure — the canvas it draws on, the face the browser has loaded —+ * so the preview, the canvas export and the ffmpeg frames set the same lines+ * at the same size. Anything that is not a cycle's face comes back as it is.+ */+export function settleCycleFace<T extends Overlay>(+ o: T,+ room: number,+ measure: (type: TextType, line: string) => number+): T {+ if ((o.kind ?? "text") !== "text") return o;+ const { cycleOwn: own, ...face } = o as TextOverlay;+ if (!own) return o;+ const text = wrapTextToRoom(face.text, room, (line) => measure(own, line));+ const lines = text.split("\n");+ let size = face.size;+ // Type widens in step with its size, so one scale brings the widest line+ // onto the room; the margin and a second pass take up what hinting rounds.+ for (let pass = 0; pass < 3 && room > 0; pass++) {+ const widest = Math.max(0, ...lines.map((line) => measure({ ...face, size }, line)));+ if (widest <= room) break;+ size *= (room / widest) * 0.999;+ }+ return { ...face, text, size } as unknown as T;+}++/** `settleCycleFace` measured the way the painter measures: on a canvas in+ * the frame's pixels, with both faces resident first. */+async function settledFace(o: TextOverlay, frame: PaintFrame, env: RenderEnv): Promise<TextOverlay> {+ const own = o.cycleOwn;+ if (!own) return o;+ const cssOf = (type: TextType) => textCssFont({ ...o, ...type }, type.size * frame.scale, env);+ await Promise.all([ensureFontLoaded(cssOf(own), o.text), ensureFontLoaded(cssOf(o), o.text)]);+ const ctx = newCanvas(env, 1, 1).getContext("2d")!;+ return settleCycleFace(o, textWrapRoom(o, frame.width), (type, line) => {+ ctx.font = cssOf(type);+ if ("letterSpacing" in ctx) ctx.letterSpacing = `${(type.letterSpacing ?? 0) * type.size * frame.scale}px`;+ return ctx.measureText(line).width;+ });+}+async function paintText(ctx: CanvasRenderingContext2D,- overlay: TextOverlay,+ cycled: TextOverlay,frame: PaintFrame,env: RenderEnv,) {+ const overlay = await settledFace(cycled, frame, env);const { width, scale } = frame;const fpx = overlay.size * scale;const cssFont = textCssFont(overlay, fpx, env);@@ -1242,10 +1295,11 @@ function inkDepth(rgba: Uint8ClampedArray, w: number, h: number): Float32Array {/** The box the last drawn character of a title takes, in frame px — laid out* the way `paintText` lays it out. */async function lastGlyphBox(- o: TextOverlay,+ cycled: TextOverlay,frame: PaintFrame,env: RenderEnv): Promise<{ x0: number; x1: number; y0: number; y1: number } | null> {+ const o = await settledFace(cycled, frame, env);const fpx = o.size * frame.scale;const cssFont = textCssFont(o, fpx, env);await ensureFontLoaded(cssFont, o.text);@@ -1435,7 +1489,7 @@ export async function measureElementBounds(const aspect = img && img.width > 0 && img.height > 0 ? img.width / img.height : 1;return { cx, cy, w, h: stickerHeight(s, frame, w, aspect) };}- const o = overlay as TextOverlay;+ const o = await settledFace(overlay as TextOverlay, frame, env);const fpx = o.size * frame.scale;const cssFont = textCssFont(o, fpx, env);await ensureFontLoaded(cssFont, o.text);diff --git a/site/packages/effects-kit/src/types.ts b/site/packages/effects-kit/src/types.tsindex a56b475f..4240019f 100644--- a/site/packages/effects-kit/src/types.ts+++ b/site/packages/effects-kit/src/types.ts@@ -119,6 +119,11 @@ export interface TextOverlay extends OverlayBase {* lines), each resolved to the color, opacity and pose it draws at. Stamped* by the word engine at paint time; absent = the line draws as one piece. */wordDraw?: WordDraw[];+ /** The title's own type while its font cycle shows another face. Stamped+ * by `withFontCycle` on the face it hands back, so every painter sets the+ * face on the lines the title breaks into in its own type+ * (`settleCycleFace`); absent on a stored title. */+ cycleOwn?: Pick<TextOverlay, "font" | "weight" | "italic" | "size" | "letterSpacing">;}export type ShapeKind =diff --git a/site/src/cut/components/AnimationTiles.tools.ts b/site/src/cut/components/AnimationTiles.tools.tsindex 29e9127d..c0516422 100644--- a/site/src/cut/components/AnimationTiles.tools.ts+++ b/site/src/cut/components/AnimationTiles.tools.ts@@ -70,7 +70,7 @@ export const OVERLAY_ANIMATION_TOOLS = [{name: "set_overlay_animation",description:- `Animate an overlay element (title, shape, or sticker): preset In/Out ramps, a Loop that runs its whole duration, and a Move that says what the element does WHILE it holds. Omitted slots keep their setting; pass "none" to clear one. In/Out styles: ${OVERLAY_ANIM_STYLE_IDS.join(", ")} — slide names are the motion direction; ${TEXT_ONLY_ANIM_STYLE_IDS.join(", ")} animate titles only; ${GLYPH_ANIM_STYLE_IDS.join(", ")} move a title's letters one at a time, and move any other kind as one piece; in_stagger/out_stagger set how much of their ramp the hand-off from the first letter to the last takes, so a 1.6s drop at stagger 0.8 lands a letter every ~0.27s, each moving for ~0.3s. ${edgeNotes()} ${DIVE_ANIM_STYLE_IDS.join(", ")} as the exit is the zoom-through-a-letter transition: the shot after the element shows once the ink has filled the frame, so it needs no freeze frames, image stills or per-letter keyframes. Loop styles: ${OVERLAY_LOOP_STYLE_IDS.join(", ")} — ${GLYPH_LOOP_STYLE_IDS.join(", ")} carry a title's letters on their own delays, and carry any other kind as one piece. A move is a slot like the others and never touches the element's keyframe track; set_overlay_keyframes is the precise way to move an element and the default reach, so name a move when it is exactly what you mean — the two compose together. Word effects — words_style, titles only — play the line word by word, timed against the cut's transcript when there is one and spread across the element's own span when there is not: an emphasis travels along a line that is fully up, and a build assembles the line as it is spoken. It is the slot for "make each word pop as I say it" and for "have the words appear one by one". A typewriter entrance on a title can carry a caret: a thin bar at the end of the typed text, solid while it types, then blinking every ${CARET_BLINK_SECONDS}s — the typed comment or search box of a call-to-action card. A hit plays one motion once at a moment inside the element (hit_at seconds from its start), between its entrance and exit: ${hitNotes()}. Use press for a button being pressed, a tap on a UI element, a like or post button; pair it with a click from stock_search kind "sound" placed at the same moment when the tap should be heard. A font cycle flickers a title through typefaces — a new face font_cycle_rate times a second, from font_cycle_at for font_cycle_seconds — and then the title settles back into its own face: the typeface-shuffle beat of a teaser title. Give each face a scale (and tracking) so it fills about the room the title's own face does; a wide tracked title cycling through condensed or hand-drawn faces wants tracking 0 and a scale above 1 on them. Time the faces to the music with font_cycle_beats — a new face on each beat of that clip's detect_beats grid, font_cycle_beat_step 2 for every other beat or 0.5 for half beats — or to a reference's own face changes with font_cycle_switches, the timeline seconds you read them at; either sets every hold and repeats the faces in order to fill the run. A face's own hold times one face by hand; faces repeat in the list to revisit a typeface.`,+ `Animate an overlay element (title, shape, or sticker): preset In/Out ramps, a Loop that runs its whole duration, and a Move that says what the element does WHILE it holds. Omitted slots keep their setting; pass "none" to clear one. In/Out styles: ${OVERLAY_ANIM_STYLE_IDS.join(", ")} — slide names are the motion direction; ${TEXT_ONLY_ANIM_STYLE_IDS.join(", ")} animate titles only; ${GLYPH_ANIM_STYLE_IDS.join(", ")} move a title's letters one at a time, and move any other kind as one piece; in_stagger/out_stagger set how much of their ramp the hand-off from the first letter to the last takes, so a 1.6s drop at stagger 0.8 lands a letter every ~0.27s, each moving for ~0.3s. ${edgeNotes()} ${DIVE_ANIM_STYLE_IDS.join(", ")} as the exit is the zoom-through-a-letter transition: the shot after the element shows once the ink has filled the frame, so it needs no freeze frames, image stills or per-letter keyframes. Loop styles: ${OVERLAY_LOOP_STYLE_IDS.join(", ")} — ${GLYPH_LOOP_STYLE_IDS.join(", ")} carry a title's letters on their own delays, and carry any other kind as one piece. A move is a slot like the others and never touches the element's keyframe track; set_overlay_keyframes is the precise way to move an element and the default reach, so name a move when it is exactly what you mean — the two compose together. Word effects — words_style, titles only — play the line word by word, timed against the cut's transcript when there is one and spread across the element's own span when there is not: an emphasis travels along a line that is fully up, and a build assembles the line as it is spoken. It is the slot for "make each word pop as I say it" and for "have the words appear one by one". A typewriter entrance on a title can carry a caret: a thin bar at the end of the typed text, solid while it types, then blinking every ${CARET_BLINK_SECONDS}s — the typed comment or search box of a call-to-action card. A hit plays one motion once at a moment inside the element (hit_at seconds from its start), between its entrance and exit: ${hitNotes()}. Use press for a button being pressed, a tap on a UI element, a like or post button; pair it with a click from stock_search kind "sound" placed at the same moment when the tap should be heard. A font cycle flickers a title through typefaces — a new face font_cycle_rate times a second, from font_cycle_at for font_cycle_seconds — and then the title settles back into its own face: the typeface-shuffle beat of a teaser title. Give each face a scale (and tracking) so it fills about the room the title's own face does; a wide tracked title cycling through condensed or hand-drawn faces wants tracking 0 and a scale above 1 on them. A face keeps the title's own line breaks: where its scale makes a line wider than the room the title wraps in, that face is set smaller to fit rather than broken onto another line. Time the faces to the music with font_cycle_beats — a new face on each beat of that clip's detect_beats grid, font_cycle_beat_step 2 for every other beat or 0.5 for half beats — or to a reference's own face changes with font_cycle_switches, the timeline seconds you read them at; either sets every hold and repeats the faces in order to fill the run. A face's own hold times one face by hand; faces repeat in the list to revisit a typeface.`,inputSchema: obj({id: str("Overlay element id"),move: {diff --git a/site/src/cut/components/OverlayLayer.tsx b/site/src/cut/components/OverlayLayer.tsxindex 3019caa1..3f33dddb 100644--- a/site/src/cut/components/OverlayLayer.tsx+++ b/site/src/cut/components/OverlayLayer.tsx@@ -20,7 +20,7 @@ import {} from "@/cut/lib/subtitles";import { dealsMarks, doodleBeat, doodleSeed, isInkless, paintDoodle, paintZapInto, SPLIT_CYAN, SPLIT_RED, zapSeed, type TextTexture } from "@donkeycut/effects-kit";import { erodeMask, textureMask } from "./textureCss";-import { countText, drawStreak, elementAtMoment, fontCycleVariants, paintElementCrop, streakTaps, textCssFont, typedChars, withFontCycle } from "@donkeycut/effects-kit";+import { countText, drawStreak, elementAtMoment, fontCycleVariants, paintElementCrop, streakTaps, textCssFont, typedChars } from "@donkeycut/effects-kit";import { holdMemory } from "@/cut/lib/memoryBudget";import { stripeGeometry, stripesSolid } from "@donkeycut/effects-kit";import { elementCameraAt } from "@/cut/lib/groupCamera";@@ -38,7 +38,7 @@ import {SHADOW,wordDrawCss,} from "@/cut/lib/textRender";-import { fitTextToFrame, textBoxSize } from "@/cut/lib/textFit";+import { cycleFaceAt, fitTextToFrame, textBoxSize } from "@/cut/lib/textFit";import {behindSubjectOverlay,clampKeyPos,@@ -362,7 +362,7 @@ export function OverlayLayer({// A font cycle sets the title in another face at this moment.// A selected title holds its own face while paused, since that is// the face its grips and the inspector edit.- overlay={inRange && (playing || !selected) ? withFontCycle(o, t - o.start) : o}+ overlay={inRange && (playing || !selected) ? cycleFaceAt(o, t - o.start, frame.w) : o}// The skimmer paints the bare frame: the item still renders, its// selection chrome (outline, resize handle) does not.selected={selected && !scrubbing}@@ -415,7 +415,7 @@ export function OverlayLayer({return (<OverlayItemkey={o.id}- overlay={withFontCycle(o, t - o.start)}+ overlay={cycleFaceAt(o, t - o.start, frame.w)}selected={false}armed={false}grouped={false}diff --git a/site/src/cut/lib/fontCycleTool.test.ts b/site/src/cut/lib/fontCycleTool.test.tsindex 9bcd6651..1d805b12 100644--- a/site/src/cut/lib/fontCycleTool.test.ts+++ b/site/src/cut/lib/fontCycleTool.test.ts@@ -3,6 +3,7 @@ import { runAiTool } from "./aiTools";import { useEditor } from "./store";import { emptySubtitles, isTextOverlay } from "./types";import { fontCycleCuts } from "@donkeycut/effects-kit";+import { cycleFaceAt, fitTextToFrame } from "./textFit";/** A title's font cycle and its heavy weights through the assistant's tools. */@@ -218,3 +219,16 @@ describe("set_overlay_animation font cycle timed to switches and beats", () => {expect(await failure({ id: o.id, font_cycle: [{ font: "impact" }], font_cycle_beats: "a1" })).toContain("font_cycle_beat_step");});});++describe("the preview sets a face on the title's own lines", () => {+ test("a face wider than the room shrinks rather than breaking the line", async () => {+ const own = { ...(await aTitle()), text: "TRAVEL DESTINATION", size: 60, letterSpacing: 0, x: 0.5 };+ const o = { ...own, anim: { fonts: { faces: [{ font: own.font, scale: 1.6 }], at: 0, seconds: 1, rate: 10 } } };+ expect(fitTextToFrame(own, 1080).split("\n").length).toBe(1);+ const face = cycleFaceAt(o, 0.5, 1080);+ expect(isTextOverlay(face) && face.size).toBeLessThan(60 * 1.6);+ expect(isTextOverlay(face) && fitTextToFrame(face, 1080)).toBe("TRAVEL DESTINATION");+ // Outside the run the title is its own, untouched.+ expect(cycleFaceAt(o, 1.5, 1080)).toBe(o);+ });+});diff --git a/site/src/cut/lib/textFit.ts b/site/src/cut/lib/textFit.tsindex ba36612f..a3414f0a 100644--- a/site/src/cut/lib/textFit.ts+++ b/site/src/cut/lib/textFit.ts@@ -1,8 +1,8 @@"use client";-import { textWrapRoom, wrapTextToRoom, LINE_HEIGHT, PLATE_PAD_X, PLATE_PAD_Y } from "@donkeycut/effects-kit";+import { settleCycleFace, textWrapRoom, withFontCycle, wrapTextToRoom, LINE_HEIGHT, PLATE_PAD_X, PLATE_PAD_Y } from "@donkeycut/effects-kit";import { createRasterCanvas } from "./raster";-import { fontStack, type TextOverlay } from "./types";+import { fontStack, type Overlay, type TextOverlay } from "./types";/*** Keeping written words inside the frame.@@ -106,6 +106,17 @@ export function fitTextToFrame(o: TextOverlay, frameW: number): string {return wrapTextToRoom(o.text, room, width);}+/** The element as it is set `tLocal` seconds in, a font cycle's face set on+ * the title's own lines the way the kit's painters set it (`settleCycleFace`),+ * so the preview never reflows a title the export keeps on one line. */+export function cycleFaceAt<T extends Overlay>(o: T, tLocal: number, frameW: number): T {+ const el = withFontCycle(o, tLocal);+ if (el === o || (el.kind ?? "text") !== "text") return el;+ return settleCycleFace(el, textWrapRoom(el as TextOverlay, frameW), (type, line) =>+ measureLine(line, textFontOf(type, type.size), type.size, type.letterSpacing ?? 0)+ );+}+/** Resting box in design pixels, measured once for resize gestures. */export function textBoxSize(o: TextOverlay, frameW: number): { width: number; height: number } {const lines = fitTextToFrame(o, frameW).split("\n");