| 
									
										
										
										
											2022-08-11 16:50:23 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-11-29 14:07:22 +01:00
										 |  |  |  * Copyright (c) 2022, Andreas Kling <kling@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>
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | JS::NonnullGCPtr<Path2D> Path2D::construct_impl(JS::Realm& realm, Optional<Variant<JS::Handle<Path2D>, DeprecatedString>> const& path) | 
					
						
							| 
									
										
										
										
											2022-09-02 23:35:06 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-25 16:38:21 -06:00
										 |  |  |     return *realm.heap().allocate<Path2D>(realm, 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
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | Path2D::Path2D(JS::Realm& realm, Optional<Variant<JS::Handle<Path2D>, DeprecatedString>> 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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-25 16:38:21 -06:00
										 |  |  |     set_prototype(&Bindings::cached_web_prototype(realm, "Path2D")); | 
					
						
							| 
									
										
										
										
											2022-09-02 23:35:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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.)
 | 
					
						
							| 
									
										
										
										
											2022-09-02 23:35:06 +02:00
										 |  |  |     if (path->has<JS::Handle<Path2D>>()) { | 
					
						
							|  |  |  |         this->path() = path->get<JS::Handle<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]
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |     auto path_instructions = SVG::AttributeParser::parse_path_data(path->get<DeprecatedString>()); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 14:07:22 +01:00
										 |  |  |     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.
 | 
					
						
							| 
									
										
										
										
											2022-08-11 16:50:23 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-02 23:35:06 +02:00
										 |  |  | Path2D::~Path2D() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-11 16:50:23 +01:00
										 |  |  | } |