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.
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.
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.
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.
26 #if ENABLE(SVG) && ENABLE(FILTERS)
27 #include "SVGFilterElement.h"
30 #include "RenderSVGResourceFilter.h"
31 #include "SVGElementInstance.h"
32 #include "SVGFilterBuilder.h"
33 #include "SVGFilterPrimitiveStandardAttributes.h"
35 #include "SVGParserUtilities.h"
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)
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
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%")
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();
79 PassRefPtr<SVGFilterElement> SVGFilterElement::create(const QualifiedName& tagName, Document* document)
81 return adoptRef(new SVGFilterElement(tagName, document));
84 const AtomicString& SVGFilterElement::filterResXIdentifier()
86 DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGFilterResX"));
90 const AtomicString& SVGFilterElement::filterResYIdentifier()
92 DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGFilterResY"));
96 void SVGFilterElement::setFilterRes(unsigned long filterResX, unsigned long filterResY)
98 setFilterResXBaseValue(filterResX);
99 setFilterResYBaseValue(filterResY);
101 if (RenderObject* object = renderer())
102 object->setNeedsLayout(true);
105 bool SVGFilterElement::isSupportedAttribute(const QualifiedName& attrName)
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);
120 return supportedAttributes.contains(attrName);
123 void SVGFilterElement::parseMappedAttribute(Attribute* attr)
125 SVGParsingError parseError = NoError;
126 const AtomicString& value = attr->value();
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) {
148 if (parseNumberOptionalNumber(value, x, y)) {
149 setFilterResXBaseValue(x);
150 setFilterResYBaseValue(y);
152 } else if (SVGURIReference::parseMappedAttribute(attr)
153 || SVGLangSpace::parseMappedAttribute(attr)
154 || SVGExternalResourcesRequired::parseMappedAttribute(attr)) {
156 ASSERT_NOT_REACHED();
158 reportAttributeParsingError(parseError, attr);
161 void SVGFilterElement::svgAttributeChanged(const QualifiedName& attrName)
163 if (!isSupportedAttribute(attrName)) {
164 SVGStyledElement::svgAttributeChanged(attrName);
168 SVGElementInstance::InvalidationGuard invalidationGuard(this);
170 if (attrName == SVGNames::xAttr
171 || attrName == SVGNames::yAttr
172 || attrName == SVGNames::widthAttr
173 || attrName == SVGNames::heightAttr)
174 updateRelativeLengthsInformation();
176 if (RenderObject* object = renderer())
177 object->setNeedsLayout(true);
180 void SVGFilterElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
182 SVGStyledElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
187 if (RenderObject* object = renderer())
188 object->setNeedsLayout(true);
191 FloatRect SVGFilterElement::filterBoundingBox(const FloatRect& objectBoundingBox) const
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());
200 filterBBox = FloatRect(x().value(this),
203 height().value(this));
208 RenderObject* SVGFilterElement::createRenderer(RenderArena* arena, RenderStyle*)
210 return new (arena) RenderSVGResourceFilter(this);
213 bool SVGFilterElement::selfHasRelativeLengths() const
215 return x().isRelative()
217 || width().isRelative()
218 || height().isRelative();