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.
This commit is contained in:
Andreas Kling 2023-08-17 18:51:32 +02:00
parent 69a81243f5
commit 429b2e5860
Notes: sideshowbarker 2024-07-17 02:21:14 +09:00
6 changed files with 13 additions and 13 deletions

View file

@ -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;
}

View file

@ -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;

View file

@ -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;
}

View file

@ -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;
};

View file

@ -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

View file

@ -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();