From 04c0ba04bf8574acdcbd5ad9fa20ac9c863f6087 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 4 Apr 2023 01:47:18 +0200 Subject: Warn about invalid paths in `iced_tiny_skia` instead of panicking --- tiny_skia/src/geometry.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'tiny_skia/src/geometry.rs') diff --git a/tiny_skia/src/geometry.rs b/tiny_skia/src/geometry.rs index c66621dd..4e3941f3 100644 --- a/tiny_skia/src/geometry.rs +++ b/tiny_skia/src/geometry.rs @@ -39,7 +39,7 @@ impl Frame { } pub fn fill(&mut self, path: &Path, fill: impl Into) { - let path = convert_path(path); + let Some(path) = convert_path(path) else { return }; let fill = fill.into(); self.primitives.push(Primitive::Fill { @@ -56,7 +56,7 @@ impl Frame { size: Size, fill: impl Into, ) { - let path = convert_path(&Path::rectangle(top_left, size)); + let Some(path) = convert_path(&Path::rectangle(top_left, size)) else { return }; let fill = fill.into(); self.primitives.push(Primitive::Fill { @@ -71,7 +71,8 @@ impl Frame { } pub fn stroke<'a>(&mut self, path: &Path, stroke: impl Into>) { - let path = convert_path(path); + let Some(path) = convert_path(path) else { return }; + let stroke = stroke.into(); let skia_stroke = into_stroke(&stroke); @@ -151,7 +152,7 @@ impl Frame { } } -fn convert_path(path: &Path) -> tiny_skia::Path { +fn convert_path(path: &Path) -> Option { use iced_graphics::geometry::path::lyon_path; let mut builder = tiny_skia::PathBuilder::new(); @@ -205,9 +206,14 @@ fn convert_path(path: &Path) -> tiny_skia::Path { } } - builder - .finish() - .expect("Convert lyon path to tiny_skia path") + let result = builder.finish(); + + #[cfg(debug_assertions)] + if result.is_none() { + log::warn!("Invalid path: {:?}", path.raw()); + } + + result } pub fn into_paint(style: Style) -> tiny_skia::Paint<'static> { -- cgit