SVGPolygonElement.cpp 1.7 KB

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