2022-02-11 15:52:42 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2024-08-09 12:00:10 +00:00
|
|
|
|
#include <LibGfx/Path.h>
|
2022-09-26 00:04:39 +00:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2024-04-27 00:09:58 +00:00
|
|
|
|
#include <LibWeb/Bindings/SVGRectElementPrototype.h>
|
2022-02-11 15:52:42 +00:00
|
|
|
|
#include <LibWeb/SVG/AttributeNames.h>
|
|
|
|
|
#include <LibWeb/SVG/AttributeParser.h>
|
2022-03-21 18:34:02 +00:00
|
|
|
|
#include <LibWeb/SVG/SVGAnimatedLength.h>
|
|
|
|
|
#include <LibWeb/SVG/SVGLength.h>
|
2022-09-03 16:43:24 +00:00
|
|
|
|
#include <LibWeb/SVG/SVGRectElement.h>
|
2022-02-11 15:52:42 +00:00
|
|
|
|
|
|
|
|
|
namespace Web::SVG {
|
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
|
GC_DEFINE_ALLOCATOR(SVGRectElement);
|
2023-11-19 18:47:52 +00:00
|
|
|
|
|
2022-02-18 20:00:52 +00:00
|
|
|
|
SVGRectElement::SVGRectElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2022-02-11 15:52:42 +00:00
|
|
|
|
: SVGGeometryElement(document, qualified_name)
|
|
|
|
|
{
|
2023-01-10 11:28:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 06:41:28 +00:00
|
|
|
|
void SVGRectElement::initialize(JS::Realm& realm)
|
2023-01-10 11:28:20 +00:00
|
|
|
|
{
|
2023-08-07 06:41:28 +00:00
|
|
|
|
Base::initialize(realm);
|
2024-03-16 12:13:08 +00:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGRectElement);
|
2022-02-11 15:52:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 13:14:16 +00:00
|
|
|
|
void SVGRectElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
|
2022-02-11 15:52:42 +00:00
|
|
|
|
{
|
2024-11-14 13:14:16 +00:00
|
|
|
|
Base::attribute_changed(name, old_value, value, namespace_);
|
2022-02-11 15:52:42 +00:00
|
|
|
|
|
|
|
|
|
if (name == SVG::AttributeNames::x) {
|
2023-11-19 05:10:36 +00:00
|
|
|
|
m_x = AttributeParser::parse_coordinate(value.value_or(String {}));
|
2022-02-11 15:52:42 +00:00
|
|
|
|
} else if (name == SVG::AttributeNames::y) {
|
2023-11-19 05:10:36 +00:00
|
|
|
|
m_y = AttributeParser::parse_coordinate(value.value_or(String {}));
|
2022-02-11 15:52:42 +00:00
|
|
|
|
} else if (name == SVG::AttributeNames::width) {
|
2023-11-19 05:10:36 +00:00
|
|
|
|
m_width = AttributeParser::parse_positive_length(value.value_or(String {}));
|
2022-02-11 15:52:42 +00:00
|
|
|
|
} else if (name == SVG::AttributeNames::height) {
|
2023-11-19 05:10:36 +00:00
|
|
|
|
m_height = AttributeParser::parse_positive_length(value.value_or(String {}));
|
2022-02-11 15:52:42 +00:00
|
|
|
|
} else if (name == SVG::AttributeNames::rx) {
|
2023-11-19 05:10:36 +00:00
|
|
|
|
m_radius_x = AttributeParser::parse_length(value.value_or(String {}));
|
2022-02-11 15:52:42 +00:00
|
|
|
|
} else if (name == SVG::AttributeNames::ry) {
|
2023-11-19 05:10:36 +00:00
|
|
|
|
m_radius_y = AttributeParser::parse_length(value.value_or(String {}));
|
2022-02-11 15:52:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-09 12:00:10 +00:00
|
|
|
|
Gfx::Path SVGRectElement::get_path(CSSPixelSize)
|
2022-02-11 15:52:42 +00:00
|
|
|
|
{
|
|
|
|
|
float width = m_width.value_or(0);
|
|
|
|
|
float height = m_height.value_or(0);
|
|
|
|
|
float x = m_x.value_or(0);
|
|
|
|
|
float y = m_y.value_or(0);
|
|
|
|
|
|
2024-08-09 12:00:10 +00:00
|
|
|
|
Gfx::Path path;
|
2022-02-11 15:52:42 +00:00
|
|
|
|
// If width or height is zero, rendering is disabled.
|
2024-03-03 20:15:06 +00:00
|
|
|
|
if (width == 0 || height == 0)
|
|
|
|
|
return path;
|
2022-02-11 15:52:42 +00:00
|
|
|
|
|
|
|
|
|
auto corner_radii = calculate_used_corner_radius_values();
|
2023-02-10 09:52:14 +00:00
|
|
|
|
float rx = corner_radii.width();
|
|
|
|
|
float ry = corner_radii.height();
|
2022-02-11 15:52:42 +00:00
|
|
|
|
|
|
|
|
|
// 1. perform an absolute moveto operation to location (x+rx,y);
|
|
|
|
|
path.move_to({ x + rx, y });
|
|
|
|
|
|
|
|
|
|
// 2, perform an absolute horizontal lineto with parameter x+width-rx;
|
|
|
|
|
path.horizontal_line_to(x + width - rx);
|
|
|
|
|
|
|
|
|
|
// 3. if both rx and ry are greater than zero,
|
|
|
|
|
// perform an absolute elliptical arc operation to coordinate (x+width,y+ry),
|
|
|
|
|
// where rx and ry are used as the equivalent parameters to the elliptical arc command,
|
|
|
|
|
// the x-axis-rotation and large-arc-flag are set to zero,
|
|
|
|
|
// the sweep-flag is set to one;
|
|
|
|
|
double x_axis_rotation = 0;
|
|
|
|
|
bool large_arc_flag = false;
|
|
|
|
|
bool sweep_flag = true;
|
|
|
|
|
if (rx > 0 && ry > 0)
|
|
|
|
|
path.elliptical_arc_to({ x + width, y + ry }, corner_radii, x_axis_rotation, large_arc_flag, sweep_flag);
|
|
|
|
|
|
|
|
|
|
// 4. perform an absolute vertical lineto parameter y+height-ry;
|
|
|
|
|
path.vertical_line_to(y + height - ry);
|
|
|
|
|
|
|
|
|
|
// 5. if both rx and ry are greater than zero,
|
|
|
|
|
// perform an absolute elliptical arc operation to coordinate (x+width-rx,y+height),
|
|
|
|
|
// using the same parameters as previously;
|
|
|
|
|
if (rx > 0 && ry > 0)
|
|
|
|
|
path.elliptical_arc_to({ x + width - rx, y + height }, corner_radii, x_axis_rotation, large_arc_flag, sweep_flag);
|
|
|
|
|
|
|
|
|
|
// 6. perform an absolute horizontal lineto parameter x+rx;
|
|
|
|
|
path.horizontal_line_to(x + rx);
|
|
|
|
|
|
|
|
|
|
// 7. if both rx and ry are greater than zero,
|
|
|
|
|
// perform an absolute elliptical arc operation to coordinate (x,y+height-ry),
|
|
|
|
|
// using the same parameters as previously;
|
|
|
|
|
if (rx > 0 && ry > 0)
|
|
|
|
|
path.elliptical_arc_to({ x, y + height - ry }, corner_radii, x_axis_rotation, large_arc_flag, sweep_flag);
|
|
|
|
|
|
|
|
|
|
// 8. perform an absolute vertical lineto parameter y+ry
|
|
|
|
|
path.vertical_line_to(y + ry);
|
|
|
|
|
|
|
|
|
|
// 9. if both rx and ry are greater than zero,
|
|
|
|
|
// perform an absolute elliptical arc operation with a segment-completing close path operation,
|
|
|
|
|
// using the same parameters as previously.
|
|
|
|
|
if (rx > 0 && ry > 0)
|
|
|
|
|
path.elliptical_arc_to({ x + rx, y }, corner_radii, x_axis_rotation, large_arc_flag, sweep_flag);
|
|
|
|
|
|
2024-08-09 12:00:10 +00:00
|
|
|
|
path.close();
|
|
|
|
|
|
2024-03-03 20:15:06 +00:00
|
|
|
|
return path;
|
2022-02-11 15:52:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 09:52:14 +00:00
|
|
|
|
Gfx::FloatSize SVGRectElement::calculate_used_corner_radius_values() const
|
2022-02-11 15:52:42 +00:00
|
|
|
|
{
|
|
|
|
|
// 1. Let rx and ry be length values.
|
|
|
|
|
float rx = 0;
|
|
|
|
|
float ry = 0;
|
|
|
|
|
|
|
|
|
|
// 2. If neither ‘rx’ nor ‘ry’ are properly specified, then set both rx and ry to 0. (This will result in square corners.)
|
|
|
|
|
if (!m_radius_x.has_value() && !m_radius_y.has_value()) {
|
|
|
|
|
rx = 0;
|
|
|
|
|
ry = 0;
|
|
|
|
|
}
|
|
|
|
|
// 3. Otherwise, if a properly specified value is provided for ‘rx’, but not for ‘ry’, then set both rx and ry to the value of ‘rx’.
|
2023-08-05 00:45:50 +00:00
|
|
|
|
else if (m_radius_x.has_value() && !m_radius_y.has_value()) {
|
2022-02-11 15:52:42 +00:00
|
|
|
|
rx = m_radius_x.value();
|
|
|
|
|
ry = m_radius_x.value();
|
|
|
|
|
}
|
|
|
|
|
// 4. Otherwise, if a properly specified value is provided for ‘ry’, but not for ‘rx’, then set both rx and ry to the value of ‘ry’.
|
2023-08-05 00:45:50 +00:00
|
|
|
|
else if (m_radius_y.has_value() && !m_radius_x.has_value()) {
|
2022-02-11 15:52:42 +00:00
|
|
|
|
rx = m_radius_y.value();
|
|
|
|
|
ry = m_radius_y.value();
|
|
|
|
|
}
|
|
|
|
|
// 5. Otherwise, both ‘rx’ and ‘ry’ were specified properly. Set rx to the value of ‘rx’ and ry to the value of ‘ry’.
|
|
|
|
|
else {
|
|
|
|
|
rx = m_radius_x.value();
|
|
|
|
|
ry = m_radius_y.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 6. If rx is greater than half of ‘width’, then set rx to half of ‘width’.
|
|
|
|
|
auto half_width = m_width.value_or(0) / 2;
|
|
|
|
|
if (rx > half_width)
|
|
|
|
|
rx = half_width;
|
|
|
|
|
|
|
|
|
|
// 7. If ry is greater than half of ‘height’, then set ry to half of ‘height’.
|
|
|
|
|
auto half_height = m_height.value_or(0) / 2;
|
|
|
|
|
if (ry > half_height)
|
|
|
|
|
ry = half_height;
|
|
|
|
|
|
|
|
|
|
// 8. The effective values of ‘rx’ and ‘ry’ are rx and ry, respectively.
|
2023-02-10 09:52:14 +00:00
|
|
|
|
return { rx, ry };
|
2022-02-11 15:52:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-21 18:34:02 +00:00
|
|
|
|
// https://www.w3.org/TR/SVG11/shapes.html#RectElementXAttribute
|
2024-11-14 15:01:23 +00:00
|
|
|
|
GC::Ref<SVGAnimatedLength> SVGRectElement::x() const
|
2022-03-21 18:34:02 +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.
|
2023-08-13 11:05:26 +00:00
|
|
|
|
auto base_length = SVGLength::create(realm(), 0, m_x.value_or(0));
|
|
|
|
|
auto anim_length = SVGLength::create(realm(), 0, m_x.value_or(0));
|
|
|
|
|
return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
|
2022-03-21 18:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/shapes.html#RectElementYAttribute
|
2024-11-14 15:01:23 +00:00
|
|
|
|
GC::Ref<SVGAnimatedLength> SVGRectElement::y() const
|
2022-03-21 18:34:02 +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.
|
2023-08-13 11:05:26 +00:00
|
|
|
|
auto base_length = SVGLength::create(realm(), 0, m_y.value_or(0));
|
|
|
|
|
auto anim_length = SVGLength::create(realm(), 0, m_y.value_or(0));
|
|
|
|
|
return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
|
2022-03-21 18:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/shapes.html#RectElementWidthAttribute
|
2024-11-14 15:01:23 +00:00
|
|
|
|
GC::Ref<SVGAnimatedLength> SVGRectElement::width() const
|
2022-03-21 18:34:02 +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.
|
2023-08-13 11:05:26 +00:00
|
|
|
|
auto base_length = SVGLength::create(realm(), 0, m_width.value_or(0));
|
|
|
|
|
auto anim_length = SVGLength::create(realm(), 0, m_width.value_or(0));
|
|
|
|
|
return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
|
2022-03-21 18:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/shapes.html#RectElementHeightAttribute
|
2024-11-14 15:01:23 +00:00
|
|
|
|
GC::Ref<SVGAnimatedLength> SVGRectElement::height() const
|
2022-03-21 18:34:02 +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.
|
2023-08-13 11:05:26 +00:00
|
|
|
|
auto base_length = SVGLength::create(realm(), 0, m_height.value_or(0));
|
|
|
|
|
auto anim_length = SVGLength::create(realm(), 0, m_height.value_or(0));
|
|
|
|
|
return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
|
2022-03-21 18:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/shapes.html#RectElementRXAttribute
|
2024-11-14 15:01:23 +00:00
|
|
|
|
GC::Ref<SVGAnimatedLength> SVGRectElement::rx() const
|
2022-03-21 18:34:02 +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.
|
2023-08-13 11:05:26 +00:00
|
|
|
|
auto base_length = SVGLength::create(realm(), 0, m_radius_x.value_or(0));
|
|
|
|
|
auto anim_length = SVGLength::create(realm(), 0, m_radius_x.value_or(0));
|
|
|
|
|
return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
|
2022-03-21 18:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/shapes.html#RectElementRYAttribute
|
2024-11-14 15:01:23 +00:00
|
|
|
|
GC::Ref<SVGAnimatedLength> SVGRectElement::ry() const
|
2022-03-21 18:34:02 +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.
|
2023-08-13 11:05:26 +00:00
|
|
|
|
auto base_length = SVGLength::create(realm(), 0, m_radius_y.value_or(0));
|
|
|
|
|
auto anim_length = SVGLength::create(realm(), 0, m_radius_y.value_or(0));
|
|
|
|
|
return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
|
2022-03-21 18:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 15:52:42 +00:00
|
|
|
|
}
|