SVGEllipseElement.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SVGEllipseElement.h"
  7. #include <LibWeb/SVG/AttributeNames.h>
  8. #include <LibWeb/SVG/AttributeParser.h>
  9. namespace Web::SVG {
  10. SVGEllipseElement::SVGEllipseElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  11. : SVGGeometryElement(document, qualified_name)
  12. {
  13. }
  14. void SVGEllipseElement::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::rx) {
  24. m_radius_x = AttributeParser::parse_positive_length(value);
  25. m_path.clear();
  26. } else if (name == SVG::AttributeNames::ry) {
  27. m_radius_y = AttributeParser::parse_positive_length(value);
  28. m_path.clear();
  29. }
  30. }
  31. Gfx::Path& SVGEllipseElement::get_path()
  32. {
  33. if (m_path.has_value())
  34. return m_path.value();
  35. float rx = m_radius_x.value_or(0);
  36. float ry = m_radius_y.value_or(0);
  37. float cx = m_center_x.value_or(0);
  38. float cy = m_center_y.value_or(0);
  39. Gfx::Path path;
  40. // A computed value of zero for either dimension, or a computed value of auto for both dimensions, disables rendering of the element.
  41. if (rx == 0 || ry == 0) {
  42. m_path = move(path);
  43. return m_path.value();
  44. }
  45. Gfx::FloatPoint radii = { rx, ry };
  46. double x_axis_rotation = 0;
  47. bool large_arc = false;
  48. bool sweep = true; // Note: Spec says it should be false, but it's wrong. https://github.com/w3c/svgwg/issues/765
  49. // 1. A move-to command to the point cx+rx,cy;
  50. path.move_to({ cx + rx, cy });
  51. // 2. arc to cx,cy+ry;
  52. path.elliptical_arc_to({ cx, cy + ry }, radii, x_axis_rotation, large_arc, sweep);
  53. // 3. arc to cx-rx,cy;
  54. path.elliptical_arc_to({ cx - rx, cy }, radii, x_axis_rotation, large_arc, sweep);
  55. // 4. arc to cx,cy-ry;
  56. path.elliptical_arc_to({ cx, cy - ry }, radii, x_axis_rotation, large_arc, sweep);
  57. // 5. arc with a segment-completing close path operation.
  58. path.elliptical_arc_to({ cx + rx, cy }, radii, x_axis_rotation, large_arc, sweep);
  59. m_path = move(path);
  60. return m_path.value();
  61. }
  62. // https://www.w3.org/TR/SVG11/shapes.html#EllipseElementCXAttribute
  63. NonnullRefPtr<SVGAnimatedLength> SVGEllipseElement::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(0, m_center_x.value_or(0));
  68. auto anim_length = SVGLength::create(0, m_center_x.value_or(0));
  69. return SVGAnimatedLength::create(move(base_length), move(anim_length));
  70. }
  71. // https://www.w3.org/TR/SVG11/shapes.html#EllipseElementCYAttribute
  72. NonnullRefPtr<SVGAnimatedLength> SVGEllipseElement::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(0, m_center_y.value_or(0));
  77. auto anim_length = SVGLength::create(0, m_center_y.value_or(0));
  78. return SVGAnimatedLength::create(move(base_length), move(anim_length));
  79. }
  80. // https://www.w3.org/TR/SVG11/shapes.html#EllipseElementRXAttribute
  81. NonnullRefPtr<SVGAnimatedLength> SVGEllipseElement::rx() 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(0, m_radius_x.value_or(0));
  86. auto anim_length = SVGLength::create(0, m_radius_x.value_or(0));
  87. return SVGAnimatedLength::create(move(base_length), move(anim_length));
  88. }
  89. // https://www.w3.org/TR/SVG11/shapes.html#EllipseElementRYAttribute
  90. NonnullRefPtr<SVGAnimatedLength> SVGEllipseElement::ry() const
  91. {
  92. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  93. // FIXME: Create a proper animated value when animations are supported.
  94. auto base_length = SVGLength::create(0, m_radius_y.value_or(0));
  95. auto anim_length = SVGLength::create(0, m_radius_y.value_or(0));
  96. return SVGAnimatedLength::create(move(base_length), move(anim_length));
  97. }
  98. }