Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This changelog also contains important changes in dependencies.

- `dominant-baseline` is now correctly inherited by nested `<tspan>` elements,
so a nested span stays on the same baseline as its siblings. (#864)
- Panics and incorrect clipping when a filter region is smaller than its input. (#1141)

## [0.48.1] 2026-08-02

Expand Down
29 changes: 22 additions & 7 deletions crates/resvg/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ pub fn apply(
source: &mut tiny_skia::Pixmap,
) {
let result = apply_inner(filter, ts, source);
let result = result.and_then(|image| apply_to_canvas(image, source));
let result = result.and_then(|(image, region)| apply_to_canvas(image, region, source));

// Clear on error.
if result.is_err() {
Expand All @@ -357,8 +357,8 @@ pub fn apply(
fn apply_inner(
filter: &usvg::filter::Filter,
ts: usvg::Transform,
source: &mut tiny_skia::Pixmap,
) -> Result<Image, Error> {
source: &tiny_skia::Pixmap,
) -> Result<(Image, IntRect), Error> {
let region = filter
.rect()
.transform(ts)
Expand All @@ -372,6 +372,17 @@ fn apply_inner(
IntRect::from_xywh(0, 0, source.width(), source.height()).ok_or(Error::InvalidRegion)?;
let region = crate::geom::fit_to_rect(region, source_rect).ok_or(Error::InvalidRegion)?;

let cropped_source = if region != source_rect {
// TODO: Avoid copying?
Some(source.copy_region(region)?)
} else {
None
};
let source = cropped_source.as_ref().unwrap_or(source);
let output_region = region;
let ts = ts.post_translate(-region.x() as f32, -region.y() as f32);
let region = region.translate_to(0, 0).ok_or(Error::InvalidRegion)?;

let mut results: Vec<FilterResult> = Vec::new();

for primitive in filter.primitives() {
Expand Down Expand Up @@ -514,7 +525,7 @@ fn apply_inner(
}

if let Some(res) = results.pop() {
Ok(res.image)
Ok((res.image, output_region))
} else {
Err(Error::NoResults)
}
Expand Down Expand Up @@ -1097,13 +1108,17 @@ fn transform_light_source(
source
}

fn apply_to_canvas(input: Image, pixmap: &mut tiny_skia::Pixmap) -> Result<(), Error> {
fn apply_to_canvas(
input: Image,
region: IntRect,
pixmap: &mut tiny_skia::Pixmap,
) -> Result<(), Error> {
let input = input.into_color_space(usvg::filter::ColorInterpolation::SRGB)?;

pixmap.fill(tiny_skia::Color::TRANSPARENT);
pixmap.draw_pixmap(
0,
0,
region.x(),
region.y(),
input.as_ref().as_ref(),
&tiny_skia::PixmapPaint::default(),
tiny_skia::Transform::identity(),
Expand Down
2 changes: 2 additions & 0 deletions crates/resvg/tests/integration/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ use crate::render;
#[test] fn filters_feDiffuseLighting_surfaceScale_eq_0() { assert_eq!(render("tests/filters/feDiffuseLighting/surfaceScale=0"), 0); }
#[test] fn filters_feDiffuseLighting_surfaceScale_eq_1_33() { assert_eq!(render("tests/filters/feDiffuseLighting/surfaceScale=1.33"), 0); }
#[test] fn filters_feDiffuseLighting_surfaceScale_eq_5() { assert_eq!(render("tests/filters/feDiffuseLighting/surfaceScale=5"), 0); }
#[test] fn filters_feDiffuseLighting_tiny_element_with_spot_light() { assert_eq!(render("tests/filters/feDiffuseLighting/tiny-element-with-spot-light"), 0); }
#[test] fn filters_feDisplacementMap_simple_case() { assert_eq!(render("tests/filters/feDisplacementMap/simple-case"), 0); }
#[test] fn filters_feDistantLight_default_attributes() { assert_eq!(render("tests/filters/feDistantLight/default-attributes"), 0); }
#[test] fn filters_feDistantLight_negative_azimuth_and_elevation() { assert_eq!(render("tests/filters/feDistantLight/negative-azimuth-and-elevation"), 0); }
Expand Down Expand Up @@ -393,6 +394,7 @@ use crate::render;
#[test] fn filters_filter_functions_one_invalid_url_in_list() { assert_eq!(render("tests/filters/filter-functions/one-invalid-url-in-list"), 0); }
#[test] fn filters_filter_functions_two_drop_shadow_function() { assert_eq!(render("tests/filters/filter-functions/two-drop-shadow-function"), 0); }
#[test] fn filters_filter_functions_two_exact_urls() { assert_eq!(render("tests/filters/filter-functions/two-exact-urls"), 0); }
#[test] fn filters_filter_functions_two_urls_with_different_regions() { assert_eq!(render("tests/filters/filter-functions/two-urls-with-different-regions"), 0); }
#[test] fn filters_filter_functions_two_urls() { assert_eq!(render("tests/filters/filter-functions/two-urls"), 0); }
#[test] fn filters_filter_functions_url_and_grayscale() { assert_eq!(render("tests/filters/filter-functions/url-and-grayscale"), 0); }
#[test] fn filters_flood_color_hsla_color() { assert_eq!(render("tests/filters/flood-color/hsla-color"), 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.
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.
Loading