2 * Copyright (C) 2007 Nicholas Shanks <contact@nickshanks.com>
3 * Copyright (C) 2008 Apple 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
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.
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.
31 #include "platform/fonts/FontDescription.h"
33 #include "RuntimeEnabledFeatures.h"
34 #include "wtf/text/AtomicStringHash.h"
35 #include "wtf/text/StringHash.h"
39 struct SameSizeAsFontDescription {
40 FontFamily familyList;
41 RefPtr<FontFeatureSettings> m_featureSettings;
43 // FXIME: Make them fit into one word.
45 uint32_t bitfields2 : 8;
48 COMPILE_ASSERT(sizeof(FontDescription) == sizeof(SameSizeAsFontDescription), FontDescription_should_stay_small);
50 TypesettingFeatures FontDescription::s_defaultTypesettingFeatures = 0;
52 bool FontDescription::s_useSubpixelTextPositioning = false;
54 FontWeight FontDescription::lighterWeight(void) const
73 return FontWeightNormal;
76 FontWeight FontDescription::bolderWeight(void) const
95 return FontWeightNormal;
98 FontTraitsMask FontDescription::traitsMask() const
100 return static_cast<FontTraitsMask>((m_italic ? FontStyleItalicMask : FontStyleNormalMask)
101 | (m_smallCaps ? FontVariantSmallCapsMask : FontVariantNormalMask)
102 | (FontWeight100Mask << (m_weight - FontWeight100)));
106 void FontDescription::setTraitsMask(FontTraitsMask traitsMask)
108 switch (traitsMask & FontWeightMask) {
109 case FontWeight100Mask:
110 setWeight(FontWeight100);
112 case FontWeight200Mask:
113 setWeight(FontWeight200);
115 case FontWeight300Mask:
116 setWeight(FontWeight300);
118 case FontWeight400Mask:
119 setWeight(FontWeight400);
121 case FontWeight500Mask:
122 setWeight(FontWeight500);
124 case FontWeight600Mask:
125 setWeight(FontWeight600);
127 case FontWeight700Mask:
128 setWeight(FontWeight700);
130 case FontWeight800Mask:
131 setWeight(FontWeight800);
133 case FontWeight900Mask:
134 setWeight(FontWeight900);
137 ASSERT_NOT_REACHED();
139 setItalic((traitsMask & FontStyleItalicMask) ? FontItalicOn : FontItalicOff);
140 setSmallCaps((traitsMask & FontVariantSmallCapsMask) ? FontSmallCapsOn : FontSmallCapsOff);
143 FontDescription FontDescription::makeNormalFeatureSettings() const
145 FontDescription normalDescription(*this);
146 normalDescription.setFeatureSettings(0);
147 return normalDescription;
150 float FontDescription::effectiveFontSize() const
152 float size = (RuntimeEnabledFeatures::subpixelFontScalingEnabled())
154 : computedPixelSize();
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();
161 FontCacheKey FontDescription::cacheKey(const AtomicString& familyName, FontTraitsMask desiredTraits) const
163 FontTraitsMask traits = desiredTraits
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
176 return FontCacheKey(familyName, effectiveFontSize(), options | traits << 9);
180 void FontDescription::setDefaultTypesettingFeatures(TypesettingFeatures typesettingFeatures)
182 s_defaultTypesettingFeatures = typesettingFeatures;
185 TypesettingFeatures FontDescription::defaultTypesettingFeatures()
187 return s_defaultTypesettingFeatures;
190 void FontDescription::updateTypesettingFeatures() const
192 m_typesettingFeatures = s_defaultTypesettingFeatures;
194 switch (textRenderingMode()) {
195 case AutoTextRendering:
198 m_typesettingFeatures &= ~(WebCore::Kerning | Ligatures);
200 case GeometricPrecision:
201 case OptimizeLegibility:
202 m_typesettingFeatures |= WebCore::Kerning | Ligatures;
207 case FontDescription::NoneKerning:
208 m_typesettingFeatures &= ~WebCore::Kerning;
210 case FontDescription::NormalKerning:
211 m_typesettingFeatures |= WebCore::Kerning;
213 case FontDescription::AutoKerning:
217 switch (commonLigaturesState()) {
218 case FontDescription::DisabledLigaturesState:
219 m_typesettingFeatures &= ~Ligatures;
221 case FontDescription::EnabledLigaturesState:
222 m_typesettingFeatures |= Ligatures;
224 case FontDescription::NormalLigaturesState:
229 } // namespace WebCore