dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / style_computer_7.lucb
#==============================================================================================
#
# style_computer_7 - CSS::StyleComputer: custom properties and the computed values of
# properties
#
# DESCRIPTION:
# Port of LibWeb/CSS/StyleComputer.cpp (lines 2241-2793): the computed values of custom
# properties (var() substitution through r33's arbitrary substitution functions),
# compute_custom_properties, the line-width keywords and border-width snapping, the
# coordinated list helpers, compute_value_of_property and the per-property computations
# of animation-name, font-feature/variation settings, border and outline widths, corner
# shapes, font-size (with the math scaling), font-style, font-weight, font-width and
# line-height.
#
# Copyright (c) 2018-2025, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2021, the SerenityOS developers.
# Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
# Copyright (c) 2024, Matthew Olsson <mattco@serenityos.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Custom properties =======================================================================
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2241 Web::CSS::StyleComputer::compute_value_of_custom_property.
pub func css_style_computer_compute_value_of_custom_property(abstract_element: DomAbstractElement, name: ak.FlyString, guarded_contexts: CssGuardedSubstitutionContexts*? = none) -> CssStyleValue*:
# https://drafts.csswg.org/css-variables/#propdef-
# The computed value of a custom property is its specified value with any arbitrary-substitution functions replaced.
# FIXME: These should probably be part of ComputedProperties.
let document = dom_abstract_element_document(&abstract_element)
let value = dom_abstract_element_get_custom_property(&abstract_element, name) else return dom_document_custom_property_initial_value(document, name)
if css_style_value_is_initial(value):
return dom_document_custom_property_initial_value(document, name)
# Unset is the same as inherit for inherited properties, and by default all custom properties are inherited.
# FIXME: Support non-inherited registered custom properties.
if css_style_value_is_inherit(value) or css_style_value_is_unset(value):
let element_to_inherit_style_from = dom_abstract_element_element_to_inherit_style_from(&abstract_element) else return dom_document_custom_property_initial_value(document, name)
let inherited_value = dom_abstract_element_get_custom_property(&element_to_inherit_style_from, name) else return dom_document_custom_property_initial_value(document, name)
return inherited_value
if css_style_value_is_revert(value):
# FIXME: Implement reverting custom properties.
_ = 0
if css_style_value_is_revert_layer(value):
# FIXME: Implement reverting custom properties.
_ = 0
if not css_style_value_is_unresolved(value) or not css_unresolved_style_value_contains_arbitrary_substitution_function(css_style_value_as_unresolved(value)):
return value
let unresolved = css_style_value_as_unresolved(value)
var params: CssParsingParams = ---
css_parsing_params_construct_css_parsing_mode(¶ms)
let property = css_property_name_and_id_from_name(name) else trap("VERIFY: a custom property name")
return css_parser_resolve_unresolved_style_value_css_parsing_params(params, abstract_element, property, unresolved, guarded_contexts)
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2278 Web::CSS::StyleComputer::compute_custom_properties.
pub func css_style_computer_compute_custom_properties(this: CssStyleComputer*, arg0: CssComputedProperties*, abstract_element_: DomAbstractElement):
var abstract_element = abstract_element_
# https://drafts.csswg.org/css-variables/#propdef-
# The computed value of a custom property is its specified value with any arbitrary-substitution functions replaced.
# FIXME: These should probably be part of ComputedProperties.
let data = dom_abstract_element_custom_property_data(&abstract_element) else return
# If this element is sharing its parent's data (no own custom properties),
# the parent has already resolved its values, so there's nothing to do.
let inherit_from = dom_abstract_element_element_to_inherit_style_from(&abstract_element)
if let parent = inherit_from:
if dom_abstract_element_custom_property_data(&parent) == data:
return
if ak.ordered_hash_map_is_empty(&data.m_own_values):
return
# Resolve var() references and only keep values that differ from parent.
# This avoids growing the hashmap to full size and then shrinking it,
# which would leave an oversized bucket array.
var parent_data: const CssCustomPropertyData*? = none
if let parent = inherit_from:
parent_data = css_inheritable_custom_property_data(parent)
var resolved_own: ak.OrderedHashMap[ak.FlyString, CssStyleProperty, ak.FlyStringTraits]
for entry in ak.ordered_hash_map_begin_const(&data.m_own_values):
let resolved_value = css_style_computer_compute_value_of_custom_property(abstract_element, entry.key)
if let parent_properties = parent_data:
if let parent_property = css_custom_property_data_get(parent_properties, entry.key):
if css_style_value_equals(resolved_value, parent_property.value):
continue
_ = ak.ordered_hash_map_set(&resolved_own, entry.key, CssStyleProperty(important = entry.value.important, property_id = entry.value.property_id, value = resolved_value))
if ak.ordered_hash_map_is_empty(&resolved_own) and parent_data != none:
dom_abstract_element_set_custom_property_data(&abstract_element, parent_data)
return
# FIXME: We should update in place so that non-recomputed children aren't left pointing at stale data
var new_parent = css_custom_property_data_parent(data)
if parent_data != none:
new_parent = parent_data
dom_abstract_element_set_custom_property_data(&abstract_element, css_custom_property_data_create(resolved_own, new_parent))
# mark: Helpers =================================================================================
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2329 Web::CSS::line_width_keyword_to_css_pixels.
func css_line_width_keyword_to_css_pixels(keyword: css_data.Keyword) -> gfx.CssPixels:
# https://drafts.csswg.org/css-backgrounds/#typedef-line-width
# The thin, medium, and thick keywords are equivalent to 1px, 3px, and 5px, respectively.
match keyword:
.thin:
return gfx.css_pixels_from_int(1)
.medium:
return gfx.css_pixels_from_int(3)
.thick:
return gfx.css_pixels_from_int(5)
_:
trap("VERIFY_NOT_REACHED")
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2346 Web::CSS::snap_a_length_as_a_border_width.
# https://www.w3.org/TR/css-values-4/#snap-a-length-as-a-border-width
func css_snap_a_length_as_a_border_width(device_pixels_per_css_pixel: f64, length: gfx.CssPixels) -> gfx.CssPixels:
# 1. Assert: len is non-negative.
assert(not gfx.lt_int(length, 0))
# 2. If len is an integer number of device pixels, do nothing.
let device_pixels = gfx.css_pixels_to_double_const(&length) * device_pixels_per_css_pixel
if device_pixels == math.trunc(device_pixels):
return length
# 3. If len is greater than zero, but less than 1 device pixel, round len up to 1 device pixel.
if device_pixels > 0.0 and device_pixels < 1.0:
return gfx.css_pixels_nearest_value_for_f64(1.0 / device_pixels_per_css_pixel)
# 4. If len is greater than 1 device pixel, round it down to the nearest integer number of device pixels.
if device_pixels > 1.0:
return gfx.css_pixels_nearest_value_for_f64(math.floor(device_pixels) / device_pixels_per_css_pixel)
return length
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2367 Web::CSS::compute_style_value_list.
func css_compute_style_value_list(style_value: CssStyleValue*, compute_entry: func(CssStyleValue*) -> CssStyleValue*) -> CssStyleValue*:
var computed_entries: ak.Vector[CssStyleValue*]
let entries = css_style_value_list_values(css_style_value_as_value_list(style_value))
for entry in ak.vector_span_const(&entries):
ak.vector_append_t(&computed_entries, compute_entry(entry))
return css_style_value_list_create(computed_entries, CssStyleValueListSeparator.comma).css_style_value()
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2377 Web::CSS::repeat_style_value_list_to_n_elements.
func css_repeat_style_value_list_to_n_elements(style_value: CssStyleValue*, n: usize) -> CssStyleValue*:
let value_list = css_style_value_as_value_list(style_value)
if css_style_value_list_size(value_list) == n:
return style_value
var repeated_values: ak.Vector[CssStyleValue*]
ak.vector_ensure_capacity(&repeated_values, n)
for i in 0..<n:
ak.vector_unchecked_append_u(&repeated_values, css_style_value_list_value_at(value_list, i, true))
return css_style_value_list_create(repeated_values, css_style_value_list_separator(value_list)).css_style_value()
# mark: compute_value_of_property ===============================================================
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2393 Web::CSS::StyleComputer::compute_value_of_property.
pub func css_style_computer_compute_value_of_property(property_id: css_data.PropertyId, specified_value: CssStyleValue*, get_property_specified_value: ak.Function1[css_data.PropertyId, CssStyleValue*], computation_context: CssComputationContext, device_pixels_per_css_pixel: f64) -> CssStyleValue*:
let absolutized_value = css_style_value_absolutized(specified_value, computation_context)
match property_id:
.animation_name:
return css_style_computer_compute_animation_name(absolutized_value)
# NB: The background properties are coordinated at compute time rather than use time, unlike other coordinating list property groups
.background_attachment, .background_clip, .background_origin, .background_position_x, .background_position_y, .background_repeat, .background_size:
let background_image = ak.function1_call(&get_property_specified_value, css_data.PropertyId.background_image)
return css_repeat_style_value_list_to_n_elements(absolutized_value, css_style_value_list_size(css_style_value_as_value_list(background_image)))
.border_bottom_width, .border_left_width, .border_right_width, .border_top_width, .outline_width:
return css_style_computer_compute_border_or_outline_width(absolutized_value, device_pixels_per_css_pixel)
.corner_bottom_left_shape, .corner_bottom_right_shape, .corner_top_left_shape, .corner_top_right_shape:
return css_style_computer_compute_corner_shape(absolutized_value)
.font_size:
let math_depth = ak.function1_call(&get_property_specified_value, css_data.PropertyId.math_depth)
return css_style_computer_compute_font_size(absolutized_value, css_integer_style_value_integer(css_style_value_as_integer(math_depth)), css_style_computer_inheritance_parent_of(computation_context.abstract_element), css_initial_values_font_size())
.font_style:
return css_style_computer_compute_font_style(absolutized_value)
.font_weight:
return css_style_computer_compute_font_weight(absolutized_value, css_style_computer_inheritance_parent_of(computation_context.abstract_element))
.font_width:
return css_style_computer_compute_font_width(absolutized_value)
.font_feature_settings, .font_variation_settings:
return css_style_computer_compute_font_feature_tag_value_list(absolutized_value)
.letter_spacing, .word_spacing:
if css_style_value_to_keyword(absolutized_value) == css_data.Keyword.normal:
return css_length_style_value_create(css_length_make_px_f64(0.0)).css_style_value()
return absolutized_value
.line_height:
return css_style_computer_compute_line_height(absolutized_value, computation_context.length_resolution_context.font_metrics.font_size)
.math_depth:
return css_style_computer_compute_math_depth(absolutized_value, css_style_computer_inheritance_parent_of(computation_context.abstract_element))
.position_area:
return css_style_computer_compute_position_area(absolutized_value)
_:
return absolutized_value
## compute_animation_name's per-entry lambda.
func css_compute_animation_name_entry(entry: CssStyleValue*) -> CssStyleValue*:
# none | <custom-ident>
if css_style_value_to_keyword(entry) == css_data.Keyword.none_ or css_style_value_is_custom_ident(entry):
return entry
# <string>
if css_style_value_is_string(entry):
let string_value = css_string_style_value_string_value(css_style_value_as_string(entry))
# AD-HOC: We shouldn't convert strings that aren't valid <custom-ident>s
let blacklist: ak.StringView[1] = [ak.sv("none")]
if not is_valid_custom_ident(string_value, blacklist):
return entry
return css_custom_ident_style_value_create(string_value).css_style_value()
trap("VERIFY_NOT_REACHED")
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2460 Web::CSS::StyleComputer::compute_animation_name.
pub func css_style_computer_compute_animation_name(absolutized_value: CssStyleValue*) -> CssStyleValue*:
# https://drafts.csswg.org/css-animations-1/#animation-name
# list, each item either a case-sensitive css identifier or the keyword none
return css_compute_style_value_list(absolutized_value, css_compute_animation_name_entry)
## compute_font_feature_tag_value_list's sort comparator: by tag.
func css_open_type_tag_less_than(context: void*?, a: const (CssStyleValue*)*, b: const (CssStyleValue*)*) -> bool:
let a_tag = css_open_type_tagged_style_value_tag(css_style_value_as_open_type_tagged(*a))
let b_tag = css_open_type_tagged_style_value_tag(css_style_value_as_open_type_tagged(*b))
return ak.fly_string_cmp(&a_tag, &b_tag) < 0
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2487 Web::CSS::StyleComputer::compute_font_feature_tag_value_list.
# https://drafts.csswg.org/css-fonts-4/#font-variation-settings-def
# https://drafts.csswg.org/css-fonts/#font-feature-settings-prop
pub func css_style_computer_compute_font_feature_tag_value_list(absolutized_value: CssStyleValue*) -> CssStyleValue*:
# NB: The computation logic is the same for both font-feature-settings and font-variation-settings, first we
# deduplicate feature tags (with latter taking precedence), then we sort them in ascending order by code unit
if css_style_value_is_keyword(absolutized_value):
return absolutized_value
let values = css_style_value_list_values(css_style_value_as_value_list(absolutized_value))
var axis_tags_map: ak.OrderedHashMap[ak.FlyString, CssStyleValue*, ak.FlyStringTraits]
for i in 0..<ak.vector_size(&values):
let axis_tag = *ak.vector_at_const(&values, i)
_ = ak.ordered_hash_map_set(&axis_tags_map, css_open_type_tagged_style_value_tag(css_style_value_as_open_type_tagged(axis_tag)), axis_tag)
var axis_tags: ak.Vector[CssStyleValue*]
for entry in ak.ordered_hash_map_begin_const(&axis_tags_map):
ak.vector_append_t(&axis_tags, entry.value)
ak.quick_sort_t_collection_t_less_than(ak.vector_span(&axis_tags), ak.Function2[const (CssStyleValue*)*, const (CssStyleValue*)*, bool](call = css_open_type_tag_less_than, context = none))
return css_style_value_list_create(axis_tags, CssStyleValueListSeparator.comma).css_style_value()
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2513 Web::CSS::StyleComputer::compute_border_or_outline_width.
pub func css_style_computer_compute_border_or_outline_width(absolutized_value: CssStyleValue*, device_pixels_per_css_pixel: f64) -> CssStyleValue*:
# https://drafts.csswg.org/css-backgrounds/#border-width
# absolute length, snapped as a border width
var absolute_length: gfx.CssPixels = ---
if css_style_value_is_keyword(absolutized_value):
absolute_length = css_line_width_keyword_to_css_pixels(css_style_value_to_keyword(absolutized_value))
else:
let length = css_length_from_style_value(absolutized_value, none)
absolute_length = css_length_absolute_length_to_px(&length)
return css_length_style_value_create(css_length_make_px_css_pixels(css_snap_a_length_as_a_border_width(device_pixels_per_css_pixel, absolute_length))).css_style_value()
## `static NonnullRefPtr<StyleValue const> const cached_round_value` of compute_corner_shape.
var css_compute_corner_shape_cached_round_value: CssStyleValue*?
## A superellipse() value of `parameter`.
func css_superellipse_of(parameter: f64) -> CssStyleValue*:
return css_superellipse_style_value_create(css_number_style_value_create(parameter).css_style_value()).css_style_value()
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2528 Web::CSS::StyleComputer::compute_corner_shape.
# https://drafts.csswg.org/css-borders-4/#propdef-corner-top-left-shape
pub func css_style_computer_compute_corner_shape(absolutized_value: CssStyleValue*) -> CssStyleValue*:
# the corresponding superellipse() value
if css_style_value_is_superellipse(absolutized_value):
return absolutized_value
match css_style_value_to_keyword(absolutized_value):
.round:
# The corner shape is a quarter of a convex ellipse. Equivalent to superellipse(1).
# NB: We cache this value since 'round' is the initial value of the `corner-*-*-shape` properties
if css_compute_corner_shape_cached_round_value == none:
# The static outlives any heap: it lives in the C heap, as the style values' other statics do.
let allocator: memory.Allocator = memory.heap
with allocator:
css_compute_corner_shape_cached_round_value = css_superellipse_of(1.0)
return css_compute_corner_shape_cached_round_value else trap("unreachable: set above")
.squircle:
# The corner shape is a quarter of a "squircle", a convex curve between round and square. Equivalent to superellipse(2).
return css_superellipse_of(2.0)
.square:
# The corner shape is a convex 90deg angle. Equivalent to superellipse(infinity).
return css_superellipse_of(math.infinity)
.bevel:
# The corner shape is a straight diagonal line, neither convex nor concave. Equivalent to superellipse(0).
return css_superellipse_of(0.0)
.scoop:
# The corner shape is a concave quarter-ellipse. Equivalent to superellipse(-1).
return css_superellipse_of(-1.0)
.notch:
# The corner shape is a concave 90deg angle. Equivalent to superellipse(-infinity).
return css_superellipse_of(-math.infinity)
_:
trap("VERIFY_NOT_REACHED")
## compute_font_size's `math_scaling_factor` lambda.
func css_math_scaling_factor(inherited_math_depth_: i32, computed_math_depth_: i32) -> f64:
var inherited_math_depth = inherited_math_depth_
var computed_math_depth = computed_math_depth_
# https://w3c.github.io/mathml-core/#the-math-script-level-property
# If the specified value font-size is math then the computed value of font-size is obtained by multiplying
# the inherited value of font-size by a nonzero scale factor calculated by the following procedure:
# 1. Let A be the inherited math-depth value, B the computed math-depth value, C be 0.71 and S be 1.0
let size_ratio = 0.71
var scale = 1.0
# 2. If A = B then return S.
var invert_scale_factor = false
if inherited_math_depth == computed_math_depth:
return scale
# If B < A, swap A and B and set InvertScaleFactor to true.
if computed_math_depth < inherited_math_depth:
let swapped = inherited_math_depth
inherited_math_depth = computed_math_depth
computed_math_depth = swapped
invert_scale_factor = true
# Otherwise B > A and set InvertScaleFactor to false.
else:
invert_scale_factor = false
# 3. Let E be B - A > 0.
# Donor quirk: `double e = (computed_math_depth - inherited_math_depth) > 0;` stores the comparison, 1.0.
let e = 1.0 if (computed_math_depth - inherited_math_depth) > 0 else 0.0
# FIXME: 4. If the inherited first available font has an OpenType MATH table:
# - If A ≤ 0 and B ≥ 2 then multiply S by scriptScriptPercentScaleDown and decrement E by 2.
# - Otherwise if A = 1 then multiply S by scriptScriptPercentScaleDown / scriptPercentScaleDown and decrement E by 1.
# - Otherwise if B = 1 then multiply S by scriptPercentScaleDown and decrement E by 1.
# 5. Multiply S by C^E.
scale *= math.pow(size_ratio, e)
# 6. Return S if InvertScaleFactor is false and 1/S otherwise.
if not invert_scale_factor:
return scale
return 1.0 / scale
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2563 Web::CSS::StyleComputer::compute_font_size.
pub func css_style_computer_compute_font_size(absolutized_value: CssStyleValue*, computed_math_depth: i32, inheritance_parent: DomAbstractElement?, initial_font_size: gfx.CssPixels) -> CssStyleValue*:
# https://drafts.csswg.org/css-fonts/#font-size-prop
# an absolute length
var inherited_font_size = initial_font_size
var inherited_math_depth = css_initial_values_math_depth()
if let parent = inheritance_parent:
let parent_computed_properties = dom_abstract_element_computed_properties(&parent) else trap("VERIFY: the parent has computed properties")
inherited_font_size = css_computed_properties_font_size(parent_computed_properties)
inherited_math_depth = css_computed_properties_math_depth(parent_computed_properties)
# <absolute-size>
if let absolute_size = css_data.keyword_to_absolute_size(css_style_value_to_keyword(absolutized_value)):
return css_length_style_value_create(css_length_make_px_css_pixels(css_style_computer_absolute_size_mapping(absolute_size, css_style_computer_default_user_font_size()))).css_style_value()
# <relative-size>
if let relative_size = css_data.keyword_to_relative_size(css_style_value_to_keyword(absolutized_value)):
return css_length_style_value_create(css_length_make_px_css_pixels(css_style_computer_relative_size_mapping(relative_size, inherited_font_size))).css_style_value()
# <length-percentage [0,∞]>
# A length value specifies an absolute font size (independent of the user agent’s font table). Negative lengths are invalid.
if css_style_value_is_length(absolutized_value):
return absolutized_value
# A percentage value specifies an absolute font size relative to the parent element’s computed font-size. Negative percentages are invalid.
if css_style_value_is_percentage(absolutized_value):
let percentage = css_percentage_style_value_percentage(css_style_value_as_percentage(absolutized_value))
return css_length_style_value_create(css_length_make_px_f64(gfx.mul_css_pixels_f64(inherited_font_size, css_percentage_as_fraction(&percentage)))).css_style_value()
if css_style_value_is_calculated(absolutized_value):
let context = CssCalculationResolutionContext(percentage_basis = CssCalculationResolutionContextPercentageBasis.css_length(css_length_make_px_css_pixels(inherited_font_size)))
let length = css_calculated_style_value_resolve_length(css_style_value_as_calculated(absolutized_value), context) else trap("VERIFY: the calculation resolves to a length")
return css_length_style_value_create(length).css_style_value()
# math
# Special mathematical scaling rules must be applied when determining the computed value of the font-size property.
if css_style_value_to_keyword(absolutized_value) == css_data.Keyword.math:
let math_scaling_factor = css_math_scaling_factor(inherited_math_depth, computed_math_depth)
return css_length_style_value_create(css_length_make_px_css_pixels(gfx.css_pixels_scaled_f64(&inherited_font_size, math_scaling_factor))).css_style_value()
trap("VERIFY_NOT_REACHED")
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2640 Web::CSS::StyleComputer::compute_font_style.
pub func css_style_computer_compute_font_style(absolutized_value: CssStyleValue*) -> CssStyleValue*:
# https://drafts.csswg.org/css-fonts-4/#font-style-prop
# the keyword specified, plus angle in degrees if specified
# NB: We always parse as a FontStyleStyleValue, but StylePropertyMap is able to set a KeywordStyleValue directly.
if css_style_value_is_keyword(absolutized_value):
let font_style = css_data.keyword_to_font_style_keyword(css_style_value_to_keyword(absolutized_value)) else trap("VERIFY: a font-style keyword")
return css_font_style_style_value_create(font_style).css_style_value()
return absolutized_value
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2652 Web::CSS::StyleComputer::compute_font_weight.
pub func css_style_computer_compute_font_weight(absolutized_value: CssStyleValue*, inheritance_parent: DomAbstractElement?) -> CssStyleValue*:
# https://drafts.csswg.org/css-fonts-4/#font-weight-prop
# a number, see below
var inherited_font_weight = css_initial_values_font_weight()
if let parent = inheritance_parent:
inherited_font_weight = css_computed_properties_font_weight(dom_abstract_element_computed_properties(&parent) else trap("VERIFY: the parent has computed properties"))
# <number [1,1000]>
if css_style_value_is_number(absolutized_value):
return absolutized_value
# AD-HOC: Anywhere we support a numbers we should also support calcs
if css_style_value_is_calculated(absolutized_value):
let number = css_calculated_style_value_resolve_number(css_style_value_as_calculated(absolutized_value), CssCalculationResolutionContext()) else trap("VERIFY: the calculation resolves to a number")
return css_number_style_value_create(number).css_style_value()
# normal
# Same as 400.
if css_style_value_to_keyword(absolutized_value) == css_data.Keyword.normal:
return css_number_style_value_create(400.0).css_style_value()
# bold
# Same as 700.
if css_style_value_to_keyword(absolutized_value) == css_data.Keyword.bold:
return css_number_style_value_create(700.0).css_style_value()
# Specified values of bolder and lighter indicate weights relative to the weight of the parent element. The
# computed weight is calculated based on the inherited font-weight value using the chart below.
#
# Inherited value (w) bolder lighter
# w < 100 400 No change
# 100 ≤ w < 350 400 100
# 350 ≤ w < 550 700 100
# 550 ≤ w < 750 900 400
# 750 ≤ w < 900 900 700
# 900 ≤ w No change 700
# bolder
# Specifies a bolder weight than the inherited value. See § 2.2.1 Relative Weights.
if css_style_value_to_keyword(absolutized_value) == css_data.Keyword.bolder:
if inherited_font_weight < 350.0:
return css_number_style_value_create(400.0).css_style_value()
if inherited_font_weight < 550.0:
return css_number_style_value_create(700.0).css_style_value()
if inherited_font_weight < 900.0:
return css_number_style_value_create(900.0).css_style_value()
return css_number_style_value_create(inherited_font_weight).css_style_value()
# lighter
# Specifies a lighter weight than the inherited value. See § 2.2.1 Relative Weights.
if css_style_value_to_keyword(absolutized_value) == css_data.Keyword.lighter:
if inherited_font_weight < 100.0:
return css_number_style_value_create(inherited_font_weight).css_style_value()
if inherited_font_weight < 550.0:
return css_number_style_value_create(100.0).css_style_value()
if inherited_font_weight < 750.0:
return css_number_style_value_create(400.0).css_style_value()
return css_number_style_value_create(700.0).css_style_value()
trap("VERIFY_NOT_REACHED")
## `PercentageStyleValue::create(Percentage(value))`.
func css_percentage_value_of(value: f64) -> CssStyleValue*:
return css_percentage_style_value_create(css_percentage_new(value)).css_style_value()
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2723 Web::CSS::StyleComputer::compute_font_width.
pub func css_style_computer_compute_font_width(absolutized_value: CssStyleValue*) -> CssStyleValue*:
# https://drafts.csswg.org/css-fonts-4/#font-width-prop
# a percentage, see below
# <percentage [0,∞]>
if css_style_value_is_percentage(absolutized_value):
return absolutized_value
# AD-HOC: We support calculated percentages as well
if css_style_value_is_calculated(absolutized_value):
let percentage = css_calculated_style_value_resolve_percentage(css_style_value_as_calculated(absolutized_value), CssCalculationResolutionContext()) else trap("VERIFY: the calculation resolves to a percentage")
return css_percentage_style_value_create(percentage).css_style_value()
match css_style_value_to_keyword(absolutized_value):
# ultra-condensed 50%
.ultra_condensed:
return css_percentage_value_of(50.0)
# extra-condensed 62.5%
.extra_condensed:
return css_percentage_value_of(62.5)
# condensed 75%
.condensed:
return css_percentage_value_of(75.0)
# semi-condensed 87.5%
.semi_condensed:
return css_percentage_value_of(87.5)
# normal 100%
.normal:
return css_percentage_value_of(100.0)
# semi-expanded 112.5%
.semi_expanded:
return css_percentage_value_of(112.5)
# expanded 125%
.expanded:
return css_percentage_value_of(125.0)
# extra-expanded 150%
.extra_expanded:
return css_percentage_value_of(150.0)
# ultra-expanded 200%
.ultra_expanded:
return css_percentage_value_of(200.0)
_:
trap("VERIFY_NOT_REACHED")
## Ported from Libraries/LibWeb/CSS/StyleComputer.cpp:2769 Web::CSS::StyleComputer::compute_line_height.
pub func css_style_computer_compute_line_height(absolutized_value: CssStyleValue*, computed_font_size: gfx.CssPixels) -> CssStyleValue*:
# https://drafts.csswg.org/css-inline-3/#line-height-property
# normal
# <length [0,∞]>
# <number [0,∞]>
if css_style_value_to_keyword(absolutized_value) == css_data.Keyword.normal or css_style_value_is_length(absolutized_value) or css_style_value_is_number(absolutized_value):
return absolutized_value
let percentage_basis_context = CssCalculationResolutionContext(percentage_basis = CssCalculationResolutionContextPercentageBasis.css_length(css_length_make_px_css_pixels(computed_font_size)))
# NOTE: We also support calc()'d lengths (percentages resolve to lengths so we don't have to handle them separately)
if css_style_value_is_calculated(absolutized_value) and css_calculated_style_value_resolves_to_length_percentage(css_style_value_as_calculated(absolutized_value)):
let length = css_calculated_style_value_resolve_length(css_style_value_as_calculated(absolutized_value), percentage_basis_context) else trap("VERIFY: the calculation resolves to a length")
return css_length_style_value_create(length).css_style_value()
# NOTE: We also support calc()'d numbers
if css_style_value_is_calculated(absolutized_value) and css_calculated_style_value_resolves_to_number(css_style_value_as_calculated(absolutized_value)):
let number = css_calculated_style_value_resolve_number(css_style_value_as_calculated(absolutized_value), percentage_basis_context) else trap("VERIFY: the calculation resolves to a number")
return css_number_style_value_create(number).css_style_value()
# <percentage [0,∞]>
if css_style_value_is_percentage(absolutized_value):
let percentage = css_percentage_style_value_percentage(css_style_value_as_percentage(absolutized_value))
return css_length_style_value_create(css_length_make_px_f64(gfx.mul_css_pixels_f64(computed_font_size, css_percentage_as_fraction(&percentage)))).css_style_value()
trap("VERIFY_NOT_REACHED")