Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGFECompositeElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22
23 #include "core/svg/SVGFECompositeElement.h"
24
25 #include "SVGNames.h"
26 #include "platform/graphics/filters/FilterEffect.h"
27 #include "core/svg/SVGElementInstance.h"
28 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
29
30 namespace WebCore {
31
32 template<> const SVGEnumerationStringEntries& getStaticStringEntries<CompositeOperationType>()
33 {
34     DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
35     if (entries.isEmpty()) {
36         entries.append(std::make_pair(FECOMPOSITE_OPERATOR_OVER, "over"));
37         entries.append(std::make_pair(FECOMPOSITE_OPERATOR_IN, "in"));
38         entries.append(std::make_pair(FECOMPOSITE_OPERATOR_OUT, "out"));
39         entries.append(std::make_pair(FECOMPOSITE_OPERATOR_ATOP, "atop"));
40         entries.append(std::make_pair(FECOMPOSITE_OPERATOR_XOR, "xor"));
41         entries.append(std::make_pair(FECOMPOSITE_OPERATOR_ARITHMETIC, "arithmetic"));
42     }
43     return entries;
44 }
45
46 inline SVGFECompositeElement::SVGFECompositeElement(Document& document)
47     : SVGFilterPrimitiveStandardAttributes(SVGNames::feCompositeTag, document)
48     , m_k1(SVGAnimatedNumber::create(this, SVGNames::k1Attr, SVGNumber::create()))
49     , m_k2(SVGAnimatedNumber::create(this, SVGNames::k2Attr, SVGNumber::create()))
50     , m_k3(SVGAnimatedNumber::create(this, SVGNames::k3Attr, SVGNumber::create()))
51     , m_k4(SVGAnimatedNumber::create(this, SVGNames::k4Attr, SVGNumber::create()))
52     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
53     , m_in2(SVGAnimatedString::create(this, SVGNames::in2Attr, SVGString::create()))
54     , m_svgOperator(SVGAnimatedEnumeration<CompositeOperationType>::create(this, SVGNames::operatorAttr, FECOMPOSITE_OPERATOR_OVER))
55 {
56     ScriptWrappable::init(this);
57
58     addToPropertyMap(m_k1);
59     addToPropertyMap(m_k2);
60     addToPropertyMap(m_k3);
61     addToPropertyMap(m_k4);
62     addToPropertyMap(m_in1);
63     addToPropertyMap(m_in2);
64     addToPropertyMap(m_svgOperator);
65 }
66
67 PassRefPtr<SVGFECompositeElement> SVGFECompositeElement::create(Document& document)
68 {
69     return adoptRef(new SVGFECompositeElement(document));
70 }
71
72 bool SVGFECompositeElement::isSupportedAttribute(const QualifiedName& attrName)
73 {
74     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
75     if (supportedAttributes.isEmpty()) {
76         supportedAttributes.add(SVGNames::inAttr);
77         supportedAttributes.add(SVGNames::in2Attr);
78         supportedAttributes.add(SVGNames::operatorAttr);
79         supportedAttributes.add(SVGNames::k1Attr);
80         supportedAttributes.add(SVGNames::k2Attr);
81         supportedAttributes.add(SVGNames::k3Attr);
82         supportedAttributes.add(SVGNames::k4Attr);
83     }
84     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
85 }
86
87 void SVGFECompositeElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
88 {
89     if (!isSupportedAttribute(name)) {
90         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
91         return;
92     }
93
94     SVGParsingError parseError = NoError;
95
96     if (name == SVGNames::inAttr)
97         m_in1->setBaseValueAsString(value, parseError);
98     else if (name == SVGNames::in2Attr)
99         m_in2->setBaseValueAsString(value, parseError);
100     else if (name == SVGNames::k1Attr)
101         m_k1->setBaseValueAsString(value, parseError);
102     else if (name == SVGNames::k2Attr)
103         m_k2->setBaseValueAsString(value, parseError);
104     else if (name == SVGNames::k3Attr)
105         m_k3->setBaseValueAsString(value, parseError);
106     else if (name == SVGNames::k4Attr)
107         m_k4->setBaseValueAsString(value, parseError);
108     else if (name == SVGNames::operatorAttr)
109         m_svgOperator->setBaseValueAsString(value, parseError);
110     else
111         ASSERT_NOT_REACHED();
112
113     reportAttributeParsingError(parseError, name, value);
114 }
115
116 bool SVGFECompositeElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
117 {
118     FEComposite* composite = static_cast<FEComposite*>(effect);
119     if (attrName == SVGNames::operatorAttr)
120         return composite->setOperation(m_svgOperator->currentValue()->enumValue());
121     if (attrName == SVGNames::k1Attr)
122         return composite->setK1(m_k1->currentValue()->value());
123     if (attrName == SVGNames::k2Attr)
124         return composite->setK2(m_k2->currentValue()->value());
125     if (attrName == SVGNames::k3Attr)
126         return composite->setK3(m_k3->currentValue()->value());
127     if (attrName == SVGNames::k4Attr)
128         return composite->setK4(m_k4->currentValue()->value());
129
130     ASSERT_NOT_REACHED();
131     return false;
132 }
133
134
135 void SVGFECompositeElement::svgAttributeChanged(const QualifiedName& attrName)
136 {
137     if (!isSupportedAttribute(attrName)) {
138         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
139         return;
140     }
141
142     SVGElement::InvalidationGuard invalidationGuard(this);
143
144     if (attrName == SVGNames::operatorAttr
145         || attrName == SVGNames::k1Attr
146         || attrName == SVGNames::k2Attr
147         || attrName == SVGNames::k3Attr
148         || attrName == SVGNames::k4Attr) {
149         primitiveAttributeChanged(attrName);
150         return;
151     }
152
153     if (attrName == SVGNames::inAttr || attrName == SVGNames::in2Attr) {
154         invalidate();
155         return;
156     }
157
158     ASSERT_NOT_REACHED();
159 }
160
161 PassRefPtr<FilterEffect> SVGFECompositeElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
162 {
163     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
164     FilterEffect* input2 = filterBuilder->getEffectById(AtomicString(m_in2->currentValue()->value()));
165
166     if (!input1 || !input2)
167         return nullptr;
168
169     RefPtr<FilterEffect> effect = FEComposite::create(filter, m_svgOperator->currentValue()->enumValue(), m_k1->currentValue()->value(), m_k2->currentValue()->value(), m_k3->currentValue()->value(), m_k4->currentValue()->value());
170     FilterEffectVector& inputEffects = effect->inputEffects();
171     inputEffects.reserveCapacity(2);
172     inputEffects.append(input1);
173     inputEffects.append(input2);
174     return effect.release();
175 }
176
177 }