SVGCircleElement.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. SVGCircleElement::SVGCircleElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  12. : SVGGeometryElement(document, qualified_name)
  13. {
  14. }
  15. JS::ThrowCompletionOr<void> SVGCircleElement::initialize(JS::Realm& realm)
  16. {
  17. MUST_OR_THROW_OOM(Base::initialize(realm));
  18. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGCircleElementPrototype>(realm, "SVGCircleElement"));
  19. return {};
  20. }
  21. void SVGCircleElement::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::r) {
  31. m_radius = AttributeParser::parse_positive_length(value);
  32. m_path.clear();
  33. }
  34. }
  35. Gfx::Path& SVGCircleElement::get_path()
  36. {
  37. if (m_path.has_value())
  38. return m_path.value();
  39. float cx = m_center_x.value_or(0);
  40. float cy = m_center_y.value_or(0);
  41. float r = m_radius.value_or(0);
  42. Gfx::Path path;
  43. // A zero radius disables rendering.
  44. if (r == 0) {
  45. m_path = move(path);
  46. return m_path.value();
  47. }
  48. bool large_arc = false;
  49. bool sweep = true;
  50. // 1. A move-to command to the point cx+r,cy;
  51. path.move_to({ cx + r, cy });
  52. // 2. arc to cx,cy+r;
  53. path.arc_to({ cx, cy + r }, r, large_arc, sweep);
  54. // 3. arc to cx-r,cy;
  55. path.arc_to({ cx - r, cy }, r, large_arc, sweep);
  56. // 4. arc to cx,cy-r;
  57. path.arc_to({ cx, cy - r }, r, large_arc, sweep);
  58. // 5. arc with a segment-completing close path operation.
  59. path.arc_to({ cx + r, cy }, r, large_arc, sweep);
  60. m_path = move(path);
  61. return m_path.value();
  62. }
  63. // https://www.w3.org/TR/SVG11/shapes.html#CircleElementCXAttribute
  64. JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::cx() const
  65. {
  66. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  67. // FIXME: Create a proper animated value when animations are supported.
  68. auto base_length = SVGLength::create(realm(), 0, m_center_x.value_or(0)).release_value_but_fixme_should_propagate_errors();
  69. auto anim_length = SVGLength::create(realm(), 0, m_center_x.value_or(0)).release_value_but_fixme_should_propagate_errors();
  70. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length)).release_value_but_fixme_should_propagate_errors();
  71. }
  72. // https://www.w3.org/TR/SVG11/shapes.html#CircleElementCYAttribute
  73. JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::cy() const
  74. {
  75. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  76. // FIXME: Create a proper animated value when animations are supported.
  77. auto base_length = SVGLength::create(realm(), 0, m_center_y.value_or(0)).release_value_but_fixme_should_propagate_errors();
  78. auto anim_length = SVGLength::create(realm(), 0, m_center_y.value_or(0)).release_value_but_fixme_should_propagate_errors();
  79. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length)).release_value_but_fixme_should_propagate_errors();
  80. }
  81. // https://www.w3.org/TR/SVG11/shapes.html#CircleElementRAttribute
  82. JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::r() const
  83. {
  84. // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
  85. // FIXME: Create a proper animated value when animations are supported.
  86. auto base_length = SVGLength::create(realm(), 0, m_radius.value_or(0)).release_value_but_fixme_should_propagate_errors();
  87. auto anim_length = SVGLength::create(realm(), 0, m_radius.value_or(0)).release_value_but_fixme_should_propagate_errors();
  88. return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length)).release_value_but_fixme_should_propagate_errors();
  89. }
  90. }