oxframe 0.10.0

Procedural animation in Rust: scenes are coroutines, frames are await points
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
//! `check` and `inspect`: a storyboard, a visual-lint report, and a scene dump.
//!
//! The project binary serves these; `oxframe-cli` forwards to them.

use std::path::PathBuf;
use std::time::Instant;

use oxframe_core::{Color, Image};
use oxframe_protocol::MarkerInfo;
use oxframe_render::CpuRenderer;
use serde::Serialize;

use crate::lint::{self, Issue, Limits, Sample, Severity};
use crate::{Engine, EngineError, Project};

/// What `check` writes and how strict it is.
#[derive(Debug, Clone)]
pub struct Options {
    /// Storyboard frames at least this far apart, plus one per marker.
    pub interval: f64,
    /// Width of a storyboard cell. Height follows the project aspect.
    pub cell_width: u32,
    /// Also write each storyboard frame at full size.
    pub full_frames: bool,
    /// Lint thresholds.
    pub limits: Limits,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            interval: 1.0,
            cell_width: 320,
            full_frames: false,
            limits: Limits::default(),
        }
    }
}

/// `out/check/report.json`.
#[derive(Debug, Clone, Serialize)]
pub struct Report {
    /// Project length in seconds.
    pub duration: f64,
    /// Problems, sorted by time.
    pub issues: Vec<Issue>,
}

/// Storyboard plus the report. `frames` is empty unless [`Options::full_frames`].
pub struct Checked {
    /// The lint report.
    pub report: Report,
    /// Grid of the sampled frames.
    pub storyboard: Image,
    /// `(relative path, image)` for full-size frames.
    pub frames: Vec<(String, Image)>,
}

/// Samples the project and lints it, without drawing.
///
/// # Errors
///
/// If the project has no scenes or a meta file is invalid.
pub fn report(project: Project, opts: &Options) -> Result<Report, EngineError> {
    let mut issues = Vec::new();
    let mut duration = 0.0_f64;
    for (label, project) in variants(project) {
        let mut report = pass(&mut Engine::new(project)?, opts, false)?.0;
        stamp(&mut report.issues, label);
        duration = duration.max(report.duration);
        issues.append(&mut report.issues);
    }
    Ok(Report { duration, issues })
}

/// Samples, lints and draws the storyboard (CPU, storyboard resolution).
///
/// # Errors
///
/// If the project has no scenes or a meta file is invalid.
pub fn check(project: Project, opts: &Options) -> Result<Checked, EngineError> {
    let mut issues = Vec::new();
    let mut duration = 0.0_f64;
    let mut storyboard = None;
    let mut frames = Vec::new();
    let jobs = variants(project);
    let multi = jobs.len() > 1;
    for (label, project) in jobs {
        let mut engine = Engine::new(project)?;
        let (mut report, cells) = pass(&mut engine, opts, true)?;
        stamp(&mut report.issues, label);
        duration = duration.max(report.duration);
        issues.append(&mut report.issues);
        let board = compose(&cells, engine.project().background);
        if multi {
            let tag = label.unwrap_or("frame").replace(':', "x");
            frames.push((format!("storyboard-{tag}.png"), board.clone()));
        }
        if storyboard.is_none() {
            storyboard = Some(board);
        }
        if opts.full_frames {
            let size = engine.size();
            let mut cpu = CpuRenderer::new();
            let prefix = label
                .map(|l| format!("{}/", l.replace(':', "x")))
                .unwrap_or_default();
            for (t, marker, _) in &cells {
                let frame = frame_at(*t, engine.fps(), engine.total_frames());
                let name = format!("{prefix}{}", frame_file(*t, marker.as_deref()));
                frames.push((name, engine.render(frame, &mut cpu, size)));
            }
        }
    }
    Ok(Checked {
        report: Report { duration, issues },
        storyboard: storyboard.expect("at least one variant"),
        frames,
    })
}

/// One project per declared aspect. No aspects means the project's own size.
fn variants(project: Project) -> Vec<(Option<&'static str>, Project)> {
    if project.aspects.is_empty() {
        return vec![(None, project)];
    }
    project
        .aspects
        .clone()
        .into_iter()
        .map(|aspect| {
            let mut sized = project.clone();
            sized.size = aspect.frame(project.size);
            (Some(aspect.label()), sized)
        })
        .collect()
}

fn stamp(issues: &mut [Issue], aspect: Option<&str>) {
    if let Some(aspect) = aspect {
        for issue in issues {
            issue.aspect = Some(aspect.to_owned());
        }
    }
}

