summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/HTML
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-11-29 14:07:22 +0100
committerAndreas Kling <kling@serenityos.org>2022-11-30 14:43:22 +0100
commitc266284559c325d9399b1f21ebea2e22bcab18f7 (patch)
tree52e76b855078d7259a8c9b74e4ec45b833a174b3 /Userland/Libraries/LibWeb/HTML
parent65d762130a65258357192f120effb4a5747697fc (diff)
downloadserenity-c266284559c325d9399b1f21ebea2e22bcab18f7.zip
LibWeb: Support creating Path2D objects from SVG path strings
This reuses the SVG path parsing code.
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML')
-rw-r--r--Userland/Libraries/LibWeb/HTML/Path2D.cpp24
1 files changed, 18 insertions, 6 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/Path2D.cpp b/Userland/Libraries/LibWeb/HTML/Path2D.cpp
index c569e899f3..049ccf5458 100644
--- a/Userland/Libraries/LibWeb/HTML/Path2D.cpp
+++ b/Userland/Libraries/LibWeb/HTML/Path2D.cpp
@@ -1,11 +1,14 @@
/*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
+ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/HTML/Path2D.h>
+#include <LibWeb/SVG/AttributeParser.h>
+#include <LibWeb/SVG/SVGPathElement.h>
namespace Web::HTML {
@@ -33,13 +36,22 @@ Path2D::Path2D(JS::Realm& realm, Optional<Variant<JS::Handle<Path2D>, String>> c
return;
}
- dbgln("TODO: Implement constructing Path2D object with an SVG path string");
+ // 4. Let svgPath be the result of parsing and interpreting path according to SVG 2's rules for path data. [SVG]
+ auto path_instructions = SVG::AttributeParser::parse_path_data(path->get<String>());
+ auto svg_path = SVG::path_from_path_instructions(path_instructions);
- // FIXME: 4. Let svgPath be the result of parsing and interpreting path according to SVG 2's rules for path data. [SVG]
- // FIXME: 5. Let (x, y) be the last point in svgPath.
- // FIXME: 6. Add all the subpaths, if any, from svgPath to output.
- // FIXME: 7. Create a new subpath in output with (x, y) as the only point in the subpath.
- // FIXME: 8. Return output.
+ if (!svg_path.segments().is_empty()) {
+ // 5. Let (x, y) be the last point in svgPath.
+ auto xy = svg_path.segments().last().point();
+
+ // 6. Add all the subpaths, if any, from svgPath to output.
+ this->path() = move(svg_path);
+
+ // 7. Create a new subpath in output with (x, y) as the only point in the subpath.
+ this->move_to(xy.x(), xy.y());
+ }
+
+ // 8. Return output.
}
Path2D::~Path2D() = default;