LibWeb: Simplify handling of font-variation-settings

Since we resolve any relative lengths at compute time there's no need
for the value to be passed around as a `NumberOrCalculated` and we can
just resolve it within `ComputedProperties::font_variation_settings`.

The only place this is used it is used with value_or so there's no need
to return it is an `Optional`.

This is only used for loading fonts (which occurs during style
computation) so there's no need to store it in `ComputedValues`
This commit is contained in:
Callum Law 2025-11-06 15:55:21 +13:00 committed by Sam Atkins
parent 4fb28539a9
commit dfa47d9ed6
Notes: github-actions[bot] 2025-12-05 10:04:40 +00:00
7 changed files with 14 additions and 23 deletions

View file

@ -1945,7 +1945,7 @@ HashMap<StringView, u8> ComputedProperties::font_feature_settings() const
return {};
}
Optional<HashMap<FlyString, NumberOrCalculated>> ComputedProperties::font_variation_settings() const
HashMap<FlyString, double> ComputedProperties::font_variation_settings() const
{
auto const& value = property(PropertyID::FontVariationSettings);
@ -1954,7 +1954,7 @@ Optional<HashMap<FlyString, NumberOrCalculated>> ComputedProperties::font_variat
if (value.is_value_list()) {
auto const& axis_tags = value.as_value_list().values();
HashMap<FlyString, NumberOrCalculated> result;
HashMap<FlyString, double> result;
result.ensure_capacity(axis_tags.size());
for (auto const& tag_value : axis_tags) {
auto const& axis_tag = tag_value->as_open_type_tagged();
@ -1963,7 +1963,7 @@ Optional<HashMap<FlyString, NumberOrCalculated>> ComputedProperties::font_variat
result.set(axis_tag.tag(), axis_tag.value()->as_number().number());
} else {
VERIFY(axis_tag.value()->is_calculated());
result.set(axis_tag.tag(), NumberOrCalculated { axis_tag.value()->as_calculated() });
result.set(axis_tag.tag(), axis_tag.value()->as_calculated().resolve_number({}).value());
}
}
return result;

View file

@ -166,7 +166,7 @@ public:
FontKerning font_kerning() const;
Optional<FlyString> font_language_override() const;
HashMap<StringView, u8> font_feature_settings() const;
Optional<HashMap<FlyString, NumberOrCalculated>> font_variation_settings() const;
HashMap<FlyString, double> font_variation_settings() const;
GridTrackSizeList grid_auto_columns() const;
GridTrackSizeList grid_auto_rows() const;
GridTrackSizeList grid_template_columns() const;

View file

@ -663,7 +663,7 @@ public:
double font_weight() const { return m_inherited.font_weight; }
Gfx::ShapeFeatures font_features() const { return m_inherited.font_features; }
Optional<FlyString> font_language_override() const { return m_inherited.font_language_override; }
Optional<HashMap<FlyString, NumberOrCalculated>> font_variation_settings() const { return m_inherited.font_variation_settings; }
HashMap<FlyString, double> font_variation_settings() const { return m_inherited.font_variation_settings; }
CSSPixels line_height() const { return m_inherited.line_height; }
Time transition_delay() const { return m_noninherited.transition_delay; }
@ -700,7 +700,7 @@ protected:
double font_weight { InitialValues::font_weight() };
Gfx::ShapeFeatures font_features { InitialValues::font_features() };
Optional<FlyString> font_language_override;
Optional<HashMap<FlyString, NumberOrCalculated>> font_variation_settings;
HashMap<FlyString, double> font_variation_settings;
CSSPixels line_height { InitialValues::line_height() };
BorderCollapse border_collapse { InitialValues::border_collapse() };
EmptyCells empty_cells { InitialValues::empty_cells() };
@ -906,7 +906,7 @@ public:
void set_font_weight(double font_weight) { m_inherited.font_weight = font_weight; }
void set_font_features(Gfx::ShapeFeatures font_features) { m_inherited.font_features = move(font_features); }
void set_font_language_override(Optional<FlyString> font_language_override) { m_inherited.font_language_override = move(font_language_override); }
void set_font_variation_settings(Optional<HashMap<FlyString, NumberOrCalculated>> value) { m_inherited.font_variation_settings = move(value); }
void set_font_variation_settings(HashMap<FlyString, double> value) { m_inherited.font_variation_settings = move(value); }
void set_line_height(CSSPixels line_height) { m_inherited.line_height = line_height; }
void set_border_spacing_horizontal(Length border_spacing_horizontal) { m_inherited.border_spacing_horizontal = move(border_spacing_horizontal); }
void set_border_spacing_vertical(Length border_spacing_vertical) { m_inherited.border_spacing_vertical = move(border_spacing_vertical); }

View file

@ -1811,7 +1811,7 @@ CSSPixels StyleComputer::relative_size_mapping(RelativeSize relative_size, CSSPi
VERIFY_NOT_REACHED();
}
RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(StyleValue const& font_family, CSSPixels const& font_size, int slope, double font_weight, Percentage const& font_width, HashMap<FlyString, NumberOrCalculated> const& font_variation_settings, Length::ResolutionContext const& length_resolution_context) const
RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(StyleValue const& font_family, CSSPixels const& font_size, int slope, double font_weight, Percentage const& font_width, HashMap<FlyString, double> const& font_variation_settings) const
{
// FIXME: We round to int here as that is what is expected by our font infrastructure below
auto width = round_to<int>(font_width.value());
@ -1836,21 +1836,14 @@ RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(
Gfx::FontVariationSettings variation;
variation.set_weight(font_weight);
CalculationResolutionContext context {
.length_resolution_context = length_resolution_context,
};
for (auto const& [tag_string, value] : font_variation_settings) {
auto string_view = tag_string.bytes_as_string_view();
if (string_view.length() != 4)
continue;
auto tag = Gfx::FourCC(string_view.characters_without_null_termination());
auto resolved_value = value.resolved(context);
if (!resolved_value.has_value())
continue;
variation.axes.set(tag, resolved_value.release_value());
variation.axes.set(tag, value);
}
for (auto const& loader : loaders) {
@ -1992,7 +1985,7 @@ void StyleComputer::compute_font(ComputedProperties& style, Optional<DOM::Abstra
auto const& font_family = style.property(CSS::PropertyID::FontFamily);
auto font_list = compute_font_for_style_values(font_family, style.font_size(), style.font_slope(), style.font_weight(), style.font_width(), style.font_variation_settings().value_or({}), font_computation_context.length_resolution_context);
auto font_list = compute_font_for_style_values(font_family, style.font_size(), style.font_slope(), style.font_weight(), style.font_width(), style.font_variation_settings());
VERIFY(font_list);
VERIFY(!font_list->is_empty());

View file

@ -150,7 +150,7 @@ public:
static CSSPixels default_user_font_size();
static CSSPixels absolute_size_mapping(AbsoluteSize, CSSPixels default_font_size);
static CSSPixels relative_size_mapping(RelativeSize, CSSPixels inherited_font_size);
RefPtr<Gfx::FontCascadeList const> compute_font_for_style_values(StyleValue const& font_family, CSSPixels const& font_size, int font_slope, double font_weight, Percentage const& font_width, HashMap<FlyString, NumberOrCalculated> const& font_variation_settings, Length::ResolutionContext const& length_resolution_context) const;
RefPtr<Gfx::FontCascadeList const> compute_font_for_style_values(StyleValue const& font_family, CSSPixels const& font_size, int font_slope, double font_weight, Percentage const& font_width, HashMap<FlyString, double> const& font_variation_settings) const;
[[nodiscard]] RefPtr<StyleValue const> recascade_font_size_if_needed(DOM::AbstractElement, CascadedProperties&) const;
void set_viewport_rect(Badge<DOM::Document>, CSSPixelRect const& viewport_rect) { m_viewport_rect = viewport_rect; }

View file

@ -148,8 +148,7 @@ void CanvasTextDrawingStyles<IncludingClass, CanvasType>::set_font(StringView fo
computed_font_style->as_font_style().to_font_slope(),
computed_font_weight->as_number().number(),
computed_font_width->as_percentage().percentage(),
{},
length_resolution_context);
{});
},
[](HTML::WorkerGlobalScope*) -> RefPtr<Gfx::FontCascadeList const> {
// FIXME: implement computing the font for HTML::WorkerGlobalScope

View file

@ -550,8 +550,7 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style)
if (auto maybe_font_language_override = computed_style.font_language_override(); maybe_font_language_override.has_value())
computed_values.set_font_language_override(maybe_font_language_override.release_value());
computed_values.set_font_features(computed_style.font_features());
if (auto maybe_font_variation_settings = computed_style.font_variation_settings(); maybe_font_variation_settings.has_value())
computed_values.set_font_variation_settings(maybe_font_variation_settings.release_value());
computed_values.set_font_variation_settings(computed_style.font_variation_settings());
auto const& border_bottom_left_radius = computed_style.property(CSS::PropertyID::BorderBottomLeftRadius);
if (border_bottom_left_radius.is_border_radius()) {