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