diff --git a/ROADMAP.md b/ROADMAP.md index f74399f93a..8b29a40471 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -124,6 +124,8 @@ Ranked; detail in [docs/roadmap.md](docs/roadmap.md) and [docs/gaps.md](docs/gap ## Progress log +- 2026-12-03: View › Show Curvature Comb (`view.curvatureComb`) draws the curvature comb of the selected paths and compound paths: teeth along the normal as long as the curvature, and a line through their tips (#10659). Headline numbers unchanged. + - 2026-12-02: The desktop Print dialog looks up installed printers in the background. Named printers become selectable after the lookup returns and the dialog applies the list. PDF File is available at once. Physical printing waits for the list to be applied, and Done can save settings while the lookup runs. The dialog shows and announces discovery and disables physical Print until the list is applied. - 2026-11-20: Color conversion indexes global swatch bases and linked-paint lookups (#8804); scores, estimates and measured budgets unchanged. diff --git a/crates/engine/src/cmd/plugin.rs b/crates/engine/src/cmd/plugin.rs index e2ffb44d0c..33b38e567b 100644 --- a/crates/engine/src/cmd/plugin.rs +++ b/crates/engine/src/cmd/plugin.rs @@ -980,6 +980,8 @@ pub const DESKTOP_MENU_ITEMS: &[(&[&str], &str)] = &[ (&["View"], "Show Corner Widget"), (&["View"], "Show Live Paint Gaps"), (&["View"], "Hide Live Paint Gaps"), + (&["View"], "Show Curvature Comb"), + (&["View"], "Hide Curvature Comb"), (&["View"], "Guides"), (&["View"], "Smart Guides"), (&["View"], "Perspective Grid"), diff --git a/crates/geom/src/curvature.rs b/crates/geom/src/curvature.rs index 0a56d6101c..ac6f721b85 100644 --- a/crates/geom/src/curvature.rs +++ b/crates/geom/src/curvature.rs @@ -219,12 +219,333 @@ pub fn circular_arc(c: CubicBez) -> Option<(Point, f64)> { ((l0 - handle).abs() <= HANDLE_TOL * handle && (l3 - handle).abs() <= HANDLE_TOL * handle).then_some((center, radius)) } +/// One tooth of a curvature comb: it starts on the curve at `at` and ends at `tip`, along the +/// curve's normal on the side away from its center of curvature, `scale` times the curvature long. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct CombTooth { + pub at: Point, + pub tip: Point, + /// The first tooth of the comb, the first after a point or segment that has no tooth, or the + /// first after the curve turns back on itself (a cusp between two teeth): a line through the + /// tips starts again here. + pub start: bool, + /// The index of the segment the tooth sits on. + pub segment: usize, +} + +/// The most teeth a curvature comb draws in all. +pub const MAX_COMB_TEETH: usize = 20_000; + +/// The curvature comb of `sp`: `samples` teeth on each segment (at least 2), evenly spaced in `t` +/// from 0 to 1, each `scale` times the signed curvature long ([`CombTooth`]). A straight segment +/// gives teeth of no length, its ends included. A point of a curved segment where it has no +/// direction (a handle drawn back onto its anchor) gives no tooth, and neither does a segment +/// with no length, a segment whose points are not finite, or a `scale` that is not finite. When +/// `samples` teeth on every segment would pass `max_teeth`, every segment gets 2 teeth, and when +/// that would still pass it, only every k-th segment gets teeth, spread along the whole subpath. +pub fn curvature_comb(sp: &SubPath, samples: usize, scale: f64, max_teeth: usize) -> Vec { + let n = sp.segment_count(); + if n == 0 || !scale.is_finite() || max_teeth < 2 { + return vec![]; + } + let per = if samples.max(2).saturating_mul(n) <= max_teeth { samples.max(2) } else { 2 }; + let stride = (per * n).div_ceil(max_teeth).max(1); + let mut teeth = Vec::with_capacity((per * n / stride).min(max_teeth)); + let mut start = true; + // The direction of the last curved tooth, carried across segments so that a cusp at an + // anchor also starts a new line through the tips. + let mut previous_direction: Option = None; + for seg in (0..n).step_by(stride) { + if stride > 1 { + start = true; + previous_direction = None; + } + let c = sp.segment(seg); + if ![c.p0, c.p1, c.p2, c.p3].iter().all(|p| p.is_finite()) { + start = true; + previous_direction = None; + continue; + } + let (d1, d2) = (c.deriv(), c.deriv().deriv()); + // The control polygon's length sets what counts as no speed. + let size = (c.p1 - c.p0).hypot() + (c.p2 - c.p1).hypot() + (c.p3 - c.p2).hypot(); + let chord = c.p3 - c.p0; + let line = sp.segment_is_line(seg) && chord.hypot() > 0.0; + for i in 0..per { + if teeth.len() >= max_teeth { + return teeth; + } + let t = i as f64 / (per - 1) as f64; + if line { + // A line stored with its handles on its anchors has no speed at its ends, but a + // direction and no curvature all along. + let at = c.eval(t); + previous_direction = None; + if !at.is_finite() { + start = true; + continue; + } + teeth.push(CombTooth { at, tip: at, start, segment: seg }); + start = false; + continue; + } + let (v, a) = (d1.eval(t).to_vec2(), d2.eval(t).to_vec2()); + let speed = v.hypot(); + if speed <= CUSP_SHARE * size || !speed.is_finite() { + start = true; + previous_direction = None; + continue; + } + let k = v.cross(a) / (speed * speed * speed); + let direction = v / speed; + start |= previous_direction.is_some_and(|previous| previous.dot(direction) < 0.0); + previous_direction = Some(direction); + let normal = Vec2::new(-direction.y, direction.x); + let at = c.eval(t); + let tip = at - normal * (k * scale); + if !at.is_finite() || !tip.is_finite() { + start = true; + previous_direction = None; + continue; + } + teeth.push(CombTooth { at, tip, start, segment: seg }); + start = false; + } + } + teeth +} + +/// Does the line through the tips of `teeth`, the comb of `sp` ([`curvature_comb`]), close where +/// it starts: `sp` is closed, only segments with no length lie between its last tooth and its +/// first across the seam, the two sit on the same point, and the curve does not turn back on itself at its +/// first anchor. A comb that skips segments to keep to its budget does not close across the ones +/// it skipped at the end. +pub fn comb_closes(sp: &SubPath, teeth: &[CombTooth]) -> bool { + let (Some(first), Some(last)) = (teeth.first(), teeth.last()) else { + return false; + }; + let n = sp.segment_count(); + if !sp.closed || teeth.len() < 2 || first.at.distance(last.at) >= 1e-9 { + return false; + } + // Only segments with no length may lie between the last tooth and the first across the seam. + let no_length = |i: usize| start_direction(sp.segment(i)) == Vec2::ZERO; + if !(last.segment + 1..n).chain(0..first.segment).all(no_length) { + return false; + } + match (neighbor(sp, 0, false), neighbor(sp, n.saturating_sub(1), true)) { + (Some((into, _)), Some((out, _))) => end_direction(into).dot(start_direction(out)) >= 0.0, + _ => false, + } +} + #[cfg(test)] mod tests { use super::*; use crate::{Anchor, shapes}; use kurbo::Rect; + fn tooth_len(t: &CombTooth) -> f64 { + t.at.distance(t.tip) + } + + /// A circle's comb points away from the center, each tooth `scale` times the cubic's own + /// curvature at its point ([`curvature_circle_with`] without arcs), all within 3% of + /// `scale / r` (the usual cubic of a quarter turn strays that little from its circle). + #[test] + fn the_comb_of_a_circle_has_teeth_of_one_length() { + let pd = shapes::ellipse(Rect::new(50.0, 50.0, 250.0, 250.0)); + let sp = &pd.subpaths[0]; + let teeth = curvature_comb(sp, 9, 1000.0, MAX_COMB_TEETH); + assert_eq!(teeth.len(), 9 * sp.segment_count()); + assert_eq!(teeth.iter().filter(|t| t.start).count(), 1, "one unbroken comb"); + let center = Point::new(150.0, 150.0); + for (i, t) in teeth.iter().enumerate() { + let (seg, at) = (i / 9, (i % 9) as f64 / 8.0); + let r = curvature_circle_with(sp.segment(seg), at, false, false).unwrap().radius; + assert!((tooth_len(t) * r - 1000.0).abs() < 1e-6, "{t:?} {r}"); + assert!((tooth_len(t) - 10.0).abs() < 0.3, "{t:?}"); + assert!(t.tip.distance(center) > t.at.distance(center), "outward: {t:?}"); + } + } + + /// A straight line has teeth of no length, its two ends included. + #[test] + fn the_comb_of_a_line_is_flat() { + let sp = SubPath::polyline(&[Point::new(0.0, 0.0), Point::new(100.0, 40.0), Point::new(200.0, 0.0)], false); + let teeth = curvature_comb(&sp, 5, 1000.0, MAX_COMB_TEETH); + assert_eq!(teeth.len(), 10); + assert!(teeth.iter().all(|t| tooth_len(t) < 1e-9), "{teeth:?}"); + assert_eq!(teeth[4].at, Point::new(100.0, 40.0), "the corner has a tooth on each side"); + } + + /// Two quarter circles of radius 100 and 50 that meet in one direction: the teeth double in + /// length at the anchor they share. + #[test] + fn the_comb_jumps_where_the_curvature_does() { + let k = 4.0 / 3.0 * (std::f64::consts::FRAC_PI_8).tan(); + let a0 = Anchor { h_out: Point::new(100.0 * k, 0.0), ..Anchor::corner(Point::new(0.0, 0.0)) }; + let a1 = Anchor { + h_in: Point::new(100.0, 100.0 - 100.0 * k), + h_out: Point::new(100.0, 100.0 + 50.0 * k), + ..Anchor::corner(Point::new(100.0, 100.0)) + }; + let a2 = Anchor { h_in: Point::new(50.0 + 50.0 * k, 150.0), ..Anchor::corner(Point::new(50.0, 150.0)) }; + let sp = SubPath::new(vec![a0, a1, a2], false); + let teeth = curvature_comb(&sp, 3, 1000.0, MAX_COMB_TEETH); + assert_eq!(teeth.len(), 6); + let (before, after) = (tooth_len(&teeth[2]), tooth_len(&teeth[3])); + assert!((after / before - 2.0).abs() < 1e-9, "{before} {after}"); + assert!((before - 10.0).abs() < 0.3, "{before}"); + assert!(teeth[2].at.distance(teeth[3].at) < 1e-9, "both teeth start on the shared anchor"); + assert_eq!(teeth.iter().filter(|t| t.start).count(), 1, "a smooth anchor keeps one unbroken comb"); + } + + /// Two segments that meet at a cusp on their shared anchor: the first segment ends heading + /// down and the second starts heading up, so the line through the tips starts again there. + #[test] + fn the_comb_breaks_at_a_cusp_on_an_anchor() { + let a0 = Anchor { h_out: Point::new(0.0, -50.0), ..Anchor::corner(Point::new(0.0, 0.0)) }; + let a1 = Anchor { h_in: Point::new(100.0, -50.0), h_out: Point::new(100.0, -25.0), ..Anchor::corner(Point::new(100.0, 0.0)) }; + let a2 = Anchor { h_in: Point::new(200.0, -25.0), ..Anchor::corner(Point::new(200.0, 0.0)) }; + let sp = SubPath::new(vec![a0, a1, a2], false); + let teeth = curvature_comb(&sp, 3, 1000.0, MAX_COMB_TEETH); + assert_eq!(teeth.len(), 6); + assert_eq!(teeth.iter().map(|t| t.start).collect::>(), [true, false, false, true, false, false]); + assert!(!comb_closes(&sp, &teeth), "an open subpath does not close"); + } + + /// A closed comb closes at its first anchor when the curve goes on smoothly there, even with a + /// cusp elsewhere, and stays open when the first anchor is the cusp. + #[test] + fn the_comb_closes_unless_its_first_anchor_is_a_cusp() { + let closed = |h_in: Point| { + let a0 = Anchor { h_in, h_out: Point::new(0.0, -50.0), ..Anchor::corner(Point::new(0.0, 0.0)) }; + let a1 = Anchor { h_in: Point::new(100.0, -50.0), h_out: Point::new(100.0, -25.0), ..Anchor::corner(Point::new(100.0, 0.0)) }; + let a2 = Anchor { h_in: Point::new(200.0, -25.0), h_out: Point::new(200.0, 50.0), ..Anchor::corner(Point::new(200.0, 0.0)) }; + SubPath::new(vec![a0, a1, a2], true) + }; + let smooth = closed(Point::new(0.0, 50.0)); + let teeth = curvature_comb(&smooth, 3, 1000.0, MAX_COMB_TEETH); + assert_eq!(teeth.iter().filter(|t| t.start).count(), 2, "the cusp at the second anchor breaks the comb"); + assert!(comb_closes(&smooth, &teeth)); + let cusp = closed(Point::new(0.0, -50.0)); + assert!(!comb_closes(&cusp, &curvature_comb(&cusp, 3, 1000.0, MAX_COMB_TEETH))); + let circle = shapes::ellipse(Rect::new(0.0, 0.0, 100.0, 100.0)); + let sp = &circle.subpaths[0]; + assert!(comb_closes(sp, &curvature_comb(sp, 5, 1000.0, MAX_COMB_TEETH))); + } + + /// A segment with no length and a handle drawn back onto its anchor do not panic and give no + /// tooth where the curve has no direction; a scale that is not finite gives no comb. + #[test] + fn the_comb_skips_points_with_no_direction() { + let p = Point::new(10.0, 10.0); + assert!(curvature_comb(&SubPath::polyline(&[p, p], false), 4, 1000.0, MAX_COMB_TEETH).is_empty()); + let curve = SubPath::new( + vec![Anchor { h_out: Point::new(50.0, 0.0), ..Anchor::corner(Point::new(0.0, 0.0)) }, Anchor::corner(Point::new(100.0, 100.0))], + false, + ); + let teeth = curvature_comb(&curve, 5, 1000.0, MAX_COMB_TEETH); + assert_eq!(teeth.len(), 4, "the end whose handle sits on its anchor has no tooth: {teeth:?}"); + assert_eq!(teeth.iter().map(|t| t.start).collect::>(), [true, false, false, false]); + let back = SubPath::new( + vec![Anchor::corner(Point::new(0.0, 0.0)), Anchor { h_in: Point::new(50.0, 0.0), ..Anchor::corner(Point::new(100.0, 100.0)) }], + false, + ); + let teeth = curvature_comb(&back, 5, 1000.0, MAX_COMB_TEETH); + assert_eq!(teeth.len(), 4, "the start, whose handle sits on its anchor, has no tooth: {teeth:?}"); + assert!(teeth[0].start, "the comb starts after the point with no tooth"); + assert!(teeth.iter().all(|t| t.at.is_finite() && t.tip.is_finite()), "{teeth:?}"); + assert!(curvature_comb(&curve, 5, f64::NAN, MAX_COMB_TEETH).is_empty()); + } + + /// A cusp between samples starts a new line through the comb's tips after the cusp. + #[test] + fn the_comb_breaks_at_a_cusp_between_samples() { + let bp = kurbo::BezPath::from_svg("M0 0 C100 100 0 100 100 0").unwrap(); + let sp = &crate::PathData::from_bezpath(&bp).subpaths[0]; + let teeth = curvature_comb(sp, 6, 1000.0, MAX_COMB_TEETH); + assert_eq!(teeth.len(), 6, "the cusp falls between teeth"); + assert_eq!(teeth.iter().map(|t| t.start).collect::>(), [true, false, false, true, false, false]); + assert!(teeth.iter().all(|t| t.at.is_finite() && t.tip.is_finite()), "{teeth:?}"); + } + + /// A tooth whose tip overflows is skipped, and the next finite tooth starts a new envelope. + #[test] + fn the_comb_breaks_after_a_nonfinite_tip() { + let sp = SubPath::new( + vec![ + Anchor::with_handles(Point::new(-1.0, 1.0), Point::new(-1.0, 1.0), Point::new(-1.0 / 3.0, -1.0 / 3.0)), + Anchor::with_handles(Point::new(1.0, 1.0), Point::new(1.0 / 3.0, -1.0 / 3.0), Point::new(1.0, 1.0)), + ], + false, + ); + let teeth = curvature_comb(&sp, 5, f64::MAX, MAX_COMB_TEETH); + assert_eq!(teeth.len(), 4, "the middle tooth overflows"); + assert!(teeth.iter().all(|t| t.at.is_finite() && t.tip.is_finite()), "{teeth:?}"); + assert_eq!(teeth.iter().map(|t| t.start).collect::>(), [true, false, true, false]); + assert!(teeth[1].at.x < 0.0 && teeth[2].at.x > 0.0, "the gap is inside the segment"); + } + + /// A speed whose cube underflows gives no tooth, and the first finite tooth starts the comb. + #[test] + fn the_comb_skips_a_tip_when_speed_cubed_underflows() { + let sp = SubPath::new( + vec![ + Anchor::with_handles(Point::ORIGIN, Point::ORIGIN, Point::new(1e-109, 0.0)), + Anchor::with_handles(Point::ORIGIN, Point::new(1e-101, 1e-101), Point::ORIGIN), + ], + false, + ); + let teeth = curvature_comb(&sp, 5, 1.0, MAX_COMB_TEETH); + assert_eq!(teeth.len(), 4, "the first tooth has a nonfinite tip"); + assert!(teeth.iter().all(|t| t.at.is_finite() && t.tip.is_finite()), "{teeth:?}"); + assert!(teeth[0].start && teeth[0].at.x > 0.0, "the comb starts after the skipped tooth"); + } + + /// A subpath with more segments than the budget allows keeps to it, with teeth spread to its + /// far end rather than stopping part way. + #[test] + fn the_comb_is_capped_and_spread() { + let points: Vec = (0..30_001).map(|i| Point::new(i as f64, (i % 2) as f64)).collect(); + let sp = SubPath::polyline(&points, false); + let teeth = curvature_comb(&sp, 16, 1.0, 1000); + assert!(teeth.len() <= 1000 && teeth.len() >= 900, "{}", teeth.len()); + assert!(teeth.last().is_some_and(|t| t.at.x > 29_000.0), "{:?}", teeth.last()); + assert_eq!(teeth.iter().filter(|t| t.start).count() * 2, teeth.len(), "each sampled segment starts a new line through the tips"); + let few = curvature_comb(&sp, 16, 1.0, 100_000); + assert_eq!(few.len(), 60_000, "too many for 16 on each, so 2 on each"); + } + + /// A capped comb that skips the last segment does not close, even when the last segment it + /// samples ends on the first anchor: the line through the tips would cross the skipped loop. + #[test] + fn a_capped_comb_does_not_close_across_a_skipped_segment() { + let sp = SubPath::new( + vec![ + Anchor::with_handles(Point::new(0.0, 0.0), Point::new(-50.0, -50.0), Point::new(50.0, 50.0)), + Anchor::with_handles(Point::new(100.0, 0.0), Point::new(80.0, 40.0), Point::new(120.0, 20.0)), + Anchor::with_handles(Point::new(100.0, 100.0), Point::new(140.0, 80.0), Point::new(60.0, 140.0)), + Anchor::with_handles(Point::new(0.0, 0.0), Point::new(30.0, 120.0), Point::new(-60.0, 60.0)), + ], + true, + ); + let teeth = curvature_comb(&sp, 16, 1000.0, 4); + let (first, last) = (&teeth[0], &teeth[teeth.len() - 1]); + assert!(last.segment < 3, "the closing segment is skipped: {teeth:?}"); + assert!(first.at.distance(last.at) < 1e-9, "the last sampled tooth sits on the first anchor"); + assert!(!comb_closes(&sp, &teeth), "the comb does not close across the skipped last segment"); + assert!(comb_closes(&sp, &curvature_comb(&sp, 16, 1000.0, MAX_COMB_TEETH)), "the full comb closes"); + let mut anchors: Vec = sp.anchors.iter().copied().collect(); + anchors[0].h_in = anchors[0].p; + anchors.insert(0, Anchor { h_in: Point::new(-50.0, -50.0), ..Anchor::corner(Point::new(0.0, 0.0)) }); + let seam = SubPath::new(anchors, true); + let full = curvature_comb(&seam, 16, 1000.0, MAX_COMB_TEETH); + assert_eq!(full.first().map(|t| t.segment), Some(1), "the segment of no length at the seam has no teeth"); + assert!(comb_closes(&seam, &full), "the comb closes across a segment of no length"); + } + /// Every segment of a circle drawn by [`shapes::ellipse`] is the usual cubic of a quarter /// turn, and the circle at any point of it is the circle itself: radius 100, center (150, 150). #[test] diff --git a/crates/mcp/src/ui_catalog.rs b/crates/mcp/src/ui_catalog.rs index 1f0c290191..618c9d7529 100644 --- a/crates/mcp/src/ui_catalog.rs +++ b/crates/mcp/src/ui_catalog.rs @@ -261,6 +261,12 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "", "{} toggle drawing the gaps of the selected Live Paint groups (those livePaint.info lists) in each group's Gap Preview Color while edges show → true when on", ), + ( + "view.curvatureComb", + "Show Curvature Comb", + "", + "{} toggle drawing the curvature comb of the selected paths and compound paths while edges show: 16 teeth per segment along the normal, on the outer side of the bend, each as long as the curvature there (a radius of 100 screen points gives a 20-point tooth at any zoom), with a line through their tips in the layer color; about 20,000 teeth at most, fewer per segment on a large selection; a view setting, not saved in the document and not an undo step → true when on", + ), ( "type.hiddenCharacters", "Show Hidden Characters", diff --git a/crates/ui-egui/src/canvas/marks.rs b/crates/ui-egui/src/canvas/marks.rs index abe7c5a6db..9993669339 100644 --- a/crates/ui-egui/src/canvas/marks.rs +++ b/crates/ui-egui/src/canvas/marks.rs @@ -835,3 +835,65 @@ pub(in crate::canvas) fn live_paint_gap_overlays(app: &mut VectorcraftApp) -> Ve }; gaps.to_vec() } + +/// Teeth View → Show Curvature Comb draws on each segment. +const COMB_SAMPLES: usize = 16; +/// The comb's length at zoom 1, in screen points squared: a curve that bends with a radius of +/// 100 screen points has teeth 20 screen points long at any zoom. VectorCraft's own value. +const COMB_SCALE: f64 = 2000.0; + +/// View → Show Curvature Comb: on each selected path, and each path of a selected compound path, a +/// tooth along the normal at evenly spaced points of every segment, as long as the curvature there, +/// and a line through the teeth's tips, in the path's layer color. At most +/// [`vectorcraft_geom::curvature::MAX_COMB_TEETH`] teeth in all, shared among the subpaths by their +/// number of segments, so a large selection gets fewer teeth everywhere. +pub(in crate::canvas) fn curvature_comb_overlays(app: &VectorcraftApp, zoom: f64) -> Vec { + use vectorcraft_geom::curvature::{MAX_COMB_TEETH, comb_closes, curvature_comb}; + let Some(st) = app.session.active() else { return vec![] }; + if !(zoom.is_finite() && zoom > 0.0) { + return vec![]; + } + let scale = COMB_SCALE / (zoom * zoom); + let mut paths = vec![]; + for &id in &st.selection.objects { + let Some(n) = st.doc.node(id).filter(|n| !n.is_guide()) else { continue }; + let color = st.doc.layer_color(id); + match &n.kind { + NodeKind::Compound { children, .. } => paths.extend(children.iter().filter_map(|c| c.path_data()).map(|pd| (pd, color))), + _ => paths.extend(n.path_data().map(|pd| (pd, color))), + } + } + let all: Vec<_> = + paths.iter().flat_map(|(pd, color)| pd.subpaths.iter().map(move |sp| (sp, *color))).filter(|(sp, _)| sp.segment_count() > 0).collect(); + // With more subpaths than the teeth allow 2 each, every k-th subpath gets a comb. + let stride = (all.len() * 2).div_ceil(MAX_COMB_TEETH).max(1); + let chosen: Vec<_> = all.into_iter().step_by(stride).collect(); + // Each chosen subpath has 2 teeth set aside; the rest are shared by number of segments. + let total: usize = chosen.iter().map(|(sp, _)| sp.segment_count()).sum(); + let spare = MAX_COMB_TEETH.saturating_sub(2 * chosen.len()); + let mut o = vec![]; + for (sp, color) in chosen { + let extra = (spare as u128) * (sp.segment_count() as u128) / (total.max(1) as u128); + let share = 2usize.saturating_add(usize::try_from(extra).unwrap_or(usize::MAX)); + let teeth = curvature_comb(sp, COMB_SAMPLES, scale, share); + let mut envelope = BezPath::new(); + for t in &teeth { + o.push(Overlay::Line { a: t.at, b: t.tip, color, dashed: false }); + if t.start { + envelope.move_to(t.tip); + } else { + envelope.line_to(t.tip); + } + } + // A closed subpath combed all the way round ends where it starts: the line closes there. + if let Some(first) = teeth.first() + && comb_closes(sp, &teeth) + { + envelope.line_to(first.tip); + } + if teeth.len() > 1 { + o.push(Overlay::Path { path: envelope, color, width: 1.0, dashed: false }); + } + } + o +} diff --git a/crates/ui-egui/src/canvas/marks/tests.rs b/crates/ui-egui/src/canvas/marks/tests.rs index 29336a8ad8..621a7b67e2 100644 --- a/crates/ui-egui/src/canvas/marks/tests.rs +++ b/crates/ui-egui/src/canvas/marks/tests.rs @@ -359,6 +359,46 @@ fn live_paint_gaps_follow_the_selected_groups() { assert!(live_paint_gap_overlays(&mut app).is_empty()); } +/// View → Show Curvature Comb: a selected circle of radius 100 gets teeth 20 screen points long at +/// zoom 1 and 10 at zoom 2, where it shows twice as large (within 3%: the circle is made of cubics), +/// and a line through their tips; a compound path gets a comb on each of its paths; nothing +/// selected draws nothing. +#[test] +fn the_curvature_comb_follows_the_selection_and_the_zoom() { + let mut app = app(); + let id = app.session.execute("shape.ellipse", &json!({"x": 100, "y": 50, "width": 200, "height": 200})).unwrap()["id"].as_u64().unwrap(); + app.session.execute("select.set", &json!({"ids": [id]})).unwrap(); + let teeth = |o: &[Overlay], zoom: f64| -> Vec { + o.iter().filter_map(|o| if let Overlay::Line { a, b, .. } = o { Some(a.distance(*b) * zoom) } else { None }).collect() + }; + let at_one = teeth(&curvature_comb_overlays(&app, 1.0), 1.0); + for zoom in [1.0, 2.0] { + let o = curvature_comb_overlays(&app, zoom); + let got = teeth(&o, zoom); + assert_eq!(got.len(), 64, "16 teeth on each of the circle's four segments"); + let want = 2000.0 / (100.0 * zoom); + assert!(got.iter().all(|t| (t - want).abs() < 0.03 * want), "{zoom}: {got:?}"); + assert!(got.iter().zip(&at_one).all(|(t, one)| (t * zoom - one).abs() < 1e-9), "zoom divides each tooth's screen length"); + assert_eq!(o.iter().filter(|o| matches!(o, Overlay::Path { .. })).count(), 1, "one line through the tips"); + let Some(Overlay::Path { path, .. }) = o.iter().find(|o| matches!(o, Overlay::Path { .. })) else { panic!("no line through the tips") }; + let ends: Vec<_> = path.elements().iter().filter_map(|e| e.end_point()).collect(); + assert_eq!(ends.first(), ends.last(), "the closed circle's line through the tips closes"); + } + let other = app.session.execute("shape.ellipse", &json!({"x": 150, "y": 100, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap(); + app.session.execute("select.set", &json!({"ids": [id, other]})).unwrap(); + app.session.execute("object.compoundPath.make", &json!({})).unwrap(); + let o = curvature_comb_overlays(&app, 1.0); + assert_eq!(o.iter().filter(|o| matches!(o, Overlay::Path { .. })).count(), 2, "the compound path's two paths"); + assert_eq!(teeth(&o, 1.0).len(), 128); + app.session.execute("select.set", &json!({"ids": []})).unwrap(); + assert!(curvature_comb_overlays(&app, 1.0).is_empty()); + assert!(app.run("view.curvatureComb", json!({"on": true})).is_err(), "it takes no parameters"); + assert!(!app.ui.view.curvature_comb, "a refused call leaves it off"); + assert!(app.run("view.curvatureComb", json!({})).unwrap() == json!(true)); + assert!(app.ui.view.curvature_comb); + assert!(app.run("view.curvatureComb", json!({})).unwrap() == json!(false)); +} + /// Draw Inside's arms: an open rectangle round the object, each arm a third of its side or 16 /// points at most, kept with the object deselected and gone with the mode. #[test] diff --git a/crates/ui-egui/src/canvas/tool_adapter.rs b/crates/ui-egui/src/canvas/tool_adapter.rs index 70f1080db5..293d2cc729 100644 --- a/crates/ui-egui/src/canvas/tool_adapter.rs +++ b/crates/ui-egui/src/canvas/tool_adapter.rs @@ -782,6 +782,9 @@ fn show_overlays(app: &mut VectorcraftApp, ui: &mut Ui, pass: &ToolPass, view_in if app.ui.view.live_paint_gaps { extra.extend(super::marks::live_paint_gap_overlays(app)); } + if app.ui.view.curvature_comb { + extra.extend(super::marks::curvature_comb_overlays(app, space.view.zoom)); + } } super::overlays::show(app, ui, &space, &list, &extra); } diff --git a/crates/ui-egui/src/i18n/tests.rs b/crates/ui-egui/src/i18n/tests.rs index e77c4bae56..cdbc54b58d 100644 --- a/crates/ui-egui/src/i18n/tests.rs +++ b/crates/ui-egui/src/i18n/tests.rs @@ -1035,6 +1035,7 @@ fn toggled_labels() -> Vec { "type.hiddenCharacters", "view.gradientAnnotator", "view.livePaintGaps", + "view.curvatureComb", "view.artboards", "view.template", "view.rulers", diff --git a/crates/ui-egui/src/menus/entries.rs b/crates/ui-egui/src/menus/entries.rs index 66353c55be..71feba3195 100644 --- a/crates/ui-egui/src/menus/entries.rs +++ b/crates/ui-egui/src/menus/entries.rs @@ -76,7 +76,7 @@ const MENU_ONLY_COMMANDS: [&str; 5] = ["imageTrace.make", "imageTrace.makeAndExp /// action ([`plays_in_app`]): Outline, Pixel Preview, Trim View, Presentation Mode and Screen Mode, /// the zoom, fit and rotate view items, and the items that show or hide or turn snapping on or off. /// The saved-view slots are not among them. -const UI_STEP_COMMANDS: [&str; 30] = [ +const UI_STEP_COMMANDS: [&str; 31] = [ "view.outline", "view.pixelPreview", "view.trimView", @@ -100,6 +100,7 @@ const UI_STEP_COMMANDS: [&str; 30] = [ "view.gradientAnnotator", "view.cornerWidget", "view.livePaintGaps", + "view.curvatureComb", "view.guides", "view.smartGuides", "view.grid", diff --git a/crates/ui-egui/src/menus/labels.rs b/crates/ui-egui/src/menus/labels.rs index 549d31b876..52ec3b33aa 100644 --- a/crates/ui-egui/src/menus/labels.rs +++ b/crates/ui-egui/src/menus/labels.rs @@ -29,6 +29,7 @@ fn view_toggle_label(app: &VectorcraftApp, id: &str) -> Option { "type.hiddenCharacters" => if v.hidden_chars { "Hide Hidden Characters" } else { "Show Hidden Characters" }.into(), "view.gradientAnnotator" => if v.gradient_annotator { "Hide Gradient Annotator" } else { "Show Gradient Annotator" }.into(), "view.livePaintGaps" => if v.live_paint_gaps { "Hide Live Paint Gaps" } else { "Show Live Paint Gaps" }.into(), + "view.curvatureComb" => if v.curvature_comb { "Hide Curvature Comb" } else { "Show Curvature Comb" }.into(), _ => return None, }; Some(text) @@ -356,6 +357,8 @@ pub const DYNAMIC_LABELS: &[&str] = &[ "Show Gradient Annotator", "Hide Live Paint Gaps", "Show Live Paint Gaps", + "Hide Curvature Comb", + "Show Curvature Comb", "Hide Artboards", "Show Artboards", "Hide Rulers", diff --git a/crates/ui-egui/src/menus/model.rs b/crates/ui-egui/src/menus/model.rs index cd4d481cc5..3e4cecdc1d 100644 --- a/crates/ui-egui/src/menus/model.rs +++ b/crates/ui-egui/src/menus/model.rs @@ -144,7 +144,7 @@ fn rows_key(app: &VectorcraftApp, input: Option) -> u64 { let (prefs, v) = (&s.prefs, &app.ui.view); (prefs.font_preview, prefs.font_samples_on_white, prefs.font_names_in_english, crate::workspaces::current_name(&app.ui)).hash(&mut h); (v.edges, v.templates, v.corner_widgets, v.shape_widgets, v.text_threads, v.hidden_chars, v.gradient_annotator).hash(&mut h); - (v.live_paint_gaps, v.artboards, v.rulers, v.video_rulers, v.bounding_box, v.guides, v.grid).hash(&mut h); + (v.live_paint_gaps, v.curvature_comb, v.artboards, v.rulers, v.video_rulers, v.bounding_box, v.guides, v.grid).hash(&mut h); (app.outline_view(), s.guides_locked(), s.slices_hidden()).hash(&mut h); // The Align To choice: Object › Align's checks and the Distribute buttons with the Artboard tool. app.ui.align_to.hash(&mut h); diff --git a/crates/ui-egui/src/menus/run.rs b/crates/ui-egui/src/menus/run.rs index 50e2a7606e..35b09d45b6 100644 --- a/crates/ui-egui/src/menus/run.rs +++ b/crates/ui-egui/src/menus/run.rs @@ -408,7 +408,7 @@ fn run_ui_part3(app: &mut VectorcraftApp, id: &str, p: &Value) -> Option Option> { +fn run_ui_part4(app: &mut VectorcraftApp, id: &str, p: &Value) -> Option> { let flag = |b: &mut bool| { *b = !*b; Ok(json!(*b)) @@ -419,6 +419,8 @@ fn run_ui_part4(app: &mut VectorcraftApp, id: &str, _p: &Value) -> Option crate::panels::align::run_align_to(app, v), "view.gradientAnnotator" => flag(&mut app.ui.view.gradient_annotator), "view.livePaintGaps" => flag(&mut app.ui.view.live_paint_gaps), + "view.curvatureComb" if !(p.is_null() || p.as_object().is_some_and(|m| m.is_empty())) => Err("view.curvatureComb takes no parameters".into()), + "view.curvatureComb" => flag(&mut app.ui.view.curvature_comb), "effect.last" => match app.last_effect.clone() { Some((e, params)) => crate::dialogs::effect::open_last(app, &e, params), None => Err("no effect applied yet".into()), diff --git a/crates/ui-egui/src/menus/tests.rs b/crates/ui-egui/src/menus/tests.rs index a59fe7f769..39909b57eb 100644 --- a/crates/ui-egui/src/menus/tests.rs +++ b/crates/ui-egui/src/menus/tests.rs @@ -773,6 +773,7 @@ fn last_effect_dialog_and_view_toggles() { ("view.textThreads", "Hide Text Threads", "Show Text Threads"), ("view.gradientAnnotator", "Hide Gradient Annotator", "Show Gradient Annotator"), ("view.livePaintGaps", "Show Live Paint Gaps", "Hide Live Paint Gaps"), + ("view.curvatureComb", "Show Curvature Comb", "Hide Curvature Comb"), ("type.hiddenCharacters", "Show Hidden Characters", "Hide Hidden Characters"), ("view.template", "Hide Template", "Show Template"), ] { diff --git a/crates/ui-egui/src/menus/tree.rs b/crates/ui-egui/src/menus/tree.rs index 7cd830ef05..e044ac2580 100644 --- a/crates/ui-egui/src/menus/tree.rs +++ b/crates/ui-egui/src/menus/tree.rs @@ -678,6 +678,7 @@ fn built_in_tree(english_names: bool, listed: &Listed) -> Vec<(&'static str, Vec c("Hide Gradient Annotator", "view.gradientAnnotator"), c("Hide Corner Widget", "view.cornerWidget"), c("Show Live Paint Gaps", "view.livePaintGaps"), + c("Show Curvature Comb", "view.curvatureComb"), Sep, sub( "Guides", diff --git a/crates/ui-egui/src/menus/ui_commands.rs b/crates/ui-egui/src/menus/ui_commands.rs index f783fd4464..6f955c6dba 100644 --- a/crates/ui-egui/src/menus/ui_commands.rs +++ b/crates/ui-egui/src/menus/ui_commands.rs @@ -262,6 +262,12 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "", "{} toggle drawing the gaps of the selected Live Paint groups (those livePaint.info lists) in each group's Gap Preview Color while edges show → true when on", ), + ( + "view.curvatureComb", + "Show Curvature Comb", + "", + "{} toggle drawing the curvature comb of the selected paths and compound paths while edges show: 16 teeth per segment along the normal, on the outer side of the bend, each as long as the curvature there (a radius of 100 screen points gives a 20-point tooth at any zoom), with a line through their tips in the layer color; about 20,000 teeth at most, fewer per segment on a large selection; a view setting, not saved in the document and not an undo step → true when on", + ), ( "type.hiddenCharacters", "Show Hidden Characters", diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 19ef45dbbc..8ff9aaf3ad 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -410,6 +410,9 @@ pub struct ViewFlags { /// Preview Color. #[serde(default)] pub live_paint_gaps: bool, + /// View → Show/Hide Curvature Comb: draw the curvature comb of the selected paths. + #[serde(default)] + pub curvature_comb: bool, /// Type → Show Hidden Characters. pub hidden_chars: bool, /// Edit › Spelling › Auto Spell Check: a red underline under the words of the type its @@ -443,6 +446,7 @@ impl Default for ViewFlags { text_threads: true, gradient_annotator: true, live_paint_gaps: false, + curvature_comb: false, hidden_chars: false, auto_spell_check: false, templates: true, diff --git a/docs/mcp.md b/docs/mcp.md index 9a29a875e1..f7bbd9830a 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -9497,6 +9497,15 @@ of more than half a degree and the anchor is not marked smooth with both handles marked smooth), when the circle would reach past the canvas (4,000,000 pt from the origin), when `t` is not from 0 to 1, when `id` is a guide, and when the path, subpath or segment does not exist. +`view.curvatureComb` (View › Show Curvature Comb, or Hide Curvature Comb while it is on) turns on the curvature comb +of the selected paths, and of each path in a selected compound path, while edges show. Each segment gets 16 evenly +spaced teeth along the normal, on the outer side of the bend, each as long as the curvature at that point, and a line +joins the teeth's tips, in the path's layer color. A straight segment has teeth of no length, and a point of a curve +where it has no direction (a handle on its anchor) has no tooth. The length is VectorCraft's own value: a bend with a +radius of 100 screen points draws 20-point teeth at any zoom. The comb draws about 20,000 teeth at most, shared among +the selected paths by their number of segments: a larger selection gets 2 teeth per segment, then teeth on every k-th +segment. The setting belongs to the view: it is not saved in the document and makes no undo step. + The Curvature Circle tool, after the Circle tool in the Ellipse tool's toolbar group, draws with this command. The pointer snaps to the nearest path that is not a guide within the tool's Snap Tolerance (`snapTolerance` below, 5 screen points by default), or within Smart Guides › Snapping Tolerance when that is larger; with Snap with Smart