SVGGraphicsElement.cpp 3.4 KB

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