LibWeb/CSS: Use initial values for @font-face descriptors

This commit is contained in:
Sam Atkins 2025-04-04 12:10:12 +01:00
commit 549205fe33
3 changed files with 23 additions and 16 deletions

View file

@ -263,6 +263,14 @@ RefPtr<CSSStyleValue const> CSSFontFaceDescriptors::descriptor(DescriptorID desc
return nullptr; return nullptr;
} }
RefPtr<CSSStyleValue const> CSSFontFaceDescriptors::descriptor_or_initial_value(DescriptorID descriptor_id) const
{
if (auto value = descriptor(descriptor_id))
return value.release_nonnull();
return descriptor_initial_value(AtRuleID::FontFace, descriptor_id);
}
WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_ascent_override(StringView value) WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_ascent_override(StringView value)
{ {
return set_property("ascent-override"sv, value, ""sv); return set_property("ascent-override"sv, value, ""sv);

View file

@ -33,6 +33,7 @@ public:
virtual StringView get_property_priority(StringView property) const override; virtual StringView get_property_priority(StringView property) const override;
RefPtr<CSSStyleValue const> descriptor(DescriptorID) const; RefPtr<CSSStyleValue const> descriptor(DescriptorID) const;
RefPtr<CSSStyleValue const> descriptor_or_initial_value(DescriptorID) const;
WebIDL::ExceptionOr<void> set_ascent_override(StringView value); WebIDL::ExceptionOr<void> set_ascent_override(StringView value);
String ascent_override() const; String ascent_override() const;

View file

@ -45,23 +45,23 @@ ParsedFontFace ParsedFontFace::from_descriptors(CSSFontFaceDescriptors const& de
}; };
FlyString font_family; FlyString font_family;
if (auto value = descriptors.descriptor(DescriptorID::FontFamily)) if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::FontFamily))
font_family = extract_font_name(*value); font_family = extract_font_name(*value);
Optional<int> weight; Optional<int> weight;
if (auto value = descriptors.descriptor(DescriptorID::FontWeight)) if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::FontWeight))
weight = value->to_font_weight(); weight = value->to_font_weight();
Optional<int> slope; Optional<int> slope;
if (auto value = descriptors.descriptor(DescriptorID::FontStyle)) if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::FontStyle))
slope = value->to_font_slope(); slope = value->to_font_slope();
Optional<int> width; Optional<int> width;
if (auto value = descriptors.descriptor(DescriptorID::FontWidth)) if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::FontWidth))
width = value->to_font_width(); width = value->to_font_width();
Vector<Source> sources; Vector<Source> sources;
if (auto value = descriptors.descriptor(DescriptorID::Src)) { if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::Src)) {
auto add_source = [&sources, extract_font_name](FontSourceStyleValue const& font_source) { auto add_source = [&sources, extract_font_name](FontSourceStyleValue const& font_source) {
font_source.source().visit( font_source.source().visit(
[&](FontSourceStyleValue::Local const& local) { [&](FontSourceStyleValue::Local const& local) {
@ -82,7 +82,7 @@ ParsedFontFace ParsedFontFace::from_descriptors(CSSFontFaceDescriptors const& de
} }
Vector<Gfx::UnicodeRange> unicode_ranges; Vector<Gfx::UnicodeRange> unicode_ranges;
if (auto value = descriptors.descriptor(DescriptorID::UnicodeRange)) { if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::UnicodeRange)) {
if (value->is_unicode_range()) { if (value->is_unicode_range()) {
unicode_ranges.append(value->as_unicode_range().unicode_range()); unicode_ranges.append(value->as_unicode_range().unicode_range());
} else if (value->is_value_list()) { } else if (value->is_value_list()) {
@ -90,39 +90,37 @@ ParsedFontFace ParsedFontFace::from_descriptors(CSSFontFaceDescriptors const& de
unicode_ranges.append(range->as_unicode_range().unicode_range()); unicode_ranges.append(range->as_unicode_range().unicode_range());
} }
} }
if (unicode_ranges.is_empty())
unicode_ranges.empend(0x0u, 0x10FFFFu);
Optional<Percentage> ascent_override; Optional<Percentage> ascent_override;
if (auto value = descriptors.descriptor(DescriptorID::AscentOverride)) if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::AscentOverride))
ascent_override = extract_percentage_or_normal(*value); ascent_override = extract_percentage_or_normal(*value);
Optional<Percentage> descent_override; Optional<Percentage> descent_override;
if (auto value = descriptors.descriptor(DescriptorID::DescentOverride)) if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::DescentOverride))
descent_override = extract_percentage_or_normal(*value); descent_override = extract_percentage_or_normal(*value);
Optional<Percentage> line_gap_override; Optional<Percentage> line_gap_override;
if (auto value = descriptors.descriptor(DescriptorID::LineGapOverride)) if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::LineGapOverride))
line_gap_override = extract_percentage_or_normal(*value); line_gap_override = extract_percentage_or_normal(*value);
FontDisplay font_display; FontDisplay font_display;
if (auto value = descriptors.descriptor(DescriptorID::FontDisplay)) if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::FontDisplay))
font_display = keyword_to_font_display(value->to_keyword()).value_or(FontDisplay::Auto); font_display = keyword_to_font_display(value->to_keyword()).value_or(FontDisplay::Auto);
Optional<FlyString> font_named_instance; Optional<FlyString> font_named_instance;
if (auto value = descriptors.descriptor(DescriptorID::FontNamedInstance)) { if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::FontNamedInstance)) {
if (value->is_string()) if (value->is_string())
font_named_instance = value->as_string().string_value(); font_named_instance = value->as_string().string_value();
} }
Optional<FlyString> font_language_override; Optional<FlyString> font_language_override;
if (auto value = descriptors.descriptor(DescriptorID::FontLanguageOverride)) { if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::FontLanguageOverride)) {
if (value->is_string()) if (value->is_string())
font_language_override = value->as_string().string_value(); font_language_override = value->as_string().string_value();
} }
Optional<OrderedHashMap<FlyString, i64>> font_feature_settings; Optional<OrderedHashMap<FlyString, i64>> font_feature_settings;
if (auto value = descriptors.descriptor(DescriptorID::FontFeatureSettings)) { if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::FontFeatureSettings)) {
if (value->to_keyword() == Keyword::Normal) { if (value->to_keyword() == Keyword::Normal) {
font_feature_settings.clear(); font_feature_settings.clear();
} else if (value->is_value_list()) { } else if (value->is_value_list()) {
@ -144,7 +142,7 @@ ParsedFontFace ParsedFontFace::from_descriptors(CSSFontFaceDescriptors const& de
} }
Optional<OrderedHashMap<FlyString, double>> font_variation_settings; Optional<OrderedHashMap<FlyString, double>> font_variation_settings;
if (auto value = descriptors.descriptor(DescriptorID::FontVariationSettings)) { if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::FontVariationSettings)) {
if (value->to_keyword() == Keyword::Normal) { if (value->to_keyword() == Keyword::Normal) {
font_variation_settings.clear(); font_variation_settings.clear();
} else if (value->is_value_list()) { } else if (value->is_value_list()) {