• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "TextRun.h"
27 
28 namespace WebCore {
29 
SVGTextMetrics()30 SVGTextMetrics::SVGTextMetrics()
31     : m_width(0)
32     , m_height(0)
33     , m_length(0)
34 {
35 }
36 
SVGTextMetrics(RenderSVGInlineText * textRenderer,const TextRun & run,unsigned position,unsigned textLength)37 SVGTextMetrics::SVGTextMetrics(RenderSVGInlineText* textRenderer, const TextRun& run, unsigned position, unsigned textLength)
38 {
39     ASSERT(textRenderer);
40 
41     float scalingFactor = textRenderer->scalingFactor();
42     ASSERT(scalingFactor);
43 
44     const Font& scaledFont = textRenderer->scaledFont();
45 
46     int extraCharsAvailable = textLength - (position + run.length());
47     int length = 0;
48 
49     // Calculate width/height using the scaled font, divide this result by the scalingFactor afterwards.
50     m_width = scaledFont.width(run, extraCharsAvailable, length, m_glyph.name) / scalingFactor;
51     m_height = scaledFont.fontMetrics().floatHeight() / scalingFactor;
52 
53     m_glyph.unicodeString = String(run.characters(), length);
54     m_glyph.isValid = true;
55 
56     ASSERT(length >= 0);
57     m_length = static_cast<unsigned>(length);
58 }
59 
operator ==(const SVGTextMetrics & other)60 bool SVGTextMetrics::operator==(const SVGTextMetrics& other)
61 {
62     return m_width == other.m_width
63         && m_height == other.m_height
64         && m_length == other.m_length
65         && m_glyph == other.m_glyph;
66 }
67 
emptyMetrics()68 SVGTextMetrics SVGTextMetrics::emptyMetrics()
69 {
70     DEFINE_STATIC_LOCAL(SVGTextMetrics, s_emptyMetrics, ());
71     s_emptyMetrics.m_length = 1;
72     return s_emptyMetrics;
73 }
74 
constructTextRun(RenderSVGInlineText * text,const UChar * characters,unsigned position,unsigned length)75 static TextRun constructTextRun(RenderSVGInlineText* text, const UChar* characters, unsigned position, unsigned length)
76 {
77     RenderStyle* style = text->style();
78     ASSERT(style);
79 
80     TextRun run(characters + position
81                 , length
82                 , false /* allowTabs */
83                 , 0 /* xPos, only relevant with allowTabs=true */
84                 , 0 /* padding, only relevant for justified text, not relevant for SVG */
85                 , TextRun::AllowTrailingExpansion
86                 , !style->isLeftToRightDirection()
87                 , style->unicodeBidi() == Override /* directionalOverride */);
88 
89 #if ENABLE(SVG_FONTS)
90     run.setReferencingRenderObject(text);
91 #endif
92 
93     // We handle letter & word spacing ourselves.
94     run.disableSpacing();
95     return run;
96 }
97 
measureCharacterRange(RenderSVGInlineText * text,unsigned position,unsigned length)98 SVGTextMetrics SVGTextMetrics::measureCharacterRange(RenderSVGInlineText* text, unsigned position, unsigned length)
99 {
100     ASSERT(text);
101     TextRun run(constructTextRun(text, text->characters(), position, length));
102     return SVGTextMetrics(text, run, position, text->textLength());
103 }
104 
105 }
106 
107 #endif // ENABLE(SVG)
108