From 429b2e5860ddf2e8c2d75ca61513c5aca1bc266c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 17 Aug 2023 18:51:32 +0200 Subject: [PATCH] LibWeb: Make FontCache per-StyleComputer This effectively makes it per-Document, but we hang it off of StyleComputer since that's what it's used for. The purpose of this is to prevent downloaded fonts from escaping the context that loaded them. There's probably a more elegant solution where we still share caching of system fonts, but let's start here. --- Userland/Libraries/LibWeb/CSS/StyleComputer.cpp | 4 ++-- Userland/Libraries/LibWeb/CSS/StyleComputer.h | 5 +++++ Userland/Libraries/LibWeb/FontCache.cpp | 8 +------- Userland/Libraries/LibWeb/FontCache.h | 3 +-- Userland/Libraries/LibWeb/Layout/Node.h | 4 +++- Userland/Libraries/LibWeb/Painting/ButtonPaintable.cpp | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index 3014822429..de35c504db 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -2222,7 +2222,7 @@ RefPtr StyleComputer::compute_font_for_style_values(DOM::Elemen return found_font; } - if (auto found_font = FontCache::the().get(font_selector)) + if (auto found_font = m_font_cache.get(font_selector)) return found_font; if (auto found_font = font_matching_algorithm(key, font_size_in_pt)) @@ -2296,7 +2296,7 @@ RefPtr StyleComputer::compute_font_for_style_values(DOM::Elemen } } - FontCache::the().set(font_selector, *found_font); + m_font_cache.set(font_selector, *found_font); return found_font; } diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.h b/Userland/Libraries/LibWeb/CSS/StyleComputer.h index 66ec3ce4a7..b3fe525de0 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.h +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.h @@ -18,6 +18,7 @@ #include #include #include +#include #include namespace Web::CSS { @@ -67,6 +68,8 @@ public: DOM::Document& document() { return m_document; } DOM::Document const& document() const { return m_document; } + FontCache& font_cache() const { return m_font_cache; } + NonnullRefPtr create_document_style() const; ErrorOr> compute_style(DOM::Element&, Optional = {}) const; @@ -201,6 +204,8 @@ private: OwnPtr m_author_rule_cache; OwnPtr m_user_agent_rule_cache; + mutable FontCache m_font_cache; + HashMap> m_loaded_fonts; Length::FontMetrics m_default_font_metrics; diff --git a/Userland/Libraries/LibWeb/FontCache.cpp b/Userland/Libraries/LibWeb/FontCache.cpp index 889ffe5699..c39318d151 100644 --- a/Userland/Libraries/LibWeb/FontCache.cpp +++ b/Userland/Libraries/LibWeb/FontCache.cpp @@ -8,12 +8,6 @@ #include #include -FontCache& FontCache::the() -{ - static FontCache cache; - return cache; -} - RefPtr FontCache::get(FontSelector const& font_selector) const { auto cached_font = m_fonts.get(font_selector); @@ -26,7 +20,7 @@ NonnullRefPtr FontCache::scaled_font(Gfx::Font const& font, flo { auto device_font_pt_size = font.point_size() * scale_factor; FontSelector font_selector = { FlyString::from_deprecated_fly_string(font.family()).release_value_but_fixme_should_propagate_errors(), device_font_pt_size, font.weight(), font.width(), font.slope() }; - if (auto cached_font = FontCache::the().get(font_selector)) { + if (auto cached_font = get(font_selector)) { return *cached_font; } diff --git a/Userland/Libraries/LibWeb/FontCache.h b/Userland/Libraries/LibWeb/FontCache.h index a0dccdf394..2b184ea667 100644 --- a/Userland/Libraries/LibWeb/FontCache.h +++ b/Userland/Libraries/LibWeb/FontCache.h @@ -33,13 +33,12 @@ struct Traits : public GenericTraits { class FontCache { public: - static FontCache& the(); + FontCache() = default; RefPtr get(FontSelector const&) const; void set(FontSelector const&, NonnullRefPtr); NonnullRefPtr scaled_font(Gfx::Font const&, float scale_factor); private: - FontCache() = default; mutable HashMap> m_fonts; }; diff --git a/Userland/Libraries/LibWeb/Layout/Node.h b/Userland/Libraries/LibWeb/Layout/Node.h index 7ea6e14c98..053b912013 100644 --- a/Userland/Libraries/LibWeb/Layout/Node.h +++ b/Userland/Libraries/LibWeb/Layout/Node.h @@ -13,7 +13,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -277,7 +279,7 @@ inline Gfx::Font const& Node::scaled_font(PaintContext& context) const inline Gfx::Font const& Node::scaled_font(float scale_factor) const { - return *FontCache::the().scaled_font(font(), scale_factor); + return document().style_computer().font_cache().scaled_font(font(), scale_factor); } inline const CSS::ImmutableComputedValues& Node::computed_values() const diff --git a/Userland/Libraries/LibWeb/Painting/ButtonPaintable.cpp b/Userland/Libraries/LibWeb/Painting/ButtonPaintable.cpp index 108783fada..688905c8d2 100644 --- a/Userland/Libraries/LibWeb/Painting/ButtonPaintable.cpp +++ b/Userland/Libraries/LibWeb/Painting/ButtonPaintable.cpp @@ -65,7 +65,7 @@ void ButtonPaintable::paint(PaintContext& context, PaintPhase phase) const painter.draw_text( text_rect.to_type(), static_cast(dom_node).value(), - FontCache::the().scaled_font(layout_box().font(), context.device_pixels_per_css_pixel()), + document().style_computer().font_cache().scaled_font(layout_box().font(), context.device_pixels_per_css_pixel()), Gfx::TextAlignment::Center, computed_values().color()); painter.restore();