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#include "core/svg/SVGFEDropShadowElement.h"
22
23#include "core/SVGNames.h"
24#include "core/rendering/RenderObject.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
30namespace blink {
31
32inline 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    addToPropertyMap(m_dx);
40    addToPropertyMap(m_dy);
41    addToPropertyMap(m_stdDeviation);
42    addToPropertyMap(m_in1);
43}
44
45DEFINE_NODE_FACTORY(SVGFEDropShadowElement)
46
47void SVGFEDropShadowElement::setStdDeviation(float x, float y)
48{
49    stdDeviationX()->baseValue()->setValue(x);
50    stdDeviationY()->baseValue()->setValue(y);
51    invalidate();
52}
53
54bool SVGFEDropShadowElement::isSupportedAttribute(const QualifiedName& attrName)
55{
56    DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
57    if (supportedAttributes.isEmpty()) {
58        supportedAttributes.add(SVGNames::inAttr);
59        supportedAttributes.add(SVGNames::dxAttr);
60        supportedAttributes.add(SVGNames::dyAttr);
61        supportedAttributes.add(SVGNames::stdDeviationAttr);
62    }
63    return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
64}
65
66void SVGFEDropShadowElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
67{
68    parseAttributeNew(name, value);
69}
70
71void SVGFEDropShadowElement::svgAttributeChanged(const QualifiedName& attrName)
72{
73    if (!isSupportedAttribute(attrName)) {
74        SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
75        return;
76    }
77
78    SVGElement::InvalidationGuard invalidationGuard(this);
79
80    if (attrName == SVGNames::inAttr
81        || attrName == SVGNames::stdDeviationAttr
82        || attrName == SVGNames::dxAttr
83        || attrName == SVGNames::dyAttr) {
84        invalidate();
85        return;
86    }
87
88    ASSERT_NOT_REACHED();
89}
90
91PassRefPtr<FilterEffect> SVGFEDropShadowElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
92{
93    RenderObject* renderer = this->renderer();
94    if (!renderer)
95        return nullptr;
96
97    if (stdDeviationX()->currentValue()->value() < 0 || stdDeviationY()->currentValue()->value() < 0)
98        return nullptr;
99
100    ASSERT(renderer->style());
101    const SVGRenderStyle& svgStyle = renderer->style()->svgStyle();
102
103    Color color = svgStyle.floodColor();
104    float opacity = svgStyle.floodOpacity();
105
106    FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
107    if (!input1)
108        return nullptr;
109
110    RefPtr<FilterEffect> effect = FEDropShadow::create(filter, stdDeviationX()->currentValue()->value(), stdDeviationY()->currentValue()->value(), m_dx->currentValue()->value(), m_dy->currentValue()->value(), color, opacity);
111    effect->inputEffects().append(input1);
112    return effect.release();
113}
114
115} // namespace blink
116