ddd5651d6f014aadf4a4219ea380197b3140d6fc
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGFEBlendElement.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/SVGFEBlendElement.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<BlendModeType>()
33 {
34     DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
35     if (entries.isEmpty()) {
36         entries.append(std::make_pair(FEBLEND_MODE_NORMAL, "normal"));
37         entries.append(std::make_pair(FEBLEND_MODE_MULTIPLY, "multiply"));
38         entries.append(std::make_pair(FEBLEND_MODE_SCREEN, "screen"));
39         entries.append(std::make_pair(FEBLEND_MODE_DARKEN, "darken"));
40         entries.append(std::make_pair(FEBLEND_MODE_LIGHTEN, "lighten"));
41     }
42     return entries;
43 }
44
45 inline SVGFEBlendElement::SVGFEBlendElement(Document& document)
46     : SVGFilterPrimitiveStandardAttributes(SVGNames::feBlendTag, document)
47     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
48     , m_in2(SVGAnimatedString::create(this, SVGNames::in2Attr, SVGString::create()))
49     , m_mode(SVGAnimatedEnumeration<BlendModeType>::create(this, SVGNames::modeAttr, FEBLEND_MODE_NORMAL))
50 {
51     ScriptWrappable::init(this);
52     addToPropertyMap(m_in1);
53     addToPropertyMap(m_in2);
54     addToPropertyMap(m_mode);
55 }
56
57 PassRefPtr<SVGFEBlendElement> SVGFEBlendElement::create(Document& document)
58 {
59     return adoptRef(new SVGFEBlendElement(document));
60 }
61
62 bool SVGFEBlendElement::isSupportedAttribute(const QualifiedName& attrName)
63 {
64     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
65     if (supportedAttributes.isEmpty()) {
66         supportedAttributes.add(SVGNames::modeAttr);
67         supportedAttributes.add(SVGNames::inAttr);
68         supportedAttributes.add(SVGNames::in2Attr);
69     }
70     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
71 }
72
73 void SVGFEBlendElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
74 {
75     if (!isSupportedAttribute(name)) {
76         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
77         return;
78     }
79
80     SVGParsingError parseError = NoError;
81
82     if (name == SVGNames::inAttr)
83         m_in1->setBaseValueAsString(value, parseError);
84     else if (name == SVGNames::in2Attr)
85         m_in2->setBaseValueAsString(value, parseError);
86     else if (name == SVGNames::modeAttr)
87         m_mode->setBaseValueAsString(value, parseError);
88     else
89         ASSERT_NOT_REACHED();
90
91     reportAttributeParsingError(parseError, name, value);
92 }
93
94 bool SVGFEBlendElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
95 {
96     FEBlend* blend = static_cast<FEBlend*>(effect);
97     if (attrName == SVGNames::modeAttr)
98         return blend->setBlendMode(m_mode->currentValue()->enumValue());
99
100     ASSERT_NOT_REACHED();
101     return false;
102 }
103
104 void SVGFEBlendElement::svgAttributeChanged(const QualifiedName& attrName)
105 {
106     if (!isSupportedAttribute(attrName)) {
107         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
108         return;
109     }
110
111     SVGElement::InvalidationGuard invalidationGuard(this);
112
113     if (attrName == SVGNames::modeAttr) {
114         primitiveAttributeChanged(attrName);
115         return;
116     }
117
118     if (attrName == SVGNames::inAttr || attrName == SVGNames::in2Attr) {
119         invalidate();
120         return;
121     }
122
123     ASSERT_NOT_REACHED();
124 }
125
126 PassRefPtr<FilterEffect> SVGFEBlendElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
127 {
128     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
129     FilterEffect* input2 = filterBuilder->getEffectById(AtomicString(m_in2->currentValue()->value()));
130
131     if (!input1 || !input2)
132         return nullptr;
133
134     RefPtr<FilterEffect> effect = FEBlend::create(filter, m_mode->currentValue()->enumValue());
135     FilterEffectVector& inputEffects = effect->inputEffects();
136     inputEffects.reserveCapacity(2);
137     inputEffects.append(input1);
138     inputEffects.append(input2);
139     return effect.release();
140 }
141
142 }