Skip to content

Commit 3829a85

Browse files
Norbiroskalenikaliaksandr
authored andcommitted
LibWeb: Add basic variable font support
Integrates the new `FontVariationSettings` from LibGfx into LibWeb to enable initial variable font functionality. Currently, only the `wght` (weight) axis is fully supported and tested. This update also introduces support for the CSS `font-variation-settings` property.
1 parent 4893816 commit 3829a85

File tree

7 files changed

+94
-24
lines changed

7 files changed

+94
-24
lines changed

Libraries/LibWeb/CSS/StyleComputer.cpp

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

242242
void 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

Libraries/LibWeb/CSS/StyleComputer.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class WEB_API StyleComputer final : public GC::Cell {
177177
static CSSPixels default_user_font_size();
178178
static CSSPixels absolute_size_mapping(AbsoluteSize, CSSPixels default_font_size);
179179
static CSSPixels relative_size_mapping(RelativeSize, CSSPixels inherited_font_size);
180-
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) const;
180+
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;
181181
[[nodiscard]] RefPtr<StyleValue const> recascade_font_size_if_needed(DOM::AbstractElement, CascadedProperties&) const;
182182

183183
void set_viewport_rect(Badge<DOM::Document>, CSSPixelRect const& viewport_rect) { m_viewport_rect = viewport_rect; }
@@ -238,8 +238,8 @@ class WEB_API StyleComputer final : public GC::Cell {
238238
LogicalAliasMappingContext compute_logical_alias_mapping_context(DOM::AbstractElement, ComputeStyleMode, MatchingRuleSet const&) const;
239239
[[nodiscard]] GC::Ptr<ComputedProperties> compute_style_impl(DOM::AbstractElement, ComputeStyleMode, Optional<bool&> did_change_custom_properties) const;
240240
[[nodiscard]] GC::Ref<CascadedProperties> compute_cascaded_values(DOM::AbstractElement, bool did_match_any_pseudo_element_rules, ComputeStyleMode, MatchingRuleSet const&, Optional<LogicalAliasMappingContext>, ReadonlySpan<PropertyID> properties_to_cascade) const;
241-
static RefPtr<Gfx::FontCascadeList const> find_matching_font_weight_ascending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, bool inclusive);
242-
static RefPtr<Gfx::FontCascadeList const> find_matching_font_weight_descending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, bool inclusive);
241+
static RefPtr<Gfx::FontCascadeList const> find_matching_font_weight_ascending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, Gfx::FontVariationSettings const& variations, bool inclusive);
242+
static RefPtr<Gfx::FontCascadeList const> find_matching_font_weight_descending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, Gfx::FontVariationSettings const& variations, bool inclusive);
243243
RefPtr<Gfx::FontCascadeList const> font_matching_algorithm(FlyString const& family_name, int weight, int slope, float font_size_in_pt) const;
244244
void compute_custom_properties(ComputedProperties&, DOM::AbstractElement) const;
245245
void compute_math_depth(ComputedProperties&, Optional<DOM::AbstractElement>) const;
@@ -323,7 +323,7 @@ class FontLoader final : public GC::Cell {
323323
Vector<Gfx::UnicodeRange> const& unicode_ranges() const { return m_unicode_ranges; }
324324
RefPtr<Gfx::Typeface const> vector_font() const { return m_vector_font; }
325325

326-
RefPtr<Gfx::Font const> font_with_point_size(float point_size);
326+
RefPtr<Gfx::Font const> font_with_point_size(float point_size, Gfx::FontVariationSettings const& variations = {});
327327
void start_loading_next_url();
328328

329329
bool is_loading() const;

Libraries/LibWeb/HTML/Canvas/CanvasTextDrawingStyles.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ void CanvasTextDrawingStyles<IncludingClass, CanvasType>::set_font(StringView fo
146146
computed_font_size->as_length().length().absolute_length_to_px(),
147147
computed_font_style->as_font_style().to_font_slope(),
148148
computed_font_weight->as_number().number(),
149-
computed_font_width->as_percentage().percentage());
149+
computed_font_width->as_percentage().percentage(),
150+
{},
151+
length_resolution_context);
150152
},
151153
[](HTML::WorkerGlobalScope*) -> RefPtr<Gfx::FontCascadeList const> {
152154
// FIXME: implement computing the font for HTML::WorkerGlobalScope
82.3 KB
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<style>
3+
* {
4+
margin: 0;
5+
}
6+
body {
7+
background-color: white;
8+
}
9+
</style>
10+
<img src="../images/variable-font-weight-ref.png">
23.4 KB
Loading
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Variable-Font-Weight</title>
6+
<link rel="match" href="../expected/variable-font-weight-ref.html" />
7+
<meta name="fuzzy" content="maxDifference=0-2;totalPixels=0-4066">
8+
<style>
9+
@font-face {
10+
font-family: 'Roboto Flex';
11+
src: url(../data/variable-font-example.woff2) format('woff2');
12+
}
13+
14+
body {
15+
font-family: 'Roboto Flex', sans-serif;
16+
font-synthesis: none;
17+
font-synthesis-weight: none;
18+
}
19+
</style>
20+
</head>
21+
<body>
22+
<span style="font-family: sans-serif">Sens serif</span>
23+
Character outside <b>all unicode-ranges</b>:
24+
25+
<h1>Character outside all unicode-ranges:</h1>
26+
Character outside all unicode-ranges:
27+
28+
<div style="font-variation-settings: 'wght' 900, 'opsz' 200;">
29+
Hello
30+
</div>
31+
</body>
32+
</html>

0 commit comments

Comments
 (0)