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