Explorar el Código

LibWeb: Parse font-weight and font-style inside @font-face rules

Andreas Kling hace 2 años
padre
commit
74bdbdf43f

+ 4 - 1
Userland/Libraries/LibWeb/CSS/FontFace.cpp

@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
+ * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -8,8 +9,10 @@
 
 namespace Web::CSS {
 
-FontFace::FontFace(FlyString font_family, Vector<Source> sources, Vector<UnicodeRange> unicode_ranges)
+FontFace::FontFace(FlyString font_family, Optional<int> weight, Optional<int> slope, Vector<Source> sources, Vector<UnicodeRange> unicode_ranges)
     : m_font_family(move(font_family))
+    , m_weight(weight)
+    , m_slope(slope)
     , m_sources(move(sources))
     , m_unicode_ranges(move(unicode_ranges))
 {

+ 7 - 2
Userland/Libraries/LibWeb/CSS/FontFace.h

@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
+ * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -20,16 +21,20 @@ public:
         Optional<FlyString> format;
     };
 
-    FontFace(FlyString font_family, Vector<Source> sources, Vector<UnicodeRange> unicode_ranges);
+    FontFace(FlyString font_family, Optional<int> weight, Optional<int> slope, Vector<Source> sources, Vector<UnicodeRange> unicode_ranges);
     ~FontFace() = default;
 
     FlyString font_family() const { return m_font_family; }
+    Optional<int> weight() const { return m_weight; }
+    Optional<int> slope() const { return m_slope; }
     Vector<Source> const& sources() const { return m_sources; }
     Vector<UnicodeRange> 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<int> m_weight { 0 };
+    Optional<int> m_slope { 0 };
     Vector<Source> m_sources;
     Vector<UnicodeRange> m_unicode_ranges;
 };

+ 17 - 1
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -5604,6 +5604,8 @@ CSSRule* Parser::parse_font_face_rule(TokenStream<ComponentValue>& tokens)
     Optional<FlyString> font_family;
     Vector<FontFace::Source> src;
     Vector<UnicodeRange> unicode_range;
+    Optional<int> weight;
+    Optional<int> 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<ComponentValue>& 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<ComponentValue>& 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<FontFace::Source> Parser::parse_font_face_src(TokenStream<ComponentValue>& component_values)