1a15d9c73ddd3827c9e24ccb7453bda90eef1062
[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 #include "SVGFETurbulenceElement.h"
23
24 #include "SVGNames.h"
25 #include "SVGParserUtilities.h"
26 #include <wtf/IsoMallocInlines.h>
27
28 namespace WebCore {
29
30 WTF_MAKE_ISO_ALLOCATED_IMPL(SVGFETurbulenceElement);
31
32 // Animated property definitions
33 DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFETurbulenceElement, SVGNames::baseFrequencyAttr, baseFrequencyXIdentifier(), BaseFrequencyX, baseFrequencyX)
34 DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFETurbulenceElement, SVGNames::baseFrequencyAttr, baseFrequencyYIdentifier(), BaseFrequencyY, baseFrequencyY)
35 DEFINE_ANIMATED_INTEGER(SVGFETurbulenceElement, SVGNames::numOctavesAttr, NumOctaves, numOctaves)
36 DEFINE_ANIMATED_NUMBER(SVGFETurbulenceElement, SVGNames::seedAttr, Seed, seed)
37 DEFINE_ANIMATED_ENUMERATION(SVGFETurbulenceElement, SVGNames::stitchTilesAttr, StitchTiles, stitchTiles, SVGStitchOptions)
38 DEFINE_ANIMATED_ENUMERATION(SVGFETurbulenceElement, SVGNames::typeAttr, Type, type, TurbulenceType)
39
40 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFETurbulenceElement)
41     REGISTER_LOCAL_ANIMATED_PROPERTY(baseFrequencyX)
42     REGISTER_LOCAL_ANIMATED_PROPERTY(baseFrequencyY)
43     REGISTER_LOCAL_ANIMATED_PROPERTY(numOctaves)
44     REGISTER_LOCAL_ANIMATED_PROPERTY(seed)
45     REGISTER_LOCAL_ANIMATED_PROPERTY(stitchTiles)
46     REGISTER_LOCAL_ANIMATED_PROPERTY(type)
47     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
48 END_REGISTER_ANIMATED_PROPERTIES
49
50 inline SVGFETurbulenceElement::SVGFETurbulenceElement(const QualifiedName& tagName, Document& document)
51     : SVGFilterPrimitiveStandardAttributes(tagName, document)
52     , m_numOctaves(1)
53     , m_stitchTiles(SVG_STITCHTYPE_NOSTITCH)
54     , m_type(TurbulenceType::Turbulence)
55 {
56     ASSERT(hasTagName(SVGNames::feTurbulenceTag));
57     registerAnimatedPropertiesForSVGFETurbulenceElement();
58 }
59
60 Ref<SVGFETurbulenceElement> SVGFETurbulenceElement::create(const QualifiedName& tagName, Document& document)
61 {
62     return adoptRef(*new SVGFETurbulenceElement(tagName, document));
63 }
64
65 const AtomicString& SVGFETurbulenceElement::baseFrequencyXIdentifier()
66 {
67     static NeverDestroyed<AtomicString> s_identifier("SVGBaseFrequencyX", AtomicString::ConstructFromLiteral);
68     return s_identifier;
69 }
70
71 const AtomicString& SVGFETurbulenceElement::baseFrequencyYIdentifier()
72 {
73     static NeverDestroyed<AtomicString> s_identifier("SVGBaseFrequencyY", AtomicString::ConstructFromLiteral);
74     return s_identifier;
75 }
76
77 void SVGFETurbulenceElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
78 {
79     if (name == SVGNames::typeAttr) {
80         TurbulenceType propertyValue = SVGPropertyTraits<TurbulenceType>::fromString(value);
81         if (propertyValue != TurbulenceType::Unknown)
82             setTypeBaseValue(propertyValue);
83         return;
84     }
85
86     if (name == SVGNames::stitchTilesAttr) {
87         SVGStitchOptions propertyValue = SVGPropertyTraits<SVGStitchOptions>::fromString(value);
88         if (propertyValue > 0)
89             setStitchTilesBaseValue(propertyValue);
90         return;
91     }
92
93     if (name == SVGNames::baseFrequencyAttr) {
94         float x, y;
95         if (parseNumberOptionalNumber(value, x, y)) {
96             setBaseFrequencyXBaseValue(x);
97             setBaseFrequencyYBaseValue(y);
98         }
99         return;
100     }
101
102     if (name == SVGNames::seedAttr) {
103         setSeedBaseValue(value.toFloat());
104         return;
105     }
106
107     if (name == SVGNames::numOctavesAttr) {
108         setNumOctavesBaseValue(value.string().toUIntStrict());
109         return;
110     }
111
112     SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
113 }
114
115 bool SVGFETurbulenceElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
116 {
117     FETurbulence* turbulence = static_cast<FETurbulence*>(effect);
118     if (attrName == SVGNames::typeAttr)
119         return turbulence->setType(type());
120     if (attrName == SVGNames::stitchTilesAttr)
121         return turbulence->setStitchTiles(stitchTiles());
122     if (attrName == SVGNames::baseFrequencyAttr)
123         return (turbulence->setBaseFrequencyX(baseFrequencyX()) || turbulence->setBaseFrequencyY(baseFrequencyY()));
124     if (attrName == SVGNames::seedAttr)
125         return turbulence->setSeed(seed());
126     if (attrName == SVGNames::numOctavesAttr)
127        return turbulence->setNumOctaves(numOctaves());
128
129     ASSERT_NOT_REACHED();
130     return false;
131 }
132
133 void SVGFETurbulenceElement::svgAttributeChanged(const QualifiedName& attrName)
134 {
135     if (attrName == SVGNames::baseFrequencyAttr || attrName == SVGNames::numOctavesAttr || attrName == SVGNames::seedAttr || attrName == SVGNames::stitchTilesAttr || attrName == SVGNames::typeAttr) {
136         InstanceInvalidationGuard guard(*this);
137         primitiveAttributeChanged(attrName);
138         return;
139     }
140
141     SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
142 }
143
144 RefPtr<FilterEffect> SVGFETurbulenceElement::build(SVGFilterBuilder*, Filter& filter)
145 {
146     if (baseFrequencyX() < 0 || baseFrequencyY() < 0)
147         return nullptr;
148     return FETurbulence::create(filter, type(), baseFrequencyX(), baseFrequencyY(), numOctaves(), seed(), stitchTiles() == SVG_STITCHTYPE_STITCH);
149 }
150
151 }