Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 #include "core/svg/SVGFETurbulenceElement.h"
24
25 #include "SVGNames.h"
26 #include "core/svg/SVGElementInstance.h"
27 #include "core/svg/SVGParserUtilities.h"
28
29 namespace WebCore {
30
31 template<> const SVGEnumerationStringEntries& getStaticStringEntries<SVGStitchOptions>()
32 {
33     DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
34     if (entries.isEmpty()) {
35         entries.append(std::make_pair(SVG_STITCHTYPE_STITCH, "stitch"));
36         entries.append(std::make_pair(SVG_STITCHTYPE_NOSTITCH, "noStitch"));
37     }
38     return entries;
39 }
40
41 template<> const SVGEnumerationStringEntries& getStaticStringEntries<TurbulenceType>()
42 {
43     DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
44     if (entries.isEmpty()) {
45         entries.append(std::make_pair(FETURBULENCE_TYPE_FRACTALNOISE, "fractalNoise"));
46         entries.append(std::make_pair(FETURBULENCE_TYPE_TURBULENCE, "turbulence"));
47     }
48     return entries;
49 }
50
51 inline SVGFETurbulenceElement::SVGFETurbulenceElement(Document& document)
52     : SVGFilterPrimitiveStandardAttributes(SVGNames::feTurbulenceTag, document)
53     , m_baseFrequency(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::baseFrequencyAttr))
54     , m_seed(SVGAnimatedNumber::create(this, SVGNames::seedAttr, SVGNumber::create(0)))
55     , m_stitchTiles(SVGAnimatedEnumeration<SVGStitchOptions>::create(this, SVGNames::stitchTilesAttr, SVG_STITCHTYPE_NOSTITCH))
56     , m_type(SVGAnimatedEnumeration<TurbulenceType>::create(this, SVGNames::typeAttr, FETURBULENCE_TYPE_TURBULENCE))
57     , m_numOctaves(SVGAnimatedInteger::create(this, SVGNames::numOctavesAttr, SVGInteger::create(1)))
58 {
59     ScriptWrappable::init(this);
60
61     addToPropertyMap(m_baseFrequency);
62     addToPropertyMap(m_seed);
63     addToPropertyMap(m_stitchTiles);
64     addToPropertyMap(m_type);
65     addToPropertyMap(m_numOctaves);
66 }
67
68 PassRefPtr<SVGFETurbulenceElement> SVGFETurbulenceElement::create(Document& document)
69 {
70     return adoptRef(new SVGFETurbulenceElement(document));
71 }
72
73 bool SVGFETurbulenceElement::isSupportedAttribute(const QualifiedName& attrName)
74 {
75     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
76     if (supportedAttributes.isEmpty()) {
77         supportedAttributes.add(SVGNames::baseFrequencyAttr);
78         supportedAttributes.add(SVGNames::numOctavesAttr);
79         supportedAttributes.add(SVGNames::seedAttr);
80         supportedAttributes.add(SVGNames::stitchTilesAttr);
81         supportedAttributes.add(SVGNames::typeAttr);
82     }
83     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
84 }
85
86 void SVGFETurbulenceElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
87 {
88     if (!isSupportedAttribute(name)) {
89         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
90         return;
91     }
92
93     SVGParsingError parseError = NoError;
94
95     if (name == SVGNames::baseFrequencyAttr)
96         m_baseFrequency->setBaseValueAsString(value, parseError);
97     else if (name == SVGNames::numOctavesAttr)
98         m_numOctaves->setBaseValueAsString(value, parseError);
99     else if (name == SVGNames::seedAttr)
100         m_seed->setBaseValueAsString(value, parseError);
101     else if (name == SVGNames::stitchTilesAttr)
102         m_stitchTiles->setBaseValueAsString(value, parseError);
103     else if (name == SVGNames::typeAttr)
104         m_type->setBaseValueAsString(value, parseError);
105     else
106         ASSERT_NOT_REACHED();
107
108     reportAttributeParsingError(parseError, name, value);
109 }
110
111 bool SVGFETurbulenceElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
112 {
113     FETurbulence* turbulence = static_cast<FETurbulence*>(effect);
114     if (attrName == SVGNames::typeAttr)
115         return turbulence->setType(m_type->currentValue()->enumValue());
116     if (attrName == SVGNames::stitchTilesAttr)
117         return turbulence->setStitchTiles(m_stitchTiles->currentValue()->enumValue());
118     if (attrName == SVGNames::baseFrequencyAttr) {
119         bool baseFrequencyXChanged = turbulence->setBaseFrequencyX(baseFrequencyX()->currentValue()->value());
120         bool baseFrequencyYChanged = turbulence->setBaseFrequencyY(baseFrequencyY()->currentValue()->value());
121         return (baseFrequencyXChanged || baseFrequencyYChanged);
122     }
123     if (attrName == SVGNames::seedAttr)
124         return turbulence->setSeed(m_seed->currentValue()->value());
125     if (attrName == SVGNames::numOctavesAttr)
126         return turbulence->setNumOctaves(m_numOctaves->currentValue()->value());
127
128     ASSERT_NOT_REACHED();
129     return false;
130 }
131
132 void SVGFETurbulenceElement::svgAttributeChanged(const QualifiedName& attrName)
133 {
134     if (!isSupportedAttribute(attrName)) {
135         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
136         return;
137     }
138
139     SVGElement::InvalidationGuard invalidationGuard(this);
140
141     if (attrName == SVGNames::baseFrequencyAttr
142         || attrName == SVGNames::numOctavesAttr
143         || attrName == SVGNames::seedAttr
144         || attrName == SVGNames::stitchTilesAttr
145         || attrName == SVGNames::typeAttr) {
146         primitiveAttributeChanged(attrName);
147         return;
148     }
149
150     ASSERT_NOT_REACHED();
151 }
152
153 PassRefPtr<FilterEffect> SVGFETurbulenceElement::build(SVGFilterBuilder*, Filter* filter)
154 {
155     if (baseFrequencyX()->currentValue()->value() < 0 || baseFrequencyY()->currentValue()->value() < 0)
156         return nullptr;
157     return FETurbulence::create(filter, m_type->currentValue()->enumValue(), baseFrequencyX()->currentValue()->value(), baseFrequencyY()->currentValue()->value(), m_numOctaves->currentValue()->value(), m_seed->currentValue()->value(), m_stitchTiles->currentValue()->enumValue() == SVG_STITCHTYPE_STITCH);
158 }
159
160 }