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