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