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