Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGFEDiffuseLightingElement.cpp
1 /*
2  * Copyright (C) 2005 Oliver Hunt <ojh16@student.canterbury.ac.nz>
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/SVGFEDiffuseLightingElement.h"
23
24 #include "core/rendering/style/RenderStyle.h"
25 #include "core/svg/SVGElementInstance.h"
26 #include "core/svg/SVGParserUtilities.h"
27 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
28 #include "platform/graphics/filters/FEDiffuseLighting.h"
29 #include "platform/graphics/filters/FilterEffect.h"
30
31 namespace WebCore {
32
33 // Animated property definitions
34
35 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEDiffuseLightingElement)
36     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
37 END_REGISTER_ANIMATED_PROPERTIES
38
39 inline SVGFEDiffuseLightingElement::SVGFEDiffuseLightingElement(Document& document)
40     : SVGFilterPrimitiveStandardAttributes(SVGNames::feDiffuseLightingTag, document)
41     , m_diffuseConstant(SVGAnimatedNumber::create(this, SVGNames::diffuseConstantAttr, SVGNumber::create(1)))
42     , m_surfaceScale(SVGAnimatedNumber::create(this, SVGNames::surfaceScaleAttr, SVGNumber::create(1)))
43     , m_kernelUnitLength(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::kernelUnitLengthAttr))
44     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
45 {
46     ScriptWrappable::init(this);
47
48     addToPropertyMap(m_diffuseConstant);
49     addToPropertyMap(m_surfaceScale);
50     addToPropertyMap(m_kernelUnitLength);
51     addToPropertyMap(m_in1);
52     registerAnimatedPropertiesForSVGFEDiffuseLightingElement();
53 }
54
55 PassRefPtr<SVGFEDiffuseLightingElement> SVGFEDiffuseLightingElement::create(Document& document)
56 {
57     return adoptRef(new SVGFEDiffuseLightingElement(document));
58 }
59
60 bool SVGFEDiffuseLightingElement::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::diffuseConstantAttr);
66         supportedAttributes.add(SVGNames::surfaceScaleAttr);
67         supportedAttributes.add(SVGNames::kernelUnitLengthAttr);
68         supportedAttributes.add(SVGNames::lighting_colorAttr); // Even though it's a SVG-CSS property, we override its handling here.
69     }
70     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
71 }
72
73 void SVGFEDiffuseLightingElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
74 {
75     if (!isSupportedAttribute(name) || name == SVGNames::lighting_colorAttr) {
76         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
77         return;
78     }
79
80     SVGParsingError parseError = NoError;
81
82     if (name == SVGNames::inAttr)
83         m_in1->setBaseValueAsString(value, parseError);
84     else if (name == SVGNames::diffuseConstantAttr)
85         m_diffuseConstant->setBaseValueAsString(value, parseError);
86     else if (name == SVGNames::surfaceScaleAttr)
87         m_surfaceScale->setBaseValueAsString(value, parseError);
88     else if (name == SVGNames::kernelUnitLengthAttr)
89         m_kernelUnitLength->setBaseValueAsString(value, parseError);
90     else
91         ASSERT_NOT_REACHED();
92
93     reportAttributeParsingError(parseError, name, value);
94 }
95
96 bool SVGFEDiffuseLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
97 {
98     FEDiffuseLighting* diffuseLighting = static_cast<FEDiffuseLighting*>(effect);
99
100     if (attrName == SVGNames::lighting_colorAttr) {
101         RenderObject* renderer = this->renderer();
102         ASSERT(renderer);
103         ASSERT(renderer->style());
104         return diffuseLighting->setLightingColor(renderer->style()->svgStyle()->lightingColor());
105     }
106     if (attrName == SVGNames::surfaceScaleAttr)
107         return diffuseLighting->setSurfaceScale(m_surfaceScale->currentValue()->value());
108     if (attrName == SVGNames::diffuseConstantAttr)
109         return diffuseLighting->setDiffuseConstant(m_diffuseConstant->currentValue()->value());
110
111     LightSource* lightSource = const_cast<LightSource*>(diffuseLighting->lightSource());
112     const SVGFELightElement* lightElement = SVGFELightElement::findLightElement(this);
113     ASSERT(lightSource);
114     ASSERT(lightElement);
115
116     if (attrName == SVGNames::azimuthAttr)
117         return lightSource->setAzimuth(lightElement->azimuth()->currentValue()->value());
118     if (attrName == SVGNames::elevationAttr)
119         return lightSource->setElevation(lightElement->elevation()->currentValue()->value());
120     if (attrName == SVGNames::xAttr)
121         return lightSource->setX(lightElement->x()->currentValue()->value());
122     if (attrName == SVGNames::yAttr)
123         return lightSource->setY(lightElement->y()->currentValue()->value());
124     if (attrName == SVGNames::zAttr)
125         return lightSource->setZ(lightElement->z()->currentValue()->value());
126     if (attrName == SVGNames::pointsAtXAttr)
127         return lightSource->setPointsAtX(lightElement->pointsAtX()->currentValue()->value());
128     if (attrName == SVGNames::pointsAtYAttr)
129         return lightSource->setPointsAtY(lightElement->pointsAtY()->currentValue()->value());
130     if (attrName == SVGNames::pointsAtZAttr)
131         return lightSource->setPointsAtZ(lightElement->pointsAtZ()->currentValue()->value());
132     if (attrName == SVGNames::specularExponentAttr)
133         return lightSource->setSpecularExponent(lightElement->specularExponent()->currentValue()->value());
134     if (attrName == SVGNames::limitingConeAngleAttr)
135         return lightSource->setLimitingConeAngle(lightElement->limitingConeAngle()->currentValue()->value());
136
137     ASSERT_NOT_REACHED();
138     return false;
139 }
140
141 void SVGFEDiffuseLightingElement::svgAttributeChanged(const QualifiedName& attrName)
142 {
143     if (!isSupportedAttribute(attrName)) {
144         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
145         return;
146     }
147
148     SVGElementInstance::InvalidationGuard invalidationGuard(this);
149
150     if (attrName == SVGNames::surfaceScaleAttr
151         || attrName == SVGNames::diffuseConstantAttr
152         || attrName == SVGNames::kernelUnitLengthAttr
153         || attrName == SVGNames::lighting_colorAttr) {
154         primitiveAttributeChanged(attrName);
155         return;
156     }
157
158     if (attrName == SVGNames::inAttr) {
159         invalidate();
160         return;
161     }
162
163     ASSERT_NOT_REACHED();
164 }
165
166 void SVGFEDiffuseLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName)
167 {
168     if (SVGFELightElement::findLightElement(this) != lightElement)
169         return;
170
171     // The light element has different attribute names.
172     primitiveAttributeChanged(attrName);
173 }
174
175 PassRefPtr<FilterEffect> SVGFEDiffuseLightingElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
176 {
177     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
178
179     if (!input1)
180         return 0;
181
182     RefPtr<LightSource> lightSource = SVGFELightElement::findLightSource(this);
183     if (!lightSource)
184         return 0;
185
186     RenderObject* renderer = this->renderer();
187     if (!renderer)
188         return 0;
189
190     ASSERT(renderer->style());
191     Color color = renderer->style()->svgStyle()->lightingColor();
192
193     RefPtr<FilterEffect> effect = FEDiffuseLighting::create(filter, color, m_surfaceScale->currentValue()->value(), m_diffuseConstant->currentValue()->value(),
194         kernelUnitLengthX()->currentValue()->value(), kernelUnitLengthY()->currentValue()->value(), lightSource.release());
195     effect->inputEffects().append(input1);
196     return effect.release();
197 }
198
199 }