1 /* 2 * Copyright (C) 2007, 2008, 2011 Apple Inc. All rights reserved. 3 * Copyright (C) 2013 Google Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 28 #include "config.h" 29 #include "core/css/FontFaceCache.h" 30 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" 41 42 namespace blink { 43 44 FontFaceCache::FontFaceCache() 45 : m_version(0) 46 { 47 } 48 49 void FontFaceCache::add(CSSFontSelector* cssFontSelector, const StyleRuleFontFace* fontFaceRule, PassRefPtrWillBeRawPtr<FontFace> prpFontFace) 50 { 51 RefPtrWillBeRawPtr<FontFace> fontFace = prpFontFace; 52 if (!m_styleRuleToFontFace.add(fontFaceRule, fontFace).isNewEntry) 53 return; 54 addFontFace(cssFontSelector, fontFace, true); 55 } 56 57 void FontFaceCache::addFontFace(CSSFontSelector* cssFontSelector, PassRefPtrWillBeRawPtr<FontFace> prpFontFace, bool cssConnected) 58 { 59 RefPtrWillBeRawPtr<FontFace> fontFace = prpFontFace; 60 61 FamilyToTraitsMap::AddResult traitsResult = m_fontFaces.add(fontFace->family(), nullptr); 62 if (!traitsResult.storedValue->value) 63 traitsResult.storedValue->value = adoptPtrWillBeNoop(new TraitsMap); 64 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()); 68 69 segmentedFontFaceResult.storedValue->value->addFontFace(fontFace, cssConnected); 70 if (cssConnected) 71 m_cssConnectedFontFaces.add(fontFace); 72 73 ++m_version; 74 } 75 76 void FontFaceCache::remove(const StyleRuleFontFace* fontFaceRule) 77 { 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); 82 } 83 } 84 85 void FontFaceCache::removeFontFace(FontFace* fontFace, bool cssConnected) 86 { 87 FamilyToTraitsMap::iterator fontFacesIter = m_fontFaces.find(fontFace->family()); 88 if (fontFacesIter == m_fontFaces.end()) 89 return; 90 TraitsMap* familyFontFaces = fontFacesIter->value.get(); 91 92 TraitsMap::iterator familyFontFacesIter = familyFontFaces->find(fontFace->traits().bitfield()); 93 if (familyFontFacesIter == familyFontFaces->end()) 94 return; 95 RefPtrWillBeRawPtr<CSSSegmentedFontFace> segmentedFontFace = familyFontFacesIter->value; 96 97 segmentedFontFace->removeFontFace(fontFace); 98 if (segmentedFontFace->isEmpty()) { 99 familyFontFaces->remove(familyFontFacesIter); 100 if (familyFontFaces->isEmpty()) 101 m_fontFaces.remove(fontFacesIter); 102 } 103 m_fonts.clear(); 104 if (cssConnected) 105 m_cssConnectedFontFaces.remove(fontFace); 106 107 ++m_version; 108 } 109 110 void FontFaceCache::clearCSSConnected() 111 { 112 for (StyleRuleToFontFace::iterator it = m_styleRuleToFontFace.begin(); it != m_styleRuleToFontFace.end(); ++it) 113 removeFontFace(it->value.get(), true); 114 m_styleRuleToFontFace.clear(); 115 } 116 117 void FontFaceCache::clearAll() 118 { 119 if (m_fontFaces.isEmpty()) 120 return; 121 122 m_fontFaces.clear(); 123 m_fonts.clear(); 124 m_styleRuleToFontFace.clear(); 125 m_cssConnectedFontFaces.clear(); 126 ++m_version; 127 } 128 129 static inline bool compareFontFaces(CSSSegmentedFontFace* first, CSSSegmentedFontFace* second, FontTraits desiredTraits) 130 { 131 const FontTraits& firstTraits = first->traits(); 132 const FontTraits& secondTraits = second->traits(); 133 134 bool firstHasDesiredVariant = firstTraits.variant() == desiredTraits.variant(); 135 bool secondHasDesiredVariant = secondTraits.variant() == desiredTraits.variant(); 136 137 if (firstHasDesiredVariant != secondHasDesiredVariant) 138 return firstHasDesiredVariant; 139 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; 148 } 149 150 bool firstHasDesiredStyle = firstTraits.style() == desiredTraits.style(); 151 bool secondHasDesiredStyle = secondTraits.style() == desiredTraits.style(); 152 153 if (firstHasDesiredStyle != secondHasDesiredStyle) 154 return firstHasDesiredStyle; 155 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; 163 } 164 if (secondTraits.weight() == desiredTraits.weight()) 165 return false; 166 167 if (firstTraits.weight() == desiredTraits.weight()) 168 return true; 169 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 } 187 }; 188 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]) 194 return false; 195 if (firstTraits.weight() == weightFallbackRule[i]) 196 return true; 197 } 198 199 return false; 200 } 201 202 CSSSegmentedFontFace* FontFaceCache::get(const FontDescription& fontDescription, const AtomicString& family) 203 { 204 TraitsMap* familyFontFaces = m_fontFaces.get(family); 205 if (!familyFontFaces || familyFontFaces->isEmpty()) 206 return 0; 207 208 FamilyToTraitsMap::AddResult traitsResult = m_fonts.add(family, nullptr); 209 if (!traitsResult.storedValue->value) 210 traitsResult.storedValue->value = adoptPtrWillBeNoop(new TraitsMap); 211 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) 219 continue; 220 if (traits.variant() == FontVariantNormal && candidateTraits.variant() != FontVariantNormal) 221 continue; 222 if (!faceResult.storedValue->value || compareFontFaces(candidate, faceResult.storedValue->value.get(), traits)) 223 faceResult.storedValue->value = candidate; 224 } 225 } 226 return faceResult.storedValue->value.get(); 227 } 228 229 void FontFaceCache::trace(Visitor* visitor) 230 { 231 #if ENABLE(OILPAN) 232 visitor->trace(m_fontFaces); 233 visitor->trace(m_fonts); 234 visitor->trace(m_styleRuleToFontFace); 235 visitor->trace(m_cssConnectedFontFaces); 236 #endif 237 } 238 239 } 240