Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / FontFaceCache.cpp
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 "FontFaceCache.h"
30
31 #include "FontFamilyNames.h"
32 #include "core/css/CSSFontSelector.h"
33 #include "core/css/CSSSegmentedFontFace.h"
34 #include "core/css/CSSValueList.h"
35 #include "core/css/FontFace.h"
36 #include "core/css/StyleRule.h"
37 #include "core/fetch/FontResource.h"
38 #include "core/fetch/ResourceFetcher.h"
39 #include "platform/fonts/FontDescription.h"
40 #include "wtf/text/AtomicString.h"
41
42 namespace WebCore {
43
44 FontFaceCache::FontFaceCache()
45     : m_version(0)
46 {
47 }
48
49 void FontFaceCache::add(CSSFontSelector* cssFontSelector, const StyleRuleFontFace* fontFaceRule, PassRefPtr<FontFace> prpFontFace)
50 {
51     RefPtr<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, PassRefPtr<FontFace> prpFontFace, bool cssConnected)
58 {
59     RefPtr<FontFace> fontFace = prpFontFace;
60
61     OwnPtr<TraitsMap>& familyFontFaces = m_fontFaces.add(fontFace->family(), nullptr).storedValue->value;
62     if (!familyFontFaces)
63         familyFontFaces = adoptPtr(new TraitsMap);
64
65     RefPtr<CSSSegmentedFontFace>& segmentedFontFace = familyFontFaces->add(fontFace->traitsMask(), 0).storedValue->value;
66     if (!segmentedFontFace)
67         segmentedFontFace = CSSSegmentedFontFace::create(cssFontSelector, static_cast<FontTraitsMask>(fontFace->traitsMask()));
68
69     segmentedFontFace->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->traitsMask());
93     if (familyFontFacesIter == familyFontFaces->end())
94         return;
95     RefPtr<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::clear()
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 static inline bool compareFontFaces(CSSSegmentedFontFace* first, CSSSegmentedFontFace* second, FontTraitsMask desiredTraitsMask)
118 {
119     FontTraitsMask firstTraitsMask = first->traitsMask();
120     FontTraitsMask secondTraitsMask = second->traitsMask();
121
122     bool firstHasDesiredVariant = firstTraitsMask & desiredTraitsMask & FontVariantMask;
123     bool secondHasDesiredVariant = secondTraitsMask & desiredTraitsMask & FontVariantMask;
124
125     if (firstHasDesiredVariant != secondHasDesiredVariant)
126         return firstHasDesiredVariant;
127
128     // We need to check font-variant css property for CSS2.1 compatibility.
129     if (desiredTraitsMask & FontVariantSmallCapsMask) {
130         // Prefer a font that has indicated that it can only support small-caps to a font that claims to support
131         // all variants. The specialized font is more likely to be true small-caps and not require synthesis.
132         bool firstRequiresSmallCaps = (firstTraitsMask & FontVariantSmallCapsMask) && !(firstTraitsMask & FontVariantNormalMask);
133         bool secondRequiresSmallCaps = (secondTraitsMask & FontVariantSmallCapsMask) && !(secondTraitsMask & FontVariantNormalMask);
134         if (firstRequiresSmallCaps != secondRequiresSmallCaps)
135             return firstRequiresSmallCaps;
136     }
137
138     bool firstHasDesiredStyle = firstTraitsMask & desiredTraitsMask & FontStyleMask;
139     bool secondHasDesiredStyle = secondTraitsMask & desiredTraitsMask & FontStyleMask;
140
141     if (firstHasDesiredStyle != secondHasDesiredStyle)
142         return firstHasDesiredStyle;
143
144     if (desiredTraitsMask & FontStyleItalicMask) {
145         // Prefer a font that has indicated that it can only support italics to a font that claims to support
146         // all styles. The specialized font is more likely to be the one the author wants used.
147         bool firstRequiresItalics = (firstTraitsMask & FontStyleItalicMask) && !(firstTraitsMask & FontStyleNormalMask);
148         bool secondRequiresItalics = (secondTraitsMask & FontStyleItalicMask) && !(secondTraitsMask & FontStyleNormalMask);
149         if (firstRequiresItalics != secondRequiresItalics)
150             return firstRequiresItalics;
151     }
152
153     if (secondTraitsMask & desiredTraitsMask & FontWeightMask)
154         return false;
155     if (firstTraitsMask & desiredTraitsMask & FontWeightMask)
156         return true;
157
158     // http://www.w3.org/TR/2011/WD-css3-fonts-20111004/#font-matching-algorithm says :
159     //   - 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.
160     //   - 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.
161     //   - If the desired weight is 400, 500 is checked first and then the rule for desired weights less than 400 is used.
162     //   - If the desired weight is 500, 400 is checked first and then the rule for desired weights less than 400 is used.
163
164     static const unsigned fallbackRuleSets = 9;
165     static const unsigned rulesPerSet = 8;
166     static const FontTraitsMask weightFallbackRuleSets[fallbackRuleSets][rulesPerSet] = {
167         { FontWeight200Mask, FontWeight300Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
168         { FontWeight100Mask, FontWeight300Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
169         { FontWeight200Mask, FontWeight100Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
170         { FontWeight500Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
171         { FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
172         { FontWeight700Mask, FontWeight800Mask, FontWeight900Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },
173         { FontWeight800Mask, FontWeight900Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },
174         { FontWeight900Mask, FontWeight700Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },
175         { FontWeight800Mask, FontWeight700Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask }
176     };
177
178     unsigned ruleSetIndex = 0;
179     unsigned w = FontWeight100Bit;
180     while (!(desiredTraitsMask & (1 << w))) {
181         w++;
182         ruleSetIndex++;
183     }
184
185     ASSERT(ruleSetIndex < fallbackRuleSets);
186     const FontTraitsMask* weightFallbackRule = weightFallbackRuleSets[ruleSetIndex];
187     for (unsigned i = 0; i < rulesPerSet; ++i) {
188         if (secondTraitsMask & weightFallbackRule[i])
189             return false;
190         if (firstTraitsMask & weightFallbackRule[i])
191             return true;
192     }
193
194     return false;
195 }
196
197 CSSSegmentedFontFace* FontFaceCache::get(const FontDescription& fontDescription, const AtomicString& family)
198 {
199     TraitsMap* familyFontFaces = m_fontFaces.get(family);
200     if (!familyFontFaces || familyFontFaces->isEmpty())
201         return 0;
202
203     OwnPtr<TraitsMap>& segmentedFontFaceCache = m_fonts.add(family, nullptr).storedValue->value;
204     if (!segmentedFontFaceCache)
205         segmentedFontFaceCache = adoptPtr(new TraitsMap);
206
207     FontTraitsMask traitsMask = fontDescription.traitsMask();
208
209     RefPtr<CSSSegmentedFontFace>& face = segmentedFontFaceCache->add(traitsMask, 0).storedValue->value;
210     if (!face) {
211         for (TraitsMap::const_iterator i = familyFontFaces->begin(); i != familyFontFaces->end(); ++i) {
212             CSSSegmentedFontFace* candidate = i->value.get();
213             unsigned candidateTraitsMask = candidate->traitsMask();
214             if ((traitsMask & FontStyleNormalMask) && !(candidateTraitsMask & FontStyleNormalMask))
215                 continue;
216             if ((traitsMask & FontVariantNormalMask) && !(candidateTraitsMask & FontVariantNormalMask))
217                 continue;
218             if (!face || compareFontFaces(candidate, face.get(), traitsMask))
219                 face = candidate;
220         }
221     }
222     return face.get();
223 }
224
225 }