SVGCircleElement.cpp 3.5 KB

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