initial import
[vuplus_webkit] / Source / WebCore / 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 #if ENABLE(SVG) && ENABLE(FILTERS)
24 #include "SVGFEBlendElement.h"
25
26 #include "Attribute.h"
27 #include "FilterEffect.h"
28 #include "SVGElementInstance.h"
29 #include "SVGFilterBuilder.h"
30 #include "SVGNames.h"
31
32 namespace WebCore {
33
34 // Animated property definitions
35 DEFINE_ANIMATED_STRING(SVGFEBlendElement, SVGNames::inAttr, In1, in1)
36 DEFINE_ANIMATED_STRING(SVGFEBlendElement, SVGNames::in2Attr, In2, in2)
37 DEFINE_ANIMATED_ENUMERATION(SVGFEBlendElement, SVGNames::modeAttr, Mode, mode, BlendModeType)
38
39 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEBlendElement)
40     REGISTER_LOCAL_ANIMATED_PROPERTY(in1)
41     REGISTER_LOCAL_ANIMATED_PROPERTY(in2)
42     REGISTER_LOCAL_ANIMATED_PROPERTY(mode)
43     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
44 END_REGISTER_ANIMATED_PROPERTIES
45
46 inline SVGFEBlendElement::SVGFEBlendElement(const QualifiedName& tagName, Document* document)
47     : SVGFilterPrimitiveStandardAttributes(tagName, document)
48     , m_mode(FEBLEND_MODE_NORMAL)
49 {
50     ASSERT(hasTagName(SVGNames::feBlendTag));
51     registerAnimatedPropertiesForSVGFEBlendElement();
52 }
53
54 PassRefPtr<SVGFEBlendElement> SVGFEBlendElement::create(const QualifiedName& tagName, Document* document)
55 {
56     return adoptRef(new SVGFEBlendElement(tagName, document));
57 }
58
59 bool SVGFEBlendElement::isSupportedAttribute(const QualifiedName& attrName)
60 {
61     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
62     if (supportedAttributes.isEmpty()) {
63         supportedAttributes.add(SVGNames::modeAttr);
64         supportedAttributes.add(SVGNames::inAttr);
65         supportedAttributes.add(SVGNames::in2Attr);
66     }
67     return supportedAttributes.contains(attrName);
68 }
69
70 void SVGFEBlendElement::parseMappedAttribute(Attribute* attr)
71 {
72     if (!isSupportedAttribute(attr->name())) {
73         SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
74         return;
75     }
76
77     const AtomicString& value = attr->value();
78     if (attr->name() == SVGNames::modeAttr) {
79         BlendModeType propertyValue = SVGPropertyTraits<BlendModeType>::fromString(value);
80         if (propertyValue > 0)
81             setModeBaseValue(propertyValue);
82         return;
83     }
84
85     if (attr->name() == SVGNames::inAttr) {
86         setIn1BaseValue(value);
87         return;
88     }
89
90     if (attr->name() == SVGNames::in2Attr) {
91         setIn2BaseValue(value);
92         return;
93     }
94
95     ASSERT_NOT_REACHED();
96 }
97
98 bool SVGFEBlendElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
99 {
100     FEBlend* blend = static_cast<FEBlend*>(effect);
101     if (attrName == SVGNames::modeAttr)
102         return blend->setBlendMode(mode());
103
104     ASSERT_NOT_REACHED();
105     return false;
106 }
107
108 void SVGFEBlendElement::svgAttributeChanged(const QualifiedName& attrName)
109 {
110     if (!isSupportedAttribute(attrName)) {
111         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
112         return;
113     }
114
115     SVGElementInstance::InvalidationGuard invalidationGuard(this);
116
117     if (attrName == SVGNames::modeAttr) {
118         primitiveAttributeChanged(attrName);
119         return;
120     }
121
122     if (attrName == SVGNames::inAttr || attrName == SVGNames::in2Attr) {
123         invalidate();
124         return;
125     }
126
127     ASSERT_NOT_REACHED();
128 }
129
130 PassRefPtr<FilterEffect> SVGFEBlendElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
131 {
132     FilterEffect* input1 = filterBuilder->getEffectById(in1());
133     FilterEffect* input2 = filterBuilder->getEffectById(in2());
134
135     if (!input1 || !input2)
136         return 0;
137
138     RefPtr<FilterEffect> effect = FEBlend::create(filter, mode());
139     FilterEffectVector& inputEffects = effect->inputEffects();
140     inputEffects.reserveCapacity(2);
141     inputEffects.append(input1);
142     inputEffects.append(input2);    
143     return effect.release();
144 }
145
146 }
147
148 #endif // ENABLE(SVG)