Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / FontCache.cpp
1 /*
2  * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
17  *
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.
28  */
29
30 #include "config.h"
31 #include "platform/fonts/FontCache.h"
32
33 #include "platform/FontFamilyNames.h"
34
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"
52
53 using namespace WTF;
54
55 namespace blink {
56
57 #if !OS(WIN)
58 FontCache::FontCache()
59     : m_purgePreventCount(0)
60 {
61 }
62 #endif // !OS(WIN)
63
64 typedef HashMap<FontCacheKey, OwnPtr<FontPlatformData>, FontCacheKeyHash, FontCacheKeyTraits> FontPlatformDataCache;
65
66 static FontPlatformDataCache* gFontPlatformDataCache = 0;
67
68 #if OS(WIN)
69 bool FontCache::s_useDirectWrite = false;
70 IDWriteFactory* FontCache::s_directWriteFactory = 0;
71 bool FontCache::s_useSubpixelPositioning = false;
72 #endif // OS(WIN)
73
74 FontCache* FontCache::fontCache()
75 {
76     DEFINE_STATIC_LOCAL(FontCache, globalFontCache, ());
77     return &globalFontCache;
78 }
79
80 FontPlatformData* FontCache::getFontPlatformData(const FontDescription& fontDescription,
81     const FontFaceCreationParams& creationParams, bool checkingAlternateName)
82 {
83     if (!gFontPlatformDataCache) {
84         gFontPlatformDataCache = new FontPlatformDataCache;
85         platformInit();
86     }
87
88     FontCacheKey key = fontDescription.cacheKey(creationParams);
89     FontPlatformData* result = 0;
90     bool foundResult;
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));
95         foundResult = result;
96     } else {
97         result = it->value.get();
98         foundResult = true;
99     }
100
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);
108         }
109         if (result)
110             gFontPlatformDataCache->set(key, adoptPtr(new FontPlatformData(*result))); // Cache the result under the old name.
111     }
112
113     return result;
114 }
115
116 #if ENABLE(OPENTYPE_VERTICAL)
117 typedef HashMap<FontCache::FontFileKey, RefPtr<OpenTypeVerticalData>, IntHash<FontCache::FontFileKey>, UnsignedWithZeroKeyHashTraits<FontCache::FontFileKey> > FontVerticalDataCache;
118
119 FontVerticalDataCache& fontVerticalDataCacheInstance()
120 {
121     DEFINE_STATIC_LOCAL(FontVerticalDataCache, fontVerticalDataCache, ());
122     return fontVerticalDataCache;
123 }
124
125 PassRefPtr<OpenTypeVerticalData> FontCache::getVerticalData(const FontFileKey& key, const FontPlatformData& platformData)
126 {
127     FontVerticalDataCache& fontVerticalDataCache = fontVerticalDataCacheInstance();
128     FontVerticalDataCache::iterator result = fontVerticalDataCache.find(key);
129     if (result != fontVerticalDataCache.end())
130         return result.get()->value;
131
132     RefPtr<OpenTypeVerticalData> verticalData = OpenTypeVerticalData::create(platformData);
133     if (!verticalData->isOpenType())
134         verticalData.clear();
135     fontVerticalDataCache.set(key, verticalData);
136     return verticalData;
137 }
138 #endif
139
140 static FontDataCache* gFontDataCache = 0;
141
142 PassRefPtr<SimpleFontData> FontCache::getFontData(const FontDescription& fontDescription, const AtomicString& family, bool checkingAlternateName, ShouldRetain shouldRetain)
143 {
144     if (FontPlatformData* platformData = getFontPlatformData(fontDescription, FontFaceCreationParams(adjustFamilyNameToAvoidUnsupportedFonts(family)), checkingAlternateName))
145         return fontDataFromFontPlatformData(platformData, shouldRetain);
146
147     return nullptr;
148 }
149
150 PassRefPtr<SimpleFontData> FontCache::fontDataFromFontPlatformData(const FontPlatformData* platformData, ShouldRetain shouldRetain)
151 {
152     if (!gFontDataCache)
153         gFontDataCache = new FontDataCache;
154
155 #if ENABLE(ASSERT)
156     if (shouldRetain == DoNotRetain)
157         ASSERT(m_purgePreventCount);
158 #endif
159
160     return gFontDataCache->get(platformData, shouldRetain);
161 }
162
163 bool FontCache::isPlatformFontAvailable(const FontDescription& fontDescription, const AtomicString& family)
164 {
165     bool checkingAlternateName = true;
166     return getFontPlatformData(fontDescription, FontFaceCreationParams(adjustFamilyNameToAvoidUnsupportedFonts(family)), checkingAlternateName);
167 }
168
169 SimpleFontData* FontCache::getNonRetainedLastResortFallbackFont(const FontDescription& fontDescription)
170 {
171     return getLastResortFallbackFont(fontDescription, DoNotRetain).leakRef();
172 }
173
174 void FontCache::releaseFontData(const SimpleFontData* fontData)
175 {
176     ASSERT(gFontDataCache);
177
178     gFontDataCache->release(fontData);
179 }
180
181 static inline void purgePlatformFontDataCache()
182 {
183     if (!gFontPlatformDataCache)
184         return;
185
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);
192     }
193     gFontPlatformDataCache->removeAll(keysToRemove);
194 }
195
196 static inline void purgeFontVerticalDataCache()
197 {
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);
206         }
207
208         gFontDataCache->markAllVerticalData();
209
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);
215         }
216         fontVerticalDataCache.removeAll(keysToRemove);
217     }
218 #endif
219 }
220
221 void FontCache::purge(PurgeSeverity PurgeSeverity)
222 {
223     // We should never be forcing the purge while the FontCachePurgePreventer is in scope.
224     ASSERT(!m_purgePreventCount || PurgeSeverity == PurgeIfNeeded);
225     if (m_purgePreventCount)
226         return;
227
228     if (!gFontDataCache || !gFontDataCache->purge(PurgeSeverity))
229         return;
230
231     purgePlatformFontDataCache();
232     purgeFontVerticalDataCache();
233 }
234
235 static bool invalidateFontCache = false;
236
237 WillBeHeapHashSet<RawPtrWillBeWeakMember<FontCacheClient> >& fontCacheClients()
238 {
239     DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<WillBeHeapHashSet<RawPtrWillBeWeakMember<FontCacheClient> > >, clients, (adoptPtrWillBeNoop(new WillBeHeapHashSet<RawPtrWillBeWeakMember<FontCacheClient> >())));
240     invalidateFontCache = true;
241     return *clients;
242 }
243
244 void FontCache::addClient(FontCacheClient* client)
245 {
246     ASSERT(!fontCacheClients().contains(client));
247     fontCacheClients().add(client);
248 }
249
250 #if !ENABLE(OILPAN)
251 void FontCache::removeClient(FontCacheClient* client)
252 {
253     ASSERT(fontCacheClients().contains(client));
254     fontCacheClients().remove(client);
255 }
256 #endif
257
258 static unsigned short gGeneration = 0;
259
260 unsigned short FontCache::generation()
261 {
262     return gGeneration;
263 }
264
265 void FontCache::invalidate()
266 {
267     if (!invalidateFontCache) {
268         ASSERT(!gFontPlatformDataCache);
269         return;
270     }
271
272     if (gFontPlatformDataCache) {
273         delete gFontPlatformDataCache;
274         gFontPlatformDataCache = new FontPlatformDataCache;
275     }
276
277     gGeneration++;
278
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)
284         clients.append(*it);
285
286     ASSERT(numClients == clients.size());
287     for (size_t i = 0; i < numClients; ++i)
288         clients[i]->fontCacheInvalidated();
289
290     purge(ForcePurge);
291 }
292
293 } // namespace blink