Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGFEDiffuseLightingElement.cpp
1 /*
2  * Copyright (C) 2005 Oliver Hunt <ojh16@student.canterbury.ac.nz>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21
22 #include "core/svg/SVGFEDiffuseLightingElement.h"
23
24 #include "core/rendering/style/RenderStyle.h"
25 #include "core/svg/SVGElementInstance.h"
26 #include "core/svg/SVGParserUtilities.h"
27 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
28 #include "platform/graphics/filters/FEDiffuseLighting.h"
29 #include "platform/graphics/filters/FilterEffect.h"
30
31 namespace WebCore {
32
33 inline SVGFEDiffuseLightingElement::SVGFEDiffuseLightingElement(Document& document)
34     : SVGFilterPrimitiveStandardAttributes(SVGNames::feDiffuseLightingTag, document)
35     , m_diffuseConstant(SVGAnimatedNumber::create(this, SVGNames::diffuseConstantAttr, SVGNumber::create(1)))
36     , m_surfaceScale(SVGAnimatedNumber::create(this, SVGNames::surfaceScaleAttr, SVGNumber::create(1)))
37     , m_kernelUnitLength(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::kernelUnitLengthAttr))
38     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
39 {
40     ScriptWrappable::init(this);
41
42     addToPropertyMap(m_diffuseConstant);
43     addToPropertyMap(m_surfaceScale);
44     addToPropertyMap(m_kernelUnitLength);
45     addToPropertyMap(m_in1);
46 }
47
48 PassRefPtr<SVGFEDiffuseLightingElement> SVGFEDiffuseLightingElement::create(Document& document)
49 {
50     return adoptRef(new SVGFEDiffuseLightingElement(document));
51 }
52
53 bool SVGFEDiffuseLightingElement::isSupportedAttribute(const QualifiedName& attrName)
54 {
55     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
56     if (supportedAttributes.isEmpty()) {
57         supportedAttributes.add(SVGNames::inAttr);
58         supportedAttributes.add(SVGNames::diffuseConstantAttr);
59         supportedAttributes.add(SVGNames::surfaceScaleAttr);
60         supportedAttributes.add(SVGNames::kernelUnitLengthAttr);
61         supportedAttributes.add(SVGNames::lighting_colorAttr); // Even though it's a SVG-CSS property, we override its handling here.
62     }
63     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
64 }
65
66 void SVGFEDiffuseLightingElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
67 {
68     if (!isSupportedAttribute(name) || name == SVGNames::lighting_colorAttr) {
69         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
70         return;
71     }
72
73     SVGParsingError parseError = NoError;
74
75     if (name == SVGNames::inAttr)
76         m_in1->setBaseValueAsString(value, parseError);
77     else if (name == SVGNames::diffuseConstantAttr)
78         m_diffuseConstant->setBaseValueAsString(value, parseError);
79     else if (name == SVGNames::surfaceScaleAttr)
80         m_surfaceScale->setBaseValueAsString(value, parseError);
81     else if (name == SVGNames::kernelUnitLengthAttr)
82         m_kernelUnitLength->setBaseValueAsString(value, parseError);
83     else
84         ASSERT_NOT_REACHED();
85
86     reportAttributeParsingError(parseError, name, value);
87 }
88
89 bool SVGFEDiffuseLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
90 {
91     FEDiffuseLighting* diffuseLighting = static_cast<FEDiffuseLighting*>(effect);
92
93     if (attrName == SVGNames::lighting_colorAttr) {
94         RenderObject* renderer = this->renderer();
95         ASSERT(renderer);
96         ASSERT(renderer->style());
97         return diffuseLighting->setLightingColor(renderer->style()->svgStyle()->lightingColor());
98     }
99     if (attrName == SVGNames::surfaceScaleAttr)
100         return diffuseLighting->setSurfaceScale(m_surfaceScale->currentValue()->value());
101     if (attrName == SVGNames::diffuseConstantAttr)
102         return diffuseLighting->setDiffuseConstant(m_diffuseConstant->currentValue()->value());
103
104     LightSource* lightSource = const_cast<LightSource*>(diffuseLighting->lightSource());
105     const SVGFELightElement* lightElement = SVGFELightElement::findLightElement(*this);
106     ASSERT(lightSource);
107     ASSERT(lightElement);
108
109     if (attrName == SVGNames::azimuthAttr)
110         return lightSource->setAzimuth(lightElement->azimuth()->currentValue()->value());
111     if (attrName == SVGNames::elevationAttr)
112         return lightSource->setElevation(lightElement->elevation()->currentValue()->value());
113     if (attrName == SVGNames::xAttr)
114         return lightSource->setX(lightElement->x()->currentValue()->value());
115     if (attrName == SVGNames::yAttr)
116         return lightSource->setY(lightElement->y()->currentValue()->value());
117     if (attrName == SVGNames::zAttr)
118         return lightSource->setZ(lightElement->z()->currentValue()->value());
119     if (attrName == SVGNames::pointsAtXAttr)
120         return lightSource->setPointsAtX(lightElement->pointsAtX()->currentValue()->value());
121     if (attrName == SVGNames::pointsAtYAttr)
122         return lightSource->setPointsAtY(lightElement->pointsAtY()->currentValue()->value());
123     if (attrName == SVGNames::pointsAtZAttr)
124         return lightSource->setPointsAtZ(lightElement->pointsAtZ()->currentValue()->value());
125     if (attrName == SVGNames::specularExponentAttr)
126         return lightSource->setSpecularExponent(lightElement->specularExponent()->currentValue()->value());
127     if (attrName == SVGNames::limitingConeAngleAttr)
128         return lightSource->setLimitingConeAngle(lightElement->limitingConeAngle()->currentValue()->value());
129
130     ASSERT_NOT_REACHED();
131     return false;
132 }
133
134 void SVGFEDiffuseLightingElement::svgAttributeChanged(const QualifiedName& attrName)
135 {
136     if (!isSupportedAttribute(attrName)) {
137         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
138         return;
139     }
140
141     SVGElement::InvalidationGuard invalidationGuard(this);
142
143     if (attrName == SVGNames::surfaceScaleAttr
144         || attrName == SVGNames::diffuseConstantAttr
145         || attrName == SVGNames::kernelUnitLengthAttr
146         || attrName == SVGNames::lighting_colorAttr) {
147         primitiveAttributeChanged(attrName);
148         return;
149     }
150
151     if (attrName == SVGNames::inAttr) {
152         invalidate();
153         return;
154     }
155
156     ASSERT_NOT_REACHED();
157 }
158
159 void SVGFEDiffuseLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName)
160 {
161     if (SVGFELightElement::findLightElement(*this) != lightElement)
162         return;
163
164     // The light element has different attribute names.
165     primitiveAttributeChanged(attrName);
166 }
167
168 PassRefPtr<FilterEffect> SVGFEDiffuseLightingElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
169 {
170     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
171
172     if (!input1)
173         return nullptr;
174
175     RefPtr<LightSource> lightSource = SVGFELightElement::findLightSource(*this);
176     if (!lightSource)
177         return nullptr;
178
179     RenderObject* renderer = this->renderer();
180     if (!renderer)
181         return nullptr;
182
183     ASSERT(renderer->style());
184     Color color = renderer->style()->svgStyle()->lightingColor();
185
186     RefPtr<FilterEffect> effect = FEDiffuseLighting::create(filter, color, m_surfaceScale->currentValue()->value(), m_diffuseConstant->currentValue()->value(),
187         kernelUnitLengthX()->currentValue()->value(), kernelUnitLengthY()->currentValue()->value(), lightSource.release());
188     effect->inputEffects().append(input1);
189     return effect.release();
190 }
191
192 }