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/SVGFETurbulenceElement.h"
24
25#include "SVGNames.h"
26#include "core/svg/SVGElementInstance.h"
27#include "core/svg/SVGParserUtilities.h"
28
29namespace WebCore {
30
31// Animated property definitions
32DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFETurbulenceElement, SVGNames::baseFrequencyAttr, baseFrequencyXIdentifier(), BaseFrequencyX, baseFrequencyX)
33DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFETurbulenceElement, SVGNames::baseFrequencyAttr, baseFrequencyYIdentifier(), BaseFrequencyY, baseFrequencyY)
34DEFINE_ANIMATED_INTEGER(SVGFETurbulenceElement, SVGNames::numOctavesAttr, NumOctaves, numOctaves)
35DEFINE_ANIMATED_NUMBER(SVGFETurbulenceElement, SVGNames::seedAttr, Seed, seed)
36DEFINE_ANIMATED_ENUMERATION(SVGFETurbulenceElement, SVGNames::stitchTilesAttr, StitchTiles, stitchTiles, SVGStitchOptions)
37DEFINE_ANIMATED_ENUMERATION(SVGFETurbulenceElement, SVGNames::typeAttr, Type, type, TurbulenceType)
38
39BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFETurbulenceElement)
40    REGISTER_LOCAL_ANIMATED_PROPERTY(baseFrequencyX)
41    REGISTER_LOCAL_ANIMATED_PROPERTY(baseFrequencyY)
42    REGISTER_LOCAL_ANIMATED_PROPERTY(numOctaves)
43    REGISTER_LOCAL_ANIMATED_PROPERTY(seed)
44    REGISTER_LOCAL_ANIMATED_PROPERTY(stitchTiles)
45    REGISTER_LOCAL_ANIMATED_PROPERTY(type)
46    REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
47END_REGISTER_ANIMATED_PROPERTIES
48
49inline SVGFETurbulenceElement::SVGFETurbulenceElement(Document& document)
50    : SVGFilterPrimitiveStandardAttributes(SVGNames::feTurbulenceTag, document)
51    , m_numOctaves(1)
52    , m_stitchTiles(SVG_STITCHTYPE_NOSTITCH)
53    , m_type(FETURBULENCE_TYPE_TURBULENCE)
54{
55    ScriptWrappable::init(this);
56    registerAnimatedPropertiesForSVGFETurbulenceElement();
57}
58
59PassRefPtr<SVGFETurbulenceElement> SVGFETurbulenceElement::create(Document& document)
60{
61    return adoptRef(new SVGFETurbulenceElement(document));
62}
63
64const AtomicString& SVGFETurbulenceElement::baseFrequencyXIdentifier()
65{
66    DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGBaseFrequencyX", AtomicString::ConstructFromLiteral));
67    return s_identifier;
68}
69
70const AtomicString& SVGFETurbulenceElement::baseFrequencyYIdentifier()
71{
72    DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGBaseFrequencyY", AtomicString::ConstructFromLiteral));
73    return s_identifier;
74}
75
76bool SVGFETurbulenceElement::isSupportedAttribute(const QualifiedName& attrName)
77{
78    DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
79    if (supportedAttributes.isEmpty()) {
80        supportedAttributes.add(SVGNames::baseFrequencyAttr);
81        supportedAttributes.add(SVGNames::numOctavesAttr);
82        supportedAttributes.add(SVGNames::seedAttr);
83        supportedAttributes.add(SVGNames::stitchTilesAttr);
84        supportedAttributes.add(SVGNames::typeAttr);
85    }
86    return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
87}
88
89void SVGFETurbulenceElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
90{
91    if (!isSupportedAttribute(name)) {
92        SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
93        return;
94    }
95
96    if (name == SVGNames::typeAttr) {
97        TurbulenceType propertyValue = SVGPropertyTraits<TurbulenceType>::fromString(value);
98        if (propertyValue > 0)
99            setTypeBaseValue(propertyValue);
100        return;
101    }
102
103    if (name == SVGNames::stitchTilesAttr) {
104        SVGStitchOptions propertyValue = SVGPropertyTraits<SVGStitchOptions>::fromString(value);
105        if (propertyValue > 0)
106            setStitchTilesBaseValue(propertyValue);
107        return;
108    }
109
110    if (name == SVGNames::baseFrequencyAttr) {
111        float x, y;
112        if (parseNumberOptionalNumber(value, x, y)) {
113            setBaseFrequencyXBaseValue(x);
114            setBaseFrequencyYBaseValue(y);
115        }
116        return;
117    }
118
119    if (name == SVGNames::seedAttr) {
120        setSeedBaseValue(value.toFloat());
121        return;
122    }
123
124    if (name == SVGNames::numOctavesAttr) {
125        setNumOctavesBaseValue(value.string().toUIntStrict());
126        return;
127    }
128
129    ASSERT_NOT_REACHED();
130}
131
132bool SVGFETurbulenceElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
133{
134    FETurbulence* turbulence = static_cast<FETurbulence*>(effect);
135    if (attrName == SVGNames::typeAttr)
136        return turbulence->setType(typeCurrentValue());
137    if (attrName == SVGNames::stitchTilesAttr)
138        return turbulence->setStitchTiles(stitchTilesCurrentValue());
139    if (attrName == SVGNames::baseFrequencyAttr) {
140        bool baseFrequencyXChanged = turbulence->setBaseFrequencyX(baseFrequencyXCurrentValue());
141        bool baseFrequencyYChanged = turbulence->setBaseFrequencyY(baseFrequencyYCurrentValue());
142        return (baseFrequencyXChanged || baseFrequencyYChanged);
143    }
144    if (attrName == SVGNames::seedAttr)
145        return turbulence->setSeed(seedCurrentValue());
146    if (attrName == SVGNames::numOctavesAttr)
147        return turbulence->setNumOctaves(numOctavesCurrentValue());
148
149    ASSERT_NOT_REACHED();
150    return false;
151}
152
153void SVGFETurbulenceElement::svgAttributeChanged(const QualifiedName& attrName)
154{
155    if (!isSupportedAttribute(attrName)) {
156        SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
157        return;
158    }
159
160    SVGElementInstance::InvalidationGuard invalidationGuard(this);
161
162    if (attrName == SVGNames::baseFrequencyAttr
163        || attrName == SVGNames::numOctavesAttr
164        || attrName == SVGNames::seedAttr
165        || attrName == SVGNames::stitchTilesAttr
166        || attrName == SVGNames::typeAttr) {
167        primitiveAttributeChanged(attrName);
168        return;
169    }
170
171    ASSERT_NOT_REACHED();
172}
173
174PassRefPtr<FilterEffect> SVGFETurbulenceElement::build(SVGFilterBuilder*, Filter* filter)
175{
176    if (baseFrequencyXCurrentValue() < 0 || baseFrequencyYCurrentValue() < 0)
177        return 0;
178    return FETurbulence::create(filter, typeCurrentValue(), baseFrequencyXCurrentValue(), baseFrequencyYCurrentValue(), numOctavesCurrentValue(), seedCurrentValue(), stitchTilesCurrentValue() == SVG_STITCHTYPE_STITCH);
179}
180
181}
182