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
use crate::canvas::Canvas;
use lyon_path::PathEvent;
pub trait Paint {
fn paint(&self, canvas: &mut Canvas);
}
pub struct Filled<D>(pub D);
impl<P: Paint> Paint for Filled<P> {
fn paint(&self, comp: &mut Canvas) {
self.0.paint(comp);
comp.fill();
}
}
pub struct Stroked<D> {
pub element: D,
pub width: f32,
}
impl<P: Paint> Paint for Stroked<P> {
fn paint(&self, comp: &mut Canvas) {
self.element.paint(comp);
comp.set_stroke_width(self.width);
comp.stroke();
}
}
impl<P> Paint for P
where
P: Iterator<Item = PathEvent> + Clone,
{
fn paint(&self, canvas: &mut Canvas) {
self.clone().for_each(|p| match p {
PathEvent::Line(line) => canvas.line_to(line.to),
PathEvent::Quadratic(q) => canvas.quadratic_to(q.ctrl, q.to),
PathEvent::Cubic(c) => canvas.cubic_to(c.ctrl1, c.ctrl2, c.to),
PathEvent::MoveTo(p) => canvas.move_to(p),
PathEvent::Close(_) => canvas.close_path(),
});
}
}