SVGPolygonElement.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. SVGPolygonElement::SVGPolygonElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  12. : SVGGeometryElement(document, qualified_name)
  13. {
  14. }
  15. void SVGPolygonElement::initialize(JS::Realm& realm)
  16. {
  17. Base::initialize(realm);
  18. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGPolygonElementPrototype>(realm, "SVGPolygonElement"));
  19. }
  20. void SVGPolygonElement::attribute_changed(DeprecatedFlyString const& name, DeprecatedString const& value)
  21. {
  22. SVGGeometryElement::attribute_changed(name, value);
  23. if (name == SVG::AttributeNames::points) {
  24. m_points = AttributeParser::parse_points(value);
  25. m_path.clear();
  26. }
  27. }
  28. Gfx::Path& SVGPolygonElement::get_path()
  29. {
  30. if (m_path.has_value())
  31. return m_path.value();
  32. Gfx::Path path;
  33. if (m_points.is_empty()) {
  34. m_path = move(path);
  35. return m_path.value();
  36. }
  37. // 1. perform an absolute moveto operation to the first coordinate pair in the list of points
  38. path.move_to(m_points.first());
  39. // 2. for each subsequent coordinate pair, perform an absolute lineto operation to that coordinate pair.
  40. for (size_t point_index = 1; point_index < m_points.size(); ++point_index)
  41. path.line_to(m_points[point_index]);
  42. // 3. perform a closepath command
  43. path.close();
  44. m_path = move(path);
  45. return m_path.value();
  46. }
  47. }