1 | use crate::{point, OutlineCurve, Point}; |
2 | #[cfg (not(feature = "std" ))] |
3 | use alloc::vec::Vec; |
4 | |
5 | #[derive (Debug, Default)] |
6 | pub(crate) struct OutlineCurveBuilder { |
7 | last: Point, |
8 | last_move: Option<Point>, |
9 | outline: Vec<OutlineCurve>, |
10 | } |
11 | |
12 | impl OutlineCurveBuilder { |
13 | #[inline ] |
14 | pub(crate) fn take_outline(mut self) -> Vec<OutlineCurve> { |
15 | // some font glyphs implicitly close, e.g. Cantarell-VF.otf |
16 | owned_ttf_parser::OutlineBuilder::close(&mut self); |
17 | self.outline |
18 | } |
19 | } |
20 | |
21 | impl owned_ttf_parser::OutlineBuilder for OutlineCurveBuilder { |
22 | #[inline ] |
23 | fn move_to(&mut self, x: f32, y: f32) { |
24 | // eprintln!("M {x} {y}"); |
25 | self.last = point(x, y); |
26 | self.last_move = Some(self.last); |
27 | } |
28 | |
29 | #[inline ] |
30 | fn line_to(&mut self, x1: f32, y1: f32) { |
31 | // eprintln!("L {x1} {y1}"); |
32 | let p1 = point(x1, y1); |
33 | self.outline.push(OutlineCurve::Line(self.last, p1)); |
34 | self.last = p1; |
35 | } |
36 | |
37 | #[inline ] |
38 | fn quad_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32) { |
39 | // eprintln!("Q {x1} {y1}"); |
40 | let p1 = point(x1, y1); |
41 | let p2 = point(x2, y2); |
42 | self.outline.push(OutlineCurve::Quad(self.last, p1, p2)); |
43 | self.last = p2; |
44 | } |
45 | |
46 | #[inline ] |
47 | fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32) { |
48 | // eprintln!("C {x1} {y1} {x3} {y3}"); |
49 | let p1 = point(x1, y1); |
50 | let p2 = point(x2, y2); |
51 | let p3 = point(x3, y3); |
52 | |
53 | self.outline |
54 | .push(OutlineCurve::Cubic(self.last, p1, p2, p3)); |
55 | self.last = p3; |
56 | } |
57 | |
58 | #[inline ] |
59 | fn close(&mut self) { |
60 | // eprintln!("Z"); |
61 | if let Some(m) = self.last_move.take() { |
62 | self.outline.push(OutlineCurve::Line(self.last, m)); |
63 | } |
64 | } |
65 | } |
66 | |