@@ -229,14 +229,14 @@ bool FontLoader::is_loading() const
229229 return m_fetch_controller && !m_vector_font;
230230}
231231
232- RefPtr<Gfx::Font const > FontLoader::font_with_point_size (float point_size)
232+ RefPtr<Gfx::Font const > FontLoader::font_with_point_size (float point_size, Gfx::FontVariationSettings const & variations )
233233{
234234 if (!m_vector_font) {
235235 if (!m_fetch_controller)
236236 start_loading_next_url ();
237237 return nullptr ;
238238 }
239- return m_vector_font->font (point_size);
239+ return m_vector_font->font (point_size, variations );
240240}
241241
242242void FontLoader::start_loading_next_url ()
@@ -330,18 +330,18 @@ struct StyleComputer::MatchingFontCandidate {
330330 FontFaceKey key;
331331 Variant<FontLoaderList*, Gfx::Typeface const *> loader_or_typeface;
332332
333- [[nodiscard]] RefPtr<Gfx::FontCascadeList const > font_with_point_size (float point_size) const
333+ [[nodiscard]] RefPtr<Gfx::FontCascadeList const > font_with_point_size (float point_size, Gfx::FontVariationSettings const & variations ) const
334334 {
335335 auto font_list = Gfx::FontCascadeList::create ();
336336 if (auto * loader_list = loader_or_typeface.get_pointer <FontLoaderList*>(); loader_list) {
337337 for (auto const & loader : **loader_list) {
338- if (auto font = loader->font_with_point_size (point_size); font)
338+ if (auto font = loader->font_with_point_size (point_size, variations ); font)
339339 font_list->add (*font, loader->unicode_ranges ());
340340 }
341341 return font_list;
342342 }
343343
344- font_list->add (loader_or_typeface.get <Gfx::Typeface const *>()->font (point_size));
344+ font_list->add (loader_or_typeface.get <Gfx::Typeface const *>()->font (point_size, variations ));
345345 return font_list;
346346 }
347347};
@@ -1668,27 +1668,27 @@ Length::FontMetrics StyleComputer::calculate_root_element_font_metrics(ComputedP
16681668 return font_metrics;
16691669}
16701670
1671- RefPtr<Gfx::FontCascadeList const > StyleComputer::find_matching_font_weight_ascending (Vector<MatchingFontCandidate> const & candidates, int target_weight, float font_size_in_pt, bool inclusive)
1671+ RefPtr<Gfx::FontCascadeList const > StyleComputer::find_matching_font_weight_ascending (Vector<MatchingFontCandidate> const & candidates, int target_weight, float font_size_in_pt, Gfx::FontVariationSettings const & variations, bool inclusive)
16721672{
16731673 using Fn = AK::Function<bool (MatchingFontCandidate const &)>;
16741674 auto pred = inclusive ? Fn ([&](auto const & matching_font_candidate) { return matching_font_candidate.key .weight >= target_weight; })
16751675 : Fn ([&](auto const & matching_font_candidate) { return matching_font_candidate.key .weight > target_weight; });
16761676 auto it = find_if (candidates.begin (), candidates.end (), pred);
16771677 for (; it != candidates.end (); ++it) {
1678- if (auto found_font = it->font_with_point_size (font_size_in_pt))
1678+ if (auto found_font = it->font_with_point_size (font_size_in_pt, variations ))
16791679 return found_font;
16801680 }
16811681 return {};
16821682}
16831683
1684- RefPtr<Gfx::FontCascadeList const > StyleComputer::find_matching_font_weight_descending (Vector<MatchingFontCandidate> const & candidates, int target_weight, float font_size_in_pt, bool inclusive)
1684+ RefPtr<Gfx::FontCascadeList const > StyleComputer::find_matching_font_weight_descending (Vector<MatchingFontCandidate> const & candidates, int target_weight, float font_size_in_pt, Gfx::FontVariationSettings const & variations, bool inclusive)
16851685{
16861686 using Fn = AK::Function<bool (MatchingFontCandidate const &)>;
16871687 auto pred = inclusive ? Fn ([&](auto const & matching_font_candidate) { return matching_font_candidate.key .weight <= target_weight; })
16881688 : Fn ([&](auto const & matching_font_candidate) { return matching_font_candidate.key .weight < target_weight; });
16891689 auto it = find_if (candidates.rbegin (), candidates.rend (), pred);
16901690 for (; it != candidates.rend (); ++it) {
1691- if (auto found_font = it->font_with_point_size (font_size_in_pt))
1691+ if (auto found_font = it->font_with_point_size (font_size_in_pt, variations ))
16921692 return found_font;
16931693 }
16941694 return {};
@@ -1732,34 +1732,38 @@ RefPtr<Gfx::FontCascadeList const> StyleComputer::font_matching_algorithm(FlyStr
17321732 // If the desired weight is inclusively between 400 and 500, weights greater than or equal to the target weight
17331733 // are checked in ascending order until 500 is hit and checked, followed by weights less than the target weight
17341734 // in descending order, followed by weights greater than 500, until a match is found.
1735+
1736+ Gfx::FontVariationSettings variations;
1737+ variations.set_weight (weight);
1738+
17351739 if (weight >= 400 && weight <= 500 ) {
17361740 auto it = find_if (matching_family_fonts.begin (), matching_family_fonts.end (),
17371741 [&](auto const & matching_font_candidate) { return matching_font_candidate.key .weight >= weight; });
17381742 for (; it != matching_family_fonts.end () && it->key .weight <= 500 ; ++it) {
1739- if (auto found_font = it->font_with_point_size (font_size_in_pt))
1743+ if (auto found_font = it->font_with_point_size (font_size_in_pt, variations ))
17401744 return found_font;
17411745 }
1742- if (auto found_font = find_matching_font_weight_descending (matching_family_fonts, weight, font_size_in_pt, false ))
1746+ if (auto found_font = find_matching_font_weight_descending (matching_family_fonts, weight, font_size_in_pt, variations, false ))
17431747 return found_font;
17441748 for (; it != matching_family_fonts.end (); ++it) {
1745- if (auto found_font = it->font_with_point_size (font_size_in_pt))
1749+ if (auto found_font = it->font_with_point_size (font_size_in_pt, variations ))
17461750 return found_font;
17471751 }
17481752 }
17491753 // If the desired weight is less than 400, weights less than or equal to the desired weight are checked in descending order
17501754 // followed by weights above the desired weight in ascending order until a match is found.
17511755 if (weight < 400 ) {
1752- if (auto found_font = find_matching_font_weight_descending (matching_family_fonts, weight, font_size_in_pt, true ))
1756+ if (auto found_font = find_matching_font_weight_descending (matching_family_fonts, weight, font_size_in_pt, variations, true ))
17531757 return found_font;
1754- if (auto found_font = find_matching_font_weight_ascending (matching_family_fonts, weight, font_size_in_pt, false ))
1758+ if (auto found_font = find_matching_font_weight_ascending (matching_family_fonts, weight, font_size_in_pt, variations, false ))
17551759 return found_font;
17561760 }
17571761 // If the desired weight is greater than 500, weights greater than or equal to the desired weight are checked in ascending order
17581762 // followed by weights below the desired weight in descending order until a match is found.
17591763 if (weight > 500 ) {
1760- if (auto found_font = find_matching_font_weight_ascending (matching_family_fonts, weight, font_size_in_pt, true ))
1764+ if (auto found_font = find_matching_font_weight_ascending (matching_family_fonts, weight, font_size_in_pt, variations, true ))
17611765 return found_font;
1762- if (auto found_font = find_matching_font_weight_descending (matching_family_fonts, weight, font_size_in_pt, false ))
1766+ if (auto found_font = find_matching_font_weight_descending (matching_family_fonts, weight, font_size_in_pt, variations, false ))
17631767 return found_font;
17641768 }
17651769 return {};
@@ -1821,7 +1825,7 @@ CSSPixels StyleComputer::relative_size_mapping(RelativeSize relative_size, CSSPi
18211825 VERIFY_NOT_REACHED ();
18221826}
18231827
1824- 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) const
1828+ 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
18251829{
18261830 // FIXME: We round to int here as that is what is expected by our font infrastructure below
18271831 auto width = round_to<int >(font_width.value ());
@@ -1842,10 +1846,32 @@ RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(
18421846 auto result = Gfx::FontCascadeList::create ();
18431847 if (auto it = m_loaded_fonts.find (key); it != m_loaded_fonts.end ()) {
18441848 auto const & loaders = it->value ;
1849+
1850+ Gfx::FontVariationSettings variation;
1851+ variation.set_weight (font_weight);
1852+
1853+ CalculationResolutionContext context {
1854+ .length_resolution_context = length_resolution_context,
1855+ };
1856+
1857+ for (auto const & [tag_string, value] : font_variation_settings) {
1858+ auto string_view = tag_string.bytes_as_string_view ();
1859+ if (string_view.length () != 4 )
1860+ continue ;
1861+ auto tag = Gfx::FourCC (string_view.characters_without_null_termination ());
1862+
1863+ auto resolved_value = value.resolved (context);
1864+ if (!resolved_value.has_value ())
1865+ continue ;
1866+
1867+ variation.axes .set (tag, resolved_value.release_value ());
1868+ }
1869+
18451870 for (auto const & loader : loaders) {
1846- if (auto found_font = loader->font_with_point_size (font_size_in_pt))
1871+ if (auto found_font = loader->font_with_point_size (font_size_in_pt, variation ))
18471872 result->add (*found_font, loader->unicode_ranges ());
18481873 }
1874+
18491875 return result;
18501876 }
18511877
@@ -1984,7 +2010,7 @@ void StyleComputer::compute_font(ComputedProperties& style, Optional<DOM::Abstra
19842010
19852011 auto const & font_family = style.property (CSS::PropertyID::FontFamily);
19862012
1987- auto font_list = compute_font_for_style_values (font_family, style.font_size (), style.font_slope (), style.font_weight (), style.font_width ());
2013+ 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 );
19882014 VERIFY (font_list);
19892015 VERIFY (!font_list->is_empty ());
19902016
0 commit comments