Skip to content

Commit 1751551

Browse files
authored
Fix issue with viewbox not being applied properly (#38)
1 parent 158bdfa commit 1751551

12 files changed

Lines changed: 14 additions & 10 deletions

File tree

src/render/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ pub fn tree_to_x_object(
2626
ctx.deferrer.push();
2727

2828
let mut child_content = Content::new();
29+
child_content.save_state();
30+
child_content.transform(ctx.get_viewbox_transform().as_array());
2931

3032
// The root of a tree is always a group, so we can directly iterate over the children
3133
for el in tree.root.children() {
3234
el.render(writer, &mut child_content, ctx);
3335
}
3436

37+
child_content.restore_state();
38+
3539
let child_content_stream = ctx.finish_content(child_content);
3640

3741
let mut x_object = writer.form_xobject(x_ref, &child_content_stream);
@@ -43,7 +47,7 @@ pub fn tree_to_x_object(
4347

4448
x_object.bbox(Rect::new(0.0, 0.0, ctx.size.width() as f32, ctx.size.height() as f32));
4549
// Apply the base transform
46-
x_object.matrix(ctx.get_base_transform().as_array());
50+
x_object.matrix(ctx.initial_transform.as_array());
4751
x_object.finish();
4852

4953
ctx.deferrer.add_x_object(x_ref)

src/util/context.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,9 @@ impl Context {
4848
self.deferrer.alloc_ref()
4949
}
5050

51-
/// Get the base transform that needs to be applied before rendering everything else (
52-
/// i.e. the initial transform passed by the user + the view box transform to account for the
53-
/// view box of the SVG).
54-
pub fn get_base_transform(&self) -> Transform {
55-
let mut base_transform = self.initial_transform;
56-
let view_box_transform =
57-
view_box_to_transform(self.view_box.rect, self.view_box.aspect, self.size);
58-
base_transform.append(&view_box_transform);
59-
base_transform
51+
// Get the viewbox transform
52+
pub fn get_viewbox_transform(&self) -> Transform {
53+
view_box_to_transform(self.view_box.rect, self.view_box.aspect, self.size)
6054
}
6155

6256
/// Returns a [`usvg` Rect](usvg::Rect) with the dimensions of the whole SVG.
-142 Bytes
Loading
-94 Bytes
Loading
-176 Bytes
Loading
-222 Bytes
Loading
-158 Bytes
Loading
-382 Bytes
Loading
107 Bytes
Loading
-108 Bytes
Loading

0 commit comments

Comments
 (0)