Open source CapCut alternative.
TypeScript
fix(site): set preview titles where the export draws them [rebuild]
David Le1cc15276888b
diff --git a/evals/cut-perf.latest-report.json b/evals/cut-perf.latest-report.jsonindex 0642d067..4cba0c60 100644--- a/evals/cut-perf.latest-report.json+++ b/evals/cut-perf.latest-report.json@@ -51,10 +51,10 @@},"frameMs": 16.7,"scrub": {- "p50": 16,- "p95": 32.3,- "mean": 18.48,- "max": 33.9,+ "p50": 15.9,+ "p95": 32.6,+ "mean": 18.4,+ "max": 32.8,"n": 41},"longTasks": {@@ -64,7 +64,7 @@"max": 0,"n": 0},- "liveSamples": 35,+ "liveSamples": 56,"machine": "desktop"},{@@ -81,10 +81,10 @@},"frameMs": 16.7,"scrub": {- "p50": 15.8,- "p95": 32.5,- "mean": 17.7,- "max": 33.3,+ "p50": 15.7,+ "p95": 32.2,+ "mean": 17.47,+ "max": 32.6,"n": 41},"longTasks": {@@ -94,7 +94,7 @@"max": 0,"n": 0},- "liveSamples": 45,+ "liveSamples": 53,"machine": "desktop"},{@@ -105,16 +105,16 @@"late": {"p50": 0,"p95": 0,- "mean": 0.02,+ "mean": 0.05,"max": 1,"n": 41},"frameMs": 16.7,"scrub": {- "p50": 15.5,- "p95": 17.4,- "mean": 15.9,- "max": 30.8,+ "p50": 14.2,+ "p95": 18.7,+ "mean": 13.39,+ "max": 32.3,"n": 41},"longTasks": {@@ -124,7 +124,7 @@"max": 0,"n": 0},- "liveSamples": 76,+ "liveSamples": 57,"machine": "desktop"},{@@ -141,9 +141,9 @@},"frameMs": 16.7,"scrub": {- "p50": 15.5,- "p95": 17.5,- "mean": 15.66,+ "p50": 15.7,+ "p95": 16.7,+ "mean": 15.69,"max": 32.3,"n": 81},@@ -172,9 +172,9 @@"frameMs": 16.7,"scrub": {"p50": 15.7,- "p95": 32.8,- "mean": 18.39,- "max": 33.8,+ "p95": 32.5,+ "mean": 18.43,+ "max": 32.8,"n": 41},"longTasks": {@@ -184,7 +184,7 @@"max": 0,"n": 0},- "liveSamples": 45,+ "liveSamples": 55,"machine": "desktop"},{@@ -195,16 +195,16 @@"late": {"p50": 0,"p95": 1,- "mean": 0.2,- "max": 2,+ "mean": 0.17,+ "max": 1,"n": 41},"frameMs": 16.7,"scrub": {- "p50": 15.9,- "p95": 32,- "mean": 18.77,- "max": 50.1,+ "p50": 15.7,+ "p95": 32.4,+ "mean": 18.42,+ "max": 32.5,"n": 41},"longTasks": {@@ -214,7 +214,7 @@"max": 0,"n": 0},- "liveSamples": 46,+ "liveSamples": 55,"machine": "desktop"},{@@ -225,16 +225,16 @@"late": {"p50": 1,"p95": 1,- "mean": 0.93,+ "mean": 0.9,"max": 1,"n": 41},"frameMs": 16.7,"scrub": {"p50": 32.3,- "p95": 33.5,- "mean": 31.07,- "max": 33.6,+ "p95": 33,+ "mean": 30.47,+ "max": 33.2,"n": 41},"longTasks": {@@ -326,13 +326,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 3241,+ "presented": 3243,"lag": {"p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 3241+ "n": 3243},"stallShare": 0,"decay": {@@ -363,13 +363,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 221,+ "presented": 222,"lag": {- "p50": 0.01,+ "p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 221+ "n": 222},"stallShare": 0,"decay": {@@ -437,13 +437,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 3444,+ "presented": 3443,"lag": {"p50": 0.01,"p95": 0.02,"mean": 0.01,"max": 0.02,- "n": 3444+ "n": 3443},"stallShare": 0,"decay": {@@ -474,13 +474,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 3444,+ "presented": 3430,"lag": {"p50": 0.02,"p95": 0.03,- "mean": 0.01,+ "mean": 0.02,"max": 0.03,- "n": 3444+ "n": 3430},"stallShare": 0,"decay": {@@ -492,11 +492,11 @@"reported": 0.04},"longTasks": {- "p50": 0,- "p95": 0,- "mean": 0,- "max": 0,- "n": 0+ "p50": 190,+ "p95": 190,+ "mean": 134.5,+ "max": 190,+ "n": 2},"liveSamples": 10,"liveSources": 1,@@ -511,13 +511,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 3444,+ "presented": 3446,"lag": {"p50": 0.02,"p95": 0.03,- "mean": 0.01,+ "mean": 0.02,"max": 0.03,- "n": 3444+ "n": 3446},"stallShare": 0,"decay": {@@ -529,10 +529,10 @@"reported": 0.04},"longTasks": {- "p50": 55,- "p95": 55,- "mean": 55,- "max": 55,+ "p50": 53,+ "p95": 53,+ "mean": 53,+ "max": 53,"n": 1},"liveSamples": 10,@@ -551,7 +551,7 @@"presented": 3444,"lag": {"p50": 0.01,- "p95": 0.01,+ "p95": 0.02,"mean": 0.01,"max": 0.02,"n": 3444@@ -576,7 +576,7 @@"liveSources": 1,"keptSources": 1,"warmMb": 0,- "memMb": 143,+ "memMb": 144,"machine": "desktop"},{@@ -589,7 +589,7 @@"lag": {"p50": 0.02,"p95": 0.03,- "mean": 0.02,+ "mean": 0.01,"max": 0.03,"n": 3444},@@ -622,13 +622,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 1620,+ "presented": 1621,"lag": {"p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 1620+ "n": 1621},"stallShare": 0,"decay": {@@ -694,9 +694,9 @@"drops": 2,"presented": 1094,"lag": {- "p50": 0.02,+ "p50": 0.01,"p95": 0.03,- "mean": 0.02,+ "mean": 0.01,"max": 0.03,"n": 1092},@@ -710,17 +710,17 @@"reported": 0.04},"longTasks": {- "p50": 120,- "p95": 120,- "mean": 94,- "max": 120,+ "p50": 128,+ "p95": 128,+ "mean": 97.5,+ "max": 128,"n": 2},"liveSamples": 10,"liveSources": 1,"keptSources": 1,"warmMb": 0,- "memMb": 218,+ "memMb": 209,"machine": "desktop"}]diff --git a/site/src/cut/components/OverlayLayer.tsx b/site/src/cut/components/OverlayLayer.tsxindex 27ecb816..1c5e1cca 100644--- a/site/src/cut/components/OverlayLayer.tsx+++ b/site/src/cut/components/OverlayLayer.tsx@@ -1,7 +1,7 @@"use client";import { guideSnapLines } from "@/cut/lib/guides";-import { createContext, Fragment, useCallback, useContext, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, type CSSProperties } from "react";+import { createContext, Fragment, useCallback, useContext, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore, type CSSProperties } from "react";import { createPortal } from "react-dom";import { releaseAnimRest, useAnimPreview } from "@/cut/lib/animPreview";import { startDrag } from "@/cut/lib/drag";@@ -39,7 +39,7 @@ import {SHADOW,wordDrawCss,} from "@/cut/lib/textRender";-import { cycleFaceAt, fitTextToFrame, textBoxSize } from "@/cut/lib/textFit";+import { cycleFaceAt, fitTextToFrame, textBoxSize, textFontOf, textLineLift } from "@/cut/lib/textFit";import {behindSubjectOverlay,clampKeyPos,@@ -443,6 +443,23 @@ export function OverlayLayer({/** The ghost copy's boxes stay out of the snap registry. */const ignoreBox = () => {};+/** Re-read a face's metrics when the page finishes loading one. */+const subscribeFaces = (changed: () => void) => {+ const fonts = typeof document === "undefined" ? undefined : document.fonts;+ fonts?.addEventListener?.("loadingdone", changed);+ return () => fonts?.removeEventListener?.("loadingdone", changed);+};++/** A title's lines set where the export's painter sets them. A CSS line box+ * centres the face's ascent and descent; the painter draws from the em box's+ * middle, a tenth of an em higher on a tall-ascent face (`textLineLift`). The+ * lift is relative, so the box, its plate and its chrome stay put and only the+ * type moves. */+function PainterLines({ font, children }: { font: string; children: React.ReactNode }) {+ const lift = useSyncExternalStore(subscribeFaces, () => textLineLift(font), () => 0);+ return <div style={lift ? { position: "relative", top: -lift } : undefined}>{children}</div>;+}+/** Text drawn word by word, each word wearing what the word engine resolved* for it. The words are walked exactly the way the canvas painter walks them* — line by line, single-spaced — so the word the preview lights is the word@@ -669,7 +686,9 @@ function SubtitleCaption({borderRadius: ov.plate ? PLATE_RADIUS_EM : undefined,}}>- {ov.wordDraw ? <WordText text={ov.text} draws={ov.wordDraw} /> : ov.text}+ <PainterLines font={textFontOf(ov, ov.size * scale)}>+ {ov.wordDraw ? <WordText text={ov.text} draws={ov.wordDraw} /> : ov.text}+ </PainterLines></div>);}@@ -1372,8 +1391,9 @@ function OverlayItem({<div style={contentStyle}>{behindHidden ? (<span className="opacity-0">{shownText}</span>- ) : isText ? (- editing ? (+ ) : isTextOverlay(o) ? (+ <PainterLines font={textFontOf(o, o.size * scale)}>+ {editing ? (<divref={editRef}className="min-w-2 whitespace-pre-wrap break-words outline-none select-text"@@ -1421,7 +1441,8 @@ function OverlayItem({{shownText}{caretBar}</span>- )+ )}+ </PainterLines>) : o.kind === "shape" && dealsMarks(o.shape) ? (<DoodleView shape={o} stageWidth={stageWidth} stageHeight={stageHeight} scale={scale} t={t} />) : o.kind === "shape" ? (diff --git a/site/src/cut/lib/textFit.ts b/site/src/cut/lib/textFit.tsindex d891160c..90177705 100644--- a/site/src/cut/lib/textFit.ts+++ b/site/src/cut/lib/textFit.ts@@ -41,6 +41,11 @@ function facesReady(): boolean {}const widths = new Map<string, number>();+/** Each face's lift (`textLineLift`), by css font. A face read while it was+ * still loading was read as its fallback, so the page finishing a load drops+ * the readings before anything reads them again. */+const lifts = new Map<string, number>();+if (typeof document !== "undefined") document.fonts?.addEventListener?.("loadingdone", () => lifts.clear());/*** Drop every measurement taken so far.@@ -54,6 +59,7 @@ const widths = new Map<string, number>();*/export function forgetTextWidths(): void {widths.clear();+ lifts.clear();settled = new WeakMap();forgetSettledFaces();}@@ -87,6 +93,42 @@ export function measureLine(return w;}+/**+ * How far above a CSS line the painter sets the same line's type, in pixels+ * of `cssFont`'s own size.+ *+ * 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.+ */+export function textLineLift(cssFont: string): number {+ const hit = lifts.get(cssFont);+ if (hit !== undefined) return hit;+ const ctx = measureCtx();+ let lift = 0;+ if (ctx) {+ ctx.font = cssFont;+ 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;+ if (Number.isFinite(cssDrop) && Number.isFinite(painterDrop)) lift = cssDrop - painterDrop;+ }+ // Bounded: every stage size is a size of its own.+ if (lifts.size > 500) lifts.clear();+ lifts.set(cssFont, lift);+ return lift;+}+/** 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(diff --git a/site/src/cut/lib/textLift.test.ts b/site/src/cut/lib/textLift.test.tsnew file mode 100644index 00000000..201af66b--- /dev/null+++ b/site/src/cut/lib/textLift.test.ts@@ -0,0 +1,51 @@+import { expect, test } from "bun:test";+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.+//+// 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+// ascent of "Hg" measured from the alphabetic and from the middle baseline.+const JOST_110 = { ascent: 118, descent: 41, alphabetic: 77, middle: 50.546875 };++/** A canvas reporting a face's metrics the way a browser's does. The+ * measurer keeps the first canvas it is handed, so the one canvas is what+ * each test re-reads. */+const reading = { lineMetrics: true };+const ctx = {+ font: "",+ letterSpacing: "0px",+ textBaseline: "alphabetic" as CanvasTextBaseline,+ measureText(text: string) {+ return {+ width: text.length * 60,+ actualBoundingBoxAscent: this.textBaseline === "middle" ? JOST_110.middle : JOST_110.alphabetic,+ ...(reading.lineMetrics+ ? { fontBoundingBoxAscent: JOST_110.ascent, fontBoundingBoxDescent: JOST_110.descent }+ : {}),+ };+ },+};+setRasterFactory({ createCanvas: () => ({ getContext: () => ctx }) } as unknown as RasterFactory);++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;+ const lift = textLineLift('700 110px "Jost"');+ expect(cssDrop - lift).toBeCloseTo(painterDrop, 6);+ // The offset the turned columns showed on a 1920 frame.+ expect(lift).toBeCloseTo(12.05, 2);+});++test("a canvas that cannot read a face's line metrics lifts nothing", () => {+ reading.lineMetrics = false;+ expect(textLineLift('700 109px "Jost"')).toBe(0);+});