blob: fd764d3acafe904f32608d7ee322b93f761a9d14 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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 "core/svg/SVGRectElement.h"
25
26#include "SVGNames.h"
27#include "core/dom/Attribute.h"
28#include "core/rendering/svg/RenderSVGPath.h"
29#include "core/rendering/svg/RenderSVGRect.h"
30#include "core/rendering/svg/RenderSVGResource.h"
31#include "core/svg/SVGElementInstance.h"
32#include "core/svg/SVGLength.h"
33
34namespace WebCore {
35
36// Animated property definitions
37DEFINE_ANIMATED_LENGTH(SVGRectElement, SVGNames::xAttr, X, x)
38DEFINE_ANIMATED_LENGTH(SVGRectElement, SVGNames::yAttr, Y, y)
39DEFINE_ANIMATED_LENGTH(SVGRectElement, SVGNames::widthAttr, Width, width)
40DEFINE_ANIMATED_LENGTH(SVGRectElement, SVGNames::heightAttr, Height, height)
41DEFINE_ANIMATED_LENGTH(SVGRectElement, SVGNames::rxAttr, Rx, rx)
42DEFINE_ANIMATED_LENGTH(SVGRectElement, SVGNames::ryAttr, Ry, ry)
43DEFINE_ANIMATED_BOOLEAN(SVGRectElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
44
45BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGRectElement)
46 REGISTER_LOCAL_ANIMATED_PROPERTY(x)
47 REGISTER_LOCAL_ANIMATED_PROPERTY(y)
48 REGISTER_LOCAL_ANIMATED_PROPERTY(width)
49 REGISTER_LOCAL_ANIMATED_PROPERTY(height)
50 REGISTER_LOCAL_ANIMATED_PROPERTY(rx)
51 REGISTER_LOCAL_ANIMATED_PROPERTY(ry)
52 REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
53 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGStyledTransformableElement)
54 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGTests)
55END_REGISTER_ANIMATED_PROPERTIES
56
57inline SVGRectElement::SVGRectElement(const QualifiedName& tagName, Document* document)
58 : SVGStyledTransformableElement(tagName, document)
59 , m_x(LengthModeWidth)
60 , m_y(LengthModeHeight)
61 , m_width(LengthModeWidth)
62 , m_height(LengthModeHeight)
63 , m_rx(LengthModeWidth)
64 , m_ry(LengthModeHeight)
65{
66 ASSERT(hasTagName(SVGNames::rectTag));
67 ScriptWrappable::init(this);
68 registerAnimatedPropertiesForSVGRectElement();
69}
70
71PassRefPtr<SVGRectElement> SVGRectElement::create(const QualifiedName& tagName, Document* document)
72{
73 return adoptRef(new SVGRectElement(tagName, document));
74}
75
76bool SVGRectElement::isSupportedAttribute(const QualifiedName& attrName)
77{
78 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
79 if (supportedAttributes.isEmpty()) {
80 SVGTests::addSupportedAttributes(supportedAttributes);
81 SVGLangSpace::addSupportedAttributes(supportedAttributes);
82 SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
83 supportedAttributes.add(SVGNames::xAttr);
84 supportedAttributes.add(SVGNames::yAttr);
85 supportedAttributes.add(SVGNames::widthAttr);
86 supportedAttributes.add(SVGNames::heightAttr);
87 supportedAttributes.add(SVGNames::rxAttr);
88 supportedAttributes.add(SVGNames::ryAttr);
89 }
90 return supportedAttributes.contains<QualifiedName, SVGAttributeHashTranslator>(attrName);
91}
92
93void SVGRectElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
94{
95 SVGParsingError parseError = NoError;
96
97 if (!isSupportedAttribute(name))
98 SVGStyledTransformableElement::parseAttribute(name, value);
99 else if (name == SVGNames::xAttr)
100 setXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
101 else if (name == SVGNames::yAttr)
102 setYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
103 else if (name == SVGNames::rxAttr)
104 setRxBaseValue(SVGLength::construct(LengthModeWidth, value, parseError, ForbidNegativeLengths));
105 else if (name == SVGNames::ryAttr)
106 setRyBaseValue(SVGLength::construct(LengthModeHeight, value, parseError, ForbidNegativeLengths));
107 else if (name == SVGNames::widthAttr)
108 setWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError, ForbidNegativeLengths));
109 else if (name == SVGNames::heightAttr)
110 setHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError, ForbidNegativeLengths));
111 else if (SVGTests::parseAttribute(name, value)
112 || SVGLangSpace::parseAttribute(name, value)
113 || SVGExternalResourcesRequired::parseAttribute(name, value)) {
114 } else
115 ASSERT_NOT_REACHED();
116
117 reportAttributeParsingError(parseError, name, value);
118}
119
120void SVGRectElement::svgAttributeChanged(const QualifiedName& attrName)
121{
122 if (!isSupportedAttribute(attrName)) {
123 SVGStyledTransformableElement::svgAttributeChanged(attrName);
124 return;
125 }
126
127 SVGElementInstance::InvalidationGuard invalidationGuard(this);
128
129 bool isLengthAttribute = attrName == SVGNames::xAttr
130 || attrName == SVGNames::yAttr
131 || attrName == SVGNames::widthAttr
132 || attrName == SVGNames::heightAttr
133 || attrName == SVGNames::rxAttr
134 || attrName == SVGNames::ryAttr;
135
136 if (isLengthAttribute)
137 updateRelativeLengthsInformation();
138
139 if (SVGTests::handleAttributeChange(this, attrName))
140 return;
141
142 RenderSVGShape* renderer = toRenderSVGShape(this->renderer());
143 if (!renderer)
144 return;
145
146 if (isLengthAttribute) {
147 renderer->setNeedsShapeUpdate();
148 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
149 return;
150 }
151
152 if (SVGLangSpace::isKnownAttribute(attrName) || SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
153 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
154 return;
155 }
156
157 ASSERT_NOT_REACHED();
158}
159
160bool SVGRectElement::selfHasRelativeLengths() const
161{
162 return x().isRelative()
163 || y().isRelative()
164 || width().isRelative()
165 || height().isRelative()
166 || rx().isRelative()
167 || ry().isRelative();
168}
169
170RenderObject* SVGRectElement::createRenderer(RenderArena* arena, RenderStyle*)
171{
172 return new (arena) RenderSVGRect(this);
173}
174
175}
176
177#endif // ENABLE(SVG)