Handle empty paths in PathBuilder::get_current_point()

The case of calling get_current_point() on empty paths has not been
handled and caused panics.
This commit is contained in:
pylbrecht 2019-10-31 19:11:56 +01:00
parent 606ad20544
commit b8b33788b6
3 changed files with 15 additions and 17 deletions

View file

@ -564,22 +564,18 @@ impl GenericPathBuilder for PathBuilder {
}
}
fn get_current_point(&mut self) -> Point2D<f32> {
fn get_current_point(&mut self) -> Option<Point2D<f32>> {
let path = self.finish();
self.0 = Some(path.as_raqote().clone().into());
for op in path.as_raqote().ops.iter().rev() {
match op {
PathOp::MoveTo(point) | PathOp::LineTo(point) => {
return Point2D::new(point.x, point.y)
},
PathOp::CubicTo(_, _, point) => return Point2D::new(point.x, point.y),
PathOp::QuadTo(_, point) => return Point2D::new(point.x, point.y),
PathOp::Close => {},
};
}
panic!("dead end");
path.as_raqote().ops.iter().last().and_then(|op| match op {
PathOp::MoveTo(point) | PathOp::LineTo(point) => Some(Point2D::new(point.x, point.y)),
PathOp::CubicTo(_, _, point) => Some(Point2D::new(point.x, point.y)),
PathOp::QuadTo(_, point) => Some(Point2D::new(point.x, point.y)),
PathOp::Close => None,
})
}
fn line_to(&mut self, point: Point2D<f32>) {
self.0.as_mut().unwrap().line_to(point.x, point.y);
}