SystemFontDatabaseCoreText.h [plain text]
#pragma once
#include "FontDescription.h"
#include <pal/spi/cocoa/CoreTextSPI.h>
#include <wtf/HashMap.h>
#include <wtf/HashTraits.h>
#include <wtf/text/AtomString.h>
#include <wtf/text/AtomStringHash.h>
namespace WebCore {
enum class SystemFontKind : uint8_t {
SystemUI,
UISerif,
UIMonospace,
UIRounded,
TextStyle
};
class SystemFontDatabaseCoreText {
public:
struct CascadeListParameters {
CascadeListParameters()
{
}
CascadeListParameters(WTF::HashTableDeletedValueType)
: fontName(WTF::HashTableDeletedValue)
{
}
bool isHashTableDeletedValue() const
{
return fontName.isHashTableDeletedValue();
}
bool operator==(const CascadeListParameters& other) const
{
return fontName == other.fontName
&& locale == other.locale
&& weight == other.weight
&& size == other.size
&& allowUserInstalledFonts == other.allowUserInstalledFonts
&& italic == other.italic;
}
unsigned hash() const
{
IntegerHasher hasher;
ASSERT(!fontName.isNull());
hasher.add(locale.existingHash());
hasher.add(locale.isNull() ? 0 : locale.existingHash());
hasher.add(weight);
hasher.add(size);
hasher.add(static_cast<unsigned>(allowUserInstalledFonts));
hasher.add(italic);
return hasher.hash();
}
struct CascadeListParametersHash : WTF::PairHash<AtomString, float> {
static unsigned hash(const CascadeListParameters& parameters)
{
return parameters.hash();
}
static bool equal(const CascadeListParameters& a, const CascadeListParameters& b)
{
return a == b;
}
static const bool safeToCompareToEmptyOrDeleted = true;
};
AtomString fontName;
AtomString locale;
CGFloat weight { 0 };
float size { 0 };
AllowUserInstalledFonts allowUserInstalledFonts { AllowUserInstalledFonts::No };
bool italic { false };
};
static SystemFontDatabaseCoreText& singleton();
Vector<RetainPtr<CTFontDescriptorRef>> cascadeList(const FontDescription&, const AtomString& cssFamily, SystemFontKind, AllowUserInstalledFonts);
String serifFamily(const String& locale);
String sansSerifFamily(const String& locale);
String cursiveFamily(const String& locale);
String fantasyFamily(const String& locale);
String monospaceFamily(const String& locale);
void clear();
private:
SystemFontDatabaseCoreText();
Vector<RetainPtr<CTFontDescriptorRef>> cascadeList(const CascadeListParameters&, SystemFontKind);
RetainPtr<CTFontRef> createSystemUIFont(const CascadeListParameters&, CFStringRef locale);
RetainPtr<CTFontRef> createSystemDesignFont(SystemFontKind, const CascadeListParameters&);
RetainPtr<CTFontRef> createTextStyleFont(const CascadeListParameters&);
static RetainPtr<CTFontRef> createFontByApplyingWeightItalicsAndFallbackBehavior(CTFontRef, CGFloat weight, bool italic, float size, AllowUserInstalledFonts, CFStringRef design = nullptr);
static RetainPtr<CTFontDescriptorRef> removeCascadeList(CTFontDescriptorRef);
static Vector<RetainPtr<CTFontDescriptorRef>> computeCascadeList(CTFontRef, CFStringRef locale);
static CascadeListParameters systemFontParameters(const FontDescription&, const AtomString& familyName, SystemFontKind, AllowUserInstalledFonts);
HashMap<CascadeListParameters, Vector<RetainPtr<CTFontDescriptorRef>>, CascadeListParameters::CascadeListParametersHash, SimpleClassHashTraits<CascadeListParameters>> m_systemFontCache;
HashMap<String, String> m_serifFamilies;
HashMap<String, String> m_sansSeriferifFamilies;
HashMap<String, String> m_cursiveFamilies;
HashMap<String, String> m_fantasyFamilies;
HashMap<String, String> m_monospaceFamilies;
};
}