| 
									
										
										
										
											2020-07-22 15:17:39 -07:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-04-22 16:53:07 -07:00
										 |  |  |  * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-01-24 15:43:44 +00:00
										 |  |  |  * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-07-22 15:17:39 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-07-22 15:17:39 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 15:43:44 +00:00
										 |  |  | #include <LibWeb/CSS/Parser/Parser.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | #include <LibWeb/HTML/Window.h>
 | 
					
						
							| 
									
										
										
										
											2022-04-10 20:16:47 +02:00
										 |  |  | #include <LibWeb/Layout/Node.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-23 09:44:42 -07:00
										 |  |  | #include <LibWeb/SVG/SVGGraphicsElement.h>
 | 
					
						
							| 
									
										
										
										
											2022-04-10 20:16:47 +02:00
										 |  |  | #include <LibWeb/SVG/SVGSVGElement.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-22 15:17:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-23 09:44:42 -07:00
										 |  |  | namespace Web::SVG { | 
					
						
							| 
									
										
										
										
											2020-07-22 15:17:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-18 21:00:52 +01:00
										 |  |  | SVGGraphicsElement::SVGGraphicsElement(DOM::Document& document, DOM::QualifiedName qualified_name) | 
					
						
							| 
									
										
										
										
											2021-02-07 11:20:15 +01:00
										 |  |  |     : SVGElement(document, move(qualified_name)) | 
					
						
							| 
									
										
										
										
											2020-07-22 15:17:39 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-25 18:04:39 -06:00
										 |  |  |     set_prototype(&Bindings::cached_web_prototype(realm(), "SVGGraphicsElement")); | 
					
						
							| 
									
										
										
										
											2020-07-22 15:17:39 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 15:43:44 +00:00
										 |  |  | void SVGGraphicsElement::apply_presentational_hints(CSS::StyleProperties& style) const | 
					
						
							| 
									
										
										
										
											2020-07-22 15:17:39 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-12 12:00:07 +01:00
										 |  |  |     CSS::Parser::ParsingContext parsing_context { document() }; | 
					
						
							| 
									
										
										
										
											2022-01-24 15:43:44 +00:00
										 |  |  |     for_each_attribute([&](auto& name, auto& value) { | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         if (name.equals_ignoring_case("fill"sv)) { | 
					
						
							| 
									
										
										
										
											2022-01-24 15:43:44 +00:00
										 |  |  |             // FIXME: The `fill` attribute and CSS `fill` property are not the same! But our support is limited enough that they are equivalent for now.
 | 
					
						
							|  |  |  |             if (auto fill_value = parse_css_value(parsing_context, value, CSS::PropertyID::Fill)) | 
					
						
							|  |  |  |                 style.set_property(CSS::PropertyID::Fill, fill_value.release_nonnull()); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         } else if (name.equals_ignoring_case("stroke"sv)) { | 
					
						
							| 
									
										
										
										
											2022-01-24 15:43:44 +00:00
										 |  |  |             // FIXME: The `stroke` attribute and CSS `stroke` property are not the same! But our support is limited enough that they are equivalent for now.
 | 
					
						
							|  |  |  |             if (auto stroke_value = parse_css_value(parsing_context, value, CSS::PropertyID::Stroke)) | 
					
						
							|  |  |  |                 style.set_property(CSS::PropertyID::Stroke, stroke_value.release_nonnull()); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         } else if (name.equals_ignoring_case("stroke-width"sv)) { | 
					
						
							| 
									
										
										
										
											2022-01-24 15:43:44 +00:00
										 |  |  |             if (auto stroke_width_value = parse_css_value(parsing_context, value, CSS::PropertyID::StrokeWidth)) | 
					
						
							|  |  |  |                 style.set_property(CSS::PropertyID::StrokeWidth, stroke_width_value.release_nonnull()); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         } else if (name.equals_ignoring_case("transform"sv)) { | 
					
						
							| 
									
										
										
										
											2022-03-17 16:59:01 +01:00
										 |  |  |             if (auto transform = parse_css_value(parsing_context, value, CSS::PropertyID::Transform)) | 
					
						
							|  |  |  |                 style.set_property(CSS::PropertyID::Transform, transform.release_nonnull()); | 
					
						
							| 
									
										
										
										
											2022-01-24 15:43:44 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-07-22 15:17:39 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-16 12:28:14 +01:00
										 |  |  | Optional<Gfx::Color> SVGGraphicsElement::fill_color() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!layout_node()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     // FIXME: In the working-draft spec, `fill` is intended to be a shorthand, with `fill-color`
 | 
					
						
							|  |  |  |     //        being what we actually want to use. But that's not final or widely supported yet.
 | 
					
						
							|  |  |  |     return layout_node()->computed_values().fill(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<Gfx::Color> SVGGraphicsElement::stroke_color() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!layout_node()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     // FIXME: In the working-draft spec, `stroke` is intended to be a shorthand, with `stroke-color`
 | 
					
						
							|  |  |  |     //        being what we actually want to use. But that's not final or widely supported yet.
 | 
					
						
							|  |  |  |     return layout_node()->computed_values().stroke(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional<float> SVGGraphicsElement::stroke_width() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!layout_node()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     // FIXME: Converting to pixels isn't really correct - values should be in "user units"
 | 
					
						
							|  |  |  |     //        https://svgwg.org/svg2-draft/coords.html#TermUserUnits
 | 
					
						
							| 
									
										
										
										
											2022-01-14 16:52:14 +00:00
										 |  |  |     if (auto width = layout_node()->computed_values().stroke_width(); width.has_value()) { | 
					
						
							|  |  |  |         // Resolved relative to the "Scaled viewport size": https://www.w3.org/TR/2017/WD-fill-stroke-3-20170413/#scaled-viewport-size
 | 
					
						
							|  |  |  |         // FIXME: This isn't right, but it's something.
 | 
					
						
							| 
									
										
										
										
											2022-04-10 20:16:47 +02:00
										 |  |  |         float viewport_width = 0; | 
					
						
							|  |  |  |         float viewport_height = 0; | 
					
						
							|  |  |  |         if (auto* svg_svg_element = first_ancestor_of_type<SVGSVGElement>()) { | 
					
						
							|  |  |  |             if (auto* svg_svg_layout_node = svg_svg_element->layout_node()) { | 
					
						
							| 
									
										
										
										
											2022-07-07 12:36:02 +02:00
										 |  |  |                 viewport_width = svg_svg_layout_node->computed_values().width().resolved(*svg_svg_layout_node, { 0, CSS::Length::Type::Px }).to_px(*svg_svg_layout_node); | 
					
						
							|  |  |  |                 viewport_height = svg_svg_layout_node->computed_values().height().resolved(*svg_svg_layout_node, { 0, CSS::Length::Type::Px }).to_px(*svg_svg_layout_node); | 
					
						
							| 
									
										
										
										
											2022-04-10 20:16:47 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         auto scaled_viewport_size = CSS::Length::make_px((viewport_width + viewport_height) * 0.5f); | 
					
						
							| 
									
										
										
										
											2022-01-27 14:38:59 +00:00
										 |  |  |         return width->resolved(*layout_node(), scaled_viewport_size).to_px(*layout_node()); | 
					
						
							| 
									
										
										
										
											2022-01-14 16:52:14 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-16 12:28:14 +01:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 15:17:39 -07:00
										 |  |  | } |