/// JSON tree of the scene at `seconds` (global time).
///
/// # Errors
///
/// If the project has no scenes or a meta file is invalid.
pub fn inspect(project: Project, seconds: f64) -> Result<serde_json::Value, EngineError> {
    inspect_value(project, seconds, false)
}

fn inspect_value(
    project: Project,
    seconds: f64,
    graph: bool,
) -> Result<serde_json::Value, EngineError> {
    let mut engine = Engine::new(project)?;
    let frame = frame_at(seconds, engine.fps(), engine.total_frames());
    let nodes = engine.seek(frame).inspect_nodes();
    let passes = graph.then(|| oxframe_2d::pass_graph(&engine.seek(frame).draw_list()));
    Ok(scene_json(
        &engine,
        seconds,
        frame,
        &nodes,
        passes.as_deref(),
    ))
}

struct When {
    t: f64,
    marker: Option<String>,
    story: bool,
}

type StoryCell = (f64, Option<String>, Image);

fn pass(
    engine: &mut Engine,
    opts: &Options,
    draw: bool,
) -> Result<(Report, Vec<StoryCell>), EngineError> {
    let fps = f64::from(engine.fps());
    let last = engine.total_frames().saturating_sub(1);
    let duration = if fps > 0.0 {
        f64::from(last) / fps
    } else {
        0.0
    };
    let mut schedule = schedule(duration, fps, &marker_times(engine), opts);
    for span in engine.transition_spans() {
        if span.frames == 0 {
            continue;
        }
        let mid = span.start + span.frames / 2;
        schedule.push(When {
            t: f64::from(mid) / fps,
            marker: Some(span.kind.to_owned()),
            story: true,
        });
    }
    schedule.sort_by(|a, b| a.t.total_cmp(&b.t));
    schedule = dedup(schedule, fps);
    let mut samples = Vec::with_capacity(schedule.len());
    let mut cells = Vec::new();
    let mut cpu = CpuRenderer::new();
    let cell = cell_size(engine.size(), opts.cell_width);
    let mut drift = Vec::new();
    for when in &schedule {
        let frame = frame_at(when.t, engine.fps(), engine.total_frames());
        if let Some((node, source)) = engine.seek(frame).draw_drift()
            && !drift.iter().any(|(_, name, _)| name == &node)
        {
            drift.push((when.t, node, source));
        }
        let nodes = engine.seek(frame).inspect_nodes();
        samples.push(Sample {
            time: when.t,
            nodes,
        });
        if draw && when.story {
            cells.push((
                when.t,
                when.marker.clone(),
                engine.render(frame, &mut cpu, cell),
            ));
        }
    }
    let has_audio =
        engine.audio().is_some() || !engine.scheduled().is_empty() || !engine.voices().is_empty();
    let mut issues = lint::lint(
        &samples,
        engine.size(),
        engine.project().background,
        has_audio,
        &opts.limits,
    );
    issues.extend(lint::voice_lines(&engine.voices()));
    for (t, node, source) in drift {
        issues.push(lint::Issue {
            rule: "nondeterministic",
            severity: lint::Severity::Error,
            time: [t, t],
            node,
            source,
            hint: "This frame differs when it is drawn twice. Draw from the time and ctx.rng(seed), not from saved numbers.".into(),
            aspect: None,
        });
    }
    issues.sort_by(|a, b| {
        a.time[0]
            .total_cmp(&b.time[0])
            .then(a.rule.cmp(b.rule))
            .then(a.node.cmp(&b.node))
    });
    Ok((Report { duration, issues }, cells))
}

fn marker_times(engine: &Engine) -> Vec<(f64, String)> {
    let fps = f64::from(engine.fps());
    let scenes = engine.scenes();
    engine
        .markers()
        .into_iter()
        .filter_map(|MarkerInfo { scene, name, time }| {
            let start = scenes.iter().find(|s| s.name == scene)?.start;
            Some((time + f64::from(start) / fps, name))
        })
        .collect()
}

