2 * Copyright (C) 2005 Oliver Hunt <ojh16@student.canterbury.ac.nz>
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.
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.
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.
22 #if ENABLE(SVG) && ENABLE(FILTERS)
23 #include "SVGFEDiffuseLightingElement.h"
26 #include "FEDiffuseLighting.h"
27 #include "FilterEffect.h"
28 #include "RenderStyle.h"
30 #include "SVGElementInstance.h"
31 #include "SVGFELightElement.h"
32 #include "SVGFilterBuilder.h"
34 #include "SVGParserUtilities.h"
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)
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
54 inline SVGFEDiffuseLightingElement::SVGFEDiffuseLightingElement(const QualifiedName& tagName, Document* document)
55 : SVGFilterPrimitiveStandardAttributes(tagName, document)
56 , m_diffuseConstant(1)
59 ASSERT(hasTagName(SVGNames::feDiffuseLightingTag));
60 registerAnimatedPropertiesForSVGFEDiffuseLightingElement();
63 PassRefPtr<SVGFEDiffuseLightingElement> SVGFEDiffuseLightingElement::create(const QualifiedName& tagName, Document* document)
65 return adoptRef(new SVGFEDiffuseLightingElement(tagName, document));
68 const AtomicString& SVGFEDiffuseLightingElement::kernelUnitLengthXIdentifier()
70 DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGKernelUnitLengthX"));
74 const AtomicString& SVGFEDiffuseLightingElement::kernelUnitLengthYIdentifier()
76 DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGKernelUnitLengthY"));
80 bool SVGFEDiffuseLightingElement::isSupportedAttribute(const QualifiedName& attrName)
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.
90 return supportedAttributes.contains(attrName);
93 void SVGFEDiffuseLightingElement::parseMappedAttribute(Attribute* attr)
95 if (!isSupportedAttribute(attr->name()) || attr->name() == SVGNames::lighting_colorAttr) {
96 SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
100 const AtomicString& value = attr->value();
101 if (attr->name() == SVGNames::inAttr) {
102 setIn1BaseValue(value);
106 if (attr->name() == SVGNames::surfaceScaleAttr) {
107 setSurfaceScaleBaseValue(value.toFloat());
111 if (attr->name() == SVGNames::diffuseConstantAttr) {
112 setDiffuseConstantBaseValue(value.toFloat());
116 if (attr->name() == SVGNames::kernelUnitLengthAttr) {
118 if (parseNumberOptionalNumber(value, x, y)) {
119 setKernelUnitLengthXBaseValue(x);
120 setKernelUnitLengthYBaseValue(y);
125 ASSERT_NOT_REACHED();
128 bool SVGFEDiffuseLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
130 FEDiffuseLighting* diffuseLighting = static_cast<FEDiffuseLighting*>(effect);
132 if (attrName == SVGNames::lighting_colorAttr) {
133 RenderObject* renderer = this->renderer();
135 ASSERT(renderer->style());
136 return diffuseLighting->setLightingColor(renderer->style()->svgStyle()->lightingColor());
138 if (attrName == SVGNames::surfaceScaleAttr)
139 return diffuseLighting->setSurfaceScale(surfaceScale());
140 if (attrName == SVGNames::diffuseConstantAttr)
141 return diffuseLighting->setDiffuseConstant(diffuseConstant());
143 LightSource* lightSource = const_cast<LightSource*>(diffuseLighting->lightSource());
144 const SVGFELightElement* lightElement = SVGFELightElement::findLightElement(this);
146 ASSERT(lightElement);
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());
169 ASSERT_NOT_REACHED();
173 void SVGFEDiffuseLightingElement::svgAttributeChanged(const QualifiedName& attrName)
175 if (!isSupportedAttribute(attrName)) {
176 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
180 SVGElementInstance::InvalidationGuard invalidationGuard(this);
182 if (attrName == SVGNames::surfaceScaleAttr
183 || attrName == SVGNames::diffuseConstantAttr
184 || attrName == SVGNames::kernelUnitLengthAttr
185 || attrName == SVGNames::lighting_colorAttr) {
186 primitiveAttributeChanged(attrName);
190 if (attrName == SVGNames::inAttr) {
195 ASSERT_NOT_REACHED();
198 void SVGFEDiffuseLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName)
200 if (SVGFELightElement::findLightElement(this) != lightElement)
203 // The light element has different attribute names.
204 primitiveAttributeChanged(attrName);
207 PassRefPtr<FilterEffect> SVGFEDiffuseLightingElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
209 FilterEffect* input1 = filterBuilder->getEffectById(in1());
214 RefPtr<LightSource> lightSource = SVGFELightElement::findLightSource(this);
218 RenderObject* renderer = this->renderer();
222 ASSERT(renderer->style());
223 Color color = renderer->style()->svgStyle()->lightingColor();
225 RefPtr<FilterEffect> effect = FEDiffuseLighting::create(filter, color, surfaceScale(), diffuseConstant(),
226 kernelUnitLengthX(), kernelUnitLengthY(), lightSource.release());
227 effect->inputEffects().append(input1);
228 return effect.release();
233 #endif // ENABLE(SVG)