SVGFESpecularLightingElement.cpp revision 81bc750723a18f21cd17d1b173cd2a4dda9cea6e
1/*
2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4 * Copyright (C) 2005 Oliver Hunt <oliver@nerget.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB.  If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#include "config.h"
23
24#if ENABLE(SVG) && ENABLE(FILTERS)
25#include "SVGFESpecularLightingElement.h"
26
27#include "Attribute.h"
28#include "FilterEffect.h"
29#include "RenderStyle.h"
30#include "SVGColor.h"
31#include "SVGFELightElement.h"
32#include "SVGFilterBuilder.h"
33#include "SVGNames.h"
34#include "SVGParserUtilities.h"
35
36namespace WebCore {
37
38// Animated property definitions
39DEFINE_ANIMATED_STRING(SVGFESpecularLightingElement, SVGNames::inAttr, In1, in1)
40DEFINE_ANIMATED_NUMBER(SVGFESpecularLightingElement, SVGNames::specularConstantAttr, SpecularConstant, specularConstant)
41DEFINE_ANIMATED_NUMBER(SVGFESpecularLightingElement, SVGNames::specularExponentAttr, SpecularExponent, specularExponent)
42DEFINE_ANIMATED_NUMBER(SVGFESpecularLightingElement, SVGNames::surfaceScaleAttr, SurfaceScale, surfaceScale)
43DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFESpecularLightingElement, SVGNames::kernelUnitLengthAttr, kernelUnitLengthXIdentifier(), KernelUnitLengthX, kernelUnitLengthX)
44DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFESpecularLightingElement, SVGNames::kernelUnitLengthAttr, kernelUnitLengthYIdentifier(), KernelUnitLengthY, kernelUnitLengthY)
45
46inline SVGFESpecularLightingElement::SVGFESpecularLightingElement(const QualifiedName& tagName, Document* document)
47    : SVGFilterPrimitiveStandardAttributes(tagName, document)
48    , m_specularConstant(1)
49    , m_specularExponent(1)
50    , m_surfaceScale(1)
51{
52}
53
54PassRefPtr<SVGFESpecularLightingElement> SVGFESpecularLightingElement::create(const QualifiedName& tagName, Document* document)
55{
56    return adoptRef(new SVGFESpecularLightingElement(tagName, document));
57}
58
59const AtomicString& SVGFESpecularLightingElement::kernelUnitLengthXIdentifier()
60{
61    DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGKernelUnitLengthX"));
62    return s_identifier;
63}
64
65const AtomicString& SVGFESpecularLightingElement::kernelUnitLengthYIdentifier()
66{
67    DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGKernelUnitLengthY"));
68    return s_identifier;
69}
70
71void SVGFESpecularLightingElement::parseMappedAttribute(Attribute* attr)
72{
73    const String& value = attr->value();
74    if (attr->name() == SVGNames::inAttr)
75        setIn1BaseValue(value);
76    else if (attr->name() == SVGNames::surfaceScaleAttr)
77        setSurfaceScaleBaseValue(value.toFloat());
78    else if (attr->name() == SVGNames::specularConstantAttr)
79        setSpecularConstantBaseValue(value.toFloat());
80    else if (attr->name() == SVGNames::specularExponentAttr)
81        setSpecularExponentBaseValue(value.toFloat());
82    else if (attr->name() == SVGNames::kernelUnitLengthAttr) {
83        float x, y;
84        if (parseNumberOptionalNumber(value, x, y)) {
85            setKernelUnitLengthXBaseValue(x);
86            setKernelUnitLengthYBaseValue(y);
87        }
88    } else
89        SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
90}
91
92bool SVGFESpecularLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
93{
94    FESpecularLighting* specularLighting = static_cast<FESpecularLighting*>(effect);
95    if (attrName == SVGNames::surfaceScaleAttr)
96        return specularLighting->setSurfaceScale(surfaceScale());
97    if (attrName == SVGNames::specularConstantAttr)
98        return specularLighting->setSpecularConstant(specularConstant());
99    if (attrName == SVGNames::specularExponentAttr)
100        return specularLighting->setSpecularExponent(specularExponent());
101
102    LightSource* lightSource = const_cast<LightSource*>(specularLighting->lightSource());
103    const SVGFELightElement* lightElement = SVGFELightElement::findLightElement(this);
104    ASSERT(lightSource);
105    ASSERT(lightElement);
106
107    if (attrName == SVGNames::azimuthAttr)
108        return lightSource->setAzimuth(lightElement->azimuth());
109    if (attrName == SVGNames::elevationAttr)
110        return lightSource->setElevation(lightElement->elevation());
111    if (attrName == SVGNames::xAttr)
112        return lightSource->setX(lightElement->x());
113    if (attrName == SVGNames::yAttr)
114        return lightSource->setY(lightElement->y());
115    if (attrName == SVGNames::zAttr)
116        return lightSource->setZ(lightElement->z());
117    if (attrName == SVGNames::pointsAtXAttr)
118        return lightSource->setPointsAtX(lightElement->pointsAtX());
119    if (attrName == SVGNames::pointsAtYAttr)
120        return lightSource->setPointsAtY(lightElement->pointsAtY());
121    if (attrName == SVGNames::pointsAtZAttr)
122        return lightSource->setPointsAtZ(lightElement->pointsAtZ());
123    if (attrName == SVGNames::specularExponentAttr)
124        return lightSource->setSpecularExponent(lightElement->specularExponent());
125    if (attrName == SVGNames::limitingConeAngleAttr)
126        return lightSource->setLimitingConeAngle(lightElement->limitingConeAngle());
127
128    ASSERT_NOT_REACHED();
129    return false;
130}
131
132void SVGFESpecularLightingElement::svgAttributeChanged(const QualifiedName& attrName)
133{
134    SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
135
136    if (attrName == SVGNames::surfaceScaleAttr
137        || attrName == SVGNames::specularConstantAttr
138        || attrName == SVGNames::specularExponentAttr
139        || attrName == SVGNames::kernelUnitLengthAttr)
140        primitiveAttributeChanged(attrName);
141
142    if (attrName == SVGNames::inAttr)
143        invalidate();
144}
145
146void SVGFESpecularLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName)
147{
148    if (SVGFELightElement::findLightElement(this) != lightElement)
149        return;
150
151    // The light element has different attribute names so attrName can identify the requested attribute.
152    primitiveAttributeChanged(attrName);
153}
154
155void SVGFESpecularLightingElement::synchronizeProperty(const QualifiedName& attrName)
156{
157    SVGFilterPrimitiveStandardAttributes::synchronizeProperty(attrName);
158
159    if (attrName == anyQName()) {
160        synchronizeIn1();
161        synchronizeSurfaceScale();
162        synchronizeSpecularConstant();
163        synchronizeSpecularExponent();
164        synchronizeKernelUnitLengthX();
165        synchronizeKernelUnitLengthY();
166        return;
167    }
168
169    if (attrName == SVGNames::inAttr)
170        synchronizeIn1();
171    else if (attrName == SVGNames::surfaceScaleAttr)
172        synchronizeSurfaceScale();
173    else if (attrName == SVGNames::specularConstantAttr)
174        synchronizeSpecularConstant();
175    else if (attrName == SVGNames::specularExponentAttr)
176        synchronizeSpecularExponent();
177    else if (attrName == SVGNames::kernelUnitLengthAttr) {
178        synchronizeKernelUnitLengthX();
179        synchronizeKernelUnitLengthY();
180    }
181}
182
183AttributeToPropertyTypeMap& SVGFESpecularLightingElement::attributeToPropertyTypeMap()
184{
185    DEFINE_STATIC_LOCAL(AttributeToPropertyTypeMap, s_attributeToPropertyTypeMap, ());
186    return s_attributeToPropertyTypeMap;
187}
188
189void SVGFESpecularLightingElement::fillAttributeToPropertyTypeMap()
190{
191    AttributeToPropertyTypeMap& attributeToPropertyTypeMap = this->attributeToPropertyTypeMap();
192
193    SVGFilterPrimitiveStandardAttributes::fillPassedAttributeToPropertyTypeMap(attributeToPropertyTypeMap);
194    attributeToPropertyTypeMap.set(SVGNames::inAttr, AnimatedString);
195    attributeToPropertyTypeMap.set(SVGNames::specularConstantAttr, AnimatedNumber);
196    attributeToPropertyTypeMap.set(SVGNames::specularExponentAttr, AnimatedNumber);
197    attributeToPropertyTypeMap.set(SVGNames::surfaceScaleAttr, AnimatedNumber);
198    attributeToPropertyTypeMap.set(SVGNames::kernelUnitLengthAttr, AnimatedNumberOptionalNumber);
199}
200
201PassRefPtr<FilterEffect> SVGFESpecularLightingElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
202{
203    FilterEffect* input1 = filterBuilder->getEffectById(in1());
204
205    if (!input1)
206        return 0;
207
208    RefPtr<LightSource> lightSource = SVGFELightElement::findLightSource(this);
209    if (!lightSource)
210        return 0;
211
212    RefPtr<RenderStyle> filterStyle = styleForRenderer();
213
214    Color color = filterStyle->svgStyle()->lightingColor();
215
216    RefPtr<FilterEffect> effect = FESpecularLighting::create(filter, color, surfaceScale(), specularConstant(),
217                                          specularExponent(), kernelUnitLengthX(), kernelUnitLengthY(), lightSource.release());
218    effect->inputEffects().append(input1);
219    return effect.release();
220}
221
222}
223
224#endif // ENABLE(SVG)
225