Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / FontDescription.cpp
1 /*
2  * Copyright (C) 2007 Nicholas Shanks <contact@nickshanks.com>
3  * Copyright (C) 2008 Apple 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  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "platform/fonts/FontDescription.h"
32
33 #include "RuntimeEnabledFeatures.h"
34 #include "wtf/text/AtomicStringHash.h"
35 #include "wtf/text/StringHash.h"
36
37 namespace WebCore {
38
39 struct SameSizeAsFontDescription {
40     FontFamily familyList;
41     RefPtr<FontFeatureSettings> m_featureSettings;
42     float sizes[4];
43     // FXIME: Make them fit into one word.
44     uint32_t bitfields;
45     uint32_t bitfields2 : 8;
46 };
47
48 COMPILE_ASSERT(sizeof(FontDescription) == sizeof(SameSizeAsFontDescription), FontDescription_should_stay_small);
49
50 TypesettingFeatures FontDescription::s_defaultTypesettingFeatures = 0;
51
52 bool FontDescription::s_useSubpixelTextPositioning = false;
53
54 FontWeight FontDescription::lighterWeight(void) const
55 {
56     switch (m_weight) {
57         case FontWeight100:
58         case FontWeight200:
59         case FontWeight300:
60         case FontWeight400:
61         case FontWeight500:
62             return FontWeight100;
63
64         case FontWeight600:
65         case FontWeight700:
66             return FontWeight400;
67
68         case FontWeight800:
69         case FontWeight900:
70             return FontWeight700;
71     }
72     ASSERT_NOT_REACHED();
73     return FontWeightNormal;
74 }
75
76 FontWeight FontDescription::bolderWeight(void) const
77 {
78     switch (m_weight) {
79         case FontWeight100:
80         case FontWeight200:
81         case FontWeight300:
82             return FontWeight400;
83
84         case FontWeight400:
85         case FontWeight500:
86             return FontWeight700;
87
88         case FontWeight600:
89         case FontWeight700:
90         case FontWeight800:
91         case FontWeight900:
92             return FontWeight900;
93     }
94     ASSERT_NOT_REACHED();
95     return FontWeightNormal;
96 }
97
98 FontTraitsMask FontDescription::traitsMask() const
99 {
100     return static_cast<FontTraitsMask>((m_italic ? FontStyleItalicMask : FontStyleNormalMask)
101             | (m_smallCaps ? FontVariantSmallCapsMask : FontVariantNormalMask)
102             | (FontWeight100Mask << (m_weight - FontWeight100)));
103
104 }
105
106 void FontDescription::setTraitsMask(FontTraitsMask traitsMask)
107 {
108     switch (traitsMask & FontWeightMask) {
109     case FontWeight100Mask:
110         setWeight(FontWeight100);
111         break;
112     case FontWeight200Mask:
113         setWeight(FontWeight200);
114         break;
115     case FontWeight300Mask:
116         setWeight(FontWeight300);
117         break;
118     case FontWeight400Mask:
119         setWeight(FontWeight400);
120         break;
121     case FontWeight500Mask:
122         setWeight(FontWeight500);
123         break;
124     case FontWeight600Mask:
125         setWeight(FontWeight600);
126         break;
127     case FontWeight700Mask:
128         setWeight(FontWeight700);
129         break;
130     case FontWeight800Mask:
131         setWeight(FontWeight800);
132         break;
133     case FontWeight900Mask:
134         setWeight(FontWeight900);
135         break;
136     default:
137         ASSERT_NOT_REACHED();
138     }
139     setItalic((traitsMask & FontStyleItalicMask) ? FontItalicOn : FontItalicOff);
140     setSmallCaps((traitsMask & FontVariantSmallCapsMask) ? FontSmallCapsOn : FontSmallCapsOff);
141 }
142
143 FontDescription FontDescription::makeNormalFeatureSettings() const
144 {
145     FontDescription normalDescription(*this);
146     normalDescription.setFeatureSettings(0);
147     return normalDescription;
148 }
149
150 float FontDescription::effectiveFontSize() const
151 {
152     float size = (RuntimeEnabledFeatures::subpixelFontScalingEnabled())
153         ? computedSize()
154         : computedPixelSize();
155
156     // Ensure that the effective precision matches the font-cache precision.
157     // This guarantees that the same precision is used regardless of cache status.
158     return floorf(size * FontCacheKey::precisionMultiplier()) / FontCacheKey::precisionMultiplier();
159 }
160
161 FontCacheKey FontDescription::cacheKey(const AtomicString& familyName, FontTraitsMask desiredTraits) const
162 {
163     FontTraitsMask traits = desiredTraits
164         ? desiredTraits
165         : traitsMask();
166
167     unsigned options =
168         static_cast<unsigned>(m_syntheticItalic) << 8 | // bit 9
169         static_cast<unsigned>(m_syntheticBold) << 7 | // bit 8
170         static_cast<unsigned>(m_fontSmoothing) << 5 | // bits 6-7
171         static_cast<unsigned>(m_textRendering) << 3 | // bits 4-5
172         static_cast<unsigned>(m_orientation) << 2 | // bit 3
173         static_cast<unsigned>(m_usePrinterFont) << 1 | // bit 2
174         static_cast<unsigned>(m_subpixelTextPosition); // bit 1
175
176     return FontCacheKey(familyName, effectiveFontSize(), options | traits << 9);
177 }
178
179
180 void FontDescription::setDefaultTypesettingFeatures(TypesettingFeatures typesettingFeatures)
181 {
182     s_defaultTypesettingFeatures = typesettingFeatures;
183 }
184
185 TypesettingFeatures FontDescription::defaultTypesettingFeatures()
186 {
187     return s_defaultTypesettingFeatures;
188 }
189
190 void FontDescription::updateTypesettingFeatures() const
191 {
192     m_typesettingFeatures = s_defaultTypesettingFeatures;
193
194     switch (textRenderingMode()) {
195     case AutoTextRendering:
196         break;
197     case OptimizeSpeed:
198         m_typesettingFeatures &= ~(WebCore::Kerning | Ligatures);
199         break;
200     case GeometricPrecision:
201     case OptimizeLegibility:
202         m_typesettingFeatures |= WebCore::Kerning | Ligatures;
203         break;
204     }
205
206     switch (kerning()) {
207     case FontDescription::NoneKerning:
208         m_typesettingFeatures &= ~WebCore::Kerning;
209         break;
210     case FontDescription::NormalKerning:
211         m_typesettingFeatures |= WebCore::Kerning;
212         break;
213     case FontDescription::AutoKerning:
214         break;
215     }
216
217     switch (commonLigaturesState()) {
218     case FontDescription::DisabledLigaturesState:
219         m_typesettingFeatures &= ~Ligatures;
220         break;
221     case FontDescription::EnabledLigaturesState:
222         m_typesettingFeatures |= Ligatures;
223         break;
224     case FontDescription::NormalLigaturesState:
225         break;
226     }
227 }
228
229 } // namespace WebCore