SVGEllipseElement.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/SVGEllipseElementPrototype.h>
  7. #include <LibWeb/HTML/Window.h>
  8. #include <LibWeb/SVG/AttributeNames.h>
  9. #include <LibWeb/SVG/AttributeParser.h>
  10. #include <LibWeb/SVG/SVGEllipseElement.h>
  11. namespace Web::SVG {
  12. JS_DEFINE_ALLOCATOR(SVGEllipseElement);
  13. SVGEllipseElement::SVGEllipseElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  14. : SVGGeometryElement(document, qualified_name)
  15. {
  16. }
  17. void SVGEllipseElement::initialize(JS::Realm& realm)
  18. {
  19. Base::initialize(realm);
  20. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGEllipseElement);
  21. }
  22. void SVGEllipseElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value)
  23. {
  24. SVGGeometryElement::attribute_changed(name, old_value, value);
  25. if (name == SVG::AttributeNames::cx) {
  26. m_center_x = AttributeParser::parse_coordinate(value.value_or(String {}));
  27. } else if (name == SVG::AttributeNames::cy) {
  28. m_center_y = AttributeParser::parse_coordinate(value.value_or(String {}));
  29. } else if (name == SVG::AttributeNames::rx) {
  30. m_radius_x = AttributeParser::parse_positive_length(value.value_or(String {}));
  31. } else if (name == SVG::AttributeNames::ry) {
  32. m_radius_y = AttributeParser::parse_positive_length(value.value_or(String {}));
  33. }
  34. }
  35. Gfx::DeprecatedPath SVGEllipseElement::get_path(CSSPixelSize)
  36. {
  37. float rx = m_radius_x.value_or(0);
  38. float ry = m_radius_y.value_or(0);
  39. float cx = m_center_x.value_or(0);
  40. float cy = m_center_y.value_or(0);
  41. Gfx::DeprecatedPath path;
  42. // A computed value of zero for either dimension, or a computed value of auto for both dimensions, disables rendering of the element.
  43. if (rx == 0 || ry == 0)
  44. return path;
  45. Gfx::FloatSize 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. return path;
  60. }
  61. // https://www.w3.org/TR/SVG11/shapes.html#EllipseElementCXAttribute
  62. JS::NonnullGCPtr<SVGAnimatedLength> SVGEllipseElement::cx() const
  63. {
  64. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  65. // FIXME: Create a proper animated value when animations are supported.
  66. auto base_length = SVGLength::create(realm(), 0, m_center_x.value_or(0));
  67. auto anim_length = SVGLength::create(realm(), 0, m_center_x.value_or(0));
  68. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  69. }
  70. // https://www.w3.org/TR/SVG11/shapes.html#EllipseElementCYAttribute
  71. JS::NonnullGCPtr<SVGAnimatedLength> SVGEllipseElement::cy() const
  72. {
  73. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  74. // FIXME: Create a proper animated value when animations are supported.
  75. auto base_length = SVGLength::create(realm(), 0, m_center_y.value_or(0));
  76. auto anim_length = SVGLength::create(realm(), 0, m_center_y.value_or(0));
  77. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  78. }
  79. // https://www.w3.org/TR/SVG11/shapes.html#EllipseElementRXAttribute
  80. JS::NonnullGCPtr<SVGAnimatedLength> SVGEllipseElement::rx() const
  81. {
  82. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  83. // FIXME: Create a proper animated value when animations are supported.
  84. auto base_length = SVGLength::create(realm(), 0, m_radius_x.value_or(0));
  85. auto anim_length = SVGLength::create(realm(), 0, m_radius_x.value_or(0));
  86. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  87. }
  88. // https://www.w3.org/TR/SVG11/shapes.html#EllipseElementRYAttribute
  89. JS::NonnullGCPtr<SVGAnimatedLength> SVGEllipseElement::ry() const
  90. {
  91. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  92. // FIXME: Create a proper animated value when animations are supported.
  93. auto base_length = SVGLength::create(realm(), 0, m_radius_y.value_or(0));
  94. auto anim_length = SVGLength::create(realm(), 0, m_radius_y.value_or(0));
  95. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  96. }
  97. }