2 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "core/svg/SVGFEMorphologyElement.h"
25 #include "platform/graphics/filters/FilterEffect.h"
26 #include "core/svg/SVGElementInstance.h"
27 #include "core/svg/SVGParserUtilities.h"
28 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
32 // Animated property definitions
33 DEFINE_ANIMATED_ENUMERATION(SVGFEMorphologyElement, SVGNames::operatorAttr, SVGOperator, svgOperator, MorphologyOperatorType)
35 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEMorphologyElement)
36 REGISTER_LOCAL_ANIMATED_PROPERTY(svgOperator)
37 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
38 END_REGISTER_ANIMATED_PROPERTIES
40 inline SVGFEMorphologyElement::SVGFEMorphologyElement(Document& document)
41 : SVGFilterPrimitiveStandardAttributes(SVGNames::feMorphologyTag, document)
42 , m_radius(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::radiusAttr))
43 , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
44 , m_svgOperator(FEMORPHOLOGY_OPERATOR_ERODE)
46 ScriptWrappable::init(this);
48 addToPropertyMap(m_radius);
49 addToPropertyMap(m_in1);
50 registerAnimatedPropertiesForSVGFEMorphologyElement();
53 PassRefPtr<SVGFEMorphologyElement> SVGFEMorphologyElement::create(Document& document)
55 return adoptRef(new SVGFEMorphologyElement(document));
58 void SVGFEMorphologyElement::setRadius(float x, float y)
60 radiusX()->baseValue()->setValue(x);
61 radiusY()->baseValue()->setValue(y);
65 bool SVGFEMorphologyElement::isSupportedAttribute(const QualifiedName& attrName)
67 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
68 if (supportedAttributes.isEmpty()) {
69 supportedAttributes.add(SVGNames::inAttr);
70 supportedAttributes.add(SVGNames::operatorAttr);
71 supportedAttributes.add(SVGNames::radiusAttr);
73 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
76 void SVGFEMorphologyElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
78 if (!isSupportedAttribute(name)) {
79 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
83 if (name == SVGNames::operatorAttr) {
84 MorphologyOperatorType propertyValue = SVGPropertyTraits<MorphologyOperatorType>::fromString(value);
85 if (propertyValue > 0)
86 setSVGOperatorBaseValue(propertyValue);
90 SVGParsingError parseError = NoError;
92 if (name == SVGNames::inAttr)
93 m_in1->setBaseValueAsString(value, parseError);
94 else if (name == SVGNames::radiusAttr)
95 m_radius->setBaseValueAsString(value, parseError);
99 reportAttributeParsingError(parseError, name, value);
102 bool SVGFEMorphologyElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
104 FEMorphology* morphology = static_cast<FEMorphology*>(effect);
105 if (attrName == SVGNames::operatorAttr)
106 return morphology->setMorphologyOperator(svgOperatorCurrentValue());
107 if (attrName == SVGNames::radiusAttr) {
108 // Both setRadius functions should be evaluated separately.
109 bool isRadiusXChanged = morphology->setRadiusX(radiusX()->currentValue()->value());
110 bool isRadiusYChanged = morphology->setRadiusY(radiusY()->currentValue()->value());
111 return isRadiusXChanged || isRadiusYChanged;
114 ASSERT_NOT_REACHED();
118 void SVGFEMorphologyElement::svgAttributeChanged(const QualifiedName& attrName)
120 if (!isSupportedAttribute(attrName)) {
121 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
125 SVGElementInstance::InvalidationGuard invalidationGuard(this);
127 if (attrName == SVGNames::operatorAttr || attrName == SVGNames::radiusAttr) {
128 primitiveAttributeChanged(attrName);
132 if (attrName == SVGNames::inAttr) {
137 ASSERT_NOT_REACHED();
140 PassRefPtr<FilterEffect> SVGFEMorphologyElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
142 FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
143 float xRadius = radiusX()->currentValue()->value();
144 float yRadius = radiusY()->currentValue()->value();
149 if (xRadius < 0 || yRadius < 0)
152 RefPtr<FilterEffect> effect = FEMorphology::create(filter, svgOperatorCurrentValue(), xRadius, yRadius);
153 effect->inputEffects().append(input1);
154 return effect.release();
157 } // namespace WebCore