initial import
[vuplus_webkit] / Source / WebCore / svg / SVGFilterElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4  * Copyright (C) 2006 Samuel Weinig <sam.weinig@gmail.com>
5  * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
6  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #include "config.h"
25
26 #if ENABLE(SVG) && ENABLE(FILTERS)
27 #include "SVGFilterElement.h"
28
29 #include "Attr.h"
30 #include "RenderSVGResourceFilter.h"
31 #include "SVGElementInstance.h"
32 #include "SVGFilterBuilder.h"
33 #include "SVGFilterPrimitiveStandardAttributes.h"
34 #include "SVGNames.h"
35 #include "SVGParserUtilities.h"
36
37 namespace WebCore {
38
39 // Animated property definitions
40 DEFINE_ANIMATED_ENUMERATION(SVGFilterElement, SVGNames::filterUnitsAttr, FilterUnits, filterUnits, SVGUnitTypes::SVGUnitType)
41 DEFINE_ANIMATED_ENUMERATION(SVGFilterElement, SVGNames::primitiveUnitsAttr, PrimitiveUnits, primitiveUnits, SVGUnitTypes::SVGUnitType)
42 DEFINE_ANIMATED_LENGTH(SVGFilterElement, SVGNames::xAttr, X, x)
43 DEFINE_ANIMATED_LENGTH(SVGFilterElement, SVGNames::yAttr, Y, y)
44 DEFINE_ANIMATED_LENGTH(SVGFilterElement, SVGNames::widthAttr, Width, width)
45 DEFINE_ANIMATED_LENGTH(SVGFilterElement, SVGNames::heightAttr, Height, height)
46 DEFINE_ANIMATED_INTEGER_MULTIPLE_WRAPPERS(SVGFilterElement, SVGNames::filterResAttr, filterResXIdentifier(), FilterResX, filterResX)
47 DEFINE_ANIMATED_INTEGER_MULTIPLE_WRAPPERS(SVGFilterElement, SVGNames::filterResAttr, filterResYIdentifier(), FilterResY, filterResY)
48 DEFINE_ANIMATED_STRING(SVGFilterElement, XLinkNames::hrefAttr, Href, href)
49 DEFINE_ANIMATED_BOOLEAN(SVGFilterElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
50
51 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFilterElement)
52     REGISTER_LOCAL_ANIMATED_PROPERTY(filterUnits)
53     REGISTER_LOCAL_ANIMATED_PROPERTY(primitiveUnits)
54     REGISTER_LOCAL_ANIMATED_PROPERTY(x)
55     REGISTER_LOCAL_ANIMATED_PROPERTY(y)
56     REGISTER_LOCAL_ANIMATED_PROPERTY(width)
57     REGISTER_LOCAL_ANIMATED_PROPERTY(height)
58     REGISTER_LOCAL_ANIMATED_PROPERTY(filterResX)
59     REGISTER_LOCAL_ANIMATED_PROPERTY(filterResY)
60     REGISTER_LOCAL_ANIMATED_PROPERTY(href)
61     REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
62 END_REGISTER_ANIMATED_PROPERTIES
63
64 inline SVGFilterElement::SVGFilterElement(const QualifiedName& tagName, Document* document)
65     : SVGStyledElement(tagName, document)
66     , m_filterUnits(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
67     , m_primitiveUnits(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE)
68     , m_x(LengthModeWidth, "-10%")
69     , m_y(LengthModeHeight, "-10%")
70     , m_width(LengthModeWidth, "120%")
71     , m_height(LengthModeHeight, "120%")
72 {
73     // Spec: If the x/y attribute is not specified, the effect is as if a value of "-10%" were specified.
74     // Spec: If the width/height attribute is not specified, the effect is as if a value of "120%" were specified.
75     ASSERT(hasTagName(SVGNames::filterTag));
76     registerAnimatedPropertiesForSVGFilterElement();
77 }
78
79 PassRefPtr<SVGFilterElement> SVGFilterElement::create(const QualifiedName& tagName, Document* document)
80 {
81     return adoptRef(new SVGFilterElement(tagName, document));
82 }
83
84 const AtomicString& SVGFilterElement::filterResXIdentifier()
85 {
86     DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGFilterResX"));
87     return s_identifier;
88 }
89
90 const AtomicString& SVGFilterElement::filterResYIdentifier()
91 {
92     DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGFilterResY"));
93     return s_identifier;
94 }
95
96 void SVGFilterElement::setFilterRes(unsigned long filterResX, unsigned long filterResY)
97 {
98     setFilterResXBaseValue(filterResX);
99     setFilterResYBaseValue(filterResY);
100
101     if (RenderObject* object = renderer())
102         object->setNeedsLayout(true);
103 }
104
105 bool SVGFilterElement::isSupportedAttribute(const QualifiedName& attrName)
106 {
107     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
108     if (supportedAttributes.isEmpty()) {
109         SVGURIReference::addSupportedAttributes(supportedAttributes);
110         SVGLangSpace::addSupportedAttributes(supportedAttributes);
111         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
112         supportedAttributes.add(SVGNames::filterUnitsAttr);
113         supportedAttributes.add(SVGNames::primitiveUnitsAttr);
114         supportedAttributes.add(SVGNames::xAttr);
115         supportedAttributes.add(SVGNames::yAttr);
116         supportedAttributes.add(SVGNames::widthAttr);
117         supportedAttributes.add(SVGNames::heightAttr);
118         supportedAttributes.add(SVGNames::filterResAttr);
119     }
120     return supportedAttributes.contains(attrName);
121 }
122
123 void SVGFilterElement::parseMappedAttribute(Attribute* attr)
124 {
125     SVGParsingError parseError = NoError;
126     const AtomicString& value = attr->value();
127
128     if (!isSupportedAttribute(attr->name()))
129         SVGStyledElement::parseMappedAttribute(attr);
130     else if (attr->name() == SVGNames::filterUnitsAttr) {
131         SVGUnitTypes::SVGUnitType propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(value);
132         if (propertyValue > 0)
133             setFilterUnitsBaseValue(propertyValue);
134     } else if (attr->name() == SVGNames::primitiveUnitsAttr) {
135         SVGUnitTypes::SVGUnitType propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(value);
136         if (propertyValue > 0)
137             setPrimitiveUnitsBaseValue(propertyValue);
138     } else if (attr->name() == SVGNames::xAttr)
139         setXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
140     else if (attr->name() == SVGNames::yAttr)
141         setYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
142     else if (attr->name() == SVGNames::widthAttr)
143         setWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
144     else if (attr->name() == SVGNames::heightAttr)
145         setHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
146     else if (attr->name() == SVGNames::filterResAttr) {
147         float x, y;
148         if (parseNumberOptionalNumber(value, x, y)) {
149             setFilterResXBaseValue(x);
150             setFilterResYBaseValue(y);
151         }
152     } else if (SVGURIReference::parseMappedAttribute(attr)
153              || SVGLangSpace::parseMappedAttribute(attr)
154              || SVGExternalResourcesRequired::parseMappedAttribute(attr)) {
155     } else
156         ASSERT_NOT_REACHED();
157
158     reportAttributeParsingError(parseError, attr);
159 }
160
161 void SVGFilterElement::svgAttributeChanged(const QualifiedName& attrName)
162 {
163     if (!isSupportedAttribute(attrName)) {
164         SVGStyledElement::svgAttributeChanged(attrName);
165         return;
166     }
167
168     SVGElementInstance::InvalidationGuard invalidationGuard(this);
169     
170     if (attrName == SVGNames::xAttr
171         || attrName == SVGNames::yAttr
172         || attrName == SVGNames::widthAttr
173         || attrName == SVGNames::heightAttr)
174         updateRelativeLengthsInformation();
175
176     if (RenderObject* object = renderer())
177         object->setNeedsLayout(true);
178 }
179
180 void SVGFilterElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
181 {
182     SVGStyledElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
183
184     if (changedByParser)
185         return;
186
187     if (RenderObject* object = renderer())
188         object->setNeedsLayout(true);
189 }
190
191 FloatRect SVGFilterElement::filterBoundingBox(const FloatRect& objectBoundingBox) const
192 {
193     FloatRect filterBBox;
194     if (filterUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
195         filterBBox = FloatRect(x().valueAsPercentage() * objectBoundingBox.width() + objectBoundingBox.x(),
196                                y().valueAsPercentage() * objectBoundingBox.height() + objectBoundingBox.y(),
197                                width().valueAsPercentage() * objectBoundingBox.width(),
198                                height().valueAsPercentage() * objectBoundingBox.height());
199     else
200         filterBBox = FloatRect(x().value(this),
201                                y().value(this),
202                                width().value(this),
203                                height().value(this));
204
205     return filterBBox;
206 }
207
208 RenderObject* SVGFilterElement::createRenderer(RenderArena* arena, RenderStyle*)
209 {
210     return new (arena) RenderSVGResourceFilter(this);
211 }
212
213 bool SVGFilterElement::selfHasRelativeLengths() const
214 {
215     return x().isRelative()
216         || y().isRelative()
217         || width().isRelative()
218         || height().isRelative();
219 }
220
221 }
222
223 #endif