浏览代码

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.
Andreas Kling 1 年之前
父节点
当前提交
429b2e5860

+ 2 - 2
Userland/Libraries/LibWeb/CSS/StyleComputer.cpp

@@ -2222,7 +2222,7 @@ RefPtr<Gfx::Font const> 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<Gfx::Font const> 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;
 }

+ 5 - 0
Userland/Libraries/LibWeb/CSS/StyleComputer.h

@@ -18,6 +18,7 @@
 #include <LibWeb/CSS/Parser/TokenStream.h>
 #include <LibWeb/CSS/Selector.h>
 #include <LibWeb/CSS/StyleProperties.h>
+#include <LibWeb/FontCache.h>
 #include <LibWeb/Forward.h>
 
 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<StyleProperties> create_document_style() const;
 
     ErrorOr<NonnullRefPtr<StyleProperties>> compute_style(DOM::Element&, Optional<CSS::Selector::PseudoElement> = {}) const;
@@ -201,6 +204,8 @@ private:
     OwnPtr<RuleCache> m_author_rule_cache;
     OwnPtr<RuleCache> m_user_agent_rule_cache;
 
+    mutable FontCache m_font_cache;
+
     HashMap<FontFaceKey, NonnullOwnPtr<FontLoader>> m_loaded_fonts;
 
     Length::FontMetrics m_default_font_metrics;

+ 1 - 7
Userland/Libraries/LibWeb/FontCache.cpp

@@ -8,12 +8,6 @@
 #include <LibGfx/Font/Font.h>
 #include <LibWeb/FontCache.h>
 
-FontCache& FontCache::the()
-{
-    static FontCache cache;
-    return cache;
-}
-
 RefPtr<Gfx::Font const> FontCache::get(FontSelector const& font_selector) const
 {
     auto cached_font = m_fonts.get(font_selector);
@@ -26,7 +20,7 @@ NonnullRefPtr<Gfx::Font const> 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;
     }
 

+ 1 - 2
Userland/Libraries/LibWeb/FontCache.h

@@ -33,13 +33,12 @@ struct Traits<FontSelector> : public GenericTraits<FontSelector> {
 
 class FontCache {
 public:
-    static FontCache& the();
+    FontCache() = default;
     RefPtr<Gfx::Font const> get(FontSelector const&) const;
     void set(FontSelector const&, NonnullRefPtr<Gfx::Font const>);
 
     NonnullRefPtr<Gfx::Font const> scaled_font(Gfx::Font const&, float scale_factor);
 
 private:
-    FontCache() = default;
     mutable HashMap<FontSelector, NonnullRefPtr<Gfx::Font const>> m_fonts;
 };

+ 3 - 1
Userland/Libraries/LibWeb/Layout/Node.h

@@ -13,7 +13,9 @@
 #include <LibJS/Heap/Cell.h>
 #include <LibJS/Heap/Handle.h>
 #include <LibWeb/CSS/ComputedValues.h>
+#include <LibWeb/CSS/StyleComputer.h>
 #include <LibWeb/CSS/StyleProperties.h>
+#include <LibWeb/DOM/Document.h>
 #include <LibWeb/FontCache.h>
 #include <LibWeb/Forward.h>
 #include <LibWeb/Layout/BoxModelMetrics.h>
@@ -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

+ 1 - 1
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<int>(),
             static_cast<HTML::HTMLInputElement const&>(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();