|
1 | 1 | /*
|
2 | 2 | * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
3 | 3 | * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
| 4 | + * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org> |
4 | 5 | *
|
5 | 6 | * SPDX-License-Identifier: BSD-2-Clause
|
6 | 7 | */
|
7 | 8 |
|
8 | 9 | #include <LibWeb/Bindings/Intrinsics.h>
|
| 10 | +#include <LibWeb/Geometry/DOMMatrix.h> |
9 | 11 | #include <LibWeb/HTML/Path2D.h>
|
10 | 12 | #include <LibWeb/SVG/AttributeParser.h>
|
11 | 13 | #include <LibWeb/SVG/SVGPathElement.h>
|
@@ -62,4 +64,37 @@ JS::ThrowCompletionOr<void> Path2D::initialize(JS::Realm& realm)
|
62 | 64 | return {};
|
63 | 65 | }
|
64 | 66 |
|
| 67 | +// https://html.spec.whatwg.org/multipage/canvas.html#dom-path2d-addpath |
| 68 | +WebIDL::ExceptionOr<void> Path2D::add_path(JS::NonnullGCPtr<Path2D> path, Geometry::DOMMatrix2DInit& transform) |
| 69 | +{ |
| 70 | + // The addPath(path, transform) method, when invoked on a Path2D object a, must run these steps: |
| 71 | + |
| 72 | + // 1. If the Path2D object path has no subpaths, then return. |
| 73 | + if (path->path().segments().is_empty()) |
| 74 | + return {}; |
| 75 | + |
| 76 | + // 2. Let matrix be the result of creating a DOMMatrix from the 2D dictionary transform. |
| 77 | + auto matrix = TRY(Geometry::DOMMatrix::create_from_dom_matrix_2d_init(realm(), transform)); |
| 78 | + |
| 79 | + // 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. |
| 80 | + if (!isfinite(matrix->m11()) || !isfinite(matrix->m12()) || !isfinite(matrix->m21()) || !isfinite(matrix->m22()) || !isfinite(matrix->m41()) || !isfinite(matrix->m42())) |
| 81 | + return {}; |
| 82 | + |
| 83 | + // 4. Create a copy of all the subpaths in path. Let this copy be known as c. |
| 84 | + // 5. Transform all the coordinates and lines in c by the transform matrix matrix. |
| 85 | + 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()) }); |
| 86 | + |
| 87 | + // 6. Let (x, y) be the last point in the last subpath of c. |
| 88 | + auto xy = copy.segments().last().point(); |
| 89 | + |
| 90 | + // 7. Add all the subpaths in c to a. |
| 91 | + // FIXME: Is this correct? |
| 92 | + this->path().add_path(copy); |
| 93 | + |
| 94 | + // 8. Create a new subpath in a with (x, y) as the only point in the subpath. |
| 95 | + this->move_to(xy.x(), xy.y()); |
| 96 | + |
| 97 | + return {}; |
| 98 | +} |
| 99 | + |
65 | 100 | }
|
0 commit comments