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 "platform/FontFamilyNames.h"
35 #include "platform/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/Vector.h"
50 #include "wtf/text/AtomicStringHash.h"
51 #include "wtf/text/StringHash.h"
58 FontCache::FontCache()
59 : m_purgePreventCount(0)
64 typedef HashMap<FontCacheKey, OwnPtr<FontPlatformData>, FontCacheKeyHash, FontCacheKeyTraits> FontPlatformDataCache;
66 static FontPlatformDataCache* gFontPlatformDataCache = 0;
69 bool FontCache::s_useDirectWrite = false;
70 IDWriteFactory* FontCache::s_directWriteFactory = 0;
71 bool FontCache::s_useSubpixelPositioning = false;
74 FontCache* FontCache::fontCache()
76 DEFINE_STATIC_LOCAL(FontCache, globalFontCache, ());
77 return &globalFontCache;
80 FontPlatformData* FontCache::getFontPlatformData(const FontDescription& fontDescription,
81 const FontFaceCreationParams& creationParams, bool checkingAlternateName)
83 if (!gFontPlatformDataCache) {
84 gFontPlatformDataCache = new FontPlatformDataCache;
88 FontCacheKey key = fontDescription.cacheKey(creationParams);
89 FontPlatformData* result = 0;
91 FontPlatformDataCache::iterator it = gFontPlatformDataCache->find(key);
92 if (it == gFontPlatformDataCache->end()) {
93 result = createFontPlatformData(fontDescription, creationParams, fontDescription.effectiveFontSize());
94 gFontPlatformDataCache->set(key, adoptPtr(result));
97 result = it->value.get();
101 if (!foundResult && !checkingAlternateName && creationParams.creationType() == CreateFontByFamily) {
102 // We were unable to find a font. We have a small set of fonts that we alias to other names,
103 // e.g., Arial/Helvetica, Courier/Courier New, etc. Try looking up the font under the aliased name.
104 const AtomicString& alternateName = alternateFamilyName(creationParams.family());
105 if (!alternateName.isEmpty()) {
106 FontFaceCreationParams createByAlternateFamily(alternateName);
107 result = getFontPlatformData(fontDescription, createByAlternateFamily, true);
110 gFontPlatformDataCache->set(key, adoptPtr(new FontPlatformData(*result))); // Cache the result under the old name.
116 #if ENABLE(OPENTYPE_VERTICAL)
117 typedef HashMap<FontCache::FontFileKey, RefPtr<OpenTypeVerticalData>, IntHash<FontCache::FontFileKey>, UnsignedWithZeroKeyHashTraits<FontCache::FontFileKey> > FontVerticalDataCache;
119 FontVerticalDataCache& fontVerticalDataCacheInstance()
121 DEFINE_STATIC_LOCAL(FontVerticalDataCache, fontVerticalDataCache, ());
122 return fontVerticalDataCache;
125 PassRefPtr<OpenTypeVerticalData> FontCache::getVerticalData(const FontFileKey& key, const FontPlatformData& platformData)
127 FontVerticalDataCache& fontVerticalDataCache = fontVerticalDataCacheInstance();
128 FontVerticalDataCache::iterator result = fontVerticalDataCache.find(key);
129 if (result != fontVerticalDataCache.end())
130 return result.get()->value;
132 RefPtr<OpenTypeVerticalData> verticalData = OpenTypeVerticalData::create(platformData);
133 if (!verticalData->isOpenType())
134 verticalData.clear();
135 fontVerticalDataCache.set(key, verticalData);
140 static FontDataCache* gFontDataCache = 0;
142 PassRefPtr<SimpleFontData> FontCache::getFontData(const FontDescription& fontDescription, const AtomicString& family, bool checkingAlternateName, ShouldRetain shouldRetain)
144 if (FontPlatformData* platformData = getFontPlatformData(fontDescription, FontFaceCreationParams(adjustFamilyNameToAvoidUnsupportedFonts(family)), checkingAlternateName))
145 return fontDataFromFontPlatformData(platformData, shouldRetain);
150 PassRefPtr<SimpleFontData> FontCache::fontDataFromFontPlatformData(const FontPlatformData* platformData, ShouldRetain shouldRetain)
153 gFontDataCache = new FontDataCache;
156 if (shouldRetain == DoNotRetain)
157 ASSERT(m_purgePreventCount);
160 return gFontDataCache->get(platformData, shouldRetain);
163 bool FontCache::isPlatformFontAvailable(const FontDescription& fontDescription, const AtomicString& family)
165 bool checkingAlternateName = true;
166 return getFontPlatformData(fontDescription, FontFaceCreationParams(adjustFamilyNameToAvoidUnsupportedFonts(family)), checkingAlternateName);
169 SimpleFontData* FontCache::getNonRetainedLastResortFallbackFont(const FontDescription& fontDescription)
171 return getLastResortFallbackFont(fontDescription, DoNotRetain).leakRef();
174 void FontCache::releaseFontData(const SimpleFontData* fontData)
176 ASSERT(gFontDataCache);
178 gFontDataCache->release(fontData);
181 static inline void purgePlatformFontDataCache()
183 if (!gFontPlatformDataCache)
186 Vector<FontCacheKey> keysToRemove;
187 keysToRemove.reserveInitialCapacity(gFontPlatformDataCache->size());
188 FontPlatformDataCache::iterator platformDataEnd = gFontPlatformDataCache->end();
189 for (FontPlatformDataCache::iterator platformData = gFontPlatformDataCache->begin(); platformData != platformDataEnd; ++platformData) {
190 if (platformData->value && !gFontDataCache->contains(platformData->value.get()))
191 keysToRemove.append(platformData->key);
193 gFontPlatformDataCache->removeAll(keysToRemove);
196 static inline void purgeFontVerticalDataCache()
198 #if ENABLE(OPENTYPE_VERTICAL)
199 FontVerticalDataCache& fontVerticalDataCache = fontVerticalDataCacheInstance();
200 if (!fontVerticalDataCache.isEmpty()) {
201 // Mark & sweep unused verticalData
202 FontVerticalDataCache::iterator verticalDataEnd = fontVerticalDataCache.end();
203 for (FontVerticalDataCache::iterator verticalData = fontVerticalDataCache.begin(); verticalData != verticalDataEnd; ++verticalData) {
204 if (verticalData->value)
205 verticalData->value->setInFontCache(false);
208 gFontDataCache->markAllVerticalData();
210 Vector<FontCache::FontFileKey> keysToRemove;
211 keysToRemove.reserveInitialCapacity(fontVerticalDataCache.size());
212 for (FontVerticalDataCache::iterator verticalData = fontVerticalDataCache.begin(); verticalData != verticalDataEnd; ++verticalData) {
213 if (!verticalData->value || !verticalData->value->inFontCache())
214 keysToRemove.append(verticalData->key);
216 fontVerticalDataCache.removeAll(keysToRemove);
221 void FontCache::purge(PurgeSeverity PurgeSeverity)
223 // We should never be forcing the purge while the FontCachePurgePreventer is in scope.
224 ASSERT(!m_purgePreventCount || PurgeSeverity == PurgeIfNeeded);
225 if (m_purgePreventCount)
228 if (!gFontDataCache || !gFontDataCache->purge(PurgeSeverity))
231 purgePlatformFontDataCache();
232 purgeFontVerticalDataCache();
235 static bool invalidateFontCache = false;
237 WillBeHeapHashSet<RawPtrWillBeWeakMember<FontCacheClient> >& fontCacheClients()
239 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<WillBeHeapHashSet<RawPtrWillBeWeakMember<FontCacheClient> > >, clients, (adoptPtrWillBeNoop(new WillBeHeapHashSet<RawPtrWillBeWeakMember<FontCacheClient> >())));
240 invalidateFontCache = true;
244 void FontCache::addClient(FontCacheClient* client)
246 ASSERT(!fontCacheClients().contains(client));
247 fontCacheClients().add(client);
251 void FontCache::removeClient(FontCacheClient* client)
253 ASSERT(fontCacheClients().contains(client));
254 fontCacheClients().remove(client);
258 static unsigned short gGeneration = 0;
260 unsigned short FontCache::generation()
265 void FontCache::invalidate()
267 if (!invalidateFontCache) {
268 ASSERT(!gFontPlatformDataCache);
272 if (gFontPlatformDataCache) {
273 delete gFontPlatformDataCache;
274 gFontPlatformDataCache = new FontPlatformDataCache;
279 WillBeHeapVector<RefPtrWillBeMember<FontCacheClient> > clients;
280 size_t numClients = fontCacheClients().size();
281 clients.reserveInitialCapacity(numClients);
282 WillBeHeapHashSet<RawPtrWillBeWeakMember<FontCacheClient> >::iterator end = fontCacheClients().end();
283 for (WillBeHeapHashSet<RawPtrWillBeWeakMember<FontCacheClient> >::iterator it = fontCacheClients().begin(); it != end; ++it)
286 ASSERT(numClients == clients.size());
287 for (size_t i = 0; i < numClients; ++i)
288 clients[i]->fontCacheInvalidated();