Home | History | Annotate | Download | only in fonts
      1 /*
      2  * Copyright (C) 2000 Lars Knoll (knoll (at) kde.org)
      3  *           (C) 2000 Antti Koivisto (koivisto (at) kde.org)
      4  *           (C) 2000 Dirk Mueller (mueller (at) kde.org)
      5  * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
      6  * Copyright (C) 2007 Nicholas Shanks <webkit (at) 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/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 "platform/text/NonCJKGlyphOrientation.h"
     39 #include "wtf/MathExtras.h"
     40 
     41 #include "wtf/RefPtr.h"
     42 
     43 #include <unicode/uscript.h>
     44 
     45 namespace WebCore {
     46 
     47 class PLATFORM_EXPORT FontDescription {
     48 public:
     49     enum GenericFamilyType { NoFamily, StandardFamily, SerifFamily, SansSerifFamily,
     50                              MonospaceFamily, CursiveFamily, FantasyFamily, PictographFamily };
     51 
     52     enum Kerning { AutoKerning, NormalKerning, NoneKerning };
     53 
     54     enum LigaturesState { NormalLigaturesState, DisabledLigaturesState, EnabledLigaturesState };
     55 
     56     FontDescription()
     57         : m_locale("en")
     58         , m_specifiedSize(0)
     59         , m_computedSize(0)
     60         , m_letterSpacing(0)
     61         , m_wordSpacing(0)
     62         , m_orientation(Horizontal)
     63         , m_nonCJKGlyphOrientation(NonCJKGlyphOrientationVerticalRight)
     64         , m_widthVariant(RegularWidth)
     65         , m_style(FontStyleNormal)
     66         , m_variant(FontVariantNormal)
     67         , m_isAbsoluteSize(false)
     68         , m_weight(FontWeightNormal)
     69         , m_stretch(FontStretchNormal)
     70         , m_genericFamily(NoFamily)
     71         , m_kerning(AutoKerning)
     72         , m_commonLigaturesState(NormalLigaturesState)
     73         , m_discretionaryLigaturesState(NormalLigaturesState)
     74         , m_historicalLigaturesState(NormalLigaturesState)
     75         , m_contextualLigaturesState(NormalLigaturesState)
     76         , m_keywordSize(0)
     77         , m_fontSmoothing(AutoSmoothing)
     78         , m_textRendering(AutoTextRendering)
     79         , m_script(USCRIPT_COMMON)
     80         , m_syntheticBold(false)
     81         , m_syntheticItalic(false)
     82         , m_subpixelTextPosition(s_useSubpixelTextPositioning)
     83         , m_typesettingFeatures(s_defaultTypesettingFeatures)
     84     {
     85     }
     86 
     87     bool operator==(const FontDescription&) const;
     88     bool operator!=(const FontDescription& other) const { return !(*this == other); }
     89 
     90     const FontFamily& family() const { return m_familyList; }
     91     FontFamily& firstFamily() { return m_familyList; }
     92     float specifiedSize() const { return m_specifiedSize; }
     93     float computedSize() const { return m_computedSize; }
     94     FontStyle style() const { return static_cast<FontStyle>(m_style); }
     95     int computedPixelSize() const { return int(m_computedSize + 0.5f); }
     96     FontVariant variant() const { return static_cast<FontVariant>(m_variant); }
     97     bool isAbsoluteSize() const { return m_isAbsoluteSize; }
     98     FontWeight weight() const { return static_cast<FontWeight>(m_weight); }
     99     FontStretch stretch() const { return static_cast<FontStretch>(m_stretch); }
    100     FontWeight lighterWeight() const;
    101     FontWeight bolderWeight() const;
    102     GenericFamilyType genericFamily() const { return static_cast<GenericFamilyType>(m_genericFamily); }
    103 
    104     // only use fixed default size when there is only one font family, and that family is "monospace"
    105     bool useFixedDefaultSize() const { return genericFamily() == MonospaceFamily && !family().next() && family().family() == FontFamilyNames::webkit_monospace; }
    106     Kerning kerning() const { return static_cast<Kerning>(m_kerning); }
    107     LigaturesState commonLigaturesState() const { return static_cast<LigaturesState>(m_commonLigaturesState); }
    108     LigaturesState discretionaryLigaturesState() const { return static_cast<LigaturesState>(m_discretionaryLigaturesState); }
    109     LigaturesState historicalLigaturesState() const { return static_cast<LigaturesState>(m_historicalLigaturesState); }
    110     LigaturesState contextualLigaturesState() const { return static_cast<LigaturesState>(m_contextualLigaturesState); }
    111     unsigned keywordSize() const { return m_keywordSize; }
    112     FontSmoothingMode fontSmoothing() const { return static_cast<FontSmoothingMode>(m_fontSmoothing); }
    113     TextRenderingMode textRendering() const { return static_cast<TextRenderingMode>(m_textRendering); }
    114     UScriptCode script() const { return static_cast<UScriptCode>(m_script); }
    115     const String& locale() const { return m_locale; }
    116     bool isSyntheticBold() const { return m_syntheticBold; }
    117     bool isSyntheticItalic() const { return m_syntheticItalic; }
    118     bool useSubpixelPositioning() const { return m_subpixelTextPosition; }
    119 
    120     FontTraits traits() const;
    121     float wordSpacing() const { return m_wordSpacing; }
    122     float letterSpacing() const { return m_letterSpacing; }
    123     FontOrientation orientation() const { return static_cast<FontOrientation>(m_orientation); }
    124     NonCJKGlyphOrientation nonCJKGlyphOrientation() const { return static_cast<NonCJKGlyphOrientation>(m_nonCJKGlyphOrientation); }
    125     FontWidthVariant widthVariant() const { return static_cast<FontWidthVariant>(m_widthVariant); }
    126     FontFeatureSettings* featureSettings() const { return m_featureSettings.get(); }
    127     FontDescription makeNormalFeatureSettings() const;
    128 
    129     float effectiveFontSize() const; // Returns either the computedSize or the computedPixelSize
    130     FontCacheKey cacheKey(const AtomicString& familyName, FontTraits desiredTraits = FontTraits(0)) const;
    131 
    132     void setFamily(const FontFamily& family) { m_familyList = family; }
    133     void setComputedSize(float s) { m_computedSize = clampToFloat(s); }
    134     void setSpecifiedSize(float s) { m_specifiedSize = clampToFloat(s); }
    135     void setStyle(FontStyle i) { m_style = i; }
    136     void setVariant(FontVariant c) { m_variant = c; }
    137     void setIsAbsoluteSize(bool s) { m_isAbsoluteSize = s; }
    138     void setWeight(FontWeight w) { m_weight = w; }
    139     void setStretch(FontStretch s) { m_stretch = s; }
    140     void setGenericFamily(GenericFamilyType genericFamily) { m_genericFamily = genericFamily; }
    141     void setKerning(Kerning kerning) { m_kerning = kerning; updateTypesettingFeatures(); }
    142     void setCommonLigaturesState(LigaturesState commonLigaturesState) { m_commonLigaturesState = commonLigaturesState; updateTypesettingFeatures(); }
    143     void setDiscretionaryLigaturesState(LigaturesState discretionaryLigaturesState) { m_discretionaryLigaturesState = discretionaryLigaturesState; updateTypesettingFeatures(); }
    144     void setHistoricalLigaturesState(LigaturesState historicalLigaturesState) { m_historicalLigaturesState = historicalLigaturesState; updateTypesettingFeatures(); }
    145     void setContextualLigaturesState(LigaturesState contextualLigaturesState) { m_contextualLigaturesState = contextualLigaturesState; updateTypesettingFeatures(); }
    146     void setKeywordSize(unsigned s) { m_keywordSize = s; }
    147     void setFontSmoothing(FontSmoothingMode smoothing) { m_fontSmoothing = smoothing; }
    148     void setTextRendering(TextRenderingMode rendering) { m_textRendering = rendering; updateTypesettingFeatures(); }
    149     void setOrientation(FontOrientation orientation) { m_orientation = orientation; }
    150     void setNonCJKGlyphOrientation(NonCJKGlyphOrientation orientation) { m_nonCJKGlyphOrientation = orientation; }
    151     void setWidthVariant(FontWidthVariant widthVariant) { m_widthVariant = widthVariant; }
    152     void setScript(UScriptCode s) { m_script = s; }
    153     void setLocale(const String& locale) { m_locale = locale; }
    154     void setSyntheticBold(bool syntheticBold) { m_syntheticBold = syntheticBold; }
    155     void setSyntheticItalic(bool syntheticItalic) { m_syntheticItalic = syntheticItalic; }
    156     void setFeatureSettings(PassRefPtr<FontFeatureSettings> settings) { m_featureSettings = settings; }
    157     void setTraits(FontTraits);
    158     void setWordSpacing(float s) { m_wordSpacing = s; }
    159     void setLetterSpacing(float s) { m_letterSpacing = s; }
    160 
    161     TypesettingFeatures typesettingFeatures() const { return static_cast<TypesettingFeatures>(m_typesettingFeatures); }
    162 
    163     static void setSubpixelPositioning(bool b) { s_useSubpixelTextPositioning = b; }
    164     static bool subpixelPositioning() { return s_useSubpixelTextPositioning; }
    165 
    166     static void setDefaultTypesettingFeatures(TypesettingFeatures);
    167     static TypesettingFeatures defaultTypesettingFeatures();
    168 
    169 private:
    170     FontFamily m_familyList; // The list of font families to be used.
    171     RefPtr<FontFeatureSettings> m_featureSettings;
    172     String m_locale;
    173 
    174     void updateTypesettingFeatures() const;
    175 
    176     float m_specifiedSize;   // Specified CSS value. Independent of rendering issues such as integer
    177                              // rounding, minimum font sizes, and zooming.
    178     float m_computedSize;    // Computed size adjusted for the minimum font size and the zoom factor.
    179 
    180     float m_letterSpacing;
    181     float m_wordSpacing;
    182 
    183     unsigned m_orientation : 1; // FontOrientation - Whether the font is rendering on a horizontal line or a vertical line.
    184     unsigned m_nonCJKGlyphOrientation : 1; // NonCJKGlyphOrientation - Only used by vertical text. Determines the default orientation for non-ideograph glyphs.
    185 
    186     unsigned m_widthVariant : 2; // FontWidthVariant
    187 
    188     unsigned m_style : 1; // FontStyle
    189     unsigned m_variant : 1; // FontVariant
    190     unsigned m_isAbsoluteSize : 1; // Whether or not CSS specified an explicit size
    191                                   // (logical sizes like "medium" don't count).
    192     unsigned m_weight : 4; // FontWeight
    193     unsigned m_stretch : 4; // FontStretch
    194     unsigned m_genericFamily : 3; // GenericFamilyType
    195 
    196     unsigned m_kerning : 2; // Kerning
    197 
    198     unsigned m_commonLigaturesState : 2;
    199     unsigned m_discretionaryLigaturesState : 2;
    200     unsigned m_historicalLigaturesState : 2;
    201     unsigned m_contextualLigaturesState : 2;
    202 
    203     unsigned m_keywordSize : 4; // We cache whether or not a font is currently represented by a CSS keyword (e.g., medium).  If so,
    204                            // then we can accurately translate across different generic families to adjust for different preference settings
    205                            // (e.g., 13px monospace vs. 16px everything else).  Sizes are 1-8 (like the HTML size values for <font>).
    206 
    207     unsigned m_fontSmoothing : 2; // FontSmoothingMode
    208     unsigned m_textRendering : 2; // TextRenderingMode
    209     unsigned m_script : 7; // Used to help choose an appropriate font for generic font families.
    210     unsigned m_syntheticBold : 1;
    211     unsigned m_syntheticItalic : 1;
    212     unsigned m_subpixelTextPosition : 1;
    213 
    214     mutable unsigned m_typesettingFeatures : 2; // TypesettingFeatures
    215 
    216     static TypesettingFeatures s_defaultTypesettingFeatures;
    217 
    218     static bool s_useSubpixelTextPositioning;
    219 };
    220 
    221 inline bool FontDescription::operator==(const FontDescription& other) const
    222 {
    223     return m_familyList == other.m_familyList
    224         && m_specifiedSize == other.m_specifiedSize
    225         && m_computedSize == other.m_computedSize
    226         && m_letterSpacing == other.m_letterSpacing
    227         && m_wordSpacing == other.m_wordSpacing
    228         && m_style == other.m_style
    229         && m_variant == other.m_variant
    230         && m_isAbsoluteSize == other.m_isAbsoluteSize
    231         && m_weight == other.m_weight
    232         && m_stretch == other.m_stretch
    233         && m_genericFamily == other.m_genericFamily
    234         && m_kerning == other.m_kerning
    235         && m_commonLigaturesState == other.m_commonLigaturesState
    236         && m_discretionaryLigaturesState == other.m_discretionaryLigaturesState
    237         && m_historicalLigaturesState == other.m_historicalLigaturesState
    238         && m_contextualLigaturesState == other.m_contextualLigaturesState
    239         && m_keywordSize == other.m_keywordSize
    240         && m_fontSmoothing == other.m_fontSmoothing
    241         && m_textRendering == other.m_textRendering
    242         && m_orientation == other.m_orientation
    243         && m_nonCJKGlyphOrientation == other.m_nonCJKGlyphOrientation
    244         && m_widthVariant == other.m_widthVariant
    245         && m_script == other.m_script
    246         && m_syntheticBold == other.m_syntheticBold
    247         && m_syntheticItalic == other.m_syntheticItalic
    248         && m_featureSettings == other.m_featureSettings
    249         && m_subpixelTextPosition == other.m_subpixelTextPosition;
    250 }
    251 
    252 }
    253 
    254 #endif
    255