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