framesheet 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -2
- package/dist/compose.d.ts +13 -0
- package/dist/compose.d.ts.map +1 -1
- package/dist/compose.js +35 -5
- package/dist/compose.js.map +1 -1
- package/dist/generated/assets.d.ts +4 -0
- package/dist/generated/assets.d.ts.map +1 -1
- package/dist/generated/assets.js +4 -0
- package/dist/generated/assets.js.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/inspect.d.ts +14 -1
- package/dist/inspect.d.ts.map +1 -1
- package/dist/inspect.js +36 -9
- package/dist/inspect.js.map +1 -1
- package/dist/marks.d.ts +49 -0
- package/dist/marks.d.ts.map +1 -0
- package/dist/marks.js +460 -0
- package/dist/marks.js.map +1 -0
- package/dist/marks.schema.json +307 -0
- package/dist/media.d.ts +22 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +306 -0
- package/dist/media.js.map +1 -0
- package/dist/models.d.ts +93 -2
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js.map +1 -1
- package/dist/pixels.d.ts +6 -0
- package/dist/pixels.d.ts.map +1 -1
- package/dist/pixels.js +38 -0
- package/dist/pixels.js.map +1 -1
- package/dist/report.schema.json +576 -4
- package/dist/sources.d.ts +11 -0
- package/dist/sources.d.ts.map +1 -1
- package/dist/sources.js +42 -1
- package/dist/sources.js.map +1 -1
- package/dist/video.d.ts +65 -0
- package/dist/video.d.ts.map +1 -0
- package/dist/video.js +440 -0
- package/dist/video.js.map +1 -0
- package/package.json +2 -1
- package/src/compose.ts +40 -6
- package/src/generated/assets.ts +6 -0
- package/src/index.ts +11 -1
- package/src/inspect.ts +44 -9
- package/src/marks.ts +481 -0
- package/src/media.ts +296 -0
- package/src/models.ts +97 -2
- package/src/pixels.ts +36 -0
- package/src/sources.ts +45 -1
- package/src/video.ts +513 -0
package/src/marks.ts
ADDED
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Marks (`marks` option): validation, which marks are active on a tile, and the pure
|
|
3
|
+
* layout of tile overlays and the timeline strip under a video sheet (port of marks.py).
|
|
4
|
+
*
|
|
5
|
+
* Layout functions return draw ops with integer coordinates and no font measuring (text
|
|
6
|
+
* widths are estimated from the character count), so they match the Python ops exactly
|
|
7
|
+
* (checked against ../schema/golden/layout/marks_layout.json).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { readFileSync } from "node:fs";
|
|
11
|
+
import type { DrawOp } from "./compose.js";
|
|
12
|
+
import type { MarkPoint, MarkRegion, Marks, MarkSpan, Rect } from "./models.js";
|
|
13
|
+
import { fixed, g } from "./pyfmt.js";
|
|
14
|
+
|
|
15
|
+
type RGB = [number, number, number];
|
|
16
|
+
export const CHAR_W = 0.58;
|
|
17
|
+
export const ELLIPSIS = "…";
|
|
18
|
+
const INK: RGB = [17, 24, 39];
|
|
19
|
+
const MUTED: RGB = [75, 85, 99];
|
|
20
|
+
const WHITE: RGB = [255, 255, 255];
|
|
21
|
+
const POINT: RGB = [236, 72, 153];
|
|
22
|
+
const POINT_TEXT: RGB = [190, 24, 93];
|
|
23
|
+
const REGION: RGB = [34, 211, 238];
|
|
24
|
+
const REGION_BAR: RGB = [8, 145, 178];
|
|
25
|
+
const SPAN_BOX: RGB = [167, 139, 250];
|
|
26
|
+
const SPAN_BAR: RGB = [124, 58, 237];
|
|
27
|
+
const STATIC: RGB = [245, 158, 11];
|
|
28
|
+
const CUT: RGB = [220, 38, 38];
|
|
29
|
+
const LANE_BG: RGB = [241, 245, 249];
|
|
30
|
+
const GUIDE: RGB = [203, 213, 225];
|
|
31
|
+
const TICK: RGB = [190, 18, 60];
|
|
32
|
+
const BAR: RGB = [71, 85, 105];
|
|
33
|
+
const BORDER: RGB = [100, 116, 139];
|
|
34
|
+
const PAD = 12;
|
|
35
|
+
const LABEL_COL = 76;
|
|
36
|
+
const ROW_H = 22;
|
|
37
|
+
const MAX_ROWS = 3;
|
|
38
|
+
const NICE_STEPS = [0.1, 0.2, 0.25, 0.5, 1, 2, 5, 10, 15, 30, 60, 120, 300, 600, 1800, 3600];
|
|
39
|
+
|
|
40
|
+
// Validation ------------------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
export class MarksError extends Error {
|
|
43
|
+
constructor(message: string) {
|
|
44
|
+
super(`Invalid marks: ${message}`);
|
|
45
|
+
this.name = "MarksError";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const isObj = (v: unknown): v is Record<string, unknown> => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
50
|
+
const isNum = (v: unknown): v is number => typeof v === "number" && Number.isFinite(v);
|
|
51
|
+
|
|
52
|
+
function keys(where: string, obj: Record<string, unknown>, allowed: string[]): void {
|
|
53
|
+
const extra = Object.keys(obj).filter((k) => !allowed.includes(k));
|
|
54
|
+
if (extra.length) throw new MarksError(`${where}: unknown field(s) ${extra.join(", ")}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function num(where: string, obj: Record<string, unknown>, key: string, opts: { min?: number; gt?: number; optional?: boolean; nullable?: boolean } = {}): number | null {
|
|
58
|
+
const v = obj[key];
|
|
59
|
+
if (v === undefined || (v === null && opts.nullable)) {
|
|
60
|
+
if (opts.optional || opts.nullable) return null;
|
|
61
|
+
throw new MarksError(`${where}.${key} is required`);
|
|
62
|
+
}
|
|
63
|
+
if (!isNum(v)) throw new MarksError(`${where}.${key} must be a number`);
|
|
64
|
+
if (opts.min !== undefined && v < opts.min) throw new MarksError(`${where}.${key} must be >= ${opts.min}`);
|
|
65
|
+
if (opts.gt !== undefined && !(v > opts.gt)) throw new MarksError(`${where}.${key} must be > ${opts.gt}`);
|
|
66
|
+
return v;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function str(where: string, obj: Record<string, unknown>, key: string, required = false): string | null {
|
|
70
|
+
const v = obj[key];
|
|
71
|
+
if (v === undefined || v === null) {
|
|
72
|
+
if (required) throw new MarksError(`${where}.${key} is required`);
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
if (typeof v !== "string") throw new MarksError(`${where}.${key} must be a string`);
|
|
76
|
+
return v;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function box(where: string, obj: Record<string, unknown>) {
|
|
80
|
+
const x = num(where, obj, "x", { nullable: true });
|
|
81
|
+
const y = num(where, obj, "y", { nullable: true });
|
|
82
|
+
const w = num(where, obj, "w", { nullable: true, gt: 0 });
|
|
83
|
+
const h = num(where, obj, "h", { nullable: true, gt: 0 });
|
|
84
|
+
const given = [x, y, w, h].map((v) => v !== null);
|
|
85
|
+
if (given.some(Boolean) && !given.every(Boolean)) throw new MarksError(`${where}: x, y, w, h must be given together (or not at all)`);
|
|
86
|
+
return { x, y, w, h };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function range(where: string, obj: Record<string, unknown>) {
|
|
90
|
+
const start = num(where, obj, "start", { min: 0 })!;
|
|
91
|
+
const end = num(where, obj, "end", { min: 0 })!;
|
|
92
|
+
if (end < start) throw new MarksError(`${where}: end must be >= start`);
|
|
93
|
+
return { start, end };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function list(where: string, value: unknown): Record<string, unknown>[] {
|
|
97
|
+
if (value === undefined) return [];
|
|
98
|
+
if (!Array.isArray(value)) throw new MarksError(`${where} must be a list`);
|
|
99
|
+
return value.map((item, i) => {
|
|
100
|
+
if (!isObj(item)) throw new MarksError(`${where}[${i}] must be an object`);
|
|
101
|
+
return item;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Validate marks (same rules as the Python `Marks` model and ../schema/marks.schema.json). */
|
|
106
|
+
export function parseMarks(value: unknown): Marks {
|
|
107
|
+
if (!isObj(value)) throw new MarksError("must be a JSON object");
|
|
108
|
+
keys("marks", value, ["frame", "points", "regions", "spans"]);
|
|
109
|
+
let frame: Marks["frame"] = null;
|
|
110
|
+
if (value.frame !== undefined && value.frame !== null) {
|
|
111
|
+
if (!isObj(value.frame)) throw new MarksError("frame must be an object");
|
|
112
|
+
keys("frame", value.frame, ["width", "height"]);
|
|
113
|
+
frame = { width: num("frame", value.frame, "width", { gt: 0 })!, height: num("frame", value.frame, "height", { gt: 0 })! };
|
|
114
|
+
}
|
|
115
|
+
const points: MarkPoint[] = list("points", value.points).map((p, i) => {
|
|
116
|
+
const where = `points[${i}]`;
|
|
117
|
+
keys(where, p, ["t", "x", "y", "label", "kind", "duration"]);
|
|
118
|
+
return {
|
|
119
|
+
t: num(where, p, "t", { min: 0 })!,
|
|
120
|
+
x: num(where, p, "x")!,
|
|
121
|
+
y: num(where, p, "y")!,
|
|
122
|
+
label: str(where, p, "label"),
|
|
123
|
+
kind: str(where, p, "kind"),
|
|
124
|
+
duration: num(where, p, "duration", { min: 0, optional: true }) ?? 0,
|
|
125
|
+
};
|
|
126
|
+
});
|
|
127
|
+
const regions: MarkRegion[] = list("regions", value.regions).map((r, i) => {
|
|
128
|
+
const where = `regions[${i}]`;
|
|
129
|
+
keys(where, r, ["start", "end", "label", "kind", "x", "y", "w", "h"]);
|
|
130
|
+
return { ...box(where, r), ...range(where, r), label: str(where, r, "label"), kind: str(where, r, "kind") };
|
|
131
|
+
});
|
|
132
|
+
const spans: MarkSpan[] = list("spans", value.spans).map((s, i) => {
|
|
133
|
+
const where = `spans[${i}]`;
|
|
134
|
+
keys(where, s, ["start", "end", "label", "kind", "x", "y", "w", "h"]);
|
|
135
|
+
return { ...box(where, s), ...range(where, s), label: str(where, s, "label", true)!, kind: str(where, s, "kind") };
|
|
136
|
+
});
|
|
137
|
+
const [fw, fh] = frame ? [frame.width, frame.height] : [1, 1];
|
|
138
|
+
const check = (where: string, x: number, y: number, w: number, h: number) => {
|
|
139
|
+
const tol = 1e-6;
|
|
140
|
+
if (x < -tol || y < -tol || x + w > fw * (1 + tol) + tol || y + h > fh * (1 + tol) + tol) {
|
|
141
|
+
throw new MarksError(`${where}: coordinates outside ${fw !== 1 || fh !== 1 ? "the frame" : "0-1"}`);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
points.forEach((p, i) => check(`points[${i}]`, p.x, p.y, 0, 0));
|
|
145
|
+
regions.forEach((r, i) => r.x !== null && check(`regions[${i}]`, r.x, r.y!, r.w!, r.h!));
|
|
146
|
+
spans.forEach((s, i) => s.x !== null && check(`spans[${i}]`, s.x, s.y!, s.w!, s.h!));
|
|
147
|
+
return { frame, points, regions, spans };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Marks from a path, a JSON string or an object. */
|
|
151
|
+
export function loadMarks(value: string | Marks | Record<string, unknown>): Marks {
|
|
152
|
+
if (typeof value !== "string") return parseMarks(value);
|
|
153
|
+
const text = value.trimStart().startsWith("{") ? value : readFileSync(value, "utf8");
|
|
154
|
+
let parsed: unknown;
|
|
155
|
+
try {
|
|
156
|
+
parsed = JSON.parse(text);
|
|
157
|
+
} catch (err) {
|
|
158
|
+
throw new MarksError(`not valid JSON (${(err as Error).message})`);
|
|
159
|
+
}
|
|
160
|
+
return parseMarks(parsed);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Activity --------------------------------------------------------------------------------
|
|
164
|
+
|
|
165
|
+
const boxOf = (m: { x: number | null; y: number | null; w: number | null; h: number | null }): Rect | null =>
|
|
166
|
+
m.x === null || m.y === null || m.w === null || m.h === null ? null : [m.x, m.y, m.w, m.h];
|
|
167
|
+
|
|
168
|
+
export function norm(marks: Marks, x: number, y: number): [number, number] {
|
|
169
|
+
return marks.frame ? [x / marks.frame.width, y / marks.frame.height] : [x, y];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const chars = (text: string) => Array.from(text);
|
|
173
|
+
|
|
174
|
+
export function estWidth(text: string, size: number): number {
|
|
175
|
+
return Math.ceil(chars(text).length * size * CHAR_W);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Truncate `text` (with an ellipsis) to an estimated width of `maxW` px. */
|
|
179
|
+
export function fit(text: string, size: number, maxW: number): string {
|
|
180
|
+
const n = Math.floor(maxW / (size * CHAR_W));
|
|
181
|
+
const cs = chars(text);
|
|
182
|
+
if (cs.length <= n) return text;
|
|
183
|
+
if (n < 2) return "";
|
|
184
|
+
return cs.slice(0, n - 1).join("") + ELLIPSIS;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Time window each tile stands for: halfway to its neighbours (range ends outside). */
|
|
188
|
+
export function tileWindows(times: number[], start: number, end: number): [number, number][] {
|
|
189
|
+
return times.map((t, i) => [i === 0 ? start : (times[i - 1] + t) / 2, i === times.length - 1 ? end : (t + times[i + 1]) / 2]);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function pointActive(p: MarkPoint, [lo, hi]: [number, number], last: boolean): boolean {
|
|
193
|
+
const a = p.t;
|
|
194
|
+
const b = p.t + p.duration;
|
|
195
|
+
return (last ? a <= hi : a < hi) && b >= lo;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const pointLabel = (p: MarkPoint) => `${p.label || p.kind || "point"} @${fixed(p.t, 2)}s`;
|
|
199
|
+
const regionLabel = (r: MarkRegion) => r.label || r.kind || "region";
|
|
200
|
+
|
|
201
|
+
/** Labels of all marks active on a tile (spans, regions, points). */
|
|
202
|
+
export function activeLabels(marks: Marks, t: number, window: [number, number], last: boolean): string[] {
|
|
203
|
+
return [
|
|
204
|
+
...marks.spans.filter((s) => s.start <= t && t <= s.end).map((s) => s.label),
|
|
205
|
+
...marks.regions.filter((r) => r.start <= t && t <= r.end).map(regionLabel),
|
|
206
|
+
...marks.points.filter((p) => pointActive(p, window, last)).map(pointLabel),
|
|
207
|
+
];
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Second label line of a tile: active spans and box-less regions. */
|
|
211
|
+
export function bandText(marks: Marks, t: number): string {
|
|
212
|
+
return [
|
|
213
|
+
...marks.spans.filter((s) => s.start <= t && t <= s.end).map((s) => s.label),
|
|
214
|
+
...marks.regions.filter((r) => boxOf(r) === null && r.start <= t && t <= r.end).map(regionLabel),
|
|
215
|
+
].join(" | ");
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export function hasBand(marks: Marks): boolean {
|
|
219
|
+
return marks.spans.length > 0 || marks.regions.some((r) => boxOf(r) === null);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Tile overlays ---------------------------------------------------------------------------
|
|
223
|
+
|
|
224
|
+
type Box4 = [number, number, number, number];
|
|
225
|
+
|
|
226
|
+
function clip(box: Box4, w: number, h: number): Box4 | null {
|
|
227
|
+
const c: Box4 = [Math.max(0, box[0]), Math.max(0, box[1]), Math.min(w - 1, box[2]), Math.min(h - 1, box[3])];
|
|
228
|
+
return c[0] <= c[2] && c[1] <= c[3] ? c : null;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function rect(ops: DrawOp[], box: Box4, fill: RGB, w: number, h: number): void {
|
|
232
|
+
const c = clip(box, w, h);
|
|
233
|
+
if (c) ops.push({ op: "rect", box: c, fill });
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function outline(ops: DrawOp[], box: Box4, color: RGB, width: number, w: number, h: number): void {
|
|
237
|
+
const c = clip(box, w, h);
|
|
238
|
+
if (c) ops.push({ op: "outline", box: c, color, width });
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function tag(ops: DrawOp[], text: string, x: number, y: number, fill: RGB, color: RGB, w: number, h: number): void {
|
|
242
|
+
if (!text) return;
|
|
243
|
+
const tw = estWidth(text, 13) + 8;
|
|
244
|
+
x = Math.max(0, Math.min(x, w - tw));
|
|
245
|
+
y = Math.max(0, Math.min(y, h - 18));
|
|
246
|
+
rect(ops, [x, y, x + tw - 1, y + 17], fill, w, h);
|
|
247
|
+
ops.push({ op: "text", x: x + 4, y: y + 2, text, color, size: 13 });
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Overlay ops for one tile image of `size` px; `content` = frame area in the viewport (CSS px). */
|
|
251
|
+
export function tileOps(
|
|
252
|
+
marks: Marks,
|
|
253
|
+
t: number,
|
|
254
|
+
window: [number, number],
|
|
255
|
+
last: boolean,
|
|
256
|
+
opts: { size: [number, number]; viewport: [number, number]; content: Rect },
|
|
257
|
+
): DrawOp[] {
|
|
258
|
+
const [w, h] = opts.size;
|
|
259
|
+
const sx = w / opts.viewport[0];
|
|
260
|
+
const sy = h / opts.viewport[1];
|
|
261
|
+
const [cx, cy, cw, ch] = opts.content;
|
|
262
|
+
const at = (nx: number, ny: number): [number, number] => {
|
|
263
|
+
const [fx, fy] = norm(marks, nx, ny);
|
|
264
|
+
return [Math.floor((cx + fx * cw) * sx), Math.floor((cy + fy * ch) * sy)];
|
|
265
|
+
};
|
|
266
|
+
const ops: DrawOp[] = [];
|
|
267
|
+
const boxed: [Rect, RGB, string][] = [];
|
|
268
|
+
for (const r of marks.regions) {
|
|
269
|
+
const b = boxOf(r);
|
|
270
|
+
if (b && r.start <= t && t <= r.end) boxed.push([b, REGION, regionLabel(r)]);
|
|
271
|
+
}
|
|
272
|
+
for (const s of marks.spans) {
|
|
273
|
+
const b = boxOf(s);
|
|
274
|
+
if (b && s.start <= t && t <= s.end) boxed.push([b, SPAN_BOX, s.label]);
|
|
275
|
+
}
|
|
276
|
+
for (const [[bx, by, bw, bh], color, label] of boxed) {
|
|
277
|
+
const [x0, y0] = at(bx, by);
|
|
278
|
+
let [x1, y1] = at(bx + bw, by + bh);
|
|
279
|
+
x1 = Math.max(x0, x1 - 1);
|
|
280
|
+
y1 = Math.max(y0, y1 - 1);
|
|
281
|
+
outline(ops, [x0 - 1, y0 - 1, x1 + 1, y1 + 1], INK, 1, w, h);
|
|
282
|
+
outline(ops, [x0, y0, x1, y1], color, 2, w, h);
|
|
283
|
+
tag(ops, fit(label, 13, Math.max(60, x1 - x0)), x0, y0 >= 18 ? y0 - 18 : y0 + 2, color, INK, w, h);
|
|
284
|
+
}
|
|
285
|
+
for (const p of marks.points) {
|
|
286
|
+
if (!pointActive(p, window, last)) continue;
|
|
287
|
+
const [x, y] = at(p.x, p.y);
|
|
288
|
+
outline(ops, [x - 7, y - 7, x + 7, y + 7], INK, 1, w, h);
|
|
289
|
+
outline(ops, [x - 6, y - 6, x + 6, y + 6], POINT, 2, w, h);
|
|
290
|
+
const ticks: Box4[] = [
|
|
291
|
+
[x - 12, y - 1, x - 8, y + 1],
|
|
292
|
+
[x + 8, y - 1, x + 12, y + 1],
|
|
293
|
+
[x - 1, y - 12, x + 1, y - 8],
|
|
294
|
+
[x - 1, y + 8, x + 1, y + 12],
|
|
295
|
+
[x - 1, y - 1, x + 1, y + 1],
|
|
296
|
+
];
|
|
297
|
+
for (const b of ticks) rect(ops, b, POINT, w, h);
|
|
298
|
+
const text = fit(pointLabel(p), 13, 220);
|
|
299
|
+
const tw = estWidth(text, 13) + 8;
|
|
300
|
+
const tx = x + 14 + tw <= w ? x + 14 : x - 14 - tw;
|
|
301
|
+
tag(ops, text, tx, y - 9, POINT, WHITE, w, h);
|
|
302
|
+
}
|
|
303
|
+
return ops;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Timeline --------------------------------------------------------------------------------
|
|
307
|
+
|
|
308
|
+
export interface Issue {
|
|
309
|
+
kind: "static" | "black" | "blank" | "cut";
|
|
310
|
+
start: number;
|
|
311
|
+
end: number;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function niceStep(span: number, width: number): number {
|
|
315
|
+
for (const step of NICE_STEPS) if (span / step <= width / 90) return step;
|
|
316
|
+
return NICE_STEPS[NICE_STEPS.length - 1];
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** Greedy row per item extent `[x0, x1]` (items sorted by x0), at most MAX_ROWS. */
|
|
320
|
+
function rows(items: [number, number][]): number[] {
|
|
321
|
+
const ends: number[] = [];
|
|
322
|
+
const out: number[] = [];
|
|
323
|
+
for (const [x0, x1] of items) {
|
|
324
|
+
let row = ends.findIndex((end) => end + 6 <= x0);
|
|
325
|
+
if (row < 0) {
|
|
326
|
+
if (ends.length < MAX_ROWS) {
|
|
327
|
+
row = ends.length;
|
|
328
|
+
ends.push(x1);
|
|
329
|
+
} else {
|
|
330
|
+
row = 0;
|
|
331
|
+
for (let r = 1; r < ends.length; r++) if (ends[r] < ends[row]) row = r;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
ends[row] = Math.max(ends[row], x1);
|
|
335
|
+
out.push(row);
|
|
336
|
+
}
|
|
337
|
+
return out;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** The timeline strip: motion, issues, spans, regions, points, tile axis. Returns `[height, ops]`. */
|
|
341
|
+
export function timelineOps(
|
|
342
|
+
width: number,
|
|
343
|
+
start: number,
|
|
344
|
+
end: number,
|
|
345
|
+
times: number[],
|
|
346
|
+
opts: { marks?: Marks | null; activity?: [number, number][] | null; issues?: Issue[] | null } = {},
|
|
347
|
+
): [number, DrawOp[]] {
|
|
348
|
+
const { marks = null, activity = null, issues = null } = opts;
|
|
349
|
+
const left = PAD + LABEL_COL;
|
|
350
|
+
const right = width - PAD - 1;
|
|
351
|
+
const span = Math.max(end - start, 1e-9);
|
|
352
|
+
const X = (t: number) => left + Math.floor(Math.min(1, Math.max(0, (t - start) / span)) * (right - left));
|
|
353
|
+
const back: DrawOp[] = [];
|
|
354
|
+
const front: DrawOp[] = [];
|
|
355
|
+
const title =
|
|
356
|
+
`timeline ${fixed(start, 2)}-${fixed(end, 2)}s: tiles #1-#${times.length}` +
|
|
357
|
+
(activity !== null ? "; motion = changed pixels per sample" : "");
|
|
358
|
+
front.push({ op: "text", x: PAD, y: 8, text: title, color: INK, size: 16 });
|
|
359
|
+
let y = 34;
|
|
360
|
+
const lanesTop = y;
|
|
361
|
+
const lane = (name: string, height: number): number => {
|
|
362
|
+
const top = y;
|
|
363
|
+
back.push({ op: "rect", box: [left, top, right, top + height - 1], fill: LANE_BG });
|
|
364
|
+
front.push({ op: "text", x: PAD, y: top + Math.floor((height - 13) / 2) - 1, text: name, color: MUTED, size: 13 });
|
|
365
|
+
y += height + 4;
|
|
366
|
+
return top;
|
|
367
|
+
};
|
|
368
|
+
if (activity !== null) {
|
|
369
|
+
const h = 34;
|
|
370
|
+
const top = lane("motion", h);
|
|
371
|
+
for (let k = 1; k < activity.length; k++) {
|
|
372
|
+
const ratio = activity[k][1];
|
|
373
|
+
if (ratio <= 0) continue;
|
|
374
|
+
const bh = Math.max(1, Math.floor(h * Math.min(1, Math.sqrt(ratio / 0.25))));
|
|
375
|
+
const x0 = X(activity[k - 1][0]);
|
|
376
|
+
const x1 = X(activity[k][0]);
|
|
377
|
+
front.push({ op: "rect", box: [x0, top + h - bh, Math.max(x0, x1 - 1), top + h - 1], fill: BAR });
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
if (issues !== null) {
|
|
381
|
+
const h = 20;
|
|
382
|
+
const top = lane("issues", h);
|
|
383
|
+
if (!issues.length) front.push({ op: "text", x: left + 4, y: top + 3, text: "none", color: MUTED, size: 12 });
|
|
384
|
+
for (const issue of issues) {
|
|
385
|
+
const x0 = X(issue.start);
|
|
386
|
+
const x1 = X(issue.end);
|
|
387
|
+
if (issue.kind === "static") {
|
|
388
|
+
front.push({ op: "rect", box: [x0, top + 3, Math.max(x0 + 1, x1), top + 16], fill: STATIC });
|
|
389
|
+
const text = fit(`static ${fixed(issue.end - issue.start, 1)}s`, 12, x1 - x0 - 4);
|
|
390
|
+
if (text) front.push({ op: "text", x: x0 + 3, y: top + 3, text, color: INK, size: 12 });
|
|
391
|
+
} else if (issue.kind === "black" || issue.kind === "blank") {
|
|
392
|
+
const box: Box4 = [x0, top + 2, Math.max(x1, x0 + 3), top + h - 3];
|
|
393
|
+
front.push({ op: "rect", box, fill: issue.kind === "black" ? [0, 0, 0] : WHITE });
|
|
394
|
+
if (issue.kind === "blank") front.push({ op: "outline", box, color: BORDER, width: 1 });
|
|
395
|
+
} else if (issue.kind === "cut") {
|
|
396
|
+
front.push({ op: "rect", box: [x0 - 1, top, x0, top + h - 1], fill: CUT });
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
if (marks !== null) {
|
|
401
|
+
const bars: [string, [number, number, string][], RGB][] = [
|
|
402
|
+
["spans", marks.spans.map((s) => [s.start, s.end, s.label]), SPAN_BAR],
|
|
403
|
+
["regions", marks.regions.map((r) => [r.start, r.end, regionLabel(r)]), REGION_BAR],
|
|
404
|
+
];
|
|
405
|
+
for (const [name, raw, color] of bars) {
|
|
406
|
+
if (!raw.length) continue;
|
|
407
|
+
// Stable sort by (start, end), like Python's sorted().
|
|
408
|
+
const items = raw.map((it, i) => [it, i] as const).sort((a, b) => a[0][0] - b[0][0] || a[0][1] - b[0][1] || a[1] - b[1]).map(([it]) => it);
|
|
409
|
+
const geo = items.map(([s, e, label]) => {
|
|
410
|
+
const x0 = X(s);
|
|
411
|
+
const x1 = Math.max(X(e), X(s) + 2);
|
|
412
|
+
const inside = fit(label, 13, x1 - x0 - 8);
|
|
413
|
+
const outside = chars(inside).length < Math.min(4, chars(label).length);
|
|
414
|
+
const text = outside ? fit(label, 13, 200) : inside;
|
|
415
|
+
const extent = outside ? x1 + 4 + estWidth(text, 13) : x1;
|
|
416
|
+
return { x0, x1, text, outside, extent };
|
|
417
|
+
});
|
|
418
|
+
const rs = rows(geo.map((gm) => [gm.x0, gm.extent]));
|
|
419
|
+
const top = lane(name, (Math.max(...rs) + 1) * ROW_H);
|
|
420
|
+
geo.forEach((gm, i) => {
|
|
421
|
+
const ry = top + rs[i] * ROW_H;
|
|
422
|
+
front.push({ op: "rect", box: [gm.x0, ry + 2, gm.x1, ry + 19], fill: color });
|
|
423
|
+
if (gm.text) {
|
|
424
|
+
front.push({
|
|
425
|
+
op: "text",
|
|
426
|
+
x: gm.outside ? gm.x1 + 4 : gm.x0 + 4,
|
|
427
|
+
y: ry + 4,
|
|
428
|
+
text: gm.text,
|
|
429
|
+
color: gm.outside ? color : WHITE,
|
|
430
|
+
size: 13,
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
if (marks.points.length) {
|
|
436
|
+
const pts = marks.points.map((p, i) => [p, i] as const).sort((a, b) => a[0].t - b[0].t || a[1] - b[1]).map(([p]) => p);
|
|
437
|
+
const geo = pts.map((p) => ({ x: X(p.t), text: fit(p.label || p.kind || "", 13, 200), xEnd: X(p.t + p.duration) }));
|
|
438
|
+
const rs = rows(geo.map((gm) => [gm.x - 2, Math.max(gm.xEnd, gm.x + 5 + estWidth(gm.text, 13))]));
|
|
439
|
+
const top = lane("points", (Math.max(...rs) + 1) * ROW_H);
|
|
440
|
+
geo.forEach((gm, i) => {
|
|
441
|
+
const ry = top + rs[i] * ROW_H;
|
|
442
|
+
if (gm.xEnd > gm.x) front.push({ op: "rect", box: [gm.x, ry + 9, gm.xEnd, ry + 12], fill: POINT });
|
|
443
|
+
front.push({ op: "rect", box: [gm.x - 1, ry + 2, gm.x + 1, ry + 19], fill: POINT });
|
|
444
|
+
if (gm.text) front.push({ op: "text", x: gm.x + 5, y: ry + 4, text: gm.text, color: POINT_TEXT, size: 13 });
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
const lanesBottom = y - 4;
|
|
449
|
+
const axis = y + 2;
|
|
450
|
+
front.push({ op: "text", x: PAD, y: axis + 4, text: "tiles", color: MUTED, size: 13 });
|
|
451
|
+
front.push({ op: "rect", box: [left, axis + 14, right, axis + 14], fill: BORDER });
|
|
452
|
+
let lastLabel = -(10 ** 9);
|
|
453
|
+
times.forEach((t, i) => {
|
|
454
|
+
const x = X(t);
|
|
455
|
+
if (lanesBottom > lanesTop) back.push({ op: "rect", box: [x, lanesTop, x, lanesBottom], fill: GUIDE });
|
|
456
|
+
front.push({ op: "rect", box: [x - 1, axis + 4, x, axis + 14], fill: TICK });
|
|
457
|
+
const text = `#${i + 1}`;
|
|
458
|
+
const tw = estWidth(text, 12);
|
|
459
|
+
const tx = x + 2 + tw <= right ? x + 2 : x - 3 - tw; // the end tile: left of its tick
|
|
460
|
+
if (tx >= lastLabel + 4) {
|
|
461
|
+
front.push({ op: "text", x: tx, y: axis, text, color: TICK, size: 12 });
|
|
462
|
+
lastLabel = tx + tw;
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
const step = niceStep(span, right - left);
|
|
466
|
+
let k = Math.ceil(start / step - 1e-9) || 0; // no -0 (Python ints)
|
|
467
|
+
lastLabel = -(10 ** 9);
|
|
468
|
+
while (k * step <= end + 1e-9) {
|
|
469
|
+
const v = k * step;
|
|
470
|
+
const x = X(v);
|
|
471
|
+
front.push({ op: "rect", box: [x, axis + 15, x, axis + 19], fill: BORDER });
|
|
472
|
+
const text = `${g(v)}s`;
|
|
473
|
+
const tw = estWidth(text, 12);
|
|
474
|
+
if (x + 2 >= lastLabel + 6 && x + 2 + tw <= width - PAD) {
|
|
475
|
+
front.push({ op: "text", x: x + 2, y: axis + 19, text, color: MUTED, size: 12 });
|
|
476
|
+
lastLabel = x + 2 + tw;
|
|
477
|
+
}
|
|
478
|
+
k++;
|
|
479
|
+
}
|
|
480
|
+
return [axis + 36 + PAD, [...back, ...front]];
|
|
481
|
+
}
|