geometry.rs

 1pub use pathfinder_geometry::*;
 2
 3use vector::{vec2f, Vector2F};
 4
 5pub(crate) struct Vertex {
 6    xy_position: Vector2F,
 7    st_position: Vector2F,
 8}
 9
10pub struct Path {
11    vertices: Vec<Vertex>,
12    start: Vector2F,
13    current: Vector2F,
14    countours_len: usize,
15}
16
17enum Kind {
18    Solid,
19    Quadratic,
20}
21
22impl Path {
23    fn new() -> Self {
24        Self {
25            vertices: Vec::new(),
26            start: vec2f(0., 0.),
27            current: vec2f(0., 0.),
28            countours_len: 0,
29        }
30    }
31
32    pub fn reset(&mut self, point: Vector2F) {
33        self.vertices.clear();
34        self.start = point;
35        self.current = point;
36        self.countours_len = 0;
37    }
38
39    pub fn line_to(&mut self, point: Vector2F) {
40        self.countours_len += 1;
41        if self.countours_len > 1 {
42            self.push_triangle(self.start, self.current, point, Kind::Solid);
43        }
44
45        self.current = point;
46    }
47
48    pub fn curve_to(&mut self, point: Vector2F, ctrl: Vector2F) {
49        self.countours_len += 1;
50        if self.countours_len > 1 {
51            self.push_triangle(self.start, self.current, point, Kind::Solid);
52        }
53
54        self.push_triangle(self.current, ctrl, point, Kind::Quadratic);
55        self.current = point;
56    }
57
58    pub(crate) fn close(self) -> Vec<Vertex> {
59        self.vertices
60    }
61
62    fn push_triangle(&mut self, a: Vector2F, b: Vector2F, c: Vector2F, kind: Kind) {
63        match kind {
64            Kind::Solid => {
65                self.vertices.push(Vertex {
66                    xy_position: a,
67                    st_position: vec2f(0., 1.),
68                });
69                self.vertices.push(Vertex {
70                    xy_position: b,
71                    st_position: vec2f(0., 1.),
72                });
73                self.vertices.push(Vertex {
74                    xy_position: c,
75                    st_position: vec2f(0., 1.),
76                });
77            }
78            Kind::Quadratic => {
79                self.vertices.push(Vertex {
80                    xy_position: a,
81                    st_position: vec2f(0., 0.),
82                });
83                self.vertices.push(Vertex {
84                    xy_position: b,
85                    st_position: vec2f(0.5, 0.),
86                });
87                self.vertices.push(Vertex {
88                    xy_position: c,
89                    st_position: vec2f(1., 1.),
90                });
91            }
92        }
93    }
94}