meo-canvas 0.2.1

Describe a picture, get image bytes back. CSS flexbox, grid and block layout on a Skia backend.
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
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
//! The entry point, and the canvas a render gives back.
//!
//! `Root` is the whole surface's front door: it carries the canvas size, the
//! renderer's options and the page root's own style, and it is the page root —
//! its children are the tree. That is the same shape the JavaScript surface
//! has, deliberately. A person moving between the two should be translating
//! syntax rather than a design.
//!
//! ```
//! use meo_canvas::{Format, Renderer, Root, Row, Styled, Text, hex, px};
//!
//! let renderer = Renderer::new();
//! let mut canvas = Root::new(520.0)
//!     .height(180.0)
//!     .background_color(hex("#101014"))
//!     .children(Row::new().padding(px(24.0)).children(Text::new("Ukasyah")))
//!     .render(&renderer)?;
//!
//! let png = canvas.to_buffer(Format::Png)?;
//! # Ok::<(), meo_canvas::BuildError>(())
//! ```
//!
//! **Resolve, measure, layout and paint happen once.** Every method on the
//! [`Canvas`] that comes back is an encode of work already done, which is why
//! two formats cost one paint.

use std::{fmt, path::Path};

use meo_canvas_core::{
    EncodeOptions, Error, ImageFormat, ImageWarning, PreparedEncode,
    RenderedCanvas, Renderer, diagnostic::Diagnostic,
};
use meo_canvas_scene::{
    OnImageError, Scene, SceneError, Size, node::HttpOptions,
};

use crate::{
    ColorSpace, ColorType, Element, IntoElements, Style, Styled,
    element::write_page,
};

/// Collects anything acceptable as children.
fn collect(children: impl IntoElements) -> Vec<Element> {
    let mut out = Vec::new();
    children.write_elements(&mut out);
    out
}

/// Where one page sits in a sequence.
///
/// The four derived numbers rather than the index alone, because each is right
/// for a different job and deriving the wrong one is a bug that looks like a
/// design choice.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PageInfo {
    /// Zero-based position in the sequence.
    pub index: u32,
    /// Total pages in this render.
    pub count: u32,
    /// Position along the sequence, `0` on the first page and `1` on the last.
    ///
    /// `index / (count - 1)`, which spans the sequence inclusively: a one-shot
    /// animation should finish at its end value on the frame the viewer stops
    /// on. The wrong curve for anything that repeats — see
    /// [`PageInfo::cycle`]. A single-page render reports `0`.
    pub progress: f32,
    /// Position around a loop, `0` on the first page and approaching `1`
    /// without reaching it.
    ///
    /// `index / count`, and the one to feed anything periodic. `1` and `0` are
    /// the same point on a circle, so driving a rotation from
    /// [`PageInfo::progress`] makes the last page a copy of the first and the
    /// animation stutters for one frame on every repeat.
    pub cycle: f32,
    /// Seconds elapsed at this page, `index / fps`.
    pub time: f32,
}

impl PageInfo {
    /// Where page `index` of `count` sits, at `fps`.
    fn new(index: u32, count: u32, fps: f32) -> Self {
        // A single page is the start of its own sequence and the whole of it at
        // once, so both curves report zero rather than dividing by no interval.
        let spread = if count > 1 {
            #[expect(
                clippy::cast_precision_loss,
                reason = "a page count large enough to lose precision in an f32 is one no renderer would finish"
            )]
            {
                (index as f32, (count - 1) as f32, count as f32)
            }
        } else {
            (0.0, 1.0, 1.0)
        };
        Self {
            index,
            count,
            progress: spread.0 / spread.1,
            cycle: spread.0 / spread.2,
            time: spread.0 / fps,
        }
    }
}

/// What went wrong between describing a canvas and painting it.
#[derive(Debug)]
#[non_exhaustive]
pub enum BuildError {
    /// The pages contradict themselves, or there are none.
    Sequence(SequenceError),
    /// The pages do not form a scene the codec can address.
    Scene(SceneError),
    /// A pass of the render failed.
    Render(Error),
    /// The bytes could not be written to the path given.
    Write(std::io::Error),
    /// A filename's extension names no format this can write.
    Format(String),
}

impl fmt::Display for BuildError {
    fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Sequence(error) => error.fmt(out),
            Self::Scene(error) => error.fmt(out),
            Self::Render(error) => error.fmt(out),
            Self::Write(error) => error.fmt(out),
            Self::Format(path) => write!(
                out,
                "cannot tell the format from {path:?}; name the file with an extension such as .png, or use `to_file_as`"
            ),
        }
    }
}

impl std::error::Error for BuildError {
    /// The error underneath, so a chain does not stop here: `anyhow`'s `{:#}`,
    /// `eyre`'s chain and any caller walking `source()` reach the
    /// `io::ErrorKind` under `Write`, the one thing a caller can act on.
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Sequence(error) => Some(error),
            Self::Scene(error) => Some(error),
            Self::Render(error) => Some(error),
            Self::Write(error) => Some(error),
            // The path is the whole of it; there is nothing underneath.
            Self::Format(_) => None,
        }
    }
}

impl From<Error> for BuildError {
    fn from(error: Error) -> Self {
        Self::Render(error)
    }
}

/// A page count that cannot mean what it says.
///
/// Refused rather than resolved by precedence. A caller who named both a page
/// count and a duration, or a count with nothing to vary per page, asked for
/// something that would not happen.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SequenceError {
    /// Both a page count and a duration were named.
    CountAndDuration,
    /// A page builder was set with no count or duration to run it over.
    BuilderWithoutLength,
    /// A count or duration was named with no page builder to vary.
    LengthWithoutBuilder,
    /// A render has at least one page.
    NoPages,
}

impl fmt::Display for SequenceError {
    fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result {
        let said = match self {
            Self::CountAndDuration => {
                "name a page count or a duration, not both; they are two spellings of one number"
            }
            Self::BuilderWithoutLength => {
                "a page builder needs `pages` or `duration`; without one there is no sequence to build"
            }
            Self::LengthWithoutBuilder => {
                "`pages` and `duration` describe a sequence, so there has to be a page builder to vary"
            }
            Self::NoPages => "a render has at least one page",
        };
        out.write_str(said)
    }
}

impl std::error::Error for SequenceError {}

/// How the pages of a render are described.
enum Content {
    /// One page, holding these children.
    Fixed(Vec<Element>),
    /// A page per call, over a sequence whose length is set separately.
    Built(Box<dyn Fn(PageInfo) -> Vec<Element>>),
}

impl fmt::Debug for Content {
    fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Fixed(children) => {
                out.debug_tuple("Fixed").field(children).finish()
            }
            Self::Built(_) => out.write_str("Built(<closure>)"),
        }
    }
}

