initial import
[vuplus_webkit] / Source / WebCore / rendering / svg / SVGTextMetrics.cpp
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 #if ENABLE(SVG)
23 #include "SVGTextMetrics.h"
24
25 #include "RenderSVGInlineText.h"
26 #include "SVGTextRunRenderingContext.h"
27
28 namespace WebCore {
29
30 SVGTextMetrics::SVGTextMetrics()
31     : m_width(0)
32     , m_height(0)
33     , m_length(0)
34 {
35 }
36
37 SVGTextMetrics::SVGTextMetrics(RenderSVGInlineText* textRenderer, const TextRun& run)
38 {
39     ASSERT(textRenderer);
40
41     float scalingFactor = textRenderer->scalingFactor();
42     ASSERT(scalingFactor);
43
44     const Font& scaledFont = textRenderer->scaledFont();
45     int length = 0;
46
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;
50
51     m_glyph.unicodeString = String(run.characters(), length);
52     m_glyph.isValid = true;
53
54     ASSERT(length >= 0);
55     m_length = static_cast<unsigned>(length);
56 }
57
58 bool SVGTextMetrics::operator==(const SVGTextMetrics& other)
59 {
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;
64 }
65
66 SVGTextMetrics SVGTextMetrics::emptyMetrics()
67 {
68     DEFINE_STATIC_LOCAL(SVGTextMetrics, s_emptyMetrics, ());
69     s_emptyMetrics.m_length = 1;
70     return s_emptyMetrics;
71 }
72
73 static TextRun constructTextRun(RenderSVGInlineText* text, const UChar* characters, unsigned position, unsigned length)
74 {
75     RenderStyle* style = text->style();
76     ASSERT(style);
77
78     TextRun run(characters + position
79                 , length
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
84                 , style->direction()
85                 , style->unicodeBidi() == Override /* directionalOverride */);
86
87     if (textRunNeedsRenderingContext(style->font()))
88         run.setRenderingContext(SVGTextRunRenderingContext::create(text));
89
90     run.disableRoundingHacks();
91
92     // We handle letter & word spacing ourselves.
93     run.disableSpacing();
94
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());
98     return run;
99 }
100
101 SVGTextMetrics SVGTextMetrics::measureCharacterRange(RenderSVGInlineText* text, unsigned position, unsigned length)
102 {
103     ASSERT(text);
104     return SVGTextMetrics(text, constructTextRun(text, text->characters(), position, length));
105 }
106
107 }
108
109 #endif // ENABLE(SVG)