From 8af6814eb1eb200329295a1a606c365f292b40b4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 08:08:16 -0800 Subject: [PATCH 1/2] Canvas: art lands on whole device pixels, so strokes stay sharp The canvas drew its rendered frame from wherever its corner fell, often between device pixels on a HiDPI or scaled display. Each frame pixel was then spread over two device pixels, so lines, arcs and shape edges looked soft and jagged with a doubled gray edge. The main canvas and the split view panes now place the frame on whole device pixels at its own pixel size. --- crates/ui-egui/src/canvas/panes.rs | 10 ++++++-- crates/ui-egui/src/canvas/raster.rs | 15 ++++++++++- crates/ui-egui/src/canvas/tests.rs | 39 +++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/canvas/panes.rs b/crates/ui-egui/src/canvas/panes.rs index 9e25bcbf93..2fff6194da 100644 --- a/crates/ui-egui/src/canvas/panes.rs +++ b/crates/ui-egui/src/canvas/panes.rs @@ -19,7 +19,7 @@ use serde_json::json; use vectorcraft_geom::{Affine, Point}; use super::BodyKind; -use super::raster::{backdrop, cache_key, frame_options, frame_ppp, frame_view, pixel_region, placed_key, plugin_key}; +use super::raster::{backdrop, cache_key, frame_options, frame_ppp, frame_view, pixel_region, placed_key, plugin_key, snapped_rect}; use crate::doc_windows::Win; use crate::state::View; use crate::{CacheKey, VectorcraftApp}; @@ -141,6 +141,7 @@ impl PaneSource { pub(super) fn new(app: &mut VectorcraftApp, ctx: &egui::Context, w: Win, cv: &CanvasView) -> Option { let env = Env::of_root(ctx); let ppp = ctx.pixels_per_point(); + let device_ppp = ppp; let space = cv.rect; let v = crate::doc_windows::view(app, w).copied()?; let st = app.session.documents().get(w.0)?; @@ -165,7 +166,12 @@ impl PaneSource { Some([x0, y0, x1, y1]) => { egui::Rect::from_two_pos(cv.to_screen(Point::new(x0 as f64, y0 as f64)), cv.to_screen(Point::new(x1 as f64, y1 as f64))) } - None => space, + // The frame's own pixel size, centered on the pane and on whole device pixels, so each + // of its pixels covers one device pixel. + None => { + let size = egui::vec2(pw as f32 / ppp, ph as f32 / ppp); + snapped_rect(space.center() - size / 2.0, size, device_ppp) + } }; let frame = PaneFrame { key: PaneKey { slot: (st.uid, w.1) }, diff --git a/crates/ui-egui/src/canvas/raster.rs b/crates/ui-egui/src/canvas/raster.rs index 9d1cdcb417..48f036012e 100644 --- a/crates/ui-egui/src/canvas/raster.rs +++ b/crates/ui-egui/src/canvas/raster.rs @@ -313,6 +313,18 @@ pub(super) fn frame_ppp(size: egui::Vec2, ppp: f32, max_side: usize) -> f32 { if longest.round() <= max { ppp } else { ppp * (max / longest) } } +/// Where a frame drawn `size` points across from `min` goes on a display of `device_ppp` pixels a +/// point: `min` moved to the nearest device pixel, the size unchanged. A canvas whose corner falls +/// between device pixels would otherwise draw every pixel of the frame across two device pixels, +/// and each edge in the art would show as a soft double line. +pub(super) fn snapped_rect(min: egui::Pos2, size: egui::Vec2, device_ppp: f32) -> egui::Rect { + if !(device_ppp.is_finite() && device_ppp > 0.0) { + return egui::Rect::from_min_size(min, size); + } + let snap = |c: f32| (c * device_ppp).round() / device_ppp; + egui::Rect::from_min_size(pos2(snap(min.x), snap(min.y)), size) +} + /// Pixel Preview: the whole document pixels (x0, y0, x1, y1) under the canvas, rotated view /// included. None when the view is degenerate, or when a side of the region is more than /// `max_side` pixels, the largest texture side the display supports. @@ -975,7 +987,8 @@ fn reproject_frame(app: &VectorcraftApp, painter: &egui::Painter, view: &CanvasV let old = CanvasView::new(old_rect, Point::new(k.cx, k.cy), k.zoom).rotation(rotation); let a = view.to_screen(old.to_doc(old_rect.min)); let b = view.to_screen(old.to_doc(old_rect.max)); - painter.image(tex.id(), egui::Rect::from_min_max(a, b), egui::Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE); + let placed = snapped_rect(a, b - a, painter.pixels_per_point()); + painter.image(tex.id(), placed, egui::Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE); } // The view turned after the last frame was rendered, and the worker renders the frame at // the new angle. Until it arrives, the last frame is drawn turned into this view: each diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index 4857c67064..3f1eddb392 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -1191,6 +1191,45 @@ fn the_art_renders_within_the_largest_texture_side() { assert_eq!(pixel_region(&view, 1024), None, "more than the display's largest texture"); } +/// On a display of 2 pixels a point, a canvas whose corner falls between device pixels still draws +/// its art's frame on whole device pixels at its own size, so each frame pixel covers one device +/// pixel and a stroke's edges stay sharp instead of spreading over two device pixels. +#[test] +fn the_art_lands_on_whole_device_pixels() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width":400,"height":300})).unwrap(); + app.canvas.worker_started = true; // every frame on this thread + let ctx = crate::testing::ui_context(); + ctx.set_pixels_per_point(2.0); + let mut shapes = vec![]; + // The first frame sets the pixels a point, the second draws at them. + for _ in 0..2 { + let raw = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(pos2(31.3, 19.7), vec2(800.3, 600.35))), ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| show(&mut app, ui)); + out.textures_delta.clear(); + shapes = flat_shapes(out.shapes); + } + let ppp = ctx.pixels_per_point(); + assert_eq!(ppp, 2.0); + let rect = app.canvas_view().unwrap().rect; + let off_grid = |c: f32| ((c * ppp) - (c * ppp).round()).abs() > 0.01; + assert!(off_grid(rect.min.x) || off_grid(rect.min.y), "the canvas {rect:?} starts between device pixels"); + let tex = app.canvas.texture.as_ref().expect("a rendered frame"); + let (tid, size) = (tex.id(), tex.size()); + let drawn = shapes + .iter() + .find_map(|s| match s { + Shape::Mesh(m) if m.texture_id == tid => Some(m.calc_bounds()), + _ => None, + }) + .expect("the frame is drawn"); + for (c, what) in [(drawn.min.x, "left"), (drawn.min.y, "top")] { + assert!(!off_grid(c), "the frame's {what} edge {c} is not on a device pixel"); + } + assert!((drawn.width() * ppp - size[0] as f32).abs() < 0.01, "{} device pixels across for {} frame pixels", drawn.width() * ppp, size[0]); + assert!((drawn.height() * ppp - size[1] as f32).abs() < 0.01, "{} device pixels down for {} frame pixels", drawn.height() * ppp, size[1]); +} + /// `rgb` as the display transform `t` shows it. fn convert8(t: &craft::color_ui::display::DisplayTransform, rgb: [u8; 3]) -> [u8; 3] { let mut px = [rgb[0], rgb[1], rgb[2], 255]; From 2eb79ddfb44eda2eed9479ef8d2bc8dab2411fb6 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 09:34:39 -0800 Subject: [PATCH 2/2] Smart Guides: a move lines up geometric edges with Use Preview Bounds on With Use Preview Bounds on, a move lined up the outside of the selection's stroke with the other art's geometric bounds, which Smart Guides always use for the targets. A stroked object then stopped half a stroke short of the edge it showed a guide for, or overlapped it by that much. The moved selection's geometric bounds now line up with the targets' geometric bounds whatever Use Preview Bounds says. Snap to Grid, Snap to Pixel and the ruler guides still use the bounds the bounding box shows. Part of #6436. --- crates/tools/src/select.rs | 10 ++++++--- crates/tools/src/select/tests.rs | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/crates/tools/src/select.rs b/crates/tools/src/select.rs index f921b19fea..7510358fc4 100644 --- a/crates/tools/src/select.rs +++ b/crates/tools/src/select.rs @@ -262,6 +262,9 @@ fn distance_overlays(cx: &ToolContext, to: DistanceTo) -> Vec { /// it snaps to the original too. pub(crate) struct MoveSnap { bounds: Option, + /// The selection's geometric bounds, which Smart Guides line up with the other art's geometric + /// bounds and space evenly between them, whether Use Preview Bounds is on or off (#6436). + edges: Option, targets: Option, points: Option, /// The targets and points for a copy, gathered with the others before the previews move the @@ -300,6 +303,7 @@ impl MoveSnap { let points = |exclude: &[NodeId]| if cx.snap_to_grid { None } else { Targets::snap_to_point(cx, exclude) }; Self { bounds: selection_bounds(cx), + edges: guides.then(|| cx.doc.bounds_of(&cx.selection.objects, false)).flatten(), targets: guides.then(|| for_move(cx, &cx.selection.objects)), points: points(&cx.selection.objects), copy_targets: guides.then(|| for_move(cx, &[])), @@ -312,7 +316,7 @@ impl MoveSnap { /// Snap to Grid and Snap to Pixel alone, for a move whose box is `bounds`: the grid lands an /// edge of the box on a gridline, and pixels put its top-left on whole pixels. pub(crate) fn grid_only(bounds: Option) -> Self { - Self { bounds, targets: None, points: None, copy_targets: None, copy_points: None, rulers: None, by_edge: false } + Self { bounds, edges: None, targets: None, points: None, copy_targets: None, copy_points: None, rulers: None, by_edge: false } } /// The ruler guides stopped showing since the move began (View › Hide Guides, or Trim View, @@ -353,7 +357,7 @@ impl MoveSnap { // The grabbed anchor landed on a path or a crossing, which a ruler guide does not pull // it off. let mut on_path = false; - if let (Some(t), Some(b)) = (targets, self.bounds) { + if let (Some(t), Some(b)) = (targets, self.edges) { // The point the selection was grabbed by, near another object's anchor or centre, // lands on it, rather than the bounds lining up a little beside it (#955). if t.has_point_near(start + d, cx.snap_tol()) { @@ -434,7 +438,7 @@ impl MoveSnap { d = q - start; guides = ov; } - if let (Some(t), Some(b)) = (targets, self.bounds) { + if let (Some(t), Some(b)) = (targets, self.edges) { if t.has_point_near(start + d, cx.snap_tol()) { let (q, ov) = t.snap_point_along(start + d, u, cx.snap_tol()); d = q - start; diff --git a/crates/tools/src/select/tests.rs b/crates/tools/src/select/tests.rs index d575fda573..e74e3d9163 100644 --- a/crates/tools/src/select/tests.rs +++ b/crates/tools/src/select/tests.rs @@ -1255,6 +1255,42 @@ fn an_alt_copy_snaps_to_the_original_left_behind() { assert_eq!(dx(&t.pointer(&c, &ev(PointerKind::Drag, 253.0, 150.0))), 103.0); } +/// With Use Preview Bounds on, Smart Guides line up the moved selection's geometric bounds with +/// the other art's, as they do with it off: an edge lands exactly on the other path's edge, and the +/// edge of a stroke does not pull it there (#6436). +#[test] +fn a_move_lines_up_geometric_edges_with_use_preview_bounds_on() { + use vectorcraft_color::{Color, Paint}; + use vectorcraft_doc::{Appearance, Node}; + // An artboard whose edges and center are out of reach, and two rectangles stroked 20 pt. + let mut d = Document::new(1000.0, 1000.0); + let l = d.layers[0].id; + let mut add = |r: Rect| { + let id = d.alloc_id(); + let wide = Appearance::basic(Paint::solid(Color::WHITE), Paint::solid(Color::BLACK), 20.0); + d.insert(Some(l), 0, Node::path(id, vectorcraft_geom::shapes::rectangle(r), wide)).unwrap(); + id + }; + let a = add(Rect::new(100.0, 100.0, 200.0, 200.0)); + add(Rect::new(300.0, 100.0, 400.0, 200.0)); + let s = Selection { objects: vec![a], ..Default::default() }; + let p = paint(); + for preview in [false, true] { + let c = ToolContext { preview_bounds: preview, ..cx(&d, &s, &p) }; + let dragged = |to: f64| { + let mut t = SelectionTool::default(); + t.pointer(&c, &ev(PointerKind::Down, 150.0, 150.0)); + let a = t.pointer(&c, &ev(PointerKind::Drag, to, 150.0)); + let Some(Action::Preview(_, v)) = a.last() else { panic!("{a:?}") }; + (v["matrix"][4].as_f64().unwrap(), v["matrix"][5].as_f64().unwrap()) + }; + // The right edge, 1 pt short of the other rectangle's left edge (x = 300), lands on it. + assert_eq!(dragged(249.0), (100.0, 0.0), "preview {preview}"); + // 9 pt short, it stays where it is dragged, though the edge of its stroke is 1 pt past there. + assert_eq!(dragged(241.0), (91.0, 0.0), "preview {preview}"); + } +} + /// Smart Guides › Snap to Last Location: a move that comes within Snapping Tolerance of where it /// began goes back there, labeled "last location", a Shift move too; farther out, with the option /// or Smart Guides off, it stays where it is dragged, and an Alt copy snaps as it does with the