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/Font.h"
29 #include "core/svg/SVGFontData.h"
30 #include "core/svg/SVGFontElement.h"
31 #include "core/svg/SVGFontFaceElement.h"
36 SVGTextLayoutEngineSpacing::SVGTextLayoutEngineSpacing(const Font& font)
42 float SVGTextLayoutEngineSpacing::calculateSVGKerning(bool isVerticalText, const SVGTextMetrics::Glyph& currentGlyph)
45 const SimpleFontData* fontData = m_font.primaryFont();
46 if (!fontData->isSVGFont()) {
47 m_lastGlyph.isValid = false;
51 ASSERT(fontData->isCustomFont());
52 ASSERT(fontData->isSVGFont());
54 RefPtr<CustomFontData> customFontData = fontData->customFontData();
55 const SVGFontData* svgFontData = static_cast<const SVGFontData*>(customFontData.get());
56 SVGFontFaceElement* svgFontFace = svgFontData->svgFontFaceElement();
59 SVGFontElement* svgFont = svgFontFace->associatedFontElement();
61 m_lastGlyph.isValid = false;
66 if (m_lastGlyph.isValid) {
68 kerning = svgFont->verticalKerningForPairOfStringsAndGlyphs(m_lastGlyph.unicodeString, m_lastGlyph.name, currentGlyph.unicodeString, currentGlyph.name);
70 kerning = svgFont->horizontalKerningForPairOfStringsAndGlyphs(m_lastGlyph.unicodeString, m_lastGlyph.name, currentGlyph.unicodeString, currentGlyph.name);
73 m_lastGlyph = currentGlyph;
74 m_lastGlyph.isValid = true;
75 kerning *= m_font.size() / m_font.fontMetrics().unitsPerEm();
82 float SVGTextLayoutEngineSpacing::calculateCSSKerningAndSpacing(const SVGRenderStyle* style, SVGElement* contextElement, UChar currentCharacter)
85 RefPtr<SVGLength> kerningLength = style->kerning();
86 if (kerningLength->unitType() == LengthTypePercentage)
87 kerning = kerningLength->valueAsPercentage() * m_font.pixelSize();
89 SVGLengthContext lengthContext(contextElement);
90 kerning = kerningLength->value(lengthContext);
93 UChar lastCharacter = m_lastCharacter;
94 m_lastCharacter = currentCharacter;
96 if (!kerning && !m_font.letterSpacing() && !m_font.wordSpacing())
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();