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 "FontBaseline.h"
24 #include <wtf/MathExtras.h>
28 const unsigned gDefaultUnitsPerEm = 1000;
33 : m_unitsPerEm(gDefaultUnitsPerEm)
43 unsigned unitsPerEm() const { return m_unitsPerEm; }
44 void setUnitsPerEm(unsigned unitsPerEm) { m_unitsPerEm = unitsPerEm; }
46 float floatAscent(FontBaseline baselineType = AlphabeticBaseline) const
48 if (baselineType == AlphabeticBaseline)
50 return floatHeight() / 2;
53 void setAscent(float ascent) { m_ascent = ascent; }
55 float floatDescent(FontBaseline baselineType = AlphabeticBaseline) const
57 if (baselineType == AlphabeticBaseline)
59 return floatHeight() / 2;
62 void setDescent(float descent) { m_descent = descent; }
64 float floatHeight(FontBaseline baselineType = AlphabeticBaseline) const
66 return floatAscent(baselineType) + floatDescent(baselineType);
69 float floatLineGap() const { return m_lineGap; }
70 void setLineGap(float lineGap) { m_lineGap = lineGap; }
72 float floatLineSpacing() const { return m_lineSpacing; }
73 void setLineSpacing(float lineSpacing) { m_lineSpacing = lineSpacing; }
75 float xHeight() const { return m_xHeight; }
76 void setXHeight(float xHeight)
82 bool hasXHeight() const { return m_hasXHeight && m_xHeight > 0; }
83 void setHasXHeight(bool hasXHeight) { m_hasXHeight = hasXHeight; }
85 // Integer variants of certain metrics, used for HTML rendering.
86 int ascent(FontBaseline baselineType = AlphabeticBaseline) const
88 if (baselineType == AlphabeticBaseline)
89 return lroundf(m_ascent);
90 return height() - height() / 2;
93 int descent(FontBaseline baselineType = AlphabeticBaseline) const
95 if (baselineType == AlphabeticBaseline)
96 return lroundf(m_descent);
100 int height(FontBaseline baselineType = AlphabeticBaseline) const
102 return ascent(baselineType) + descent(baselineType);
105 int lineGap() const { return lroundf(m_lineGap); }
106 int lineSpacing() const { return lroundf(m_lineSpacing); }
108 bool hasIdenticalAscentDescentAndLineGap(const FontMetrics& other) const
110 return ascent() == other.ascent() && descent() == other.descent() && lineGap() == other.lineGap();
114 friend class SimpleFontData;
118 m_unitsPerEm = gDefaultUnitsPerEm;
124 m_hasXHeight = false;
127 unsigned m_unitsPerEm;
136 static inline float scaleEmToUnits(float x, unsigned unitsPerEm)
138 return unitsPerEm ? x / unitsPerEm : x;
141 } // namespace WebCore
143 #endif // FontMetrics_h