瀏覽代碼

LibWeb: Implement SVGStopElement (<stop>)

This is used to specify the color/position of color stops for SVG
gradients.
MacDue 2 年之前
父節點
當前提交
b19d2634f6

+ 1 - 0
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -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

+ 3 - 0
Userland/Libraries/LibWeb/DOM/ElementFactory.cpp

@@ -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)));
 

+ 2 - 1
Userland/Libraries/LibWeb/SVG/AttributeNames.h

@@ -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)                 \

+ 63 - 0
Userland/Libraries/LibWeb/SVG/SVGStopElement.cpp

@@ -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 {};
+}
+
+}

+ 39 - 0
Userland/Libraries/LibWeb/SVG/SVGStopElement.h

@@ -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;
+};
+
+}

+ 7 - 0
Userland/Libraries/LibWeb/SVG/SVGStopElement.idl

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

+ 1 - 0
Userland/Libraries/LibWeb/idl_files.cmake

@@ -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)