Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

feat(captions): measure the caption band against the frame, not the footage

EtienneLescotd585f3a366a4
diff --git a/src/lib/ai-edition/captions/captions.test.ts b/src/lib/ai-edition/captions/captions.test.ts
index 96a83473a..61fd5ef4d 100644
--- a/src/lib/ai-edition/captions/captions.test.ts
+++ b/src/lib/ai-edition/captions/captions.test.ts
@@ -2,8 +2,11 @@ import { describe, expect, it } from "vitest";
import type { AxcutDocument, AxcutTranscript } from "../schema";
import { captionCuesToTextRegions, deriveCaptionCues } from "./cues";
import {
+ CAPTION_BAND_HEIGHT_PCT,
captionBackgroundCss,
captionBandRect,
+ captionInkHeightPct,
+ captionOffsetRange,
DEFAULT_CAPTION_SETTINGS,
getCaptionSettings,
patchCaptionSettings,
@@ -114,11 +117,72 @@ describe("caption settings", () => {
expect(getCaptionSettings(next)).toMatchObject({ minWordsPerLine: 3, maxWordsPerLine: 9 });
});
- it("keeps the band inside the frame however far the offset is pushed", () => {
- const rect = captionBandRect({ ...ON, verticalPosition: "bottom", offsetY: 45 });
- expect(rect.y + rect.height).toBeLessThanOrEqual(100);
- const top = captionBandRect({ ...ON, verticalPosition: "top", offsetY: -45 });
- expect(top.y).toBeGreaterThanOrEqual(0);
+ it("leaves an untouched project exactly where it was", () => {
+ // The frame/screen-rect reinterpretation is the only intended visual change. The
+ // anchor arithmetic must not move on top of it, or every existing project shifts.
+ expect(captionBandRect({ ...ON, verticalPosition: "top" })).toMatchObject({ x: 10, y: 3 });
+ expect(captionBandRect({ ...ON, verticalPosition: "middle" })).toMatchObject({ x: 10, y: 39 });
+ expect(captionBandRect({ ...ON, verticalPosition: "bottom" })).toMatchObject({ x: 10, y: 75 });
+ });
+
+ it("pushes the ink onto the frame edge, and no further", () => {
+ // The band is a 22% box whose text the renderers centre, so the box has to hang off
+ // the frame for the glyphs to reach the edge — asserting the BOX stays inside is what
+ // used to stop the caption a half-band short (#396). What must stay inside is the ink.
+ for (const verticalPosition of ["top", "middle", "bottom"] as const) {
+ const settings = { ...ON, verticalPosition };
+ const range = captionOffsetRange(settings);
+ const half = captionInkHeightPct(settings) / 2;
+
+ const low = captionBandRect({ ...settings, offsetY: range.y.min });
+ expect(low.y + low.height / 2 - half).toBeCloseTo(0, 6);
+ expect(low.y).toBeLessThan(0);
+
+ const high = captionBandRect({ ...settings, offsetY: range.y.max });
+ expect(high.y + high.height / 2 + half).toBeCloseTo(100, 6);
+ expect(high.y + high.height).toBeGreaterThan(100);
+ }
+ });
+
+ it("clamps an offset the anchor cannot reach instead of drawing off-frame", () => {
+ const pushed = captionBandRect({ ...ON, verticalPosition: "bottom", offsetY: 100 });
+ const capped = captionBandRect({
+ ...ON,
+ verticalPosition: "bottom",
+ offsetY: captionOffsetRange(ON).y.max,
+ });
+ expect(pushed).toEqual(capped);
+ });
+
+ it("lets the band reach the left and right frame edges, but never past them", () => {
+ const range = captionOffsetRange(ON);
+ expect(captionBandRect({ ...ON, offsetX: range.x.min }).x).toBeCloseTo(0, 6);
+ expect(captionBandRect({ ...ON, offsetX: range.x.max }).x).toBeCloseTo(100 - ON.width, 6);
+ expect(captionBandRect({ ...ON, offsetX: 100 }).x).toBeCloseTo(100 - ON.width, 6);
+ });
+
+ it("gives a full-width band no horizontal travel to offer", () => {
+ const full = { ...ON, width: 100 };
+ const range = captionOffsetRange(full);
+ expect(range.x.min).toBeCloseTo(0, 6);
+ expect(range.x.max).toBeCloseTo(0, 6);
+ expect(captionBandRect({ ...full, offsetX: 40 }).x).toBeCloseTo(0, 6);
+ });
+
+ it("shrinks the reach as the font grows, so big captions still fit", () => {
+ // The guaranteed-visible slice is font-derived: a 200px caption fills the whole band,
+ // leaving nothing to spill, while a small one can hang most of the band off-frame.
+ const small = captionOffsetRange({ ...ON, fontSize: 12 }).y.max;
+ const large = captionOffsetRange({ ...ON, fontSize: 200 }).y.max;
+ expect(small).toBeGreaterThan(large);
+ expect(large).toBeCloseTo(100 - CAPTION_BAND_HEIGHT_PCT - 75, 6);
+ });
+
+ it("normalises an offset left over from another anchor on read", () => {
+ // The stored value, the slider position and the drawn band stay the same number.
+ const parked = patchCaptionSettings(doc(), { enabled: true, offsetY: 45 });
+ const read = getCaptionSettings(parked);
+ expect(read.offsetY).toBeCloseTo(captionOffsetRange(read).y.max, 6);
});
it("folds the opacity into the background colour, and reports 'transparent' when off", () => {
diff --git a/src/lib/ai-edition/captions/cues.ts b/src/lib/ai-edition/captions/cues.ts
index d69515499..64133e641 100644
--- a/src/lib/ai-edition/captions/cues.ts
+++ b/src/lib/ai-edition/captions/cues.ts
@@ -231,6 +231,18 @@ export function captionCueAt(cues: CaptionCue[], timeMs: number): CaptionCue | n
return found;
}
+/**
+ * A caption's region, which is an `AnnotationRegion` measured against a different box.
+ *
+ * The `space` marker is why this type exists instead of a field on `AnnotationRegion`:
+ * an annotation is authored on top of the visible video and must keep tracking the
+ * screen rect, while a caption belongs to the output frame. Widening the shared type
+ * would also put a frame-space band — whose `y` legitimately goes negative when it
+ * overhangs the frame edge — through `annotationRegionSchema`, which bounds position
+ * to 0..100. Captions are never stored, so they never meet that schema.
+ */
+export type CaptionTextRegion = AnnotationRegion & { space: "frame" };
+
/**
* Cues as text annotation regions, so the export renderer draws captions through
* the exact same text path as annotations (wrapping, background plate,
@@ -242,9 +254,10 @@ export function captionCueAt(cues: CaptionCue[], timeMs: number): CaptionCue | n
export function captionCuesToTextRegions(
cues: CaptionCue[],
settings: CaptionSettings,
-): AnnotationRegion[] {
+): CaptionTextRegion[] {
const rect = captionBandRect(settings);
return cues.map((cue, index) => ({
+ space: "frame" as const,
id: cue.id,
startMs: cue.startMs,
endMs: cue.endMs,
diff --git a/src/lib/ai-edition/captions/index.ts b/src/lib/ai-edition/captions/index.ts
index 648602f39..6ed325c71 100644
--- a/src/lib/ai-edition/captions/index.ts
+++ b/src/lib/ai-edition/captions/index.ts
@@ -1,4 +1,4 @@
-export type { CaptionCue } from "./cues";
+export type { CaptionCue, CaptionTextRegion } from "./cues";
export {
CAPTION_Z_INDEX_BASE,
captionCueAt,
@@ -8,6 +8,8 @@ export {
sourceSpanToTimelineSpans,
} from "./cues";
export type {
+ CaptionBandRect,
+ CaptionOffsetRange,
CaptionSettings,
CaptionSettingsPatch,
CaptionTextAlign,
@@ -15,8 +17,11 @@ export type {
} from "./settings";
export {
CAPTION_BAND_HEIGHT_PCT,
+ CAPTION_EDGE_MARGIN_PCT,
captionBackgroundCss,
captionBandRect,
+ captionInkHeightPct,
+ captionOffsetRange,
DEFAULT_CAPTION_SETTINGS,
getCaptionSettings,
patchCaptionSettings,
diff --git a/src/lib/ai-edition/captions/settings.ts b/src/lib/ai-edition/captions/settings.ts
index e3501d2c6..6440ec321 100644
--- a/src/lib/ai-edition/captions/settings.ts
+++ b/src/lib/ai-edition/captions/settings.ts
@@ -42,8 +42,15 @@ export interface CaptionSettings {
backgroundOpacity: number;
verticalPosition: CaptionVerticalPosition;
textAlign: CaptionTextAlign;
- /** Fine vertical nudge, in % of frame height, applied on top of the anchor. */
+ /** Fine vertical nudge, in % of OUTPUT FRAME height, applied on top of the anchor.
+ * Positive moves down. The reachable span depends on the anchor — see
+ * `captionOffsetRange`, which the inspector uses for its slider bounds so that
+ * every position on the slider is a position the band can actually take. */
offsetY: number;
+ /** Fine horizontal nudge, in % of OUTPUT FRAME width, applied on top of the
+ * (centred) anchor. Positive moves toward the right edge of the exported frame —
+ * this is frame geometry, so it is never mirrored by an RTL interface locale. */
+ offsetX: number;
/** Caption band width, in % of frame width. */
width: number;
/** Lower bound on words shown at once. */
@@ -65,6 +72,7 @@ export const DEFAULT_CAPTION_SETTINGS: CaptionSettings = {
verticalPosition: "bottom",
textAlign: "center",
offsetY: 0,
+ offsetX: 0,
width: 80,
minWordsPerLine: 2,
maxWordsPerLine: 7,
@@ -75,7 +83,97 @@ export const DEFAULT_CAPTION_SETTINGS: CaptionSettings = {
export const CAPTION_BAND_HEIGHT_PCT = 22;
/** Margin between the band and the frame edge for the top/bottom anchors, in %. */
-const CAPTION_EDGE_MARGIN_PCT = 3;
+export const CAPTION_EDGE_MARGIN_PCT = 3;
+
+/** Reference frame height the px-valued settings are authored against, matching
+ * `annotationScale.ts` — `fontSize` is "pixels at a 1080-high frame". */
+const CAPTION_REFERENCE_FRAME_HEIGHT = 1080;
+
+/** Line box as a multiple of the font size. Mirrors the rasterizers so the band
+ * maths and the drawn glyphs agree: `text_linux.rs` is `font_size * 1.4`, and the
+ * other two backends lay out through the same `text_plate` box model. */
+const CAPTION_LINE_HEIGHT_EM = 1.4;
+
+/** Vertical padding the background plate adds above AND below the text block,
+ * as a multiple of the font size — `text_plate.rs::PAD_Y_EM`. */
+const CAPTION_PLATE_PAD_Y_EM = 0.1;
+
+/** Lines the band is sized to hold. The band is a fixed 22% box and all three
+ * rasterizers centre the text inside it, so this is what decides how much of the
+ * box is guaranteed to carry ink — and therefore how far the box may hang off the
+ * frame before a caption would be clipped (see `captionOffsetRange`). */
+const CAPTION_BAND_CAPACITY_LINES = 2;
+
+/**
+ * Height of the drawn caption block — the background plate when it is on, the text
+ * block alone when it is off — as a % of frame height, capped at the band it lives
+ * in. The band is deliberately taller than its content, so this is the slice of the
+ * band that actually carries pixels.
+ */
+export function captionInkHeightPct(settings: CaptionSettings): number {
+ const lines = CAPTION_BAND_CAPACITY_LINES * CAPTION_LINE_HEIGHT_EM;
+ const plate = settings.backgroundEnabled ? 2 * CAPTION_PLATE_PAD_Y_EM : 0;
+ const px = clamp(settings.fontSize, 12, 200) * (lines + plate);
+ return Math.min(CAPTION_BAND_HEIGHT_PCT, (px / CAPTION_REFERENCE_FRAME_HEIGHT) * 100);
+}
+
+/**
+ * How far the band may hang off the top/bottom of the frame, in % of frame height.
+ *
+ * This is the whole of the "the offset can't reach the edge" half of #396. The band
+ * is a 22%-tall box whose text every renderer centres, so a band stopped flush at the
+ * frame edge still leaves its glyphs half a band short of it. Letting the box spill by
+ * exactly its empty margin puts the ink on the edge while keeping every drawn pixel
+ * on-frame — and costs nothing in the rasterizers, which already clip to the box.
+ */
+function captionBandOverhangPct(settings: CaptionSettings): number {
+ return Math.max(0, (CAPTION_BAND_HEIGHT_PCT - captionInkHeightPct(settings)) / 2);
+}
+
+/** The band's anchor position before the user's nudge, in % of the frame. */
+function captionAnchor(settings: CaptionSettings): { x: number; y: number } {
+ const width = clamp(settings.width, 20, 100);
+ const height = CAPTION_BAND_HEIGHT_PCT;
+ return {
+ x: (100 - width) / 2,
+ y:
+ settings.verticalPosition === "top"
+ ? CAPTION_EDGE_MARGIN_PCT
+ : settings.verticalPosition === "middle"
+ ? (100 - height) / 2
+ : 100 - height - CAPTION_EDGE_MARGIN_PCT,
+ };
+}
+
+/** Inclusive min/max for each offset, in % of the frame. */
+export interface CaptionOffsetRange {
+ x: { min: number; max: number };
+ y: { min: number; max: number };
+}
+
+/**
+ * The offsets the current settings can actually honour.
+ *
+ * Both the reader's clamp and the inspector's sliders come from here, so the two can
+ * never disagree: every value the slider can produce moves the band, and no value it
+ * can produce is silently discarded. The old code hard-coded ±45 in both places and
+ * then clamped the *result*, which is why nearly half the bottom-anchored slider's
+ * travel did nothing at all.
+ */
+export function captionOffsetRange(settings: CaptionSettings): CaptionOffsetRange {
+ const width = clamp(settings.width, 20, 100);
+ const anchor = captionAnchor(settings);
+ const overhang = captionBandOverhangPct(settings);
+ return {
+ // Horizontally the band stays wholly on-frame: `textAlign` lets a line hug the
+ // band's own edge, so an overhang here would push text off the frame.
+ x: { min: -anchor.x, max: 100 - width - anchor.x },
+ y: {
+ min: -overhang - anchor.y,
+ max: 100 - CAPTION_BAND_HEIGHT_PCT + overhang - anchor.y,
+ },
+ };
+}
const VERTICAL_POSITIONS: readonly CaptionVerticalPosition[] = ["top", "middle", "bottom"];
const TEXT_ALIGNS: readonly CaptionTextAlign[] = ["left", "center", "right"];
@@ -124,7 +222,7 @@ export function getCaptionSettings(doc: AxcutDocument | null | undefined): Capti
const minWords = Math.round(readNumber(raw.minWordsPerLine, d.minWordsPerLine, 1, 12));
const maxWords = Math.round(readNumber(raw.maxWordsPerLine, d.maxWordsPerLine, 1, 12));
- return {
+ const settings: CaptionSettings = {
enabled: readBoolean(raw.enabled, d.enabled),
// `null` is a meaningful value here ("show the original"), so an explicit
// null must survive; only a missing/garbage entry falls back to the default.
@@ -138,11 +236,24 @@ export function getCaptionSettings(doc: AxcutDocument | null | undefined): Capti
backgroundOpacity: readNumber(raw.backgroundOpacity, d.backgroundOpacity, 0, 1),
verticalPosition: readEnum(raw.verticalPosition, VERTICAL_POSITIONS, d.verticalPosition),
textAlign: readEnum(raw.textAlign, TEXT_ALIGNS, d.textAlign),
- offsetY: readNumber(raw.offsetY, d.offsetY, -45, 45),
+ // Read wide here, then clamp to what the geometry allows below: the reachable
+ // span depends on the anchor, the width and the font size, which are only known
+ // once the rest of the object is built.
+ offsetY: readNumber(raw.offsetY, d.offsetY, -100, 100),
+ offsetX: readNumber(raw.offsetX, d.offsetX, -100, 100),
width: readNumber(raw.width, d.width, 20, 100),
minWordsPerLine: Math.min(minWords, maxWords),
maxWordsPerLine: Math.max(minWords, maxWords),
};
+
+ // Normalising here rather than at the draw call keeps the stored value, the slider
+ // position and the drawn band the same number. A value that the current anchor
+ // cannot reach — a leftover from another anchor, or from the old ±45 domain — is
+ // pulled to the nearest reachable one instead of being clamped invisibly later.
+ const range = captionOffsetRange(settings);
+ settings.offsetY = clamp(settings.offsetY, range.y.min, range.y.max);
+ settings.offsetX = clamp(settings.offsetX, range.x.min, range.x.max);
+ return settings;
}
export type CaptionSettingsPatch = Partial<CaptionSettings>;
@@ -162,7 +273,12 @@ export function patchCaptionSettings(
};
}
-/** Where the caption band sits in the frame, as annotation-style percentages. */
+/** Where the caption band sits, as percentages of the OUTPUT FRAME.
+ *
+ * Not of the screen rect: captions are subtitles, so they belong to the frame the
+ * viewer sees and must hold still when padding resizes the footage underneath them.
+ * `cues.ts` stamps the regions it builds from this with `space: "frame"`, which is
+ * what tells the compositor to measure them against the frame (see `CaptionTextRegion`). */
export interface CaptionBandRect {
x: number;
y: number;
@@ -171,24 +287,22 @@ export interface CaptionBandRect {
}
/**
- * The band is always horizontally centred — `textAlign` aligns the text *inside*
- * it, which is how subtitles behave everywhere. Vertical placement is the anchor
- * preset plus the user's nudge, clamped so the band can never leave the frame.
+ * Anchor preset plus the user's nudge, on both axes.
+ *
+ * `textAlign` aligns the text *inside* the band, which is how subtitles behave
+ * everywhere; `offsetX` moves the band itself, which is the only way to reach a
+ * corner. Offsets are clamped to `captionOffsetRange` — the same span the inspector
+ * hands its sliders, so nothing the user can dial in is quietly thrown away.
*/
export function captionBandRect(settings: CaptionSettings): CaptionBandRect {
const width = clamp(settings.width, 20, 100);
- const height = CAPTION_BAND_HEIGHT_PCT;
- const anchorY =
- settings.verticalPosition === "top"
- ? CAPTION_EDGE_MARGIN_PCT
- : settings.verticalPosition === "middle"
- ? (100 - height) / 2
- : 100 - height - CAPTION_EDGE_MARGIN_PCT;
+ const anchor = captionAnchor(settings);
+ const range = captionOffsetRange(settings);
return {
- x: (100 - width) / 2,
- y: clamp(anchorY + settings.offsetY, 0, 100 - height),
+ x: anchor.x + clamp(settings.offsetX, range.x.min, range.x.max),
+ y: anchor.y + clamp(settings.offsetY, range.y.min, range.y.max),
width,
- height,
+ height: CAPTION_BAND_HEIGHT_PCT,
};
}
diff --git a/src/native/sceneDescription.ts b/src/native/sceneDescription.ts
index 02246f58d..df4c68e44 100644
--- a/src/native/sceneDescription.ts
+++ b/src/native/sceneDescription.ts
@@ -104,7 +104,13 @@ export interface SceneSpeedRegion {
* SCREEN layer's rect, not of the output frame — the web overlay is handed
* `layout.screenRect` as its container. And they are deliberately NOT affected by the zoom
* crop: the overlay is a sibling of the element carrying the zoom transform, so annotations
- * hold still while the content zooms underneath them. */
+ * hold still while the content zooms underneath them.
+ *
+ * `space: "frame"` opts an entry out of that and measures it against the output frame instead.
+ * Only captions set it: an annotation is authored on top of the visible video, so it must track
+ * the screen rect, whereas a subtitle belongs to the frame the viewer sees and has to hold still
+ * when padding resizes the footage under it (issue #396). The key is omitted entirely for
+ * annotations, so their payload — and the older binaries that read it — are unchanged. */
export interface SceneAnnotation {
id: string;
startSec: number;
@@ -112,7 +118,10 @@ export interface SceneAnnotation {
/** See `SceneZoomRegion.clipIndex`. */
clipIndex?: number;
kind: "text" | "image" | "figure" | "blur";
- /** Rect as fractions of the screen layer (x, y top-left). */
+ /** Which box `x`/`y`/`w`/`h` — and `text.fontSizeRel` — are fractions of. Absent means
+ * `"screen"`, the historical behaviour and the only one annotations ever use. */
+ space?: "frame";
+ /** Rect as fractions of the box named by `space` (x, y top-left). */
x: number;
y: number;
w: number;
@@ -125,10 +134,12 @@ export interface SceneAnnotation {
content: string;
color: string;
backgroundColor: string;
- /** Font size as a FRACTION of the screen rect's height, like everything else in this
- * struct — multiply by the rect's height in output pixels. See `annotationScale.ts`:
- * the preview applies the identical product against its own box, so preview and render
- * agree at any resolution. */
+ /** Font size as a FRACTION of the height of the box named by `space`, like everything
+ * else in this struct — multiply by that box's height in output pixels. See
+ * `annotationScale.ts`: the preview applies the identical product against its own box,
+ * so preview and render agree at any resolution. The denominator MUST follow `space`:
+ * measuring the rect against the frame while sizing the text off the screen rect would
+ * hold a caption still and still shrink its text with the padding slider. */
fontSizeRel: number;
fontFamily: string;
fontWeight: "normal" | "bold";
@@ -485,10 +496,15 @@ export function buildSceneDescription(
// authored in RAW document time and the compositor matches each frame's SOURCE time.
//
// Captions join the annotations here rather than getting a bridge of their own: they are text
- // over the screen layer with the same geometry and the same time base, so the compositor
- // already knows how to draw them. Skipping this would have left them visible in the DOM
+ // with the same time base drawn through the same rasterizer, so the compositor already knows
+ // how to draw them. Skipping this would have left them visible in the DOM
// overlay and absent from the composited pixels — the exact preview/render gap the annotation
// work above closed. They are derived on the fly and never stored (see lib/ai-edition/captions).
+ //
+ // What they do NOT share is the reference box: each caption region carries `space: "frame"`,
+ // so the compositor measures it against the output frame while annotations stay on the screen
+ // rect. Subtitles have to sit where the viewer's frame ends, not where the footage does, or
+ // they slide inward the moment padding shrinks the screen rect (issue #396).
const captionSettings = getCaptionSettings(document);
const captionRegions = captionCuesToTextRegions(
deriveCaptionCues(document, captionSettings, getCaptionTranslations(document)),
@@ -726,13 +742,18 @@ export function buildSceneDescription(
annotations: projectedAnnotations
.map((region) => {
const style = region.style;
+ // Only captions carry a space; annotations must keep emitting the exact same keys
+ // they always have, so the field is omitted rather than sent as null/undefined.
+ const space = (region as { space?: "frame" }).space;
const base = {
id: region.id,
startSec: region.startMs / 1000,
endSec: region.endMs / 1000,
clipIndex: region.clipIndex,
kind: region.type,
- // Authored as percentages of the screen rect; the native side wants fractions.
+ ...(space ? { space } : {}),
+ // Authored as percentages of the box named by `space` — the screen rect unless
+ // this is a caption; the native side wants fractions either way.
x: region.position.x / 100,
y: region.position.y / 100,
w: region.size.width / 100,