LibWeb: Store font-weight in ComputedProperties in computed form

We now also more closely follow the spec when computing values for
font-weight and we now:
 - Support relative lengths in `calc()`s
 - Properly clamp `calc()`s
 - Support relative keywords (e.g. lighter, bolder)
 - Respect that font-weight can be a non-integer number.

This does expose a few false positives in the font-weight-computed.html
WPT test. This is because we don't recompute non-inherited font-weight
within `recompute_inherited_style` which means that relative keyword
values can fall out of sync with their parent's value. These previously
passed as we treated `bolder` and `lighter` as aliases for `bold` and
`normal` respectively.
This commit is contained in:
Callum Law 2025-09-02 01:14:06 +12:00 committed by Sam Atkins
parent cfbe0244d4
commit 39484e2027
Notes: github-actions[bot] 2025-09-19 09:08:40 +00:00
15 changed files with 214 additions and 117 deletions

View file

@ -9,6 +9,7 @@
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
#include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/Canvas/CanvasState.h>
@ -108,6 +109,7 @@ public:
// NOTE: The initial value here is non-standard as the default font is "10px sans-serif"
auto inherited_font_size = CSSPixels { 10 };
auto inherited_font_weight = CSS::InitialValues::font_weight();
auto length_resolution_context = CSS::Length::ResolutionContext::for_window(*document->window());
if constexpr (SameAs<CanvasType, HTML::HTMLCanvasElement>) {
@ -117,13 +119,20 @@ public:
computed_math_depth = canvas_element.computed_properties()->math_depth();
inherited_math_depth = canvas_element.computed_properties()->math_depth();
inherited_font_size = canvas_element.computed_properties()->font_size();
inherited_font_weight = canvas_element.computed_properties()->font_weight();
length_resolution_context = CSS::Length::ResolutionContext::for_element(DOM::AbstractElement { canvas_element });
}
}
auto const& computed_font_size = CSS::StyleComputer::compute_font_size(font_size, computed_math_depth, inherited_font_size, inherited_math_depth, length_resolution_context);
auto const& computed_font_weight = CSS::StyleComputer::compute_font_weight(font_weight, inherited_font_weight, length_resolution_context);
return document->style_computer().compute_font_for_style_values(font_family, computed_font_size->as_length().length().absolute_length_to_px(), font_style, font_weight, font_width);
return document->style_computer().compute_font_for_style_values(
font_family,
computed_font_size->as_length().length().absolute_length_to_px(),
font_style,
computed_font_weight->as_number().number(),
font_width);
},
[](HTML::WorkerGlobalScope*) -> RefPtr<Gfx::FontCascadeList const> {
// FIXME: implement computing the font for HTML::WorkerGlobalScope