diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-02-11 17:28:24 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-11 21:38:27 +0100 |
commit | 116a1f485cc251bef8f61dec0e58556d74adb649 (patch) | |
tree | 925e74fa710e2084cc37256b209e56990b08b341 /Userland/Libraries/LibWeb/SVG | |
parent | 17912330c40a6e67869366c9d7ba604348fc860f (diff) | |
download | serenity-116a1f485cc251bef8f61dec0e58556d74adb649.zip |
LibWeb: Add SVG `<polyline>` element and test case :^)
Diffstat (limited to 'Userland/Libraries/LibWeb/SVG')
-rw-r--r-- | Userland/Libraries/LibWeb/SVG/AttributeNames.h | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/SVG/AttributeParser.cpp | 26 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/SVG/AttributeParser.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/SVG/SVGPolylineElement.cpp | 51 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/SVG/SVGPolylineElement.h | 30 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/SVG/SVGPolylineElement.idl | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/SVG/TagNames.h | 1 |
7 files changed, 116 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/SVG/AttributeNames.h b/Userland/Libraries/LibWeb/SVG/AttributeNames.h index 22d491d7aa..8da65cd1ef 100644 --- a/Userland/Libraries/LibWeb/SVG/AttributeNames.h +++ b/Userland/Libraries/LibWeb/SVG/AttributeNames.h @@ -45,6 +45,7 @@ namespace Web::SVG::AttributeNames { E(patternContentUnits) \ E(patternTransform) \ E(patternUnits) \ + E(points) \ E(pointsAtX) \ E(pointsAtY) \ E(pointsAtZ) \ diff --git a/Userland/Libraries/LibWeb/SVG/AttributeParser.cpp b/Userland/Libraries/LibWeb/SVG/AttributeParser.cpp index e6eb4c5d9d..41d0acecdb 100644 --- a/Userland/Libraries/LibWeb/SVG/AttributeParser.cpp +++ b/Userland/Libraries/LibWeb/SVG/AttributeParser.cpp @@ -64,6 +64,32 @@ Optional<float> AttributeParser::parse_positive_length(StringView input) return result; } +Vector<Gfx::FloatPoint> AttributeParser::parse_points(StringView input) +{ + AttributeParser parser { input }; + + parser.parse_whitespace(); + + // FIXME: "If an odd number of coordinates is provided, then the element is in error, with the same user agent behavior + // as occurs with an incorrectly specified βpathβ element. In such error cases the user agent will drop the last, + // odd coordinate and otherwise render the shape." + // The parser currently doesn't notice that there is a missing coordinate, so make it notice! + auto coordinate_pair_sequence = parser.parse_coordinate_pair_sequence(); + + parser.parse_whitespace(); + if (!parser.done()) + return {}; + + // FIXME: This is awkward. Can we return Gfx::FloatPoints from some of these parsing methods instead of Vector<float>? + Vector<Gfx::FloatPoint> points; + points.ensure_capacity(coordinate_pair_sequence.size()); + + for (auto const& pair : coordinate_pair_sequence) + points.empend(pair[0], pair[1]); + + return points; +} + void AttributeParser::parse_drawto() { if (match('M') || match('m')) { diff --git a/Userland/Libraries/LibWeb/SVG/AttributeParser.h b/Userland/Libraries/LibWeb/SVG/AttributeParser.h index 6564b6baa3..126ed3f942 100644 --- a/Userland/Libraries/LibWeb/SVG/AttributeParser.h +++ b/Userland/Libraries/LibWeb/SVG/AttributeParser.h @@ -9,6 +9,7 @@ #include <AK/String.h> #include <AK/Vector.h> +#include <LibGfx/Point.h> namespace Web::SVG { @@ -42,6 +43,7 @@ public: static Optional<float> parse_coordinate(StringView input); static Optional<float> parse_length(StringView input); static Optional<float> parse_positive_length(StringView input); + static Vector<Gfx::FloatPoint> parse_points(StringView input); private: void parse_drawto(); diff --git a/Userland/Libraries/LibWeb/SVG/SVGPolylineElement.cpp b/Userland/Libraries/LibWeb/SVG/SVGPolylineElement.cpp new file mode 100644 index 0000000000..a6a58dd2e3 --- /dev/null +++ b/Userland/Libraries/LibWeb/SVG/SVGPolylineElement.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "SVGPolylineElement.h" +#include <LibWeb/SVG/AttributeNames.h> +#include <LibWeb/SVG/AttributeParser.h> + +namespace Web::SVG { + +SVGPolylineElement::SVGPolylineElement(DOM::Document& document, QualifiedName qualified_name) + : SVGGeometryElement(document, qualified_name) +{ +} + +void SVGPolylineElement::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& SVGPolylineElement::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]); + + m_path = move(path); + return m_path.value(); +} + +} diff --git a/Userland/Libraries/LibWeb/SVG/SVGPolylineElement.h b/Userland/Libraries/LibWeb/SVG/SVGPolylineElement.h new file mode 100644 index 0000000000..cfb5c4df81 --- /dev/null +++ b/Userland/Libraries/LibWeb/SVG/SVGPolylineElement.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 SVGPolylineElement final : public SVGGeometryElement { +public: + using WrapperType = Bindings::SVGPolylineElementWrapper; + + SVGPolylineElement(DOM::Document&, QualifiedName); + virtual ~SVGPolylineElement() 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/SVGPolylineElement.idl b/Userland/Libraries/LibWeb/SVG/SVGPolylineElement.idl new file mode 100644 index 0000000000..d0f10a6585 --- /dev/null +++ b/Userland/Libraries/LibWeb/SVG/SVGPolylineElement.idl @@ -0,0 +1,5 @@ +[Exposed=Window] +interface SVGPolylineElement : SVGGeometryElement { +}; + +// SVGPolylineElement includes SVGAnimatedPoints; diff --git a/Userland/Libraries/LibWeb/SVG/TagNames.h b/Userland/Libraries/LibWeb/SVG/TagNames.h index 9bd914f748..3d1f3ad9ed 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(polyline) \ __ENUMERATE_SVG_TAG(rect) \ __ENUMERATE_SVG_TAG(svg) |