diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-02-11 17:36:05 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-11 21:38:27 +0100 |
commit | 2fad940b0b6f5bfe6eb8ec626fad0e17f2ca754d (patch) | |
tree | d10d548a4e894947f25a6293096d0e008308e90f /Userland/Libraries/LibWeb/SVG | |
parent | 116a1f485cc251bef8f61dec0e58556d74adb649 (diff) | |
download | serenity-2fad940b0b6f5bfe6eb8ec626fad0e17f2ca754d.zip |
LibWeb: Add SVG `<polygon>` element and test case :^)
Diffstat (limited to 'Userland/Libraries/LibWeb/SVG')
-rw-r--r-- | Userland/Libraries/LibWeb/SVG/SVGPolygonElement.cpp | 54 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/SVG/SVGPolygonElement.h | 30 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/SVG/SVGPolygonElement.idl | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/SVG/TagNames.h | 1 |
4 files changed, 90 insertions, 0 deletions
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 <atkinssj@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "SVGPolygonElement.h" +#include <LibWeb/SVG/AttributeNames.h> +#include <LibWeb/SVG/AttributeParser.h> + +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 <atkinssj@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <LibWeb/SVG/SVGGeometryElement.h> + +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<Gfx::Path> m_path; + + Vector<Gfx::FloatPoint> 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) |