LibWeb: Use compute_font_weight for font-weight descriptor

This allows us to remove `StyleValue::to_font_weight`
This commit is contained in:
Callum Law 2025-09-02 16:16:03 +12:00 committed by Sam Atkins
parent 44eaa7ad98
commit 45a4e9cd0f
3 changed files with 14 additions and 32 deletions

View File

@ -7,7 +7,9 @@
#include <LibWeb/CSS/CSSFontFaceDescriptors.h>
#include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/CSS/CSSStyleSheet.h>
#include <LibWeb/CSS/ParsedFontFace.h>
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
#include <LibWeb/CSS/StyleValues/CustomIdentStyleValue.h>
#include <LibWeb/CSS/StyleValues/FontSourceStyleValue.h>
@ -19,6 +21,7 @@
#include <LibWeb/CSS/StyleValues/StringStyleValue.h>
#include <LibWeb/CSS/StyleValues/StyleValueList.h>
#include <LibWeb/CSS/StyleValues/UnicodeRangeStyleValue.h>
#include <LibWeb/DOM/Document.h>
namespace Web::CSS {
@ -73,8 +76,17 @@ ParsedFontFace ParsedFontFace::from_descriptors(CSSFontFaceDescriptors const& de
font_family = extract_font_name(*value);
Optional<int> weight;
if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::FontWeight))
weight = value->to_font_weight();
if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::FontWeight)) {
// https://drafts.csswg.org/css-fonts-4/#font-prop-desc
// The auto values for these three descriptors have the following effects:
// - For font selection purposes, the font is selected as if the appropriate normal value (normal, normal or normal) is chosen
// - FIXME: For variation axis clamping, clamping does not occur
if (value->to_keyword() == Keyword::Auto)
weight = 400;
else
// NOTE: The value we pass here for inherited_font_weight is irrelevant as relative keywords (lighter, bolder) should be disallowed at parse time
weight = StyleComputer::compute_font_weight(*value, 0, Length::ResolutionContext::for_window(*descriptors.parent_rule()->parent_style_sheet()->owning_document()->window()))->as_number().number();
}
Optional<int> slope;
if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::FontStyle))

View File

@ -151,35 +151,6 @@ GC::Ref<CSSStyleValue> StyleValue::reify(JS::Realm& realm, String const& associa
return CSSStyleValue::create(realm, associated_property, to_string(SerializationMode::Normal));
}
int StyleValue::to_font_weight() const
{
if (is_keyword()) {
switch (as_keyword().keyword()) {
case Keyword::Normal:
return Gfx::FontWeight::Regular;
case Keyword::Bold:
return Gfx::FontWeight::Bold;
case Keyword::Lighter:
// FIXME: This should be relative to the parent.
return Gfx::FontWeight::Regular;
case Keyword::Bolder:
// FIXME: This should be relative to the parent.
return Gfx::FontWeight::Bold;
default:
return Gfx::FontWeight::Regular;
}
}
if (is_number()) {
return round_to<int>(as_number().number());
}
if (is_calculated()) {
auto maybe_weight = as_calculated().resolve_integer_deprecated({});
if (maybe_weight.has_value())
return maybe_weight.value();
}
return Gfx::FontWeight::Regular;
}
int StyleValue::to_font_slope() const
{
// FIXME: Implement oblique <angle>

View File

@ -211,7 +211,6 @@ public:
virtual Vector<Parser::ComponentValue> tokenize() const;
virtual GC::Ref<CSSStyleValue> reify(JS::Realm&, String const& associated_property) const;
[[nodiscard]] int to_font_weight() const;
[[nodiscard]] int to_font_slope() const;
[[nodiscard]] int to_font_width() const;