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 "SVGFELightElement.h"
26
27 #include "Attribute.h"
28 #include "RenderObject.h"
29 #include "RenderSVGResource.h"
30 #include "SVGFEDiffuseLightingElement.h"
31 #include "SVGFESpecularLightingElement.h"
32 #include "SVGFilterElement.h"
33 #include "SVGFilterPrimitiveStandardAttributes.h"
34 #include "SVGNames.h"
35
36 namespace WebCore {
37
38 // Animated property definitions
DEFINE_ANIMATED_NUMBER(SVGFELightElement,SVGNames::azimuthAttr,Azimuth,azimuth)39 DEFINE_ANIMATED_NUMBER(SVGFELightElement, SVGNames::azimuthAttr, Azimuth, azimuth)
40 DEFINE_ANIMATED_NUMBER(SVGFELightElement, SVGNames::elevationAttr, Elevation, elevation)
41 DEFINE_ANIMATED_NUMBER(SVGFELightElement, SVGNames::xAttr, X, x)
42 DEFINE_ANIMATED_NUMBER(SVGFELightElement, SVGNames::yAttr, Y, y)
43 DEFINE_ANIMATED_NUMBER(SVGFELightElement, SVGNames::zAttr, Z, z)
44 DEFINE_ANIMATED_NUMBER(SVGFELightElement, SVGNames::pointsAtXAttr, PointsAtX, pointsAtX)
45 DEFINE_ANIMATED_NUMBER(SVGFELightElement, SVGNames::pointsAtYAttr, PointsAtY, pointsAtY)
46 DEFINE_ANIMATED_NUMBER(SVGFELightElement, SVGNames::pointsAtZAttr, PointsAtZ, pointsAtZ)
47 DEFINE_ANIMATED_NUMBER(SVGFELightElement, SVGNames::specularExponentAttr, SpecularExponent, specularExponent)
48 DEFINE_ANIMATED_NUMBER(SVGFELightElement, SVGNames::limitingConeAngleAttr, LimitingConeAngle, limitingConeAngle)
49
50 SVGFELightElement::SVGFELightElement(const QualifiedName& tagName, Document* document)
51 : SVGElement(tagName, document)
52 , m_specularExponent(1)
53 {
54 }
55
findLightElement(const SVGElement * svgElement)56 SVGFELightElement* SVGFELightElement::findLightElement(const SVGElement* svgElement)
57 {
58 for (Node* node = svgElement->firstChild(); node; node = node->nextSibling()) {
59 if (node->hasTagName(SVGNames::feDistantLightTag)
60 || node->hasTagName(SVGNames::fePointLightTag)
61 || node->hasTagName(SVGNames::feSpotLightTag)) {
62 return static_cast<SVGFELightElement*>(node);
63 }
64 }
65 return 0;
66 }
67
findLightSource(const SVGElement * svgElement)68 PassRefPtr<LightSource> SVGFELightElement::findLightSource(const SVGElement* svgElement)
69 {
70 SVGFELightElement* lightNode = findLightElement(svgElement);
71 if (!lightNode)
72 return 0;
73 return lightNode->lightSource();
74 }
75
parseMappedAttribute(Attribute * attr)76 void SVGFELightElement::parseMappedAttribute(Attribute* attr)
77 {
78 const String& value = attr->value();
79 if (attr->name() == SVGNames::azimuthAttr)
80 setAzimuthBaseValue(value.toFloat());
81 else if (attr->name() == SVGNames::elevationAttr)
82 setElevationBaseValue(value.toFloat());
83 else if (attr->name() == SVGNames::xAttr)
84 setXBaseValue(value.toFloat());
85 else if (attr->name() == SVGNames::yAttr)
86 setYBaseValue(value.toFloat());
87 else if (attr->name() == SVGNames::zAttr)
88 setZBaseValue(value.toFloat());
89 else if (attr->name() == SVGNames::pointsAtXAttr)
90 setPointsAtXBaseValue(value.toFloat());
91 else if (attr->name() == SVGNames::pointsAtYAttr)
92 setPointsAtYBaseValue(value.toFloat());
93 else if (attr->name() == SVGNames::pointsAtZAttr)
94 setPointsAtZBaseValue(value.toFloat());
95 else if (attr->name() == SVGNames::specularExponentAttr)
96 setSpecularExponentBaseValue(value.toFloat());
97 else if (attr->name() == SVGNames::limitingConeAngleAttr)
98 setLimitingConeAngleBaseValue(value.toFloat());
99 else
100 SVGElement::parseMappedAttribute(attr);
101 }
102
svgAttributeChanged(const QualifiedName & attrName)103 void SVGFELightElement::svgAttributeChanged(const QualifiedName& attrName)
104 {
105 SVGElement::svgAttributeChanged(attrName);
106
107 if (attrName == SVGNames::azimuthAttr
108 || attrName == SVGNames::elevationAttr
109 || attrName == SVGNames::xAttr
110 || attrName == SVGNames::yAttr
111 || attrName == SVGNames::zAttr
112 || attrName == SVGNames::pointsAtXAttr
113 || attrName == SVGNames::pointsAtYAttr
114 || attrName == SVGNames::pointsAtZAttr
115 || attrName == SVGNames::specularExponentAttr
116 || attrName == SVGNames::limitingConeAngleAttr) {
117 ContainerNode* parent = parentNode();
118 if (!parent)
119 return;
120
121 RenderObject* renderer = parent->renderer();
122 if (!renderer || !renderer->isSVGResourceFilterPrimitive())
123 return;
124
125 if (parent->hasTagName(SVGNames::feDiffuseLightingTag)) {
126 SVGFEDiffuseLightingElement* diffuseLighting = static_cast<SVGFEDiffuseLightingElement*>(parent);
127 diffuseLighting->lightElementAttributeChanged(this, attrName);
128 return;
129 } else if (parent->hasTagName(SVGNames::feSpecularLightingTag)) {
130 SVGFESpecularLightingElement* specularLighting = static_cast<SVGFESpecularLightingElement*>(parent);
131 specularLighting->lightElementAttributeChanged(this, attrName);
132 return;
133 }
134 }
135 }
136
synchronizeProperty(const QualifiedName & attrName)137 void SVGFELightElement::synchronizeProperty(const QualifiedName& attrName)
138 {
139 SVGElement::synchronizeProperty(attrName);
140
141 if (attrName == anyQName()) {
142 synchronizeAzimuth();
143 synchronizeElevation();
144 synchronizeX();
145 synchronizeY();
146 synchronizeZ();
147 synchronizePointsAtX();
148 synchronizePointsAtY();
149 synchronizePointsAtZ();
150 synchronizeSpecularExponent();
151 synchronizeLimitingConeAngle();
152 return;
153 }
154
155 if (attrName == SVGNames::azimuthAttr)
156 synchronizeAzimuth();
157 else if (attrName == SVGNames::elevationAttr)
158 synchronizeElevation();
159 else if (attrName == SVGNames::xAttr)
160 synchronizeX();
161 else if (attrName == SVGNames::yAttr)
162 synchronizeY();
163 else if (attrName == SVGNames::zAttr)
164 synchronizeZ();
165 else if (attrName == SVGNames::pointsAtXAttr)
166 synchronizePointsAtX();
167 else if (attrName == SVGNames::pointsAtYAttr)
168 synchronizePointsAtY();
169 else if (attrName == SVGNames::pointsAtZAttr)
170 synchronizePointsAtZ();
171 else if (attrName == SVGNames::specularExponentAttr)
172 synchronizeSpecularExponent();
173 else if (attrName == SVGNames::limitingConeAngleAttr)
174 synchronizeLimitingConeAngle();
175 }
176
attributeToPropertyTypeMap()177 AttributeToPropertyTypeMap& SVGFELightElement::attributeToPropertyTypeMap()
178 {
179 DEFINE_STATIC_LOCAL(AttributeToPropertyTypeMap, s_attributeToPropertyTypeMap, ());
180 return s_attributeToPropertyTypeMap;
181 }
182
fillAttributeToPropertyTypeMap()183 void SVGFELightElement::fillAttributeToPropertyTypeMap()
184 {
185 AttributeToPropertyTypeMap& attributeToPropertyTypeMap = this->attributeToPropertyTypeMap();
186 attributeToPropertyTypeMap.set(SVGNames::azimuthAttr, AnimatedNumber);
187 attributeToPropertyTypeMap.set(SVGNames::elevationAttr, AnimatedNumber);
188 attributeToPropertyTypeMap.set(SVGNames::xAttr, AnimatedNumber);
189 attributeToPropertyTypeMap.set(SVGNames::yAttr, AnimatedNumber);
190 attributeToPropertyTypeMap.set(SVGNames::zAttr, AnimatedNumber);
191 attributeToPropertyTypeMap.set(SVGNames::pointsAtXAttr, AnimatedNumber);
192 attributeToPropertyTypeMap.set(SVGNames::pointsAtYAttr, AnimatedNumber);
193 attributeToPropertyTypeMap.set(SVGNames::pointsAtZAttr, AnimatedNumber);
194 attributeToPropertyTypeMap.set(SVGNames::specularExponentAttr, AnimatedNumber);
195 attributeToPropertyTypeMap.set(SVGNames::limitingConeAngleAttr, AnimatedNumber);
196 }
197
childrenChanged(bool changedByParser,Node * beforeChange,Node * afterChange,int childCountDelta)198 void SVGFELightElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
199 {
200 SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
201
202 if (!changedByParser) {
203 if (ContainerNode* parent = parentNode()) {
204 RenderObject* renderer = parent->renderer();
205 if (renderer && renderer->isSVGResourceFilterPrimitive())
206 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
207 }
208 }
209 }
210
211 }
212
213 #endif // ENABLE(SVG)
214