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#include "core/svg/SVGRectElement.h"
24
25#include "core/rendering/svg/RenderSVGRect.h"
26#include "core/rendering/svg/RenderSVGResource.h"
27#include "core/svg/SVGLength.h"
28
29namespace blink {
30
31inline SVGRectElement::SVGRectElement(Document& document)
32    : SVGGeometryElement(SVGNames::rectTag, document)
33    , m_x(SVGAnimatedLength::create(this, SVGNames::xAttr, SVGLength::create(LengthModeWidth), AllowNegativeLengths))
34    , m_y(SVGAnimatedLength::create(this, SVGNames::yAttr, SVGLength::create(LengthModeHeight), AllowNegativeLengths))
35    , m_width(SVGAnimatedLength::create(this, SVGNames::widthAttr, SVGLength::create(LengthModeWidth), ForbidNegativeLengths))
36    , m_height(SVGAnimatedLength::create(this, SVGNames::heightAttr, SVGLength::create(LengthModeHeight), ForbidNegativeLengths))
37    , m_rx(SVGAnimatedLength::create(this, SVGNames::rxAttr, SVGLength::create(LengthModeWidth), ForbidNegativeLengths))
38    , m_ry(SVGAnimatedLength::create(this, SVGNames::ryAttr, SVGLength::create(LengthModeHeight), ForbidNegativeLengths))
39{
40    addToPropertyMap(m_x);
41    addToPropertyMap(m_y);
42    addToPropertyMap(m_width);
43    addToPropertyMap(m_height);
44    addToPropertyMap(m_rx);
45    addToPropertyMap(m_ry);
46}
47
48DEFINE_NODE_FACTORY(SVGRectElement)
49
50bool SVGRectElement::isSupportedAttribute(const QualifiedName& attrName)
51{
52    DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
53    if (supportedAttributes.isEmpty()) {
54        supportedAttributes.add(SVGNames::xAttr);
55        supportedAttributes.add(SVGNames::yAttr);
56        supportedAttributes.add(SVGNames::widthAttr);
57        supportedAttributes.add(SVGNames::heightAttr);
58        supportedAttributes.add(SVGNames::rxAttr);
59        supportedAttributes.add(SVGNames::ryAttr);
60    }
61    return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
62}
63
64void SVGRectElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
65{
66    parseAttributeNew(name, value);
67}
68
69void SVGRectElement::svgAttributeChanged(const QualifiedName& attrName)
70{
71    if (!isSupportedAttribute(attrName)) {
72        SVGGeometryElement::svgAttributeChanged(attrName);
73        return;
74    }
75
76    SVGElement::InvalidationGuard invalidationGuard(this);
77
78    bool isLengthAttribute = attrName == SVGNames::xAttr
79                          || attrName == SVGNames::yAttr
80                          || attrName == SVGNames::widthAttr
81                          || attrName == SVGNames::heightAttr
82                          || attrName == SVGNames::rxAttr
83                          || attrName == SVGNames::ryAttr;
84
85    if (isLengthAttribute)
86        updateRelativeLengthsInformation();
87
88    RenderSVGShape* renderer = toRenderSVGShape(this->renderer());
89    if (!renderer)
90        return;
91
92    if (isLengthAttribute) {
93        renderer->setNeedsShapeUpdate();
94        RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
95        return;
96    }
97
98    ASSERT_NOT_REACHED();
99}
100
101bool SVGRectElement::selfHasRelativeLengths() const
102{
103    return m_x->currentValue()->isRelative()
104        || m_y->currentValue()->isRelative()
105        || m_width->currentValue()->isRelative()
106        || m_height->currentValue()->isRelative()
107        || m_rx->currentValue()->isRelative()
108        || m_ry->currentValue()->isRelative();
109}
110
111RenderObject* SVGRectElement::createRenderer(RenderStyle*)
112{
113    return new RenderSVGRect(this);
114}
115
116} // namespace blink
117