From 9e924ec5f08c57242ea4134aa85b053577f52eff Mon Sep 17 00:00:00 2001 From: Romet Tagobert Date: Tue, 16 Jan 2024 10:57:48 +0200 Subject: [PATCH] Fix unwraps in SVG scaling (#3826) Added error messages when scaling to invalid sizes instead of panicking through unwrap, returning to previous behavior. Closes . --- crates/egui_extras/src/image.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/crates/egui_extras/src/image.rs b/crates/egui_extras/src/image.rs index 31263aa9a66..a0b042e26ae 100644 --- a/crates/egui_extras/src/image.rs +++ b/crates/egui_extras/src/image.rs @@ -245,10 +245,27 @@ pub fn load_svg_bytes_with_size( let mut size = rtree.size.to_int_size(); match size_hint { None => (), - Some(SizeHint::Size(w, h)) => size = size.scale_to(IntSize::from_wh(w, h).unwrap()), - Some(SizeHint::Height(h)) => size = size.scale_to_height(h).unwrap(), - Some(SizeHint::Width(w)) => size = size.scale_to_width(w).unwrap(), - Some(SizeHint::Scale(z)) => size = size.scale_by(z.into_inner()).unwrap(), + Some(SizeHint::Size(w, h)) => { + size = size.scale_to( + IntSize::from_wh(w, h).ok_or_else(|| format!("Failed to scale SVG to {w}x{h}"))?, + ); + } + Some(SizeHint::Height(h)) => { + size = size + .scale_to_height(h) + .ok_or_else(|| format!("Failed to scale SVG to height {h}"))?; + } + Some(SizeHint::Width(w)) => { + size = size + .scale_to_width(w) + .ok_or_else(|| format!("Failed to scale SVG to width {w}"))?; + } + Some(SizeHint::Scale(z)) => { + let z_inner = z.into_inner(); + size = size + .scale_by(z_inner) + .ok_or_else(|| format!("Failed to scale SVG by {z_inner}"))?; + } }; let (w, h) = (size.width(), size.height());