LibWeb/CSS: Parse font-named-instance descriptor

This commit is contained in:
Sam Atkins 2024-09-26 14:45:55 +01:00 committed by Andreas Kling
parent 3eb6d510fd
commit 7c50a31402
Notes: github-actions[bot] 2024-09-28 12:43:44 +00:00
5 changed files with 45 additions and 4 deletions

View file

@ -388,7 +388,18 @@ void FontFace::load_font_source()
auto& style_computer = const_cast<StyleComputer&>(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<int>(), 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 };
ParsedFontFace parsed_font_face {
font->m_family,
font->m_weight.to_number<int>(),
0, // FIXME: slope
font->m_urls,
font->m_unicode_ranges,
{}, // FIXME: ascent_override
{}, // FIXME: descent_override
{}, // FIXME: line_gap_override
FontDisplay::Auto, // FIXME: font_display
{}, // font-named-instance doesn't exist in FontFace
};
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 {

View file

@ -9,8 +9,9 @@
namespace Web::CSS {
ParsedFontFace::ParsedFontFace(FlyString font_family, Optional<int> weight, Optional<int> slope, Vector<Source> sources, Vector<Gfx::UnicodeRange> unicode_ranges, Optional<Percentage> ascent_override, Optional<Percentage> descent_override, Optional<Percentage> line_gap_override, FontDisplay font_display)
ParsedFontFace::ParsedFontFace(FlyString font_family, Optional<int> weight, Optional<int> slope, 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)
: m_font_family(move(font_family))
, m_font_named_instance(move(font_named_instance))
, m_weight(weight)
, m_slope(slope)
, m_sources(move(sources))

View file

@ -23,13 +23,14 @@ public:
Optional<FlyString> format;
};
ParsedFontFace(FlyString font_family, Optional<int> weight, Optional<int> slope, Vector<Source> sources, Vector<Gfx::UnicodeRange> unicode_ranges, Optional<Percentage> ascent_override, Optional<Percentage> descent_override, Optional<Percentage> line_gap_override, FontDisplay font_display);
ParsedFontFace(FlyString font_family, Optional<int> weight, Optional<int> slope, 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);
~ParsedFontFace() = default;
Optional<Percentage> ascent_override() const { return m_ascent_override; }
Optional<Percentage> descent_override() const { return m_descent_override; }
FontDisplay font_display() const { return m_font_display; }
FlyString font_family() const { return m_font_family; }
Optional<FlyString> font_named_instance() const { return m_font_named_instance; }
Optional<int> slope() const { return m_slope; }
Optional<int> weight() const { return m_weight; }
Optional<Percentage> line_gap_override() const { return m_line_gap_override; }
@ -38,6 +39,7 @@ public:
private:
FlyString m_font_family;
Optional<FlyString> m_font_named_instance;
Optional<int> m_weight { 0 };
Optional<int> m_slope { 0 };
Vector<Source> m_sources;

View file

@ -5451,6 +5451,7 @@ JS::GCPtr<CSSFontFaceRule> Parser::parse_font_face_rule(TokenStream<ComponentVal
auto declarations_and_at_rules = parse_a_list_of_declarations(tokens);
Optional<FlyString> font_family;
Optional<FlyString> font_named_instance;
Vector<ParsedFontFace::Source> src;
Vector<Gfx::UnicodeRange> unicode_range;
Optional<int> weight;
@ -5578,6 +5579,27 @@ JS::GCPtr<CSSFontFaceRule> Parser::parse_font_face_rule(TokenStream<ComponentVal
font_family = String::join(' ', font_family_parts).release_value_but_fixme_should_propagate_errors();
continue;
}
if (declaration.name().equals_ignoring_ascii_case("font-named-instance"sv)) {
// auto | <string>
TokenStream token_stream { declaration.values() };
token_stream.skip_whitespace();
auto& token = token_stream.next_token();
token_stream.skip_whitespace();
if (token_stream.has_next_token()) {
dbgln_if(CSS_PARSER_DEBUG, "CSSParser: Unexpected trailing tokens in font-named-instance");
continue;
}
if (token.is_ident("auto"sv)) {
font_named_instance.clear();
} else if (token.is(Token::Type::String)) {
font_named_instance = token.token().string();
} else {
dbgln_if(CSS_PARSER_DEBUG, "CSSParser: Failed to parse font-named-instance from {}", token.to_debug_string());
}
continue;
}
if (declaration.name().equals_ignoring_ascii_case("font-style"sv)) {
TokenStream token_stream { declaration.values() };
if (auto value = parse_css_value(CSS::PropertyID::FontStyle, token_stream); !value.is_error()) {
@ -5630,7 +5652,7 @@ JS::GCPtr<CSSFontFaceRule> Parser::parse_font_face_rule(TokenStream<ComponentVal
unicode_range.empend(0x0u, 0x10FFFFu);
}
return CSSFontFaceRule::create(m_context.realm(), ParsedFontFace { font_family.release_value(), weight, slope, move(src), move(unicode_range), move(ascent_override), move(descent_override), move(line_gap_override), font_display });
return CSSFontFaceRule::create(m_context.realm(), ParsedFontFace { font_family.release_value(), weight, slope, move(src), move(unicode_range), move(ascent_override), move(descent_override), move(line_gap_override), font_display, move(font_named_instance) });
}
Vector<ParsedFontFace::Source> Parser::parse_as_font_face_src()

View file

@ -694,6 +694,11 @@ void dump_font_face_rule(StringBuilder& builder, CSS::CSSFontFaceRule const& rul
indent(builder, indent_levels + 1);
builder.appendff("display: {}\n", CSS::to_string(font_face.font_display()));
if (font_face.font_named_instance().has_value()) {
indent(builder, indent_levels + 1);
builder.appendff("named-instance: {}\n", font_face.font_named_instance().value());
}
}
void dump_import_rule(StringBuilder& builder, CSS::CSSImportRule const& rule, int indent_levels)