2 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Antti Koivisto (koivisto@kde.org)
4 * (C) 2000 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
6 * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIother.m_ If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 #ifndef FontDescription_h
26 #define FontDescription_h
28 #include "platform/FontFamilyNames.h"
29 #include "platform/fonts/FontCacheKey.h"
30 #include "platform/fonts/FontFamily.h"
31 #include "platform/fonts/FontFeatureSettings.h"
32 #include "platform/fonts/FontOrientation.h"
33 #include "platform/fonts/FontSmoothingMode.h"
34 #include "platform/fonts/FontTraits.h"
35 #include "platform/fonts/FontWidthVariant.h"
36 #include "platform/fonts/TextRenderingMode.h"
37 #include "platform/fonts/TypesettingFeatures.h"
38 #include "wtf/MathExtras.h"
40 #include "wtf/RefPtr.h"
42 #include <unicode/uscript.h>
46 const float FontSizeAdjustNone
= -1;
48 class PLATFORM_EXPORT FontDescription
{
50 enum GenericFamilyType
{ NoFamily
, StandardFamily
, SerifFamily
, SansSerifFamily
,
51 MonospaceFamily
, CursiveFamily
, FantasyFamily
, PictographFamily
};
53 enum Kerning
{ AutoKerning
, NormalKerning
, NoneKerning
};
55 enum LigaturesState
{ NormalLigaturesState
, DisabledLigaturesState
, EnabledLigaturesState
};
61 , m_sizeAdjust(FontSizeAdjustNone
)
64 , m_orientation(static_cast<unsigned>(FontOrientation::Horizontal
))
65 , m_widthVariant(RegularWidth
)
66 , m_style(FontStyleNormal
)
67 , m_variant(FontVariantNormal
)
68 , m_isAbsoluteSize(false)
69 , m_weight(FontWeightNormal
)
70 , m_stretch(FontStretchNormal
)
71 , m_genericFamily(NoFamily
)
72 , m_kerning(AutoKerning
)
73 , m_commonLigaturesState(NormalLigaturesState
)
74 , m_discretionaryLigaturesState(NormalLigaturesState
)
75 , m_historicalLigaturesState(NormalLigaturesState
)
76 , m_contextualLigaturesState(NormalLigaturesState
)
78 , m_fontSmoothing(AutoSmoothing
)
79 , m_textRendering(AutoTextRendering
)
80 , m_script(USCRIPT_COMMON
)
81 , m_syntheticBold(false)
82 , m_syntheticItalic(false)
83 , m_subpixelTextPosition(s_useSubpixelTextPositioning
)
84 , m_typesettingFeatures(s_defaultTypesettingFeatures
)
88 bool operator==(const FontDescription
&) const;
89 bool operator!=(const FontDescription
& other
) const { return !(*this == other
); }
91 struct VariantLigatures
{
92 VariantLigatures(LigaturesState state
= NormalLigaturesState
)
94 , discretionary(state
)
101 unsigned discretionary
: 2;
102 unsigned historical
: 2;
103 unsigned contextual
: 2;
107 Size(unsigned keyword
, float value
, bool isAbsolute
)
109 , isAbsolute(isAbsolute
)
113 unsigned keyword
: 4; // FontDescription::keywordSize
114 unsigned isAbsolute
: 1; // FontDescription::isAbsoluteSize
118 struct FamilyDescription
{
119 FamilyDescription(GenericFamilyType genericFamily
) : genericFamily(genericFamily
) { }
120 FamilyDescription(GenericFamilyType genericFamily
, const FontFamily
& family
)
121 : genericFamily(genericFamily
)
125 GenericFamilyType genericFamily
;
129 const FontFamily
& family() const { return m_familyList
; }
130 FamilyDescription
familyDescription() const { return FamilyDescription(genericFamily(), family()); }
131 FontFamily
& firstFamily() { return m_familyList
; }
132 Size
size() const { return Size(m_keywordSize
, m_specifiedSize
, m_isAbsoluteSize
); }
133 float specifiedSize() const { return m_specifiedSize
; }
134 float computedSize() const { return m_computedSize
; }
135 float adjustedSize() const { return m_adjustedSize
; }
136 float sizeAdjust() const { return m_sizeAdjust
; }
137 bool hasSizeAdjust() const { return m_sizeAdjust
!= FontSizeAdjustNone
; }
138 FontStyle
style() const { return static_cast<FontStyle
>(m_style
); }
139 int computedPixelSize() const { return int(m_computedSize
+ 0.5f
); }
140 FontVariant
variant() const { return static_cast<FontVariant
>(m_variant
); }
141 bool isAbsoluteSize() const { return m_isAbsoluteSize
; }
142 FontWeight
weight() const { return static_cast<FontWeight
>(m_weight
); }
143 FontStretch
stretch() const { return static_cast<FontStretch
>(m_stretch
); }
144 static FontWeight
lighterWeight(FontWeight
);
145 static FontWeight
bolderWeight(FontWeight
);
146 static Size
largerSize(const Size
&);
147 static Size
smallerSize(const Size
&);
148 GenericFamilyType
genericFamily() const { return static_cast<GenericFamilyType
>(m_genericFamily
); }
150 // only use fixed default size when there is only one font family, and that family is "monospace"
151 bool isMonospace() const
153 return genericFamily() == MonospaceFamily
&& !family().next() && family().family() == FontFamilyNames::webkit_monospace
;
155 Kerning
kerning() const { return static_cast<Kerning
>(m_kerning
); }
156 VariantLigatures
variantLigatures() const;
157 LigaturesState
commonLigaturesState() const { return static_cast<LigaturesState
>(m_commonLigaturesState
); }
158 LigaturesState
discretionaryLigaturesState() const { return static_cast<LigaturesState
>(m_discretionaryLigaturesState
); }
159 LigaturesState
historicalLigaturesState() const { return static_cast<LigaturesState
>(m_historicalLigaturesState
); }
160 LigaturesState
contextualLigaturesState() const { return static_cast<LigaturesState
>(m_contextualLigaturesState
); }
161 unsigned keywordSize() const { return m_keywordSize
; }
162 FontSmoothingMode
fontSmoothing() const { return static_cast<FontSmoothingMode
>(m_fontSmoothing
); }
163 TextRenderingMode
textRendering() const { return static_cast<TextRenderingMode
>(m_textRendering
); }
164 UScriptCode
script() const { return static_cast<UScriptCode
>(m_script
); }
165 const AtomicString
& locale(bool includeDefault
= true) const;
166 bool isSyntheticBold() const { return m_syntheticBold
; }
167 bool isSyntheticItalic() const { return m_syntheticItalic
; }
168 bool useSubpixelPositioning() const { return m_subpixelTextPosition
; }
170 FontTraits
traits() const;
171 float wordSpacing() const { return m_wordSpacing
; }
172 float letterSpacing() const { return m_letterSpacing
; }
173 FontOrientation
orientation() const { return static_cast<FontOrientation
>(m_orientation
); }
174 bool isVerticalAnyUpright() const { return blink::isVerticalAnyUpright(orientation()); }
175 bool isVerticalNonCJKUpright() const { return blink::isVerticalNonCJKUpright(orientation()); }
176 bool isVerticalUpright(UChar32 character
) const { return blink::isVerticalUpright(orientation(), character
); }
177 bool isVerticalBaseline() const { return blink::isVerticalBaseline(orientation()); }
178 FontWidthVariant
widthVariant() const { return static_cast<FontWidthVariant
>(m_widthVariant
); }
179 FontFeatureSettings
* featureSettings() const { return m_featureSettings
.get(); }
181 float effectiveFontSize() const; // Returns either the computedSize or the computedPixelSize
182 FontCacheKey
cacheKey(const FontFaceCreationParams
&, FontTraits desiredTraits
= FontTraits(0)) const;
184 void setFamily(const FontFamily
& family
) { m_familyList
= family
; }
185 void setComputedSize(float s
) { m_computedSize
= clampTo
<float>(s
); }
186 void setSpecifiedSize(float s
) { m_specifiedSize
= clampTo
<float>(s
); }
187 void setAdjustedSize(float s
) { m_adjustedSize
= clampTo
<float>(s
); }
188 void setSizeAdjust(float aspect
) { m_sizeAdjust
= clampTo
<float>(aspect
); }
189 void setStyle(FontStyle i
) { m_style
= i
; }
190 void setVariant(FontVariant c
) { m_variant
= c
; }
191 void setVariantLigatures(const VariantLigatures
&);
192 void setIsAbsoluteSize(bool s
) { m_isAbsoluteSize
= s
; }
193 void setWeight(FontWeight w
) { m_weight
= w
; }
194 void setStretch(FontStretch s
) { m_stretch
= s
; }
195 void setGenericFamily(GenericFamilyType genericFamily
) { m_genericFamily
= genericFamily
; }
196 void setKerning(Kerning kerning
) { m_kerning
= kerning
; updateTypesettingFeatures(); }
197 void setKeywordSize(unsigned s
) { m_keywordSize
= s
; }
198 void setFontSmoothing(FontSmoothingMode smoothing
) { m_fontSmoothing
= smoothing
; }
199 void setTextRendering(TextRenderingMode rendering
) { m_textRendering
= rendering
; updateTypesettingFeatures(); }
200 void setOrientation(FontOrientation orientation
) { m_orientation
= static_cast<unsigned>(orientation
); }
201 void setWidthVariant(FontWidthVariant widthVariant
) { m_widthVariant
= widthVariant
; }
202 void setScript(UScriptCode s
) { m_script
= s
; }
203 void setLocale(const AtomicString
& locale
) { m_locale
= locale
; }
204 void setSyntheticBold(bool syntheticBold
) { m_syntheticBold
= syntheticBold
; }
205 void setSyntheticItalic(bool syntheticItalic
) { m_syntheticItalic
= syntheticItalic
; }
206 void setFeatureSettings(PassRefPtr
<FontFeatureSettings
> settings
) { m_featureSettings
= settings
; }
207 void setTraits(FontTraits
);
208 void setWordSpacing(float s
) { m_wordSpacing
= s
; }
209 void setLetterSpacing(float s
) { m_letterSpacing
= s
; updateTypesettingFeatures(); }
211 TypesettingFeatures
typesettingFeatures() const { return static_cast<TypesettingFeatures
>(m_typesettingFeatures
); }
213 static void setSubpixelPositioning(bool b
) { s_useSubpixelTextPositioning
= b
; }
214 static bool subpixelPositioning() { return s_useSubpixelTextPositioning
; }
216 static void setDefaultTypesettingFeatures(TypesettingFeatures
);
217 static TypesettingFeatures
defaultTypesettingFeatures();
220 FontFamily m_familyList
; // The list of font families to be used.
221 RefPtr
<FontFeatureSettings
> m_featureSettings
;
222 AtomicString m_locale
;
224 void updateTypesettingFeatures() const;
226 float m_specifiedSize
; // Specified CSS value. Independent of rendering issues such as integer
227 // rounding, minimum font sizes, and zooming.
228 float m_computedSize
; // Computed size adjusted for the minimum font size and the zoom factor.
230 // (Given aspect value / aspect value of a font family) * specifiedSize.
231 // This value is adjusted for the minimum font size and the zoom factor
232 // as well as a computed size is.
233 float m_adjustedSize
;
235 // Given aspect value, i.e. font-size-adjust.
238 float m_letterSpacing
;
241 unsigned m_orientation
: static_cast<unsigned>(FontOrientation::BitCount
);
243 unsigned m_widthVariant
: 2; // FontWidthVariant
245 unsigned m_style
: 2; // FontStyle
246 unsigned m_variant
: 1; // FontVariant
247 unsigned m_isAbsoluteSize
: 1; // Whether or not CSS specified an explicit size
248 // (logical sizes like "medium" don't count).
249 unsigned m_weight
: 4; // FontWeight
250 unsigned m_stretch
: 4; // FontStretch
251 unsigned m_genericFamily
: 3; // GenericFamilyType
253 unsigned m_kerning
: 2; // Kerning
255 unsigned m_commonLigaturesState
: 2;
256 unsigned m_discretionaryLigaturesState
: 2;
257 unsigned m_historicalLigaturesState
: 2;
258 unsigned m_contextualLigaturesState
: 2;
260 unsigned m_keywordSize
: 4; // We cache whether or not a font is currently represented by a CSS keyword (e.g., medium). If so,
261 // then we can accurately translate across different generic families to adjust for different preference settings
262 // (e.g., 13px monospace vs. 16px everything else). Sizes are 1-8 (like the HTML size values for <font>).
264 unsigned m_fontSmoothing
: 2; // FontSmoothingMode
265 unsigned m_textRendering
: 2; // TextRenderingMode
266 unsigned m_script
: 7; // Used to help choose an appropriate font for generic font families.
267 unsigned m_syntheticBold
: 1;
268 unsigned m_syntheticItalic
: 1;
269 unsigned m_subpixelTextPosition
: 1;
271 mutable unsigned m_typesettingFeatures
: 2; // TypesettingFeatures
273 static TypesettingFeatures s_defaultTypesettingFeatures
;
275 static bool s_useSubpixelTextPositioning
;
278 inline bool FontDescription::operator==(const FontDescription
& other
) const
280 return m_familyList
== other
.m_familyList
281 && m_specifiedSize
== other
.m_specifiedSize
282 && m_computedSize
== other
.m_computedSize
283 && m_adjustedSize
== other
.m_adjustedSize
284 && m_sizeAdjust
== other
.m_sizeAdjust
285 && m_letterSpacing
== other
.m_letterSpacing
286 && m_wordSpacing
== other
.m_wordSpacing
287 && m_style
== other
.m_style
288 && m_variant
== other
.m_variant
289 && m_isAbsoluteSize
== other
.m_isAbsoluteSize
290 && m_weight
== other
.m_weight
291 && m_stretch
== other
.m_stretch
292 && m_genericFamily
== other
.m_genericFamily
293 && m_kerning
== other
.m_kerning
294 && m_commonLigaturesState
== other
.m_commonLigaturesState
295 && m_discretionaryLigaturesState
== other
.m_discretionaryLigaturesState
296 && m_historicalLigaturesState
== other
.m_historicalLigaturesState
297 && m_contextualLigaturesState
== other
.m_contextualLigaturesState
298 && m_keywordSize
== other
.m_keywordSize
299 && m_fontSmoothing
== other
.m_fontSmoothing
300 && m_textRendering
== other
.m_textRendering
301 && m_orientation
== other
.m_orientation
302 && m_widthVariant
== other
.m_widthVariant
303 && m_script
== other
.m_script
304 && m_syntheticBold
== other
.m_syntheticBold
305 && m_syntheticItalic
== other
.m_syntheticItalic
306 && m_featureSettings
== other
.m_featureSettings
307 && m_subpixelTextPosition
== other
.m_subpixelTextPosition
;