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/SVGComponentTransferFunctionElement.h"
24
25#include "core/SVGNames.h"
26#include "core/dom/Attribute.h"
27#include "core/svg/SVGFEComponentTransferElement.h"
28#include "core/svg/SVGNumberList.h"
29
30namespace blink {
31
32template<> const SVGEnumerationStringEntries& getStaticStringEntries<ComponentTransferType>()
33{
34    DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
35    if (entries.isEmpty()) {
36        entries.append(std::make_pair(FECOMPONENTTRANSFER_TYPE_IDENTITY, "identity"));
37        entries.append(std::make_pair(FECOMPONENTTRANSFER_TYPE_TABLE, "table"));
38        entries.append(std::make_pair(FECOMPONENTTRANSFER_TYPE_DISCRETE, "discrete"));
39        entries.append(std::make_pair(FECOMPONENTTRANSFER_TYPE_LINEAR, "linear"));
40        entries.append(std::make_pair(FECOMPONENTTRANSFER_TYPE_GAMMA, "gamma"));
41    }
42    return entries;
43}
44
45SVGComponentTransferFunctionElement::SVGComponentTransferFunctionElement(const QualifiedName& tagName, Document& document)
46    : SVGElement(tagName, document)
47    , m_tableValues(SVGAnimatedNumberList::create(this, SVGNames::tableValuesAttr, SVGNumberList::create()))
48    , m_slope(SVGAnimatedNumber::create(this, SVGNames::slopeAttr, SVGNumber::create(1)))
49    , m_intercept(SVGAnimatedNumber::create(this, SVGNames::interceptAttr, SVGNumber::create()))
50    , m_amplitude(SVGAnimatedNumber::create(this, SVGNames::amplitudeAttr, SVGNumber::create(1)))
51    , m_exponent(SVGAnimatedNumber::create(this, SVGNames::exponentAttr, SVGNumber::create(1)))
52    , m_offset(SVGAnimatedNumber::create(this, SVGNames::offsetAttr, SVGNumber::create()))
53    , m_type(SVGAnimatedEnumeration<ComponentTransferType>::create(this, SVGNames::typeAttr, FECOMPONENTTRANSFER_TYPE_IDENTITY))
54{
55    addToPropertyMap(m_tableValues);
56    addToPropertyMap(m_slope);
57    addToPropertyMap(m_intercept);
58    addToPropertyMap(m_amplitude);
59    addToPropertyMap(m_exponent);
60    addToPropertyMap(m_offset);
61    addToPropertyMap(m_type);
62}
63
64bool SVGComponentTransferFunctionElement::isSupportedAttribute(const QualifiedName& attrName)
65{
66    DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
67    if (supportedAttributes.isEmpty()) {
68        supportedAttributes.add(SVGNames::typeAttr);
69        supportedAttributes.add(SVGNames::tableValuesAttr);
70        supportedAttributes.add(SVGNames::slopeAttr);
71        supportedAttributes.add(SVGNames::interceptAttr);
72        supportedAttributes.add(SVGNames::amplitudeAttr);
73        supportedAttributes.add(SVGNames::exponentAttr);
74        supportedAttributes.add(SVGNames::offsetAttr);
75    }
76    return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
77}
78
79void SVGComponentTransferFunctionElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
80{
81    parseAttributeNew(name, value);
82}
83
84void SVGComponentTransferFunctionElement::svgAttributeChanged(const QualifiedName& attrName)
85{
86    if (!isSupportedAttribute(attrName)) {
87        SVGElement::svgAttributeChanged(attrName);
88        return;
89    }
90
91    SVGElement::InvalidationGuard invalidationGuard(this);
92
93    invalidateFilterPrimitiveParent(this);
94}
95
96ComponentTransferFunction SVGComponentTransferFunctionElement::transferFunction() const
97{
98    ComponentTransferFunction func;
99    func.type = m_type->currentValue()->enumValue();
100    func.slope = m_slope->currentValue()->value();
101    func.intercept = m_intercept->currentValue()->value();
102    func.amplitude = m_amplitude->currentValue()->value();
103    func.exponent = m_exponent->currentValue()->value();
104    func.offset = m_offset->currentValue()->value();
105    func.tableValues = m_tableValues->currentValue()->toFloatVector();
106    return func;
107}
108
109} // namespace blink
110