/// The canvas to draw, and the tree to draw on it.
///
/// `Root` is the page root as well as the entry point: its style is the page's
/// style, and its children are the tree. That is why `background_color` here
/// paints the canvas rather than a box inside it.
#[derive(Debug)]
pub struct Root {
    /// Width in logical pixels.
    width: f32,
    /// Height in logical pixels, or the floor when [`Root::content_height`] is
    /// what set it.
    height: f32,
    /// What a render does when an image source cannot be resolved.
    on_image_error: OnImageError,
    http: HttpOptions,
    /// Whether the height comes from the content rather than from the caller.
    content_height: bool,
    /// Device-pixel multiplier applied at paint time.
    scale: f32,
    /// The page root's own style.
    style: Style,
    /// A name carried through for diagnostics.
    name: Option<String>,
    /// Whether to rasterise on the GPU, where the caller said.
    gpu: Option<bool>,
    /// The pixel layout to composite in, where the caller said.
    color_type: Option<ColorType>,
    /// The colour space to composite in, where the caller said.
    color_space: Option<ColorSpace>,
    /// What to draw.
    content: Content,
    /// How many pages, when a builder describes them.
    pages: Option<u32>,
    /// How long the sequence runs, in seconds.
    duration: Option<f32>,
    /// The rate a duration and a page's time are derived at.
    fps: f32,
}

impl Root {
    /// The rate a sequence is timed at when nothing says otherwise.
    pub const DEFAULT_FPS: f32 = 30.0;
    /// The scale a canvas has when nothing sets one.
    ///
    /// One device pixel per logical pixel. Not a judgement about quality: a
    /// caller rendering for a display multiplies it, and a default above one
    /// would quadruple the memory of every render that never asked.
    pub const DEFAULT_SCALE: f32 = 1.0;

    /// A canvas of the given size in logical pixels.
    ///
    /// Bare pixels rather than a [`Length`](crate::scene::Length), and
    /// deliberately: a canvas size is device-independent pixels, and a
    /// percentage of nothing has no meaning.
    ///
    /// **The height is optional and comes from the content when not set**, the
    /// same as leaving `height` out on the JavaScript surface. Add one with
    /// [`Root::height`], or a lower bound with [`Root::min_height`].
    ///
    /// Rust has no optional argument, and a second constructor taking two
    /// numbers would make the caller pick a spelling before knowing there was
    /// a choice. `Root` configures everything else -- scale, name, gpu -- by
    /// chaining, so the height does too, and the two surfaces then say the
    /// same thing: a width is required and a height is not.
    ///
    /// There is no matching form for the width. Text breaks into lines against
    /// a width, so a width has to be known before anything can be measured,
    /// while a height is a result of that measuring.
    #[must_use]
    pub const fn new(width: f32) -> Self {
        Self {
            width,
            height: 0.0,
            content_height: true,
            scale: Self::DEFAULT_SCALE,
            on_image_error: OnImageError::Placeholder,
            http: HttpOptions::new(),
            // A page lays its children out as flex, and says so: the scene's
            // default is `block`, which would stack them and stop honouring
            // `gap` and the alignments. The JavaScript surface's `Box` does
            // the same.
            style: Style::new()
                .display(meo_canvas_scene::style::layout::Display::Flex),
            name: None,
            gpu: None,
            color_type: None,
            color_space: None,
            content: Content::Fixed(Vec::new()),
            pages: None,
            duration: None,
            fps: Self::DEFAULT_FPS,
        }
    }

    /// Fixes the canvas height, instead of taking it from the content.
    #[must_use]
    pub const fn height(mut self, height: f32) -> Self {
        self.height = height;
        self.content_height = false;
        self
    }

    /// The least the canvas may be while its height comes from its content.
    ///
    /// Does nothing once [`Root::height`] has fixed a height: a floor under a
    /// number that is already stated has nothing to raise.
    #[must_use]
    pub const fn min_height(mut self, floor: f32) -> Self {
        if self.content_height {
            self.height = floor;
        }
        self
    }

    /// The device-pixel multiplier.
    ///
    /// Layout always solves at scale one, so this changes resolution and
    /// nothing else about where things sit.
    #[must_use]
    pub const fn scale(mut self, scale: f32) -> Self {
        self.scale = scale;
        self
    }

    /// A name carried through for diagnostics.
    ///
    /// Every page gets it, because every page is this root. The renderer never
    /// reads it.
    #[must_use]
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Rasterise on the GPU when there is one. `false` forces the CPU.
    ///
    /// A property of the scene rather than of the [`Renderer`], because the
    /// canvas is the thing being rasterised — `scale` lives there for the same
    /// reason. Saying nothing leaves it to the renderer, which is a different
    /// thing from saying `true`: a renderer someone set to CPU stays there.
    ///
    /// Asking is not getting. A build without GPU support, or a driver that
    /// declines, falls back — and a float [`ColorType`] forces the CPU whatever
    /// this says, since no GPU composites float. Set it `false` for output that
    /// must be identical between machines: the two rasterisers resolve
    /// anti-aliased edges a level or two apart, which a pixel comparison sees.
    #[must_use]
    pub const fn gpu(mut self, gpu: bool) -> Self {
        self.gpu = Some(gpu);
        self
    }

    /// What to do when an image source cannot be resolved.
    ///
    /// Defaults to [`OnImageError::Placeholder`]: a URL that cannot be fetched
    /// or decoded draws a neutral mark, and the failure is recorded on
    /// [`Canvas::warnings`]. The image takes the box Chrome gives a broken
    /// `<img>` with no `alt` under every setting: an `auto` axis that nothing
    /// stretches is 16 pixels, and a block-level or stretched one fills its
    /// container. A
    /// [`Path`](meo_canvas_scene::node::ImageSource::Path) or
    /// [`Bytes`](meo_canvas_scene::node::ImageSource::Bytes) source fails the
    /// render whatever this says -- the caller is holding that input and can
    /// check it before rendering, where a fetch's outcome does not exist until
    /// the render runs.
    ///
    /// [`OnImageError::Throw`] fails the render instead, for a caller whose
    /// URLs come from a manifest they control.
    ///
    /// **Every setting records the warning.** This chooses what is drawn, not
    /// what is known.
    #[must_use]
    pub const fn on_image_error(mut self, policy: OnImageError) -> Self {
        self.on_image_error = policy;
        self
    }

    /// What every URL in this scene is fetched with.
    ///
    /// The same option as npm's `RootProps.httpOptions`: a page pulling twenty
    /// images from one origin says its credential once rather than twenty
    /// times. A source's own options merge **over** these,
    /// one header name at a time, so
    /// [`Image::url_with`](crate::Image::url_with) at one source does not drop
    /// what the scene set for the rest.
    ///
    /// Only a build with the `net` feature fetches at all; without it a URL is
    /// [`Error::UnresolvedSource`] and these
    /// are carried in the scene and never sent.
    ///
    /// ```
    /// use meo_canvas::{Root, scene::HttpOptions};
    ///
    /// let root = Root::new(320.0)
    ///     .height(200.0)
    ///     .http_options(HttpOptions::new().header("authorization", "Bearer t"));
    /// ```
    #[must_use]
    pub fn http_options(mut self, http: HttpOptions) -> Self {
        self.http = http;
        self
    }