fn schedule(duration: f64, fps: f64, markers: &[(f64, String)], opts: &Options) -> Vec<When> {
    let mut raw = vec![When {
        t: 0.0,
        marker: None,
        story: true,
    }];
    let step = opts.limits.sample_step.max(1.0 / 120.0);
    let mut t = 0.0;
    while t + step < duration - 1e-9 {
        t += step;
        raw.push(When {
            t,
            marker: None,
            story: false,
        });
    }
    if duration > 1e-9 {
        raw.push(When {
            t: duration,
            marker: None,
            story: true,
        });
    }
    if opts.interval > 0.0 {
        let mut t = 0.0;
        while t + opts.interval < duration - 1e-9 {
            t += opts.interval;
            raw.push(When {
                t,
                marker: None,
                story: true,
            });
        }
    }
    for (t, name) in markers {
        if *t >= -1e-6 && *t <= duration + 1e-6 {
            raw.push(When {
                t: t.clamp(0.0, duration),
                marker: Some(name.clone()),
                story: true,
            });
        }
    }
    raw.sort_by(|a, b| a.t.total_cmp(&b.t));
    dedup(raw, fps)
}

fn dedup(raw: Vec<When>, fps: f64) -> Vec<When> {
    let tol = 0.5 / fps.max(1.0);
    let mut out: Vec<When> = Vec::new();
    for w in raw {
        if let Some(prev) = out.last_mut()
            && (w.t - prev.t).abs() <= tol
        {
            prev.story |= w.story;
            if prev.marker.is_none() {
                prev.marker = w.marker;
            }
        } else {
            out.push(w);
        }
    }
    out
}

fn scene_json(
    engine: &Engine,
    seconds: f64,
    frame: u32,
    nodes: &[oxframe_2d::InspectNode],
    passes: Option<&[oxframe_2d::Pass]>,
) -> serde_json::Value {
    let (w, h) = engine.size();
    let mut value = serde_json::json!({
        "time": seconds,
        "frame": frame,
        "size": [w, h],
        "nodes": nodes.iter().map(|n| serde_json::json!({
            "id": n.id,
            "parent": n.parent,
            "kind": n.kind,
            "name": n.name,
            "source": n.source,
            "bounds": n.bounds,
            "opacity": n.opacity,
            "text": n.text,
            "order": n.order,
            "in_camera": n.in_camera,
        })).collect::<Vec<_>>(),
    });
    if let Some(tr) = engine.transition_at(frame) {
        value["transition"] = serde_json::json!({
            "kind": tr.kind,
            "dir": tr.dir,
            "progress": tr.progress,
            "overlap": tr.overlap,
        });
    }
    if let Some(passes) = passes {
        value["passes"] = serde_json::json!(
            passes
                .iter()
                .enumerate()
                .map(|(order, pass)| {
                    let name = nodes
                        .iter()
                        .find(|n| n.id == pass.node.0)
                        .map(|n| n.name.as_str());
                    serde_json::json!({
                        "order": order,
                        "kind": pass.kind.name(),
                        "node": pass.node.0,
                        "name": name,
                        "items": pass.items,
                        "depth": pass.depth,
                        "opacity": pass.opacity,
                        "blend": pass.blend,
                        "filtered": pass.filtered,
                    })
                })
                .collect::<Vec<_>>()
        );
    }
    value
}

fn frame_at(t: f64, fps: u32, total: u32) -> u32 {
    let last = total.saturating_sub(1);
    ((t.max(0.0) * f64::from(fps)).round() as u32).min(last)
}

fn cell_size(project: (u32, u32), width: u32) -> (u32, u32) {
    let width = width.max(16);
    let height =
        (f64::from(width) * f64::from(project.1) / f64::from(project.0.max(1))).round() as u32;
    (width, height.max(16))
}

fn compose(cells: &[StoryCell], _background: Color) -> Image {
    if cells.is_empty() {
        return Image {
            width: 1,
            height: 1,
            data: vec![0, 0, 0, 255],
        };
    }
    let (cw, ch) = (cells[0].2.width, cells[0].2.height);
    let cols = if cells.len() <= 4 {
        cells.len() as u32
    } else {
        4
    };
    let rows = cells.len().div_ceil(cols as usize) as u32;
    let pad = 8u32;
    let label = 18u32;
    let width = cols * cw + (cols + 1) * pad;
    let height = rows * (ch + label) + (rows + 1) * pad;
    let mut img = Image {
        width,
        height,
        data: [0x11, 0x11, 0x14, 0xff].repeat((width as usize) * (height as usize)),
    };
    for (i, (t, marker, cell)) in cells.iter().enumerate() {
        let col = i as u32 % cols;
        let row = i as u32 / cols;
        let x = pad + col * (cw + pad);
        let y = pad + row * (ch + label + pad);
        blit(&mut img, x, y, cell);
        let caption = match marker {
            Some(name) => format!("{t:.2} {name}"),
            None => format!("{t:.2}"),
        };
        draw_text(&mut img, x, y + ch + 4, &caption, [0xee, 0xee, 0xee, 0xff]);
    }
    img
}

