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
use crate::dependencies::Dependency;
use crate::views::{Bounds, Output, Size, Transform, View};

mod rounded;

pub trait Path: Sized {
    fn draw(&self, x: f32, y: f32, w: f32, h: f32, transform: &Transform, onto: &mut impl Output);

    fn fill(self) -> Shape<Self> {
        self.fixed(f32::INFINITY, f32::INFINITY)
    }

    fn fixed(self, width: f32, height: f32) -> Shape<Self> {
        Shape {
            size: Size::new(width, height),
            path: self,
        }
    }
}

/// [Least-squares approximation of the circle using cubic Bézier curves][site]
///
/// > David Ellsworth found the optimal value of c:  
/// >
/// > c ≈ 0.5519703814011128603134107
///
/// [site]: https://spencermortensen.com/articles/least-squares-bezier-circle/
pub(crate) const K: f32 = 0.4480296; // 1 - 0.5519703814011128603134107 rounded to f32

pub struct Rectangle {
    pub rgba: [u8; 4],
}

impl Path for Rectangle {
    #[inline(always)]
    fn draw(&self, x: f32, y: f32, w: f32, h: f32, transform: &Transform, onto: &mut impl Output) {
        rounded::rectangle(x, y, w, h, 0.0, 0.0, 0.0, self.rgba, transform, onto);
    }
}

impl Rectangle {
    pub fn rounded(self, rx: f32, ry: f32) -> RoundedRectangle {
        RoundedRectangle {
            rgba: self.rgba,
            rx,
            ry,
        }
    }
}

pub struct RoundedRectangle {
    rgba: [u8; 4],
    rx: f32,
    ry: f32,
}

impl Path for RoundedRectangle {
    #[inline(always)]
    fn draw(&self, x: f32, y: f32, w: f32, h: f32, transform: &Transform, onto: &mut impl Output) {
        rounded::rectangle(x, y, w, h, self.rx, self.ry, K, self.rgba, transform, onto);
    }
}

impl RoundedRectangle {
    pub fn continuous(self) -> ContinuousRoundedRectangle {
        ContinuousRoundedRectangle {
            rgba: self.rgba,
            rx: self.rx,
            ry: self.ry,
        }
    }
}

pub struct ContinuousRoundedRectangle {
    rgba: [u8; 4],
    rx: f32,
    ry: f32,
}

impl Path for ContinuousRoundedRectangle {
    #[inline(always)]
    fn draw(&self, x: f32, y: f32, w: f32, h: f32, transform: &Transform, onto: &mut impl Output) {
        // continuous corners are much smaller than circular ones; scale them up a bit
        let c = std::f32::consts::E;
        let rx = (self.rx * c).min(w / 2.0);
        let ry = (self.ry * c).min(h / 2.0);
        rounded::rectangle(x, y, w, h, rx, ry, 0.0, self.rgba, transform, onto);
    }
}

pub struct Ellipse {
    pub rgba: [u8; 4],
}

impl Path for Ellipse {
    #[inline(always)]
    fn draw(&self, x: f32, y: f32, w: f32, h: f32, transform: &Transform, onto: &mut impl Output) {
        let rx = w / 2.0;
        let ry = h / 2.0;
        rounded::rectangle(x, y, w, h, rx, ry, K, self.rgba, transform, onto);
    }
}

pub struct Circle {
    pub rgba: [u8; 4],
}

impl Path for Circle {
    #[inline(always)]
    fn draw(&self, x: f32, y: f32, w: f32, h: f32, transform: &Transform, onto: &mut impl Output) {
        let r = f32::min(w, h) / 2.0;
        rounded::rectangle(x, y, w, h, r, r, K, self.rgba, transform, onto);
    }
}

#[doc(hidden)]
pub struct Shape<T> {
    size: Size,
    path: T,
}

impl<T: Path> View for Shape<T> {
    #[inline(always)]
    fn size(&self) -> Size {
        self.size
    }

    #[inline]
    fn draw(&self, bounds: Bounds, onto: &mut impl Output) {
        let size = match (self.size.width.is_finite(), self.size.height.is_finite()) {
            (true, true) => self.size,
            (false, false) => bounds.size(),
            (true, false) => Size::new(self.size.width, bounds.height()),
            (false, true) => Size::new(bounds.width(), self.size.height),
        };

        self.path.draw(
            bounds.min.x,
            bounds.min.y,
            size.width,
            size.height,
            &Dependency::<Transform>::new().unwrap_or_default(),
            onto,
        );
    }

    #[inline(always)]
    #[allow(refining_impl_trait)]
    fn fixed(mut self, width: f32, height: f32) -> Self {
        self.size = Size::new(width, height);
        self
    }

    #[inline(always)]
    #[allow(refining_impl_trait)]
    fn width(mut self, width: f32) -> Self {
        self.size.width = width;
        self
    }

    #[inline(always)]
    #[allow(refining_impl_trait)]
    fn height(mut self, height: f32) -> Self {
        self.size.height = height;
        self
    }
}