Upstream version 7.35.139.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 template<> const SVGEnumerationStringEntries& getStaticStringEntries<MorphologyOperatorType>()
33 {
34     DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
35     if (entries.isEmpty()) {
36         entries.append(std::make_pair(FEMORPHOLOGY_OPERATOR_ERODE, "erode"));
37         entries.append(std::make_pair(FEMORPHOLOGY_OPERATOR_DILATE, "dilate"));
38     }
39     return entries;
40 }
41
42 inline SVGFEMorphologyElement::SVGFEMorphologyElement(Document& document)
43     : SVGFilterPrimitiveStandardAttributes(SVGNames::feMorphologyTag, document)
44     , m_radius(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::radiusAttr))
45     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
46     , m_svgOperator(SVGAnimatedEnumeration<MorphologyOperatorType>::create(this, SVGNames::operatorAttr, FEMORPHOLOGY_OPERATOR_ERODE))
47 {
48     ScriptWrappable::init(this);
49
50     addToPropertyMap(m_radius);
51     addToPropertyMap(m_in1);
52     addToPropertyMap(m_svgOperator);
53 }
54
55 PassRefPtr<SVGFEMorphologyElement> SVGFEMorphologyElement::create(Document& document)
56 {
57     return adoptRef(new SVGFEMorphologyElement(document));
58 }
59
60 void SVGFEMorphologyElement::setRadius(float x, float y)
61 {
62     radiusX()->baseValue()->setValue(x);
63     radiusY()->baseValue()->setValue(y);
64     invalidate();
65 }
66
67 bool SVGFEMorphologyElement::isSupportedAttribute(const QualifiedName& attrName)
68 {
69     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
70     if (supportedAttributes.isEmpty()) {
71         supportedAttributes.add(SVGNames::inAttr);
72         supportedAttributes.add(SVGNames::operatorAttr);
73         supportedAttributes.add(SVGNames::radiusAttr);
74     }
75     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
76 }
77
78 void SVGFEMorphologyElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
79 {
80     if (!isSupportedAttribute(name)) {
81         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
82         return;
83     }
84
85     SVGParsingError parseError = NoError;
86
87     if (name == SVGNames::inAttr)
88         m_in1->setBaseValueAsString(value, parseError);
89     else if (name == SVGNames::radiusAttr)
90         m_radius->setBaseValueAsString(value, parseError);
91     else if (name == SVGNames::operatorAttr)
92         m_svgOperator->setBaseValueAsString(value, parseError);
93     else
94         ASSERT_NOT_REACHED();
95
96     reportAttributeParsingError(parseError, name, value);
97 }
98
99 bool SVGFEMorphologyElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
100 {
101     FEMorphology* morphology = static_cast<FEMorphology*>(effect);
102     if (attrName == SVGNames::operatorAttr)
103         return morphology->setMorphologyOperator(m_svgOperator->currentValue()->enumValue());
104     if (attrName == SVGNames::radiusAttr) {
105         // Both setRadius functions should be evaluated separately.
106         bool isRadiusXChanged = morphology->setRadiusX(radiusX()->currentValue()->value());
107         bool isRadiusYChanged = morphology->setRadiusY(radiusY()->currentValue()->value());
108         return isRadiusXChanged || isRadiusYChanged;
109     }
110
111     ASSERT_NOT_REACHED();
112     return false;
113 }
114
115 void SVGFEMorphologyElement::svgAttributeChanged(const QualifiedName& attrName)
116 {
117     if (!isSupportedAttribute(attrName)) {
118         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
119         return;
120     }
121
122     SVGElementInstance::InvalidationGuard invalidationGuard(this);
123
124     if (attrName == SVGNames::operatorAttr || attrName == SVGNames::radiusAttr) {
125         primitiveAttributeChanged(attrName);
126         return;
127     }
128
129     if (attrName == SVGNames::inAttr) {
130         invalidate();
131         return;
132     }
133
134     ASSERT_NOT_REACHED();
135 }
136
137 PassRefPtr<FilterEffect> SVGFEMorphologyElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
138 {
139     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
140     float xRadius = radiusX()->currentValue()->value();
141     float yRadius = radiusY()->currentValue()->value();
142
143     if (!input1)
144         return nullptr;
145
146     if (xRadius < 0 || yRadius < 0)
147         return nullptr;
148
149     RefPtr<FilterEffect> effect = FEMorphology::create(filter, m_svgOperator->currentValue()->enumValue(), xRadius, yRadius);
150     effect->inputEffects().append(input1);
151     return effect.release();
152 }
153
154 } // namespace WebCore