Truly independent web browser
LibWeb: Scale SVG filter lengths by the bounding box when asked to
sideshowbarkerd3f2c55213a1
diff --git a/Libraries/LibWeb/Rust/src/painting/svg_filter.rs b/Libraries/LibWeb/Rust/src/painting/svg_filter.rsindex 7a902914b6a..6791ca2d18c 100644--- a/Libraries/LibWeb/Rust/src/painting/svg_filter.rs+++ b/Libraries/LibWeb/Rust/src/painting/svg_filter.rs@@ -216,6 +216,29 @@ impl SvgFilterGraphBuilder {self.image_rendering = image_rendering;}+ /// A horizontal length of a primitive, such as a blur's x standard deviation, in device pixels.+ // https://drafts.csswg.org/filter-effects-1/#element-attrdef-filter-primitiveunits+ // If primitiveUnits="objectBoundingBox", then any length values within the filter definitions represent fractions or+ // percentages of the bounding box on the referencing element.+ fn horizontal_length(&self, length: f32) -> f32 {+ match self.target {+ Some(target) if target.region.primitive_units_are_object_bounding_box => {+ length * target.object_bounding_box.width * self.scale+ }+ _ => length * self.scale,+ }+ }++ /// A vertical length of a primitive, in device pixels; see [`Self::horizontal_length`].+ fn vertical_length(&self, length: f32) -> f32 {+ match self.target {+ Some(target) if target.region.primitive_units_are_object_bounding_box => {+ length * target.object_bounding_box.height * self.scale+ }+ _ => length * self.scale,+ }+ }+/// Draws an feImage over `rect` for a graph that has no target to resolve subregions against.pub(crate) fn set_image_rect_without_target(&mut self, rect: FloatRect, image_rendering: u8) {self.image_rect_without_target = Some(rect);@@ -451,8 +474,8 @@ impl SvgFilterGraphBuilder {}}FfiSvgFilterPrimitiveKind::GaussianBlur => in_space(Filter::gaussian_blur(- values.std_deviation_x * self.scale,- values.std_deviation_y * self.scale,+ self.horizontal_length(values.std_deviation_x),+ self.vertical_length(values.std_deviation_y),self.resolve_input_in(&primitive.in1, space),)),FfiSvgFilterPrimitiveKind::ColorMatrix => {@@ -550,8 +573,8 @@ impl SvgFilterGraphBuilder {.collect(),}),FfiSvgFilterPrimitiveKind::Morphology => {- let radius_x = values.radius_x * self.scale;- let radius_y = values.radius_y * self.scale;+ let radius_x = self.horizontal_length(values.radius_x);+ let radius_y = self.vertical_length(values.radius_y);let input = boxed(self.resolve_input_in(&primitive.in1, space));match values.morphology_operator {MORPHOLOGY_OPERATOR_ERODE => in_space(Filter::Erode {@@ -573,8 +596,8 @@ impl SvgFilterGraphBuilder {let input = self.resolve_input(&primitive.in1);FilterResult::new(Filter::Offset {- dx: values.dx * self.scale,- dy: values.dy * self.scale,+ dx: self.horizontal_length(values.dx),+ dy: self.vertical_length(values.dy),input: boxed(input.filter),},input.color_space,@@ -593,8 +616,8 @@ impl SvgFilterGraphBuilder {input: boxed(input.clone()),};let blurred = Filter::gaussian_blur(- values.std_deviation_x * self.scale,- values.std_deviation_y * self.scale,+ self.horizontal_length(values.std_deviation_x),+ self.vertical_length(values.std_deviation_y),Some(alpha),);// 2. Offset the result of step 1 by dx and dy as specified on the feDropShadow element,@@ -602,8 +625,8 @@ impl SvgFilterGraphBuilder {//// <feOffset dx="dx-of-feDropShadow" dy="dy-of-feDropShadow" result="offsetblur"/>let offset_blur = Filter::Offset {- dx: values.dx * self.scale,- dy: values.dy * self.scale,+ dx: self.horizontal_length(values.dx),+ dy: self.vertical_length(values.dy),input: Some(Box::new(blurred)),};// 3. Do processing as if an feFlood element with flood-color and flood-opacity as@@ -675,7 +698,7 @@ impl SvgFilterGraphBuilder {displacement: boxed(displacement),// FIXME: Skia's displacement map takes a single scale factor, so the// horizontal scale applies to both axes.- scale: values.scale * self.scale,+ scale: self.horizontal_length(values.scale),x_channel_selector: channel_selector(values.x_channel_selector),y_channel_selector: channel_selector(values.y_channel_selector),},@@ -918,6 +941,24 @@ mod tests {);}+ #[test]+ fn bounding_box_primitive_units_scale_lengths_by_the_box() {+ let crop_of = |filter| Filter::Crop {+ rect: FloatRect::new(20.0, 40.0, 400.0, 200.0),+ input: Some(Box::new(filter)),+ };+ let source = Filter::Crop {+ rect: FloatRect::new(20.0, 40.0, 400.0, 200.0),+ input: None,+ };+ let mut blur = blur("SourceGraphic", 0.1, "");+ blur.values.std_deviation_y = 0.2;+ assert_eq!(+ targeted_graph(target(true), [blur]),+ Some(crop_of(Filter::gaussian_blur(20.0, 20.0, Some(source))))+ );+ }+#[test]fn an_empty_filter_produces_nothing() {assert_eq!(graph([], 1.0), None);diff --git a/Tests/LibWeb/Ref/expected/svg/filter-primitive-lengths-in-bounding-box-units-ref.html b/Tests/LibWeb/Ref/expected/svg/filter-primitive-lengths-in-bounding-box-units-ref.htmlnew file mode 100644index 00000000000..ca7b72a46e0--- /dev/null+++ b/Tests/LibWeb/Ref/expected/svg/filter-primitive-lengths-in-bounding-box-units-ref.html@@ -0,0 +1,6 @@+<!DOCTYPE html>+<style>+ body { margin: 0; }+ div { position: absolute; left: 20px; top: 5px; width: 100px; height: 50px; background: green; }+</style>+<div></div>diff --git a/Tests/LibWeb/Ref/input/svg/filter-primitive-lengths-in-bounding-box-units.html b/Tests/LibWeb/Ref/input/svg/filter-primitive-lengths-in-bounding-box-units.htmlnew file mode 100644index 00000000000..4c78c492565--- /dev/null+++ b/Tests/LibWeb/Ref/input/svg/filter-primitive-lengths-in-bounding-box-units.html@@ -0,0 +1,13 @@+<!DOCTYPE html>+<link rel="match" href="../../expected/svg/filter-primitive-lengths-in-bounding-box-units-ref.html" />+<style>+ body { margin: 0; }+ div { width: 100px; height: 50px; background: green; filter: url(#offset); }+</style>+<svg width="0" height="0" style="position: absolute">+ <filter id="offset" x="0" y="0" width="2" height="2" primitiveUnits="objectBoundingBox">+ <feOffset dx="0.2" dy="0.1" />+ </filter>+</svg>+<!-- Under objectBoundingBox, dx and dy are fractions of the box's width and height. -->+<div></div>