1use std::fmt::Debug;
2
3use lyon_geom::{CubicBezierSegment, Point, QuadraticBezierSegment, SvgArc, Vector, point, vector};
4use uom::si::{
5 f64::Length,
6 length::{inch, millimeter},
7};
8
9use crate::Turtle;
10
11#[derive(Debug)]
13pub struct DpiConvertingTurtle<T: Turtle> {
14 pub dpi: f64,
15 pub inner: T,
16}
17
18impl<T: Turtle> DpiConvertingTurtle<T> {
19 fn to_mm(&self, value: f64) -> f64 {
20 Length::new::<inch>(value / self.dpi).get::<millimeter>()
21 }
22
23 fn point_to_mm(&self, p: Point<f64>) -> Point<f64> {
24 point(self.to_mm(p.x), self.to_mm(p.y))
25 }
26
27 fn vector_to_mm(&self, v: Vector<f64>) -> Vector<f64> {
28 vector(self.to_mm(v.x), self.to_mm(v.y))
29 }
30}
31
32impl<T: Turtle> Turtle for DpiConvertingTurtle<T> {
33 fn begin(&mut self) {
34 self.inner.begin()
35 }
36
37 fn end(&mut self) {
38 self.inner.end()
39 }
40
41 fn comment(&mut self, comment: String) {
42 self.inner.comment(comment)
43 }
44
45 fn move_to(&mut self, to: Point<f64>) {
46 self.inner.move_to(self.point_to_mm(to))
47 }
48
49 fn line_to(&mut self, to: Point<f64>) {
50 self.inner.line_to(self.point_to_mm(to))
51 }
52
53 fn arc(
54 &mut self,
55 SvgArc {
56 from,
57 to,
58 radii,
59 x_rotation,
60 flags,
61 }: SvgArc<f64>,
62 ) {
63 self.inner.arc(SvgArc {
64 from: self.point_to_mm(from),
65 to: self.point_to_mm(to),
66 radii: self.vector_to_mm(radii),
67 x_rotation,
68 flags,
69 })
70 }
71
72 fn cubic_bezier(
73 &mut self,
74 CubicBezierSegment {
75 from,
76 ctrl1,
77 ctrl2,
78 to,
79 }: CubicBezierSegment<f64>,
80 ) {
81 self.inner.cubic_bezier(CubicBezierSegment {
82 from: self.point_to_mm(from),
83 ctrl1: self.point_to_mm(ctrl1),
84 ctrl2: self.point_to_mm(ctrl2),
85 to: self.point_to_mm(to),
86 })
87 }
88
89 fn quadratic_bezier(
90 &mut self,
91 QuadraticBezierSegment { from, ctrl, to }: QuadraticBezierSegment<f64>,
92 ) {
93 self.inner.quadratic_bezier(QuadraticBezierSegment {
94 from: self.point_to_mm(from),
95 to: self.point_to_mm(to),
96 ctrl: self.point_to_mm(ctrl),
97 })
98 }
99}