Serenity Operating System
at master 92 lines 4.5 kB view raw
1/* 2 * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org> 3 * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include <LibWeb/Bindings/Intrinsics.h> 9#include <LibWeb/CSS/Parser/Parser.h> 10#include <LibWeb/Layout/Node.h> 11#include <LibWeb/SVG/SVGGraphicsElement.h> 12#include <LibWeb/SVG/SVGSVGElement.h> 13 14namespace Web::SVG { 15 16SVGGraphicsElement::SVGGraphicsElement(DOM::Document& document, DOM::QualifiedName qualified_name) 17 : SVGElement(document, move(qualified_name)) 18{ 19} 20 21JS::ThrowCompletionOr<void> SVGGraphicsElement::initialize(JS::Realm& realm) 22{ 23 MUST_OR_THROW_OOM(Base::initialize(realm)); 24 set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGGraphicsElementPrototype>(realm, "SVGGraphicsElement")); 25 26 return {}; 27} 28 29void SVGGraphicsElement::apply_presentational_hints(CSS::StyleProperties& style) const 30{ 31 CSS::Parser::ParsingContext parsing_context { document() }; 32 for_each_attribute([&](auto& name, auto& value) { 33 if (name.equals_ignoring_ascii_case("fill"sv)) { 34 // FIXME: The `fill` attribute and CSS `fill` property are not the same! But our support is limited enough that they are equivalent for now. 35 if (auto fill_value = parse_css_value(parsing_context, value, CSS::PropertyID::Fill)) 36 style.set_property(CSS::PropertyID::Fill, fill_value.release_nonnull()); 37 } else if (name.equals_ignoring_ascii_case("stroke"sv)) { 38 // FIXME: The `stroke` attribute and CSS `stroke` property are not the same! But our support is limited enough that they are equivalent for now. 39 if (auto stroke_value = parse_css_value(parsing_context, value, CSS::PropertyID::Stroke)) 40 style.set_property(CSS::PropertyID::Stroke, stroke_value.release_nonnull()); 41 } else if (name.equals_ignoring_ascii_case("stroke-width"sv)) { 42 if (auto stroke_width_value = parse_css_value(parsing_context, value, CSS::PropertyID::StrokeWidth)) 43 style.set_property(CSS::PropertyID::StrokeWidth, stroke_width_value.release_nonnull()); 44 } else if (name.equals_ignoring_ascii_case("transform"sv)) { 45 if (auto transform = parse_css_value(parsing_context, value, CSS::PropertyID::Transform)) 46 style.set_property(CSS::PropertyID::Transform, transform.release_nonnull()); 47 } 48 }); 49} 50 51Optional<Gfx::Color> SVGGraphicsElement::fill_color() const 52{ 53 if (!layout_node()) 54 return {}; 55 // FIXME: In the working-draft spec, `fill` is intended to be a shorthand, with `fill-color` 56 // being what we actually want to use. But that's not final or widely supported yet. 57 return layout_node()->computed_values().fill(); 58} 59 60Optional<Gfx::Color> SVGGraphicsElement::stroke_color() const 61{ 62 if (!layout_node()) 63 return {}; 64 // FIXME: In the working-draft spec, `stroke` is intended to be a shorthand, with `stroke-color` 65 // being what we actually want to use. But that's not final or widely supported yet. 66 return layout_node()->computed_values().stroke(); 67} 68 69Optional<float> SVGGraphicsElement::stroke_width() const 70{ 71 if (!layout_node()) 72 return {}; 73 // FIXME: Converting to pixels isn't really correct - values should be in "user units" 74 // https://svgwg.org/svg2-draft/coords.html#TermUserUnits 75 if (auto width = layout_node()->computed_values().stroke_width(); width.has_value()) { 76 // Resolved relative to the "Scaled viewport size": https://www.w3.org/TR/2017/WD-fill-stroke-3-20170413/#scaled-viewport-size 77 // FIXME: This isn't right, but it's something. 78 CSSPixels viewport_width = 0; 79 CSSPixels viewport_height = 0; 80 if (auto* svg_svg_element = first_ancestor_of_type<SVGSVGElement>()) { 81 if (auto* svg_svg_layout_node = svg_svg_element->layout_node()) { 82 viewport_width = svg_svg_layout_node->computed_values().width().resolved(*svg_svg_layout_node, CSS::Length::make_px(0)).to_px(*svg_svg_layout_node); 83 viewport_height = svg_svg_layout_node->computed_values().height().resolved(*svg_svg_layout_node, CSS::Length::make_px(0)).to_px(*svg_svg_layout_node); 84 } 85 } 86 auto scaled_viewport_size = CSS::Length::make_px((viewport_width + viewport_height) * 0.5f); 87 return width->resolved(*layout_node(), scaled_viewport_size).to_px(*layout_node()).value(); 88 } 89 return {}; 90} 91 92}