Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / FontMetrics.h
1 /*
2  * Copyright (C) Research In Motion Limited 2010-2011. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifndef FontMetrics_h
21 #define FontMetrics_h
22
23 #include "platform/fonts/FontBaseline.h"
24 #include "wtf/MathExtras.h"
25
26 namespace WebCore {
27
28 const unsigned gDefaultUnitsPerEm = 1000;
29
30 class FontMetrics {
31 public:
32     FontMetrics()
33         : m_unitsPerEm(gDefaultUnitsPerEm)
34         , m_ascent(0)
35         , m_descent(0)
36         , m_lineGap(0)
37         , m_lineSpacing(0)
38         , m_xHeight(0)
39         , m_zeroWidth(0)
40         , m_underlinethickness(0)
41         , m_hasXHeight(false)
42         , m_hasZeroWidth(false)
43     {
44     }
45
46     unsigned unitsPerEm() const { return m_unitsPerEm; }
47     void setUnitsPerEm(unsigned unitsPerEm) { m_unitsPerEm = unitsPerEm; }
48
49     float floatAscent(FontBaseline baselineType = AlphabeticBaseline) const
50     {
51         if (baselineType == AlphabeticBaseline)
52             return m_ascent;
53         return floatHeight() / 2;
54     }
55
56     void setAscent(float ascent) { m_ascent = ascent; }
57
58     float floatDescent(FontBaseline baselineType = AlphabeticBaseline) const
59     {
60         if (baselineType == AlphabeticBaseline)
61             return m_descent;
62         return floatHeight() / 2;
63     }
64
65     void setDescent(float descent) { m_descent = descent; }
66
67     float floatHeight(FontBaseline baselineType = AlphabeticBaseline) const
68     {
69         return floatAscent(baselineType) + floatDescent(baselineType);
70     }
71
72     float floatLineGap() const { return m_lineGap; }
73     void setLineGap(float lineGap) { m_lineGap = lineGap; }
74
75     float floatLineSpacing() const { return m_lineSpacing; }
76     void setLineSpacing(float lineSpacing) { m_lineSpacing = lineSpacing; }
77
78     float xHeight() const { return m_xHeight; }
79     void setXHeight(float xHeight)
80     {
81         m_xHeight = xHeight;
82         m_hasXHeight = true;
83     }
84
85     bool hasXHeight() const { return m_hasXHeight && m_xHeight > 0; }
86     void setHasXHeight(bool hasXHeight) { m_hasXHeight = hasXHeight; }
87
88     // Integer variants of certain metrics, used for HTML rendering.
89     int ascent(FontBaseline baselineType = AlphabeticBaseline) const
90     {
91         if (baselineType == AlphabeticBaseline)
92             return lroundf(m_ascent);
93         return height() - height() / 2;
94     }
95
96     int descent(FontBaseline baselineType = AlphabeticBaseline) const
97     {
98         if (baselineType == AlphabeticBaseline)
99             return lroundf(m_descent);
100         return height() / 2;
101     }
102
103     int height(FontBaseline baselineType = AlphabeticBaseline) const
104     {
105         return ascent(baselineType) + descent(baselineType);
106     }
107
108     int lineGap() const { return lroundf(m_lineGap); }
109     int lineSpacing() const { return lroundf(m_lineSpacing); }
110
111     bool hasIdenticalAscentDescentAndLineGap(const FontMetrics& other) const
112     {
113         return ascent() == other.ascent() && descent() == other.descent() && lineGap() == other.lineGap();
114     }
115
116     float zeroWidth() const { return m_zeroWidth; }
117     void setZeroWidth(float zeroWidth)
118     {
119         m_zeroWidth = zeroWidth;
120         m_hasZeroWidth = true;
121     }
122
123     bool hasZeroWidth() const { return m_hasZeroWidth; }
124     void setHasZeroWidth(bool hasZeroWidth) { m_hasZeroWidth = hasZeroWidth; }
125
126     float underlineThickness() const { return m_underlinethickness; }
127     void setUnderlineThickness(float underlineThickness) { m_underlinethickness = underlineThickness; }
128
129 private:
130     friend class SimpleFontData;
131
132     void reset()
133     {
134         m_unitsPerEm = gDefaultUnitsPerEm;
135         m_ascent = 0;
136         m_descent = 0;
137         m_lineGap = 0;
138         m_lineSpacing = 0;
139         m_xHeight = 0;
140         m_hasXHeight = false;
141         m_underlinethickness = 0;
142     }
143
144     unsigned m_unitsPerEm;
145     float m_ascent;
146     float m_descent;
147     float m_lineGap;
148     float m_lineSpacing;
149     float m_xHeight;
150     float m_zeroWidth;
151     float m_underlinethickness;
152     bool m_hasXHeight;
153     bool m_hasZeroWidth;
154 };
155
156 inline float scaleEmToUnits(float x, unsigned unitsPerEm)
157 {
158     return unitsPerEm ? x / unitsPerEm : x;
159 }
160
161 } // namespace WebCore
162
163 #endif // FontMetrics_h