• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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[2];
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 bool FontDescription::s_useSubpixelTextPositioning = false;
51 
lighterWeight(void) const52 FontWeight FontDescription::lighterWeight(void) const
53 {
54     switch (m_weight) {
55         case FontWeight100:
56         case FontWeight200:
57         case FontWeight300:
58         case FontWeight400:
59         case FontWeight500:
60             return FontWeight100;
61 
62         case FontWeight600:
63         case FontWeight700:
64             return FontWeight400;
65 
66         case FontWeight800:
67         case FontWeight900:
68             return FontWeight700;
69     }
70     ASSERT_NOT_REACHED();
71     return FontWeightNormal;
72 }
73 
bolderWeight(void) const74 FontWeight FontDescription::bolderWeight(void) const
75 {
76     switch (m_weight) {
77         case FontWeight100:
78         case FontWeight200:
79         case FontWeight300:
80             return FontWeight400;
81 
82         case FontWeight400:
83         case FontWeight500:
84             return FontWeight700;
85 
86         case FontWeight600:
87         case FontWeight700:
88         case FontWeight800:
89         case FontWeight900:
90             return FontWeight900;
91     }
92     ASSERT_NOT_REACHED();
93     return FontWeightNormal;
94 }
95 
traitsMask() const96 FontTraitsMask FontDescription::traitsMask() const
97 {
98     return static_cast<FontTraitsMask>((m_italic ? FontStyleItalicMask : FontStyleNormalMask)
99             | (m_smallCaps ? FontVariantSmallCapsMask : FontVariantNormalMask)
100             | (FontWeight100Mask << (m_weight - FontWeight100)));
101 
102 }
103 
setTraitsMask(FontTraitsMask traitsMask)104 void FontDescription::setTraitsMask(FontTraitsMask traitsMask)
105 {
106     switch (traitsMask & FontWeightMask) {
107     case FontWeight100Mask:
108         setWeight(FontWeight100);
109         break;
110     case FontWeight200Mask:
111         setWeight(FontWeight200);
112         break;
113     case FontWeight300Mask:
114         setWeight(FontWeight300);
115         break;
116     case FontWeight400Mask:
117         setWeight(FontWeight400);
118         break;
119     case FontWeight500Mask:
120         setWeight(FontWeight500);
121         break;
122     case FontWeight600Mask:
123         setWeight(FontWeight600);
124         break;
125     case FontWeight700Mask:
126         setWeight(FontWeight700);
127         break;
128     case FontWeight800Mask:
129         setWeight(FontWeight800);
130         break;
131     case FontWeight900Mask:
132         setWeight(FontWeight900);
133         break;
134     default:
135         ASSERT_NOT_REACHED();
136     }
137     setItalic((traitsMask & FontStyleItalicMask) ? FontItalicOn : FontItalicOff);
138     setSmallCaps((traitsMask & FontVariantSmallCapsMask) ? FontSmallCapsOn : FontSmallCapsOff);
139 }
140 
makeNormalFeatureSettings() const141 FontDescription FontDescription::makeNormalFeatureSettings() const
142 {
143     FontDescription normalDescription(*this);
144     normalDescription.setFeatureSettings(0);
145     return normalDescription;
146 }
147 
effectiveFontSize() const148 float FontDescription::effectiveFontSize() const
149 {
150     float size = (RuntimeEnabledFeatures::subpixelFontScalingEnabled())
151         ? computedSize()
152         : computedPixelSize();
153 
154     // Ensure that the effective precision matches the font-cache precision.
155     // This guarantees that the same precision is used regardless of cache status.
156     return floorf(size * FontCacheKey::precisionMultiplier()) / FontCacheKey::precisionMultiplier();
157 }
158 
cacheKey(const AtomicString & familyName,FontTraitsMask desiredTraits) const159 FontCacheKey FontDescription::cacheKey(const AtomicString& familyName, FontTraitsMask desiredTraits) const
160 {
161     FontTraitsMask traits = desiredTraits
162         ? desiredTraits
163         : traitsMask();
164 
165     unsigned options =
166         static_cast<unsigned>(m_syntheticItalic) << 8 | // bit 9
167         static_cast<unsigned>(m_syntheticBold) << 7 | // bit 8
168         static_cast<unsigned>(m_fontSmoothing) << 5 | // bits 6-7
169         static_cast<unsigned>(m_textRendering) << 3 | // bits 4-5
170         static_cast<unsigned>(m_orientation) << 2 | // bit 3
171         static_cast<unsigned>(m_usePrinterFont) << 1 | // bit 2
172         static_cast<unsigned>(m_subpixelTextPosition); // bit 1
173 
174     return FontCacheKey(familyName, effectiveFontSize(), options | traits << 9);
175 }
176 
177 } // namespace WebCore
178