fn blit(dst: &mut Image, x: u32, y: u32, src: &Image) {
    for row in 0..src.height {
        let dy = y + row;
        if dy >= dst.height {
            break;
        }
        let copy = src.width.min(dst.width.saturating_sub(x));
        let s = (row * src.width) as usize * 4;
        let d = ((dy * dst.width + x) as usize) * 4;
        let n = copy as usize * 4;
        dst.data[d..d + n].copy_from_slice(&src.data[s..s + n]);
    }
}

fn draw_text(img: &mut Image, x: u32, y: u32, text: &str, color: [u8; 4]) {
    let mut pen = x;
    for ch in text.chars() {
        if pen + 8 > img.width {
            break;
        }
        for (row, bits) in glyph(ch).iter().enumerate() {
            for col in 0..5 {
                if bits & (1u8 << (4 - col)) == 0 {
                    continue;
                }
                put(img, pen + col, y + row as u32, color);
            }
        }
        pen += 6;
    }
}

fn put(img: &mut Image, x: u32, y: u32, color: [u8; 4]) {
    if x >= img.width || y >= img.height {
        return;
    }
    let i = ((y * img.width + x) as usize) * 4;
    img.data[i..i + 4].copy_from_slice(&color);
}

/// 5×7 glyphs, top row first, bit 4 is the left pixel.
fn glyph(c: char) -> [u8; 7] {
    match c.to_ascii_lowercase() {
        '0' => [
            0b01110, 0b10001, 0b10011, 0b10101, 0b11001, 0b10001, 0b01110,
        ],
        '1' => [
            0b00100, 0b01100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110,
        ],
        '2' => [
            0b01110, 0b10001, 0b00001, 0b00010, 0b00100, 0b01000, 0b11111,
        ],
        '3' => [
            0b11110, 0b00001, 0b00001, 0b01110, 0b00001, 0b00001, 0b11110,
        ],
        '4' => [
            0b00010, 0b00110, 0b01010, 0b10010, 0b11111, 0b00010, 0b00010,
        ],
        '5' => [
            0b11111, 0b10000, 0b11110, 0b00001, 0b00001, 0b10001, 0b01110,
        ],
        '6' => [
            0b00110, 0b01000, 0b10000, 0b11110, 0b10001, 0b10001, 0b01110,
        ],
        '7' => [
            0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b01000, 0b01000,
        ],
        '8' => [
            0b01110, 0b10001, 0b10001, 0b01110, 0b10001, 0b10001, 0b01110,
        ],
        '9' => [
            0b01110, 0b10001, 0b10001, 0b01111, 0b00001, 0b00010, 0b01100,
        ],
        'a' => [
            0b00000, 0b01110, 0b00001, 0b01111, 0b10001, 0b10001, 0b01111,
        ],
        'b' => [
            0b10000, 0b10000, 0b11110, 0b10001, 0b10001, 0b10001, 0b11110,
        ],
        'c' => [
            0b00000, 0b01110, 0b10000, 0b10000, 0b10000, 0b10001, 0b01110,
        ],
        'd' => [
            0b00001, 0b00001, 0b01111, 0b10001, 0b10001, 0b10001, 0b01111,
        ],
        'e' => [
            0b00000, 0b01110, 0b10001, 0b11111, 0b10000, 0b10001, 0b01110,
        ],
        'f' => [
            0b00110, 0b01001, 0b01000, 0b11100, 0b01000, 0b01000, 0b01000,
        ],
        'g' => [
            0b01111, 0b10001, 0b10001, 0b01111, 0b00001, 0b10001, 0b01110,
        ],
        'h' => [
            0b10000, 0b10000, 0b11110, 0b10001, 0b10001, 0b10001, 0b10001,
        ],
        'i' => [
            0b00100, 0b00000, 0b01100, 0b00100, 0b00100, 0b00100, 0b01110,
        ],
        'j' => [
            0b00010, 0b00000, 0b00010, 0b00010, 0b00010, 0b10010, 0b01100,
        ],
        'k' => [
            0b10000, 0b10010, 0b10100, 0b11000, 0b10100, 0b10010, 0b10001,
        ],
        'l' => [
            0b01100, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110,
        ],
        'm' => [
            0b00000, 0b11010, 0b10101, 0b10101, 0b10001, 0b10001, 0b10001,
        ],
        'n' => [
            0b00000, 0b11110, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001,
        ],
        'o' => [
            0b00000, 0b01110, 0b10001, 0b10001, 0b10001, 0b10001, 0b01110,
        ],
        'p' => [
            0b11110, 0b10001, 0b10001, 0b11110, 0b10000, 0b10000, 0b10000,
        ],
        'q' => [
            0b01111, 0b10001, 0b10001, 0b01111, 0b00001, 0b00001, 0b00001,
        ],
        'r' => [
            0b00000, 0b10110, 0b11001, 0b10000, 0b10000, 0b10000, 0b10000,
        ],
        's' => [
            0b00000, 0b01111, 0b10000, 0b01110, 0b00001, 0b10001, 0b11110,
        ],
        't' => [
            0b01000, 0b01000, 0b11100, 0b01000, 0b01000, 0b01001, 0b00110,
        ],
        'u' => [
            0b00000, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01111,
        ],
        'v' => [
            0b00000, 0b10001, 0b10001, 0b10001, 0b10001, 0b01010, 0b00100,
        ],
        'w' => [
            0b00000, 0b10001, 0b10001, 0b10101, 0b10101, 0b10101, 0b01010,
        ],
        'x' => [
            0b00000, 0b10001, 0b01010, 0b00100, 0b01010, 0b10001, 0b10001,
        ],
        'y' => [
            0b10001, 0b10001, 0b10001, 0b01111, 0b00001, 0b10001, 0b01110,
        ],
        'z' => [
            0b00000, 0b11111, 0b00010, 0b00100, 0b01000, 0b10000, 0b11111,
        ],
        '.' => [0, 0, 0, 0, 0, 0b01100, 0b01100],
        ':' => [0, 0b01100, 0b01100, 0, 0b01100, 0b01100, 0],
        '-' | '_' => [0, 0, 0, 0b11111, 0, 0, 0],
        ' ' => [0; 7],
        _ => [
            0b11111, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b11111,
        ],
    }
}

