1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 12:37:45 +00:00

LibGfx+LibWeb: Move Gfx::ScaledFont caching from LibWeb into LibGfx

Before this change, we would only cache and reuse Gfx::ScaledFont
instances for downloaded CSS fonts.

By moving it into Gfx::VectorFont, we get caching for all vector fonts,
including local system TTFs etc.

This avoids a *lot* of style invalidations in LibWeb, since we now vend
the same Gfx::Font pointer for the same font when used repeatedly.
This commit is contained in:
Andreas Kling 2023-12-25 12:45:18 +01:00
parent bf8107b247
commit f900957d26
16 changed files with 54 additions and 137 deletions

View file

@ -55,7 +55,6 @@
#include <LibWeb/CSS/StyleValues/UnsetStyleValue.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/FontCache.h>
#include <LibWeb/HTML/HTMLBRElement.h>
#include <LibWeb/HTML/HTMLHtmlElement.h>
#include <LibWeb/Layout/Node.h>
@ -122,19 +121,7 @@ public:
start_loading_next_url();
return nullptr;
}
if (auto it = m_cached_fonts.find(point_size); it != m_cached_fonts.end())
return it->value;
// FIXME: It might be nicer to have a global cap on the number of fonts we cache
// instead of doing it at the per-font level like this.
constexpr size_t max_cached_font_size_count = 64;
if (m_cached_fonts.size() > max_cached_font_size_count)
m_cached_fonts.remove(m_cached_fonts.begin());
auto font = adopt_ref(*new Gfx::ScaledFont(*m_vector_font, point_size, point_size));
m_cached_fonts.set(point_size, font);
return font;
return m_vector_font->scaled_font(point_size);
}
private:
@ -189,8 +176,6 @@ private:
Vector<Gfx::UnicodeRange> m_unicode_ranges;
RefPtr<Gfx::VectorFont> m_vector_font;
Vector<AK::URL> m_urls;
HashMap<float, NonnullRefPtr<Gfx::ScaledFont>> mutable m_cached_fonts;
};
struct StyleComputer::MatchingFontCandidate {
@ -1796,14 +1781,11 @@ RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(
// FIXME: Implement the full font-matching algorithm: https://www.w3.org/TR/css-fonts-4/#font-matching-algorithm
// Note: This is modified by the find_font() lambda
FontSelector font_selector;
bool monospace = false;
float const font_size_in_pt = font_size_in_px * 0.75f;
auto find_font = [&](FlyString const& family) -> RefPtr<Gfx::FontCascadeList const> {
font_selector = { family, font_size_in_pt, weight, width, slope };
FontFaceKey key {
.family_name = family,
.weight = weight,
@ -2381,9 +2363,8 @@ CSSPixelRect StyleComputer::viewport_rect() const
return {};
}
void StyleComputer::did_load_font(FlyString const& family_name)
void StyleComputer::did_load_font(FlyString const&)
{
m_font_cache.did_load_font({}, family_name);
document().invalidate_style();
}

View file

@ -16,7 +16,6 @@
#include <LibWeb/CSS/CSSStyleDeclaration.h>
#include <LibWeb/CSS/Selector.h>
#include <LibWeb/CSS/StyleProperties.h>
#include <LibWeb/FontCache.h>
#include <LibWeb/Forward.h>
namespace Web::CSS {
@ -48,8 +47,6 @@ 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::Type> = {}) const;
@ -184,8 +181,6 @@ private:
OwnPtr<RuleCache> m_user_agent_rule_cache;
JS::Handle<CSSStyleSheet> m_user_style_sheet;
mutable FontCache m_font_cache;
using FontLoaderList = Vector<NonnullOwnPtr<FontLoader>>;
HashMap<FontFaceKey, FontLoaderList> m_loaded_fonts;

View file

@ -28,7 +28,6 @@
#include <LibWeb/CSS/StyleValues/StringStyleValue.h>
#include <LibWeb/CSS/StyleValues/StyleValueList.h>
#include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
#include <LibWeb/FontCache.h>
#include <LibWeb/Layout/BlockContainer.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Platform/FontPlugin.h>