2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
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.
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.
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.
23 #include "core/svg/SVGFETurbulenceElement.h"
26 #include "core/svg/SVGElementInstance.h"
27 #include "core/svg/SVGParserUtilities.h"
31 template<> const SVGEnumerationStringEntries& getStaticStringEntries<SVGStitchOptions>()
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"));
41 template<> const SVGEnumerationStringEntries& getStaticStringEntries<TurbulenceType>()
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"));
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)))
59 ScriptWrappable::init(this);
61 addToPropertyMap(m_baseFrequency);
62 addToPropertyMap(m_seed);
63 addToPropertyMap(m_stitchTiles);
64 addToPropertyMap(m_type);
65 addToPropertyMap(m_numOctaves);
68 PassRefPtr<SVGFETurbulenceElement> SVGFETurbulenceElement::create(Document& document)
70 return adoptRef(new SVGFETurbulenceElement(document));
73 bool SVGFETurbulenceElement::isSupportedAttribute(const QualifiedName& attrName)
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);
83 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
86 void SVGFETurbulenceElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
88 if (!isSupportedAttribute(name)) {
89 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
93 SVGParsingError parseError = NoError;
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);
106 ASSERT_NOT_REACHED();
108 reportAttributeParsingError(parseError, name, value);
111 bool SVGFETurbulenceElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
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);
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());
128 ASSERT_NOT_REACHED();
132 void SVGFETurbulenceElement::svgAttributeChanged(const QualifiedName& attrName)
134 if (!isSupportedAttribute(attrName)) {
135 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
139 SVGElement::InvalidationGuard invalidationGuard(this);
141 if (attrName == SVGNames::baseFrequencyAttr
142 || attrName == SVGNames::numOctavesAttr
143 || attrName == SVGNames::seedAttr
144 || attrName == SVGNames::stitchTilesAttr
145 || attrName == SVGNames::typeAttr) {
146 primitiveAttributeChanged(attrName);
150 ASSERT_NOT_REACHED();
153 PassRefPtr<FilterEffect> SVGFETurbulenceElement::build(SVGFilterBuilder*, Filter* filter)
155 if (baseFrequencyX()->currentValue()->value() < 0 || baseFrequencyY()->currentValue()->value() < 0)
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);