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
use pathfinder_geometry::line_segment::LineSegment2F;
use pathfinder_geometry::vector::Vector2F;
use std::mem;
pub trait OutlineSink {
fn move_to(&mut self, to: Vector2F);
fn line_to(&mut self, to: Vector2F);
fn quadratic_curve_to(&mut self, ctrl: Vector2F, to: Vector2F);
fn cubic_curve_to(&mut self, ctrl: LineSegment2F, to: Vector2F);
fn close(&mut self);
}
#[derive(Clone, PartialEq, Debug)]
pub struct Outline {
pub contours: Vec<Contour>,
}
#[derive(Clone, PartialEq, Debug)]
pub struct Contour {
pub positions: Vec<Vector2F>,
pub flags: Vec<PointFlags>,
}
bitflags! {
pub struct PointFlags: u8 {
const CONTROL_POINT_0 = 0x01;
const CONTROL_POINT_1 = 0x02;
}
}
#[derive(Clone, Debug)]
pub struct OutlineBuilder {
outline: Outline,
current_contour: Contour,
}
impl Outline {
#[inline]
pub fn new() -> Outline {
Outline { contours: vec![] }
}
pub fn copy_to<S>(&self, sink: &mut S)
where
S: OutlineSink,
{
for contour in &self.contours {
contour.copy_to(sink);
}
}
}
impl Contour {
#[inline]
pub fn new() -> Contour {
Contour {
positions: vec![],
flags: vec![],
}
}
#[inline]
pub fn push(&mut self, position: Vector2F, flags: PointFlags) {
self.positions.push(position);
self.flags.push(flags);
}
pub fn copy_to<S>(&self, sink: &mut S)
where
S: OutlineSink,
{
debug_assert_eq!(self.positions.len(), self.flags.len());
if self.positions.is_empty() {
return;
}
sink.move_to(self.positions[0]);
let mut iter = self.positions[1..].iter().zip(self.flags[1..].iter());
while let Some((&position_0, &flags_0)) = iter.next() {
if flags_0 == PointFlags::empty() {
sink.line_to(position_0);
continue;
}
let (&position_1, &flags_1) = iter.next().expect("Invalid outline!");
if flags_1 == PointFlags::empty() {
sink.quadratic_curve_to(position_0, position_1);
continue;
}
let (&position_2, &flags_2) = iter.next().expect("Invalid outline!");
debug_assert_eq!(flags_2, PointFlags::empty());
sink.cubic_curve_to(LineSegment2F::new(position_0, position_1), position_2);
}
sink.close();
}
}
impl OutlineBuilder {
#[inline]
pub fn new() -> OutlineBuilder {
OutlineBuilder {
outline: Outline::new(),
current_contour: Contour::new(),
}
}
#[inline]
pub fn into_outline(self) -> Outline {
self.outline
}
#[inline]
pub fn take_outline(&mut self) -> Outline {
assert!(self.current_contour.positions.is_empty());
self.current_contour = Contour::new();
mem::replace(&mut self.outline, Outline::new())
}
}
impl OutlineSink for OutlineBuilder {
#[inline]
fn move_to(&mut self, to: Vector2F) {
self.current_contour.push(to, PointFlags::empty());
}
#[inline]
fn line_to(&mut self, to: Vector2F) {
self.current_contour.push(to, PointFlags::empty());
}
#[inline]
fn quadratic_curve_to(&mut self, ctrl: Vector2F, to: Vector2F) {
self.current_contour.push(ctrl, PointFlags::CONTROL_POINT_0);
self.current_contour.push(to, PointFlags::empty());
}
#[inline]
fn cubic_curve_to(&mut self, ctrl: LineSegment2F, to: Vector2F) {
self.current_contour
.push(ctrl.from(), PointFlags::CONTROL_POINT_0);
self.current_contour
.push(ctrl.to(), PointFlags::CONTROL_POINT_1);
self.current_contour.push(to, PointFlags::empty());
}
#[inline]
fn close(&mut self) {
self.outline
.contours
.push(mem::replace(&mut self.current_contour, Contour::new()));
}
}