// THE GAP CUE (lane gap-cue, slice 1): a small mark at the join a clip's gap // leaves -- "» 27 s" -- so a viewer can tell that time was skipped, and // roughly how much, without the mark pulling the eye from the footage. // // This module is the RULE, once, for every reader: the build draws the mark // into the clip's segment (umtool report-to-video gap-mark.mjs), and umtool's // pages (Play the cut, the clip bench) draw the same mark at the same seconds // from the same functions. It holds the settings and their precedence, the // label, and the timing; nothing here knows about ffmpeg or the DOM. // // THE SETTINGS, most specific wins: // // render.gapCue { mark?, min?, corner?, label?, place?, border? } the project's (a look key: a preset, // a style or a take patch carries it) // clip.gapCue { mark?, min?, corner?, label?, place?, border? } one clip's, over the project's // a gap's `mark` true | false one gap's, over both // // mark draw the mark at a join (default false: OFF, so a cut that says nothing renders as it always did) // min a gap shorter than this draws nothing (seconds, default 2: a breath taken out is not worth a mark) // corner auto | tl | tr | bl | br (default auto: the layout's own free corner) // label the text: `{s}` the length skipped, `{why}` the gap's reason, and a part in [ ] drawn only when // the gap has one (default "» {s}[ · {why}]": "» 27 s", or "» 26 s · dead air"; from a minute "» 1:12") // place footage | deck (default footage: over the footage at its corner; deck: in the deck's band, at the // end of the source line, left of the QR -- only where a deck is drawn: elsewhere footage) // border accent | #rrggbb (default accent: the border in the deck's accent, `render.palette.accent`, the colour // the deck draws its rule and dots in; a render with none, GAP_CUE_BORDER_FALLBACK) // // A gap may also carry `why` ("dead-air" | "idle" | "other"): what it was cut // for, and `reason`, a few words of its own (at most GAP_REASON_MAX). The mark // says the reason (operator, 20 Nov: "add the reason to the mark"): `reason` // when it is set, else why's word (GAP_WHY_WORDS: "dead air", "idle"; "other" // says nothing). A fast-forward default (slice 3) will reach only the gaps // marked dead air, so a gap cut to HIDE what is in it never plays. A gap's // `corner` stands its mark in that corner, over its clip's (a stream's layout // can change inside one clip). // // THE LOOK (GAP_CUE_LOOK) is here too, so the build's drawtext (gap-mark.mjs), // the gap-cue module's page (chrome-gap-cue.mjs, gapCueCss) and umtool's // element over a raw picture (GapMarkChip, gapCueBoxStyle) draw one mark -- // in the colours gapCueLook gives each clip's (its border: gapCueBorderOf). // // WHO DRAWS IT (lane gap-cue, slice 1d): wherever a deck is drawn, the gap-cue // MODULE (a HyperFrames page per mark, at its own seconds); the drawtext into // the clip's segment only where no chrome is laid over the picture. // // THE TIMING, on the segment's clock (seconds from its first frame): the mark // comes up over GAP_CUE_MOTION.in at the join, holds, and goes over // GAP_CUE_MOTION.out -- GAP_CUE_LIFE in all. Joins closer together than that // are ONE mark: it comes up at the first, says the seconds of all of them, and // goes a life after the last. It never outlives the segment. // // PURE and dependency-free: the client bundle takes it as it is. const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const fin = (v) => typeof v === "number" && Number.isFinite(v); const EPS = 1e-6; /** The settings a cut that says nothing has: the mark off. */ export const GAP_CUE_DEFAULTS = Object.freeze({ mark: false, min: 2, corner: "auto", label: "» {s}[ · {why}]", place: "footage", border: "accent" }); /** The keys `render.gapCue` and a clip's `gapCue` take. */ export const GAP_CUE_KEYS = Object.freeze(["mark", "min", "corner", "label", "place", "border"]); /** `border`'s word for the deck's accent (`render.palette.accent`): the default. */ export const GAP_CUE_BORDER_ACCENT = "accent"; /** The border's colour when the render has no palette accent: the amber the operator picked (variant B, 20 Nov). */ export const GAP_CUE_BORDER_FALLBACK = "#f5b300"; /** A border colour a setting may name: `#rrggbb`. */ const BORDER_HEX_RE = /^#[0-9a-fA-F]{6}$/; /** A palette's colour as the deck reads it (`#rgb` or `#rrggbb`: chrome-deck.mjs HEX_RE). */ const PALETTE_HEX_RE = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/; /** Is `v` a `border` setting: "accent" or `#rrggbb`. */ const borderOk = (v) => v === GAP_CUE_BORDER_ACCENT || (typeof v === "string" && BORDER_HEX_RE.test(v)); /** Where the mark is drawn: over the footage (at its corner), or in the deck's band (the end of the source line). */ export const GAP_CUE_PLACES = Object.freeze(["footage", "deck"]); /** Where the mark may stand: the layout's choice, or a corner of its box. */ export const GAP_CUE_CORNERS = Object.freeze(["auto", "tl", "tr", "bl", "br"]); /** `min`'s range, in seconds. */ export const GAP_CUE_MIN = Object.freeze({ min: 0.5, max: 60 }); /** A label's longest, in characters. */ export const GAP_CUE_LABEL_MAX = 40; /** What a gap was cut for (`why`). */ export const GAP_WHY = Object.freeze(["dead-air", "idle", "other"]); /** What the mark says for each `why` ("other" says nothing: a gap's `reason` says it). */ export const GAP_WHY_WORDS = Object.freeze({ "dead-air": "dead air", idle: "idle", other: "" }); /** A gap's own `reason`'s longest, in characters (one line). */ export const GAP_REASON_MAX = 24; /** The corners a gap's own `corner` may name (its mark stands there, over its clip's). */ export const GAP_OWN_CORNERS = Object.freeze(["tl", "tr", "bl", "br"]); /** * The characters a label or a reason may not hold: every control character (a tab, a newline, NUL…), DEL and the * line and paragraph separators -- the mark is ONE line, drawn as written (gap-mark.mjs: drawtext, expansion=none). */ const CONTROL = /[\u0000-\u001f\u007f\u2028\u2029]/; /** Is `v` a gap's reason: one line of words, 1 to GAP_REASON_MAX characters once trimmed, no control character and no `[ ] { }`. */ export const gapReasonOk = (v) => typeof v === "string" && v.trim().length > 0 && v.trim().length <= GAP_REASON_MAX && !CONTROL.test(v) && !/[[\]{}]/.test(v); /** The sentence for a reason that is not one. */ export const GAP_REASON_RULE = `a few words, at most ${GAP_REASON_MAX} characters on one line (no [ ] { }, no tab or other control character)`; /** Is `v` a label's text, its length and characters (its {s} and [ ] parts aside). */ const labelLineOk = (v) => v.length <= GAP_CUE_LABEL_MAX && !CONTROL.test(v); /** * THE LOOK, once (operator, 20 Nov: "more visible and readable with a border"; then "Go with variant B", the * accent border): sizes as fractions of the frame's short side -- `font` the text (0.03: 32 px at 1080), `inset` * from the box it stands in, `stroke` the border's width (at least `strokeMin` px) -- `pad` the box round the text * in ems; the text `fg` on `box` at `boxAlpha`, inside a `strokeColor` border at `strokeAlpha`. `strokeColor` here * is the fallback: a mark is drawn in gapCueLook's, the deck's accent unless its `border` says. The whole mark * fades together (GAP_CUE_MOTION). */ export const GAP_CUE_LOOK = Object.freeze({ font: 0.03, pad: 0.38, inset: 0.026, stroke: 0.0019, strokeMin: 2, fg: "#ffffff", box: "#101010", boxAlpha: 0.9, strokeColor: GAP_CUE_BORDER_FALLBACK, strokeAlpha: 1, }); /** The mark's motion (seconds) and its opacity at full. */ export const GAP_CUE_MOTION = Object.freeze({ in: 0.15, hold: 0.6, out: 0.35, peak: 0.9 }); /** How long one mark is up: in, hold and out. */ export const GAP_CUE_LIFE = GAP_CUE_MOTION.in + GAP_CUE_MOTION.hold + GAP_CUE_MOTION.out; /** `#rgb` as `#rrggbb` (a palette may say either; drawtext reads only the long form). */ const longHex = (hex) => (hex.length === 4 ? `#${[...hex.slice(1)].map((c) => c + c).join("")}` : hex); /** * THE BORDER'S COLOUR one clip's mark is drawn in (`#rrggbb`): its `border` setting (the clip's over the project's, * clipGapCueOf) when it names a colour; else -- "accent", the default, or absent -- the deck's accent, the render's * `palette.accent` (as the deck reads it: chrome-deck.mjs's rule and dots, the brand's when one is resolved); a * render with no palette accent, GAP_CUE_BORDER_FALLBACK. The one resolver: the module's page (through the slice's * marks), the drawtext and umtool's element all take it from here. * * @param {Record | null | undefined} render * @param {Record | null | undefined} [entry] * @returns {string} */ export function gapCueBorderOf(render, entry = null) { const b = clipGapCueOf(render, entry).border; if (BORDER_HEX_RE.test(b)) return b; const accent = render?.palette?.accent; return typeof accent === "string" && PALETTE_HEX_RE.test(accent) ? longHex(accent) : GAP_CUE_BORDER_FALLBACK; } /** * THE LOOK one clip's mark is drawn in: GAP_CUE_LOOK with its border's colour (gapCueBorderOf) -- or, with * `border` given (a colour the slice already resolved onto a mark), that colour. * * @param {Record | null | undefined} render * @param {Record | null | undefined} [entry] * @param {{ border?: string | null }} [o] */ export function gapCueLook(render, entry = null, { border = null } = {}) { const c = typeof border === "string" && PALETTE_HEX_RE.test(border) ? longHex(border) : gapCueBorderOf(render, entry); return Object.freeze({ ...GAP_CUE_LOOK, strokeColor: c }); } /** `#rrggbb` at `a`, for CSS. */ const rgbaOf = (hex, a) => { const n = Number.parseInt(String(hex).replace(/^#/, ""), 16); return `rgba(${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255}, ${a})`; }; /** * The mark's box as CSS values, from THE LOOK (`look` over GAP_CUE_LOOK): its font size `font` (px, or any CSS * length), the box round the text (`pad` em), the border (`stroke`: px, or a CSS length), the text's colour, and * the background -- the box over the border's colour inside the border, as the drawtext lays its two boxes * (gap-mark.mjs gapMarkFilters), so the inside reads the same. `lineHeight` the text's line box. What the module's * page (gapCueCss) and umtool's element over a raw picture (GapMarkChip) both draw: one look. * * @param {{ font: number | string, stroke: number | string, lineHeight?: number, look?: Partial }} o */ export function gapCueBoxStyle({ font, stroke, lineHeight = 1.2, look = GAP_CUE_LOOK }) { const L = { ...GAP_CUE_LOOK, ...look }; const len = (v) => (typeof v === "number" ? `${v}px` : String(v)); const inner = rgbaOf(L.box, L.boxAlpha); const edge = rgbaOf(L.strokeColor, L.strokeAlpha); return { fontSize: len(font), lineHeight: String(lineHeight), padding: `${L.pad}em`, color: L.fg, border: `${len(stroke)} solid ${edge}`, background: `linear-gradient(${inner}, ${inner}), ${edge}`, backgroundClip: "padding-box", whiteSpace: "nowrap", }; } /** * gapCueBoxStyle as a CSS declaration block (`selector { … }`), for a page: the module's (chrome-gap-cue.mjs). * `family` the face (the deck's own: DeckSansBold). * * @param {string} selector * @param {Parameters[0] & { family?: string }} o */ export function gapCueCss(selector, { family = "'DeckSansBold', sans-serif", ...o }) { const s = gapCueBoxStyle(o); const kebab = (k) => k.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`); const decls = Object.entries(s).map(([k, v]) => `${kebab(k)}: ${v};`).join(" "); return `${selector} { position: absolute; box-sizing: border-box; font-family: ${family}; ${decls} overflow: hidden; text-overflow: ellipsis; }`; } const q = (v) => JSON.stringify(v); /** * Every reason a `gapCue` block cannot be built ([] when it can; absent is fine). * * @param {unknown} v * @param {string} [where] * @returns {string[]} */ export function gapCueErrors(v, where = "render.gapCue") { if (v === undefined || v === null) return []; if (!isObj(v)) return [`${where} must be { mark?, min?, corner?, label?, place?, border? }`]; const errors = []; for (const k of Object.keys(v)) if (!GAP_CUE_KEYS.includes(k)) errors.push(`${where}.${k} is not a key (${GAP_CUE_KEYS.join(", ")})`); if (v.mark !== undefined && typeof v.mark !== "boolean") errors.push(`${where}.mark is true (draw the mark at a join) or false, not ${q(v.mark)}`); if (v.min !== undefined && !(fin(v.min) && v.min >= GAP_CUE_MIN.min && v.min <= GAP_CUE_MIN.max)) { errors.push(`${where}.min is seconds from ${GAP_CUE_MIN.min} to ${GAP_CUE_MIN.max} (a shorter gap draws nothing), not ${q(v.min)}`); } if (v.corner !== undefined && !GAP_CUE_CORNERS.includes(/** @type {string} */ (v.corner))) errors.push(`${where}.corner is one of ${GAP_CUE_CORNERS.join(", ")}, not ${q(v.corner)}`); if (v.place !== undefined && !GAP_CUE_PLACES.includes(/** @type {string} */ (v.place))) { errors.push(`${where}.place is footage (over the footage, at its corner) or deck (in the deck's band, at the end of the source line), not ${q(v.place)}`); } if (v.border !== undefined && !borderOk(v.border)) { errors.push(`${where}.border is accent (the deck's accent colour) or a #rrggbb colour, not ${q(v.border)}`); } if (v.label !== undefined) { if (typeof v.label !== "string" || !v.label.includes("{s}")) errors.push(`${where}.label is text holding {s} (the seconds skipped), e.g. "» {s}" or "{s} later"`); else if (!labelLineOk(v.label)) errors.push(`${where}.label is one line of at most ${GAP_CUE_LABEL_MAX} characters (no tab or other control character)`); else if (!labelPartsOk(v.label)) errors.push(`${where}.label: a part in [ ] holds {why} and is drawn only when the gap has a reason, e.g. "» {s}[ · {why}]" (no [ ] inside another)`); } return errors; } /** * Every reason a manifest's gap cue settings cannot be built: `render.gapCue` * and each clip's `gapCue` (a gap's own `mark` and `why` are gaps.mjs's). * * @param {{ render?: unknown, timeline?: unknown }} manifest * @returns {string[]} */ export function validateGapCue(manifest) { const render = isObj(manifest?.render) ? manifest.render : {}; const errors = gapCueErrors(render.gapCue, "render.gapCue"); const timeline = Array.isArray(manifest?.timeline) ? manifest.timeline : []; timeline.forEach((e, i) => { if (!isObj(e) || e.gapCue === undefined) return; const where = `timeline[${i}] (${e.id ?? "?"}).gapCue`; if (e.type !== "clip") errors.push(`${where}: only a clip has gaps to mark`); else errors.push(...gapCueErrors(e.gapCue, where)); }); return errors; } /** Are a label's [ ] parts well formed: not nested, each closed, each holding {why}. */ function labelPartsOk(label) { let open = -1; for (let i = 0; i < label.length; i += 1) { if (label[i] === "[") { if (open >= 0) return false; open = i; } else if (label[i] === "]") { if (open < 0 || !label.slice(open, i).includes("{why}")) return false; open = -1; } } return open < 0; } /** The well-formed keys of a `gapCue` block (anything else dropped). */ function own(v) { if (!isObj(v)) return {}; const out = {}; if (typeof v.mark === "boolean") out.mark = v.mark; if (fin(v.min) && v.min >= GAP_CUE_MIN.min && v.min <= GAP_CUE_MIN.max) out.min = v.min; if (GAP_CUE_CORNERS.includes(v.corner)) out.corner = v.corner; if (GAP_CUE_PLACES.includes(v.place)) out.place = v.place; if (borderOk(v.border)) out.border = v.border; if (typeof v.label === "string" && v.label.includes("{s}") && labelLineOk(v.label) && labelPartsOk(v.label)) out.label = v.label; return out; } /** * The project's settings, the defaults filled in. * * @param {Record | null | undefined} render * @returns {{ mark: boolean, min: number, corner: string, label: string, place: string, border: string }} */ export const gapCueOf = (render) => ({ ...GAP_CUE_DEFAULTS, ...own(render?.gapCue) }); /** * One clip's settings: its own `gapCue` over the project's. * * @param {Record | null | undefined} render * @param {Record | null | undefined} entry */ export const clipGapCueOf = (render, entry) => ({ ...gapCueOf(render), ...own(entry?.gapCue) }); /** * Is one gap marked: its own `mark` when it says, else its clip's (`cue`, clipGapCueOf). * * @param {{ mark: boolean }} cue * @param {{ mark?: unknown } | null | undefined} gap */ export const gapMarkOn = (cue, gap) => (typeof gap?.mark === "boolean" ? gap.mark : cue.mark); /** Is the mark on anywhere in this clip: for the clip, or for one of its gaps? */ export function gapCueWanted(render, entry) { const cue = clipGapCueOf(render, entry); const gaps = Array.isArray(entry?.gaps) ? entry.gaps : []; return gaps.some((g) => gapMarkOn(cue, g)); } /** * The length skipped, as the mark says it: whole seconds under a minute * ("27 s"), then m:ss ("1:12"), then h:mm:ss ("1:02:03"). * * @param {number} seconds */ export function gapCueLength(seconds) { const r = Math.max(0, Math.round(Number(seconds) || 0)); if (r < 60) return `${r} s`; const p2 = (n) => String(n).padStart(2, "0"); const h = Math.floor(r / 3600); const m = Math.floor((r % 3600) / 60); const s = r % 60; return h ? `${h}:${p2(m)}:${p2(s)}` : `${m}:${p2(s)}`; } /** * What a gap's mark says it was cut for: its own `reason` when set, else its * `why`'s word (GAP_WHY_WORDS), else "". * * @param {{ why?: unknown, reason?: unknown } | null | undefined} gap */ export function gapWhyWord(gap) { const r = typeof gap?.reason === "string" ? gap.reason.trim() : ""; if (r) return r; return typeof gap?.why === "string" ? (GAP_WHY_WORDS[/** @type {keyof typeof GAP_WHY_WORDS} */ (gap.why)] ?? "") : ""; } /** * What a join's own gaps (`own`: the clip's gaps it is -- one, or two the * frame grid joined) say beyond their seconds: their reason when they all * give the same (gapWhyWord), and the first corner one names. Only the keys * they have. * * @param {Array<{ why?: unknown, reason?: unknown, corner?: unknown }>} own * @returns {{ why?: string, corner?: string }} */ export function gapOwnSays(own) { const words = (own ?? []).map((c) => gapWhyWord(c)); const why = words.length && words.every((w) => w && w === words[0]) ? words[0] : ""; const corner = (own ?? []).map((c) => c?.corner).find((c) => typeof c === "string" && GAP_OWN_CORNERS.includes(/** @type {any} */ (c))); return { ...(why ? { why } : {}), ...(typeof corner === "string" ? { corner } : {}) }; } /** * The mark's text: `label` with `{s}` the length skipped and `{why}` the * reason (gapWhyWord); a part in [ ] only when there is a reason. * * @param {number} seconds * @param {string} [label] * @param {string} [why] */ export function gapCueText(seconds, label = GAP_CUE_DEFAULTS.label, why = "") { const w = String(why ?? "").trim(); return String(label) .replace(/\[([^\]]*)\]/g, (_, part) => (w ? part : "")) .split("{why}").join(w) .split("{s}").join(gapCueLength(seconds)); } /** * The marks a segment draws, from its joins. * * `joins`: each `{ at, seconds, mark, play?, why?, corner? }` -- `at` the * join on the segment's clock, `seconds` the source seconds the gap left * out, `mark` whether this gap is marked (gapMarkOn), `play` the seconds the * gap itself plays (0, a skip; slice 3's fast-forward), `why` what its mark * says it was cut for (gapWhyWord), `corner` its own corner. A gap unmarked, * or under `min`, draws nothing and joins no mark. Marked joins less than * `life` apart are one mark, the seconds of each in it -- its reason only * when they all say the same, its corner the first that names one. A mark never outlives * the segment (`seconds`), and one with under a fifth of a second left is * not drawn. `why` and `corner` are on a mark only when it has them. * * @param {Array<{ at: number, seconds: number, mark: boolean, play?: number, why?: string, corner?: string | null }>} joins * @param {{ min?: number, seconds?: number, life?: number }} [opts] * @returns {Array<{ at: number, until: number, seconds: number, count: number, why?: string, corner?: string }>} */ export function gapCues(joins, { min = GAP_CUE_DEFAULTS.min, seconds = Infinity, life = GAP_CUE_LIFE } = {}) { const marked = (joins ?? []) .filter((j) => j && j.mark === true && fin(j.at) && fin(j.seconds) && j.seconds >= min - EPS) .sort((a, b) => a.at - b.at); const out = []; let open = null; for (const j of marked) { const end = j.at + (fin(j.play) && j.play > 0 ? j.play : 0) + life; const why = typeof j.why === "string" ? j.why : ""; if (open && j.at < open.until - EPS) { open.until = Math.max(open.until, end); open.seconds += j.seconds; open.count += 1; if (open.why !== why) open.why = ""; // Its corner the first of its joins' that names one (gapOwnSays's rule for one join's own gaps). if (!open.corner && typeof j.corner === "string" && j.corner) open.corner = j.corner; continue; } open = { at: j.at, until: end, seconds: j.seconds, count: 1, why, corner: typeof j.corner === "string" ? j.corner : "" }; out.push(open); } const r4 = (v) => Math.round(v * 10000) / 10000; return out .map((c) => ({ at: r4(Math.max(0, c.at)), until: r4(Math.min(seconds, c.until)), seconds: r4(c.seconds), count: c.count, ...(c.why ? { why: c.why } : {}), ...(c.corner ? { corner: c.corner } : {}), })) .filter((c) => c.until - c.at >= 0.2 - EPS); } /** * The mark's opacity at `t` (the segment's clock): up over `in`, down over * `out`, GAP_CUE_MOTION.peak between; 0 outside it. The build's alpha * expression is this, written for ffmpeg (gap-mark.mjs). * * @param {number} t * @param {{ at: number, until: number }} cue */ export function gapCueAlpha(t, cue) { if (!(t >= cue.at && t <= cue.until)) return 0; const k = Math.min((t - cue.at) / GAP_CUE_MOTION.in, (cue.until - t) / GAP_CUE_MOTION.out); return GAP_CUE_MOTION.peak * Math.max(0, Math.min(1, k)); } /** * A patch of a `gapCue` block applied (the look's writer and a clip's): each * key merged over what it holds, a key null drops it, nothing left is no * block (null). `patch` null drops the whole block. * * @param {unknown} current * @param {unknown} patch * @returns {Record | null} */ export function patchGapCue(current, patch) { if (patch === null) return null; if (!isObj(patch)) return /** @type {any} */ (patch); const next = { ...(isObj(current) ? current : {}) }; for (const [k, v] of Object.entries(patch)) { if (v === null) delete next[k]; else next[k] = v; } return Object.keys(next).length ? next : null; }