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#if ENABLE(SVG) && ENABLE(FILTERS) 23#include "SVGFEDiffuseLightingElement.h" 24 25#include "Attr.h" 26#include "FEDiffuseLighting.h" 27#include "FilterEffect.h" 28#include "RenderStyle.h" 29#include "SVGColor.h" 30#include "SVGElementInstance.h" 31#include "SVGFELightElement.h" 32#include "SVGFilterBuilder.h" 33#include "SVGNames.h" 34#include "SVGParserUtilities.h" 35 36namespace WebCore { 37 38// Animated property definitions 39DEFINE_ANIMATED_STRING(SVGFEDiffuseLightingElement, SVGNames::inAttr, In1, in1) 40DEFINE_ANIMATED_NUMBER(SVGFEDiffuseLightingElement, SVGNames::diffuseConstantAttr, DiffuseConstant, diffuseConstant) 41DEFINE_ANIMATED_NUMBER(SVGFEDiffuseLightingElement, SVGNames::surfaceScaleAttr, SurfaceScale, surfaceScale) 42DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFEDiffuseLightingElement, SVGNames::kernelUnitLengthAttr, kernelUnitLengthXIdentifier(), KernelUnitLengthX, kernelUnitLengthX) 43DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFEDiffuseLightingElement, SVGNames::kernelUnitLengthAttr, kernelUnitLengthYIdentifier(), KernelUnitLengthY, kernelUnitLengthY) 44 45BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEDiffuseLightingElement) 46 REGISTER_LOCAL_ANIMATED_PROPERTY(in1) 47 REGISTER_LOCAL_ANIMATED_PROPERTY(diffuseConstant) 48 REGISTER_LOCAL_ANIMATED_PROPERTY(surfaceScale) 49 REGISTER_LOCAL_ANIMATED_PROPERTY(kernelUnitLengthX) 50 REGISTER_LOCAL_ANIMATED_PROPERTY(kernelUnitLengthY) 51 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes) 52END_REGISTER_ANIMATED_PROPERTIES 53 54inline SVGFEDiffuseLightingElement::SVGFEDiffuseLightingElement(const QualifiedName& tagName, Document* document) 55 : SVGFilterPrimitiveStandardAttributes(tagName, document) 56 , m_diffuseConstant(1) 57 , m_surfaceScale(1) 58{ 59 ASSERT(hasTagName(SVGNames::feDiffuseLightingTag)); 60 registerAnimatedPropertiesForSVGFEDiffuseLightingElement(); 61} 62 63PassRefPtr<SVGFEDiffuseLightingElement> SVGFEDiffuseLightingElement::create(const QualifiedName& tagName, Document* document) 64{ 65 return adoptRef(new SVGFEDiffuseLightingElement(tagName, document)); 66} 67 68const AtomicString& SVGFEDiffuseLightingElement::kernelUnitLengthXIdentifier() 69{ 70 DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGKernelUnitLengthX", AtomicString::ConstructFromLiteral)); 71 return s_identifier; 72} 73 74const AtomicString& SVGFEDiffuseLightingElement::kernelUnitLengthYIdentifier() 75{ 76 DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGKernelUnitLengthY", AtomicString::ConstructFromLiteral)); 77 return s_identifier; 78} 79 80bool SVGFEDiffuseLightingElement::isSupportedAttribute(const QualifiedName& attrName) 81{ 82 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ()); 83 if (supportedAttributes.isEmpty()) { 84 supportedAttributes.add(SVGNames::inAttr); 85 supportedAttributes.add(SVGNames::diffuseConstantAttr); 86 supportedAttributes.add(SVGNames::surfaceScaleAttr); 87 supportedAttributes.add(SVGNames::kernelUnitLengthAttr); 88 supportedAttributes.add(SVGNames::lighting_colorAttr); // Even though it's a SVG-CSS property, we override its handling here. 89 } 90 return supportedAttributes.contains<QualifiedName, SVGAttributeHashTranslator>(attrName); 91} 92 93void SVGFEDiffuseLightingElement::parseAttribute(const QualifiedName& name, const AtomicString& value) 94{ 95 if (!isSupportedAttribute(name) || name == SVGNames::lighting_colorAttr) { 96 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value); 97 return; 98 } 99 100 if (name == SVGNames::inAttr) { 101 setIn1BaseValue(value); 102 return; 103 } 104 105 if (name == SVGNames::surfaceScaleAttr) { 106 setSurfaceScaleBaseValue(value.toFloat()); 107 return; 108 } 109 110 if (name == SVGNames::diffuseConstantAttr) { 111 setDiffuseConstantBaseValue(value.toFloat()); 112 return; 113 } 114 115 if (name == SVGNames::kernelUnitLengthAttr) { 116 float x, y; 117 if (parseNumberOptionalNumber(value, x, y)) { 118 setKernelUnitLengthXBaseValue(x); 119 setKernelUnitLengthYBaseValue(y); 120 } 121 return; 122 } 123 124 ASSERT_NOT_REACHED(); 125} 126 127bool SVGFEDiffuseLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName) 128{ 129 FEDiffuseLighting* diffuseLighting = static_cast<FEDiffuseLighting*>(effect); 130 131 if (attrName == SVGNames::lighting_colorAttr) { 132 RenderObject* renderer = this->renderer(); 133 ASSERT(renderer); 134 ASSERT(renderer->style()); 135 return diffuseLighting->setLightingColor(renderer->style()->svgStyle()->lightingColor()); 136 } 137 if (attrName == SVGNames::surfaceScaleAttr) 138 return diffuseLighting->setSurfaceScale(surfaceScale()); 139 if (attrName == SVGNames::diffuseConstantAttr) 140 return diffuseLighting->setDiffuseConstant(diffuseConstant()); 141 142 LightSource* lightSource = const_cast<LightSource*>(diffuseLighting->lightSource()); 143 const SVGFELightElement* lightElement = SVGFELightElement::findLightElement(this); 144 ASSERT(lightSource); 145 ASSERT(lightElement); 146 147 if (attrName == SVGNames::azimuthAttr) 148 return lightSource->setAzimuth(lightElement->azimuth()); 149 if (attrName == SVGNames::elevationAttr) 150 return lightSource->setElevation(lightElement->elevation()); 151 if (attrName == SVGNames::xAttr) 152 return lightSource->setX(lightElement->x()); 153 if (attrName == SVGNames::yAttr) 154 return lightSource->setY(lightElement->y()); 155 if (attrName == SVGNames::zAttr) 156 return lightSource->setZ(lightElement->z()); 157 if (attrName == SVGNames::pointsAtXAttr) 158 return lightSource->setPointsAtX(lightElement->pointsAtX()); 159 if (attrName == SVGNames::pointsAtYAttr) 160 return lightSource->setPointsAtY(lightElement->pointsAtY()); 161 if (attrName == SVGNames::pointsAtZAttr) 162 return lightSource->setPointsAtZ(lightElement->pointsAtZ()); 163 if (attrName == SVGNames::specularExponentAttr) 164 return lightSource->setSpecularExponent(lightElement->specularExponent()); 165 if (attrName == SVGNames::limitingConeAngleAttr) 166 return lightSource->setLimitingConeAngle(lightElement->limitingConeAngle()); 167 168 ASSERT_NOT_REACHED(); 169 return false; 170} 171 172void SVGFEDiffuseLightingElement::svgAttributeChanged(const QualifiedName& attrName) 173{ 174 if (!isSupportedAttribute(attrName)) { 175 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName); 176 return; 177 } 178 179 SVGElementInstance::InvalidationGuard invalidationGuard(this); 180 181 if (attrName == SVGNames::surfaceScaleAttr 182 || attrName == SVGNames::diffuseConstantAttr 183 || attrName == SVGNames::kernelUnitLengthAttr 184 || attrName == SVGNames::lighting_colorAttr) { 185 primitiveAttributeChanged(attrName); 186 return; 187 } 188 189 if (attrName == SVGNames::inAttr) { 190 invalidate(); 191 return; 192 } 193 194 ASSERT_NOT_REACHED(); 195} 196 197void SVGFEDiffuseLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName) 198{ 199 if (SVGFELightElement::findLightElement(this) != lightElement) 200 return; 201 202 // The light element has different attribute names. 203 primitiveAttributeChanged(attrName); 204} 205 206PassRefPtr<FilterEffect> SVGFEDiffuseLightingElement::build(SVGFilterBuilder* filterBuilder, Filter* filter) 207{ 208 FilterEffect* input1 = filterBuilder->getEffectById(in1()); 209 210 if (!input1) 211 return 0; 212 213 RefPtr<LightSource> lightSource = SVGFELightElement::findLightSource(this); 214 if (!lightSource) 215 return 0; 216 217 RenderObject* renderer = this->renderer(); 218 if (!renderer) 219 return 0; 220 221 ASSERT(renderer->style()); 222 Color color = renderer->style()->svgStyle()->lightingColor(); 223 224 RefPtr<FilterEffect> effect = FEDiffuseLighting::create(filter, color, surfaceScale(), diffuseConstant(), 225 kernelUnitLengthX(), kernelUnitLengthY(), lightSource.release()); 226 effect->inputEffects().append(input1); 227 return effect.release(); 228} 229 230} 231 232#endif // ENABLE(SVG) 233