1 /* 2 Copyright (C) 2005 Oliver Hunt <ojh16 (at) 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 "MappedAttribute.h" 27 #include "RenderObject.h" 28 #include "SVGColor.h" 29 #include "SVGFEDiffuseLighting.h" 30 #include "SVGFELightElement.h" 31 #include "SVGNames.h" 32 #include "SVGParserUtilities.h" 33 #include "SVGRenderStyle.h" 34 #include "SVGResourceFilter.h" 35 36 namespace WebCore { 37 38 char SVGKernelUnitLengthXIdentifier[] = "SVGKernelUnitLengthX"; 39 char SVGKernelUnitLengthYIdentifier[] = "SVGKernelUnitLengthY"; 40 41 SVGFEDiffuseLightingElement::SVGFEDiffuseLightingElement(const QualifiedName& tagName, Document* doc) 42 : SVGFilterPrimitiveStandardAttributes(tagName, doc) 43 , m_diffuseConstant(1.0f) 44 , m_surfaceScale(1.0f) 45 { 46 } 47 48 SVGFEDiffuseLightingElement::~SVGFEDiffuseLightingElement() 49 { 50 } 51 52 void SVGFEDiffuseLightingElement::parseMappedAttribute(MappedAttribute *attr) 53 { 54 const String& value = attr->value(); 55 if (attr->name() == SVGNames::inAttr) 56 setIn1BaseValue(value); 57 else if (attr->name() == SVGNames::surfaceScaleAttr) 58 setSurfaceScaleBaseValue(value.toFloat()); 59 else if (attr->name() == SVGNames::diffuseConstantAttr) 60 setDiffuseConstantBaseValue(value.toInt()); 61 else if (attr->name() == SVGNames::kernelUnitLengthAttr) { 62 float x, y; 63 if (parseNumberOptionalNumber(value, x, y)) { 64 setKernelUnitLengthXBaseValue(x); 65 setKernelUnitLengthYBaseValue(y); 66 } 67 } else 68 SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr); 69 } 70 71 void SVGFEDiffuseLightingElement::synchronizeProperty(const QualifiedName& attrName) 72 { 73 SVGFilterPrimitiveStandardAttributes::synchronizeProperty(attrName); 74 75 if (attrName == anyQName()) { 76 synchronizeIn1(); 77 synchronizeSurfaceScale(); 78 synchronizeDiffuseConstant(); 79 synchronizeKernelUnitLengthX(); 80 synchronizeKernelUnitLengthY(); 81 return; 82 } 83 84 if (attrName == SVGNames::inAttr) 85 synchronizeIn1(); 86 else if (attrName == SVGNames::surfaceScaleAttr) 87 synchronizeSurfaceScale(); 88 else if (attrName == SVGNames::diffuseConstantAttr) 89 synchronizeDiffuseConstant(); 90 else if (attrName == SVGNames::kernelUnitLengthAttr) { 91 synchronizeKernelUnitLengthX(); 92 synchronizeKernelUnitLengthY(); 93 } 94 } 95 96 bool SVGFEDiffuseLightingElement::build(SVGResourceFilter* filterResource) 97 { 98 FilterEffect* input1 = filterResource->builder()->getEffectById(in1()); 99 100 if (!input1) 101 return false; 102 103 RefPtr<RenderStyle> filterStyle = styleForRenderer(); 104 Color color = filterStyle->svgStyle()->lightingColor(); 105 106 RefPtr<FilterEffect> effect = FEDiffuseLighting::create(input1, color, surfaceScale(), diffuseConstant(), 107 kernelUnitLengthX(), kernelUnitLengthY(), findLights()); 108 filterResource->addFilterEffect(this, effect.release()); 109 110 return true; 111 } 112 113 PassRefPtr<LightSource> SVGFEDiffuseLightingElement::findLights() const 114 { 115 for (Node* n = firstChild(); n; n = n->nextSibling()) { 116 if (n->hasTagName(SVGNames::feDistantLightTag) || 117 n->hasTagName(SVGNames::fePointLightTag) || 118 n->hasTagName(SVGNames::feSpotLightTag)) { 119 SVGFELightElement* lightNode = static_cast<SVGFELightElement*>(n); 120 return lightNode->lightSource(); 121 } 122 } 123 124 return 0; 125 } 126 127 } 128 129 #endif // ENABLE(SVG) 130