SVGPolygonElement.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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/SVGPolygonElement.h>
  10. namespace Web::SVG {
  11. JS_DEFINE_ALLOCATOR(SVGPolygonElement);
  12. SVGPolygonElement::SVGPolygonElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  13. : SVGGeometryElement(document, qualified_name)
  14. {
  15. }
  16. void SVGPolygonElement::initialize(JS::Realm& realm)
  17. {
  18. Base::initialize(realm);
  19. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGPolygonElement);
  20. }
  21. void SVGPolygonElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  22. {
  23. SVGGeometryElement::attribute_changed(name, value);
  24. if (name == SVG::AttributeNames::points)
  25. m_points = AttributeParser::parse_points(value.value_or(String {}));
  26. }
  27. Gfx::Path SVGPolygonElement::get_path(CSSPixelSize)
  28. {
  29. Gfx::Path path;
  30. if (m_points.is_empty())
  31. return path;
  32. // 1. perform an absolute moveto operation to the first coordinate pair in the list of points
  33. path.move_to(m_points.first());
  34. // 2. for each subsequent coordinate pair, perform an absolute lineto operation to that coordinate pair.
  35. for (size_t point_index = 1; point_index < m_points.size(); ++point_index)
  36. path.line_to(m_points[point_index]);
  37. // 3. perform a closepath command
  38. path.close();
  39. return path;
  40. }
  41. }