decal 0.6.0

Declarative DSL for describing scenes and rendering them to SVG or PNG
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
use crate::{
    layout::{
        FontRegistry,
        ImageCache,
        ImageOptions,
        Node,
        NodeId,
        NodeKind,
        RasterizeOptions,
        RenderContext,
        SvgDimensions,
        Typography,
        VectorizeError,
        VectorizeOptions,
    },
    paint::Resources,
    prelude::ViewBox,
    primitives::Size,
    utils::ElementWriter,
};
use parking_lot::Mutex;
use resvg::render;
use smallvec::SmallVec;
use std::{
    fmt::Write,
    sync::Arc,
};
#[cfg(feature = "grid")]
use taffy::compute_grid_layout;
use taffy::{
    compute_block_layout,
    compute_cached_layout,
    compute_flexbox_layout,
    compute_leaf_layout,
    compute_root_layout,
    prelude::TaffyMaxContent,
    print_tree,
    round_layout,
    CacheTree,
    LayoutPartialTree,
    PrintTree,
    RoundTree,
    TraversePartialTree,
    TraverseTree,
};
use thiserror::Error;
use tiny_skia::Pixmap;
use usvg::{
    ImageHrefResolver,
    ImageKind,
    Tree,
};

const ROOT_ID: usize = 0;
const INLINE_FRAG_CASCADE: usize = 16;

