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