Brush: honor the minimum Normal tip outline (#17161)

This commit is contained in:
Terra854 committed 2026-11-27 14:00:26 +08:00
1 parent d70e57ebb8
commit ef4cbb4ae4
7 files changed
+39 -9

No files matched your search

+3 -2
View File
@@ -480,8 +480,9 @@ fn stroke_brush(s: &Session, p: &Value) -> Result<BrushSettings> {
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)
}
@@ -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]
@@ -8,7 +8,7 @@ pub fn specs() -> Vec<CommandSpec> {
"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
)]
+4 -1
View File
@@ -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<f32>) -> 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;
+1 -1
View File
@@ -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)
@@ -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.
+2 -1
View File
@@ -91,7 +91,8 @@ pub(crate) fn soft_tip_boundary(app: &PhotocraftApp, tool: Tool) -> Option<f32>
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 {