blob: b00d3df0a4cb4c04b5dfc82a1a2bf855e5e0cb35 [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
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010023#include "core/svg/SVGRectElement.h"
24
25#include "SVGNames.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010026#include "core/rendering/svg/RenderSVGRect.h"
27#include "core/rendering/svg/RenderSVGResource.h"
28#include "core/svg/SVGElementInstance.h"
29#include "core/svg/SVGLength.h"
30
31namespace WebCore {
32
33// Animated property definitions
34DEFINE_ANIMATED_LENGTH(SVGRectElement, SVGNames::xAttr, X, x)
35DEFINE_ANIMATED_LENGTH(SVGRectElement, SVGNames::yAttr, Y, y)
36DEFINE_ANIMATED_LENGTH(SVGRectElement, SVGNames::widthAttr, Width, width)
37DEFINE_ANIMATED_LENGTH(SVGRectElement, SVGNames::heightAttr, Height, height)
38DEFINE_ANIMATED_LENGTH(SVGRectElement, SVGNames::rxAttr, Rx, rx)
39DEFINE_ANIMATED_LENGTH(SVGRectElement, SVGNames::ryAttr, Ry, ry)
40DEFINE_ANIMATED_BOOLEAN(SVGRectElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
41
42BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGRectElement)
43 REGISTER_LOCAL_ANIMATED_PROPERTY(x)
44 REGISTER_LOCAL_ANIMATED_PROPERTY(y)
45 REGISTER_LOCAL_ANIMATED_PROPERTY(width)
46 REGISTER_LOCAL_ANIMATED_PROPERTY(height)
47 REGISTER_LOCAL_ANIMATED_PROPERTY(rx)
48 REGISTER_LOCAL_ANIMATED_PROPERTY(ry)
49 REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
Ben Murdoch591b9582013-07-10 11:41:44 +010050 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGGraphicsElement)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010051END_REGISTER_ANIMATED_PROPERTIES
52
53inline SVGRectElement::SVGRectElement(const QualifiedName& tagName, Document* document)
Ben Murdoch591b9582013-07-10 11:41:44 +010054 : SVGGraphicsElement(tagName, document)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010055 , m_x(LengthModeWidth)
56 , m_y(LengthModeHeight)
57 , m_width(LengthModeWidth)
58 , m_height(LengthModeHeight)
59 , m_rx(LengthModeWidth)
60 , m_ry(LengthModeHeight)
61{
62 ASSERT(hasTagName(SVGNames::rectTag));
63 ScriptWrappable::init(this);
64 registerAnimatedPropertiesForSVGRectElement();
65}
66
67PassRefPtr<SVGRectElement> SVGRectElement::create(const QualifiedName& tagName, Document* document)
68{
69 return adoptRef(new SVGRectElement(tagName, document));
70}
71
72bool SVGRectElement::isSupportedAttribute(const QualifiedName& attrName)
73{
74 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
75 if (supportedAttributes.isEmpty()) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010076 SVGLangSpace::addSupportedAttributes(supportedAttributes);
77 SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
78 supportedAttributes.add(SVGNames::xAttr);
79 supportedAttributes.add(SVGNames::yAttr);
80 supportedAttributes.add(SVGNames::widthAttr);
81 supportedAttributes.add(SVGNames::heightAttr);
82 supportedAttributes.add(SVGNames::rxAttr);
83 supportedAttributes.add(SVGNames::ryAttr);
84 }
Ben Murdoch591b9582013-07-10 11:41:44 +010085 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010086}
87
88void SVGRectElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
89{
90 SVGParsingError parseError = NoError;
91
92 if (!isSupportedAttribute(name))
Ben Murdoch591b9582013-07-10 11:41:44 +010093 SVGGraphicsElement::parseAttribute(name, value);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010094 else if (name == SVGNames::xAttr)
95 setXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
96 else if (name == SVGNames::yAttr)
97 setYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
98 else if (name == SVGNames::rxAttr)
99 setRxBaseValue(SVGLength::construct(LengthModeWidth, value, parseError, ForbidNegativeLengths));
100 else if (name == SVGNames::ryAttr)
101 setRyBaseValue(SVGLength::construct(LengthModeHeight, value, parseError, ForbidNegativeLengths));
102 else if (name == SVGNames::widthAttr)
103 setWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError, ForbidNegativeLengths));
104 else if (name == SVGNames::heightAttr)
105 setHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError, ForbidNegativeLengths));
Ben Murdoch591b9582013-07-10 11:41:44 +0100106 else if (SVGLangSpace::parseAttribute(name, value)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100107 || SVGExternalResourcesRequired::parseAttribute(name, value)) {
108 } else
109 ASSERT_NOT_REACHED();
110
111 reportAttributeParsingError(parseError, name, value);
112}
113
114void SVGRectElement::svgAttributeChanged(const QualifiedName& attrName)
115{
116 if (!isSupportedAttribute(attrName)) {
Ben Murdoch591b9582013-07-10 11:41:44 +0100117 SVGGraphicsElement::svgAttributeChanged(attrName);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100118 return;
119 }
120
121 SVGElementInstance::InvalidationGuard invalidationGuard(this);
122
123 bool isLengthAttribute = attrName == SVGNames::xAttr
124 || attrName == SVGNames::yAttr
125 || attrName == SVGNames::widthAttr
126 || attrName == SVGNames::heightAttr
127 || attrName == SVGNames::rxAttr
128 || attrName == SVGNames::ryAttr;
129
130 if (isLengthAttribute)
131 updateRelativeLengthsInformation();
132
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100133 RenderSVGShape* renderer = toRenderSVGShape(this->renderer());
134 if (!renderer)
135 return;
136
137 if (isLengthAttribute) {
138 renderer->setNeedsShapeUpdate();
139 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
140 return;
141 }
142
143 if (SVGLangSpace::isKnownAttribute(attrName) || SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
144 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
145 return;
146 }
147
148 ASSERT_NOT_REACHED();
149}
150
151bool SVGRectElement::selfHasRelativeLengths() const
152{
153 return x().isRelative()
154 || y().isRelative()
155 || width().isRelative()
156 || height().isRelative()
157 || rx().isRelative()
158 || ry().isRelative();
159}
160
Ben Murdoch591b9582013-07-10 11:41:44 +0100161RenderObject* SVGRectElement::createRenderer(RenderStyle*)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100162{
Ben Murdoch591b9582013-07-10 11:41:44 +0100163 return new (document()->renderArena()) RenderSVGRect(this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100164}
165
166}