Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGFEMorphologyElement.cpp
1 /*
2  * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21
22 #include "core/svg/SVGFEMorphologyElement.h"
23
24 #include "SVGNames.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"
29
30 namespace WebCore {
31
32 // Animated property definitions
33 DEFINE_ANIMATED_ENUMERATION(SVGFEMorphologyElement, SVGNames::operatorAttr, SVGOperator, svgOperator, MorphologyOperatorType)
34
35 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEMorphologyElement)
36     REGISTER_LOCAL_ANIMATED_PROPERTY(svgOperator)
37     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
38 END_REGISTER_ANIMATED_PROPERTIES
39
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)
45 {
46     ScriptWrappable::init(this);
47
48     addToPropertyMap(m_radius);
49     addToPropertyMap(m_in1);
50     registerAnimatedPropertiesForSVGFEMorphologyElement();
51 }
52
53 PassRefPtr<SVGFEMorphologyElement> SVGFEMorphologyElement::create(Document& document)
54 {
55     return adoptRef(new SVGFEMorphologyElement(document));
56 }
57
58 void SVGFEMorphologyElement::setRadius(float x, float y)
59 {
60     radiusX()->baseValue()->setValue(x);
61     radiusY()->baseValue()->setValue(y);
62     invalidate();
63 }
64
65 bool SVGFEMorphologyElement::isSupportedAttribute(const QualifiedName& attrName)
66 {
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);
72     }
73     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
74 }
75
76 void SVGFEMorphologyElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
77 {
78     if (!isSupportedAttribute(name)) {
79         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
80         return;
81     }
82
83     if (name == SVGNames::operatorAttr) {
84         MorphologyOperatorType propertyValue = SVGPropertyTraits<MorphologyOperatorType>::fromString(value);
85         if (propertyValue > 0)
86             setSVGOperatorBaseValue(propertyValue);
87         return;
88     }
89
90     SVGParsingError parseError = NoError;
91
92     if (name == SVGNames::inAttr)
93         m_in1->setBaseValueAsString(value, parseError);
94     else if (name == SVGNames::radiusAttr)
95         m_radius->setBaseValueAsString(value, parseError);
96     else
97         ASSERT_NOT_REACHED();
98
99     reportAttributeParsingError(parseError, name, value);
100 }
101
102 bool SVGFEMorphologyElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
103 {
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;
112     }
113
114     ASSERT_NOT_REACHED();
115     return false;
116 }
117
118 void SVGFEMorphologyElement::svgAttributeChanged(const QualifiedName& attrName)
119 {
120     if (!isSupportedAttribute(attrName)) {
121         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
122         return;
123     }
124
125     SVGElementInstance::InvalidationGuard invalidationGuard(this);
126
127     if (attrName == SVGNames::operatorAttr || attrName == SVGNames::radiusAttr) {
128         primitiveAttributeChanged(attrName);
129         return;
130     }
131
132     if (attrName == SVGNames::inAttr) {
133         invalidate();
134         return;
135     }
136
137     ASSERT_NOT_REACHED();
138 }
139
140 PassRefPtr<FilterEffect> SVGFEMorphologyElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
141 {
142     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
143     float xRadius = radiusX()->currentValue()->value();
144     float yRadius = radiusY()->currentValue()->value();
145
146     if (!input1)
147         return 0;
148
149     if (xRadius < 0 || yRadius < 0)
150         return 0;
151
152     RefPtr<FilterEffect> effect = FEMorphology::create(filter, svgOperatorCurrentValue(), xRadius, yRadius);
153     effect->inputEffects().append(input1);
154     return effect.release();
155 }
156
157 } // namespace WebCore