Upstream version 7.36.149.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 "FontFamilyNames.h"
34
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"
51
52 using namespace WTF;
53
54 namespace WebCore {
55
56 #if !OS(WIN)
57 FontCache::FontCache()
58     : m_purgePreventCount(0)
59 {
60 }
61 #endif // !OS(WIN)
62
63 typedef HashMap<FontCacheKey, OwnPtr<FontPlatformData>, FontCacheKeyHash, FontCacheKeyTraits> FontPlatformDataCache;
64
65 static FontPlatformDataCache* gFontPlatformDataCache = 0;
66
67 #if OS(WIN)
68 bool FontCache::s_useDirectWrite = false;
69 IDWriteFactory* FontCache::s_directWriteFactory = 0;
70 bool FontCache::s_useSubpixelPositioning = false;
71 #endif // OS(WIN)
72
73 FontCache* FontCache::fontCache()
74 {
75     DEFINE_STATIC_LOCAL(FontCache, globalFontCache, ());
76     return &globalFontCache;
77 }
78
79 FontPlatformData* FontCache::getFontPlatformData(const FontDescription& fontDescription,
80     const AtomicString& passedFamilyName, bool checkingAlternateName)
81 {
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));
88 #else
89     const AtomicString& familyName = passedFamilyName;
90 #endif
91
92     if (!gFontPlatformDataCache) {
93         gFontPlatformDataCache = new FontPlatformDataCache;
94         platformInit();
95     }
96
97     FontCacheKey key = fontDescription.cacheKey(familyName);
98     FontPlatformData* result = 0;
99     bool foundResult;
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;
105     } else {
106         result = it->value.get();
107         foundResult = true;
108     }
109
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);
116         if (result)
117             gFontPlatformDataCache->set(key, adoptPtr(new FontPlatformData(*result))); // Cache the result under the old name.
118     }
119
120     return result;
121 }
122
123 #if ENABLE(OPENTYPE_VERTICAL)
124 typedef HashMap<FontCache::FontFileKey, RefPtr<OpenTypeVerticalData>, WTF::IntHash<FontCache::FontFileKey>, WTF::UnsignedWithZeroKeyHashTraits<FontCache::FontFileKey> > FontVerticalDataCache;
125
126 FontVerticalDataCache& fontVerticalDataCacheInstance()
127 {
128     DEFINE_STATIC_LOCAL(FontVerticalDataCache, fontVerticalDataCache, ());
129     return fontVerticalDataCache;
130 }
131
132 PassRefPtr<OpenTypeVerticalData> FontCache::getVerticalData(const FontFileKey& key, const FontPlatformData& platformData)
133 {
134     FontVerticalDataCache& fontVerticalDataCache = fontVerticalDataCacheInstance();
135     FontVerticalDataCache::iterator result = fontVerticalDataCache.find(key);
136     if (result != fontVerticalDataCache.end())
137         return result.get()->value;
138
139     RefPtr<OpenTypeVerticalData> verticalData = OpenTypeVerticalData::create(platformData);
140     if (!verticalData->isOpenType())
141         verticalData.clear();
142     fontVerticalDataCache.set(key, verticalData);
143     return verticalData;
144 }
145 #endif
146
147 static FontDataCache* gFontDataCache = 0;
148
149 PassRefPtr<SimpleFontData> FontCache::getFontData(const FontDescription& fontDescription, const AtomicString& family, bool checkingAlternateName, ShouldRetain shouldRetain)
150 {
151     if (FontPlatformData* platformData = getFontPlatformData(fontDescription, adjustFamilyNameToAvoidUnsupportedFonts(family), checkingAlternateName))
152         return fontDataFromFontPlatformData(platformData, shouldRetain);
153
154     return nullptr;
155 }
156
157 PassRefPtr<SimpleFontData> FontCache::fontDataFromFontPlatformData(const FontPlatformData* platformData, ShouldRetain shouldRetain)
158 {
159     if (!gFontDataCache)
160         gFontDataCache = new FontDataCache;
161
162 #if !ASSERT_DISABLED
163     if (shouldRetain == DoNotRetain)
164         ASSERT(m_purgePreventCount);
165 #endif
166
167     return gFontDataCache->get(platformData, shouldRetain);
168 }
169
170 bool FontCache::isPlatformFontAvailable(const FontDescription& fontDescription, const AtomicString& family)
171 {
172     bool checkingAlternateName = true;
173     return getFontPlatformData(fontDescription, family, checkingAlternateName);
174 }
175
176 SimpleFontData* FontCache::getNonRetainedLastResortFallbackFont(const FontDescription& fontDescription)
177 {
178     return getLastResortFallbackFont(fontDescription, DoNotRetain).leakRef();
179 }
180
181 void FontCache::releaseFontData(const SimpleFontData* fontData)
182 {
183     ASSERT(gFontDataCache);
184
185     gFontDataCache->release(fontData);
186 }
187
188 static inline void purgePlatformFontDataCache()
189 {
190     if (!gFontPlatformDataCache)
191         return;
192
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);
199     }
200     gFontPlatformDataCache->removeAll(keysToRemove);
201 }
202
203 static inline void purgeFontVerticalDataCache()
204 {
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);
213         }
214
215         gFontDataCache->markAllVerticalData();
216
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);
222         }
223         fontVerticalDataCache.removeAll(keysToRemove);
224     }
225 #endif
226 }
227
228 void FontCache::purge(PurgeSeverity PurgeSeverity)
229 {
230     // We should never be forcing the purge while the FontCachePurgePreventer is in scope.
231     ASSERT(!m_purgePreventCount || PurgeSeverity == PurgeIfNeeded);
232     if (m_purgePreventCount)
233         return;
234
235     if (!gFontDataCache || !gFontDataCache->purge(PurgeSeverity))
236         return;
237
238     purgePlatformFontDataCache();
239     purgeFontVerticalDataCache();
240 }
241
242 static bool invalidateFontCache = false;
243
244 WillBeHeapHashSet<RawPtrWillBeWeakMember<FontCacheClient> >& fontCacheClients()
245 {
246 #if ENABLE(OILPAN)
247     DEFINE_STATIC_LOCAL(Persistent<HeapHashSet<WeakMember<FontCacheClient> > >, clients, (new HeapHashSet<WeakMember<FontCacheClient> >()));
248 #else
249     DEFINE_STATIC_LOCAL(HashSet<RawPtr<FontCacheClient> >*, clients, (new HashSet<RawPtr<FontCacheClient> >()));
250 #endif
251     invalidateFontCache = true;
252     return *clients;
253 }
254
255 void FontCache::addClient(FontCacheClient* client)
256 {
257     ASSERT(!fontCacheClients().contains(client));
258     fontCacheClients().add(client);
259 }
260
261 #if !ENABLE(OILPAN)
262 void FontCache::removeClient(FontCacheClient* client)
263 {
264     ASSERT(fontCacheClients().contains(client));
265     fontCacheClients().remove(client);
266 }
267 #endif
268
269 static unsigned short gGeneration = 0;
270
271 unsigned short FontCache::generation()
272 {
273     return gGeneration;
274 }
275
276 void FontCache::invalidate()
277 {
278     if (!invalidateFontCache) {
279         ASSERT(!gFontPlatformDataCache);
280         return;
281     }
282
283     if (gFontPlatformDataCache) {
284         delete gFontPlatformDataCache;
285         gFontPlatformDataCache = new FontPlatformDataCache;
286     }
287
288     gGeneration++;
289
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)
295         clients.append(*it);
296
297     ASSERT(numClients == clients.size());
298     for (size_t i = 0; i < numClients; ++i)
299         clients[i]->fontCacheInvalidated();
300
301     purge(ForcePurge);
302 }
303
304 } // namespace WebCore