SVGTextMetrics.cpp [plain text]
#include "config.h"
#if ENABLE(SVG)
#include "SVGTextMetrics.h"
#include "RenderSVGInlineText.h"
#include "SVGTextRunRenderingContext.h"
#include "WidthIterator.h"
namespace WebCore {
SVGTextMetrics::SVGTextMetrics()
: m_width(0)
, m_height(0)
, m_length(0)
{
}
SVGTextMetrics::SVGTextMetrics(SVGTextMetrics::MetricsType)
: m_width(0)
, m_height(0)
, m_length(1)
{
}
SVGTextMetrics::SVGTextMetrics(RenderSVGInlineText* textRenderer, const TextRun& run)
{
ASSERT(textRenderer);
float scalingFactor = textRenderer->scalingFactor();
ASSERT(scalingFactor);
const Font& scaledFont = textRenderer->scaledFont();
int length = 0;
m_width = scaledFont.width(run, length, m_glyph.name) / scalingFactor;
m_height = scaledFont.fontMetrics().floatHeight() / scalingFactor;
m_glyph.unicodeString = String(run.characters(), length);
m_glyph.isValid = true;
ASSERT(length >= 0);
m_length = static_cast<unsigned>(length);
}
TextRun SVGTextMetrics::constructTextRun(RenderSVGInlineText* text, const UChar* characters, unsigned position, unsigned length)
{
RenderStyle* style = text->style();
ASSERT(style);
TextRun run(characters + position
, length
, false
, 0
, 0
, TextRun::AllowTrailingExpansion
, style->direction()
, isOverride(style->unicodeBidi()) );
if (textRunNeedsRenderingContext(style->font()))
run.setRenderingContext(SVGTextRunRenderingContext::create(text));
run.disableRoundingHacks();
run.disableRoundingHacks();
run.disableSpacing();
run.setCharactersLength(text->textLength() - position);
ASSERT(run.charactersLength() >= run.length());
return run;
}
SVGTextMetrics SVGTextMetrics::measureCharacterRange(RenderSVGInlineText* text, unsigned position, unsigned length)
{
ASSERT(text);
return SVGTextMetrics(text, constructTextRun(text, text->characters(), position, length));
}
SVGTextMetrics::SVGTextMetrics(RenderSVGInlineText* text, unsigned position, unsigned length, float width, const String& glyphName)
{
ASSERT(text);
bool needsContext = textRunNeedsRenderingContext(text->style()->font());
float scalingFactor = text->scalingFactor();
ASSERT(scalingFactor);
m_width = width / scalingFactor;
m_height = text->scaledFont().fontMetrics().floatHeight() / scalingFactor;
if (needsContext) {
m_glyph.isValid = true;
m_glyph.unicodeString = String(text->characters() + position, length);
m_glyph.name = glyphName;
}
m_length = length;
}
}
#endif // ENABLE(SVG)