2022-02-11 16:56:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2024-08-09 14:00:10 +02:00
|
|
|
#include <LibGfx/Path.h>
|
2022-09-30 17:16:16 -06:00
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2024-04-27 12:09:58 +12:00
|
|
|
#include <LibWeb/Bindings/SVGLineElementPrototype.h>
|
2022-02-11 16:56:36 +00:00
|
|
|
#include <LibWeb/SVG/AttributeNames.h>
|
|
|
|
#include <LibWeb/SVG/AttributeParser.h>
|
2022-08-28 13:42:07 +02:00
|
|
|
#include <LibWeb/SVG/SVGLineElement.h>
|
2022-02-11 16:56:36 +00:00
|
|
|
|
|
|
|
namespace Web::SVG {
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(SVGLineElement);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
2022-02-18 21:00:52 +01:00
|
|
|
SVGLineElement::SVGLineElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2022-02-11 16:56:36 +00:00
|
|
|
: SVGGeometryElement(document, qualified_name)
|
|
|
|
{
|
2023-01-10 06:28:20 -05:00
|
|
|
}
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
void SVGLineElement::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGLineElement);
|
2025-04-20 16:22:57 +02:00
|
|
|
Base::initialize(realm);
|
2022-02-11 16:56:36 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 08:14:16 -05:00
|
|
|
void SVGLineElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
|
2022-02-11 16:56:36 +00:00
|
|
|
{
|
2024-11-14 08:14:16 -05:00
|
|
|
Base::attribute_changed(name, old_value, value, namespace_);
|
2022-02-11 16:56:36 +00:00
|
|
|
|
|
|
|
if (name == SVG::AttributeNames::x1) {
|
2024-07-21 19:21:29 +02:00
|
|
|
m_x1 = AttributeParser::parse_number_percentage(value.value_or(String {}));
|
2022-02-11 16:56:36 +00:00
|
|
|
} else if (name == SVG::AttributeNames::y1) {
|
2024-07-21 19:21:29 +02:00
|
|
|
m_y1 = AttributeParser::parse_number_percentage(value.value_or(String {}));
|
2022-02-11 16:56:36 +00:00
|
|
|
} else if (name == SVG::AttributeNames::x2) {
|
2024-07-21 19:21:29 +02:00
|
|
|
m_x2 = AttributeParser::parse_number_percentage(value.value_or(String {}));
|
2022-02-11 16:56:36 +00:00
|
|
|
} else if (name == SVG::AttributeNames::y2) {
|
2024-07-21 19:21:29 +02:00
|
|
|
m_y2 = AttributeParser::parse_number_percentage(value.value_or(String {}));
|
2022-02-11 16:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 14:00:10 +02:00
|
|
|
Gfx::Path SVGLineElement::get_path(CSSPixelSize viewport_size)
|
2022-02-11 16:56:36 +00:00
|
|
|
{
|
2024-07-21 19:21:29 +02:00
|
|
|
auto const viewport_width = viewport_size.width().to_float();
|
|
|
|
auto const viewport_height = viewport_size.height().to_float();
|
|
|
|
|
2024-08-09 14:00:10 +02:00
|
|
|
Gfx::Path path;
|
2024-07-21 19:21:29 +02:00
|
|
|
float const x1 = m_x1.value_or({ 0, false }).resolve_relative_to(viewport_width);
|
|
|
|
float const y1 = m_y1.value_or({ 0, false }).resolve_relative_to(viewport_height);
|
|
|
|
float const x2 = m_x2.value_or({ 0, false }).resolve_relative_to(viewport_width);
|
|
|
|
float const y2 = m_y2.value_or({ 0, false }).resolve_relative_to(viewport_height);
|
2022-02-11 16:56:36 +00:00
|
|
|
|
|
|
|
// 1. perform an absolute moveto operation to absolute location (x1,y1)
|
|
|
|
path.move_to({ x1, y1 });
|
|
|
|
|
|
|
|
// 2. perform an absolute lineto operation to absolute location (x2,y2)
|
|
|
|
path.line_to({ x2, y2 });
|
|
|
|
|
2024-03-03 20:15:06 +00:00
|
|
|
return path;
|
2022-02-11 16:56:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 16:31:52 +00:00
|
|
|
// https://www.w3.org/TR/SVG11/shapes.html#LineElementX1Attribute
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<SVGAnimatedLength> SVGLineElement::x1() const
|
2022-03-22 16:31:52 +00:00
|
|
|
{
|
|
|
|
// FIXME: Populate the unit type when it is parsed (0 here is "unknown").
|
|
|
|
// FIXME: Create a proper animated value when animations are supported.
|
2024-07-21 19:21:29 +02:00
|
|
|
auto base_length = SVGLength::create(realm(), 0, m_x1.value_or({ 0, false }).value());
|
|
|
|
auto anim_length = SVGLength::create(realm(), 0, m_x1.value_or({ 0, false }).value());
|
2023-08-13 13:05:26 +02:00
|
|
|
return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
|
2022-03-22 16:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/shapes.html#LineElementY1Attribute
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<SVGAnimatedLength> SVGLineElement::y1() const
|
2022-03-22 16:31:52 +00:00
|
|
|
{
|
|
|
|
// FIXME: Populate the unit type when it is parsed (0 here is "unknown").
|
|
|
|
// FIXME: Create a proper animated value when animations are supported.
|
2024-07-21 19:21:29 +02:00
|
|
|
auto base_length = SVGLength::create(realm(), 0, m_y1.value_or({ 0, false }).value());
|
|
|
|
auto anim_length = SVGLength::create(realm(), 0, m_y1.value_or({ 0, false }).value());
|
2023-08-13 13:05:26 +02:00
|
|
|
return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
|
2022-03-22 16:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/shapes.html#LineElementX2Attribute
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<SVGAnimatedLength> SVGLineElement::x2() const
|
2022-03-22 16:31:52 +00:00
|
|
|
{
|
|
|
|
// FIXME: Populate the unit type when it is parsed (0 here is "unknown").
|
|
|
|
// FIXME: Create a proper animated value when animations are supported.
|
2024-07-21 19:21:29 +02:00
|
|
|
auto base_length = SVGLength::create(realm(), 0, m_x2.value_or({ 0, false }).value());
|
|
|
|
auto anim_length = SVGLength::create(realm(), 0, m_x2.value_or({ 0, false }).value());
|
2023-08-13 13:05:26 +02:00
|
|
|
return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
|
2022-03-22 16:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/shapes.html#LineElementY2Attribute
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<SVGAnimatedLength> SVGLineElement::y2() const
|
2022-03-22 16:31:52 +00:00
|
|
|
{
|
|
|
|
// FIXME: Populate the unit type when it is parsed (0 here is "unknown").
|
|
|
|
// FIXME: Create a proper animated value when animations are supported.
|
2024-07-21 19:21:29 +02:00
|
|
|
auto base_length = SVGLength::create(realm(), 0, m_y2.value_or({ 0, false }).value());
|
|
|
|
auto anim_length = SVGLength::create(realm(), 0, m_y2.value_or({ 0, false }).value());
|
2023-08-13 13:05:26 +02:00
|
|
|
return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
|
2022-03-22 16:31:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 16:56:36 +00:00
|
|
|
}
|