SVGGraphicsElement.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/SVG/SVGGraphicsElement.h>
  8. namespace Web::SVG {
  9. SVGGraphicsElement::SVGGraphicsElement(DOM::Document& document, QualifiedName qualified_name)
  10. : SVGElement(document, move(qualified_name))
  11. {
  12. }
  13. void SVGGraphicsElement::parse_attribute(FlyString const& name, String const& value)
  14. {
  15. SVGElement::parse_attribute(name, value);
  16. if (name == "fill") {
  17. m_fill_color = Gfx::Color::from_string(value).value_or(Color::Transparent);
  18. } else if (name == "stroke") {
  19. m_stroke_color = Gfx::Color::from_string(value).value_or(Color::Transparent);
  20. } else if (name == "stroke-width") {
  21. auto result = value.to_int();
  22. if (result.has_value())
  23. m_stroke_width = result.value();
  24. }
  25. }
  26. Optional<Gfx::Color> SVGGraphicsElement::fill_color() const
  27. {
  28. if (m_fill_color.has_value())
  29. return m_fill_color;
  30. if (!layout_node())
  31. return {};
  32. // FIXME: In the working-draft spec, `fill` is intended to be a shorthand, with `fill-color`
  33. // being what we actually want to use. But that's not final or widely supported yet.
  34. return layout_node()->computed_values().fill();
  35. }
  36. Optional<Gfx::Color> SVGGraphicsElement::stroke_color() const
  37. {
  38. if (m_stroke_color.has_value())
  39. return m_stroke_color;
  40. if (!layout_node())
  41. return {};
  42. // FIXME: In the working-draft spec, `stroke` is intended to be a shorthand, with `stroke-color`
  43. // being what we actually want to use. But that's not final or widely supported yet.
  44. return layout_node()->computed_values().stroke();
  45. }
  46. Optional<float> SVGGraphicsElement::stroke_width() const
  47. {
  48. if (m_stroke_width.has_value())
  49. return m_stroke_width;
  50. if (!layout_node())
  51. return {};
  52. // FIXME: Converting to pixels isn't really correct - values should be in "user units"
  53. // https://svgwg.org/svg2-draft/coords.html#TermUserUnits
  54. if (auto width = layout_node()->computed_values().stroke_width(); width.has_value()) {
  55. // Resolved relative to the "Scaled viewport size": https://www.w3.org/TR/2017/WD-fill-stroke-3-20170413/#scaled-viewport-size
  56. // FIXME: This isn't right, but it's something.
  57. auto scaled_viewport_size = CSS::Length::make_px((client_width() + client_height()) * 0.5f);
  58. return width->resolved(scaled_viewport_size).to_px(*layout_node());
  59. }
  60. return {};
  61. }
  62. }