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 "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"
38 template<> const SVGEnumerationStringEntries& getStaticStringEntries<SVGLengthAdjustType>()
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"));
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 {
52 static PassRefPtr<SVGAnimatedTextLength> create(SVGTextContentElement* contextElement)
54 return adoptRef(new SVGAnimatedTextLength(contextElement));
57 virtual SVGLengthTearOff* baseVal() override
59 SVGTextContentElement* textContentElement = toSVGTextContentElement(contextElement());
60 if (!textContentElement->textLengthIsSpecifiedByUser())
61 baseValue()->newValueSpecifiedUnits(LengthTypeNumber, textContentElement->getComputedTextLength());
63 return SVGAnimatedLength::baseVal();
67 SVGAnimatedTextLength(SVGTextContentElement* contextElement)
68 : SVGAnimatedLength(contextElement, SVGNames::textLengthAttr, SVGLength::create(LengthModeOther), ForbidNegativeLengths)
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))
80 addToPropertyMap(m_textLength);
81 addToPropertyMap(m_lengthAdjust);
84 unsigned SVGTextContentElement::getNumberOfChars()
86 document().updateLayoutIgnorePendingStylesheets();
87 return SVGTextQuery(renderer()).numberOfCharacters();
90 float SVGTextContentElement::getComputedTextLength()
92 document().updateLayoutIgnorePendingStylesheets();
93 return SVGTextQuery(renderer()).textLength();
96 float SVGTextContentElement::getSubStringLength(unsigned charnum, unsigned nchars, ExceptionState& exceptionState)
98 document().updateLayoutIgnorePendingStylesheets();
100 unsigned numberOfChars = getNumberOfChars();
101 if (charnum >= numberOfChars) {
102 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("charnum", charnum, getNumberOfChars()));
106 if (nchars > numberOfChars - charnum)
107 nchars = numberOfChars - charnum;
109 return SVGTextQuery(renderer()).subStringLength(charnum, nchars);
112 PassRefPtr<SVGPointTearOff> SVGTextContentElement::getStartPositionOfChar(unsigned charnum, ExceptionState& exceptionState)
114 document().updateLayoutIgnorePendingStylesheets();
116 if (charnum > getNumberOfChars()) {
117 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("charnum", charnum, getNumberOfChars()));
121 FloatPoint point = SVGTextQuery(renderer()).startPositionOfCharacter(charnum);
122 return SVGPointTearOff::create(SVGPoint::create(point), 0, PropertyIsNotAnimVal);
125 PassRefPtr<SVGPointTearOff> SVGTextContentElement::getEndPositionOfChar(unsigned charnum, ExceptionState& exceptionState)
127 document().updateLayoutIgnorePendingStylesheets();
129 if (charnum > getNumberOfChars()) {
130 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("charnum", charnum, getNumberOfChars()));
134 FloatPoint point = SVGTextQuery(renderer()).endPositionOfCharacter(charnum);
135 return SVGPointTearOff::create(SVGPoint::create(point), 0, PropertyIsNotAnimVal);
138 PassRefPtr<SVGRectTearOff> SVGTextContentElement::getExtentOfChar(unsigned charnum, ExceptionState& exceptionState)
140 document().updateLayoutIgnorePendingStylesheets();
142 if (charnum > getNumberOfChars()) {
143 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("charnum", charnum, getNumberOfChars()));
147 FloatRect rect = SVGTextQuery(renderer()).extentOfCharacter(charnum);
148 return SVGRectTearOff::create(SVGRect::create(rect), 0, PropertyIsNotAnimVal);
151 float SVGTextContentElement::getRotationOfChar(unsigned charnum, ExceptionState& exceptionState)
153 document().updateLayoutIgnorePendingStylesheets();
155 if (charnum > getNumberOfChars()) {
156 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("charnum", charnum, getNumberOfChars()));
160 return SVGTextQuery(renderer()).rotationOfCharacter(charnum);
163 int SVGTextContentElement::getCharNumAtPosition(PassRefPtr<SVGPointTearOff> point, ExceptionState& exceptionState)
165 document().updateLayoutIgnorePendingStylesheets();
166 return SVGTextQuery(renderer()).characterNumberAtPosition(point->target()->value());
169 void SVGTextContentElement::selectSubString(unsigned charnum, unsigned nchars, ExceptionState& exceptionState)
171 unsigned numberOfChars = getNumberOfChars();
172 if (charnum >= numberOfChars) {
173 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexExceedsMaximumBound("charnum", charnum, getNumberOfChars()));
177 if (nchars > numberOfChars - charnum)
178 nchars = numberOfChars - charnum;
180 ASSERT(document().frame());
182 // Find selection start
183 VisiblePosition start(firstPositionInNode(const_cast<SVGTextContentElement*>(this)));
184 for (unsigned i = 0; i < charnum; ++i)
185 start = start.next();
187 // Find selection end
188 VisiblePosition end(start);
189 for (unsigned i = 0; i < nchars; ++i)
192 document().frame()->selection().setSelection(VisibleSelection(start, end));
195 bool SVGTextContentElement::isSupportedAttribute(const QualifiedName& attrName)
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);
203 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
206 bool SVGTextContentElement::isPresentationAttribute(const QualifiedName& name) const
208 if (name.matches(XMLNames::spaceAttr))
210 return SVGGraphicsElement::isPresentationAttribute(name);
213 void SVGTextContentElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
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));
220 if (value == preserveString)
221 addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValuePre);
223 addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValueNowrap);
227 void SVGTextContentElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
229 parseAttributeNew(name, value);
232 void SVGTextContentElement::svgAttributeChanged(const QualifiedName& attrName)
234 if (!isSupportedAttribute(attrName)) {
235 SVGGraphicsElement::svgAttributeChanged(attrName);
239 if (attrName == SVGNames::textLengthAttr)
240 m_textLengthIsSpecifiedByUser = true;
242 SVGElement::InvalidationGuard invalidationGuard(this);
244 if (RenderObject* renderer = this->renderer())
245 markForLayoutAndParentResourceInvalidation(renderer);
248 bool SVGTextContentElement::selfHasRelativeLengths() const
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.
256 SVGTextContentElement* SVGTextContentElement::elementFromRenderer(RenderObject* renderer)
261 if (!renderer->isSVGText() && !renderer->isSVGInline())
264 SVGElement* element = toSVGElement(renderer->node());
266 return isSVGTextContentElement(*element) ? toSVGTextContentElement(element) : 0;