fn frame_file(t: f64, marker: Option<&str>) -> String {
    match marker {
        Some(name) => format!("frames/{t:.2}-{name}.png"),
        None => format!("frames/{t:.2}.png"),
    }
}

/// `check` subcommand of a project binary.
pub fn command_check(project: Project, args: impl Iterator<Item = String>) -> i32 {
    let opts = match parse_check(args) {
        Ok(o) => o,
        Err(e) => return crate::cli::usage_error(&e),
    };
    let started = Instant::now();
    let checked = match check(project, &opts.spec) {
        Ok(c) => c,
        Err(e) => {
            eprintln!("error: {e}");
            return crate::cli::EXIT_ERROR;
        }
    };
    if let Err(e) = write_output(&opts.out, &checked) {
        eprintln!("error: {e}");
        return crate::cli::EXIT_ENV;
    }
    let errors = checked
        .report
        .issues
        .iter()
        .filter(|i| i.severity == Severity::Error)
        .count();
    for issue in &checked.report.issues {
        eprintln!("{}", format_issue(issue));
    }
    eprintln!(
        "check: {} issue(s), {} error(s), {:.2} s → {}/storyboard.png",
        checked.report.issues.len(),
        errors,
        started.elapsed().as_secs_f64(),
        opts.out.display()
    );
    println!("{}/report.json", opts.out.display());
    println!("{}/storyboard.png", opts.out.display());
    if errors > 0 {
        crate::cli::EXIT_ERROR
    } else {
        crate::cli::EXIT_OK
    }
}

/// `inspect` subcommand of a project binary.
pub fn command_inspect(project: Project, args: impl Iterator<Item = String>) -> i32 {
    let mut at = None;
    let mut scene = None;
    let mut graph = false;
    let mut args = args;
    while let Some(flag) = args.next() {
        let mut value = || args.next().ok_or_else(|| format!("{flag} needs a value"));
        match flag.as_str() {
            "--at" => {
                let v = match value() {
                    Ok(v) => v,
                    Err(e) => return crate::cli::usage_error(&e),
                };
                at = Some(match v.parse::<f64>() {
                    Ok(t) => t,
                    Err(_) => return crate::cli::usage_error(&format!("bad time `{v}`")),
                });
            }
            "--scene" => match value() {
                Ok(v) => scene = Some(v),
                Err(e) => return crate::cli::usage_error(&e),
            },
            "--graph" => graph = true,
            other => return crate::cli::usage_error(&format!("unknown option `{other}`")),
        }
    }
    let Some(mut at) = at else {
        return crate::cli::usage_error("inspect needs --at <seconds>");
    };
    let engine = match Engine::new(project) {
        Ok(e) => e,
        Err(e) => {
            eprintln!("error: {e}");
            return crate::cli::EXIT_ERROR;
        }
    };
    if let Some(name) = &scene {
        let Some(info) = engine.scenes().into_iter().find(|s| &s.name == name) else {
            eprintln!("error: no scene named `{name}`");
            return crate::cli::EXIT_ERROR;
        };
        at += f64::from(info.start) / f64::from(engine.fps());
    }
    match inspect_engine(engine, at, graph) {
        Ok(json) => {
            println!("{json}");
            crate::cli::EXIT_OK
        }
        Err(e) => {
            eprintln!("error: {e}");
            crate::cli::EXIT_ERROR
        }
    }
}

