Home | History | Annotate | Download | only in fonts
      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 "platform/fonts/FontBaseline.h"
     24 #include "wtf/MathExtras.h"
     25 
     26 namespace blink {
     27 
     28 const unsigned gDefaultUnitsPerEm = 1000;
     29 
     30 class FontMetrics {
     31 public:
     32     FontMetrics()
     33         : m_unitsPerEm(gDefaultUnitsPerEm)
     34         , m_ascent(0)
     35         , m_descent(0)
     36         , m_lineGap(0)
     37         , m_lineSpacing(0)
     38         , m_xHeight(0)
     39         , m_zeroWidth(0)
     40         , m_underlinethickness(0)
     41         , m_underlinePosition(0)
     42         , m_hasXHeight(false)
     43         , m_hasZeroWidth(false)
     44     {
     45     }
     46 
     47     unsigned unitsPerEm() const { return m_unitsPerEm; }
     48     void setUnitsPerEm(unsigned unitsPerEm) { m_unitsPerEm = unitsPerEm; }
     49 
     50     float floatAscent(FontBaseline baselineType = AlphabeticBaseline) const
     51     {
     52         if (baselineType == AlphabeticBaseline)
     53             return m_ascent;
     54         return floatHeight() / 2;
     55     }
     56 
     57     void setAscent(float ascent) { m_ascent = ascent; }
     58 
     59     float floatDescent(FontBaseline baselineType = AlphabeticBaseline) const
     60     {
     61         if (baselineType == AlphabeticBaseline)
     62             return m_descent;
     63         return floatHeight() / 2;
     64     }
     65 
     66     void setDescent(float descent) { m_descent = descent; }
     67 
     68     float floatHeight(FontBaseline baselineType = AlphabeticBaseline) const
     69     {
     70         return floatAscent(baselineType) + floatDescent(baselineType);
     71     }
     72 
     73     float floatLineGap() const { return m_lineGap; }
     74     void setLineGap(float lineGap) { m_lineGap = lineGap; }
     75 
     76     float floatLineSpacing() const { return m_lineSpacing; }
     77     void setLineSpacing(float lineSpacing) { m_lineSpacing = lineSpacing; }
     78 
     79     float xHeight() const { return m_xHeight; }
     80     void setXHeight(float xHeight)
     81     {
     82         m_xHeight = xHeight;
     83         m_hasXHeight = true;
     84     }
     85 
     86     bool hasXHeight() const { return m_hasXHeight && m_xHeight > 0; }
     87     void setHasXHeight(bool hasXHeight) { m_hasXHeight = hasXHeight; }
     88 
     89     // Integer variants of certain metrics, used for HTML rendering.
     90     int ascent(FontBaseline baselineType = AlphabeticBaseline) const
     91     {
     92         if (baselineType == AlphabeticBaseline)
     93             return lroundf(m_ascent);
     94         return height() - height() / 2;
     95     }
     96 
     97     int descent(FontBaseline baselineType = AlphabeticBaseline) const
     98     {
     99         if (baselineType == AlphabeticBaseline)
    100             return lroundf(m_descent);
    101         return height() / 2;
    102     }
    103 
    104     int height(FontBaseline baselineType = AlphabeticBaseline) const
    105     {
    106         return ascent(baselineType) + descent(baselineType);
    107     }
    108 
    109     int lineGap() const { return lroundf(m_lineGap); }
    110     int lineSpacing() const { return lroundf(m_lineSpacing); }
    111 
    112     bool hasIdenticalAscentDescentAndLineGap(const FontMetrics& other) const
    113     {
    114         return ascent() == other.ascent() && descent() == other.descent() && lineGap() == other.lineGap();
    115     }
    116 
    117     float zeroWidth() const { return m_zeroWidth; }
    118     void setZeroWidth(float zeroWidth)
    119     {
    120         m_zeroWidth = zeroWidth;
    121         m_hasZeroWidth = true;
    122     }
    123 
    124     bool hasZeroWidth() const { return m_hasZeroWidth; }
    125     void setHasZeroWidth(bool hasZeroWidth) { m_hasZeroWidth = hasZeroWidth; }
    126 
    127     float underlineThickness() const { return m_underlinethickness; }
    128     void setUnderlineThickness(float underlineThickness) { m_underlinethickness = underlineThickness; }
    129 
    130     float underlinePosition() const { return m_underlinePosition; }
    131     void setUnderlinePosition(float underlinePosition) { m_underlinePosition = underlinePosition; }
    132 
    133 private:
    134     friend class SimpleFontData;
    135 
    136     void reset()
    137     {
    138         m_unitsPerEm = gDefaultUnitsPerEm;
    139         m_ascent = 0;
    140         m_descent = 0;
    141         m_lineGap = 0;
    142         m_lineSpacing = 0;
    143         m_xHeight = 0;
    144         m_hasXHeight = false;
    145         m_underlinethickness = 0;
    146         m_underlinePosition = 0;
    147     }
    148 
    149     unsigned m_unitsPerEm;
    150     float m_ascent;
    151     float m_descent;
    152     float m_lineGap;
    153     float m_lineSpacing;
    154     float m_xHeight;
    155     float m_zeroWidth;
    156     float m_underlinethickness;
    157     float m_underlinePosition;
    158     bool m_hasXHeight;
    159     bool m_hasZeroWidth;
    160 };
    161 
    162 inline float scaleEmToUnits(float x, unsigned unitsPerEm)
    163 {
    164     return unitsPerEm ? x / unitsPerEm : x;
    165 }
    166 
    167 } // namespace blink
    168 
    169 #endif // FontMetrics_h
    170