1/*
2 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21
22#if ENABLE(SVG) && ENABLE(FILTERS)
23#include "SVGFEDropShadowElement.h"
24
25#include "Attribute.h"
26#include "RenderStyle.h"
27#include "SVGElementInstance.h"
28#include "SVGFilterBuilder.h"
29#include "SVGNames.h"
30#include "SVGParserUtilities.h"
31#include "SVGRenderStyle.h"
32
33namespace WebCore {
34
35// Animated property definitions
36DEFINE_ANIMATED_STRING(SVGFEDropShadowElement, SVGNames::inAttr, In1, in1)
37DEFINE_ANIMATED_NUMBER(SVGFEDropShadowElement, SVGNames::dxAttr, Dx, dx)
38DEFINE_ANIMATED_NUMBER(SVGFEDropShadowElement, SVGNames::dyAttr, Dy, dy)
39DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFEDropShadowElement, SVGNames::stdDeviationAttr, stdDeviationXIdentifier(), StdDeviationX, stdDeviationX)
40DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFEDropShadowElement, SVGNames::stdDeviationAttr, stdDeviationYIdentifier(), StdDeviationY, stdDeviationY)
41
42BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEDropShadowElement)
43    REGISTER_LOCAL_ANIMATED_PROPERTY(in1)
44    REGISTER_LOCAL_ANIMATED_PROPERTY(dx)
45    REGISTER_LOCAL_ANIMATED_PROPERTY(dy)
46    REGISTER_LOCAL_ANIMATED_PROPERTY(stdDeviationX)
47    REGISTER_LOCAL_ANIMATED_PROPERTY(stdDeviationY)
48    REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
49END_REGISTER_ANIMATED_PROPERTIES
50
51inline SVGFEDropShadowElement::SVGFEDropShadowElement(const QualifiedName& tagName, Document* document)
52    : SVGFilterPrimitiveStandardAttributes(tagName, document)
53    , m_dx(2)
54    , m_dy(2)
55    , m_stdDeviationX(2)
56    , m_stdDeviationY(2)
57{
58    ASSERT(hasTagName(SVGNames::feDropShadowTag));
59    registerAnimatedPropertiesForSVGFEDropShadowElement();
60}
61
62PassRefPtr<SVGFEDropShadowElement> SVGFEDropShadowElement::create(const QualifiedName& tagName, Document* document)
63{
64    return adoptRef(new SVGFEDropShadowElement(tagName, document));
65}
66
67const AtomicString& SVGFEDropShadowElement::stdDeviationXIdentifier()
68{
69    DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGStdDeviationX", AtomicString::ConstructFromLiteral));
70    return s_identifier;
71}
72
73const AtomicString& SVGFEDropShadowElement::stdDeviationYIdentifier()
74{
75    DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGStdDeviationY", AtomicString::ConstructFromLiteral));
76    return s_identifier;
77}
78
79void SVGFEDropShadowElement::setStdDeviation(float x, float y)
80{
81    setStdDeviationXBaseValue(x);
82    setStdDeviationYBaseValue(y);
83    invalidate();
84}
85
86bool SVGFEDropShadowElement::isSupportedAttribute(const QualifiedName& attrName)
87{
88    DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
89    if (supportedAttributes.isEmpty()) {
90        supportedAttributes.add(SVGNames::inAttr);
91        supportedAttributes.add(SVGNames::dxAttr);
92        supportedAttributes.add(SVGNames::dyAttr);
93        supportedAttributes.add(SVGNames::stdDeviationAttr);
94    }
95    return supportedAttributes.contains<QualifiedName, SVGAttributeHashTranslator>(attrName);
96}
97
98void SVGFEDropShadowElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
99{
100    if (!isSupportedAttribute(name)) {
101        SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
102        return;
103    }
104
105    if (name == SVGNames::stdDeviationAttr) {
106        float x, y;
107        if (parseNumberOptionalNumber(value, x, y)) {
108            setStdDeviationXBaseValue(x);
109            setStdDeviationYBaseValue(y);
110        }
111        return;
112    }
113
114    if (name == SVGNames::inAttr) {
115        setIn1BaseValue(value);
116        return;
117    }
118
119    if (name == SVGNames::dxAttr) {
120        setDxBaseValue(value.toFloat());
121        return;
122    }
123
124    if (name == SVGNames::dyAttr) {
125        setDyBaseValue(value.toFloat());
126        return;
127    }
128
129    ASSERT_NOT_REACHED();
130}
131
132void SVGFEDropShadowElement::svgAttributeChanged(const QualifiedName& attrName)
133{
134    if (!isSupportedAttribute(attrName)) {
135        SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
136        return;
137    }
138
139    SVGElementInstance::InvalidationGuard invalidationGuard(this);
140
141    if (attrName == SVGNames::inAttr
142        || attrName == SVGNames::stdDeviationAttr
143        || attrName == SVGNames::dxAttr
144        || attrName == SVGNames::dyAttr) {
145        invalidate();
146        return;
147    }
148
149    ASSERT_NOT_REACHED();
150}
151
152PassRefPtr<FilterEffect> SVGFEDropShadowElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
153{
154    RenderObject* renderer = this->renderer();
155    if (!renderer)
156        return 0;
157
158    if (stdDeviationX() < 0 || stdDeviationY() < 0)
159        return 0;
160
161    ASSERT(renderer->style());
162    const SVGRenderStyle* svgStyle = renderer->style()->svgStyle();
163
164    Color color = svgStyle->floodColor();
165    float opacity = svgStyle->floodOpacity();
166
167    FilterEffect* input1 = filterBuilder->getEffectById(in1());
168    if (!input1)
169        return 0;
170
171    RefPtr<FilterEffect> effect = FEDropShadow::create(filter, stdDeviationX(), stdDeviationY(), dx(), dy(), color, opacity);
172    effect->inputEffects().append(input1);
173    return effect.release();
174}
175
176}
177
178#endif // ENABLE(SVG)
179