From ef4cbb4ae4ddcf5f23d0f2b086a689ba99d941ac Mon Sep 17 00:00:00 2001 From: Terra854 Date: Fri, 27 Nov 2026 14:00:26 +0800 Subject: [PATCH] Brush: honor the minimum Normal tip outline (#17161) --- crates/engine-paint/src/cmd/brush_cmds.rs | 5 ++-- .../engine-paint/src/cmd/brush_cmds/tests.rs | 8 ++++--- .../src/cmd/table/paint/stroke.rs | 2 +- crates/paint/src/lib.rs | 5 +++- crates/paint/src/render.rs | 2 +- crates/ui-egui/src/canvas/paint_tool/tests.rs | 23 +++++++++++++++++++ crates/ui-egui/src/tools/brush_cursor.rs | 3 ++- 7 files changed, 39 insertions(+), 9 deletions(-) diff --git a/crates/engine-paint/src/cmd/brush_cmds.rs b/crates/engine-paint/src/cmd/brush_cmds.rs index 614d9111c2..977c875588 100644 --- a/crates/engine-paint/src/cmd/brush_cmds.rs +++ b/crates/engine-paint/src/cmd/brush_cmds.rs @@ -480,8 +480,9 @@ fn stroke_brush(s: &Session, p: &Value) -> Result { let cmd = "paint.stroke"; let mut brush = with_blend_mode(resolve_brush(s, p, cmd)?, p); if let Some(boundary) = p.get("tipBoundary") { - let fraction = boundary.as_f64().map(|v| v as f32).filter(|v| *v > 0.0 && *v <= 1.0); - brush.tip_boundary = Some(fraction.ok_or_else(|| bad(cmd, "tipBoundary must be greater than 0 and at most 1"))?); + let max = photocraft_paint::MAX_TIP_BOUNDARY; + let fraction = boundary.as_f64().filter(|v| *v <= f64::from(max)).map(|v| v as f32).filter(|v| *v > 0.0); + brush.tip_boundary = Some(fraction.ok_or_else(|| bad(cmd, format!("tipBoundary must be greater than 0 and at most {max}")))?); } Ok(brush) } diff --git a/crates/engine-paint/src/cmd/brush_cmds/tests.rs b/crates/engine-paint/src/cmd/brush_cmds/tests.rs index cbf4080da5..51fa94f601 100644 --- a/crates/engine-paint/src/cmd/brush_cmds/tests.rs +++ b/crates/engine-paint/src/cmd/brush_cmds/tests.rs @@ -56,9 +56,11 @@ fn soft_round_stroke_fades_to_zero_at_its_tip_boundary() { } } let mut s = session(96, 96); - let p = json!({"points": [[48.5, 48.5]], "size": 60, "tipBoundary": "invalid"}); - assert!(LiveStroke::begin(&s, &p).is_err()); - assert!(s.execute("paint.stroke", p).is_err()); + for invalid in [json!("invalid"), json!(2.000_000_01)] { + let p = json!({"points": [[48.5, 48.5]], "size": 60, "tipBoundary": invalid}); + assert!(LiveStroke::begin(&s, &p).is_err()); + assert!(s.execute("paint.stroke", p).is_err()); + } } #[test] diff --git a/crates/engine-paint/src/cmd/table/paint/stroke.rs b/crates/engine-paint/src/cmd/table/paint/stroke.rs index 5bc1352b90..d279c3e8f3 100644 --- a/crates/engine-paint/src/cmd/table/paint/stroke.rs +++ b/crates/engine-paint/src/cmd/table/paint/stroke.rs @@ -8,7 +8,7 @@ pub fn specs() -> Vec { "Brush Stroke", [], None, - r##"{"points":[[x,y,pressure?,tiltX?,tiltY?,rotation?,timeMs?,wheel?],…],"brush":{…BrushSettings}?,"preset":name?,"size":0.5..5000 px?,"hardness":0..1?,"opacity":0..1?,"flow":0..1?,"fill":bool=false (fill the region the closed `points` enclose with the brush colour, its edge anti-aliased and softened by hardness, instead of stamping the outline; a closed gesture paints a filled dab, not a ring),"spacing":0..10?,"color":"#rrggbb"?=foreground,"mode":"normal|multiply|screen|…|clear (erases with this brush)"="normal","erase":bool?,"smoothing":0..1?,"zoom":number=1,"seed":u64?,"tipBoundary":number in (0,1]? (fade a round dab to zero at this fraction of its nominal radius),"target":"pixels"|"mask"|"filterMask"|"quickMask"|{"channel":i}=Channels panel target}"##, + r##"{"points":[[x,y,pressure?,tiltX?,tiltY?,rotation?,timeMs?,wheel?],…],"brush":{…BrushSettings}?,"preset":name?,"size":0.5..5000 px?,"hardness":0..1?,"opacity":0..1?,"flow":0..1?,"fill":bool=false (fill the region the closed `points` enclose with the brush colour, its edge anti-aliased and softened by hardness, instead of stamping the outline; a closed gesture paints a filled dab, not a ring),"spacing":0..10?,"color":"#rrggbb"?=foreground,"mode":"normal|multiply|screen|…|clear (erases with this brush)"="normal","erase":bool?,"smoothing":0..1?,"zoom":number=1,"seed":u64?,"tipBoundary":number in (0,2]? (fade a round dab to zero at this fraction of its nominal radius, using a 0.5 px minimum nominal radius),"target":"pixels"|"mask"|"filterMask"|"quickMask"|{"channel":i}=Channels panel target}"##, has_paintable, crate::brush_cmds::paint_stroke )] diff --git a/crates/paint/src/lib.rs b/crates/paint/src/lib.rs index 311a4e0ace..967376c21a 100644 --- a/crates/paint/src/lib.rs +++ b/crates/paint/src/lib.rs @@ -169,6 +169,9 @@ pub fn tip_shape(radius: f32, hardness: f32) -> (f32, f32) { ((r0 * radius).min(radius - edge).max(0.0), (s * radius).max(edge).max(1e-3)) } +/// The 1 px minimum Normal outline can be twice the minimum 0.5 px dab radius. +pub const MAX_TIP_BOUNDARY: f32 = 2.0; + /// Dab-invariant round-tip geometry, shared by all pixels and subpixel samples. #[derive(Clone, Copy)] pub(crate) struct TipProfile { @@ -187,7 +190,7 @@ impl TipProfile { pub(crate) fn with_boundary(radius: f32, hardness: f32, fraction: Option) -> Self { let mut tip = Self::new(radius, hardness); - if let Some(fraction) = fraction.filter(|f| *f > 0.0 && *f <= 1.0) { + if let Some(fraction) = fraction.filter(|f| *f > 0.0 && *f <= MAX_TIP_BOUNDARY) { // Scale the solid core with the footprint, then taper the remaining width to zero. tip.reach = radius * fraction; tip.core *= fraction; diff --git a/crates/paint/src/render.rs b/crates/paint/src/render.rs index ec839635f3..7354db4f83 100644 --- a/crates/paint/src/render.rs +++ b/crates/paint/src/render.rs @@ -282,7 +282,7 @@ impl BrushContext { d.radius * std::f32::consts::SQRT_2 } else if aliased { d.radius - } else if let Some(fraction) = self.brush.tip_boundary.filter(|f| !dual && *f > 0.0 && *f <= 1.0) { + } else if let Some(fraction) = self.brush.tip_boundary.filter(|f| !dual && *f > 0.0 && *f <= crate::MAX_TIP_BOUNDARY) { d.radius * fraction } else { crate::tip_reach(d.radius, hardness).max(d.radius) diff --git a/crates/ui-egui/src/canvas/paint_tool/tests.rs b/crates/ui-egui/src/canvas/paint_tool/tests.rs index 7dc6324d61..17765fa01d 100644 --- a/crates/ui-egui/src/canvas/paint_tool/tests.rs +++ b/crates/ui-egui/src/canvas/paint_tool/tests.rs @@ -369,6 +369,29 @@ fn soft_brush_click_stays_inside_the_selected_cursor_outline() { } } +#[test] +fn one_pixel_soft_normal_brush_marks_the_pixel_corner_inside_its_outline() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 32, "height": 32, "background": "transparent"})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 1, "hardness": 0.0}})).unwrap(); + app.ui.tool = Tool::Brush; + assert_eq!(app.session.prefs().cursors.painting, photocraft_engine::prefs::PaintingCursor::NormalTip); + + let check = |doc: &Document| { + let surface = doc.layers[0].surface().unwrap(); + for y in 14..18 { + for x in 14..18 { + // The click straddles four pixel centres inside the cursor's 1 px radius. + assert_eq!(surface.rgba(x, y)[3] > 0.0, (15..=16).contains(&x) && (15..=16).contains(&y), "pixel ({x}, {y})"); + } + } + }; + tool_event(&mut app, ToolEvent::Down { x: 16.0, y: 16.0, pressure: 1.0 }, M); + check(&display_doc(&mut app, 0).0); + tool_event(&mut app, ToolEvent::Up { x: 16.0, y: 16.0 }, 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 34bf0b2616..56bf935399 100644 --- a/crates/ui-egui/src/tools/brush_cursor.rs +++ b/crates/ui-egui/src/tools/brush_cursor.rs @@ -91,7 +91,8 @@ pub(crate) fn soft_tip_boundary(app: &PhotocraftApp, tool: Tool) -> Option if !is_plain(brush) { return Some(photocraft_paint::half_coverage_radius(1.0, brush.hardness)); } - Some((normal_round_radius(brush) / (brush.size / 2.0).max(0.5)).min(1.0)) + // 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)) } fn normal_round_radius(brush: &BrushSettings) -> f32 {