Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion crates/usvg/src/parser/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,16 @@ fn resolve_svg_size(svg: &SvgNode, opt: &Options) -> (Result<Size, Error>, bool)
svg.convert_user_length(AId::Height, &state, def)
};

Size::from_wh(w, h)
// If only one of height/width is not specified, its value should be
// computed from the other and the viewbox' aspect ratio.
match (
svg.attribute::<Length>(AId::Width),
svg.attribute::<Length>(AId::Height),
) {
(Some(_), None) => Size::from_wh(w, vbox.height() * w / vbox.width()),
(None, Some(_)) => Size::from_wh(vbox.width() * h / vbox.height(), h),
(_, _) => Size::from_wh(w, h),
}
} else {
Size::from_wh(
svg.convert_user_length(AId::Width, &state, def),
Expand Down
28 changes: 28 additions & 0 deletions crates/usvg/tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,34 @@ fn size_detection_5() {
assert_eq!(tree.size(), usvg::Size::from_wh(100.0, 100.0).unwrap());
}

#[test]
fn size_detection_6() {
let svg = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10' width='30'/>";
let tree = usvg::Tree::from_str(&svg, &usvg::Options::default()).unwrap();
assert_eq!(tree.size(), usvg::Size::from_wh(30.0, 30.0).unwrap());
}

#[test]
fn size_detection_7() {
let svg = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 20' width='30'/>";
let tree = usvg::Tree::from_str(&svg, &usvg::Options::default()).unwrap();
assert_eq!(tree.size(), usvg::Size::from_wh(30.0, 60.0).unwrap());
}

#[test]
fn size_detection_8() {
let svg = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10' height='30'/>";
let tree = usvg::Tree::from_str(&svg, &usvg::Options::default()).unwrap();
assert_eq!(tree.size(), usvg::Size::from_wh(30.0, 30.0).unwrap());
}

#[test]
fn size_detection_9() {
let svg = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 20' height='30'/>";
let tree = usvg::Tree::from_str(&svg, &usvg::Options::default()).unwrap();
assert_eq!(tree.size(), usvg::Size::from_wh(15.0, 30.0).unwrap());
}

#[test]
fn invalid_size_1() {
let svg = "<svg width='0' height='0' viewBox='0 0 10 20' xmlns='http://www.w3.org/2000/svg'/>";
Expand Down
Loading