From 28de8119c6f88b4ea4876fedf36f4f57d16bfde1 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 21 Nov 2026 02:04:43 -0800 Subject: [PATCH 1/2] Canvas: a long dashed overlay line draws only the dashes on screen A dashed tool overlay line was dashed over its whole screen length, so a line that runs far past the canvas at a high zoom built a dash shape for every 7 px of it, nearly all of them off screen. A line millions of pixels long built hundreds of thousands of shapes every frame. The line is now clipped to the canvas in f64 before it is dashed, with the pattern started where the whole line would have it, so the visible dashes stay where they were. A line off the canvas, or with an end at infinity, draws nothing. --- crates/ui-egui/src/canvas/overlays.rs | 45 +++++++++++++++++-- crates/ui-egui/src/canvas/tests.rs | 64 +++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/canvas/overlays.rs b/crates/ui-egui/src/canvas/overlays.rs index b4d2d44aac..b94a739997 100644 --- a/crates/ui-egui/src/canvas/overlays.rs +++ b/crates/ui-egui/src/canvas/overlays.rs @@ -8,7 +8,7 @@ use super::artboards::names_an_artboard; use super::input::marquee_through; use super::outline::{KEY_OUTLINE, c32, node_outline, selection_hit_at, stroke_path, to_screen_path, too_many_anchors}; use super::selection_marks::{MarkOptions, SelectionMarks}; -use super::{BezPath, Color32, CornerRadius, NodeKind, Overlay, Pos2, Shape, Stroke, StrokeKind, Tokens, VectorcraftApp, Xf, vec2}; +use super::{BezPath, Color32, CornerRadius, NodeKind, Overlay, Point, Pos2, Shape, Stroke, StrokeKind, Tokens, VectorcraftApp, Xf, vec2}; pub(super) fn hover_highlight(app: &VectorcraftApp, p: &egui::Painter, xf: &Xf) { let Some(h) = app.hover_doc else { return }; @@ -452,6 +452,42 @@ pub(super) fn dashes_from(line: &[Pos2], s: Stroke, from: f64, out: &mut Vec) { + if !(a.is_finite() && b.is_finite()) { + return; + } + let r = clip.expand(s.width.max(1.0)); + let (left, right, top, bottom) = (f64::from(r.left()), f64::from(r.right()), f64::from(r.top()), f64::from(r.bottom())); + let d = b - a; + let (mut t0, mut t1) = (0.0_f64, 1.0_f64); + // Each edge as (the run toward it, the room before it), as `grid::clip_segment` does in `f32`. + for (run, room) in [(-d.x, a.x - left), (d.x, right - a.x), (-d.y, a.y - top), (d.y, bottom - a.y)] { + if run == 0.0 { + if room < 0.0 { + return; + } + } else if run < 0.0 { + t0 = t0.max(room / run); + } else { + t1 = t1.min(room / run); + } + } + if t0 > t1 { + return; + } + let at = |t: f64| { + let p = a + d * t; + egui::pos2(p.x as f32, p.y as f32) + }; + // How far along the whole line the part on the canvas starts. + dashes_from(&[at(t0), at(t1)], s, d.hypot() * t0, out); +} + /// Add to `out` each line of `line` that has a length as its own [`Shape::LineSegment`], as /// [`Shape::dashed_line`] draws a dash. fn segments(line: &[Pos2], s: Stroke, out: &mut Vec) { @@ -562,11 +598,12 @@ pub(super) fn draw_overlays(p: &egui::Painter, xf: &Xf, overlays: &[Overlay], t: } Overlay::Line { a, b, color, dashed } => { let s = Stroke::new(1.0, c32(*color)); - let (a, b) = (xf.to_screen(*a), xf.to_screen(*b)); if *dashed { - p.extend(Shape::dashed_line(&[a, b], s, 4.0, 3.0)); + let mut dashes = vec![]; + dashed_line_in(p.clip_rect(), xf.affine() * *a, xf.affine() * *b, s, &mut dashes); + p.extend(dashes); } else { - p.line_segment([a, b], s); + p.line_segment([xf.to_screen(*a), xf.to_screen(*b)], s); } } Overlay::Anchor { p: pt, color, filled, size } => anchor_square(p, xf.to_screen(*pt), c32(*color), *filled, *size), diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index d5244aa234..c040e31d35 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -211,6 +211,70 @@ fn a_line_dashed_in_pieces_dashes_as_the_whole_line_does() { } } +/// A dashed [`Overlay::Line`] much longer than the canvas draws only the dashes on the canvas, +/// where the whole line dashed in one go has them, and a line off the canvas or with an end at +/// infinity draws none. +#[test] +fn a_long_dashed_overlay_line_draws_only_the_dashes_on_the_canvas() { + let s = Stroke::new(1.0, Color32::WHITE); + let clip = egui::Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)); + // The stretches of x the dashes cover inside `lo..hi`, each dash as one stretch. + let spans = |shapes: &[Shape], lo: f32, hi: f32| -> Vec<(f32, f32)> { + let mut out: Vec<(f32, f32)> = shapes + .iter() + .map(|s| match s { + Shape::LineSegment { points: [a, b], .. } => (a.x.min(b.x).max(lo), a.x.max(b.x).min(hi)), + other => panic!("{other:?}"), + }) + .filter(|(a, b)| b > a) + .collect(); + out.sort_by(|a, b| a.0.total_cmp(&b.0)); + out + }; + // A line from 1,000 px left of the canvas to 1,500 px right of it, along y = 150. + let (a, b) = (pos2(-1000.0, 150.0), pos2(1900.0, 150.0)); + let mut drawn = vec![]; + overlays::dashed_line_in(clip, Point::new(-1000.0, 150.0), Point::new(1900.0, 150.0), s, &mut drawn); + let (lo, hi) = (clip.left() - 1.0, clip.right() + 1.0); + let (want, got) = (spans(&Shape::dashed_line(&[a, b], s, 4.0, 3.0), lo, hi), spans(&drawn, lo, hi)); + assert_eq!(want.len(), got.len(), "{want:?} {got:?}"); + assert!(want.iter().zip(&got).all(|(w, g)| (w.0 - g.0).abs() < 1e-2 && (w.1 - g.1).abs() < 1e-2), "{want:?} {got:?}"); + assert!(drawn.len() <= 60, "only the dashes on the canvas: {}", drawn.len()); + // Ends 10,000,000,000 px away on both sides still keep the part on the canvas. + let mut far = vec![]; + overlays::dashed_line_in(clip, Point::new(-1e10, 150.0), Point::new(1e10, 150.0), s, &mut far); + let far = spans(&far, lo, hi); + assert!(far.len() >= 50 && far.len() <= 60, "{far:?}"); + assert!(far.iter().all(|(a, b)| b - a <= 4.0 + 1e-3), "{far:?}"); + // Drawn through the overlays at a view where the line is 2,000,000 px long. + let ctx = egui::Context::default(); + crate::theme::install_fonts(&ctx); + crate::theme::apply(&ctx, crate::theme::Brightness::MediumDark); + let prefs = vectorcraft_engine::Prefs::default(); + let segments = |line: Overlay| { + let raw = egui::RawInput { screen_rect: Some(clip), ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| { + let t = Tokens::get(ui.ctx()); + let xf = Xf { rect: ui.max_rect(), zoom: 1.0, center: Point::new(200.0, 150.0), rot: 0.3 }; + overlays::draw_overlays( + ui.painter(), + &xf, + std::slice::from_ref(&line), + &t, + overlays::HandleLook::of(&prefs), + &mut overlays::SharedPaths::default(), + ); + }); + out.textures_delta.clear(); + out.shapes.iter().filter(|c| matches!(c.shape, Shape::LineSegment { .. })).count() + }; + let line = |a: Point, b: Point| Overlay::Line { a, b, color: [0xff, 0, 0], dashed: true }; + let n = segments(line(Point::new(-1e6, 150.0), Point::new(1e6, 150.0))); + assert!(n > 0 && n <= 80, "{n} dashes"); + assert_eq!(segments(line(Point::new(-1e6, 5000.0), Point::new(1e6, 5000.0))), 0, "a line off the canvas"); + assert_eq!(segments(line(Point::new(0.0, 150.0), Point::new(f64::INFINITY, 150.0))), 0, "an end at infinity"); +} + #[test] fn subpixel_dock_layout_does_not_change_pointer_document_coordinates() { let mut observed = Vec::new(); From 4ec3a4cba243a0d4f92f1ca38a0ae32e34e1a796 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 15:42:39 -0800 Subject: [PATCH 2/2] Canvas: test a long dashed overlay line through paint The test paints a dashed Overlay::Line that runs far past both sides of the canvas through paint and checks that only the dashes near the canvas are drawn, all on the line. --- crates/ui-egui/src/canvas/overlays/tests.rs | 45 +++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/crates/ui-egui/src/canvas/overlays/tests.rs b/crates/ui-egui/src/canvas/overlays/tests.rs index c70e573139..6c7d8e21d1 100644 --- a/crates/ui-egui/src/canvas/overlays/tests.rs +++ b/crates/ui-egui/src/canvas/overlays/tests.rs @@ -127,6 +127,51 @@ fn a_dashed_line_with_far_ends_keeps_its_part_on_the_canvas() { assert!(run.iter().all(|p| (p.y - p.x - 72.35).abs() < 1e-2 && clip.expand(1e-2).contains(*p)), "{run:?}"); } +/// The line segments in `shapes`, with nested shape lists opened. +fn segments(shapes: &[egui::Shape]) -> Vec<[Pos2; 2]> { + let mut out = Vec::new(); + for s in shapes { + match s { + egui::Shape::LineSegment { points, .. } => out.push(*points), + egui::Shape::Vec(inner) => out.extend(segments(inner)), + other => panic!("{other:?}"), + } + } + out +} + +/// A long dashed line overlay painted through `paint` gives only the dashes near the canvas, each on the line. +#[test] +fn a_long_dashed_line_overlay_paints_only_its_dashes_on_the_canvas() { + use craft::i18n::Direction; + use craft::theme::{Contrast, Mode}; + use craft::ui::style::Ink; + use craft::ui::testing::Frames; + use craft::ui::testing::style::{paint_frame, tokens}; + + let view = view(); + let tokens = tokens(Mode::Light, Contrast::Standard); + let style = ToolMarksStyle { + metrics: Metrics::new(), + dark: StyleColor(Color32::BLACK), + ink: StyleColor(Color32::WHITE), + accent: StyleColor(Color32::BLUE), + pad: Vec2::ZERO, + radius: CornerRadius::ZERO, + handle: 3.0, + }; + let line = Overlay::Line { a: Point::new(-1000.0, 100.0), b: Point::new(1900.0, 100.0), color: [255, 0, 255], dashed: true }; + let mut frames = Frames::new(); + let (shapes, _) = paint_frame(&mut frames, &tokens, Direction::Ltr, Ink::OnPanel, |draw| { + paint(draw, &view, std::slice::from_ref(&line), &PathOptionsSettings::default(), &style); + }); + let drawn = segments(&shapes); + assert!(!drawn.is_empty(), "the part on the canvas is drawn"); + assert!(drawn.len() <= 40, "only the dashes near the canvas: {}", drawn.len()); + let near = view.rect.expand(32.0); + assert!(drawn.iter().flatten().all(|p| near.contains(*p) && (p.y - 100.0).abs() < 1e-3), "{drawn:?}"); +} + /// A turned view clips the line to the canvas too, and a line off the canvas or with an end that is not finite draws /// nothing. #[test]