font-variant-* properties in @font-face declarations should be honored
[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     if (RefPtr<CSSValue> fontVariant = style.getPropertyCSSValue(CSSPropertyFontVariant)) {
153         // font-variant descriptor can be a value list.
154         if (fontVariant->isPrimitiveValue()) {
155             RefPtr<CSSValueList> list = CSSValueList::createCommaSeparated();
156             list->append(fontVariant.releaseNonNull());
157             fontVariant = list.releaseNonNull();
158         } else if (!is<CSSValueList>(*fontVariant))
159             return Nullopt;
160
161         CSSValueList& variantList = downcast<CSSValueList>(*fontVariant);
162         unsigned numVariants = variantList.length();
163         if (!numVariants)
164             return Nullopt;
165
166         for (unsigned i = 0; i < numVariants; ++i) {
167             switch (downcast<CSSPrimitiveValue>(variantList.itemWithoutBoundsCheck(i))->getValueID()) {
168                 case CSSValueNormal:
169                     traitsMask |= FontVariantNormalMask;
170                     break;
171                 case CSSValueSmallCaps:
172                     traitsMask |= FontVariantSmallCapsMask;
173                     break;
174                 default:
175                     break;
176             }
177         }
178     } else
179         traitsMask |= FontVariantMask;
180
181     return static_cast<FontTraitsMask>(traitsMask);
182 }
183
184 static Ref<CSSFontFace> createFontFace(CSSValueList& srcList, FontTraitsMask traitsMask, Document* document, const StyleRuleFontFace& fontFaceRule, bool isInitiatingElementInUserAgentShadowTree)
185 {
186     RefPtr<CSSFontFaceRule> rule;
187 #if ENABLE(FONT_LOAD_EVENTS)
188     // FIXME: https://bugs.webkit.org/show_bug.cgi?id=112116 - This CSSFontFaceRule has no parent.
189     if (RuntimeEnabledFeatures::sharedFeatures().fontLoadEventsEnabled())
190         rule = static_pointer_cast<CSSFontFaceRule>(fontFaceRule.createCSSOMWrapper());
191 #else
192     UNUSED_PARAM(fontFaceRule);
193 #endif
194     Ref<CSSFontFace> fontFace = CSSFontFace::create(traitsMask, WTF::move(rule));
195
196     int srcLength = srcList.length();
197
198     bool foundSVGFont = false;
199
200     for (int i = 0; i < srcLength; i++) {
201         // An item in the list either specifies a string (local font name) or a URL (remote font to download).
202         CSSFontFaceSrcValue& item = downcast<CSSFontFaceSrcValue>(*srcList.itemWithoutBoundsCheck(i));
203         std::unique_ptr<CSSFontFaceSource> source;
204
205 #if ENABLE(SVG_FONTS)
206         foundSVGFont = item.isSVGFontFaceSrc() || item.svgFontFaceElement();
207 #endif
208         if (!item.isLocal()) {
209             Settings* settings = document ? document->settings() : nullptr;
210             bool allowDownloading = foundSVGFont || (settings && settings->downloadableBinaryFontsEnabled());
211             if (allowDownloading && item.isSupportedFormat() && document) {
212                 CachedFont* cachedFont = item.cachedFont(document, foundSVGFont, isInitiatingElementInUserAgentShadowTree);
213                 if (cachedFont) {
214                     source = std::make_unique<CSSFontFaceSource>(item.resource(), cachedFont);
215 #if ENABLE(SVG_FONTS)
216                     if (foundSVGFont)
217                         source->setHasExternalSVGFont();
218 #endif
219                 }
220             }
221         } else {
222             source = std::make_unique<CSSFontFaceSource>(item.resource());
223         }
224
225         if (source) {
226 #if ENABLE(SVG_FONTS)
227             source->setSVGFontFaceElement(item.svgFontFaceElement());
228 #endif
229             fontFace->addSource(WTF::move(source));
230         }
231     }
232
233     return fontFace;
234 }
235
236 static String familyNameFromPrimitive(const CSSPrimitiveValue& value)
237 {
238     if (value.isFontFamily())
239         return value.fontFamily().familyName;
240     if (!value.isValueID())
241         return { };
242
243     // We need to use the raw text for all the generic family types, since @font-face is a way of actually
244     // defining what font to use for those types.
245     switch (value.getValueID()) {
246     case CSSValueSerif:
247         return serifFamily;
248     case CSSValueSansSerif:
249         return sansSerifFamily;
250     case CSSValueCursive:
251         return cursiveFamily;
252     case CSSValueFantasy:
253         return fantasyFamily;
254     case CSSValueMonospace:
255         return monospaceFamily;
256     case CSSValueWebkitPictograph:
257         return pictographFamily;
258     default:
259         return { };
260     }
261 }
262
263 static Vector<Ref<CSSFontFace>> constructFamilyFontFaces(const String& familyName, HashMap<String, Vector<Ref<CSSFontFace>>, CaseFoldingHash>& locallyInstalledFontFaces)
264 {
265     auto result = Vector<Ref<CSSFontFace>>();
266
267     ASSERT(!locallyInstalledFontFaces.contains(familyName));
268
269     Vector<FontTraitsMask> traitsMasks = FontCache::singleton().getTraitsInFamily(familyName);
270     if (!traitsMasks.isEmpty()) {
271         Vector<Ref<CSSFontFace>> faces = { };
272         for (auto mask : traitsMasks) {
273             Ref<CSSFontFace> face = CSSFontFace::create(mask, nullptr, true);
274             face->addSource(std::make_unique<CSSFontFaceSource>(familyName));
275             ASSERT(face->isValid());
276             faces.append(WTF::move(face));
277         }
278         locallyInstalledFontFaces.add(familyName, WTF::move(faces));
279     }
280     return result;
281 }
282
283 void CSSFontSelector::addFontFaceRule(const StyleRuleFontFace& fontFaceRule, bool isInitiatingElementInUserAgentShadowTree)
284 {
285     const StyleProperties& style = fontFaceRule.properties();
286     RefPtr<CSSValue> fontFamily = style.getPropertyCSSValue(CSSPropertyFontFamily);
287     RefPtr<CSSValue> src = style.getPropertyCSSValue(CSSPropertySrc);
288     RefPtr<CSSValue> unicodeRange = style.getPropertyCSSValue(CSSPropertyUnicodeRange);
289     RefPtr<CSSValue> featureSettings = style.getPropertyCSSValue(CSSPropertyFontFeatureSettings);
290     RefPtr<CSSValue> variantLigatures = style.getPropertyCSSValue(CSSPropertyFontVariantLigatures);
291     RefPtr<CSSValue> variantPosition = style.getPropertyCSSValue(CSSPropertyFontVariantPosition);
292     RefPtr<CSSValue> variantCaps = style.getPropertyCSSValue(CSSPropertyFontVariantCaps);
293     RefPtr<CSSValue> variantNumeric = style.getPropertyCSSValue(CSSPropertyFontVariantNumeric);
294     RefPtr<CSSValue> variantAlternates = style.getPropertyCSSValue(CSSPropertyFontVariantAlternates);
295     RefPtr<CSSValue> variantEastAsian = style.getPropertyCSSValue(CSSPropertyFontVariantEastAsian);
296     if (!is<CSSValueList>(fontFamily.get()) || !is<CSSValueList>(src.get()) || (unicodeRange && !is<CSSValueList>(*unicodeRange)))
297         return;
298
299     CSSValueList& familyList = downcast<CSSValueList>(*fontFamily);
300     if (!familyList.length())
301         return;
302
303     CSSValueList& srcList = downcast<CSSValueList>(*src);
304     if (!srcList.length())
305         return;
306
307     CSSValueList* rangeList = downcast<CSSValueList>(unicodeRange.get());
308
309     auto computedTraitsMask = computeTraitsMask(style);
310     if (!computedTraitsMask)
311         return;
312     auto traitsMask = computedTraitsMask.value();
313
314     Ref<CSSFontFace> fontFace = createFontFace(srcList, traitsMask, m_document, fontFaceRule, isInitiatingElementInUserAgentShadowTree);
315     if (!fontFace->isValid())
316         return;
317
318     if (rangeList) {
319         unsigned numRanges = rangeList->length();
320         for (unsigned i = 0; i < numRanges; i++) {
321             CSSUnicodeRangeValue& range = downcast<CSSUnicodeRangeValue>(*rangeList->itemWithoutBoundsCheck(i));
322             fontFace->addRange(range.from(), range.to());
323         }
324     }
325
326     if (featureSettings) {
327         for (auto& item : downcast<CSSValueList>(*featureSettings)) {
328             auto& feature = downcast<CSSFontFeatureValue>(item.get());
329             fontFace->insertFeature(FontFeature(feature.tag(), feature.value()));
330         }
331     }
332
333     if (variantLigatures)
334         applyValueFontVariantLigatures(fontFace.get(), *variantLigatures);
335
336     if (variantPosition)
337         fontFace->setVariantPosition(downcast<CSSPrimitiveValue>(*variantPosition));
338
339     if (variantCaps)
340         fontFace->setVariantCaps(downcast<CSSPrimitiveValue>(*variantCaps));
341
342     if (variantNumeric)
343         applyValueFontVariantNumeric(fontFace.get(), *variantNumeric);
344
345     if (variantAlternates)
346         fontFace->setVariantAlternates(downcast<CSSPrimitiveValue>(*variantAlternates));
347
348     if (variantEastAsian)
349         applyValueFontVariantEastAsian(fontFace.get(), *variantEastAsian);
350
351     for (auto& item : familyList) {
352         String familyName = familyNameFromPrimitive(downcast<CSSPrimitiveValue>(item.get()));
353         if (familyName.isEmpty())
354             continue;
355
356         auto addResult = m_fontFaces.add(familyName, Vector<Ref<CSSFontFace>>());
357         auto& familyFontFaces = addResult.iterator->value;
358         if (addResult.isNewEntry)
359             familyFontFaces = constructFamilyFontFaces(familyName, m_locallyInstalledFontFaces);
360
361         familyFontFaces.append(fontFace.copyRef());
362         
363         ++m_version;
364     }
365 }
366
367 void CSSFontSelector::registerForInvalidationCallbacks(FontSelectorClient& client)
368 {
369     m_clients.add(&client);
370 }
371
372 void CSSFontSelector::unregisterForInvalidationCallbacks(FontSelectorClient& client)
373 {
374     m_clients.remove(&client);
375 }
376
377 void CSSFontSelector::dispatchInvalidationCallbacks()
378 {
379     ++m_version;
380
381     Vector<FontSelectorClient*> clients;
382     copyToVector(m_clients, clients);
383     for (size_t i = 0; i < clients.size(); ++i)
384         clients[i]->fontsNeedUpdate(*this);
385 }
386
387 void CSSFontSelector::fontLoaded()
388 {
389     dispatchInvalidationCallbacks();
390 }
391
392 void CSSFontSelector::fontCacheInvalidated()
393 {
394     dispatchInvalidationCallbacks();
395 }
396
397 static const AtomicString& resolveGenericFamily(Document* document, const FontDescription& fontDescription, const AtomicString& familyName)
398 {
399     if (!document || !document->frame())
400         return familyName;
401
402     const Settings& settings = document->frame()->settings();
403
404     UScriptCode script = fontDescription.script();
405     if (familyName == serifFamily)
406         return settings.serifFontFamily(script);
407     if (familyName == sansSerifFamily)
408         return settings.sansSerifFontFamily(script);
409     if (familyName == cursiveFamily)
410         return settings.cursiveFontFamily(script);
411     if (familyName == fantasyFamily)
412         return settings.fantasyFontFamily(script);
413     if (familyName == monospaceFamily)
414         return settings.fixedFontFamily(script);
415     if (familyName == pictographFamily)
416         return settings.pictographFontFamily(script);
417     if (familyName == standardFamily)
418         return settings.standardFontFamily(script);
419
420     return familyName;
421 }
422
423 static FontTraitsMask desiredTraitsMaskForComparison;
424
425 static inline bool compareFontFaces(const CSSFontFace& first, const CSSFontFace& second)
426 {
427     FontTraitsMask firstTraitsMask = first.traitsMask();
428     FontTraitsMask secondTraitsMask = second.traitsMask();
429
430     bool firstHasDesiredVariant = firstTraitsMask & desiredTraitsMaskForComparison & FontVariantMask;
431     bool secondHasDesiredVariant = secondTraitsMask & desiredTraitsMaskForComparison & FontVariantMask;
432
433     if (firstHasDesiredVariant != secondHasDesiredVariant)
434         return firstHasDesiredVariant;
435
436     // We need to check font-variant css property for CSS2.1 compatibility.
437     if ((desiredTraitsMaskForComparison & FontVariantSmallCapsMask) && !first.isLocalFallback() && !second.isLocalFallback()) {
438         // Prefer a font that has indicated that it can only support small-caps to a font that claims to support
439         // all variants.  The specialized font is more likely to be true small-caps and not require synthesis.
440         bool firstRequiresSmallCaps = (firstTraitsMask & FontVariantSmallCapsMask) && !(firstTraitsMask & FontVariantNormalMask);
441         bool secondRequiresSmallCaps = (secondTraitsMask & FontVariantSmallCapsMask) && !(secondTraitsMask & FontVariantNormalMask);
442         if (firstRequiresSmallCaps != secondRequiresSmallCaps)
443             return firstRequiresSmallCaps;
444     }
445
446     bool firstHasDesiredStyle = firstTraitsMask & desiredTraitsMaskForComparison & FontStyleMask;
447     bool secondHasDesiredStyle = secondTraitsMask & desiredTraitsMaskForComparison & FontStyleMask;
448
449     if (firstHasDesiredStyle != secondHasDesiredStyle)
450         return firstHasDesiredStyle;
451
452     if ((desiredTraitsMaskForComparison & FontStyleItalicMask) && !first.isLocalFallback() && !second.isLocalFallback()) {
453         // Prefer a font that has indicated that it can only support italics to a font that claims to support
454         // all styles.  The specialized font is more likely to be the one the author wants used.
455         bool firstRequiresItalics = (firstTraitsMask & FontStyleItalicMask) && !(firstTraitsMask & FontStyleNormalMask);
456         bool secondRequiresItalics = (secondTraitsMask & FontStyleItalicMask) && !(secondTraitsMask & FontStyleNormalMask);
457         if (firstRequiresItalics != secondRequiresItalics)
458             return firstRequiresItalics;
459     }
460
461     if (secondTraitsMask & desiredTraitsMaskForComparison & FontWeightMask)
462         return false;
463     if (firstTraitsMask & desiredTraitsMaskForComparison & FontWeightMask)
464         return true;
465
466     // http://www.w3.org/TR/2011/WD-css3-fonts-20111004/#font-matching-algorithm says :
467     //   - 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.
468     //   - 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.
469     //   - If the desired weight is 400, 500 is checked first and then the rule for desired weights less than 400 is used.
470     //   - If the desired weight is 500, 400 is checked first and then the rule for desired weights less than 400 is used.
471
472     static const unsigned fallbackRuleSets = 9;
473     static const unsigned rulesPerSet = 8;
474     static const FontTraitsMask weightFallbackRuleSets[fallbackRuleSets][rulesPerSet] = {
475         { FontWeight200Mask, FontWeight300Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
476         { FontWeight100Mask, FontWeight300Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
477         { FontWeight200Mask, FontWeight100Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
478         { FontWeight500Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
479         { FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
480         { FontWeight700Mask, FontWeight800Mask, FontWeight900Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },
481         { FontWeight800Mask, FontWeight900Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },
482         { FontWeight900Mask, FontWeight700Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },
483         { FontWeight800Mask, FontWeight700Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask }
484     };
485
486     unsigned ruleSetIndex = 0;
487     unsigned w = FontWeight100Bit;
488     while (!(desiredTraitsMaskForComparison & (1 << w))) {
489         w++;
490         ruleSetIndex++;
491     }
492
493     ASSERT_WITH_SECURITY_IMPLICATION(ruleSetIndex < fallbackRuleSets);
494     const FontTraitsMask* weightFallbackRule = weightFallbackRuleSets[ruleSetIndex];
495     for (unsigned i = 0; i < rulesPerSet; ++i) {
496         if (secondTraitsMask & weightFallbackRule[i])
497             return false;
498         if (firstTraitsMask & weightFallbackRule[i])
499             return true;
500     }
501
502     return false;
503 }
504
505 FontRanges CSSFontSelector::fontRangesForFamily(const FontDescription& fontDescription, const AtomicString& familyName)
506 {
507     // FIXME: The spec (and Firefox) says user specified generic families (sans-serif etc.) should be resolved before the @font-face lookup too.
508     bool resolveGenericFamilyFirst = familyName == standardFamily;
509
510     AtomicString familyForLookup = resolveGenericFamilyFirst ? resolveGenericFamily(m_document, fontDescription, familyName) : familyName;
511     CSSSegmentedFontFace* face = getFontFace(fontDescription, familyForLookup);
512     if (!face) {
513         if (!resolveGenericFamilyFirst)
514             familyForLookup = resolveGenericFamily(m_document, fontDescription, familyName);
515         return FontRanges(FontCache::singleton().fontForFamily(fontDescription, familyForLookup));
516     }
517
518     return face->fontRanges(fontDescription);
519 }
520
521 CSSSegmentedFontFace* CSSFontSelector::getFontFace(const FontDescription& fontDescription, const AtomicString& family)
522 {
523     auto iterator = m_fontFaces.find(family);
524     if (iterator == m_fontFaces.end())
525         return nullptr;
526     auto& familyFontFaces = iterator->value;
527
528     auto& segmentedFontFaceCache = m_fonts.add(family, HashMap<unsigned, RefPtr<CSSSegmentedFontFace>>()).iterator->value;
529
530     FontTraitsMask traitsMask = fontDescription.traitsMask();
531
532     RefPtr<CSSSegmentedFontFace>& face = segmentedFontFaceCache.add(traitsMask, nullptr).iterator->value;
533     if (!face) {
534         face = CSSSegmentedFontFace::create(this);
535
536         Vector<std::reference_wrapper<CSSFontFace>, 32> candidateFontFaces;
537         for (int i = familyFontFaces.size() - 1; i >= 0; --i) {
538             CSSFontFace& candidate = familyFontFaces[i];
539             unsigned candidateTraitsMask = candidate.traitsMask();
540             if ((traitsMask & FontStyleNormalMask) && !(candidateTraitsMask & FontStyleNormalMask))
541                 continue;
542             if ((traitsMask & FontVariantNormalMask) && !(candidateTraitsMask & FontVariantNormalMask))
543                 continue;
544 #if ENABLE(SVG_FONTS)
545             // For SVG Fonts that specify that they only support the "normal" variant, we will assume they are incapable
546             // of small-caps synthesis and just ignore the font face as a candidate.
547             if (candidate.hasSVGFontFaceSource() && (traitsMask & FontVariantSmallCapsMask) && !(candidateTraitsMask & FontVariantSmallCapsMask))
548                 continue;
549 #endif
550             candidateFontFaces.append(candidate);
551         }
552
553         auto iterator = m_locallyInstalledFontFaces.find(family);
554         if (iterator != m_locallyInstalledFontFaces.end()) {
555             for (auto& candidate : iterator->value) {
556                 unsigned candidateTraitsMask = candidate->traitsMask();
557                 if ((traitsMask & FontStyleNormalMask) && !(candidateTraitsMask & FontStyleNormalMask))
558                     continue;
559                 if ((traitsMask & FontVariantNormalMask) && !(candidateTraitsMask & FontVariantNormalMask))
560                     continue;
561                 candidateFontFaces.append(candidate);
562             }
563         }
564
565         desiredTraitsMaskForComparison = traitsMask;
566         std::stable_sort(candidateFontFaces.begin(), candidateFontFaces.end(), compareFontFaces);
567         for (auto& candidate : candidateFontFaces)
568             face->appendFontFace(candidate.get());
569     }
570     return face.get();
571 }
572
573 void CSSFontSelector::clearDocument()
574 {
575     if (!m_document) {
576         ASSERT(!m_beginLoadingTimer.isActive());
577         ASSERT(m_fontsToBeginLoading.isEmpty());
578         return;
579     }
580
581     m_beginLoadingTimer.stop();
582
583     CachedResourceLoader& cachedResourceLoader = m_document->cachedResourceLoader();
584     for (auto& fontHandle : m_fontsToBeginLoading) {
585         // Balances incrementRequestCount() in beginLoadingFontSoon().
586         cachedResourceLoader.decrementRequestCount(fontHandle.get());
587     }
588     m_fontsToBeginLoading.clear();
589
590     m_document = nullptr;
591 }
592
593 void CSSFontSelector::beginLoadingFontSoon(CachedFont* font)
594 {
595     if (!m_document)
596         return;
597
598     m_fontsToBeginLoading.append(font);
599     // Increment the request count now, in order to prevent didFinishLoad from being dispatched
600     // after this font has been requested but before it began loading. Balanced by
601     // decrementRequestCount() in beginLoadTimerFired() and in clearDocument().
602     m_document->cachedResourceLoader().incrementRequestCount(font);
603     m_beginLoadingTimer.startOneShot(0);
604 }
605
606 void CSSFontSelector::beginLoadTimerFired()
607 {
608     Vector<CachedResourceHandle<CachedFont>> fontsToBeginLoading;
609     fontsToBeginLoading.swap(m_fontsToBeginLoading);
610
611     // CSSFontSelector could get deleted via beginLoadIfNeeded() or loadDone() unless protected.
612     Ref<CSSFontSelector> protect(*this);
613
614     CachedResourceLoader& cachedResourceLoader = m_document->cachedResourceLoader();
615     for (auto& fontHandle : fontsToBeginLoading) {
616         fontHandle->beginLoadIfNeeded(cachedResourceLoader);
617         // Balances incrementRequestCount() in beginLoadingFontSoon().
618         cachedResourceLoader.decrementRequestCount(fontHandle.get());
619     }
620     // Ensure that if the request count reaches zero, the frame loader will know about it.
621     cachedResourceLoader.loadDone(nullptr);
622     // New font loads may be triggered by layout after the document load is complete but before we have dispatched
623     // didFinishLoading for the frame. Make sure the delegate is always dispatched by checking explicitly.
624     if (m_document && m_document->frame())
625         m_document->frame()->loader().checkLoadComplete();
626 }
627
628 bool CSSFontSelector::resolvesFamilyFor(const FontCascadeDescription& description) const
629 {
630     for (unsigned i = 0; i < description.familyCount(); ++i) {
631         const AtomicString& familyName = description.familyAt(i);
632         if (familyName.isEmpty())
633             continue;
634         if (m_fontFaces.contains(familyName))
635             return true;
636         static NeverDestroyed<String> webkitPrefix("-webkit-");
637         if (familyName.startsWith(webkitPrefix.get()))
638             return true;
639     }
640     return false;
641 }
642
643 size_t CSSFontSelector::fallbackFontCount()
644 {
645     if (!m_document)
646         return 0;
647
648     if (Settings* settings = m_document->settings())
649         return settings->fontFallbackPrefersPictographs() ? 1 : 0;
650
651     return 0;
652 }
653
654 PassRefPtr<Font> CSSFontSelector::fallbackFontAt(const FontDescription& fontDescription, size_t index)
655 {
656     ASSERT_UNUSED(index, !index);
657
658     if (!m_document)
659         return nullptr;
660
661     Settings* settings = m_document->settings();
662     if (!settings || !settings->fontFallbackPrefersPictographs())
663         return nullptr;
664
665     return FontCache::singleton().fontForFamily(fontDescription, settings->pictographFontFamily());
666 }
667
668 }