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