From 2fad940b0b6f5bfe6eb8ec626fad0e17f2ca754d Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 11 Feb 2022 17:36:05 +0000 Subject: [PATCH] LibWeb: Add SVG `` element and test case :^) --- Base/res/html/misc/svg.html | 11 +++- .../LibWeb/Bindings/NodeWrapperFactory.cpp | 4 ++ .../LibWeb/Bindings/WindowObjectHelper.h | 3 ++ Userland/Libraries/LibWeb/CMakeLists.txt | 2 + .../Libraries/LibWeb/DOM/ElementFactory.cpp | 3 ++ Userland/Libraries/LibWeb/Forward.h | 2 + .../LibWeb/SVG/SVGPolygonElement.cpp | 54 +++++++++++++++++++ .../Libraries/LibWeb/SVG/SVGPolygonElement.h | 30 +++++++++++ .../LibWeb/SVG/SVGPolygonElement.idl | 5 ++ Userland/Libraries/LibWeb/SVG/TagNames.h | 1 + 10 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 Userland/Libraries/LibWeb/SVG/SVGPolygonElement.cpp create mode 100644 Userland/Libraries/LibWeb/SVG/SVGPolygonElement.h create mode 100644 Userland/Libraries/LibWeb/SVG/SVGPolygonElement.idl diff --git a/Base/res/html/misc/svg.html b/Base/res/html/misc/svg.html index a6c14b8c31..5488b8b178 100644 --- a/Base/res/html/misc/svg.html +++ b/Base/res/html/misc/svg.html @@ -11,7 +11,7 @@ - + @@ -75,6 +75,15 @@ 375,850 375,750 425,750 425,850 475,850 475,725 525,725 525,850 575,850" /> + + + + diff --git a/Userland/Libraries/LibWeb/Bindings/NodeWrapperFactory.cpp b/Userland/Libraries/LibWeb/Bindings/NodeWrapperFactory.cpp index 293e48960b..77a3600956 100644 --- a/Userland/Libraries/LibWeb/Bindings/NodeWrapperFactory.cpp +++ b/Userland/Libraries/LibWeb/Bindings/NodeWrapperFactory.cpp @@ -86,6 +86,7 @@ #include #include #include +#include #include #include #include @@ -165,6 +166,7 @@ #include #include #include +#include #include #include #include @@ -325,6 +327,8 @@ NodeWrapper* wrap(JS::GlobalObject& global_object, DOM::Node& node) return static_cast(wrap_impl(global_object, verify_cast(node))); if (is(node)) return static_cast(wrap_impl(global_object, verify_cast(node))); + if (is(node)) + return static_cast(wrap_impl(global_object, verify_cast(node))); if (is(node)) return static_cast(wrap_impl(global_object, verify_cast(node))); if (is(node)) diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h b/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h index bd6dd77ccd..e1f1798084 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h +++ b/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h @@ -261,6 +261,8 @@ #include #include #include +#include +#include #include #include #include @@ -448,6 +450,7 @@ ADD_WINDOW_OBJECT_INTERFACE(SVGGraphicsElement) \ ADD_WINDOW_OBJECT_INTERFACE(SVGLineElement) \ ADD_WINDOW_OBJECT_INTERFACE(SVGPathElement) \ + ADD_WINDOW_OBJECT_INTERFACE(SVGPolygonElement) \ ADD_WINDOW_OBJECT_INTERFACE(SVGPolylineElement) \ ADD_WINDOW_OBJECT_INTERFACE(SVGRectElement) \ ADD_WINDOW_OBJECT_INTERFACE(SVGSVGElement) \ diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 84c5d482f1..4fa30b1b47 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -274,6 +274,7 @@ set(SOURCES SVG/SVGCircleElement.cpp SVG/SVGEllipseElement.cpp SVG/SVGLineElement.cpp + SVG/SVGPolygonElement.cpp SVG/SVGPolylineElement.cpp SVG/SVGRectElement.cpp SVG/SVGSVGElement.cpp @@ -521,6 +522,7 @@ libweb_js_wrapper(SVG/SVGCircleElement) libweb_js_wrapper(SVG/SVGEllipseElement) libweb_js_wrapper(SVG/SVGLineElement) libweb_js_wrapper(SVG/SVGPathElement) +libweb_js_wrapper(SVG/SVGPolygonElement) libweb_js_wrapper(SVG/SVGPolylineElement) libweb_js_wrapper(SVG/SVGRectElement) libweb_js_wrapper(SVG/SVGSVGElement) diff --git a/Userland/Libraries/LibWeb/DOM/ElementFactory.cpp b/Userland/Libraries/LibWeb/DOM/ElementFactory.cpp index bfca713f4f..bd74ecd862 100644 --- a/Userland/Libraries/LibWeb/DOM/ElementFactory.cpp +++ b/Userland/Libraries/LibWeb/DOM/ElementFactory.cpp @@ -81,6 +81,7 @@ #include #include #include +#include #include #include #include @@ -247,6 +248,8 @@ NonnullRefPtr create_element(Document& document, const FlyString& tag_n return adopt_ref(*new SVG::SVGLineElement(document, move(qualified_name))); if (lowercase_tag_name == SVG::TagNames::path) return adopt_ref(*new SVG::SVGPathElement(document, move(qualified_name))); + if (lowercase_tag_name == SVG::TagNames::polygon) + return adopt_ref(*new SVG::SVGPolygonElement(document, move(qualified_name))); if (lowercase_tag_name == SVG::TagNames::polyline) return adopt_ref(*new SVG::SVGPolylineElement(document, move(qualified_name))); if (lowercase_tag_name == SVG::TagNames::rect) diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 03d09b15c0..f2d019fd31 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -251,6 +251,7 @@ class SVGGeometryElement; class SVGGraphicsElement; class SVGLineElement; class SVGPathElement; +class SVGPolygonElement; class SVGPolylineElement; class SVGRectElement; class SVGSVGElement; @@ -461,6 +462,7 @@ class SVGGeometryElementWrapper; class SVGGraphicsElementWrapper; class SVGLineElementWrapper; class SVGPathElementWrapper; +class SVGPolygonElementWrapper; class SVGPolylineElementWrapper; class SVGRectElementWrapper; class SVGSVGElementWrapper; diff --git a/Userland/Libraries/LibWeb/SVG/SVGPolygonElement.cpp b/Userland/Libraries/LibWeb/SVG/SVGPolygonElement.cpp new file mode 100644 index 0000000000..86d85bc260 --- /dev/null +++ b/Userland/Libraries/LibWeb/SVG/SVGPolygonElement.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2022, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "SVGPolygonElement.h" +#include +#include + +namespace Web::SVG { + +SVGPolygonElement::SVGPolygonElement(DOM::Document& document, QualifiedName qualified_name) + : SVGGeometryElement(document, qualified_name) +{ +} + +void SVGPolygonElement::parse_attribute(FlyString const& name, String const& value) +{ + SVGGeometryElement::parse_attribute(name, value); + + if (name == SVG::AttributeNames::points) { + m_points = AttributeParser::parse_points(value); + m_path.clear(); + } +} + +Gfx::Path& SVGPolygonElement::get_path() +{ + if (m_path.has_value()) + return m_path.value(); + + Gfx::Path path; + + if (m_points.is_empty()) { + m_path = move(path); + return m_path.value(); + } + + // 1. perform an absolute moveto operation to the first coordinate pair in the list of points + path.move_to(m_points.first()); + + // 2. for each subsequent coordinate pair, perform an absolute lineto operation to that coordinate pair. + for (size_t point_index = 1; point_index < m_points.size(); ++point_index) + path.line_to(m_points[point_index]); + + // 3. perform a closepath command + path.close(); + + m_path = move(path); + return m_path.value(); +} + +} diff --git a/Userland/Libraries/LibWeb/SVG/SVGPolygonElement.h b/Userland/Libraries/LibWeb/SVG/SVGPolygonElement.h new file mode 100644 index 0000000000..8e3bc1bb16 --- /dev/null +++ b/Userland/Libraries/LibWeb/SVG/SVGPolygonElement.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2022, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Web::SVG { + +class SVGPolygonElement final : public SVGGeometryElement { +public: + using WrapperType = Bindings::SVGPolygonElementWrapper; + + SVGPolygonElement(DOM::Document&, QualifiedName); + virtual ~SVGPolygonElement() override = default; + + virtual void parse_attribute(FlyString const& name, String const& value) override; + + virtual Gfx::Path& get_path() override; + +private: + Optional m_path; + + Vector m_points; +}; + +} diff --git a/Userland/Libraries/LibWeb/SVG/SVGPolygonElement.idl b/Userland/Libraries/LibWeb/SVG/SVGPolygonElement.idl new file mode 100644 index 0000000000..58f7a16af4 --- /dev/null +++ b/Userland/Libraries/LibWeb/SVG/SVGPolygonElement.idl @@ -0,0 +1,5 @@ +[Exposed=Window] +interface SVGPolygonElement : SVGGeometryElement { +}; + +// SVGPolygonElement includes SVGAnimatedPoints; diff --git a/Userland/Libraries/LibWeb/SVG/TagNames.h b/Userland/Libraries/LibWeb/SVG/TagNames.h index 3d1f3ad9ed..d1b4150a73 100644 --- a/Userland/Libraries/LibWeb/SVG/TagNames.h +++ b/Userland/Libraries/LibWeb/SVG/TagNames.h @@ -16,6 +16,7 @@ namespace Web::SVG::TagNames { __ENUMERATE_SVG_TAG(g) \ __ENUMERATE_SVG_TAG(line) \ __ENUMERATE_SVG_TAG(path) \ + __ENUMERATE_SVG_TAG(polygon) \ __ENUMERATE_SVG_TAG(polyline) \ __ENUMERATE_SVG_TAG(rect) \ __ENUMERATE_SVG_TAG(svg)