2023-03-24 16:35:18 +00:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2023-03-24 16:35:18 +00:00
|
|
|
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
|
2025-02-21 16:45:07 +00:00
|
|
|
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
|
2023-03-24 16:35:18 +00:00
|
|
|
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "RadialGradientStyleValue.h"
|
2023-11-07 12:11:04 +00:00
|
|
|
#include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
|
2025-12-11 01:15:17 +13:00
|
|
|
#include <LibWeb/CSS/StyleValues/RadialSizeStyleValue.h>
|
2023-08-16 12:45:23 +01:00
|
|
|
#include <LibWeb/Layout/Node.h>
|
2025-02-02 15:55:26 +01:00
|
|
|
#include <LibWeb/Painting/DisplayListRecorder.h>
|
2023-03-24 16:35:18 +00:00
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
2026-01-08 12:02:18 +00:00
|
|
|
void RadialGradientStyleValue::serialize(StringBuilder& builder, SerializationMode mode) const
|
2023-03-24 16:35:18 +00:00
|
|
|
{
|
|
|
|
|
if (is_repeating())
|
2023-08-22 14:08:15 +01:00
|
|
|
builder.append("repeating-"sv);
|
2025-04-08 13:50:50 -04:00
|
|
|
builder.append("radial-gradient("sv);
|
2025-02-19 21:02:12 +11:00
|
|
|
|
2026-01-08 12:02:18 +00:00
|
|
|
// AD-HOC: We need to check the serialized size to determine if it should be included.
|
2025-12-11 01:15:17 +13:00
|
|
|
auto const& serialized_size = m_properties.size->to_string(mode);
|
|
|
|
|
|
|
|
|
|
bool has_size = serialized_size != "farthest-corner"sv;
|
2025-12-24 23:05:32 +13:00
|
|
|
bool has_position = !m_properties.position->is_center(mode);
|
2026-03-18 21:51:26 +13:00
|
|
|
bool has_color_space = m_properties.color_interpolation_method && m_properties.color_interpolation_method->as_color_interpolation_method().color_interpolation_method() != ColorInterpolationMethodStyleValue::default_color_interpolation_method(m_properties.color_syntax);
|
2023-03-24 16:35:18 +00:00
|
|
|
|
2025-12-11 01:15:17 +13:00
|
|
|
if (has_size)
|
2026-01-08 12:02:18 +00:00
|
|
|
m_properties.size->serialize(builder, mode);
|
2023-03-24 16:35:18 +00:00
|
|
|
|
2025-02-19 21:02:12 +11:00
|
|
|
if (has_position) {
|
|
|
|
|
if (has_size)
|
|
|
|
|
builder.append(' ');
|
|
|
|
|
|
2026-01-08 12:02:18 +00:00
|
|
|
builder.append("at "sv);
|
|
|
|
|
m_properties.position->serialize(builder, mode);
|
2025-02-19 21:02:12 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (has_color_space) {
|
|
|
|
|
if (has_size || has_position)
|
|
|
|
|
builder.append(' ');
|
|
|
|
|
|
2026-03-18 21:51:26 +13:00
|
|
|
m_properties.color_interpolation_method->serialize(builder, mode);
|
2025-02-19 21:02:12 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (has_size || has_position || has_color_space)
|
|
|
|
|
builder.append(", "sv);
|
2023-03-24 16:35:18 +00:00
|
|
|
|
2024-12-07 00:59:49 +01:00
|
|
|
serialize_color_stop_list(builder, m_properties.color_stop_list, mode);
|
2023-08-22 14:08:15 +01:00
|
|
|
builder.append(')');
|
2023-03-24 16:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-11 01:15:17 +13:00
|
|
|
CSSPixelSize RadialGradientStyleValue::resolve_size(CSSPixelPoint center, CSSPixelRect const& reference_box, Layout::NodeWithStyle const& node) const
|
2023-03-24 16:35:18 +00:00
|
|
|
{
|
2025-12-11 01:15:17 +13:00
|
|
|
if (m_properties.ending_shape == EndingShape::Circle) {
|
|
|
|
|
auto radius = m_properties.size->as_radial_size().resolve_circle_size(center, reference_box, node);
|
|
|
|
|
return CSSPixelSize { radius, radius };
|
2023-03-24 16:35:18 +00:00
|
|
|
}
|
2025-12-11 01:15:17 +13:00
|
|
|
|
|
|
|
|
return m_properties.size->as_radial_size().resolve_ellipse_size(center, reference_box, node);
|
2023-03-24 16:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-18 16:13:37 +00:00
|
|
|
void RadialGradientStyleValue::resolve_for_size(Layout::NodeWithStyle const& node, CSSPixelSize paint_size) const
|
2023-03-24 16:35:18 +00:00
|
|
|
{
|
|
|
|
|
CSSPixelRect gradient_box { { 0, 0 }, paint_size };
|
2023-11-07 12:11:04 +00:00
|
|
|
auto center = m_properties.position->resolved(node, gradient_box);
|
2025-12-11 01:15:17 +13:00
|
|
|
auto gradient_size = resolve_size(center, gradient_box, node);
|
2025-02-21 16:45:07 +00:00
|
|
|
|
|
|
|
|
ResolvedDataCacheKey cache_key {
|
|
|
|
|
.length_resolution_context = Length::ResolutionContext::for_layout_node(node),
|
|
|
|
|
.size = paint_size,
|
2023-03-24 16:35:18 +00:00
|
|
|
};
|
2025-02-21 16:45:07 +00:00
|
|
|
if (m_resolved_data_cache_key != cache_key) {
|
|
|
|
|
m_resolved_data_cache_key = move(cache_key);
|
|
|
|
|
m_resolved = ResolvedData {
|
|
|
|
|
Painting::resolve_radial_gradient_data(node, gradient_size, *this),
|
|
|
|
|
gradient_size,
|
|
|
|
|
center,
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-03-24 16:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-20 17:16:15 +00:00
|
|
|
ValueComparingNonnullRefPtr<StyleValue const> RadialGradientStyleValue::absolutized(ComputationContext const& context) const
|
|
|
|
|
{
|
|
|
|
|
Vector<ColorStopListElement> absolutized_color_stops;
|
|
|
|
|
absolutized_color_stops.ensure_capacity(m_properties.color_stop_list.size());
|
|
|
|
|
for (auto const& color_stop : m_properties.color_stop_list) {
|
|
|
|
|
absolutized_color_stops.unchecked_append(color_stop.absolutized(context));
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 01:15:17 +13:00
|
|
|
auto absolutized_size = m_properties.size->absolutized(context);
|
2025-11-20 17:16:15 +00:00
|
|
|
NonnullRefPtr absolutized_position = m_properties.position->absolutized(context)->as_position();
|
|
|
|
|
|
2026-03-18 21:51:26 +13:00
|
|
|
auto absolutized_color_interpolation_method = m_properties.color_interpolation_method ? ValueComparingRefPtr<StyleValue const> { m_properties.color_interpolation_method->absolutized(context) } : nullptr;
|
|
|
|
|
|
|
|
|
|
return create(m_properties.ending_shape, move(absolutized_size), move(absolutized_position), move(absolutized_color_stops), m_properties.repeating, move(absolutized_color_interpolation_method));
|
2025-11-20 17:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-08 10:11:51 +01:00
|
|
|
bool RadialGradientStyleValue::equals(StyleValue const& other) const
|
2023-03-24 16:35:18 +00:00
|
|
|
{
|
|
|
|
|
if (type() != other.type())
|
|
|
|
|
return false;
|
|
|
|
|
auto& other_gradient = other.as_radial_gradient();
|
|
|
|
|
return m_properties == other_gradient.m_properties;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-31 23:07:26 +02:00
|
|
|
void RadialGradientStyleValue::paint(DisplayListRecordingContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
|
2023-03-24 16:35:18 +00:00
|
|
|
{
|
|
|
|
|
VERIFY(m_resolved.has_value());
|
2024-03-03 15:49:53 +01:00
|
|
|
auto center = context.rounded_device_point(m_resolved->center).to_type<int>();
|
|
|
|
|
auto size = context.rounded_device_size(m_resolved->gradient_size).to_type<int>();
|
2024-08-06 15:26:47 +03:00
|
|
|
context.display_list_recorder().fill_rect_with_radial_gradient(dest_rect.to_type<int>(), m_resolved->data, center, size);
|
2023-03-24 16:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|