SVGEllipseElement.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. JS::ThrowCompletionOr<void> SVGEllipseElement::initialize(JS::Realm& realm)
  16. {
  17. MUST_OR_THROW_OOM(Base::initialize(realm));
  18. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGEllipseElementPrototype>(realm, "SVGEllipseElement"));
  19. return {};
  20. }
  21. void SVGEllipseElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
  22. {
  23. SVGGeometryElement::parse_attribute(name, value);
  24. if (name == SVG::AttributeNames::cx) {
  25. m_center_x = AttributeParser::parse_coordinate(value);
  26. m_path.clear();
  27. } else if (name == SVG::AttributeNames::cy) {
  28. m_center_y = AttributeParser::parse_coordinate(value);
  29. m_path.clear();
  30. } else if (name == SVG::AttributeNames::rx) {
  31. m_radius_x = AttributeParser::parse_positive_length(value);
  32. m_path.clear();
  33. } else if (name == SVG::AttributeNames::ry) {
  34. m_radius_y = AttributeParser::parse_positive_length(value);
  35. m_path.clear();
  36. }
  37. }
  38. Gfx::Path& SVGEllipseElement::get_path()
  39. {
  40. if (m_path.has_value())
  41. return m_path.value();
  42. float rx = m_radius_x.value_or(0);
  43. float ry = m_radius_y.value_or(0);
  44. float cx = m_center_x.value_or(0);
  45. float cy = m_center_y.value_or(0);
  46. Gfx::Path path;
  47. // A computed value of zero for either dimension, or a computed value of auto for both dimensions, disables rendering of the element.
  48. if (rx == 0 || ry == 0) {
  49. m_path = move(path);
  50. return m_path.value();
  51. }
  52. Gfx::FloatSize radii = { rx, ry };
  53. double x_axis_rotation = 0;
  54. bool large_arc = false;
  55. bool sweep = true; // Note: Spec says it should be false, but it's wrong. https://github.com/w3c/svgwg/issues/765
  56. // 1. A move-to command to the point cx+rx,cy;
  57. path.move_to({ cx + rx, cy });
  58. // 2. arc to cx,cy+ry;
  59. path.elliptical_arc_to({ cx, cy + ry }, radii, x_axis_rotation, large_arc, sweep);
  60. // 3. arc to cx-rx,cy;
  61. path.elliptical_arc_to({ cx - rx, cy }, radii, x_axis_rotation, large_arc, sweep);
  62. // 4. arc to cx,cy-ry;
  63. path.elliptical_arc_to({ cx, cy - ry }, radii, x_axis_rotation, large_arc, sweep);
  64. // 5. arc with a segment-completing close path operation.
  65. path.elliptical_arc_to({ cx + rx, cy }, radii, x_axis_rotation, large_arc, sweep);
  66. m_path = move(path);
  67. return m_path.value();
  68. }
  69. // https://www.w3.org/TR/SVG11/shapes.html#EllipseElementCXAttribute
  70. JS::NonnullGCPtr<SVGAnimatedLength> SVGEllipseElement::cx() const
  71. {
  72. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  73. // FIXME: Create a proper animated value when animations are supported.
  74. auto base_length = SVGLength::create(realm(), 0, m_center_x.value_or(0)).release_value_but_fixme_should_propagate_errors();
  75. auto anim_length = SVGLength::create(realm(), 0, m_center_x.value_or(0)).release_value_but_fixme_should_propagate_errors();
  76. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length)).release_value_but_fixme_should_propagate_errors();
  77. }
  78. // https://www.w3.org/TR/SVG11/shapes.html#EllipseElementCYAttribute
  79. JS::NonnullGCPtr<SVGAnimatedLength> SVGEllipseElement::cy() const
  80. {
  81. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  82. // FIXME: Create a proper animated value when animations are supported.
  83. auto base_length = SVGLength::create(realm(), 0, m_center_y.value_or(0)).release_value_but_fixme_should_propagate_errors();
  84. auto anim_length = SVGLength::create(realm(), 0, m_center_y.value_or(0)).release_value_but_fixme_should_propagate_errors();
  85. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length)).release_value_but_fixme_should_propagate_errors();
  86. }
  87. // https://www.w3.org/TR/SVG11/shapes.html#EllipseElementRXAttribute
  88. JS::NonnullGCPtr<SVGAnimatedLength> SVGEllipseElement::rx() const
  89. {
  90. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  91. // FIXME: Create a proper animated value when animations are supported.
  92. auto base_length = SVGLength::create(realm(), 0, m_radius_x.value_or(0)).release_value_but_fixme_should_propagate_errors();
  93. auto anim_length = SVGLength::create(realm(), 0, m_radius_x.value_or(0)).release_value_but_fixme_should_propagate_errors();
  94. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length)).release_value_but_fixme_should_propagate_errors();
  95. }
  96. // https://www.w3.org/TR/SVG11/shapes.html#EllipseElementRYAttribute
  97. JS::NonnullGCPtr<SVGAnimatedLength> SVGEllipseElement::ry() const
  98. {
  99. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  100. // FIXME: Create a proper animated value when animations are supported.
  101. auto base_length = SVGLength::create(realm(), 0, m_radius_y.value_or(0)).release_value_but_fixme_should_propagate_errors();
  102. auto anim_length = SVGLength::create(realm(), 0, m_radius_y.value_or(0)).release_value_but_fixme_should_propagate_errors();
  103. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length)).release_value_but_fixme_should_propagate_errors();
  104. }
  105. }