Procházet zdrojové kódy

LibWeb/CSS: Parse font-width descriptor and its font-stretch alias

Sam Atkins před 10 měsíci
rodič
revize
b1870e7029

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

@@ -392,7 +392,8 @@ void FontFace::load_font_source()
             ParsedFontFace parsed_font_face {
                 font->m_family,
                 font->m_weight.to_number<int>(),
-                0, // FIXME: slope
+                0,                      // FIXME: slope
+                Gfx::FontWidth::Normal, // FIXME: width
                 font->m_urls,
                 font->m_unicode_ranges,
                 {},                // FIXME: ascent_override

+ 2 - 1
Userland/Libraries/LibWeb/CSS/ParsedFontFace.cpp

@@ -9,11 +9,12 @@
 
 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, Optional<FlyString> font_named_instance)
+ParsedFontFace::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)
     : m_font_family(move(font_family))
     , m_font_named_instance(move(font_named_instance))
     , m_weight(weight)
     , m_slope(slope)
+    , m_width(width)
     , m_sources(move(sources))
     , m_unicode_ranges(move(unicode_ranges))
     , m_ascent_override(move(ascent_override))

+ 5 - 3
Userland/Libraries/LibWeb/CSS/ParsedFontFace.h

@@ -23,7 +23,7 @@ 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, Optional<FlyString> font_named_instance);
+    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);
     ~ParsedFontFace() = default;
 
     Optional<Percentage> ascent_override() const { return m_ascent_override; }
@@ -33,6 +33,7 @@ public:
     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<int> width() const { return m_width; }
     Optional<Percentage> line_gap_override() const { return m_line_gap_override; }
     Vector<Source> const& sources() const { return m_sources; }
     Vector<Gfx::UnicodeRange> const& unicode_ranges() const { return m_unicode_ranges; }
@@ -40,8 +41,9 @@ public:
 private:
     FlyString m_font_family;
     Optional<FlyString> m_font_named_instance;
-    Optional<int> m_weight { 0 };
-    Optional<int> m_slope { 0 };
+    Optional<int> m_weight;
+    Optional<int> m_slope;
+    Optional<int> m_width;
     Vector<Source> m_sources;
     Vector<Gfx::UnicodeRange> m_unicode_ranges;
     Optional<Percentage> m_ascent_override;

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

@@ -5457,6 +5457,7 @@ JS::GCPtr<CSSFontFaceRule> Parser::parse_font_face_rule(TokenStream<ComponentVal
     Vector<Gfx::UnicodeRange> unicode_range;
     Optional<int> weight;
     Optional<int> slope;
+    Optional<int> width;
     Optional<Percentage> ascent_override;
     Optional<Percentage> descent_override;
     Optional<Percentage> line_gap_override;
@@ -5615,6 +5616,14 @@ JS::GCPtr<CSSFontFaceRule> Parser::parse_font_face_rule(TokenStream<ComponentVal
             }
             continue;
         }
+        if (declaration.name().equals_ignoring_ascii_case("font-width"sv)
+            || declaration.name().equals_ignoring_ascii_case("font-stretch"sv)) {
+            TokenStream token_stream { declaration.values() };
+            if (auto value = parse_css_value(CSS::PropertyID::FontWidth, token_stream); !value.is_error()) {
+                width = value.value()->to_font_width();
+            }
+            continue;
+        }
         if (declaration.name().equals_ignoring_ascii_case("line-gap-override"sv)) {
             auto value = parse_as_percentage_or_normal(declaration.values());
             if (value.is_error()) {
@@ -5653,7 +5662,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, move(font_named_instance) });
+    return CSSFontFaceRule::create(m_context.realm(), ParsedFontFace { font_family.release_value(), move(weight), move(slope), move(width), 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()

+ 5 - 0
Userland/Libraries/LibWeb/Dump.cpp

@@ -662,6 +662,11 @@ void dump_font_face_rule(StringBuilder& builder, CSS::CSSFontFaceRule const& rul
         builder.appendff("slope: {}\n", font_face.slope().value());
     }
 
+    if (font_face.width().has_value()) {
+        indent(builder, indent_levels + 1);
+        builder.appendff("width: {}\n", font_face.width().value());
+    }
+
     indent(builder, indent_levels + 1);
     builder.append("sources:\n"sv);
     for (auto const& source : font_face.sources()) {