// gap-mark.mjs (lane gap-cue, slice 1): where a segment's joins are, which are // marked, where each mark stands, and the drawtext that draws it. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { GAP_CUE_LIFE, gapCueLook } from "yt-dlp-transcript-common/lib/report/gapCue.mjs"; import { GAP_MARK_FIT_MIN, censorAvoidRects, centredIn, gapMarkDrift, gapMarkFilters, gapMarkFit, gapMarkPlaces, gapMarkRect, gapMarkSize, insideRect, recordGapMarks, segmentGapJoins, segmentGapMarks, twoCropMarkBox, verticalMarkBox, } from "./gap-mark.mjs"; import { clipGeometry, desktopGeometry, desktopSafeArea, safeArea } from "./vertical.mjs"; import { stackedGeometry } from "./vertical-deck.mjs"; import { verticalRender } from "./vertical.mjs"; test("the joins: each gap's on the segment's clock (the seconds before it that play), its length, its mark", () => { // A segment from source 100 s: gaps at 110–137 (27 s) and 150–153 (3 s), on its own clock 10–37 and 50–53. const joins = segmentGapJoins({ segGaps: [{ from: 10, to: 37 }, { from: 50, to: 53 }], clipGaps: [{ from: 110, to: 137 }, { from: 150, to: 153, mark: false }], segStart: 100, cue: { mark: true }, }); assert.deepEqual(joins, [{ at: 10, seconds: 27, mark: true }, { at: 23, seconds: 3, mark: false }]); // A clip cut from its pieces: the seconds a join says are the recording's (toReal). const pieced = segmentGapJoins({ segGaps: [{ from: 4, to: 4.5 }], clipGaps: [{ from: 5, to: 5.5 }], segStart: 1, cue: { mark: true }, toReal: (t) => (t >= 5.5 ? t + 184.5 : t) }); assert.deepEqual(pieced, [{ at: 4, seconds: 185, mark: true }]); }); test("the marks: the clip's settings over the project's; under min none; the label's text", () => { const joins = [{ at: 2, seconds: 27, mark: true }, { at: 8, seconds: 1.5, mark: true }, { at: 12, seconds: 75, mark: true }]; const marks = segmentGapMarks({ render: { gapCue: { mark: true } }, entry: { type: "clip", gapCue: { label: "{s} later" } }, joins, seconds: 20 }); assert.deepEqual(marks.map((m) => [m.at, m.text]), [[2, "27 s later"], [12, "1:15 later"]]); assert.equal(marks[0].until, Number((2 + GAP_CUE_LIFE).toFixed(4))); }); test("the place: the layout's corner, inset; off a hidden region while it is up; never a busy corner", () => { const size = gapMarkSize(1920, 1080); // GAP_CUE_LOOK (operator, 20 Nov: bigger, a border): 3 % of 1080, the box 0.38 em round it, a 2 px stroke. assert.deepEqual(size, { fontSize: 32, border: 12, stroke: 2, inset: 28 }); const box = { x: 100, y: 50, w: 1600, h: 900 }; const marks = [{ at: 2, until: 3.1, text: "» 27 s" }, { at: 9, until: 10.1, text: "» 27 s" }]; const r = gapMarkRect(6, size); assert.deepEqual(r, { w: 116 + 28, h: 39 + 28 }, "six Plex Mono glyphs at 32 px, the box and the stroke round them"); const bl = gapMarkPlaces({ marks, box, want: "auto", prefer: "bl", size }); assert.deepEqual(bl.map((p) => p.corner), ["bl", "bl"]); assert.deepEqual(bl[0].rect, { x: 128, y: 50 + 900 - 28 - r.h, w: r.w, h: r.h }); // A box over the bottom left from 1.5 to 4 s: the first mark moves to the top left, the second stays. const avoid = [{ x: 100, y: 800, w: 400, h: 150, from: 1.5, to: 4 }]; assert.deepEqual(gapMarkPlaces({ marks, box, want: "auto", prefer: "bl", size, avoid }).map((p) => p.corner), ["tl", "bl"]); // An asked corner wins unless something is under it; a busy corner is skipped. assert.deepEqual(gapMarkPlaces({ marks, box, want: "br", prefer: "bl", size }).map((p) => p.corner), ["br", "br"]); assert.deepEqual(gapMarkPlaces({ marks, box, want: "auto", prefer: "tl", size, busy: ["tl"] }).map((p) => p.corner), ["tr", "tr"]); // A mark's own corner (its gap's, operator 20 Nov: a stream's layout can change inside one clip) over the clip's. assert.deepEqual(gapMarkPlaces({ marks: [{ ...marks[0], corner: "tr" }, marks[1]], box, want: "br", prefer: "bl", size }).map((p) => p.corner), ["tr", "br"]); }); test("the reason and the corner a join's own gaps give: the reason when they all say the same, the first corner", () => { const joins = segmentGapJoins({ segGaps: [{ from: 10, to: 37 }, { from: 50, to: 53 }], clipGaps: [{ from: 110, to: 137, why: "dead-air", corner: "tr" }, { from: 150, to: 153, why: "other" }], segStart: 100, cue: { mark: true }, }); assert.deepEqual(joins, [{ at: 10, seconds: 27, mark: true, why: "dead air", corner: "tr" }, { at: 23, seconds: 3, mark: true }]); const marks = segmentGapMarks({ render: { gapCue: { mark: true } }, entry: { type: "clip" }, joins, seconds: 40 }); assert.deepEqual(marks.map((m) => [m.text, m.corner ?? null]), [["» 27 s · dead air", "tr"], ["» 3 s", null]]); }); test("a hidden region in the frame: its source fractions through the fit, its seconds on the segment's clock", () => { // A 1280×720 source fitted into a 1600×900 box at (100, 50); a region on its left half over source 120–130 s. const dest = centredIn({ x: 100, y: 50, w: 1600, h: 900 }, { w: 1600, h: 900 }); const [a] = censorAvoidRects([{ box: [0, 0.5, 0.5, 0.5], from: 120, to: 130 }], { sw: 1280, sh: 720, dest, from: 100, gaps: [{ from: 105, to: 110 }], seconds: 60 }); assert.deepEqual(a, { x: 100, y: 500, w: 800, h: 450, from: 15, to: 25 }); // A 9:16 crop shown full: the region through the crop. const [b] = censorAvoidRects([{ box: [0.25, 0, 0.25, 1], from: null, to: null }], { sw: 1920, sh: 1080, region: { x: 480, y: 0, w: 608, h: 1080 }, dest: { x: 0, y: 0, w: 1080, h: 1920 } }); assert.deepEqual([b.x, b.w, b.from, b.to], [0, 853, null, null]); }); test("the filters: per mark its border (the text unseen, a wider box) then its text on its box, faded by alpha over its seconds", () => { const size = gapMarkSize(1920, 1080); const f = gapMarkFilters({ marks: [{ at: 2, until: 3.1 }], places: [{ rect: { x: 128, y: 970 } }], files: ["/out/segments/c1.gapmark-0.txt"], size, font: "/fonts/Mono.ttf", }); const at = "x=142:y=984:alpha='0.9*clip(min((t-2)/0.15,(3.1-t)/0.35),0,1)':enable='between(t,2,3.1)'"; assert.equal( f, // Variant B with no look passed: the fallback amber border at full opacity, the box at 0.9. `drawtext=textfile='/out/segments/c1.gapmark-0.txt':expansion=none:fontfile='/fonts/Mono.ttf':fontsize=32:fontcolor=0xffffff@0:box=1:boxcolor=0xf5b300@1:boxborderw=14:${at},` + `drawtext=textfile='/out/segments/c1.gapmark-0.txt':expansion=none:fontfile='/fonts/Mono.ttf':fontsize=32:fontcolor=0xffffff:box=1:boxcolor=0x101010@0.9:boxborderw=12:${at}`, ); assert.equal(gapMarkFilters({ marks: [], places: [], files: [], size }), ""); }); test("the drawtext's border (variant B): the colour the clip's look resolves -- the deck's accent, a setting's colour, the fallback with none", () => { const size = gapMarkSize(1920, 1080); const one = (look) => gapMarkFilters({ marks: [{ at: 2, until: 3.1 }], places: [{ rect: { x: 128, y: 970 } }], files: ["/f.txt"], size, font: "/m.ttf", look }); const borderOf = (f) => /fontcolor=0xffffff@0:box=1:boxcolor=([^:]+):/.exec(f)?.[1]; const boxOf = (f) => /fontcolor=0xffffff:box=1:boxcolor=([^:]+):/.exec(f)?.[1]; const clip = { type: "clip", id: "c1", gaps: [{ from: 3, to: 9 }] }; assert.equal(borderOf(one(gapCueLook({ palette: { accent: "#1fb5a8" } }, clip))), "0x1fb5a8@1", "the palette's accent"); assert.equal(borderOf(one(gapCueLook({ palette: { accent: "#e8742a" } }, clip))), "0xe8742a@1"); assert.equal(borderOf(one(gapCueLook({}, clip))), "0xf5b300@1", "no palette: the fallback"); assert.equal(borderOf(one(gapCueLook({ palette: { accent: "#1fb5a8" }, gapCue: { border: "#3aa0ff" } }, clip))), "0x3aa0ff@1", "the project's colour"); assert.equal(borderOf(one(gapCueLook({ palette: { accent: "#1fb5a8" }, gapCue: { border: "#3aa0ff" } }, { ...clip, gapCue: { border: "accent" } }))), "0x1fb5a8@1", "the clip's accent over it"); // The text white, the box near-black at 0.9, whatever the border. assert.equal(boxOf(one(gapCueLook({ palette: { accent: "#1fb5a8" } }, clip))), "0x101010@0.9"); assert.match(one(gapCueLook({ palette: { accent: "#1fb5a8" } }, clip)), /fontcolor=0xffffff:box=1/); }); test("every drawtext draws its text as written: expansion=none, so a % or a backslash in a reason is words (review 1, 1)", () => { const size = gapMarkSize(1920, 1080); const marks = [{ at: 2, until: 3.1 }, { at: 6, until: 7.1 }]; const f = gapMarkFilters({ marks, places: [{ rect: { x: 0, y: 0 } }, { rect: { x: 0, y: 0 } }], files: ["/a.txt", "/b.txt"], size }); const each = f.split(/,(?=drawtext=)/); assert.equal(each.length, 4, "a border and a text per mark"); for (const d of each) assert.match(d, /^drawtext=textfile='[^']+':expansion=none:/); }); test("a mark too wide for its box is drawn smaller, then cut, and never stands off the box (review 1, 9)", () => { // A 1080-wide vertical frame: 32 px, inset 28. A 40-character label with a 24-character reason: 64 characters. const size = gapMarkSize(1080, 1920); const box = { x: 0, y: 0, w: 1080, h: 1920 }; const text = `» 1:02:03 later and later and later · ${"r".repeat(24)}`; const full = gapMarkRect([...text].length, size); assert.ok(full.w > box.w, `as it is it would run off (${full.w} px)`); const fit = gapMarkFit(text, size, box.w - 2 * size.inset); assert.ok(fit.size.fontSize >= Math.ceil(size.fontSize * GAP_MARK_FIT_MIN) && fit.size.fontSize < size.fontSize, `shrunk to its floor at most (${fit.size.fontSize})`); assert.ok(gapMarkRect([...fit.text].length, fit.size).w <= box.w - 2 * size.inset, "it fits"); assert.equal(fit.text, text, "at a smaller face every word is kept"); // Narrower still (a 720-wide box): at the floor and cut, ending "…". const cut = gapMarkFit(text, size, 720 - 2 * size.inset); assert.equal(cut.size.fontSize, Math.ceil(size.fontSize * GAP_MARK_FIT_MIN)); assert.ok(gapMarkRect([...cut.text].length, cut.size).w <= 720 - 2 * size.inset, "it fits"); assert.ok(cut.text.endsWith("…") && text.startsWith(cut.text.slice(0, -1)), `cut, ending … (${cut.text})`); // Placed: every corner's rect inside the box; the record's text is the drawn one. for (const want of ["tl", "tr", "bl", "br"]) { for (const [b, f] of [[box, fit], [{ x: 0, y: 0, w: 720, h: 1280 }, cut]]) { const [p] = gapMarkPlaces({ marks: [{ at: 1, until: 2.1, text }], box: b, want, prefer: "tl", size }); assert.ok(p.rect.x >= b.x && p.rect.x + p.rect.w <= b.x + b.w && p.rect.y >= b.y, `${want}: inside (${JSON.stringify(p.rect)})`); assert.equal(p.text, f.text); assert.equal(p.size.fontSize, f.size.fontSize); } } // Only a little too wide: a smaller face, every word kept. const near = gapMarkFit("» 27 s · dead air", size, gapMarkRect(17, size).w - 10); assert.equal(near.text, "» 27 s · dead air"); assert.ok(near.size.fontSize < size.fontSize); // One that fits is as it was; the filters draw a fitted one at its own size. assert.deepEqual(gapMarkFit("» 27 s", size, 1000), { text: "» 27 s", size }); const f = gapMarkFilters({ marks: [{ at: 1, until: 2.1 }], places: [{ rect: { x: 0, y: 0 }, size: near.size }], files: ["/a.txt"], size }); assert.match(f, new RegExp(`fontsize=${near.size.fontSize}:`)); // A box narrower than any mark: at the box's own edge, never left of it. const [tiny] = gapMarkPlaces({ marks: [{ at: 1, until: 2.1, text: "» 27 s" }], box: { x: 100, y: 0, w: 60, h: 400 }, want: "tr", prefer: "tl", size }); assert.equal(tiny.rect.x, 100); assert.deepEqual(insideRect({ x: 0, y: 0, w: 1080, h: 960 }, { x: 60, y: 192, w: 960, h: 1536 }), { x: 60, y: 192, w: 960, h: 768 }); }); test("a clip cut from its pieces with no pad: the join between pieces is marked, from the recording's seconds (review 1, 8)", () => { // Pieces 1–5 and 190–200 joined end to end: on the joined file's clock the gap is nothing; in the record it is 185 s. const render = { gapCue: { mark: true } }; const entry = { type: "clip", id: "j1", start: 1, end: 200, gaps: [{ from: 5, to: 190, why: "idle" }] }; const record = { start: 1, end: 200, gaps: [{ from: 5, to: 190 }] }; const marks = recordGapMarks({ render, entry, record }); assert.deepEqual(marks.map((m) => [m.at, m.seconds, m.text]), [[4, 185, "» 3:05 · idle"]]); // The same as the joins the build takes for it (segmentGapJoins over the recording's seconds). const joins = segmentGapJoins({ segGaps: [{ from: 4, to: 189 }], clipGaps: entry.gaps, segStart: 1, cue: { mark: true } }); assert.deepEqual(segmentGapMarks({ render, entry, joins, seconds: 14 }), marks); // The joined file's own clock (what gapsOnSegment saw) has no gap: there the build used to draw nothing. assert.deepEqual(segmentGapJoins({ segGaps: [], clipGaps: [{ from: 4, to: 4 }], segStart: 0, cue: { mark: true } }), []); }); test("gapMarkDrift: a segment's recorded marks against what the settings draw now (--chrome-only's note, review 1, 13)", () => { const entry = { type: "clip", id: "c1", gaps: [{ from: 13, to: 40 }] }; const record = { start: 10, end: 60, gaps: [{ from: 13, to: 40 }] }; const on = { gapCue: { mark: true } }; const drawn = recordGapMarks({ render: on, entry, record }).map((m) => ({ at: m.at, until: m.until, seconds: m.seconds, text: m.text, corner: "bl" })); assert.equal(gapMarkDrift({ render: on, entry, record: { ...record, gapMarks: drawn } }), null, "as built"); assert.equal(gapMarkDrift({ render: {}, entry, record }), null, "off, none drawn"); assert.equal(gapMarkDrift({ render: on, entry, record }), "1 mark to draw, none drawn"); assert.equal(gapMarkDrift({ render: {}, entry, record: { ...record, gapMarks: drawn } }), "its 1 mark no longer wanted"); assert.match(gapMarkDrift({ render: { gapCue: { mark: true, label: "{s} later" } }, entry, record: { ...record, gapMarks: drawn } }) ?? "", /"» 27 s" now says "27 s later"/); assert.match(gapMarkDrift({ render: on, entry: { ...entry, gapCue: { corner: "tr" } }, record: { ...record, gapMarks: drawn } }) ?? "", /another corner/); // A text cut to fit agrees with the text it was cut from. assert.equal(gapMarkDrift({ render: on, entry, record: { ...record, gapMarks: [{ ...drawn[0], text: "» 2…" }] } }), null); // Its border (variant B): judged when recorded (a record from before kept none, and is not judged by it). const teal = { ...on, palette: { accent: "#1fb5a8" } }; assert.equal(gapMarkDrift({ render: teal, entry, record: { ...record, gapMarks: [{ ...drawn[0], border: "#1fb5a8" }] } }), null); assert.equal(gapMarkDrift({ render: teal, entry: { ...entry, gapCue: { border: "#3aa0ff" } }, record: { ...record, gapMarks: [{ ...drawn[0], border: "#1fb5a8" }] } }), "the mark's border is #3aa0ff now, drawn #1fb5a8"); }); test("a vertical clip's mark stands inside the safe area in every layout; a split's at its picture's top right, off its captions (review 1, 3 and 4)", () => { const render = { format: "vertical" }; const [W, H] = [1080, 1920]; const a = safeArea(render, W, H); const size = gapMarkSize(W, H); const inside = (r, b) => r.x >= b.x && r.y >= b.y && r.x + r.w <= b.x + b.w && r.y + r.h <= b.y + b.h; const safe = { x: a.x0, y: a.y0, w: a.x1 - a.x0, h: a.y1 - a.y0 }; const want = { framed: "tl", quote: "tl", split: "tr", full: "tr", overlay: "tr" }; for (const layout of Object.keys(want)) { const geo = clipGeometry(layout, render, W, H); const { box, prefer } = verticalMarkBox(geo, a); assert.equal(prefer, want[layout], layout); assert.ok(inside(box, safe), `${layout}: its box inside the safe area (${JSON.stringify(box)})`); for (const corner of ["tl", "tr", "bl", "br"]) { const [p] = gapMarkPlaces({ marks: [{ at: 1, until: 2.1, text: "» 27 s · dead air" }], box, want: corner, prefer, size }); assert.ok(inside(p.rect, safe), `${layout} ${corner}: the mark inside the safe area (${JSON.stringify(p.rect)})`); } } // The split: its captions stand at its picture's foot (align 2, marginV from the frame's bottom): the mark at the top. const split = clipGeometry("split", render, W, H); const [sp] = gapMarkPlaces({ marks: [{ at: 1, until: 2.1, text: "» 27 s" }], ...verticalMarkBox(split, a), want: "auto", size }); const captionsTop = H - split.captions.marginV - split.size * 3; assert.equal(sp.corner, "tr"); assert.ok(sp.rect.y + sp.rect.h < captionsTop, `above the captions (${sp.rect.y + sp.rect.h} < ${captionsTop})`); // A desktop clip: its own safe area. const d = desktopGeometry({}, 1920, 1080, { qr: null }); const ds = desktopSafeArea({}, 1920, 1080); assert.ok(inside(verticalMarkBox(d, ds).box, { x: ds.x0, y: ds.y0, w: ds.x1 - ds.x0, h: ds.y1 - ds.y0 })); // Twocrop: crop one's top right, inside the safe area. const g = stackedGeometry(verticalRender({ width: 1920, height: 1080, fps: 30, format: "vertical", chrome: { engine: "hyperframes", layout: "deck", deck: {} } })); const t = twoCropMarkBox(g); assert.equal(t.prefer, "tr"); assert.ok(inside(t.box, { x: g.safe.x0, y: g.safe.y0, w: g.safe.x1 - g.safe.x0, h: g.safe.y1 - g.safe.y0 })); assert.ok(t.box.y >= g.twocrop.crop1.y && t.box.y + t.box.h <= g.twocrop.crop1.y + g.twocrop.crop1.height, "within crop one"); });