#ifndef FontCache_h
#define FontCache_h
#include <limits.h>
#include "platform/PlatformExport.h"
#include "wtf/Forward.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefPtr.h"
#include "wtf/Vector.h"
#include "wtf/text/CString.h"
#include "wtf/text/WTFString.h"
#include "wtf/unicode/Unicode.h"
#if OS(WIN)
#include "SkFontMgr.h"
#include <windows.h>
#include <objidl.h>
#include <mlang.h>
struct IDWriteFactory;
#endif
#if OS(ANDROID)
#include <unicode/uscript.h>
#endif
class SkTypeface;
namespace WebCore {
class FontCacheClient;
class FontPlatformData;
class FontData;
class FontDescription;
class OpenTypeVerticalData;
class SimpleFontData;
enum ShouldRetain { Retain, DoNotRetain };
enum PurgeSeverity { PurgeIfNeeded, ForcePurge };
class PLATFORM_EXPORT FontCache {
friend class FontCachePurgePreventer;
WTF_MAKE_NONCOPYABLE(FontCache); WTF_MAKE_FAST_ALLOCATED;
public:
static FontCache* fontCache();
void releaseFontData(const SimpleFontData*);
PassRefPtr<SimpleFontData> platformFallbackForCharacter(const FontDescription&, UChar32, const SimpleFontData* fontDataToSubstitute);
void platformInit();
PassRefPtr<SimpleFontData> getFontData(const FontDescription&, const AtomicString&, bool checkingAlternateName = false, ShouldRetain = Retain);
PassRefPtr<SimpleFontData> getLastResortFallbackFont(const FontDescription&, ShouldRetain = Retain);
SimpleFontData* getNonRetainedLastResortFallbackFont(const FontDescription&);
bool isPlatformFontAvailable(const FontDescription&, const AtomicString&);
void addClient(FontCacheClient*);
void removeClient(FontCacheClient*);
unsigned short generation();
void invalidate();
#if OS(WIN)
PassRefPtr<SimpleFontData> fontDataFromDescriptionAndLogFont(const FontDescription&, ShouldRetain, const LOGFONT&, wchar_t* outFontFamilyName);
#endif
#if OS(WIN)
bool useSubpixelPositioning() const { return s_useSubpixelPositioning; }
SkFontMgr* fontManager() { return m_fontManager.get(); }
static void setUseDirectWrite(bool useDirectWrite) { s_useDirectWrite = useDirectWrite; }
static void setDirectWriteFactory(IDWriteFactory* factory) { s_directWriteFactory = factory; }
static void setUseSubpixelPositioning(bool useSubpixelPositioning) { s_useSubpixelPositioning = useSubpixelPositioning; }
#endif
#if ENABLE(OPENTYPE_VERTICAL)
typedef uint32_t FontFileKey;
PassRefPtr<OpenTypeVerticalData> getVerticalData(const FontFileKey&, const FontPlatformData&);
#endif
#if OS(ANDROID)
static AtomicString getGenericFamilyNameForScript(const AtomicString& familyName, UScriptCode);
#else
struct SimpleFontFamily {
String name;
bool isBold;
bool isItalic;
};
static void getFontFamilyForCharacter(UChar32, const char* preferredLocale, SimpleFontFamily*);
#endif
private:
FontCache();
~FontCache();
void purge(PurgeSeverity = PurgeIfNeeded);
void disablePurging() { m_purgePreventCount++; }
void enablePurging()
{
ASSERT(m_purgePreventCount);
if (!--m_purgePreventCount)
purge(PurgeIfNeeded);
}
FontPlatformData* getFontPlatformData(const FontDescription&, const AtomicString& family, bool checkingAlternateName = false);
FontPlatformData* createFontPlatformData(const FontDescription&, const AtomicString& family, float fontSize);
PassRefPtr<SkTypeface> createTypeface(const FontDescription&, const AtomicString& family, CString& name);
PassRefPtr<SimpleFontData> fontDataFromFontPlatformData(const FontPlatformData*, ShouldRetain = Retain);
int m_purgePreventCount;
#if OS(WIN)
OwnPtr<SkFontMgr> m_fontManager;
static bool s_useDirectWrite;
static IDWriteFactory* s_directWriteFactory;
static bool s_useSubpixelPositioning;
#endif
#if OS(MACOSX) || OS(ANDROID)
friend class ComplexTextController;
#endif
friend class SimpleFontData;
friend class FontFallbackList;
};
class PLATFORM_EXPORT FontCachePurgePreventer {
public:
FontCachePurgePreventer() { FontCache::fontCache()->disablePurging(); }
~FontCachePurgePreventer() { FontCache::fontCache()->enablePurging(); }
};
}
#endif