Skip to content

Commit

Permalink
Fix unwraps in SVG scaling (#3826)
Browse files Browse the repository at this point in the history
Added error messages when scaling to invalid sizes instead of panicking
through unwrap, returning to previous behavior.

Closes <#3825>.
  • Loading branch information
amPerl authored Jan 16, 2024
1 parent 4c8b95f commit 9e924ec
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions crates/egui_extras/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down

0 comments on commit 9e924ec

Please sign in to comment.