SVGGraphicsElement.cpp 4.4 KB

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