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