dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / parsed_font_face.lucb
#==============================================================================================
#
# parsed_font_face - CSS::ParsedFontFace: an @font-face rule's descriptors as values the
# font computer uses
#
# DESCRIPTION:
# Port of LibWeb/CSS/ParsedFontFace.cpp (lines 1-241) and the inline members of
# ParsedFontFace.h (lines 19-64): the sources of a src descriptor, the family, weight
# range, slope, width, unicode ranges, metric overrides, font-display, named instance,
# language override and feature and variation settings of a CSSFontFaceDescriptors.
#
# Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
# Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Sources and descriptors =================================================================
## sources_from_style_value's add_source lambda: the source of one FontSourceStyleValue.
func css_parsed_font_face_add_source(sources: ak.Vector[CssParsedFontFaceSource]*, font_source: CssFontSourceStyleValue*):
match css_font_source_style_value_source(font_source):
.css_font_source_style_value_local(local_):
ak.vector_append_t(sources, CssParsedFontFaceSource(local_or_url = .fly_string(css_string_from_style_value(local_.name)), format_ = none, tech = ak.Vector[css_data.FontTech]()))
.css_url(url):
ak.vector_append_t(sources, CssParsedFontFaceSource(local_or_url = .css_url(url), format_ = css_font_source_style_value_format(font_source), tech = css_font_source_style_value_tech(font_source)))
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.cpp:30 Web::CSS::ParsedFontFace::sources_from_style_value.
pub func css_parsed_font_face_sources_from_style_value(style_value: CssStyleValue*) -> ak.Vector[CssParsedFontFaceSource]:
var sources: ak.Vector[CssParsedFontFaceSource]
if css_style_value_is_font_source(style_value):
css_parsed_font_face_add_source(&sources, css_style_value_as_font_source(style_value))
elif css_style_value_is_value_list(style_value):
let values = css_style_value_list_values(css_style_value_as_value_list(style_value))
for source in ak.vector_span_const(&values):
css_parsed_font_face_add_source(&sources, css_style_value_as_font_source(source))
return sources
## from_descriptors' extract_percentage_or_normal lambda.
func css_parsed_font_face_extract_percentage_or_normal(value: CssStyleValue*) -> CssPercentage?:
if css_style_value_is_percentage(value):
return css_percentage_style_value_percentage(css_style_value_as_percentage(value))
if css_style_value_is_calculated(value):
# FIXME: These should probably be simplified already?
return css_calculated_style_value_resolve_percentage(css_style_value_as_calculated(value), CssCalculationResolutionContext())
if css_style_value_to_keyword(value) == css_data.Keyword.normal:
return none
return none
## `descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(id))`.
func css_parsed_font_face_descriptor(descriptors: CssCssFontFaceDescriptors*, id: css_data.DescriptorId) -> CssStyleValue*?:
return css_descriptors_descriptor_or_initial_value(descriptors.css_descriptors(), css_descriptor_name_and_id_from_id(id))
## `static_cast<int>(StyleComputer::compute_font_weight(value, {})->as_number().number())`.
func css_parsed_font_face_computed_weight(value: CssStyleValue*) -> i32:
return (i32)css_number_style_value_number(css_style_value_as_number(css_style_computer_compute_font_weight(value, none)))
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.cpp:52 Web::CSS::ParsedFontFace::from_descriptors.
pub func css_parsed_font_face_from_descriptors(descriptors: CssCssFontFaceDescriptors*) -> CssParsedFontFace:
var font_family: ak.FlyString
if let value = css_parsed_font_face_descriptor(descriptors, css_data.DescriptorId.font_family):
font_family = css_string_from_style_value(value)
let parent_rule = css_style_declaration_parent_rule(descriptors.css_style_declaration()) else trap("VERIFY: descriptors.parent_rule()")
let parent_style_sheet = css_rule_parent_style_sheet(parent_rule) else trap("VERIFY: parent_rule()->parent_style_sheet()")
let owning_document = css_css_style_sheet_owning_document(parent_style_sheet) else trap("VERIFY: parent_style_sheet()->owning_document()")
let computation_context = CssComputationContext(length_resolution_context = css_length_resolution_context_for_document(owning_document))
var weight: CssFontWeightRange?
if let value = css_parsed_font_face_descriptor(descriptors, css_data.DescriptorId.font_weight):
# https://drafts.csswg.org/css-fonts-4/#font-prop-desc
# The auto values for these three descriptors have the following effects:
# - For font selection purposes, the font is selected as if the appropriate normal value (normal, normal or normal) is chosen
# - FIXME: For variation axis clamping, clamping does not occur
if css_style_value_to_keyword(value) == css_data.Keyword.auto:
weight = CssFontWeightRange(min = 400, max = 400)
else:
let absolutized = css_style_value_absolutized(value, computation_context)
let weight_values = css_style_value_list_values(css_style_value_as_value_list(absolutized))
if ak.vector_size(&weight_values) == 1:
let one_weight = css_parsed_font_face_computed_weight(*ak.vector_at_const(&weight_values, 0))
weight = CssFontWeightRange(min = one_weight, max = one_weight)
elif ak.vector_size(&weight_values) == 2:
# https://w3c.github.io/csswg-drafts/css-fonts/#font-prop-desc
# User agents must swap the computed value of the startpoint and endpoint of the range in order to forbid decreasing ranges.
let first_weight = css_parsed_font_face_computed_weight(*ak.vector_at_const(&weight_values, 0))
let second_weight = css_parsed_font_face_computed_weight(*ak.vector_at_const(&weight_values, 1))
weight = CssFontWeightRange(min = ak.min(first_weight, second_weight), max = ak.max(first_weight, second_weight))
else:
trap("VERIFY_NOT_REACHED")
var slope: i32?
if let value = css_parsed_font_face_descriptor(descriptors, css_data.DescriptorId.font_style):
# https://drafts.csswg.org/css-fonts-4/#font-prop-desc
# The auto values for these three descriptors have the following effects:
# - For font selection purposes, the font is selected as if the appropriate normal value (normal, normal or normal) is chosen
# - FIXME: For variation axis clamping, clamping does not occur
if css_style_value_to_keyword(value) == css_data.Keyword.auto:
slope = 0
else:
slope = css_font_style_style_value_to_font_slope(css_style_value_as_font_style(css_style_computer_compute_font_style(css_style_value_absolutized(value, computation_context))))
var width: i32?
if let value = css_parsed_font_face_descriptor(descriptors, css_data.DescriptorId.font_width):
# https://drafts.csswg.org/css-fonts-4/#font-prop-desc
# The auto values for these three descriptors have the following effects:
# - For font selection purposes, the font is selected as if the appropriate normal value (normal, normal or normal) is chosen
# - FIXME: For variation axis clamping, clamping does not occur
if css_style_value_to_keyword(value) == css_data.Keyword.auto:
width = 100
else:
width = (i32)css_dimension_style_value_raw_value(css_style_value_as_percentage(css_style_computer_compute_font_width(css_style_value_absolutized(value, computation_context))).css_dimension_style_value())
var sources: ak.Vector[CssParsedFontFaceSource]
if let value = css_parsed_font_face_descriptor(descriptors, css_data.DescriptorId.src):
sources = css_parsed_font_face_sources_from_style_value(value)
var unicode_ranges: ak.Vector[web_fonts.UnicodeRange]
if let value = css_parsed_font_face_descriptor(descriptors, css_data.DescriptorId.unicode_range):
let ranges = css_style_value_list_values(css_style_value_as_value_list(value))
for range in ak.vector_span_const(&ranges):
ak.vector_append_t(&unicode_ranges, css_unicode_range_style_value_unicode_range(css_style_value_as_unicode_range(range)))
var ascent_override: CssPercentage?
if let value = css_parsed_font_face_descriptor(descriptors, css_data.DescriptorId.ascent_override):
ascent_override = css_parsed_font_face_extract_percentage_or_normal(value)
var descent_override: CssPercentage?
if let value = css_parsed_font_face_descriptor(descriptors, css_data.DescriptorId.descent_override):
descent_override = css_parsed_font_face_extract_percentage_or_normal(value)
var line_gap_override: CssPercentage?
if let value = css_parsed_font_face_descriptor(descriptors, css_data.DescriptorId.line_gap_override):
line_gap_override = css_parsed_font_face_extract_percentage_or_normal(value)
# NB: FontDisplay font_display; is left uninitialized in C++ when the descriptor is missing; it never is (there is
# an initial value), and the port starts from the enum's zero, auto.
var font_display = css_data.FontDisplay.auto
if let value = css_parsed_font_face_descriptor(descriptors, css_data.DescriptorId.font_display):
font_display = css_data.keyword_to_font_display(css_style_value_to_keyword(value)) else css_data.FontDisplay.auto
var font_named_instance: ak.FlyString?
if let value = css_parsed_font_face_descriptor(descriptors, css_data.DescriptorId.font_named_instance):
if css_style_value_is_string(value):
font_named_instance = css_string_style_value_string_value(css_style_value_as_string(value))
var font_language_override: ak.FlyString?
if let value = css_parsed_font_face_descriptor(descriptors, css_data.DescriptorId.font_language_override):
if css_style_value_is_string(value):
font_language_override = css_string_style_value_string_value(css_style_value_as_string(value))
var font_feature_settings: ak.OrderedHashMap[ak.FlyString, i32, ak.FlyStringTraits]?
if let value = css_parsed_font_face_descriptor(descriptors, css_data.DescriptorId.font_feature_settings):
if css_style_value_to_keyword(value) == css_data.Keyword.normal:
# NB: Optional::clear() on an empty optional.
font_feature_settings = none
elif css_style_value_is_value_list(value):
let feature_tags = css_style_value_list_values(css_style_value_as_value_list(value))
var settings: ak.OrderedHashMap[ak.FlyString, i32, ak.FlyStringTraits]
ak.ordered_hash_map_ensure_capacity(&settings, ak.vector_size(&feature_tags))
for feature_tag_style_value in ak.vector_span_const(&feature_tags):
let feature_tag = css_style_value_as_open_type_tagged(feature_tag_style_value)
# FIXME: We should absolutize feature_tag.value() in case there are relative lengths
_ = ak.ordered_hash_map_set(&settings, css_open_type_tagged_style_value_tag(feature_tag), css_int_from_style_value(css_open_type_tagged_style_value_value(feature_tag)))
font_feature_settings = settings
var font_variation_settings: ak.OrderedHashMap[ak.FlyString, f64, ak.FlyStringTraits]?
if let value = css_parsed_font_face_descriptor(descriptors, css_data.DescriptorId.font_variation_settings):
if css_style_value_to_keyword(value) == css_data.Keyword.normal:
# NB: Optional::clear() on an empty optional.
font_variation_settings = none
elif css_style_value_is_value_list(value):
let variation_tags = css_style_value_list_values(css_style_value_as_value_list(value))
var settings: ak.OrderedHashMap[ak.FlyString, f64, ak.FlyStringTraits]
ak.ordered_hash_map_ensure_capacity(&settings, ak.vector_size(&variation_tags))
# FIXME: Absolutize these values to handle relative lengths within calcs
for variation_tag in ak.vector_span_const(&variation_tags):
let tagged = css_style_value_as_open_type_tagged(variation_tag)
_ = ak.ordered_hash_map_set(&settings, css_open_type_tagged_style_value_tag(tagged), css_number_from_style_value(css_open_type_tagged_style_value_value(tagged), none))
font_variation_settings = settings
var font_face: CssParsedFontFace = ---
css_parsed_font_face_construct(&font_face, parent_rule, font_family, weight, slope, width, sources, unicode_ranges, ascent_override, descent_override, line_gap_override, font_display, font_named_instance, font_language_override, font_feature_settings, font_variation_settings)
return font_face
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.cpp:222 Web::CSS::ParsedFontFace::ParsedFontFace.
pub func css_parsed_font_face_construct(this: CssParsedFontFace*, parent_rule: CssRule*, font_family: ak.FlyString, weight: CssFontWeightRange?, slope: i32?, width: i32?, sources: ak.Vector[CssParsedFontFaceSource], unicode_ranges: ak.Vector[web_fonts.UnicodeRange], ascent_override: CssPercentage?, descent_override: CssPercentage?, line_gap_override: CssPercentage?, font_display: css_data.FontDisplay, font_named_instance: ak.FlyString?, font_language_override: ak.FlyString?, font_feature_settings: ak.OrderedHashMap[ak.FlyString, i32, ak.FlyStringTraits]?, font_variation_settings: ak.OrderedHashMap[ak.FlyString, f64, ak.FlyStringTraits]?):
*this = CssParsedFontFace(
m_parent_rule = parent_rule,
m_font_family = font_family,
m_font_named_instance = font_named_instance,
m_weight = weight,
m_slope = slope,
m_width = width,
m_sources = sources,
m_unicode_ranges = unicode_ranges,
m_ascent_override = ascent_override,
m_descent_override = descent_override,
m_line_gap_override = line_gap_override,
m_font_display = font_display,
m_font_language_override = font_language_override,
m_font_feature_settings = font_feature_settings,
m_font_variation_settings = font_variation_settings,
)
# mark: ParsedFontFace.h ========================================================================
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:33 Web::CSS::ParsedFontFace::parent_rule.
pub func css_parsed_font_face_parent_rule(this: const CssParsedFontFace*) -> CssRule*:
return this.m_parent_rule
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:34 Web::CSS::ParsedFontFace::ascent_override.
pub func css_parsed_font_face_ascent_override(this: const CssParsedFontFace*) -> CssPercentage?:
return this.m_ascent_override
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:35 Web::CSS::ParsedFontFace::descent_override.
pub func css_parsed_font_face_descent_override(this: const CssParsedFontFace*) -> CssPercentage?:
return this.m_descent_override
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:36 Web::CSS::ParsedFontFace::font_display.
pub func css_parsed_font_face_font_display(this: const CssParsedFontFace*) -> css_data.FontDisplay:
return this.m_font_display
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:37 Web::CSS::ParsedFontFace::font_family.
pub func css_parsed_font_face_font_family(this: const CssParsedFontFace*) -> ak.FlyString:
return this.m_font_family
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:38 Web::CSS::ParsedFontFace::font_feature_settings.
pub func css_parsed_font_face_font_feature_settings(this: const CssParsedFontFace*) -> ak.OrderedHashMap[ak.FlyString, i32, ak.FlyStringTraits]?:
return this.m_font_feature_settings
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:39 Web::CSS::ParsedFontFace::font_language_override.
pub func css_parsed_font_face_font_language_override(this: const CssParsedFontFace*) -> ak.FlyString?:
return this.m_font_language_override
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:40 Web::CSS::ParsedFontFace::font_named_instance.
pub func css_parsed_font_face_font_named_instance(this: const CssParsedFontFace*) -> ak.FlyString?:
return this.m_font_named_instance
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:41 Web::CSS::ParsedFontFace::font_variation_settings.
pub func css_parsed_font_face_font_variation_settings(this: const CssParsedFontFace*) -> ak.OrderedHashMap[ak.FlyString, f64, ak.FlyStringTraits]?:
return this.m_font_variation_settings
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:42 Web::CSS::ParsedFontFace::slope.
pub func css_parsed_font_face_slope(this: const CssParsedFontFace*) -> i32?:
return this.m_slope
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:43 Web::CSS::ParsedFontFace::weight.
pub func css_parsed_font_face_weight(this: const CssParsedFontFace*) -> CssFontWeightRange?:
return this.m_weight
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:44 Web::CSS::ParsedFontFace::width.
pub func css_parsed_font_face_width(this: const CssParsedFontFace*) -> i32?:
return this.m_width
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:45 Web::CSS::ParsedFontFace::line_gap_override.
pub func css_parsed_font_face_line_gap_override(this: const CssParsedFontFace*) -> CssPercentage?:
return this.m_line_gap_override
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:46 Web::CSS::ParsedFontFace::sources.
pub func css_parsed_font_face_sources(this: const CssParsedFontFace*) -> ak.Vector[CssParsedFontFaceSource]:
return this.m_sources
## Ported from Libraries/LibWeb/CSS/ParsedFontFace.h:47 Web::CSS::ParsedFontFace::unicode_ranges.
pub func css_parsed_font_face_unicode_ranges(this: const CssParsedFontFace*) -> ak.Vector[web_fonts.UnicodeRange]:
return this.m_unicode_ranges