mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:08:10 +00:00
LibHTML: Add a simple font cache
The FontCache caches the result of font lookups. The cache key is a simple object called FontSelector which consists of the font family and font weight (both strings.) This drastically reduces time spent in font lookup.
This commit is contained in:
parent
76e2c08fbf
commit
60be51f908
4 changed files with 63 additions and 0 deletions
34
Libraries/LibHTML/FontCache.h
Normal file
34
Libraries/LibHTML/FontCache.h
Normal file
|
@ -0,0 +1,34 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/String.h>
|
||||
|
||||
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<FontSelector> : public GenericTraits<FontSelector> {
|
||||
static unsigned hash(const FontSelector& key) { return pair_int_hash(key.family.hash(), key.weight.hash()); }
|
||||
};
|
||||
}
|
||||
|
||||
class FontCache {
|
||||
public:
|
||||
static FontCache& the();
|
||||
RefPtr<Font> get(const FontSelector&) const;
|
||||
void set(const FontSelector&, NonnullRefPtr<Font>);
|
||||
|
||||
private:
|
||||
FontCache() {}
|
||||
mutable HashMap<FontSelector, NonnullRefPtr<Font>> m_fonts;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue