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#include "SVGTextLayoutEngineSpacing.h"
22
23#include "FontCascade.h"
24#include "SVGLengthContext.h"
25#include "SVGRenderStyle.h"
26
27#if ENABLE(SVG_FONTS)
28#include "SVGFontElement.h"
29#include "SVGFontFaceElement.h"
30#endif
31
32namespace WebCore {
33
34SVGTextLayoutEngineSpacing::SVGTextLayoutEngineSpacing(const FontCascade& font)
35 : m_font(font)
36 , m_lastCharacter(0)
37{
38}
39
40float SVGTextLayoutEngineSpacing::calculateCSSKerningAndSpacing(const SVGRenderStyle* style, SVGElement* contextElement, const UChar* currentCharacter)
41{
42 float kerning = 0;
43 auto kerningLength = style->kerning();
44 if (kerningLength.unitType() == LengthTypePercentage)
45 kerning = kerningLength.valueAsPercentage() * m_font.pixelSize();
46 else {
47 SVGLengthContext lengthContext(contextElement);
48 kerning = kerningLength.value(lengthContext);
49 }
50
51 const UChar* lastCharacter = m_lastCharacter;
52 m_lastCharacter = currentCharacter;
53
54 if (!kerning && !m_font.letterSpacing() && !m_font.wordSpacing())
55 return 0;
56
57 float spacing = m_font.letterSpacing() + kerning;
58 if (currentCharacter && lastCharacter && m_font.wordSpacing()) {
59 if (FontCascade::treatAsSpace(*currentCharacter) && !FontCascade::treatAsSpace(*lastCharacter))
60 spacing += m_font.wordSpacing();
61 }
62
63 return spacing;
64}
65
66}
67