Update To 11.40.268.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 "core/SVGNames.h"
25 #include "platform/graphics/filters/FilterEffect.h"
26 #include "core/svg/SVGParserUtilities.h"
27 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
28
29 namespace blink {
30
31 template<> const SVGEnumerationStringEntries& getStaticStringEntries<MorphologyOperatorType>()
32 {
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"));
37     }
38     return entries;
39 }
40
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))
46 {
47     addToPropertyMap(m_radius);
48     addToPropertyMap(m_in1);
49     addToPropertyMap(m_svgOperator);
50 }
51
52 DEFINE_NODE_FACTORY(SVGFEMorphologyElement)
53
54 bool SVGFEMorphologyElement::isSupportedAttribute(const QualifiedName& attrName)
55 {
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);
61     }
62     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
63 }
64
65 void SVGFEMorphologyElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
66 {
67     parseAttributeNew(name, value);
68 }
69
70 bool SVGFEMorphologyElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
71 {
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;
80     }
81
82     ASSERT_NOT_REACHED();
83     return false;
84 }
85
86 void SVGFEMorphologyElement::svgAttributeChanged(const QualifiedName& attrName)
87 {
88     if (!isSupportedAttribute(attrName)) {
89         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
90         return;
91     }
92
93     SVGElement::InvalidationGuard invalidationGuard(this);
94
95     if (attrName == SVGNames::operatorAttr || attrName == SVGNames::radiusAttr) {
96         primitiveAttributeChanged(attrName);
97         return;
98     }
99
100     if (attrName == SVGNames::inAttr) {
101         invalidate();
102         return;
103     }
104
105     ASSERT_NOT_REACHED();
106 }
107
108 PassRefPtr<FilterEffect> SVGFEMorphologyElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
109 {
110     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
111     float xRadius = radiusX()->currentValue()->value();
112     float yRadius = radiusY()->currentValue()->value();
113
114     if (!input1)
115         return nullptr;
116
117     if (xRadius < 0 || yRadius < 0)
118         return nullptr;
119
120     RefPtr<FilterEffect> effect = FEMorphology::create(filter, m_svgOperator->currentValue()->enumValue(), xRadius, yRadius);
121     effect->inputEffects().append(input1);
122     return effect.release();
123 }
124
125 } // namespace blink