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
use lyon_geom::{Box2D, CubicBezierSegment, Point, QuadraticBezierSegment, SvgArc};

use super::Turtle;

/// Generates a bounding box for all draw operations, used to properly apply [crate::ConversionConfig::origin]
#[derive(Debug, Default)]
pub struct PreprocessTurtle {
    pub bounding_box: Box2D<f64>,
}

impl Turtle for PreprocessTurtle {
    fn begin(&mut self) {}

    fn end(&mut self) {}

    fn comment(&mut self, _comment: String) {}

    fn move_to(&mut self, to: Point<f64>) {
        self.bounding_box = Box2D::from_points([self.bounding_box.min, self.bounding_box.max, to]);
    }

    fn line_to(&mut self, to: Point<f64>) {
        self.bounding_box = Box2D::from_points([self.bounding_box.min, self.bounding_box.max, to]);
    }

    fn arc(&mut self, svg_arc: SvgArc<f64>) {
        if svg_arc.is_straight_line() {
            self.line_to(svg_arc.to);
        } else {
            self.bounding_box = self.bounding_box.union(&svg_arc.to_arc().bounding_box());
        }
    }

    fn cubic_bezier(&mut self, cbs: CubicBezierSegment<f64>) {
        self.bounding_box = self.bounding_box.union(&cbs.bounding_box());
    }

    fn quadratic_bezier(&mut self, qbs: QuadraticBezierSegment<f64>) {
        self.bounding_box = self.bounding_box.union(&qbs.bounding_box());
    }
}