Remove the Timer parameters from timer callbacks
[WebKit-https.git] / Source / WebCore / css / CSSFontSelector.cpp
1 /*
2  * Copyright (C) 2007, 2008, 2011, 2013 Apple Inc. All rights reserved.
3  *           (C) 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
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  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "CSSFontSelector.h"
29
30 #include "CachedFont.h"
31 #include "CSSFontFace.h"
32 #include "CSSFontFaceRule.h"
33 #include "CSSFontFaceSource.h"
34 #include "CSSFontFaceSrcValue.h"
35 #include "CSSPrimitiveValue.h"
36 #include "CSSPropertyNames.h"
37 #include "CSSSegmentedFontFace.h"
38 #include "CSSUnicodeRangeValue.h"
39 #include "CSSValueKeywords.h"
40 #include "CSSValueList.h"
41 #include "CachedResourceLoader.h"
42 #include "Document.h"
43 #include "FontCache.h"
44 #include "Frame.h"
45 #include "FrameLoader.h"
46 #include "SVGFontFaceElement.h"
47 #include "SVGNames.h"
48 #include "Settings.h"
49 #include "SimpleFontData.h"
50 #include "StyleProperties.h"
51 #include "StyleResolver.h"
52 #include "StyleRule.h"
53 #include "WebKitFontFamilyNames.h"
54 #include <wtf/Ref.h>
55 #include <wtf/text/AtomicString.h>
56
57 namespace WebCore {
58
59 static unsigned fontSelectorId;
60
61 CSSFontSelector::CSSFontSelector(Document* document)
62     : m_document(document)
63     , m_beginLoadingTimer(*this, &CSSFontSelector::beginLoadTimerFired)
64     , m_uniqueId(++fontSelectorId)
65     , m_version(0)
66     
67 {
68     // FIXME: An old comment used to say there was no need to hold a reference to m_document
69     // because "we are guaranteed to be destroyed before the document". But there does not
70     // seem to be any such guarantee.
71
72     ASSERT(m_document);
73     fontCache().addClient(this);
74 }
75
76 CSSFontSelector::~CSSFontSelector()
77 {
78     clearDocument();
79     fontCache().removeClient(this);
80 }
81
82 bool CSSFontSelector::isEmpty() const
83 {
84     return m_fonts.isEmpty();
85 }
86
87 void CSSFontSelector::addFontFaceRule(const StyleRuleFontFace* fontFaceRule)
88 {
89     // Obtain the font-family property and the src property.  Both must be defined.
90     const StyleProperties& style = fontFaceRule->properties();
91     RefPtr<CSSValue> fontFamily = style.getPropertyCSSValue(CSSPropertyFontFamily);
92     RefPtr<CSSValue> src = style.getPropertyCSSValue(CSSPropertySrc);
93     RefPtr<CSSValue> unicodeRange = style.getPropertyCSSValue(CSSPropertyUnicodeRange);
94     if (!is<CSSValueList>(fontFamily.get()) || !is<CSSValueList>(src.get()) || (unicodeRange && !is<CSSValueList>(*unicodeRange)))
95         return;
96
97     CSSValueList& familyList = downcast<CSSValueList>(*fontFamily);
98     if (!familyList.length())
99         return;
100
101     CSSValueList& srcList = downcast<CSSValueList>(*src);
102     if (!srcList.length())
103         return;
104
105     CSSValueList* rangeList = downcast<CSSValueList>(unicodeRange.get());
106
107     unsigned traitsMask = 0;
108
109     if (RefPtr<CSSValue> fontStyle = style.getPropertyCSSValue(CSSPropertyFontStyle)) {
110         if (!is<CSSPrimitiveValue>(*fontStyle))
111             return;
112
113         switch (downcast<CSSPrimitiveValue>(*fontStyle).getValueID()) {
114         case CSSValueNormal:
115             traitsMask |= FontStyleNormalMask;
116             break;
117         case CSSValueItalic:
118         case CSSValueOblique:
119             traitsMask |= FontStyleItalicMask;
120             break;
121         default:
122             break;
123         }
124     } else
125         traitsMask |= FontStyleNormalMask;
126
127     if (RefPtr<CSSValue> fontWeight = style.getPropertyCSSValue(CSSPropertyFontWeight)) {
128         if (!is<CSSPrimitiveValue>(*fontWeight))
129             return;
130
131         switch (downcast<CSSPrimitiveValue>(*fontWeight).getValueID()) {
132         case CSSValueBold:
133         case CSSValue700:
134             traitsMask |= FontWeight700Mask;
135             break;
136         case CSSValueNormal:
137         case CSSValue400:
138             traitsMask |= FontWeight400Mask;
139             break;
140         case CSSValue900:
141             traitsMask |= FontWeight900Mask;
142             break;
143         case CSSValue800:
144             traitsMask |= FontWeight800Mask;
145             break;
146         case CSSValue600:
147             traitsMask |= FontWeight600Mask;
148             break;
149         case CSSValue500:
150             traitsMask |= FontWeight500Mask;
151             break;
152         case CSSValue300:
153             traitsMask |= FontWeight300Mask;
154             break;
155         case CSSValue200:
156             traitsMask |= FontWeight200Mask;
157             break;
158         case CSSValue100:
159             traitsMask |= FontWeight100Mask;
160             break;
161         default:
162             break;
163         }
164     } else
165         traitsMask |= FontWeight400Mask;
166
167     if (RefPtr<CSSValue> fontVariant = style.getPropertyCSSValue(CSSPropertyFontVariant)) {
168         // font-variant descriptor can be a value list.
169         if (fontVariant->isPrimitiveValue()) {
170             RefPtr<CSSValueList> list = CSSValueList::createCommaSeparated();
171             list->append(fontVariant.releaseNonNull());
172             fontVariant = list.releaseNonNull();
173         } else if (!is<CSSValueList>(*fontVariant))
174             return;
175
176         CSSValueList& variantList = downcast<CSSValueList>(*fontVariant);
177         unsigned numVariants = variantList.length();
178         if (!numVariants)
179             return;
180
181         for (unsigned i = 0; i < numVariants; ++i) {
182             switch (downcast<CSSPrimitiveValue>(variantList.itemWithoutBoundsCheck(i))->getValueID()) {
183                 case CSSValueNormal:
184                     traitsMask |= FontVariantNormalMask;
185                     break;
186                 case CSSValueSmallCaps:
187                     traitsMask |= FontVariantSmallCapsMask;
188                     break;
189                 default:
190                     break;
191             }
192         }
193     } else
194         traitsMask |= FontVariantMask;
195
196     // Each item in the src property's list is a single CSSFontFaceSource. Put them all into a CSSFontFace.
197     RefPtr<CSSFontFace> fontFace;
198
199     int srcLength = srcList.length();
200
201     bool foundSVGFont = false;
202
203     for (int i = 0; i < srcLength; i++) {
204         // An item in the list either specifies a string (local font name) or a URL (remote font to download).
205         CSSFontFaceSrcValue& item = downcast<CSSFontFaceSrcValue>(*srcList.itemWithoutBoundsCheck(i));
206         std::unique_ptr<CSSFontFaceSource> source;
207
208 #if ENABLE(SVG_FONTS)
209         foundSVGFont = item.isSVGFontFaceSrc() || item.svgFontFaceElement();
210 #endif
211         if (!item.isLocal()) {
212             Settings* settings = m_document ? m_document->frame() ? &m_document->frame()->settings() : 0 : 0;
213             bool allowDownloading = foundSVGFont || (settings && settings->downloadableBinaryFontsEnabled());
214             if (allowDownloading && item.isSupportedFormat() && m_document) {
215                 CachedFont* cachedFont = item.cachedFont(m_document, foundSVGFont);
216                 if (cachedFont) {
217                     source = std::make_unique<CSSFontFaceSource>(item.resource(), cachedFont);
218 #if ENABLE(SVG_FONTS)
219                     if (foundSVGFont)
220                         source->setHasExternalSVGFont();
221 #endif
222                 }
223             }
224         } else {
225             source = std::make_unique<CSSFontFaceSource>(item.resource());
226         }
227
228         if (!fontFace) {
229             RefPtr<CSSFontFaceRule> rule;
230 #if ENABLE(FONT_LOAD_EVENTS)
231             // FIXME: https://bugs.webkit.org/show_bug.cgi?id=112116 - This CSSFontFaceRule has no parent.
232             if (RuntimeEnabledFeatures::sharedFeatures().fontLoadEventsEnabled())
233                 rule = static_pointer_cast<CSSFontFaceRule>(fontFaceRule->createCSSOMWrapper());
234 #endif
235             fontFace = CSSFontFace::create(static_cast<FontTraitsMask>(traitsMask), rule);
236         }
237
238         if (source) {
239 #if ENABLE(SVG_FONTS)
240             source->setSVGFontFaceElement(item.svgFontFaceElement());
241 #endif
242             fontFace->addSource(WTF::move(source));
243         }
244     }
245
246     ASSERT(fontFace);
247
248     if (fontFace && !fontFace->isValid())
249         return;
250
251     if (rangeList) {
252         unsigned numRanges = rangeList->length();
253         for (unsigned i = 0; i < numRanges; i++) {
254             CSSUnicodeRangeValue& range = downcast<CSSUnicodeRangeValue>(*rangeList->itemWithoutBoundsCheck(i));
255             fontFace->addRange(range.from(), range.to());
256         }
257     }
258
259     // Hash under every single family name.
260     int familyLength = familyList.length();
261     for (int i = 0; i < familyLength; i++) {
262         CSSPrimitiveValue* item = downcast<CSSPrimitiveValue>(familyList.itemWithoutBoundsCheck(i));
263         String familyName;
264         if (item->isString()) {
265             familyName = item->getStringValue();
266         } else if (item->isValueID()) {
267             // We need to use the raw text for all the generic family types, since @font-face is a way of actually
268             // defining what font to use for those types.
269             switch (item->getValueID()) {
270                 case CSSValueSerif:
271                     familyName = serifFamily;
272                     break;
273                 case CSSValueSansSerif:
274                     familyName = sansSerifFamily;
275                     break;
276                 case CSSValueCursive:
277                     familyName = cursiveFamily;
278                     break;
279                 case CSSValueFantasy:
280                     familyName = fantasyFamily;
281                     break;
282                 case CSSValueMonospace:
283                     familyName = monospaceFamily;
284                     break;
285                 case CSSValueWebkitPictograph:
286                     familyName = pictographFamily;
287                     break;
288                 default:
289                     break;
290             }
291         }
292
293         if (familyName.isEmpty())
294             continue;
295
296         std::unique_ptr<Vector<RefPtr<CSSFontFace>>>& familyFontFaces = m_fontFaces.add(familyName, nullptr).iterator->value;
297         if (!familyFontFaces) {
298             familyFontFaces = std::make_unique<Vector<RefPtr<CSSFontFace>>>();
299
300             ASSERT(!m_locallyInstalledFontFaces.contains(familyName));
301
302             Vector<unsigned> locallyInstalledFontsTraitsMasks;
303             fontCache().getTraitsInFamily(familyName, locallyInstalledFontsTraitsMasks);
304             if (unsigned numLocallyInstalledFaces = locallyInstalledFontsTraitsMasks.size()) {
305                 auto familyLocallyInstalledFaces = std::make_unique<Vector<RefPtr<CSSFontFace>>>();
306
307                 for (unsigned i = 0; i < numLocallyInstalledFaces; ++i) {
308                     RefPtr<CSSFontFace> locallyInstalledFontFace = CSSFontFace::create(static_cast<FontTraitsMask>(locallyInstalledFontsTraitsMasks[i]), 0, true);
309                     locallyInstalledFontFace->addSource(std::make_unique<CSSFontFaceSource>(familyName));
310                     ASSERT(locallyInstalledFontFace->isValid());
311                     familyLocallyInstalledFaces->append(locallyInstalledFontFace);
312                 }
313
314                 m_locallyInstalledFontFaces.set(familyName, WTF::move(familyLocallyInstalledFaces));
315             }
316         }
317
318         familyFontFaces->append(fontFace);
319         
320         ++m_version;
321     }
322 }
323
324 void CSSFontSelector::registerForInvalidationCallbacks(FontSelectorClient* client)
325 {
326     m_clients.add(client);
327 }
328
329 void CSSFontSelector::unregisterForInvalidationCallbacks(FontSelectorClient* client)
330 {
331     m_clients.remove(client);
332 }
333
334 void CSSFontSelector::dispatchInvalidationCallbacks()
335 {
336     ++m_version;
337
338     Vector<FontSelectorClient*> clients;
339     copyToVector(m_clients, clients);
340     for (size_t i = 0; i < clients.size(); ++i)
341         clients[i]->fontsNeedUpdate(this);
342
343     // FIXME: Make Document a FontSelectorClient so that it can simply register for invalidation callbacks.
344     if (!m_document)
345         return;
346     if (StyleResolver* styleResolver = m_document->styleResolverIfExists())
347         styleResolver->invalidateMatchedPropertiesCache();
348     if (m_document->inPageCache() || !m_document->renderView())
349         return;
350     m_document->scheduleForcedStyleRecalc();
351 }
352
353 void CSSFontSelector::fontLoaded()
354 {
355     dispatchInvalidationCallbacks();
356 }
357
358 void CSSFontSelector::fontCacheInvalidated()
359 {
360     dispatchInvalidationCallbacks();
361 }
362
363 static PassRefPtr<SimpleFontData> fontDataForGenericFamily(Document* document, const FontDescription& fontDescription, const AtomicString& familyName)
364 {
365     if (!document || !document->frame())
366         return 0;
367
368     const Settings& settings = document->frame()->settings();
369
370     AtomicString genericFamily;
371     UScriptCode script = fontDescription.script();
372
373     if (familyName == serifFamily)
374         genericFamily = settings.serifFontFamily(script);
375     else if (familyName == sansSerifFamily)
376         genericFamily = settings.sansSerifFontFamily(script);
377     else if (familyName == cursiveFamily)
378         genericFamily = settings.cursiveFontFamily(script);
379     else if (familyName == fantasyFamily)
380         genericFamily = settings.fantasyFontFamily(script);
381     else if (familyName == monospaceFamily)
382         genericFamily = settings.fixedFontFamily(script);
383     else if (familyName == pictographFamily)
384         genericFamily = settings.pictographFontFamily(script);
385     else if (familyName == standardFamily)
386         genericFamily = settings.standardFontFamily(script);
387
388     if (!genericFamily.isEmpty())
389         return fontCache().getCachedFontData(fontDescription, genericFamily);
390
391     return nullptr;
392 }
393
394 static FontTraitsMask desiredTraitsMaskForComparison;
395
396 static inline bool compareFontFaces(CSSFontFace* first, CSSFontFace* second)
397 {
398     FontTraitsMask firstTraitsMask = first->traitsMask();
399     FontTraitsMask secondTraitsMask = second->traitsMask();
400
401     bool firstHasDesiredVariant = firstTraitsMask & desiredTraitsMaskForComparison & FontVariantMask;
402     bool secondHasDesiredVariant = secondTraitsMask & desiredTraitsMaskForComparison & FontVariantMask;
403
404     if (firstHasDesiredVariant != secondHasDesiredVariant)
405         return firstHasDesiredVariant;
406
407     // We need to check font-variant css property for CSS2.1 compatibility.
408     if ((desiredTraitsMaskForComparison & FontVariantSmallCapsMask) && !first->isLocalFallback() && !second->isLocalFallback()) {
409         // Prefer a font that has indicated that it can only support small-caps to a font that claims to support
410         // all variants.  The specialized font is more likely to be true small-caps and not require synthesis.
411         bool firstRequiresSmallCaps = (firstTraitsMask & FontVariantSmallCapsMask) && !(firstTraitsMask & FontVariantNormalMask);
412         bool secondRequiresSmallCaps = (secondTraitsMask & FontVariantSmallCapsMask) && !(secondTraitsMask & FontVariantNormalMask);
413         if (firstRequiresSmallCaps != secondRequiresSmallCaps)
414             return firstRequiresSmallCaps;
415     }
416
417     bool firstHasDesiredStyle = firstTraitsMask & desiredTraitsMaskForComparison & FontStyleMask;
418     bool secondHasDesiredStyle = secondTraitsMask & desiredTraitsMaskForComparison & FontStyleMask;
419
420     if (firstHasDesiredStyle != secondHasDesiredStyle)
421         return firstHasDesiredStyle;
422
423     if ((desiredTraitsMaskForComparison & FontStyleItalicMask) && !first->isLocalFallback() && !second->isLocalFallback()) {
424         // Prefer a font that has indicated that it can only support italics to a font that claims to support
425         // all styles.  The specialized font is more likely to be the one the author wants used.
426         bool firstRequiresItalics = (firstTraitsMask & FontStyleItalicMask) && !(firstTraitsMask & FontStyleNormalMask);
427         bool secondRequiresItalics = (secondTraitsMask & FontStyleItalicMask) && !(secondTraitsMask & FontStyleNormalMask);
428         if (firstRequiresItalics != secondRequiresItalics)
429             return firstRequiresItalics;
430     }
431
432     if (secondTraitsMask & desiredTraitsMaskForComparison & FontWeightMask)
433         return false;
434     if (firstTraitsMask & desiredTraitsMaskForComparison & FontWeightMask)
435         return true;
436
437     // http://www.w3.org/TR/2011/WD-css3-fonts-20111004/#font-matching-algorithm says :
438     //   - If the desired weight is less than 400, weights below the desired weight are checked in descending order followed by weights above the desired weight in ascending order until a match is found.
439     //   - If the desired weight is greater than 500, weights above the desired weight are checked in ascending order followed by weights below the desired weight in descending order until a match is found.
440     //   - If the desired weight is 400, 500 is checked first and then the rule for desired weights less than 400 is used.
441     //   - If the desired weight is 500, 400 is checked first and then the rule for desired weights less than 400 is used.
442
443     static const unsigned fallbackRuleSets = 9;
444     static const unsigned rulesPerSet = 8;
445     static const FontTraitsMask weightFallbackRuleSets[fallbackRuleSets][rulesPerSet] = {
446         { FontWeight200Mask, FontWeight300Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
447         { FontWeight100Mask, FontWeight300Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
448         { FontWeight200Mask, FontWeight100Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
449         { FontWeight500Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
450         { FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
451         { FontWeight700Mask, FontWeight800Mask, FontWeight900Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },
452         { FontWeight800Mask, FontWeight900Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },
453         { FontWeight900Mask, FontWeight700Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },
454         { FontWeight800Mask, FontWeight700Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask }
455     };
456
457     unsigned ruleSetIndex = 0;
458     unsigned w = FontWeight100Bit;
459     while (!(desiredTraitsMaskForComparison & (1 << w))) {
460         w++;
461         ruleSetIndex++;
462     }
463
464     ASSERT_WITH_SECURITY_IMPLICATION(ruleSetIndex < fallbackRuleSets);
465     const FontTraitsMask* weightFallbackRule = weightFallbackRuleSets[ruleSetIndex];
466     for (unsigned i = 0; i < rulesPerSet; ++i) {
467         if (secondTraitsMask & weightFallbackRule[i])
468             return false;
469         if (firstTraitsMask & weightFallbackRule[i])
470             return true;
471     }
472
473     return false;
474 }
475
476 PassRefPtr<FontData> CSSFontSelector::getFontData(const FontDescription& fontDescription, const AtomicString& familyName)
477 {
478     if (m_fontFaces.isEmpty()) {
479         if (familyName.startsWith("-webkit-"))
480             return fontDataForGenericFamily(m_document, fontDescription, familyName);
481         if (fontDescription.genericFamily() == FontDescription::StandardFamily && !fontDescription.isSpecifiedFont())
482             return fontDataForGenericFamily(m_document, fontDescription, standardFamily);
483         return 0;
484     }
485
486     CSSSegmentedFontFace* face = getFontFace(fontDescription, familyName);
487     // If no face was found, then return 0 and let the OS come up with its best match for the name.
488     if (!face) {
489         // If we were handed a generic family, but there was no match, go ahead and return the correct font based off our
490         // settings.
491         if (fontDescription.genericFamily() == FontDescription::StandardFamily && !fontDescription.isSpecifiedFont())
492             return fontDataForGenericFamily(m_document, fontDescription, standardFamily);
493         return fontDataForGenericFamily(m_document, fontDescription, familyName);
494     }
495
496     // We have a face. Ask it for a font data. If it cannot produce one, it will fail, and the OS will take over.
497     return face->getFontData(fontDescription);
498 }
499
500 CSSSegmentedFontFace* CSSFontSelector::getFontFace(const FontDescription& fontDescription, const AtomicString& family)
501 {
502     Vector<RefPtr<CSSFontFace>>* familyFontFaces = m_fontFaces.get(family);
503     if (!familyFontFaces || familyFontFaces->isEmpty())
504         return 0;
505
506     std::unique_ptr<HashMap<unsigned, RefPtr<CSSSegmentedFontFace>>>& segmentedFontFaceCache = m_fonts.add(family, nullptr).iterator->value;
507     if (!segmentedFontFaceCache)
508         segmentedFontFaceCache = std::make_unique<HashMap<unsigned, RefPtr<CSSSegmentedFontFace>>>();
509
510     FontTraitsMask traitsMask = fontDescription.traitsMask();
511
512     RefPtr<CSSSegmentedFontFace>& face = segmentedFontFaceCache->add(traitsMask, nullptr).iterator->value;
513     if (!face) {
514         face = CSSSegmentedFontFace::create(this);
515
516         // Collect all matching faces and sort them in order of preference.
517         Vector<CSSFontFace*, 32> candidateFontFaces;
518         for (int i = familyFontFaces->size() - 1; i >= 0; --i) {
519             CSSFontFace* candidate = familyFontFaces->at(i).get();
520             unsigned candidateTraitsMask = candidate->traitsMask();
521             if ((traitsMask & FontStyleNormalMask) && !(candidateTraitsMask & FontStyleNormalMask))
522                 continue;
523             if ((traitsMask & FontVariantNormalMask) && !(candidateTraitsMask & FontVariantNormalMask))
524                 continue;
525 #if ENABLE(SVG_FONTS)
526             // For SVG Fonts that specify that they only support the "normal" variant, we will assume they are incapable
527             // of small-caps synthesis and just ignore the font face as a candidate.
528             if (candidate->hasSVGFontFaceSource() && (traitsMask & FontVariantSmallCapsMask) && !(candidateTraitsMask & FontVariantSmallCapsMask))
529                 continue;
530 #endif
531             candidateFontFaces.append(candidate);
532         }
533
534         if (Vector<RefPtr<CSSFontFace>>* familyLocallyInstalledFontFaces = m_locallyInstalledFontFaces.get(family)) {
535             unsigned numLocallyInstalledFontFaces = familyLocallyInstalledFontFaces->size();
536             for (unsigned i = 0; i < numLocallyInstalledFontFaces; ++i) {
537                 CSSFontFace* candidate = familyLocallyInstalledFontFaces->at(i).get();
538                 unsigned candidateTraitsMask = candidate->traitsMask();
539                 if ((traitsMask & FontStyleNormalMask) && !(candidateTraitsMask & FontStyleNormalMask))
540                     continue;
541                 if ((traitsMask & FontVariantNormalMask) && !(candidateTraitsMask & FontVariantNormalMask))
542                     continue;
543                 candidateFontFaces.append(candidate);
544             }
545         }
546
547         desiredTraitsMaskForComparison = traitsMask;
548         std::stable_sort(candidateFontFaces.begin(), candidateFontFaces.end(), compareFontFaces);
549         unsigned numCandidates = candidateFontFaces.size();
550         for (unsigned i = 0; i < numCandidates; ++i)
551             face->appendFontFace(candidateFontFaces[i]);
552     }
553     return face.get();
554 }
555
556 void CSSFontSelector::clearDocument()
557 {
558     if (!m_document) {
559         ASSERT(!m_beginLoadingTimer.isActive());
560         ASSERT(m_fontsToBeginLoading.isEmpty());
561         return;
562     }
563
564     m_beginLoadingTimer.stop();
565
566     CachedResourceLoader* cachedResourceLoader = m_document->cachedResourceLoader();
567     for (size_t i = 0; i < m_fontsToBeginLoading.size(); ++i) {
568         // Balances incrementRequestCount() in beginLoadingFontSoon().
569         cachedResourceLoader->decrementRequestCount(m_fontsToBeginLoading[i].get());
570     }
571
572     m_fontsToBeginLoading.clear();
573
574     m_document = 0;
575 }
576
577 void CSSFontSelector::beginLoadingFontSoon(CachedFont* font)
578 {
579     if (!m_document)
580         return;
581
582     m_fontsToBeginLoading.append(font);
583     // Increment the request count now, in order to prevent didFinishLoad from being dispatched
584     // after this font has been requested but before it began loading. Balanced by
585     // decrementRequestCount() in beginLoadTimerFired() and in clearDocument().
586     m_document->cachedResourceLoader()->incrementRequestCount(font);
587     m_beginLoadingTimer.startOneShot(0);
588 }
589
590 void CSSFontSelector::beginLoadTimerFired()
591 {
592     Vector<CachedResourceHandle<CachedFont>> fontsToBeginLoading;
593     fontsToBeginLoading.swap(m_fontsToBeginLoading);
594
595     // CSSFontSelector could get deleted via beginLoadIfNeeded() or loadDone() unless protected.
596     Ref<CSSFontSelector> protect(*this);
597
598     CachedResourceLoader* cachedResourceLoader = m_document->cachedResourceLoader();
599     for (size_t i = 0; i < fontsToBeginLoading.size(); ++i) {
600         fontsToBeginLoading[i]->beginLoadIfNeeded(cachedResourceLoader);
601         // Balances incrementRequestCount() in beginLoadingFontSoon().
602         cachedResourceLoader->decrementRequestCount(fontsToBeginLoading[i].get());
603     }
604     // Ensure that if the request count reaches zero, the frame loader will know about it.
605     cachedResourceLoader->loadDone(0);
606     // New font loads may be triggered by layout after the document load is complete but before we have dispatched
607     // didFinishLoading for the frame. Make sure the delegate is always dispatched by checking explicitly.
608     if (m_document && m_document->frame())
609         m_document->frame()->loader().checkLoadComplete();
610 }
611
612 bool CSSFontSelector::resolvesFamilyFor(const FontDescription& description) const
613 {
614     for (unsigned i = 0; i < description.familyCount(); ++i) {
615         const AtomicString& familyName = description.familyAt(i);
616         if (description.genericFamily() == FontDescription::StandardFamily && !description.isSpecifiedFont())
617             return true;
618         if (familyName.isEmpty())
619             continue;
620         if (m_fontFaces.contains(familyName))
621             return true;
622         DEPRECATED_DEFINE_STATIC_LOCAL(String, webkitPrefix, ("-webkit-"));
623         if (familyName.startsWith(webkitPrefix))
624             return true;
625             
626     }
627     return false;
628 }
629
630 size_t CSSFontSelector::fallbackFontDataCount()
631 {
632     if (!m_document)
633         return 0;
634
635     if (Settings* settings = m_document->settings())
636         return settings->fontFallbackPrefersPictographs() ? 1 : 0;
637
638     return 0;
639 }
640
641 PassRefPtr<FontData> CSSFontSelector::getFallbackFontData(const FontDescription& fontDescription, size_t index)
642 {
643     ASSERT_UNUSED(index, !index);
644
645     if (!m_document)
646         return 0;
647
648     Settings* settings = m_document->settings();
649     if (!settings || !settings->fontFallbackPrefersPictographs())
650         return 0;
651
652     return fontCache().getCachedFontData(fontDescription, settings->pictographFontFamily());
653 }
654
655 }