Upstream version 7.36.149.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 inline SVGFEDropShadowElement::SVGFEDropShadowElement(Document& document)
34     : SVGFilterPrimitiveStandardAttributes(SVGNames::feDropShadowTag, document)
35     , m_dx(SVGAnimatedNumber::create(this, SVGNames::dxAttr, SVGNumber::create(2)))
36     , m_dy(SVGAnimatedNumber::create(this, SVGNames::dyAttr, SVGNumber::create(2)))
37     , m_stdDeviation(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::stdDeviationAttr, 2, 2))
38     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
39 {
40     ScriptWrappable::init(this);
41
42     addToPropertyMap(m_dx);
43     addToPropertyMap(m_dy);
44     addToPropertyMap(m_stdDeviation);
45     addToPropertyMap(m_in1);
46 }
47
48 PassRefPtr<SVGFEDropShadowElement> SVGFEDropShadowElement::create(Document& document)
49 {
50     return adoptRef(new SVGFEDropShadowElement(document));
51 }
52
53 void SVGFEDropShadowElement::setStdDeviation(float x, float y)
54 {
55     stdDeviationX()->baseValue()->setValue(x);
56     stdDeviationY()->baseValue()->setValue(y);
57     invalidate();
58 }
59
60 bool SVGFEDropShadowElement::isSupportedAttribute(const QualifiedName& attrName)
61 {
62     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
63     if (supportedAttributes.isEmpty()) {
64         supportedAttributes.add(SVGNames::inAttr);
65         supportedAttributes.add(SVGNames::dxAttr);
66         supportedAttributes.add(SVGNames::dyAttr);
67         supportedAttributes.add(SVGNames::stdDeviationAttr);
68     }
69     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
70 }
71
72 void SVGFEDropShadowElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
73 {
74     if (!isSupportedAttribute(name)) {
75         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
76         return;
77     }
78
79     SVGParsingError parseError = NoError;
80
81     if (name == SVGNames::inAttr)
82         m_in1->setBaseValueAsString(value, parseError);
83     else if (name == SVGNames::dxAttr)
84         m_dx->setBaseValueAsString(value, parseError);
85     else if (name == SVGNames::dyAttr)
86         m_dy->setBaseValueAsString(value, parseError);
87     else if (name == SVGNames::stdDeviationAttr)
88         m_stdDeviation->setBaseValueAsString(value, parseError);
89     else
90         ASSERT_NOT_REACHED();
91
92     reportAttributeParsingError(parseError, name, value);
93 }
94
95 void SVGFEDropShadowElement::svgAttributeChanged(const QualifiedName& attrName)
96 {
97     if (!isSupportedAttribute(attrName)) {
98         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
99         return;
100     }
101
102     SVGElement::InvalidationGuard invalidationGuard(this);
103
104     if (attrName == SVGNames::inAttr
105         || attrName == SVGNames::stdDeviationAttr
106         || attrName == SVGNames::dxAttr
107         || attrName == SVGNames::dyAttr) {
108         invalidate();
109         return;
110     }
111
112     ASSERT_NOT_REACHED();
113 }
114
115 PassRefPtr<FilterEffect> SVGFEDropShadowElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
116 {
117     RenderObject* renderer = this->renderer();
118     if (!renderer)
119         return nullptr;
120
121     if (stdDeviationX()->currentValue()->value() < 0 || stdDeviationY()->currentValue()->value() < 0)
122         return nullptr;
123
124     ASSERT(renderer->style());
125     const SVGRenderStyle* svgStyle = renderer->style()->svgStyle();
126
127     Color color = svgStyle->floodColor();
128     float opacity = svgStyle->floodOpacity();
129
130     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
131     if (!input1)
132         return nullptr;
133
134     RefPtr<FilterEffect> effect = FEDropShadow::create(filter, stdDeviationX()->currentValue()->value(), stdDeviationY()->currentValue()->value(), m_dx->currentValue()->value(), m_dy->currentValue()->value(), color, opacity);
135     effect->inputEffects().append(input1);
136     return effect.release();
137 }
138
139 }