2 * Copyright (C) 2007, 2008, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include "core/css/FontFaceCache.h"
31 #include "core/css/CSSFontSelector.h"
32 #include "core/css/CSSSegmentedFontFace.h"
33 #include "core/css/CSSValueList.h"
34 #include "core/css/FontFace.h"
35 #include "core/css/StyleRule.h"
36 #include "core/fetch/FontResource.h"
37 #include "core/fetch/ResourceFetcher.h"
38 #include "platform/FontFamilyNames.h"
39 #include "platform/fonts/FontDescription.h"
40 #include "wtf/text/AtomicString.h"
44 FontFaceCache::FontFaceCache()
49 void FontFaceCache::add(CSSFontSelector* cssFontSelector, const StyleRuleFontFace* fontFaceRule, PassRefPtrWillBeRawPtr<FontFace> prpFontFace)
51 RefPtrWillBeRawPtr<FontFace> fontFace = prpFontFace;
52 if (!m_styleRuleToFontFace.add(fontFaceRule, fontFace).isNewEntry)
54 addFontFace(cssFontSelector, fontFace, true);
57 void FontFaceCache::addFontFace(CSSFontSelector* cssFontSelector, PassRefPtrWillBeRawPtr<FontFace> prpFontFace, bool cssConnected)
59 RefPtrWillBeRawPtr<FontFace> fontFace = prpFontFace;
61 FamilyToTraitsMap::AddResult traitsResult = m_fontFaces.add(fontFace->family(), nullptr);
62 if (!traitsResult.storedValue->value)
63 traitsResult.storedValue->value = adoptPtrWillBeNoop(new TraitsMap);
65 TraitsMap::AddResult segmentedFontFaceResult = traitsResult.storedValue->value->add(fontFace->traits().bitfield(), nullptr);
66 if (!segmentedFontFaceResult.storedValue->value)
67 segmentedFontFaceResult.storedValue->value = CSSSegmentedFontFace::create(cssFontSelector, fontFace->traits());
69 segmentedFontFaceResult.storedValue->value->addFontFace(fontFace, cssConnected);
71 m_cssConnectedFontFaces.add(fontFace);
76 void FontFaceCache::remove(const StyleRuleFontFace* fontFaceRule)
78 StyleRuleToFontFace::iterator it = m_styleRuleToFontFace.find(fontFaceRule);
79 if (it != m_styleRuleToFontFace.end()) {
80 removeFontFace(it->value.get(), true);
81 m_styleRuleToFontFace.remove(it);
85 void FontFaceCache::removeFontFace(FontFace* fontFace, bool cssConnected)
87 FamilyToTraitsMap::iterator fontFacesIter = m_fontFaces.find(fontFace->family());
88 if (fontFacesIter == m_fontFaces.end())
90 TraitsMap* familyFontFaces = fontFacesIter->value.get();
92 TraitsMap::iterator familyFontFacesIter = familyFontFaces->find(fontFace->traits().bitfield());
93 if (familyFontFacesIter == familyFontFaces->end())
95 RefPtrWillBeRawPtr<CSSSegmentedFontFace> segmentedFontFace = familyFontFacesIter->value;
97 segmentedFontFace->removeFontFace(fontFace);
98 if (segmentedFontFace->isEmpty()) {
99 familyFontFaces->remove(familyFontFacesIter);
100 if (familyFontFaces->isEmpty())
101 m_fontFaces.remove(fontFacesIter);
105 m_cssConnectedFontFaces.remove(fontFace);
110 void FontFaceCache::clearCSSConnected()
112 for (StyleRuleToFontFace::iterator it = m_styleRuleToFontFace.begin(); it != m_styleRuleToFontFace.end(); ++it)
113 removeFontFace(it->value.get(), true);
114 m_styleRuleToFontFace.clear();
117 void FontFaceCache::clearAll()
119 if (m_fontFaces.isEmpty())
124 m_styleRuleToFontFace.clear();
125 m_cssConnectedFontFaces.clear();
129 static inline bool compareFontFaces(CSSSegmentedFontFace* first, CSSSegmentedFontFace* second, FontTraits desiredTraits)
131 const FontTraits& firstTraits = first->traits();
132 const FontTraits& secondTraits = second->traits();
134 bool firstHasDesiredVariant = firstTraits.variant() == desiredTraits.variant();
135 bool secondHasDesiredVariant = secondTraits.variant() == desiredTraits.variant();
137 if (firstHasDesiredVariant != secondHasDesiredVariant)
138 return firstHasDesiredVariant;
140 // We need to check font-variant css property for CSS2.1 compatibility.
141 if (desiredTraits.variant() == FontVariantSmallCaps) {
142 // Prefer a font that has indicated that it can only support small-caps to a font that claims to support
143 // all variants. The specialized font is more likely to be true small-caps and not require synthesis.
144 bool firstRequiresSmallCaps = firstTraits.variant() == FontVariantSmallCaps;
145 bool secondRequiresSmallCaps = secondTraits.variant() == FontVariantSmallCaps;
146 if (firstRequiresSmallCaps != secondRequiresSmallCaps)
147 return firstRequiresSmallCaps;
150 bool firstHasDesiredStyle = firstTraits.style() == desiredTraits.style();
151 bool secondHasDesiredStyle = secondTraits.style() == desiredTraits.style();
153 if (firstHasDesiredStyle != secondHasDesiredStyle)
154 return firstHasDesiredStyle;
156 if (desiredTraits.style() == FontStyleItalic) {
157 // Prefer a font that has indicated that it can only support italics to a font that claims to support
158 // all styles. The specialized font is more likely to be the one the author wants used.
159 bool firstRequiresItalics = firstTraits.style() == FontStyleItalic;
160 bool secondRequiresItalics = secondTraits.style() == FontStyleItalic;
161 if (firstRequiresItalics != secondRequiresItalics)
162 return firstRequiresItalics;
164 if (secondTraits.weight() == desiredTraits.weight())
167 if (firstTraits.weight() == desiredTraits.weight())
170 // http://www.w3.org/TR/2011/WD-css3-fonts-20111004/#font-matching-algorithm says :
171 // - 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.
172 // - 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.
173 // - If the desired weight is 400, 500 is checked first and then the rule for desired weights less than 400 is used.
174 // - If the desired weight is 500, 400 is checked first and then the rule for desired weights less than 400 is used.
175 static const unsigned fallbackRuleSets = 9;
176 static const unsigned rulesPerSet = 8;
177 static const FontWeight weightFallbackRuleSets[fallbackRuleSets][rulesPerSet] = {
178 { FontWeight200, FontWeight300, FontWeight400, FontWeight500, FontWeight600, FontWeight700, FontWeight800, FontWeight900 },
179 { FontWeight100, FontWeight300, FontWeight400, FontWeight500, FontWeight600, FontWeight700, FontWeight800, FontWeight900 },
180 { FontWeight200, FontWeight100, FontWeight400, FontWeight500, FontWeight600, FontWeight700, FontWeight800, FontWeight900 },
181 { FontWeight500, FontWeight300, FontWeight200, FontWeight100, FontWeight600, FontWeight700, FontWeight800, FontWeight900 },
182 { FontWeight400, FontWeight300, FontWeight200, FontWeight100, FontWeight600, FontWeight700, FontWeight800, FontWeight900 },
183 { FontWeight700, FontWeight800, FontWeight900, FontWeight500, FontWeight400, FontWeight300, FontWeight200, FontWeight100 },
184 { FontWeight800, FontWeight900, FontWeight600, FontWeight500, FontWeight400, FontWeight300, FontWeight200, FontWeight100 },
185 { FontWeight900, FontWeight700, FontWeight600, FontWeight500, FontWeight400, FontWeight300, FontWeight200, FontWeight100 },
186 { FontWeight800, FontWeight700, FontWeight600, FontWeight500, FontWeight400, FontWeight300, FontWeight200, FontWeight100 }
189 unsigned ruleSetIndex = static_cast<unsigned>(desiredTraits.weight());
190 ASSERT(ruleSetIndex < fallbackRuleSets);
191 const FontWeight* weightFallbackRule = weightFallbackRuleSets[ruleSetIndex];
192 for (unsigned i = 0; i < rulesPerSet; ++i) {
193 if (secondTraits.weight() == weightFallbackRule[i])
195 if (firstTraits.weight() == weightFallbackRule[i])
202 CSSSegmentedFontFace* FontFaceCache::get(const FontDescription& fontDescription, const AtomicString& family)
204 TraitsMap* familyFontFaces = m_fontFaces.get(family);
205 if (!familyFontFaces || familyFontFaces->isEmpty())
208 FamilyToTraitsMap::AddResult traitsResult = m_fonts.add(family, nullptr);
209 if (!traitsResult.storedValue->value)
210 traitsResult.storedValue->value = adoptPtrWillBeNoop(new TraitsMap);
212 FontTraits traits = fontDescription.traits();
213 TraitsMap::AddResult faceResult = traitsResult.storedValue->value->add(traits.bitfield(), nullptr);
214 if (!faceResult.storedValue->value) {
215 for (TraitsMap::const_iterator i = familyFontFaces->begin(); i != familyFontFaces->end(); ++i) {
216 CSSSegmentedFontFace* candidate = i->value.get();
217 FontTraits candidateTraits = candidate->traits();
218 if (traits.style() == FontStyleNormal && candidateTraits.style() != FontStyleNormal)
220 if (traits.variant() == FontVariantNormal && candidateTraits.variant() != FontVariantNormal)
222 if (!faceResult.storedValue->value || compareFontFaces(candidate, faceResult.storedValue->value.get(), traits))
223 faceResult.storedValue->value = candidate;
226 return faceResult.storedValue->value.get();
229 void FontFaceCache::trace(Visitor* visitor)
232 visitor->trace(m_fontFaces);
233 visitor->trace(m_fonts);
234 visitor->trace(m_styleRuleToFontFace);
235 visitor->trace(m_cssConnectedFontFaces);