Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGFEDisplacementMapElement.cpp
1 /*
2  * Copyright (C) 2006 Oliver Hunt <oliver@nerget.com>
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/SVGFEDisplacementMapElement.h"
23
24 #include "SVGNames.h"
25 #include "platform/graphics/filters/FilterEffect.h"
26 #include "core/svg/SVGElementInstance.h"
27 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
28
29 namespace WebCore {
30
31 template<> const SVGEnumerationStringEntries& getStaticStringEntries<ChannelSelectorType>()
32 {
33     DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
34     if (entries.isEmpty()) {
35         entries.append(std::make_pair(CHANNEL_R, "R"));
36         entries.append(std::make_pair(CHANNEL_G, "G"));
37         entries.append(std::make_pair(CHANNEL_B, "B"));
38         entries.append(std::make_pair(CHANNEL_A, "A"));
39     }
40     return entries;
41 }
42
43 inline SVGFEDisplacementMapElement::SVGFEDisplacementMapElement(Document& document)
44     : SVGFilterPrimitiveStandardAttributes(SVGNames::feDisplacementMapTag, document)
45     , m_scale(SVGAnimatedNumber::create(this, SVGNames::scaleAttr, SVGNumber::create(0)))
46     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
47     , m_in2(SVGAnimatedString::create(this, SVGNames::in2Attr, SVGString::create()))
48     , m_xChannelSelector(SVGAnimatedEnumeration<ChannelSelectorType>::create(this, SVGNames::xChannelSelectorAttr, CHANNEL_A))
49     , m_yChannelSelector(SVGAnimatedEnumeration<ChannelSelectorType>::create(this, SVGNames::yChannelSelectorAttr, CHANNEL_A))
50 {
51     ScriptWrappable::init(this);
52
53     addToPropertyMap(m_scale);
54     addToPropertyMap(m_in1);
55     addToPropertyMap(m_in2);
56     addToPropertyMap(m_xChannelSelector);
57     addToPropertyMap(m_yChannelSelector);
58 }
59
60 PassRefPtr<SVGFEDisplacementMapElement> SVGFEDisplacementMapElement::create(Document& document)
61 {
62     return adoptRef(new SVGFEDisplacementMapElement(document));
63 }
64
65 bool SVGFEDisplacementMapElement::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::in2Attr);
71         supportedAttributes.add(SVGNames::xChannelSelectorAttr);
72         supportedAttributes.add(SVGNames::yChannelSelectorAttr);
73         supportedAttributes.add(SVGNames::scaleAttr);
74     }
75     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
76 }
77
78 void SVGFEDisplacementMapElement::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::in2Attr)
90         m_in2->setBaseValueAsString(value, parseError);
91     else if (name == SVGNames::scaleAttr)
92         m_scale->setBaseValueAsString(value, parseError);
93     else if (name == SVGNames::xChannelSelectorAttr)
94         m_xChannelSelector->setBaseValueAsString(value, parseError);
95     else if (name == SVGNames::yChannelSelectorAttr)
96         m_yChannelSelector->setBaseValueAsString(value, parseError);
97     else
98         ASSERT_NOT_REACHED();
99
100     reportAttributeParsingError(parseError, name, value);
101 }
102
103 bool SVGFEDisplacementMapElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
104 {
105     FEDisplacementMap* displacementMap = static_cast<FEDisplacementMap*>(effect);
106     if (attrName == SVGNames::xChannelSelectorAttr)
107         return displacementMap->setXChannelSelector(m_xChannelSelector->currentValue()->enumValue());
108     if (attrName == SVGNames::yChannelSelectorAttr)
109         return displacementMap->setYChannelSelector(m_yChannelSelector->currentValue()->enumValue());
110     if (attrName == SVGNames::scaleAttr)
111         return displacementMap->setScale(m_scale->currentValue()->value());
112
113     ASSERT_NOT_REACHED();
114     return false;
115 }
116
117 void SVGFEDisplacementMapElement::svgAttributeChanged(const QualifiedName& attrName)
118 {
119     if (!isSupportedAttribute(attrName)) {
120         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
121         return;
122     }
123
124     SVGElementInstance::InvalidationGuard invalidationGuard(this);
125
126     if (attrName == SVGNames::xChannelSelectorAttr || attrName == SVGNames::yChannelSelectorAttr || attrName == SVGNames::scaleAttr) {
127         primitiveAttributeChanged(attrName);
128         return;
129     }
130
131     if (attrName == SVGNames::inAttr || attrName == SVGNames::in2Attr) {
132         invalidate();
133         return;
134     }
135
136     ASSERT_NOT_REACHED();
137 }
138
139 PassRefPtr<FilterEffect> SVGFEDisplacementMapElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
140 {
141     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
142     FilterEffect* input2 = filterBuilder->getEffectById(AtomicString(m_in2->currentValue()->value()));
143
144     if (!input1 || !input2)
145         return nullptr;
146
147     RefPtr<FilterEffect> effect = FEDisplacementMap::create(filter, m_xChannelSelector->currentValue()->enumValue(), m_yChannelSelector->currentValue()->enumValue(), m_scale->currentValue()->value());
148     FilterEffectVector& inputEffects = effect->inputEffects();
149     inputEffects.reserveCapacity(2);
150     inputEffects.append(input1);
151     inputEffects.append(input2);
152     return effect.release();
153 }
154
155 }