/// The error that may occur during rasterization of a scene.
#[derive(Debug, Error)]
pub enum RasterizeError {
    #[error("failed to vectorize")]
    Vectorize(#[from] VectorizeError),
    #[error("failed to write to the output stream")]
    Write(#[from] std::fmt::Error),
    #[error("failed to parse svg")]
    Parse(#[from] usvg::Error),
    #[error("failed to allocate pixmap")]
    PixmapAlloc,
}

/// The central scene container responsible for layout, rendering, and resource
/// management.
#[derive(Debug)]
pub struct Scene {
    pub(crate) fonts: Arc<Mutex<FontRegistry>>,
    pub(crate) resources: Mutex<Resources>,
    nodes: Vec<Node>,
}

impl Scene {
    /// Creates a new scene with the given root node.
    ///
    /// # Arguments
    /// - `root`: The root [`Node`] of the scene.
    ///
    /// # Returns
    /// - [`Self`]
    pub fn new(root: Node) -> Self {
        let mut resources = Resources::default();

        // register resources
        for resource in &root.resources {
            resources.get_or_add_resource(resource.clone());
        }

        Self {
            fonts: Arc::new(Mutex::new(FontRegistry::new())),
            resources: Mutex::new(resources),
            nodes: vec![root],
        }
    }

    #[cfg(test)]
    pub(crate) fn empty() -> Self {
        Self {
            fonts: Arc::new(Mutex::new(FontRegistry::new())),
            resources: Mutex::new(Resources::default()),
            nodes: vec![],
        }
    }

    /// Returns the [`NodeId`] of the root node.
    ///
    /// # Returns
    /// - [`NodeId`] corresponding to the root.
    pub fn root_id(&self) -> NodeId {
        ROOT_ID.into()
    }

    /// Returns the total number of nodes in the scene.
    ///
    /// # Returns
    /// - The number of nodes.
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// Appends a child node to the scene under the given parent node.
    ///
    /// # Arguments
    /// - `parent_id`: The [`NodeId`] of the parent node.
    /// - `child`: The child [`Node`] to append.
    ///
    /// # Returns
    /// - [`NodeId`] of the newly added child.
    ///
    /// # Warning
    /// Panics if the parent node is atomic and cannot contain children.
    pub fn append_child(&mut self, parent_id: NodeId, mut child: Node) -> NodeId {
        self.assert_non_atomic(parent_id);
        let parent = &self.nodes[parent_id];
        child.typography.cascade_from(&parent.typography);

        // copy the resolved typography into its metadata
        if let NodeKind::Text(ref mut meta) = child.kind {
            meta.typography(child.typography.clone());
        }

        // register resources
        {
            let mut resources = self.resources.lock();
            for resource in &child.resources {
                resources.get_or_add_resource(resource.clone());
            }
        }

        self.nodes.push(child);
        let child_id = self.nodes.len() - 1;
        self.nodes[parent_id].children.push(child_id);
        child_id.into()
    }

    /// Appends an entire scene as a subtree to the current scene under the
    /// given parent node.
    ///
    /// All node indices and typography cascades are adjusted accordingly.
    ///
    /// # Arguments
    /// - `parent_id`: The [`NodeId`] of the parent node.
    /// - `scene`: The scene to append.
    pub fn append_scene(&mut self, parent_id: NodeId, mut scene: Scene) {
        if scene.nodes.is_empty() {
            return;
        }

        self.assert_non_atomic(parent_id);

        let parent_typography = &self.nodes[parent_id].typography;
        cascade_typography_subtree(&mut scene.nodes, parent_typography);

        let root_id = self.nodes.len(); // scene root node
        self.nodes.reserve(scene.nodes.len()); // pre-allocation
        self.nodes[parent_id].children.push(root_id);

        for mut node in scene.nodes {
            // update child indices after adding them to main arena
            for child_id in node.children.iter_mut() {
                *child_id += root_id;
            }

            // register resources
            {
                let mut resources = self.resources.lock();
                for resource in &node.resources {
                    resources.get_or_add_resource(resource.clone());
                }
            }

            self.nodes.push(node);
        }
    }

    /// Prints the scene graph in a tree format.
    #[allow(dead_code)]
    pub(crate) fn print_tree(&self) {
        print_tree(self, taffy::NodeId::from(ROOT_ID));
    }

    /// Streams the vectorized SVG representation into the given output writer.
    ///
    /// # Arguments
    /// - `out`: The output writer.
    /// - `options`: The [`VectorizeOptions`] value.
    ///
    /// # Returns
    /// - Scene size on success.
    /// - [`VectorizeError`] on failure.
    pub(crate) fn stream_vector<T>(
        &self,
        out: &mut T,
        options: &VectorizeOptions,
    ) -> Result<Size<f32>, VectorizeError>
    where
        T: Write,
    {
        if self.nodes.is_empty() {
            return Err(VectorizeError::EmptyScene);
        }

        let root = &self.nodes[ROOT_ID];
        let size = Size::from(root.final_layout.size);

        if size.width == 0.0 || size.height == 0.0 {
            return Err(VectorizeError::InvalidSize);
        }

        let view_box = ViewBox::new(0.0, 0.0, size.width, size.height);
        let mut svg = ElementWriter::new(out, "svg")?
            .attr_if(
                "xmlns",
                "http://www.w3.org/2000/svg",
                !options.omit_svg_xmlns,
            )?
            .attr("viewBox", (view_box,))?;

        match &options.svg_dimensions {
            SvgDimensions::Omit => {}
            SvgDimensions::Layout => {
                svg = svg.attrs([("width", size.width), ("height", size.height)])?;
            }
            SvgDimensions::Custom { width, height } => {
                svg = svg.attrs([("width", width.as_str()), ("height", height.as_str())])?;
            }
        };

        svg.open()?;

        //

        self.emit_node(
            &mut RenderContext {
                out,
                scene: &self,
                scene_size: size,
            },
            None,
            None,
        )?;

        //

        let resources = self.resources.lock();

        if !resources.is_empty() {
            ElementWriter::new(out, "defs")?
                .content(|out| out.write_fmt(format_args!("{resources}")))?
                .close()?;
        }

        ElementWriter::close_tag(out, "svg")?;

        Ok(size)
    }

    /// Vectorizes the scene into an SVG string.
    ///
    /// # Arguments
    /// - `options`: The [`VectorizeOptions`] value.
    ///
    /// # Returns
    /// - On success, a tuple containing:
    ///     - [`String`]: The SVG string.
    ///     - [`Size<f32>`]: Scene size.
    /// - [`VectorizeError`] on failure.
    pub(crate) fn vectorize(
        &self,
        options: &VectorizeOptions,
    ) -> Result<(String, Size<f32>), VectorizeError> {
        let mut out = String::new();
        self.stream_vector(&mut out, options)
            .map(|scene_size| (out, scene_size))
    }

    /// Rasterizes the scene into a [`Pixmap`].
    ///
    /// # Arguments
    /// - `image_cache`: Shared image cache.
    /// - `options`: The [`RasterizeOptions`] value.
    ///
    /// # Returns
    /// - On success, a tuple containing:
    ///     - [`Pixmap`]: Pixmap containing the image data.
    ///     - [`Size<f32>`]: Scene size.
    /// - [`RasterizeError`] on failure.
    pub(crate) fn rasterize(
        &self,
        image_cache: &ImageCache,
        options: &RasterizeOptions,
    ) -> Result<(Pixmap, Size<f32>), RasterizeError> {
        let tf = options.root_transform;
        let mut usvg_options = usvg::Options {
            shape_rendering: options.shape_rendering,
            text_rendering: options.text_rendering,
            image_rendering: options.image_rendering,
            image_href_resolver: ImageHrefResolver {
                resolve_string: Box::new(move |href: &str, usvg_opts: &usvg::Options| {
                    fetch_image_cached(image_cache, href, &options.image, usvg_opts)
                }),
                ..Default::default()
            },
            ..Default::default()
        };

        if let Some(ref resolve_data) = options.image.href_data_resolver {
            usvg_options.image_href_resolver.resolve_data = Box::new(
                move |mime: &str, data: Arc<Vec<u8>>, opts: &usvg::Options| {
                    resolve_data(mime, data, opts)
                },
            );
        }

        let (svg, size) = self.vectorize(&options.vectorize_options)?;
        let tree = Tree::from_str(&svg, &usvg_options).map_err(RasterizeError::Parse)?;
        let mut pixmap = Pixmap::new(size.width() as u32, size.height() as u32)
            .ok_or(RasterizeError::PixmapAlloc)?;

        render(&tree, tf, &mut pixmap.as_mut());

        if options.debug {
            let mut bboxes = Vec::new();
            let mut stroke_bboxes = Vec::new();

            collect_bboxes(tree.root(), &mut bboxes, &mut stroke_bboxes);

            let stroke = tiny_skia::Stroke::default();
            let mut paint = tiny_skia::Paint::default();
            paint.set_color_rgba8(224, 16, 0, 195);

            for bbox in bboxes {
                let path = tiny_skia::PathBuilder::from_rect(bbox);
                pixmap.stroke_path(&path, &paint, &stroke, tf, None);
            }

            paint.set_color_rgba8(0, 45, 255, 127);

            for bbox in stroke_bboxes {
                let path = tiny_skia::PathBuilder::from_rect(bbox);
                pixmap.stroke_path(&path, &paint, &stroke, tf, None);
            }
        }

        Ok((pixmap, size))
    }

    /// Computes layout for all nodes in the scene.
    pub(crate) fn compute_layout(&mut self) {
        let root_id = taffy::NodeId::from(self.root_id());
        compute_root_layout(self, root_id, taffy::Size::MAX_CONTENT);
        round_layout(self, root_id);
    }

    /// Sets the font registry used for layout and rendering.
    ///
    /// # Arguments
    /// - `fonts`: The next font registry.
    pub(crate) fn set_fonts(&mut self, fonts: Arc<Mutex<FontRegistry>>) {
        self.fonts = fonts;
    }

    /// Panics if the node with the given `id` is atomic (cannot have children).
    ///
    /// Note: This is a safety check. The macro should prevent adding children
    /// to atomic nodes at compile time.
    fn assert_non_atomic(&self, id: NodeId) {
        if self.nodes[id].kind.is_atomic() {
            panic!("node with id {id} is atomic and cannot contain children");
        }
    }

    #[inline(always)]
    fn node_from_id(&self, node_id: taffy::NodeId) -> &Node {
        &self.nodes[usize::from(node_id)]
    }

    #[inline(always)]
    fn node_from_id_mut(&mut self, node_id: taffy::NodeId) -> &mut Node {
        &mut self.nodes[usize::from(node_id)]
    }

    /// Emits a node and its subtree into the render context.
    pub(crate) fn emit_node<T>(
        &self,
        ctx: &mut RenderContext<T>,
        node_id: Option<taffy::NodeId>,
        stop_at: Option<taffy::NodeId>,
    ) -> Result<(), VectorizeError>
    where
        T: Write,
    {
        if stop_at.is_some() && node_id == stop_at {
            return Ok(());
        }

        let node_idx = node_id.map(usize::from).unwrap_or(ROOT_ID);
        let node = &self.nodes[node_idx];

        if node.visual.visible && !matches!(node.layout.display, taffy::Display::None) {
            node.pre_emit(ctx)?;

            for child_id in &node.children {
                self.emit_node(ctx, Some(taffy::NodeId::from(*child_id)), stop_at)?;
            }

            node.post_emit(ctx)?;
        }

        Ok(())
    }
}

/// Converts raw image bytes into [`ImageKind`].
///
/// # Arguments
/// - `data`: The raw image data.
///
/// # Returns
/// - `Some(ImageKind)` on success.
/// - `None` when kind of image cannot be inferred.
fn to_image_kind(data: Arc<Vec<u8>>) -> Option<ImageKind> {
    match infer::get(&data)?.mime_type() {
        "image/png" => Some(ImageKind::PNG(data)),
        "image/jpeg" => Some(ImageKind::JPEG(data)),
        "image/webp" => Some(ImageKind::WEBP(data)),
        "image/gif" => Some(ImageKind::GIF(data)),
        _ => None,
    }
}

/// Resolves and caches external images referenced by `href`.
///
/// # Arguments
/// - `image_cache`: The shared image cache.
/// - `href`: The image reference.
/// - `opts`: The [`ImageOptions`] value.
/// - `usvg_opts`: The [`Options`] value.
///
/// # Returns
/// - `Some(ImageKind)` on success
/// - `None` during failure
///
/// [`Options`]: usvg::Options
fn fetch_image_cached(
    image_cache: &ImageCache,
    href: &str,
    opts: &ImageOptions,
    usvg_opts: &usvg::Options,
) -> Option<ImageKind> {
    let skip_cache = opts.disable_caching
        || opts.cache_ignore_list.iter().any(|item| item == href)
        || opts
            .cache_ignore_fn
            .as_ref()
            .is_some_and(|ignore_fn| ignore_fn(href));

    if !skip_cache {
        if let Some(image) = image_cache.lock().get(href) {
            return Some(image.clone());
        }
    }

    let image = if let Some(resolve) = &opts.href_string_resolver {
        resolve(href, usvg_opts)
    } else {
        let mut res = ureq::get(href).call().ok()?;

        if !res.status().is_success() {
            return None;
        }

        let buf = res.body_mut().read_to_vec().ok()?;
        to_image_kind(Arc::new(buf))
    };

    if !skip_cache {
        if let Some(kind) = image.clone() {
            image_cache.lock().push(href.to_string(), kind);
        }
    }

    image
}

/// Cascades typography values through a subtree rooted at the first node.
///
/// # Arguments
/// - `nodes`: The node list.
/// - `parent_typography`: The parent node's [`Typography`].
fn cascade_typography_subtree(nodes: &mut [Node], parent_typography: &Typography) {
    let mut stack: SmallVec<[(usize, Typography); INLINE_FRAG_CASCADE]> = SmallVec::new();
    stack.push((ROOT_ID, parent_typography.clone()));

    while let Some((idx, mut parent)) = stack.pop() {
        let node = &mut nodes[idx];
        node.typography.cascade_from(&parent);

        if let NodeKind::Text(ref mut meta) = node.kind {
            meta.typography(node.typography.clone());
        }

        if !node.children.is_empty() {
            parent = node.typography.clone();
        }

        for &child in &node.children {
            stack.push((child, parent.clone()));
        }
    }
}

/// Collects bounding boxes and stroke bounding boxes for debugging output.
fn collect_bboxes(
    parent: &usvg::Group,
    bboxes: &mut Vec<usvg::Rect>,
    stroke_bboxes: &mut Vec<usvg::Rect>,
) {
    for node in parent.children() {
        if let usvg::Node::Group(ref group) = node {
            collect_bboxes(group, bboxes, stroke_bboxes);
        }

        let bbox = node.abs_bounding_box();
        bboxes.push(bbox);

        let stroke_bbox = node.abs_stroke_bounding_box();
        if bbox != stroke_bbox {
            stroke_bboxes.push(stroke_bbox);
        }
    }
}

impl std::ops::Index<NodeId> for Vec<Node> {
    type Output = Node;

    fn index(&self, id: NodeId) -> &Self::Output {
        &self[usize::from(id)]
    }
}

impl std::ops::IndexMut<NodeId> for Vec<Node> {
    fn index_mut(&mut self, id: NodeId) -> &mut Self::Output {
        &mut self[usize::from(id)]
    }
}

mod sealed {
    pub struct ChildIter<'a>(pub std::slice::Iter<'a, usize>);
}

impl Iterator for sealed::ChildIter<'_> {
    type Item = taffy::NodeId;
    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().copied().map(taffy::NodeId::from)
    }
}

