Skip to main content

tumo_path/raster/
path.rs

1use super::*;
2
3use crate::{PathCmd, PathCore};
4
5#[derive(Debug)]
6pub struct FixedPath<P: FixedCore> {
7	p: P,
8	current: Point,
9	start: FixedPoint,
10	closed: bool,
11}
12impl<P: FixedCore> FixedPath<P> {
13	pub fn new(p: P) -> Self {
14		Self {
15			p,
16			current: Point::ZERO,
17			start: FixedPoint::ZERO,
18			closed: true,
19		}
20	}
21	pub fn take(mut self) -> P {
22		if !self.closed {
23			self.p.line_to_fixed(self.start);
24		}
25		self.p
26	}
27}
28impl<P: FixedCore> PathCore for FixedPath<P> {
29	fn current(&self) -> Point {
30		self.current
31	}
32	#[inline]
33	fn move_to_impl(&mut self, to: Point) -> &mut Self {
34		if !self.closed {
35			self.p.line_to_fixed(self.start);
36		}
37		self.start = to.into();
38		self.p.move_to_fixed(self.start);
39		self.current = to;
40		self.closed = false;
41		self
42	}
43	#[inline]
44	fn line_to_impl(&mut self, to: Point) -> &mut Self {
45		self.p.line_to_fixed(to.into());
46		self.current = to;
47		self.closed = false;
48		self
49	}
50	#[inline]
51	fn quadratic_to_impl(&mut self, c1: Point, to: Point) -> &mut Self {
52		self.p.quadratic_to_fixed(c1.into(), to.into());
53		self.current = to;
54		self.closed = false;
55		self
56	}
57	#[inline]
58	fn cubic_to_impl(&mut self, c1: Point, c2: Point, to: Point) -> &mut Self {
59		self.p.cubic_to_fixed(c1.into(), c2.into(), to.into());
60		self.current = to;
61		self.closed = false;
62		self
63	}
64	#[inline]
65	fn close(&mut self) -> &mut Self {
66		if !self.closed {
67			self.p.line_to_fixed(self.start);
68		}
69		self.closed = true;
70		self
71	}
72}
73impl<P: FixedCore> core::iter::Extend<PathCmd> for FixedPath<P> {
74	fn extend<T: IntoIterator<Item = PathCmd>>(&mut self, iter: T) {
75		for cmd in iter {
76			match cmd {
77				PathCmd::MoveTo(to) => self.move_to(to),
78				PathCmd::LineTo(to) => self.line_to(to),
79				PathCmd::QuadraticTo(c1, to) => self.quadratic_to(c1, to),
80				PathCmd::CubicTo(c1, c2, to) => self.cubic_to(c1, c2, to),
81				PathCmd::Close => self.close(),
82			};
83		}
84	}
85}