    /// The pixel layout to composite in.
    ///
    /// Governs the precision everything is drawn at and the depth the encoded
    /// formats that carry one write. Saying nothing leaves it to the renderer.
    #[must_use]
    pub const fn color_type(mut self, color_type: ColorType) -> Self {
        self.color_type = Some(color_type);
        self
    }

    /// The colour space to composite in.
    ///
    /// Fixed for the whole render rather than chosen per export: colours are
    /// interpreted in it, and one outside its gamut is clipped as it is drawn.
    /// Saying nothing leaves it to the renderer.
    #[must_use]
    pub const fn color_space(mut self, color_space: ColorSpace) -> Self {
        self.color_space = Some(color_space);
        self
    }

    /// The tree to draw, **replacing** anything already set.
    #[must_use]
    pub fn children(mut self, children: impl IntoElements) -> Self {
        self.content = Content::Fixed(collect(children));
        self
    }

    /// A tree per page, for a sequence.
    ///
    /// Needs [`pages`](Self::pages) or [`duration`](Self::duration) to say how
    /// long the sequence is. This is where the surfaces differ in syntax and
    /// not in shape: JavaScript passes a function as `children` because it can,
    /// and Rust names the two forms apart because it cannot.
    #[must_use]
    pub fn page_builder<C: IntoElements>(
        mut self,
        builder: impl Fn(PageInfo) -> C + 'static,
    ) -> Self {
        self.content =
            Content::Built(Box::new(move |page| collect(builder(page))));
        self
    }

    /// How many pages to render.
    #[must_use]
    pub const fn pages(mut self, pages: u32) -> Self {
        self.pages = Some(pages);
        self
    }

    /// How long the sequence runs, in seconds.
    ///
    /// The page count becomes `ceil(duration * fps)`.
    #[must_use]
    pub const fn duration(mut self, seconds: f32) -> Self {
        self.duration = Some(seconds);
        self
    }

    /// The rate a duration and a page's time are derived at.
    ///
    /// Describes the render, not the encode. An animation encoded to play at
    /// this rate needs it named in [`EncodeOptions`] as well.
    #[must_use]
    pub const fn fps(mut self, fps: f32) -> Self {
        self.fps = fps;
        self
    }

    /// How many pages this describes, and at what rate.
    fn sequence(&self) -> Result<(u32, f32), SequenceError> {
        let built = matches!(self.content, Content::Built(_));

        if self.pages.is_some() && self.duration.is_some() {
            return Err(SequenceError::CountAndDuration);
        }

        #[expect(
            clippy::cast_possible_truncation,
            clippy::cast_sign_loss,
            reason = "a duration long enough to overflow a u32 of pages is one no renderer would finish, and a negative one is refused as a count below one"
        )]
        let asked = self
            .pages
            .or_else(|| self.duration.map(|s| (s * self.fps).ceil() as u32));

        let Some(count) = asked else {
            if built {
                return Err(SequenceError::BuilderWithoutLength);
            }
            return Ok((1, self.fps));
        };
        if !built {
            return Err(SequenceError::LengthWithoutBuilder);
        }
        if count == 0 {
            return Err(SequenceError::NoPages);
        }
        Ok((count, self.fps))
    }

    /// Flattens this into a scene.
    ///
    /// What [`render`](Self::render) hands to the renderer, and public for a
    /// caller who wants the scene itself — to write to disk, to send over the
    /// wire, or to render more than once.
    ///
    /// # Errors
    ///
    /// Returns [`BuildError::Sequence`] when the page count contradicts itself,
    /// and [`BuildError::Scene`] when the pages together hold more nodes than
    /// the codec can address.
    pub fn into_scene(self) -> Result<(Scene, Vec<Diagnostic>), BuildError> {
        let (count, fps) = self.sequence().map_err(BuildError::Sequence)?;

        // The width came from `Root::new`, which is a `const fn` returning
        // `Self` and so has nowhere to report a number that is not a length.
        // This is where it reports: the first call that can fail.
        let size = Size::new(self.width, self.height);
        if !meo_canvas_scene::size_is_pixels(size) {
            return Err(BuildError::Scene(SceneError::canvas_size(
                self.width,
                self.height,
            )));
        }

        let mut scene = Scene::new(size);
        scene.content_height = self.content_height;
        scene.scale = self.scale;
        scene.gpu = self.gpu;
        scene.on_image_error = self.on_image_error;
        scene.http = self.http;
        scene.color_type = self.color_type;
        scene.color_space = self.color_space;

        let page_root = |children: Vec<Element>| {
            let root = Element::new(meo_canvas_scene::node::NodeKind::Box)
                .with_style(self.style.clone())
                .children(children);
            match &self.name {
                Some(name) => root.name(name.clone()),
                None => root,
            }
        };

        // One list across every page: a caller fixing their markup does not
        // care which page carried it.
        let mut found: Vec<Diagnostic> = Vec::new();

        // `Scene::new` already made one page, so the first tree styles it and
        // every later one adds its own root.
        let write = |scene: &mut Scene,
                     first: bool,
                     children: Vec<Element>,
                     found: &mut Vec<Diagnostic>|
         -> Result<(), BuildError> {
            let root = if first {
                scene
                    .root()
                    .ok_or(BuildError::Sequence(SequenceError::NoPages))?
            } else {
                scene.push_page().map_err(BuildError::Scene)?
            };
            write_page(scene, root, page_root(children), found)
                .map_err(BuildError::Scene)
        };

        match &self.content {
            Content::Fixed(children) => {
                write(&mut scene, true, children.clone(), &mut found)?;
            }
            Content::Built(builder) => {
                for index in 0..count {
                    let children = builder(PageInfo::new(index, count, fps));
                    write(&mut scene, index == 0, children, &mut found)?;
                }
            }
        }
        Ok((scene, found))
    }

    /// Paints every page and returns the canvas to encode from.
    ///
    /// The GPU request and the registered families live on the [`Renderer`]
    /// rather than here: a renderer outlives any one scene, which is why a
    /// server registers its fonts once, and carrying them in both places would
    /// be two settings that can disagree. The JavaScript surface, which
    /// exposes no renderer, puts them on `Root`.
    ///
    /// # Errors
    ///
    /// Returns whatever [`into_scene`](Self::into_scene) reports, and
    /// [`BuildError::Render`] when a pass of the render fails — a font the
    /// renderer does not hold, an image it cannot read, a URL it does not
    /// fetch.
    pub fn render(self, renderer: &Renderer) -> Result<Canvas, BuildError> {
        let (scene, mut diagnostics) = self.into_scene()?;
        let painted = renderer.render(&scene)?;
        diagnostics.extend_from_slice(painted.diagnostics());
        Ok(Canvas {
            painted,
            diagnostics,
        })
    }
}

