1 /* 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> 7 * 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. 12 * 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. 17 * 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, USm_ 22 * 23 */ 24 25 #ifndef FontDescription_h 26 #define FontDescription_h 27 28 #include "platform/FontFamilyNames.h" 29 #include "platform/fonts/FixedPitchFontType.h" 30 #include "platform/fonts/FontCacheKey.h" 31 #include "platform/fonts/FontFamily.h" 32 #include "platform/fonts/FontFeatureSettings.h" 33 #include "platform/fonts/FontOrientation.h" 34 #include "platform/fonts/FontSmoothingMode.h" 35 #include "platform/fonts/FontTraits.h" 36 #include "platform/fonts/FontWidthVariant.h" 37 #include "platform/fonts/TextRenderingMode.h" 38 #include "platform/fonts/TypesettingFeatures.h" 39 #include "platform/text/NonCJKGlyphOrientation.h" 40 #include "wtf/MathExtras.h" 41 42 #include "wtf/RefPtr.h" 43 44 #include <unicode/uscript.h> 45 46 namespace blink { 47 48 class PLATFORM_EXPORT FontDescription { 49 public: 50 enum GenericFamilyType { NoFamily, StandardFamily, SerifFamily, SansSerifFamily, 51 MonospaceFamily, CursiveFamily, FantasyFamily, PictographFamily }; 52 53 enum Kerning { AutoKerning, NormalKerning, NoneKerning }; 54 55 enum LigaturesState { NormalLigaturesState, DisabledLigaturesState, EnabledLigaturesState }; 56 FontDescription()57 FontDescription() 58 : m_locale("en") 59 , m_specifiedSize(0) 60 , m_computedSize(0) 61 , m_letterSpacing(0) 62 , m_wordSpacing(0) 63 , m_orientation(Horizontal) 64 , m_nonCJKGlyphOrientation(NonCJKGlyphOrientationVerticalRight) 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) 77 , m_keywordSize(0) 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) 85 { 86 } 87 88 bool operator==(const FontDescription&) const; 89 bool operator!=(const FontDescription& other) const { return !(*this == other); } 90 91 struct VariantLigatures { VariantLigaturesVariantLigatures92 VariantLigatures() 93 : common(NormalLigaturesState) 94 , discretionary(NormalLigaturesState) 95 , historical(NormalLigaturesState) 96 , contextual(NormalLigaturesState) 97 { 98 } 99 100 unsigned common : 2; 101 unsigned discretionary : 2; 102 unsigned historical : 2; 103 unsigned contextual : 2; 104 }; 105 106 struct Size { SizeSize107 Size(unsigned keyword, float value, bool isAbsolute) 108 : keyword(keyword) 109 , isAbsolute(isAbsolute) 110 , value(value) 111 { 112 } 113 unsigned keyword : 4; // FontDescription::keywordSize 114 unsigned isAbsolute : 1; // FontDescription::isAbsoluteSize 115 float value; 116 }; 117 family()118 const FontFamily& family() const { return m_familyList; } firstFamily()119 FontFamily& firstFamily() { return m_familyList; } size()120 Size size() const { return Size(m_keywordSize, m_specifiedSize, m_isAbsoluteSize); } specifiedSize()121 float specifiedSize() const { return m_specifiedSize; } computedSize()122 float computedSize() const { return m_computedSize; } style()123 FontStyle style() const { return static_cast<FontStyle>(m_style); } computedPixelSize()124 int computedPixelSize() const { return int(m_computedSize + 0.5f); } variant()125 FontVariant variant() const { return static_cast<FontVariant>(m_variant); } isAbsoluteSize()126 bool isAbsoluteSize() const { return m_isAbsoluteSize; } weight()127 FontWeight weight() const { return static_cast<FontWeight>(m_weight); } stretch()128 FontStretch stretch() const { return static_cast<FontStretch>(m_stretch); } 129 static FontWeight lighterWeight(FontWeight); 130 static FontWeight bolderWeight(FontWeight); 131 static Size largerSize(const Size&); 132 static Size smallerSize(const Size&); genericFamily()133 GenericFamilyType genericFamily() const { return static_cast<GenericFamilyType>(m_genericFamily); } 134 135 // only use fixed default size when there is only one font family, and that family is "monospace" fixedPitchFontType()136 FixedPitchFontType fixedPitchFontType() const 137 { 138 if (genericFamily() == MonospaceFamily && !family().next() && family().family() == FontFamilyNames::webkit_monospace) 139 return FixedPitchFont; 140 return NonFixedPitchFont; 141 } kerning()142 Kerning kerning() const { return static_cast<Kerning>(m_kerning); } 143 VariantLigatures variantLigatures() const; commonLigaturesState()144 LigaturesState commonLigaturesState() const { return static_cast<LigaturesState>(m_commonLigaturesState); } discretionaryLigaturesState()145 LigaturesState discretionaryLigaturesState() const { return static_cast<LigaturesState>(m_discretionaryLigaturesState); } historicalLigaturesState()146 LigaturesState historicalLigaturesState() const { return static_cast<LigaturesState>(m_historicalLigaturesState); } contextualLigaturesState()147 LigaturesState contextualLigaturesState() const { return static_cast<LigaturesState>(m_contextualLigaturesState); } keywordSize()148 unsigned keywordSize() const { return m_keywordSize; } fontSmoothing()149 FontSmoothingMode fontSmoothing() const { return static_cast<FontSmoothingMode>(m_fontSmoothing); } textRendering()150 TextRenderingMode textRendering() const { return static_cast<TextRenderingMode>(m_textRendering); } script()151 UScriptCode script() const { return static_cast<UScriptCode>(m_script); } locale()152 const String& locale() const { return m_locale; } isSyntheticBold()153 bool isSyntheticBold() const { return m_syntheticBold; } isSyntheticItalic()154 bool isSyntheticItalic() const { return m_syntheticItalic; } useSubpixelPositioning()155 bool useSubpixelPositioning() const { return m_subpixelTextPosition; } 156 157 FontTraits traits() const; wordSpacing()158 float wordSpacing() const { return m_wordSpacing; } letterSpacing()159 float letterSpacing() const { return m_letterSpacing; } orientation()160 FontOrientation orientation() const { return static_cast<FontOrientation>(m_orientation); } nonCJKGlyphOrientation()161 NonCJKGlyphOrientation nonCJKGlyphOrientation() const { return static_cast<NonCJKGlyphOrientation>(m_nonCJKGlyphOrientation); } widthVariant()162 FontWidthVariant widthVariant() const { return static_cast<FontWidthVariant>(m_widthVariant); } featureSettings()163 FontFeatureSettings* featureSettings() const { return m_featureSettings.get(); } 164 165 float effectiveFontSize() const; // Returns either the computedSize or the computedPixelSize 166 FontCacheKey cacheKey(const FontFaceCreationParams&, FontTraits desiredTraits = FontTraits(0)) const; 167 setFamily(const FontFamily & family)168 void setFamily(const FontFamily& family) { m_familyList = family; } setComputedSize(float s)169 void setComputedSize(float s) { m_computedSize = clampToFloat(s); } setSpecifiedSize(float s)170 void setSpecifiedSize(float s) { m_specifiedSize = clampToFloat(s); } setStyle(FontStyle i)171 void setStyle(FontStyle i) { m_style = i; } setVariant(FontVariant c)172 void setVariant(FontVariant c) { m_variant = c; } 173 void setVariantLigatures(const VariantLigatures&); setIsAbsoluteSize(bool s)174 void setIsAbsoluteSize(bool s) { m_isAbsoluteSize = s; } setWeight(FontWeight w)175 void setWeight(FontWeight w) { m_weight = w; } setStretch(FontStretch s)176 void setStretch(FontStretch s) { m_stretch = s; } setGenericFamily(GenericFamilyType genericFamily)177 void setGenericFamily(GenericFamilyType genericFamily) { m_genericFamily = genericFamily; } setKerning(Kerning kerning)178 void setKerning(Kerning kerning) { m_kerning = kerning; updateTypesettingFeatures(); } setKeywordSize(unsigned s)179 void setKeywordSize(unsigned s) { m_keywordSize = s; } setFontSmoothing(FontSmoothingMode smoothing)180 void setFontSmoothing(FontSmoothingMode smoothing) { m_fontSmoothing = smoothing; } setTextRendering(TextRenderingMode rendering)181 void setTextRendering(TextRenderingMode rendering) { m_textRendering = rendering; updateTypesettingFeatures(); } setOrientation(FontOrientation orientation)182 void setOrientation(FontOrientation orientation) { m_orientation = orientation; } setNonCJKGlyphOrientation(NonCJKGlyphOrientation orientation)183 void setNonCJKGlyphOrientation(NonCJKGlyphOrientation orientation) { m_nonCJKGlyphOrientation = orientation; } setWidthVariant(FontWidthVariant widthVariant)184 void setWidthVariant(FontWidthVariant widthVariant) { m_widthVariant = widthVariant; } setScript(UScriptCode s)185 void setScript(UScriptCode s) { m_script = s; } setLocale(const String & locale)186 void setLocale(const String& locale) { m_locale = locale; } setSyntheticBold(bool syntheticBold)187 void setSyntheticBold(bool syntheticBold) { m_syntheticBold = syntheticBold; } setSyntheticItalic(bool syntheticItalic)188 void setSyntheticItalic(bool syntheticItalic) { m_syntheticItalic = syntheticItalic; } setFeatureSettings(PassRefPtr<FontFeatureSettings> settings)189 void setFeatureSettings(PassRefPtr<FontFeatureSettings> settings) { m_featureSettings = settings; } 190 void setTraits(FontTraits); setWordSpacing(float s)191 void setWordSpacing(float s) { m_wordSpacing = s; } setLetterSpacing(float s)192 void setLetterSpacing(float s) { m_letterSpacing = s; } 193 typesettingFeatures()194 TypesettingFeatures typesettingFeatures() const { return static_cast<TypesettingFeatures>(m_typesettingFeatures); } 195 setSubpixelPositioning(bool b)196 static void setSubpixelPositioning(bool b) { s_useSubpixelTextPositioning = b; } subpixelPositioning()197 static bool subpixelPositioning() { return s_useSubpixelTextPositioning; } 198 199 static void setDefaultTypesettingFeatures(TypesettingFeatures); 200 static TypesettingFeatures defaultTypesettingFeatures(); 201 202 private: 203 FontFamily m_familyList; // The list of font families to be used. 204 RefPtr<FontFeatureSettings> m_featureSettings; 205 String m_locale; 206 207 void updateTypesettingFeatures() const; 208 209 float m_specifiedSize; // Specified CSS value. Independent of rendering issues such as integer 210 // rounding, minimum font sizes, and zooming. 211 float m_computedSize; // Computed size adjusted for the minimum font size and the zoom factor. 212 213 float m_letterSpacing; 214 float m_wordSpacing; 215 216 unsigned m_orientation : 1; // FontOrientation - Whether the font is rendering on a horizontal line or a vertical line. 217 unsigned m_nonCJKGlyphOrientation : 1; // NonCJKGlyphOrientation - Only used by vertical text. Determines the default orientation for non-ideograph glyphs. 218 219 unsigned m_widthVariant : 2; // FontWidthVariant 220 221 unsigned m_style : 1; // FontStyle 222 unsigned m_variant : 1; // FontVariant 223 unsigned m_isAbsoluteSize : 1; // Whether or not CSS specified an explicit size 224 // (logical sizes like "medium" don't count). 225 unsigned m_weight : 4; // FontWeight 226 unsigned m_stretch : 4; // FontStretch 227 unsigned m_genericFamily : 3; // GenericFamilyType 228 229 unsigned m_kerning : 2; // Kerning 230 231 unsigned m_commonLigaturesState : 2; 232 unsigned m_discretionaryLigaturesState : 2; 233 unsigned m_historicalLigaturesState : 2; 234 unsigned m_contextualLigaturesState : 2; 235 236 unsigned m_keywordSize : 4; // We cache whether or not a font is currently represented by a CSS keyword (e.g., medium). If so, 237 // then we can accurately translate across different generic families to adjust for different preference settings 238 // (e.g., 13px monospace vs. 16px everything else). Sizes are 1-8 (like the HTML size values for <font>). 239 240 unsigned m_fontSmoothing : 2; // FontSmoothingMode 241 unsigned m_textRendering : 2; // TextRenderingMode 242 unsigned m_script : 7; // Used to help choose an appropriate font for generic font families. 243 unsigned m_syntheticBold : 1; 244 unsigned m_syntheticItalic : 1; 245 unsigned m_subpixelTextPosition : 1; 246 247 mutable unsigned m_typesettingFeatures : 2; // TypesettingFeatures 248 249 static TypesettingFeatures s_defaultTypesettingFeatures; 250 251 static bool s_useSubpixelTextPositioning; 252 }; 253 254 inline bool FontDescription::operator==(const FontDescription& other) const 255 { 256 return m_familyList == other.m_familyList 257 && m_specifiedSize == other.m_specifiedSize 258 && m_computedSize == other.m_computedSize 259 && m_letterSpacing == other.m_letterSpacing 260 && m_wordSpacing == other.m_wordSpacing 261 && m_style == other.m_style 262 && m_variant == other.m_variant 263 && m_isAbsoluteSize == other.m_isAbsoluteSize 264 && m_weight == other.m_weight 265 && m_stretch == other.m_stretch 266 && m_genericFamily == other.m_genericFamily 267 && m_kerning == other.m_kerning 268 && m_commonLigaturesState == other.m_commonLigaturesState 269 && m_discretionaryLigaturesState == other.m_discretionaryLigaturesState 270 && m_historicalLigaturesState == other.m_historicalLigaturesState 271 && m_contextualLigaturesState == other.m_contextualLigaturesState 272 && m_keywordSize == other.m_keywordSize 273 && m_fontSmoothing == other.m_fontSmoothing 274 && m_textRendering == other.m_textRendering 275 && m_orientation == other.m_orientation 276 && m_nonCJKGlyphOrientation == other.m_nonCJKGlyphOrientation 277 && m_widthVariant == other.m_widthVariant 278 && m_script == other.m_script 279 && m_syntheticBold == other.m_syntheticBold 280 && m_syntheticItalic == other.m_syntheticItalic 281 && m_featureSettings == other.m_featureSettings 282 && m_subpixelTextPosition == other.m_subpixelTextPosition; 283 } 284 285 } // namespace blink 286 287 #endif 288