Skip to main content

tumo_path/path/
vec.rs

1use super::*;
2
3/// A PathCore implementation that stores its data in vectors.
4/// 
5/// This path type is useful for creating paths with a dynamic size and can be
6/// used to store paths in a vector.
7///
8#[derive(Debug, Clone, PartialEq, Hash, Default)]
9pub struct Path {
10	pub verbs: Vec<PathVerb>,
11	pub points: Vec<Point>,
12}
13impl Path {
14	#[inline(always)]
15	pub fn iter(&self) -> PathIter<'_> {
16		self.into_iter()
17	}
18	#[inline]
19	pub fn clear(&mut self) {
20		self.verbs.clear();
21		self.points.clear();
22	}
23	#[inline]
24	pub fn reset(&mut self) -> &mut Self {
25		self.clear();
26		self
27	}
28	#[inline]
29	pub fn len(&self) -> (usize, usize) {
30		(self.verbs.len(), self.points.len())
31	}
32	#[inline]
33	pub fn slice<'a>(&'a self, verb_range: core::ops::Range<usize>, point_range: core::ops::Range<usize>) -> PathSlice<'a> {
34		PathSlice(&self.verbs[verb_range], &self.points[point_range])
35	}
36}
37
38impl PathCore for Path {
39	fn current(&self) -> Point {
40		self.points.last().copied().unwrap_or_default()
41	}
42	fn move_to_impl(&mut self, to: Point) -> &mut Self {
43		self.verbs.push(PathVerb::MoveTo);
44		self.points.push(to);
45		self
46	}
47	fn line_to_impl(&mut self, to: Point) -> &mut Self {
48		self.verbs.push(PathVerb::LineTo);
49		self.points.push(to);
50		self
51	}
52	fn quadratic_to_impl(&mut self, c1: Point, to: Point) -> &mut Self {
53		self.verbs.push(PathVerb::QuadraticTo);
54		self.points.push(c1);
55		self.points.push(to);
56		self
57	}
58	fn cubic_to_impl(&mut self, c1: Point, c2: Point, to: Point) -> &mut Self {
59		self.verbs.push(PathVerb::CubicTo);
60		self.points.push(c1);
61		self.points.push(c2);
62		self.points.push(to);
63		self
64	}
65	fn close(&mut self) -> &mut Self {
66		self.verbs.push(PathVerb::Close);
67		self
68	}
69}
70
71impl PathTransform for Path {
72	fn transform(&mut self, transform: &Transform) {
73		for point in &mut self.points {
74			*point = transform.transform_point(*point);
75		}
76	}
77}
78
79impl core::iter::FromIterator<PathCmd> for Path {
80	fn from_iter<T: IntoIterator<Item = PathCmd>>(iter: T) -> Self {
81		let mut this = Self::default();
82		this.extend(iter);
83		this
84	}
85}
86
87impl core::iter::Extend<PathCmd> for Path {
88	fn extend<T: IntoIterator<Item = PathCmd>>(&mut self, iter: T) {
89		for cmd in iter {
90			cmd.apply(self);
91		}
92	}
93}
94
95impl<'a> core::iter::IntoIterator for &'a Path {
96	type Item = PathCmd;
97	type IntoIter = PathIter<'a>;
98	fn into_iter(self) -> Self::IntoIter {
99		PathIter {
100			verbs: self.verbs.iter(),
101			points: self.points.iter(),
102		}
103	}
104}