2 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
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.
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.
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.
23 #include "SVGTextMetrics.h"
25 #include "RenderSVGInlineText.h"
26 #include "SVGTextRunRenderingContext.h"
30 SVGTextMetrics::SVGTextMetrics()
37 SVGTextMetrics::SVGTextMetrics(RenderSVGInlineText* textRenderer, const TextRun& run)
41 float scalingFactor = textRenderer->scalingFactor();
42 ASSERT(scalingFactor);
44 const Font& scaledFont = textRenderer->scaledFont();
47 // Calculate width/height using the scaled font, divide this result by the scalingFactor afterwards.
48 m_width = scaledFont.width(run, length, m_glyph.name) / scalingFactor;
49 m_height = scaledFont.fontMetrics().floatHeight() / scalingFactor;
51 m_glyph.unicodeString = String(run.characters(), length);
52 m_glyph.isValid = true;
55 m_length = static_cast<unsigned>(length);
58 bool SVGTextMetrics::operator==(const SVGTextMetrics& other)
60 return m_width == other.m_width
61 && m_height == other.m_height
62 && m_length == other.m_length
63 && m_glyph == other.m_glyph;
66 SVGTextMetrics SVGTextMetrics::emptyMetrics()
68 DEFINE_STATIC_LOCAL(SVGTextMetrics, s_emptyMetrics, ());
69 s_emptyMetrics.m_length = 1;
70 return s_emptyMetrics;
73 static TextRun constructTextRun(RenderSVGInlineText* text, const UChar* characters, unsigned position, unsigned length)
75 RenderStyle* style = text->style();
78 TextRun run(characters + position
80 , false /* allowTabs */
81 , 0 /* xPos, only relevant with allowTabs=true */
82 , 0 /* padding, only relevant for justified text, not relevant for SVG */
83 , TextRun::AllowTrailingExpansion
85 , style->unicodeBidi() == Override /* directionalOverride */);
87 if (textRunNeedsRenderingContext(style->font()))
88 run.setRenderingContext(SVGTextRunRenderingContext::create(text));
90 run.disableRoundingHacks();
92 // We handle letter & word spacing ourselves.
95 // Propagate the maximum length of the characters buffer to the TextRun, even when we're only processing a substring.
96 run.setCharactersLength(text->textLength() - position);
97 ASSERT(run.charactersLength() >= run.length());
101 SVGTextMetrics SVGTextMetrics::measureCharacterRange(RenderSVGInlineText* text, unsigned position, unsigned length)
104 return SVGTextMetrics(text, constructTextRun(text, text->characters(), position, length));
109 #endif // ENABLE(SVG)