SVGCircleElement.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/CSS/Parser/Parser.h>
  8. #include <LibWeb/Layout/Node.h>
  9. #include <LibWeb/SVG/AttributeNames.h>
  10. #include <LibWeb/SVG/AttributeParser.h>
  11. #include <LibWeb/SVG/SVGCircleElement.h>
  12. #include <LibWeb/SVG/SVGViewport.h>
  13. namespace Web::SVG {
  14. JS_DEFINE_ALLOCATOR(SVGCircleElement);
  15. SVGCircleElement::SVGCircleElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  16. : SVGGeometryElement(document, qualified_name)
  17. {
  18. }
  19. void SVGCircleElement::initialize(JS::Realm& realm)
  20. {
  21. Base::initialize(realm);
  22. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGCircleElement);
  23. }
  24. void SVGCircleElement::apply_presentational_hints(CSS::StyleProperties& style) const
  25. {
  26. Base::apply_presentational_hints(style);
  27. auto parsing_context = CSS::Parser::ParsingContext { document(), CSS::Parser::ParsingContext::Mode::SVGPresentationAttribute };
  28. auto cx_attribute = attribute(SVG::AttributeNames::cx);
  29. if (auto cx_value = parse_css_value(parsing_context, cx_attribute.value_or(String {}), CSS::PropertyID::Cx))
  30. style.set_property(CSS::PropertyID::Cx, cx_value.release_nonnull());
  31. auto cy_attribute = attribute(SVG::AttributeNames::cy);
  32. if (auto cy_value = parse_css_value(parsing_context, cy_attribute.value_or(String {}), CSS::PropertyID::Cy))
  33. style.set_property(CSS::PropertyID::Cy, cy_value.release_nonnull());
  34. auto r_attribute = attribute(SVG::AttributeNames::r);
  35. if (auto r_value = parse_css_value(parsing_context, r_attribute.value_or(String {}), CSS::PropertyID::R))
  36. style.set_property(CSS::PropertyID::R, r_value.release_nonnull());
  37. }
  38. Gfx::Path SVGCircleElement::get_path(CSSPixelSize viewport_size)
  39. {
  40. auto* node = layout_node();
  41. auto cx = float(node->computed_values().cx().to_px(*node, viewport_size.width()));
  42. auto cy = float(node->computed_values().cy().to_px(*node, viewport_size.height()));
  43. // Percentages refer to the normalized diagonal of the current SVG viewport
  44. // (see Units: https://svgwg.org/svg2-draft/coords.html#Units)
  45. auto r = float(node->computed_values().r().to_px(*node, normalized_diagonal_length(viewport_size)));
  46. // A zero radius disables rendering.
  47. if (r == 0)
  48. return {};
  49. Gfx::Path path;
  50. bool large_arc = false;
  51. bool sweep = true;
  52. // 1. A move-to command to the point cx+r,cy;
  53. path.move_to({ cx + r, cy });
  54. // 2. arc to cx,cy+r;
  55. path.arc_to({ cx, cy + r }, r, large_arc, sweep);
  56. // 3. arc to cx-r,cy;
  57. path.arc_to({ cx - r, cy }, r, large_arc, sweep);
  58. // 4. arc to cx,cy-r;
  59. path.arc_to({ cx, cy - r }, r, large_arc, sweep);
  60. // 5. arc with a segment-completing close path operation.
  61. path.arc_to({ cx + r, cy }, r, large_arc, sweep);
  62. return path;
  63. }
  64. // https://www.w3.org/TR/SVG11/shapes.html#CircleElementCXAttribute
  65. JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::cx() const
  66. {
  67. // FIXME: Create a proper animated value when animations are supported.
  68. auto make_length = [&] {
  69. if (auto cx = computed_css_values()->length_percentage(CSS::PropertyID::Cx); cx.has_value())
  70. return SVGLength::from_length_percentage(realm(), *cx);
  71. return SVGLength::create(realm(), 0, 0.0f);
  72. };
  73. return SVGAnimatedLength::create(realm(), make_length(), make_length());
  74. }
  75. // https://www.w3.org/TR/SVG11/shapes.html#CircleElementCYAttribute
  76. JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::cy() const
  77. {
  78. // FIXME: Create a proper animated value when animations are supported.
  79. auto make_length = [&] {
  80. if (auto cy = computed_css_values()->length_percentage(CSS::PropertyID::Cy); cy.has_value())
  81. return SVGLength::from_length_percentage(realm(), *cy);
  82. return SVGLength::create(realm(), 0, 0.0f);
  83. };
  84. return SVGAnimatedLength::create(realm(), make_length(), make_length());
  85. }
  86. // https://www.w3.org/TR/SVG11/shapes.html#CircleElementRAttribute
  87. JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::r() const
  88. {
  89. // FIXME: Create a proper animated value when animations are supported.
  90. auto make_length = [&] {
  91. if (auto r = computed_css_values()->length_percentage(CSS::PropertyID::R); r.has_value())
  92. return SVGLength::from_length_percentage(realm(), *r);
  93. return SVGLength::create(realm(), 0, 0.0f);
  94. };
  95. return SVGAnimatedLength::create(realm(), make_length(), make_length());
  96. }
  97. }