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
//! Context for reusing dynamic memory allocations.

use super::geometry::{Bounds, BoundsBuilder, Transform};
use super::path_builder::{PathBuilder, TransformSink};
use super::path_data::PathData;
use super::raster::HeapStorage;
use super::segment::Segment;
use super::stroke::stroke_with_storage;
use super::style::{Fill, Style};

use crate::lib::Vec;
use core::borrow::Borrow;

/// Scratch memory for reusable heap allocations.
#[derive(Default)]
pub struct Scratch {
    pub(super) inner: Inner,
    pub(super) render: HeapStorage,
}

impl Scratch {
    /// Creates a new scratch memory context.
    pub fn new() -> Self {
        Self::default()
    }

    /// Applies the style and transform to the path and emits the result to the specified sink.
    pub fn apply<'a>(
        &mut self,
        data: impl PathData,
        style: impl Into<Style<'a>>,
        transform: Option<Transform>,
        sink: &mut impl PathBuilder,
    ) -> Fill {
        self.inner.apply(data, &style.into(), transform, sink)
    }

    /// Computes the bounding box of the path.
    pub fn bounds<'a>(
        &mut self,
        data: impl PathData,
        style: impl Into<Style<'a>>,
        transform: Option<Transform>,
    ) -> Bounds {
        let style = style.into();
        let mut bounds = BoundsBuilder::new();
        self.apply(data, style, transform, &mut bounds);
        bounds.build()
    }
}

#[derive(Default)]
pub(super) struct Inner {
    pub segments: Vec<Segment>,
}

impl Inner {
    pub fn apply(
        &mut self,
        data: impl PathData,
        style: &Style,
        transform: Option<Transform>,
        sink: &mut impl PathBuilder,
    ) -> Fill {
        match style {
            Style::Fill(fill) => {
                if let Some(transform) = transform {
                    let mut transform_sink = TransformSink { sink, transform };
                    data.copy_to(&mut transform_sink);
                    *fill
                } else {
                    data.copy_to(sink);
                    *fill
                }
            }
            Style::Stroke(stroke) => {
                if let Some(transform) = transform {
                    if stroke.scale {
                        let mut transform_sink = TransformSink { sink, transform };
                        stroke_with_storage(
                            data.commands(),
                            &stroke,
                            &mut transform_sink,
                            &mut self.segments,
                        );
                    } else {
                        stroke_with_storage(
                            data.commands()
                                .map(|cmd| cmd.borrow().transform(&transform)),
                            &stroke,
                            sink,
                            &mut self.segments,
                        );
                    }
                } else {
                    stroke_with_storage(data.commands(), &stroke, sink, &mut self.segments);
                }
                Fill::NonZero
            }
        }
    }
}