Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGTextContentElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Rob Buis <buis@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22 #include "core/svg/SVGTextContentElement.h"
23
24 #include "bindings/core/v8/ExceptionMessages.h"
25 #include "bindings/core/v8/ExceptionState.h"
26 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
27 #include "core/CSSPropertyNames.h"
28 #include "core/CSSValueKeywords.h"
29 #include "core/SVGNames.h"
30 #include "core/XMLNames.h"
31 #include "core/editing/FrameSelection.h"
32 #include "core/frame/LocalFrame.h"
33 #include "core/rendering/RenderObject.h"
34 #include "core/rendering/svg/SVGTextQuery.h"
35
36 namespace blink {
37
38 template<> const SVGEnumerationStringEntries& getStaticStringEntries<SVGLengthAdjustType>()
39 {
40     DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
41     if (entries.isEmpty()) {
42         entries.append(std::make_pair(SVGLengthAdjustSpacing, "spacing"));
43         entries.append(std::make_pair(SVGLengthAdjustSpacingAndGlyphs, "spacingAndGlyphs"));
44     }
45     return entries;
46 }
47
48 // SVGTextContentElement's 'textLength' attribute needs special handling.
49 // It should return getComputedTextLength() when textLength is not specified manually.
50 class SVGAnimatedTextLength final : public SVGAnimatedLength {
51 public:
52     static PassRefPtr<SVGAnimatedTextLength> create(SVGTextContentElement* contextElement)
53     {
54         return adoptRef(new SVGAnimatedTextLength(contextElement));
55     }
56
57     virtual SVGLengthTearOff* baseVal() override
58     {
59         SVGTextContentElement* textContentElement = toSVGTextContentElement(contextElement());
60         if (!textContentElement->textLengthIsSpecifiedByUser())
61             baseValue()->newValueSpecifiedUnits(LengthTypeNumber, textContentElement->getComputedTextLength());
62
63         return SVGAnimatedLength::baseVal();
64     }
65
66 private:
67     SVGAnimatedTextLength(SVGTextContentElement* contextElement)
68         : SVGAnimatedLength(contextElement, SVGNames::textLengthAttr, SVGLength::create(LengthModeOther), ForbidNegativeLengths)
69     {
70     }
71 };
72
73
74 SVGTextContentElement::SVGTextContentElement(const QualifiedName& tagName, Document& document)
75     : SVGGraphicsElement(tagName, document)
76     , m_textLength(SVGAnimatedTextLength::create(this))
77     , m_textLengthIsSpecifiedByUser(false)
78     , m_lengthAdjust(SVGAnimatedEnumeration<SVGLengthAdjustType>::create(this, SVGNames::lengthAdjustAttr, SVGLengthAdjustSpacing))
79 {
80     addToPropertyMap(m_textLength);
81     addToPropertyMap(m_lengthAdjust);
82 }
83
84 unsigned SVGTextContentElement::getNumberOfChars()
85 {
86     document().updateLayoutIgnorePendingStylesheets();
87     return SVGTextQuery(renderer()).numberOfCharacters();
88 }
89
90 float SVGTextContentElement::getComputedTextLength()
91 {
92     document().updateLayoutIgnorePendingStylesheets();
93     return SVGTextQuery(renderer()).textLength();
94 }
95
96 float SVGTextContentElement::getSubStringLength(unsigned charnum, unsigned nchars, ExceptionState& exceptionState)
97 {
98     document().updateLayoutIgnorePendingStylesheets();
99
100     unsigned numberOfChars = getNumberOfChars();
101     if (charnum >= numberOfChars) {
102         exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("charnum", charnum, getNumberOfChars()));
103         return 0.0f;
104     }
105
106     if (nchars > numberOfChars - charnum)
107         nchars = numberOfChars - charnum;
108
109     return SVGTextQuery(renderer()).subStringLength(charnum, nchars);
110 }
111
112 PassRefPtr<SVGPointTearOff> SVGTextContentElement::getStartPositionOfChar(unsigned charnum, ExceptionState& exceptionState)
113 {
114     document().updateLayoutIgnorePendingStylesheets();
115
116     if (charnum > getNumberOfChars()) {
117         exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("charnum", charnum, getNumberOfChars()));
118         return nullptr;
119     }
120
121     FloatPoint point = SVGTextQuery(renderer()).startPositionOfCharacter(charnum);
122     return SVGPointTearOff::create(SVGPoint::create(point), 0, PropertyIsNotAnimVal);
123 }
124
125 PassRefPtr<SVGPointTearOff> SVGTextContentElement::getEndPositionOfChar(unsigned charnum, ExceptionState& exceptionState)
126 {
127     document().updateLayoutIgnorePendingStylesheets();
128
129     if (charnum > getNumberOfChars()) {
130         exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("charnum", charnum, getNumberOfChars()));
131         return nullptr;
132     }
133
134     FloatPoint point = SVGTextQuery(renderer()).endPositionOfCharacter(charnum);
135     return SVGPointTearOff::create(SVGPoint::create(point), 0, PropertyIsNotAnimVal);
136 }
137
138 PassRefPtr<SVGRectTearOff> SVGTextContentElement::getExtentOfChar(unsigned charnum, ExceptionState& exceptionState)
139 {
140     document().updateLayoutIgnorePendingStylesheets();
141
142     if (charnum > getNumberOfChars()) {
143         exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("charnum", charnum, getNumberOfChars()));
144         return nullptr;
145     }
146
147     FloatRect rect = SVGTextQuery(renderer()).extentOfCharacter(charnum);
148     return SVGRectTearOff::create(SVGRect::create(rect), 0, PropertyIsNotAnimVal);
149 }
150
151 float SVGTextContentElement::getRotationOfChar(unsigned charnum, ExceptionState& exceptionState)
152 {
153     document().updateLayoutIgnorePendingStylesheets();
154
155     if (charnum > getNumberOfChars()) {
156         exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("charnum", charnum, getNumberOfChars()));
157         return 0.0f;
158     }
159
160     return SVGTextQuery(renderer()).rotationOfCharacter(charnum);
161 }
162
163 int SVGTextContentElement::getCharNumAtPosition(PassRefPtr<SVGPointTearOff> point, ExceptionState& exceptionState)
164 {
165     document().updateLayoutIgnorePendingStylesheets();
166     return SVGTextQuery(renderer()).characterNumberAtPosition(point->target()->value());
167 }
168
169 void SVGTextContentElement::selectSubString(unsigned charnum, unsigned nchars, ExceptionState& exceptionState)
170 {
171     unsigned numberOfChars = getNumberOfChars();
172     if (charnum >= numberOfChars) {
173         exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("charnum", charnum, getNumberOfChars()));
174         return;
175     }
176
177     if (nchars > numberOfChars - charnum)
178         nchars = numberOfChars - charnum;
179
180     ASSERT(document().frame());
181
182     // Find selection start
183     VisiblePosition start(firstPositionInNode(const_cast<SVGTextContentElement*>(this)));
184     for (unsigned i = 0; i < charnum; ++i)
185         start = start.next();
186
187     // Find selection end
188     VisiblePosition end(start);
189     for (unsigned i = 0; i < nchars; ++i)
190         end = end.next();
191
192     document().frame()->selection().setSelection(VisibleSelection(start, end));
193 }
194
195 bool SVGTextContentElement::isSupportedAttribute(const QualifiedName& attrName)
196 {
197     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
198     if (supportedAttributes.isEmpty()) {
199         supportedAttributes.add(SVGNames::lengthAdjustAttr);
200         supportedAttributes.add(SVGNames::textLengthAttr);
201         supportedAttributes.add(XMLNames::spaceAttr);
202     }
203     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
204 }
205
206 bool SVGTextContentElement::isPresentationAttribute(const QualifiedName& name) const
207 {
208     if (name.matches(XMLNames::spaceAttr))
209         return true;
210     return SVGGraphicsElement::isPresentationAttribute(name);
211 }
212
213 void SVGTextContentElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
214 {
215     if (!isSupportedAttribute(name))
216         SVGGraphicsElement::collectStyleForPresentationAttribute(name, value, style);
217     else if (name.matches(XMLNames::spaceAttr)) {
218         DEFINE_STATIC_LOCAL(const AtomicString, preserveString, ("preserve", AtomicString::ConstructFromLiteral));
219
220         if (value == preserveString)
221             addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValuePre);
222         else
223             addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValueNowrap);
224     }
225 }
226
227 void SVGTextContentElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
228 {
229     parseAttributeNew(name, value);
230 }
231
232 void SVGTextContentElement::svgAttributeChanged(const QualifiedName& attrName)
233 {
234     if (!isSupportedAttribute(attrName)) {
235         SVGGraphicsElement::svgAttributeChanged(attrName);
236         return;
237     }
238
239     if (attrName == SVGNames::textLengthAttr)
240         m_textLengthIsSpecifiedByUser = true;
241
242     SVGElement::InvalidationGuard invalidationGuard(this);
243
244     if (RenderObject* renderer = this->renderer())
245         markForLayoutAndParentResourceInvalidation(renderer);
246 }
247
248 bool SVGTextContentElement::selfHasRelativeLengths() const
249 {
250     // Any element of the <text> subtree is advertized as using relative lengths.
251     // On any window size change, we have to relayout the text subtree, as the
252     // effective 'on-screen' font size may change.
253     return true;
254 }
255
256 SVGTextContentElement* SVGTextContentElement::elementFromRenderer(RenderObject* renderer)
257 {
258     if (!renderer)
259         return 0;
260
261     if (!renderer->isSVGText() && !renderer->isSVGInline())
262         return 0;
263
264     SVGElement* element = toSVGElement(renderer->node());
265     ASSERT(element);
266     return isSVGTextContentElement(*element) ? toSVGTextContentElement(element) : 0;
267 }
268
269 }