| 
									
										
										
										
											2022-08-11 16:50:23 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-02-27 18:23:31 +00:00
										 |  |  |  * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-08-11 16:50:23 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 16:38:21 -06:00
										 |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-27 12:09:58 +12:00
										 |  |  | #include <LibWeb/Bindings/Path2DPrototype.h>
 | 
					
						
							| 
									
										
										
										
											2023-02-27 18:23:31 +00:00
										 |  |  | #include <LibWeb/Geometry/DOMMatrix.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-11 16:50:23 +01:00
										 |  |  | #include <LibWeb/HTML/Path2D.h>
 | 
					
						
							| 
									
										
										
										
											2022-11-29 14:07:22 +01:00
										 |  |  | #include <LibWeb/SVG/AttributeParser.h>
 | 
					
						
							|  |  |  | #include <LibWeb/SVG/SVGPathElement.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-11 16:50:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(Path2D); | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | WebIDL::ExceptionOr<GC::Ref<Path2D>> Path2D::construct_impl(JS::Realm& realm, Optional<Variant<GC::Root<Path2D>, String>> const& path) | 
					
						
							| 
									
										
										
										
											2022-09-02 23:35:06 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |     return realm.create<Path2D>(realm, path); | 
					
						
							| 
									
										
										
										
											2022-09-02 23:35:06 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-11 16:50:23 +01:00
										 |  |  | // https://html.spec.whatwg.org/multipage/canvas.html#dom-path2d
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | Path2D::Path2D(JS::Realm& realm, Optional<Variant<GC::Root<Path2D>, String>> const& path) | 
					
						
							| 
									
										
										
										
											2022-09-25 16:38:21 -06:00
										 |  |  |     : PlatformObject(realm) | 
					
						
							| 
									
										
										
										
											2022-09-04 16:56:15 +02:00
										 |  |  |     , CanvasPath(static_cast<Bindings::PlatformObject&>(*this)) | 
					
						
							| 
									
										
										
										
											2022-08-11 16:50:23 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. Let output be a new Path2D object.
 | 
					
						
							|  |  |  |     // 2. If path is not given, then return output.
 | 
					
						
							|  |  |  |     if (!path.has_value()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. If path is a Path2D object, then add all subpaths of path to output and return output.
 | 
					
						
							|  |  |  |     //    (In other words, it returns a copy of the argument.)
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     if (path->has<GC::Root<Path2D>>()) { | 
					
						
							|  |  |  |         this->path() = path->get<GC::Root<Path2D>>()->path(); | 
					
						
							| 
									
										
										
										
											2022-08-11 16:50:23 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 14:07:22 +01:00
										 |  |  |     // 4. Let svgPath be the result of parsing and interpreting path according to SVG 2's rules for path data. [SVG]
 | 
					
						
							| 
									
										
										
										
											2023-08-27 18:55:01 +12:00
										 |  |  |     auto path_instructions = SVG::AttributeParser::parse_path_data(path->get<String>()); | 
					
						
							| 
									
										
										
										
											2022-11-29 14:07:22 +01:00
										 |  |  |     auto svg_path = SVG::path_from_path_instructions(path_instructions); | 
					
						
							| 
									
										
										
										
											2022-08-11 16:50:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-17 20:23:17 +00:00
										 |  |  |     if (!svg_path.is_empty()) { | 
					
						
							| 
									
										
										
										
											2022-11-29 14:07:22 +01:00
										 |  |  |         // 5. Let (x, y) be the last point in svgPath.
 | 
					
						
							| 
									
										
										
										
											2024-03-17 20:23:17 +00:00
										 |  |  |         auto xy = svg_path.last_point(); | 
					
						
							| 
									
										
										
										
											2022-11-29 14:07:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // 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.
 | 
					
						
							| 
									
										
										
										
											2022-08-11 16:50:23 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-02 23:35:06 +02:00
										 |  |  | Path2D::~Path2D() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  | void Path2D::initialize(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2023-11-22 12:55:21 +13:00
										 |  |  |     set_prototype(&Bindings::ensure_web_prototype<Bindings::Path2DPrototype>(realm, "Path2D"_fly_string)); | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-27 18:23:31 +00:00
										 |  |  | // https://html.spec.whatwg.org/multipage/canvas.html#dom-path2d-addpath
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | WebIDL::ExceptionOr<void> Path2D::add_path(GC::Ref<Path2D> path, Geometry::DOMMatrix2DInit& transform) | 
					
						
							| 
									
										
										
										
											2023-02-27 18:23:31 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     // 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.
 | 
					
						
							| 
									
										
										
										
											2024-03-17 20:23:17 +00:00
										 |  |  |     if (path->path().is_empty()) | 
					
						
							| 
									
										
										
										
											2023-02-27 18:23:31 +00:00
										 |  |  |         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.
 | 
					
						
							| 
									
										
										
										
											2024-03-17 20:23:17 +00:00
										 |  |  |     auto xy = copy.last_point(); | 
					
						
							| 
									
										
										
										
											2023-02-27 18:23:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 7. Add all the subpaths in c to a.
 | 
					
						
							|  |  |  |     // FIXME: Is this correct?
 | 
					
						
							| 
									
										
										
										
											2024-03-17 20:23:17 +00:00
										 |  |  |     this->path().append_path(copy); | 
					
						
							| 
									
										
										
										
											2023-02-27 18:23:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 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 {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-11 16:50:23 +01:00
										 |  |  | } |