mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Brush: keep Standard coverage and centre a subpixel dab (#17161)
Standard and Precise cursors have no tip outline, so leave tipBoundary unset and keep the existing soft falloff. When a bounded subpixel dab sits between the 4x4 supersamples, use the pixel centre's coverage so a click still paints. Spacing warnings compare the path to the bounded dab's reach, not the nominal radius.
This commit is contained in:
1 parent
a3389ad06c
commit
ad90e98871
6 files changed
+69
-5
No files matched your search
@@ -453,7 +453,7 @@ fn with_blend_mode(mut b: BrushSettings, p: &Value) -> BrushSettings {
|
||||
/// without any other sign (#8389). Returns that pitch and a warning naming it; `None` when the
|
||||
/// stroke keeps its coverage or Spacing is off (dabs then follow the stroke's speed, not a pitch).
|
||||
/// Whether the first dab covers the path is judged by that dab's own diameter, so a light first
|
||||
/// point that shrinks a pressure-sized dab still warns.
|
||||
/// point that shrinks a pressure-sized dab still warns. A `tipBoundary` shortens that reach.
|
||||
fn spacing_warning(brush: &BrushSettings, pts: &[StrokePoint]) -> Option<(f64, String)> {
|
||||
use photocraft_paint::dynamics::{DabBuilder, StepInput, apply_pose, base_diameter};
|
||||
let brush = brush.bounded_for_render();
|
||||
@@ -466,7 +466,9 @@ fn spacing_warning(brush: &BrushSettings, pts: &[StrokePoint]) -> Option<(f64, S
|
||||
let step = StepInput { point: first, direction: 0.0, initial_direction: 0.0, step: 0 };
|
||||
let diameter = base_diameter(&brush, &step).max(1.0);
|
||||
let length: f64 = pts.windows(2).map(|w| (w[1].x - w[0].x).hypot(w[1].y - w[0].y)).filter(|d| d.is_finite()).sum();
|
||||
(length > f64::from(diameter) / 2.0 && pitch > length).then(|| {
|
||||
let radius = f64::from(diameter) / 2.0;
|
||||
let reach = brush.tip_boundary.filter(|f| *f > 0.0 && *f <= photocraft_paint::MAX_TIP_BOUNDARY).map_or(radius, |f| radius * f64::from(f));
|
||||
(length > reach && pitch > length).then(|| {
|
||||
let dab = if (diameter - brush.size).abs() < 0.05 { format!("size {}", brush.size) } else { format!("the first dab's {diameter:.1} px (size {} at its pressure)", brush.size) };
|
||||
let warning = format!(
|
||||
"spacing {} × {dab} puts dabs {pitch:.1} px apart, more than the {length:.1} px path, so the stroke got only its first dab; lower `spacing` for a continuous stroke",
|
||||
|
||||
@@ -63,6 +63,23 @@ fn soft_round_stroke_fades_to_zero_at_its_tip_boundary() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_bounded_soft_stroke_warns_when_spacing_leaves_the_path_past_its_reach() {
|
||||
// #8389: the first dab's reach, not the nominal radius, decides whether the path was covered.
|
||||
let mut s = session(200, 80);
|
||||
let r = s
|
||||
.execute(
|
||||
"paint.stroke",
|
||||
json!({"points": [[40, 40], [70, 40]], "size": 80, "hardness": 0, "spacing": 1, "tipBoundary": 0.55, "color": "#000000"}),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(r["pitch"].as_f64(), Some(80.0), "{r}");
|
||||
let warnings = stroke_warnings(&r);
|
||||
assert_eq!(warnings.len(), 1, "{r}");
|
||||
assert!(warnings[0].contains("30.0 px path"), "{r}");
|
||||
assert_eq!(rgba(&s, 70, 40)[3], 0.0, "the endpoint is past the 22 px reach");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stroke_from_far_off_the_canvas_paints_only_what_reaches_it() {
|
||||
// Dabs that cannot reach the canvas are not rasterized (their coverage is never read).
|
||||
|
||||
@@ -149,6 +149,17 @@ fn bounded_one_pixel_soft_tip_tapers_as_the_pixel_centre_reaches_the_edge() {
|
||||
assert_eq!(outside, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bounded_subpixel_soft_tip_paints_when_the_dab_sits_between_supersamples() {
|
||||
// A traced Normal 0.5 px, 0% hardness outline is smaller than the 4x4 sample grid (nearest
|
||||
// sample at about 0.177 px). A dab on the pixel centre must still leave a mark.
|
||||
let outline = 0.25 * crate::half_coverage_radius(1.0, 0.0);
|
||||
let brush = BrushSettings { hardness: 0.0, tip_boundary: Some(outline / 0.5), ..brush() };
|
||||
let r = raster(&brush, &Dab::round(Point::new(50.5, 50.5), 0.5, 1.0));
|
||||
assert!(at(&r, 50, 50) > 0.0, "coverage at the pixel centre, outline {outline}");
|
||||
assert_eq!(at(&r, 51, 50), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sampled_tip_is_scaled_and_oriented() {
|
||||
// Tip: left half painted.
|
||||
|
||||
@@ -239,7 +239,14 @@ impl TipProfile {
|
||||
pub(crate) fn cap_supersampled_coverage(self, distance: f32, average: f32) -> f32 {
|
||||
// Inward samples can still be inside the tip as the pixel centre reaches the boundary.
|
||||
// The centre's falloff keeps their average from leaving a visible jump at the cutoff.
|
||||
if self.bounded { average.min(self.falloff(distance)) } else { average }
|
||||
// When the tip sits between the 4×4 sample points, every sample can be outside while the
|
||||
// centre is still inside; keep the centre's coverage so the dab is not dropped.
|
||||
if self.bounded {
|
||||
let centre = self.falloff(distance);
|
||||
if average > 0.0 { average.min(centre) } else { centre }
|
||||
} else {
|
||||
average
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -402,7 +402,7 @@ fn subpixel_soft_brush_uses_the_displayed_outline() {
|
||||
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)] {
|
||||
for (y, distance, painted) in [(12, outside, false), (20, inside, true), (24, 0.0, true)] {
|
||||
let x = 16.5 - distance;
|
||||
let point_y = f64::from(y) + 0.5;
|
||||
let check = |doc: &Document| {
|
||||
@@ -418,6 +418,32 @@ fn subpixel_soft_brush_uses_the_displayed_outline() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn soft_brush_in_standard_and_precise_cursors_keeps_coverage_past_the_nominal_radius() {
|
||||
use photocraft_engine::prefs::PaintingCursor;
|
||||
|
||||
for (mode, setting) in [(PaintingCursor::Standard, "standard"), (PaintingCursor::Precise, "precise")] {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 128, "height": 128, "background": "transparent"})).unwrap();
|
||||
app.run("tools.setBrush", json!({"brush": {"size": 80, "hardness": 0.0}})).unwrap();
|
||||
app.run("prefs.set", json!({"values": {"cursors.painting": setting}})).unwrap();
|
||||
assert_eq!(app.session.prefs().cursors.painting, mode);
|
||||
app.ui.tool = Tool::Brush;
|
||||
let check = |doc: &Document| {
|
||||
let s = doc.layers[0].surface().unwrap();
|
||||
assert!(s.rgba(64, 64)[3] > 0.9);
|
||||
// 45 px is past the 40 px nominal radius; these cursors have no outline to clip to.
|
||||
assert!(s.rgba(64 + 45, 64)[3] > 0.0, "{mode:?}");
|
||||
};
|
||||
tool_event(&mut app, ToolEvent::Down { x: 64.5, y: 64.5, pressure: 1.0 }, M);
|
||||
check(&display_doc(&mut app, 0).0);
|
||||
tool_event(&mut app, ToolEvent::Up { x: 64.5, y: 64.5 }, M);
|
||||
check(&app.session.active().unwrap().doc);
|
||||
let (_, params) = app.session.journal.iter().rev().find(|(id, _)| id == "paint.stroke").unwrap();
|
||||
assert!(params.get("tipBoundary").is_none(), "{mode:?} {params}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn brush_drag_shows_the_real_stroke_and_commits_it() {
|
||||
// The drag shows the real soft stroke, not a hard flat stand-in.
|
||||
|
||||
@@ -89,7 +89,8 @@ pub(crate) fn soft_tip_boundary(app: &PhotocraftApp, tool: Tool) -> Option<f32>
|
||||
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),
|
||||
// These cursors have no tip outline to match; keep the unbounded soft falloff.
|
||||
PaintingPref::Standard | PaintingPref::Precise => return None,
|
||||
};
|
||||
// The outline uses the selected size, while the renderer uses at least a half-pixel dab radius.
|
||||
Some(outline_radius / radius.max(0.5))
|
||||
|
||||
Reference in new issue
Block a user