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

LibWeb: Flesh out SVGMaskElement a bit

- Add method to resolve the masking area (based on the target element
  size)
- Parse the maskUnits and maskContentUnits attributes
This commit is contained in:
MacDue 2023-09-10 14:00:47 +01:00 committed by Andreas Kling
parent 0af8d81f48
commit 650180811e
5 changed files with 55 additions and 6 deletions

View file

@ -497,15 +497,17 @@ Optional<PreserveAspectRatio> AttributeParser::parse_preserve_aspect_ratio(Strin
}
// https://svgwg.org/svg2-draft/pservers.html#LinearGradientElementGradientUnitsAttribute
Optional<GradientUnits> AttributeParser::parse_gradient_units(StringView input)
// https://drafts.fxtf.org/css-masking/#element-attrdef-mask-maskunits
// https://drafts.fxtf.org/css-masking/#element-attrdef-mask-maskcontentunits
Optional<SVGUnits> AttributeParser::parse_units(StringView input)
{
GenericLexer lexer { input };
lexer.ignore_while(whitespace);
auto gradient_units_string = lexer.consume_until(whitespace);
if (gradient_units_string == "userSpaceOnUse"sv)
return GradientUnits::UserSpaceOnUse;
return SVGUnits::UserSpaceOnUse;
if (gradient_units_string == "objectBoundingBox"sv)
return GradientUnits::ObjectBoundingBox;
return SVGUnits::ObjectBoundingBox;
return {};
}

View file

@ -88,11 +88,15 @@ struct PreserveAspectRatio {
MeetOrSlice meet_or_slice { MeetOrSlice::Meet };
};
enum class GradientUnits {
enum class SVGUnits {
ObjectBoundingBox,
UserSpaceOnUse
};
using GradientUnits = SVGUnits;
using MaskUnits = SVGUnits;
using MaskContentUnits = SVGUnits;
enum class SpreadMethod {
Pad,
Repeat,
@ -149,7 +153,7 @@ public:
static Vector<PathInstruction> parse_path_data(StringView input);
static Optional<Vector<Transform>> parse_transform(StringView input);
static Optional<PreserveAspectRatio> parse_preserve_aspect_ratio(StringView input);
static Optional<GradientUnits> parse_gradient_units(StringView input);
static Optional<SVGUnits> parse_units(StringView input);
static Optional<SpreadMethod> parse_spread_method(StringView input);
private:

View file

@ -21,7 +21,7 @@ void SVGGradientElement::attribute_changed(DeprecatedFlyString const& name, Depr
{
SVGElement::attribute_changed(name, value);
if (name == AttributeNames::gradientUnits) {
m_gradient_units = AttributeParser::parse_gradient_units(value);
m_gradient_units = AttributeParser::parse_units(value);
} else if (name == AttributeNames::spreadMethod) {
m_spread_method = AttributeParser::parse_spread_method(value);
} else if (name == AttributeNames::gradientTransform) {

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -7,6 +8,7 @@
#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>
namespace Web::SVG {
@ -29,4 +31,34 @@ JS::GCPtr<Layout::Node> SVGMaskElement::create_layout_node(NonnullRefPtr<CSS::St
return heap().allocate_without_realm<Layout::SVGGraphicsBox>(document(), *this, move(style));
}
void SVGMaskElement::attribute_changed(DeprecatedFlyString const& name, DeprecatedString const& value)
{
SVGGraphicsElement::attribute_changed(name, value);
if (name == AttributeNames::maskUnits) {
m_mask_units = AttributeParser::parse_units(value);
} else if (name == AttributeNames::maskContentUnits) {
m_mask_content_units = AttributeParser::parse_units(value);
}
}
MaskContentUnits SVGMaskElement::mask_content_units() const
{
return m_mask_content_units.value_or(MaskContentUnits::UserSpaceOnUse);
}
MaskUnits SVGMaskElement::mask_units() const
{
return m_mask_units.value_or(MaskUnits::ObjectBoundingBox);
}
CSSPixelRect SVGMaskElement::resolve_masking_area(CSSPixelRect const& mask_target) const
{
if (mask_units() == SVG::MaskUnits::UserSpaceOnUse) {
dbgln("SVG: maskUnits=userSpaceOnUse is not supported");
return {};
}
// TODO: Resolve this based on the x, y, width, and height of the mask.
return mask_target.inflated(mask_target.size().scaled(CSSPixels(2) / 10));
}
}

View file

@ -6,6 +6,7 @@
#pragma once
#include <LibWeb/SVG/AttributeParser.h>
#include <LibWeb/SVG/SVGGraphicsElement.h>
namespace Web::SVG {
@ -16,11 +17,21 @@ class SVGMaskElement final : public SVGGraphicsElement {
public:
virtual ~SVGMaskElement() override;
virtual void attribute_changed(DeprecatedFlyString const& name, DeprecatedString const& value) override;
virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
CSSPixelRect resolve_masking_area(CSSPixelRect const& mask_target) const;
MaskContentUnits mask_content_units() const;
MaskUnits mask_units() const;
private:
SVGMaskElement(DOM::Document&, DOM::QualifiedName);
virtual void initialize(JS::Realm&) override;
Optional<MaskContentUnits> m_mask_content_units = {};
Optional<MaskUnits> m_mask_units = {};
};
}