From a3389ad06c491bee8cb3f0ffeac9b9aa86cac854 Mon Sep 17 00:00:00 2001 From: Terra854 Date: Fri, 27 Nov 2026 14:52:43 +0800 Subject: [PATCH] Match subpixel soft dabs to the brush outline --- crates/ui-egui/src/canvas/paint_tool/tests.rs | 26 +++++++++++++++++++ crates/ui-egui/src/tools/brush_cursor.rs | 18 ++++++------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/canvas/paint_tool/tests.rs b/crates/ui-egui/src/canvas/paint_tool/tests.rs index 17765fa01d7..ba18b45a29b 100644 --- a/crates/ui-egui/src/canvas/paint_tool/tests.rs +++ b/crates/ui-egui/src/canvas/paint_tool/tests.rs @@ -392,6 +392,32 @@ fn one_pixel_soft_normal_brush_marks_the_pixel_corner_inside_its_outline() { check(&app.session.active().unwrap().doc); } +#[test] +fn subpixel_soft_brush_uses_the_displayed_outline() { + // Full Size shows a 0.25 px radius for either outline. The traced Normal tip is smaller still. + for (mode, angle, inside, outside) in [("fullSizeTip", 0.0, 0.125, 0.375), ("fullSizeTip", 30.0, 0.125, 0.375), ("normalTip", 30.0, 0.125, 0.1875)] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 32, "height": 32, "background": "transparent", "depth": 32})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 0.5, "hardness": 0.0, "angle": angle}})).unwrap(); + app.run("prefs.set", json!({"values": {"cursors.painting": mode}})).unwrap(); + app.ui.tool = Tool::Brush; + assert_eq!(app.session.tools.brush.size, 0.5); + for (y, distance, painted) in [(12, outside, false), (20, inside, true)] { + let x = 16.5 - distance; + let point_y = f64::from(y) + 0.5; + let check = |doc: &Document| { + let surface = doc.layers[0].surface().unwrap(); + assert_eq!(surface.rgba(16, y)[3] > 0.0, painted, "{mode}, angle {angle}, distance {distance}"); + assert_eq!(surface.rgba(15, y)[3], 0.0); + }; + tool_event(&mut app, ToolEvent::Down { x, y: point_y, pressure: 1.0 }, M); + check(&display_doc(&mut app, 0).0); + tool_event(&mut app, ToolEvent::Up { x, y: point_y }, M); + check(&app.session.active().unwrap().doc); + } + } +} + #[test] fn brush_drag_shows_the_real_stroke_and_commits_it() { // The drag shows the real soft stroke, not a hard flat stand-in. diff --git a/crates/ui-egui/src/tools/brush_cursor.rs b/crates/ui-egui/src/tools/brush_cursor.rs index 56bf935399b..8e925302ff3 100644 --- a/crates/ui-egui/src/tools/brush_cursor.rs +++ b/crates/ui-egui/src/tools/brush_cursor.rs @@ -84,15 +84,15 @@ pub(crate) fn soft_tip_boundary(app: &PhotocraftApp, tool: Tool) -> Option if !matches!(tool, Tool::Brush | Tool::Eraser) || aliased(app, tool).is_some() || brush.hardness >= 1.0 || !matches!(*brush.tip, TipShape::Round) { return None; } - if app.session.prefs().cursors.painting != PaintingPref::NormalTip { - return Some(1.0); - } - // Traced tips use the outline's radius units. The framework's round cursor uses document pixels. - if !is_plain(brush) { - return Some(photocraft_paint::half_coverage_radius(1.0, brush.hardness)); - } - // A tiny dab still needs to reach the cursor's 1 px minimum Normal radius. - Some(normal_round_radius(brush) / (brush.size / 2.0).max(0.5)) + let radius = brush.size / 2.0; + let outline_radius = match app.session.prefs().cursors.painting { + PaintingPref::NormalTip if is_plain(brush) => normal_round_radius(brush), + PaintingPref::NormalTip => radius * photocraft_paint::half_coverage_radius(1.0, brush.hardness), + PaintingPref::FullSizeTip => radius, + PaintingPref::Standard | PaintingPref::Precise => return Some(1.0), + }; + // The outline uses the selected size, while the renderer uses at least a half-pixel dab radius. + Some(outline_radius / radius.max(0.5)) } fn normal_round_radius(brush: &BrushSettings) -> f32 {