impl TraversePartialTree for Scene {
    type ChildIter<'a> = sealed::ChildIter<'a>;

    fn child_ids(&self, node_id: taffy::NodeId) -> Self::ChildIter<'_> {
        sealed::ChildIter(self.node_from_id(node_id).children.iter())
    }

    fn child_count(&self, node_id: taffy::NodeId) -> usize {
        self.node_from_id(node_id).children.len()
    }

    fn get_child_id(&self, node_id: taffy::NodeId, index: usize) -> taffy::NodeId {
        taffy::NodeId::from(self.node_from_id(node_id).children[index])
    }
}

impl TraverseTree for Scene {}

impl LayoutPartialTree for Scene {
    type CoreContainerStyle<'a>
        = &'a taffy::Style
    where
        Self: 'a;

    type CustomIdent = String;

    fn get_core_container_style(&self, node_id: taffy::NodeId) -> Self::CoreContainerStyle<'_> {
        &self.node_from_id(node_id).layout
    }

    fn resolve_calc_value(&self, _val: *const (), _basis: f32) -> f32 {
        0.0
    }

    fn set_unrounded_layout(&mut self, node_id: taffy::NodeId, layout: &taffy::Layout) {
        self.node_from_id_mut(node_id).unrounded_layout = *layout;
    }

    fn compute_child_layout(
        &mut self,
        node_id: taffy::NodeId,
        inputs: taffy::tree::LayoutInput,
    ) -> taffy::tree::LayoutOutput {
        compute_cached_layout(self, node_id, inputs, |tree, node_id, inputs| {
            let node = &mut tree.nodes[usize::from(node_id)];
            match node.kind {
                NodeKind::Block => compute_block_layout(tree, node_id, inputs),
                NodeKind::Flex | NodeKind::Column | NodeKind::Row => {
                    compute_flexbox_layout(tree, node_id, inputs)
                }
                #[cfg(feature = "grid")]
                NodeKind::Grid => compute_grid_layout(tree, node_id, inputs),
                NodeKind::Text(ref mut meta) => compute_leaf_layout(
                    inputs,
                    &node.layout,
                    |_val, _basis| 0.0,
                    |known_dimensions, available_space| {
                        meta.measure(known_dimensions, available_space, tree.fonts.clone())
                    },
                ),
                NodeKind::Image(ref mut meta) => compute_leaf_layout(
                    inputs,
                    &node.layout,
                    |_val, _basis| 0.0,
                    |known_dimensions, _available_space| meta.measure(known_dimensions),
                ),
            }
        })
    }
}

