LibWeb: Implement Path2D#addPath

Required by Ruffle.
This commit is contained in:
Luke Wilde 2023-02-27 18:23:31 +00:00 committed by Andreas Kling
parent 1f97adbee8
commit a964ebc255
Notes: sideshowbarker 2024-07-17 07:25:39 +09:00
3 changed files with 40 additions and 1 deletions

View File

@ -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 {};
}
}

View File

@ -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&);

View File

@ -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;