initial import
[vuplus_webkit] / Source / WebCore / svg / SVGEllipseElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2006, 2007 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 #if ENABLE(SVG)
24 #include "SVGEllipseElement.h"
25
26 #include "Attribute.h"
27 #include "FloatPoint.h"
28 #include "RenderSVGPath.h"
29 #include "RenderSVGResource.h"
30 #include "SVGElementInstance.h"
31 #include "SVGLength.h"
32 #include "SVGNames.h"
33
34 namespace WebCore {
35
36 // Animated property definitions
37 DEFINE_ANIMATED_LENGTH(SVGEllipseElement, SVGNames::cxAttr, Cx, cx)
38 DEFINE_ANIMATED_LENGTH(SVGEllipseElement, SVGNames::cyAttr, Cy, cy)
39 DEFINE_ANIMATED_LENGTH(SVGEllipseElement, SVGNames::rxAttr, Rx, rx)
40 DEFINE_ANIMATED_LENGTH(SVGEllipseElement, SVGNames::ryAttr, Ry, ry)
41 DEFINE_ANIMATED_BOOLEAN(SVGEllipseElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
42
43 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGEllipseElement)
44     REGISTER_LOCAL_ANIMATED_PROPERTY(cx)
45     REGISTER_LOCAL_ANIMATED_PROPERTY(cy)
46     REGISTER_LOCAL_ANIMATED_PROPERTY(rx)
47     REGISTER_LOCAL_ANIMATED_PROPERTY(ry)
48     REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
49     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGStyledTransformableElement)
50     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGTests)
51 END_REGISTER_ANIMATED_PROPERTIES
52
53 inline SVGEllipseElement::SVGEllipseElement(const QualifiedName& tagName, Document* document)
54     : SVGStyledTransformableElement(tagName, document)
55     , m_cx(LengthModeWidth)
56     , m_cy(LengthModeHeight)
57     , m_rx(LengthModeWidth)
58     , m_ry(LengthModeHeight)
59 {
60     ASSERT(hasTagName(SVGNames::ellipseTag));
61     registerAnimatedPropertiesForSVGEllipseElement();
62 }    
63
64 PassRefPtr<SVGEllipseElement> SVGEllipseElement::create(const QualifiedName& tagName, Document* document)
65 {
66     return adoptRef(new SVGEllipseElement(tagName, document));
67 }
68
69 bool SVGEllipseElement::isSupportedAttribute(const QualifiedName& attrName)
70 {
71     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
72     if (supportedAttributes.isEmpty()) {
73         SVGTests::addSupportedAttributes(supportedAttributes);
74         SVGLangSpace::addSupportedAttributes(supportedAttributes);
75         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
76         supportedAttributes.add(SVGNames::cxAttr);
77         supportedAttributes.add(SVGNames::cyAttr);
78         supportedAttributes.add(SVGNames::rxAttr);
79         supportedAttributes.add(SVGNames::ryAttr);
80     }
81     return supportedAttributes.contains(attrName);
82 }
83
84 void SVGEllipseElement::parseMappedAttribute(Attribute* attr)
85 {
86     SVGParsingError parseError = NoError;
87
88     if (!isSupportedAttribute(attr->name()))
89         SVGStyledTransformableElement::parseMappedAttribute(attr);
90     else if (attr->name() == SVGNames::cxAttr)
91         setCxBaseValue(SVGLength::construct(LengthModeWidth, attr->value(), parseError));
92     else if (attr->name() == SVGNames::cyAttr)
93         setCyBaseValue(SVGLength::construct(LengthModeHeight, attr->value(), parseError));
94     else if (attr->name() == SVGNames::rxAttr)
95         setRxBaseValue(SVGLength::construct(LengthModeWidth, attr->value(), parseError, ForbidNegativeLengths));
96     else if (attr->name() == SVGNames::ryAttr)
97         setRyBaseValue(SVGLength::construct(LengthModeHeight, attr->value(), parseError, ForbidNegativeLengths));
98     else if (SVGTests::parseMappedAttribute(attr)
99              || SVGLangSpace::parseMappedAttribute(attr)
100              || SVGExternalResourcesRequired::parseMappedAttribute(attr)) {
101     } else
102         ASSERT_NOT_REACHED();
103
104     reportAttributeParsingError(parseError, attr);
105 }
106
107 void SVGEllipseElement::svgAttributeChanged(const QualifiedName& attrName)
108 {
109     if (!isSupportedAttribute(attrName)) {
110         SVGStyledTransformableElement::svgAttributeChanged(attrName);
111         return;
112     }
113
114     SVGElementInstance::InvalidationGuard invalidationGuard(this);
115
116     bool isLengthAttribute = attrName == SVGNames::cxAttr
117                           || attrName == SVGNames::cyAttr
118                           || attrName == SVGNames::rxAttr
119                           || attrName == SVGNames::ryAttr;
120
121     if (isLengthAttribute)
122         updateRelativeLengthsInformation();
123  
124     if (SVGTests::handleAttributeChange(this, attrName))
125         return;
126
127     RenderSVGPath* renderer = static_cast<RenderSVGPath*>(this->renderer());
128     if (!renderer)
129         return;
130
131     if (isLengthAttribute) {
132         renderer->setNeedsPathUpdate();
133         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
134         return;
135     }
136
137     if (SVGLangSpace::isKnownAttribute(attrName) || SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
138         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
139         return;
140     }
141
142     ASSERT_NOT_REACHED();
143 }
144
145 void SVGEllipseElement::toPathData(Path& path) const
146 {
147     ASSERT(path.isEmpty());
148
149     float radiusX = rx().value(this);
150     if (radiusX <= 0)
151         return;
152
153     float radiusY = ry().value(this);
154     if (radiusY <= 0)
155         return;
156
157     path.addEllipse(FloatRect(cx().value(this) - radiusX, cy().value(this) - radiusY, radiusX * 2, radiusY * 2));
158 }
159  
160 bool SVGEllipseElement::selfHasRelativeLengths() const
161 {
162     return cx().isRelative()
163         || cy().isRelative()
164         || rx().isRelative()
165         || ry().isRelative();
166 }
167
168 }
169
170 #endif // ENABLE(SVG)