LibWeb: Implement SVGStopElement (<stop>)

This is used to specify the color/position of color stops for SVG
gradients.
This commit is contained in:
MacDue 2023-04-20 18:51:00 +01:00 committed by Andreas Kling
parent 297d8eebcd
commit b19d2634f6
Notes: sideshowbarker 2024-07-17 00:47:29 +09:00
7 changed files with 116 additions and 1 deletions

View file

@ -505,6 +505,7 @@ set(SOURCES
SVG/SVGPolylineElement.cpp
SVG/SVGRectElement.cpp
SVG/SVGSVGElement.cpp
SVG/SVGStopElement.cpp
SVG/SVGTextContentElement.cpp
SVG/TagNames.cpp
SVG/ViewBox.cpp

View file

@ -93,6 +93,7 @@
#include <LibWeb/SVG/SVGPolylineElement.h>
#include <LibWeb/SVG/SVGRectElement.h>
#include <LibWeb/SVG/SVGSVGElement.h>
#include <LibWeb/SVG/SVGStopElement.h>
#include <LibWeb/SVG/SVGTextContentElement.h>
#include <LibWeb/SVG/TagNames.h>
#include <LibWeb/WebIDL/AbstractOperations.h>
@ -441,6 +442,8 @@ static WebIDL::ExceptionOr<JS::GCPtr<SVG::SVGElement>> create_svg_element(JS::Re
return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGRectElement>(realm, document, move(qualified_name)));
if (local_name == SVG::TagNames::g)
return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGGElement>(realm, document, move(qualified_name)));
if (local_name == SVG::TagNames::stop)
return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGStopElement>(realm, document, move(qualified_name)));
if (local_name == SVG::TagNames::text)
return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGTextContentElement>(realm, document, move(qualified_name)));

View file

@ -42,6 +42,7 @@ namespace Web::SVG::AttributeNames {
E(maskContentUnits) \
E(maskUnits) \
E(numOctaves) \
E(offset) \
E(pathLength) \
E(patternContentUnits) \
E(patternTransform) \
@ -59,9 +60,9 @@ namespace Web::SVG::AttributeNames {
E(repeatCount) \
E(repeatDur) \
E(requiredExtensions) \
E(requiredFeatures) \
E(rx) \
E(ry) \
E(requiredFeatures) \
E(specularConstant) \
E(specularExponent) \
E(spreadMethod) \

View file

@ -0,0 +1,63 @@
/*
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
#include <LibWeb/Layout/BlockContainer.h>
#include <LibWeb/SVG/AttributeNames.h>
#include <LibWeb/SVG/AttributeParser.h>
#include <LibWeb/SVG/SVGStopElement.h>
namespace Web::SVG {
SVGStopElement::SVGStopElement(DOM::Document& document, DOM::QualifiedName qualified_name)
: SVGElement(document, qualified_name)
{
}
void SVGStopElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
{
SVGElement::parse_attribute(name, value);
if (name == SVG::AttributeNames::offset) {
m_offset = AttributeParser::parse_number_percentage(value);
}
}
void SVGStopElement::apply_presentational_hints(CSS::StyleProperties& style) const
{
CSS::Parser::ParsingContext parsing_context { document() };
for_each_attribute([&](auto& name, auto& value) {
if (name.equals_ignoring_ascii_case("stop-color"sv)) {
CSS::Parser::ParsingContext parsing_context { document() };
if (auto stop_color = parse_css_value(parsing_context, value, CSS::PropertyID::StopColor)) {
style.set_property(CSS::PropertyID::StopColor, stop_color.release_nonnull());
}
}
});
}
Gfx::Color SVGStopElement::stop_color() const
{
if (auto css_values = computed_css_values())
return css_values->stop_color();
return Color::Black;
}
JS::NonnullGCPtr<SVGAnimatedNumber> SVGStopElement::offset() const
{
TODO();
}
JS::ThrowCompletionOr<void> SVGStopElement::initialize(JS::Realm& realm)
{
MUST_OR_THROW_OOM(Base::initialize(realm));
set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGStopElementPrototype>(realm, "SVGStopElement"));
return {};
}
}

View file

@ -0,0 +1,39 @@
/*
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/SVG/AttributeParser.h>
#include <LibWeb/SVG/SVGAnimatedNumber.h>
#include <LibWeb/SVG/SVGElement.h>
namespace Web::SVG {
class SVGStopElement final : public SVGElement {
WEB_PLATFORM_OBJECT(SVGStopElement, SVGElement);
public:
virtual ~SVGStopElement() override = default;
virtual void parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value) override;
JS::NonnullGCPtr<SVGAnimatedNumber> offset() const;
virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
NumberPercentage stop_offset() const { return m_offset.value_or(NumberPercentage::create_number(0)); }
Gfx::Color stop_color() const;
private:
SVGStopElement(DOM::Document&, DOM::QualifiedName);
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
Optional<NumberPercentage> m_offset;
Optional<Gfx::Color> m_color;
};
}

View file

@ -0,0 +1,7 @@
#import <SVG/SVGElement.idl>
#import <SVG/SVGAnimatedNumber.idl>
[Exposed=Window]
interface SVGStopElement : SVGElement {
[SameObject] readonly attribute SVGAnimatedNumber offset;
};

View file

@ -207,6 +207,7 @@ libweb_js_bindings(SVG/SVGPolygonElement)
libweb_js_bindings(SVG/SVGPolylineElement)
libweb_js_bindings(SVG/SVGRectElement)
libweb_js_bindings(SVG/SVGSVGElement)
libweb_js_bindings(SVG/SVGStopElement)
libweb_js_bindings(SVG/SVGTextContentElement)
libweb_js_bindings(Selection/Selection)
libweb_js_bindings(UIEvents/FocusEvent)