impl CacheTree for Scene {
    fn cache_get(
        &self,
        node_id: taffy::NodeId,
        known_dimensions: taffy::Size<Option<f32>>,
        available_space: taffy::Size<taffy::AvailableSpace>,
        run_mode: taffy::RunMode,
    ) -> Option<taffy::LayoutOutput> {
        self.node_from_id(node_id)
            .cache
            .get(known_dimensions, available_space, run_mode)
    }

    fn cache_store(
        &mut self,
        node_id: taffy::NodeId,
        known_dimensions: taffy::Size<Option<f32>>,
        available_space: taffy::Size<taffy::AvailableSpace>,
        run_mode: taffy::RunMode,
        layout_output: taffy::LayoutOutput,
    ) {
        self.node_from_id_mut(node_id).cache.store(
            known_dimensions,
            available_space,
            run_mode,
            layout_output,
        )
    }

    fn cache_clear(&mut self, node_id: taffy::NodeId) {
        self.node_from_id_mut(node_id).cache.clear();
    }
}

impl taffy::LayoutBlockContainer for Scene {
    type BlockContainerStyle<'a>
        = &'a taffy::Style
    where
        Self: 'a;

    type BlockItemStyle<'a>
        = &'a taffy::Style
    where
        Self: 'a;

    fn get_block_container_style(&self, node_id: taffy::NodeId) -> Self::BlockContainerStyle<'_> {
        &self.node_from_id(node_id).layout
    }

    fn get_block_child_style(&self, child_node_id: taffy::NodeId) -> Self::BlockItemStyle<'_> {
        &self.node_from_id(child_node_id).layout
    }
}

