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 "FontBaseline.h"
24#include <wtf/MathExtras.h>
25
26namespace WebCore {
27
28class FontMetrics {
29public:
30 static const unsigned defaultUnitsPerEm = 1000;
31
32 unsigned unitsPerEm() const { return m_unitsPerEm; }
33 void setUnitsPerEm(unsigned unitsPerEm) { m_unitsPerEm = unitsPerEm; }
34
35 float floatAscent(FontBaseline baselineType = AlphabeticBaseline) const
36 {
37 if (baselineType == AlphabeticBaseline)
38 return m_ascent;
39 return floatHeight() / 2;
40 }
41
42 void setAscent(float ascent) { m_ascent = ascent; }
43
44 float floatDescent(FontBaseline baselineType = AlphabeticBaseline) const
45 {
46 if (baselineType == AlphabeticBaseline)
47 return m_descent;
48 return floatHeight() / 2;
49 }
50
51 void setDescent(float descent) { m_descent = descent; }
52
53 float floatHeight(FontBaseline baselineType = AlphabeticBaseline) const
54 {
55 return floatAscent(baselineType) + floatDescent(baselineType);
56 }
57
58 float floatLineGap() const { return m_lineGap; }
59 void setLineGap(float lineGap) { m_lineGap = lineGap; }
60
61 float floatLineSpacing() const { return m_lineSpacing; }
62 void setLineSpacing(float lineSpacing) { m_lineSpacing = lineSpacing; }
63
64 float xHeight() const { return m_xHeight; }
65 void setXHeight(float xHeight) { m_xHeight = xHeight; }
66 bool hasXHeight() const { return m_xHeight > 0; }
67
68 bool hasCapHeight() const { return m_capHeight > 0; }
69 float floatCapHeight() const { return m_capHeight; }
70 void setCapHeight(float capHeight) { m_capHeight = capHeight; }
71
72 // Integer variants of certain metrics, used for HTML rendering.
73 int ascent(FontBaseline baselineType = AlphabeticBaseline) const
74 {
75 if (baselineType == AlphabeticBaseline)
76 return lroundf(m_ascent);
77 return height() - height() / 2;
78 }
79
80 int descent(FontBaseline baselineType = AlphabeticBaseline) const
81 {
82 if (baselineType == AlphabeticBaseline)
83 return lroundf(m_descent);
84 return height() / 2;
85 }
86
87 int height(FontBaseline baselineType = AlphabeticBaseline) const
88 {
89 return ascent(baselineType) + descent(baselineType);
90 }
91
92 int lineGap() const { return lroundf(m_lineGap); }
93 int lineSpacing() const { return lroundf(m_lineSpacing); }
94
95 int capHeight() const { return lroundf(m_capHeight); }
96
97 bool hasIdenticalAscentDescentAndLineGap(const FontMetrics& other) const
98 {
99 return ascent() == other.ascent() && descent() == other.descent() && lineGap() == other.lineGap();
100 }
101
102 float zeroWidth() const { return m_zeroWidth; }
103 void setZeroWidth(float zeroWidth) { m_zeroWidth = zeroWidth; }
104
105 float underlinePosition() const { return m_underlinePosition; }
106 void setUnderlinePosition(float underlinePosition) { m_underlinePosition = underlinePosition; }
107
108 float underlineThickness() const { return m_underlineThickness; }
109 void setUnderlineThickness(float underlineThickness) { m_underlineThickness = underlineThickness; }
110
111private:
112 friend class Font;
113
114 void reset()
115 {
116 m_unitsPerEm = defaultUnitsPerEm;
117 m_ascent = 0;
118 m_descent = 0;
119 m_lineGap = 0;
120 m_lineSpacing = 0;
121 m_xHeight = 0;
122 m_capHeight = 0;
123 m_zeroWidth = 0;
124 }
125
126 unsigned m_unitsPerEm { defaultUnitsPerEm };
127 float m_ascent { 0 };
128 float m_descent { 0 };
129 float m_lineGap { 0 };
130 float m_lineSpacing { 0 };
131 float m_zeroWidth { 0 };
132 float m_xHeight { 0 };
133 float m_capHeight { 0 };
134 float m_underlinePosition { 0 };
135 float m_underlineThickness { 0 };
136};
137
138static inline float scaleEmToUnits(float x, unsigned unitsPerEm)
139{
140 return unitsPerEm ? x / unitsPerEm : x;
141}
142
143} // namespace WebCore
144
145#endif // FontMetrics_h
146