Serenity Operating System
1/*
2 * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibWeb/Bindings/Intrinsics.h>
8#include <LibWeb/SVG/AttributeNames.h>
9#include <LibWeb/SVG/AttributeParser.h>
10#include <LibWeb/SVG/SVGPolygonElement.h>
11
12namespace Web::SVG {
13
14SVGPolygonElement::SVGPolygonElement(DOM::Document& document, DOM::QualifiedName qualified_name)
15 : SVGGeometryElement(document, qualified_name)
16{
17}
18
19JS::ThrowCompletionOr<void> SVGPolygonElement::initialize(JS::Realm& realm)
20{
21 MUST_OR_THROW_OOM(Base::initialize(realm));
22 set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGPolygonElementPrototype>(realm, "SVGPolygonElement"));
23
24 return {};
25}
26
27void SVGPolygonElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
28{
29 SVGGeometryElement::parse_attribute(name, value);
30
31 if (name == SVG::AttributeNames::points) {
32 m_points = AttributeParser::parse_points(value);
33 m_path.clear();
34 }
35}
36
37Gfx::Path& SVGPolygonElement::get_path()
38{
39 if (m_path.has_value())
40 return m_path.value();
41
42 Gfx::Path path;
43
44 if (m_points.is_empty()) {
45 m_path = move(path);
46 return m_path.value();
47 }
48
49 // 1. perform an absolute moveto operation to the first coordinate pair in the list of points
50 path.move_to(m_points.first());
51
52 // 2. for each subsequent coordinate pair, perform an absolute lineto operation to that coordinate pair.
53 for (size_t point_index = 1; point_index < m_points.size(); ++point_index)
54 path.line_to(m_points[point_index]);
55
56 // 3. perform a closepath command
57 path.close();
58
59 m_path = move(path);
60 return m_path.value();
61}
62
63}