SVGCircleElement.cpp 3.5 KB

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