From 3eb6d510fd0c7d4b6b21b64ac36a06571ea1fd9f Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 26 Sep 2024 14:07:46 +0100 Subject: [PATCH] LibWeb/CSS: Parse font-display descriptor --- Userland/Libraries/LibWeb/CSS/Enums.json | 7 +++++++ Userland/Libraries/LibWeb/CSS/FontFace.cpp | 2 +- Userland/Libraries/LibWeb/CSS/Keywords.json | 3 +++ .../Libraries/LibWeb/CSS/ParsedFontFace.cpp | 3 ++- .../Libraries/LibWeb/CSS/ParsedFontFace.h | 5 ++++- .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 20 ++++++++++++++++++- Userland/Libraries/LibWeb/Dump.cpp | 3 +++ 7 files changed, 39 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Enums.json b/Userland/Libraries/LibWeb/CSS/Enums.json index a4a9acc5260..ef10b620bda 100644 --- a/Userland/Libraries/LibWeb/CSS/Enums.json +++ b/Userland/Libraries/LibWeb/CSS/Enums.json @@ -212,6 +212,13 @@ "inline-start", "inline-end" ], + "font-display": [ + "auto", + "block", + "swap", + "fallback", + "optional" + ], "font-variant": [ "normal", "small-caps" diff --git a/Userland/Libraries/LibWeb/CSS/FontFace.cpp b/Userland/Libraries/LibWeb/CSS/FontFace.cpp index 5e82efb24ef..2f940964df3 100644 --- a/Userland/Libraries/LibWeb/CSS/FontFace.cpp +++ b/Userland/Libraries/LibWeb/CSS/FontFace.cpp @@ -388,7 +388,7 @@ void FontFace::load_font_source() auto& style_computer = const_cast(window.document()->style_computer()); // FIXME: The ParsedFontFace is kind of expensive to create. We should be using a shared sub-object for the data - ParsedFontFace parsed_font_face { font->m_family, font->m_weight.to_number(), 0 /* FIXME: slope */, font->m_urls, font->m_unicode_ranges, /* FIXME: ascent_override */ {}, /* FIXME: descent_override */ {}, /* FIXME: line_gap_override */ {} }; + ParsedFontFace parsed_font_face { font->m_family, font->m_weight.to_number(), 0 /* FIXME: slope */, font->m_urls, font->m_unicode_ranges, /* FIXME: ascent_override */ {}, /* FIXME: descent_override */ {}, /* FIXME: line_gap_override */ {}, /* FIXME: font_display */ FontDisplay::Auto }; if (auto loader = style_computer.load_font_face(parsed_font_face, move(on_load), move(on_error)); loader.has_value()) loader->start_loading_next_url(); } else { diff --git a/Userland/Libraries/LibWeb/CSS/Keywords.json b/Userland/Libraries/LibWeb/CSS/Keywords.json index bc8405b748b..0ba873b362d 100644 --- a/Userland/Libraries/LibWeb/CSS/Keywords.json +++ b/Userland/Libraries/LibWeb/CSS/Keywords.json @@ -149,6 +149,7 @@ "expanded", "extra-condensed", "extra-expanded", + "fallback", "fantasy", "fast", "field", @@ -268,6 +269,7 @@ "open-quote", "optimizequality", "optimizespeed", + "optional", "outset", "outside", "overline", @@ -351,6 +353,7 @@ "subtractive", "super", "sw-resize", + "swap", "table", "table-caption", "table-cell", diff --git a/Userland/Libraries/LibWeb/CSS/ParsedFontFace.cpp b/Userland/Libraries/LibWeb/CSS/ParsedFontFace.cpp index a6146f10b97..e77f9e11335 100644 --- a/Userland/Libraries/LibWeb/CSS/ParsedFontFace.cpp +++ b/Userland/Libraries/LibWeb/CSS/ParsedFontFace.cpp @@ -9,7 +9,7 @@ namespace Web::CSS { -ParsedFontFace::ParsedFontFace(FlyString font_family, Optional weight, Optional slope, Vector sources, Vector unicode_ranges, Optional ascent_override, Optional descent_override, Optional line_gap_override) +ParsedFontFace::ParsedFontFace(FlyString font_family, Optional weight, Optional slope, Vector sources, Vector unicode_ranges, Optional ascent_override, Optional descent_override, Optional line_gap_override, FontDisplay font_display) : m_font_family(move(font_family)) , m_weight(weight) , m_slope(slope) @@ -18,6 +18,7 @@ ParsedFontFace::ParsedFontFace(FlyString font_family, Optional weight, Opti , m_ascent_override(move(ascent_override)) , m_descent_override(move(descent_override)) , m_line_gap_override(move(line_gap_override)) + , m_font_display(font_display) { } diff --git a/Userland/Libraries/LibWeb/CSS/ParsedFontFace.h b/Userland/Libraries/LibWeb/CSS/ParsedFontFace.h index 89538dddffe..0e4553cd1c4 100644 --- a/Userland/Libraries/LibWeb/CSS/ParsedFontFace.h +++ b/Userland/Libraries/LibWeb/CSS/ParsedFontFace.h @@ -10,6 +10,7 @@ #include #include #include +#include #include namespace Web::CSS { @@ -22,11 +23,12 @@ public: Optional format; }; - ParsedFontFace(FlyString font_family, Optional weight, Optional slope, Vector sources, Vector unicode_ranges, Optional ascent_override, Optional descent_override, Optional line_gap_override); + ParsedFontFace(FlyString font_family, Optional weight, Optional slope, Vector sources, Vector unicode_ranges, Optional ascent_override, Optional descent_override, Optional line_gap_override, FontDisplay font_display); ~ParsedFontFace() = default; Optional ascent_override() const { return m_ascent_override; } Optional descent_override() const { return m_descent_override; } + FontDisplay font_display() const { return m_font_display; } FlyString font_family() const { return m_font_family; } Optional slope() const { return m_slope; } Optional weight() const { return m_weight; } @@ -43,6 +45,7 @@ private: Optional m_ascent_override; Optional m_descent_override; Optional m_line_gap_override; + FontDisplay m_font_display; }; } diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index f07183740fb..64554a27813 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -5458,6 +5458,7 @@ JS::GCPtr Parser::parse_font_face_rule(TokenStream ascent_override; Optional descent_override; Optional line_gap_override; + FontDisplay font_display = FontDisplay::Auto; // "normal" is returned as nullptr auto parse_as_percentage_or_normal = [&](Vector const& values) -> ErrorOr> { @@ -5516,6 +5517,23 @@ JS::GCPtr Parser::parse_font_face_rule(TokenStreamto_keyword()); + if (value.has_value()) { + font_display = *value; + } else { + dbgln_if(CSS_PARSER_DEBUG, "CSSParser: `{}` is not a valid value for font-display", keyword_value->to_string()); + } + } + } + continue; + } if (declaration.name().equals_ignoring_ascii_case("font-family"sv)) { // FIXME: This is very similar to, but different from, the logic in parse_font_family_value(). // Ideally they could share code. @@ -5612,7 +5630,7 @@ JS::GCPtr Parser::parse_font_face_rule(TokenStream Parser::parse_as_font_face_src() diff --git a/Userland/Libraries/LibWeb/Dump.cpp b/Userland/Libraries/LibWeb/Dump.cpp index 39242e23736..9db03f4da6a 100644 --- a/Userland/Libraries/LibWeb/Dump.cpp +++ b/Userland/Libraries/LibWeb/Dump.cpp @@ -691,6 +691,9 @@ void dump_font_face_rule(StringBuilder& builder, CSS::CSSFontFaceRule const& rul indent(builder, indent_levels + 1); builder.appendff("line-gap-override: {}\n", font_face.line_gap_override().value()); } + + indent(builder, indent_levels + 1); + builder.appendff("display: {}\n", CSS::to_string(font_face.font_display())); } void dump_import_rule(StringBuilder& builder, CSS::CSSImportRule const& rule, int indent_levels)