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 template<> const SVGEnumerationStringEntries& getStaticStringEntries<CompositeOperationType>()
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"));
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))
56 ScriptWrappable::init(this);
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);
67 PassRefPtr<SVGFECompositeElement> SVGFECompositeElement::create(Document& document)
69 return adoptRef(new SVGFECompositeElement(document));
72 bool SVGFECompositeElement::isSupportedAttribute(const QualifiedName& attrName)
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);
84 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
87 void SVGFECompositeElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
89 if (!isSupportedAttribute(name)) {
90 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
94 SVGParsingError parseError = NoError;
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);
111 ASSERT_NOT_REACHED();
113 reportAttributeParsingError(parseError, name, value);
116 bool SVGFECompositeElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
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());
130 ASSERT_NOT_REACHED();
135 void SVGFECompositeElement::svgAttributeChanged(const QualifiedName& attrName)
137 if (!isSupportedAttribute(attrName)) {
138 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
142 SVGElement::InvalidationGuard invalidationGuard(this);
144 if (attrName == SVGNames::operatorAttr
145 || attrName == SVGNames::k1Attr
146 || attrName == SVGNames::k2Attr
147 || attrName == SVGNames::k3Attr
148 || attrName == SVGNames::k4Attr) {
149 primitiveAttributeChanged(attrName);
153 if (attrName == SVGNames::inAttr || attrName == SVGNames::in2Attr) {
158 ASSERT_NOT_REACHED();
161 PassRefPtr<FilterEffect> SVGFECompositeElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
163 FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
164 FilterEffect* input2 = filterBuilder->getEffectById(AtomicString(m_in2->currentValue()->value()));
166 if (!input1 || !input2)
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();