Home | History | Annotate | Download | only in graphics
      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 <wtf/MathExtras.h>
     24 
     25 namespace WebCore {
     26 
     27 const unsigned gDefaultUnitsPerEm = 1000;
     28 
     29 class FontMetrics {
     30 public:
     31     FontMetrics()
     32         : m_unitsPerEm(gDefaultUnitsPerEm)
     33         , m_ascent(0)
     34         , m_descent(0)
     35         , m_lineGap(0)
     36         , m_lineSpacing(0)
     37         , m_xHeight(0)
     38     {
     39     }
     40 
     41     unsigned unitsPerEm() const { return m_unitsPerEm; }
     42     void setUnitsPerEm(unsigned unitsPerEm) { m_unitsPerEm = unitsPerEm; }
     43 
     44     float floatAscent(FontBaseline baselineType = AlphabeticBaseline) const
     45     {
     46         if (baselineType == AlphabeticBaseline)
     47             return m_ascent;
     48         return floatHeight() / 2;
     49     }
     50 
     51     void setAscent(float ascent) { m_ascent = ascent; }
     52 
     53     float floatDescent(FontBaseline baselineType = AlphabeticBaseline) const
     54     {
     55         if (baselineType == AlphabeticBaseline)
     56             return m_descent;
     57         return floatHeight() / 2;
     58     }
     59 
     60     void setDescent(float descent) { m_descent = descent; }
     61 
     62     float floatHeight(FontBaseline baselineType = AlphabeticBaseline) const
     63     {
     64         return floatAscent(baselineType) + floatDescent(baselineType);
     65     }
     66 
     67     float floatLineGap() const { return m_lineGap; }
     68     void setLineGap(float lineGap) { m_lineGap = lineGap; }
     69 
     70     float floatLineSpacing() const { return m_lineSpacing; }
     71     void setLineSpacing(float lineSpacing) { m_lineSpacing = lineSpacing; }
     72 
     73     float xHeight() const { return m_xHeight; }
     74     void setXHeight(float xHeight) { m_xHeight = xHeight; }
     75 
     76     // Integer variants of certain metrics, used for HTML rendering.
     77     int ascent(FontBaseline baselineType = AlphabeticBaseline) const
     78     {
     79         if (baselineType == AlphabeticBaseline)
     80             return lroundf(m_ascent);
     81         return height() - height() / 2;
     82     }
     83 
     84     int descent(FontBaseline baselineType = AlphabeticBaseline) const
     85     {
     86         if (baselineType == AlphabeticBaseline)
     87             return lroundf(m_descent);
     88         return height() / 2;
     89     }
     90 
     91     int height(FontBaseline baselineType = AlphabeticBaseline) const
     92     {
     93         return ascent(baselineType) + descent(baselineType);
     94     }
     95 
     96     int lineGap() const { return lroundf(m_lineGap); }
     97     int lineSpacing() const { return lroundf(m_lineSpacing); }
     98 
     99     bool hasIdenticalAscentDescentAndLineGap(const FontMetrics& other) const
    100     {
    101         return ascent() == other.ascent() && descent() == other.descent() && lineGap() == other.lineGap();
    102     }
    103 
    104 private:
    105     friend class SimpleFontData;
    106 
    107     void reset()
    108     {
    109         m_unitsPerEm = gDefaultUnitsPerEm;
    110         m_ascent = 0;
    111         m_descent = 0;
    112         m_lineGap = 0;
    113         m_lineSpacing = 0;
    114         m_xHeight = 0;
    115     }
    116 
    117     unsigned m_unitsPerEm;
    118     float m_ascent;
    119     float m_descent;
    120     float m_lineGap;
    121     float m_lineSpacing;
    122     float m_xHeight;
    123 };
    124 
    125 } // namespace WebCore
    126 
    127 #endif // FontMetrics_h
    128