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>
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.
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.
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.
22 #include "core/svg/SVGTextContentElement.h"
24 #include "CSSPropertyNames.h"
25 #include "CSSValueKeywords.h"
28 #include "bindings/v8/ExceptionState.h"
29 #include "bindings/v8/ExceptionStatePlaceholder.h"
30 #include "core/editing/FrameSelection.h"
31 #include "core/frame/Frame.h"
32 #include "core/rendering/RenderObject.h"
33 #include "core/rendering/svg/RenderSVGResource.h"
34 #include "core/rendering/svg/SVGTextQuery.h"
35 #include "core/svg/SVGElementInstance.h"
39 // Define custom animated property 'textLength'.
40 const SVGPropertyInfo* SVGTextContentElement::textLengthPropertyInfo()
42 static const SVGPropertyInfo* s_propertyInfo = 0;
43 if (!s_propertyInfo) {
44 s_propertyInfo = new SVGPropertyInfo(AnimatedLength,
46 SVGNames::textLengthAttr,
47 SVGNames::textLengthAttr.localName(),
48 &SVGTextContentElement::synchronizeTextLength,
49 &SVGTextContentElement::lookupOrCreateTextLengthWrapper);
51 return s_propertyInfo;
54 // Animated property definitions
55 DEFINE_ANIMATED_ENUMERATION(SVGTextContentElement, SVGNames::lengthAdjustAttr, LengthAdjust, lengthAdjust, SVGLengthAdjustType)
56 DEFINE_ANIMATED_BOOLEAN(SVGTextContentElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
58 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGTextContentElement)
59 REGISTER_LOCAL_ANIMATED_PROPERTY(textLength)
60 REGISTER_LOCAL_ANIMATED_PROPERTY(lengthAdjust)
61 REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
62 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGGraphicsElement)
63 END_REGISTER_ANIMATED_PROPERTIES
65 SVGTextContentElement::SVGTextContentElement(const QualifiedName& tagName, Document& document)
66 : SVGGraphicsElement(tagName, document)
67 , m_textLength(LengthModeOther)
68 , m_specifiedTextLength(LengthModeOther)
69 , m_lengthAdjust(SVGLengthAdjustSpacing)
71 ScriptWrappable::init(this);
72 registerAnimatedPropertiesForSVGTextContentElement();
75 void SVGTextContentElement::synchronizeTextLength(SVGElement* contextElement)
77 ASSERT(contextElement);
78 SVGTextContentElement* ownerType = toSVGTextContentElement(contextElement);
79 if (!ownerType->m_textLength.shouldSynchronize)
81 AtomicString value(SVGPropertyTraits<SVGLength>::toString(ownerType->m_specifiedTextLength));
82 ownerType->m_textLength.synchronize(ownerType, textLengthPropertyInfo()->attributeName, value);
85 PassRefPtr<SVGAnimatedProperty> SVGTextContentElement::lookupOrCreateTextLengthWrapper(SVGElement* contextElement)
87 ASSERT(contextElement);
88 SVGTextContentElement* ownerType = toSVGTextContentElement(contextElement);
89 return SVGAnimatedProperty::lookupOrCreateWrapper<SVGTextContentElement, SVGAnimatedLength, SVGLength>
90 (ownerType, textLengthPropertyInfo(), ownerType->m_textLength.value);
93 PassRefPtr<SVGAnimatedLength> SVGTextContentElement::textLength()
95 DEFINE_STATIC_LOCAL(SVGLength, defaultTextLength, (LengthModeOther));
96 if (m_specifiedTextLength == defaultTextLength)
97 m_textLength.value.newValueSpecifiedUnits(LengthTypeNumber, getComputedTextLength(), ASSERT_NO_EXCEPTION);
99 m_textLength.shouldSynchronize = true;
100 return static_pointer_cast<SVGAnimatedLength>(lookupOrCreateTextLengthWrapper(this));
104 unsigned SVGTextContentElement::getNumberOfChars()
106 document().updateLayoutIgnorePendingStylesheets();
107 return SVGTextQuery(renderer()).numberOfCharacters();
110 float SVGTextContentElement::getComputedTextLength()
112 document().updateLayoutIgnorePendingStylesheets();
113 return SVGTextQuery(renderer()).textLength();
116 float SVGTextContentElement::getSubStringLength(unsigned charnum, unsigned nchars, ExceptionState& es)
118 document().updateLayoutIgnorePendingStylesheets();
120 unsigned numberOfChars = getNumberOfChars();
121 if (charnum >= numberOfChars) {
122 es.throwUninformativeAndGenericDOMException(IndexSizeError);
126 if (nchars > numberOfChars - charnum)
127 nchars = numberOfChars - charnum;
129 return SVGTextQuery(renderer()).subStringLength(charnum, nchars);
132 SVGPoint SVGTextContentElement::getStartPositionOfChar(unsigned charnum, ExceptionState& es)
134 document().updateLayoutIgnorePendingStylesheets();
136 if (charnum > getNumberOfChars()) {
137 es.throwUninformativeAndGenericDOMException(IndexSizeError);
141 return SVGTextQuery(renderer()).startPositionOfCharacter(charnum);
144 SVGPoint SVGTextContentElement::getEndPositionOfChar(unsigned charnum, ExceptionState& es)
146 document().updateLayoutIgnorePendingStylesheets();
148 if (charnum > getNumberOfChars()) {
149 es.throwUninformativeAndGenericDOMException(IndexSizeError);
153 return SVGTextQuery(renderer()).endPositionOfCharacter(charnum);
156 SVGRect SVGTextContentElement::getExtentOfChar(unsigned charnum, ExceptionState& es)
158 document().updateLayoutIgnorePendingStylesheets();
160 if (charnum > getNumberOfChars()) {
161 es.throwUninformativeAndGenericDOMException(IndexSizeError);
165 return SVGTextQuery(renderer()).extentOfCharacter(charnum);
168 float SVGTextContentElement::getRotationOfChar(unsigned charnum, ExceptionState& es)
170 document().updateLayoutIgnorePendingStylesheets();
172 if (charnum > getNumberOfChars()) {
173 es.throwUninformativeAndGenericDOMException(IndexSizeError);
177 return SVGTextQuery(renderer()).rotationOfCharacter(charnum);
180 int SVGTextContentElement::getCharNumAtPosition(const SVGPoint& point)
182 document().updateLayoutIgnorePendingStylesheets();
183 return SVGTextQuery(renderer()).characterNumberAtPosition(point);
186 void SVGTextContentElement::selectSubString(unsigned charnum, unsigned nchars, ExceptionState& es)
188 unsigned numberOfChars = getNumberOfChars();
189 if (charnum >= numberOfChars) {
190 es.throwUninformativeAndGenericDOMException(IndexSizeError);
194 if (nchars > numberOfChars - charnum)
195 nchars = numberOfChars - charnum;
197 ASSERT(document().frame());
199 // Find selection start
200 VisiblePosition start(firstPositionInNode(const_cast<SVGTextContentElement*>(this)));
201 for (unsigned i = 0; i < charnum; ++i)
202 start = start.next();
204 // Find selection end
205 VisiblePosition end(start);
206 for (unsigned i = 0; i < nchars; ++i)
209 document().frame()->selection().setSelection(VisibleSelection(start, end));
212 bool SVGTextContentElement::isSupportedAttribute(const QualifiedName& attrName)
214 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
215 if (supportedAttributes.isEmpty()) {
216 SVGLangSpace::addSupportedAttributes(supportedAttributes);
217 SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
218 supportedAttributes.add(SVGNames::lengthAdjustAttr);
219 supportedAttributes.add(SVGNames::textLengthAttr);
221 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
224 bool SVGTextContentElement::isPresentationAttribute(const QualifiedName& name) const
226 if (name.matches(XMLNames::spaceAttr))
228 return SVGGraphicsElement::isPresentationAttribute(name);
231 void SVGTextContentElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
233 if (!isSupportedAttribute(name))
234 SVGGraphicsElement::collectStyleForPresentationAttribute(name, value, style);
235 else if (name.matches(XMLNames::spaceAttr)) {
236 DEFINE_STATIC_LOCAL(const AtomicString, preserveString, ("preserve", AtomicString::ConstructFromLiteral));
238 if (value == preserveString)
239 addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValuePre);
241 addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValueNowrap);
245 void SVGTextContentElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
247 SVGParsingError parseError = NoError;
249 if (!isSupportedAttribute(name))
250 SVGGraphicsElement::parseAttribute(name, value);
251 else if (name == SVGNames::lengthAdjustAttr) {
252 SVGLengthAdjustType propertyValue = SVGPropertyTraits<SVGLengthAdjustType>::fromString(value);
253 if (propertyValue > 0)
254 setLengthAdjustBaseValue(propertyValue);
255 } else if (name == SVGNames::textLengthAttr) {
256 m_textLength.value = SVGLength::construct(LengthModeOther, value, parseError, ForbidNegativeLengths);
257 } else if (SVGExternalResourcesRequired::parseAttribute(name, value)) {
258 } else if (SVGLangSpace::parseAttribute(name, value)) {
260 ASSERT_NOT_REACHED();
262 reportAttributeParsingError(parseError, name, value);
265 void SVGTextContentElement::svgAttributeChanged(const QualifiedName& attrName)
267 if (!isSupportedAttribute(attrName)) {
268 SVGGraphicsElement::svgAttributeChanged(attrName);
272 SVGElementInstance::InvalidationGuard invalidationGuard(this);
274 if (attrName == SVGNames::textLengthAttr)
275 m_specifiedTextLength = m_textLength.value;
277 if (RenderObject* renderer = this->renderer())
278 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
281 bool SVGTextContentElement::selfHasRelativeLengths() const
283 // Any element of the <text> subtree is advertized as using relative lengths.
284 // On any window size change, we have to relayout the text subtree, as the
285 // effective 'on-screen' font size may change.
289 SVGTextContentElement* SVGTextContentElement::elementFromRenderer(RenderObject* renderer)
294 if (!renderer->isSVGText() && !renderer->isSVGInline())
297 SVGElement* element = toSVGElement(renderer->node());
300 if (!element->isTextContent())
303 return toSVGTextContentElement(element);