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 "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/FontTraitsMask.h"
     35 #include "platform/fonts/FontWidthVariant.h"
     36 #include "platform/fonts/TextRenderingMode.h"
     37 #include "platform/text/NonCJKGlyphOrientation.h"
     38 #include "wtf/MathExtras.h"
     39 
     40 #include "wtf/RefPtr.h"
     41 
     42 namespace WebCore {
     43 
     44 enum FontWeight {
     45     FontWeight100,
     46     FontWeight200,
     47     FontWeight300,
     48     FontWeight400,
     49     FontWeight500,
     50     FontWeight600,
     51     FontWeight700,
     52     FontWeight800,
     53     FontWeight900,
     54     FontWeightNormal = FontWeight400,
     55     FontWeightBold = FontWeight700
     56 };
     57 
     58 enum FontItalic {
     59     FontItalicOff = 0,
     60     FontItalicOn = 1
     61 };
     62 
     63 enum FontSmallCaps {
     64     FontSmallCapsOff = 0,
     65     FontSmallCapsOn = 1
     66 };
     67 
     68 class PLATFORM_EXPORT FontDescription {
     69 public:
     70     enum GenericFamilyType { NoFamily, StandardFamily, SerifFamily, SansSerifFamily,
     71                              MonospaceFamily, CursiveFamily, FantasyFamily, PictographFamily };
     72 
     73     enum Kerning { AutoKerning, NormalKerning, NoneKerning };
     74 
     75     enum LigaturesState { NormalLigaturesState, DisabledLigaturesState, EnabledLigaturesState };
     76 
     77     FontDescription()
     78         : m_specifiedSize(0)
     79         , m_computedSize(0)
     80         , m_orientation(Horizontal)
     81         , m_nonCJKGlyphOrientation(NonCJKGlyphOrientationVerticalRight)
     82         , m_widthVariant(RegularWidth)
     83         , m_italic(FontItalicOff)
     84         , m_smallCaps(FontSmallCapsOff)
     85         , m_isAbsoluteSize(false)
     86         , m_weight(FontWeightNormal)
     87         , m_genericFamily(NoFamily)
     88         , m_usePrinterFont(false)
     89         , m_kerning(AutoKerning)
     90         , m_commonLigaturesState(NormalLigaturesState)
     91         , m_discretionaryLigaturesState(NormalLigaturesState)
     92         , m_historicalLigaturesState(NormalLigaturesState)
     93         , m_keywordSize(0)
     94         , m_fontSmoothing(AutoSmoothing)
     95         , m_textRendering(AutoTextRendering)
     96         , m_isSpecifiedFont(false)
     97         , m_script(USCRIPT_COMMON)
     98         , m_syntheticBold(false)
     99         , m_syntheticItalic(false)
    100         , m_subpixelTextPosition(s_useSubpixelTextPositioning)
    101     {
    102     }
    103 
    104     bool operator==(const FontDescription&) const;
    105     bool operator!=(const FontDescription& other) const { return !(*this == other); }
    106 
    107     const FontFamily& family() const { return m_familyList; }
    108     FontFamily& firstFamily() { return m_familyList; }
    109     float specifiedSize() const { return m_specifiedSize; }
    110     float computedSize() const { return m_computedSize; }
    111     FontItalic italic() const { return static_cast<FontItalic>(m_italic); }
    112     int computedPixelSize() const { return int(m_computedSize + 0.5f); }
    113     FontSmallCaps smallCaps() const { return static_cast<FontSmallCaps>(m_smallCaps); }
    114     bool isAbsoluteSize() const { return m_isAbsoluteSize; }
    115     FontWeight weight() const { return static_cast<FontWeight>(m_weight); }
    116     FontWeight lighterWeight() const;
    117     FontWeight bolderWeight() const;
    118     GenericFamilyType genericFamily() const { return static_cast<GenericFamilyType>(m_genericFamily); }
    119     bool usePrinterFont() const { return m_usePrinterFont; }
    120     // only use fixed default size when there is only one font family, and that family is "monospace"
    121     bool useFixedDefaultSize() const { return genericFamily() == MonospaceFamily && !family().next() && family().family() == FontFamilyNames::webkit_monospace; }
    122     Kerning kerning() const { return static_cast<Kerning>(m_kerning); }
    123     LigaturesState commonLigaturesState() const { return static_cast<LigaturesState>(m_commonLigaturesState); }
    124     LigaturesState discretionaryLigaturesState() const { return static_cast<LigaturesState>(m_discretionaryLigaturesState); }
    125     LigaturesState historicalLigaturesState() const { return static_cast<LigaturesState>(m_historicalLigaturesState); }
    126     unsigned keywordSize() const { return m_keywordSize; }
    127     FontSmoothingMode fontSmoothing() const { return static_cast<FontSmoothingMode>(m_fontSmoothing); }
    128     TextRenderingMode textRenderingMode() const { return static_cast<TextRenderingMode>(m_textRendering); }
    129     UScriptCode script() const { return static_cast<UScriptCode>(m_script); }
    130     bool isSyntheticBold() const { return m_syntheticBold; }
    131     bool isSyntheticItalic() const { return m_syntheticItalic; }
    132     bool useSubpixelPositioning() const { return m_subpixelTextPosition; }
    133 
    134     FontTraitsMask traitsMask() const;
    135     bool isSpecifiedFont() const { return m_isSpecifiedFont; }
    136     FontOrientation orientation() const { return static_cast<FontOrientation>(m_orientation); }
    137     NonCJKGlyphOrientation nonCJKGlyphOrientation() const { return static_cast<NonCJKGlyphOrientation>(m_nonCJKGlyphOrientation); }
    138     FontWidthVariant widthVariant() const { return static_cast<FontWidthVariant>(m_widthVariant); }
    139     FontFeatureSettings* featureSettings() const { return m_featureSettings.get(); }
    140     FontDescription makeNormalFeatureSettings() const;
    141 
    142     float effectiveFontSize() const; // Returns either the computedSize or the computedPixelSize
    143     FontCacheKey cacheKey(const AtomicString& familyName, FontTraitsMask desiredTraits = static_cast<FontTraitsMask>(0)) const;
    144 
    145     void setFamily(const FontFamily& family) { m_familyList = family; }
    146     void setComputedSize(float s) { m_computedSize = clampToFloat(s); }
    147     void setSpecifiedSize(float s) { m_specifiedSize = clampToFloat(s); }
    148     void setItalic(FontItalic i) { m_italic = i; }
    149     void setItalic(bool i) { setItalic(i ? FontItalicOn : FontItalicOff); }
    150     void setSmallCaps(FontSmallCaps c) { m_smallCaps = c; }
    151     void setSmallCaps(bool c) { setSmallCaps(c ? FontSmallCapsOn : FontSmallCapsOff); }
    152     void setIsAbsoluteSize(bool s) { m_isAbsoluteSize = s; }
    153     void setWeight(FontWeight w) { m_weight = w; }
    154     void setGenericFamily(GenericFamilyType genericFamily) { m_genericFamily = genericFamily; }
    155 #if OS(MACOSX)
    156     void setUsePrinterFont(bool) { }
    157 #else
    158     void setUsePrinterFont(bool p) { m_usePrinterFont = p; }
    159 #endif
    160     void setKerning(Kerning kerning) { m_kerning = kerning; }
    161     void setCommonLigaturesState(LigaturesState commonLigaturesState) { m_commonLigaturesState = commonLigaturesState; }
    162     void setDiscretionaryLigaturesState(LigaturesState discretionaryLigaturesState) { m_discretionaryLigaturesState = discretionaryLigaturesState; }
    163     void setHistoricalLigaturesState(LigaturesState historicalLigaturesState) { m_historicalLigaturesState = historicalLigaturesState; }
    164     void setKeywordSize(unsigned s) { m_keywordSize = s; }
    165     void setFontSmoothing(FontSmoothingMode smoothing) { m_fontSmoothing = smoothing; }
    166     void setTextRenderingMode(TextRenderingMode rendering) { m_textRendering = rendering; }
    167     void setIsSpecifiedFont(bool isSpecifiedFont) { m_isSpecifiedFont = isSpecifiedFont; }
    168     void setOrientation(FontOrientation orientation) { m_orientation = orientation; }
    169     void setNonCJKGlyphOrientation(NonCJKGlyphOrientation orientation) { m_nonCJKGlyphOrientation = orientation; }
    170     void setWidthVariant(FontWidthVariant widthVariant) { m_widthVariant = widthVariant; }
    171     void setScript(UScriptCode s) { m_script = s; }
    172     void setSyntheticBold(bool syntheticBold) { m_syntheticBold = syntheticBold; }
    173     void setSyntheticItalic(bool syntheticItalic) { m_syntheticItalic = syntheticItalic; }
    174     void setFeatureSettings(PassRefPtr<FontFeatureSettings> settings) { m_featureSettings = settings; }
    175     void setTraitsMask(FontTraitsMask);
    176 
    177     static void setSubpixelPositioning(bool b) { s_useSubpixelTextPositioning = b; }
    178     static bool subpixelPositioning() { return s_useSubpixelTextPositioning; }
    179 
    180 private:
    181     FontFamily m_familyList; // The list of font families to be used.
    182     RefPtr<FontFeatureSettings> m_featureSettings;
    183 
    184     float m_specifiedSize;   // Specified CSS value. Independent of rendering issues such as integer
    185                              // rounding, minimum font sizes, and zooming.
    186     float m_computedSize;    // Computed size adjusted for the minimum font size and the zoom factor.
    187 
    188     unsigned m_orientation : 1; // FontOrientation - Whether the font is rendering on a horizontal line or a vertical line.
    189     unsigned m_nonCJKGlyphOrientation : 1; // NonCJKGlyphOrientation - Only used by vertical text. Determines the default orientation for non-ideograph glyphs.
    190 
    191     unsigned m_widthVariant : 2; // FontWidthVariant
    192 
    193     unsigned m_italic : 1; // FontItalic
    194     unsigned m_smallCaps : 1; // FontSmallCaps
    195     unsigned m_isAbsoluteSize : 1; // Whether or not CSS specified an explicit size
    196                                   // (logical sizes like "medium" don't count).
    197     unsigned m_weight : 8; // FontWeight
    198     unsigned m_genericFamily : 3; // GenericFamilyType
    199     unsigned m_usePrinterFont : 1;
    200 
    201     unsigned m_kerning : 2; // Kerning
    202 
    203     unsigned m_commonLigaturesState : 2;
    204     unsigned m_discretionaryLigaturesState : 2;
    205     unsigned m_historicalLigaturesState : 2;
    206 
    207     unsigned m_keywordSize : 4; // We cache whether or not a font is currently represented by a CSS keyword (e.g., medium).  If so,
    208                            // then we can accurately translate across different generic families to adjust for different preference settings
    209                            // (e.g., 13px monospace vs. 16px everything else).  Sizes are 1-8 (like the HTML size values for <font>).
    210 
    211     unsigned m_fontSmoothing : 2; // FontSmoothingMode
    212     unsigned m_textRendering : 2; // TextRenderingMode
    213     unsigned m_isSpecifiedFont : 1; // True if a web page specifies a non-generic font family as the first font family.
    214     unsigned m_script : 7; // Used to help choose an appropriate font for generic font families.
    215     unsigned m_syntheticBold : 1;
    216     unsigned m_syntheticItalic : 1;
    217     unsigned m_subpixelTextPosition : 1;
    218 
    219     static bool s_useSubpixelTextPositioning;
    220 };
    221 
    222 inline bool FontDescription::operator==(const FontDescription& other) const
    223 {
    224     return m_familyList == other.m_familyList
    225         && m_specifiedSize == other.m_specifiedSize
    226         && m_computedSize == other.m_computedSize
    227         && m_italic == other.m_italic
    228         && m_smallCaps == other.m_smallCaps
    229         && m_isAbsoluteSize == other.m_isAbsoluteSize
    230         && m_weight == other.m_weight
    231         && m_genericFamily == other.m_genericFamily
    232         && m_usePrinterFont == other.m_usePrinterFont
    233         && m_kerning == other.m_kerning
    234         && m_commonLigaturesState == other.m_commonLigaturesState
    235         && m_discretionaryLigaturesState == other.m_discretionaryLigaturesState
    236         && m_historicalLigaturesState == other.m_historicalLigaturesState
    237         && m_keywordSize == other.m_keywordSize
    238         && m_fontSmoothing == other.m_fontSmoothing
    239         && m_textRendering == other.m_textRendering
    240         && m_isSpecifiedFont == other.m_isSpecifiedFont
    241         && m_orientation == other.m_orientation
    242         && m_nonCJKGlyphOrientation == other.m_nonCJKGlyphOrientation
    243         && m_widthVariant == other.m_widthVariant
    244         && m_script == other.m_script
    245         && m_syntheticBold == other.m_syntheticBold
    246         && m_syntheticItalic == other.m_syntheticItalic
    247         && m_featureSettings == other.m_featureSettings
    248         && m_subpixelTextPosition == other.m_subpixelTextPosition;
    249 }
    250 
    251 }
    252 
    253 #endif
    254