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 template<> const SVGEnumerationStringEntries& getStaticStringEntries<MorphologyOperatorType>()
34 DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
35 if (entries.isEmpty()) {
36 entries.append(std::make_pair(FEMORPHOLOGY_OPERATOR_UNKNOWN, emptyString()));
37 entries.append(std::make_pair(FEMORPHOLOGY_OPERATOR_ERODE, "erode"));
38 entries.append(std::make_pair(FEMORPHOLOGY_OPERATOR_DILATE, "dilate"));
43 inline SVGFEMorphologyElement::SVGFEMorphologyElement(Document& document)
44 : SVGFilterPrimitiveStandardAttributes(SVGNames::feMorphologyTag, document)
45 , m_radius(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::radiusAttr))
46 , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
47 , m_svgOperator(SVGAnimatedEnumeration<MorphologyOperatorType>::create(this, SVGNames::operatorAttr, FEMORPHOLOGY_OPERATOR_ERODE))
49 ScriptWrappable::init(this);
51 addToPropertyMap(m_radius);
52 addToPropertyMap(m_in1);
53 addToPropertyMap(m_svgOperator);
56 PassRefPtr<SVGFEMorphologyElement> SVGFEMorphologyElement::create(Document& document)
58 return adoptRef(new SVGFEMorphologyElement(document));
61 void SVGFEMorphologyElement::setRadius(float x, float y)
63 radiusX()->baseValue()->setValue(x);
64 radiusY()->baseValue()->setValue(y);
68 bool SVGFEMorphologyElement::isSupportedAttribute(const QualifiedName& attrName)
70 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
71 if (supportedAttributes.isEmpty()) {
72 supportedAttributes.add(SVGNames::inAttr);
73 supportedAttributes.add(SVGNames::operatorAttr);
74 supportedAttributes.add(SVGNames::radiusAttr);
76 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
79 void SVGFEMorphologyElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
81 if (!isSupportedAttribute(name)) {
82 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
86 SVGParsingError parseError = NoError;
88 if (name == SVGNames::inAttr)
89 m_in1->setBaseValueAsString(value, parseError);
90 else if (name == SVGNames::radiusAttr)
91 m_radius->setBaseValueAsString(value, parseError);
92 else if (name == SVGNames::operatorAttr)
93 m_svgOperator->setBaseValueAsString(value, parseError);
97 reportAttributeParsingError(parseError, name, value);
100 bool SVGFEMorphologyElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
102 FEMorphology* morphology = static_cast<FEMorphology*>(effect);
103 if (attrName == SVGNames::operatorAttr)
104 return morphology->setMorphologyOperator(m_svgOperator->currentValue()->enumValue());
105 if (attrName == SVGNames::radiusAttr) {
106 // Both setRadius functions should be evaluated separately.
107 bool isRadiusXChanged = morphology->setRadiusX(radiusX()->currentValue()->value());
108 bool isRadiusYChanged = morphology->setRadiusY(radiusY()->currentValue()->value());
109 return isRadiusXChanged || isRadiusYChanged;
112 ASSERT_NOT_REACHED();
116 void SVGFEMorphologyElement::svgAttributeChanged(const QualifiedName& attrName)
118 if (!isSupportedAttribute(attrName)) {
119 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
123 SVGElementInstance::InvalidationGuard invalidationGuard(this);
125 if (attrName == SVGNames::operatorAttr || attrName == SVGNames::radiusAttr) {
126 primitiveAttributeChanged(attrName);
130 if (attrName == SVGNames::inAttr) {
135 ASSERT_NOT_REACHED();
138 PassRefPtr<FilterEffect> SVGFEMorphologyElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
140 FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
141 float xRadius = radiusX()->currentValue()->value();
142 float yRadius = radiusY()->currentValue()->value();
147 if (xRadius < 0 || yRadius < 0)
150 RefPtr<FilterEffect> effect = FEMorphology::create(filter, m_svgOperator->currentValue()->enumValue(), xRadius, yRadius);
151 effect->inputEffects().append(input1);
152 return effect.release();
155 } // namespace WebCore