SVGFETurbulenceElement.cpp revision cad810f21b803229eb11403f9209855525a25d57
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 "SVGNames.h"
28#include "SVGParserUtilities.h"
29
30namespace WebCore {
31
32// Animated property definitions
33DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFETurbulenceElement, SVGNames::baseFrequencyAttr, baseFrequencyXIdentifier(), BaseFrequencyX, baseFrequencyX)
34DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFETurbulenceElement, SVGNames::baseFrequencyAttr, baseFrequencyYIdentifier(), BaseFrequencyY, baseFrequencyY)
35DEFINE_ANIMATED_INTEGER(SVGFETurbulenceElement, SVGNames::numOctavesAttr, NumOctaves, numOctaves)
36DEFINE_ANIMATED_NUMBER(SVGFETurbulenceElement, SVGNames::seedAttr, Seed, seed)
37DEFINE_ANIMATED_ENUMERATION(SVGFETurbulenceElement, SVGNames::stitchTilesAttr, StitchTiles, stitchTiles)
38DEFINE_ANIMATED_ENUMERATION(SVGFETurbulenceElement, SVGNames::typeAttr, Type, type)
39
40inline SVGFETurbulenceElement::SVGFETurbulenceElement(const QualifiedName& tagName, Document* document)
41    : SVGFilterPrimitiveStandardAttributes(tagName, document)
42    , m_numOctaves(1)
43    , m_stitchTiles(SVG_STITCHTYPE_NOSTITCH)
44    , m_type(FETURBULENCE_TYPE_TURBULENCE)
45{
46}
47
48PassRefPtr<SVGFETurbulenceElement> SVGFETurbulenceElement::create(const QualifiedName& tagName, Document* document)
49{
50    return adoptRef(new SVGFETurbulenceElement(tagName, document));
51}
52
53const AtomicString& SVGFETurbulenceElement::baseFrequencyXIdentifier()
54{
55    DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGBaseFrequencyX"));
56    return s_identifier;
57}
58
59const AtomicString& SVGFETurbulenceElement::baseFrequencyYIdentifier()
60{
61    DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGBaseFrequencyY"));
62    return s_identifier;
63}
64
65void SVGFETurbulenceElement::parseMappedAttribute(Attribute* attr)
66{
67    const String& value = attr->value();
68    if (attr->name() == SVGNames::typeAttr) {
69        if (value == "fractalNoise")
70            setTypeBaseValue(FETURBULENCE_TYPE_FRACTALNOISE);
71        else if (value == "turbulence")
72            setTypeBaseValue(FETURBULENCE_TYPE_TURBULENCE);
73    } else if (attr->name() == SVGNames::stitchTilesAttr) {
74        if (value == "stitch")
75            setStitchTilesBaseValue(SVG_STITCHTYPE_STITCH);
76        else if (value == "noStitch")
77            setStitchTilesBaseValue(SVG_STITCHTYPE_NOSTITCH);
78    } else if (attr->name() == SVGNames::baseFrequencyAttr) {
79        float x, y;
80        if (parseNumberOptionalNumber(value, x, y)) {
81            setBaseFrequencyXBaseValue(x);
82            setBaseFrequencyYBaseValue(y);
83        }
84    } else if (attr->name() == SVGNames::seedAttr)
85        setSeedBaseValue(value.toFloat());
86    else if (attr->name() == SVGNames::numOctavesAttr)
87        setNumOctavesBaseValue(value.toUIntStrict());
88    else
89        SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
90}
91
92void SVGFETurbulenceElement::svgAttributeChanged(const QualifiedName& attrName)
93{
94    SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
95
96    if (attrName == SVGNames::baseFrequencyAttr
97        || attrName == SVGNames::numOctavesAttr
98        || attrName == SVGNames::seedAttr
99        || attrName == SVGNames::stitchTilesAttr
100        || attrName == SVGNames::typeAttr)
101        invalidate();
102}
103
104void SVGFETurbulenceElement::synchronizeProperty(const QualifiedName& attrName)
105{
106    SVGFilterPrimitiveStandardAttributes::synchronizeProperty(attrName);
107
108    if (attrName == anyQName()) {
109        synchronizeType();
110        synchronizeStitchTiles();
111        synchronizeBaseFrequencyX();
112        synchronizeBaseFrequencyY();
113        synchronizeSeed();
114        synchronizeNumOctaves();
115        return;
116    }
117
118    if (attrName == SVGNames::typeAttr)
119        synchronizeType();
120    else if (attrName == SVGNames::stitchTilesAttr)
121        synchronizeStitchTiles();
122    else if (attrName == SVGNames::baseFrequencyAttr) {
123        synchronizeBaseFrequencyX();
124        synchronizeBaseFrequencyY();
125    } else if (attrName == SVGNames::seedAttr)
126        synchronizeSeed();
127    else if (attrName == SVGNames::numOctavesAttr)
128        synchronizeNumOctaves();
129}
130
131PassRefPtr<FilterEffect> SVGFETurbulenceElement::build(SVGFilterBuilder*, Filter* filter)
132{
133    if (baseFrequencyX() < 0 || baseFrequencyY() < 0)
134        return 0;
135
136    return FETurbulence::create(filter, static_cast<TurbulanceType>(type()), baseFrequencyX(),
137                baseFrequencyY(), numOctaves(), seed(), stitchTiles() == SVG_STITCHTYPE_STITCH);
138}
139
140}
141
142#endif // ENABLE(SVG)
143