diff --git a/src/stroke.rs b/src/stroke.rs index 9daf7021..7678d802 100644 --- a/src/stroke.rs +++ b/src/stroke.rs @@ -650,7 +650,7 @@ fn dash_impl>( ) -> DashIterator { let mut dash_ix = 0; let mut dash_remaining = dash_offset; - let mut is_active = true; + let mut is_active = false; // Find place in dashes array for initial offset. while dash_remaining > 0.0 { let dash_len = dashes[dash_ix]; @@ -771,11 +771,11 @@ impl<'a, T: Iterator> DashIterator<'a, T> { self.is_active = !self.is_active; self.t += t1 * (1.0 - self.t); self.seg_remaining -= self.dash_remaining; + self.dash_remaining = self.dashes[self.dash_ix]; self.dash_ix += 1; if self.dash_ix == self.dashes.len() { self.dash_ix = 0; } - self.dash_remaining = self.dashes[self.dash_ix]; } else { if self.is_active { let seg = self.current_seg.subsegment(self.t..1.0); @@ -808,7 +808,9 @@ impl<'a, T: Iterator> DashIterator<'a, T> { #[cfg(test)] mod tests { - use crate::{stroke, Cap::Butt, CubicBez, Join::Miter, Shape, Stroke}; + use crate::{ + dash, segments, stroke, Cap::Butt, CubicBez, Join::Miter, Line, PathSeg, Shape, Stroke + }; // A degenerate stroke with a cusp at the endpoint. #[test] @@ -858,4 +860,20 @@ mod tests { assert!(stroked.is_finite()); } } + + #[test] + fn dash_sequence() { + let shape = Line::new((0.0, 0.0), (21.0, 0.0)); + let dashes = [1., 5., 2., 5.]; + let expansion = [ + PathSeg::Line(Line::new((0., 0.), (1., 0.))), + PathSeg::Line(Line::new((6., 0.), (8., 0.))), + PathSeg::Line(Line::new((13., 0.), (14., 0.))), + PathSeg::Line(Line::new((19., 0.), (21., 0.))), + ]; + let iter = segments(dash(shape.path_elements(0.), 0., &dashes)); + for (got, want) in iter.zip(expansion) { + assert_eq!(got, want); + } + } }