impl Styled for Root {
    fn style_mut(&mut self) -> &mut Style {
        &mut self.style
    }
}

/// A painted canvas, and the ways to read it back.
///
/// Returned by [`Root::render`]; a caller never builds one. It holds the
/// painted surface and nothing else, so every method here is an encode of work
/// already done.
///
/// The binding is `mut` because encoding takes `&mut self` — every encode entry
/// point in the renderer beneath does, since writing a format prepares the page
/// sequence first. A signature hiding that behind interior mutability would let
/// two encodes read as independent when they are not.
#[derive(Debug)]
pub struct Canvas {
    /// The painted pages.
    painted: RenderedCanvas,
    /// What the caller wrote that could not be used: markup, raised while the
    /// scene was built, then a filter chain the render dropped. A caller who
    /// builds the scene takes the first from [`Root::into_scene`] and the
    /// second from `RenderedCanvas::diagnostics`.
    diagnostics: Vec<Diagnostic>,
}

impl Canvas {
    /// What this render's input said that could not be used: a markup tag, or
    /// a `filter` or `backdrop_filter` chain the renderer refuses, which is
    /// drawn as though it were not set.
    ///
    /// **Separate from an image warning**, which reports that the world did
    /// not answer. A diagnostic reports that the input did not say what it
    /// meant, which is the caller's to fix rather than the network's.
    #[must_use]
    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }

    /// Encodes the canvas and returns the bytes.
    ///
    /// # Errors
    ///
    /// Returns [`BuildError::Render`] when the encoder refuses the surface.
    pub fn to_buffer(
        &mut self,
        format: ImageFormat,
    ) -> Result<Vec<u8>, BuildError> {
        self.to_buffer_with(format, &EncodeOptions::default())
    }

    /// Encodes the canvas with quality and container settings.
    ///
    /// # Errors
    ///
    /// Returns [`BuildError::Render`] when the encoder refuses the surface or
    /// the options do not fit the painted pages.
    pub fn to_buffer_with(
        &mut self,
        format: ImageFormat,
        options: &EncodeOptions,
    ) -> Result<Vec<u8>, BuildError> {
        Ok(self.painted.to_buffer(format, options)?)
    }

    /// Takes the half of an encode that needs this canvas, and stops there.
    ///
    /// The returned [`PreparedEncode`] is [`Send`] where this canvas is not,
    /// so the expensive half of an export runs wherever the caller puts it
    /// while this canvas stays on the thread that owns it. At 4000x4000 that
    /// is 97 ms of the 100 a `to_buffer` costs.
    ///
    /// The JavaScript surface's `toBuffer` settles off the event loop for the
    /// same reason a caller serving requests from a thread pool wants this.
    ///
    /// One handle is one format: encoding a second format is a second call,
    /// made here rather than on the worker.
    ///
    /// ```
    /// use meo_canvas::{EncodeOptions, Format, Renderer, Root};
    ///
    /// let renderer = Renderer::new();
    /// let mut canvas = Root::new(64.0).height(32.0).render(&renderer)?;
    ///
    /// let pending =
    ///     canvas.prepare_encode(Format::Png, &EncodeOptions::default())?;
    /// let png = std::thread::spawn(move || pending.encode())
    ///     .join()
    ///     .expect("the encoding thread")?;
    /// assert!(!png.bytes.is_empty());
    /// # Ok::<(), meo_canvas::BuildError>(())
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`BuildError::Render`] when the options do not fit the painted
    /// pages. Failures that belong to encoding itself are raised by
    /// [`PreparedEncode::encode`].
    pub fn prepare_encode(
        &mut self,
        format: ImageFormat,
        options: &EncodeOptions,
    ) -> Result<PreparedEncode, BuildError> {
        Ok(self.painted.prepare_encode(format, options)?)
    }

    /// Encodes the canvas and writes it to `path`, taking the format from the
    /// extension.
    ///
    /// `canvas.to_file("out.png")`, the same call a JavaScript caller writes,
    /// and it accepts the same extensions — `.raw` among them. An extension
    /// naming no format is an error rather than a default: writing a PNG
    /// because nothing said otherwise turns a typo into a file whose name lies
    /// about its contents.
    ///
    /// Resolved through [`ImageFormat::from_named`] rather than
    /// `from_extension`, which refuses `raw` — correctly, for a filename found
    /// on disk, and wrongly for one the caller has just typed.
    ///
    /// # Errors
    ///
    /// Returns [`BuildError::Format`] when the extension names no format, and
    /// [`BuildError::Render`] or [`BuildError::Write`] when the encode or the
    /// write fails.
    pub fn to_file(
        &mut self,
        path: impl AsRef<Path>,
    ) -> Result<(), BuildError> {
        let path = path.as_ref();
        let format = path
            .extension()
            .and_then(|extension| extension.to_str())
            .and_then(ImageFormat::from_named)
            .ok_or_else(|| BuildError::Format(path.display().to_string()))?;
        self.to_file_with(path, format, &EncodeOptions::default())
    }

    /// Encodes the canvas in the named format and writes it to `path`.
    ///
    /// # Errors
    ///
    /// As [`Canvas::to_file_with`], whose notes on the two error variants
    /// apply here too.
    pub fn to_file_as(
        &mut self,
        path: impl AsRef<Path>,
        format: ImageFormat,
    ) -> Result<(), BuildError> {
        self.to_file_with(path, format, &EncodeOptions::default())
    }

    /// Encodes the canvas with options and writes it to `path`.
    ///
    /// **The bytes are written where they are encoded**: a format that gathers
    /// every page streams into the file rather than existing whole in memory
    /// first.
    ///
    /// # Why the path is opened before the encode
    ///
    /// So a path that cannot be written is still [`BuildError::Write`] with
    /// its [`std::io::ErrorKind`] intact — a missing directory, a permission
    /// refusal, a read-only filesystem. The renderer folds a write failure
    /// into an encode error carrying a message rather than a kind, and the
    /// kind is what a caller can act on. It also fails sooner. A failure
    /// *during* the write — a full disk — still arrives as
    /// [`BuildError::Render`].
    ///
    /// # Why the probe neither truncates nor leaves a file behind
    ///
    /// **A failed encode must not destroy the file already there.** An
    /// existing path is opened for writing without truncating; truncation
    /// happens in the renderer, on the success path. A path that does not
    /// exist is created with `create_new` and removed again, so a failed encode
    /// leaves nothing. That removal's error is the one swallowed here: it can
    /// only leave behind the empty file this call has just created.
    ///
    /// # Errors
    ///
    /// Returns [`BuildError::Write`] when the path cannot be opened for
    /// writing, and [`BuildError::Render`] when the options do not suit the
    /// format, when the encoder refuses the surface, or when the write fails
    /// after it has begun.
    pub fn to_file_with(
        &mut self,
        path: impl AsRef<Path>,
        format: ImageFormat,
        options: &EncodeOptions,
    ) -> Result<(), BuildError> {
        let path = path.as_ref();
        // Opened and dropped, not written through: the renderer opens the path
        // itself. This is here to ask the filesystem the question while the
        // answer is still an `io::Error`. See the notes above for why it does
        // not truncate and why it cleans up after itself.
        match std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(path)
        {
            Ok(file) => {
                drop(file);
                drop(std::fs::remove_file(path));
            }
            Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
                drop(
                    std::fs::OpenOptions::new()
                        .write(true)
                        .open(path)
                        .map_err(BuildError::Write)?,
                );
            }
            Err(error) => return Err(BuildError::Write(error)),
        }
        Ok(self.painted.prepare_encode(format, options)?.write(path)?)
    }

    /// Encodes the canvas and returns a `data:` URL.
    ///
    /// # Errors
    ///
    /// Returns [`BuildError::Render`] when the encoder refuses the surface.
    pub fn to_url(
        &mut self,
        format: ImageFormat,
    ) -> Result<String, BuildError> {
        let bytes = self.to_buffer(format)?;
        Ok(format!(
            "data:{};base64,{}",
            format.media_type(),
            base64(&bytes)
        ))
    }

    /// The `HTMLCanvasElement` spelling of [`to_url`](Self::to_url).
    ///
    /// Taking a quality rather than an options object, because the DOM method
    /// it is named after does.
    ///
    /// # Errors
    ///
    /// Returns [`BuildError::Render`] when the encoder refuses the surface.
    pub fn to_data_url(
        &mut self,
        format: ImageFormat,
        quality: Option<f32>,
    ) -> Result<String, BuildError> {
        let options = EncodeOptions {
            quality,
            ..EncodeOptions::default()
        };
        let bytes = self.to_buffer_with(format, &options)?;
        Ok(format!(
            "data:{};base64,{}",
            format.media_type(),
            base64(&bytes)
        ))
    }

    /// How many pages were painted.
    #[must_use]
    pub fn page_count(&self) -> usize {
        self.painted.page_count()
    }

    /// The device-pixel multiplier the pages were drawn at.
    #[must_use]
    pub const fn scale(&self) -> f32 {
        self.painted.scale()
    }

    /// Every image source that could not be resolved, in node order.
    ///
    /// **Empty is the ordinary answer and always safe to ask for**, so
    /// `warnings().is_empty()` needs no guard. One entry per distinct source
    /// rather than per node.
    ///
    /// Populated whatever [`Root::on_image_error`] chose, including
    /// [`OnImageError::Ignore`]: a caller who finds the mark distracting keeps
    /// the diagnostic, because turning the drawing off must not turn the
    /// knowing off.
    #[must_use]
    pub fn warnings(&self) -> &[ImageWarning] {
        self.painted.warnings()
    }

    /// Whether the GPU was asked for.
    ///
    /// **Asking is not getting** -- compare [`Canvas::engine`]. This is what
    /// [`Root::gpu`] was told, and it is `true` when nothing was said, because
    /// that is the renderer's own default.
    #[must_use]
    pub const fn gpu(&self) -> bool {
        self.painted.gpu()
    }

    /// Which rasteriser drew the pages: `"gpu"` or `"cpu"`.
    ///
    /// The outcome rather than the request, and they disagree: a build with no
    /// GPU backend compiled, a machine with no device, a driver that declines,
    /// and a float `color_type` all rasterise on the CPU whatever `gpu` says.
    /// **Without this a caller who asks for the GPU and gets the CPU has no way
    /// to find out**, and neither has a test. The JavaScript surface's canvas
    /// reports the same.
    #[must_use]
    pub fn engine(&self) -> &'static str {
        self.painted.engine()
    }
}

