Skip to content

Commit cb8c91c

Browse files
committed
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`
1 parent 9d515ec commit cb8c91c

File tree

7 files changed

+14
-23
lines changed

7 files changed

+14
-23
lines changed

Libraries/LibWeb/CSS/ComputedProperties.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,7 +1885,7 @@ HashMap<StringView, u8> ComputedProperties::font_feature_settings() const
18851885
return {};
18861886
}
18871887

1888-
Optional<HashMap<FlyString, NumberOrCalculated>> ComputedProperties::font_variation_settings() const
1888+
HashMap<FlyString, double> ComputedProperties::font_variation_settings() const
18891889
{
18901890
auto const& value = property(PropertyID::FontVariationSettings);
18911891

@@ -1894,7 +1894,7 @@ Optional<HashMap<FlyString, NumberOrCalculated>> ComputedProperties::font_variat
18941894

18951895
if (value.is_value_list()) {
18961896
auto const& axis_tags = value.as_value_list().values();
1897-
HashMap<FlyString, NumberOrCalculated> result;
1897+
HashMap<FlyString, double> result;
18981898
result.ensure_capacity(axis_tags.size());
18991899
for (auto const& tag_value : axis_tags) {
19001900
auto const& axis_tag = tag_value->as_open_type_tagged();
@@ -1903,7 +1903,7 @@ Optional<HashMap<FlyString, NumberOrCalculated>> ComputedProperties::font_variat
19031903
result.set(axis_tag.tag(), axis_tag.value()->as_number().number());
19041904
} else {
19051905
VERIFY(axis_tag.value()->is_calculated());
1906-
result.set(axis_tag.tag(), NumberOrCalculated { axis_tag.value()->as_calculated() });
1906+
result.set(axis_tag.tag(), axis_tag.value()->as_calculated().resolve_number({}).value());
19071907
}
19081908
}
19091909
return result;

Libraries/LibWeb/CSS/ComputedProperties.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class WEB_API ComputedProperties final : public JS::Cell {
161161
FontKerning font_kerning() const;
162162
Optional<FlyString> font_language_override() const;
163163
HashMap<StringView, u8> font_feature_settings() const;
164-
Optional<HashMap<FlyString, NumberOrCalculated>> font_variation_settings() const;
164+
HashMap<FlyString, double> font_variation_settings() const;
165165
GridTrackSizeList grid_auto_columns() const;
166166
GridTrackSizeList grid_auto_rows() const;
167167
GridTrackSizeList grid_template_columns() const;

Libraries/LibWeb/CSS/ComputedValues.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ class ComputedValues {
643643
double font_weight() const { return m_inherited.font_weight; }
644644
Gfx::ShapeFeatures font_features() const { return m_inherited.font_features; }
645645
Optional<FlyString> font_language_override() const { return m_inherited.font_language_override; }
646-
Optional<HashMap<FlyString, NumberOrCalculated>> font_variation_settings() const { return m_inherited.font_variation_settings; }
646+
HashMap<FlyString, double> font_variation_settings() const { return m_inherited.font_variation_settings; }
647647
CSSPixels line_height() const { return m_inherited.line_height; }
648648
Time transition_delay() const { return m_noninherited.transition_delay; }
649649

@@ -680,7 +680,7 @@ class ComputedValues {
680680
double font_weight { InitialValues::font_weight() };
681681
Gfx::ShapeFeatures font_features { InitialValues::font_features() };
682682
Optional<FlyString> font_language_override;
683-
Optional<HashMap<FlyString, NumberOrCalculated>> font_variation_settings;
683+
HashMap<FlyString, double> font_variation_settings;
684684
CSSPixels line_height { InitialValues::line_height() };
685685
BorderCollapse border_collapse { InitialValues::border_collapse() };
686686
EmptyCells empty_cells { InitialValues::empty_cells() };
@@ -883,7 +883,7 @@ class MutableComputedValues final : public ComputedValues {
883883
void set_font_weight(double font_weight) { m_inherited.font_weight = font_weight; }
884884
void set_font_features(Gfx::ShapeFeatures font_features) { m_inherited.font_features = move(font_features); }
885885
void set_font_language_override(Optional<FlyString> font_language_override) { m_inherited.font_language_override = move(font_language_override); }
886-
void set_font_variation_settings(Optional<HashMap<FlyString, NumberOrCalculated>> value) { m_inherited.font_variation_settings = move(value); }
886+
void set_font_variation_settings(HashMap<FlyString, double> value) { m_inherited.font_variation_settings = move(value); }
887887
void set_line_height(CSSPixels line_height) { m_inherited.line_height = line_height; }
888888
void set_border_spacing_horizontal(Length border_spacing_horizontal) { m_inherited.border_spacing_horizontal = move(border_spacing_horizontal); }
889889
void set_border_spacing_vertical(Length border_spacing_vertical) { m_inherited.border_spacing_vertical = move(border_spacing_vertical); }

Libraries/LibWeb/CSS/StyleComputer.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,7 +1854,7 @@ CSSPixels StyleComputer::relative_size_mapping(RelativeSize relative_size, CSSPi
18541854
VERIFY_NOT_REACHED();
18551855
}
18561856

1857-
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
1857+
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
18581858
{
18591859
// FIXME: We round to int here as that is what is expected by our font infrastructure below
18601860
auto width = round_to<int>(font_width.value());
@@ -1879,21 +1879,14 @@ RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(
18791879
Gfx::FontVariationSettings variation;
18801880
variation.set_weight(font_weight);
18811881

1882-
CalculationResolutionContext context {
1883-
.length_resolution_context = length_resolution_context,
1884-
};
1885-
18861882
for (auto const& [tag_string, value] : font_variation_settings) {
18871883
auto string_view = tag_string.bytes_as_string_view();
18881884
if (string_view.length() != 4)
18891885
continue;
1890-
auto tag = Gfx::FourCC(string_view.characters_without_null_termination());
18911886

1892-
auto resolved_value = value.resolved(context);
1893-
if (!resolved_value.has_value())
1894-
continue;
1887+
auto tag = Gfx::FourCC(string_view.characters_without_null_termination());
18951888

1896-
variation.axes.set(tag, resolved_value.release_value());
1889+
variation.axes.set(tag, value);
18971890
}
18981891

18991892
for (auto const& loader : loaders) {
@@ -2045,7 +2038,7 @@ void StyleComputer::compute_font(ComputedProperties& style, Optional<DOM::Abstra
20452038

20462039
auto const& font_family = style.property(CSS::PropertyID::FontFamily);
20472040

2048-
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);
2041+
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());
20492042
VERIFY(font_list);
20502043
VERIFY(!font_list->is_empty());
20512044

Libraries/LibWeb/CSS/StyleComputer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class WEB_API StyleComputer final : public GC::Cell {
186186
static CSSPixels default_user_font_size();
187187
static CSSPixels absolute_size_mapping(AbsoluteSize, CSSPixels default_font_size);
188188
static CSSPixels relative_size_mapping(RelativeSize, CSSPixels inherited_font_size);
189-
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;
189+
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;
190190
[[nodiscard]] RefPtr<StyleValue const> recascade_font_size_if_needed(DOM::AbstractElement, CascadedProperties&) const;
191191

192192
void set_viewport_rect(Badge<DOM::Document>, CSSPixelRect const& viewport_rect) { m_viewport_rect = viewport_rect; }

Libraries/LibWeb/HTML/Canvas/CanvasTextDrawingStyles.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ void CanvasTextDrawingStyles<IncludingClass, CanvasType>::set_font(StringView fo
147147
computed_font_style->as_font_style().to_font_slope(),
148148
computed_font_weight->as_number().number(),
149149
computed_font_width->as_percentage().percentage(),
150-
{},
151-
length_resolution_context);
150+
{});
152151
},
153152
[](HTML::WorkerGlobalScope*) -> RefPtr<Gfx::FontCascadeList const> {
154153
// FIXME: implement computing the font for HTML::WorkerGlobalScope

Libraries/LibWeb/Layout/Node.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,7 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style)
532532
if (auto maybe_font_language_override = computed_style.font_language_override(); maybe_font_language_override.has_value())
533533
computed_values.set_font_language_override(maybe_font_language_override.release_value());
534534
computed_values.set_font_features(computed_style.font_features());
535-
if (auto maybe_font_variation_settings = computed_style.font_variation_settings(); maybe_font_variation_settings.has_value())
536-
computed_values.set_font_variation_settings(maybe_font_variation_settings.release_value());
535+
computed_values.set_font_variation_settings(computed_style.font_variation_settings());
537536

538537
auto const& border_bottom_left_radius = computed_style.property(CSS::PropertyID::BorderBottomLeftRadius);
539538
if (border_bottom_left_radius.is_border_radius()) {

0 commit comments

Comments
 (0)