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)
41 , m_hasZeroWidth(false)
45 unsigned unitsPerEm() const { return m_unitsPerEm; }
46 void setUnitsPerEm(unsigned unitsPerEm) { m_unitsPerEm = unitsPerEm; }
48 float floatAscent(FontBaseline baselineType = AlphabeticBaseline) const
50 if (baselineType == AlphabeticBaseline)
52 return floatHeight() / 2;
55 void setAscent(float ascent) { m_ascent = ascent; }
57 float floatDescent(FontBaseline baselineType = AlphabeticBaseline) const
59 if (baselineType == AlphabeticBaseline)
61 return floatHeight() / 2;
64 void setDescent(float descent) { m_descent = descent; }
66 float floatHeight(FontBaseline baselineType = AlphabeticBaseline) const
68 return floatAscent(baselineType) + floatDescent(baselineType);
71 float floatLineGap() const { return m_lineGap; }
72 void setLineGap(float lineGap) { m_lineGap = lineGap; }
74 float floatLineSpacing() const { return m_lineSpacing; }
75 void setLineSpacing(float lineSpacing) { m_lineSpacing = lineSpacing; }
77 float xHeight() const { return m_xHeight; }
78 void setXHeight(float xHeight)
84 bool hasXHeight() const { return m_hasXHeight && m_xHeight > 0; }
85 void setHasXHeight(bool hasXHeight) { m_hasXHeight = hasXHeight; }
87 // Integer variants of certain metrics, used for HTML rendering.
88 int ascent(FontBaseline baselineType = AlphabeticBaseline) const
90 if (baselineType == AlphabeticBaseline)
91 return lroundf(m_ascent);
92 return height() - height() / 2;
95 int descent(FontBaseline baselineType = AlphabeticBaseline) const
97 if (baselineType == AlphabeticBaseline)
98 return lroundf(m_descent);
102 int height(FontBaseline baselineType = AlphabeticBaseline) const
104 return ascent(baselineType) + descent(baselineType);
107 int lineGap() const { return lroundf(m_lineGap); }
108 int lineSpacing() const { return lroundf(m_lineSpacing); }
110 bool hasIdenticalAscentDescentAndLineGap(const FontMetrics& other) const
112 return ascent() == other.ascent() && descent() == other.descent() && lineGap() == other.lineGap();
115 float zeroWidth() const { return m_zeroWidth; }
116 void setZeroWidth(float zeroWidth)
118 m_zeroWidth = zeroWidth;
119 m_hasZeroWidth = true;
122 bool hasZeroWidth() const { return m_hasZeroWidth; }
123 void setHasZeroWidth(bool hasZeroWidth) { m_hasZeroWidth = hasZeroWidth; }
126 friend class SimpleFontData;
130 m_unitsPerEm = gDefaultUnitsPerEm;
136 m_hasXHeight = false;
139 unsigned m_unitsPerEm;
150 inline float scaleEmToUnits(float x, unsigned unitsPerEm)
152 return unitsPerEm ? x / unitsPerEm : x;
155 } // namespace WebCore
157 #endif // FontMetrics_h