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"
24 #include "core/SVGNames.h"
25 #include "platform/graphics/filters/FilterEffect.h"
26 #include "core/svg/SVGParserUtilities.h"
27 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
31 template<> const SVGEnumerationStringEntries& getStaticStringEntries<MorphologyOperatorType>()
33 DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
34 if (entries.isEmpty()) {
35 entries.append(std::make_pair(FEMORPHOLOGY_OPERATOR_ERODE, "erode"));
36 entries.append(std::make_pair(FEMORPHOLOGY_OPERATOR_DILATE, "dilate"));
41 inline SVGFEMorphologyElement::SVGFEMorphologyElement(Document& document)
42 : SVGFilterPrimitiveStandardAttributes(SVGNames::feMorphologyTag, document)
43 , m_radius(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::radiusAttr))
44 , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
45 , m_svgOperator(SVGAnimatedEnumeration<MorphologyOperatorType>::create(this, SVGNames::operatorAttr, FEMORPHOLOGY_OPERATOR_ERODE))
47 addToPropertyMap(m_radius);
48 addToPropertyMap(m_in1);
49 addToPropertyMap(m_svgOperator);
52 DEFINE_NODE_FACTORY(SVGFEMorphologyElement)
54 bool SVGFEMorphologyElement::isSupportedAttribute(const QualifiedName& attrName)
56 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
57 if (supportedAttributes.isEmpty()) {
58 supportedAttributes.add(SVGNames::inAttr);
59 supportedAttributes.add(SVGNames::operatorAttr);
60 supportedAttributes.add(SVGNames::radiusAttr);
62 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
65 void SVGFEMorphologyElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
67 parseAttributeNew(name, value);
70 bool SVGFEMorphologyElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
72 FEMorphology* morphology = static_cast<FEMorphology*>(effect);
73 if (attrName == SVGNames::operatorAttr)
74 return morphology->setMorphologyOperator(m_svgOperator->currentValue()->enumValue());
75 if (attrName == SVGNames::radiusAttr) {
76 // Both setRadius functions should be evaluated separately.
77 bool isRadiusXChanged = morphology->setRadiusX(radiusX()->currentValue()->value());
78 bool isRadiusYChanged = morphology->setRadiusY(radiusY()->currentValue()->value());
79 return isRadiusXChanged || isRadiusYChanged;
86 void SVGFEMorphologyElement::svgAttributeChanged(const QualifiedName& attrName)
88 if (!isSupportedAttribute(attrName)) {
89 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
93 SVGElement::InvalidationGuard invalidationGuard(this);
95 if (attrName == SVGNames::operatorAttr || attrName == SVGNames::radiusAttr) {
96 primitiveAttributeChanged(attrName);
100 if (attrName == SVGNames::inAttr) {
105 ASSERT_NOT_REACHED();
108 PassRefPtr<FilterEffect> SVGFEMorphologyElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
110 FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
111 float xRadius = radiusX()->currentValue()->value();
112 float yRadius = radiusY()->currentValue()->value();
117 if (xRadius < 0 || yRadius < 0)
120 RefPtr<FilterEffect> effect = FEMorphology::create(filter, m_svgOperator->currentValue()->enumValue(), xRadius, yRadius);
121 effect->inputEffects().append(input1);
122 return effect.release();