1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:18:13 +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,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<CSSStyleSheet const&>(sheet).rules()) {
if (!is<CSSFontFaceRule>(rule))
continue;
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()))
continue;
for (auto const& rule : static_cast<CSSStyleSheet const&>(sheet).rules()) {
if (!is<CSSFontFaceRule>(rule))
continue;
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()))
continue;
LoadRequest request;
auto url = m_document.parse_url(font_face.sources().first().url.to_string());
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));
}
});
LoadRequest request;
auto url = m_document.parse_url(font_face.sources().first().url.to_string());
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));
}
}
}