Skip to content

Truly independent web browser

LibWeb: Keep a backdrop filter’s feImage at its aspect ratio

sideshowbarker31afb14ca981
diff --git a/Libraries/LibWeb/Rust/src/painting/svg_filter.rs b/Libraries/LibWeb/Rust/src/painting/svg_filter.rs
index 6791ca2d18c..be4a3b3b1d1 100644
--- a/Libraries/LibWeb/Rust/src/painting/svg_filter.rs
+++ b/Libraries/LibWeb/Rust/src/painting/svg_filter.rs
@@ -524,27 +524,38 @@ impl SvgFilterGraphBuilder {
));
};
let src_rect = values.image_src_rect;
+ let natural_size = if values.image_natural_width > 0.0 && values.image_natural_height > 0.0 {
+ (values.image_natural_width, values.image_natural_height)
+ } else {
+ (src_rect.width as f32, src_rect.height as f32)
+ };
let Some(subregion) = subregion else {
- let dest_rect = self.image_rect_without_target?;
+ // An image drawn over the box without a subregion still fits it as its preserveAspectRatio says,
+ // and is cut to the box like any other primitive's result, before a later primitive moves it.
+ let box_rect = self.image_rect_without_target?;
+ let dest_rect = crate::painting::record::paint::svg::image_draw_rect(
+ box_rect,
+ natural_size,
+ values.image_preserve_aspect_ratio_align,
+ values.image_preserve_aspect_ratio_meet_or_slice,
+ );
return Some(FilterResult::new(
- Filter::Image {
- frame_id: frame.id(),
- src_rect,
- dest_rect,
- scaling_mode: crate::painting::record::paint::background::to_gfx_scaling_mode(
- self.image_rendering,
- (src_rect.width, src_rect.height),
- (dest_rect.width.round() as i32, dest_rect.height.round() as i32),
- ),
+ Filter::Crop {
+ rect: box_rect,
+ input: Some(Box::new(Filter::Image {
+ frame_id: frame.id(),
+ src_rect,
+ dest_rect,
+ scaling_mode: crate::painting::record::paint::background::to_gfx_scaling_mode(
+ self.image_rendering,
+ (src_rect.width, src_rect.height),
+ (dest_rect.width.round() as i32, dest_rect.height.round() as i32),
+ ),
+ })),
},
InterpolationColorSpace::SRGB,
));
};
- let natural_size = if values.image_natural_width > 0.0 && values.image_natural_height > 0.0 {
- (values.image_natural_width, values.image_natural_height)
- } else {
- (src_rect.width as f32, src_rect.height as f32)
- };
let draw_rect = crate::painting::record::paint::svg::image_draw_rect(
subregion,
natural_size,
diff --git a/Tests/LibWeb/Ref/expected/svg/backdrop-filter-feimage-is-cut-to-the-box-ref.html b/Tests/LibWeb/Ref/expected/svg/backdrop-filter-feimage-is-cut-to-the-box-ref.html
new file mode 100644
index 00000000000..48950fe36ab
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/svg/backdrop-filter-feimage-is-cut-to-the-box-ref.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<style>
+ body { margin: 0; }
+ div { position: absolute; left: 50px; top: 0; width: 50px; height: 100px; background: green; }
+</style>
+<div></div>
diff --git a/Tests/LibWeb/Ref/expected/svg/backdrop-filter-feimage-keeps-its-aspect-ratio-ref.html b/Tests/LibWeb/Ref/expected/svg/backdrop-filter-feimage-keeps-its-aspect-ratio-ref.html
new file mode 100644
index 00000000000..751768deaea
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/svg/backdrop-filter-feimage-keeps-its-aspect-ratio-ref.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<style>
+ body { margin: 0; }
+ div { position: absolute; left: 0; top: 25px; width: 100px; height: 50px; background: green; }
+</style>
+<div></div>
diff --git a/Tests/LibWeb/Ref/input/svg/backdrop-filter-feimage-is-cut-to-the-box.html b/Tests/LibWeb/Ref/input/svg/backdrop-filter-feimage-is-cut-to-the-box.html
new file mode 100644
index 00000000000..26451078363
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/svg/backdrop-filter-feimage-is-cut-to-the-box.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<link rel="match" href="../../expected/svg/backdrop-filter-feimage-is-cut-to-the-box-ref.html" />
+<!-- The image resamples where the crop cuts it, which blends the one column at the crop's edge. -->
+<meta name="fuzzy" content="maxDifference=0-64;totalPixels=0-100">
+<style>
+ body { margin: 0; }
+ div { width: 100px; height: 100px; backdrop-filter: url(#image); }
+</style>
+<svg width="0" height="0" style="position: absolute">
+ <filter id="image" x="0" y="0" width="1" height="1">
+ <feImage preserveAspectRatio="xMidYMid slice" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 100'%3E%3Crect width='200' height='100' fill='red'/%3E%3Crect x='50' width='100' height='100' fill='green'/%3E%3C/svg%3E" />
+ <feOffset dx="50" dy="0" />
+ </filter>
+</svg>
+<!-- The sliced image reaches past the box, but is cut to it before the offset moves it, so no red comes in. -->
+<div></div>
diff --git a/Tests/LibWeb/Ref/input/svg/backdrop-filter-feimage-keeps-its-aspect-ratio.html b/Tests/LibWeb/Ref/input/svg/backdrop-filter-feimage-keeps-its-aspect-ratio.html
new file mode 100644
index 00000000000..4dcb6b59008
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/svg/backdrop-filter-feimage-keeps-its-aspect-ratio.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<link rel="match" href="../../expected/svg/backdrop-filter-feimage-keeps-its-aspect-ratio-ref.html" />
+<style>
+ body { margin: 0; }
+ div { width: 100px; height: 100px; backdrop-filter: url(#image); }
+</style>
+<svg width="0" height="0" style="position: absolute">
+ <filter id="image" x="0" y="0" width="1" height="1">
+ <feImage href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 100'%3E%3Crect width='200' height='100' fill='green'/%3E%3C/svg%3E" />
+ </filter>
+</svg>
+<!-- A 2:1 image drawn over a square box keeps its aspect ratio, centered. -->
+<div></div>