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