1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:17:46 +00:00

LibWeb: Add initial support for nesting SVG viewports

Previously, we were handling viewBoxes/viewports in a slightly hacky
way, asking graphics elements to figure out what viewBox to use during
layout. This does not work in all cases, and can't allow for more
complex SVGs where it is possible to have nested viewports.

This commit makes the SVGFormattingContext keep track of the
viewport/boxes, and it now lays out each viewport recursively, where
each nested `<svg>` or `<symbol>` can establish a new viewport.

This fixes some previous edge cases, and starts to allow nested
viewports (there's still some issues to resolve there).

Fixes #22931
This commit is contained in:
MacDue 2024-01-27 16:23:22 +00:00 committed by Andreas Kling
parent 546143e9a6
commit 5cf1570f40
13 changed files with 229 additions and 67 deletions

View file

@ -232,19 +232,4 @@ Optional<float> SVGGraphicsElement::stroke_width() const
return width.to_px(*layout_node(), scaled_viewport_size).to_double();
}
Optional<ViewBox> SVGGraphicsElement::view_box() const
{
if (auto* svg_svg_element = shadow_including_first_ancestor_of_type<SVGSVGElement>()) {
if (svg_svg_element->view_box().has_value())
return svg_svg_element->view_box();
}
if (auto* svg_symbol_element = shadow_including_first_ancestor_of_type<SVGSymbolElement>()) {
if (svg_symbol_element->view_box().has_value())
return svg_symbol_element->view_box();
}
return {};
}
}

View file

@ -47,8 +47,6 @@ public:
JS::GCPtr<SVG::SVGMaskElement const> mask() const;
Optional<ViewBox> view_box() const;
protected:
SVGGraphicsElement(DOM::Document&, DOM::QualifiedName);

View file

@ -9,11 +9,13 @@
#include <LibGfx/Bitmap.h>
#include <LibWeb/SVG/AttributeParser.h>
#include <LibWeb/SVG/SVGGraphicsElement.h>
#include <LibWeb/SVG/SVGViewport.h>
#include <LibWeb/SVG/ViewBox.h>
namespace Web::SVG {
class SVGSVGElement final : public SVGGraphicsElement {
class SVGSVGElement final : public SVGGraphicsElement
, public SVGViewport {
WEB_PLATFORM_OBJECT(SVGSVGElement, SVGGraphicsElement);
JS_DECLARE_ALLOCATOR(SVGSVGElement);
@ -25,8 +27,8 @@ public:
virtual bool requires_svg_container() const override { return false; }
virtual bool is_svg_container() const override { return true; }
[[nodiscard]] Optional<ViewBox> view_box() const;
Optional<PreserveAspectRatio> const& preserve_aspect_ratio() const { return m_preserve_aspect_ratio; }
virtual Optional<ViewBox> view_box() const override;
virtual Optional<PreserveAspectRatio> preserve_aspect_ratio() const override { return m_preserve_aspect_ratio; }
void set_fallback_view_box_for_svg_as_image(Optional<ViewBox>);

View file

@ -7,10 +7,12 @@
#pragma once
#include <LibWeb/SVG/SVGGraphicsElement.h>
#include <LibWeb/SVG/SVGViewport.h>
namespace Web::SVG {
class SVGSymbolElement final : public SVGGraphicsElement {
class SVGSymbolElement final : public SVGGraphicsElement
, public SVGViewport {
WEB_PLATFORM_OBJECT(SVGSymbolElement, SVGGraphicsElement);
JS_DECLARE_ALLOCATOR(SVGSymbolElement);
@ -19,7 +21,12 @@ public:
void apply_presentational_hints(CSS::StyleProperties& style) const override;
Optional<ViewBox> view_box() const { return m_view_box; }
virtual Optional<ViewBox> view_box() const override { return m_view_box; }
virtual Optional<PreserveAspectRatio> preserve_aspect_ratio() const override
{
// FIXME: Support the `preserveAspectRatio` attribute on <symbol>.
return {};
}
private:
SVGSymbolElement(DOM::Document&, DOM::QualifiedName);

View file

@ -0,0 +1,21 @@
/*
* Copyright (c) 2024, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/SVG/AttributeParser.h>
#include <LibWeb/SVG/ViewBox.h>
namespace Web::SVG {
class SVGViewport {
public:
virtual Optional<ViewBox> view_box() const = 0;
virtual Optional<PreserveAspectRatio> preserve_aspect_ratio() const = 0;
virtual ~SVGViewport() = default;
};
}