dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / style_values / linear_gradient_style_value.lucb
#==============================================================================================
#
# linear_gradient_style_value - LinearGradientStyleValue: linear-gradient() and its repeating and -webkit- forms
#
# DESCRIPTION:
# Port of LibWeb/CSS/StyleValues/LinearGradientStyleValue.cpp (lines 1-153) and the inline
# members of LinearGradientStyleValue.h.
#
# Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
# Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
# Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# Note: The sides must be before the corners in this enum (as this order is used in parsing).
# mark: Creation ================================================================================
## Ported from Libraries/LibWeb/CSS/StyleValues/LinearGradientStyleValue.h:41 Web::CSS::LinearGradientStyleValue::create.
pub func css_linear_gradient_style_value_create(direction: CssLinearGradientStyleValueGradientDirection, color_stop_list: ak.Vector[CssColorStopListElement], type_: CssLinearGradientStyleValueGradientType, repeating: CssGradientRepeating, color_interpolation_method: CssStyleValue*?) -> CssLinearGradientStyleValue*:
assert(not ak.vector_is_empty(&color_stop_list))
let any_non_legacy = css_color_stop_list_has_non_legacy_color(&color_stop_list)
let this = &(new CssLinearGradientStyleValue[1] --- else trap("out of memory"))[0]
css_linear_gradient_style_value_construct(this, direction, color_stop_list, type_, repeating, color_interpolation_method, CssColorSyntax.modern if any_non_legacy else CssColorSyntax.legacy)
return this
## Ported from Libraries/LibWeb/CSS/StyleValues/LinearGradientStyleValue.h:90 Web::CSS::LinearGradientStyleValue::LinearGradientStyleValue.
pub func css_linear_gradient_style_value_construct(this: CssLinearGradientStyleValue*, direction: CssLinearGradientStyleValueGradientDirection, color_stop_list: ak.Vector[CssColorStopListElement], type_: CssLinearGradientStyleValueGradientType, repeating: CssGradientRepeating, color_interpolation_method: CssStyleValue*?, color_syntax: CssColorSyntax):
css_style_value_construct(this.css_style_value(), CssStyleValueType.linear_gradient)
css_style_value_set_class(this.css_style_value(), (const CssStyleValueVTable*)&css_linear_gradient_style_value_vtable, &css_linear_gradient_style_value_class)
this.m_properties = CssLinearGradientStyleValueProperties(direction = direction, color_stop_list = color_stop_list, gradient_type = type_, repeating = repeating, color_interpolation_method = color_interpolation_method, color_syntax = color_syntax)
this.m_resolved_data_cache_key = none
this.m_resolved = none
# mark: Serialization and equality ==============================================================
## The `side_or_corner_to_string` lambda of LinearGradientStyleValue::serialize.
func css_side_or_corner_to_string(value: CssSideOrCorner) -> str:
match value:
.top:
return "top"
.bottom:
return "bottom"
.left:
return "left"
.right:
return "right"
.top_left:
return "left top"
.top_right:
return "right top"
.bottom_left:
return "left bottom"
.bottom_right:
return "right bottom"
_:
trap("VERIFY_NOT_REACHED")
## `Variant<NonnullRefPtr<StyleValue const>, SideOrCorner>`'s `==`: the same alternative, and the same
## value pointer or side.
func css_gradient_directions_equal(a: CssLinearGradientStyleValueGradientDirection, b: CssLinearGradientStyleValueGradientDirection) -> bool:
match a:
.css_style_value(left):
match b:
.css_style_value(right):
return left == right
_:
return false
.css_side_or_corner(left):
match b:
.css_side_or_corner(right):
return left == right
_:
return false
## Ported from Libraries/LibWeb/CSS/StyleValues/LinearGradientStyleValue.cpp:18 Web::CSS::LinearGradientStyleValue::serialize (override).
pub func css_linear_gradient_style_value_serialize_impl(base: CssStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
let this = (CssLinearGradientStyleValue*)base
let default_direction = CssSideOrCorner.top if this.m_properties.gradient_type == CssLinearGradientStyleValueGradientType.web_kit else CssSideOrCorner.bottom
let has_direction = not css_gradient_directions_equal(this.m_properties.direction, .css_side_or_corner(default_direction))
var has_color_space = false
if let method = this.m_properties.color_interpolation_method:
has_color_space = css_color_interpolation_method_style_value_color_interpolation_method(css_style_value_as_color_interpolation_method(method)) != css_color_interpolation_method_style_value_default_color_interpolation_method(this.m_properties.color_syntax)
if this.m_properties.gradient_type == CssLinearGradientStyleValueGradientType.web_kit:
ak.string_builder_append(builder, "-webkit-")
if css_linear_gradient_style_value_is_repeating(this):
ak.string_builder_append(builder, "repeating-")
ak.string_builder_append(builder, "linear-gradient(")
if has_direction:
match this.m_properties.direction:
.css_side_or_corner(side_or_corner):
css_appendff(builder, "{}{}", [ak.arg_str("to " if this.m_properties.gradient_type == CssLinearGradientStyleValueGradientType.standard else ""), ak.arg_str(css_side_or_corner_to_string(side_or_corner))])
.css_style_value(angle):
css_style_value_serialize(angle, builder, mode)
if has_color_space:
ak.string_builder_append_u8(builder, ' ')
if has_color_space:
css_style_value_serialize(this.m_properties.color_interpolation_method else trap("VERIFY_NOT_REACHED"), builder, mode)
if has_direction or has_color_space:
ak.string_builder_append(builder, ", ")
css_serialize_color_stop_list(builder, this.m_properties.color_stop_list, mode)
ak.string_builder_append(builder, ")")
## Ported from Libraries/LibWeb/CSS/StyleValues/LinearGradientStyleValue.cpp:75 Web::CSS::LinearGradientStyleValue::absolutized (override).
pub func css_linear_gradient_style_value_absolutized_impl(base: CssStyleValue*, context: CssComputationContext) -> CssStyleValue*:
let this = (CssLinearGradientStyleValue*)base
let absolutized_color_stops = css_color_stop_list_absolutized(&this.m_properties.color_stop_list, context)
var absolutized_color_interpolation_method: CssStyleValue*? = none
if let method = this.m_properties.color_interpolation_method:
absolutized_color_interpolation_method = css_style_value_absolutized(method, context)
return css_linear_gradient_style_value_create(this.m_properties.direction, absolutized_color_stops, this.m_properties.gradient_type, this.m_properties.repeating, absolutized_color_interpolation_method).css_style_value()
## Ported from Libraries/LibWeb/CSS/StyleValues/LinearGradientStyleValue.cpp:88 Web::CSS::LinearGradientStyleValue::equals (override).
pub func css_linear_gradient_style_value_equals_impl(base: CssStyleValue*, other_: CssStyleValue*) -> bool:
let this = (CssLinearGradientStyleValue*)base
if css_style_value_type(base) != css_style_value_type(other_):
return false
let other = css_style_value_as_linear_gradient(other_)
# NB: Properties::operator== is defaulted.
let a = &this.m_properties
let b = &other.m_properties
return css_gradient_directions_equal(a.direction, b.direction) and css_color_stop_lists_equal(&a.color_stop_list, &b.color_stop_list) and a.gradient_type == b.gradient_type and a.repeating == b.repeating and css_optional_style_values_equal(a.color_interpolation_method, b.color_interpolation_method) and a.color_syntax == b.color_syntax
## Ported from Libraries/LibWeb/CSS/StyleValues/LinearGradientStyleValue.h:53 Web::CSS::LinearGradientStyleValue::is_computationally_independent (override).
pub func css_linear_gradient_style_value_is_computationally_independent_impl(base: CssStyleValue*) -> bool:
let this = (CssLinearGradientStyleValue*)base
var is_direction_computationally_independent = true
match this.m_properties.direction:
.css_style_value(value):
is_direction_computationally_independent = css_style_value_is_computationally_independent(value)
.css_side_or_corner(_):
is_direction_computationally_independent = true
if not is_direction_computationally_independent:
return false
for stop in ak.vector_span_const(&this.m_properties.color_stop_list):
if not css_style_value_is_computationally_independent(stop.color_stop.color else trap("VERIFY: a color stop's color")):
return false
return css_is_null_or_computationally_independent(this.m_properties.color_interpolation_method)
# mark: Accessors ===============================================================================
## Ported from Libraries/LibWeb/CSS/StyleValues/LinearGradientStyleValue.h:64 Web::CSS::LinearGradientStyleValue::color_stop_list.
pub func css_linear_gradient_style_value_color_stop_list(this: CssLinearGradientStyleValue*) -> ak.Vector[CssColorStopListElement]:
return this.m_properties.color_stop_list
# FIXME: This (and the any_non_legacy code in the constructor) is duplicated in the separate gradient classes,
# should this logic be pulled into some kind of GradientStyleValue superclass?
# It could also contain the "gradient related things" currently in AbstractImageStyleValue.h
## Ported from Libraries/LibWeb/CSS/StyleValues/LinearGradientStyleValue.h:72 Web::CSS::LinearGradientStyleValue::interpolation_method.
pub func css_linear_gradient_style_value_interpolation_method(this: CssLinearGradientStyleValue*) -> display_list.ColorInterpolationMethod:
if let method = this.m_properties.color_interpolation_method:
return css_color_interpolation_method_style_value_color_interpolation_method(css_style_value_as_color_interpolation_method(method))
return css_color_interpolation_method_style_value_default_color_interpolation_method(this.m_properties.color_syntax)
## Ported from Libraries/LibWeb/CSS/StyleValues/LinearGradientStyleValue.h:80 Web::CSS::LinearGradientStyleValue::is_repeating.
pub func css_linear_gradient_style_value_is_repeating(this: CssLinearGradientStyleValue*) -> bool:
return this.m_properties.repeating == CssGradientRepeating.yes
## The `corner_angle_degrees` lambda of LinearGradientStyleValue::angle_degrees.
func css_linear_gradient_corner_angle_degrees(gradient_size: gfx.CssPixelSize) -> f64:
return ak.to_degrees_f64(math.atan2(gradient_size.m_height.to_double(), gradient_size.m_width.to_double()))
## Ported from Libraries/LibWeb/CSS/StyleValues/LinearGradientStyleValue.cpp:96 Web::CSS::LinearGradientStyleValue::angle_degrees.
pub func css_linear_gradient_style_value_angle_degrees(this: CssLinearGradientStyleValue*, gradient_size: gfx.CssPixelSize) -> f32:
match this.m_properties.direction:
.css_side_or_corner(side_or_corner):
var angle = 0.0
match side_or_corner:
.top:
angle = 0.0
.bottom:
angle = 180.0
.left:
angle = 270.0
.right:
angle = 90.0
.top_right:
angle = css_linear_gradient_corner_angle_degrees(gradient_size)
.bottom_left:
angle = css_linear_gradient_corner_angle_degrees(gradient_size) + 180.0
.top_left:
angle = -css_linear_gradient_corner_angle_degrees(gradient_size)
.bottom_right:
angle = -(css_linear_gradient_corner_angle_degrees(gradient_size) + 180.0)
_:
trap("VERIFY_NOT_REACHED")
# Note: For unknowable reasons the angles are opposite on the -webkit- version
if this.m_properties.gradient_type == CssLinearGradientStyleValueGradientType.web_kit:
return (f32)(angle + 180.0)
return (f32)angle
.css_style_value(style_value):
let angle = css_angle_from_style_value(style_value, none)
return (f32)css_angle_to_degrees(&angle)
# mark: Painting ================================================================================
## Ported from Libraries/LibWeb/CSS/StyleValues/LinearGradientStyleValue.cpp:135 Web::CSS::LinearGradientStyleValue::resolve_for_size (override).
pub func css_linear_gradient_style_value_resolve_for_size_impl(base: CssAbstractImageStyleValue*, node: LayoutNodeWithStyle*, size: gfx.CssPixelSize):
let this = (CssLinearGradientStyleValue*)base
let cache_key = CssLinearGradientStyleValueResolvedDataCacheKey(length_resolution_context = css_length_resolution_context_for_layout_node((LayoutNode*)node), size = size)
if this.m_resolved_data_cache_key != cache_key:
this.m_resolved_data_cache_key = cache_key
this.m_resolved = paint_resolve_linear_gradient_data(node, size, this)
## Ported from Libraries/LibWeb/CSS/StyleValues/LinearGradientStyleValue.h:86 Web::CSS::LinearGradientStyleValue::is_paintable (override).
pub func css_linear_gradient_style_value_is_paintable_impl(base: CssAbstractImageStyleValue*) -> bool:
_ = base
return true
## Ported from Libraries/LibWeb/CSS/StyleValues/LinearGradientStyleValue.cpp:147 Web::CSS::LinearGradientStyleValue::paint (override).
pub func css_linear_gradient_style_value_paint_impl(base: CssAbstractImageStyleValue*, context: DisplayListRecordingContext*, dest_rect: gfx.DevicePixelRect, image_rendering: css_data.ImageRendering):
_ = image_rendering
let this = (CssLinearGradientStyleValue*)base
let resolved = this.m_resolved else trap("VERIFY: resolved gradient data")
display_list.display_list_recorder_fill_rect_with_linear_gradient(display_list_recording_context_display_list_recorder(context), dest_rect, resolved)