From 563be77452e5ce53749eec7cbf3dae0fa9a7a649 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sun, 15 Nov 2026 13:52:13 +0000 Subject: [PATCH] ui-canvas: path_runs flattens per segment with a bounded point count and breaks runs at non-finite segments --- .../src/canvas_controls/outline/mod.rs | 62 ++++++++++++++++--- .../ui-canvas/tests/it/canvas_controls/mod.rs | 2 + .../tests/it/canvas_controls/outline.rs | 36 +++++++++++ 3 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 crates/ui-canvas/tests/it/canvas_controls/outline.rs diff --git a/crates/ui-canvas/src/canvas_controls/outline/mod.rs b/crates/ui-canvas/src/canvas_controls/outline/mod.rs index 70765a9aa..150ffeb5b 100644 --- a/crates/ui-canvas/src/canvas_controls/outline/mod.rs +++ b/crates/ui-canvas/src/canvas_controls/outline/mod.rs @@ -28,6 +28,10 @@ const EDGE_ALPHA: f32 = 0.95; /// How far, in screen points, a flattened curve may leave the path. const FLATTEN_TOLERANCE: f64 = 0.25; +/// About how many points one flattened segment may have (the tolerance stays at `FLATTEN_TOLERANCE` for segments +/// up to about 250k screen points across). +const MAX_SEGMENT_POINTS: f64 = 1000.0; + /// What a click under the pointer would pick, in document units. #[derive(Clone, Debug, PartialEq)] #[non_exhaustive] @@ -88,25 +92,63 @@ pub fn path_runs(view: &CanvasView, path: &BezPath) -> Vec<(Vec, bool)> { let screen = view.affine() * path.clone(); let mut out = Vec::new(); let mut points = Vec::new(); - let mut end = |points: &mut Vec, closed: bool| { + let end = |out: &mut Vec<_>, points: &mut Vec, closed: bool| { let run = std::mem::take(points); if run.len() > 1 { out.push((run, closed)); } }; - kurbo::flatten(screen.elements().iter().copied(), FLATTEN_TOLERANCE, |element| match element { - PathEl::MoveTo(p) => { - end(&mut points, false); - points.push(pos2(p.x as f32, p.y as f32)); + let (mut start, mut current) = (kurbo::Point::ZERO, kurbo::Point::ZERO); + for element in screen.elements().iter().copied() { + match element { + PathEl::MoveTo(p) => { + end(&mut out, &mut points, false); + points.push(pos2(p.x as f32, p.y as f32)); + (start, current) = (p, p); + } + PathEl::ClosePath => { + end(&mut out, &mut points, true); + current = start; + } + segment => { + let Some(to) = segment.end_point() else { continue }; + let hull = segment_hull(current, segment); + current = to; + if !hull.iter().all(|p| p.is_finite()) { + // A segment with a non-finite point has no drawable shape; the run breaks there and the next + // one starts at its end. + end(&mut out, &mut points, false); + if to.is_finite() { + points.push(pos2(to.x as f32, to.y as f32)); + } + continue; + } + let extent = hull.iter().fold(Rect::from_points(hull[0], hull[0]), |r, p| r.union_pt(*p)); + // The point count of a flattened curve grows with the square root of its size over the tolerance; + // a segment far larger than any screen keeps a bounded count at a coarser tolerance. + let tolerance = FLATTEN_TOLERANCE.max(extent.width().max(extent.height()) / MAX_SEGMENT_POINTS.powi(2)); + kurbo::flatten([PathEl::MoveTo(hull[0]), segment], tolerance, |flat| { + if let PathEl::LineTo(p) = flat { + points.push(pos2(p.x as f32, p.y as f32)); + } + }); + } } - PathEl::LineTo(p) => points.push(pos2(p.x as f32, p.y as f32)), - PathEl::ClosePath => end(&mut points, true), - _ => {} - }); - end(&mut points, false); + } + end(&mut out, &mut points, false); out } +/// The start point and every point of one drawing segment. +fn segment_hull(from: kurbo::Point, segment: PathEl) -> Vec { + match segment { + PathEl::LineTo(p) => vec![from, p], + PathEl::QuadTo(a, p) => vec![from, a, p], + PathEl::CurveTo(a, b, p) => vec![from, a, b, p], + PathEl::MoveTo(_) | PathEl::ClosePath => vec![from], + } +} + /// Strokes one run: the kit's halo-and-line outline in the overlay colours, or a line of the host's `color` alone. fn stroke_run(painter: &ContentPainterCx<'_>, run: Vec, closed: bool, color: Option, width: f32) { match color { diff --git a/crates/ui-canvas/tests/it/canvas_controls/mod.rs b/crates/ui-canvas/tests/it/canvas_controls/mod.rs index 489a64a7f..37938ec64 100644 --- a/crates/ui-canvas/tests/it/canvas_controls/mod.rs +++ b/crates/ui-canvas/tests/it/canvas_controls/mod.rs @@ -8,4 +8,6 @@ mod guides; mod marquee; +mod outline; + mod task_bar; diff --git a/crates/ui-canvas/tests/it/canvas_controls/outline.rs b/crates/ui-canvas/tests/it/canvas_controls/outline.rs new file mode 100644 index 000000000..caec8d01c --- /dev/null +++ b/crates/ui-canvas/tests/it/canvas_controls/outline.rs @@ -0,0 +1,36 @@ +//! `path_runs`: the screen polylines a path outline is stroked from. + +use craft_behavior::CanvasView; +use craft_behavior::kurbo::{BezPath, Point}; +use craft_ui::canvas_controls::path_runs; +use craft_ui::testing::trial; +use egui::{Pos2, Rect, vec2}; + +fn view() -> CanvasView { + CanvasView::new(Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)), Point::new(200.0, 150.0), 1.0) +} + +#[trial] +fn a_curve_far_larger_than_the_screen_flattens_to_a_bounded_run() { + let far = Point::new(1e40, 0.0); + let mut path = BezPath::new(); + path.move_to(far); + path.curve_to(far, Point::new(20.0, 20.0), Point::new(20.0, 20.0)); + path.curve_to((60.0, 20.0), (60.0, 120.0), (40.0, 120.0)); + let runs = path_runs(&view(), &path); + assert_eq!(runs.len(), 1); + assert!(runs[0].0.len() < 10_000); +} + +#[trial] +fn a_non_finite_segment_breaks_the_run_and_the_rest_still_draws() { + let mut path = BezPath::new(); + path.move_to((10.0, 10.0)); + path.line_to((50.0, 10.0)); + path.line_to((f64::NAN, 10.0)); + path.line_to((50.0, 80.0)); + path.line_to((90.0, 80.0)); + let runs = path_runs(&view(), &path); + assert_eq!(runs.len(), 2); + assert!(runs.iter().flat_map(|(points, _)| points).all(|p| p.x.is_finite() && p.y.is_finite())); +}