Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Fix zero tangent angle in Type on a Path at collapsed handles

NetterSilver3871100e3f81
diff --git a/crates/geom/src/warp.rs b/crates/geom/src/warp.rs
index dacdedd06..6dd0cda9b 100644
--- a/crates/geom/src/warp.rs
+++ b/crates/geom/src/warp.rs
@@ -2,6 +2,20 @@
use kurbo::{Affine, BezPath, ParamCurve, ParamCurveArclen, ParamCurveDeriv, PathSeg, Point};
+/// Direction at a curve end whose derivative vanishes: from the end anchor towards the nearest
+/// control point that differs from it (`at_end` selects the last anchor).
+fn first_distinct(pts: &[Point], at_end: bool) -> kurbo::Vec2 {
+ let n = pts.len();
+ for k in 1..n {
+ let (a, b) = if at_end { (pts[n - 1 - k], pts[n - 1]) } else { (pts[0], pts[k]) };
+ let v = b - a;
+ if v.hypot2() > 1e-18 {
+ return v;
+ }
+ }
+ kurbo::Vec2::ZERO
+}
+
/// A path measured for placing glyphs along it.
#[derive(Clone, Debug)]
pub struct PathWarp {
@@ -29,12 +43,24 @@ impl PathWarp {
let n = self.segs.len();
for (i, (seg, len)) in self.segs.iter().enumerate() {
if s <= *len || i + 1 == n {
- let t = if *len > 1e-9 { seg.inv_arclen(s.min(*len), 1e-4) } else { 0.0 };
+ let t = if *len <= 1e-9 || s <= 0.0 {
+ 0.0
+ } else if s >= *len {
+ 1.0
+ } else {
+ seg.inv_arclen(s, 1e-4)
+ };
let p = seg.eval(t);
let d = match seg {
PathSeg::Line(l) => l.p1 - l.p0,
- PathSeg::Quad(q) => q.deriv().eval(t).to_vec2(),
- PathSeg::Cubic(c) => c.deriv().eval(t).to_vec2(),
+ PathSeg::Quad(q) => {
+ let d = q.deriv().eval(t).to_vec2();
+ if d.hypot2() > 1e-18 { d } else { first_distinct(&[q.p0, q.p1, q.p2], t > 0.5) }
+ }
+ PathSeg::Cubic(c) => {
+ let d = c.deriv().eval(t).to_vec2();
+ if d.hypot2() > 1e-18 { d } else { first_distinct(&[c.p0, c.p1, c.p2, c.p3], t > 0.5) }
+ }
};
let mut a = d.y.atan2(d.x);
if self.flip {
@@ -98,4 +124,19 @@ mod tests {
assert!((w.nearest(Point::new(30.0, -8.0)) - 30.0).abs() < 1e-6);
assert!((flipped.nearest(Point::new(30.0, -8.0)) - 170.0).abs() < 1e-6);
}
+
+ #[test]
+ fn collapsed_handles_fall_back_to_the_next_control_point() {
+ let half_pi = std::f64::consts::FRAC_PI_2;
+ let mut bp = BezPath::new();
+ bp.move_to((0.0, 0.0));
+ bp.curve_to((0.0, 0.0), (0.0, 10.0), (10.0, 10.0));
+ let w = PathWarp::new(&bp, false);
+ assert!((w.at(0.0).unwrap().1 - half_pi).abs() < 1e-9, "start handle on the anchor");
+ let mut bp = BezPath::new();
+ bp.move_to((0.0, 0.0));
+ bp.curve_to((10.0, 0.0), (10.0, 10.0), (10.0, 10.0));
+ let w = PathWarp::new(&bp, false);
+ assert!((w.at(w.length()).unwrap().1 - half_pi).abs() < 1e-9, "end handle on the anchor");
+ }
}