SVGEllipseElement.cpp 4.8 KB

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