SVGCircleElement.cpp 3.7 KB

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