2 * Copyright (C) Research In Motion Limited 2010-2011. All rights reserved.
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.
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.
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.
23 #include "platform/fonts/FontBaseline.h"
24 #include "wtf/MathExtras.h"
28 const unsigned gDefaultUnitsPerEm = 1000;
33 : m_unitsPerEm(gDefaultUnitsPerEm)
40 , m_underlinethickness(0)
41 , m_underlinePosition(0)
43 , m_hasZeroWidth(false)
47 unsigned unitsPerEm() const { return m_unitsPerEm; }
48 void setUnitsPerEm(unsigned unitsPerEm) { m_unitsPerEm = unitsPerEm; }
50 float floatAscent(FontBaseline baselineType = AlphabeticBaseline) const
52 if (baselineType == AlphabeticBaseline)
54 return floatHeight() / 2;
57 void setAscent(float ascent) { m_ascent = ascent; }
59 float floatDescent(FontBaseline baselineType = AlphabeticBaseline) const
61 if (baselineType == AlphabeticBaseline)
63 return floatHeight() / 2;
66 void setDescent(float descent) { m_descent = descent; }
68 float floatHeight(FontBaseline baselineType = AlphabeticBaseline) const
70 return floatAscent(baselineType) + floatDescent(baselineType);
73 float floatLineGap() const { return m_lineGap; }
74 void setLineGap(float lineGap) { m_lineGap = lineGap; }
76 float floatLineSpacing() const { return m_lineSpacing; }
77 void setLineSpacing(float lineSpacing) { m_lineSpacing = lineSpacing; }
79 float xHeight() const { return m_xHeight; }
80 void setXHeight(float xHeight)
86 bool hasXHeight() const { return m_hasXHeight && m_xHeight > 0; }
87 void setHasXHeight(bool hasXHeight) { m_hasXHeight = hasXHeight; }
89 // Integer variants of certain metrics, used for HTML rendering.
90 int ascent(FontBaseline baselineType = AlphabeticBaseline) const
92 if (baselineType == AlphabeticBaseline)
93 return lroundf(m_ascent);
94 return height() - height() / 2;
97 int descent(FontBaseline baselineType = AlphabeticBaseline) const
99 if (baselineType == AlphabeticBaseline)
100 return lroundf(m_descent);
104 int height(FontBaseline baselineType = AlphabeticBaseline) const
106 return ascent(baselineType) + descent(baselineType);
109 int lineGap() const { return lroundf(m_lineGap); }
110 int lineSpacing() const { return lroundf(m_lineSpacing); }
112 bool hasIdenticalAscentDescentAndLineGap(const FontMetrics& other) const
114 return ascent() == other.ascent() && descent() == other.descent() && lineGap() == other.lineGap();
117 float zeroWidth() const { return m_zeroWidth; }
118 void setZeroWidth(float zeroWidth)
120 m_zeroWidth = zeroWidth;
121 m_hasZeroWidth = true;
124 bool hasZeroWidth() const { return m_hasZeroWidth; }
125 void setHasZeroWidth(bool hasZeroWidth) { m_hasZeroWidth = hasZeroWidth; }
127 float underlineThickness() const { return m_underlinethickness; }
128 void setUnderlineThickness(float underlineThickness) { m_underlinethickness = underlineThickness; }
130 float underlinePosition() const { return m_underlinePosition; }
131 void setUnderlinePosition(float underlinePosition) { m_underlinePosition = underlinePosition; }
134 friend class SimpleFontData;
138 m_unitsPerEm = gDefaultUnitsPerEm;
144 m_hasXHeight = false;
145 m_underlinethickness = 0;
146 m_underlinePosition = 0;
149 unsigned m_unitsPerEm;
156 float m_underlinethickness;
157 float m_underlinePosition;
162 inline float scaleEmToUnits(float x, unsigned unitsPerEm)
164 return unitsPerEm ? x / unitsPerEm : x;
167 } // namespace WebCore
169 #endif // FontMetrics_h