SVGCircleElement.cpp 3.5 KB

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