@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,1021 @@
1
+ import { analyzeLaunchStory } from "./launch-story";
2
+ import type {
3
+ LaunchComposition,
4
+ LaunchIssue,
5
+ LaunchScene,
6
+ LaunchTheme,
7
+ LaunchTransition,
8
+ } from "./launch-types";
9
+ import { validateLaunchComposition } from "./launch-validate";
10
+ import { motionGraphemes } from "./motion-evaluate";
11
+ import { analyzeMotionQuality, estimateMotionReadingSeconds } from "./motion-quality";
12
+ import type {
13
+ MotionGroupLayer,
14
+ MotionImageCrop,
15
+ MotionLayer,
16
+ MotionScene,
17
+ MotionTextLayer,
18
+ } from "./motion-types";
19
+ import { estimateMotionTextLines } from "./motion-validate";
20
+
21
+ export type MotionRecipeImage = { asset: string; crop?: MotionImageCrop };
22
+ export type MotionRecipeStyle = "editorial" | "product" | "technical";
23
+ type RecipeBase = {
24
+ id: string;
25
+ title: string;
26
+ style?: MotionRecipeStyle;
27
+ durationS?: number;
28
+ motion?: "on" | "off";
29
+ transition?: LaunchTransition;
30
+ };
31
+ export type FocusMotionRecipe = RecipeBase & {
32
+ recipe: "focus";
33
+ image: MotionRecipeImage;
34
+ body?: string;
35
+ };
36
+ export type CompareMotionRecipe = RecipeBase & {
37
+ recipe: "compare";
38
+ before: { label: string; image: MotionRecipeImage };
39
+ after: { label: string; image: MotionRecipeImage };
40
+ body?: string;
41
+ };
42
+ export type StepsMotionRecipe = RecipeBase & {
43
+ recipe: "steps";
44
+ steps: { label: string; body?: string }[];
45
+ };
46
+ export type MotionRecipe = FocusMotionRecipe | CompareMotionRecipe | StepsMotionRecipe;
47
+ export type MotionStoryBrief = Omit<LaunchComposition, "scenes"> & {
48
+ scenes: (LaunchScene | MotionRecipe)[];
49
+ };
50
+ export type MotionStoryResult = { composition?: LaunchComposition; issues: LaunchIssue[] };
51
+ type Box = { x: number; y: number; width: number; height: number };
52
+ const record = (v: unknown): v is Record<string, unknown> =>
53
+ v !== null && typeof v === "object" && !Array.isArray(v);
54
+ const finite = (v: unknown): v is number => typeof v === "number" && Number.isFinite(v);
55
+ const error = (path: string, message: string): LaunchIssue => ({
56
+ severity: "error",
57
+ path,
58
+ message,
59
+ });
60
+
61
+ function parseRecipe(
62
+ raw: Record<string, unknown>,
63
+ path: string,
64
+ issues: LaunchIssue[],
65
+ ): MotionRecipe | undefined {
66
+ const start = issues.length;
67
+ const add = (p: string, message: string) => issues.push(error(p, message));
68
+ const fields = (obj: Record<string, unknown>, keys: string[], p: string) => {
69
+ for (const key of Object.keys(obj))
70
+ if (!keys.includes(key)) add(`${p}.${key}`, "unsupported recipe field");
71
+ };
72
+ const text = (v: unknown, p: string, max: number) => {
73
+ if (typeof v !== "string" || !v.trim() || motionGraphemes(v).length > max)
74
+ add(
75
+ p,
76
+ `must be non-empty copy of at most ${max} graphemes; shorter copy may be required to fit the chosen layout`,
77
+ );
78
+ };
79
+ const image = (v: unknown, p: string) => {
80
+ if (!record(v)) {
81
+ add(p, "must be an image object with a local asset path");
82
+ return;
83
+ }
84
+ fields(v, ["asset", "crop"], p);
85
+ if (
86
+ typeof v.asset !== "string" ||
87
+ !v.asset.trim() ||
88
+ v.asset.length > 4096 ||
89
+ v.asset.includes("\0") ||
90
+ !/\.(png|jpe?g|webp)$/i.test(v.asset) ||
91
+ (/^[a-z][a-z\d+.-]*:/i.test(v.asset) && !/^[a-z]:[\\/]/i.test(v.asset))
92
+ )
93
+ add(
94
+ `${p}.asset`,
95
+ "must be a local PNG/JPEG/WebP path; use the asset checker to verify the actual file",
96
+ );
97
+ if (v.crop !== undefined) {
98
+ if (!record(v.crop)) add(`${p}.crop`, "must be an object");
99
+ else {
100
+ fields(v.crop, ["x", "y", "width", "height"], `${p}.crop`);
101
+ for (const key of ["x", "y", "width", "height"])
102
+ if (
103
+ !finite(v.crop[key]) ||
104
+ !Number.isInteger(v.crop[key]) ||
105
+ v.crop[key] < (key === "x" || key === "y" ? 0 : 1) ||
106
+ v.crop[key] > 32768
107
+ )
108
+ add(`${p}.crop.${key}`, "must be an integer source-pixel value in the supported range");
109
+ }
110
+ }
111
+ };
112
+ const common = ["id", "recipe", "title", "style", "durationS", "motion", "transition"];
113
+ if (typeof raw.recipe !== "string" || !["focus", "compare", "steps"].includes(raw.recipe)) {
114
+ add(`${path}.recipe`, "must be focus, compare, or steps");
115
+ return;
116
+ }
117
+ fields(
118
+ raw,
119
+ [
120
+ ...common,
121
+ ...(raw.recipe === "focus"
122
+ ? ["image", "body"]
123
+ : raw.recipe === "compare"
124
+ ? ["before", "after", "body"]
125
+ : ["steps"]),
126
+ ],
127
+ path,
128
+ );
129
+ text(raw.id, `${path}.id`, 200);
130
+ text(raw.title, `${path}.title`, 160);
131
+ if (
132
+ raw.style !== undefined &&
133
+ raw.style !== "editorial" &&
134
+ raw.style !== "product" &&
135
+ raw.style !== "technical"
136
+ )
137
+ add(`${path}.style`, "must be editorial, product, or technical");
138
+ if (
139
+ raw.durationS !== undefined &&
140
+ (!finite(raw.durationS) || raw.durationS <= 0 || raw.durationS > 86400)
141
+ )
142
+ add(`${path}.durationS`, "must be finite, positive and no more than 86400s");
143
+ if (raw.motion !== undefined && raw.motion !== "on" && raw.motion !== "off")
144
+ add(`${path}.motion`, "must be on or off");
145
+ if (raw.transition !== undefined) {
146
+ if (!record(raw.transition)) add(`${path}.transition`, "must be an object");
147
+ else {
148
+ fields(raw.transition, ["type", "durationS"], `${path}.transition`);
149
+ if (
150
+ typeof raw.transition.type !== "string" ||
151
+ !["cut", "fade", "slide"].includes(raw.transition.type)
152
+ )
153
+ add(`${path}.transition.type`, "must be cut, fade, or slide");
154
+ if (
155
+ raw.transition.durationS !== undefined &&
156
+ (!finite(raw.transition.durationS) || raw.transition.durationS <= 0)
157
+ )
158
+ add(`${path}.transition.durationS`, "must be finite and positive");
159
+ }
160
+ }
161
+ if (raw.recipe === "focus") image(raw.image, `${path}.image`);
162
+ if (raw.recipe === "focus" || raw.recipe === "compare")
163
+ if (raw.body !== undefined) text(raw.body, `${path}.body`, 360);
164
+ if (raw.recipe === "compare")
165
+ for (const side of ["before", "after"]) {
166
+ const item = raw[side];
167
+ if (!record(item)) add(`${path}.${side}`, "must contain a label and image");
168
+ else {
169
+ fields(item, ["label", "image"], `${path}.${side}`);
170
+ text(item.label, `${path}.${side}.label`, 80);
171
+ image(item.image, `${path}.${side}.image`);
172
+ }
173
+ }
174
+ if (raw.recipe === "steps") {
175
+ if (!Array.isArray(raw.steps) || raw.steps.length < 2 || raw.steps.length > 5)
176
+ add(`${path}.steps`, "must contain 2 to 5 steps");
177
+ else
178
+ raw.steps.forEach((step, i) => {
179
+ const p = `${path}.steps[${i}]`;
180
+ if (!record(step)) {
181
+ add(p, "must contain a label and optional body");
182
+ return;
183
+ }
184
+ fields(step, ["label", "body"], p);
185
+ text(step.label, `${p}.label`, 80);
186
+ if (step.body !== undefined) text(step.body, `${p}.body`, 200);
187
+ });
188
+ }
189
+ return issues.length === start ? (raw as unknown as MotionRecipe) : undefined;
190
+ }
191
+
192
+ function buildRecipe(
193
+ recipe: MotionRecipe,
194
+ path: string,
195
+ output: LaunchComposition["output"],
196
+ theme: LaunchTheme,
197
+ issues: LaunchIssue[],
198
+ ): MotionScene {
199
+ const W = output.width,
200
+ H = output.height,
201
+ S = Math.min(W, H),
202
+ style = recipe.style,
203
+ portrait =
204
+ H > W ||
205
+ (H === W &&
206
+ (recipe.recipe === "focus" || (recipe.recipe === "steps" && recipe.steps.length <= 3))),
207
+ margin =
208
+ S *
209
+ (style === "editorial"
210
+ ? 0.085
211
+ : style === "product"
212
+ ? 0.05
213
+ : style === "technical"
214
+ ? 0.055
215
+ : 0.07),
216
+ gap =
217
+ S *
218
+ (style === "editorial"
219
+ ? 0.055
220
+ : style === "product"
221
+ ? 0.032
222
+ : style === "technical"
223
+ ? 0.028
224
+ : 0.045),
225
+ pad =
226
+ S *
227
+ (style === "editorial"
228
+ ? 0.01
229
+ : style === "product"
230
+ ? 0.007
231
+ : style === "technical"
232
+ ? 0.009
233
+ : 0.012);
234
+ const innerW = W - 2 * margin,
235
+ innerH = H - 2 * margin,
236
+ left = -W / 2 + margin,
237
+ top = -H / 2 + margin;
238
+ const fonts = {
239
+ title: Math.min(
240
+ 512,
241
+ Math.max(
242
+ 24,
243
+ S *
244
+ (style === "editorial"
245
+ ? 0.072
246
+ : style === "product"
247
+ ? 0.056
248
+ : style === "technical"
249
+ ? 0.052
250
+ : 0.06),
251
+ ),
252
+ ),
253
+ label: Math.min(
254
+ 512,
255
+ Math.max(
256
+ 14,
257
+ S *
258
+ (style === "editorial"
259
+ ? 0.034
260
+ : style === "product"
261
+ ? 0.038
262
+ : style === "technical"
263
+ ? 0.031
264
+ : 0.037),
265
+ ),
266
+ ),
267
+ body: Math.min(
268
+ 512,
269
+ Math.max(
270
+ 14,
271
+ S *
272
+ (style === "editorial"
273
+ ? 0.027
274
+ : style === "product"
275
+ ? 0.029
276
+ : style === "technical"
277
+ ? 0.025
278
+ : 0.03),
279
+ ),
280
+ ),
281
+ number: Math.min(
282
+ 512,
283
+ Math.max(
284
+ 14,
285
+ S *
286
+ (style === "editorial"
287
+ ? 0.026
288
+ : style === "product"
289
+ ? 0.03
290
+ : style === "technical"
291
+ ? 0.024
292
+ : 0.03),
293
+ ),
294
+ ),
295
+ };
296
+ const layers: MotionLayer[] = [],
297
+ texts: string[] = [];
298
+ let latestEntrance = 0;
299
+ const text = (
300
+ id: string,
301
+ copy: string,
302
+ box: Box,
303
+ role: keyof typeof fonts,
304
+ sourcePath: string,
305
+ fill = theme.ink,
306
+ padding = pad,
307
+ ): MotionTextLayer => {
308
+ const size = fonts[role],
309
+ width = box.width - padding * 2,
310
+ height = box.height - padding * 2,
311
+ lineHeight = size * 1.28;
312
+ if (
313
+ width <= 0 ||
314
+ height <= 0 ||
315
+ estimateMotionTextLines(copy, width, size) * lineHeight > height + 0.001
316
+ )
317
+ issues.push(
318
+ error(
319
+ sourcePath,
320
+ `copy does not fit the ${recipe.recipe} ${W === H ? "square" : portrait ? "portrait" : "landscape"} ${role} region; shorten it or use a custom motion scene with a larger text box`,
321
+ ),
322
+ );
323
+ texts.push(copy);
324
+ return {
325
+ id,
326
+ type: "text",
327
+ text: copy,
328
+ x: box.x,
329
+ y: box.y,
330
+ width: Math.max(0.001, width),
331
+ height: Math.max(0.001, height),
332
+ fontSize: size,
333
+ lineHeight,
334
+ fontFamily: theme.fontFamily,
335
+ fontWeight:
336
+ role === "title"
337
+ ? style === "product"
338
+ ? 700
339
+ : style === "technical"
340
+ ? 600
341
+ : 650
342
+ : role === "label"
343
+ ? style === "technical"
344
+ ? 550
345
+ : 600
346
+ : style === "editorial"
347
+ ? 400
348
+ : 450,
349
+ fill,
350
+ align: "left",
351
+ };
352
+ };
353
+ const group = (id: string, children: MotionLayer[], delay: number): MotionGroupLayer => {
354
+ const animated = recipe.motion !== "off" && id !== "heading" && id !== "message";
355
+ const entranceS =
356
+ style === "editorial"
357
+ ? 0.68
358
+ : style === "product"
359
+ ? 0.62
360
+ : style === "technical"
361
+ ? 0.42
362
+ : 0.55,
363
+ finish = delay + entranceS;
364
+ if (animated) latestEntrance = Math.max(latestEntrance, finish);
365
+ const keyframes = (a: number, b: number) => [
366
+ { atS: 0, value: a },
367
+ ...(delay > 0 ? [{ atS: delay, value: a }] : []),
368
+ { atS: finish, value: b, easing: "ease-out" as const },
369
+ ];
370
+ return {
371
+ id,
372
+ type: "group",
373
+ children,
374
+ ...(!animated
375
+ ? {}
376
+ : {
377
+ animations: [
378
+ { property: "opacity", keyframes: keyframes(0, 1) },
379
+ ...(style === "technical"
380
+ ? [{ property: "x" as const, keyframes: keyframes(-S * 0.018, 0) }]
381
+ : [
382
+ {
383
+ property: "y" as const,
384
+ keyframes: keyframes(
385
+ S * (style === "editorial" ? 0.014 : style === "product" ? 0.018 : 0.024),
386
+ 0,
387
+ ),
388
+ },
389
+ ]),
390
+ ...(style === "product"
391
+ ? [
392
+ { property: "scaleX" as const, keyframes: keyframes(0.965, 1) },
393
+ { property: "scaleY" as const, keyframes: keyframes(0.965, 1) },
394
+ ]
395
+ : []),
396
+ ],
397
+ }),
398
+ };
399
+ };
400
+ const rule = (id: string, points: [number, number][], delay = 0.08): MotionLayer => {
401
+ const finish = delay + (style === "technical" ? 0.34 : 0.52),
402
+ animated = recipe.motion !== "off";
403
+ if (animated) latestEntrance = Math.max(latestEntrance, finish);
404
+ return {
405
+ id,
406
+ type: "line",
407
+ points,
408
+ stroke: theme.accent,
409
+ strokeWidth: Math.max(1, S * (style === "technical" ? 0.0015 : 0.001)),
410
+ end: 1,
411
+ ...(animated
412
+ ? {
413
+ animations: [
414
+ {
415
+ property: "end",
416
+ keyframes: [
417
+ { atS: 0, value: 0 },
418
+ ...(delay > 0 ? [{ atS: delay, value: 0 }] : []),
419
+ { atS: finish, value: 1, easing: "ease-out" },
420
+ ],
421
+ },
422
+ ],
423
+ }
424
+ : {}),
425
+ };
426
+ };
427
+ const picturePad = style === "editorial" ? S * 0.022 : pad;
428
+ const picture = (id: string, image: MotionRecipeImage, box: Box): MotionLayer[] => {
429
+ const radius =
430
+ S *
431
+ (style === "editorial"
432
+ ? 0
433
+ : style === "product"
434
+ ? 0.026
435
+ : style === "technical"
436
+ ? 0.003
437
+ : 0.016),
438
+ imageRadius =
439
+ S *
440
+ (style === "editorial"
441
+ ? 0
442
+ : style === "product"
443
+ ? 0.021
444
+ : style === "technical"
445
+ ? 0.002
446
+ : 0.008);
447
+ return [
448
+ {
449
+ id: `${id}-surface`,
450
+ type: "rect",
451
+ ...box,
452
+ fill: theme.surface,
453
+ radius,
454
+ ...(style === "product"
455
+ ? {
456
+ shadow: {
457
+ color: "#00000024",
458
+ blur: Math.min(256, S * 0.032),
459
+ offsetY: Math.min(512, S * 0.011),
460
+ },
461
+ }
462
+ : style === "technical"
463
+ ? { stroke: theme.accent, strokeWidth: Math.max(1, S * 0.0015) }
464
+ : {}),
465
+ },
466
+ {
467
+ id,
468
+ type: "image",
469
+ ...box,
470
+ width: box.width - 2 * picturePad,
471
+ height: box.height - 2 * picturePad,
472
+ asset: image.asset,
473
+ ...(image.crop ? { crop: { ...image.crop } } : {}),
474
+ fit: "contain",
475
+ radius: imageRadius,
476
+ },
477
+ ];
478
+ };
479
+ const fittedFocusBox = (image: MotionRecipeImage, box: Box): Box => {
480
+ if (!style || !image.crop) return box;
481
+ const mediaAspect = image.crop.width / image.crop.height,
482
+ availableWidth = Math.max(0.001, box.width - 2 * picturePad),
483
+ availableHeight = Math.max(0.001, box.height - 2 * picturePad);
484
+ if (availableWidth / availableHeight > mediaAspect) {
485
+ const innerWidth = availableHeight * mediaAspect;
486
+ return {
487
+ ...box,
488
+ width: innerWidth + 2 * picturePad,
489
+ height: availableHeight + 2 * picturePad,
490
+ };
491
+ }
492
+ const innerHeight = availableWidth / mediaAspect;
493
+ return { ...box, width: availableWidth + 2 * picturePad, height: innerHeight + 2 * picturePad };
494
+ };
495
+ const stepSurface = (id: string, box: Box): MotionLayer | undefined => {
496
+ if (style !== "product" && style !== "technical") return undefined;
497
+ return {
498
+ id: `${id}-surface`,
499
+ type: "rect",
500
+ ...box,
501
+ fill: theme.surface,
502
+ radius: style === "product" ? S * 0.02 : S * 0.002,
503
+ ...(style === "product"
504
+ ? {
505
+ shadow: {
506
+ color: "#0000001F",
507
+ blur: Math.min(256, S * 0.022),
508
+ offsetY: Math.min(512, S * 0.008),
509
+ },
510
+ }
511
+ : { stroke: theme.accent, strokeWidth: Math.max(1, S * 0.0015) }),
512
+ };
513
+ };
514
+ if (recipe.recipe === "focus") {
515
+ if (!portrait) {
516
+ const copyW =
517
+ (innerW - gap) *
518
+ (style === "editorial"
519
+ ? 0.44
520
+ : style === "product"
521
+ ? 0.29
522
+ : style === "technical"
523
+ ? 0.34
524
+ : 0.36),
525
+ imageW = innerW - gap - copyW,
526
+ titleH = S * (style === "editorial" ? 0.32 : style === "technical" ? 0.22 : 0.26),
527
+ bodyH = recipe.body
528
+ ? S * (style === "editorial" ? 0.27 : style === "technical" ? 0.25 : 0.31)
529
+ : 0,
530
+ total = titleH + (recipe.body ? gap + bodyH : 0),
531
+ x = left + copyW / 2;
532
+ const content = [
533
+ text(
534
+ "title",
535
+ recipe.title,
536
+ { x, y: -total / 2 + titleH / 2, width: copyW, height: titleH },
537
+ "title",
538
+ `${path}.title`,
539
+ ),
540
+ ];
541
+ if (recipe.body)
542
+ content.push(
543
+ text(
544
+ "body",
545
+ recipe.body,
546
+ { x, y: total / 2 - bodyH / 2, width: copyW, height: bodyH },
547
+ "body",
548
+ `${path}.body`,
549
+ ),
550
+ );
551
+ const visualBox = fittedFocusBox(recipe.image, {
552
+ x: left + copyW + gap + imageW / 2,
553
+ y: 0,
554
+ width: imageW,
555
+ height: innerH,
556
+ });
557
+ layers.push(
558
+ group("message", content, 0),
559
+ ...(style === "editorial" || style === "technical"
560
+ ? [
561
+ rule(
562
+ `${style}-divider`,
563
+ [
564
+ [left + copyW + gap / 2, -innerH * 0.42],
565
+ [left + copyW + gap / 2, innerH * 0.42],
566
+ ],
567
+ 0.06,
568
+ ),
569
+ ]
570
+ : []),
571
+ group("visual", picture("image", recipe.image, visualBox), 0.12),
572
+ );
573
+ } else {
574
+ const titleH = S * (style === "editorial" ? 0.26 : style === "technical" ? 0.19 : 0.22),
575
+ bodyH = recipe.body
576
+ ? S * (style === "editorial" ? 0.2 : style === "technical" ? 0.21 : 0.24)
577
+ : 0,
578
+ imageH = innerH - titleH - bodyH - gap * (recipe.body ? 2 : 1),
579
+ sourceAware = style !== undefined && recipe.image.crop !== undefined,
580
+ fitted = fittedFocusBox(recipe.image, {
581
+ x: 0,
582
+ y: 0,
583
+ width: innerW,
584
+ height: imageH,
585
+ }),
586
+ stackH = titleH + gap + fitted.height + (recipe.body ? gap + bodyH : 0),
587
+ stackTop = sourceAware ? -stackH / 2 : top,
588
+ imageTop = stackTop + titleH + gap,
589
+ bodyY = sourceAware
590
+ ? imageTop + fitted.height + gap + bodyH / 2
591
+ : H / 2 - margin - bodyH / 2;
592
+ layers.push(
593
+ group(
594
+ "heading",
595
+ [
596
+ text(
597
+ "title",
598
+ recipe.title,
599
+ { x: 0, y: stackTop + titleH / 2, width: innerW, height: titleH },
600
+ "title",
601
+ `${path}.title`,
602
+ ),
603
+ ],
604
+ 0,
605
+ ),
606
+ );
607
+ if (style === "editorial" || style === "technical")
608
+ layers.push(
609
+ rule(
610
+ `${style}-divider`,
611
+ [
612
+ [left, stackTop + titleH + gap * 0.38],
613
+ [left + innerW, stackTop + titleH + gap * 0.38],
614
+ ],
615
+ 0.06,
616
+ ),
617
+ );
618
+ layers.push(
619
+ group(
620
+ "visual",
621
+ picture("image", recipe.image, {
622
+ ...fitted,
623
+ y: imageTop + fitted.height / 2,
624
+ }),
625
+ 0.12,
626
+ ),
627
+ );
628
+ if (recipe.body)
629
+ layers.push(
630
+ group(
631
+ "message",
632
+ [
633
+ text(
634
+ "body",
635
+ recipe.body,
636
+ { x: 0, y: bodyY, width: innerW, height: bodyH },
637
+ "body",
638
+ `${path}.body`,
639
+ ),
640
+ ],
641
+ 0.2,
642
+ ),
643
+ );
644
+ }
645
+ } else if (recipe.recipe === "compare") {
646
+ const titleH = S * (style === "editorial" ? 0.22 : style === "technical" ? 0.15 : 0.18),
647
+ bodyH = recipe.body
648
+ ? S * (style === "editorial" ? 0.11 : style === "technical" ? 0.1 : 0.13)
649
+ : 0,
650
+ headH = titleH + (recipe.body ? gap * 0.45 + bodyH : 0),
651
+ contentTop = top + headH + gap,
652
+ contentH = innerH - headH - gap,
653
+ labelH = S * (style === "editorial" ? 0.09 : style === "technical" ? 0.075 : 0.11);
654
+ const header = [
655
+ text(
656
+ "title",
657
+ recipe.title,
658
+ { x: 0, y: top + titleH / 2, width: innerW, height: titleH },
659
+ "title",
660
+ `${path}.title`,
661
+ ),
662
+ ];
663
+ if (recipe.body)
664
+ header.push(
665
+ text(
666
+ "body",
667
+ recipe.body,
668
+ { x: 0, y: top + titleH + gap * 0.45 + bodyH / 2, width: innerW, height: bodyH },
669
+ "body",
670
+ `${path}.body`,
671
+ ),
672
+ );
673
+ layers.push(group("heading", header, 0));
674
+ if (style === "editorial")
675
+ layers.push(
676
+ rule(
677
+ "editorial-heading-rule",
678
+ [
679
+ [left, contentTop - gap * 0.48],
680
+ [left + innerW, contentTop - gap * 0.48],
681
+ ],
682
+ 0.06,
683
+ ),
684
+ );
685
+ if (style === "technical" && !portrait)
686
+ layers.push(
687
+ rule(
688
+ "technical-centerline",
689
+ [
690
+ [0, contentTop],
691
+ [0, contentTop + contentH],
692
+ ],
693
+ 0.08,
694
+ ),
695
+ );
696
+ (["before", "after"] as const).forEach((side, i) => {
697
+ const item = recipe[side],
698
+ width = portrait ? innerW : (innerW - gap) / 2,
699
+ height = portrait ? (contentH - gap) / 2 : contentH,
700
+ x = portrait ? 0 : left + i * (width + gap) + width / 2,
701
+ y = contentTop + (portrait ? i * (height + gap) : 0);
702
+ const children: MotionLayer[] = [
703
+ text(
704
+ `${side}-label`,
705
+ item.label,
706
+ { x, y: y + labelH / 2, width, height: labelH },
707
+ "label",
708
+ `${path}.${side}.label`,
709
+ ),
710
+ ...picture(`${side}-image`, item.image, {
711
+ x,
712
+ y: y + labelH + gap * 0.35 + (height - labelH - gap * 0.35) / 2,
713
+ width,
714
+ height: height - labelH - gap * 0.35,
715
+ }),
716
+ ];
717
+ layers.push(group(side, children, 0.15 + i * 0.12));
718
+ });
719
+ } else {
720
+ const compactRows = W === H && recipe.steps.length >= 4,
721
+ titleH = compactRows
722
+ ? estimateMotionTextLines(recipe.title, innerW - 2 * pad, fonts.title) *
723
+ fonts.title *
724
+ 1.28 +
725
+ 2 * pad
726
+ : S * 0.21,
727
+ headingGap = compactRows ? S * 0.025 : gap,
728
+ contentTop = top + titleH + headingGap,
729
+ contentH = innerH - titleH - headingGap,
730
+ n = recipe.steps.length,
731
+ stepGap = compactRows ? S * 0.01 : gap * 0.65,
732
+ rowSteps = portrait || style === "technical";
733
+ if (compactRows && contentH < stepGap * (n - 1) + n * (fonts.label * 1.28 + S * 0.006))
734
+ issues.push(
735
+ error(
736
+ `${path}.title`,
737
+ "title leaves too little room for the square step rows; shorten it or use a custom motion scene with more room",
738
+ ),
739
+ );
740
+ layers.push(
741
+ group(
742
+ "heading",
743
+ [
744
+ text(
745
+ "title",
746
+ recipe.title,
747
+ { x: 0, y: top + titleH / 2, width: innerW, height: titleH },
748
+ "title",
749
+ `${path}.title`,
750
+ ),
751
+ ],
752
+ 0,
753
+ ),
754
+ );
755
+ if (style === "editorial")
756
+ layers.push(
757
+ rule(
758
+ "editorial-heading-rule",
759
+ [
760
+ [left, contentTop - headingGap * 0.48],
761
+ [left + innerW, contentTop - headingGap * 0.48],
762
+ ],
763
+ 0.06,
764
+ ),
765
+ );
766
+ recipe.steps.forEach((step, i) => {
767
+ if (compactRows) {
768
+ // Full-width copy prevents ordinary words from breaking across narrow columns.
769
+ // Smaller inner padding preserves the 14px floor even in a 320px square.
770
+ const rowH = (contentH - stepGap * (n - 1)) / n,
771
+ rowY = contentTop + i * (rowH + stepGap),
772
+ inset = S * 0.003,
773
+ numberW = Math.max(S * 0.06, fonts.number * 1.4 + 2 * inset),
774
+ copyX = left + numberW + S * 0.018,
775
+ copyW = innerW - numberW - S * 0.018,
776
+ labelH =
777
+ estimateMotionTextLines(step.label, copyW - 2 * inset, fonts.label) *
778
+ fonts.label *
779
+ 1.28 +
780
+ 2 * inset,
781
+ bodyGap = S * 0.005,
782
+ bodyH = rowH - labelH - bodyGap;
783
+ if (labelH > rowH + 0.001) {
784
+ issues.push(
785
+ error(
786
+ `${path}.steps[${i}].label`,
787
+ "copy does not fit its square step row; shorten the label or use a custom motion scene with a taller row",
788
+ ),
789
+ );
790
+ return;
791
+ }
792
+ const children: MotionLayer[] = [
793
+ text(
794
+ `step-${i + 1}-number`,
795
+ String(i + 1).padStart(2, "0"),
796
+ { x: left + numberW / 2, y: rowY + labelH / 2, width: numberW, height: labelH },
797
+ "number",
798
+ `${path}.steps[${i}].label`,
799
+ theme.accent,
800
+ inset,
801
+ ),
802
+ text(
803
+ `step-${i + 1}-label`,
804
+ step.label,
805
+ { x: copyX + copyW / 2, y: rowY + labelH / 2, width: copyW, height: labelH },
806
+ "label",
807
+ `${path}.steps[${i}].label`,
808
+ theme.ink,
809
+ inset,
810
+ ),
811
+ ];
812
+ const surface = stepSurface(`step-${i + 1}`, {
813
+ x: 0,
814
+ y: rowY + rowH / 2,
815
+ width: innerW,
816
+ height: rowH,
817
+ });
818
+ if (surface) children.unshift(surface);
819
+ if (style === "editorial")
820
+ children.unshift(
821
+ rule(
822
+ `step-${i + 1}-rule`,
823
+ [
824
+ [left, rowY],
825
+ [left + innerW, rowY],
826
+ ],
827
+ 0.08 + i * 0.1,
828
+ ),
829
+ );
830
+ if (step.body)
831
+ children.push(
832
+ text(
833
+ `step-${i + 1}-body`,
834
+ step.body,
835
+ {
836
+ x: copyX + copyW / 2,
837
+ y: rowY + labelH + bodyGap + bodyH / 2,
838
+ width: copyW,
839
+ height: bodyH,
840
+ },
841
+ "body",
842
+ `${path}.steps[${i}].body`,
843
+ theme.ink,
844
+ inset,
845
+ ),
846
+ );
847
+ layers.push(group(`step-${i + 1}`, children, 0.12 * (i + 1)));
848
+ return;
849
+ }
850
+ const width = rowSteps ? innerW : (innerW - stepGap * (n - 1)) / n,
851
+ height = rowSteps ? (contentH - stepGap * (n - 1)) / n : contentH,
852
+ x = rowSteps ? left : left + i * (width + stepGap),
853
+ y = contentTop + (rowSteps ? i * (height + stepGap) : 0),
854
+ numberW = Math.max(S * 0.08, fonts.number * 1.4 + 2 * pad),
855
+ numberH = Math.max(S * 0.07, fonts.number * 1.28 + 2 * pad);
856
+ const labelH =
857
+ style === "editorial"
858
+ ? Math.min(S * 0.11, height * 0.4)
859
+ : Math.min(S * 0.13, height * 0.42),
860
+ bodyH = height - (rowSteps ? labelH + pad : labelH + S * 0.09 + gap * 0.5),
861
+ copyX = rowSteps ? x + numberW + pad : x,
862
+ copyW = rowSteps ? width - numberW - pad : width;
863
+ const children: MotionLayer[] = [
864
+ text(
865
+ `step-${i + 1}-number`,
866
+ String(i + 1).padStart(2, "0"),
867
+ { x: x + numberW / 2, y: y + numberH / 2, width: numberW, height: numberH },
868
+ "number",
869
+ `${path}.steps[${i}].label`,
870
+ theme.accent,
871
+ ),
872
+ text(
873
+ `step-${i + 1}-label`,
874
+ step.label,
875
+ {
876
+ x: copyX + copyW / 2,
877
+ y: y + (rowSteps ? 0 : S * 0.09) + labelH / 2,
878
+ width: copyW,
879
+ height: labelH,
880
+ },
881
+ "label",
882
+ `${path}.steps[${i}].label`,
883
+ ),
884
+ ];
885
+ const surface = stepSurface(`step-${i + 1}`, {
886
+ x: x + width / 2,
887
+ y: y + height / 2,
888
+ width,
889
+ height,
890
+ });
891
+ if (surface) children.unshift(surface);
892
+ if (style === "editorial")
893
+ children.unshift(
894
+ rule(
895
+ `step-${i + 1}-rule`,
896
+ [
897
+ [x, y],
898
+ [x + width, y],
899
+ ],
900
+ 0.08 + i * 0.1,
901
+ ),
902
+ );
903
+ if (step.body)
904
+ children.push(
905
+ text(
906
+ `step-${i + 1}-body`,
907
+ step.body,
908
+ { x: copyX + copyW / 2, y: y + height - bodyH / 2, width: copyW, height: bodyH },
909
+ "body",
910
+ `${path}.steps[${i}].body`,
911
+ ),
912
+ );
913
+ layers.push(group(`step-${i + 1}`, children, 0.12 * (i + 1)));
914
+ });
915
+ }
916
+ const transition = recipe.transition ?? { type: "cut" };
917
+ const exit =
918
+ recipe.motion === "off" || transition.type === "cut" ? 0 : (transition.durationS ?? 0.45);
919
+ const reading = Math.max(
920
+ 1.2,
921
+ texts.reduce((sum, value) => sum + estimateMotionReadingSeconds(value, 0), 0) + 0.4,
922
+ );
923
+ const minimum = (recipe.motion === "off" ? 0 : Math.max(latestEntrance, exit)) + reading + exit;
924
+ const derived = Math.ceil(minimum * output.fps - 1e-8) / output.fps;
925
+ const durationS = recipe.durationS ?? derived;
926
+ if (recipe.durationS !== undefined && durationS + 0.001 < minimum)
927
+ issues.push(
928
+ error(
929
+ `${path}.durationS`,
930
+ `too short for this copy and motion; allow at least ${derived.toFixed(3)}s, shorten the copy, or disable motion`,
931
+ ),
932
+ );
933
+ return {
934
+ id: recipe.id,
935
+ type: "motion",
936
+ durationS,
937
+ designSize: { width: W, height: H },
938
+ motion: recipe.motion ?? "on",
939
+ transition,
940
+ layers,
941
+ };
942
+ }
943
+
944
+ /** Expand guided recipes into ordinary editable layers. No asset I/O, capture or rendering. */
945
+ export function composeMotionStory(value: unknown): MotionStoryResult {
946
+ const issues: LaunchIssue[] = [];
947
+ if (!record(value)) return { issues: [error("$", "must be a motion story JSON object")] };
948
+ for (const key of Object.keys(value))
949
+ if (!["version", "output", "theme", "scenes", "audio", "story"].includes(key))
950
+ issues.push(error(key, "unsupported motion story field"));
951
+ issues.push(
952
+ ...validateLaunchComposition({
953
+ version: value.version,
954
+ output: value.output,
955
+ theme: value.theme,
956
+ scenes: [
957
+ { id: "header-check", type: "title", durationS: 1, lines: ["Check"], motion: "off" },
958
+ ],
959
+ }),
960
+ );
961
+ if (!Array.isArray(value.scenes) || !value.scenes.length)
962
+ issues.push(error("scenes", "must contain at least one scene or recipe"));
963
+ if (issues.length) return { issues };
964
+ const output = value.output as LaunchComposition["output"],
965
+ theme = value.theme as LaunchTheme;
966
+ const generated = new Set<number>();
967
+ const scenes = (value.scenes as unknown[]).map((raw, i) => {
968
+ if (!record(raw) || !Object.hasOwn(raw, "recipe")) return raw;
969
+ generated.add(i);
970
+ const path = `scenes[${i}]`,
971
+ recipe = parseRecipe(raw, path, issues);
972
+ if (!recipe) return raw;
973
+ if (Math.min(output.width, output.height) < 320) {
974
+ issues.push(
975
+ error(
976
+ "output",
977
+ "guided recipes require a short edge of at least 320px; use a custom motion scene for smaller output",
978
+ ),
979
+ );
980
+ return raw;
981
+ }
982
+ return buildRecipe(recipe, path, output, theme, issues);
983
+ });
984
+ if (issues.some((x) => x.severity === "error")) return { issues };
985
+ const candidate = {
986
+ version: value.version,
987
+ output,
988
+ theme,
989
+ scenes,
990
+ ...(value.audio !== undefined ? { audio: value.audio } : {}),
991
+ ...(value.story !== undefined ? { story: value.story } : {}),
992
+ };
993
+ issues.push(...validateLaunchComposition(candidate));
994
+ if (issues.some((x) => x.severity === "error")) return { issues };
995
+ let composition: LaunchComposition;
996
+ try {
997
+ composition = structuredClone(candidate) as LaunchComposition;
998
+ } catch {
999
+ return { issues: [error("$", "must contain only JSON-compatible data")] };
1000
+ }
1001
+ for (const finding of analyzeMotionQuality(composition)) {
1002
+ const sceneIndex = Number(finding.path.match(/^scenes\[(\d+)\]/)?.[1]);
1003
+ const provable =
1004
+ generated.has(sceneIndex) &&
1005
+ (finding.message.startsWith("Resting bounds") ||
1006
+ finding.message.startsWith("Text contrast") ||
1007
+ finding.message.startsWith("Text is small"));
1008
+ if (provable) {
1009
+ const contrast = finding.message.startsWith("Text contrast");
1010
+ const recipe = (value.scenes as Record<string, unknown>[])[sceneIndex]?.recipe;
1011
+ issues.push({
1012
+ ...finding,
1013
+ severity: "error",
1014
+ path: contrast ? "theme" : `scenes[${sceneIndex}]`,
1015
+ message: `${finding.message} In ${recipe} recipe scenes[${sceneIndex}], ${contrast ? "adjust brief theme.ink, theme.canvas, theme.surface or theme.accent to improve contrast" : "adjust the brief copy or output dimensions"}.`,
1016
+ });
1017
+ } else issues.push(finding);
1018
+ }
1019
+ issues.push(...analyzeLaunchStory(composition));
1020
+ return issues.some((x) => x.severity === "error") ? { issues } : { composition, issues };
1021
+ }