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 crate::widget::canvas::path::{arc, Arc, Path};
use iced_native::{Point, Size};
use lyon::path::builder::SvgPathBuilder;
#[allow(missing_debug_implementations)]
pub struct Builder {
raw: lyon::path::builder::WithSvg<lyon::path::path::BuilderImpl>,
}
impl Builder {
pub fn new() -> Builder {
Builder {
raw: lyon::path::Path::builder().with_svg(),
}
}
#[inline]
pub fn move_to(&mut self, point: Point) {
let _ = self.raw.move_to(lyon::math::Point::new(point.x, point.y));
}
#[inline]
pub fn line_to(&mut self, point: Point) {
let _ = self.raw.line_to(lyon::math::Point::new(point.x, point.y));
}
#[inline]
pub fn arc(&mut self, arc: Arc) {
self.ellipse(arc.into());
}
pub fn arc_to(&mut self, a: Point, b: Point, radius: f32) {
use lyon::{math, path};
let start = self.raw.current_position();
let mid = math::Point::new(a.x, a.y);
let end = math::Point::new(b.x, b.y);
if start == mid || mid == end || radius == 0.0 {
let _ = self.raw.line_to(mid);
return;
}
let double_area = start.x * (mid.y - end.y)
+ mid.x * (end.y - start.y)
+ end.x * (start.y - mid.y);
if double_area == 0.0 {
let _ = self.raw.line_to(mid);
return;
}
let to_start = (start - mid).normalize();
let to_end = (end - mid).normalize();
let inner_angle = to_start.dot(to_end).acos();
let origin_angle = inner_angle / 2.0;
let origin_adjacent = radius / origin_angle.tan();
let arc_start = mid + to_start * origin_adjacent;
let arc_end = mid + to_end * origin_adjacent;
let sweep = to_start.cross(to_end) < 0.0;
let _ = self.raw.line_to(arc_start);
self.raw.arc_to(
math::Vector::new(radius, radius),
math::Angle::radians(0.0),
path::ArcFlags {
large_arc: false,
sweep,
},
arc_end,
);
}
pub fn ellipse(&mut self, arc: arc::Elliptical) {
use lyon::{geom, math};
let arc = geom::Arc {
center: math::Point::new(arc.center.x, arc.center.y),
radii: math::Vector::new(arc.radii.x, arc.radii.y),
x_rotation: math::Angle::radians(arc.rotation),
start_angle: math::Angle::radians(arc.start_angle),
sweep_angle: math::Angle::radians(arc.end_angle - arc.start_angle),
};
let _ = self.raw.move_to(arc.sample(0.0));
arc.for_each_quadratic_bezier(&mut |curve| {
let _ = self.raw.quadratic_bezier_to(curve.ctrl, curve.to);
});
}
#[inline]
pub fn bezier_curve_to(
&mut self,
control_a: Point,
control_b: Point,
to: Point,
) {
use lyon::math;
let _ = self.raw.cubic_bezier_to(
math::Point::new(control_a.x, control_a.y),
math::Point::new(control_b.x, control_b.y),
math::Point::new(to.x, to.y),
);
}
#[inline]
pub fn quadratic_curve_to(&mut self, control: Point, to: Point) {
use lyon::math;
let _ = self.raw.quadratic_bezier_to(
math::Point::new(control.x, control.y),
math::Point::new(to.x, to.y),
);
}
#[inline]
pub fn rectangle(&mut self, top_left: Point, size: Size) {
self.move_to(top_left);
self.line_to(Point::new(top_left.x + size.width, top_left.y));
self.line_to(Point::new(
top_left.x + size.width,
top_left.y + size.height,
));
self.line_to(Point::new(top_left.x, top_left.y + size.height));
self.close();
}
#[inline]
pub fn circle(&mut self, center: Point, radius: f32) {
self.arc(Arc {
center,
radius,
start_angle: 0.0,
end_angle: 2.0 * std::f32::consts::PI,
});
}
#[inline]
pub fn close(&mut self) {
self.raw.close()
}
#[inline]
pub fn build(self) -> Path {
Path {
raw: self.raw.build(),
}
}
}
impl Default for Builder {
fn default() -> Self {
Self::new()
}
}