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
import { describe, expect, it } from "vitest";
import type { AxcutAnnotationRegion } from "@/lib/ai-edition/schema";
import {
convertAnnotationKind,
fitTextBox,
fitTextInFrame,
type MeasureText,
measureTextBlock,
refitFrameAnnotations,
toFrameSpace,
} from "./placement";
const WIDE = 16 / 9;
// The footage padded in to 80 % of a 16:9 frame, centred.
const FOOTAGE = { x: 0.1, y: 0.1, width: 0.8, height: 0.8 };
function region(overrides: Partial<AxcutAnnotationRegion> = {}): AxcutAnnotationRegion {
return {
id: "ann1",
startMs: 0,
endMs: 1000,
type: "text",
content: "Hello",
position: { x: 35, y: 40 },
size: { width: 30, height: 20 },
style: {
color: "#ffffff",
backgroundColor: "transparent",
fontSize: 32,
fontFamily: "Inter",
fontWeight: "bold",
fontStyle: "normal",
textDecoration: "none",
textAlign: "center",
textAnimation: "none",
},
zIndex: 1,
...overrides,
};
}
const center = (r: Pick<AxcutAnnotationRegion, "position" | "size">) => ({
x: r.position.x + r.size.width / 2,
y: r.position.y + r.size.height / 2,
});
/** 100 px wide and 40 px tall per line, whatever the text: the box math, not the font. */
const measure: MeasureText = (content) => ({
width: 100,
height: 40 * content.split("\n").length,
});
describe("toFrameSpace", () => {
it("moves an annotation off the footage without moving it on screen", () => {
const patch = toFrameSpace(region(), FOOTAGE);
expect(patch?.space).toBe("frame");
// 10 % + 35 % of 80 %, and so on: the same pixels, measured on the frame.
expect(patch?.position?.x).toBeCloseTo(38, 6);
expect(patch?.position?.y).toBeCloseTo(42, 6);
expect(patch?.size?.width).toBeCloseTo(24, 6);
expect(patch?.size?.height).toBeCloseTo(16, 6);
});
it("keeps the letters their size: the text size follows the reference box's height", () => {
// 32 px of an 80 %-high footage is 25.6 px of the frame, rounded.
expect(toFrameSpace(region(), FOOTAGE)?.style?.fontSize).toBe(26);
});
it("leaves a blur on the footage, and an annotation already on the frame alone", () => {
expect(toFrameSpace(region({ type: "blur" }), FOOTAGE)).toBeNull();
expect(toFrameSpace(region({ space: "frame" }), FOOTAGE)).toBeNull();
});
});
describe("fitTextBox", () => {
it("hugs the text: its width plus the plate's margin, measured against the frame", () => {
const { size } = fitTextBox(region({ space: "frame" }), WIDE, measure);
// 100 px + (0.4 em of plate + 0.25 em of slack) × 32, on a 1920-wide reference frame.
expect(size.width).toBeCloseTo(((100 + 0.65 * 32) / 1920) * 100, 6);
// 40 px + (0.2 em + 0.125 em) × 32, on 1080.
expect(size.height).toBeCloseTo(((40 + 0.325 * 32) / 1080) * 100, 6);
});
it("keeps the box where it was: its centre does not move", () => {
const before = region({ space: "frame" });
const after = fitTextBox(before, WIDE, measure);
expect(center(after).x).toBeCloseTo(center(before).x, 6);
expect(center(after).y).toBeCloseTo(center(before).y, 6);
});
it("grows with the words and with the size", () => {
const one = fitTextBox(region({ space: "frame" }), WIDE, measure).size;
const two = fitTextBox(region({ space: "frame", content: "a\nb" }), WIDE, measure).size;
expect(two.height).toBeGreaterThan(one.height);
const big = fitTextBox(
region({ space: "frame", style: { ...region().style, fontSize: 96 } }),
WIDE,
measureTextBlock,
).size;
const small = fitTextBox(region({ space: "frame" }), WIDE, measureTextBlock).size;
expect(big.width / small.width).toBeCloseTo(3, 6);
});
it("stays in the frame at its edge, never at a negative position", () => {
const atEdge = fitTextBox(
region({ space: "frame", position: { x: 99, y: 99 }, size: { width: 1, height: 1 } }),
WIDE,
measure,
);
expect(atEdge.position.x + atEdge.size.width).toBeCloseTo(100, 6);
expect(atEdge.position.y + atEdge.size.height).toBeCloseTo(100, 6);
const huge = fitTextBox(
region({ space: "frame", content: "x".repeat(400) }),
WIDE,
measureTextBlock,
);
expect(huge.size.width).toBeGreaterThan(100);
expect(huge.position.x).toBe(0);
});
});
describe("measureTextBlock without a canvas", () => {
it("counts lines and the longest one", () => {
const one = measureTextBlock("abcd", region().style);
const two = measureTextBlock("ab\nabcd", region().style);
expect(two.width).toBe(one.width);
expect(two.height).toBe(one.height * 2);
});
});
describe("convertAnnotationKind", () => {
const context = { footage: FOOTAGE, frameAspect: WIDE, measure };
it("puts a blur on the footage, at the default size, where the text was", () => {
const text = region({
space: "frame",
position: { x: 45, y: 45 },
size: { width: 10, height: 10 },
});
const patch = convertAnnotationKind(text, "blur", context);
expect(patch.type).toBe("blur");
expect(patch.space).toBeUndefined();
expect("space" in patch).toBe(true);
expect(patch.size).toEqual({ width: 30, height: 20 });
// Centred on the text's centre, (50, 50) on the frame, which is (50, 50) on the footage.
expect(center({ position: patch.position!, size: patch.size! })).toEqual({ x: 50, y: 50 });
});
it("takes a blur back to the frame without moving it on screen", () => {
const blur = region({ type: "blur", content: "" });
const patch = convertAnnotationKind(blur, "figure", context);
expect(patch.space).toBe("frame");
expect(patch.position?.x).toBeCloseTo(38, 6);
expect(patch.size?.width).toBeCloseTo(24, 6);
});
it("keeps the box between image and arrow, and fits a text to its words", () => {
const arrow = region({ type: "figure", content: "", space: "frame" });
const image = convertAnnotationKind(arrow, "image", context);
expect({ position: image.position, size: image.size }).toEqual({
position: arrow.position,
size: arrow.size,
});
const text = convertAnnotationKind(arrow, "text", context);
expect(text.size).toEqual(fitTextBox({ ...arrow, space: "frame" }, WIDE, measure).size);
expect(center({ position: text.position!, size: text.size! }).x).toBeCloseTo(
center(arrow).x,
6,
);
});
it("parks each content in its slot and brings it back", () => {
const text = region({ space: "frame", content: "hello" });
const toImage = convertAnnotationKind(text, "image", context);
expect(toImage).toMatchObject({ textContent: "hello", content: "" });
const back = convertAnnotationKind(
{ ...text, ...toImage, content: "data:image/png;base64,AAAA" },
"text",
context,
);
expect(back).toMatchObject({ content: "hello", imageContent: "data:image/png;base64,AAAA" });
});
it("changes only the kind when no footage is known", () => {
const patch = convertAnnotationKind(region(), "figure", { ...context, footage: null });
expect(patch).toEqual({ textContent: "Hello", type: "figure", content: "" });
});
it("takes a blur off the frame even when no footage is known", () => {
const text = region({ space: "frame" });
const patch = convertAnnotationKind(text, "blur", { ...context, footage: null });
expect(patch.type).toBe("blur");
expect("space" in patch && patch.space === undefined).toBe(true);
expect(patch.position).toBeUndefined();
});
});
/** 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);
});
it("stops at the minimum size, its box still fitted to its one line", () => {
// 200 px per px of font: 1600 px at the 8 px floor, wider than a 9:16 frame's 607.5 px.
const endless: MeasureText = (_content, style) => ({
width: 200 * style.fontSize,
height: 1.2 * style.fontSize,
});
const fitted = fitTextInFrame(region({ space: "frame" }), 9 / 16, endless);
expect(fitted.style.fontSize).toBe(8);
expect(fitted).toMatchObject(fitTextBox(fitted, 9 / 16, endless));
expect(fitted.size.width).toBeGreaterThan(100);
expect(fitted.position.x).toBe(0);
});
});
describe("refitFrameAnnotations", () => {
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, 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);
expect(after.size.height).toBe(6);
expect(center(after).x).toBeCloseTo(50, 6);
expect(after.position.y).toBe(40);
});
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([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)));
expect(after.size.width).toBeCloseTo(100, 6);
expect(after.position.x).toBe(0);
expect(after.size.height).toBeCloseTo(6 * shrink, 6);
expect(center(after).y).toBeCloseTo(43, 6);
expect(after.style).toBe(image.style);
});
it("leaves the footage's annotations, and an unchanged format, alone", () => {
const blur = region({ type: "blur" });
expect(refitFrameAnnotations([blur], WIDE, 1)[0]).toBe(blur);
const list = [region({ space: "frame" })];
expect(refitFrameAnnotations(list, WIDE, WIDE)).toBe(list);
});
});