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/svg/SVGLengthContext.h"
25#include "platform/fonts/Character.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
34namespace blink {
35
36SVGTextLayoutEngineSpacing::SVGTextLayoutEngineSpacing(const Font& font, float effectiveZoom)
37    : m_font(font)
38    , m_lastCharacter(0)
39    , m_effectiveZoom(effectiveZoom)
40#if ENABLE(SVG_FONTS)
41    , m_lastGlyph(0)
42#endif
43{
44    ASSERT(m_effectiveZoom);
45}
46
47float SVGTextLayoutEngineSpacing::calculateSVGKerning(bool isVerticalText, Glyph currentGlyph)
48{
49#if ENABLE(SVG_FONTS)
50    const SimpleFontData* fontData = m_font.primaryFont();
51    if (!fontData->isSVGFont()) {
52        m_lastGlyph = 0;
53        return 0;
54    }
55
56    ASSERT(fontData->isCustomFont());
57    ASSERT(fontData->isSVGFont());
58
59    RefPtr<CustomFontData> customFontData = fontData->customFontData();
60    SVGFontFaceElement* svgFontFace = toSVGFontData(customFontData)->svgFontFaceElement();
61    ASSERT(svgFontFace);
62
63    SVGFontElement* svgFont = svgFontFace->associatedFontElement();
64    if (!svgFont) {
65        m_lastGlyph = 0;
66        return 0;
67    }
68
69    float kerning = 0;
70    if (m_lastGlyph) {
71        if (isVerticalText)
72            kerning = svgFont->verticalKerningForPairOfGlyphs(m_lastGlyph, currentGlyph);
73        else
74            kerning = svgFont->horizontalKerningForPairOfGlyphs(m_lastGlyph, currentGlyph);
75
76        kerning *= m_font.fontDescription().computedSize() / m_font.fontMetrics().unitsPerEm();
77    }
78
79    m_lastGlyph = currentGlyph;
80    return kerning;
81#else
82    return 0;
83#endif
84}
85
86float SVGTextLayoutEngineSpacing::calculateCSSSpacing(UChar currentCharacter)
87{
88    UChar lastCharacter = m_lastCharacter;
89    m_lastCharacter = currentCharacter;
90
91    if (!m_font.fontDescription().letterSpacing() && !m_font.fontDescription().wordSpacing())
92        return 0;
93
94    float spacing = m_font.fontDescription().letterSpacing();
95    if (currentCharacter && lastCharacter && m_font.fontDescription().wordSpacing()) {
96        if (Character::treatAsSpace(currentCharacter) && !Character::treatAsSpace(lastCharacter))
97            spacing += m_font.fontDescription().wordSpacing();
98    }
99
100    if (m_effectiveZoom != 1)
101        spacing = spacing / m_effectiveZoom;
102
103    return spacing;
104}
105
106}
107