SVGPolylineElement.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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/SVGPolylineElement.h>
  10. namespace Web::SVG {
  11. SVGPolylineElement::SVGPolylineElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  12. : SVGGeometryElement(document, qualified_name)
  13. {
  14. set_prototype(&Bindings::cached_web_prototype(realm(), "SVGPolylineElement"));
  15. }
  16. void SVGPolylineElement::parse_attribute(FlyString const& name, String const& value)
  17. {
  18. SVGGeometryElement::parse_attribute(name, value);
  19. if (name == SVG::AttributeNames::points) {
  20. m_points = AttributeParser::parse_points(value);
  21. m_path.clear();
  22. }
  23. }
  24. Gfx::Path& SVGPolylineElement::get_path()
  25. {
  26. if (m_path.has_value())
  27. return m_path.value();
  28. Gfx::Path path;
  29. if (m_points.is_empty()) {
  30. m_path = move(path);
  31. return m_path.value();
  32. }
  33. // 1. perform an absolute moveto operation to the first coordinate pair in the list of points
  34. path.move_to(m_points.first());
  35. // 2. for each subsequent coordinate pair, perform an absolute lineto operation to that coordinate pair.
  36. for (size_t point_index = 1; point_index < m_points.size(); ++point_index)
  37. path.line_to(m_points[point_index]);
  38. m_path = move(path);
  39. return m_path.value();
  40. }
  41. }