SVGGraphicsElement.cpp 4.1 KB

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