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 "RenderObject.h"
30 #include "SVGDocumentExtensions.h"
32 #include "SVGTextQuery.h"
33 #include "SelectionController.h"
38 // Animated property definitions
39 DEFINE_ANIMATED_LENGTH(SVGTextContentElement, SVGNames::textLengthAttr, TextLength, textLength)
40 DEFINE_ANIMATED_ENUMERATION(SVGTextContentElement, SVGNames::lengthAdjustAttr, LengthAdjust, lengthAdjust)
41 DEFINE_ANIMATED_BOOLEAN(SVGTextContentElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
43 SVGTextContentElement::SVGTextContentElement(const QualifiedName& tagName, Document* document)
44 : SVGStyledElement(tagName, document)
45 , m_textLength(LengthModeOther)
46 , m_lengthAdjust(LENGTHADJUST_SPACING)
50 unsigned SVGTextContentElement::getNumberOfChars() const
52 document()->updateLayoutIgnorePendingStylesheets();
53 return SVGTextQuery(renderer()).numberOfCharacters();
56 float SVGTextContentElement::getComputedTextLength() const
58 document()->updateLayoutIgnorePendingStylesheets();
59 return SVGTextQuery(renderer()).textLength();
62 float SVGTextContentElement::getSubStringLength(unsigned charnum, unsigned nchars, ExceptionCode& ec) const
64 document()->updateLayoutIgnorePendingStylesheets();
66 unsigned numberOfChars = getNumberOfChars();
67 if (charnum >= numberOfChars) {
72 return SVGTextQuery(renderer()).subStringLength(charnum, nchars);
75 FloatPoint SVGTextContentElement::getStartPositionOfChar(unsigned charnum, ExceptionCode& ec) const
77 document()->updateLayoutIgnorePendingStylesheets();
79 if (charnum > getNumberOfChars()) {
84 return SVGTextQuery(renderer()).startPositionOfCharacter(charnum);
87 FloatPoint SVGTextContentElement::getEndPositionOfChar(unsigned charnum, ExceptionCode& ec) const
89 document()->updateLayoutIgnorePendingStylesheets();
91 if (charnum > getNumberOfChars()) {
96 return SVGTextQuery(renderer()).endPositionOfCharacter(charnum);
99 FloatRect SVGTextContentElement::getExtentOfChar(unsigned charnum, ExceptionCode& ec) const
101 document()->updateLayoutIgnorePendingStylesheets();
103 if (charnum > getNumberOfChars()) {
108 return SVGTextQuery(renderer()).extentOfCharacter(charnum);
111 float SVGTextContentElement::getRotationOfChar(unsigned charnum, ExceptionCode& ec) const
113 document()->updateLayoutIgnorePendingStylesheets();
115 if (charnum > getNumberOfChars()) {
120 return SVGTextQuery(renderer()).rotationOfCharacter(charnum);
123 int SVGTextContentElement::getCharNumAtPosition(const FloatPoint& point) const
125 document()->updateLayoutIgnorePendingStylesheets();
126 return SVGTextQuery(renderer()).characterNumberAtPosition(point);
129 void SVGTextContentElement::selectSubString(unsigned charnum, unsigned nchars, ExceptionCode& ec) const
131 unsigned numberOfChars = getNumberOfChars();
132 if (charnum >= numberOfChars) {
137 if (nchars > numberOfChars - charnum)
138 nchars = numberOfChars - charnum;
141 ASSERT(document()->frame());
143 SelectionController* controller = document()->frame()->selection();
147 // Find selection start
148 VisiblePosition start(const_cast<SVGTextContentElement*>(this), 0, SEL_DEFAULT_AFFINITY);
149 for (unsigned i = 0; i < charnum; ++i)
150 start = start.next();
152 // Find selection end
153 VisiblePosition end(start);
154 for (unsigned i = 0; i < nchars; ++i)
157 controller->setSelection(VisibleSelection(start, end));
160 void SVGTextContentElement::parseMappedAttribute(Attribute* attr)
162 if (attr->name() == SVGNames::lengthAdjustAttr) {
163 if (attr->value() == "spacing")
164 setLengthAdjustBaseValue(LENGTHADJUST_SPACING);
165 else if (attr->value() == "spacingAndGlyphs")
166 setLengthAdjustBaseValue(LENGTHADJUST_SPACINGANDGLYPHS);
167 } else if (attr->name() == SVGNames::textLengthAttr) {
168 setTextLengthBaseValue(SVGLength(LengthModeOther, attr->value()));
169 if (textLengthBaseValue().value(this) < 0.0)
170 document()->accessSVGExtensions()->reportError("A negative value for text attribute <textLength> is not allowed");
172 if (SVGTests::parseMappedAttribute(attr))
174 if (SVGLangSpace::parseMappedAttribute(attr)) {
175 if (attr->name().matches(XMLNames::spaceAttr)) {
176 DEFINE_STATIC_LOCAL(const AtomicString, preserveString, ("preserve"));
178 if (attr->value() == preserveString)
179 addCSSProperty(attr, CSSPropertyWhiteSpace, CSSValuePre);
181 addCSSProperty(attr, CSSPropertyWhiteSpace, CSSValueNowrap);
185 if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
188 SVGStyledElement::parseMappedAttribute(attr);
192 void SVGTextContentElement::synchronizeProperty(const QualifiedName& attrName)
194 SVGStyledElement::synchronizeProperty(attrName);
196 if (attrName == anyQName()) {
197 synchronizeLengthAdjust();
198 synchronizeTextLength();
199 synchronizeExternalResourcesRequired();
200 SVGTests::synchronizeProperties(this, attrName);
204 if (attrName == SVGNames::lengthAdjustAttr)
205 synchronizeLengthAdjust();
206 else if (attrName == SVGNames::textLengthAttr)
207 synchronizeTextLength();
208 else if (SVGExternalResourcesRequired::isKnownAttribute(attrName))
209 synchronizeExternalResourcesRequired();
210 else if (SVGTests::isKnownAttribute(attrName))
211 SVGTests::synchronizeProperties(this, attrName);
214 void SVGTextContentElement::svgAttributeChanged(const QualifiedName& attrName)
216 SVGStyledElement::svgAttributeChanged(attrName);
218 if (SVGTests::handleAttributeChange(this, attrName))
221 // FIXME: also handle attribute changes for lengthAdjust and textLength
224 void SVGTextContentElement::fillPassedAttributeToPropertyTypeMap(AttributeToPropertyTypeMap& attributeToPropertyTypeMap)
226 SVGStyledElement::fillPassedAttributeToPropertyTypeMap(attributeToPropertyTypeMap);
228 attributeToPropertyTypeMap.set(SVGNames::textLengthAttr, AnimatedLength);
229 attributeToPropertyTypeMap.set(SVGNames::lengthAdjustAttr, AnimatedEnumeration);
232 bool SVGTextContentElement::isKnownAttribute(const QualifiedName& attrName)
234 return attrName.matches(SVGNames::lengthAdjustAttr)
235 || attrName.matches(SVGNames::textLengthAttr)
236 || SVGLangSpace::isKnownAttribute(attrName)
237 || SVGExternalResourcesRequired::isKnownAttribute(attrName)
238 || SVGStyledElement::isKnownAttribute(attrName);
241 bool SVGTextContentElement::selfHasRelativeLengths() const
243 // Any element of the <text> subtree is advertized as using relative lengths.
244 // On any window size change, we have to relayout the text subtree, as the
245 // effective 'on-screen' font size may change.
249 SVGTextContentElement* SVGTextContentElement::elementFromRenderer(RenderObject* renderer)
254 if (!renderer->isSVGText() && !renderer->isSVGInline())
257 Node* node = renderer->node();
259 ASSERT(node->isSVGElement());
261 if (!node->hasTagName(SVGNames::textTag)
262 && !node->hasTagName(SVGNames::tspanTag)
263 #if ENABLE(SVG_FONTS)
264 && !node->hasTagName(SVGNames::altGlyphTag)
266 && !node->hasTagName(SVGNames::trefTag)
267 && !node->hasTagName(SVGNames::textPathTag))
270 return static_cast<SVGTextContentElement*>(node);
275 #endif // ENABLE(SVG)