SVGCircleElement.cpp 3.3 KB

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