fix crash occurring when a path cannot be created

this can occur if tiny_skia decides it doesn't like the path (e.g. circle with negative radius)
i don't really care to render these since they're degenerate cases anyways
This commit is contained in:
liquidex 2024-10-23 21:45:11 +02:00
parent b059ef4c35
commit 43e6951f7d

View file

@ -89,7 +89,7 @@ impl<'a> Renderer<'a> {
Ok(()) Ok(())
} }
fn shape_to_path(shape: &Shape) -> Path { fn shape_to_path(shape: &Shape) -> Option<Path> {
let mut pb = PathBuilder::new(); let mut pb = PathBuilder::new();
match shape { match shape {
Shape::Point(vec) => { Shape::Point(vec) => {
@ -111,7 +111,7 @@ impl<'a> Renderer<'a> {
pb.push_circle(position.x, position.y, *radius); pb.push_circle(position.x, position.y, *radius);
} }
} }
pb.finish().unwrap() pb.finish()
} }
fn render_stroke(&mut self, _vm: &Vm, _value: Value, stroke: &Stroke) -> Result<(), Exception> { fn render_stroke(&mut self, _vm: &Vm, _value: Value, stroke: &Stroke) -> Result<(), Exception> {
@ -120,8 +120,7 @@ impl<'a> Renderer<'a> {
..default_paint() ..default_paint()
}; };
let transform = self.transform(); let transform = self.transform();
let path = Self::shape_to_path(&stroke.shape); if let Some(path) = Self::shape_to_path(&stroke.shape) {
self.pixmap_mut().stroke_path( self.pixmap_mut().stroke_path(
&path, &path,
&paint, &paint,
@ -133,6 +132,7 @@ impl<'a> Renderer<'a> {
transform, transform,
None, None,
); );
}
Ok(()) Ok(())
} }
@ -142,11 +142,12 @@ impl<'a> Renderer<'a> {
shader: Shader::SolidColor(tiny_skia_color(fill.color)), shader: Shader::SolidColor(tiny_skia_color(fill.color)),
..default_paint() ..default_paint()
}; };
let transform = self.transform();
let path = Self::shape_to_path(&fill.shape);
let transform = self.transform();
if let Some(path) = Self::shape_to_path(&fill.shape) {
self.pixmap_mut() self.pixmap_mut()
.fill_path(&path, &paint, FillRule::EvenOdd, transform, None); .fill_path(&path, &paint, FillRule::EvenOdd, transform, None);
}
Ok(()) Ok(())
} }