@open-take/compositor 0.4.3 → 0.5.0

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.
@@ -0,0 +1,374 @@
1
+ import { statSync } from "node:fs";
2
+ import { resolve } from "node:path";
3
+ import { validateLaunchStory } from "./launch-story";
4
+ import type { LaunchIssue, LaunchScene } from "./launch-types";
5
+ import { validateMotionScene } from "./motion-validate";
6
+
7
+ const record = (value: unknown): value is Record<string, unknown> =>
8
+ value !== null && typeof value === "object" && !Array.isArray(value);
9
+ const number = (value: unknown): value is number =>
10
+ typeof value === "number" && Number.isFinite(value);
11
+ const issue = (path: string, message: string): LaunchIssue => ({
12
+ severity: "error",
13
+ path,
14
+ message,
15
+ });
16
+ const colorKeys = ["canvas", "ink", "surface", "accent", "dark"];
17
+ const validColor = (value: unknown): boolean => {
18
+ if (typeof value !== "string") return false;
19
+ if (/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(value)) return true;
20
+ const match = value.match(
21
+ /^(rgb|rgba)\(\s*(\d+(?:\.\d+)?|\.\d+)\s*,\s*(\d+(?:\.\d+)?|\.\d+)\s*,\s*(\d+(?:\.\d+)?|\.\d+)(?:\s*,\s*(\d+(?:\.\d+)?|\.\d+))?\s*\)$/i,
22
+ );
23
+ return (
24
+ !!match &&
25
+ (match[1]!.toLowerCase() === "rgba") === (match[5] !== undefined) &&
26
+ match.slice(2, 5).every((x) => Number(x) <= 255) &&
27
+ (match[5] === undefined || Number(match[5]) <= 1)
28
+ );
29
+ };
30
+
31
+ /** Validate untrusted JSON before any timing/layout evaluator can access it. */
32
+ export function validateLaunchComposition(value: unknown, baseDir?: string): LaunchIssue[] {
33
+ const out: LaunchIssue[] = [];
34
+ if (!record(value)) return [issue("$", "must be a JSON object")];
35
+ const add = (path: string, message: string) => out.push(issue(path, message));
36
+ const bounded = (
37
+ v: unknown,
38
+ p: string,
39
+ min: number,
40
+ max = Number.MAX_SAFE_INTEGER,
41
+ ): v is number => {
42
+ if (!number(v) || v < min || v > max) {
43
+ add(p, `must be finite and between ${min} and ${max}`);
44
+ return false;
45
+ }
46
+ return true;
47
+ };
48
+ const text = (v: unknown, p: string, max = 200, empty = false): v is string => {
49
+ if (typeof v !== "string" || (!empty && !v.trim())) {
50
+ add(p, "must be a non-empty string");
51
+ return false;
52
+ }
53
+ if (v.length > max) add(p, `exceeds the readable ${max}-character bound`);
54
+ return true;
55
+ };
56
+ const optionalText = (obj: Record<string, unknown>, key: string, p: string, max: number) => {
57
+ if (obj[key] !== undefined) text(obj[key], `${p}.${key}`, max);
58
+ };
59
+ const enumValue = (v: unknown, p: string, choices: string[]) => {
60
+ if (typeof v !== "string" || !choices.includes(v)) add(p, `must be ${choices.join(", ")}`);
61
+ };
62
+ const theme = (v: unknown, p: string, partial: boolean) => {
63
+ if (!record(v)) {
64
+ add(p, "must be an object");
65
+ return;
66
+ }
67
+ for (const key of colorKeys)
68
+ if ((!partial || v[key] !== undefined) && !validColor(v[key]))
69
+ add(`${p}.${key}`, "must be a valid #RRGGBB/#RRGGBBAA/rgb()/rgba() color");
70
+ if (!partial || v.fontFamily !== undefined) text(v.fontFamily, `${p}.fontFamily`, 300);
71
+ };
72
+ const asset = (v: unknown, p: string) => {
73
+ if (!text(v, p, 4096)) return;
74
+ if (v.includes("\0")) {
75
+ add(p, "must be a local path without NUL characters");
76
+ return;
77
+ }
78
+ if (baseDir) {
79
+ try {
80
+ if (!statSync(resolve(baseDir, v)).isFile()) add(p, `asset is not a regular file: ${v}`);
81
+ } catch {
82
+ add(p, `missing asset: ${v}`);
83
+ }
84
+ }
85
+ };
86
+ if (value.version !== 1) add("version", "must be the supported launch composition version 1");
87
+ const output = record(value.output) ? value.output : {};
88
+ for (const key of ["width", "height"])
89
+ if (bounded(output[key], `output.${key}`, 2, 16384) && output[key] % 2 !== 0)
90
+ add(`output.${key}`, "must be an even integer");
91
+ bounded(output.fps, "output.fps", 1, 120);
92
+ theme(value.theme, "theme", false);
93
+ if (!Array.isArray(value.scenes) || value.scenes.length === 0)
94
+ add("scenes", "must contain at least one scene");
95
+ const scenes: unknown[] = Array.isArray(value.scenes) ? value.scenes : [];
96
+ const ids = new Set<string>(),
97
+ ends = new Map<string, number>();
98
+ let total = 0;
99
+ for (const [i, raw] of scenes.entries()) {
100
+ const p = `scenes[${i}]`;
101
+ if (!record(raw)) {
102
+ add(p, "must be an object");
103
+ continue;
104
+ }
105
+ const scene = raw;
106
+ if (text(scene.id, `${p}.id`, 200)) {
107
+ if (ids.has(scene.id)) add(`${p}.id`, `duplicate id ${JSON.stringify(scene.id)}`);
108
+ ids.add(scene.id);
109
+ }
110
+ enumValue(scene.type, `${p}.type`, ["title", "footage", "ui-morph", "end-card", "motion"]);
111
+ if (scene.background !== undefined)
112
+ enumValue(scene.background, `${p}.background`, ["canvas", "dark"]);
113
+ if (scene.motion !== undefined) enumValue(scene.motion, `${p}.motion`, ["on", "off"]);
114
+ if (scene.colors !== undefined) theme(scene.colors, `${p}.colors`, true);
115
+ let duration = 0;
116
+ if (scene.type !== "ui-morph" || scene.durationS !== undefined) {
117
+ if (bounded(scene.durationS, `${p}.durationS`, Number.MIN_VALUE, 86400))
118
+ duration = scene.durationS;
119
+ }
120
+ if (scene.type === "motion") {
121
+ out.push(...validateMotionScene(scene, p, baseDir));
122
+ } else if (scene.type === "title") {
123
+ const lines: unknown[] = Array.isArray(scene.lines) ? scene.lines : [];
124
+ if (!lines.length || lines.length > 6)
125
+ add(`${p}.lines`, "must contain between 1 and 6 non-empty string lines");
126
+ for (const [j, line] of lines.entries())
127
+ if (
128
+ text(line, `${p}.lines[${j}]`, 90) &&
129
+ estimatedTextWidth(line, lines.length > 2 ? 92 : 112) > 1760
130
+ )
131
+ add(`${p}.lines[${j}]`, "is too wide for the title region; split it into more lines");
132
+ optionalText(scene, "brand", p, 60);
133
+ if (scene.staggerS !== undefined) bounded(scene.staggerS, `${p}.staggerS`, 0, 86400);
134
+ if (
135
+ scene.motion !== "off" &&
136
+ 0.18 +
137
+ Math.max(0, lines.length - 1) * (number(scene.staggerS) ? scene.staggerS : 0.36) +
138
+ 0.45 >
139
+ duration
140
+ )
141
+ add(
142
+ `${p}.durationS`,
143
+ "last line cannot finish entering within scene duration; increase duration or set motion to off",
144
+ );
145
+ } else if (scene.type === "footage") {
146
+ asset(scene.asset, `${p}.asset`);
147
+ if (scene.trimStartS !== undefined) bounded(scene.trimStartS, `${p}.trimStartS`, 0);
148
+ if (scene.fit !== undefined) enumValue(scene.fit, `${p}.fit`, ["contain", "cover"]);
149
+ optionalText(scene, "caption", p, 80);
150
+ if (typeof scene.caption === "string" && estimatedTextWidth(scene.caption, 34) > 1720)
151
+ add(`${p}.caption`, "is too wide for the caption region");
152
+ if (scene.frame !== undefined) {
153
+ if (!record(scene.frame)) add(`${p}.frame`, "must be an object");
154
+ else {
155
+ bounded(scene.frame.inset, `${p}.frame.inset`, 0, 500);
156
+ bounded(
157
+ scene.frame.radius,
158
+ `${p}.frame.radius`,
159
+ 0,
160
+ number(scene.frame.inset) ? Math.max(0, 540 - scene.frame.inset) : 540,
161
+ );
162
+ }
163
+ }
164
+ } else if (scene.type === "ui-morph") {
165
+ let width = 1100,
166
+ compact = 112,
167
+ expanded = 218,
168
+ radius = 34;
169
+ if (scene.panel !== undefined) {
170
+ if (!record(scene.panel)) add(`${p}.panel`, "must be an object");
171
+ else {
172
+ if (bounded(scene.panel.width, `${p}.panel.width`, 400, 1700)) width = scene.panel.width;
173
+ if (bounded(scene.panel.compactHeight, `${p}.panel.compactHeight`, 112, 680))
174
+ compact = scene.panel.compactHeight;
175
+ if (bounded(scene.panel.expandedHeight, `${p}.panel.expandedHeight`, 180, 680))
176
+ expanded = scene.panel.expandedHeight;
177
+ if (
178
+ bounded(
179
+ scene.panel.radius,
180
+ `${p}.panel.radius`,
181
+ 0,
182
+ Math.min(width, compact, expanded) / 2,
183
+ )
184
+ )
185
+ radius = scene.panel.radius;
186
+ }
187
+ }
188
+ optionalText(scene, "eyebrow", p, 60);
189
+ const states: unknown[] = Array.isArray(scene.states) ? scene.states : [];
190
+ if (!states.length) add(`${p}.states`, "must contain at least one state");
191
+ let derived = 0;
192
+ for (const [j, rawState] of states.entries()) {
193
+ const q = `${p}.states[${j}]`;
194
+ if (!record(rawState)) {
195
+ add(q, "must be an object");
196
+ continue;
197
+ }
198
+ const state = rawState;
199
+ enumValue(state.icon, `${q}.icon`, [
200
+ "record",
201
+ "style",
202
+ "refine",
203
+ "camera",
204
+ "spark",
205
+ "target",
206
+ ]);
207
+ text(state.label, `${q}.label`, 24);
208
+ text(state.prompt, `${q}.prompt`, 90, j === 0);
209
+ for (const key of ["transitionS", "typeS", "typeDelayS", "holdS"])
210
+ bounded(
211
+ state[key],
212
+ `${q}.${key}`,
213
+ key === "transitionS" || key === "typeS" ? Number.MIN_VALUE : 0,
214
+ 86400,
215
+ );
216
+ if ([state.transitionS, state.typeS, state.typeDelayS, state.holdS].every(number))
217
+ derived +=
218
+ Math.max(
219
+ state.transitionS as number,
220
+ (state.typeDelayS as number) + (state.typeS as number),
221
+ ) + (state.holdS as number);
222
+ if (state.panelWidth !== undefined) bounded(state.panelWidth, `${q}.panelWidth`, 400, 1700);
223
+ if (state.panelHeight !== undefined)
224
+ bounded(
225
+ state.panelHeight,
226
+ `${q}.panelHeight`,
227
+ j === 0 && state.prompt === "" ? 112 : 180,
228
+ 680,
229
+ );
230
+ const pw = number(state.panelWidth) ? state.panelWidth : width,
231
+ ph = number(state.panelHeight) ? state.panelHeight : j === 0 ? compact : expanded;
232
+ if (radius > Math.min(pw, ph) / 2)
233
+ add(`${q}.panelHeight`, "panel dimensions must fit the configured corner radius");
234
+ if (typeof state.prompt === "string" && estimatedTextWidth(state.prompt, 46) > pw - 150)
235
+ add(
236
+ `${q}.prompt`,
237
+ `is too wide for its ${pw}px panel; shorten it or increase panelWidth`,
238
+ );
239
+ if (
240
+ typeof state.label === "string" &&
241
+ (132 + state.label.length * 11 > pw - 100 ||
242
+ estimatedTextWidth(state.label, 30) > 50 + state.label.length * 11)
243
+ )
244
+ add(`${q}.label`, "is too wide for its chip; shorten the label");
245
+ if (j === 0 && typeof state.prompt === "string" && state.prompt.trim() && ph < 180)
246
+ add(`${q}.panelHeight`, "a visible prompt needs an expanded panel of at least 180px");
247
+ }
248
+ if (number(scene.durationS) && Math.abs(scene.durationS - derived) > 0.000001)
249
+ add(`${p}.durationS`, `must equal the ${derived.toFixed(3)}s derived from state timings`);
250
+ duration = derived;
251
+ } else if (scene.type === "end-card") {
252
+ text(scene.headline, `${p}.headline`, 110);
253
+ if (scene.motion !== "off" && duration < 1.3)
254
+ add(
255
+ `${p}.durationS`,
256
+ "CTA cannot finish entering before 1.3s; increase duration or set motion to off",
257
+ );
258
+ if (typeof scene.headline === "string") {
259
+ const wrapped: string[] = [];
260
+ for (const authored of scene.headline.split("\n")) {
261
+ let line = "";
262
+ for (const word of authored.split(" ")) {
263
+ if ((line + " " + word).trim().length > 30 && line) {
264
+ wrapped.push(line);
265
+ line = word;
266
+ } else line = (line + " " + word).trim();
267
+ }
268
+ if (line) wrapped.push(line);
269
+ }
270
+ if (wrapped.length > 3 || wrapped.some((line) => estimatedTextWidth(line, 92) > 1760))
271
+ add(
272
+ `${p}.headline`,
273
+ "cannot fit the three-line headline region; shorten copy or add suitable line breaks",
274
+ );
275
+ }
276
+ text(scene.brand, `${p}.brand`, 60);
277
+ text(scene.cta, `${p}.cta`, 100);
278
+ if (typeof scene.cta === "string" && estimatedTextWidth(scene.cta, 28) > 1700)
279
+ add(`${p}.cta`, "is too wide for the CTA region");
280
+ if (
281
+ typeof scene.brand === "string" &&
282
+ estimatedTextWidth(scene.brand, 30) + scene.brand.length * 4 > 1760
283
+ )
284
+ add(`${p}.brand`, "is too wide for the brand region");
285
+ }
286
+ if (scene.transition !== undefined) {
287
+ if (!record(scene.transition)) add(`${p}.transition`, "must be an object");
288
+ else {
289
+ enumValue(scene.transition.type, `${p}.transition.type`, ["cut", "fade", "slide"]);
290
+ if (scene.transition.durationS !== undefined)
291
+ bounded(
292
+ scene.transition.durationS,
293
+ `${p}.transition.durationS`,
294
+ Number.MIN_VALUE,
295
+ duration / 2,
296
+ );
297
+ }
298
+ }
299
+ total += duration;
300
+ if (typeof scene.id === "string") ends.set(scene.id, total);
301
+ }
302
+ if (!Number.isFinite(total) || total > 86400)
303
+ add("scenes", "total duration must be finite and no more than 86400s");
304
+ if (value.audio !== undefined && !Array.isArray(value.audio)) add("audio", "must be an array");
305
+ const audio: unknown[] = Array.isArray(value.audio) ? value.audio : [],
306
+ audioIds = new Set<string>();
307
+ for (const [i, raw] of audio.entries()) {
308
+ const p = `audio[${i}]`;
309
+ if (!record(raw)) {
310
+ add(p, "must be an object");
311
+ continue;
312
+ }
313
+ const track = raw;
314
+ if (text(track.id, `${p}.id`, 200)) {
315
+ if (audioIds.has(track.id)) add(`${p}.id`, "duplicate audio id");
316
+ audioIds.add(track.id);
317
+ }
318
+ enumValue(track.kind, `${p}.kind`, ["music", "narration", "sfx"]);
319
+ asset(track.asset, `${p}.asset`);
320
+ if ((track.atS === undefined) === (track.afterSceneId === undefined))
321
+ add(p, "set exactly one of atS or afterSceneId");
322
+ if (track.atS !== undefined) bounded(track.atS, `${p}.atS`, 0);
323
+ if (
324
+ track.afterSceneId !== undefined &&
325
+ (typeof track.afterSceneId !== "string" || !ends.has(track.afterSceneId))
326
+ )
327
+ add(`${p}.afterSceneId`, "does not name a scene");
328
+ if (track.offsetS !== undefined) {
329
+ bounded(track.offsetS, `${p}.offsetS`, -86400, 86400);
330
+ if (track.atS !== undefined)
331
+ add(`${p}.offsetS`, "requires afterSceneId; use atS for absolute placement");
332
+ }
333
+ for (const key of ["trimStartS", "fadeInS", "fadeOutS", "durationS"])
334
+ if (track[key] !== undefined)
335
+ bounded(track[key], `${p}.${key}`, key === "durationS" ? Number.MIN_VALUE : 0);
336
+ if (track.gain !== undefined) bounded(track.gain, `${p}.gain`, 0, 8);
337
+ for (const key of ["loop", "duckMusic"])
338
+ if (track[key] !== undefined && typeof track[key] !== "boolean")
339
+ add(`${p}.${key}`, "must be a boolean");
340
+ if (track.loop === true && track.kind !== "music")
341
+ add(`${p}.loop`, "is supported only for music");
342
+ if (track.duckMusic === true && track.kind !== "narration")
343
+ add(`${p}.duckMusic`, "is supported only for narration");
344
+ const at = number(track.atS)
345
+ ? track.atS
346
+ : (typeof track.afterSceneId === "string" ? (ends.get(track.afterSceneId) ?? 0) : 0) +
347
+ (number(track.offsetS) ? track.offsetS : 0);
348
+ const duration = number(track.durationS) ? track.durationS : total - at;
349
+ if (at < 0 || at >= total)
350
+ add(`${p}.atS`, "resolved position must be within the composition duration");
351
+ if (at + duration > total + 0.000001)
352
+ add(`${p}.durationS`, "extends past the composition duration");
353
+ const fades =
354
+ (number(track.fadeInS) ? track.fadeInS : 0) + (number(track.fadeOutS) ? track.fadeOutS : 0);
355
+ if (fades > duration)
356
+ add(`${p}.fadeOutS`, "fade-in plus fade-out exceeds the resolved track duration");
357
+ }
358
+ out.push(
359
+ ...validateLaunchStory(
360
+ value.story,
361
+ out.some((item) => item.path.startsWith("scenes")) ? undefined : (scenes as LaunchScene[]),
362
+ ),
363
+ );
364
+ return out;
365
+ }
366
+
367
+ function estimatedTextWidth(text: string, size: number): number {
368
+ let units = 0;
369
+ for (const char of text) units += (char.codePointAt(0) ?? 0) > 255 ? 1 : 0.58;
370
+ return units * size;
371
+ }
372
+ export function formatLaunchIssues(issues: LaunchIssue[]): string {
373
+ return issues.map((x) => `${x.severity}: ${x.path}: ${x.message}`).join("\n");
374
+ }
@@ -0,0 +1,145 @@
1
+ import type {
2
+ EvaluatedMotionLayer,
3
+ EvaluatedMotionScene,
4
+ MotionEasing,
5
+ MotionLayer,
6
+ MotionScene,
7
+ MotionTrack,
8
+ } from "./motion-types";
9
+
10
+ export type MotionColor = [number, number, number, number];
11
+ /** Same explicit color syntax as launch themes; no browser-dependent named colors. */
12
+ export function parseMotionColor(value: unknown): MotionColor | undefined {
13
+ if (typeof value !== "string") return undefined;
14
+ if (/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(value))
15
+ return [
16
+ Number.parseInt(value.slice(1, 3), 16),
17
+ Number.parseInt(value.slice(3, 5), 16),
18
+ Number.parseInt(value.slice(5, 7), 16),
19
+ value.length === 9 ? Number.parseInt(value.slice(7, 9), 16) / 255 : 1,
20
+ ];
21
+ const m = value.match(
22
+ /^(rgb|rgba)\(\s*(\d+(?:\.\d+)?|\.\d+)\s*,\s*(\d+(?:\.\d+)?|\.\d+)\s*,\s*(\d+(?:\.\d+)?|\.\d+)(?:\s*,\s*(\d+(?:\.\d+)?|\.\d+))?\s*\)$/i,
23
+ );
24
+ if (!m || (m[1]!.toLowerCase() === "rgba") !== (m[5] !== undefined)) return undefined;
25
+ const rgba: MotionColor = [
26
+ Number(m[2]),
27
+ Number(m[3]),
28
+ Number(m[4]),
29
+ m[5] === undefined ? 1 : Number(m[5]),
30
+ ];
31
+ return rgba.every((n, i) => Number.isFinite(n) && n >= 0 && n <= (i === 3 ? 1 : 255))
32
+ ? rgba
33
+ : undefined;
34
+ }
35
+ const clamp = (value: number) => Math.max(0, Math.min(1, value));
36
+ function ease(t: number, easing: MotionEasing = "linear"): number {
37
+ const p = clamp(t);
38
+ switch (easing) {
39
+ case "ease-in":
40
+ return p ** 3;
41
+ case "ease-out":
42
+ return 1 - (1 - p) ** 3;
43
+ case "ease-in-out":
44
+ return p < 0.5 ? 4 * p ** 3 : 1 - (-2 * p + 2) ** 3 / 2;
45
+ case "step":
46
+ return p < 1 ? 0 : 1;
47
+ default:
48
+ return p;
49
+ }
50
+ }
51
+ export function evaluateMotionTrack(track: MotionTrack, localS: number): number | string {
52
+ const frames = track.keyframes;
53
+ if (localS <= frames[0]!.atS) return frames[0]!.value;
54
+ for (let i = 1; i < frames.length; i++) {
55
+ const to = frames[i]!,
56
+ from = frames[i - 1]!;
57
+ if (localS > to.atS) continue;
58
+ const p = ease((localS - from.atS) / (to.atS - from.atS), to.easing);
59
+ if (p === 0) return from.value;
60
+ if (p === 1) return to.value;
61
+ if (typeof from.value === "number" && typeof to.value === "number")
62
+ return from.value + (to.value - from.value) * p;
63
+ const a = parseMotionColor(from.value)!,
64
+ b = parseMotionColor(to.value)!;
65
+ const rgba = a.map((n, j) => n + (b[j]! - n) * p);
66
+ return `rgba(${rgba
67
+ .slice(0, 3)
68
+ .map((n) => Number(n.toFixed(4)))
69
+ .join(",")},${Number(rgba[3]!.toFixed(6))})`;
70
+ }
71
+ return frames.at(-1)!.value;
72
+ }
73
+ const segmenter = new Intl.Segmenter("en", { granularity: "grapheme" });
74
+ export function motionGraphemes(text: string): string[] {
75
+ return Array.from(segmenter.segment(text), (part) => part.segment);
76
+ }
77
+
78
+ /** Input must have passed validation. Evaluation is pure and shared by Node and the browser. */
79
+ export function evaluateMotionScene(scene: MotionScene, localS: number): EvaluatedMotionScene {
80
+ const evaluate = (layer: MotionLayer): EvaluatedMotionLayer => {
81
+ const { animations, ...authored } = layer;
82
+ const result: Record<string, unknown> = {
83
+ x: 0,
84
+ y: 0,
85
+ opacity: 1,
86
+ scaleX: 1,
87
+ scaleY: 1,
88
+ rotation: 0,
89
+ ...authored,
90
+ };
91
+ if (scene.motion !== "off")
92
+ for (const track of animations ?? [])
93
+ result[track.property] = evaluateMotionTrack(track, localS);
94
+ if (layer.type === "group") {
95
+ result.children = layer.children.map(evaluate);
96
+ result.clip = layer.clip ?? false;
97
+ result.radius ??= 0;
98
+ }
99
+ if (layer.type === "rect" || layer.type === "image" || layer.type === "video")
100
+ result.radius ??= 0;
101
+ if ((layer.type === "rect" || layer.type === "image" || layer.type === "video") && layer.shadow)
102
+ result.shadow = { ...layer.shadow };
103
+ if (layer.type === "rect" || layer.type === "ellipse") result.strokeWidth ??= 0;
104
+ if (layer.type === "line") {
105
+ result.end ??= 1;
106
+ result.strokeWidth ??= 2;
107
+ result.points = layer.points.map((point) => [...point]);
108
+ }
109
+ if (layer.type === "image") {
110
+ result.fit ??= "contain";
111
+ if (layer.crop) result.crop = { ...layer.crop };
112
+ }
113
+ if (layer.type === "video") {
114
+ result.fit ??= "contain";
115
+ result.trimStartS ??= 0;
116
+ result.startS ??= 0;
117
+ result.mediaTimeS =
118
+ (result.trimStartS as number) +
119
+ Math.max(0, Math.min(layer.durationS, localS - (result.startS as number)));
120
+ }
121
+ if (layer.type === "text") {
122
+ result.fullText = layer.text;
123
+ result.reveal ??= 1;
124
+ result.align ??= "center";
125
+ result.fontWeight ??= 500;
126
+ result.lineHeight = layer.lineHeight ?? (result.fontSize as number) * 1.2;
127
+ const graphemes = motionGraphemes(layer.text);
128
+ result.text = graphemes
129
+ .slice(0, Math.floor(graphemes.length * clamp(result.reveal as number) + 1e-9))
130
+ .join("");
131
+ }
132
+ return result as EvaluatedMotionLayer;
133
+ };
134
+ return {
135
+ designSize: { ...(scene.designSize ?? { width: 1920, height: 1080 }) },
136
+ layers: scene.layers.map(evaluate),
137
+ };
138
+ }
139
+
140
+ /** Visit validated layers in paint order, including nested image assets. */
141
+ export function walkMotionLayers(layers: MotionLayer[]): MotionLayer[] {
142
+ return layers.flatMap((layer) =>
143
+ layer.type === "group" ? [layer, ...walkMotionLayers(layer.children)] : [layer],
144
+ );
145
+ }