SVGCircleElement.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/SVGCircleElementPrototype.h>
  7. #include <LibWeb/HTML/Window.h>
  8. #include <LibWeb/SVG/AttributeNames.h>
  9. #include <LibWeb/SVG/AttributeParser.h>
  10. #include <LibWeb/SVG/SVGCircleElement.h>
  11. namespace Web::SVG {
  12. SVGCircleElement::SVGCircleElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  13. : SVGGeometryElement(document, qualified_name)
  14. {
  15. set_prototype(&window().ensure_web_prototype<Bindings::SVGCircleElementPrototype>("SVGCircleElement"));
  16. }
  17. void SVGCircleElement::parse_attribute(FlyString const& name, String const& value)
  18. {
  19. SVGGeometryElement::parse_attribute(name, value);
  20. if (name == SVG::AttributeNames::cx) {
  21. m_center_x = AttributeParser::parse_coordinate(value);
  22. m_path.clear();
  23. } else if (name == SVG::AttributeNames::cy) {
  24. m_center_y = AttributeParser::parse_coordinate(value);
  25. m_path.clear();
  26. } else if (name == SVG::AttributeNames::r) {
  27. m_radius = AttributeParser::parse_positive_length(value);
  28. m_path.clear();
  29. }
  30. }
  31. Gfx::Path& SVGCircleElement::get_path()
  32. {
  33. if (m_path.has_value())
  34. return m_path.value();
  35. float cx = m_center_x.value_or(0);
  36. float cy = m_center_y.value_or(0);
  37. float r = m_radius.value_or(0);
  38. Gfx::Path path;
  39. // A zero radius disables rendering.
  40. if (r == 0) {
  41. m_path = move(path);
  42. return m_path.value();
  43. }
  44. bool large_arc = false;
  45. bool sweep = true;
  46. // 1. A move-to command to the point cx+r,cy;
  47. path.move_to({ cx + r, cy });
  48. // 2. arc to cx,cy+r;
  49. path.arc_to({ cx, cy + r }, r, large_arc, sweep);
  50. // 3. arc to cx-r,cy;
  51. path.arc_to({ cx - r, cy }, r, large_arc, sweep);
  52. // 4. arc to cx,cy-r;
  53. path.arc_to({ cx, cy - r }, r, large_arc, sweep);
  54. // 5. arc with a segment-completing close path operation.
  55. path.arc_to({ cx + r, cy }, r, large_arc, sweep);
  56. m_path = move(path);
  57. return m_path.value();
  58. }
  59. // https://www.w3.org/TR/SVG11/shapes.html#CircleElementCXAttribute
  60. JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::cx() const
  61. {
  62. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  63. // FIXME: Create a proper animated value when animations are supported.
  64. auto base_length = SVGLength::create(window(), 0, m_center_x.value_or(0));
  65. auto anim_length = SVGLength::create(window(), 0, m_center_x.value_or(0));
  66. return SVGAnimatedLength::create(window(), move(base_length), move(anim_length));
  67. }
  68. // https://www.w3.org/TR/SVG11/shapes.html#CircleElementCYAttribute
  69. JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::cy() const
  70. {
  71. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  72. // FIXME: Create a proper animated value when animations are supported.
  73. auto base_length = SVGLength::create(window(), 0, m_center_y.value_or(0));
  74. auto anim_length = SVGLength::create(window(), 0, m_center_y.value_or(0));
  75. return SVGAnimatedLength::create(window(), move(base_length), move(anim_length));
  76. }
  77. // https://www.w3.org/TR/SVG11/shapes.html#CircleElementRAttribute
  78. JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::r() const
  79. {
  80. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  81. // FIXME: Create a proper animated value when animations are supported.
  82. auto base_length = SVGLength::create(window(), 0, m_radius.value_or(0));
  83. auto anim_length = SVGLength::create(window(), 0, m_radius.value_or(0));
  84. return SVGAnimatedLength::create(window(), move(base_length), move(anim_length));
  85. }
  86. }