This source file includes following definitions.
- fontCache
- getFontPlatformData
- fontVerticalDataCacheInstance
- getVerticalData
- getFontData
- fontDataFromFontPlatformData
- isPlatformFontAvailable
- getNonRetainedLastResortFallbackFont
- releaseFontData
- purgePlatformFontDataCache
- purgeFontVerticalDataCache
- purge
- addClient
- removeClient
- generation
- invalidate
#include "config.h"
#include "platform/fonts/FontCache.h"
#include "FontFamilyNames.h"
#include "RuntimeEnabledFeatures.h"
#include "platform/fonts/AlternateFontFamily.h"
#include "platform/fonts/FontCacheClient.h"
#include "platform/fonts/FontCacheKey.h"
#include "platform/fonts/FontDataCache.h"
#include "platform/fonts/FontDescription.h"
#include "platform/fonts/FontFallbackList.h"
#include "platform/fonts/FontPlatformData.h"
#include "platform/fonts/FontSmoothingMode.h"
#include "platform/fonts/TextRenderingMode.h"
#include "platform/fonts/opentype/OpenTypeVerticalData.h"
#include "wtf/HashMap.h"
#include "wtf/ListHashSet.h"
#include "wtf/StdLibExtras.h"
#include "wtf/text/AtomicStringHash.h"
#include "wtf/text/StringHash.h"
using namespace WTF;
namespace WebCore {
#if !OS(WIN)
FontCache::FontCache()
: m_purgePreventCount(0)
{
}
#endif
typedef HashMap<FontCacheKey, OwnPtr<FontPlatformData>, FontCacheKeyHash, FontCacheKeyTraits> FontPlatformDataCache;
static FontPlatformDataCache* gFontPlatformDataCache = 0;
#if OS(WIN)
bool FontCache::s_useDirectWrite = false;
IDWriteFactory* FontCache::s_directWriteFactory = 0;
bool FontCache::s_useSubpixelPositioning = false;
#endif
FontCache* FontCache::fontCache()
{
DEFINE_STATIC_LOCAL(FontCache, globalFontCache, ());
return &globalFontCache;
}
FontPlatformData* FontCache::getFontPlatformData(const FontDescription& fontDescription,
const AtomicString& passedFamilyName, bool checkingAlternateName)
{
#if OS(WIN) && ENABLE(OPENTYPE_VERTICAL)
const AtomicString& familyName = (passedFamilyName.isEmpty() || passedFamilyName[0] != '@') ?
passedFamilyName : AtomicString(passedFamilyName.impl()->substring(1));
#else
const AtomicString& familyName = passedFamilyName;
#endif
if (!gFontPlatformDataCache) {
gFontPlatformDataCache = new FontPlatformDataCache;
platformInit();
}
FontCacheKey key = fontDescription.cacheKey(familyName);
FontPlatformData* result = 0;
bool foundResult;
FontPlatformDataCache::iterator it = gFontPlatformDataCache->find(key);
if (it == gFontPlatformDataCache->end()) {
result = createFontPlatformData(fontDescription, familyName, fontDescription.effectiveFontSize());
gFontPlatformDataCache->set(key, adoptPtr(result));
foundResult = result;
} else {
result = it->value.get();
foundResult = true;
}
if (!foundResult && !checkingAlternateName) {
const AtomicString& alternateName = alternateFamilyName(familyName);
if (!alternateName.isEmpty())
result = getFontPlatformData(fontDescription, alternateName, true);
if (result)
gFontPlatformDataCache->set(key, adoptPtr(new FontPlatformData(*result)));
}
return result;
}
#if ENABLE(OPENTYPE_VERTICAL)
typedef HashMap<FontCache::FontFileKey, RefPtr<OpenTypeVerticalData>, WTF::IntHash<FontCache::FontFileKey>, WTF::UnsignedWithZeroKeyHashTraits<FontCache::FontFileKey> > FontVerticalDataCache;
FontVerticalDataCache& fontVerticalDataCacheInstance()
{
DEFINE_STATIC_LOCAL(FontVerticalDataCache, fontVerticalDataCache, ());
return fontVerticalDataCache;
}
PassRefPtr<OpenTypeVerticalData> FontCache::getVerticalData(const FontFileKey& key, const FontPlatformData& platformData)
{
FontVerticalDataCache& fontVerticalDataCache = fontVerticalDataCacheInstance();
FontVerticalDataCache::iterator result = fontVerticalDataCache.find(key);
if (result != fontVerticalDataCache.end())
return result.get()->value;
RefPtr<OpenTypeVerticalData> verticalData = OpenTypeVerticalData::create(platformData);
if (!verticalData->isOpenType())
verticalData.clear();
fontVerticalDataCache.set(key, verticalData);
return verticalData;
}
#endif
static FontDataCache* gFontDataCache = 0;
PassRefPtr<SimpleFontData> FontCache::getFontData(const FontDescription& fontDescription, const AtomicString& family, bool checkingAlternateName, ShouldRetain shouldRetain)
{
if (FontPlatformData* platformData = getFontPlatformData(fontDescription, adjustFamilyNameToAvoidUnsupportedFonts(family), checkingAlternateName))
return fontDataFromFontPlatformData(platformData, shouldRetain);
return nullptr;
}
PassRefPtr<SimpleFontData> FontCache::fontDataFromFontPlatformData(const FontPlatformData* platformData, ShouldRetain shouldRetain)
{
if (!gFontDataCache)
gFontDataCache = new FontDataCache;
#if !ASSERT_DISABLED
if (shouldRetain == DoNotRetain)
ASSERT(m_purgePreventCount);
#endif
return gFontDataCache->get(platformData, shouldRetain);
}
bool FontCache::isPlatformFontAvailable(const FontDescription& fontDescription, const AtomicString& family)
{
bool checkingAlternateName = true;
return getFontPlatformData(fontDescription, family, checkingAlternateName);
}
SimpleFontData* FontCache::getNonRetainedLastResortFallbackFont(const FontDescription& fontDescription)
{
return getLastResortFallbackFont(fontDescription, DoNotRetain).leakRef();
}
void FontCache::releaseFontData(const SimpleFontData* fontData)
{
ASSERT(gFontDataCache);
gFontDataCache->release(fontData);
}
static inline void purgePlatformFontDataCache()
{
if (!gFontPlatformDataCache)
return;
Vector<FontCacheKey> keysToRemove;
keysToRemove.reserveInitialCapacity(gFontPlatformDataCache->size());
FontPlatformDataCache::iterator platformDataEnd = gFontPlatformDataCache->end();
for (FontPlatformDataCache::iterator platformData = gFontPlatformDataCache->begin(); platformData != platformDataEnd; ++platformData) {
if (platformData->value && !gFontDataCache->contains(platformData->value.get()))
keysToRemove.append(platformData->key);
}
size_t keysToRemoveCount = keysToRemove.size();
for (size_t i = 0; i < keysToRemoveCount; ++i)
gFontPlatformDataCache->remove(keysToRemove[i]);
}
static inline void purgeFontVerticalDataCache()
{
#if ENABLE(OPENTYPE_VERTICAL)
FontVerticalDataCache& fontVerticalDataCache = fontVerticalDataCacheInstance();
if (!fontVerticalDataCache.isEmpty()) {
FontVerticalDataCache::iterator verticalDataEnd = fontVerticalDataCache.end();
for (FontVerticalDataCache::iterator verticalData = fontVerticalDataCache.begin(); verticalData != verticalDataEnd; ++verticalData) {
if (verticalData->value)
verticalData->value->setInFontCache(false);
}
gFontDataCache->markAllVerticalData();
Vector<FontCache::FontFileKey> keysToRemove;
keysToRemove.reserveInitialCapacity(fontVerticalDataCache.size());
for (FontVerticalDataCache::iterator verticalData = fontVerticalDataCache.begin(); verticalData != verticalDataEnd; ++verticalData) {
if (!verticalData->value || !verticalData->value->inFontCache())
keysToRemove.append(verticalData->key);
}
for (size_t i = 0, count = keysToRemove.size(); i < count; ++i)
fontVerticalDataCache.take(keysToRemove[i]);
}
#endif
}
void FontCache::purge(PurgeSeverity PurgeSeverity)
{
ASSERT(!m_purgePreventCount || PurgeSeverity == PurgeIfNeeded);
if (m_purgePreventCount)
return;
if (!gFontDataCache || !gFontDataCache->purge(PurgeSeverity))
return;
purgePlatformFontDataCache();
purgeFontVerticalDataCache();
}
static HashSet<FontCacheClient*>* gClients;
void FontCache::addClient(FontCacheClient* client)
{
if (!gClients)
gClients = new HashSet<FontCacheClient*>;
ASSERT(!gClients->contains(client));
gClients->add(client);
}
void FontCache::removeClient(FontCacheClient* client)
{
ASSERT(gClients);
ASSERT(gClients->contains(client));
gClients->remove(client);
}
static unsigned short gGeneration = 0;
unsigned short FontCache::generation()
{
return gGeneration;
}
void FontCache::invalidate()
{
if (!gClients) {
ASSERT(!gFontPlatformDataCache);
return;
}
if (gFontPlatformDataCache) {
delete gFontPlatformDataCache;
gFontPlatformDataCache = new FontPlatformDataCache;
}
gGeneration++;
Vector<RefPtr<FontCacheClient> > clients;
size_t numClients = gClients->size();
clients.reserveInitialCapacity(numClients);
HashSet<FontCacheClient*>::iterator end = gClients->end();
for (HashSet<FontCacheClient*>::iterator it = gClients->begin(); it != end; ++it)
clients.append(*it);
ASSERT(numClients == clients.size());
for (size_t i = 0; i < numClients; ++i)
clients[i]->fontCacheInvalidated();
purge(ForcePurge);
}
}