From 74bdbdf43fb9bf6ae93ee30c37dc3d0ddcba4b3b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 24 May 2023 15:35:04 +0200 Subject: LibWeb: Parse font-weight and font-style inside @font-face rules --- Userland/Libraries/LibWeb/CSS/FontFace.cpp | 5 ++++- Userland/Libraries/LibWeb/CSS/FontFace.h | 9 +++++++-- Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 18 +++++++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) (limited to 'Userland/Libraries') diff --git a/Userland/Libraries/LibWeb/CSS/FontFace.cpp b/Userland/Libraries/LibWeb/CSS/FontFace.cpp index 8babc28f5b..1d1eb415a8 100644 --- a/Userland/Libraries/LibWeb/CSS/FontFace.cpp +++ b/Userland/Libraries/LibWeb/CSS/FontFace.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2022-2023, Sam Atkins + * Copyright (c) 2023, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,8 +9,10 @@ namespace Web::CSS { -FontFace::FontFace(FlyString font_family, Vector sources, Vector unicode_ranges) +FontFace::FontFace(FlyString font_family, Optional weight, Optional slope, Vector sources, Vector unicode_ranges) : m_font_family(move(font_family)) + , m_weight(weight) + , m_slope(slope) , m_sources(move(sources)) , m_unicode_ranges(move(unicode_ranges)) { diff --git a/Userland/Libraries/LibWeb/CSS/FontFace.h b/Userland/Libraries/LibWeb/CSS/FontFace.h index 9ae577511f..04a112a2d0 100644 --- a/Userland/Libraries/LibWeb/CSS/FontFace.h +++ b/Userland/Libraries/LibWeb/CSS/FontFace.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2022-2023, Sam Atkins + * Copyright (c) 2023, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -20,16 +21,20 @@ public: Optional format; }; - FontFace(FlyString font_family, Vector sources, Vector unicode_ranges); + FontFace(FlyString font_family, Optional weight, Optional slope, Vector sources, Vector unicode_ranges); ~FontFace() = default; FlyString font_family() const { return m_font_family; } + Optional weight() const { return m_weight; } + Optional slope() const { return m_slope; } Vector const& sources() const { return m_sources; } Vector const& unicode_ranges() const { return m_unicode_ranges; } - // FIXME: font-style, font-weight, font-stretch, font-feature-settings + // FIXME: font-stretch, font-feature-settings private: FlyString m_font_family; + Optional m_weight { 0 }; + Optional m_slope { 0 }; Vector m_sources; Vector m_unicode_ranges; }; diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 719d5512aa..de1a196102 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -5604,6 +5604,8 @@ CSSRule* Parser::parse_font_face_rule(TokenStream& tokens) Optional font_family; Vector src; Vector unicode_range; + Optional weight; + Optional slope; for (auto& declaration_or_at_rule : declarations_and_at_rules) { if (declaration_or_at_rule.is_at_rule()) { @@ -5612,6 +5614,20 @@ CSSRule* Parser::parse_font_face_rule(TokenStream& tokens) } auto const& declaration = declaration_or_at_rule.declaration(); + if (declaration.name().equals_ignoring_ascii_case("font-weight"sv)) { + TokenStream token_stream { declaration.values() }; + if (auto value = parse_css_value(CSS::PropertyID::FontWeight, token_stream); !value.is_error()) { + weight = value.value()->to_font_weight(); + } + 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()) { + slope = value.value()->to_font_slope(); + } + 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. @@ -5698,7 +5714,7 @@ CSSRule* Parser::parse_font_face_rule(TokenStream& tokens) unicode_range.empend(0x0u, 0x10FFFFu); } - return CSSFontFaceRule::create(m_context.realm(), FontFace { font_family.release_value(), move(src), move(unicode_range) }).release_value_but_fixme_should_propagate_errors(); + return CSSFontFaceRule::create(m_context.realm(), FontFace { font_family.release_value(), weight, slope, move(src), move(unicode_range) }).release_value_but_fixme_should_propagate_errors(); } Vector Parser::parse_font_face_src(TokenStream& component_values) -- cgit v1.2.3