Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use f64 when approximating SVG arcs with quadratic beziers #913

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/geom/src/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -31,6 +33,16 @@ pub struct SvgArc<S> {
}

impl<S: Scalar> Arc<S> {
pub fn cast<NewS: NumCast>(self) -> Arc<NewS> {
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<S>, radius: S) -> Self {
Arc {
Expand Down
9 changes: 9 additions & 0 deletions crates/geom/src/quadratic_bezier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +23,14 @@ pub struct QuadraticBezierSegment<S> {
}

impl<S: Scalar> QuadraticBezierSegment<S> {
pub fn cast<NewS: NumCast>(self) -> QuadraticBezierSegment<NewS> {
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<S> {
let t2 = t * t;
Expand Down
3 changes: 2 additions & 1 deletion crates/path/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,8 @@ impl<Builder: PathBuilder> WithSvg<Builder> {
self.builder.line_to(arc_start, &self.attribute_buffer);
}

arc.for_each_quadratic_bezier(&mut |curve| {
arc.cast::<f64>().for_each_quadratic_bezier(&mut |curve| {
let curve = curve.cast::<f32>();
self.builder
.quadratic_bezier_to(curve.ctrl, curve.to, &self.attribute_buffer);
self.current_position = curve.to;
Expand Down
Loading