/// Base64, for a `data:` URL.
fn base64(bytes: &[u8]) -> String {
    const ALPHABET: &[u8; 64] =
        b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
    for chunk in bytes.chunks(3) {
        let (a, b, c) = (
            u32::from(chunk[0]),
            chunk.get(1).map_or(0, |byte| u32::from(*byte)),
            chunk.get(2).map_or(0, |byte| u32::from(*byte)),
        );
        let triple = (a << 16) | (b << 8) | c;

        out.push(ALPHABET[((triple >> 18) & 63) as usize] as char);
        out.push(ALPHABET[((triple >> 12) & 63) as usize] as char);
        out.push(if chunk.len() > 1 {
            ALPHABET[((triple >> 6) & 63) as usize] as char
        } else {
            '='
        });
        out.push(if chunk.len() > 2 {
            ALPHABET[(triple & 63) as usize] as char
        } else {
            '='
        });
    }
    out
}

#[cfg(test)]
mod tests {
    use meo_canvas_scene::node::NodeKind;

    use super::{BuildError, PageInfo, Root, SequenceError};
    use crate::{
        Box, ColorSpace, ColorType, Column, EncodeOptions, Format, Renderer,
        Row, Styled, Text, hex_rgb, px,
    };

    #[test]
    fn a_build_error_hands_back_what_went_wrong_underneath_it() {
        // `source()` must reach the `io::ErrorKind` under `Write`: `Display`
        // forwards, so the message looks complete whether or not the chain is.
        use std::error::Error as _;

        let refused = Root::new(f32::NAN)
            .children([Box::new()])
            .into_scene()
            .err()
            .unwrap_or_else(|| unreachable!("a NaN width is refused"));
        let source = refused
            .source()
            .unwrap_or_else(|| unreachable!("the scene error is underneath"));
        assert!(
            source
                .downcast_ref::<meo_canvas_scene::SceneError>()
                .is_some(),
            "the cause came back as something other than the scene error"
        );

        // The `io::Error` case, which is the one worth reaching: a caller
        // deciding whether to retry needs the kind, not the sentence.
        let io = BuildError::Write(std::io::Error::new(
            std::io::ErrorKind::PermissionDenied,
            "no",
        ));
        let kind = io
            .source()
            .and_then(|error| error.downcast_ref::<std::io::Error>())
            .map(std::io::Error::kind);
        assert_eq!(kind, Some(std::io::ErrorKind::PermissionDenied));
    }

