dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / font_feature_data.lucb
#==============================================================================================
#
# font_feature_data - CSS::FontFeatureData: the font-variant-*, font-feature-settings,
# font-kerning and text-rendering values as OpenType shape features
#
# DESCRIPTION:
# Port of LibWeb/CSS/FontFeatureData.cpp (lines 1-423) and the operator== and AK::Traits
# hashes of FontFeatureData.h (lines 17-172). The C++ structs compare with a defaulted
# operator==; they hold FlyStrings (a union), so Luce gives them no `==` and the
# comparisons are spelled out here. The traits in generated/types_traits call the hashes.
#
# Copyright (c) 2024, Johan Dahlin <jdahlin@gmail.com>
# Copyright (c) 2026, Callum Law <callumlaw1709@outlook.com>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: FontFeatureData.h: equality =============================================================
## Ported from Libraries/LibWeb/CSS/FontFeatureData.h:32 Web::CSS::FontFeatureValueKey::operator== (defaulted).
pub func css_font_feature_value_key_eq(this: const CssFontFeatureValueKey*, other: const CssFontFeatureValueKey*) -> bool:
return this.type_ == other.type_ and ak.fly_string_eq_fly_string(&this.name, &other.name)
## Ported from Libraries/LibWeb/CSS/FontFeatureData.h:40 Web::CSS::FontVariantAlternates::operator== (defaulted).
pub func css_font_variant_alternates_eq(this: const CssFontVariantAlternates*, other: const CssFontVariantAlternates*) -> bool:
if this.historical_forms != other.historical_forms:
return false
let entries = ak.vector_span_const(&this.font_feature_value_entries)
let other_entries = ak.vector_span_const(&other.font_feature_value_entries)
if entries.length != other_entries.length:
return false
for i in 0..<entries.length:
if not css_font_feature_value_key_eq(&entries[i], &other_entries[i]):
return false
return true
## Ported from Libraries/LibWeb/CSS/FontFeatureData.h:87 Web::CSS::FontFeatureData::operator== (defaulted):
## the members in declaration order (FontVariantEastAsian, FontVariantLigatures and
## FontVariantNumeric hold only scalars and optional enums, so they have Luce's ==).
pub func css_font_feature_data_eq(this: const CssFontFeatureData*, other: const CssFontFeatureData*) -> bool:
if let alternates = this.font_variant_alternates:
let other_alternates = other.font_variant_alternates else return false
if not css_font_variant_alternates_eq(&alternates, &other_alternates):
return false
elif other.font_variant_alternates != none:
return false
return this.font_variant_caps == other.font_variant_caps and this.font_variant_east_asian == other.font_variant_east_asian and this.font_variant_emoji == other.font_variant_emoji and this.font_variant_ligatures == other.font_variant_ligatures and this.font_variant_numeric == other.font_variant_numeric and this.font_variant_position == other.font_variant_position and ak.hash_map_eq(&this.font_feature_settings, &other.font_feature_settings) and this.font_kerning == other.font_kerning and this.text_rendering == other.text_rendering
# mark: FontFeatureData.h: AK::Traits ===========================================================
## `optional.has_value() ? to_underlying(optional.value()) : -1` in the hashes below, where -1
## is converted to pair_int_hash's unsigned argument.
let css_font_feature_data_no_value: u32 = 0xFFFFFFFF
## Ported from Libraries/LibWeb/CSS/FontFeatureData.h:96 AK::Traits<Web::CSS::FontFeatureValueKey>::hash.
pub func css_font_feature_value_key_traits_hash(data: const CssFontFeatureValueKey*) -> u32:
return ak.pair_int_hash((u32)data.type_, ak.fly_string_hash(&data.name))
## Ported from Libraries/LibWeb/CSS/FontFeatureData.h:104 AK::Traits<Web::CSS::FontVariantAlternates>::hash.
pub func css_font_variant_alternates_traits_hash(data: const CssFontVariantAlternates*) -> u32:
var hash_: u32 = 1 if data.historical_forms else 0
for entry in ak.vector_span_const(&data.font_feature_value_entries):
hash_ = ak.pair_int_hash(hash_, css_font_feature_value_key_traits_hash(&entry))
return hash_
## Ported from Libraries/LibWeb/CSS/FontFeatureData.h:117 AK::Traits<Web::CSS::FontVariantEastAsian>::hash.
pub func css_font_variant_east_asian_traits_hash(data: const CssFontVariantEastAsian*) -> u32:
var hash_: u32 = 1 if data.ruby else 0
hash_ = ak.pair_int_hash(hash_, match data.variant:
.some(variant) => (u32)variant
.none => css_font_feature_data_no_value)
hash_ = ak.pair_int_hash(hash_, match data.width:
.some(width) => (u32)width
.none => css_font_feature_data_no_value)
return hash_
## Ported from Libraries/LibWeb/CSS/FontFeatureData.h:128 AK::Traits<Web::CSS::FontVariantLigatures>::hash.
pub func css_font_variant_ligatures_traits_hash(data: const CssFontVariantLigatures*) -> u32:
var hash_: u32 = 1 if data.none_ else 0
hash_ = ak.pair_int_hash(hash_, match data.common:
.some(common) => (u32)common
.none => css_font_feature_data_no_value)
hash_ = ak.pair_int_hash(hash_, match data.discretionary:
.some(discretionary) => (u32)discretionary
.none => css_font_feature_data_no_value)
hash_ = ak.pair_int_hash(hash_, match data.historical:
.some(historical) => (u32)historical
.none => css_font_feature_data_no_value)
hash_ = ak.pair_int_hash(hash_, match data.contextual:
.some(contextual) => (u32)contextual
.none => css_font_feature_data_no_value)
return hash_
## Ported from Libraries/LibWeb/CSS/FontFeatureData.h:141 AK::Traits<Web::CSS::FontVariantNumeric>::hash.
pub func css_font_variant_numeric_traits_hash(data: const CssFontVariantNumeric*) -> u32:
var hash_: u32 = 1 if data.ordinal else 0
hash_ = ak.pair_int_hash(hash_, 1 if data.slashed_zero else 0)
hash_ = ak.pair_int_hash(hash_, match data.figure:
.some(figure) => (u32)figure
.none => css_font_feature_data_no_value)
hash_ = ak.pair_int_hash(hash_, match data.spacing:
.some(spacing) => (u32)spacing
.none => css_font_feature_data_no_value)
hash_ = ak.pair_int_hash(hash_, match data.fraction:
.some(fraction) => (u32)fraction
.none => css_font_feature_data_no_value)
return hash_
## Ported from Libraries/LibWeb/CSS/FontFeatureData.h:154 AK::Traits<Web::CSS::FontFeatureData>::hash.
pub func css_font_feature_data_traits_hash(data: const CssFontFeatureData*) -> u32:
var hash_: u32 = 0
hash_ = ak.pair_int_hash(hash_, match data.font_variant_alternates:
.some(alternates) => css_font_variant_alternates_traits_hash(&alternates)
.none => css_font_feature_data_no_value)
hash_ = ak.pair_int_hash(hash_, (u32)data.font_variant_caps)
hash_ = ak.pair_int_hash(hash_, match data.font_variant_east_asian:
.some(east_asian) => css_font_variant_east_asian_traits_hash(&east_asian)
.none => css_font_feature_data_no_value)
hash_ = ak.pair_int_hash(hash_, (u32)data.font_variant_emoji)
hash_ = ak.pair_int_hash(hash_, match data.font_variant_ligatures:
.some(ligatures) => css_font_variant_ligatures_traits_hash(&ligatures)
.none => css_font_feature_data_no_value)
hash_ = ak.pair_int_hash(hash_, match data.font_variant_numeric:
.some(numeric) => css_font_variant_numeric_traits_hash(&numeric)
.none => css_font_feature_data_no_value)
hash_ = ak.pair_int_hash(hash_, (u32)data.font_variant_position)
hash_ = ak.pair_int_hash(hash_, (u32)data.font_kerning)
hash_ = ak.pair_int_hash(hash_, (u32)data.text_rendering)
for entry in ak.hash_map_begin_const(&data.font_feature_settings):
hash_ = ak.pair_int_hash(hash_, ak.pair_int_hash(ak.fly_string_hash(&entry.key), (u32)entry.value))
return hash_
# mark: FontFeatureData.cpp =====================================================================
## Ported from Libraries/LibWeb/CSS/FontFeatureData.cpp:13 Web::CSS::font_feature_value_type_from_string.
pub func css_font_feature_value_type_from_string(string: ak.FlyString) -> CssFontFeatureValueType?:
if ak.fly_string_eq_string_view(&string, ak.sv("stylistic")):
return CssFontFeatureValueType.stylistic
if ak.fly_string_eq_string_view(&string, ak.sv("styleset")):
return CssFontFeatureValueType.styleset
if ak.fly_string_eq_string_view(&string, ak.sv("character-variant")):
return CssFontFeatureValueType.character_variant
if ak.fly_string_eq_string_view(&string, ak.sv("swash")):
return CssFontFeatureValueType.swash
if ak.fly_string_eq_string_view(&string, ak.sv("ornaments")):
return CssFontFeatureValueType.ornaments
if ak.fly_string_eq_string_view(&string, ak.sv("annotation")):
return CssFontFeatureValueType.annotation
return none
## `features.set("<tag>"_fly_string, value)` on a HashMap<FlyString, u8>.
func css_font_feature_set(features: ak.HashMap[ak.FlyString, u8, ak.FlyStringTraits]*, tag: str, value: u8):
_ = ak.hash_map_set(features, ak.fly_string_from_str(tag), value)
## to_shape_features' disable_all_ligatures lambda.
func css_font_feature_data_disable_all_ligatures(features: ak.HashMap[ak.FlyString, u8, ak.FlyStringTraits]*):
css_font_feature_set(features, "liga", 0)
css_font_feature_set(features, "clig", 0)
css_font_feature_set(features, "dlig", 0)
css_font_feature_set(features, "hlig", 0)
css_font_feature_set(features, "calt", 0)
## `features.set(MUST(String::formatted("<prefix>{:02}", feature)), value)`: the u32 value
## stored in the HashMap<FlyString, u8> narrows as C++'s implicit conversion does.
func css_font_feature_set_numbered(features: ak.HashMap[ak.FlyString, u8, ak.FlyStringTraits]*, format_string: str, feature: u32, value: u32):
let name = css_string_formatted(format_string, [ak.arg_u64((u64)feature)])
_ = ak.hash_map_set(features, ak.fly_string_from_string(name), (u8)value)
## to_shape_features' font_variant_features lambda: the features of the font-variant
## properties, font-kerning and text-rendering.
func css_font_feature_data_font_variant_features(this: const CssFontFeatureData*, font_feature_values: const ak.HashMap[CssFontFeatureValueKey, ak.Vector[u32], CssFontFeatureValueKeyTraits]*) -> ak.HashMap[ak.FlyString, u8, ak.FlyStringTraits]:
var features: ak.HashMap[ak.FlyString, u8, ak.FlyStringTraits]
# 6.4 https://drafts.csswg.org/css-fonts/#font-variant-ligatures-prop
if let ligature = this.font_variant_ligatures:
if ligature.none_:
# Specifies that all types of ligatures and contextual forms covered by this property are explicitly disabled.
css_font_feature_data_disable_all_ligatures(&features)
else:
if let common = ligature.common:
match common:
.common_ligatures:
# Enables display of common ligatures (OpenType features: liga, clig).
css_font_feature_set(&features, "liga", 1)
css_font_feature_set(&features, "clig", 1)
.no_common_ligatures:
# Disables display of common ligatures (OpenType features: liga, clig).
css_font_feature_set(&features, "liga", 0)
css_font_feature_set(&features, "clig", 0)
_: _ = 0
if let discretionary = ligature.discretionary:
match discretionary:
.discretionary_ligatures:
# Enables display of discretionary ligatures (OpenType feature: dlig).
css_font_feature_set(&features, "dlig", 1)
.no_discretionary_ligatures:
# Disables display of discretionary ligatures (OpenType feature: dlig).
css_font_feature_set(&features, "dlig", 0)
_: _ = 0
if let historical = ligature.historical:
match historical:
.historical_ligatures:
# Enables display of historical ligatures (OpenType feature: hlig).
css_font_feature_set(&features, "hlig", 1)
.no_historical_ligatures:
# Disables display of historical ligatures (OpenType feature: hlig).
css_font_feature_set(&features, "hlig", 0)
_: _ = 0
if let contextual = ligature.contextual:
match contextual:
.contextual:
# Enables display of contextual ligatures (OpenType feature: calt).
css_font_feature_set(&features, "calt", 1)
.no_contextual:
# Disables display of contextual ligatures (OpenType feature: calt).
css_font_feature_set(&features, "calt", 0)
_: _ = 0
elif this.text_rendering == css_data.TextRendering.optimizespeed:
# AD-HOC: Disable ligatures if font-variant-ligatures is set to normal and text rendering is set to optimize speed.
css_font_feature_data_disable_all_ligatures(&features)
else:
# A value of normal specifies that common default features are enabled, as described in detail in the next section.
css_font_feature_set(&features, "liga", 1)
css_font_feature_set(&features, "clig", 1)
# 6.5 https://drafts.csswg.org/css-fonts/#font-variant-position-prop
match this.font_variant_position:
.normal:
# None of the features listed below are enabled.
_ = 0
.sub:
# Enables display of subscripts (OpenType feature: subs).
css_font_feature_set(&features, "subs", 1)
.super:
# Enables display of superscripts (OpenType feature: sups).
css_font_feature_set(&features, "sups", 1)
_: _ = 0
# 6.6 https://drafts.csswg.org/css-fonts/#font-variant-caps-prop
match this.font_variant_caps:
.normal:
# None of the features listed below are enabled.
_ = 0
.small_caps:
# Enables display of small capitals (OpenType feature: smcp). Small-caps glyphs typically use the form of uppercase letters but are reduced to the size of lowercase letters.
css_font_feature_set(&features, "smcp", 1)
.all_small_caps:
# Enables display of small capitals for both upper and lowercase letters (OpenType features: c2sc, smcp).
css_font_feature_set(&features, "c2sc", 1)
css_font_feature_set(&features, "smcp", 1)
.petite_caps:
# Enables display of petite capitals (OpenType feature: pcap).
css_font_feature_set(&features, "pcap", 1)
.all_petite_caps:
# Enables display of petite capitals for both upper and lowercase letters (OpenType features: c2pc, pcap).
css_font_feature_set(&features, "c2pc", 1)
css_font_feature_set(&features, "pcap", 1)
.unicase:
# Enables display of mixture of small capitals for uppercase letters with normal lowercase letters (OpenType feature: unic).
css_font_feature_set(&features, "unic", 1)
.titling_caps:
# Enables display of titling capitals (OpenType feature: titl).
css_font_feature_set(&features, "titl", 1)
_: _ = 0
# 6.7 https://drafts.csswg.org/css-fonts/#font-variant-numeric-prop
if let numeric = this.font_variant_numeric:
if numeric.figure == css_data.NumericFigureValue.oldstyle_nums:
# Enables display of old-style numerals (OpenType feature: onum).
css_font_feature_set(&features, "onum", 1)
elif numeric.figure == css_data.NumericFigureValue.lining_nums:
# Enables display of lining numerals (OpenType feature: lnum).
css_font_feature_set(&features, "lnum", 1)
if numeric.spacing == css_data.NumericSpacingValue.proportional_nums:
# Enables display of proportional numerals (OpenType feature: pnum).
css_font_feature_set(&features, "pnum", 1)
elif numeric.spacing == css_data.NumericSpacingValue.tabular_nums:
# Enables display of tabular numerals (OpenType feature: tnum).
css_font_feature_set(&features, "tnum", 1)
if numeric.fraction == css_data.NumericFractionValue.diagonal_fractions:
# Enables display of diagonal fractions (OpenType feature: frac).
css_font_feature_set(&features, "frac", 1)
elif numeric.fraction == css_data.NumericFractionValue.stacked_fractions:
# Enables display of stacked fractions (OpenType feature: afrc).
css_font_feature_set(&features, "afrc", 1)
if numeric.ordinal:
# Enables display of letter forms used with ordinal numbers (OpenType feature: ordn).
css_font_feature_set(&features, "ordn", 1)
if numeric.slashed_zero:
# Enables display of slashed zeros (OpenType feature: zero).
css_font_feature_set(&features, "zero", 1)
# 6.8 https://drafts.csswg.org/css-fonts/#font-variant-alternates-prop
# FIXME: These values never apply to generic font families
if let alternates = this.font_variant_alternates:
if alternates.historical_forms:
# Enables display of historical forms (OpenType feature: hist).
css_font_feature_set(&features, "hist", 1)
for key in ak.vector_span_const(&alternates.font_feature_value_entries):
let values = ak.hash_map_get_k_const(font_feature_values, key) else continue
match key.type_:
.stylistic:
# Enables display of stylistic alternates (font specific, OpenType feature: salt <feature-index>).
# stylistic(<feature-value-name>)
css_font_feature_set(&features, "salt", (u8)*ak.vector_at_const(values, 0))
.styleset:
# Enables display with stylistic sets (font specific, OpenType feature: ss<feature-index> OpenType
# currently defines ss01 through ss20).
# https://drafts.csswg.org/css-fonts/#multi-value-features
# For the styleset() property value and @styleset rule, multiple values indicate the style sets to
# be enabled. Values between 1 and 99 enable OpenType features ss01 through ss99. However, the
# OpenType standard only officially defines ss01 through ss20. For OpenType fonts, values greater
# than 99 or equal to 0 do not generate a syntax error when parsed but enable no OpenType features.
for value in ak.vector_span_const(values):
if value > 99 or value == 0:
continue
css_font_feature_set_numbered(&features, "ss{:02}", value, 1)
.character_variant:
# Enables display of specific character variants (font specific, OpenType feature:
# cv<feature-index> OpenType currently defines cv01 through cv99).
# https://drafts.csswg.org/css-fonts/#multi-value-features
# For <@character-variant>, a single value between 1 and 99 indicates the enabling of OpenType
# feature cv01 through cv99. For OpenType fonts, values greater than 99 or equal to 0 are ignored
# but do not generate a syntax error when parsed but enable no OpenType features. When two values
# are listed, the first value indicates the feature used and the second the value passed for that
# feature.
let feature = *ak.vector_at_const(values, 0)
if feature > 99 or feature == 0:
continue
let value = *ak.vector_at_const(values, 1) if ak.vector_size(values) > 1 else 1
css_font_feature_set_numbered(&features, "cv{:02}", feature, value)
.swash:
# Enables display of swash glyphs (font specific, OpenType feature: swsh <feature-index>,
# cswh <feature-index>).
css_font_feature_set(&features, "swsh", (u8)*ak.vector_at_const(values, 0))
css_font_feature_set(&features, "cswh", (u8)*ak.vector_at_const(values, 0))
.ornaments:
# Enables replacement of default glyphs with ornaments, if provided in the font (font specific,
# OpenType feature: ornm <feature-index>).
css_font_feature_set(&features, "ornm", (u8)*ak.vector_at_const(values, 0))
.annotation:
# annotation(<feature-value-name>)
# Enables display of alternate annotation forms (font specific, OpenType feature: nalt <feature-index>).
css_font_feature_set(&features, "nalt", (u8)*ak.vector_at_const(values, 0))
_: _ = 0
# 6.10 https://drafts.csswg.org/css-fonts/#font-variant-east-asian-prop
if let east_asian = this.font_variant_east_asian:
if let variant = east_asian.variant:
match variant:
.jis78:
# Enables display of JIS78 forms (OpenType feature: jp78).
css_font_feature_set(&features, "jp78", 1)
.jis83:
# Enables display of JIS83 forms (OpenType feature: jp83).
css_font_feature_set(&features, "jp83", 1)
.jis90:
# Enables display of JIS90 forms (OpenType feature: jp90).
css_font_feature_set(&features, "jp90", 1)
.jis04:
# Enables display of JIS04 forms (OpenType feature: jp04).
css_font_feature_set(&features, "jp04", 1)
.simplified:
# Enables display of simplified forms (OpenType feature: smpl).
css_font_feature_set(&features, "smpl", 1)
.traditional:
# Enables display of traditional forms (OpenType feature: trad).
css_font_feature_set(&features, "trad", 1)
_: _ = 0
if let width = east_asian.width:
match width:
.full_width:
# Enables display of full-width forms (OpenType feature: fwid).
css_font_feature_set(&features, "fwid", 1)
.proportional_width:
# Enables display of proportional-width forms (OpenType feature: pwid).
css_font_feature_set(&features, "pwid", 1)
_: _ = 0
if east_asian.ruby:
# Enables display of ruby forms (OpenType feature: ruby).
css_font_feature_set(&features, "ruby", 1)
# FIXME: vkrn should be enabled for vertical text.
match this.font_kerning:
.auto:
# AD-HOC: Disable kerning if font-kerning is set to normal and text rendering is set to optimize speed.
css_font_feature_set(&features, "kern", 1 if this.text_rendering != css_data.TextRendering.optimizespeed else 0)
.normal:
css_font_feature_set(&features, "kern", 1)
.none_:
css_font_feature_set(&features, "kern", 0)
_: _ = 0
return features
## Ported from Libraries/LibWeb/CSS/FontFeatureData.cpp:31 Web::CSS::FontFeatureData::to_shape_features.
pub func css_font_feature_data_to_shape_features(this: const CssFontFeatureData*, font_feature_values: const ak.HashMap[CssFontFeatureValueKey, ak.Vector[u32], CssFontFeatureValueKeyTraits]*) -> ak.Vector[web_fonts.ShapeFeature]:
var merged_features: ak.HashMap[ak.FlyString, u8, ak.FlyStringTraits]
# https://drafts.csswg.org/css-fonts-4/#feature-variation-precedence
# FIXME: 1. Font features enabled by default are applied, including features required for a given script. See § 7.1
# Default features for a description of these.
# FIXME: 2. Font variations as enabled by the font-weight, font-width, and font-style properties are applied.
#
# The application of the value enabled by font-style is affected by font selection, because this property
# might select an italic or an oblique font. The value applied is the closest matching value as
# determined by the font matching algorithm. User agents must apply at most one value due to the
# font-style property; both "ital" and "slnt" values must not be set together.
#
# If the selected font is defined in an @font-face rule, then the values applied at this step should be
# clamped to the value of the font-weight, font-width, and font-style descriptors in that @font-face
# rule.
#
# Then, the values applied in this step should be clamped (possibly again) to the values that are
# supported by the font.
# FIXME: 3. The language specified by the inherited value of lang/xml:lang is applied.
# FIXME: 4. If the font is defined via an @font-face rule, the font language override implied by the
# font-language-override descriptor in the @font-face rule is applied.
# FIXME: 5. If the font is defined via an @font-face rule, that @font-face rule includes at least one valid
# font-named-instance descriptor with a value other than 'font-named-instance/none', and the loaded font
# resource includes a named instance with that name according to the § 5.1 Localized name matching rules,
# then all the variation values represented by that named instance are applied. These values are clamped
# to the values that are supported by the font.
# FIXME: 6. If the font is defined via an @font-face rule, the font variations implied by the
# font-variation-settings descriptor in the @font-face rule are applied.
# FIXME: 7. If the font is defined via an @font-face rule, the font features implied by the font-feature-settings
# descriptor in the @font-face rule are applied.
# FIXME: 8. The font language override implied by the value of the font-language-override property is applied.
# FIXME: 9. Font variations implied by the value of the font-optical-sizing property are applied.
# 10. Font features implied by the value of the font-variant property, the related font-variant subproperties and
# any other CSS property that uses OpenType features (e.g. the font-kerning property) are applied.
let font_variant_features = css_font_feature_data_font_variant_features(this, font_feature_values)
ak.hash_map_update(&merged_features, &font_variant_features)
# FIXME: 11. Feature settings determined by properties other than font-variant or font-feature-settings are
# applied. For example, setting a non-default value for the letter-spacing property disables optional
# ligatures.
# FIXME: 12. Font variations implied by the value of the font-variation-settings property are applied. These values
# should be clamped to the values that are supported by the font.
# 13. Font features implied by the value of font-feature-settings property are applied.
ak.hash_map_update(&merged_features, &this.font_feature_settings)
var shape_features: ak.Vector[web_fonts.ShapeFeature]
ak.vector_ensure_capacity(&shape_features, ak.hash_map_size(&merged_features))
for it in ak.hash_map_begin(&merged_features):
let key_string_view = ak.fly_string_bytes(&it.key)
ak.vector_unchecked_append_u(&shape_features, web_fonts.ShapeFeature(tag = [key_string_view[0], key_string_view[1], key_string_view[2], key_string_view[3]], value = (u32)it.value))
return shape_features