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/SVGLineElement.h>
11
12namespace Web::SVG {
13
14SVGLineElement::SVGLineElement(DOM::Document& document, DOM::QualifiedName qualified_name)
15 : SVGGeometryElement(document, qualified_name)
16{
17}
18
19JS::ThrowCompletionOr<void> SVGLineElement::initialize(JS::Realm& realm)
20{
21 MUST_OR_THROW_OOM(Base::initialize(realm));
22 set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGLineElementPrototype>(realm, "SVGLineElement"));
23
24 return {};
25}
26
27void SVGLineElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
28{
29 SVGGeometryElement::parse_attribute(name, value);
30
31 if (name == SVG::AttributeNames::x1) {
32 m_x1 = AttributeParser::parse_coordinate(value);
33 m_path.clear();
34 } else if (name == SVG::AttributeNames::y1) {
35 m_y1 = AttributeParser::parse_coordinate(value);
36 m_path.clear();
37 } else if (name == SVG::AttributeNames::x2) {
38 m_x2 = AttributeParser::parse_coordinate(value);
39 m_path.clear();
40 } else if (name == SVG::AttributeNames::y2) {
41 m_y2 = AttributeParser::parse_coordinate(value);
42 m_path.clear();
43 }
44}
45
46Gfx::Path& SVGLineElement::get_path()
47{
48 if (m_path.has_value())
49 return m_path.value();
50
51 Gfx::Path path;
52 float x1 = m_x1.value_or(0);
53 float y1 = m_y1.value_or(0);
54 float x2 = m_x2.value_or(0);
55 float y2 = m_y2.value_or(0);
56
57 // 1. perform an absolute moveto operation to absolute location (x1,y1)
58 path.move_to({ x1, y1 });
59
60 // 2. perform an absolute lineto operation to absolute location (x2,y2)
61 path.line_to({ x2, y2 });
62
63 m_path = move(path);
64 return m_path.value();
65}
66
67// https://www.w3.org/TR/SVG11/shapes.html#LineElementX1Attribute
68JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::x1() const
69{
70 // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
71 // FIXME: Create a proper animated value when animations are supported.
72 auto base_length = SVGLength::create(realm(), 0, m_x1.value_or(0)).release_value_but_fixme_should_propagate_errors();
73 auto anim_length = SVGLength::create(realm(), 0, m_x1.value_or(0)).release_value_but_fixme_should_propagate_errors();
74 return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length)).release_value_but_fixme_should_propagate_errors();
75}
76
77// https://www.w3.org/TR/SVG11/shapes.html#LineElementY1Attribute
78JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::y1() const
79{
80 // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
81 // FIXME: Create a proper animated value when animations are supported.
82 auto base_length = SVGLength::create(realm(), 0, m_y1.value_or(0)).release_value_but_fixme_should_propagate_errors();
83 auto anim_length = SVGLength::create(realm(), 0, m_y1.value_or(0)).release_value_but_fixme_should_propagate_errors();
84 return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length)).release_value_but_fixme_should_propagate_errors();
85}
86
87// https://www.w3.org/TR/SVG11/shapes.html#LineElementX2Attribute
88JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::x2() const
89{
90 // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
91 // FIXME: Create a proper animated value when animations are supported.
92 auto base_length = SVGLength::create(realm(), 0, m_x2.value_or(0)).release_value_but_fixme_should_propagate_errors();
93 auto anim_length = SVGLength::create(realm(), 0, m_x2.value_or(0)).release_value_but_fixme_should_propagate_errors();
94 return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length)).release_value_but_fixme_should_propagate_errors();
95}
96
97// https://www.w3.org/TR/SVG11/shapes.html#LineElementY2Attribute
98JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::y2() const
99{
100 // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
101 // FIXME: Create a proper animated value when animations are supported.
102 auto base_length = SVGLength::create(realm(), 0, m_y2.value_or(0)).release_value_but_fixme_should_propagate_errors();
103 auto anim_length = SVGLength::create(realm(), 0, m_y2.value_or(0)).release_value_but_fixme_should_propagate_errors();
104 return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length)).release_value_but_fixme_should_propagate_errors();
105}
106
107}