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:
FontMetrics()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_hasXHeight(false)
41 , m_hasZeroWidth(false)
42 {
43 }
44
unitsPerEm()45 unsigned unitsPerEm() const { return m_unitsPerEm; }
setUnitsPerEm(unsigned unitsPerEm)46 void setUnitsPerEm(unsigned unitsPerEm) { m_unitsPerEm = unitsPerEm; }
47
48 float floatAscent(FontBaseline baselineType = AlphabeticBaseline) const
49 {
50 if (baselineType == AlphabeticBaseline)
51 return m_ascent;
52 return floatHeight() / 2;
53 }
54
setAscent(float ascent)55 void setAscent(float ascent) { m_ascent = ascent; }
56
57 float floatDescent(FontBaseline baselineType = AlphabeticBaseline) const
58 {
59 if (baselineType == AlphabeticBaseline)
60 return m_descent;
61 return floatHeight() / 2;
62 }
63
setDescent(float descent)64 void setDescent(float descent) { m_descent = descent; }
65
66 float floatHeight(FontBaseline baselineType = AlphabeticBaseline) const
67 {
68 return floatAscent(baselineType) + floatDescent(baselineType);
69 }
70
floatLineGap()71 float floatLineGap() const { return m_lineGap; }
setLineGap(float lineGap)72 void setLineGap(float lineGap) { m_lineGap = lineGap; }
73
floatLineSpacing()74 float floatLineSpacing() const { return m_lineSpacing; }
setLineSpacing(float lineSpacing)75 void setLineSpacing(float lineSpacing) { m_lineSpacing = lineSpacing; }
76
xHeight()77 float xHeight() const { return m_xHeight; }
setXHeight(float xHeight)78 void setXHeight(float xHeight)
79 {
80 m_xHeight = xHeight;
81 m_hasXHeight = true;
82 }
83
hasXHeight()84 bool hasXHeight() const { return m_hasXHeight && m_xHeight > 0; }
setHasXHeight(bool hasXHeight)85 void setHasXHeight(bool hasXHeight) { m_hasXHeight = hasXHeight; }
86
87 // Integer variants of certain metrics, used for HTML rendering.
88 int ascent(FontBaseline baselineType = AlphabeticBaseline) const
89 {
90 if (baselineType == AlphabeticBaseline)
91 return lroundf(m_ascent);
92 return height() - height() / 2;
93 }
94
95 int descent(FontBaseline baselineType = AlphabeticBaseline) const
96 {
97 if (baselineType == AlphabeticBaseline)
98 return lroundf(m_descent);
99 return height() / 2;
100 }
101
102 int height(FontBaseline baselineType = AlphabeticBaseline) const
103 {
104 return ascent(baselineType) + descent(baselineType);
105 }
106
lineGap()107 int lineGap() const { return lroundf(m_lineGap); }
lineSpacing()108 int lineSpacing() const { return lroundf(m_lineSpacing); }
109
hasIdenticalAscentDescentAndLineGap(const FontMetrics & other)110 bool hasIdenticalAscentDescentAndLineGap(const FontMetrics& other) const
111 {
112 return ascent() == other.ascent() && descent() == other.descent() && lineGap() == other.lineGap();
113 }
114
zeroWidth()115 float zeroWidth() const { return m_zeroWidth; }
setZeroWidth(float zeroWidth)116 void setZeroWidth(float zeroWidth)
117 {
118 m_zeroWidth = zeroWidth;
119 m_hasZeroWidth = true;
120 }
121
hasZeroWidth()122 bool hasZeroWidth() const { return m_hasZeroWidth; }
setHasZeroWidth(bool hasZeroWidth)123 void setHasZeroWidth(bool hasZeroWidth) { m_hasZeroWidth = hasZeroWidth; }
124
125 private:
126 friend class SimpleFontData;
127
reset()128 void reset()
129 {
130 m_unitsPerEm = gDefaultUnitsPerEm;
131 m_ascent = 0;
132 m_descent = 0;
133 m_lineGap = 0;
134 m_lineSpacing = 0;
135 m_xHeight = 0;
136 m_hasXHeight = false;
137 }
138
139 unsigned m_unitsPerEm;
140 float m_ascent;
141 float m_descent;
142 float m_lineGap;
143 float m_lineSpacing;
144 float m_xHeight;
145 float m_zeroWidth;
146 bool m_hasXHeight;
147 bool m_hasZeroWidth;
148 };
149
scaleEmToUnits(float x,unsigned unitsPerEm)150 inline float scaleEmToUnits(float x, unsigned unitsPerEm)
151 {
152 return unitsPerEm ? x / unitsPerEm : x;
153 }
154
155 } // namespace WebCore
156
157 #endif // FontMetrics_h
158