fn inspect_engine(engine: Engine, seconds: f64, graph: bool) -> Result<String, EngineError> {
    let mut engine = engine;
    let frame = frame_at(seconds, engine.fps(), engine.total_frames());
    let nodes = engine.seek(frame).inspect_nodes();
    let passes = graph.then(|| oxframe_2d::pass_graph(&engine.seek(frame).draw_list()));
    let value = scene_json(&engine, seconds, frame, &nodes, passes.as_deref());
    Ok(serde_json::to_string_pretty(&value).expect("json"))
}

struct CheckArgs {
    out: PathBuf,
    spec: Options,
}

fn parse_check(mut args: impl Iterator<Item = String>) -> Result<CheckArgs, String> {
    let mut out = PathBuf::from("out/check");
    let mut spec = Options::default();
    while let Some(flag) = args.next() {
        let mut value = || args.next().ok_or_else(|| format!("{flag} needs a value"));
        match flag.as_str() {
            "--out" => out = value()?.into(),
            "--interval" => spec.interval = num(&value()?, "interval")?,
            "--safe-inset" => spec.limits.safe_inset = num(&value()?, "safe-inset")?,
            "--min-contrast" => spec.limits.min_contrast = num(&value()?, "min-contrast")?,
            "--min-motion" => spec.limits.min_motion = num(&value()?, "min-motion")?,
            "--max-still" => spec.limits.max_still = num(&value()?, "max-still")?,
            "--chars-per-second" => {
                spec.limits.chars_per_second = num(&value()?, "chars-per-second")?;
            }
            "--min-chars" => {
                spec.limits.min_chars = num(&value()?, "min-chars")? as usize;
            }
            "--cell-width" => spec.cell_width = num(&value()?, "cell-width")? as u32,
            "--frames" => spec.full_frames = true,
            other => return Err(format!("unknown option `{other}`")),
        }
    }
    Ok(CheckArgs { out, spec })
}

fn num(v: &str, name: &str) -> Result<f64, String> {
    v.parse().map_err(|_| format!("bad {name} `{v}`"))
}

fn write_output(dir: &std::path::Path, checked: &Checked) -> std::io::Result<()> {
    std::fs::create_dir_all(dir)?;
    std::fs::write(
        dir.join("report.json"),
        serde_json::to_string_pretty(&checked.report).expect("json"),
    )?;
    std::fs::write(dir.join("storyboard.png"), checked.storyboard.encode_png())?;
    for (rel, image) in &checked.frames {
        let path = dir.join(rel);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        std::fs::write(path, image.encode_png())?;
    }
    Ok(())
}

