// WHERE THE GAP MARK STANDS (lane gap-cue): the pure half of gap-mark.mjs -- // the marks a segment's joins make, their sizes, their corner and the hop off // what must stay clear -- with no font file and nothing of node's, so the // deck's schedule (deck.mjs, the gap-cue module's slice) and umtool's pages // import it as they import gaps.mjs. gap-mark.mjs re-exports it and adds the // drawtext that draws a mark into a clip's segment. // // WHERE. In a box the layout names -- the footage as it plays (the deck's // footage box, the plain cut's picture, a vertical cut's picture or crop one // inside its safe area) -- at a corner `inset` in from its edges: the // layout's own free corner (`auto`: the deck's stamp stands top-left and its // posts top-right, so bottom-left; the plain cut's captions sit at its foot // and its code at the bottom right, so top-left; a vertical split's captions // at its picture's foot, so top-right), or the one `corner` says. A hidden // region up while the mark is, or a code, under that corner moves it to the // next free one (tl, tr, bl, br); none free, it stays where it was asked. A // mark too wide for its box is drawn smaller, then cut ("…"): never off it. // // PURE: no fs, no ffmpeg run. import { GAP_CUE_LOOK, clipGapCueOf, gapCueBorderOf, gapCueText, gapCueWanted, gapCues, gapMarkOn, gapOwnSays } from "yt-dlp-transcript-common/lib/report/gapCue.mjs"; import { offsetIn, renderGaps } from "./gaps.mjs"; /** Plex Mono's advance, in ems: every glyph the same width. */ const ADVANCE = 0.6; const EPS = 1e-6; /** * The mark's sizes for a `W` × `H` frame (GAP_CUE_LOOK): its font size, the * box round the text (`border`, drawtext's boxborderw), the border's stroke, * and the inset from the box it stands in. `look` overrides GAP_CUE_LOOK's * numbers (the operator's stills of the variants). */ export function gapMarkSize(W, H, look = GAP_CUE_LOOK) { const L = { ...GAP_CUE_LOOK, ...look }; const s = Math.min(W, H); const fontSize = Math.max(12, Math.round(s * L.font)); return { fontSize, border: Math.round(fontSize * L.pad), stroke: L.stroke > 0 ? Math.max(L.strokeMin, Math.round(s * L.stroke)) : 0, inset: Math.max(8, Math.round(s * L.inset)), }; } /** How wide a text draws in Plex Mono at `fontSize` px: every glyph ADVANCE em (the drawtext's face). */ export const monoWidth = (text, fontSize) => Math.ceil([...String(text)].length * ADVANCE * fontSize); /** * About how wide a text draws in the deck's bold sans at `fontSize` px -- each character at a bold sans's advance * for its kind, a little over the measured widths (as deck.mjs markerLabelWidth reckons a label), so a mark called * fitting does fit. What the gap-cue module's page (DeckSansBold) is placed and hopped by; the page itself sizes * the element to its text. */ export function sansWidth(text, fontSize) { let em = 0; for (const ch of String(text)) { if (ch === " ") em += 0.26; else if (/[.,:;'!|’‘·]/.test(ch)) em += 0.3; else if (/[0-9]/.test(ch)) em += 0.58; else if (/[MWmw@&]/.test(ch)) em += 0.88; else if (/[A-Z]/.test(ch)) em += 0.66; else if (/[»«…]/.test(ch)) em += 0.6; else em += 0.56; } return Math.ceil(em * fontSize); } /** * The rect a text of `chars` characters takes with its box and border, at `size` (gapMarkSize) -- `textW` its * text's own width when it is measured otherwise (a proportional face: sansWidth), else the mono advance. */ export function gapMarkRect(chars, size, textW = null) { const edge = size.border + (size.stroke ?? 0); return { w: (textW ?? Math.ceil(chars * ADVANCE * size.fontSize)) + 2 * edge, h: Math.ceil(size.fontSize * 1.2) + 2 * edge }; } /** * A segment's JOINS on its own clock, from the gaps it really left out: * `segGaps` (gaps.mjs gapsOnSegment, seconds from its first frame, before * the gaps are taken out) and the clip's gaps as the render applies them * (`clipGaps`, source seconds -- with their own `mark` -- on the clock * `segStart` is on). `toReal` maps that clock to the recording's (a clip cut * from its pieces), so the seconds a join says are the recording's. * * @param {{ segGaps: Array<{ from: number, to: number }>, clipGaps: Array<{ from: number, to: number, mark?: boolean }>, * segStart: number, cue: { mark: boolean }, toReal?: (t: number) => number }} o * Each join says its reason (gapWhyWord: the clip's gaps it is agreeing, else * none) and its own corner (the first of them that names one), only when it * has them. * * @returns {Array<{ at: number, seconds: number, mark: boolean, why?: string, corner?: string }>} */ export function segmentGapJoins({ segGaps, clipGaps, segStart, cue, toReal = (t) => t }) { let removed = 0; return (segGaps ?? []).map((g) => { const a = segStart + g.from; const b = segStart + g.to; // The clip's gaps this one is (gapsOnSegment may have joined two that touch on the frame grid). const own = (clipGaps ?? []).filter((c) => c.to > a - 0.05 && c.from < b + 0.05); const join = { at: g.from - removed, seconds: toReal(b) - toReal(a), mark: own.length ? own.some((c) => gapMarkOn(cue, c)) : cue.mark, ...gapOwnSays(own) }; removed += g.to - g.from; return join; }); } /** * The marks one segment draws: its joins (segmentGapJoins) under the clip's * settings (clipGapCueOf: `min`, and `label` for the text). [] when nothing * is marked -- the segment's graph is then the one it always was. * * @param {{ render: any, entry: any, joins: Array<{ at: number, seconds: number, mark: boolean }>, seconds: number }} o * @returns {Array<{ at: number, until: number, seconds: number, count: number, text: string }>} */ export function segmentGapMarks({ render, entry, joins, seconds }) { const cue = clipGapCueOf(render, entry); return gapCues(joins, { min: cue.min, seconds }).map((c) => ({ ...c, text: gapCueText(c.seconds, cue.label, c.why) })); } /** * The marks a segment built with cut record `record` (its `start`, `end` and `gaps`: the recording's seconds, * every second left out -- between a clip's pieces too) draws under the settings NOW (`render`, the clip's `entry`): * its joins from the record, as the build takes them for a clip cut from its pieces. [] when nothing is marked. * * @param {{ render: any, entry: any, record: { start?: number, end?: number, gaps?: Array<{ from: number, to: number }> } | null }} o */ export function recordGapMarks({ render, entry, record }) { const gaps = Array.isArray(record?.gaps) ? record.gaps : []; const start = Number(record?.start); const end = Number(record?.end); if (!gaps.length || !Number.isFinite(start) || !Number.isFinite(end) || !gapCueWanted(render, entry)) return []; const seconds = end - start - gaps.reduce((n, g) => n + (g.to - g.from), 0); return segmentGapMarks({ render, entry, seconds, joins: segmentGapJoins({ segGaps: gaps.map((g) => ({ from: g.from - start, to: g.to - start })), clipGaps: renderGaps(entry, render), segStart: start, cue: clipGapCueOf(render, entry) }), }); } /** * What the corner a mark ASKS for is (its gap's own, else the clip's; "auto" none): what a record keeps beside * the corner it stood at, so a change of it is seen. * * @param {{ corner?: string }} mark @param {string} want the clip's (clipGapCueOf) */ export const gapMarkAsked = (mark, want) => (typeof mark.corner === "string" && mark.corner ? mark.corner : want && want !== "auto" ? want : null); /** * Do a segment's recorded marks (its cut record's `gapMarks`: each `{at, until, seconds, text, corner, asked?, border?}`) * still say what the settings now draw (recordGapMarks)? null when they do, else what changed, in words -- for a * pass that rebuilds no segment (`--chrome-only`), which would otherwise lay the old marks (or none) without a word. * A recorded text cut to fit ("…") agrees with the text it was cut from. * * @param {{ render: any, entry: any, record: any }} o * @returns {string | null} */ export function gapMarkDrift({ render, entry, record }) { const now = recordGapMarks({ render, entry, record }); const was = Array.isArray(record?.gapMarks) ? record.gapMarks : []; if (!now.length && !was.length) return null; if (!was.length) return `${now.length} mark${now.length === 1 ? "" : "s"} to draw, none drawn`; if (!now.length) return `its ${was.length} mark${was.length === 1 ? "" : "s"} no longer wanted`; if (now.length !== was.length) return `${was.length} mark${was.length === 1 ? "" : "s"} drawn, ${now.length} now`; const want = clipGapCueOf(render, entry).corner; const sameText = (drawn, says) => drawn === says || (typeof drawn === "string" && drawn.endsWith("…") && says.startsWith(drawn.slice(0, -1).trimEnd())); for (const [k, m] of now.entries()) { const w = was[k]; if (Math.abs(m.at - Number(w?.at)) > 0.01 || Math.abs(m.until - Number(w?.until)) > 0.01) return `the mark at ${m.at} s moved`; if (!sameText(w?.text, m.text)) return `"${w?.text}" now says "${m.text}"`; if ((w?.asked ?? null) !== gapMarkAsked(m, want)) return `the mark at ${m.at} s asks for another corner`; // A record from before the border was kept (none recorded) is not judged by it. if (typeof w?.border === "string" && w.border !== gapCueBorderOf(render, entry)) return `the mark's border is ${gapCueBorderOf(render, entry)} now, drawn ${w.border}`; } return null; } const CORNERS = ["tl", "tr", "bl", "br"]; /** * A rect on `box`'s corner `c`, `inset` in. * * @param {{ x: number, y: number, w: number, h: number }} box * @param {string} c * @param {{ w: number, h: number }} r * @param {number} inset */ function cornerRect(box, c, r, inset) { const x = c.endsWith("l") ? box.x + inset : box.x + box.w - inset - r.w; const y = c.startsWith("t") ? box.y + inset : box.y + box.h - inset - r.h; // Never out of its box: a mark wider (or taller) than the box less its insets stands at the box's own edge. const cx = Math.max(box.x, Math.min(x, box.x + box.w - r.w)); const cy = Math.max(box.y, Math.min(y, box.y + box.h - r.h)); return { x: Math.round(cx), y: Math.round(cy), w: r.w, h: r.h }; } /** The smallest a mark's face is shrunk to, to fit its box (of its size): past it, its text is cut. */ export const GAP_MARK_FIT_MIN = 0.75; /** * A mark's text and size to fit `maxW` px (its box less its insets): as it is when it fits; else its face shrunk, * to GAP_MARK_FIT_MIN of its size at most (the box and border with it); still too wide, cut to what fits, ending * "…". A long label and a long reason on a 1080-wide vertical frame (64 characters, about 1230 px at 32 px). * * @param {string} text * @param {ReturnType} size * @param {number} maxW * @param {(text: string, fontSize: number) => number} [measure] the face's width (monoWidth: the drawtext's) * @returns {{ text: string, size: ReturnType }} */ export function gapMarkFit(text, size, maxW, measure = monoWidth) { const chars = [...text]; const rectAt = (t, s) => gapMarkRect([...t].length, s, measure(t, s.fontSize)); if (rectAt(text, size).w <= maxW) return { text, size }; const pad = size.fontSize > 0 ? size.border / size.fontSize : 0; const at = (fontSize) => ({ ...size, fontSize, border: Math.round(fontSize * pad) }); const floor = Math.max(12, Math.ceil(size.fontSize * GAP_MARK_FIT_MIN)); for (let f = size.fontSize - 1; f >= floor; f -= 1) { if (rectAt(text, at(f)).w <= maxW) return { text, size: at(f) }; } const s = at(Math.min(floor, size.fontSize)); if (measure !== monoWidth) { // A proportional face: the longest head that fits with its "…". for (let n = chars.length - 1; n >= 1; n -= 1) { const t = `${chars.slice(0, n).join("").trimEnd()}…`; if (rectAt(t, s).w <= maxW) return { text: t, size: s }; } return { text: "…", size: s }; } const edge = s.border + (s.stroke ?? 0); const n = Math.max(1, Math.floor((maxW - 2 * edge) / (ADVANCE * s.fontSize))); return { text: n >= chars.length ? text : `${chars.slice(0, Math.max(0, n - 1)).join("").trimEnd()}…`, size: s }; } const overlaps = (a, b) => a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h; /** * The corner each mark stands at: `want` (the clip's `corner`; `auto` is the * layout's `prefer`), else -- when something to keep clear of (`avoid`: rects * in the frame, each over `from`–`to` segment seconds, or always) is under * it while the mark is up -- the next free one in tl, tr, bl, br; none free, * `want`'s. A corner in `busy` (a code the layout draws there) is never free. * * @param {{ marks: Array<{ at: number, until: number, text: string, corner?: string }>, box: { x: number, y: number, w: number, h: number }, * want: string, prefer: string, size: ReturnType, * avoid?: Array<{ x: number, y: number, w: number, h: number, from?: number | null, to?: number | null }>, busy?: string[], * measure?: (text: string, fontSize: number) => number }} o * Each place carries the mark's `text` and `size` as fitted to the box * (gapMarkFit): what is drawn, and what the record says. * * @returns {Array<{ corner: string, rect: { x: number, y: number, w: number, h: number }, text: string, size: ReturnType }>} */ export function gapMarkPlaces({ marks, box, want, prefer, size, avoid = [], busy = [], measure = monoWidth }) { return marks.map((m) => { // A mark's own corner (its gap's `corner`) over the clip's. const asked = typeof m.corner === "string" && CORNERS.includes(m.corner) ? m.corner : want; const first = asked === "auto" || !CORNERS.includes(asked) ? prefer : asked; const order = [first, ...CORNERS.filter((c) => c !== first)]; // Fitted to its box (gapMarkFit): never wider than the box less its insets. const fit = gapMarkFit(m.text, size, box.w - 2 * size.inset, measure); const r = gapMarkRect([...fit.text].length, fit.size, measure(fit.text, fit.size.fontSize)); const up = avoid.filter((a) => (a.from ?? -Infinity) < m.until - EPS && (a.to ?? Infinity) > m.at + EPS); for (const c of order) { if (busy.includes(c)) continue; const rect = cornerRect(box, c, r, size.inset); if (!up.some((a) => overlaps(rect, a))) return { corner: c, rect, text: fit.text, size: fit.size }; } return { corner: first, rect: cornerRect(box, first, r, size.inset), text: fit.text, size: fit.size }; }); } /** * A picture's hidden regions (clip-censor.mjs censorRegionsOf: fractions of * the SOURCE picture, source seconds) as rects in the frame to keep the mark * off, over their seconds on the segment's clock: the source picture `sw` × * `sh`, its part `region` (source px; the whole of it when fitted) shown in * `dest` (frame px). `from` / `gaps` / `seconds` as censorParts reads them. */ export function censorAvoidRects(regions, { sw, sh, region = null, dest, from = 0, gaps = [], seconds = Infinity }) { const src = region ?? { x: 0, y: 0, w: sw, h: sh }; return (regions ?? []).map((g) => { const [bx, by, bw, bh] = g.box; const x0 = dest.x + ((bx * sw - src.x) / src.w) * dest.w; const y0 = dest.y + ((by * sh - src.y) / src.h) * dest.h; const x1 = dest.x + (((bx + bw) * sw - src.x) / src.w) * dest.w; const y1 = dest.y + (((by + bh) * sh - src.y) / src.h) * dest.h; return { x: Math.floor(x0), y: Math.floor(y0), w: Math.ceil(x1 - x0), h: Math.ceil(y1 - y0), from: g.from === null || g.from === undefined ? null : Math.max(0, offsetIn(g.from, from, gaps)), to: g.to === null || g.to === undefined ? null : Math.min(seconds, offsetIn(g.to, from, gaps)), }; }); } /** The part of rect `a` inside rect `b` (each `{x, y, w, h}`): a mark's box kept inside a safe area. Empty: `b`. */ export function insideRect(a, b) { const x0 = Math.max(a.x, b.x); const y0 = Math.max(a.y, b.y); const x1 = Math.min(a.x + a.w, b.x + b.w); const y1 = Math.min(a.y + a.h, b.y + b.h); return x1 > x0 && y1 > y0 ? { x: x0, y: y0, w: x1 - x0, h: y1 - y0 } : { ...b }; } const rectOf = (b) => ({ x: b.x, y: b.y, w: b.w ?? b.width, h: b.h ?? b.height }); const safeRect = (a) => ({ x: a.x0, y: a.y0, w: a.x1 - a.x0, h: a.y1 - a.y0 }); /** * Where a vertical clip's mark stands (vertical.mjs clipGeometry / desktopGeometry `geo`, its safe area `safe` * `{x0, y0, x1, y1}`): always inside the safe area -- the platform draws its own buttons and bands outside it. A * framed or quote layout's picture's top left (a framed picture's hook stands above it; a quote has no tag); a * split's picture's top right (its tag is top left, its captions at the picture's foot, the side post under it); * a full-bleed (full, overlay) or desktop clip's safe area's top right (the tag and the hook are at its left). * * @param {{ layout: string, video: { x: number, y: number, w?: number, h?: number, width?: number, height?: number } }} geo * @param {{ x0: number, y0: number, x1: number, y1: number }} safe * @returns {{ box: { x: number, y: number, w: number, h: number }, prefer: "tl" | "tr" }} */ export function verticalMarkBox(geo, safe) { const fullBleed = !["framed", "quote", "split"].includes(geo.layout); return { box: fullBleed ? safeRect(safe) : insideRect(rectOf(geo.video), safeRect(safe)), prefer: fullBleed || geo.layout === "split" ? "tr" : "tl" }; } /** * Where a twocrop clip's mark stands in the stacked deck (vertical-deck.mjs stackedGeometry `g`): crop one's top * right, inside the safe area (the source line and the rail stand above crop one, the strip under it). * * @param {{ safe: { x0: number, y0: number, x1: number, y1: number }, twocrop: { crop1: { x: number, y: number, width: number, height: number } } }} g */ export const twoCropMarkBox = (g) => ({ box: insideRect(rectOf(g.twocrop.crop1), safeRect(g.safe)), prefer: /** @type {const} */ ("tr") }); /** A fitted picture's rect in a box (force_original_aspect_ratio=decrease, centred): `fitted` is its size. */ export const centredIn = (box, fitted) => ({ x: box.x + Math.floor((box.w - fitted.w) / 2), y: box.y + Math.floor((box.h - fitted.h) / 2), w: fitted.w, h: fitted.h });