dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / style_values / transformation_style_value_1.lucb
#==============================================================================================
#
# transformation_style_value_1 - TransformationStyleValue: a transform function and its matrix
#
# DESCRIPTION:
# Port of LibWeb/CSS/StyleValues/TransformationStyleValue.cpp (lines 1-382) and the inline
# members of TransformationStyleValue.h: creation, the identity transform functions and
# to_matrix (its `length_to_px` and `get_value` lambdas are functions of their own taking the
# captures as parameters). Gfx::VectorN's length() and normalized(), which gfx does not port
# yet, are the css_float_vector3_* helpers.
#
# Copyright (c) 2018-2024, 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>
# Copyright (c) 2024, Steffen T. Larssen <dudedbz@gmail.com>
# Copyright (c) 2024-2025, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
# Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
from luce_std import math32
# mark: Creation ================================================================================
## Ported from Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.h:20 Web::CSS::TransformationStyleValue::create.
pub func css_transformation_style_value_create(property: css_data.PropertyId, transform_function: css_data.TransformFunction, values: ak.Vector[CssStyleValue*]) -> CssTransformationStyleValue*:
let this = &(new CssTransformationStyleValue[1] --- else trap("out of memory"))[0]
css_transformation_style_value_construct(this, property, transform_function, values)
return this
## Ported from Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.h:46 Web::CSS::TransformationStyleValue::TransformationStyleValue.
pub func css_transformation_style_value_construct(this: CssTransformationStyleValue*, property: css_data.PropertyId, transform_function: css_data.TransformFunction, values: ak.Vector[CssStyleValue*]):
css_style_value_construct(this.css_style_value(), CssStyleValueType.transformation)
css_style_value_set_class(this.css_style_value(), (const CssStyleValueVTable*)&css_transformation_style_value_vtable, &css_transformation_style_value_class)
this.m_properties = CssTransformationStyleValueProperties(property = property, transform_function = transform_function, values = values)
## A StyleValueVector of the given values.
func css_style_value_vector_of(values: const (CssStyleValue*)[]) -> ak.Vector[CssStyleValue*]:
var result = ak.Vector[CssStyleValue*]()
for value in values:
ak.vector_append_t(&result, value)
return result
## identity_transformation's `identity_parameters` lambda.
func css_identity_parameters(transform_function: css_data.TransformFunction) -> ak.Vector[CssStyleValue*]:
let number_zero = css_number_style_value_create(0.0).css_style_value()
let number_one = css_number_style_value_create(1.0).css_style_value()
match transform_function:
.matrix:
let values: CssStyleValue*[6] = [number_one, number_zero, number_zero, number_one, number_zero, number_zero]
return css_style_value_vector_of(values)
.matrix3_d:
let values: CssStyleValue*[16] = [
number_one, number_zero, number_zero, number_zero,
number_zero, number_one, number_zero, number_zero,
number_zero, number_zero, number_one, number_zero,
number_zero, number_zero, number_zero, number_one,
]
return css_style_value_vector_of(values)
.perspective:
let values: CssStyleValue*[1] = [css_keyword_style_value_create(css_data.Keyword.none_).css_style_value()]
return css_style_value_vector_of(values)
.rotate, .rotate_x, .rotate_y, .rotate_z:
let values: CssStyleValue*[1] = [css_angle_style_value_create(css_angle_make_degrees(0.0)).css_style_value()]
return css_style_value_vector_of(values)
.rotate3_d:
let values: CssStyleValue*[4] = [number_one, number_one, number_one, css_angle_style_value_create(css_angle_make_degrees(0.0)).css_style_value()]
return css_style_value_vector_of(values)
.skew, .skew_x, .skew_y, .translate, .translate_x, .translate_y, .translate_z:
let values: CssStyleValue*[1] = [css_length_style_value_create(css_length_make_px_f64(0.0)).css_style_value()]
return css_style_value_vector_of(values)
.translate3_d:
let values: CssStyleValue*[3] = [
css_length_style_value_create(css_length_make_px_f64(0.0)).css_style_value(),
css_length_style_value_create(css_length_make_px_f64(0.0)).css_style_value(),
css_length_style_value_create(css_length_make_px_f64(0.0)).css_style_value(),
]
return css_style_value_vector_of(values)
.scale, .scale_x, .scale_y, .scale_z:
let values: CssStyleValue*[1] = [number_one]
return css_style_value_vector_of(values)
.scale3_d:
let values: CssStyleValue*[3] = [number_one, number_one, number_one]
return css_style_value_vector_of(values)
_:
trap("VERIFY_NOT_REACHED")
## Ported from Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.cpp:38 Web::CSS::TransformationStyleValue::identity_transformation.
pub func css_transformation_style_value_identity_transformation(transform_function: css_data.TransformFunction) -> CssTransformationStyleValue*:
# https://drafts.csswg.org/css-transforms-1/#identity-transform-function
# A transform function that is equivalent to a identity 4x4 matrix (see Mathematical Description of Transform
# Functions). Examples for identity transform functions are translate(0), translateX(0), translateY(0), scale(1),
# scaleX(1), scaleY(1), rotate(0), skew(0, 0), skewX(0), skewY(0) and matrix(1, 0, 0, 1, 0, 0).
# https://drafts.csswg.org/css-transforms-2/#identity-transform-function
# In addition to the identity transform function in CSS Transforms, examples for identity transform functions
# include translate3d(0, 0, 0), translateZ(0), scaleZ(1), rotate3d(1, 1, 1, 0), rotateX(0), rotateY(0), rotateZ(0)
# and matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1). A special case is perspective: perspective(none).
# The value of m34 becomes infinitesimal small and the transform function is therefore assumed to be equal to the
# identity matrix.
return css_transformation_style_value_create(css_data.PropertyId.transform, transform_function, css_identity_parameters(transform_function))
## Ported from Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.h:28 Web::CSS::TransformationStyleValue::transform_function.
pub func css_transformation_style_value_transform_function(this: CssTransformationStyleValue*) -> css_data.TransformFunction:
return this.m_properties.transform_function
## Ported from Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.h:29 Web::CSS::TransformationStyleValue::values.
pub func css_transformation_style_value_values(this: CssTransformationStyleValue*) -> ak.Vector[CssStyleValue*]:
return this.m_properties.values
# mark: to_matrix ===============================================================================
## `FloatMatrix4x4(m00, m01, ..., m33)`: a matrix from its 16 elements in row-major order.
func css_float_matrix(elements: f32[16]) -> gfx.FloatMatrix4X4:
var matrix = gfx.float_matrix4_x4_identity()
gfx.float_matrix4_x4_construct(&matrix, elements)
return matrix
## to_matrix's `length_to_px` lambda.
func css_transformation_length_to_px(paintable_box: PaintPaintableBox*?, length: CssLength) -> f32!:
if let box = paintable_box:
return css_length_to_px_layout_node(&length, paint_paintable_layout_node(box.paint_paintable())).to_float()
if css_length_is_absolute(&length):
return css_length_absolute_length_to_px(&length).to_float()
error(ak.ak_error, "Transform contains non absolute units")
## to_matrix's `get_value` lambda: the argument at `argument_index` as a float (radians, px, a number or a fraction).
func css_transformation_get_value(this: CssTransformationStyleValue*, paintable_box: PaintPaintableBox*?, function_metadata: const css_data.TransformFunctionMetadata*, argument_index: usize, reference_length: gfx.CssPixels? = none) -> f32!:
let transformation_value = *ak.vector_at_const(&this.m_properties.values, argument_index)
var context = CssCalculationResolutionContext()
if let box = paintable_box:
context.length_resolution_context = css_length_resolution_context_for_layout_node(paint_paintable_layout_node(box.paint_paintable()))
if let reference = reference_length:
context.percentage_basis = CssCalculationResolutionContextPercentageBasis.css_length(css_length_make_px_css_pixels(reference))
if css_style_value_is_calculated(transformation_value):
let calculated = css_style_value_as_calculated(transformation_value)
match ak.vector_at_const(&function_metadata.parameters, argument_index).type_:
.angle:
if not css_calculated_style_value_resolves_to_angle(calculated):
error(ak.ak_error, "Calculated angle parameter to transform function doesn't resolve to an angle.")
if let resolved = css_calculated_style_value_resolve_angle(calculated, context):
return (f32)css_angle_to_radians(&resolved)
error(ak.ak_error, "Couldn't resolve calculated angle.")
.length, .length_none:
if not css_calculated_style_value_resolves_to_length(calculated):
error(ak.ak_error, "Calculated length parameter to transform function doesn't resolve to a length.")
if let resolved = css_calculated_style_value_resolve_length(calculated, context):
return try css_transformation_length_to_px(paintable_box, resolved)
error(ak.ak_error, "Couldn't resolve calculated length.")
.length_percentage:
if not css_calculated_style_value_resolves_to_length_percentage(calculated):
error(ak.ak_error, "Calculated length-percentage parameter to transform function doesn't resolve to a length-percentage.")
if let resolved = css_calculated_style_value_resolve_length(calculated, context):
return try css_transformation_length_to_px(paintable_box, resolved)
error(ak.ak_error, "Couldn't resolve calculated length-percentage.")
.number:
if not css_calculated_style_value_resolves_to_number(calculated):
error(ak.ak_error, "Calculated number parameter to transform function doesn't resolve to a number.")
if let resolved = css_calculated_style_value_resolve_number(calculated, context):
return (f32)resolved
error(ak.ak_error, "Couldn't resolve calculated number.")
.number_percentage:
if css_calculated_style_value_resolves_to_number(calculated):
if css_calculated_style_value_resolve_number(calculated, context) != none:
return (f32)(css_calculated_style_value_resolve_number(calculated, context) else trap("Optional::value(): empty"))
error(ak.ak_error, "Couldn't resolve calculated number.")
if css_calculated_style_value_resolves_to_percentage(calculated):
if css_calculated_style_value_resolve_percentage(calculated, context) != none:
let percentage = css_calculated_style_value_resolve_percentage(calculated, context) else trap("Optional::value(): empty")
return (f32)css_percentage_as_fraction(&percentage)
error(ak.ak_error, "Couldn't resolve calculated percentage.")
error(ak.ak_error, "Calculated number/percentage parameter to transform function doesn't resolve to a number or percentage.")
_:
_ = 0
if css_style_value_is_length(transformation_value):
return try css_transformation_length_to_px(paintable_box, css_length_style_value_length(css_style_value_as_length(transformation_value)))
if css_style_value_is_percentage(transformation_value):
let percentage = css_percentage_style_value_percentage(css_style_value_as_percentage(transformation_value))
if ak.vector_at_const(&function_metadata.parameters, argument_index).type_ == css_data.TransformFunctionParameterType.number_percentage:
return (f32)css_percentage_as_fraction(&percentage)
let reference = reference_length else error(ak.ak_error, "Can't resolve percentage to length without a reference value.")
let reference_px = css_length_make_px_css_pixels(reference)
return try css_transformation_length_to_px(paintable_box, css_length_percentage_of(&reference_px, percentage))
if css_style_value_is_number(transformation_value):
return (f32)css_number_style_value_number(css_style_value_as_number(transformation_value))
if css_style_value_is_angle(transformation_value):
let angle = css_angle_style_value_angle(css_style_value_as_angle(transformation_value))
return (f32)css_angle_to_radians(&angle)
let text = css_style_value_to_string(transformation_value, css_syntax.SerializationMode.normal)
ak.dbgln(f"FIXME: Unsupported value in transform! {ak.string_view_text(ak.string_bytes_as_string_view(&text))}")
error(ak.ak_error, "Unsupported value in transform function")
## Ported from Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.cpp:101 Web::CSS::TransformationStyleValue::to_matrix.
pub func css_transformation_style_value_to_matrix(this: CssTransformationStyleValue*, paintable_box: PaintPaintableBox*?) -> gfx.FloatMatrix4X4!:
let count = ak.vector_size(&this.m_properties.values)
let function_metadata = css_data.transform_function_metadata(this.m_properties.transform_function)
let metadata = &function_metadata
var width: gfx.CssPixels? = none
var height: gfx.CssPixels? = none
if let box = paintable_box:
let reference_box = paint_paintable_box_transform_reference_box(box)
width = gfx.css_pixel_rect_width(&reference_box)
height = gfx.css_pixel_rect_height(&reference_box)
match this.m_properties.transform_function:
.perspective:
# https://drafts.csswg.org/css-transforms-2/#perspective
if count == 1:
if css_style_value_to_keyword(*ak.vector_first_const(&this.m_properties.values)) == css_data.Keyword.none_:
return gfx.float_matrix4_x4_identity()
# FIXME: Add support for the 'perspective-origin' CSS property.
var distance = try css_transformation_get_value(this, paintable_box, metadata, 0)
# If the depth value is less than '1px', it must be treated as '1px' for the purpose of rendering, for
# computing the resolved value of 'transform', and when used as the endpoint of interpolation.
# Note: The intent of the above rules on values less than '1px' is that they cover the cases where
# the 'perspective()' function needs to be converted into a matrix.
if distance < 1.0:
distance = 1.0
return css_float_matrix([1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, -1.0 / distance, 1.0])
.matrix:
if count == 6:
return css_float_matrix([try css_transformation_get_value(this, paintable_box, metadata, 0), try css_transformation_get_value(this, paintable_box, metadata, 2), 0.0, try css_transformation_get_value(this, paintable_box, metadata, 4),
try css_transformation_get_value(this, paintable_box, metadata, 1), try css_transformation_get_value(this, paintable_box, metadata, 3), 0.0, try css_transformation_get_value(this, paintable_box, metadata, 5),
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0])
.matrix3_d:
if count == 16:
return css_float_matrix([try css_transformation_get_value(this, paintable_box, metadata, 0), try css_transformation_get_value(this, paintable_box, metadata, 4), try css_transformation_get_value(this, paintable_box, metadata, 8), try css_transformation_get_value(this, paintable_box, metadata, 12),
try css_transformation_get_value(this, paintable_box, metadata, 1), try css_transformation_get_value(this, paintable_box, metadata, 5), try css_transformation_get_value(this, paintable_box, metadata, 9), try css_transformation_get_value(this, paintable_box, metadata, 13),
try css_transformation_get_value(this, paintable_box, metadata, 2), try css_transformation_get_value(this, paintable_box, metadata, 6), try css_transformation_get_value(this, paintable_box, metadata, 10), try css_transformation_get_value(this, paintable_box, metadata, 14),
try css_transformation_get_value(this, paintable_box, metadata, 3), try css_transformation_get_value(this, paintable_box, metadata, 7), try css_transformation_get_value(this, paintable_box, metadata, 11), try css_transformation_get_value(this, paintable_box, metadata, 15)])
.translate:
if count == 1:
return css_float_matrix([1.0, 0.0, 0.0, try css_transformation_get_value(this, paintable_box, metadata, 0, width),
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0])
if count == 2:
return css_float_matrix([1.0, 0.0, 0.0, try css_transformation_get_value(this, paintable_box, metadata, 0, width),
0.0, 1.0, 0.0, try css_transformation_get_value(this, paintable_box, metadata, 1, height),
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0])
.translate3_d:
return css_float_matrix([1.0, 0.0, 0.0, try css_transformation_get_value(this, paintable_box, metadata, 0, width),
0.0, 1.0, 0.0, try css_transformation_get_value(this, paintable_box, metadata, 1, height),
0.0, 0.0, 1.0, try css_transformation_get_value(this, paintable_box, metadata, 2),
0.0, 0.0, 0.0, 1.0])
.translate_x:
if count == 1:
return css_float_matrix([1.0, 0.0, 0.0, try css_transformation_get_value(this, paintable_box, metadata, 0, width),
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0])
.translate_y:
if count == 1:
return css_float_matrix([1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, try css_transformation_get_value(this, paintable_box, metadata, 0, height),
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0])
.translate_z:
if count == 1:
return css_float_matrix([1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, try css_transformation_get_value(this, paintable_box, metadata, 0),
0.0, 0.0, 0.0, 1.0])
.scale:
if count == 1:
return css_float_matrix([try css_transformation_get_value(this, paintable_box, metadata, 0), 0.0, 0.0, 0.0,
0.0, try css_transformation_get_value(this, paintable_box, metadata, 0), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0])
if count == 2:
return css_float_matrix([try css_transformation_get_value(this, paintable_box, metadata, 0), 0.0, 0.0, 0.0,
0.0, try css_transformation_get_value(this, paintable_box, metadata, 1), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0])
.scale3_d:
if count == 3:
return css_float_matrix([try css_transformation_get_value(this, paintable_box, metadata, 0), 0.0, 0.0, 0.0,
0.0, try css_transformation_get_value(this, paintable_box, metadata, 1), 0.0, 0.0,
0.0, 0.0, try css_transformation_get_value(this, paintable_box, metadata, 2), 0.0,
0.0, 0.0, 0.0, 1.0])
.scale_x:
if count == 1:
return css_float_matrix([try css_transformation_get_value(this, paintable_box, metadata, 0), 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0])
.scale_y:
if count == 1:
return css_float_matrix([1.0, 0.0, 0.0, 0.0,
0.0, try css_transformation_get_value(this, paintable_box, metadata, 0), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0])
.scale_z:
if count == 1:
return css_float_matrix([1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, try css_transformation_get_value(this, paintable_box, metadata, 0), 0.0,
0.0, 0.0, 0.0, 1.0])
.rotate3_d:
if count == 4:
let axis = gfx.FloatVector3(m_data = [try css_transformation_get_value(this, paintable_box, metadata, 0), try css_transformation_get_value(this, paintable_box, metadata, 1), try css_transformation_get_value(this, paintable_box, metadata, 2)])
let epsilon: f32 = 1e-5
if gfx_float_vector3_length(axis) < epsilon:
return gfx.float_matrix4_x4_identity()
return gfx.rotation_matrix(gfx_float_vector3_normalized(axis), try css_transformation_get_value(this, paintable_box, metadata, 3))
.rotate_x:
if count == 1:
return gfx.rotation_matrix(gfx.FloatVector3(m_data = [1.0, 0.0, 0.0]), try css_transformation_get_value(this, paintable_box, metadata, 0))
.rotate_y:
if count == 1:
return gfx.rotation_matrix(gfx.FloatVector3(m_data = [0.0, 1.0, 0.0]), try css_transformation_get_value(this, paintable_box, metadata, 0))
.rotate, .rotate_z:
if count == 1:
return gfx.rotation_matrix(gfx.FloatVector3(m_data = [0.0, 0.0, 1.0]), try css_transformation_get_value(this, paintable_box, metadata, 0))
.skew:
if count == 1:
return css_float_matrix([1.0, math32.tan(try css_transformation_get_value(this, paintable_box, metadata, 0)), 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0])
if count == 2:
return css_float_matrix([1.0, math32.tan(try css_transformation_get_value(this, paintable_box, metadata, 0)), 0.0, 0.0,
math32.tan(try css_transformation_get_value(this, paintable_box, metadata, 1)), 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0])
.skew_x:
if count == 1:
return css_float_matrix([1.0, math32.tan(try css_transformation_get_value(this, paintable_box, metadata, 0)), 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0])
.skew_y:
if count == 1:
return css_float_matrix([1.0, 0.0, 0.0, 0.0,
math32.tan(try css_transformation_get_value(this, paintable_box, metadata, 0)), 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0])
_:
_ = 0
if css_libweb_css_debug:
ak.dbgln(f"FIXME: Unhandled transformation function {ak.string_view_text(css_data.to_string_transform_function(this.m_properties.transform_function))} with {count} arguments")
return gfx.float_matrix4_x4_identity()