From 2fb0bba408b97a3793f39c8e762bda85daf6f931 Mon Sep 17 00:00:00 2001 From: Giacomo Stevanato Date: Fri, 12 Jul 2024 10:10:03 +0200 Subject: [PATCH 1/2] Add Arc::cast and QuadraticBezierSegment::cast --- crates/geom/src/arc.rs | 12 ++++++++++++ crates/geom/src/quadratic_bezier.rs | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/crates/geom/src/arc.rs b/crates/geom/src/arc.rs index 31dac3d7..4e64235f 100644 --- a/crates/geom/src/arc.rs +++ b/crates/geom/src/arc.rs @@ -3,6 +3,8 @@ use core::mem::swap; use core::ops::Range; +use num_traits::NumCast; + use crate::scalar::{cast, Float, Scalar}; use crate::segment::{BoundingBox, Segment}; use crate::{point, vector, Angle, Box2D, Point, Rotation, Transform, Vector}; @@ -31,6 +33,16 @@ pub struct SvgArc { } impl Arc { + pub fn cast(self) -> Arc { + Arc { + center: self.center.cast(), + radii: self.radii.cast(), + start_angle: self.start_angle.cast(), + sweep_angle: self.sweep_angle.cast(), + x_rotation: self.x_rotation.cast(), + } + } + /// Create simple circle. pub fn circle(center: Point, radius: S) -> Self { Arc { diff --git a/crates/geom/src/quadratic_bezier.rs b/crates/geom/src/quadratic_bezier.rs index 32f8afa8..07c3a48d 100644 --- a/crates/geom/src/quadratic_bezier.rs +++ b/crates/geom/src/quadratic_bezier.rs @@ -4,6 +4,7 @@ use crate::traits::Transformation; use crate::{point, Box2D, Point, Vector}; use crate::{CubicBezierSegment, Line, LineEquation, LineSegment, Triangle}; use arrayvec::ArrayVec; +use num_traits::NumCast; use core::mem; use core::ops::Range; @@ -22,6 +23,14 @@ pub struct QuadraticBezierSegment { } impl QuadraticBezierSegment { + pub fn cast(self) -> QuadraticBezierSegment { + QuadraticBezierSegment { + from: self.from.cast(), + ctrl: self.ctrl.cast(), + to: self.to.cast(), + } + } + /// Sample the curve at t (expecting t between 0 and 1). pub fn sample(&self, t: S) -> Point { let t2 = t * t; From 94248f66d4b0f8db5d5b3c11a3b14457fa3a280b Mon Sep 17 00:00:00 2001 From: Giacomo Stevanato Date: Fri, 12 Jul 2024 10:10:38 +0200 Subject: [PATCH 2/2] Svg builder: use f64 arcs when approximating with quadratic beziers --- crates/path/src/builder.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/path/src/builder.rs b/crates/path/src/builder.rs index 1f443fe5..827a0396 100644 --- a/crates/path/src/builder.rs +++ b/crates/path/src/builder.rs @@ -1303,7 +1303,8 @@ impl WithSvg { self.builder.line_to(arc_start, &self.attribute_buffer); } - arc.for_each_quadratic_bezier(&mut |curve| { + arc.cast::().for_each_quadratic_bezier(&mut |curve| { + let curve = curve.cast::(); self.builder .quadratic_bezier_to(curve.ctrl, curve.to, &self.attribute_buffer); self.current_position = curve.to;