Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

fix(caption): preserve authored gaps and paint joined backgrounds across lines

kashorinb5b8fb7fea64
diff --git a/packages/caption-fine-studio/src/index.ts b/packages/caption-fine-studio/src/index.ts
index 2393076b..6f51fe88 100644
--- a/packages/caption-fine-studio/src/index.ts
+++ b/packages/caption-fine-studio/src/index.ts
@@ -114,10 +114,8 @@ function cueText(document: CaptionDocument | undefined, unitIds: readonly string
const selected = new Set(unitIds);
return document.words
.filter((word) => selected.has(word.unitId))
- .map((word) => word.text)
- .join(" ")
- .replace(/([\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}])\s+(?=[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}])/gu, "$1")
- .trim();
+ .map((word, index) => (index === 0 ? "" : word.separatorBefore) + word.text)
+ .join("");
}
export function projectCaptionContents(context: StudioTrackCompanionContext): readonly StudioEntityDraft[] {
diff --git a/packages/caption-fine-studio/test/adapter.test.ts b/packages/caption-fine-studio/test/adapter.test.ts
index f267e2a3..e72ac7da 100644
--- a/packages/caption-fine-studio/test/adapter.test.ts
+++ b/packages/caption-fine-studio/test/adapter.test.ts
@@ -73,6 +73,21 @@ test("Caption Companion projects Cue text and Style from public domain values",
title: "#1",
layers: [{ kind: "text", role: "content", text: "真实字幕" }],
});
+ for (const [texts, separators, expected] of [
+ [["3", "D"], ["", ""], "3D"],
+ [["3", "개월"], ["", ""], "3개월"],
+ [["是的", "就是这样"], ["", " "], "是的 就是这样"],
+ [["hello", "world"], [" ", " "], "hello world"],
+ ] as const) {
+ const variant = { ...document, words: document.words.map((word, index) => ({ ...word,
+ text: texts[index]!, separatorBefore: separators[index]!,
+ })) };
+ const values = new Map(context.values);
+ values.set("story.caption", variant);
+ assert.equal(projectCaptionContents({ ...context, values })[0]!.display.layers[0]!.kind, "text");
+ assert.deepEqual(projectCaptionContents({ ...context, values })[0]!.display.layers,
+ [{ kind: "text", role: "content", text: expected }]);
+ }
assert.equal(cue?.presentation?.chrome, "standard");
assert.equal(cue?.parameterReferences, undefined);
assert.deepEqual([cue?.startFrame, cue?.endFrameExclusive], [10, 20]);
diff --git a/packages/caption-fine/README.md b/packages/caption-fine/README.md
index 1cfd3644..3ae9d43f 100644
--- a/packages/caption-fine/README.md
+++ b/packages/caption-fine/README.md
@@ -97,6 +97,18 @@ The underlying character anchors remain available to other Timeline consumers. `
`word-gap` sizes the separators authored in `CaptionDisplayWord.separatorBefore`. A numeric or
writing-system boundary does not create a space: `3D` and `3개월` stay joined, while `是的 就是这样`
keeps its space. Base text, active text, underlines and joined boxes consume the same boundaries.
+For `active-box: trail; active-box-continuity: joined`, Fine measures the complete stationary Cue,
+including the inactive suffix, and joins its painted line rectangles. An over-wide word's internal
+lines are included. Padding and borders extend beyond those rectangles without changing text flow;
+overlapping line backgrounds share one outline and paint translucent color once. Cue motion then
+moves text and decoration together.
+
+This decoration uses HyperFrames' existing local browser-program extension, owned by Fine. Its
+measurement copy uses the same exact-font text and available width, is removed synchronously after
+measurement, and is recomputed when a frame is sought (including after fonts finish loading). It
+adds no layout records to Script, Caption, Timeline or Runtime. Other backends must support the
+selected program format explicitly.
+
`letter-spacing` controls glyph tracking. Exact font fallbacks supply the required glyphs; the layout
does not select a font by language.
diff --git a/packages/caption-fine/package.json b/packages/caption-fine/package.json
index 857573ad..bcc0faed 100644
--- a/packages/caption-fine/package.json
+++ b/packages/caption-fine/package.json
@@ -15,6 +15,7 @@
"@hypit/component-kit": "workspace:*",
"@hypit/composition": "workspace:*",
"@hypit/elaborator": "workspace:*",
+ "@hypit/hyperframes": "workspace:*",
"@hypit/media": "workspace:*",
"@hypit/narrative": "workspace:*",
"@hypit/program-space": "workspace:*",
diff --git a/packages/caption-fine/src/joined-box.ts b/packages/caption-fine/src/joined-box.ts
new file mode 100644
index 00000000..308b5518
--- /dev/null
+++ b/packages/caption-fine/src/joined-box.ts
@@ -0,0 +1,114 @@
+/**
+ * Fine's joined decoration is measured after browser layout. Its temporary, untransformed copy
+ * uses the same exact-font children and content width, so Cue motion cannot distort measurements.
+ * Nothing is retained outside the program or written back into Script/Caption/Timeline.
+ */
+export const joinedBoxSetup = String.raw`
+const layout = root.querySelector('[data-fine-box-layout]');
+const layers = [...root.querySelectorAll('[data-caption-active-box="joined"]')];
+const ns = 'http://www.w3.org/2000/svg';
+const paths = layers.map(layer => {
+ const svg = document.createElementNS(ns, 'svg');
+ svg.style.cssText = 'position:absolute;inset:0;width:100%;height:100%;overflow:visible';
+ const path = document.createElementNS(ns, 'path');
+ path.setAttribute('fill', data.background);
+ path.setAttribute('stroke', data.borderColor);
+ path.setAttribute('stroke-width', String(data.borderWidth));
+ svg.append(path); layer.append(svg); return path;
+});
+// Union on actual rectangle edges. Tracing only the exterior removes internal borders and
+// paints translucent fills once, including where line padding overlaps the neighbouring line.
+const outline = rectangles => {
+ const xs = [...new Set(rectangles.flatMap(r => [r.left, r.right]))].sort((a,b) => a-b);
+ const ys = [...new Set(rectangles.flatMap(r => [r.top, r.bottom]))].sort((a,b) => a-b);
+ const cells = ys.slice(1).map((_, y) => xs.slice(1).map((_, x) => rectangles.some(r =>
+ r.left <= xs[x] && r.right >= xs[x+1] && r.top <= ys[y] && r.bottom >= ys[y+1])));
+ const edges = new Map();
+ const key = (x,y) => x+','+y;
+ const add = (x,y,ex,ey) => {
+ const k = key(x,y), list = edges.get(k) || [];
+ list.push([ex,ey]); edges.set(k,list);
+ };
+ for (let y=0;y<cells.length;y++) for (let x=0;x<cells[y].length;x++) {
+ if (!cells[y][x]) continue;
+ if (!cells[y-1]?.[x]) add(x,y,x+1,y);
+ if (!cells[y]?.[x+1]) add(x+1,y,x+1,y+1);
+ if (!cells[y+1]?.[x]) add(x+1,y+1,x,y+1);
+ if (!cells[y]?.[x-1]) add(x,y+1,x,y);
+ }
+ const contours = [];
+ while (edges.size) {
+ const start = edges.keys().next().value.split(',').map(Number);
+ const points = []; let current = start, previous;
+ do {
+ points.push([xs[current[0]],ys[current[1]]]);
+ const k = key(...current), next = edges.get(k);
+ let index = 0;
+ if (next.length > 1 && previous) {
+ const dx = current[0]-previous[0], dy = current[1]-previous[1];
+ // At a point contact, keep each contour on its own side of the corner.
+ index = next.findIndex(p => dx*(p[1]-current[1])-dy*(p[0]-current[0]) > 0);
+ if (index < 0) index = 0;
+ }
+ const target = next.splice(index,1)[0];
+ if (!next.length) edges.delete(k);
+ previous = current; current = target;
+ } while (current[0] !== start[0] || current[1] !== start[1]);
+ const corners = points.filter((p,i) => {
+ const a = points[(i+points.length-1)%points.length], b = points[(i+1)%points.length];
+ return (p[0]-a[0])*(b[1]-p[1]) !== (p[1]-a[1])*(b[0]-p[0]);
+ });
+ const rounded = corners.map((p,i) => {
+ const a = corners[(i+corners.length-1)%corners.length], b = corners[(i+1)%corners.length];
+ const before = Math.hypot(p[0]-a[0],p[1]-a[1]), after = Math.hypot(b[0]-p[0],b[1]-p[1]);
+ const radius = Math.min(data.radius, before/2, after/2);
+ return { p, start: [p[0]+(a[0]-p[0])*radius/before,p[1]+(a[1]-p[1])*radius/before],
+ end: [p[0]+(b[0]-p[0])*radius/after,p[1]+(b[1]-p[1])*radius/after], radius,
+ turn: (p[0]-a[0])*(b[1]-p[1])-(p[1]-a[1])*(b[0]-p[0]) };
+ });
+ contours.push(rounded.map((c,i) => (i ? 'L' : 'M')+c.start.join(' ')
+ +(c.radius ? 'A'+c.radius+' '+c.radius+' 0 0 '+(c.turn>0 ? 1 : 0)+' '+c.end.join(' ') : '')).join(' ')+'Z');
+ }
+ return contours.join(' ');
+};
+return () => {
+ const measure = layout.cloneNode(true);
+ // Exact font declarations are already inline on the typed text children. Copy the inherited
+ // flow properties explicitly; the measurement must not inherit the document body's typography.
+ const style = getComputedStyle(layout);
+ const rootStyle = getComputedStyle(root);
+ const offsetX = parseFloat(rootStyle.paddingLeft), offsetY = parseFloat(rootStyle.paddingTop);
+ for (const name of ['font-size','line-height','text-align','direction','letter-spacing','word-spacing']) {
+ measure.style.setProperty(name,style.getPropertyValue(name));
+ }
+ measure.style.cssText += ';position:fixed;left:0;top:0;margin:0;padding:0;border:0;box-sizing:content-box;'
+ +'width:'+style.width+';height:auto;max-width:none;transform:none;visibility:hidden;pointer-events:none';
+ measure.removeAttribute('id');
+ for (const child of measure.querySelectorAll('[id]')) child.removeAttribute('id');
+ document.body.append(measure);
+ let words;
+ try {
+ const origin = measure.getBoundingClientRect();
+ words = [...measure.querySelectorAll('[data-fine-box-word]')].map(word => {
+ const range = document.createRange(); range.selectNodeContents(word);
+ return [...range.getClientRects()].filter(r => r.width > 0 && r.height > 0).map(r => ({
+ left:r.left-origin.left+offsetX, right:r.right-origin.left+offsetX,
+ top:r.top-origin.top+offsetY, bottom:r.bottom-origin.top+offsetY,
+ }));
+ });
+ } finally { measure.remove(); }
+ const lines = new Map(); let wordIndex = 0;
+ for (let index=0;index<paths.length;index++) {
+ for (let count=0;count<data.wordCounts[index];count++) for (const rect of words[wordIndex++]) {
+ const line = lines.get(rect.top);
+ if (!line) lines.set(rect.top,{...rect});
+ else { line.left=Math.min(line.left,rect.left); line.right=Math.max(line.right,rect.right);
+ line.bottom=Math.max(line.bottom,rect.bottom); }
+ }
+ paths[index].setAttribute('d',outline([...lines.values()].map(r => ({
+ left:r.left-data.paddingX-data.borderWidth/2, right:r.right+data.paddingX+data.borderWidth/2,
+ top:r.top-data.paddingY-data.borderWidth/2, bottom:r.bottom+data.paddingY+data.borderWidth/2,
+ }))));
+ }
+};
+`;
diff --git a/packages/caption-fine/src/render.ts b/packages/caption-fine/src/render.ts
index ae1540bc..7a6471af 100644
--- a/packages/caption-fine/src/render.ts
+++ b/packages/caption-fine/src/render.ts
@@ -8,6 +8,7 @@ import type {
VisualColorPaint,
VisualElement,
VisualKeyframe,
+ VisualProgramElement,
VisualStyleDeclaration,
VisualTextElement,
VisualTextPaintLayer,
@@ -21,6 +22,8 @@ import type { CanvasSpace, SpatialFrame, SpatialRegionTimeline } from "@hypit/sp
import { assertFineCaptionParameters, FINE_CAPTION_FAMILY } from "./style.js";
import { assertFineCaptionSchedule } from "./schedule.js";
import { uniformGap, wordGaps } from "./spacing.js";
+import { browserProgram } from "@hypit/hyperframes";
+import { joinedBoxSetup } from "./joined-box.js";
import type {
FineCaptionActiveUnderline,
FineCaptionGlyphPaint,
@@ -670,7 +673,7 @@ function cueElements(
readonly canvas: CanvasSpace;
},
): VisualElement[] {
- type UnorderedVisualElement = Omit<VisualBoxElement, "order"> | Omit<VisualTextElement, "order">;
+ type UnorderedVisualElement = Omit<VisualBoxElement, "order"> | Omit<VisualTextElement, "order"> | Omit<VisualProgramElement, "order">;
const elements: VisualElement[] = [];
let order = 0;
const push = (element: UnorderedVisualElement): void => {
@@ -702,6 +705,14 @@ function cueElements(
const marginStart = parameters.layout.direction === "rtl" ? "margin-right" : "margin-left";
const spacedStyle = (spaced: boolean): readonly VisualStyleDeclaration[] =>
spaced ? [{ name: marginStart, value: gapPx }] : [];
+ const atomFlow = (words: readonly CaptionDisplayWord[]): VisualStyleDeclaration[] => [
+ ...(uniformGap(wordGaps(words)) === true ? [{ name: "column-gap", value: gapPx }] : []),
+ { name: "display", value: "inline-flex" },
+ { name: "min-width", value: "0" },
+ { name: "overflow-wrap", value: "anywhere" },
+ { name: "white-space", value: "normal" },
+ { name: "word-break", value: parameters.layout.wrap === "grapheme" ? "break-all" : "normal" },
+ ];
const fonts = parameters.typography.exactFonts;
const rowStarts = structuralRowStarts(atoms, parameters.layout.maxWordsPerLine);
if (parameters.layout.maxLines !== undefined) {
@@ -790,60 +801,58 @@ function cueElements(
});
if (parameters.activeBox.mode === "trail" && parameters.activeBox.continuity === "joined") {
- for (const [prefixIndex, atom] of atoms.entries()) {
- const timing = atomFrames.get(atom.id);
- if (timing === undefined) throw new Error(`Fine Caption is missing timing for Atom ${atom.id}`);
- const next = atoms[prefixIndex + 1];
+ // Fine owns this drawing program. The renderer only executes its existing browser-program
+ // format; it learns nothing about captions, activation units or their layout rules.
+ push({
+ id: "joined-boxes", parent: "cue", kind: "program",
+ style: [
+ { name: "position", value: "absolute" }, { name: "inset", value: "0" },
+ { name: "padding", value: `${compactNumber(parameters.cueBox.paddingYPx)}px ${compactNumber(parameters.cueBox.paddingXPx)}px` },
+ ],
+ program: browserProgram({
+ html: "{{joined-layout}}" + atoms.map((_, index) => `{{joined-box-${index + 1}}}`).join(""),
+ setup: joinedBoxSetup,
+ css: ":scope { pointer-events: none; }",
+ data: {
+ wordCounts: atomWords.map(words => words.length),
+ background: parameters.activeBox.background, borderColor: parameters.activeBox.borderColor,
+ borderWidth: parameters.activeBox.borderWidthPx, radius: parameters.activeBox.radiusPx,
+ paddingX: parameters.activeBox.paddingXPx, paddingY: parameters.activeBox.paddingYPx,
+ },
+ }),
+ });
+ push({ id: "joined-layout", parent: "joined-boxes", kind: "box",
+ style: [{ name: "visibility", value: "hidden" }],
+ attributes: [{ name: "data-fine-box-layout", value: "" }],
+ });
+ // The complete stationary Cue supplies layout, including the inactive suffix. Its actual
+ // word fragments determine the background; decoration contributes no width to text flow.
+ for (const [index, words] of atomWords.entries()) {
+ if (index > 0 && rowStarts.has(index)) push({ id: `joined-break-${index}`, parent: "joined-layout", kind: "box",
+ style: [{ name: "display", value: "block" }, { name: "height", value: "0" }],
+ });
+ if (!rowStarts.has(index) && words[0]!.separatorBefore === " ") pushSeparator(`joined-gap-${index}`, "joined-layout");
+ const groupId = `joined-group-${index}`;
+ push({ id: groupId, parent: "joined-layout", kind: "box", style: [...atomFlow(words), { name: "max-width", value: "100%" }] });
+ const gaps = wordGaps(words);
+ for (const [wordIndex, word] of words.entries()) {
+ push({ id: `${groupId}-word-${wordIndex}`, parent: groupId, kind: "text", text: word.text, fonts,
+ style: [...transparentGlyphStyle(parameters),
+ ...(uniformGap(gaps) === undefined ? spacedStyle(gaps[wordIndex] ?? false) : [])],
+ attributes: [{ name: "data-fine-box-word", value: "" }],
+ });
+ }
+ }
+ for (const [index, atom] of atoms.entries()) {
+ const timing = atomFrames.get(atom.id)!;
+ const next = atoms[index + 1];
const nextStart = next === undefined ? durationFrames : atomFrames.get(next.id)?.start;
if (nextStart === undefined) throw new Error("Fine Caption is missing timing for the next Atom");
- const layerId = `joined-box-${prefixIndex + 1}`;
- push({
- id: layerId,
- parent: "cue",
- kind: "box",
- style: [
- { name: "box-sizing", value: "border-box" },
- { name: "direction", value: parameters.layout.direction },
- { name: "inset", value: "0" },
- { name: "padding", value: `${compactNumber(parameters.cueBox.paddingYPx)}px ${compactNumber(parameters.cueBox.paddingXPx)}px` },
- { name: "position", value: "absolute" },
- { name: "text-align", value: parameters.layout.textAlign },
- ],
+ push({ id: `joined-box-${index + 1}`, parent: "joined-boxes", kind: "box",
+ style: [{ name: "position", value: "absolute" }, { name: "inset", value: "0" }],
animation: activeBoxAnimation(parameters, timing.start, nextStart, durationFrames, "current"),
attributes: [{ name: "data-caption-active-box", value: "joined" }],
});
- push({
- id: `${layerId}-text`,
- parent: layerId,
- kind: "box",
- style: [
- ...transparentGlyphStyle(parameters).filter(({ name }) => name !== "white-space"),
- { name: "-webkit-box-decoration-break", value: "clone" },
- { name: "background", value: parameters.activeBox.background },
- { name: "border-color", value: parameters.activeBox.borderColor },
- { name: "border-radius", value: `${compactNumber(parameters.activeBox.radiusPx)}px` },
- { name: "border-style", value: "solid" },
- { name: "border-width", value: `${compactNumber(parameters.activeBox.borderWidthPx)}px` },
- { name: "box-decoration-break", value: "clone" },
- { name: "display", value: "inline" },
- { name: "padding", value: `${compactNumber(parameters.activeBox.paddingYPx)}px ${compactNumber(parameters.activeBox.paddingXPx)}px` },
- { name: "white-space", value: "normal" },
- ],
- });
- for (const [index, words] of atomWords.slice(0, prefixIndex + 1).entries()) {
- if (index > 0 && rowStarts.has(index)) push({ id: `${layerId}-break-${index}`, parent: `${layerId}-text`, kind: "box",
- style: [{ name: "display", value: "block" }, { name: "height", value: "0" }],
- });
- if (!rowStarts.has(index) && words[0]!.separatorBefore === " ") pushSeparator(`${layerId}-gap-${index}`, `${layerId}-text`);
- const groupId = `${layerId}-group-${index}`;
- push({ id: groupId, parent: `${layerId}-text`, kind: "box", style: [{ name: "display", value: "inline-flex" }] });
- for (const [wordIndex, word] of words.entries()) {
- push({ id: `${groupId}-word-${wordIndex}`, parent: groupId, kind: "text", text: word.text, fonts,
- style: [...transparentGlyphStyle(parameters),
- ...(wordIndex > 0 && word.separatorBefore === " " ? spacedStyle(true) : [])],
- });
- }
- }
}
}
@@ -917,13 +926,8 @@ function cueElements(
parent: responseId,
kind: "box",
style: [
- ...(wordGap === true ? [{ name: "column-gap", value: gapPx }] as const : []),
- { name: "display", value: "inline-flex" },
- { name: "min-width", value: "0" },
- { name: "overflow-wrap", value: "anywhere" },
+ ...atomFlow(atomWords[atomIndex]!),
{ name: "position", value: "relative" },
- { name: "white-space", value: "normal" },
- { name: "word-break", value: parameters.layout.wrap === "grapheme" ? "break-all" : "normal" },
],
attributes: [{ name: "data-caption-atom", value: atom.id }],
});
diff --git a/packages/caption-fine/test/caption-fine.test.ts b/packages/caption-fine/test/caption-fine.test.ts
index cde0c875..d4aa4b85 100644
--- a/packages/caption-fine/test/caption-fine.test.ts
+++ b/packages/caption-fine/test/caption-fine.test.ts
@@ -567,13 +567,13 @@ test("Fine uses author separators in ordinary text, shared groups, active copies
for (const id of ["atom-1-base-3", "atom-1-active-3", "atom-1-underline-3"]) {
assert.equal(grouped.find(element => element.id === id)!.style.find(style => style.name === "margin-left")?.value, "14px");
}
- const backgroundWords = grouped.filter(element => element.id.startsWith("joined-box-2-group-") && element.kind === "text");
+ const backgroundWords = grouped.filter(element => element.id.startsWith("joined-group-") && element.kind === "text");
assert.deepEqual(backgroundWords.map(element => element.kind === "text" && element.text), ["3", "개월", "만에", "완료했습니다"]);
- assert.equal(grouped.find(element => element.id === "joined-box-2-group-0-word-2")!.style.find(style => style.name === "margin-left")?.value, "14px");
- assert.equal(grouped.find(element => element.id === "joined-box-2-gap-1")!.kind, "text");
+ assert.equal(grouped.find(element => element.id === "joined-group-0-word-2")!.style.find(style => style.name === "margin-left")?.value, "14px");
+ assert.equal(grouped.find(element => element.id === "joined-gap-1")!.kind, "text");
const chinese = render("是的 就是这样");
assert.equal(chinese.find(element => element.id === "gap-2")!.style.find(style => style.name === "letter-spacing")?.value, "14px");
assert.equal(chinese.find(element => element.id === "atom-2-entry")!.style.some(style => style.name === "margin-left"), false);
const joined = render("3D");
- assert.equal(joined.some(element => element.style.some(style => style.name === "column-gap" || style.name === "margin-left")), false);
+ assert.equal(joined.some(element => element.style.some(style => (style.name === "column-gap" || style.name === "margin-left") && style.value !== "0px")), false);
});
diff --git a/packages/caption-fine/test/layout.browser.test.ts b/packages/caption-fine/test/layout.browser.test.ts
new file mode 100644
index 00000000..79c88f21
--- /dev/null
+++ b/packages/caption-fine/test/layout.browser.test.ts
@@ -0,0 +1,133 @@
+import assert from "node:assert/strict";
+import { existsSync } from "node:fs";
+import { compileHyperframesDocument, materializeHyperframesHtml } from "@hypit/hyperframes";
+import { sealComposition } from "@hypit/composition";
+import sharp from "sharp";
+import test from "node:test";
+import { captureBrowserExecutablePath, withCapture } from "@hypit/browser-capture";
+import { captionDocument, parseScript } from "@hypit/script";
+import { sealTimeline } from "@hypit/timeline";
+import type { SvsRecipe } from "@hypit/svs";
+import { projectProgramWindow } from "../../../test/temporal-fixture.js";
+import { fixtureResource } from "../../../test/fixture-resource.js";
+import { fineCaptionStyle, renderFineCaption, scheduleFineCaption } from "../src/index.js";
+
+const executablePath = process.env.HYPIT_CAPTURE_TEST_BROWSER ?? await captureBrowserExecutablePath();
+
+function cueHtml(source: string, width: number, extra: SvsRecipe["properties"]): string {
+ const document = captionDocument(parseScript("layout", `<line>${source}</line>`), "caption", "story");
+ const timeline = sealTimeline({ id: "timeline", items: [], durationSec: 4, frameRate: { numerator: 30, denominator: 1 } });
+ const style = fineCaptionStyle("plain", { path: "caption", properties: {
+ align: "center", background: "#00000000", fill: "#000000", "line-height": 1.2,
+ "anchor-x": "left", "anchor-y": "top", padding: "0", radius: 0, "stack-order": 1, size: 36, width: 1, x: 0, y: 0, "word-gap": 14, "inline-size": "fixed",
+ karaoke: "trail", "active-box": "trail", "active-box-continuity": "joined", "active-box-padding": "0", "active-box-background": "#00800080", "active-box-radius": 0, ...extra,
+ } }, [{ sources: [{ artifact: { kind: "blob", resource: fixtureResource("font"), size: 1, mediaType: "font/woff2" } }], weight: 400, style: "normal" }]);
+ const program = { id: "p", documentId: document.id, styles: [style], uses: [{ styleId: "plain",
+ window: projectProgramWindow({ itemId: "use", semantic: timeline, projection: { start: { ref: "program.start" }, end: { ref: "program.end" } } }),
+ }] };
+ const timed = { spaceId: timeline.id, narrativeId: "story", documentId: document.id, cues: [{ id: "cue", startFrame: 0, endFrameExclusive: 90,
+ units: document.units.map((unit, index) => ({ unitId: unit.id, startFrame: index * 5, endFrameExclusive: index * 5 + 5 })),
+ }] };
+ const track = renderFineCaption(scheduleFineCaption(timed, program, document), program, document, timeline);
+ const compiled = compileHyperframesDocument(sealComposition({ id: "test",
+ canvas: { width, height: 500, clearColor: "#ffffff" }, tracks: [track] }), timeline);
+ // Both copies use the same face. Exact-font loading is exercised by renderer integration.
+ return materializeHyperframesHtml(compiled, () => "data:font/woff2;base64,AA==")
+ .replace("</head>", `<style>[data-hypit-element-id]{font-family:Arial!important}[data-composition-id]{width:${width}px;height:500px}</style></head>`);
+
+}
+
+test("joined activation keeps complete Cue geometry through alignment and wrapping", {
+ skip: !existsSync(executablePath) && "Selected capture browser is not prepared",
+}, async () => {
+ await withCapture({ launch: { executablePath } }, async ({ page }) => {
+ for (const style of [{ align: "left" }, { align: "center" }, { align: "right" },
+ { align: "center", "active-box-padding": "5 8", "active-box-border-width": 2, padding: "7 11" },
+ { align: "center", "max-words-per-line": 2 }, { align: "right", direction: "rtl" },
+ { align: "center", "stroke-width": 3 }, { align: "center", "atom-reveal": "typewriter" }]) {
+ for (const [source, width] of [["hello world more text", 170], ["supercalifragilisticexpialidocious", 170],
+ ["<3개월 만에|> 완료했습니다", 500], ["3D hello", 350]] as const) {
+ await page.setContent(cueHtml(source, width, style));
+ const mismatches = await page.evaluate(() => Array.from(document.querySelectorAll("[data-hypit-element-id]")).flatMap(element => {
+ const match = /^joined-group-(\d+)-word-(\d+)$/u.exec(element.getAttribute("data-hypit-element-id")!);
+ if (!match) return [];
+ const base = document.querySelector(`[data-hypit-element-id="atom-${Number(match[1]) + 1}-base-${Number(match[2]) + 1}"]`)!;
+ const actual = element.getBoundingClientRect(), expected = base.getBoundingClientRect();
+ return (["x", "y", "width", "height"] as const).flatMap(key => Math.abs(actual[key] - expected[key]) > 0.1 ? [`${element.id}:${key}`] : []);
+ }));
+ assert.deepEqual(mismatches, [], JSON.stringify({ source, width, style }));
+ }
+ }
+ });
+});
+
+test("joined backgrounds cover wrapped glyphs, preserve opacity and seek without stale geometry", {
+ skip: !existsSync(executablePath) && "Selected capture browser is not prepared",
+}, async () => {
+ await withCapture({ launch: { executablePath } }, async ({ page }) => {
+ for (const [source, width] of [["supercalifragilisticexpialidocious more", 170],
+ ["<3개월 만에|> 완료했습니다", 170], ["是的 就是这样 3D hello", 170]] as const) {
+ await page.setContent(cueHtml(source, width, {
+ "active-box-padding": "12 8", "active-box-border-width": 2, "active-box-border-color": "#000000",
+ "active-box-radius": 8,
+ }));
+ // Reverse/repeated seeks and a resized Cue exercise measurement after layout changes.
+ for (const [frame, scale, size] of [[0, 1, width], [20, 1, width], [5, .7, width],
+ [0, 1, width + 40], [20, 1, width]] as const) {
+ const coverage = await page.evaluate(({ frame, scale, size }) => {
+ const cue = document.querySelector<HTMLElement>('[data-hypit-element-id="cue"]')!;
+ cue.style.width = `${size}px`;
+ // The box is measured in its own coordinates even while its ancestors move.
+ cue.style.transform = `rotate(12deg) scale(${scale})`;
+ window.dispatchEvent(new CustomEvent("hf-seek", { detail: { time: frame / 30 } }));
+ const error = (window as unknown as { __hypitBrowserProgramError?: string }).__hypitBrowserProgramError;
+ const layers = Array.from(document.querySelectorAll<HTMLElement>('[data-caption-active-box="joined"]'));
+ const index = Math.min(Math.floor(frame / 5), layers.length - 1);
+ const path = layers[index]!.querySelector<SVGPathElement>('path')!;
+ const missing: string[] = [];
+ for (const word of Array.from(document.querySelectorAll<HTMLElement>('[data-fine-box-word]'))) {
+ const match = /^joined-group-(\d+)-word-(\d+)$/u.exec(word.getAttribute("data-hypit-element-id")!)!;
+ if (Number(match[1]) > index) continue;
+ // The local mirror has identical base layout. Offset rectangles are untransformed;
+ // use browser Range fragments before applying Cue motion for coverage checks.
+ cue.style.transform = "none";
+ const range = document.createRange(); range.selectNodeContents(word);
+ for (const rect of Array.from(range.getClientRects())) {
+ const point = new DOMPoint((rect.left + rect.right) / 2, (rect.top + rect.bottom) / 2)
+ .matrixTransform(path.getScreenCTM()!.inverse());
+ if (!path.isPointInFill(point)) missing.push(word.getAttribute("data-hypit-element-id")!);
+ }
+ cue.style.transform = `rotate(12deg) scale(${scale})`;
+ }
+ return { error, missing, path: path.getAttribute("d"), count: layers.filter(layer => Number(getComputedStyle(layer).opacity) > 0).length };
+ }, { frame, scale, size });
+ assert.equal(coverage.error, undefined);
+ assert.deepEqual(coverage.missing, [], JSON.stringify({ source, frame, scale, size }));
+ assert.ok(coverage.path);
+ assert.equal(coverage.count, 1);
+ }
+ }
+ await page.setContent(cueHtml("supercalifragilisticexpialidocious", 170, {
+ "active-box-padding": "12 8", "active-box-border-width": 2, "active-box-border-color": "#000000",
+ }));
+ const samples = await page.evaluate(() => {
+ const word = document.querySelector('[data-hypit-element-id="atom-1-base-1"]')!;
+ const range = document.createRange(); range.selectNodeContents(word);
+ const lines = Array.from(range.getClientRects());
+ for (const atom of Array.from(document.querySelectorAll<HTMLElement>('[data-caption-atom]'))) atom.style.visibility = "hidden";
+ window.dispatchEvent(new CustomEvent("hf-seek", { detail: { time: 1 } }));
+ return {
+ count: lines.length,
+ points: lines.map(line => ({ x: (line.left + line.right) / 2, y: (line.top + line.bottom) / 2 })).concat(
+ lines.slice(1).map((line, i) => ({ x: (Math.max(line.left, lines[i]!.left) + Math.min(line.right, lines[i]!.right)) / 2,
+ y: (lines[i]!.bottom + line.top) / 2 }))),
+ };
+ });
+ assert.ok(samples.count > 1, "The fixture must actually wrap within one word");
+ const { data, info } = await sharp(await page.screenshot()).removeAlpha().raw().toBuffer({ resolveWithObject: true });
+ for (const point of samples.points) {
+ const offset = (Math.floor(point.y) * info.width + Math.floor(point.x)) * info.channels;
+ assert.deepEqual([...data.subarray(offset, offset + 3)], [127, 191, 127], `background at ${JSON.stringify(point)}`);
+ }
+ });
+});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 27aafb8e..fcc8f30c 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -632,6 +632,9 @@ importers:
'@hypit/elaborator':
specifier: workspace:*
version: link:../elaborator
+ '@hypit/hyperframes':
+ specifier: workspace:*
+ version: link:../hyperframes
'@hypit/markup':
specifier: workspace:*
version: link:../markup