OSDN Git Service

Merge WebKit at r78450: Initial merge by git.
[android-x86/external-webkit.git] / Source / WebCore / 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 #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 "SVGFELightElement.h"
32 #include "SVGFilterBuilder.h"
33 #include "SVGNames.h"
34 #include "SVGParserUtilities.h"
35
36 namespace WebCore {
37
38 // Animated property definitions
39 DEFINE_ANIMATED_STRING(SVGFESpecularLightingElement, SVGNames::inAttr, In1, in1)
40 DEFINE_ANIMATED_NUMBER(SVGFESpecularLightingElement, SVGNames::specularConstantAttr, SpecularConstant, specularConstant)
41 DEFINE_ANIMATED_NUMBER(SVGFESpecularLightingElement, SVGNames::specularExponentAttr, SpecularExponent, specularExponent)
42 DEFINE_ANIMATED_NUMBER(SVGFESpecularLightingElement, SVGNames::surfaceScaleAttr, SurfaceScale, surfaceScale)
43 DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFESpecularLightingElement, SVGNames::kernelUnitLengthAttr, kernelUnitLengthXIdentifier(), KernelUnitLengthX, kernelUnitLengthX)
44 DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFESpecularLightingElement, SVGNames::kernelUnitLengthAttr, kernelUnitLengthYIdentifier(), KernelUnitLengthY, kernelUnitLengthY)
45
46 inline SVGFESpecularLightingElement::SVGFESpecularLightingElement(const QualifiedName& tagName, Document* document)
47     : SVGFilterPrimitiveStandardAttributes(tagName, document)
48     , m_specularConstant(1)
49     , m_specularExponent(1)
50     , m_surfaceScale(1)
51 {
52 }
53
54 PassRefPtr<SVGFESpecularLightingElement> SVGFESpecularLightingElement::create(const QualifiedName& tagName, Document* document)
55 {
56     return adoptRef(new SVGFESpecularLightingElement(tagName, document));
57 }
58
59 const AtomicString& SVGFESpecularLightingElement::kernelUnitLengthXIdentifier()
60 {
61     DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGKernelUnitLengthX"));
62     return s_identifier;
63 }
64
65 const AtomicString& SVGFESpecularLightingElement::kernelUnitLengthYIdentifier()
66 {
67     DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGKernelUnitLengthY"));
68     return s_identifier;
69 }
70
71 void SVGFESpecularLightingElement::parseMappedAttribute(Attribute* attr)
72 {    
73     const String& value = attr->value();
74     if (attr->name() == SVGNames::inAttr)
75         setIn1BaseValue(value);
76     else if (attr->name() == SVGNames::surfaceScaleAttr)
77         setSurfaceScaleBaseValue(value.toFloat());
78     else if (attr->name() == SVGNames::specularConstantAttr)
79         setSpecularConstantBaseValue(value.toFloat());
80     else if (attr->name() == SVGNames::specularExponentAttr)
81         setSpecularExponentBaseValue(value.toFloat());
82     else if (attr->name() == SVGNames::kernelUnitLengthAttr) {
83         float x, y;
84         if (parseNumberOptionalNumber(value, x, y)) {
85             setKernelUnitLengthXBaseValue(x);
86             setKernelUnitLengthYBaseValue(y);
87         }
88     } else
89         SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
90 }
91
92 void SVGFESpecularLightingElement::svgAttributeChanged(const QualifiedName& attrName)
93 {
94     SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
95
96     if (attrName == SVGNames::inAttr
97         || attrName == SVGNames::surfaceScaleAttr
98         || attrName == SVGNames::specularConstantAttr
99         || attrName == SVGNames::specularExponentAttr
100         || attrName == SVGNames::kernelUnitLengthAttr)
101         invalidate();
102 }
103
104 void SVGFESpecularLightingElement::synchronizeProperty(const QualifiedName& attrName)
105 {
106     SVGFilterPrimitiveStandardAttributes::synchronizeProperty(attrName);
107
108     if (attrName == anyQName()) {
109         synchronizeIn1();
110         synchronizeSurfaceScale();
111         synchronizeSpecularConstant();
112         synchronizeSpecularExponent();
113         synchronizeKernelUnitLengthX();
114         synchronizeKernelUnitLengthY();
115         return;
116     }
117
118     if (attrName == SVGNames::inAttr)
119         synchronizeIn1();
120     else if (attrName == SVGNames::surfaceScaleAttr)
121         synchronizeSurfaceScale();
122     else if (attrName == SVGNames::specularConstantAttr)
123         synchronizeSpecularConstant();
124     else if (attrName == SVGNames::specularExponentAttr)
125         synchronizeSpecularExponent();
126     else if (attrName == SVGNames::kernelUnitLengthAttr) {
127         synchronizeKernelUnitLengthX();
128         synchronizeKernelUnitLengthY();
129     }
130 }
131
132 AttributeToPropertyTypeMap& SVGFESpecularLightingElement::attributeToPropertyTypeMap()
133 {
134     DEFINE_STATIC_LOCAL(AttributeToPropertyTypeMap, s_attributeToPropertyTypeMap, ());
135     return s_attributeToPropertyTypeMap;
136 }
137
138 void SVGFESpecularLightingElement::fillAttributeToPropertyTypeMap()
139 {
140     AttributeToPropertyTypeMap& attributeToPropertyTypeMap = this->attributeToPropertyTypeMap();
141
142     SVGFilterPrimitiveStandardAttributes::fillPassedAttributeToPropertyTypeMap(attributeToPropertyTypeMap);
143     attributeToPropertyTypeMap.set(SVGNames::inAttr, AnimatedString);
144     attributeToPropertyTypeMap.set(SVGNames::specularConstantAttr, AnimatedNumber);
145     attributeToPropertyTypeMap.set(SVGNames::specularExponentAttr, AnimatedNumber);
146     attributeToPropertyTypeMap.set(SVGNames::surfaceScaleAttr, AnimatedNumber);
147     attributeToPropertyTypeMap.set(SVGNames::kernelUnitLengthAttr, AnimatedNumberOptionalNumber);
148 }
149
150 PassRefPtr<LightSource> SVGFESpecularLightingElement::findLights() const
151 {
152     for (Node* node = firstChild(); node; node = node->nextSibling()) {
153         if (node->hasTagName(SVGNames::feDistantLightTag)
154             || node->hasTagName(SVGNames::fePointLightTag)
155             || node->hasTagName(SVGNames::feSpotLightTag)) {
156             SVGFELightElement* lightNode = static_cast<SVGFELightElement*>(node); 
157             return lightNode->lightSource();
158         }
159     }
160
161     return 0;
162 }
163
164 PassRefPtr<FilterEffect> SVGFESpecularLightingElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
165 {
166     FilterEffect* input1 = filterBuilder->getEffectById(in1());
167     
168     if (!input1)
169         return 0;
170     
171     RefPtr<RenderStyle> filterStyle = styleForRenderer();    
172     
173     Color color = filterStyle->svgStyle()->lightingColor();
174
175     RefPtr<FilterEffect> effect = FESpecularLighting::create(filter, color, surfaceScale(), specularConstant(), 
176                                           specularExponent(), kernelUnitLengthX(), kernelUnitLengthY(), findLights());
177     effect->inputEffects().append(input1);
178     return effect.release();
179 }
180
181 }
182
183 #endif // ENABLE(SVG)