SVGTextLayoutEngineSpacing.cpp [plain text]
#include "config.h"
#include "SVGTextLayoutEngineSpacing.h"
#include "FontCascade.h"
#include "SVGLengthContext.h"
#include "SVGRenderStyle.h"
#if ENABLE(SVG_FONTS)
#include "SVGFontData.h"
#include "SVGFontElement.h"
#include "SVGFontFaceElement.h"
#endif
namespace WebCore {
SVGTextLayoutEngineSpacing::SVGTextLayoutEngineSpacing(const FontCascade& font)
: m_font(font)
, m_lastCharacter(0)
{
}
float SVGTextLayoutEngineSpacing::calculateSVGKerning(bool isVerticalText, const SVGTextMetrics::Glyph& currentGlyph)
{
#if ENABLE(SVG_FONTS)
const Font& font = m_font.primaryFont();
if (!font.isSVGFont()) {
m_lastGlyph.isValid = false;
return 0;
}
ASSERT(font.isCustomFont());
ASSERT(font.isSVGFont());
auto* svgFontData = static_cast<const SVGFontData*>(font.svgData());
SVGFontFaceElement* svgFontFace = svgFontData->svgFontFaceElement();
ASSERT(svgFontFace);
SVGFontElement* svgFont = svgFontFace->associatedFontElement();
if (!svgFont) {
m_lastGlyph.isValid = false;
return 0;
}
float kerning = 0;
if (m_lastGlyph.isValid) {
if (isVerticalText)
kerning = svgFont->verticalKerningForPairOfStringsAndGlyphs(m_lastGlyph.unicodeString, m_lastGlyph.name, currentGlyph.unicodeString, currentGlyph.name);
else
kerning = svgFont->horizontalKerningForPairOfStringsAndGlyphs(m_lastGlyph.unicodeString, m_lastGlyph.name, currentGlyph.unicodeString, currentGlyph.name);
}
m_lastGlyph = currentGlyph;
m_lastGlyph.isValid = true;
kerning *= m_font.size() / m_font.fontMetrics().unitsPerEm();
return kerning;
#else
UNUSED_PARAM(isVerticalText);
UNUSED_PARAM(currentGlyph);
return false;
#endif
}
float SVGTextLayoutEngineSpacing::calculateCSSKerningAndSpacing(const SVGRenderStyle* style, SVGElement* contextElement, const UChar* currentCharacter)
{
float kerning = 0;
SVGLength kerningLength = style->kerning();
if (kerningLength.unitType() == LengthTypePercentage)
kerning = kerningLength.valueAsPercentage() * m_font.pixelSize();
else {
SVGLengthContext lengthContext(contextElement);
kerning = kerningLength.value(lengthContext);
}
const UChar* lastCharacter = m_lastCharacter;
m_lastCharacter = currentCharacter;
if (!kerning && !m_font.letterSpacing() && !m_font.wordSpacing())
return 0;
float spacing = m_font.letterSpacing() + kerning;
if (currentCharacter && lastCharacter && m_font.wordSpacing()) {
if (FontCascade::treatAsSpace(*currentCharacter) && !FontCascade::treatAsSpace(*lastCharacter))
spacing += m_font.wordSpacing();
}
return spacing;
}
}