From c266284559c325d9399b1f21ebea2e22bcab18f7 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 29 Nov 2022 14:07:22 +0100 Subject: [PATCH] LibWeb: Support creating Path2D objects from SVG path strings This reuses the SVG path parsing code. --- Userland/Libraries/LibWeb/HTML/Path2D.cpp | 24 +++++++++++++++++------ 1 file 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 + * Copyright (c) 2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include +#include +#include namespace Web::HTML { @@ -33,13 +36,22 @@ Path2D::Path2D(JS::Realm& realm, Optional, 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()); + 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;