2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
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.
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.
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.
23 #include "core/svg/SVGFECompositeElement.h"
26 #include "platform/graphics/filters/FilterEffect.h"
27 #include "core/svg/SVGElementInstance.h"
28 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
32 // Animated property definitions
33 DEFINE_ANIMATED_ENUMERATION(SVGFECompositeElement, SVGNames::operatorAttr, SVGOperator, svgOperator, CompositeOperationType)
35 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFECompositeElement)
36 REGISTER_LOCAL_ANIMATED_PROPERTY(svgOperator)
37 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
38 END_REGISTER_ANIMATED_PROPERTIES
40 inline SVGFECompositeElement::SVGFECompositeElement(Document& document)
41 : SVGFilterPrimitiveStandardAttributes(SVGNames::feCompositeTag, document)
42 , m_k1(SVGAnimatedNumber::create(this, SVGNames::k1Attr, SVGNumber::create()))
43 , m_k2(SVGAnimatedNumber::create(this, SVGNames::k2Attr, SVGNumber::create()))
44 , m_k3(SVGAnimatedNumber::create(this, SVGNames::k3Attr, SVGNumber::create()))
45 , m_k4(SVGAnimatedNumber::create(this, SVGNames::k4Attr, SVGNumber::create()))
46 , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
47 , m_in2(SVGAnimatedString::create(this, SVGNames::in2Attr, SVGString::create()))
48 , m_svgOperator(FECOMPOSITE_OPERATOR_OVER)
50 ScriptWrappable::init(this);
52 addToPropertyMap(m_k1);
53 addToPropertyMap(m_k2);
54 addToPropertyMap(m_k3);
55 addToPropertyMap(m_k4);
56 addToPropertyMap(m_in1);
57 addToPropertyMap(m_in2);
58 registerAnimatedPropertiesForSVGFECompositeElement();
61 PassRefPtr<SVGFECompositeElement> SVGFECompositeElement::create(Document& document)
63 return adoptRef(new SVGFECompositeElement(document));
66 bool SVGFECompositeElement::isSupportedAttribute(const QualifiedName& attrName)
68 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
69 if (supportedAttributes.isEmpty()) {
70 supportedAttributes.add(SVGNames::inAttr);
71 supportedAttributes.add(SVGNames::in2Attr);
72 supportedAttributes.add(SVGNames::operatorAttr);
73 supportedAttributes.add(SVGNames::k1Attr);
74 supportedAttributes.add(SVGNames::k2Attr);
75 supportedAttributes.add(SVGNames::k3Attr);
76 supportedAttributes.add(SVGNames::k4Attr);
78 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
81 void SVGFECompositeElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
83 if (!isSupportedAttribute(name)) {
84 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
88 if (name == SVGNames::operatorAttr) {
89 CompositeOperationType propertyValue = SVGPropertyTraits<CompositeOperationType>::fromString(value);
90 if (propertyValue > 0)
91 setSVGOperatorBaseValue(propertyValue);
95 SVGParsingError parseError = NoError;
97 if (name == SVGNames::inAttr)
98 m_in1->setBaseValueAsString(value, parseError);
99 else if (name == SVGNames::in2Attr)
100 m_in2->setBaseValueAsString(value, parseError);
101 else if (name == SVGNames::k1Attr)
102 m_k1->setBaseValueAsString(value, parseError);
103 else if (name == SVGNames::k2Attr)
104 m_k2->setBaseValueAsString(value, parseError);
105 else if (name == SVGNames::k3Attr)
106 m_k3->setBaseValueAsString(value, parseError);
107 else if (name == SVGNames::k4Attr)
108 m_k4->setBaseValueAsString(value, parseError);
110 ASSERT_NOT_REACHED();
112 reportAttributeParsingError(parseError, name, value);
115 bool SVGFECompositeElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
117 FEComposite* composite = static_cast<FEComposite*>(effect);
118 if (attrName == SVGNames::operatorAttr)
119 return composite->setOperation(svgOperatorCurrentValue());
120 if (attrName == SVGNames::k1Attr)
121 return composite->setK1(m_k1->currentValue()->value());
122 if (attrName == SVGNames::k2Attr)
123 return composite->setK2(m_k2->currentValue()->value());
124 if (attrName == SVGNames::k3Attr)
125 return composite->setK3(m_k3->currentValue()->value());
126 if (attrName == SVGNames::k4Attr)
127 return composite->setK4(m_k4->currentValue()->value());
129 ASSERT_NOT_REACHED();
134 void SVGFECompositeElement::svgAttributeChanged(const QualifiedName& attrName)
136 if (!isSupportedAttribute(attrName)) {
137 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
141 SVGElementInstance::InvalidationGuard invalidationGuard(this);
143 if (attrName == SVGNames::operatorAttr
144 || attrName == SVGNames::k1Attr
145 || attrName == SVGNames::k2Attr
146 || attrName == SVGNames::k3Attr
147 || attrName == SVGNames::k4Attr) {
148 primitiveAttributeChanged(attrName);
152 if (attrName == SVGNames::inAttr || attrName == SVGNames::in2Attr) {
157 ASSERT_NOT_REACHED();
160 PassRefPtr<FilterEffect> SVGFECompositeElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
162 FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
163 FilterEffect* input2 = filterBuilder->getEffectById(AtomicString(m_in2->currentValue()->value()));
165 if (!input1 || !input2)
168 RefPtr<FilterEffect> effect = FEComposite::create(filter, svgOperatorCurrentValue(), m_k1->currentValue()->value(), m_k2->currentValue()->value(), m_k3->currentValue()->value(), m_k4->currentValue()->value());
169 FilterEffectVector& inputEffects = effect->inputEffects();
170 inputEffects.reserveCapacity(2);
171 inputEffects.append(input1);
172 inputEffects.append(input2);
173 return effect.release();