1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 21:57:34 +00:00

LibWeb: Special case SVG masks during layout

Rather than try to lay out masks normally, this updates the TreeBuilder
to create layout nodes for masks as a child of their user (i.e. the
masked element). This allows each use of a mask to be laid out
differently, which makes supporting `maskContentUnits=objectBoundingBox`
fairly easy.

The `SVGFormattingContext` is then updated to lay out masks last (as
their sizing may depend on their parent), and treats them like
viewports.

This is pretty ad-hoc, but the SVG specification does not give any
guidance on how to actually implement this.
This commit is contained in:
MacDue 2024-03-11 18:26:58 +00:00 committed by Andreas Kling
parent 15e3b0ebde
commit 163b6bb401
18 changed files with 232 additions and 76 deletions

View file

@ -27,10 +27,4 @@ void SVGDefsElement::initialize(JS::Realm& realm)
set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGDefsElementPrototype>(realm, "SVGDefsElement"_fly_string));
}
JS::GCPtr<Layout::Node> SVGDefsElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
{
// FIXME: We need this layout node so any <mask>s inside this element get layout computed.
return heap().allocate_without_realm<Layout::SVGBox>(document(), *this, move(style));
}
}

View file

@ -17,7 +17,10 @@ class SVGDefsElement final : public SVGGraphicsElement {
public:
virtual ~SVGDefsElement();
virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override
{
return nullptr;
}
private:
SVGDefsElement(DOM::Document&, DOM::QualifiedName);

View file

@ -7,7 +7,6 @@
#include <LibWeb/Bindings/SVGMaskElementPrototype.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Layout/SVGGraphicsBox.h>
#include <LibWeb/SVG/AttributeNames.h>
#include <LibWeb/SVG/SVGMaskElement.h>
@ -28,9 +27,10 @@ void SVGMaskElement::initialize(JS::Realm& realm)
set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGMaskElementPrototype>(realm, "SVGMaskElement"_fly_string));
}
JS::GCPtr<Layout::Node> SVGMaskElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
JS::GCPtr<Layout::Node> SVGMaskElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties>)
{
return heap().allocate_without_realm<Layout::SVGGraphicsBox>(document(), *this, move(style));
// Masks are handled as a special case in the TreeBuilder.
return nullptr;
}
void SVGMaskElement::attribute_changed(FlyString const& name, Optional<String> const& value)

View file

@ -8,16 +8,34 @@
#include <LibWeb/SVG/AttributeParser.h>
#include <LibWeb/SVG/SVGGraphicsElement.h>
#include <LibWeb/SVG/SVGViewport.h>
namespace Web::SVG {
class SVGMaskElement final : public SVGGraphicsElement {
class SVGMaskElement final : public SVGGraphicsElement
, public SVGViewport {
WEB_PLATFORM_OBJECT(SVGMaskElement, SVGGraphicsElement);
JS_DECLARE_ALLOCATOR(SVGMaskElement);
public:
virtual ~SVGMaskElement() override;
virtual Optional<ViewBox> view_box() const override
{
// maskContentUnits = objectBoundingBox acts like the mask is sized to the bounding box
// of the target element, with a viewBox of "0 0 1 1".
if (mask_content_units() == MaskContentUnits::ObjectBoundingBox)
return ViewBox { 0, 0, 1, 1 };
return {};
}
virtual Optional<PreserveAspectRatio> preserve_aspect_ratio() const override
{
// preserveAspectRatio = none (allow mask to be scaled in both x and y to match target size)
return PreserveAspectRatio { PreserveAspectRatio::Align::None, {} };
}
virtual void attribute_changed(FlyString const& name, Optional<String> const& value) override;
virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;

View file

@ -6,8 +6,7 @@
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
#include <LibWeb/Layout/BlockContainer.h>
#include <LibWeb/CSS/StyleProperties.h>
#include <LibWeb/SVG/AttributeNames.h>
#include <LibWeb/SVG/AttributeParser.h>
#include <LibWeb/SVG/SVGStopElement.h>