initial import
[vuplus_webkit] / 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"));
69     return s_identifier;
70 }
71
72 const AtomicString& SVGFETurbulenceElement::baseFrequencyYIdentifier()
73 {
74     DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGBaseFrequencyY"));
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(attrName);
89 }
90
91 void SVGFETurbulenceElement::parseMappedAttribute(Attribute* attr)
92 {
93     if (!isSupportedAttribute(attr->name())) {
94         SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
95         return;
96     }
97
98     const AtomicString& value = attr->value();
99     if (attr->name() == SVGNames::typeAttr) {
100         TurbulenceType propertyValue = SVGPropertyTraits<TurbulenceType>::fromString(value);
101         if (propertyValue > 0)
102             setTypeBaseValue(propertyValue);
103         return;
104     }
105
106     if (attr->name() == SVGNames::stitchTilesAttr) {
107         SVGStitchOptions propertyValue = SVGPropertyTraits<SVGStitchOptions>::fromString(value);
108         if (propertyValue > 0)
109             setStitchTilesBaseValue(propertyValue);
110         return;
111     }
112
113     if (attr->name() == SVGNames::baseFrequencyAttr) {
114         float x, y;
115         if (parseNumberOptionalNumber(value, x, y)) {
116             setBaseFrequencyXBaseValue(x);
117             setBaseFrequencyYBaseValue(y);
118         }
119         return;
120     }
121
122     if (attr->name() == SVGNames::seedAttr) {
123         setSeedBaseValue(value.toFloat());
124         return;
125     }
126
127     if (attr->name() == SVGNames::numOctavesAttr) {
128         setNumOctavesBaseValue(value.string().toUIntStrict());
129         return;
130     }
131
132     ASSERT_NOT_REACHED();
133 }
134
135 bool SVGFETurbulenceElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
136 {
137     FETurbulence* turbulence = static_cast<FETurbulence*>(effect);
138     if (attrName == SVGNames::typeAttr)
139         return turbulence->setType(type());
140     if (attrName == SVGNames::stitchTilesAttr)
141         return turbulence->setStitchTiles(stitchTiles());
142     if (attrName == SVGNames::baseFrequencyAttr)
143         return (turbulence->setBaseFrequencyX(baseFrequencyX()) || turbulence->setBaseFrequencyY(baseFrequencyY()));
144     if (attrName == SVGNames::seedAttr)
145         return turbulence->setSeed(seed());
146     if (attrName == SVGNames::numOctavesAttr)
147        return turbulence->setNumOctaves(numOctaves());
148
149     ASSERT_NOT_REACHED();
150     return false;
151 }
152
153 void 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
174 PassRefPtr<FilterEffect> SVGFETurbulenceElement::build(SVGFilterBuilder*, Filter* filter)
175 {
176     if (baseFrequencyX() < 0 || baseFrequencyY() < 0)
177         return 0;
178     return FETurbulence::create(filter, type(), baseFrequencyX(), baseFrequencyY(), numOctaves(), seed(), stitchTiles() == SVG_STITCHTYPE_STITCH);
179 }
180
181 }
182
183 #endif // ENABLE(SVG)