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 "platform/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;
44 // FXIME: Make them fit into one word.
46 uint32_t bitfields2 : 7;
49 COMPILE_ASSERT(sizeof(FontDescription) == sizeof(SameSizeAsFontDescription), FontDescription_should_stay_small);
51 TypesettingFeatures FontDescription::s_defaultTypesettingFeatures = 0;
53 bool FontDescription::s_useSubpixelTextPositioning = false;
55 FontWeight FontDescription::lighterWeight(FontWeight weight)
74 return FontWeightNormal;
77 FontWeight FontDescription::bolderWeight(FontWeight weight)
96 return FontWeightNormal;
99 FontDescription::Size FontDescription::largerSize(const Size& size)
101 return Size(0, size.value * 1.2, size.isAbsolute);
104 FontDescription::Size FontDescription::smallerSize(const Size& size)
106 return Size(0, size.value / 1.2, size.isAbsolute);
109 FontTraits FontDescription::traits() const
111 return FontTraits(style(), variant(), weight(), stretch());
114 FontDescription::VariantLigatures FontDescription::variantLigatures() const
116 VariantLigatures ligatures;
118 ligatures.common = commonLigaturesState();
119 ligatures.discretionary = discretionaryLigaturesState();
120 ligatures.historical = historicalLigaturesState();
121 ligatures.contextual = contextualLigaturesState();
126 void FontDescription::setTraits(FontTraits traits)
128 setStyle(traits.style());
129 setVariant(traits.variant());
130 setWeight(traits.weight());
131 setStretch(traits.stretch());
134 void FontDescription::setVariantLigatures(const VariantLigatures& ligatures)
136 m_commonLigaturesState = ligatures.common;
137 m_discretionaryLigaturesState = ligatures.discretionary;
138 m_historicalLigaturesState = ligatures.historical;
139 m_contextualLigaturesState = ligatures.contextual;
141 updateTypesettingFeatures();
144 float FontDescription::effectiveFontSize() const
146 float size = (RuntimeEnabledFeatures::subpixelFontScalingEnabled())
148 : computedPixelSize();
150 // Ensure that the effective precision matches the font-cache precision.
151 // This guarantees that the same precision is used regardless of cache status.
152 return floorf(size * FontCacheKey::precisionMultiplier()) / FontCacheKey::precisionMultiplier();
155 FontCacheKey FontDescription::cacheKey(const FontFaceCreationParams& creationParams, FontTraits desiredTraits) const
157 FontTraits fontTraits = desiredTraits.bitfield() ? desiredTraits : traits();
160 static_cast<unsigned>(m_syntheticItalic) << 7 | // bit 8
161 static_cast<unsigned>(m_syntheticBold) << 6 | // bit 7
162 static_cast<unsigned>(m_fontSmoothing) << 4 | // bits 5-6
163 static_cast<unsigned>(m_textRendering) << 2 | // bits 3-4
164 static_cast<unsigned>(m_orientation) << 1 | // bit 2
165 static_cast<unsigned>(m_subpixelTextPosition); // bit 1
167 return FontCacheKey(creationParams, effectiveFontSize(), options | fontTraits.bitfield() << 8);
171 void FontDescription::setDefaultTypesettingFeatures(TypesettingFeatures typesettingFeatures)
173 s_defaultTypesettingFeatures = typesettingFeatures;
176 TypesettingFeatures FontDescription::defaultTypesettingFeatures()
178 return s_defaultTypesettingFeatures;
181 void FontDescription::updateTypesettingFeatures() const
183 m_typesettingFeatures = s_defaultTypesettingFeatures;
185 switch (textRendering()) {
186 case AutoTextRendering:
189 m_typesettingFeatures &= ~(blink::Kerning | Ligatures);
191 case GeometricPrecision:
192 case OptimizeLegibility:
193 m_typesettingFeatures |= blink::Kerning | Ligatures;
198 case FontDescription::NoneKerning:
199 m_typesettingFeatures &= ~blink::Kerning;
201 case FontDescription::NormalKerning:
202 m_typesettingFeatures |= blink::Kerning;
204 case FontDescription::AutoKerning:
208 // As per CSS (http://dev.w3.org/csswg/css-text-3/#letter-spacing-property),
209 // When the effective letter-spacing between two characters is not zero (due to
210 // either justification or non-zero computed letter-spacing), user agents should
211 // not apply optional ligatures.
212 if (m_letterSpacing == 0) {
213 switch (commonLigaturesState()) {
214 case FontDescription::DisabledLigaturesState:
215 m_typesettingFeatures &= ~Ligatures;
217 case FontDescription::EnabledLigaturesState:
218 m_typesettingFeatures |= Ligatures;
220 case FontDescription::NormalLigaturesState:
224 if (discretionaryLigaturesState() == FontDescription::EnabledLigaturesState
225 || historicalLigaturesState() == FontDescription::EnabledLigaturesState
226 || contextualLigaturesState() == FontDescription::EnabledLigaturesState) {
227 m_typesettingFeatures |= blink::Ligatures;