    #[test]
    fn a_written_file_is_the_buffer_and_a_bad_path_keeps_its_error_kind() {
        use std::error::Error as _;

        // Two claims about `to_file_with`, and the second is why it is not
        // simply `prepare_encode(..).write(..)`. The first: streaming writes
        // the same file buffering does, byte for byte.
        let renderer = Renderer::new();
        let mut canvas = Root::new(16.0)
            .height(8.0)
            .render(&renderer)
            .unwrap_or_else(|error| unreachable!("{error}"));
        let expected = canvas
            .to_buffer(Format::Png)
            .unwrap_or_else(|error| unreachable!("{error}"));

        let path = std::env::temp_dir().join("meo-canvas-root-to-file.png");
        canvas
            .to_file(&path)
            .unwrap_or_else(|error| unreachable!("{error}"));
        let written = std::fs::read(&path)
            .unwrap_or_else(|error| unreachable!("{error}"));
        assert_eq!(written, expected, "the file and the buffer differ");
        std::fs::remove_file(&path)
            .unwrap_or_else(|error| unreachable!("{error}"));

        // The second: a path that cannot be opened is still a `Write` with a
        // real `io::ErrorKind` under it. Without the file created before the
        // encode, the renderer's sentence-carrying error would arrive as
        // `Render` and the kind would be gone.
        let missing = std::env::temp_dir()
            .join("meo-canvas-no-such-directory")
            .join("out.png");
        let refused = canvas.to_file(&missing);
        let Err(error) = refused else {
            unreachable!("writing into a missing directory succeeded");
        };
        assert!(
            matches!(error, BuildError::Write(_)),
            "a missing directory reported {error:?} rather than a write error"
        );
        let kind = error
            .source()
            .and_then(|cause| cause.downcast_ref::<std::io::Error>())
            .map(std::io::Error::kind);
        assert_eq!(kind, Some(std::io::ErrorKind::NotFound));
    }

    #[test]
    fn a_refused_encode_leaves_the_previous_file_alone() {
        // The failure the probe must not cause: `File::create` truncates, so
        // probing with it would empty the path before an encode refused for an
        // ordinary reason -- `fps` on a still format -- and leave yesterday's
        // render a zero-byte file.
        let renderer = Renderer::new();
        let mut canvas = Root::new(16.0)
            .height(8.0)
            .render(&renderer)
            .unwrap_or_else(|error| unreachable!("{error}"));

        let path = std::env::temp_dir().join("meo-canvas-not-clobbered.png");
        canvas
            .to_file(&path)
            .unwrap_or_else(|error| unreachable!("{error}"));
        let before = std::fs::read(&path)
            .unwrap_or_else(|error| unreachable!("{error}"));
        assert!(!before.is_empty(), "the first write produced nothing");

        // A frame rate means nothing to a PNG, which `EncodeOptions::validate`
        // refuses before anything is drawn.
        let refused = canvas.to_file_with(
            &path,
            Format::Png,
            &EncodeOptions {
                fps: Some(30.0),
                ..EncodeOptions::default()
            },
        );
        assert!(refused.is_err(), "a frame rate on a PNG was accepted");

        let after = std::fs::read(&path)
            .unwrap_or_else(|error| unreachable!("{error}"));
        assert_eq!(
            after, before,
            "a refused encode changed the file that was already there"
        );
        std::fs::remove_file(&path)
            .unwrap_or_else(|error| unreachable!("{error}"));
    }

    #[test]
    fn a_refused_encode_leaves_no_file_where_there_was_none() {
        // The other half: the probe creates a path that does not exist, so it
        // has to remove it again. Left behind, a refused encode would put a
        // zero-byte file where it used to put nothing at all.
        let renderer = Renderer::new();
        let mut canvas = Root::new(16.0)
            .height(8.0)
            .render(&renderer)
            .unwrap_or_else(|error| unreachable!("{error}"));

        let path = std::env::temp_dir().join("meo-canvas-no-residue.png");
        drop(std::fs::remove_file(&path));

        let refused = canvas.to_file_with(
            &path,
            Format::Png,
            &EncodeOptions {
                fps: Some(30.0),
                ..EncodeOptions::default()
            },
        );
        assert!(refused.is_err(), "a frame rate on a PNG was accepted");
        assert!(
            !path.exists(),
            "a refused encode left a file at a path that had none"
        );
    }

    #[test]
    fn a_root_whose_width_is_not_a_length_is_refused() {
        // `Root::new` is a `const fn` returning `Self`, so it has nowhere to
        // report a width that is not a number of pixels. `into_scene` is the
        // first call that can, and until 5 September 2026 it did not: a root
        // of `NaN` built a scene sized `NaN by 0.0` and rendered nothing.
        for width in [f32::NAN, -100.0, f32::INFINITY] {
            let refused = Root::new(width).children([Box::new()]).into_scene();
            assert!(
                matches!(
                    refused,
                    Err(BuildError::Scene(
                        meo_canvas_scene::SceneError::CanvasSize { .. }
                    ))
                ),
                "a width of {width} was accepted"
            );
        }

        // A height set explicitly reaches the same check.
        let refused = Root::new(10.0)
            .height(-1.0)
            .children([Box::new()])
            .into_scene();
        assert!(matches!(
            refused,
            Err(BuildError::Scene(
                meo_canvas_scene::SceneError::CanvasSize { .. }
            ))
        ));

        // And zero stays a canvas, as it is on the element surface.
        assert!(Root::new(0.0).children([Box::new()]).into_scene().is_ok());
    }

    #[test]
    fn to_file_accepts_the_extensions_the_javascript_surface_does() {
        // `.raw` among them. `ImageFormat::from_extension` refuses it, rightly
        // for a filename found on disk -- a `.bin` of pixel bytes implies no
        // format -- but a path the caller has just typed is the other question.
        let dir = std::env::temp_dir()
            .join(format!("meo-to-file-{}", std::process::id()));
        std::fs::create_dir_all(&dir)
            .unwrap_or_else(|error| unreachable!("{error}"));

        let mut canvas = Root::new(8.0)
            .height(4.0)
            .children(Box::new())
            .render(&Renderer::new())
            .unwrap_or_else(|error| unreachable!("{error}"));

        for extension in ["png", "raw", "RAW"] {
            let path = dir.join(format!("out.{extension}"));
            assert!(
                canvas.to_file(&path).is_ok(),
                ".{extension} should name a format"
            );
        }
        // Eight by four at four bytes a pixel, so the bytes are the pixels and
        // nothing else -- which is the whole of what `raw` promises.
        assert_eq!(
            std::fs::metadata(dir.join("out.raw"))
                .unwrap_or_else(|error| unreachable!("{error}"))
                .len(),
            8 * 4 * 4
        );

        // `.bin` is upstream's name for the container and not a format tag, so
        // neither surface takes it. Refusing it here is the parity, not a gap.
        for extension in ["bin", "nonsense"] {
            let path = dir.join(format!("out.{extension}"));
            assert!(
                matches!(canvas.to_file(&path), Err(BuildError::Format(_))),
                ".{extension} names no format"
            );
        }

        std::fs::remove_dir_all(&dir).ok();
    }

