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)
42 , m_hasZeroWidth(false)
46 unsigned unitsPerEm() const { return m_unitsPerEm; }
47 void setUnitsPerEm(unsigned unitsPerEm) { m_unitsPerEm = unitsPerEm; }
49 float floatAscent(FontBaseline baselineType = AlphabeticBaseline) const
51 if (baselineType == AlphabeticBaseline)
53 return floatHeight() / 2;
56 void setAscent(float ascent) { m_ascent = ascent; }
58 float floatDescent(FontBaseline baselineType = AlphabeticBaseline) const
60 if (baselineType == AlphabeticBaseline)
62 return floatHeight() / 2;
65 void setDescent(float descent) { m_descent = descent; }
67 float floatHeight(FontBaseline baselineType = AlphabeticBaseline) const
69 return floatAscent(baselineType) + floatDescent(baselineType);
72 float floatLineGap() const { return m_lineGap; }
73 void setLineGap(float lineGap) { m_lineGap = lineGap; }
75 float floatLineSpacing() const { return m_lineSpacing; }
76 void setLineSpacing(float lineSpacing) { m_lineSpacing = lineSpacing; }
78 float xHeight() const { return m_xHeight; }
79 void setXHeight(float xHeight)
85 bool hasXHeight() const { return m_hasXHeight && m_xHeight > 0; }
86 void setHasXHeight(bool hasXHeight) { m_hasXHeight = hasXHeight; }
88 // Integer variants of certain metrics, used for HTML rendering.
89 int ascent(FontBaseline baselineType = AlphabeticBaseline) const
91 if (baselineType == AlphabeticBaseline)
92 return lroundf(m_ascent);
93 return height() - height() / 2;
96 int descent(FontBaseline baselineType = AlphabeticBaseline) const
98 if (baselineType == AlphabeticBaseline)
99 return lroundf(m_descent);
103 int height(FontBaseline baselineType = AlphabeticBaseline) const
105 return ascent(baselineType) + descent(baselineType);
108 int lineGap() const { return lroundf(m_lineGap); }
109 int lineSpacing() const { return lroundf(m_lineSpacing); }
111 bool hasIdenticalAscentDescentAndLineGap(const FontMetrics& other) const
113 return ascent() == other.ascent() && descent() == other.descent() && lineGap() == other.lineGap();
116 float zeroWidth() const { return m_zeroWidth; }
117 void setZeroWidth(float zeroWidth)
119 m_zeroWidth = zeroWidth;
120 m_hasZeroWidth = true;
123 bool hasZeroWidth() const { return m_hasZeroWidth; }
124 void setHasZeroWidth(bool hasZeroWidth) { m_hasZeroWidth = hasZeroWidth; }
126 float underlineThickness() const { return m_underlinethickness; }
127 void setUnderlineThickness(float underlineThickness) { m_underlinethickness = underlineThickness; }
130 friend class SimpleFontData;
134 m_unitsPerEm = gDefaultUnitsPerEm;
140 m_hasXHeight = false;
141 m_underlinethickness = 0;
144 unsigned m_unitsPerEm;
151 float m_underlinethickness;
156 inline float scaleEmToUnits(float x, unsigned unitsPerEm)
158 return unitsPerEm ? x / unitsPerEm : x;
161 } // namespace WebCore
163 #endif // FontMetrics_h