OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. |
3 * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com> | 3 * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com> |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * | 8 * |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 namespace blink { | 60 namespace blink { |
61 | 61 |
62 #if !OS(WIN) && !OS(LINUX) | 62 #if !OS(WIN) && !OS(LINUX) |
63 FontCache::FontCache() | 63 FontCache::FontCache() |
64 : m_purgePreventCount(0) | 64 : m_purgePreventCount(0) |
65 , m_fontManager(nullptr) | 65 , m_fontManager(nullptr) |
66 { | 66 { |
67 } | 67 } |
68 #endif // !OS(WIN) && !OS(LINUX) | 68 #endif // !OS(WIN) && !OS(LINUX) |
69 | 69 |
70 typedef HashMap<FontCacheKey, OwnPtr<FontPlatformData>, FontCacheKeyHash, FontCa cheKeyTraits> FontPlatformDataCache; | 70 typedef HashMap<unsigned, OwnPtr<FontPlatformData>> SizedFontPlatformDataSet; |
eae
2016/05/17 14:42:55
Long term we should probably try to divorce the si
| |
71 typedef HashMap<FontCacheKey, SizedFontPlatformDataSet, FontCacheKeyHash, FontCa cheKeyTraits> FontPlatformDataCache; | |
71 typedef HashMap<FallbackListCompositeKey, OwnPtr<ShapeCache>, FallbackListCompos iteKeyHash, FallbackListCompositeKeyTraits> FallbackListShaperCache; | 72 typedef HashMap<FallbackListCompositeKey, OwnPtr<ShapeCache>, FallbackListCompos iteKeyHash, FallbackListCompositeKeyTraits> FallbackListShaperCache; |
72 | 73 |
73 static FontPlatformDataCache* gFontPlatformDataCache = nullptr; | 74 static FontPlatformDataCache* gFontPlatformDataCache = nullptr; |
74 static FallbackListShaperCache* gFallbackListShaperCache = nullptr; | 75 static FallbackListShaperCache* gFallbackListShaperCache = nullptr; |
75 | 76 |
76 SkFontMgr* FontCache::s_fontManager = nullptr; | 77 SkFontMgr* FontCache::s_fontManager = nullptr; |
77 | 78 |
78 #if OS(WIN) | 79 #if OS(WIN) |
79 bool FontCache::s_antialiasedTextEnabled = false; | 80 bool FontCache::s_antialiasedTextEnabled = false; |
80 bool FontCache::s_lcdTextEnabled = false; | 81 bool FontCache::s_lcdTextEnabled = false; |
81 float FontCache::s_deviceScaleFactor = 1.0; | 82 float FontCache::s_deviceScaleFactor = 1.0; |
82 bool FontCache::s_useSkiaFontFallback = false; | 83 bool FontCache::s_useSkiaFontFallback = false; |
83 #endif // OS(WIN) | 84 #endif // OS(WIN) |
84 | 85 |
85 FontCache* FontCache::fontCache() | 86 FontCache* FontCache::fontCache() |
86 { | 87 { |
87 DEFINE_STATIC_LOCAL(FontCache, globalFontCache, ()); | 88 DEFINE_STATIC_LOCAL(FontCache, globalFontCache, ()); |
88 return &globalFontCache; | 89 return &globalFontCache; |
89 } | 90 } |
90 | 91 |
91 FontPlatformData* FontCache::getFontPlatformData(const FontDescription& fontDesc ription, | 92 FontPlatformData* FontCache::getFontPlatformData(const FontDescription& fontDesc ription, |
92 const FontFaceCreationParams& creationParams, bool checkingAlternateName) | 93 const FontFaceCreationParams& creationParams, bool checkingAlternateName) |
93 { | 94 { |
94 if (!gFontPlatformDataCache) { | 95 if (!gFontPlatformDataCache) { |
95 gFontPlatformDataCache = new FontPlatformDataCache; | 96 gFontPlatformDataCache = new FontPlatformDataCache; |
96 platformInit(); | 97 platformInit(); |
97 } | 98 } |
98 | 99 |
100 float size = fontDescription.effectiveFontSize(); | |
99 FontCacheKey key = fontDescription.cacheKey(creationParams); | 101 FontCacheKey key = fontDescription.cacheKey(creationParams); |
102 | |
103 // Remove the font size from the cache key, and handle the font size separat ely in the inner | |
104 // HashMap. So that different size of FontPlatformData can share underlying SkTypeface. | |
105 key.clearFontSize(); | |
106 | |
100 FontPlatformData* result; | 107 FontPlatformData* result; |
101 bool foundResult; | 108 bool foundResult; |
109 | |
102 { | 110 { |
103 // addResult's scope must end before we recurse for alternate family nam es below, | 111 // addResult's scope must end before we recurse for alternate family nam es below, |
104 // to avoid trigering its dtor hash-changed asserts. | 112 // to avoid trigering its dtor hash-changed asserts. |
105 auto addResult = gFontPlatformDataCache->add(key, nullptr); | 113 SizedFontPlatformDataSet* sizedFonts = &gFontPlatformDataCache->add(key, SizedFontPlatformDataSet()).storedValue->value; |
106 if (addResult.isNewEntry) | 114 bool wasEmpty = sizedFonts->isEmpty(); |
107 addResult.storedValue->value = createFontPlatformData(fontDescriptio n, creationParams, fontDescription.effectiveFontSize()); | 115 FontPlatformData* anotherSize = wasEmpty ? nullptr : sizedFonts->begin() ->value.get(); |
116 auto addResult = sizedFonts->add(size ? size * FontCacheKey::precisionMu ltiplier() : -2, nullptr); | |
eae
2016/05/17 14:42:55
-2?
tzik
2016/05/17 14:56:19
Added a comment here. 0 and -1 are reserved for Ha
| |
117 OwnPtr<FontPlatformData>* found = &addResult.storedValue->value; | |
118 if (addResult.isNewEntry) { | |
119 if (wasEmpty) | |
120 *found = createFontPlatformData(fontDescription, creationParams, size); | |
121 else | |
eae
2016/05/17 14:42:55
How about using sizedFonts->begin()->value.get() d
tzik
2016/05/17 14:56:19
Hmm, since we are adding new entry into sizedFonts
| |
122 *found = scaleFontPlatformData(anotherSize, fontDescription, cre ationParams, size); | |
123 } | |
108 | 124 |
109 result = addResult.storedValue->value.get(); | 125 result = found->get(); |
110 foundResult = result || !addResult.isNewEntry; | 126 foundResult = result || !addResult.isNewEntry; |
111 } | 127 } |
112 | 128 |
113 if (!foundResult && !checkingAlternateName && creationParams.creationType() == CreateFontByFamily) { | 129 if (!foundResult && !checkingAlternateName && creationParams.creationType() == CreateFontByFamily) { |
114 // We were unable to find a font. We have a small set of fonts that we a lias to other names, | 130 // We were unable to find a font. We have a small set of fonts that we a lias to other names, |
115 // e.g., Arial/Helvetica, Courier/Courier New, etc. Try looking up the f ont under the aliased name. | 131 // e.g., Arial/Helvetica, Courier/Courier New, etc. Try looking up the f ont under the aliased name. |
116 const AtomicString& alternateName = alternateFamilyName(creationParams.f amily()); | 132 const AtomicString& alternateName = alternateFamilyName(creationParams.f amily()); |
117 if (!alternateName.isEmpty()) { | 133 if (!alternateName.isEmpty()) { |
118 FontFaceCreationParams createByAlternateFamily(alternateName); | 134 FontFaceCreationParams createByAlternateFamily(alternateName); |
119 result = getFontPlatformData(fontDescription, createByAlternateFamil y, true); | 135 result = getFontPlatformData(fontDescription, createByAlternateFamil y, true); |
120 } | 136 } |
121 if (result) | 137 if (result) { |
122 gFontPlatformDataCache->set(key, adoptPtr(new FontPlatformData(*resu lt))); // Cache the result under the old name. | 138 // Cache the result under the old name. |
139 auto adding = &gFontPlatformDataCache->add(key, SizedFontPlatformDat aSet()).storedValue->value; | |
140 adding->set(size, adoptPtr(new FontPlatformData(*result))); | |
141 } | |
123 } | 142 } |
124 | 143 |
125 return result; | 144 return result; |
126 } | 145 } |
127 | 146 |
147 PassOwnPtr<FontPlatformData> FontCache::scaleFontPlatformData(const FontPlatform Data* fontPlatformData, const FontDescription& fontDescription, const FontFaceCr eationParams& creationParams, float fontSize) | |
148 { | |
149 #if OS(MACOSX) | |
150 return createFontPlatformData(fontDescription, creationParams, fontSize); | |
151 #else | |
152 if (!fontPlatformData) | |
153 return nullptr; | |
eae
2016/05/17 14:42:55
When would this be null?
tzik
2016/05/17 14:56:19
This can be null if the first createFontPlatformDa
| |
154 return adoptPtr(new FontPlatformData(*fontPlatformData, fontSize)); | |
155 #endif | |
156 } | |
157 | |
128 ShapeCache* FontCache::getShapeCache(const FallbackListCompositeKey& key) | 158 ShapeCache* FontCache::getShapeCache(const FallbackListCompositeKey& key) |
129 { | 159 { |
130 if (!gFallbackListShaperCache) | 160 if (!gFallbackListShaperCache) |
131 gFallbackListShaperCache = new FallbackListShaperCache; | 161 gFallbackListShaperCache = new FallbackListShaperCache; |
132 | 162 |
133 FallbackListShaperCache::iterator it = gFallbackListShaperCache->find(key); | 163 FallbackListShaperCache::iterator it = gFallbackListShaperCache->find(key); |
134 ShapeCache* result = nullptr; | 164 ShapeCache* result = nullptr; |
135 if (it == gFallbackListShaperCache->end()) { | 165 if (it == gFallbackListShaperCache->end()) { |
136 result = new ShapeCache(); | 166 result = new ShapeCache(); |
137 gFallbackListShaperCache->set(key, adoptPtr(result)); | 167 gFallbackListShaperCache->set(key, adoptPtr(result)); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 gFontDataCache->release(fontData); | 249 gFontDataCache->release(fontData); |
220 } | 250 } |
221 | 251 |
222 static inline void purgePlatformFontDataCache() | 252 static inline void purgePlatformFontDataCache() |
223 { | 253 { |
224 if (!gFontPlatformDataCache) | 254 if (!gFontPlatformDataCache) |
225 return; | 255 return; |
226 | 256 |
227 Vector<FontCacheKey> keysToRemove; | 257 Vector<FontCacheKey> keysToRemove; |
228 keysToRemove.reserveInitialCapacity(gFontPlatformDataCache->size()); | 258 keysToRemove.reserveInitialCapacity(gFontPlatformDataCache->size()); |
229 FontPlatformDataCache::iterator platformDataEnd = gFontPlatformDataCache->en d(); | 259 for (auto& sizedFonts : *gFontPlatformDataCache) { |
230 for (FontPlatformDataCache::iterator platformData = gFontPlatformDataCache-> begin(); platformData != platformDataEnd; ++platformData) { | 260 Vector<float> sizesToRemove; |
231 if (platformData->value && !gFontDataCache->contains(platformData->value .get())) | 261 sizesToRemove.reserveInitialCapacity(sizedFonts.value.size()); |
232 keysToRemove.append(platformData->key); | 262 for (const auto& platformData : sizedFonts.value) { |
263 if (platformData.value && !gFontDataCache->contains(platformData.val ue.get())) | |
264 sizesToRemove.append(platformData.key); | |
265 } | |
266 sizedFonts.value.removeAll(sizesToRemove); | |
267 if (sizedFonts.value.isEmpty()) | |
268 keysToRemove.append(sizedFonts.key); | |
233 } | 269 } |
234 gFontPlatformDataCache->removeAll(keysToRemove); | 270 gFontPlatformDataCache->removeAll(keysToRemove); |
235 } | 271 } |
236 | 272 |
237 static inline void purgeFontVerticalDataCache() | 273 static inline void purgeFontVerticalDataCache() |
238 { | 274 { |
239 FontVerticalDataCache& fontVerticalDataCache = fontVerticalDataCacheInstance (); | 275 FontVerticalDataCache& fontVerticalDataCache = fontVerticalDataCacheInstance (); |
240 if (!fontVerticalDataCache.isEmpty()) { | 276 if (!fontVerticalDataCache.isEmpty()) { |
241 // Mark & sweep unused verticalData | 277 // Mark & sweep unused verticalData |
242 FontVerticalDataCache::iterator verticalDataEnd = fontVerticalDataCache. end(); | 278 FontVerticalDataCache::iterator verticalDataEnd = fontVerticalDataCache. end(); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
384 case USCRIPT_SIMPLIFIED_HAN: | 420 case USCRIPT_SIMPLIFIED_HAN: |
385 return "zh-Hans"; | 421 return "zh-Hans"; |
386 case USCRIPT_TRADITIONAL_HAN: | 422 case USCRIPT_TRADITIONAL_HAN: |
387 return "zh-Hant"; | 423 return "zh-Hant"; |
388 default: | 424 default: |
389 return locale.ascii(); | 425 return locale.ascii(); |
390 } | 426 } |
391 } | 427 } |
392 | 428 |
393 } // namespace blink | 429 } // namespace blink |
OLD | NEW |