2023-04-22 18:51:00 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2024-04-27 12:09:58 +12:00
|
|
|
|
#include <LibWeb/Bindings/SVGLinearGradientElementPrototype.h>
|
2023-04-22 18:51:00 +01:00
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
2024-06-13 15:22:46 +03:00
|
|
|
|
#include <LibWeb/Painting/PaintStyle.h>
|
2023-04-22 18:51:00 +01:00
|
|
|
|
#include <LibWeb/SVG/AttributeNames.h>
|
|
|
|
|
#include <LibWeb/SVG/AttributeParser.h>
|
|
|
|
|
#include <LibWeb/SVG/SVGLinearGradientElement.h>
|
|
|
|
|
#include <LibWeb/SVG/SVGStopElement.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::SVG {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(SVGLinearGradientElement);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
|
2023-04-22 18:51:00 +01:00
|
|
|
|
SVGLinearGradientElement::SVGLinearGradientElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
|
|
|
|
: SVGGradientElement(document, qualified_name)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void SVGLinearGradientElement::initialize(JS::Realm& realm)
|
2023-04-22 18:51:00 +01:00
|
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGLinearGradientElement);
|
2025-04-20 16:22:57 +02:00
|
|
|
|
Base::initialize(realm);
|
2023-04-22 18:51:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 08:14:16 -05:00
|
|
|
|
void SVGLinearGradientElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
|
2023-04-22 18:51:00 +01:00
|
|
|
|
{
|
2024-11-14 08:14:16 -05:00
|
|
|
|
Base::attribute_changed(name, old_value, value, namespace_);
|
2023-04-22 18:51:00 +01:00
|
|
|
|
|
|
|
|
|
// FIXME: Should allow for `<number-percentage> | <length>` for x1, x2, y1, y2
|
|
|
|
|
if (name == SVG::AttributeNames::x1) {
|
2023-11-19 18:10:36 +13:00
|
|
|
|
m_x1 = AttributeParser::parse_number_percentage(value.value_or(String {}));
|
2023-04-22 18:51:00 +01:00
|
|
|
|
} else if (name == SVG::AttributeNames::y1) {
|
2023-11-19 18:10:36 +13:00
|
|
|
|
m_y1 = AttributeParser::parse_number_percentage(value.value_or(String {}));
|
2023-04-22 18:51:00 +01:00
|
|
|
|
} else if (name == SVG::AttributeNames::x2) {
|
2023-11-19 18:10:36 +13:00
|
|
|
|
m_x2 = AttributeParser::parse_number_percentage(value.value_or(String {}));
|
2023-04-22 18:51:00 +01:00
|
|
|
|
} else if (name == SVG::AttributeNames::y2) {
|
2023-11-19 18:10:36 +13:00
|
|
|
|
m_y2 = AttributeParser::parse_number_percentage(value.value_or(String {}));
|
2023-04-22 18:51:00 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementX1Attribute
|
|
|
|
|
NumberPercentage SVGLinearGradientElement::start_x() const
|
2024-03-10 12:18:09 +01:00
|
|
|
|
{
|
|
|
|
|
HashTable<SVGGradientElement const*> seen_gradients;
|
|
|
|
|
return start_x_impl(seen_gradients);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NumberPercentage SVGLinearGradientElement::start_x_impl(HashTable<SVGGradientElement const*>& seen_gradients) const
|
2023-04-22 18:51:00 +01:00
|
|
|
|
{
|
|
|
|
|
if (m_x1.has_value())
|
|
|
|
|
return *m_x1;
|
2024-03-10 12:18:09 +01:00
|
|
|
|
if (auto gradient = linked_linear_gradient(seen_gradients))
|
|
|
|
|
return gradient->start_x_impl(seen_gradients);
|
2023-04-22 18:51:00 +01:00
|
|
|
|
// If the attribute is not specified, the effect is as if a value of '0%' were specified.
|
|
|
|
|
return NumberPercentage::create_percentage(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementY1Attribute
|
|
|
|
|
NumberPercentage SVGLinearGradientElement::start_y() const
|
2024-03-10 12:18:09 +01:00
|
|
|
|
{
|
|
|
|
|
HashTable<SVGGradientElement const*> seen_gradients;
|
|
|
|
|
return start_y_impl(seen_gradients);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NumberPercentage SVGLinearGradientElement::start_y_impl(HashTable<SVGGradientElement const*>& seen_gradients) const
|
2023-04-22 18:51:00 +01:00
|
|
|
|
{
|
|
|
|
|
if (m_y1.has_value())
|
|
|
|
|
return *m_y1;
|
2024-03-10 12:18:09 +01:00
|
|
|
|
if (auto gradient = linked_linear_gradient(seen_gradients))
|
|
|
|
|
return gradient->start_y_impl(seen_gradients);
|
2023-04-22 18:51:00 +01:00
|
|
|
|
// If the attribute is not specified, the effect is as if a value of '0%' were specified.
|
|
|
|
|
return NumberPercentage::create_percentage(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementX2Attribute
|
|
|
|
|
NumberPercentage SVGLinearGradientElement::end_x() const
|
2024-03-10 12:18:09 +01:00
|
|
|
|
{
|
|
|
|
|
HashTable<SVGGradientElement const*> seen_gradients;
|
|
|
|
|
return end_x_impl(seen_gradients);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NumberPercentage SVGLinearGradientElement::end_x_impl(HashTable<SVGGradientElement const*>& seen_gradients) const
|
2023-04-22 18:51:00 +01:00
|
|
|
|
{
|
|
|
|
|
if (m_x2.has_value())
|
|
|
|
|
return *m_x2;
|
2024-03-10 12:18:09 +01:00
|
|
|
|
if (auto gradient = linked_linear_gradient(seen_gradients))
|
|
|
|
|
return gradient->end_x_impl(seen_gradients);
|
2023-04-22 18:51:00 +01:00
|
|
|
|
// If the attribute is not specified, the effect is as if a value of '100%' were specified.
|
|
|
|
|
return NumberPercentage::create_percentage(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementY2Attribute
|
|
|
|
|
NumberPercentage SVGLinearGradientElement::end_y() const
|
2024-03-10 12:18:09 +01:00
|
|
|
|
{
|
|
|
|
|
HashTable<SVGGradientElement const*> seen_gradients;
|
|
|
|
|
return end_y_impl(seen_gradients);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NumberPercentage SVGLinearGradientElement::end_y_impl(HashTable<SVGGradientElement const*>& seen_gradients) const
|
2023-04-22 18:51:00 +01:00
|
|
|
|
{
|
|
|
|
|
if (m_y2.has_value())
|
|
|
|
|
return *m_y2;
|
2024-03-10 12:18:09 +01:00
|
|
|
|
if (auto gradient = linked_linear_gradient(seen_gradients))
|
|
|
|
|
return gradient->end_y_impl(seen_gradients);
|
2023-04-22 18:51:00 +01:00
|
|
|
|
// If the attribute is not specified, the effect is as if a value of '0%' were specified.
|
|
|
|
|
return NumberPercentage::create_percentage(0);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 15:22:46 +03:00
|
|
|
|
Optional<Painting::PaintStyle> SVGLinearGradientElement::to_gfx_paint_style(SVGPaintContext const& paint_context) const
|
2023-04-22 18:51:00 +01:00
|
|
|
|
{
|
|
|
|
|
auto units = gradient_units();
|
|
|
|
|
// FIXME: Resolve percentages properly
|
|
|
|
|
Gfx::FloatPoint start_point {};
|
|
|
|
|
Gfx::FloatPoint end_point {};
|
2024-10-28 19:38:53 +01:00
|
|
|
|
|
2023-04-22 18:51:00 +01:00
|
|
|
|
// https://svgwg.org/svg2-draft/pservers.html#LinearGradientElementGradientUnitsAttribute
|
|
|
|
|
if (units == GradientUnits::ObjectBoundingBox) {
|
|
|
|
|
// If gradientUnits="objectBoundingBox", the user coordinate system for attributes ‘x1’, ‘y1’, ‘x2’ and ‘y2’
|
|
|
|
|
// is established using the bounding box of the element to which the gradient is applied (see Object bounding
|
|
|
|
|
// box units) and then applying the transform specified by attribute ‘gradientTransform’. Percentages represent
|
|
|
|
|
// values relative to the bounding box for the object.
|
|
|
|
|
// Note: For gradientUnits="objectBoundingBox" both "100%" and "1" are treated the same.
|
2023-05-02 23:05:47 +01:00
|
|
|
|
start_point = { start_x().value(), start_y().value() };
|
|
|
|
|
end_point = { end_x().value(), end_y().value() };
|
2023-04-22 18:51:00 +01:00
|
|
|
|
} else {
|
|
|
|
|
// GradientUnits::UserSpaceOnUse
|
|
|
|
|
// If gradientUnits="userSpaceOnUse", ‘x1’, ‘y1’, ‘x2’, and ‘y2’ represent values in the coordinate system
|
|
|
|
|
// that results from taking the current user coordinate system in place at the time when the gradient element
|
|
|
|
|
// is referenced (i.e., the user coordinate system for the element referencing the gradient element via a
|
|
|
|
|
// fill or stroke property) and then applying the transform specified by attribute ‘gradientTransform’.
|
|
|
|
|
// Percentages represent values relative to the current SVG viewport.
|
2024-10-28 19:38:53 +01:00
|
|
|
|
start_point = {
|
2023-04-22 18:51:00 +01:00
|
|
|
|
start_x().resolve_relative_to(paint_context.viewport.width()),
|
|
|
|
|
start_y().resolve_relative_to(paint_context.viewport.height()),
|
|
|
|
|
};
|
2024-10-28 19:38:53 +01:00
|
|
|
|
end_point = {
|
2023-04-22 18:51:00 +01:00
|
|
|
|
end_x().resolve_relative_to(paint_context.viewport.width()),
|
|
|
|
|
end_y().resolve_relative_to(paint_context.viewport.height()),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!m_paint_style) {
|
2024-06-13 15:22:46 +03:00
|
|
|
|
m_paint_style = Painting::SVGLinearGradientPaintStyle::create(start_point, end_point);
|
2023-05-02 23:05:47 +01:00
|
|
|
|
// FIXME: Update stops in DOM changes:
|
|
|
|
|
add_color_stops(*m_paint_style);
|
2023-04-22 18:51:00 +01:00
|
|
|
|
} else {
|
|
|
|
|
m_paint_style->set_start_point(start_point);
|
|
|
|
|
m_paint_style->set_end_point(end_point);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-02 23:05:47 +01:00
|
|
|
|
m_paint_style->set_gradient_transform(gradient_paint_transform(paint_context));
|
2024-06-13 15:22:46 +03:00
|
|
|
|
m_paint_style->set_spread_method(to_painting_spread_method(spread_method()));
|
2023-04-22 18:51:00 +01:00
|
|
|
|
return *m_paint_style;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<SVGAnimatedLength> SVGLinearGradientElement::x1() const
|
2023-04-22 18:51:00 +01:00
|
|
|
|
{
|
2024-03-11 15:34:24 +01:00
|
|
|
|
// FIXME: Implement this properly.
|
|
|
|
|
return SVGAnimatedLength::create(realm(), SVGLength::create(realm(), 0, 0), SVGLength::create(realm(), 0, 0));
|
2023-04-22 18:51:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<SVGAnimatedLength> SVGLinearGradientElement::y1() const
|
2023-04-22 18:51:00 +01:00
|
|
|
|
{
|
2024-03-11 15:34:24 +01:00
|
|
|
|
// FIXME: Implement this properly.
|
|
|
|
|
return SVGAnimatedLength::create(realm(), SVGLength::create(realm(), 0, 0), SVGLength::create(realm(), 0, 0));
|
2023-04-22 18:51:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<SVGAnimatedLength> SVGLinearGradientElement::x2() const
|
2023-04-22 18:51:00 +01:00
|
|
|
|
{
|
2024-03-11 15:34:24 +01:00
|
|
|
|
// FIXME: Implement this properly.
|
|
|
|
|
return SVGAnimatedLength::create(realm(), SVGLength::create(realm(), 0, 0), SVGLength::create(realm(), 0, 0));
|
2023-04-22 18:51:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<SVGAnimatedLength> SVGLinearGradientElement::y2() const
|
2023-04-22 18:51:00 +01:00
|
|
|
|
{
|
2024-03-11 15:34:24 +01:00
|
|
|
|
// FIXME: Implement this properly.
|
|
|
|
|
return SVGAnimatedLength::create(realm(), SVGLength::create(realm(), 0, 0), SVGLength::create(realm(), 0, 0));
|
2023-04-22 18:51:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|