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.
24 #include "SVGTextContentElement.h"
26 #include "CSSPropertyNames.h"
27 #include "CSSValueKeywords.h"
29 #include "FrameSelection.h"
30 #include "RenderObject.h"
31 #include "RenderSVGResource.h"
32 #include "RenderSVGText.h"
33 #include "SVGDocumentExtensions.h"
34 #include "SVGElementInstance.h"
36 #include "SVGTextQuery.h"
41 // Define custom animated property 'textLength'.
42 const SVGPropertyInfo* SVGTextContentElement::textLengthPropertyInfo()
44 static const SVGPropertyInfo* s_propertyInfo = 0;
45 if (!s_propertyInfo) {
46 s_propertyInfo = new SVGPropertyInfo(AnimatedLength,
47 SVGNames::textLengthAttr,
48 SVGNames::textLengthAttr.localName(),
49 &SVGTextContentElement::synchronizeTextLength,
50 &SVGTextContentElement::lookupOrCreateTextLengthWrapper);
52 return s_propertyInfo;
55 // Animated property definitions
56 DEFINE_ANIMATED_ENUMERATION(SVGTextContentElement, SVGNames::lengthAdjustAttr, LengthAdjust, lengthAdjust, SVGLengthAdjustType)
57 DEFINE_ANIMATED_BOOLEAN(SVGTextContentElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
59 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGTextContentElement)
60 REGISTER_LOCAL_ANIMATED_PROPERTY(textLength)
61 REGISTER_LOCAL_ANIMATED_PROPERTY(lengthAdjust)
62 REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
63 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGStyledElement)
64 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGTests)
65 END_REGISTER_ANIMATED_PROPERTIES
67 SVGTextContentElement::SVGTextContentElement(const QualifiedName& tagName, Document* document)
68 : SVGStyledElement(tagName, document)
69 , m_textLength(LengthModeOther)
70 , m_specifiedTextLength(LengthModeOther)
71 , m_lengthAdjust(SVGLengthAdjustSpacing)
73 registerAnimatedPropertiesForSVGTextContentElement();
76 void SVGTextContentElement::synchronizeTextLength(void* contextElement)
78 ASSERT(contextElement);
79 SVGTextContentElement* ownerType = static_cast<SVGTextContentElement*>(contextElement);
80 if (!ownerType->m_textLength.shouldSynchronize)
82 AtomicString value(SVGPropertyTraits<SVGLength>::toString(ownerType->m_specifiedTextLength));
83 SVGAnimatedPropertySynchronizer<true>::synchronize(ownerType, textLengthPropertyInfo()->attributeName, value);
86 PassRefPtr<SVGAnimatedProperty> SVGTextContentElement::lookupOrCreateTextLengthWrapper(void* contextElement)
88 ASSERT(contextElement);
89 SVGTextContentElement* ownerType = static_cast<SVGTextContentElement*>(contextElement);
90 return SVGAnimatedProperty::lookupOrCreateWrapper<SVGTextContentElement, SVGAnimatedLength, SVGLength, true>
91 (ownerType, textLengthPropertyInfo(), ownerType->m_textLength.value);
94 PassRefPtr<SVGAnimatedLength> SVGTextContentElement::textLengthAnimated()
96 DEFINE_STATIC_LOCAL(SVGLength, defaultTextLength, (LengthModeOther));
97 if (m_specifiedTextLength == defaultTextLength) {
99 m_textLength.value.newValueSpecifiedUnits(LengthTypeNumber, getComputedTextLength(), ec);
103 m_textLength.shouldSynchronize = true;
104 return static_pointer_cast<SVGAnimatedLength>(lookupOrCreateTextLengthWrapper(this));
108 unsigned SVGTextContentElement::getNumberOfChars()
110 document()->updateLayoutIgnorePendingStylesheets();
111 return SVGTextQuery(renderer()).numberOfCharacters();
114 float SVGTextContentElement::getComputedTextLength()
116 document()->updateLayoutIgnorePendingStylesheets();
117 return SVGTextQuery(renderer()).textLength();
120 float SVGTextContentElement::getSubStringLength(unsigned charnum, unsigned nchars, ExceptionCode& ec)
122 document()->updateLayoutIgnorePendingStylesheets();
124 unsigned numberOfChars = getNumberOfChars();
125 if (charnum >= numberOfChars) {
130 return SVGTextQuery(renderer()).subStringLength(charnum, nchars);
133 FloatPoint SVGTextContentElement::getStartPositionOfChar(unsigned charnum, ExceptionCode& ec)
135 document()->updateLayoutIgnorePendingStylesheets();
137 if (charnum > getNumberOfChars()) {
142 return SVGTextQuery(renderer()).startPositionOfCharacter(charnum);
145 FloatPoint SVGTextContentElement::getEndPositionOfChar(unsigned charnum, ExceptionCode& ec)
147 document()->updateLayoutIgnorePendingStylesheets();
149 if (charnum > getNumberOfChars()) {
154 return SVGTextQuery(renderer()).endPositionOfCharacter(charnum);
157 FloatRect SVGTextContentElement::getExtentOfChar(unsigned charnum, ExceptionCode& ec)
159 document()->updateLayoutIgnorePendingStylesheets();
161 if (charnum > getNumberOfChars()) {
166 return SVGTextQuery(renderer()).extentOfCharacter(charnum);
169 float SVGTextContentElement::getRotationOfChar(unsigned charnum, ExceptionCode& ec)
171 document()->updateLayoutIgnorePendingStylesheets();
173 if (charnum > getNumberOfChars()) {
178 return SVGTextQuery(renderer()).rotationOfCharacter(charnum);
181 int SVGTextContentElement::getCharNumAtPosition(const FloatPoint& point)
183 document()->updateLayoutIgnorePendingStylesheets();
184 return SVGTextQuery(renderer()).characterNumberAtPosition(point);
187 void SVGTextContentElement::selectSubString(unsigned charnum, unsigned nchars, ExceptionCode& ec)
189 unsigned numberOfChars = getNumberOfChars();
190 if (charnum >= numberOfChars) {
195 if (nchars > numberOfChars - charnum)
196 nchars = numberOfChars - charnum;
199 ASSERT(document()->frame());
201 FrameSelection* selection = document()->frame()->selection();
205 // Find selection start
206 VisiblePosition start(firstPositionInNode(const_cast<SVGTextContentElement*>(this)));
207 for (unsigned i = 0; i < charnum; ++i)
208 start = start.next();
210 // Find selection end
211 VisiblePosition end(start);
212 for (unsigned i = 0; i < nchars; ++i)
215 selection->setSelection(VisibleSelection(start, end));
218 bool SVGTextContentElement::isSupportedAttribute(const QualifiedName& attrName)
220 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
221 if (supportedAttributes.isEmpty()) {
222 SVGTests::addSupportedAttributes(supportedAttributes);
223 SVGLangSpace::addSupportedAttributes(supportedAttributes);
224 SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
225 supportedAttributes.add(SVGNames::lengthAdjustAttr);
226 supportedAttributes.add(SVGNames::textLengthAttr);
228 return supportedAttributes.contains(attrName);
231 void SVGTextContentElement::parseMappedAttribute(Attribute* attr)
233 SVGParsingError parseError = NoError;
235 if (!isSupportedAttribute(attr->name()))
236 SVGStyledElement::parseMappedAttribute(attr);
237 else if (attr->name() == SVGNames::lengthAdjustAttr) {
238 SVGLengthAdjustType propertyValue = SVGPropertyTraits<SVGLengthAdjustType>::fromString(attr->value());
239 if (propertyValue > 0)
240 setLengthAdjustBaseValue(propertyValue);
241 } else if (attr->name() == SVGNames::textLengthAttr) {
242 m_textLength.value = SVGLength::construct(LengthModeOther, attr->value(), parseError, ForbidNegativeLengths);
243 } else if (SVGTests::parseMappedAttribute(attr)
244 || SVGExternalResourcesRequired::parseMappedAttribute(attr)) {
245 } else if (SVGLangSpace::parseMappedAttribute(attr)) {
246 if (attr->name().matches(XMLNames::spaceAttr)) {
247 DEFINE_STATIC_LOCAL(const AtomicString, preserveString, ("preserve"));
249 if (attr->value() == preserveString)
250 addCSSProperty(attr, CSSPropertyWhiteSpace, CSSValuePre);
252 addCSSProperty(attr, CSSPropertyWhiteSpace, CSSValueNowrap);
255 ASSERT_NOT_REACHED();
257 reportAttributeParsingError(parseError, attr);
260 void SVGTextContentElement::svgAttributeChanged(const QualifiedName& attrName)
262 if (!isSupportedAttribute(attrName)) {
263 SVGStyledElement::svgAttributeChanged(attrName);
267 SVGElementInstance::InvalidationGuard invalidationGuard(this);
269 if (SVGTests::handleAttributeChange(this, attrName))
272 if (attrName == SVGNames::textLengthAttr)
273 m_specifiedTextLength = m_textLength.value;
275 if (RenderObject* renderer = this->renderer())
276 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
279 bool SVGTextContentElement::selfHasRelativeLengths() const
281 // Any element of the <text> subtree is advertized as using relative lengths.
282 // On any window size change, we have to relayout the text subtree, as the
283 // effective 'on-screen' font size may change.
287 SVGTextContentElement* SVGTextContentElement::elementFromRenderer(RenderObject* renderer)
292 if (!renderer->isSVGText() && !renderer->isSVGInline())
295 Node* node = renderer->node();
297 ASSERT(node->isSVGElement());
299 if (!node->hasTagName(SVGNames::textTag)
300 && !node->hasTagName(SVGNames::tspanTag)
301 #if ENABLE(SVG_FONTS)
302 && !node->hasTagName(SVGNames::altGlyphTag)
304 && !node->hasTagName(SVGNames::trefTag)
305 && !node->hasTagName(SVGNames::textPathTag))
308 return static_cast<SVGTextContentElement*>(node);
311 void SVGTextContentElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
313 SVGStyledElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
315 if (changedByParser || !renderer())
318 if (RenderSVGText* textRenderer = RenderSVGText::locateRenderSVGTextAncestor(renderer()))
319 textRenderer->setNeedsPositioningValuesUpdate();
324 #endif // ENABLE(SVG)