LibWeb/CSS: Make "CSSStyleValue -> list of font sources" code accessible

We'll want this in FontFace.
This commit is contained in:
Sam Atkins 2025-04-04 15:30:36 +01:00
parent 3288c71953
commit 0121e7028f
Notes: github-actions[bot] 2025-04-07 09:01:54 +00:00
2 changed files with 34 additions and 26 deletions

View file

@ -21,16 +21,40 @@
namespace Web::CSS { 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())
if (value.is_string()) return value.as_string().string_value();
return value.as_string().string_value(); if (value.is_custom_ident())
if (value.is_custom_ident()) return value.as_custom_ident().custom_ident();
return value.as_custom_ident().custom_ident(); return FlyString {};
return FlyString {}; }
Vector<ParsedFontFace::Source> ParsedFontFace::sources_from_style_value(CSSStyleValue const& style_value)
{
Vector<Source> 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<Percentage> { auto extract_percentage_or_normal = [](CSSStyleValue const& value) -> Optional<Percentage> {
if (value.is_percentage()) if (value.is_percentage())
return value.as_percentage().percentage(); return value.as_percentage().percentage();
@ -61,25 +85,8 @@ ParsedFontFace ParsedFontFace::from_descriptors(CSSFontFaceDescriptors const& de
width = value->to_font_width(); width = value->to_font_width();
Vector<Source> sources; Vector<Source> sources;
if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::Src)) { if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::Src))
auto add_source = [&sources, extract_font_name](FontSourceStyleValue const& font_source) { sources = sources_from_style_value(*value);
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());
}
}
Vector<Gfx::UnicodeRange> unicode_ranges; Vector<Gfx::UnicodeRange> unicode_ranges;
if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::UnicodeRange)) { if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::UnicodeRange)) {

View file

@ -24,6 +24,7 @@ public:
Optional<FlyString> format; Optional<FlyString> format;
}; };
static Vector<Source> sources_from_style_value(CSSStyleValue const&);
static ParsedFontFace from_descriptors(CSSFontFaceDescriptors const&); static ParsedFontFace from_descriptors(CSSFontFaceDescriptors const&);
ParsedFontFace(FlyString font_family, Optional<int> weight, Optional<int> slope, Optional<int> width, Vector<Source> sources, Vector<Gfx::UnicodeRange> unicode_ranges, Optional<Percentage> ascent_override, Optional<Percentage> descent_override, Optional<Percentage> line_gap_override, FontDisplay font_display, Optional<FlyString> font_named_instance, Optional<FlyString> font_language_override, Optional<OrderedHashMap<FlyString, i64>> font_feature_settings, Optional<OrderedHashMap<FlyString, double>> font_variation_settings); ParsedFontFace(FlyString font_family, Optional<int> weight, Optional<int> slope, Optional<int> width, Vector<Source> sources, Vector<Gfx::UnicodeRange> unicode_ranges, Optional<Percentage> ascent_override, Optional<Percentage> descent_override, Optional<Percentage> line_gap_override, FontDisplay font_display, Optional<FlyString> font_named_instance, Optional<FlyString> font_language_override, Optional<OrderedHashMap<FlyString, i64>> font_feature_settings, Optional<OrderedHashMap<FlyString, double>> font_variation_settings);