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