From 6d3f63752b0f8db65bc8eaaebf0e329a0765e758 Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Fri, 30 Oct 2026 15:28:29 +0700 Subject: [PATCH] Linetypes: one ranges function that takes the layout Co-Authored-By: Claude Opus 5.5 --- crates/render/src/linetype.rs | 20 ++++++++------------ crates/render/src/wide.rs | 2 +- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/crates/render/src/linetype.rs b/crates/render/src/linetype.rs index 2c2b39350..e2a63e5c1 100644 --- a/crates/render/src/linetype.rs +++ b/crates/render/src/linetype.rs @@ -212,15 +212,11 @@ pub fn apply_as(pts: &[Vec2], lt: &Linetype, scale: f64, min_pattern: f64, how: } } -/// The dashes of the pattern along an open run of length `len`, as (start, end) distances; a dot -/// has start == end. Laid out like [`apply`]. `None` when the run is drawn continuous: shorter -/// than one period, a scaled pattern shorter than `min_pattern`, or too many repeats. -pub fn ranges(len: f64, lt: &Linetype, scale: f64, min_pattern: f64) -> Option> { - ranges_as(len, lt, scale, min_pattern, Layout::Open) -} - -/// [`ranges`] with the run laid out as `how` says. -pub fn ranges_as(len: f64, lt: &Linetype, scale: f64, min_pattern: f64, how: Layout) -> Option> { +/// The dashes of the pattern along a run of length `len` laid out as `how` says, as (start, end) +/// distances; a dot has start == end. Laid out like [`apply_as`]. `None` when the run is drawn +/// continuous: shorter than one period, a scaled pattern shorter than `min_pattern`, or too many +/// repeats. +pub fn ranges(len: f64, lt: &Linetype, scale: f64, min_pattern: f64, how: Layout) -> Option> { if len > 0.0 { scaled(lt, scale, min_pattern).and_then(|p| layout(len, &p, how)) } else { None } } @@ -254,7 +250,7 @@ mod tests { #[test] fn an_all_gap_pattern_draws_nothing_without_spinning() { let lt = Linetype::simple("GAPS", "", &[-0.001, -0.002]); - assert_eq!(ranges(1.0, <, 1.0, 0.0), Some(Vec::new())); + assert_eq!(ranges(1.0, <, 1.0, 0.0, Layout::Open), Some(Vec::new())); assert!(apply(&[Vec2::ZERO, Vec2::new(1.0, 0.0)], <, 1.0, 0.0).is_empty()); } @@ -375,7 +371,7 @@ mod tests { assert_eq!(f, 1.0); // Every whole dash starts a gap (1) after a period (3) starts. let whole: Vec = - ranges(len, <, 1.0, 0.0).unwrap().into_iter().filter(|(a, b)| (b - a - 2.0).abs() < 1e-9).map(|(a, _)| a).collect(); + ranges(len, <, 1.0, 0.0, Layout::Open).unwrap().into_iter().filter(|(a, b)| (b - a - 2.0).abs() < 1e-9).map(|(a, _)| a).collect(); assert!(!whole.is_empty(), "{len}"); for a in whole { let k = (a - 1.0 - start) / 3.0; @@ -409,6 +405,6 @@ mod tests { fn a_run_shorter_than_one_period_is_continuous() { let lt = Linetype::simple("DASHED", "", &[2.0, -1.0]); assert_eq!(apply(&[Vec2::ZERO, Vec2::new(2.99, 0.0)], <, 1.0, 0.0).len(), 1); - assert_eq!(ranges(2.99, <, 1.0, 0.0), None); + assert_eq!(ranges(2.99, <, 1.0, 0.0, Layout::Open), None); } } diff --git a/crates/render/src/wide.rs b/crates/render/src/wide.rs index 75f8a5c69..7ba78773e 100644 --- a/crates/render/src/wide.rs +++ b/crates/render/src/wide.rs @@ -89,7 +89,7 @@ pub fn dashed(p: &LwPolyline, tol: f64, lt: &Linetype, scale: f64, min_pattern: let mut centres = Vec::new(); for run in &runs { let len = run.last().map(|x| x.s).unwrap_or(0.0); - let dashes = linetype::ranges_as(len, lt, scale, min_pattern, how).unwrap_or_else(|| vec![(0.0, len)]); + let dashes = linetype::ranges(len, lt, scale, min_pattern, how).unwrap_or_else(|| vec![(0.0, len)]); for (a, b) in dashes { if b - a <= 1e-12 { if let Some(x) = at(run, a) {