Home | History | Annotate | Download | only in svg
      1 /*
      2  * Copyright (C) Research In Motion Limited 2010. 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 #include "config.h"
     21 
     22 #if ENABLE(SVG)
     23 #include "SVGTextLayoutEngineSpacing.h"
     24 
     25 #include "Font.h"
     26 #include "SVGRenderStyle.h"
     27 
     28 #if ENABLE(SVG_FONTS)
     29 #include "SVGFontElement.h"
     30 #endif
     31 
     32 namespace WebCore {
     33 
     34 SVGTextLayoutEngineSpacing::SVGTextLayoutEngineSpacing(const Font& font)
     35     : m_font(font)
     36     , m_lastCharacter(0)
     37 {
     38 }
     39 
     40 float SVGTextLayoutEngineSpacing::calculateSVGKerning(bool isVerticalText, const SVGTextMetrics::Glyph& currentGlyph)
     41 {
     42 #if ENABLE(SVG_FONTS)
     43     if (!m_font.isSVGFont()) {
     44         m_lastGlyph.isValid = false;
     45         return 0;
     46     }
     47 
     48     SVGFontElement* svgFont = m_font.svgFont();
     49     ASSERT(svgFont);
     50 
     51     float kerning = 0;
     52     if (m_lastGlyph.isValid) {
     53         if (isVerticalText)
     54             kerning = svgFont->verticalKerningForPairOfStringsAndGlyphs(m_lastGlyph.unicodeString, m_lastGlyph.name, currentGlyph.unicodeString, currentGlyph.name);
     55         else
     56             kerning = svgFont->horizontalKerningForPairOfStringsAndGlyphs(m_lastGlyph.unicodeString, m_lastGlyph.name, currentGlyph.unicodeString, currentGlyph.name);
     57     }
     58 
     59     m_lastGlyph = currentGlyph;
     60     m_lastGlyph.isValid = true;
     61     kerning *= m_font.size() / m_font.fontMetrics().unitsPerEm();
     62     return kerning;
     63 #else
     64     UNUSED_PARAM(isVerticalText);
     65     UNUSED_PARAM(currentGlyph);
     66     return false;
     67 #endif
     68 }
     69 
     70 float SVGTextLayoutEngineSpacing::calculateCSSKerningAndSpacing(const SVGRenderStyle* style, SVGElement* lengthContext, const UChar* currentCharacter)
     71 {
     72     float kerning = 0;
     73     SVGLength kerningLength = style->kerning();
     74     if (kerningLength.unitType() == LengthTypePercentage)
     75         kerning = kerningLength.valueAsPercentage() * m_font.pixelSize();
     76     else
     77         kerning = kerningLength.value(lengthContext);
     78 
     79     const UChar* lastCharacter = m_lastCharacter;
     80     m_lastCharacter = currentCharacter;
     81 
     82     if (!kerning && !m_font.letterSpacing() && !m_font.wordSpacing())
     83         return 0;
     84 
     85     float spacing = m_font.letterSpacing() + kerning;
     86     if (currentCharacter && lastCharacter && m_font.wordSpacing()) {
     87         if (Font::treatAsSpace(*currentCharacter) && !Font::treatAsSpace(*lastCharacter))
     88             spacing += m_font.wordSpacing();
     89     }
     90 
     91     return spacing;
     92 }
     93 
     94 }
     95 
     96 #endif // ENABLE(SVG)
     97