Skip to content

Write HTML. Render video. Built for agents.

fix(engine): preserve out-of-range HDR object positions (#5266)

Abdullah M. Nazif95d5494baab9
diff --git a/packages/engine/src/utils/alphaBlit.test.ts b/packages/engine/src/utils/alphaBlit.test.ts
index e4e7c9364d..0890afb4a6 100644
--- a/packages/engine/src/utils/alphaBlit.test.ts
+++ b/packages/engine/src/utils/alphaBlit.test.ts
@@ -1250,6 +1250,47 @@ function readRgb16(buf: Buffer, width: number, x: number, y: number): [number, n
}
describe("resampleRgb48leObjectFit", () => {
+ it.each(["-50% 50%", "150% 50%", "50% -50%", "50% 150%"])(
+ "allows %s to position none-fit content outside the box",
+ (position) => {
+ const src = makeHdrFrame(2, 2, 40000, 30000, 20000);
+ const out = resampleRgb48leObjectFit(src, 2, 2, 6, 6, "none", position);
+
+ expect(out).toEqual(Buffer.alloc(6 * 6 * 6));
+ },
+ );
+
+ it.each([
+ { position: "-25% 50%", x: 0, y: 2, count: 2 },
+ { position: "125% 50%", x: 5, y: 2, count: 2 },
+ { position: "50% -25%", x: 2, y: 0, count: 2 },
+ { position: "50% 125%", x: 2, y: 5, count: 2 },
+ { position: "-25% -25%", x: 0, y: 0, count: 1 },
+ ])("clips none-fit content placed at $position", ({ position, x, y, count }) => {
+ const src = makeHdrFrame(2, 2, 40000, 30000, 20000);
+ const out = resampleRgb48leObjectFit(src, 2, 2, 6, 6, "none", position);
+ let colored = 0;
+ for (let pixel = 0; pixel < 36; pixel++) {
+ if (out.readUInt16LE(pixel * 6) > 0) colored++;
+ }
+
+ expect(colored).toBe(count);
+ expect(readRgb16(out, 6, x, y)).toEqual([40000, 30000, 20000]);
+ });
+
+ it.each([
+ { position: "-50% 50%", blackX: 0, coloredX: 1 },
+ { position: "150% 50%", blackX: 1, coloredX: 0 },
+ ])("moves cover-fit content beyond the box at $position", ({ position, blackX, coloredX }) => {
+ const src = makeHdrFrame(4, 2, 40000, 30000, 20000);
+ const out = resampleRgb48leObjectFit(src, 4, 2, 2, 2, "cover", position);
+
+ for (const y of [0, 1]) {
+ expect(readRgb16(out, 2, blackX, y)).toEqual([0, 0, 0]);
+ expect(readRgb16(out, 2, coloredX, y)).toEqual([40000, 30000, 20000]);
+ }
+ });
+
it("returns the same buffer unchanged for identity fill resample", () => {
const src = makeHdrFrame(4, 4, 40000, 30000, 20000);
const out = resampleRgb48leObjectFit(src, 4, 4, 4, 4, "fill");
diff --git a/packages/engine/src/utils/alphaBlit.ts b/packages/engine/src/utils/alphaBlit.ts
index 6b585eb653..99abcf51fa 100644
--- a/packages/engine/src/utils/alphaBlit.ts
+++ b/packages/engine/src/utils/alphaBlit.ts
@@ -724,8 +724,8 @@ export function blitRgb48leAffine(
export type ObjectFit = "fill" | "cover" | "contain" | "none" | "scale-down";
/**
- * Parse a single axis of a CSS `object-position` string into a fraction in
- * `[0, 1]` (proportion of the slack space along that axis).
+ * Parse a single axis of a CSS `object-position` string into a fraction of
+ * the slack space along that axis. Percentages may lie outside `[0, 1]`.
*
* Defaults to 0.5 (centered) for unrecognized inputs to match CSS, which
* resolves invalid `object-position` values to the initial value (`50% 50%`).
@@ -737,7 +737,7 @@ function parseObjectPositionAxis(value: string, axis: "x" | "y"): number {
if (lower === "center" || lower === "") return 0.5;
if (lower.endsWith("%")) {
const pct = parseFloat(lower) / 100;
- return Number.isFinite(pct) ? Math.max(0, Math.min(1, pct)) : 0.5;
+ return Number.isFinite(pct) ? pct : 0.5;
}
// Pixel values (e.g. "10px") aren't fractional; without the slack-space
// numerator we can't honor them precisely. Fall back to center — this is
@@ -748,7 +748,7 @@ function parseObjectPositionAxis(value: string, axis: "x" | "y"): number {
/**
* Parse a CSS `object-position` string like `"50% 50%"`, `"center top"`, or
- * `"25% 75%"` into normalized `[0, 1]` fractions for X and Y.
+ * `"25% 75%"` into fractions for X and Y, preserving percentages outside `[0, 1]`.
*
* The fractions express how the slack space (the portion of the layout box
* not covered by the rendered content) should be distributed between the