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 #include "core/svg/SVGFEDiffuseLightingElement.h"
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"
33 inline SVGFEDiffuseLightingElement::SVGFEDiffuseLightingElement(Document& document)
34 : SVGFilterPrimitiveStandardAttributes(SVGNames::feDiffuseLightingTag, document)
35 , m_diffuseConstant(SVGAnimatedNumber::create(this, SVGNames::diffuseConstantAttr, SVGNumber::create(1)))
36 , m_surfaceScale(SVGAnimatedNumber::create(this, SVGNames::surfaceScaleAttr, SVGNumber::create(1)))
37 , m_kernelUnitLength(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::kernelUnitLengthAttr))
38 , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
40 ScriptWrappable::init(this);
42 addToPropertyMap(m_diffuseConstant);
43 addToPropertyMap(m_surfaceScale);
44 addToPropertyMap(m_kernelUnitLength);
45 addToPropertyMap(m_in1);
48 PassRefPtr<SVGFEDiffuseLightingElement> SVGFEDiffuseLightingElement::create(Document& document)
50 return adoptRef(new SVGFEDiffuseLightingElement(document));
53 bool SVGFEDiffuseLightingElement::isSupportedAttribute(const QualifiedName& attrName)
55 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
56 if (supportedAttributes.isEmpty()) {
57 supportedAttributes.add(SVGNames::inAttr);
58 supportedAttributes.add(SVGNames::diffuseConstantAttr);
59 supportedAttributes.add(SVGNames::surfaceScaleAttr);
60 supportedAttributes.add(SVGNames::kernelUnitLengthAttr);
61 supportedAttributes.add(SVGNames::lighting_colorAttr); // Even though it's a SVG-CSS property, we override its handling here.
63 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
66 void SVGFEDiffuseLightingElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
68 if (!isSupportedAttribute(name) || name == SVGNames::lighting_colorAttr) {
69 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
73 SVGParsingError parseError = NoError;
75 if (name == SVGNames::inAttr)
76 m_in1->setBaseValueAsString(value, parseError);
77 else if (name == SVGNames::diffuseConstantAttr)
78 m_diffuseConstant->setBaseValueAsString(value, parseError);
79 else if (name == SVGNames::surfaceScaleAttr)
80 m_surfaceScale->setBaseValueAsString(value, parseError);
81 else if (name == SVGNames::kernelUnitLengthAttr)
82 m_kernelUnitLength->setBaseValueAsString(value, parseError);
86 reportAttributeParsingError(parseError, name, value);
89 bool SVGFEDiffuseLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
91 FEDiffuseLighting* diffuseLighting = static_cast<FEDiffuseLighting*>(effect);
93 if (attrName == SVGNames::lighting_colorAttr) {
94 RenderObject* renderer = this->renderer();
96 ASSERT(renderer->style());
97 return diffuseLighting->setLightingColor(renderer->style()->svgStyle()->lightingColor());
99 if (attrName == SVGNames::surfaceScaleAttr)
100 return diffuseLighting->setSurfaceScale(m_surfaceScale->currentValue()->value());
101 if (attrName == SVGNames::diffuseConstantAttr)
102 return diffuseLighting->setDiffuseConstant(m_diffuseConstant->currentValue()->value());
104 LightSource* lightSource = const_cast<LightSource*>(diffuseLighting->lightSource());
105 const SVGFELightElement* lightElement = SVGFELightElement::findLightElement(*this);
107 ASSERT(lightElement);
109 if (attrName == SVGNames::azimuthAttr)
110 return lightSource->setAzimuth(lightElement->azimuth()->currentValue()->value());
111 if (attrName == SVGNames::elevationAttr)
112 return lightSource->setElevation(lightElement->elevation()->currentValue()->value());
113 if (attrName == SVGNames::xAttr)
114 return lightSource->setX(lightElement->x()->currentValue()->value());
115 if (attrName == SVGNames::yAttr)
116 return lightSource->setY(lightElement->y()->currentValue()->value());
117 if (attrName == SVGNames::zAttr)
118 return lightSource->setZ(lightElement->z()->currentValue()->value());
119 if (attrName == SVGNames::pointsAtXAttr)
120 return lightSource->setPointsAtX(lightElement->pointsAtX()->currentValue()->value());
121 if (attrName == SVGNames::pointsAtYAttr)
122 return lightSource->setPointsAtY(lightElement->pointsAtY()->currentValue()->value());
123 if (attrName == SVGNames::pointsAtZAttr)
124 return lightSource->setPointsAtZ(lightElement->pointsAtZ()->currentValue()->value());
125 if (attrName == SVGNames::specularExponentAttr)
126 return lightSource->setSpecularExponent(lightElement->specularExponent()->currentValue()->value());
127 if (attrName == SVGNames::limitingConeAngleAttr)
128 return lightSource->setLimitingConeAngle(lightElement->limitingConeAngle()->currentValue()->value());
130 ASSERT_NOT_REACHED();
134 void SVGFEDiffuseLightingElement::svgAttributeChanged(const QualifiedName& attrName)
136 if (!isSupportedAttribute(attrName)) {
137 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
141 SVGElement::InvalidationGuard invalidationGuard(this);
143 if (attrName == SVGNames::surfaceScaleAttr
144 || attrName == SVGNames::diffuseConstantAttr
145 || attrName == SVGNames::kernelUnitLengthAttr
146 || attrName == SVGNames::lighting_colorAttr) {
147 primitiveAttributeChanged(attrName);
151 if (attrName == SVGNames::inAttr) {
156 ASSERT_NOT_REACHED();
159 void SVGFEDiffuseLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName)
161 if (SVGFELightElement::findLightElement(*this) != lightElement)
164 // The light element has different attribute names.
165 primitiveAttributeChanged(attrName);
168 PassRefPtr<FilterEffect> SVGFEDiffuseLightingElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
170 FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
175 RefPtr<LightSource> lightSource = SVGFELightElement::findLightSource(*this);
179 RenderObject* renderer = this->renderer();
183 ASSERT(renderer->style());
184 Color color = renderer->style()->svgStyle()->lightingColor();
186 RefPtr<FilterEffect> effect = FEDiffuseLighting::create(filter, color, m_surfaceScale->currentValue()->value(), m_diffuseConstant->currentValue()->value(),
187 kernelUnitLengthX()->currentValue()->value(), kernelUnitLengthY()->currentValue()->value(), lightSource.release());
188 effect->inputEffects().append(input1);
189 return effect.release();