    /// The scene `root` describes, or the failure it reports.
    fn scene_of(root: Root) -> meo_canvas_scene::Scene {
        root.into_scene()
            .map_or_else(|error| unreachable!("{error}"), |(scene, _)| scene)
    }

    #[test]
    fn a_root_takes_its_size_and_defaults_the_scale() {
        let scene = scene_of(Root::new(120.0).height(60.0));

        assert_eq!(scene.size.width.to_bits(), 120.0_f32.to_bits());
        assert_eq!(scene.size.height.to_bits(), 60.0_f32.to_bits());
        assert_eq!(scene.scale.to_bits(), Root::DEFAULT_SCALE.to_bits());
    }

    #[test]
    fn the_scale_reaches_the_scene_without_moving_the_layout() {
        // Layout always solves at one, so this changes resolution and nothing
        // about where things sit.
        let scene = scene_of(Root::new(10.0).height(10.0).scale(3.0));

        assert_eq!(scene.scale.to_bits(), 3.0_f32.to_bits());
    }

    #[test]
    fn the_root_is_the_page_rather_than_a_box_inside_it() {
        // `background_color` on `Root` paints the canvas. A wrapper node would
        // paint a box the size of the content instead, which is the bug this
        // shape exists to make impossible.
        let scene = scene_of(
            Root::new(10.0)
                .height(10.0)
                .background_color(hex_rgb(0x10_10_14))
                .children(Row::new()),
        );
        let root = scene
            .get(scene.pages[0])
            .unwrap_or_else(|| unreachable!("the scene has no root"));

        assert_eq!(root.paint.background_color, hex_rgb(0x10_10_14).into());
        assert_eq!(root.children.len(), 1);
    }

    #[test]
    fn children_take_one_or_many_and_skip_what_did_not_render() {
        let one = scene_of(Root::new(10.0).height(10.0).children(Row::new()));
        let many = scene_of(
            Root::new(10.0)
                .height(10.0)
                .children([Row::new(), Column::new()]),
        );
        let conditional = scene_of(Root::new(10.0).height(10.0).children([
            Some(Row::new()),
            None,
            Some(Column::new()),
        ]));

        let count = |scene: &meo_canvas_scene::Scene| {
            scene
                .get(scene.pages[0])
                .map_or(0, |root| root.children.len())
        };
        assert_eq!(count(&one), 1);
        assert_eq!(count(&many), 2);
        // The `None` contributes nothing rather than an empty node, which is
        // what `cond && Text(…)` means on the other surface.
        assert_eq!(count(&conditional), 2);
    }

    #[test]
    fn a_flat_setter_writes_the_same_field_the_style_does() {
        // The trait's whole claim: one list of properties, two places they can
        // be written, and no chance of the two drifting apart.
        let scene =
            scene_of(Root::new(10.0).height(10.0).children(
                Row::new().gap(px(4.0)).padding(crate::all(px(8.0))),
            ));
        let root = scene
            .get(scene.pages[0])
            .unwrap_or_else(|| unreachable!("the scene has no root"));
        let child = scene
            .get(root.children[0])
            .unwrap_or_else(|| unreachable!("the root has no child"));

        assert_eq!(child.layout.gap.0, meo_canvas_scene::Length::Points(4.0));
        assert_eq!(
            child.layout.padding.top,
            meo_canvas_scene::Length::Points(8.0)
        );
    }

    #[test]
    fn a_name_reaches_the_node_it_was_written_on() {
        // Not a style property: the scene keeps it on the node beside the kind
        // and nothing inherits it, so it does not go through `properties!`.
        let scene = scene_of(
            Root::new(10.0)
                .height(10.0)
                .name("page")
                .children(Row::new().name("card")),
        );
        let root = scene
            .get(scene.pages[0])
            .unwrap_or_else(|| unreachable!("the scene has no root"));
        let child = scene
            .get(root.children[0])
            .unwrap_or_else(|| unreachable!("the root has no child"));

        assert_eq!(root.name.as_deref(), Some("page"));
        assert_eq!(child.name.as_deref(), Some("card"));
    }

    #[test]
    fn a_name_is_absent_when_none_was_given() {
        let scene = scene_of(Root::new(10.0).height(10.0).children(Row::new()));
        let root = scene
            .get(scene.pages[0])
            .unwrap_or_else(|| unreachable!("the scene has no root"));

        assert_eq!(root.name, None);
    }

    #[test]
    fn the_surface_options_reach_the_scene_and_stay_absent_otherwise() {
        // Absent is not `false`: the renderer decides when the caller said
        // nothing, and a default written here would take that decision away
        // from a renderer someone deliberately set to CPU.
        let silent = scene_of(Root::new(10.0).height(10.0));
        let stated = scene_of(
            Root::new(10.0)
                .height(10.0)
                .gpu(false)
                .color_type(ColorType::F32)
                .color_space(ColorSpace::DisplayP3),
        );

        assert_eq!(silent.gpu, None);
        assert_eq!(silent.color_type, None);
        assert_eq!(silent.color_space, None);
        assert_eq!(stated.gpu, Some(false));
        assert_eq!(stated.color_type, Some(ColorType::F32));
        assert_eq!(stated.color_space, Some(ColorSpace::DisplayP3));
    }

    #[test]
    fn the_two_rasterisers_do_not_draw_the_same_pixels() {
        // Two real renders that must differ, which a copied flag cannot fake.
        // Text is load-bearing: the rasterisers differ only on anti-aliased
        // edges, so a plain filled box would fail this. With no GPU compiled
        // in, both are the CPU and the test says so.
        let renderer = Renderer::new();
        let rounded = || {
            Box::new()
                .size(px(120.0), px(60.0))
                .border_radius(24.0)
                .background_color(hex_rgb(0xff_ff_ff))
        };

        let mut on = Root::new(200.0)
            .height(80.0)
            .gpu(true)
            .children(rounded())
            .render(&renderer)
            .unwrap_or_else(|error| unreachable!("{error}"));
        let mut off = Root::new(200.0)
            .height(80.0)
            .gpu(false)
            .children(rounded())
            .render(&renderer)
            .unwrap_or_else(|error| unreachable!("{error}"));

        let with = on
            .to_buffer(Format::Png)
            .unwrap_or_else(|error| unreachable!("{error}"));
        let without = off
            .to_buffer(Format::Png)
            .unwrap_or_else(|error| unreachable!("{error}"));

        // Branch on the engine that drew, not the backend compiled in: a
        // headless runner compiles Vulkan, finds no device and falls back, so
        // both renders are the CPU's. There the GPU path is not verified, and
        // the `else` arm asserts only a clean fallback.
        if !cfg!(any(feature = "metal", feature = "vulkan")) {
            assert_eq!(
                on.engine(),
                "cpu",
                "no GPU backend is compiled in, so no render can have used one"
            );
        }
        if on.engine() == "gpu" {
            assert_ne!(
                with, without,
                "a GPU device drew one of these, so the two rasterisers should not agree to the byte"
            );
        } else {
            assert_eq!(
                with, without,
                "no GPU device was used, so both renders are the CPU's and must match"
            );
        }
    }

