diff --git a/Libraries/LibHTML/CSS/StyleProperties.cpp b/Libraries/LibHTML/CSS/StyleProperties.cpp index 2ad7ef1447..744e1eeac4 100644 --- a/Libraries/LibHTML/CSS/StyleProperties.cpp +++ b/Libraries/LibHTML/CSS/StyleProperties.cpp @@ -1,5 +1,6 @@ #include #include +#include #include void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr value) @@ -44,6 +45,11 @@ void StyleProperties::load_font() const auto font_family = string_or_fallback(CSS::PropertyID::FontFamily, "Katica"); auto font_weight = string_or_fallback(CSS::PropertyID::FontWeight, "normal"); + if (auto cached_font = FontCache::the().get({ font_family, font_weight })) { + m_font = cached_font; + return; + } + String weight; if (font_weight == "lighter") weight = "Thin"; @@ -92,6 +98,7 @@ void StyleProperties::load_font() const #endif m_font = Font::load_from_file(String::format("/res/fonts/%s", file_name.characters())); + FontCache::the().set({ font_family, font_weight }, *m_font); } int StyleProperties::line_height() const diff --git a/Libraries/LibHTML/FontCache.cpp b/Libraries/LibHTML/FontCache.cpp new file mode 100644 index 0000000000..045f18372e --- /dev/null +++ b/Libraries/LibHTML/FontCache.cpp @@ -0,0 +1,21 @@ +#include +#include + +FontCache& FontCache::the() +{ + static FontCache cache; + return cache; +} + +RefPtr FontCache::get(const FontSelector& font_selector) const +{ + auto cached_font = m_fonts.get(font_selector); + if (cached_font.has_value()) + return cached_font.value(); + return nullptr; +} + +void FontCache::set(const FontSelector& font_selector, NonnullRefPtr font) +{ + m_fonts.set(font_selector, move(font)); +} diff --git a/Libraries/LibHTML/FontCache.h b/Libraries/LibHTML/FontCache.h new file mode 100644 index 0000000000..2d25a9f9ee --- /dev/null +++ b/Libraries/LibHTML/FontCache.h @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + +class Font; + +struct FontSelector { + String family; + String weight; + + bool operator==(const FontSelector& other) const + { + return family == other.family && weight == other.weight; + } +}; + +namespace AK { +template<> +struct Traits : public GenericTraits { + static unsigned hash(const FontSelector& key) { return pair_int_hash(key.family.hash(), key.weight.hash()); } +}; +} + +class FontCache { +public: + static FontCache& the(); + RefPtr get(const FontSelector&) const; + void set(const FontSelector&, NonnullRefPtr); + +private: + FontCache() {} + mutable HashMap> m_fonts; +}; diff --git a/Libraries/LibHTML/Makefile.shared b/Libraries/LibHTML/Makefile.shared index 48d8a87bab..b97bc80429 100644 --- a/Libraries/LibHTML/Makefile.shared +++ b/Libraries/LibHTML/Makefile.shared @@ -51,6 +51,7 @@ LIBHTML_OBJS = \ Layout/LineBox.o \ Layout/LineBoxFragment.o \ Layout/LayoutTreeBuilder.o \ + FontCache.o \ ResourceLoader.o \ HtmlView.o \ Frame.o \