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/SVGFEGaussianBlurElement.h"
24
25#include "SVGNames.h"
26#include "platform/graphics/filters/FilterEffect.h"
27#include "core/svg/SVGElementInstance.h"
28#include "core/svg/SVGParserUtilities.h"
29#include "core/svg/graphics/filters/SVGFilterBuilder.h"
30
31namespace WebCore {
32
33// Animated property definitions
34DEFINE_ANIMATED_STRING(SVGFEGaussianBlurElement, SVGNames::inAttr, In1, in1)
35DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFEGaussianBlurElement, SVGNames::stdDeviationAttr, stdDeviationXIdentifier(), StdDeviationX, stdDeviationX)
36DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFEGaussianBlurElement, SVGNames::stdDeviationAttr, stdDeviationYIdentifier(), StdDeviationY, stdDeviationY)
37
38BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEGaussianBlurElement)
39    REGISTER_LOCAL_ANIMATED_PROPERTY(in1)
40    REGISTER_LOCAL_ANIMATED_PROPERTY(stdDeviationX)
41    REGISTER_LOCAL_ANIMATED_PROPERTY(stdDeviationY)
42    REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
43END_REGISTER_ANIMATED_PROPERTIES
44
45inline SVGFEGaussianBlurElement::SVGFEGaussianBlurElement(Document& document)
46    : SVGFilterPrimitiveStandardAttributes(SVGNames::feGaussianBlurTag, document)
47{
48    ScriptWrappable::init(this);
49    registerAnimatedPropertiesForSVGFEGaussianBlurElement();
50}
51
52PassRefPtr<SVGFEGaussianBlurElement> SVGFEGaussianBlurElement::create(Document& document)
53{
54    return adoptRef(new SVGFEGaussianBlurElement(document));
55}
56
57const AtomicString& SVGFEGaussianBlurElement::stdDeviationXIdentifier()
58{
59    DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGStdDeviationX", AtomicString::ConstructFromLiteral));
60    return s_identifier;
61}
62
63const AtomicString& SVGFEGaussianBlurElement::stdDeviationYIdentifier()
64{
65    DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGStdDeviationY", AtomicString::ConstructFromLiteral));
66    return s_identifier;
67}
68
69void SVGFEGaussianBlurElement::setStdDeviation(float x, float y)
70{
71    setStdDeviationXBaseValue(x);
72    setStdDeviationYBaseValue(y);
73    invalidate();
74}
75
76bool SVGFEGaussianBlurElement::isSupportedAttribute(const QualifiedName& attrName)
77{
78    DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
79    if (supportedAttributes.isEmpty()) {
80        supportedAttributes.add(SVGNames::inAttr);
81        supportedAttributes.add(SVGNames::stdDeviationAttr);
82    }
83    return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
84}
85
86void SVGFEGaussianBlurElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
87{
88    if (!isSupportedAttribute(name)) {
89        SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
90        return;
91    }
92
93    if (name == SVGNames::stdDeviationAttr) {
94        float x, y;
95        if (parseNumberOptionalNumber(value, x, y)) {
96            setStdDeviationXBaseValue(x);
97            setStdDeviationYBaseValue(y);
98        }
99        return;
100    }
101
102    if (name == SVGNames::inAttr) {
103        setIn1BaseValue(value);
104        return;
105    }
106
107    ASSERT_NOT_REACHED();
108}
109
110void SVGFEGaussianBlurElement::svgAttributeChanged(const QualifiedName& attrName)
111{
112    if (!isSupportedAttribute(attrName)) {
113        SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
114        return;
115    }
116
117    SVGElementInstance::InvalidationGuard invalidationGuard(this);
118
119    if (attrName == SVGNames::inAttr || attrName == SVGNames::stdDeviationAttr) {
120        invalidate();
121        return;
122    }
123
124    ASSERT_NOT_REACHED();
125}
126
127PassRefPtr<FilterEffect> SVGFEGaussianBlurElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
128{
129    FilterEffect* input1 = filterBuilder->getEffectById(in1CurrentValue());
130
131    if (!input1)
132        return 0;
133
134    if (stdDeviationXCurrentValue() < 0 || stdDeviationYCurrentValue() < 0)
135        return 0;
136
137    RefPtr<FilterEffect> effect = FEGaussianBlur::create(filter, stdDeviationXCurrentValue(), stdDeviationYCurrentValue());
138    effect->inputEffects().append(input1);
139    return effect.release();
140}
141
142}
143