From 6e70670e0be3591455dedb670c21a9f414fc35f6 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 8 Apr 2022 21:27:35 +0200 Subject: [PATCH] LibWeb: Load fonts from style sheet once when sheet is added Previously, we were running the "load fonts if needed" machine at the start of every style computation. That was a lot of unnecessary work, especially on sites with lots of style rules, since we had to traverse every style sheet to see if any @font-face rules needed loading. With this patch, we now load fonts once per sheet, right after adding it to a document's style sheet list. --- .../Libraries/LibWeb/CSS/StyleComputer.cpp | 32 +++++++++---------- Userland/Libraries/LibWeb/CSS/StyleComputer.h | 4 +-- .../Libraries/LibWeb/CSS/StyleSheetList.cpp | 3 +- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index 5e49161bbc..d3074d4c5a 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -1120,7 +1120,6 @@ NonnullRefPtr StyleComputer::create_document_style() const NonnullRefPtr StyleComputer::compute_style(DOM::Element& element, Optional pseudo_element) const { - load_fonts_if_needed(); build_rule_cache_if_needed(); auto style = StyleProperties::create(); @@ -1268,23 +1267,22 @@ void StyleComputer::did_load_font([[maybe_unused]] FlyString const& family_name) document().invalidate_style(); } -void StyleComputer::load_fonts_if_needed() const +void StyleComputer::load_fonts_from_sheet(CSSStyleSheet const& sheet) { - for_each_stylesheet(CascadeOrigin::Author, [&](StyleSheet const& sheet) { - for (auto const& rule : static_cast(sheet).rules()) { - if (!is(rule)) - continue; - auto const& font_face = static_cast(rule).font_face(); - if (font_face.sources().is_empty()) - continue; - if (m_loaded_fonts.contains(font_face.font_family())) - continue; + for (auto const& rule : static_cast(sheet).rules()) { + if (!is(rule)) + continue; + auto const& font_face = static_cast(rule).font_face(); + if (font_face.sources().is_empty()) + continue; + if (m_loaded_fonts.contains(font_face.font_family())) + continue; - LoadRequest request; - auto url = m_document.parse_url(font_face.sources().first().url.to_string()); - auto loader = make(const_cast(*this), font_face.font_family(), move(url)); - const_cast(*this).m_loaded_fonts.set(font_face.font_family(), move(loader)); - } - }); + LoadRequest request; + auto url = m_document.parse_url(font_face.sources().first().url.to_string()); + auto loader = make(const_cast(*this), font_face.font_family(), move(url)); + const_cast(*this).m_loaded_fonts.set(font_face.font_family(), move(loader)); + } } + } diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.h b/Userland/Libraries/LibWeb/CSS/StyleComputer.h index aab5d88bfe..d54393ae88 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.h +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.h @@ -74,6 +74,8 @@ public: void did_load_font(FlyString const& family_name); + void load_fonts_from_sheet(CSSStyleSheet const&); + private: void compute_cascaded_values(StyleProperties&, DOM::Element&, Optional) const; void compute_font(StyleProperties&, DOM::Element const*, Optional) const; @@ -113,8 +115,6 @@ private: }; OwnPtr m_rule_cache; - void load_fonts_if_needed() const; - class FontLoader; HashMap> m_loaded_fonts; }; diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp b/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp index a43ab4711a..18f177009c 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp @@ -13,9 +13,10 @@ void StyleSheetList::add_sheet(NonnullRefPtr sheet) { VERIFY(!m_sheets.contains_slow(sheet)); sheet->set_style_sheet_list({}, this); - m_sheets.append(move(sheet)); + m_sheets.append(sheet); m_document.style_computer().invalidate_rule_cache(); + m_document.style_computer().load_fonts_from_sheet(*sheet); m_document.invalidate_style(); }