2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "platform/fonts/FontCache.h"
33 #include "FontFamilyNames.h"
35 #include "RuntimeEnabledFeatures.h"
36 #include "platform/fonts/AlternateFontFamily.h"
37 #include "platform/fonts/FontCacheClient.h"
38 #include "platform/fonts/FontCacheKey.h"
39 #include "platform/fonts/FontDataCache.h"
40 #include "platform/fonts/FontDescription.h"
41 #include "platform/fonts/FontFallbackList.h"
42 #include "platform/fonts/FontPlatformData.h"
43 #include "platform/fonts/FontSmoothingMode.h"
44 #include "platform/fonts/TextRenderingMode.h"
45 #include "platform/fonts/opentype/OpenTypeVerticalData.h"
46 #include "wtf/HashMap.h"
47 #include "wtf/ListHashSet.h"
48 #include "wtf/StdLibExtras.h"
49 #include "wtf/text/AtomicStringHash.h"
50 #include "wtf/text/StringHash.h"
57 FontCache::FontCache()
58 : m_purgePreventCount(0)
63 typedef HashMap<FontCacheKey, OwnPtr<FontPlatformData>, FontCacheKeyHash, FontCacheKeyTraits> FontPlatformDataCache;
65 static FontPlatformDataCache* gFontPlatformDataCache = 0;
68 bool FontCache::s_useDirectWrite = false;
69 IDWriteFactory* FontCache::s_directWriteFactory = 0;
70 bool FontCache::s_useSubpixelPositioning = false;
73 FontCache* FontCache::fontCache()
75 DEFINE_STATIC_LOCAL(FontCache, globalFontCache, ());
76 return &globalFontCache;
79 FontPlatformData* FontCache::getFontPlatformData(const FontDescription& fontDescription,
80 const AtomicString& passedFamilyName, bool checkingAlternateName)
82 #if OS(WIN) && ENABLE(OPENTYPE_VERTICAL)
83 // Leading "@" in the font name enables Windows vertical flow flag for the font.
84 // Because we do vertical flow by ourselves, we don't want to use the Windows feature.
85 // IE disregards "@" regardless of the orientatoin, so we follow the behavior.
86 const AtomicString& familyName = (passedFamilyName.isEmpty() || passedFamilyName[0] != '@') ?
87 passedFamilyName : AtomicString(passedFamilyName.impl()->substring(1));
89 const AtomicString& familyName = passedFamilyName;
92 if (!gFontPlatformDataCache) {
93 gFontPlatformDataCache = new FontPlatformDataCache;
97 FontCacheKey key = fontDescription.cacheKey(familyName);
98 FontPlatformData* result = 0;
100 FontPlatformDataCache::iterator it = gFontPlatformDataCache->find(key);
101 if (it == gFontPlatformDataCache->end()) {
102 result = createFontPlatformData(fontDescription, familyName, fontDescription.effectiveFontSize());
103 gFontPlatformDataCache->set(key, adoptPtr(result));
104 foundResult = result;
106 result = it->value.get();
110 if (!foundResult && !checkingAlternateName) {
111 // We were unable to find a font. We have a small set of fonts that we alias to other names,
112 // e.g., Arial/Helvetica, Courier/Courier New, etc. Try looking up the font under the aliased name.
113 const AtomicString& alternateName = alternateFamilyName(familyName);
114 if (!alternateName.isEmpty())
115 result = getFontPlatformData(fontDescription, alternateName, true);
117 gFontPlatformDataCache->set(key, adoptPtr(new FontPlatformData(*result))); // Cache the result under the old name.
123 #if ENABLE(OPENTYPE_VERTICAL)
124 typedef HashMap<FontCache::FontFileKey, RefPtr<OpenTypeVerticalData>, WTF::IntHash<FontCache::FontFileKey>, WTF::UnsignedWithZeroKeyHashTraits<FontCache::FontFileKey> > FontVerticalDataCache;
126 FontVerticalDataCache& fontVerticalDataCacheInstance()
128 DEFINE_STATIC_LOCAL(FontVerticalDataCache, fontVerticalDataCache, ());
129 return fontVerticalDataCache;
132 PassRefPtr<OpenTypeVerticalData> FontCache::getVerticalData(const FontFileKey& key, const FontPlatformData& platformData)
134 FontVerticalDataCache& fontVerticalDataCache = fontVerticalDataCacheInstance();
135 FontVerticalDataCache::iterator result = fontVerticalDataCache.find(key);
136 if (result != fontVerticalDataCache.end())
137 return result.get()->value;
139 RefPtr<OpenTypeVerticalData> verticalData = OpenTypeVerticalData::create(platformData);
140 if (!verticalData->isOpenType())
141 verticalData.clear();
142 fontVerticalDataCache.set(key, verticalData);
147 static FontDataCache* gFontDataCache = 0;
149 PassRefPtr<SimpleFontData> FontCache::getFontData(const FontDescription& fontDescription, const AtomicString& family, bool checkingAlternateName, ShouldRetain shouldRetain)
151 if (FontPlatformData* platformData = getFontPlatformData(fontDescription, adjustFamilyNameToAvoidUnsupportedFonts(family), checkingAlternateName))
152 return fontDataFromFontPlatformData(platformData, shouldRetain);
157 PassRefPtr<SimpleFontData> FontCache::fontDataFromFontPlatformData(const FontPlatformData* platformData, ShouldRetain shouldRetain)
160 gFontDataCache = new FontDataCache;
163 if (shouldRetain == DoNotRetain)
164 ASSERT(m_purgePreventCount);
167 return gFontDataCache->get(platformData, shouldRetain);
170 bool FontCache::isPlatformFontAvailable(const FontDescription& fontDescription, const AtomicString& family)
172 bool checkingAlternateName = true;
173 return getFontPlatformData(fontDescription, family, checkingAlternateName);
176 SimpleFontData* FontCache::getNonRetainedLastResortFallbackFont(const FontDescription& fontDescription)
178 return getLastResortFallbackFont(fontDescription, DoNotRetain).leakRef();
181 void FontCache::releaseFontData(const SimpleFontData* fontData)
183 ASSERT(gFontDataCache);
185 gFontDataCache->release(fontData);
188 static inline void purgePlatformFontDataCache()
190 if (!gFontPlatformDataCache)
193 Vector<FontCacheKey> keysToRemove;
194 keysToRemove.reserveInitialCapacity(gFontPlatformDataCache->size());
195 FontPlatformDataCache::iterator platformDataEnd = gFontPlatformDataCache->end();
196 for (FontPlatformDataCache::iterator platformData = gFontPlatformDataCache->begin(); platformData != platformDataEnd; ++platformData) {
197 if (platformData->value && !gFontDataCache->contains(platformData->value.get()))
198 keysToRemove.append(platformData->key);
200 gFontPlatformDataCache->removeAll(keysToRemove);
203 static inline void purgeFontVerticalDataCache()
205 #if ENABLE(OPENTYPE_VERTICAL)
206 FontVerticalDataCache& fontVerticalDataCache = fontVerticalDataCacheInstance();
207 if (!fontVerticalDataCache.isEmpty()) {
208 // Mark & sweep unused verticalData
209 FontVerticalDataCache::iterator verticalDataEnd = fontVerticalDataCache.end();
210 for (FontVerticalDataCache::iterator verticalData = fontVerticalDataCache.begin(); verticalData != verticalDataEnd; ++verticalData) {
211 if (verticalData->value)
212 verticalData->value->setInFontCache(false);
215 gFontDataCache->markAllVerticalData();
217 Vector<FontCache::FontFileKey> keysToRemove;
218 keysToRemove.reserveInitialCapacity(fontVerticalDataCache.size());
219 for (FontVerticalDataCache::iterator verticalData = fontVerticalDataCache.begin(); verticalData != verticalDataEnd; ++verticalData) {
220 if (!verticalData->value || !verticalData->value->inFontCache())
221 keysToRemove.append(verticalData->key);
223 fontVerticalDataCache.removeAll(keysToRemove);
228 void FontCache::purge(PurgeSeverity PurgeSeverity)
230 // We should never be forcing the purge while the FontCachePurgePreventer is in scope.
231 ASSERT(!m_purgePreventCount || PurgeSeverity == PurgeIfNeeded);
232 if (m_purgePreventCount)
235 if (!gFontDataCache || !gFontDataCache->purge(PurgeSeverity))
238 purgePlatformFontDataCache();
239 purgeFontVerticalDataCache();
242 static bool invalidateFontCache = false;
244 WillBeHeapHashSet<RawPtrWillBeWeakMember<FontCacheClient> >& fontCacheClients()
247 DEFINE_STATIC_LOCAL(Persistent<HeapHashSet<WeakMember<FontCacheClient> > >, clients, (new HeapHashSet<WeakMember<FontCacheClient> >()));
249 DEFINE_STATIC_LOCAL(HashSet<RawPtr<FontCacheClient> >*, clients, (new HashSet<RawPtr<FontCacheClient> >()));
251 invalidateFontCache = true;
255 void FontCache::addClient(FontCacheClient* client)
257 ASSERT(!fontCacheClients().contains(client));
258 fontCacheClients().add(client);
262 void FontCache::removeClient(FontCacheClient* client)
264 ASSERT(fontCacheClients().contains(client));
265 fontCacheClients().remove(client);
269 static unsigned short gGeneration = 0;
271 unsigned short FontCache::generation()
276 void FontCache::invalidate()
278 if (!invalidateFontCache) {
279 ASSERT(!gFontPlatformDataCache);
283 if (gFontPlatformDataCache) {
284 delete gFontPlatformDataCache;
285 gFontPlatformDataCache = new FontPlatformDataCache;
290 WillBeHeapVector<RefPtrWillBeMember<FontCacheClient> > clients;
291 size_t numClients = fontCacheClients().size();
292 clients.reserveInitialCapacity(numClients);
293 WillBeHeapHashSet<RawPtrWillBeWeakMember<FontCacheClient> >::iterator end = fontCacheClients().end();
294 for (WillBeHeapHashSet<RawPtrWillBeWeakMember<FontCacheClient> >::iterator it = fontCacheClients().begin(); it != end; ++it)
297 ASSERT(numClients == clients.size());
298 for (size_t i = 0; i < numClients; ++i)
299 clients[i]->fontCacheInvalidated();
304 } // namespace WebCore