2 * Copyright (C) Research In Motion Limited 2010. 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.
22 #include "core/rendering/svg/SVGTextLayoutEngineSpacing.h"
24 #include "core/rendering/style/SVGRenderStyle.h"
25 #include "core/svg/SVGLengthContext.h"
26 #include "platform/fonts/Character.h"
27 #include "platform/fonts/Font.h"
30 #include "core/svg/SVGFontData.h"
31 #include "core/svg/SVGFontElement.h"
32 #include "core/svg/SVGFontFaceElement.h"
37 SVGTextLayoutEngineSpacing::SVGTextLayoutEngineSpacing(const Font& font)
43 float SVGTextLayoutEngineSpacing::calculateSVGKerning(bool isVerticalText, const SVGTextMetrics::Glyph& currentGlyph)
46 const SimpleFontData* fontData = m_font.primaryFont();
47 if (!fontData->isSVGFont()) {
48 m_lastGlyph.isValid = false;
52 ASSERT(fontData->isCustomFont());
53 ASSERT(fontData->isSVGFont());
55 RefPtr<CustomFontData> customFontData = fontData->customFontData();
56 const SVGFontData* svgFontData = static_cast<const SVGFontData*>(customFontData.get());
57 SVGFontFaceElement* svgFontFace = svgFontData->svgFontFaceElement();
60 SVGFontElement* svgFont = svgFontFace->associatedFontElement();
62 m_lastGlyph.isValid = false;
67 if (m_lastGlyph.isValid) {
69 kerning = svgFont->verticalKerningForPairOfStringsAndGlyphs(m_lastGlyph.unicodeString, m_lastGlyph.name, currentGlyph.unicodeString, currentGlyph.name);
71 kerning = svgFont->horizontalKerningForPairOfStringsAndGlyphs(m_lastGlyph.unicodeString, m_lastGlyph.name, currentGlyph.unicodeString, currentGlyph.name);
74 m_lastGlyph = currentGlyph;
75 m_lastGlyph.isValid = true;
76 kerning *= m_font.fontDescription().computedSize() / m_font.fontMetrics().unitsPerEm();
83 float SVGTextLayoutEngineSpacing::calculateCSSKerningAndSpacing(const SVGRenderStyle* style, SVGElement* contextElement, UChar currentCharacter)
86 RefPtr<SVGLength> kerningLength = style->kerning();
87 if (kerningLength->unitType() == LengthTypePercentage)
88 kerning = kerningLength->valueAsPercentage() * m_font.fontDescription().computedPixelSize();
90 SVGLengthContext lengthContext(contextElement);
91 kerning = kerningLength->value(lengthContext);
94 UChar lastCharacter = m_lastCharacter;
95 m_lastCharacter = currentCharacter;
97 if (!kerning && !m_font.fontDescription().letterSpacing() && !m_font.fontDescription().wordSpacing())
100 float spacing = m_font.fontDescription().letterSpacing() + kerning;
101 if (currentCharacter && lastCharacter && m_font.fontDescription().wordSpacing()) {
102 if (Character::treatAsSpace(currentCharacter) && !Character::treatAsSpace(lastCharacter))
103 spacing += m_font.fontDescription().wordSpacing();