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 #include "core/rendering/svg/SVGTextLayoutEngineSpacing.h"
     23 
     24 #include "core/rendering/style/SVGRenderStyle.h"
     25 #include "core/svg/SVGLengthContext.h"
     26 #include "platform/fonts/Font.h"
     27 
     28 #if ENABLE(SVG_FONTS)
     29 #include "core/svg/SVGFontData.h"
     30 #include "core/svg/SVGFontElement.h"
     31 #include "core/svg/SVGFontFaceElement.h"
     32 #endif
     33 
     34 namespace WebCore {
     35 
     36 SVGTextLayoutEngineSpacing::SVGTextLayoutEngineSpacing(const Font& font)
     37     : m_font(font)
     38     , m_lastCharacter(0)
     39 {
     40 }
     41 
     42 float SVGTextLayoutEngineSpacing::calculateSVGKerning(bool isVerticalText, const SVGTextMetrics::Glyph& currentGlyph)
     43 {
     44 #if ENABLE(SVG_FONTS)
     45     const SimpleFontData* fontData = m_font.primaryFont();
     46     if (!fontData->isSVGFont()) {
     47         m_lastGlyph.isValid = false;
     48         return 0;
     49     }
     50 
     51     ASSERT(fontData->isCustomFont());
     52     ASSERT(fontData->isSVGFont());
     53 
     54     RefPtr<CustomFontData> customFontData = fontData->customFontData();
     55     const SVGFontData* svgFontData = static_cast<const SVGFontData*>(customFontData.get());
     56     SVGFontFaceElement* svgFontFace = svgFontData->svgFontFaceElement();
     57     ASSERT(svgFontFace);
     58 
     59     SVGFontElement* svgFont = svgFontFace->associatedFontElement();
     60     if (!svgFont) {
     61         m_lastGlyph.isValid = false;
     62         return 0;
     63     }
     64 
     65     float kerning = 0;
     66     if (m_lastGlyph.isValid) {
     67         if (isVerticalText)
     68             kerning = svgFont->verticalKerningForPairOfStringsAndGlyphs(m_lastGlyph.unicodeString, m_lastGlyph.name, currentGlyph.unicodeString, currentGlyph.name);
     69         else
     70             kerning = svgFont->horizontalKerningForPairOfStringsAndGlyphs(m_lastGlyph.unicodeString, m_lastGlyph.name, currentGlyph.unicodeString, currentGlyph.name);
     71     }
     72 
     73     m_lastGlyph = currentGlyph;
     74     m_lastGlyph.isValid = true;
     75     kerning *= m_font.size() / m_font.fontMetrics().unitsPerEm();
     76     return kerning;
     77 #else
     78     return false;
     79 #endif
     80 }
     81 
     82 float SVGTextLayoutEngineSpacing::calculateCSSKerningAndSpacing(const SVGRenderStyle* style, SVGElement* contextElement, UChar currentCharacter)
     83 {
     84     float kerning = 0;
     85     SVGLength kerningLength = style->kerning();
     86     if (kerningLength.unitType() == LengthTypePercentage)
     87         kerning = kerningLength.valueAsPercentage() * m_font.pixelSize();
     88     else {
     89         SVGLengthContext lengthContext(contextElement);
     90         kerning = kerningLength.value(lengthContext);
     91     }
     92 
     93     UChar lastCharacter = m_lastCharacter;
     94     m_lastCharacter = currentCharacter;
     95 
     96     if (!kerning && !m_font.letterSpacing() && !m_font.wordSpacing())
     97         return 0;
     98 
     99     float spacing = m_font.letterSpacing() + kerning;
    100     if (currentCharacter && lastCharacter && m_font.wordSpacing()) {
    101         if (Font::treatAsSpace(currentCharacter) && !Font::treatAsSpace(lastCharacter))
    102             spacing += m_font.wordSpacing();
    103     }
    104 
    105     return spacing;
    106 }
    107 
    108 }
    109