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