initial import
[vuplus_webkit] / Source / WebCore / 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 #if ENABLE(SVG) && ENABLE(FILTERS)
23 #include "SVGFEDiffuseLightingElement.h"
24
25 #include "Attr.h"
26 #include "FEDiffuseLighting.h"
27 #include "FilterEffect.h"
28 #include "RenderStyle.h"
29 #include "SVGColor.h"
30 #include "SVGElementInstance.h"
31 #include "SVGFELightElement.h"
32 #include "SVGFilterBuilder.h"
33 #include "SVGNames.h"
34 #include "SVGParserUtilities.h"
35
36 namespace WebCore {
37
38 // Animated property definitions
39 DEFINE_ANIMATED_STRING(SVGFEDiffuseLightingElement, SVGNames::inAttr, In1, in1)
40 DEFINE_ANIMATED_NUMBER(SVGFEDiffuseLightingElement, SVGNames::diffuseConstantAttr, DiffuseConstant, diffuseConstant)
41 DEFINE_ANIMATED_NUMBER(SVGFEDiffuseLightingElement, SVGNames::surfaceScaleAttr, SurfaceScale, surfaceScale)
42 DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFEDiffuseLightingElement, SVGNames::kernelUnitLengthAttr, kernelUnitLengthXIdentifier(), KernelUnitLengthX, kernelUnitLengthX)
43 DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFEDiffuseLightingElement, SVGNames::kernelUnitLengthAttr, kernelUnitLengthYIdentifier(), KernelUnitLengthY, kernelUnitLengthY)
44
45 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEDiffuseLightingElement)
46     REGISTER_LOCAL_ANIMATED_PROPERTY(in1)
47     REGISTER_LOCAL_ANIMATED_PROPERTY(diffuseConstant)
48     REGISTER_LOCAL_ANIMATED_PROPERTY(surfaceScale)
49     REGISTER_LOCAL_ANIMATED_PROPERTY(kernelUnitLengthX)
50     REGISTER_LOCAL_ANIMATED_PROPERTY(kernelUnitLengthY)
51     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
52 END_REGISTER_ANIMATED_PROPERTIES
53
54 inline SVGFEDiffuseLightingElement::SVGFEDiffuseLightingElement(const QualifiedName& tagName, Document* document)
55     : SVGFilterPrimitiveStandardAttributes(tagName, document)
56     , m_diffuseConstant(1)
57     , m_surfaceScale(1)
58 {
59     ASSERT(hasTagName(SVGNames::feDiffuseLightingTag));
60     registerAnimatedPropertiesForSVGFEDiffuseLightingElement();
61 }
62
63 PassRefPtr<SVGFEDiffuseLightingElement> SVGFEDiffuseLightingElement::create(const QualifiedName& tagName, Document* document)
64 {
65     return adoptRef(new SVGFEDiffuseLightingElement(tagName, document));
66 }
67
68 const AtomicString& SVGFEDiffuseLightingElement::kernelUnitLengthXIdentifier()
69 {
70     DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGKernelUnitLengthX"));
71     return s_identifier;
72 }
73
74 const AtomicString& SVGFEDiffuseLightingElement::kernelUnitLengthYIdentifier()
75 {
76     DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGKernelUnitLengthY"));
77     return s_identifier;
78 }
79
80 bool SVGFEDiffuseLightingElement::isSupportedAttribute(const QualifiedName& attrName)
81 {
82     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
83     if (supportedAttributes.isEmpty()) {
84         supportedAttributes.add(SVGNames::inAttr);
85         supportedAttributes.add(SVGNames::diffuseConstantAttr);
86         supportedAttributes.add(SVGNames::surfaceScaleAttr);
87         supportedAttributes.add(SVGNames::kernelUnitLengthAttr);
88         supportedAttributes.add(SVGNames::lighting_colorAttr); // Even though it's a SVG-CSS property, we override its handling here.
89     }
90     return supportedAttributes.contains(attrName);
91 }
92
93 void SVGFEDiffuseLightingElement::parseMappedAttribute(Attribute* attr)
94 {
95     if (!isSupportedAttribute(attr->name()) || attr->name() == SVGNames::lighting_colorAttr) {
96         SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
97         return;
98     }
99
100     const AtomicString& value = attr->value();
101     if (attr->name() == SVGNames::inAttr) {
102         setIn1BaseValue(value);
103         return;
104     }
105
106     if (attr->name() == SVGNames::surfaceScaleAttr) {
107         setSurfaceScaleBaseValue(value.toFloat());
108         return;
109     }
110
111     if (attr->name() == SVGNames::diffuseConstantAttr) {
112         setDiffuseConstantBaseValue(value.toFloat());
113         return;
114     }
115
116     if (attr->name() == SVGNames::kernelUnitLengthAttr) {
117         float x, y;
118         if (parseNumberOptionalNumber(value, x, y)) {
119             setKernelUnitLengthXBaseValue(x);
120             setKernelUnitLengthYBaseValue(y);
121         }
122         return;
123     }
124
125     ASSERT_NOT_REACHED();
126 }
127
128 bool SVGFEDiffuseLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
129 {
130     FEDiffuseLighting* diffuseLighting = static_cast<FEDiffuseLighting*>(effect);
131
132     if (attrName == SVGNames::lighting_colorAttr) {
133         RenderObject* renderer = this->renderer();
134         ASSERT(renderer);
135         ASSERT(renderer->style());
136         return diffuseLighting->setLightingColor(renderer->style()->svgStyle()->lightingColor());
137     }
138     if (attrName == SVGNames::surfaceScaleAttr)
139         return diffuseLighting->setSurfaceScale(surfaceScale());
140     if (attrName == SVGNames::diffuseConstantAttr)
141         return diffuseLighting->setDiffuseConstant(diffuseConstant());
142
143     LightSource* lightSource = const_cast<LightSource*>(diffuseLighting->lightSource());
144     const SVGFELightElement* lightElement = SVGFELightElement::findLightElement(this);
145     ASSERT(lightSource);
146     ASSERT(lightElement);
147
148     if (attrName == SVGNames::azimuthAttr)
149         return lightSource->setAzimuth(lightElement->azimuth());
150     if (attrName == SVGNames::elevationAttr)
151         return lightSource->setElevation(lightElement->elevation());
152     if (attrName == SVGNames::xAttr)
153         return lightSource->setX(lightElement->x());
154     if (attrName == SVGNames::yAttr)
155         return lightSource->setY(lightElement->y());
156     if (attrName == SVGNames::zAttr)
157         return lightSource->setZ(lightElement->z());
158     if (attrName == SVGNames::pointsAtXAttr)
159         return lightSource->setPointsAtX(lightElement->pointsAtX());
160     if (attrName == SVGNames::pointsAtYAttr)
161         return lightSource->setPointsAtY(lightElement->pointsAtY());
162     if (attrName == SVGNames::pointsAtZAttr)
163         return lightSource->setPointsAtZ(lightElement->pointsAtZ());
164     if (attrName == SVGNames::specularExponentAttr)
165         return lightSource->setSpecularExponent(lightElement->specularExponent());
166     if (attrName == SVGNames::limitingConeAngleAttr)
167         return lightSource->setLimitingConeAngle(lightElement->limitingConeAngle());
168
169     ASSERT_NOT_REACHED();
170     return false;
171 }
172
173 void SVGFEDiffuseLightingElement::svgAttributeChanged(const QualifiedName& attrName)
174 {
175     if (!isSupportedAttribute(attrName)) {
176         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
177         return;
178     }
179
180     SVGElementInstance::InvalidationGuard invalidationGuard(this);
181     
182     if (attrName == SVGNames::surfaceScaleAttr
183         || attrName == SVGNames::diffuseConstantAttr
184         || attrName == SVGNames::kernelUnitLengthAttr
185         || attrName == SVGNames::lighting_colorAttr) {
186         primitiveAttributeChanged(attrName);
187         return;
188     }
189
190     if (attrName == SVGNames::inAttr) {
191         invalidate();
192         return;
193     }
194
195     ASSERT_NOT_REACHED();
196 }
197
198 void SVGFEDiffuseLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName)
199 {
200     if (SVGFELightElement::findLightElement(this) != lightElement)
201         return;
202
203     // The light element has different attribute names.
204     primitiveAttributeChanged(attrName);
205 }
206
207 PassRefPtr<FilterEffect> SVGFEDiffuseLightingElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
208 {
209     FilterEffect* input1 = filterBuilder->getEffectById(in1());
210
211     if (!input1)
212         return 0;
213
214     RefPtr<LightSource> lightSource = SVGFELightElement::findLightSource(this);
215     if (!lightSource)
216         return 0;
217
218     RenderObject* renderer = this->renderer();
219     if (!renderer)
220         return 0;
221     
222     ASSERT(renderer->style());
223     Color color = renderer->style()->svgStyle()->lightingColor();
224
225     RefPtr<FilterEffect> effect = FEDiffuseLighting::create(filter, color, surfaceScale(), diffuseConstant(),
226                                                                 kernelUnitLengthX(), kernelUnitLengthY(), lightSource.release());
227     effect->inputEffects().append(input1);
228     return effect.release();
229 }
230
231 }
232
233 #endif // ENABLE(SVG)