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 "SVGComponentTransferFunctionElement.h"
25
26#include "Attribute.h"
27#include "SVGFEComponentTransferElement.h"
28#include "SVGNames.h"
29#include "SVGNumberList.h"
30
31namespace WebCore {
32
33// Animated property definitions
34DEFINE_ANIMATED_ENUMERATION(SVGComponentTransferFunctionElement, SVGNames::typeAttr, Type, type)
35DEFINE_ANIMATED_NUMBER_LIST(SVGComponentTransferFunctionElement, SVGNames::tableValuesAttr, TableValues, tableValues)
36DEFINE_ANIMATED_NUMBER(SVGComponentTransferFunctionElement, SVGNames::slopeAttr, Slope, slope)
37DEFINE_ANIMATED_NUMBER(SVGComponentTransferFunctionElement, SVGNames::interceptAttr, Intercept, intercept)
38DEFINE_ANIMATED_NUMBER(SVGComponentTransferFunctionElement, SVGNames::amplitudeAttr, Amplitude, amplitude)
39DEFINE_ANIMATED_NUMBER(SVGComponentTransferFunctionElement, SVGNames::exponentAttr, Exponent, exponent)
40DEFINE_ANIMATED_NUMBER(SVGComponentTransferFunctionElement, SVGNames::offsetAttr, Offset, offset)
41
42SVGComponentTransferFunctionElement::SVGComponentTransferFunctionElement(const QualifiedName& tagName, Document* document)
43    : SVGElement(tagName, document)
44    , m_type(FECOMPONENTTRANSFER_TYPE_UNKNOWN)
45    , m_slope(1)
46    , m_amplitude(1)
47    , m_exponent(1)
48{
49}
50
51void SVGComponentTransferFunctionElement::parseMappedAttribute(Attribute* attr)
52{
53    const String& value = attr->value();
54    if (attr->name() == SVGNames::typeAttr) {
55        if (value == "identity")
56            setTypeBaseValue(FECOMPONENTTRANSFER_TYPE_IDENTITY);
57        else if (value == "table")
58            setTypeBaseValue(FECOMPONENTTRANSFER_TYPE_TABLE);
59        else if (value == "discrete")
60            setTypeBaseValue(FECOMPONENTTRANSFER_TYPE_DISCRETE);
61        else if (value == "linear")
62            setTypeBaseValue(FECOMPONENTTRANSFER_TYPE_LINEAR);
63        else if (value == "gamma")
64            setTypeBaseValue(FECOMPONENTTRANSFER_TYPE_GAMMA);
65    } else if (attr->name() == SVGNames::tableValuesAttr) {
66        SVGNumberList newList;
67        newList.parse(value);
68        detachAnimatedTableValuesListWrappers(newList.size());
69        setTableValuesBaseValue(newList);
70    } else if (attr->name() == SVGNames::slopeAttr)
71        setSlopeBaseValue(value.toFloat());
72    else if (attr->name() == SVGNames::interceptAttr)
73        setInterceptBaseValue(value.toFloat());
74    else if (attr->name() == SVGNames::amplitudeAttr)
75        setAmplitudeBaseValue(value.toFloat());
76    else if (attr->name() == SVGNames::exponentAttr)
77        setExponentBaseValue(value.toFloat());
78    else if (attr->name() == SVGNames::offsetAttr)
79        setOffsetBaseValue(value.toFloat());
80    else
81        SVGElement::parseMappedAttribute(attr);
82}
83
84void SVGComponentTransferFunctionElement::svgAttributeChanged(const QualifiedName& attrName)
85{
86    SVGElement::svgAttributeChanged(attrName);
87
88    if (attrName == SVGNames::typeAttr) {
89        ComponentTransferType componentType = static_cast<ComponentTransferType>(type());
90        if (componentType < FECOMPONENTTRANSFER_TYPE_UNKNOWN || componentType > FECOMPONENTTRANSFER_TYPE_GAMMA)
91            setTypeBaseValue(FECOMPONENTTRANSFER_TYPE_UNKNOWN);
92    }
93}
94
95void SVGComponentTransferFunctionElement::synchronizeProperty(const QualifiedName& attrName)
96{
97    SVGElement::synchronizeProperty(attrName);
98
99    if (attrName == anyQName()) {
100        synchronizeType();
101        synchronizeTableValues();
102        synchronizeSlope();
103        synchronizeIntercept();
104        synchronizeAmplitude();
105        synchronizeExponent();
106        synchronizeOffset();
107        return;
108    }
109
110    if (attrName == SVGNames::typeAttr)
111        synchronizeType();
112    else if (attrName == SVGNames::tableValuesAttr)
113        synchronizeTableValues();
114    else if (attrName == SVGNames::slopeAttr)
115        synchronizeSlope();
116    else if (attrName == SVGNames::interceptAttr)
117        synchronizeIntercept();
118    else if (attrName == SVGNames::amplitudeAttr)
119        synchronizeAmplitude();
120    else if (attrName == SVGNames::exponentAttr)
121        synchronizeExponent();
122    else if (attrName == SVGNames::offsetAttr)
123        synchronizeOffset();
124}
125
126AttributeToPropertyTypeMap& SVGComponentTransferFunctionElement::attributeToPropertyTypeMap()
127{
128    DEFINE_STATIC_LOCAL(AttributeToPropertyTypeMap, s_attributeToPropertyTypeMap, ());
129    return s_attributeToPropertyTypeMap;
130}
131
132void SVGComponentTransferFunctionElement::fillAttributeToPropertyTypeMap()
133{
134    AttributeToPropertyTypeMap& attributeToPropertyTypeMap = this->attributeToPropertyTypeMap();
135    attributeToPropertyTypeMap.set(SVGNames::typeAttr, AnimatedEnumeration);
136    attributeToPropertyTypeMap.set(SVGNames::tableValuesAttr, AnimatedNumberList);
137    attributeToPropertyTypeMap.set(SVGNames::slopeAttr, AnimatedNumber);
138    attributeToPropertyTypeMap.set(SVGNames::interceptAttr, AnimatedNumber);
139    attributeToPropertyTypeMap.set(SVGNames::amplitudeAttr, AnimatedNumber);
140    attributeToPropertyTypeMap.set(SVGNames::exponentAttr, AnimatedNumber);
141    attributeToPropertyTypeMap.set(SVGNames::offsetAttr, AnimatedNumber);
142}
143
144ComponentTransferFunction SVGComponentTransferFunctionElement::transferFunction() const
145{
146    ComponentTransferFunction func;
147    func.type = static_cast<ComponentTransferType>(type());
148    func.slope = slope();
149    func.intercept = intercept();
150    func.amplitude = amplitude();
151    func.exponent = exponent();
152    func.offset = offset();
153    func.tableValues = tableValues();
154    return func;
155}
156
157}
158
159#endif // ENABLE(SVG)
160