882f8e0857a4cb50c37f5fa9fd91a4a8322f75b3
[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_UNKNOWN, emptyString()));
37         entries.append(std::make_pair(FEMORPHOLOGY_OPERATOR_ERODE, "erode"));
38         entries.append(std::make_pair(FEMORPHOLOGY_OPERATOR_DILATE, "dilate"));
39     }
40     return entries;
41 }
42
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))
48 {
49     ScriptWrappable::init(this);
50
51     addToPropertyMap(m_radius);
52     addToPropertyMap(m_in1);
53     addToPropertyMap(m_svgOperator);
54 }
55
56 PassRefPtr<SVGFEMorphologyElement> SVGFEMorphologyElement::create(Document& document)
57 {
58     return adoptRef(new SVGFEMorphologyElement(document));
59 }
60
61 void SVGFEMorphologyElement::setRadius(float x, float y)
62 {
63     radiusX()->baseValue()->setValue(x);
64     radiusY()->baseValue()->setValue(y);
65     invalidate();
66 }
67
68 bool SVGFEMorphologyElement::isSupportedAttribute(const QualifiedName& attrName)
69 {
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);
75     }
76     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
77 }
78
79 void SVGFEMorphologyElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
80 {
81     if (!isSupportedAttribute(name)) {
82         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
83         return;
84     }
85
86     SVGParsingError parseError = NoError;
87
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);
94     else
95         ASSERT_NOT_REACHED();
96
97     reportAttributeParsingError(parseError, name, value);
98 }
99
100 bool SVGFEMorphologyElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
101 {
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;
110     }
111
112     ASSERT_NOT_REACHED();
113     return false;
114 }
115
116 void SVGFEMorphologyElement::svgAttributeChanged(const QualifiedName& attrName)
117 {
118     if (!isSupportedAttribute(attrName)) {
119         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
120         return;
121     }
122
123     SVGElementInstance::InvalidationGuard invalidationGuard(this);
124
125     if (attrName == SVGNames::operatorAttr || attrName == SVGNames::radiusAttr) {
126         primitiveAttributeChanged(attrName);
127         return;
128     }
129
130     if (attrName == SVGNames::inAttr) {
131         invalidate();
132         return;
133     }
134
135     ASSERT_NOT_REACHED();
136 }
137
138 PassRefPtr<FilterEffect> SVGFEMorphologyElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
139 {
140     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
141     float xRadius = radiusX()->currentValue()->value();
142     float yRadius = radiusY()->currentValue()->value();
143
144     if (!input1)
145         return nullptr;
146
147     if (xRadius < 0 || yRadius < 0)
148         return nullptr;
149
150     RefPtr<FilterEffect> effect = FEMorphology::create(filter, m_svgOperator->currentValue()->enumValue(), xRadius, yRadius);
151     effect->inputEffects().append(input1);
152     return effect.release();
153 }
154
155 } // namespace WebCore