Skip to content

Commit

Permalink
Fix relative units handling when use references symbol (#832)
Browse files Browse the repository at this point in the history
Closes #829
  • Loading branch information
LaurenzV authored Oct 14, 2024
1 parent 0e99286 commit 6298c1d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/resvg/tests/integration/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,7 @@ use crate::render;
#[test] fn structure_symbol_unused_symbol() { assert_eq!(render("tests/structure/symbol/unused-symbol"), 0); }
#[test] fn structure_symbol_with_custom_use_size() { assert_eq!(render("tests/structure/symbol/with-custom-use-size"), 0); }
#[test] fn structure_symbol_with_overflow_visible() { assert_eq!(render("tests/structure/symbol/with-overflow-visible"), 0); }
#[test] fn structure_symbol_with_size_on_use_and_relative_units() { assert_eq!(render("tests/structure/symbol/with-size-on-use-and-relative-units"), 0); }
#[test] fn structure_symbol_with_transform_on_use_no_size() { assert_eq!(render("tests/structure/symbol/with-transform-on-use-no-size"), 0); }
#[test] fn structure_symbol_with_transform_on_use() { assert_eq!(render("tests/structure/symbol/with-transform-on-use"), 0); }
#[test] fn structure_symbol_with_transform() { assert_eq!(render("tests/structure/symbol/with-transform"), 0); }
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions crates/usvg/src/parser/use_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,32 @@ pub(crate) fn convert(
let linked_to_symbol = child.tag_name() == Some(EId::Symbol);

if linked_to_symbol {
// If a `use` element has a width/height attribute and references a symbol
// then relative units (like percentages) should be resolved relative
// to the width/height of the `use` element, and not the original SVG.
// This is why we need to (potentially) adapt the view box here.
use_state.view_box = {
let def = Length::new(100.0, LengthUnit::Percent);
let x = use_state.view_box.x();
let y = use_state.view_box.y();

let width = if node.has_attribute(AId::Width) {
node.convert_user_length(AId::Width, &use_state, def)
} else {
use_state.view_box.width()
};

let height = if node.has_attribute(AId::Height) {
node.convert_user_length(AId::Height, &use_state, def)
} else {
use_state.view_box.height()
};

NonZeroRect::from_xywh(x, y, width, height)
// Fail silently if the rect is not valid.
.unwrap_or(use_state.view_box)
};

if let Some(ts) = viewbox_transform(node, child, &use_state) {
new_ts = new_ts.pre_concat(ts);
}
Expand Down

0 comments on commit 6298c1d

Please sign in to comment.