2 * Copyright (C) Research In Motion Limited 2010-2012. 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.
22 #include "core/rendering/svg/SVGTextMetrics.h"
24 #include "core/rendering/svg/RenderSVGInlineText.h"
25 #include "core/rendering/svg/SVGTextRunRenderingContext.h"
29 SVGTextMetrics::SVGTextMetrics()
37 SVGTextMetrics::SVGTextMetrics(SVGTextMetrics::MetricsType)
45 SVGTextMetrics::SVGTextMetrics(RenderSVGInlineText* textRenderer, const TextRun& run)
49 float scalingFactor = textRenderer->scalingFactor();
50 ASSERT(scalingFactor);
52 const Font& scaledFont = textRenderer->scaledFont();
55 // Calculate width/height using the scaled font, divide this result by the scalingFactor afterwards.
56 m_width = scaledFont.width(run, length, m_glyph) / scalingFactor;
57 m_height = scaledFont.fontMetrics().floatHeight() / scalingFactor;
60 m_length = static_cast<unsigned>(length);
63 TextRun SVGTextMetrics::constructTextRun(RenderSVGInlineText* text, unsigned position, unsigned length)
65 RenderStyle* style = text->style();
68 TextRun run(static_cast<const LChar*>(0) // characters, will be set below if non-zero.
69 , 0 // length, will be set below if non-zero.
70 , 0 // xPos, only relevant with allowTabs=true
71 , 0 // padding, only relevant for justified text, not relevant for SVG
72 , TextRun::AllowTrailingExpansion
74 , isOverride(style->unicodeBidi()) /* directionalOverride */);
78 run.setText(text->characters8() + position, length);
80 run.setText(text->characters16() + position, length);
83 if (textRunNeedsRenderingContext(style->font()))
84 run.setRenderingContext(SVGTextRunRenderingContext::create(text));
86 run.disableRoundingHacks();
88 // We handle letter & word spacing ourselves.
91 // Propagate the maximum length of the characters buffer to the TextRun, even when we're only processing a substring.
92 run.setCharactersLength(text->textLength() - position);
93 ASSERT(run.charactersLength() >= run.length());
97 SVGTextMetrics SVGTextMetrics::measureCharacterRange(RenderSVGInlineText* text, unsigned position, unsigned length)
100 return SVGTextMetrics(text, constructTextRun(text, position, length));
103 SVGTextMetrics::SVGTextMetrics(RenderSVGInlineText* text, unsigned position, unsigned length, float width, Glyph glyphNameGlyphId)
107 bool needsContext = textRunNeedsRenderingContext(text->style()->font());
108 float scalingFactor = text->scalingFactor();
109 ASSERT(scalingFactor);
111 m_width = width / scalingFactor;
112 m_height = text->scaledFont().fontMetrics().floatHeight() / scalingFactor;
113 m_glyph = needsContext ? glyphNameGlyphId : 0;