Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
fix(agent): fit a text annotation to its words in the shipped fonts, and refit it on a format change
Etienne Lescot2a6379b3f7cf
diff --git a/electron/ai-edition/agent-tools.test.ts b/electron/ai-edition/agent-tools.test.tsindex da1a68171..a5730b583 100644--- a/electron/ai-edition/agent-tools.test.ts+++ b/electron/ai-edition/agent-tools.test.ts@@ -1,4 +1,5 @@-import { describe, expect, it } from "vitest";+import path from "node:path";+import { describe, expect, it, vi } from "vitest";import {type AxcutAnnotationRegion,type AxcutDocument,@@ -7,6 +8,7 @@ import {} from "../../src/lib/ai-edition/schema";import { ZOOM_DEPTH_SCALES } from "../../src/lib/ai-edition/timeline/zoom-scale";import { executeAgentTool, isMutatingTool, MUTATING_TOOL_NAMES } from "./agent-tools";+import { measureTextInShippedFonts } from "./text-measure";/** ponytail: every fixture in this file starts from the SAME instant.* `createEmptyDocument` reads the wall clock when a caller hands it no@@ -843,6 +845,60 @@ describe("executeAgentTool", () => {).toBe(false);});+ it("fits a title to its words in the shipped font, in 16:9 and once switched to 9:16", () => {+ // The e2e pass's turn: a bold Inter title at 64 px, then the vertical format. Measured with+ // the estimate Node used to fall back on, the box held 554 px of text for 587 px of words,+ // and the 9:16 export showed "Demo" with "OpenScreen" wrapped out of the box.+ vi.stubEnv("OPENSCREEN_FONTS_DIR", path.resolve(__dirname, "../../public/fonts"));+ try {+ /** The room the box leaves its words, inside the plate, against their measured width. */+ const room = (ann: AxcutAnnotationRegion | undefined, aspect: number) => {+ if (!ann) throw new Error("no annotation");+ const box = (ann.size.width / 100) * 1080 * aspect - 0.4 * ann.style.fontSize;+ return box - measureTextInShippedFonts(ann.content ?? "", ann.style).width;+ };+ let document = executeAgentTool(+ fixtureDocument(),+ "setAspectRatio",+ JSON.stringify({ aspectRatio: "16:9" }),+ ).document as AxcutDocument;+ const added = executeAgentTool(+ document,+ "addAnnotation",+ JSON.stringify({ startSec: 1, endSec: 4, text: "Demo OpenScreen", fontSize: 64, x: 50 }),+ );+ const id = JSON.parse(added.resultJson).annotationId;+ document = added.document as AxcutDocument;+ const wide = document.annotations.find((a) => a.id === id);+ expect(wide?.style.fontSize).toBe(64);+ expect(room(wide, 16 / 9)).toBeGreaterThan(0);++ document = executeAgentTool(+ document,+ "setAspectRatio",+ JSON.stringify({ aspectRatio: "9:16" }),+ ).document as AxcutDocument;+ const tall = document.annotations.find((a) => a.id === id);+ expect(room(tall, 9 / 16)).toBeGreaterThan(0);+ expect(tall?.size.width).toBeLessThanOrEqual(100);+ // 64 px of Inter bold does not fit a portrait frame's width: brought down just enough.+ expect(tall?.style.fontSize).toBeLessThan(64);+ expect(tall?.style.fontSize).toBeGreaterThan(55);+ expect(centreOf(tall)).toEqual({ x: expect.closeTo(50, 5), y: expect.closeTo(50, 5) });++ // Added straight into the portrait frame, the same title fits too.+ const direct = executeAgentTool(+ document,+ "addAnnotation",+ JSON.stringify({ startSec: 5, endSec: 6, text: "Demo OpenScreen", fontSize: 64 }),+ ).document?.annotations.at(-1);+ expect(room(direct, 9 / 16)).toBeGreaterThan(0);+ expect(direct?.size.width).toBeLessThanOrEqual(100);+ } finally {+ vi.unstubAllEnvs();+ }+ });+it("refuses a set that would run into another region, instead of landing it elsewhere", () => {let document = fixtureDocument();const add = (name: string, args: Record<string, unknown>) => {diff --git a/electron/ai-edition/agent-tools.ts b/electron/ai-edition/agent-tools.tsindex cecef747a..de60eea44 100644--- a/electron/ai-edition/agent-tools.ts+++ b/electron/ai-edition/agent-tools.ts@@ -17,7 +17,7 @@import { z } from "zod";import { DEFAULT_TEXT_PLATE } from "../../src/lib/ai-edition/annotations/background";-import { fitTextBox } from "../../src/lib/ai-edition/annotations/placement";+import { fitTextInFrame } from "../../src/lib/ai-edition/annotations/placement";import {collapseTracksToPills,patchAudioTrack,@@ -67,6 +67,7 @@ import {} from "../../src/lib/ai-edition/timeline/zoom-transitions";import { SETTING_BOUNDS } from "../../src/lib/projectDefaults";import { ASPECT_RATIO_PRESETS } from "../../src/utils/aspectRatioUtils";+import { measureTextInShippedFonts } from "./text-measure";export interface AgentToolExecution {ok: boolean;@@ -200,7 +201,8 @@ function applyAnnotationLook(/*** A text annotation on the output frame, its box sized to its text around `center` (frame* percentages): what the editor's "add text" does, so the agent's text sits where the editor's- * would, is measured the same, and stays put when the picture zooms.+ * would, is measured in the same fonts, and stays put when the picture zooms. A size too large for+ * the frame's width is brought down to fit on its line, as a format change does.*/function placeTextOnFrame<T extends AxcutAnnotationRegion>(region: T,@@ -214,7 +216,7 @@ function placeTextOnFrame<T extends AxcutAnnotationRegion>(position: center,size: { width: 0, height: 0 },};- return { ...centred, ...fitTextBox(centred, frameAspect) };+ return fitTextInFrame(centred, frameAspect, measureTextInShippedFonts);}// v5: a modifier is stored as clip-anchored fragment(s), and adjacent regions with the@@ -1461,8 +1463,13 @@ export function executeAgentTool(const parsed = setAspectRatioArgs.safeParse(args);if (!parsed.success) return failure(parsed.error.message);const previous = getEditorSettings(document).aspectRatio;- // The editor's own write: the frame-placed annotations keep their shape and centre.- const next = applySettingsPatch(document, { aspectRatio: parsed.data.aspectRatio });+ // The editor's own write: the frame-placed annotations keep their centre, and a text is+ // refitted to its words in the new frame, measured in the shipped fonts.+ const next = applySettingsPatch(+ document,+ { aspectRatio: parsed.data.aspectRatio },+ measureTextInShippedFonts,+ );return {ok: true,document: next,diff --git a/electron/ai-edition/text-measure.ts b/electron/ai-edition/text-measure.tsnew file mode 100644index 000000000..6f4ace490--- /dev/null+++ b/electron/ai-edition/text-measure.ts@@ -0,0 +1,50 @@+// Text measured in the main process, where the agent's tools run.+//+// The editor sizes a text box with a canvas, in the fonts the compositor draws. Node has no+// canvas, so the agent's `fitTextBox` fell back to an average advance that is too narrow for+// Inter bold, and the compositor wrapped the last word of an agent's title out of its box. This+// reads the same font files instead (`public/fonts` in dev, `<resources>/fonts` packaged, the+// directory `OPENSCREEN_FONTS_DIR` names once the compositor is loaded).++import fs from "node:fs";+import path from "node:path";+import {+ type FontMetrics,+ measureWithFontTables,+ readFontMetrics,+} from "../../src/lib/ai-edition/annotations/fontTables";+import { textFontFile } from "../../src/lib/textFonts";++/** The directories the shipped font files may be in, most specific first. */+function fontDirs(): string[] {+ return [+ process.env.OPENSCREEN_FONTS_DIR,+ process.env.VITE_PUBLIC && path.join(process.env.VITE_PUBLIC, "fonts"),+ process.resourcesPath && path.join(process.resourcesPath, "fonts"),+ ].filter((dir): dir is string => typeof dir === "string" && dir.length > 0);+}++const parsed = new Map<string, FontMetrics | null>();++function readFont(file: string): FontMetrics | null {+ if (!parsed.has(file)) {+ let metrics: FontMetrics | null = null;+ try {+ metrics = readFontMetrics(fs.readFileSync(file));+ } catch {+ // Unreadable: measured by the estimate, like a missing file.+ }+ parsed.set(file, metrics);+ }+ return parsed.get(file) ?? null;+}++/** The words of a text annotation, measured in the font files the compositor draws them in. */+export const measureTextInShippedFonts = measureWithFontTables((family, weight) => {+ const name = path.basename(textFontFile(family, weight));+ for (const dir of fontDirs()) {+ const file = path.join(dir, name);+ if (fs.existsSync(file)) return readFont(file);+ }+ return null;+});diff --git a/src/lib/ai-edition/annotations/fontTables.test.ts b/src/lib/ai-edition/annotations/fontTables.test.tsnew file mode 100644index 000000000..892a4450a--- /dev/null+++ b/src/lib/ai-edition/annotations/fontTables.test.ts@@ -0,0 +1,95 @@+import { readFileSync } from "node:fs";+import path from "node:path";+import { describe, expect, it } from "vitest";+import { TEXT_FONT_FAMILIES, textFontFile } from "@/lib/textFonts";+import { type FontMetrics, measureWithFontTables, readFontMetrics } from "./fontTables";++const FONTS = path.resolve(__dirname, "../../../../public");++function shipped(family: (typeof TEXT_FONT_FAMILIES)[number], weight: "normal" | "bold") {+ const metrics = readFontMetrics(readFileSync(path.join(FONTS, textFontFile(family, weight))));+ if (!metrics) throw new Error(`${family} ${weight} unreadable`);+ return metrics;+}++const style = (+ overrides: Partial<Parameters<ReturnType<typeof measureWithFontTables>>[1]> = {},+) => ({+ color: "#ffffff",+ backgroundColor: "rgba(0, 0, 0, 0.7)",+ fontSize: 64,+ fontFamily: "Inter",+ fontWeight: "bold" as const,+ fontStyle: "normal" as const,+ textDecoration: "none" as const,+ textAlign: "center" as const,+ textAnimation: "none" as const,+ ...overrides,+});++describe("readFontMetrics", () => {+ it("reads every shipped font", () => {+ for (const family of TEXT_FONT_FAMILIES) {+ for (const weight of ["normal", "bold"] as const) {+ const font = shipped(family, weight);+ expect(font.unitsPerEm).toBeGreaterThan(0);+ expect(font.lineHeight).toBeGreaterThan(font.unitsPerEm);+ expect(font.advanceOf("A".codePointAt(0) ?? 0)).toBeGreaterThan(0);+ }+ }+ });++ it("knows a glyph the font lacks from one it has", () => {+ const inter = shipped("Inter", "bold");+ expect(inter.advanceOf(" ".codePointAt(0) ?? 0)).toBeGreaterThan(0);+ // A CJK ideograph: not in Inter.+ expect(inter.advanceOf(0x4e2d)).toBeNull();+ });++ it("refuses bytes that are not a font", () => {+ expect(readFontMetrics(new Uint8Array([1, 2, 3, 4]))).toBeNull();+ });+});++describe("measureWithFontTables", () => {+ const measure = measureWithFontTables(shipped);++ it("measures Inter bold at its real width, wider than the old estimate", () => {+ // What the chat agent stored for "Demo OpenScreen" at 64 px: 15 × 0.56 em, 538 px. Inter+ // bold draws it 586 px wide, and the compositor wrapped the second word out of the box.+ const block = measure("Demo OpenScreen", style());+ expect(block.width).toBeCloseTo(586.5, 1);+ expect(block.width).toBeGreaterThan(15 * 0.56 * 64 + 40);+ });++ it("tells the weights and the families apart, and scales with the size", () => {+ const bold = measure("Demo OpenScreen", style());+ expect(measure("Demo OpenScreen", style({ fontWeight: "normal" })).width).toBeLessThan(+ bold.width,+ );+ expect(measure("Demo OpenScreen", style({ fontFamily: "Oswald" })).width).toBeLessThan(+ bold.width,+ );+ expect(measure("Demo OpenScreen", style({ fontSize: 32 })).width).toBeCloseTo(+ bold.width / 2,+ 6,+ );+ });++ it("takes the longest line, and one line height per line", () => {+ const one = measure("Demo", style());+ const two = measure("Demo\nOpenScreen", style());+ expect(two.width).toBeCloseTo(measure("OpenScreen", style()).width, 6);+ expect(two.height).toBeCloseTo(2 * one.height, 6);+ // Inter's hhea line is about 1.21 em.+ expect(one.height / 64).toBeGreaterThan(1.1);+ expect(one.height / 64).toBeLessThan(1.35);+ });++ it("counts a character the font lacks as an em, and falls back without the font", () => {+ expect(measure("中", style()).width).toBeCloseTo(64, 6);+ const none: (f: string, w: string) => FontMetrics | null = () => null;+ const fallback = measureWithFontTables(none)("Demo OpenScreen", style());+ expect(fallback.width).toBeCloseTo(15 * 0.56 * 64, 6);+ });+});diff --git a/src/lib/ai-edition/annotations/fontTables.ts b/src/lib/ai-edition/annotations/fontTables.tsnew file mode 100644index 000000000..1f7aa0d33--- /dev/null+++ b/src/lib/ai-edition/annotations/fontTables.ts@@ -0,0 +1,162 @@+// Mesurer un texte sans canvas : les chasses lues dans les tables du fichier de police.+//+// L'agent du chat tourne dans le processus principal d'Electron, où il n'y a ni `OffscreenCanvas`+// ni `document.fonts`. `measureTextBlock` y retombait sur une chasse moyenne de 0,56 em, trop+// étroite pour l'Inter gras d'une annotation : « Demo OpenScreen » à 64 px mesurait 538 px pour+// 610 px réels, et le compositeur renvoyait « OpenScreen » à la ligne, hors de la boîte.+//+// Les polices livrées (`public/fonts`) sont des TrueType statiques : `cmap` donne le glyphe d'un+// caractère, `hmtx` sa chasse, `hhea` la hauteur de ligne que Chromium rend dans+// `fontBoundingBoxAscent + fontBoundingBoxDescent`. Le crénage (GPOS) est ignoré : il resserre+// presque toujours, la somme des chasses majore donc la largeur rendue.++import type { AxcutAnnotationRegion } from "@/lib/ai-edition/schema";+import { resolveTextFontFamily, type TextFontFamily } from "@/lib/textFonts";+import { type MeasureText, measureTextBlock } from "./placement";++/** Ce qu'il faut d'une police pour mesurer une ligne, en unités de sa grille (`unitsPerEm`). */+export interface FontMetrics {+ unitsPerEm: number;+ /** Hauteur d'une ligne : `hhea.ascender - hhea.descender`. */+ lineHeight: number;+ /** La chasse du glyphe d'un point de code, `null` s'il n'est pas dans la police. */+ advanceOf: (codePoint: number) => number | null;+}++type Table = { offset: number; length: number };++function tablesOf(view: DataView): Map<string, Table> {+ const tables = new Map<string, Table>();+ const count = view.getUint16(4);+ for (let i = 0; i < count; i++) {+ const record = 12 + 16 * i;+ const tag = String.fromCharCode(+ view.getUint8(record),+ view.getUint8(record + 1),+ view.getUint8(record + 2),+ view.getUint8(record + 3),+ );+ tables.set(tag, { offset: view.getUint32(record + 8), length: view.getUint32(record + 12) });+ }+ return tables;+}++/** Le glyphe d'un point de code, d'une sous-table `cmap` au format 4 (BMP) ou 12 (tout Unicode). */+function glyphLookup(view: DataView, cmap: Table): ((codePoint: number) => number) | null {+ const count = view.getUint16(cmap.offset + 2);+ let best: { format: number; offset: number } | null = null;+ for (let i = 0; i < count; i++) {+ const record = cmap.offset + 4 + 8 * i;+ const platform = view.getUint16(record);+ const encoding = view.getUint16(record + 2);+ const offset = cmap.offset + view.getUint32(record + 4);+ const unicode = platform === 0 || (platform === 3 && (encoding === 1 || encoding === 10));+ if (!unicode) continue;+ const format = view.getUint16(offset);+ if (format === 12) {+ best = { format, offset };+ break;+ }+ if (format === 4 && !best) best = { format, offset };+ }+ if (!best) return null;+ const at = best.offset;+ if (best.format === 12) {+ const groups = view.getUint32(at + 12);+ return (codePoint) => {+ for (let g = 0; g < groups; g++) {+ const group = at + 16 + 12 * g;+ const start = view.getUint32(group);+ if (codePoint < start) return 0;+ if (codePoint <= view.getUint32(group + 4)) {+ return view.getUint32(group + 8) + (codePoint - start);+ }+ }+ return 0;+ };+ }+ const segments = view.getUint16(at + 6) / 2;+ const ends = at + 14;+ const starts = ends + 2 * segments + 2;+ const deltas = starts + 2 * segments;+ const rangeOffsets = deltas + 2 * segments;+ return (codePoint) => {+ if (codePoint > 0xffff) return 0;+ for (let s = 0; s < segments; s++) {+ if (codePoint > view.getUint16(ends + 2 * s)) continue;+ const start = view.getUint16(starts + 2 * s);+ if (codePoint < start) return 0;+ const delta = view.getUint16(deltas + 2 * s);+ const rangeOffset = view.getUint16(rangeOffsets + 2 * s);+ if (rangeOffset === 0) return (codePoint + delta) & 0xffff;+ const glyph = view.getUint16(rangeOffsets + 2 * s + rangeOffset + 2 * (codePoint - start));+ return glyph === 0 ? 0 : (glyph + delta) & 0xffff;+ }+ return 0;+ };+}++/** Les tables d'un fichier TrueType ou OpenType ; `null` pour ce qui n'en est pas un. */+export function readFontMetrics(bytes: Uint8Array): FontMetrics | null {+ try {+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);+ const tables = tablesOf(view);+ const head = tables.get("head");+ const hhea = tables.get("hhea");+ const hmtx = tables.get("hmtx");+ const cmap = tables.get("cmap");+ if (!head || !hhea || !hmtx || !cmap) return null;+ const unitsPerEm = view.getUint16(head.offset + 18);+ const lineHeight = view.getInt16(hhea.offset + 4) - view.getInt16(hhea.offset + 6);+ const metrics = view.getUint16(hhea.offset + 34);+ const glyphOf = glyphLookup(view, cmap);+ if (!glyphOf || !(unitsPerEm > 0) || metrics === 0) return null;+ return {+ unitsPerEm,+ lineHeight,+ advanceOf: (codePoint) => {+ const glyph = glyphOf(codePoint);+ if (glyph === 0) return null;+ // Au-delà de `numberOfHMetrics`, chaque glyphe reprend la dernière chasse.+ return view.getUint16(hmtx.offset + 4 * Math.min(glyph, metrics - 1));+ },+ };+ } catch {+ return null;+ }+}++// Un caractère absent de la police est dessiné dans une police de secours (un idéogramme, un+// emoji) : compté pour un cadratin, plutôt large.+const MISSING_ADVANCE_EM = 1;++/**+ * Un `MeasureText` qui lit les chasses dans les polices que le compositeur dessine. `fontFor`+ * donne la police d'une famille et d'une graisse, `null` quand elle manque : ce style-là est+ * alors mesuré par `measureTextBlock`, comme avant.+ */+export function measureWithFontTables(+ fontFor: (family: TextFontFamily, weight: "normal" | "bold") => FontMetrics | null,+): MeasureText {+ return (content: string, style: AxcutAnnotationRegion["style"]) => {+ const font = fontFor(+ resolveTextFontFamily(style.fontFamily),+ style.fontWeight === "bold" ? "bold" : "normal",+ );+ if (!font) return measureTextBlock(content, style);+ const scale = style.fontSize / font.unitsPerEm;+ const lineWidth = (line: string) => {+ let units = 0;+ for (const char of line) {+ const codePoint = char.codePointAt(0) ?? 0;+ units += font.advanceOf(codePoint) ?? MISSING_ADVANCE_EM * font.unitsPerEm;+ }+ return units * scale;+ };+ const lines = content.split("\n");+ return {+ width: Math.max(0, ...lines.map(lineWidth)),+ height: lines.length * font.lineHeight * scale,+ };+ };+}diff --git a/src/lib/ai-edition/annotations/placement.test.ts b/src/lib/ai-edition/annotations/placement.test.tsindex 6d6dfeaa0..94e485436 100644--- a/src/lib/ai-edition/annotations/placement.test.ts+++ b/src/lib/ai-edition/annotations/placement.test.ts@@ -3,6 +3,7 @@ import type { AxcutAnnotationRegion } from "@/lib/ai-edition/schema";import {convertAnnotationKind,fitTextBox,+ fitTextInFrame,type MeasureText,measureTextBlock,refitFrameAnnotations,@@ -194,14 +195,84 @@ describe("convertAnnotationKind", () => {});});+/** A text whose width grows with its size, as a real one does: 20 px per px of font. */+const proportional: MeasureText = (_content, style) => ({+ width: 20 * style.fontSize,+ height: 1.2 * style.fontSize,+});++/** The box's width in pixels at the reference height, in a frame of `aspect`. */+const widthPx = (r: Pick<AxcutAnnotationRegion, "size">, aspect: number) =>+ (r.size.width / 100) * 1080 * aspect;++describe("fitTextInFrame", () => {+ it("is fitTextBox while the text fits the frame's width", () => {+ const text = region({ space: "frame" });+ expect(fitTextInFrame(text, WIDE, measure)).toEqual({+ ...text,+ ...fitTextBox(text, WIDE, measure),+ });+ });++ it("brings a size the frame cannot hold on one line down until it fits", () => {+ // 32 px: 640 px of text, 661 px of box, in a 9:16 frame 607.5 px wide at the reference+ // height.+ const text = region({+ space: "frame",+ position: { x: 45, y: 40 },+ size: { width: 10, height: 6 },+ });+ const fitted = fitTextInFrame(text, 9 / 16, proportional);+ expect(fitted.style.fontSize).toBe(29);+ expect(fitted.size.width).toBeLessThanOrEqual(100);+ expect(fitted).toMatchObject(fitTextBox(fitted, 9 / 16, proportional));+ expect(center(fitted).y).toBeCloseTo(43, 6);+ });+});+describe("refitFrameAnnotations", () => {- it("keeps each frame annotation's shape and centre across a format change", () => {+ it("refits a text box to its words in the new frame, around its centre", () => {+ // Stored too narrow for its words, as the chat agent's estimate left it.const text = region({space: "frame",position: { x: 45, y: 40 },size: { width: 10, height: 6 },});- const [after] = refitFrameAnnotations([text], WIDE, 9 / 16);+ const [after] = refitFrameAnnotations([text], WIDE, 9 / 16, measure);+ expect(after.size).toEqual(fitTextBox(text, 9 / 16, measure).size);+ expect(center(after).x).toBeCloseTo(50, 6);+ expect(center(after).y).toBeCloseTo(43, 6);+ expect(after.style).toBe(text.style);+ // The same box in pixels as a fit in the old frame: the text keeps its line.+ const before = fitTextBox(text, WIDE, measure);+ expect(widthPx(after, 9 / 16)).toBeCloseTo(widthPx(before, WIDE), 6);+ // And back: a 9:16 box refitted to 16:9 is the 16:9 fit.+ const [back] = refitFrameAnnotations([after], 9 / 16, WIDE, measure);+ expect(back.size.width).toBeCloseTo(before.size.width, 6);+ });++ it("brings a text down to the new frame's width, its box with it", () => {+ const banner = region({+ space: "frame",+ position: { x: 33, y: 40 },+ size: { width: 34, height: 6 },+ });+ const [wide] = refitFrameAnnotations([banner], 9 / 16, WIDE, proportional);+ expect(wide.style.fontSize).toBe(32);+ const [tall] = refitFrameAnnotations([banner], WIDE, 9 / 16, proportional);+ expect(tall.style.fontSize).toBe(29);+ expect(tall.size.width).toBeLessThanOrEqual(100);+ expect(tall.position.x).toBeGreaterThanOrEqual(0);+ });++ it("keeps an image's shape and centre across a format change", () => {+ const image = region({+ type: "image",+ space: "frame",+ position: { x: 45, y: 40 },+ size: { width: 10, height: 6 },+ });+ const [after] = refitFrameAnnotations([image], WIDE, 9 / 16);// A width measured on the frame's height: three times the share of a frame three times// narrower in pixels.expect(after.size.width).toBeCloseTo(10 * (WIDE / (9 / 16)), 6);@@ -210,13 +281,14 @@ describe("refitFrameAnnotations", () => {expect(after.position.y).toBe(40);});- it("shrinks one that no longer fits the frame's width, its text with it", () => {- const banner = region({+ it("shrinks an image that no longer fits the frame's width", () => {+ const image = region({+ type: "image",space: "frame",position: { x: 30, y: 40 },size: { width: 40, height: 6 },});- const [after] = refitFrameAnnotations([banner], WIDE, 9 / 16);+ const [after] = refitFrameAnnotations([image], WIDE, 9 / 16);// 40 % of a 16:9 frame is 126 % of a 9:16 one: brought back to the full width, around the// same centre, and shrunk as a whole.const shrink = 100 / (40 * (WIDE / (9 / 16)));@@ -224,10 +296,7 @@ describe("refitFrameAnnotations", () => {expect(after.position.x).toBe(0);expect(after.size.height).toBeCloseTo(6 * shrink, 6);expect(center(after).y).toBeCloseTo(43, 6);- // Rounded down, so the words still fit the shrunk box.- expect(after.style.fontSize).toBe(Math.floor(32 * shrink));- const image = region({ type: "image", space: "frame", size: { width: 40, height: 6 } });- expect(refitFrameAnnotations([image], WIDE, 9 / 16)[0].style).toBe(image.style);+ expect(after.style).toBe(image.style);});it("leaves the footage's annotations, and an unchanged format, alone", () => {diff --git a/src/lib/ai-edition/annotations/placement.ts b/src/lib/ai-edition/annotations/placement.tsindex 5b1699cbf..ffcf1da0f 100644--- a/src/lib/ai-edition/annotations/placement.ts+++ b/src/lib/ai-edition/annotations/placement.ts@@ -206,6 +206,27 @@ export function fitTextBox();}+/**+ * `fitTextBox`, pour un texte qui doit tenir dans la largeur du cadre : trop large pour lui, sa+ * police est réduite juste assez, arrondie vers le bas, puis sa boîte retaillée sur la nouvelle+ * taille. Sans ça, un titre posé en 16:9 et passé en 9:16 garde une police que le cadre portrait,+ * 3,2 fois plus étroit, ne peut pas contenir sur une ligne, et le compositeur la coupe en deux.+ */+export function fitTextInFrame<T extends Region>(+ region: T,+ frameAspect: number,+ measure: MeasureText = measureTextBlock,+): T {+ const fitted = { ...region, ...fitTextBox(region, frameAspect, measure) };+ if (fitted.size.width <= 100) return fitted;+ const fontSize = clampToBound(+ Math.floor(region.style.fontSize * (100 / fitted.size.width)),+ "annotationFontSize",+ );+ const smaller = { ...region, style: { ...region.style, fontSize } };+ return { ...smaller, ...fitTextBox(smaller, frameAspect, measure) };+}+/*** Patch à appliquer quand l'utilisateur change le type d'une annotation.*@@ -290,16 +311,19 @@ export function convertAnnotationKind(/*** Les annotations du cadre après un changement de format, de `fromAspect` à `toAspect` (largeur- * sur hauteur). Chacune garde son centre, et sa forme à l'image : sa largeur est mesurée, comme- * la police, sur la hauteur du cadre. Sans ça, un texte taillé en 16:9 garde en 9:16 le même- * pourcentage d'une largeur trois fois plus petite en pixels, et le compositeur le renvoie à la- * ligne ; une flèche penche. Celle qui ne tiendrait plus en largeur est réduite pour y tenir. Le- * reste ne bouge pas : un flou suit le footage, qui garde son ratio.+ * sur hauteur). Chacune garde son centre. Un texte est retaillé sur ses mots dans le nouveau cadre+ * (`fitTextInFrame`), comme le ferait l'inspecteur : une boîte mal mesurée à sa création ne reste+ * pas fausse, et une police que le cadre ne peut plus contenir sur une ligne est réduite. Une image+ * ou une flèche garde sa forme à l'image : sa largeur est mesurée, comme la police, sur la hauteur+ * du cadre, sans quoi elle se déformerait (une flèche penche) ; celle qui ne tiendrait plus en+ * largeur est réduite pour y tenir. Le reste ne bouge pas : un flou suit le footage, qui garde son+ * ratio.*/export function refitFrameAnnotations<T extends Region>(annotations: T[],fromAspect: number,toAspect: number,+ measure: MeasureText = measureTextBlock,): T[] {if (!(fromAspect > 0 && toAspect > 0) || Math.abs(fromAspect - toAspect) < 1e-6) {return annotations;@@ -307,22 +331,12 @@ export function refitFrameAnnotations<T extends Region>(const scale = fromAspect / toAspect;return annotations.map((annotation) => {if (annotation.space !== "frame") return annotation;+ if (annotation.type === "text") return fitTextInFrame(annotation, toAspect, measure);const rect = rectOf(annotation);const width = rect.width * scale;- // Plus large que le nouveau cadre : réduite d'un bloc, son texte avec elle, pour rester à- // l'image au lieu d'en déborder.+ // Plus large que le nouveau cadre : réduite d'un bloc pour rester à l'image au lieu d'en+ // déborder.const shrink = Math.min(1, 100 / width);- const refit = {- ...annotation,- ...around(centerOf(rect), width * shrink, rect.height * shrink),- };- if (shrink === 1 || annotation.type !== "text") return refit;- // Arrondie vers le bas : un texte plus grand d'un cheveu que sa boîte réduite passerait à la- // ligne.- const fontSize = clampToBound(- Math.floor(annotation.style.fontSize * shrink),- "annotationFontSize",- );- return { ...refit, style: { ...annotation.style, fontSize } };+ return { ...annotation, ...around(centerOf(rect), width * shrink, rect.height * shrink) };});}diff --git a/src/lib/ai-edition/store/settingsPatch.ts b/src/lib/ai-edition/store/settingsPatch.tsindex 89d00dc6b..3b34606b4 100644--- a/src/lib/ai-edition/store/settingsPatch.ts+++ b/src/lib/ai-edition/store/settingsPatch.ts@@ -1,17 +1,22 @@// A settings patch applied to a document, with what it implies for the rest of it. Pure, so the// editor's hook and the agent's tools apply a setting through the same code.-import { refitFrameAnnotations } from "../annotations/placement";+import { type MeasureText, refitFrameAnnotations } from "../annotations/placement";import { resolveAspectRatioValue } from "../document/outputFormat";import type { AxcutDocument } from "../schema";import { type EditorSettingsPatch, getEditorSettings, patchEditorSettings } from "./editorSettings";/*** `patchEditorSettings`, plus what a new format does to the annotations placed on the frame:- * each keeps its shape and its centre (`refitFrameAnnotations`), in the same write, so one undo- * takes both back.+ * each keeps its centre, and a text its fit to its words (`refitFrameAnnotations`), in the same+ * write, so one undo takes both back. `measure` measures those words: the renderer's canvas by+ * default, the font files where there is no canvas (the agent, in the main process).*/-export function applySettingsPatch(doc: AxcutDocument, patch: EditorSettingsPatch): AxcutDocument {+export function applySettingsPatch(+ doc: AxcutDocument,+ patch: EditorSettingsPatch,+ measure?: MeasureText,+): AxcutDocument {const next = patchEditorSettings(doc, patch);// ponytail: only a format picked in the settings refits. Under Auto the frame can also// reshape when clips change, and those annotations keep their shares of the frame; refit@@ -21,6 +26,7 @@ export function applySettingsPatch(doc: AxcutDocument, patch: EditorSettingsPatcnext.annotations,resolveAspectRatioValue(doc, getEditorSettings(doc).aspectRatio),resolveAspectRatioValue(next, getEditorSettings(next).aspectRatio),+ measure,);return annotations === next.annotations ? next : { ...next, annotations };}diff --git a/src/lib/ai-edition/store/useEditorSettings.test.ts b/src/lib/ai-edition/store/useEditorSettings.test.tsindex bb991aa1b..f482b6c4a 100644--- a/src/lib/ai-edition/store/useEditorSettings.test.ts+++ b/src/lib/ai-edition/store/useEditorSettings.test.ts@@ -12,6 +12,7 @@import { act, renderHook } from "@testing-library/react";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { fitTextBox } from "../annotations/placement";import type { AxcutDocument } from "../schema";import { axcutSchemaVersion } from "../schema";import { getEditorSettings } from "./editorSettings";@@ -205,9 +206,8 @@ describe("useEditorSettings drag snapshots", () => {});const after = useProjectStore.getState().document?.annotations[0];- // Its width is a share of a frame now 3.16 times narrower in pixels: the share grows by- // as much, around the same centre, so the text keeps its line and its place.- expect(after?.size.width).toBeCloseTo((10 * (16 / 9)) / (9 / 16), 6);+ // Refitted to its words in the 9:16 frame, as the inspector would, around the same centre.+ expect(after?.size).toEqual(fitTextBox(text, 9 / 16).size);expect((after?.position.x ?? 0) + (after?.size.width ?? 0) / 2).toBeCloseTo(50, 6);expect(undo()).toBe(true);expect(useProjectStore.getState().document?.annotations[0]).toEqual(text);