From 43e6951f7d9eb692c8b8b423fc25249cbc6ecfc0 Mon Sep 17 00:00:00 2001 From: liquidev Date: Wed, 23 Oct 2024 21:45:11 +0200 Subject: [PATCH] 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 --- crates/haku/src/render.rs | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/crates/haku/src/render.rs b/crates/haku/src/render.rs index ed8aa74..b31fa7d 100644 --- a/crates/haku/src/render.rs +++ b/crates/haku/src/render.rs @@ -89,7 +89,7 @@ impl<'a> Renderer<'a> { Ok(()) } - fn shape_to_path(shape: &Shape) -> Path { + fn shape_to_path(shape: &Shape) -> Option { let mut pb = PathBuilder::new(); match shape { Shape::Point(vec) => { @@ -111,7 +111,7 @@ impl<'a> Renderer<'a> { 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> { @@ -120,19 +120,19 @@ impl<'a> Renderer<'a> { ..default_paint() }; let transform = self.transform(); - let path = Self::shape_to_path(&stroke.shape); - - self.pixmap_mut().stroke_path( - &path, - &paint, - &SStroke { - width: stroke.thickness, - line_cap: LineCap::Round, - ..Default::default() - }, - transform, - None, - ); + if let Some(path) = Self::shape_to_path(&stroke.shape) { + self.pixmap_mut().stroke_path( + &path, + &paint, + &SStroke { + width: stroke.thickness, + line_cap: LineCap::Round, + ..Default::default() + }, + transform, + None, + ); + } Ok(()) } @@ -142,11 +142,12 @@ impl<'a> Renderer<'a> { shader: Shader::SolidColor(tiny_skia_color(fill.color)), ..default_paint() }; - let transform = self.transform(); - let path = Self::shape_to_path(&fill.shape); - self.pixmap_mut() - .fill_path(&path, &paint, FillRule::EvenOdd, transform, None); + let transform = self.transform(); + if let Some(path) = Self::shape_to_path(&fill.shape) { + self.pixmap_mut() + .fill_path(&path, &paint, FillRule::EvenOdd, transform, None); + } Ok(()) }