1/*
2    Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3                  2004, 2005, 2006 Rob Buis <buis@kde.org>
4                  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 "MappedAttribute.h"
28#include "RenderObject.h"
29#include "SVGColor.h"
30#include "SVGFELightElement.h"
31#include "SVGNames.h"
32#include "SVGParserUtilities.h"
33#include "SVGResourceFilter.h"
34
35namespace WebCore {
36
37SVGFESpecularLightingElement::SVGFESpecularLightingElement(const QualifiedName& tagName, Document* doc)
38    : SVGFilterPrimitiveStandardAttributes(tagName, doc)
39    , m_specularConstant(1.0f)
40    , m_specularExponent(1.0f)
41    , m_surfaceScale(1.0f)
42{
43}
44
45SVGFESpecularLightingElement::~SVGFESpecularLightingElement()
46{
47}
48
49void SVGFESpecularLightingElement::parseMappedAttribute(MappedAttribute* attr)
50{
51    const String& value = attr->value();
52    if (attr->name() == SVGNames::inAttr)
53        setIn1BaseValue(value);
54    else if (attr->name() == SVGNames::surfaceScaleAttr)
55        setSurfaceScaleBaseValue(value.toFloat());
56    else if (attr->name() == SVGNames::specularConstantAttr)
57        setSpecularConstantBaseValue(value.toFloat());
58    else if (attr->name() == SVGNames::specularExponentAttr)
59        setSpecularExponentBaseValue(value.toFloat());
60    else if (attr->name() == SVGNames::kernelUnitLengthAttr) {
61        float x, y;
62        if (parseNumberOptionalNumber(value, x, y)) {
63            setKernelUnitLengthXBaseValue(x);
64            setKernelUnitLengthYBaseValue(y);
65        }
66    } else
67        SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
68}
69
70void SVGFESpecularLightingElement::synchronizeProperty(const QualifiedName& attrName)
71{
72    SVGFilterPrimitiveStandardAttributes::synchronizeProperty(attrName);
73
74    if (attrName == anyQName()) {
75        synchronizeIn1();
76        synchronizeSurfaceScale();
77        synchronizeSpecularConstant();
78        synchronizeSpecularExponent();
79        synchronizeKernelUnitLengthX();
80        synchronizeKernelUnitLengthY();
81        return;
82    }
83
84    if (attrName == SVGNames::inAttr)
85        synchronizeIn1();
86    else if (attrName == SVGNames::surfaceScaleAttr)
87        synchronizeSurfaceScale();
88    else if (attrName == SVGNames::specularConstantAttr)
89        synchronizeSpecularConstant();
90    else if (attrName == SVGNames::specularExponentAttr)
91        synchronizeSpecularExponent();
92    else if (attrName == SVGNames::kernelUnitLengthAttr) {
93        synchronizeKernelUnitLengthX();
94        synchronizeKernelUnitLengthY();
95    }
96}
97
98PassRefPtr<LightSource> SVGFESpecularLightingElement::findLights() const
99{
100    for (Node* n = firstChild(); n; n = n->nextSibling()) {
101        if (n->hasTagName(SVGNames::feDistantLightTag) ||
102            n->hasTagName(SVGNames::fePointLightTag) ||
103            n->hasTagName(SVGNames::feSpotLightTag)) {
104            SVGFELightElement* lightNode = static_cast<SVGFELightElement*>(n);
105            return lightNode->lightSource();
106        }
107    }
108
109    return 0;
110}
111
112bool SVGFESpecularLightingElement::build(SVGResourceFilter* filterResource)
113{
114    FilterEffect* input1 = filterResource->builder()->getEffectById(in1());
115
116    if (!input1)
117        return false;
118
119    RefPtr<RenderStyle> filterStyle = styleForRenderer();
120
121    Color color = filterStyle->svgStyle()->lightingColor();
122
123    RefPtr<FilterEffect> effect = FESpecularLighting::create(input1, color, surfaceScale(), specularConstant(),
124                                        specularExponent(), kernelUnitLengthX(), kernelUnitLengthY(), findLights());
125    filterResource->addFilterEffect(this, effect.release());
126
127    return true;
128}
129
130}
131
132#endif // ENABLE(SVG)
133