Truly independent web browser
LibWeb: Keep an SVG image’s layout under a non-uniform scale
sideshowbarker67e67777978c
diff --git a/Libraries/LibWeb/Rust/src/painting/record/vector_images.rs b/Libraries/LibWeb/Rust/src/painting/record/vector_images.rsindex 80ddfcaf674..28597378286 100644--- a/Libraries/LibWeb/Rust/src/painting/record/vector_images.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/vector_images.rs@@ -135,9 +135,13 @@ pub(crate) fn vector_image_render_geometry(has_active_view_box: bool,) -> VectorImageRenderGeometry {if has_active_view_box {+ // The image is rendered at one scale in both axes, since a viewport of other proportions than those of its+ // destination can change how the image is laid out.+ let scale = positive_scale_or_one(accumulated_scale.width.max(accumulated_scale.height))+ .min(MAXIMUM_RASTER_DIMENSION as f32 / dest_rect.width.max(dest_rect.height));let raster_size = IntSize {- width: raster_dimension(dest_rect.width, accumulated_scale.width),- height: raster_dimension(dest_rect.height, accumulated_scale.height),+ width: raster_dimension(dest_rect.width, scale),+ height: raster_dimension(dest_rect.height, scale),};return VectorImageRenderGeometry {css_width: CssPixels::from_integer(i64::from(raster_size.width)),@@ -163,3 +167,33 @@ pub(crate) fn vector_image_render_geometry(},}}++#[cfg(test)]+mod tests {+ use super::*;++ fn list_size(width: f32, height: f32, scale_x: f32, scale_y: f32, has_active_view_box: bool) -> (i32, i32) {+ let geometry = vector_image_render_geometry(+ FloatRect::new(0.0, 0.0, width, height),+ FloatSize {+ width: scale_x,+ height: scale_y,+ },+ has_active_view_box,+ );+ (geometry.list_size.width, geometry.list_size.height)+ }++ #[test]+ fn vector_image_keeps_the_proportions_of_its_destination() {+ for has_active_view_box in [true, false] {+ assert_eq!(list_size(24.0, 12.0, 1.0, 1.0, has_active_view_box), (24, 12));+ assert_eq!(list_size(24.0, 12.0, 4.0, 4.0, has_active_view_box), (96, 48));+ assert_eq!(list_size(24.0, 12.0, 2.0, 3.0, has_active_view_box), (72, 36));+ assert_eq!(list_size(24.0, 12.0, 3.0, 0.5, has_active_view_box), (72, 36));+ assert_eq!(list_size(24.0, 12.0, 0.5, 0.25, has_active_view_box), (12, 6));+ assert_eq!(list_size(24.0, 12.0, f32::NAN, 2.0, has_active_view_box), (48, 24));+ assert_eq!(list_size(8192.0, 4096.0, 4.0, 4.0, has_active_view_box), (16384, 8192));+ }+ }+}diff --git a/Tests/LibWeb/Ref/expected/svg-image-view-box-under-non-uniform-scale-ref.html b/Tests/LibWeb/Ref/expected/svg-image-view-box-under-non-uniform-scale-ref.htmlnew file mode 100644index 00000000000..e0c7706cabc--- /dev/null+++ b/Tests/LibWeb/Ref/expected/svg-image-view-box-under-non-uniform-scale-ref.html@@ -0,0 +1,9 @@+<!DOCTYPE html>+<style>+body { margin: 0; }+img, div { position: absolute; top: 8px; width: 48px; height: 96px; }+img { left: 8px; }+div { left: 64px; background: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' preserveAspectRatio='none'><circle cx='12' cy='12' r='10' fill='none' stroke='blue' stroke-width='1'/></svg>") no-repeat; background-size: 48px 96px; }+</style>+<img src="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' preserveAspectRatio='none'><circle cx='12' cy='12' r='10' fill='none' stroke='blue' stroke-width='1'/></svg>">+<div></div>diff --git a/Tests/LibWeb/Ref/input/svg-image-view-box-under-non-uniform-scale.html b/Tests/LibWeb/Ref/input/svg-image-view-box-under-non-uniform-scale.htmlnew file mode 100644index 00000000000..ae0ac99bf11--- /dev/null+++ b/Tests/LibWeb/Ref/input/svg-image-view-box-under-non-uniform-scale.html@@ -0,0 +1,10 @@+<!DOCTYPE html>+<link rel="match" href="../expected/svg-image-view-box-under-non-uniform-scale-ref.html">+<style>+body { margin: 0; }+img, div { position: absolute; top: 8px; width: 24px; height: 24px; transform: scale(2, 4); transform-origin: top left; }+img { left: 8px; }+div { left: 64px; background: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><circle cx='12' cy='12' r='10' fill='none' stroke='blue' stroke-width='1'/></svg>") no-repeat; }+</style>+<img src="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><circle cx='12' cy='12' r='10' fill='none' stroke='blue' stroke-width='1'/></svg>">+<div></div>