    #[test]
    fn a_page_builder_runs_once_per_page() {
        let scene =
            scene_of(Root::new(10.0).height(10.0).pages(3).page_builder(
                |page| Text::new(format!("page {}", page.index)),
            ));

        assert_eq!(scene.pages.len(), 3);
    }

    #[test]
    fn a_duration_becomes_a_page_count_at_the_rate_given() {
        // `ceil(duration * fps)`: a fraction of a page is still a page that
        // has to be drawn.
        let scene = scene_of(
            Root::new(10.0)
                .height(10.0)
                .duration(0.1)
                .fps(24.0)
                .page_builder(|_| Row::new()),
        );

        assert_eq!(scene.pages.len(), 3);
    }

    #[test]
    fn a_page_knows_where_it_sits() {
        let first = PageInfo::new(0, 4, 10.0);
        let last = PageInfo::new(3, 4, 10.0);

        // `progress` reaches one on the last page; `cycle` never does, because
        // the page after the last is the next loop's first.
        assert_eq!(first.progress.to_bits(), 0.0_f32.to_bits());
        assert_eq!(last.progress.to_bits(), 1.0_f32.to_bits());
        assert_eq!(last.cycle.to_bits(), 0.75_f32.to_bits());
        assert_eq!(last.time.to_bits(), 0.3_f32.to_bits());
    }

    #[test]
    fn a_single_page_reports_both_curves_as_zero() {
        let only = PageInfo::new(0, 1, 30.0);

        assert_eq!(only.progress.to_bits(), 0.0_f32.to_bits());
        assert_eq!(only.cycle.to_bits(), 0.0_f32.to_bits());
    }

    #[test]
    fn a_sequence_that_contradicts_itself_is_refused() {
        let both = Root::new(10.0)
            .height(10.0)
            .pages(2)
            .duration(1.0)
            .page_builder(|_| Row::new())
            .into_scene();
        let builder_alone = Root::new(10.0)
            .height(10.0)
            .page_builder(|_| Row::new())
            .into_scene();
        let length_alone = Root::new(10.0)
            .height(10.0)
            .pages(2)
            .children(Row::new())
            .into_scene();
        let none = Root::new(10.0)
            .height(10.0)
            .pages(0)
            .page_builder(|_| Row::new())
            .into_scene();

        assert!(matches!(
            both,
            Err(BuildError::Sequence(SequenceError::CountAndDuration))
        ));
        assert!(matches!(
            builder_alone,
            Err(BuildError::Sequence(SequenceError::BuilderWithoutLength))
        ));
        assert!(matches!(
            length_alone,
            Err(BuildError::Sequence(SequenceError::LengthWithoutBuilder))
        ));
        assert!(matches!(
            none,
            Err(BuildError::Sequence(SequenceError::NoPages))
        ));
    }

    #[test]
    fn a_canvas_encodes_twice_from_one_paint() {
        let renderer = Renderer::new();
        let mut canvas = Root::new(8.0)
            .height(4.0)
            .background_color(hex_rgb(0x10_10_14))
            .render(&renderer)
            .unwrap_or_else(|error| unreachable!("{error}"));

        let png = canvas
            .to_buffer(Format::Png)
            .unwrap_or_else(|error| unreachable!("{error}"));
        let jpg = canvas
            .to_buffer(Format::Jpeg)
            .unwrap_or_else(|error| unreachable!("{error}"));

        assert_eq!(&png[..8], b"\x89PNG\r\n\x1a\n");
        assert_eq!(&jpg[..2], b"\xff\xd8");
        assert_eq!(canvas.page_count(), 1);
    }

    #[test]
    fn a_filename_names_the_format_it_is_written_in() {
        // The same call a JavaScript caller writes,
        // `canvas.to_file("out.png")`. An extension naming no format is
        // refused rather than defaulted: a typo would otherwise produce
        // a file whose name lies about its contents.
        let renderer = Renderer::new();
        let mut canvas = Root::new(4.0)
            .height(4.0)
            .render(&renderer)
            .unwrap_or_else(|error| unreachable!("{error}"));

        let directory = std::env::temp_dir().join("meo-canvas-to-file");
        std::fs::create_dir_all(&directory)
            .unwrap_or_else(|error| unreachable!("{error}"));

        canvas
            .to_file(directory.join("out.png"))
            .unwrap_or_else(|error| unreachable!("{error}"));
        let written = std::fs::read(directory.join("out.png"))
            .unwrap_or_else(|error| unreachable!("{error}"));

        assert_eq!(&written[..8], b"\x89PNG\r\n\x1a\n");
        assert!(matches!(
            canvas.to_file(directory.join("out.nonsense")),
            Err(BuildError::Format(_))
        ));
        // `raw` is not inferable -- a `.bin` of pixel bytes is a file nothing
        // reads back -- so it is named rather than guessed.
        assert!(matches!(
            canvas.to_file(directory.join("out.bin")),
            Err(BuildError::Format(_))
        ));
        canvas
            .to_file_as(directory.join("out.bin"), Format::Raw)
            .unwrap_or_else(|error| unreachable!("{error}"));
    }

    #[test]
    fn a_data_url_carries_the_format_and_the_bytes() {
        let renderer = Renderer::new();
        let mut canvas = Root::new(4.0)
            .height(4.0)
            .render(&renderer)
            .unwrap_or_else(|error| unreachable!("{error}"));

        let url = canvas
            .to_url(Format::Png)
            .unwrap_or_else(|error| unreachable!("{error}"));

        assert!(url.starts_with("data:image/png;base64,"), "{url}");
        // The PNG signature, base64-encoded, is what every PNG data URL opens
        // with -- so this says the bytes are the image rather than anything.
        assert!(url.contains("iVBORw0KGgo"), "{url}");
    }

    #[test]
    fn a_page_root_holds_the_tree_rather_than_wrapping_it() {
        let scene =
            scene_of(Root::new(10.0).height(10.0).children(
                Row::new().children([Text::new("a"), Text::new("b")]),
            ));
        let root = scene
            .get(scene.pages[0])
            .unwrap_or_else(|| unreachable!("no root"));
        let row = scene
            .get(root.children[0])
            .unwrap_or_else(|| unreachable!("no row"));

        assert!(matches!(root.kind, NodeKind::Box));
        assert_eq!(row.children.len(), 2);
    }
}