Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGTextPositioningElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 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
23 #include "core/svg/SVGTextPositioningElement.h"
24
25 #include "SVGNames.h"
26 #include "core/rendering/svg/RenderSVGResource.h"
27 #include "core/rendering/svg/RenderSVGText.h"
28 #include "core/svg/SVGElementInstance.h"
29 #include "core/svg/SVGLengthList.h"
30 #include "core/svg/SVGNumberList.h"
31
32 namespace WebCore {
33
34 SVGTextPositioningElement::SVGTextPositioningElement(const QualifiedName& tagName, Document& document)
35     : SVGTextContentElement(tagName, document)
36     , m_x(SVGAnimatedLengthList::create(this, SVGNames::xAttr, SVGLengthList::create(LengthModeWidth)))
37     , m_y(SVGAnimatedLengthList::create(this, SVGNames::yAttr, SVGLengthList::create(LengthModeHeight)))
38     , m_dx(SVGAnimatedLengthList::create(this, SVGNames::dxAttr, SVGLengthList::create(LengthModeWidth)))
39     , m_dy(SVGAnimatedLengthList::create(this, SVGNames::dyAttr, SVGLengthList::create(LengthModeHeight)))
40     , m_rotate(SVGAnimatedNumberList::create(this, SVGNames::rotateAttr, SVGNumberList::create()))
41 {
42     ScriptWrappable::init(this);
43
44     addToPropertyMap(m_x);
45     addToPropertyMap(m_y);
46     addToPropertyMap(m_dx);
47     addToPropertyMap(m_dy);
48     addToPropertyMap(m_rotate);
49 }
50
51 bool SVGTextPositioningElement::isSupportedAttribute(const QualifiedName& attrName)
52 {
53     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
54     if (supportedAttributes.isEmpty()) {
55         supportedAttributes.add(SVGNames::xAttr);
56         supportedAttributes.add(SVGNames::yAttr);
57         supportedAttributes.add(SVGNames::dxAttr);
58         supportedAttributes.add(SVGNames::dyAttr);
59         supportedAttributes.add(SVGNames::rotateAttr);
60     }
61     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
62 }
63
64 void SVGTextPositioningElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
65 {
66     if (!isSupportedAttribute(name)) {
67         SVGTextContentElement::parseAttribute(name, value);
68         return;
69     }
70
71     SVGParsingError parseError = NoError;
72
73     if (name == SVGNames::xAttr)
74         m_x->setBaseValueAsString(value, parseError);
75     else if (name == SVGNames::yAttr)
76         m_y->setBaseValueAsString(value, parseError);
77     else if (name == SVGNames::dxAttr)
78         m_dx->setBaseValueAsString(value, parseError);
79     else if (name == SVGNames::dyAttr)
80         m_dy->setBaseValueAsString(value, parseError);
81     else if (name == SVGNames::rotateAttr)
82         m_rotate->setBaseValueAsString(value, parseError);
83     else
84         ASSERT_NOT_REACHED();
85
86     reportAttributeParsingError(parseError, name, value);
87 }
88
89 void SVGTextPositioningElement::svgAttributeChanged(const QualifiedName& attrName)
90 {
91     if (!isSupportedAttribute(attrName)) {
92         SVGTextContentElement::svgAttributeChanged(attrName);
93         return;
94     }
95
96     SVGElement::InvalidationGuard invalidationGuard(this);
97
98     bool updateRelativeLengths = attrName == SVGNames::xAttr
99                               || attrName == SVGNames::yAttr
100                               || attrName == SVGNames::dxAttr
101                               || attrName == SVGNames::dyAttr;
102
103     if (updateRelativeLengths)
104         updateRelativeLengthsInformation();
105
106     RenderObject* renderer = this->renderer();
107     if (!renderer)
108         return;
109
110     if (updateRelativeLengths || attrName == SVGNames::rotateAttr) {
111         if (RenderSVGText* textRenderer = RenderSVGText::locateRenderSVGTextAncestor(renderer))
112             textRenderer->setNeedsPositioningValuesUpdate();
113         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
114         return;
115     }
116
117     ASSERT_NOT_REACHED();
118 }
119
120 SVGTextPositioningElement* SVGTextPositioningElement::elementFromRenderer(RenderObject* renderer)
121 {
122     if (!renderer)
123         return 0;
124
125     if (!renderer->isSVGText() && !renderer->isSVGInline())
126         return 0;
127
128     Node* node = renderer->node();
129     ASSERT(node);
130     ASSERT(node->isSVGElement());
131
132     return isSVGTextPositioningElement(*node) ? toSVGTextPositioningElement(node) : 0;
133 }
134
135 }