From 0121e7028f91af583f15482bc4fde07051f29b30 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 4 Apr 2025 15:30:36 +0100 Subject: [PATCH] LibWeb/CSS: Make "CSSStyleValue -> list of font sources" code accessible We'll want this in FontFace. --- Libraries/LibWeb/CSS/ParsedFontFace.cpp | 59 ++++++++++++++----------- Libraries/LibWeb/CSS/ParsedFontFace.h | 1 + 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/Libraries/LibWeb/CSS/ParsedFontFace.cpp b/Libraries/LibWeb/CSS/ParsedFontFace.cpp index e4f3ae0ce84..94f11f54d40 100644 --- a/Libraries/LibWeb/CSS/ParsedFontFace.cpp +++ b/Libraries/LibWeb/CSS/ParsedFontFace.cpp @@ -21,16 +21,40 @@ namespace Web::CSS { -ParsedFontFace ParsedFontFace::from_descriptors(CSSFontFaceDescriptors const& descriptors) +static FlyString extract_font_name(CSSStyleValue const& value) { - auto extract_font_name = [](CSSStyleValue const& value) { - if (value.is_string()) - return value.as_string().string_value(); - if (value.is_custom_ident()) - return value.as_custom_ident().custom_ident(); - return FlyString {}; + if (value.is_string()) + return value.as_string().string_value(); + if (value.is_custom_ident()) + return value.as_custom_ident().custom_ident(); + return FlyString {}; +} + +Vector ParsedFontFace::sources_from_style_value(CSSStyleValue const& style_value) +{ + Vector sources; + auto add_source = [&sources](FontSourceStyleValue const& font_source) { + font_source.source().visit( + [&](FontSourceStyleValue::Local const& local) { + sources.empend(extract_font_name(local.name), OptionalNone {}); + }, + [&](URL::URL const& url) { + // FIXME: tech() + sources.empend(url, font_source.format()); + }); }; + if (style_value.is_font_source()) { + add_source(style_value.as_font_source()); + } else if (style_value.is_value_list()) { + for (auto const& source : style_value.as_value_list().values()) + add_source(source->as_font_source()); + } + return sources; +} + +ParsedFontFace ParsedFontFace::from_descriptors(CSSFontFaceDescriptors const& descriptors) +{ auto extract_percentage_or_normal = [](CSSStyleValue const& value) -> Optional { if (value.is_percentage()) return value.as_percentage().percentage(); @@ -61,25 +85,8 @@ ParsedFontFace ParsedFontFace::from_descriptors(CSSFontFaceDescriptors const& de width = value->to_font_width(); Vector sources; - if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::Src)) { - auto add_source = [&sources, extract_font_name](FontSourceStyleValue const& font_source) { - font_source.source().visit( - [&](FontSourceStyleValue::Local const& local) { - sources.empend(extract_font_name(local.name), OptionalNone {}); - }, - [&](URL::URL const& url) { - // FIXME: tech() - sources.empend(url, font_source.format()); - }); - }; - - if (value->is_font_source()) { - add_source(value->as_font_source()); - } else if (value->is_value_list()) { - for (auto const& source : value->as_value_list().values()) - add_source(source->as_font_source()); - } - } + if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::Src)) + sources = sources_from_style_value(*value); Vector unicode_ranges; if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::UnicodeRange)) { diff --git a/Libraries/LibWeb/CSS/ParsedFontFace.h b/Libraries/LibWeb/CSS/ParsedFontFace.h index 09b03f173a6..b52bcde095e 100644 --- a/Libraries/LibWeb/CSS/ParsedFontFace.h +++ b/Libraries/LibWeb/CSS/ParsedFontFace.h @@ -24,6 +24,7 @@ public: Optional format; }; + static Vector sources_from_style_value(CSSStyleValue const&); static ParsedFontFace from_descriptors(CSSFontFaceDescriptors const&); ParsedFontFace(FlyString font_family, Optional weight, Optional slope, Optional width, Vector sources, Vector unicode_ranges, Optional ascent_override, Optional descent_override, Optional line_gap_override, FontDisplay font_display, Optional font_named_instance, Optional font_language_override, Optional> font_feature_settings, Optional> font_variation_settings);