Skip to content

Commit

Permalink
Every nested svg element defines a new viewBox now.
Browse files Browse the repository at this point in the history
Closes #494
  • Loading branch information
RazrFalcon committed Feb 12, 2022
1 parent e80f593 commit 8282b84
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This changelog also contains important changes in dependencies.
- `usvg::ImageKind` stores data as `Arc<Vec<u8>>` and not just `Vec<u8>` now.

### Fixed
- Every nested `svg` element defines a new viewBox now. Previously, we were always using the root one.
- Correctly handle SVG size calculation when SVG doesn't have a size and any elements.
- Improve groups ungrouping speed.

Expand Down
Binary file added tests/png/e-svg-037.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/png/e-svg-038.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/png/e-svg-039.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tests/svg/e-svg-037.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tests/svg/e-svg-038.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions tests/svg/e-svg-039.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,9 @@ use render::render;
#[test] fn e_svg_031() { assert_eq!(render("e-svg-031"), 0); }
#[test] fn e_svg_032() { assert_eq!(render("e-svg-032"), 0); }
#[test] fn e_svg_033() { assert_eq!(render("e-svg-033"), 0); }
#[test] fn e_svg_037() { assert_eq!(render("e-svg-037"), 0); }
#[test] fn e_svg_038() { assert_eq!(render("e-svg-038"), 0); }
#[test] fn e_svg_039() { assert_eq!(render("e-svg-039"), 0); }
#[test] fn e_switch_001() { assert_eq!(render("e-switch-001"), 0); }
#[test] fn e_switch_002() { assert_eq!(render("e-switch-002"), 0); }
#[test] fn e_switch_003() { assert_eq!(render("e-switch-003"), 0); }
Expand Down
3 changes: 3 additions & 0 deletions usvg/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ pub struct State<'a> {
pub(crate) parent_clip_path: Option<svgtree::Node<'a>>,
pub(crate) parent_marker: Option<svgtree::Node<'a>>,
pub(crate) fe_image_link: bool,
/// The size of the root SVG element.
/// Right now, used only by use_node::get_clip_rect.
pub(crate) size: Size,
/// A viewBox of the parent SVG element.
pub(crate) view_box: Rect,
pub(crate) opt: &'a OptionsRef<'a>,
}
Expand Down
23 changes: 20 additions & 3 deletions usvg/src/use_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,29 @@ pub(crate) fn convert_svg(
new_ts.append(&ts);
}

if let Some(clip_rect) = get_clip_rect(node, node, state) {
// We have to create a new state which would have its viewBox set to the current SVG element.
// Note that we're not updating State::size - it's a completely different property.
let mut state = state.clone();
state.view_box = {
if let Some(vb) = node.get_viewbox() {
vb
} else {
// No `viewBox` attribute? Then use `x`, `y`, `width` and `height` instead.
let x = node.convert_user_length(AId::X, &state, Length::zero());
let y = node.convert_user_length(AId::Y, &state, Length::zero());
let def = Length::new(100.0, LengthUnit::Percent);
let w = node.convert_user_length(AId::Width, &state, def);
let h = node.convert_user_length(AId::Height, &state, def);
Rect::new(x, y, w, h).unwrap_or(state.view_box)
}
};

if let Some(clip_rect) = get_clip_rect(node, node, &state) {
let mut g = clip_element(node, clip_rect, orig_ts, id_generator, parent, tree);
convert_children(node, new_ts, state, id_generator, &mut g, tree);
convert_children(node, new_ts, &state, id_generator, &mut g, tree);
} else {
orig_ts.append(&new_ts);
convert_children(node, orig_ts, state, id_generator, parent, tree);
convert_children(node, orig_ts, &state, id_generator, parent, tree);
}
}

Expand Down

0 comments on commit 8282b84

Please sign in to comment.