Skip to content

Commit c31e7dd

Browse files
fix(cli): check times a line held across a split clip as one hold (#7733)
* fix(cli): check times a line held across a split clip as one hold A clip split in two hands its line to a second element at the same place. line_unread timed each element alone, so the first half warned while the same text stayed on screen. * refactor(cli): name the split-clip line hand-off instead of commenting it
1 parent 4313241 commit c31e7dd

2 files changed

Lines changed: 66 additions & 16 deletions

File tree

‎packages/cli/src/utils/craftAudit.test.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ interface Shown {
2626
block?: number;
2727
/** The block's rendered text; defaults to `text`. */
2828
blockText?: string;
29+
/** Top edge in px; defaults to a row per key. */
30+
top?: number;
2931
}
3032

3133
function part(shown: Shown): CraftTextPart {
@@ -40,7 +42,7 @@ function part(shown: Shown): CraftTextPart {
4042
blockText: shown.blockText ?? shown.text,
4143
opacity: 1,
4244
fontPx,
43-
rect: { left: 100, top: 100 + shown.key * 10, width: 800, height: fontPx * 1.2 },
45+
rect: { left: 100, top: shown.top ?? 100 + shown.key * 10, width: 800, height: fontPx * 1.2 },
4446
captions: shown.captions ?? false,
4547
};
4648
}
@@ -218,6 +220,25 @@ describe("line_unread", () => {
218220
expect(await detectLineUnread(film(12, shown), 12, CANVAS, probeFor(shown))).toEqual([]);
219221
});
220222

223+
// fails without the fix: each instance of a split clip was timed alone, so its 0.5 s first half warned.
224+
it("times a line held across a split clip's two instances as one hold", async () => {
225+
const line = "Your reminder card holds here";
226+
const shown: Shown[] = [
227+
{ key: 1, text: line, from: 3, to: 3.5, top: 800 },
228+
{ key: 2, text: line, from: 3.5, to: 8, top: 800 },
229+
];
230+
expect(await detectLineUnread(film(8, shown), 8, CANVAS, probeFor(shown))).toEqual([]);
231+
});
232+
233+
it("still times a different line that takes over the same place on its own", async () => {
234+
const shown: Shown[] = [
235+
{ key: 1, text: "Every team gets one shared calendar now", from: 3, to: 3.5, top: 800 },
236+
{ key: 2, text: "Start your free trial today", from: 3.5, to: 8, top: 800 },
237+
];
238+
const findings = await detectLineUnread(film(8, shown), 8, CANVAS, probeFor(shown));
239+
expect(findings.map((finding) => finding.selector)).toEqual(["#t1"]);
240+
});
241+
221242
it("skips text too small to be a reading line", async () => {
222243
const shown: Shown[] = [
223244
{ key: 1, text: "Legal fine print that flashes by", from: 1, to: 1.3, fontPx: 18 },

‎packages/cli/src/utils/craftAudit.ts‎

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ interface CraftRect {
211211
}
212212

213213
interface BlockTrack {
214-
key: number;
214+
blockKeys: Set<number>;
215215
selector: string;
216216
sourceFile: string;
217217
/** Frame index → what the block showed there. */
@@ -258,20 +258,47 @@ interface Anchor {
258258
}
259259

260260
function trackBlocks(frames: CraftFrame[], readHeight: number): BlockTrack[] {
261-
const tracks = new Map<number, BlockTrack>();
261+
const tracks: BlockTrack[] = [];
262+
const byKey = new Map<number, BlockTrack>();
262263
frames.forEach((frame, index) => {
263264
for (const [key, block] of readableBlocks(frame, readHeight)) {
264-
const track = tracks.get(key) ?? {
265-
key,
266-
selector: block.selector,
267-
sourceFile: block.sourceFile,
268-
seen: new Map<number, BlockFrame>(),
269-
};
265+
let track = byKey.get(key) ?? trackContinuedBy(tracks, index, block);
266+
if (!track) {
267+
track = {
268+
blockKeys: new Set(),
269+
selector: block.selector,
270+
sourceFile: block.sourceFile,
271+
seen: new Map(),
272+
};
273+
tracks.push(track);
274+
}
275+
track.blockKeys.add(key);
276+
byKey.set(key, track);
270277
track.seen.set(index, { text: block.text, rect: block.rect });
271-
tracks.set(key, track);
272278
}
273279
});
274-
return [...tracks.values()];
280+
return tracks;
281+
}
282+
283+
function trackContinuedBy(
284+
tracks: BlockTrack[],
285+
index: number,
286+
block: BlockFrame,
287+
): BlockTrack | undefined {
288+
return tracks.find((track) => {
289+
const last = track.seen.get(index) ?? track.seen.get(index - 1);
290+
return !!last && last.text === block.text && sameBox(last.rect, block.rect);
291+
});
292+
}
293+
294+
const SAME_BOX_OVERLAP = 0.9;
295+
296+
function sameBox(a: CraftRect, b: CraftRect): boolean {
297+
const width = Math.min(a.right, b.right) - Math.max(a.left, b.left);
298+
const height = Math.min(a.bottom, b.bottom) - Math.max(a.top, b.top);
299+
if (width <= 0 || height <= 0) return false;
300+
const area = (rect: CraftRect) => (rect.right - rect.left) * (rect.bottom - rect.top);
301+
return (width * height) / Math.max(area(a), area(b)) >= SAME_BOX_OVERLAP;
275302
}
276303

277304
/** Maximal runs of consecutive frame indices. */
@@ -374,8 +401,10 @@ async function refineWindow(
374401
canvas: Canvas,
375402
): Promise<LineWindow> {
376403
const readHeight = T.readHeightAt1080 * (Math.min(canvas.width, canvas.height) / 1080);
377-
const visibleAt = async (time: number) =>
378-
readableBlocks(await probe(time), readHeight).has(candidate.track.key);
404+
const visibleAt = async (time: number) => {
405+
const blocks = readableBlocks(await probe(time), readHeight);
406+
return [...candidate.track.blockKeys].some((key) => blocks.has(key));
407+
};
379408
const window = { ...candidate.window };
380409
for (let step = 0; step < T.refineSteps; step += 1) {
381410
if (window.startIn > window.startOut) {
@@ -445,7 +474,7 @@ function lineUnreadFinding(
445474
words,
446475
seconds: round(held),
447476
floorSeconds: round(floor),
448-
measured: measuredAround(frames, track.key, candidate.firstIndex, candidate.lastIndex),
477+
measured: measuredAround(frames, track.blockKeys, candidate.firstIndex, candidate.lastIndex),
449478
};
450479
}
451480

@@ -454,7 +483,7 @@ const MEASURED_SPAN = 3;
454483
/** The samples at both ends of the line's run, as the sampler measured them. */
455484
function measuredAround(
456485
frames: CraftFrame[],
457-
key: number,
486+
keys: Set<number>,
458487
first: number,
459488
last: number,
460489
): NonNullable<CraftFinding["measured"]> {
@@ -466,7 +495,7 @@ function measuredAround(
466495
for (const index of [...indices].sort((a, b) => a - b)) {
467496
const frame = frames[index];
468497
if (!frame) continue;
469-
const parts = frame.parts.filter((part) => part.block === key && !part.captions);
498+
const parts = frame.parts.filter((part) => keys.has(part.block) && !part.captions);
470499
if (parts.length === 0) {
471500
out.push({ time: round(frame.time), opacity: null, heightPx: null });
472501
continue;

0 commit comments

Comments
 (0)