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 "SVGElementInstance.h"
32#include "SVGFELightElement.h"
33#include "SVGFilterBuilder.h"
34#include "SVGNames.h"
35#include "SVGParserUtilities.h"
36
37namespace WebCore {
38
39// Animated property definitions
40DEFINE_ANIMATED_STRING(SVGFESpecularLightingElement, SVGNames::inAttr, In1, in1)
41DEFINE_ANIMATED_NUMBER(SVGFESpecularLightingElement, SVGNames::specularConstantAttr, SpecularConstant, specularConstant)
42DEFINE_ANIMATED_NUMBER(SVGFESpecularLightingElement, SVGNames::specularExponentAttr, SpecularExponent, specularExponent)
43DEFINE_ANIMATED_NUMBER(SVGFESpecularLightingElement, SVGNames::surfaceScaleAttr, SurfaceScale, surfaceScale)
44DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFESpecularLightingElement, SVGNames::kernelUnitLengthAttr, kernelUnitLengthXIdentifier(), KernelUnitLengthX, kernelUnitLengthX)
45DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFESpecularLightingElement, SVGNames::kernelUnitLengthAttr, kernelUnitLengthYIdentifier(), KernelUnitLengthY, kernelUnitLengthY)
46
47BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFESpecularLightingElement)
48    REGISTER_LOCAL_ANIMATED_PROPERTY(in1)
49    REGISTER_LOCAL_ANIMATED_PROPERTY(specularConstant)
50    REGISTER_LOCAL_ANIMATED_PROPERTY(specularExponent)
51    REGISTER_LOCAL_ANIMATED_PROPERTY(surfaceScale)
52    REGISTER_LOCAL_ANIMATED_PROPERTY(kernelUnitLengthX)
53    REGISTER_LOCAL_ANIMATED_PROPERTY(kernelUnitLengthY)
54    REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
55END_REGISTER_ANIMATED_PROPERTIES
56
57inline SVGFESpecularLightingElement::SVGFESpecularLightingElement(const QualifiedName& tagName, Document* document)
58    : SVGFilterPrimitiveStandardAttributes(tagName, document)
59    , m_specularConstant(1)
60    , m_specularExponent(1)
61    , m_surfaceScale(1)
62{
63    ASSERT(hasTagName(SVGNames::feSpecularLightingTag));
64    registerAnimatedPropertiesForSVGFESpecularLightingElement();
65}
66
67PassRefPtr<SVGFESpecularLightingElement> SVGFESpecularLightingElement::create(const QualifiedName& tagName, Document* document)
68{
69    return adoptRef(new SVGFESpecularLightingElement(tagName, document));
70}
71
72const AtomicString& SVGFESpecularLightingElement::kernelUnitLengthXIdentifier()
73{
74    DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGKernelUnitLengthX", AtomicString::ConstructFromLiteral));
75    return s_identifier;
76}
77
78const AtomicString& SVGFESpecularLightingElement::kernelUnitLengthYIdentifier()
79{
80    DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGKernelUnitLengthY", AtomicString::ConstructFromLiteral));
81    return s_identifier;
82}
83
84bool SVGFESpecularLightingElement::isSupportedAttribute(const QualifiedName& attrName)
85{
86    DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
87    if (supportedAttributes.isEmpty()) {
88        supportedAttributes.add(SVGNames::inAttr);
89        supportedAttributes.add(SVGNames::specularConstantAttr);
90        supportedAttributes.add(SVGNames::specularExponentAttr);
91        supportedAttributes.add(SVGNames::surfaceScaleAttr);
92        supportedAttributes.add(SVGNames::kernelUnitLengthAttr);
93    }
94    return supportedAttributes.contains<QualifiedName, SVGAttributeHashTranslator>(attrName);
95}
96
97void SVGFESpecularLightingElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
98{
99    if (!isSupportedAttribute(name)) {
100        SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
101        return;
102    }
103
104    if (name == SVGNames::inAttr) {
105        setIn1BaseValue(value);
106        return;
107    }
108
109    if (name == SVGNames::surfaceScaleAttr) {
110        setSurfaceScaleBaseValue(value.toFloat());
111        return;
112    }
113
114    if (name == SVGNames::specularConstantAttr) {
115        setSpecularConstantBaseValue(value.toFloat());
116        return;
117    }
118
119    if (name == SVGNames::specularExponentAttr) {
120        setSpecularExponentBaseValue(value.toFloat());
121        return;
122    }
123
124    if (name == SVGNames::kernelUnitLengthAttr) {
125        float x, y;
126        if (parseNumberOptionalNumber(value, x, y)) {
127            setKernelUnitLengthXBaseValue(x);
128            setKernelUnitLengthYBaseValue(y);
129        }
130        return;
131    }
132
133    ASSERT_NOT_REACHED();
134}
135
136bool SVGFESpecularLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
137{
138    FESpecularLighting* specularLighting = static_cast<FESpecularLighting*>(effect);
139
140    if (attrName == SVGNames::lighting_colorAttr) {
141        RenderObject* renderer = this->renderer();
142        ASSERT(renderer);
143        ASSERT(renderer->style());
144        return specularLighting->setLightingColor(renderer->style()->svgStyle()->lightingColor());
145    }
146    if (attrName == SVGNames::surfaceScaleAttr)
147        return specularLighting->setSurfaceScale(surfaceScale());
148    if (attrName == SVGNames::specularConstantAttr)
149        return specularLighting->setSpecularConstant(specularConstant());
150    if (attrName == SVGNames::specularExponentAttr)
151        return specularLighting->setSpecularExponent(specularExponent());
152
153    LightSource* lightSource = const_cast<LightSource*>(specularLighting->lightSource());
154    const SVGFELightElement* lightElement = SVGFELightElement::findLightElement(this);
155    ASSERT(lightSource);
156    ASSERT(lightElement);
157
158    if (attrName == SVGNames::azimuthAttr)
159        return lightSource->setAzimuth(lightElement->azimuth());
160    if (attrName == SVGNames::elevationAttr)
161        return lightSource->setElevation(lightElement->elevation());
162    if (attrName == SVGNames::xAttr)
163        return lightSource->setX(lightElement->x());
164    if (attrName == SVGNames::yAttr)
165        return lightSource->setY(lightElement->y());
166    if (attrName == SVGNames::zAttr)
167        return lightSource->setZ(lightElement->z());
168    if (attrName == SVGNames::pointsAtXAttr)
169        return lightSource->setPointsAtX(lightElement->pointsAtX());
170    if (attrName == SVGNames::pointsAtYAttr)
171        return lightSource->setPointsAtY(lightElement->pointsAtY());
172    if (attrName == SVGNames::pointsAtZAttr)
173        return lightSource->setPointsAtZ(lightElement->pointsAtZ());
174    if (attrName == SVGNames::specularExponentAttr)
175        return lightSource->setSpecularExponent(lightElement->specularExponent());
176    if (attrName == SVGNames::limitingConeAngleAttr)
177        return lightSource->setLimitingConeAngle(lightElement->limitingConeAngle());
178
179    ASSERT_NOT_REACHED();
180    return false;
181}
182
183void SVGFESpecularLightingElement::svgAttributeChanged(const QualifiedName& attrName)
184{
185    if (!isSupportedAttribute(attrName)) {
186        SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
187        return;
188    }
189
190    SVGElementInstance::InvalidationGuard invalidationGuard(this);
191
192    if (attrName == SVGNames::surfaceScaleAttr
193        || attrName == SVGNames::specularConstantAttr
194        || attrName == SVGNames::specularExponentAttr
195        || attrName == SVGNames::kernelUnitLengthAttr) {
196        primitiveAttributeChanged(attrName);
197        return;
198    }
199
200    if (attrName == SVGNames::inAttr) {
201        invalidate();
202        return;
203    }
204
205    ASSERT_NOT_REACHED();
206}
207
208void SVGFESpecularLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName)
209{
210    if (SVGFELightElement::findLightElement(this) != lightElement)
211        return;
212
213    // The light element has different attribute names so attrName can identify the requested attribute.
214    primitiveAttributeChanged(attrName);
215}
216
217PassRefPtr<FilterEffect> SVGFESpecularLightingElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
218{
219    FilterEffect* input1 = filterBuilder->getEffectById(in1());
220
221    if (!input1)
222        return 0;
223
224    RefPtr<LightSource> lightSource = SVGFELightElement::findLightSource(this);
225    if (!lightSource)
226        return 0;
227
228    RenderObject* renderer = this->renderer();
229    if (!renderer)
230        return 0;
231
232    ASSERT(renderer->style());
233    Color color = renderer->style()->svgStyle()->lightingColor();
234
235    RefPtr<FilterEffect> effect = FESpecularLighting::create(filter, color, surfaceScale(), specularConstant(),
236                                          specularExponent(), kernelUnitLengthX(), kernelUnitLengthY(), lightSource.release());
237    effect->inputEffects().append(input1);
238    return effect.release();
239}
240
241}
242
243#endif // ENABLE(SVG)
244