impl taffy::LayoutFlexboxContainer for Scene {
    type FlexboxContainerStyle<'a>
        = &'a taffy::Style
    where
        Self: 'a;

    type FlexboxItemStyle<'a>
        = &'a taffy::Style
    where
        Self: 'a;

    fn get_flexbox_container_style(
        &self,
        node_id: taffy::NodeId,
    ) -> Self::FlexboxContainerStyle<'_> {
        &self.node_from_id(node_id).layout
    }

    fn get_flexbox_child_style(&self, child_node_id: taffy::NodeId) -> Self::FlexboxItemStyle<'_> {
        &self.node_from_id(child_node_id).layout
    }
}

#[cfg(feature = "grid")]
impl taffy::LayoutGridContainer for Scene {
    type GridContainerStyle<'a>
        = &'a taffy::Style
    where
        Self: 'a;

    type GridItemStyle<'a>
        = &'a taffy::Style
    where
        Self: 'a;

    fn get_grid_container_style(&self, node_id: taffy::NodeId) -> Self::GridContainerStyle<'_> {
        &self.node_from_id(node_id).layout
    }

    fn get_grid_child_style(&self, child_node_id: taffy::NodeId) -> Self::GridItemStyle<'_> {
        &self.node_from_id(child_node_id).layout
    }
}

impl RoundTree for Scene {
    fn get_unrounded_layout(&self, node_id: taffy::NodeId) -> taffy::Layout {
        self.node_from_id(node_id).unrounded_layout
    }

    fn set_final_layout(&mut self, node_id: taffy::NodeId, layout: &taffy::Layout) {
        let node = self.node_from_id_mut(node_id);
        node.final_layout = *layout;
        node.apply_layout_effects();
    }
}

impl PrintTree for Scene {
    fn get_debug_label(&self, node_id: taffy::NodeId) -> &'static str {
        match self.node_from_id(node_id).kind {
            NodeKind::Block => "BLOCK",
            NodeKind::Flex => "FLEX",
            NodeKind::Column => "COLUMN",
            NodeKind::Row => "ROW",
            #[cfg(feature = "grid")]
            NodeKind::Grid => "GRID",
            NodeKind::Text(_) => "TEXT",
            NodeKind::Image(_) => "IMAGE",
        }
    }

    fn get_final_layout(&self, node_id: taffy::NodeId) -> taffy::Layout {
        self.node_from_id(node_id).final_layout
    }
}