summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorLuke Wilde <lukew@serenityos.org>2023-02-27 18:23:31 +0000
committerAndreas Kling <kling@serenityos.org>2023-02-27 20:55:09 +0100
commita964ebc25540ffbe1540a0e7a179c556d4119be5 (patch)
treec3f7eb84590758048cd95df12e0b8e9f33c72ad6 /Userland/Libraries
parent1f97adbee80b56c13116263204b37f0b71fc8f60 (diff)
downloadserenity-a964ebc25540ffbe1540a0e7a179c556d4119be5.zip
LibWeb: Implement Path2D#addPath
Required by Ruffle.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibWeb/HTML/Path2D.cpp35
-rw-r--r--Userland/Libraries/LibWeb/HTML/Path2D.h3
-rw-r--r--Userland/Libraries/LibWeb/HTML/Path2D.idl3
3 files changed, 40 insertions, 1 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/Path2D.cpp b/Userland/Libraries/LibWeb/HTML/Path2D.cpp
index 99ec7653e8..aa0708c939 100644
--- a/Userland/Libraries/LibWeb/HTML/Path2D.cpp
+++ b/Userland/Libraries/LibWeb/HTML/Path2D.cpp
@@ -1,11 +1,13 @@
/*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/Intrinsics.h>
+#include <LibWeb/Geometry/DOMMatrix.h>
#include <LibWeb/HTML/Path2D.h>
#include <LibWeb/SVG/AttributeParser.h>
#include <LibWeb/SVG/SVGPathElement.h>
@@ -62,4 +64,37 @@ JS::ThrowCompletionOr<void> Path2D::initialize(JS::Realm& realm)
return {};
}
+// https://html.spec.whatwg.org/multipage/canvas.html#dom-path2d-addpath
+WebIDL::ExceptionOr<void> Path2D::add_path(JS::NonnullGCPtr<Path2D> path, Geometry::DOMMatrix2DInit& transform)
+{
+ // The addPath(path, transform) method, when invoked on a Path2D object a, must run these steps:
+
+ // 1. If the Path2D object path has no subpaths, then return.
+ if (path->path().segments().is_empty())
+ return {};
+
+ // 2. Let matrix be the result of creating a DOMMatrix from the 2D dictionary transform.
+ auto matrix = TRY(Geometry::DOMMatrix::create_from_dom_matrix_2d_init(realm(), transform));
+
+ // 3. If one or more of matrix's m11 element, m12 element, m21 element, m22 element, m41 element, or m42 element are infinite or NaN, then return.
+ if (!isfinite(matrix->m11()) || !isfinite(matrix->m12()) || !isfinite(matrix->m21()) || !isfinite(matrix->m22()) || !isfinite(matrix->m41()) || !isfinite(matrix->m42()))
+ return {};
+
+ // 4. Create a copy of all the subpaths in path. Let this copy be known as c.
+ // 5. Transform all the coordinates and lines in c by the transform matrix matrix.
+ auto copy = path->path().copy_transformed(Gfx::AffineTransform { static_cast<float>(matrix->m11()), static_cast<float>(matrix->m12()), static_cast<float>(matrix->m21()), static_cast<float>(matrix->m22()), static_cast<float>(matrix->m41()), static_cast<float>(matrix->m42()) });
+
+ // 6. Let (x, y) be the last point in the last subpath of c.
+ auto xy = copy.segments().last().point();
+
+ // 7. Add all the subpaths in c to a.
+ // FIXME: Is this correct?
+ this->path().add_path(copy);
+
+ // 8. Create a new subpath in a with (x, y) as the only point in the subpath.
+ this->move_to(xy.x(), xy.y());
+
+ return {};
+}
+
}
diff --git a/Userland/Libraries/LibWeb/HTML/Path2D.h b/Userland/Libraries/LibWeb/HTML/Path2D.h
index 81f7155c05..0749f6480e 100644
--- a/Userland/Libraries/LibWeb/HTML/Path2D.h
+++ b/Userland/Libraries/LibWeb/HTML/Path2D.h
@@ -9,6 +9,7 @@
#include <AK/RefCounted.h>
#include <LibGfx/Path.h>
#include <LibWeb/Bindings/PlatformObject.h>
+#include <LibWeb/Geometry/DOMMatrixReadOnly.h>
#include <LibWeb/HTML/Canvas/CanvasPath.h>
namespace Web::HTML {
@@ -25,6 +26,8 @@ public:
virtual ~Path2D() override;
+ WebIDL::ExceptionOr<void> add_path(JS::NonnullGCPtr<Path2D> path, Geometry::DOMMatrix2DInit& transform);
+
private:
Path2D(JS::Realm&, Optional<Variant<JS::Handle<Path2D>, DeprecatedString>> const&);
diff --git a/Userland/Libraries/LibWeb/HTML/Path2D.idl b/Userland/Libraries/LibWeb/HTML/Path2D.idl
index 64264dd2c9..8879e34018 100644
--- a/Userland/Libraries/LibWeb/HTML/Path2D.idl
+++ b/Userland/Libraries/LibWeb/HTML/Path2D.idl
@@ -1,3 +1,4 @@
+#import <Geometry/DOMMatrixReadOnly.idl>
#import <HTML/Canvas/CanvasPath.idl>
// https://html.spec.whatwg.org/multipage/canvas.html#path2d
@@ -5,7 +6,7 @@
interface Path2D {
constructor(optional (Path2D or DOMString) path);
- // FIXME: undefined addPath(Path2D path, optional DOMMatrix2DInit transform = {});
+ undefined addPath(Path2D path, optional DOMMatrix2DInit transform = {});
};
Path2D includes CanvasPath;