fn format_issue(issue: &Issue) -> String {
    let where_ = issue
        .source
        .as_deref()
        .map(|s| format!(" ({s})"))
        .unwrap_or_default();
    let aspect = issue
        .aspect
        .as_deref()
        .map(|a| format!(" [{a}]"))
        .unwrap_or_default();
    format!(
        "{} {} {:.2}..{:.2} {}{where_}: {}{aspect}",
        match issue.severity {
            Severity::Error => "error",
            Severity::Warning => "warning",
        },
        issue.rule,
        issue.time[0],
        issue.time[1],
        issue.node,
        issue.hint
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{BoxFuture, Custom, NodeBuilder, Particles, SceneDef, SourceLocation, View};
    use oxframe_core::Color;
    use oxframe_protocol::Meta;
    use std::collections::BTreeMap;

    fn scene(name: &'static str, factory: fn(View) -> BoxFuture) -> SceneDef {
        SceneDef::new(
            name,
            SourceLocation {
                file: "src/scene.rs",
                manifest_dir: env!("CARGO_MANIFEST_DIR"),
            },
            factory,
        )
    }

    fn project(factory: fn(View) -> BoxFuture) -> Project {
        Project::new("check")
            .scene(scene("main", factory))
            .write_meta(false)
            .inline_meta(BTreeMap::<String, Meta>::new())
    }

    fn tokens(view: View) -> BoxFuture {
        Box::pin(async move {
            let _ = view.add(crate::Txt::new().text("Hi").name("title"));
            let _ = view.add(
                crate::Rect::new()
                    .size(80.0)
                    .fill(crate::Accent)
                    .name("mark"),
            );
            crate::wait_for(0.2).await;
        })
    }

    fn hold(view: View) -> BoxFuture {
        Box::pin(async move {
            let _ = view.add(
                crate::Rect::new()
                    .size(120.0)
                    .fill(Color::WHITE)
                    .name("box"),
            );
            crate::wait_for(0.5).await;
        })
    }

    fn parked(view: View) -> BoxFuture {
        Box::pin(async move {
            let _ = view.add(
                crate::Rect::new()
                    .size(80.0)
                    .x(1400.0)
                    .fill(Color::WHITE)
                    .name("gone"),
            );
            crate::wait_for(1.0).await;
        })
    }

    fn long(view: View) -> BoxFuture {
        Box::pin(async move {
            let r = view.add(crate::Rect::new().size(80.0).fill(Color::WHITE).name("box"));
            r.x().to(200.0, 30.0).await;
        })
    }

    #[test]
    fn check_runs_every_declared_aspect() {
        fn wide(view: View) -> BoxFuture {
            Box::pin(async move {
                let _ = view.add(
                    crate::Rect::new()
                        .size(40.0)
                        .x(700.0)
                        .fill(Color::WHITE)
                        .name("side"),
                );
                crate::wait_for(0.5).await;
            })
        }
        let report = report(
            project(wide).aspects([crate::Aspect::Landscape, crate::Aspect::Portrait]),
            &Options::default(),
        )
        .unwrap();
        let portrait = report
            .issues
            .iter()
            .any(|i| i.rule == "out_of_frame" && i.aspect.as_deref() == Some("9:16"));
        let landscape = report
            .issues
            .iter()
            .any(|i| i.rule == "out_of_frame" && i.aspect.as_deref() == Some("16:9"));
        assert!(portrait, "{:?}", report.issues);
        assert!(!landscape, "{:?}", report.issues);
    }

    #[test]
    fn clean_scene_has_no_errors_and_a_storyboard() {
        let checked = check(project(hold), &Options::default()).unwrap();
        assert!(
            checked
                .report
                .issues
                .iter()
                .all(|i| i.severity != Severity::Error)
        );
        assert!(checked.storyboard.width > 100 && checked.storyboard.data.len() > 1000);
    }

    #[test]
    fn parked_node_is_an_error() {
        let report = report(project(parked), &Options::default()).unwrap();
        assert!(
            report
                .issues
                .iter()
                .any(|i| i.rule == "out_of_frame" && i.node == "gone")
        );
    }

    #[test]
    fn custom_that_keeps_a_counter_fails_check() {
        fn drift(view: View) -> BoxFuture {
            Box::pin(async move {
                let n = std::cell::Cell::new(0.0);
                let _ = view.add(
                    Custom::new(move |ctx, _| {
                        let x = n.get();
                        n.set(x + 1.0);
                        ctx.circle((x, 0.0), 6.0).fill(Color::WHITE);
                    })
                    .size(40.0, 40.0)
                    .name("drift"),
                );
                crate::wait_for(0.2).await;
            })
        }
        let report = report(project(drift), &Options::default()).unwrap();
        assert!(
            report
                .issues
                .iter()
                .any(|i| i.rule == "nondeterministic" && i.node == "drift"),
            "{:?}",
            report.issues
        );
    }

    #[test]
    fn spray_seeks_back_to_the_same_frame() {
        fn spray(view: View) -> BoxFuture {
            Box::pin(async move {
                let _ = view.add(
                    Custom::new(|ctx, t| {
                        let mut rng = ctx.rng(3);
                        let y = (rng.range(0.0..40.0) + t * 20.0) % 40.0;
                        ctx.circle((0.0, y), 5.0).fill(Color::WHITE);
                    })
                    .size(20.0, 80.0)
                    .name("rain"),
                );
                let p = view.add(
                    Particles::new()
                        .seed(7)
                        .gravity(400.0)
                        .life(1.0)
                        .fill(Color::WHITE)
                        .toward_camera()
                        .name("spray"),
                );
                p.burst((0.0, 0.0), 6).await;
                crate::wait_for(0.4).await;
            })
        }
        let mut engine = crate::Engine::new(project(spray)).unwrap();
        let frame = engine.total_frames() / 2;
        let paint = |engine: &mut crate::Engine, frame: u32| {
            engine
                .seek(frame)
                .draw_list()
                .iter()
                .map(|item| format!("{:?}", item.prim))
                .collect::<Vec<_>>()
        };
        let first = paint(&mut engine, frame);
        engine.seek(0);
        let second = paint(&mut engine, frame);
        assert_eq!(first, second);
        assert!(engine.seek(frame).draw_drift().is_none());
    }

    #[cfg(feature = "gpu")]
    #[test]
    fn custom_circle_matches_on_cpu_and_gpu() {
        fn dot(view: View) -> BoxFuture {
            Box::pin(async move {
                let _ = view.add(
                    Custom::new(|ctx, _| {
                        ctx.circle((0.0, 0.0), 30.0).fill(Color::WHITE);
                    })
                    .size(80.0, 80.0),
                );
                crate::wait_for(0.05).await;
            })
        }
        let mut engine = crate::Engine::new(project(dot)).unwrap();
        let size = engine.size();
        let cpu = engine.render(0, &mut crate::render::CpuRenderer::new(), size);
        let mut gpu = match crate::render::GpuRenderer::new() {
            Ok(g) if g.is_software() && std::env::var_os("OXFRAME_GPU_TESTS").is_none() => return,
            Ok(g) => g,
            Err(_) => return,
        };
        let gpu_img = engine.render(0, &mut gpu, size);
        let px = |img: &crate::render::Image, x, y| {
            let i = ((y * img.width + x) * 4) as usize;
            [
                img.data[i],
                img.data[i + 1],
                img.data[i + 2],
                img.data[i + 3],
            ]
        };
        let (cx, cy) = (size.0 / 2, size.1 / 2);
        assert_eq!(px(&cpu, cx, cy), px(&gpu_img, cx, cy));
    }

    #[test]
    fn thirty_seconds_stays_under_five() {
        let started = Instant::now();
        let checked = check(project(long), &Options::default()).unwrap();
        let elapsed = started.elapsed().as_secs_f64();
        assert!(checked.storyboard.width > 0);
        assert!(elapsed < 5.0, "check of a 30 s scene took {elapsed:.2} s");
    }

    #[test]
    fn theme_recolors_tokens_and_text() {
        use crate::{Engine, Theme};
        let sample = |theme: Theme| {
            let mut engine = Engine::new(project(tokens).theme(theme)).unwrap();
            let nodes = engine.seek(0).inspect_nodes();
            let fill = |name: &str| {
                nodes
                    .iter()
                    .find(|n| n.name == name)
                    .unwrap()
                    .fill
                    .to_rgba8()
            };
            (fill("title"), fill("mark"))
        };
        let dark = sample(Theme::dark());
        let light = sample(Theme::light());
        assert_eq!(dark.0, Color::WHITE.to_rgba8());
        assert_ne!(dark.0, light.0, "text follows the theme");
        assert_ne!(dark.1, light.1, "Accent follows the theme");
        assert_eq!(dark.1, Theme::dark().accent.to_rgba8());
    }

    #[test]
    fn inspect_names_the_node() {
        let json = inspect(project(hold), 0.1).unwrap();
        let nodes = json["nodes"].as_array().unwrap();
        assert!(
            nodes
                .iter()
                .any(|n| n["name"] == "box" && n["kind"] == "Rect")
        );
        assert!(json.get("passes").is_none());
    }

    #[test]
    fn inspect_graph_lists_passes_only_when_asked() {
        fn soft(view: View) -> BoxFuture {
            Box::pin(async move {
                let _ = view.add(
                    crate::Rect::new()
                        .size(40.0)
                        .fill(Color::WHITE)
                        .blur(2.0)
                        .name("soft"),
                );
                crate::wait_for(0.2).await;
            })
        }
        let plain = inspect(project(hold), 0.1).unwrap();
        assert!(plain.get("passes").is_none());
        let json = inspect_value(project(soft), 0.1, true).unwrap();
        let passes = json["passes"].as_array().unwrap();
        assert!(passes.iter().any(|p| p["kind"] == "vector"));
        assert!(
            passes
                .iter()
                .any(|p| p["kind"] == "composite" && p["filtered"] == true)
        );
    }
}