Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGFEDropShadowElement.cpp
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 #include "core/svg/SVGFEDropShadowElement.h"
23
24 #include "SVGNames.h"
25 #include "core/rendering/style/RenderStyle.h"
26 #include "core/rendering/style/SVGRenderStyle.h"
27 #include "core/svg/SVGElementInstance.h"
28 #include "core/svg/SVGParserUtilities.h"
29 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
30
31 namespace WebCore {
32
33 // Animated property definitions
34
35 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEDropShadowElement)
36     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
37 END_REGISTER_ANIMATED_PROPERTIES
38
39 inline SVGFEDropShadowElement::SVGFEDropShadowElement(Document& document)
40     : SVGFilterPrimitiveStandardAttributes(SVGNames::feDropShadowTag, document)
41     , m_dx(SVGAnimatedNumber::create(this, SVGNames::dxAttr, SVGNumber::create(2)))
42     , m_dy(SVGAnimatedNumber::create(this, SVGNames::dyAttr, SVGNumber::create(2)))
43     , m_stdDeviation(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::stdDeviationAttr, 2, 2))
44     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
45 {
46     ScriptWrappable::init(this);
47
48     addToPropertyMap(m_dx);
49     addToPropertyMap(m_dy);
50     addToPropertyMap(m_stdDeviation);
51     addToPropertyMap(m_in1);
52     registerAnimatedPropertiesForSVGFEDropShadowElement();
53 }
54
55 PassRefPtr<SVGFEDropShadowElement> SVGFEDropShadowElement::create(Document& document)
56 {
57     return adoptRef(new SVGFEDropShadowElement(document));
58 }
59
60 void SVGFEDropShadowElement::setStdDeviation(float x, float y)
61 {
62     stdDeviationX()->baseValue()->setValue(x);
63     stdDeviationY()->baseValue()->setValue(y);
64     invalidate();
65 }
66
67 bool SVGFEDropShadowElement::isSupportedAttribute(const QualifiedName& attrName)
68 {
69     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
70     if (supportedAttributes.isEmpty()) {
71         supportedAttributes.add(SVGNames::inAttr);
72         supportedAttributes.add(SVGNames::dxAttr);
73         supportedAttributes.add(SVGNames::dyAttr);
74         supportedAttributes.add(SVGNames::stdDeviationAttr);
75     }
76     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
77 }
78
79 void SVGFEDropShadowElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
80 {
81     if (!isSupportedAttribute(name)) {
82         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
83         return;
84     }
85
86     SVGParsingError parseError = NoError;
87
88     if (name == SVGNames::inAttr)
89         m_in1->setBaseValueAsString(value, parseError);
90     else if (name == SVGNames::dxAttr)
91         m_dx->setBaseValueAsString(value, parseError);
92     else if (name == SVGNames::dyAttr)
93         m_dy->setBaseValueAsString(value, parseError);
94     else if (name == SVGNames::stdDeviationAttr)
95         m_stdDeviation->setBaseValueAsString(value, parseError);
96     else
97         ASSERT_NOT_REACHED();
98
99     reportAttributeParsingError(parseError, name, value);
100 }
101
102 void SVGFEDropShadowElement::svgAttributeChanged(const QualifiedName& attrName)
103 {
104     if (!isSupportedAttribute(attrName)) {
105         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
106         return;
107     }
108
109     SVGElementInstance::InvalidationGuard invalidationGuard(this);
110
111     if (attrName == SVGNames::inAttr
112         || attrName == SVGNames::stdDeviationAttr
113         || attrName == SVGNames::dxAttr
114         || attrName == SVGNames::dyAttr) {
115         invalidate();
116         return;
117     }
118
119     ASSERT_NOT_REACHED();
120 }
121
122 PassRefPtr<FilterEffect> SVGFEDropShadowElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
123 {
124     RenderObject* renderer = this->renderer();
125     if (!renderer)
126         return 0;
127
128     if (stdDeviationX()->currentValue()->value() < 0 || stdDeviationY()->currentValue()->value() < 0)
129         return 0;
130
131     ASSERT(renderer->style());
132     const SVGRenderStyle* svgStyle = renderer->style()->svgStyle();
133
134     Color color = svgStyle->floodColor();
135     float opacity = svgStyle->floodOpacity();
136
137     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
138     if (!input1)
139         return 0;
140
141     RefPtr<FilterEffect> effect = FEDropShadow::create(filter, stdDeviationX()->currentValue()->value(), stdDeviationY()->currentValue()->value(), m_dx->currentValue()->value(), m_dy->currentValue()->value(), color, opacity);
142     effect->inputEffects().append(input1);
143     return effect.release();
144 }
145
146 }