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 parseAttributeNew(name, value);
65 bool SVGFEDiffuseLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
67 FEDiffuseLighting* diffuseLighting = static_cast<FEDiffuseLighting*>(effect);
69 if (attrName == SVGNames::lighting_colorAttr) {
70 RenderObject* renderer = this->renderer();
72 ASSERT(renderer->style());
73 return diffuseLighting->setLightingColor(renderer->style()->svgStyle().lightingColor());
75 if (attrName == SVGNames::surfaceScaleAttr)
76 return diffuseLighting->setSurfaceScale(m_surfaceScale->currentValue()->value());
77 if (attrName == SVGNames::diffuseConstantAttr)
78 return diffuseLighting->setDiffuseConstant(m_diffuseConstant->currentValue()->value());
80 LightSource* lightSource = const_cast<LightSource*>(diffuseLighting->lightSource());
81 const SVGFELightElement* lightElement = SVGFELightElement::findLightElement(*this);
84 ASSERT(effect->filter());
86 if (attrName == SVGNames::azimuthAttr)
87 return lightSource->setAzimuth(lightElement->azimuth()->currentValue()->value());
88 if (attrName == SVGNames::elevationAttr)
89 return lightSource->setElevation(lightElement->elevation()->currentValue()->value());
90 if (attrName == SVGNames::xAttr || attrName == SVGNames::yAttr || attrName == SVGNames::zAttr)
91 return lightSource->setPosition(effect->filter()->resolve3dPoint(lightElement->position()));
92 if (attrName == SVGNames::pointsAtXAttr || attrName == SVGNames::pointsAtYAttr || attrName == SVGNames::pointsAtZAttr)
93 return lightSource->setPointsAt(effect->filter()->resolve3dPoint(lightElement->pointsAt()));
94 if (attrName == SVGNames::specularExponentAttr)
95 return lightSource->setSpecularExponent(lightElement->specularExponent()->currentValue()->value());
96 if (attrName == SVGNames::limitingConeAngleAttr)
97 return lightSource->setLimitingConeAngle(lightElement->limitingConeAngle()->currentValue()->value());
103 void SVGFEDiffuseLightingElement::svgAttributeChanged(const QualifiedName& attrName)
105 if (!isSupportedAttribute(attrName)) {
106 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
110 SVGElement::InvalidationGuard invalidationGuard(this);
112 if (attrName == SVGNames::surfaceScaleAttr
113 || attrName == SVGNames::diffuseConstantAttr
114 || attrName == SVGNames::kernelUnitLengthAttr
115 || attrName == SVGNames::lighting_colorAttr) {
116 primitiveAttributeChanged(attrName);
120 if (attrName == SVGNames::inAttr) {
125 ASSERT_NOT_REACHED();
128 void SVGFEDiffuseLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName)
130 if (SVGFELightElement::findLightElement(*this) != lightElement)
133 // The light element has different attribute names.
134 primitiveAttributeChanged(attrName);
137 PassRefPtr<FilterEffect> SVGFEDiffuseLightingElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
139 FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
144 SVGFELightElement* lightNode = SVGFELightElement::findLightElement(*this);
148 RenderObject* renderer = this->renderer();
152 ASSERT(renderer->style());
153 Color color = renderer->style()->svgStyle().lightingColor();
155 RefPtr<LightSource> lightSource = lightNode->lightSource(filter);
156 RefPtr<FilterEffect> effect = FEDiffuseLighting::create(filter, color, m_surfaceScale->currentValue()->value(), m_diffuseConstant->currentValue()->value(),
157 kernelUnitLengthX()->currentValue()->value(), kernelUnitLengthY()->currentValue()->value(), lightSource.release());
158 effect->inputEffects().append(input1);
159 return effect.release();