Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGFESpecularLightingElement.cpp
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 #include "core/svg/SVGFESpecularLightingElement.h"
25
26 #include "platform/graphics/filters/FilterEffect.h"
27 #include "core/rendering/style/RenderStyle.h"
28 #include "core/svg/SVGElementInstance.h"
29 #include "core/svg/SVGParserUtilities.h"
30 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
31
32 namespace WebCore {
33
34 inline SVGFESpecularLightingElement::SVGFESpecularLightingElement(Document& document)
35     : SVGFilterPrimitiveStandardAttributes(SVGNames::feSpecularLightingTag, document)
36     , m_specularConstant(SVGAnimatedNumber::create(this, SVGNames::specularConstantAttr, SVGNumber::create(1)))
37     , m_specularExponent(SVGAnimatedNumber::create(this, SVGNames::specularExponentAttr, SVGNumber::create(1)))
38     , m_surfaceScale(SVGAnimatedNumber::create(this, SVGNames::surfaceScaleAttr, SVGNumber::create(1)))
39     , m_kernelUnitLength(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::surfaceScaleAttr))
40     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
41 {
42     ScriptWrappable::init(this);
43
44     addToPropertyMap(m_specularConstant);
45     addToPropertyMap(m_specularExponent);
46     addToPropertyMap(m_surfaceScale);
47     addToPropertyMap(m_kernelUnitLength);
48     addToPropertyMap(m_in1);
49 }
50
51 PassRefPtr<SVGFESpecularLightingElement> SVGFESpecularLightingElement::create(Document& document)
52 {
53     return adoptRef(new SVGFESpecularLightingElement(document));
54 }
55
56 bool SVGFESpecularLightingElement::isSupportedAttribute(const QualifiedName& attrName)
57 {
58     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
59     if (supportedAttributes.isEmpty()) {
60         supportedAttributes.add(SVGNames::inAttr);
61         supportedAttributes.add(SVGNames::specularConstantAttr);
62         supportedAttributes.add(SVGNames::specularExponentAttr);
63         supportedAttributes.add(SVGNames::surfaceScaleAttr);
64         supportedAttributes.add(SVGNames::kernelUnitLengthAttr);
65     }
66     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
67 }
68
69 void SVGFESpecularLightingElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
70 {
71     if (!isSupportedAttribute(name)) {
72         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
73         return;
74     }
75
76     SVGParsingError parseError = NoError;
77
78     if (name == SVGNames::inAttr)
79         m_in1->setBaseValueAsString(value, parseError);
80     else if (name == SVGNames::surfaceScaleAttr)
81         m_surfaceScale->setBaseValueAsString(value, parseError);
82     else if (name == SVGNames::specularConstantAttr)
83         m_specularConstant->setBaseValueAsString(value, parseError);
84     else if (name == SVGNames::specularExponentAttr)
85         m_specularExponent->setBaseValueAsString(value, parseError);
86     else if (name == SVGNames::kernelUnitLengthAttr)
87         m_kernelUnitLength->setBaseValueAsString(value, parseError);
88     else
89         ASSERT_NOT_REACHED();
90
91     reportAttributeParsingError(parseError, name, value);
92 }
93
94 bool SVGFESpecularLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
95 {
96     FESpecularLighting* specularLighting = static_cast<FESpecularLighting*>(effect);
97
98     if (attrName == SVGNames::lighting_colorAttr) {
99         RenderObject* renderer = this->renderer();
100         ASSERT(renderer);
101         ASSERT(renderer->style());
102         return specularLighting->setLightingColor(renderer->style()->svgStyle()->lightingColor());
103     }
104     if (attrName == SVGNames::surfaceScaleAttr)
105         return specularLighting->setSurfaceScale(m_surfaceScale->currentValue()->value());
106     if (attrName == SVGNames::specularConstantAttr)
107         return specularLighting->setSpecularConstant(m_specularConstant->currentValue()->value());
108     if (attrName == SVGNames::specularExponentAttr)
109         return specularLighting->setSpecularExponent(m_specularExponent->currentValue()->value());
110
111     LightSource* lightSource = const_cast<LightSource*>(specularLighting->lightSource());
112     SVGFELightElement* lightElement = SVGFELightElement::findLightElement(*this);
113     ASSERT(lightSource);
114     ASSERT(lightElement);
115
116     if (attrName == SVGNames::azimuthAttr)
117         return lightSource->setAzimuth(lightElement->azimuth()->currentValue()->value());
118     if (attrName == SVGNames::elevationAttr)
119         return lightSource->setElevation(lightElement->elevation()->currentValue()->value());
120     if (attrName == SVGNames::xAttr)
121         return lightSource->setX(lightElement->x()->currentValue()->value());
122     if (attrName == SVGNames::yAttr)
123         return lightSource->setY(lightElement->y()->currentValue()->value());
124     if (attrName == SVGNames::zAttr)
125         return lightSource->setZ(lightElement->z()->currentValue()->value());
126     if (attrName == SVGNames::pointsAtXAttr)
127         return lightSource->setPointsAtX(lightElement->pointsAtX()->currentValue()->value());
128     if (attrName == SVGNames::pointsAtYAttr)
129         return lightSource->setPointsAtY(lightElement->pointsAtY()->currentValue()->value());
130     if (attrName == SVGNames::pointsAtZAttr)
131         return lightSource->setPointsAtZ(lightElement->pointsAtZ()->currentValue()->value());
132     if (attrName == SVGNames::specularExponentAttr)
133         return lightSource->setSpecularExponent(lightElement->specularExponent()->currentValue()->value());
134     if (attrName == SVGNames::limitingConeAngleAttr)
135         return lightSource->setLimitingConeAngle(lightElement->limitingConeAngle()->currentValue()->value());
136
137     ASSERT_NOT_REACHED();
138     return false;
139 }
140
141 void SVGFESpecularLightingElement::svgAttributeChanged(const QualifiedName& attrName)
142 {
143     if (!isSupportedAttribute(attrName)) {
144         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
145         return;
146     }
147
148     SVGElement::InvalidationGuard invalidationGuard(this);
149
150     if (attrName == SVGNames::surfaceScaleAttr
151         || attrName == SVGNames::specularConstantAttr
152         || attrName == SVGNames::specularExponentAttr
153         || attrName == SVGNames::kernelUnitLengthAttr) {
154         primitiveAttributeChanged(attrName);
155         return;
156     }
157
158     if (attrName == SVGNames::inAttr) {
159         invalidate();
160         return;
161     }
162
163     ASSERT_NOT_REACHED();
164 }
165
166 void SVGFESpecularLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName)
167 {
168     if (SVGFELightElement::findLightElement(*this) != lightElement)
169         return;
170
171     // The light element has different attribute names so attrName can identify the requested attribute.
172     primitiveAttributeChanged(attrName);
173 }
174
175 PassRefPtr<FilterEffect> SVGFESpecularLightingElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
176 {
177     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
178
179     if (!input1)
180         return nullptr;
181
182     RefPtr<LightSource> lightSource = SVGFELightElement::findLightSource(*this);
183     if (!lightSource)
184         return nullptr;
185
186     RenderObject* renderer = this->renderer();
187     if (!renderer)
188         return nullptr;
189
190     ASSERT(renderer->style());
191     Color color = renderer->style()->svgStyle()->lightingColor();
192
193     RefPtr<FilterEffect> effect = FESpecularLighting::create(filter, color, m_surfaceScale->currentValue()->value(), m_specularConstant->currentValue()->value(),
194         m_specularExponent->currentValue()->value(), kernelUnitLengthX()->currentValue()->value(), kernelUnitLengthY()->currentValue()->value(), lightSource.release());
195     effect->inputEffects().append(input1);
196     return effect.release();
197 }
198
199 }