Search backward for a non-coincident point if the two ending points of a capped
stroke are coincident.
This prevents a divide by zero in normalization.
@@ -15,6 +15,7 @@ use crate::segment::Segment;
use pathfinder_geometry::line_segment::LineSegment2F;
use pathfinder_geometry::rect::RectF;
use pathfinder_geometry::transform2d::Transform2F;
+use pathfinder_geometry::util::EPSILON;
use pathfinder_geometry::vector::Vector2F;
use std::f32;
@@ -117,7 +118,21 @@ impl<'a> OutlineStrokeToFill<'a> {
}
let width = self.style.line_width;
- let (p0, p1) = (contour.position_of_last(2), contour.position_of_last(1));
+ let p1 = contour.position_of_last(1);
+
+ // Determine the ending gradient.
+ let mut p0;
+ let mut p0_index = contour.len() - 2;
+ loop {
+ p0 = contour.position_of(p0_index);
+ if (p1 - p0).square_length() > EPSILON {
+ break;
+ }
+ if p0_index == 0 {
+ return;
+ }
+ p0_index -= 1;
+ }
let gradient = (p1 - p0).normalize();
match self.style.line_cap {
