Skip to content

Commit

Permalink
Don't skip first dash in dash pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikh committed Jun 10, 2024
1 parent d6791b4 commit 2ee61dd
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/stroke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ fn dash_impl<T: Iterator<Item = PathEl>>(
) -> DashIterator<T> {
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];
Expand Down Expand Up @@ -771,11 +771,11 @@ impl<'a, T: Iterator<Item = PathEl>> 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);
Expand Down Expand Up @@ -808,7 +808,9 @@ impl<'a, T: Iterator<Item = PathEl>> 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]
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 2ee61dd

Please sign in to comment.