1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:17:45 +00:00

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.
This commit is contained in:
Andreas Kling 2022-04-08 21:27:35 +02:00
parent 5b72a9cb11
commit 6e70670e0b
3 changed files with 19 additions and 20 deletions

View file

@ -1120,7 +1120,6 @@ NonnullRefPtr<StyleProperties> StyleComputer::create_document_style() const
NonnullRefPtr<StyleProperties> StyleComputer::compute_style(DOM::Element& element, Optional<CSS::Selector::PseudoElement> pseudo_element) const
{
load_fonts_if_needed();
build_rule_cache_if_needed();
auto style = StyleProperties::create();
@ -1268,9 +1267,8 @@ 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<CSSStyleSheet const&>(sheet).rules()) {
if (!is<CSSFontFaceRule>(rule))
continue;
@ -1285,6 +1283,6 @@ void StyleComputer::load_fonts_if_needed() const
auto loader = make<FontLoader>(const_cast<StyleComputer&>(*this), font_face.font_family(), move(url));
const_cast<StyleComputer&>(*this).m_loaded_fonts.set(font_face.font_family(), move(loader));
}
});
}
}

View file

@ -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<CSS::Selector::PseudoElement>) const;
void compute_font(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement>) const;
@ -113,8 +115,6 @@ private:
};
OwnPtr<RuleCache> m_rule_cache;
void load_fonts_if_needed() const;
class FontLoader;
HashMap<String, NonnullOwnPtr<FontLoader>> m_loaded_fonts;
};

View file

@ -13,9 +13,10 @@ void StyleSheetList::add_sheet(NonnullRefPtr<CSSStyleSheet> 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();
}