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