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