Skip to content

Truly independent web browser

LibWeb: Fit an SVG image as its preserveAspectRatio says

sideshowbarker39f6564d4605
diff --git a/Libraries/LibWeb/Layout/ImageProvider.h b/Libraries/LibWeb/Layout/ImageProvider.h
index ce472245dd1..1f0bb84ddba 100644
--- a/Libraries/LibWeb/Layout/ImageProvider.h
+++ b/Libraries/LibWeb/Layout/ImageProvider.h
@@ -11,6 +11,7 @@
#include <LibGfx/DecodedImageFrame.h>
#include <LibGfx/Size.h>
#include <LibWeb/Forward.h>
+#include <LibWeb/SVG/AttributeParsing.h>
#include <LibWebCommon/PixelUnits.h>
namespace Web::Layout {
@@ -39,6 +40,9 @@ public:
Optional<CSSPixelSize> intrinsic_size() const;
virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const;
+ // How the image is fitted into the rectangle it's drawn in, which only an SVG image element lets authors change.
+ virtual SVG::PreserveAspectRatio preserve_aspect_ratio() const { return {}; }
+
Optional<Gfx::DecodedImageFrame> current_image_frame(Optional<Gfx::IntSize> size = {}) const;
Optional<Gfx::DecodedImageFrame> default_image_frame(Optional<Gfx::IntSize> size = {}) const;
diff --git a/Libraries/LibWeb/Painting/PaintFacts.cpp b/Libraries/LibWeb/Painting/PaintFacts.cpp
index b2ea7586cab..e5d5bbeeb70 100644
--- a/Libraries/LibWeb/Painting/PaintFacts.cpp
+++ b/Libraries/LibWeb/Painting/PaintFacts.cpp
@@ -215,9 +215,12 @@ template<typename Queue>
static void with_replaced_image_paint_facts(Layout::ImageProvider const& image_provider, Queue queue)
{
Optional<Gfx::DecodedImageFrame> current_frame;
+ auto preserve_aspect_ratio = image_provider.preserve_aspect_ratio();
Layout::RustFFI::FfiReplacedImagePaintFacts facts {
.natural = natural_size_facts(image_provider.intrinsic_width(), image_provider.intrinsic_height(), image_provider.intrinsic_aspect_ratio()),
.content = image_content_facts(image_provider.decoded_image_data(), current_frame),
+ .preserve_aspect_ratio_align = static_cast<u8>(to_underlying(preserve_aspect_ratio.align)),
+ .preserve_aspect_ratio_meet_or_slice = static_cast<u8>(to_underlying(preserve_aspect_ratio.meet_or_slice)),
};
queue(facts);
}
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index c80ecb6804f..e8d74789c73 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -284,7 +284,8 @@ impl FfiSvgNumberPercentage {
}
pub(crate) const PRESERVE_ASPECT_RATIO_NONE: u8 = 0;
-const PRESERVE_ASPECT_RATIO_X_MIN_Y_MIN: u8 = 1;
+pub(crate) const PRESERVE_ASPECT_RATIO_X_MIN_Y_MIN: u8 = 1;
+pub(crate) const PRESERVE_ASPECT_RATIO_X_MID_Y_MID: u8 = 5;
pub(crate) const PRESERVE_ASPECT_RATIO_X_MAX_Y_MAX: u8 = 9;
pub(crate) const MEET_OR_SLICE_MEET: u8 = 0;
pub(crate) const MEET_OR_SLICE_SLICE: u8 = 1;
diff --git a/Libraries/LibWeb/Rust/src/painting/host/paint.rs b/Libraries/LibWeb/Rust/src/painting/host/paint.rs
index e17085b7790..266de70a356 100644
--- a/Libraries/LibWeb/Rust/src/painting/host/paint.rs
+++ b/Libraries/LibWeb/Rust/src/painting/host/paint.rs
@@ -370,6 +370,8 @@ pub enum FfiVideoRepresentation {
pub struct FfiReplacedImagePaintFacts {
pub natural: FfiNaturalSize,
pub content: FfiImageContent,
+ pub preserve_aspect_ratio_align: u8,
+ pub preserve_aspect_ratio_meet_or_slice: u8,
}
#[derive(Clone, Copy, Debug)]
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
index 537213b67f9..1a1cb4e85dc 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
@@ -625,6 +625,43 @@ pub(crate) fn svg_image_unquantized_device_rect(
)
}
+// https://svgwg.org/svg2-draft/coords.html#PreserveAspectRatioAttribute
+// Where an image of `natural_size` is drawn to fit `image_rect` as its preserveAspectRatio says, taking its own size as
+// its view box.
+fn image_draw_rect(image_rect: FloatRect, natural_size: (f32, f32), align: u8, meet_or_slice: u8) -> FloatRect {
+ use crate::layout::svg_formatting_context::{
+ MEET_OR_SLICE_SLICE, PRESERVE_ASPECT_RATIO_NONE, PRESERVE_ASPECT_RATIO_X_MIN_Y_MIN,
+ };
+ // NB: With "none", the image is scaled non-uniformly to fill the rectangle exactly.
+ if natural_size.0 <= 0.0 || natural_size.1 <= 0.0 || align == PRESERVE_ASPECT_RATIO_NONE {
+ return image_rect;
+ }
+ let scale_x = image_rect.width / natural_size.0;
+ let scale_y = image_rect.height / natural_size.1;
+ // "meet" shows all of the image within the rectangle; "slice" covers all of the rectangle with the image.
+ let scale = if meet_or_slice == MEET_OR_SLICE_SLICE {
+ scale_x.max(scale_y)
+ } else {
+ scale_x.min(scale_y)
+ };
+ let width = natural_size.0 * scale;
+ let height = natural_size.1 * scale;
+ // NB: The alignments count up from xMinYMin through min, mid and max along x, then through min, mid and max along
+ // y, each putting none, half or all of the space left over before the image.
+ let alignment = align.saturating_sub(PRESERVE_ASPECT_RATIO_X_MIN_Y_MIN);
+ let offset = |alignment_in_axis: u8, free_space: f32| match alignment_in_axis {
+ 0 => 0.0,
+ 1 => free_space / 2.0,
+ _ => free_space,
+ };
+ FloatRect::new(
+ image_rect.x + offset(alignment % 3, image_rect.width - width),
+ image_rect.y + offset(alignment / 3, image_rect.height - height),
+ width,
+ height,
+ )
+}
+
pub(crate) fn paint_image_element<O: Observer>(
recorder: &mut PaintRecorder<'_, O>,
paintable: NodeSlotId,
@@ -659,19 +696,12 @@ pub(crate) fn paint_image_element<O: Observer>(
(Some(width), Some(height)) => (width.to_float(), height.to_float()),
_ => (image_rect.width, image_rect.height),
};
- // FIXME: Respect the preserveAspectRatio attribute instead of assuming its default value.
- let mut draw_rect = image_rect;
- if natural_size.0 > 0.0 && natural_size.1 > 0.0 {
- let contain_scale = (image_rect.width / natural_size.0).min(image_rect.height / natural_size.1);
- let width = natural_size.0 * contain_scale;
- let height = natural_size.1 * contain_scale;
- draw_rect = FloatRect::new(
- image_rect.x + image_rect.width / 2.0 - width / 2.0,
- image_rect.y + image_rect.height / 2.0 - height / 2.0,
- width,
- height,
- );
- }
+ let draw_rect = image_draw_rect(
+ image_rect,
+ natural_size,
+ image.preserve_aspect_ratio_align,
+ image.preserve_aspect_ratio_meet_or_slice,
+ );
if draw_rect.is_empty() {
return;
}
diff --git a/Libraries/LibWeb/Rust/src/painting/replaced_paint_facts.rs b/Libraries/LibWeb/Rust/src/painting/replaced_paint_facts.rs
index 6ebd4fad490..34924f92ce7 100644
--- a/Libraries/LibWeb/Rust/src/painting/replaced_paint_facts.rs
+++ b/Libraries/LibWeb/Rust/src/painting/replaced_paint_facts.rs
@@ -54,10 +54,25 @@ impl VideoPaintFacts {
}
}
-#[derive(Clone, Debug, Default, PartialEq)]
+#[derive(Clone, Debug, PartialEq)]
pub(crate) struct ImagePaintFacts {
pub natural: SizeWithAspectRatio,
pub content: ImageContent,
+ /// How an SVG image element fits the image into its rectangle, in the encoding of its `preserveAspectRatio`
+ /// attribute facts.
+ pub preserve_aspect_ratio_align: u8,
+ pub preserve_aspect_ratio_meet_or_slice: u8,
+}
+
+impl Default for ImagePaintFacts {
+ fn default() -> Self {
+ Self {
+ natural: SizeWithAspectRatio::default(),
+ content: ImageContent::default(),
+ preserve_aspect_ratio_align: crate::layout::svg_formatting_context::PRESERVE_ASPECT_RATIO_X_MID_Y_MID,
+ preserve_aspect_ratio_meet_or_slice: crate::layout::svg_formatting_context::MEET_OR_SLICE_MEET,
+ }
+ }
}
impl ImagePaintFacts {
@@ -68,6 +83,8 @@ impl ImagePaintFacts {
Self {
natural: SizeWithAspectRatio::from_ffi(&facts.natural),
content: unsafe { ImageContent::from_ffi(&facts.content) },
+ preserve_aspect_ratio_align: facts.preserve_aspect_ratio_align,
+ preserve_aspect_ratio_meet_or_slice: facts.preserve_aspect_ratio_meet_or_slice,
}
}
}
diff --git a/Libraries/LibWeb/SVG/SVGImageElement.cpp b/Libraries/LibWeb/SVG/SVGImageElement.cpp
index d280a7d104b..b75221b880a 100644
--- a/Libraries/LibWeb/SVG/SVGImageElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGImageElement.cpp
@@ -91,6 +91,19 @@ void SVGImageElement::attribute_changed(Utf16FlyString const& name, Optional<Utf
process_the_url(href);
}
+
+ if (name == SVG::AttributeNames::preserveAspectRatio)
+ Painting::push_replaced_image_paint_facts(*this, *this);
+}
+
+// https://svgwg.org/svg2-draft/embedded.html#ImageElement
+SVG::PreserveAspectRatio SVGImageElement::preserve_aspect_ratio() const
+{
+ // An unparseable value is the initial value, xMidYMid meet.
+ auto value = get_attribute(SVG::AttributeNames::preserveAspectRatio);
+ if (!value.has_value())
+ return {};
+ return parse_preserve_aspect_ratio(*value).value_or({});
}
// https://www.w3.org/TR/SVG2/linking.html#processingURL
diff --git a/Libraries/LibWeb/SVG/SVGImageElement.h b/Libraries/LibWeb/SVG/SVGImageElement.h
index 871f9b0c2f9..973a1819542 100644
--- a/Libraries/LibWeb/SVG/SVGImageElement.h
+++ b/Libraries/LibWeb/SVG/SVGImageElement.h
@@ -48,6 +48,7 @@ public:
// ^Layout::ImageProvider
virtual GC::Ptr<HTML::DecodedImageData> decoded_image_data() const override;
+ virtual SVG::PreserveAspectRatio preserve_aspect_ratio() const override;
protected:
SVGImageElement(DOM::Document&, DOM::QualifiedName);
diff --git a/Tests/LibWeb/Ref/expected/svg-image-preserve-aspect-ratio-ref.html b/Tests/LibWeb/Ref/expected/svg-image-preserve-aspect-ratio-ref.html
new file mode 100644
index 00000000000..6aac6bed9b2
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/svg-image-preserve-aspect-ratio-ref.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<style>
+ body { margin: 0; }
+ svg { display: block; image-rendering: pixelated; }
+</style>
+<svg width="200" height="120"><rect width="90" height="50" fill="red"/><rect x="90" width="90" height="50" fill="lime"/><rect y="50" width="90" height="50" fill="blue"/><rect x="90" y="50" width="90" height="50" fill="black"/></svg>
+<svg width="200" height="120"><rect width="90" height="50" fill="red"/><rect x="90" width="90" height="50" fill="lime"/><rect y="50" width="90" height="50" fill="blue"/><rect x="90" y="50" width="90" height="50" fill="black"/></svg>
+<svg width="200" height="120"><rect width="50" height="50" fill="red"/><rect x="50" width="50" height="50" fill="lime"/><rect y="50" width="50" height="50" fill="blue"/><rect x="50" y="50" width="50" height="50" fill="black"/></svg>
+<svg width="200" height="120"><rect width="90" height="90" fill="red"/><rect x="90" width="90" height="90" fill="lime"/><rect y="90" width="90" height="10" fill="blue"/><rect x="90" y="90" width="90" height="10" fill="black"/></svg>
+<svg width="200" height="120"><rect width="90" height="50" fill="red"/><rect x="90" width="90" height="50" fill="lime"/><rect y="50" width="90" height="50" fill="blue"/><rect x="90" y="50" width="90" height="50" fill="black"/></svg>
diff --git a/Tests/LibWeb/Ref/input/svg-image-preserve-aspect-ratio.html b/Tests/LibWeb/Ref/input/svg-image-preserve-aspect-ratio.html
new file mode 100644
index 00000000000..98d0498cf99
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/svg-image-preserve-aspect-ratio.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/svg-image-preserve-aspect-ratio-ref.html">
+<style>
+ body { margin: 0; }
+ svg { display: block; image-rendering: pixelated; }
+</style>
+<svg width="200" height="120"><image width="180" height="100" preserveAspectRatio="none" href="data:image/webp;base64,UklGRioAAABXRUJQVlA4TB4AAAAvAUAAAB8gEEjaH3qN+RcQFPk/2vwHsqVgFyL6HwE="/></svg>
+<svg width="200" height="120"><defs><image id="i" width="180" height="100" preserveAspectRatio="none" href="data:image/webp;base64,UklGRioAAABXRUJQVlA4TB4AAAAvAUAAAB8gEEjaH3qN+RcQFPk/2vwHsqVgFyL6HwE="/></defs><use href="#i"/></svg>
+<svg width="200" height="120"><image width="180" height="100" preserveAspectRatio="xMinYMax meet" href="data:image/webp;base64,UklGRioAAABXRUJQVlA4TB4AAAAvAUAAAB8gEEjaH3qN+RcQFPk/2vwHsqVgFyL6HwE="/></svg>
+<svg width="200" height="120"><image width="180" height="100" preserveAspectRatio="xMaxYMin slice" href="data:image/webp;base64,UklGRioAAABXRUJQVlA4TB4AAAAvAUAAAB8gEEjaH3qN+RcQFPk/2vwHsqVgFyL6HwE="/></svg>
+<svg width="200" height="120"><image id="changed" width="180" height="100" href="data:image/webp;base64,UklGRioAAABXRUJQVlA4TB4AAAAvAUAAAB8gEEjaH3qN+RcQFPk/2vwHsqVgFyL6HwE="/></svg>
+<script>
+ document.getElementById("changed").setAttribute("preserveAspectRatio", "none");
+</script>