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/SVGFECompositeElement.h"
24
25#include "core/SVGNames.h"
26#include "platform/graphics/filters/FilterEffect.h"
27#include "core/svg/graphics/filters/SVGFilterBuilder.h"
28
29namespace blink {
30
31template<> const SVGEnumerationStringEntries& getStaticStringEntries<CompositeOperationType>()
32{
33    DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
34    if (entries.isEmpty()) {
35        entries.append(std::make_pair(FECOMPOSITE_OPERATOR_OVER, "over"));
36        entries.append(std::make_pair(FECOMPOSITE_OPERATOR_IN, "in"));
37        entries.append(std::make_pair(FECOMPOSITE_OPERATOR_OUT, "out"));
38        entries.append(std::make_pair(FECOMPOSITE_OPERATOR_ATOP, "atop"));
39        entries.append(std::make_pair(FECOMPOSITE_OPERATOR_XOR, "xor"));
40        entries.append(std::make_pair(FECOMPOSITE_OPERATOR_ARITHMETIC, "arithmetic"));
41    }
42    return entries;
43}
44
45inline SVGFECompositeElement::SVGFECompositeElement(Document& document)
46    : SVGFilterPrimitiveStandardAttributes(SVGNames::feCompositeTag, document)
47    , m_k1(SVGAnimatedNumber::create(this, SVGNames::k1Attr, SVGNumber::create()))
48    , m_k2(SVGAnimatedNumber::create(this, SVGNames::k2Attr, SVGNumber::create()))
49    , m_k3(SVGAnimatedNumber::create(this, SVGNames::k3Attr, SVGNumber::create()))
50    , m_k4(SVGAnimatedNumber::create(this, SVGNames::k4Attr, SVGNumber::create()))
51    , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
52    , m_in2(SVGAnimatedString::create(this, SVGNames::in2Attr, SVGString::create()))
53    , m_svgOperator(SVGAnimatedEnumeration<CompositeOperationType>::create(this, SVGNames::operatorAttr, FECOMPOSITE_OPERATOR_OVER))
54{
55    addToPropertyMap(m_k1);
56    addToPropertyMap(m_k2);
57    addToPropertyMap(m_k3);
58    addToPropertyMap(m_k4);
59    addToPropertyMap(m_in1);
60    addToPropertyMap(m_in2);
61    addToPropertyMap(m_svgOperator);
62}
63
64DEFINE_NODE_FACTORY(SVGFECompositeElement)
65
66bool SVGFECompositeElement::isSupportedAttribute(const QualifiedName& attrName)
67{
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);
77    }
78    return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
79}
80
81void SVGFECompositeElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
82{
83    if (!isSupportedAttribute(name)) {
84        SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
85        return;
86    }
87
88    SVGParsingError parseError = NoError;
89
90    if (name == SVGNames::inAttr)
91        m_in1->setBaseValueAsString(value, parseError);
92    else if (name == SVGNames::in2Attr)
93        m_in2->setBaseValueAsString(value, parseError);
94    else if (name == SVGNames::k1Attr)
95        m_k1->setBaseValueAsString(value, parseError);
96    else if (name == SVGNames::k2Attr)
97        m_k2->setBaseValueAsString(value, parseError);
98    else if (name == SVGNames::k3Attr)
99        m_k3->setBaseValueAsString(value, parseError);
100    else if (name == SVGNames::k4Attr)
101        m_k4->setBaseValueAsString(value, parseError);
102    else if (name == SVGNames::operatorAttr)
103        m_svgOperator->setBaseValueAsString(value, parseError);
104    else
105        ASSERT_NOT_REACHED();
106
107    reportAttributeParsingError(parseError, name, value);
108}
109
110bool SVGFECompositeElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
111{
112    FEComposite* composite = static_cast<FEComposite*>(effect);
113    if (attrName == SVGNames::operatorAttr)
114        return composite->setOperation(m_svgOperator->currentValue()->enumValue());
115    if (attrName == SVGNames::k1Attr)
116        return composite->setK1(m_k1->currentValue()->value());
117    if (attrName == SVGNames::k2Attr)
118        return composite->setK2(m_k2->currentValue()->value());
119    if (attrName == SVGNames::k3Attr)
120        return composite->setK3(m_k3->currentValue()->value());
121    if (attrName == SVGNames::k4Attr)
122        return composite->setK4(m_k4->currentValue()->value());
123
124    ASSERT_NOT_REACHED();
125    return false;
126}
127
128
129void SVGFECompositeElement::svgAttributeChanged(const QualifiedName& attrName)
130{
131    if (!isSupportedAttribute(attrName)) {
132        SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
133        return;
134    }
135
136    SVGElement::InvalidationGuard invalidationGuard(this);
137
138    if (attrName == SVGNames::operatorAttr
139        || attrName == SVGNames::k1Attr
140        || attrName == SVGNames::k2Attr
141        || attrName == SVGNames::k3Attr
142        || attrName == SVGNames::k4Attr) {
143        primitiveAttributeChanged(attrName);
144        return;
145    }
146
147    if (attrName == SVGNames::inAttr || attrName == SVGNames::in2Attr) {
148        invalidate();
149        return;
150    }
151
152    ASSERT_NOT_REACHED();
153}
154
155PassRefPtr<FilterEffect> SVGFECompositeElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
156{
157    FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
158    FilterEffect* input2 = filterBuilder->getEffectById(AtomicString(m_in2->currentValue()->value()));
159
160    if (!input1 || !input2)
161        return nullptr;
162
163    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());
164    FilterEffectVector& inputEffects = effect->inputEffects();
165    inputEffects.reserveCapacity(2);
166    inputEffects.append(input1);
167    inputEffects.append(input2);
168    return effect.release();
169}
170
171} // namespace blink
172