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
20 changes: 19 additions & 1 deletion crates/resvg/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,25 @@ fn render_group(

if !group.filters().is_empty() {
for filter in group.filters() {
crate::filter::apply(filter, transform, &mut sub_pixmap);
// Ensure that our filters also do not exceed the layer size
let filter_transform = if let Some((shrunk, tf_rect)) =
filter.rect().transform(transform).and_then(|tf_rect| {
let int_rect = tf_rect.to_int_rect();
if int_rect.width() <= ctx.max_bbox.width()
|| int_rect.height() <= ctx.max_bbox.height()
{
return None;
}
crate::geom::fit_to_rect(tf_rect.to_int_rect(), ctx.max_bbox)
.map(|new_rect| (new_rect, tf_rect))
}) {
let s_w = shrunk.width() as f32 / tf_rect.to_int_rect().width() as f32;
let s_h = shrunk.height() as f32 / tf_rect.to_int_rect().height() as f32;
transform.pre_scale(s_w, s_h)
Comment on lines +122 to +124
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't it also happen that the shrunken bbox has a different x/y starting point? In which case we probably also have to apply a translational transform? Or am I missing something?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that pre_scale will apply the scaling to any translation that follows as well, unless I've misunderstood what you mean. Maybe an extra test could be good 😅

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's say that the original bbox was top-left: (-30, -30) bottom-right: (120, 120) and is then shrunk to top-left: (0, 0) bottom-right: (75, 75). In this case, you will correctly apply a scale of (0.5, 0.5) to reduce the width from 150 to 75, but you also need to translate by (30, 30) to move the origin to (0, 0), or am I missing something?

Would indeed be good to have a test, but not sure how easy it is to construct one.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the rendering of firefox and chrome, I can see that the huge region example gaussian blur is much more noticeable than the output, so it is still not 100% correct, I guess, but it doesn't panic at least.

In addition, if i make a new test that shifts the blur to an x and y of 50, the image is sharp on the top left corner and the result is clipped. It is not rendered any differently by resvg though. I think that the primitive transform handling in apply_inner may not be correct, but I'll have to look more into it.

} else {
transform
};
crate::filter::apply(filter, filter_transform, &mut sub_pixmap);
}
}

Expand Down
Binary file modified crates/resvg/tests/tests/filters/filter/huge-region.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading