SVGCircleElement.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/SVG/AttributeNames.h>
  8. #include <LibWeb/SVG/AttributeParser.h>
  9. #include <LibWeb/SVG/SVGCircleElement.h>
  10. namespace Web::SVG {
  11. JS_DEFINE_ALLOCATOR(SVGCircleElement);
  12. SVGCircleElement::SVGCircleElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  13. : SVGGeometryElement(document, qualified_name)
  14. {
  15. }
  16. void SVGCircleElement::initialize(JS::Realm& realm)
  17. {
  18. Base::initialize(realm);
  19. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGCircleElementPrototype>(realm, "SVGCircleElement"_fly_string));
  20. }
  21. void SVGCircleElement::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. m_path.clear();
  27. } else if (name == SVG::AttributeNames::cy) {
  28. m_center_y = AttributeParser::parse_coordinate(value.value_or(String {}));
  29. m_path.clear();
  30. } else if (name == SVG::AttributeNames::r) {
  31. m_radius = AttributeParser::parse_positive_length(value.value_or(String {}));
  32. m_path.clear();
  33. }
  34. }
  35. Gfx::Path SVGCircleElement::get_path(CSSPixelSize viewport_size)
  36. {
  37. float cx = m_center_x.value_or(0);
  38. float cy = m_center_y.value_or(0);
  39. float r = m_radius.value_or(0);
  40. Gfx::Path path;
  41. // A zero radius disables rendering.
  42. if (r == 0)
  43. return {};
  44. bool large_arc = false;
  45. bool sweep = true;
  46. // 1. A move-to command to the point cx+r,cy;
  47. path.move_to({ cx + r, cy });
  48. // 2. arc to cx,cy+r;
  49. path.arc_to({ cx, cy + r }, r, large_arc, sweep);
  50. // 3. arc to cx-r,cy;
  51. path.arc_to({ cx - r, cy }, r, large_arc, sweep);
  52. // 4. arc to cx,cy-r;
  53. path.arc_to({ cx, cy - r }, r, large_arc, sweep);
  54. // 5. arc with a segment-completing close path operation.
  55. path.arc_to({ cx + r, cy }, r, large_arc, sweep);
  56. return path;
  57. }
  58. // https://www.w3.org/TR/SVG11/shapes.html#CircleElementCXAttribute
  59. JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::cx() const
  60. {
  61. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  62. // FIXME: Create a proper animated value when animations are supported.
  63. auto base_length = SVGLength::create(realm(), 0, m_center_x.value_or(0));
  64. auto anim_length = SVGLength::create(realm(), 0, m_center_x.value_or(0));
  65. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  66. }
  67. // https://www.w3.org/TR/SVG11/shapes.html#CircleElementCYAttribute
  68. JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::cy() const
  69. {
  70. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  71. // FIXME: Create a proper animated value when animations are supported.
  72. auto base_length = SVGLength::create(realm(), 0, m_center_y.value_or(0));
  73. auto anim_length = SVGLength::create(realm(), 0, m_center_y.value_or(0));
  74. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  75. }
  76. // https://www.w3.org/TR/SVG11/shapes.html#CircleElementRAttribute
  77. JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::r() const
  78. {
  79. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  80. // FIXME: Create a proper animated value when animations are supported.
  81. auto base_length = SVGLength::create(realm(), 0, m_radius.value_or(0));
  82. auto anim_length = SVGLength::create(realm(), 0, m_radius.value_or(0));
  83. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
  84. }
  85. }