diff options
author | Andreas Kling <kling@serenityos.org> | 2023-05-24 15:35:30 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-05-24 17:19:18 +0200 |
commit | 38b2cba6a2077a397aae0df2207296e67e63fbf2 (patch) | |
tree | 93d248092c446e8f019b9635271ffd0b2207ee12 /Userland/Libraries/LibWeb | |
parent | 74bdbdf43fb9bf6ae93ee30c37dc3d0ddcba4b3b (diff) | |
download | serenity-38b2cba6a2077a397aae0df2207296e67e63fbf2.zip |
LibWeb: Honor font-weight and font-style when using downloaded fonts
Instead of keying downloaded @font-face fonts on just the family name,
we now key them on a tuple of the family name, weight and slope.
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleComputer.cpp | 27 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleComputer.h | 11 |
2 files changed, 34 insertions, 4 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index 2563e539de..f06d5e6809 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -59,6 +59,16 @@ #include <LibWeb/Platform/FontPlugin.h> #include <stdio.h> +namespace AK { + +// traits for FontFaceKey +template<> +struct Traits<Web::CSS::FontFaceKey> : public GenericTraits<Web::CSS::FontFaceKey> { + static unsigned hash(Web::CSS::FontFaceKey const& key) { return pair_int_hash(key.family_name.hash(), pair_int_hash(key.weight, key.slope)); } +}; + +} + namespace Web::CSS { StyleComputer::StyleComputer(DOM::Document& document) @@ -1263,7 +1273,13 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele auto find_font = [&](String const& family) -> RefPtr<Gfx::Font const> { font_selector = { family, font_size_in_pt, weight, width, slope }; - if (auto it = m_loaded_fonts.find(family); it != m_loaded_fonts.end()) { + FontFaceKey key { + .family_name = family, + .weight = weight, + .slope = slope, + }; + + if (auto it = m_loaded_fonts.find(key); it != m_loaded_fonts.end()) { auto& loader = *it->value; if (auto found_font = loader.font_with_point_size(font_size_in_pt)) return found_font; @@ -1700,7 +1716,12 @@ void StyleComputer::load_fonts_from_sheet(CSSStyleSheet const& sheet) auto const& font_face = static_cast<CSSFontFaceRule const&>(*rule).font_face(); if (font_face.sources().is_empty()) continue; - if (m_loaded_fonts.contains(font_face.font_family())) + FontFaceKey key { + .family_name = font_face.font_family(), + .weight = font_face.weight().value_or(0), + .slope = font_face.slope().value_or(0), + }; + if (m_loaded_fonts.contains(key)) continue; Vector<AK::URL> urls; @@ -1713,7 +1734,7 @@ void StyleComputer::load_fonts_from_sheet(CSSStyleSheet const& sheet) continue; auto loader = make<FontLoader>(const_cast<StyleComputer&>(*this), font_face.font_family(), move(urls)); - const_cast<StyleComputer&>(*this).m_loaded_fonts.set(font_face.font_family().to_string(), move(loader)); + const_cast<StyleComputer&>(*this).m_loaded_fonts.set(key, move(loader)); } } diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.h b/Userland/Libraries/LibWeb/CSS/StyleComputer.h index 2503075a9e..83b997f94f 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.h +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.h @@ -47,6 +47,15 @@ private: bool m_marked { false }; }; +struct FontFaceKey { + FlyString family_name; + int weight { 0 }; + int slope { 0 }; + + [[nodiscard]] u32 hash() const { return pair_int_hash(family_name.hash(), pair_int_hash(weight, slope)); } + [[nodiscard]] bool operator==(FontFaceKey const&) const = default; +}; + class StyleComputer { public: explicit StyleComputer(DOM::Document&); @@ -132,7 +141,7 @@ private: OwnPtr<RuleCache> m_user_agent_rule_cache; class FontLoader; - HashMap<String, NonnullOwnPtr<FontLoader>> m_loaded_fonts; + HashMap<FontFaceKey, NonnullOwnPtr<FontLoader>> m_loaded_fonts; Length::FontMetrics m_default_font_metrics; Length::FontMetrics m_root_element_font_metrics; |