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.
21 #include "core/svg/SVGFEDiffuseLightingElement.h"
23 #include "core/rendering/RenderObject.h"
24 #include "core/rendering/style/RenderStyle.h"
25 #include "core/svg/SVGParserUtilities.h"
26 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
27 #include "platform/graphics/filters/FEDiffuseLighting.h"
28 #include "platform/graphics/filters/FilterEffect.h"
32 inline SVGFEDiffuseLightingElement::SVGFEDiffuseLightingElement(Document& document)
33 : SVGFilterPrimitiveStandardAttributes(SVGNames::feDiffuseLightingTag, document)
34 , m_diffuseConstant(SVGAnimatedNumber::create(this, SVGNames::diffuseConstantAttr, SVGNumber::create(1)))
35 , m_surfaceScale(SVGAnimatedNumber::create(this, SVGNames::surfaceScaleAttr, SVGNumber::create(1)))
36 , m_kernelUnitLength(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::kernelUnitLengthAttr))
37 , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
39 addToPropertyMap(m_diffuseConstant);
40 addToPropertyMap(m_surfaceScale);
41 addToPropertyMap(m_kernelUnitLength);
42 addToPropertyMap(m_in1);
45 DEFINE_NODE_FACTORY(SVGFEDiffuseLightingElement)
47 bool SVGFEDiffuseLightingElement::isSupportedAttribute(const QualifiedName& attrName)
49 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
50 if (supportedAttributes.isEmpty()) {
51 supportedAttributes.add(SVGNames::inAttr);
52 supportedAttributes.add(SVGNames::diffuseConstantAttr);
53 supportedAttributes.add(SVGNames::surfaceScaleAttr);
54 supportedAttributes.add(SVGNames::kernelUnitLengthAttr);
55 supportedAttributes.add(SVGNames::lighting_colorAttr); // Even though it's a SVG-CSS property, we override its handling here.
57 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
60 void SVGFEDiffuseLightingElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
62 if (!isSupportedAttribute(name) || name == SVGNames::lighting_colorAttr) {
63 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
67 SVGParsingError parseError = NoError;
69 if (name == SVGNames::inAttr)
70 m_in1->setBaseValueAsString(value, parseError);
71 else if (name == SVGNames::diffuseConstantAttr)
72 m_diffuseConstant->setBaseValueAsString(value, parseError);
73 else if (name == SVGNames::surfaceScaleAttr)
74 m_surfaceScale->setBaseValueAsString(value, parseError);
75 else if (name == SVGNames::kernelUnitLengthAttr)
76 m_kernelUnitLength->setBaseValueAsString(value, parseError);
80 reportAttributeParsingError(parseError, name, value);
83 bool SVGFEDiffuseLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
85 FEDiffuseLighting* diffuseLighting = static_cast<FEDiffuseLighting*>(effect);
87 if (attrName == SVGNames::lighting_colorAttr) {
88 RenderObject* renderer = this->renderer();
90 ASSERT(renderer->style());
91 return diffuseLighting->setLightingColor(renderer->style()->svgStyle().lightingColor());
93 if (attrName == SVGNames::surfaceScaleAttr)
94 return diffuseLighting->setSurfaceScale(m_surfaceScale->currentValue()->value());
95 if (attrName == SVGNames::diffuseConstantAttr)
96 return diffuseLighting->setDiffuseConstant(m_diffuseConstant->currentValue()->value());
98 LightSource* lightSource = const_cast<LightSource*>(diffuseLighting->lightSource());
99 const SVGFELightElement* lightElement = SVGFELightElement::findLightElement(*this);
101 ASSERT(lightElement);
102 ASSERT(effect->filter());
104 if (attrName == SVGNames::azimuthAttr)
105 return lightSource->setAzimuth(lightElement->azimuth()->currentValue()->value());
106 if (attrName == SVGNames::elevationAttr)
107 return lightSource->setElevation(lightElement->elevation()->currentValue()->value());
108 if (attrName == SVGNames::xAttr || attrName == SVGNames::yAttr || attrName == SVGNames::zAttr)
109 return lightSource->setPosition(effect->filter()->resolve3dPoint(lightElement->position()));
110 if (attrName == SVGNames::pointsAtXAttr || attrName == SVGNames::pointsAtYAttr || attrName == SVGNames::pointsAtZAttr)
111 return lightSource->setPointsAt(effect->filter()->resolve3dPoint(lightElement->pointsAt()));
112 if (attrName == SVGNames::specularExponentAttr)
113 return lightSource->setSpecularExponent(lightElement->specularExponent()->currentValue()->value());
114 if (attrName == SVGNames::limitingConeAngleAttr)
115 return lightSource->setLimitingConeAngle(lightElement->limitingConeAngle()->currentValue()->value());
117 ASSERT_NOT_REACHED();
121 void SVGFEDiffuseLightingElement::svgAttributeChanged(const QualifiedName& attrName)
123 if (!isSupportedAttribute(attrName)) {
124 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
128 SVGElement::InvalidationGuard invalidationGuard(this);
130 if (attrName == SVGNames::surfaceScaleAttr
131 || attrName == SVGNames::diffuseConstantAttr
132 || attrName == SVGNames::kernelUnitLengthAttr
133 || attrName == SVGNames::lighting_colorAttr) {
134 primitiveAttributeChanged(attrName);
138 if (attrName == SVGNames::inAttr) {
143 ASSERT_NOT_REACHED();
146 void SVGFEDiffuseLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName)
148 if (SVGFELightElement::findLightElement(*this) != lightElement)
151 // The light element has different attribute names.
152 primitiveAttributeChanged(attrName);
155 PassRefPtr<FilterEffect> SVGFEDiffuseLightingElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
157 FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
162 SVGFELightElement* lightNode = SVGFELightElement::findLightElement(*this);
166 RenderObject* renderer = this->renderer();
170 ASSERT(renderer->style());
171 Color color = renderer->style()->svgStyle().lightingColor();
173 RefPtr<LightSource> lightSource = lightNode->lightSource(filter);
174 RefPtr<FilterEffect> effect = FEDiffuseLighting::create(filter, color, m_surfaceScale->currentValue()->value(), m_diffuseConstant->currentValue()->value(),
175 kernelUnitLengthX()->currentValue()->value(), kernelUnitLengthY()->currentValue()->value(), lightSource.release());
176 effect->inputEffects().append(input1);
177 return effect.release();