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