diff --git a/crates/brush/src/tests.rs b/crates/brush/src/tests.rs index 0bb297e425..15b8f252aa 100644 --- a/crates/brush/src/tests.rs +++ b/crates/brush/src/tests.rs @@ -2218,6 +2218,22 @@ fn pattern_brush_scale_from_the_pen_fits_start_and_end_tiles_where_the_scale_fal let r = out.first().and_then(Node::geometric_bounds); assert!(r.is_some_and(|r| near(r.x0, span.0, 1e-6) && near(r.x1, span.1, 1e-6)), "start {start}: {r:?}"); } + // 400 % at the start, 100 % from 15 to 20 pt, 400 % from 40 pt on, mirrored for the end: a 30 pt + // tile at 100 % fits at its middle, although at the scale at its first length's middle it is as + // long as that length. + for (start, points, span) in [ + (true, vec![(0.0, 1.0), (0.15, 0.0), (0.2, 0.0), (0.4, 1.0), (1.0, 1.0)], (0.0, 30.0)), + (false, vec![(0.0, 1.0), (0.6, 1.0), (0.8, 0.0), (0.85, 0.0), (1.0, 1.0)], (70.0, 100.0)), + ] { + let mut b = scaled_pattern(PatternFit::Stretch, false, 100.0, Some(Variation::Pressure), Some(400.0)); + if let BrushKind::Pattern(p) = &mut b.kind { + let tile = Some(rect_art(30.0, 4.0)); + (p.start, p.end) = if start { (tile, None) } else { (None, tile) }; + } + let out = stroke_pieces(&doc, &b, &bp, &pressed(points)); + let r = out.first().and_then(Node::geometric_bounds); + assert!(r.is_some_and(|r| near(r.x0, span.0, 1e-6) && near(r.x1, span.1, 1e-6)), "start {start}: {r:?}"); + } } /// On a closed path, a tile that Approximate Path starts before the path's start takes the scale @@ -2249,6 +2265,47 @@ fn pattern_brush_scale_from_the_pen_wraps_before_a_closed_paths_start() { assert!(before > 0, "no first tile starts before the path's start"); } +/// On a closed path, a tile over the path's start follows the scale on both sides of the start, +/// even when the scale is the same at the tile's ends, its middle and every pen sample within it. +#[test] +fn pattern_brush_scale_from_the_pen_changes_where_a_closed_path_wraps_round() { + let doc = Document::new(1.0, 1.0); + let mut b = scaled_pattern(PatternFit::Approximate, false, 100.0, Some(Variation::Pressure), Some(300.0)); + if let BrushKind::Pattern(p) = &mut b.kind { + p.side = rect_art(54.0, 4.0); + } + // A closed path 256 pt long without corners, from (0, 0) along +x: its sides in each quarter + // turn, and the same turned three more times. + let quarter = [(28.0, 0.0), (12.0, 5.0), (4.0, 3.0), (3.0, 4.0), (5.0, 12.0)]; + let mut bp = BezPath::new(); + let mut at = (0.0, 0.0); + bp.move_to(at); + for turn in 0..4 { + for &(x, y) in &quarter { + let (dx, dy) = match turn { + 0 => (x, y), + 1 => (-y, x), + 2 => (-x, -y), + _ => (y, -x), + }; + at = (at.0 + dx, at.1 + dy); + if at != (0.0, 0.0) { + bp.line_to(at); + } + } + } + bp.close_path(); + // 100 % up to 4 pt before the end, rising to 300 % at the end. Approximate Path places five + // 54 pt tiles, the first from 7 pt before the start: at 100 % at both its ends, its middle and + // the samples at 4 pt before the start and at the start, but larger from 4 pt before it. + let st = pressed(vec![(0.0, 0.0), (252.0 / 256.0, 0.0), (1.0, 1.0)]); + let out = stroke_pieces(&doc, &b, &bp, &st); + let Some(first) = out.first().and_then(Node::geometric_bounds) else { panic!("no tiles") }; + // The 4 pt tall tile is 2 pt either side of the path at 100 %, and 5 pt at 1 pt before the + // start, where the scale is 250 %. + assert!(first.y0 < -3.5, "{first:?}"); +} + /// Definitions from files and commands are kept within their ranges. #[test] fn sanitize_keeps_values_within_their_ranges() { diff --git a/crates/brush/src/warp.rs b/crates/brush/src/warp.rs index b61b16dd81..06d9655b21 100644 --- a/crates/brush/src/warp.rs +++ b/crates/brush/src/warp.rs @@ -649,7 +649,8 @@ fn tile_on(t: &Track, tile: &(Node, Rect), s0: f64, s1: f64, cross: f64) -> Opti /// A tile placed on [s0, s1] of the track, `k(s)` across at arc length `s` when the scale varies /// (`marks`: the arc lengths of the recorded pen samples in order, between which the scale runs /// in a straight line), else `k` at the middle of the span across all of it ([`tile_on`]), as it -/// is too when the scale is the same at both ends of the span and at every sample within it. +/// is too when the scale is the same at both ends of the span, at every sample within it and, at +/// a closed track's start within it, on both sides of the start. fn tile_scaled(t: &Track, tile: &(Node, Rect), s0: f64, s1: f64, k: &dyn Fn(f64) -> f64, marks: Option<&[f64]>) -> Option { let mid = k((s0 + s1) / 2.0); // Only the samples within the span are looked at. @@ -658,7 +659,12 @@ fn tile_scaled(t: &Track, tile: &(Node, Rect), s0: f64, s1: f64, k: &dyn Fn(f64) let to = from + marks[from..].partition_point(|&s| s < s1); marks[from..to].iter().all(|&s| k(s) == mid) }; - let even = |marks: &[f64]| k(s0) == mid && k(s1) == mid && within(marks); + // A closed track's scale goes from the one at its end to the one at its start where it wraps + // round, so a span over its start (or a track's length before or after it) takes both. + let l = t.len(); + let seam = t.closed && l > 0.0 && (s0 / l).ceil() <= (s1 / l).floor(); + let ends = || !seam || (k(0.0) == mid && k(l) == mid); + let even = |marks: &[f64]| k(s0) == mid && k(s1) == mid && within(marks) && ends(); if mid <= 0.0 || marks.is_none_or(even) { return tile_on(t, tile, s0, s1, mid); } @@ -728,13 +734,14 @@ fn pattern_scale(pb: &PatternBrush, weight: f64, stroke: &StrokeLayer) -> (f64, /// tried in order (evenly spaced ones, and `breaks`, the lengths whose middle falls on a recorded /// pen sample), and the range between the last one the tile is too long for and the first one it /// fits is halved. A tile that fits at no length up to `room` gets the length it needs at `room`, -/// longer than `room`. -fn end_tile_length(width: f64, room: f64, k_mid: &dyn Fn(f64) -> f64, breaks: &[f64]) -> f64 { +/// longer than `room`. `breaks` is `None` when the scale is the same along the whole track, and +/// the tile then takes its length at that scale. +fn end_tile_length(width: f64, room: f64, k_mid: &dyn Fn(f64) -> f64, breaks: Option<&[f64]>) -> f64 { let length = |w: f64| width * k_mid(w); let first = length(0.0); - if length(first) == first || room.is_nan() || room <= 0.0 { + let Some(breaks) = breaks.filter(|_| !room.is_nan() && room > 0.0) else { return first; - } + }; let mut tries: Vec = (1..=END_SCAN_STEPS).map(|i| room * i as f64 / END_SCAN_STEPS as f64).collect(); tries.extend(breaks.iter().copied().filter(|&w| w > 0.0 && w < room)); tries.sort_by(f64::total_cmp); @@ -1171,8 +1178,8 @@ pub(crate) fn pattern(pb: &PatternBrush, bp: &BezPath, weight: f64, stroke: &Str && let Some(st) = &start { // As long as the scale at its middle. - let breaks: Vec = marks.iter().flatten().map(|&m| 2.0 * (m - a)).collect(); - let w = end_tile_length(st.1.width(), b - a, &|w| ks(a + w / 2.0), &breaks); + let breaks: Option> = marks.as_ref().map(|m| m.iter().map(|&m| 2.0 * (m - a)).collect()); + let w = end_tile_length(st.1.width(), b - a, &|w| ks(a + w / 2.0), breaks.as_deref()); if b - a > w { out.take(tile_scaled(&t, st, a, a + w, &ks, marks.as_deref())); a += w; @@ -1182,8 +1189,8 @@ pub(crate) fn pattern(pb: &PatternBrush, bp: &BezPath, weight: f64, stroke: &Str && ri + 1 == nruns && let Some(en) = &end { - let breaks: Vec = marks.iter().flatten().map(|&m| 2.0 * (b - m)).collect(); - let w = end_tile_length(en.1.width(), b - a, &|w| ks(b - w / 2.0), &breaks); + let breaks: Option> = marks.as_ref().map(|m| m.iter().map(|&m| 2.0 * (b - m)).collect()); + let w = end_tile_length(en.1.width(), b - a, &|w| ks(b - w / 2.0), breaks.as_deref()); if b - a > w { out.take(tile_scaled(&t, en, b - w, b, &ks, marks.as_deref())); b -= w;