SVGEllipseElement.cpp 4.6 KB

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