| 
									
										
										
										
											2022-02-11 16:14:58 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 18:04:39 -06:00
										 |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-11 16:14:58 +00:00
										 |  |  | #include <LibWeb/SVG/AttributeNames.h>
 | 
					
						
							|  |  |  | #include <LibWeb/SVG/AttributeParser.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | #include <LibWeb/SVG/SVGCircleElement.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-11 16:14:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::SVG { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  | JS_DEFINE_ALLOCATOR(SVGCircleElement); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-18 21:00:52 +01:00
										 |  |  | SVGCircleElement::SVGCircleElement(DOM::Document& document, DOM::QualifiedName qualified_name) | 
					
						
							| 
									
										
										
										
											2022-02-11 16:14:58 +00:00
										 |  |  |     : SVGGeometryElement(document, qualified_name) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  | void SVGCircleElement::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::SVGCircleElementPrototype>(realm, "SVGCircleElement"_fly_string)); | 
					
						
							| 
									
										
										
										
											2022-02-11 16:14:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-19 18:10:36 +13:00
										 |  |  | void SVGCircleElement::attribute_changed(FlyString const& name, Optional<String> const& value) | 
					
						
							| 
									
										
										
										
											2022-02-11 16:14:58 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-03 17:08:37 +02:00
										 |  |  |     SVGGeometryElement::attribute_changed(name, value); | 
					
						
							| 
									
										
										
										
											2022-02-11 16:14:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (name == SVG::AttributeNames::cx) { | 
					
						
							| 
									
										
										
										
											2023-11-19 18:10:36 +13:00
										 |  |  |         m_center_x = AttributeParser::parse_coordinate(value.value_or(String {})); | 
					
						
							| 
									
										
										
										
											2022-02-11 16:14:58 +00:00
										 |  |  |         m_path.clear(); | 
					
						
							|  |  |  |     } else if (name == SVG::AttributeNames::cy) { | 
					
						
							| 
									
										
										
										
											2023-11-19 18:10:36 +13:00
										 |  |  |         m_center_y = AttributeParser::parse_coordinate(value.value_or(String {})); | 
					
						
							| 
									
										
										
										
											2022-02-11 16:14:58 +00:00
										 |  |  |         m_path.clear(); | 
					
						
							|  |  |  |     } else if (name == SVG::AttributeNames::r) { | 
					
						
							| 
									
										
										
										
											2023-11-19 18:10:36 +13:00
										 |  |  |         m_radius = AttributeParser::parse_positive_length(value.value_or(String {})); | 
					
						
							| 
									
										
										
										
											2022-02-11 16:14:58 +00:00
										 |  |  |         m_path.clear(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-03 20:15:06 +00:00
										 |  |  | Gfx::Path SVGCircleElement::get_path(CSSPixelSize viewport_size) | 
					
						
							| 
									
										
										
										
											2022-02-11 16:14:58 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     float cx = m_center_x.value_or(0); | 
					
						
							|  |  |  |     float cy = m_center_y.value_or(0); | 
					
						
							|  |  |  |     float r = m_radius.value_or(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Gfx::Path path; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // A zero radius disables rendering.
 | 
					
						
							| 
									
										
										
										
											2024-03-03 20:15:06 +00:00
										 |  |  |     if (r == 0) | 
					
						
							|  |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2022-02-11 16:14:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool large_arc = false; | 
					
						
							|  |  |  |     bool sweep = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. A move-to command to the point cx+r,cy;
 | 
					
						
							|  |  |  |     path.move_to({ cx + r, cy }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. arc to cx,cy+r;
 | 
					
						
							|  |  |  |     path.arc_to({ cx, cy + r }, r, large_arc, sweep); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. arc to cx-r,cy;
 | 
					
						
							|  |  |  |     path.arc_to({ cx - r, cy }, r, large_arc, sweep); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 4. arc to cx,cy-r;
 | 
					
						
							|  |  |  |     path.arc_to({ cx, cy - r }, r, large_arc, sweep); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 5. arc with a segment-completing close path operation.
 | 
					
						
							|  |  |  |     path.arc_to({ cx + r, cy }, r, large_arc, sweep); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-03 20:15:06 +00:00
										 |  |  |     return path; | 
					
						
							| 
									
										
										
										
											2022-02-11 16:14:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-22 16:37:16 +00:00
										 |  |  | // https://www.w3.org/TR/SVG11/shapes.html#CircleElementCXAttribute
 | 
					
						
							| 
									
										
										
										
											2022-09-02 14:04:59 +02:00
										 |  |  | JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::cx() const | 
					
						
							| 
									
										
										
										
											2022-03-22 16:37:16 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
 | 
					
						
							|  |  |  |     // FIXME: Create a proper animated value when animations are supported.
 | 
					
						
							| 
									
										
										
										
											2023-08-13 13:05:26 +02:00
										 |  |  |     auto base_length = SVGLength::create(realm(), 0, m_center_x.value_or(0)); | 
					
						
							|  |  |  |     auto anim_length = SVGLength::create(realm(), 0, m_center_x.value_or(0)); | 
					
						
							|  |  |  |     return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length)); | 
					
						
							| 
									
										
										
										
											2022-03-22 16:37:16 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://www.w3.org/TR/SVG11/shapes.html#CircleElementCYAttribute
 | 
					
						
							| 
									
										
										
										
											2022-09-02 14:04:59 +02:00
										 |  |  | JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::cy() const | 
					
						
							| 
									
										
										
										
											2022-03-22 16:37:16 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
 | 
					
						
							|  |  |  |     // FIXME: Create a proper animated value when animations are supported.
 | 
					
						
							| 
									
										
										
										
											2023-08-13 13:05:26 +02:00
										 |  |  |     auto base_length = SVGLength::create(realm(), 0, m_center_y.value_or(0)); | 
					
						
							|  |  |  |     auto anim_length = SVGLength::create(realm(), 0, m_center_y.value_or(0)); | 
					
						
							|  |  |  |     return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length)); | 
					
						
							| 
									
										
										
										
											2022-03-22 16:37:16 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://www.w3.org/TR/SVG11/shapes.html#CircleElementRAttribute
 | 
					
						
							| 
									
										
										
										
											2022-09-02 14:04:59 +02:00
										 |  |  | JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::r() const | 
					
						
							| 
									
										
										
										
											2022-03-22 16:37:16 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
 | 
					
						
							|  |  |  |     // FIXME: Create a proper animated value when animations are supported.
 | 
					
						
							| 
									
										
										
										
											2023-08-13 13:05:26 +02:00
										 |  |  |     auto base_length = SVGLength::create(realm(), 0, m_radius.value_or(0)); | 
					
						
							|  |  |  |     auto anim_length = SVGLength::create(realm(), 0, m_radius.value_or(0)); | 
					
						
							|  |  |  |     return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length)); | 
					
						
							| 
									
										
										
										
											2022-03-22 16:37:16 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 16:14:58 +00:00
										 |  |  | } |