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/platform/graphics/Font.h" 25 #include "core/rendering/style/SVGRenderStyle.h" 26 #include "core/svg/SVGLengthContext.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 #else 33 #include "wtf/UnusedParam.h" 34 #endif 35 36 namespace WebCore { 37 38 SVGTextLayoutEngineSpacing::SVGTextLayoutEngineSpacing(const Font& font) 39 : m_font(font) 40 , m_lastCharacter(0) 41 { 42 } 43 44 float SVGTextLayoutEngineSpacing::calculateSVGKerning(bool isVerticalText, const SVGTextMetrics::Glyph& currentGlyph) 45 { 46 #if ENABLE(SVG_FONTS) 47 const SimpleFontData* fontData = m_font.primaryFont(); 48 if (!fontData->isSVGFont()) { 49 m_lastGlyph.isValid = false; 50 return 0; 51 } 52 53 ASSERT(fontData->isCustomFont()); 54 ASSERT(fontData->isSVGFont()); 55 56 const SVGFontData* svgFontData = static_cast<const SVGFontData*>(fontData->fontData()); 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.size() / m_font.fontMetrics().unitsPerEm(); 77 return kerning; 78 #else 79 UNUSED_PARAM(isVerticalText); 80 UNUSED_PARAM(currentGlyph); 81 return false; 82 #endif 83 } 84 85 float SVGTextLayoutEngineSpacing::calculateCSSKerningAndSpacing(const SVGRenderStyle* style, SVGElement* contextElement, UChar currentCharacter) 86 { 87 float kerning = 0; 88 SVGLength kerningLength = style->kerning(); 89 if (kerningLength.unitType() == LengthTypePercentage) 90 kerning = kerningLength.valueAsPercentage() * m_font.pixelSize(); 91 else { 92 SVGLengthContext lengthContext(contextElement); 93 kerning = kerningLength.value(lengthContext); 94 } 95 96 UChar lastCharacter = m_lastCharacter; 97 m_lastCharacter = currentCharacter; 98 99 if (!kerning && !m_font.letterSpacing() && !m_font.wordSpacing()) 100 return 0; 101 102 float spacing = m_font.letterSpacing() + kerning; 103 if (currentCharacter && lastCharacter && m_font.wordSpacing()) { 104 if (Font::treatAsSpace(currentCharacter) && !Font::treatAsSpace(lastCharacter)) 105 spacing += m_font.wordSpacing(); 106 } 107 108 return spacing; 109 } 110 111 } 112