File indexing completed on 2024-04-28 15:24:30

0001 /*
0002      Copyright (C) 2005 Oliver Hunt <ojh16@student.canterbury.ac.nz>
0003 
0004      This library is free software; you can redistribute it and/or
0005      modify it under the terms of the GNU Library General Public
0006      License as published by the Free Software Foundation; either
0007      version 2 of the License, or (at your option) any later version.
0008 
0009      This library is distributed in the hope that it will be useful,
0010      but WITHOUT ANY WARRANTY; without even the implied warranty of
0011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012      Library General Public License for more details.
0013 
0014      You should have received a copy of the GNU Library General Public License
0015      along with this library; see the file COPYING.LIB.  If not, write to
0016      the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017      Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #if ENABLE(SVG) && ENABLE(SVG_FILTERS)
0021 #include "SVGFEDiffuseLightingElement.h"
0022 
0023 #include "Attr.h"
0024 #include "RenderObject.h"
0025 #include "SVGColor.h"
0026 #include "SVGFELightElement.h"
0027 #include "SVGNames.h"
0028 #include "SVGParserUtilities.h"
0029 #include "SVGRenderStyle.h"
0030 #include "SVGFEDiffuseLighting.h"
0031 #include "SVGResourceFilter.h"
0032 
0033 namespace WebCore
0034 {
0035 
0036 SVGFEDiffuseLightingElement::SVGFEDiffuseLightingElement(const QualifiedName &tagName, Document *doc)
0037     : SVGFilterPrimitiveStandardAttributes(tagName, doc)
0038     , m_diffuseConstant(1.0f)
0039     , m_surfaceScale(1.0f)
0040     , m_kernelUnitLengthX(0.0f)
0041     , m_kernelUnitLengthY(0.0f)
0042     , m_filterEffect(0)
0043 {
0044 }
0045 
0046 SVGFEDiffuseLightingElement::~SVGFEDiffuseLightingElement()
0047 {
0048     delete m_filterEffect;
0049 }
0050 
0051 ANIMATED_PROPERTY_DEFINITIONS(SVGFEDiffuseLightingElement, String, String, string, In1, in1, SVGNames::inAttr, m_in1)
0052 ANIMATED_PROPERTY_DEFINITIONS(SVGFEDiffuseLightingElement, float, Number, number, DiffuseConstant, diffuseConstant, SVGNames::diffuseConstantAttr, m_diffuseConstant)
0053 ANIMATED_PROPERTY_DEFINITIONS(SVGFEDiffuseLightingElement, float, Number, number, SurfaceScale, surfaceScale, SVGNames::surfaceScaleAttr, m_surfaceScale)
0054 ANIMATED_PROPERTY_DEFINITIONS_WITH_CUSTOM_IDENTIFIER(SVGFEDiffuseLightingElement, float, Number, number, KernelUnitLengthX, kernelUnitLengthX, SVGNames::kernelUnitLengthAttr, "kernelUnitLengthX", m_kernelUnitLengthX)
0055 ANIMATED_PROPERTY_DEFINITIONS_WITH_CUSTOM_IDENTIFIER(SVGFEDiffuseLightingElement, float, Number, number, KernelUnitLengthY, kernelUnitLengthY, SVGNames::kernelUnitLengthAttr, "kernelUnitLengthY", m_kernelUnitLengthY)
0056 
0057 void SVGFEDiffuseLightingElement::parseMappedAttribute(MappedAttribute *attr)
0058 {
0059     const String &value = attr->value();
0060     if (attr->name() == SVGNames::inAttr) {
0061         setIn1BaseValue(value);
0062     } else if (attr->name() == SVGNames::surfaceScaleAttr) {
0063         setSurfaceScaleBaseValue(value.toFloat());
0064     } else if (attr->name() == SVGNames::diffuseConstantAttr) {
0065         setDiffuseConstantBaseValue(value.toInt());
0066     } else if (attr->name() == SVGNames::kernelUnitLengthAttr) {
0067         float x, y;
0068         if (parseNumberOptionalNumber(value, x, y)) {
0069             setKernelUnitLengthXBaseValue(x);
0070             setKernelUnitLengthYBaseValue(y);
0071         }
0072     } else {
0073         SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
0074     }
0075 }
0076 
0077 SVGFilterEffect *SVGFEDiffuseLightingElement::filterEffect(SVGResourceFilter *filter) const
0078 {
0079     if (!m_filterEffect) {
0080         m_filterEffect = new SVGFEDiffuseLighting(filter);
0081     }
0082 
0083     m_filterEffect->setIn(in1());
0084     m_filterEffect->setDiffuseConstant(diffuseConstant());
0085     m_filterEffect->setSurfaceScale(surfaceScale());
0086     m_filterEffect->setKernelUnitLengthX(kernelUnitLengthX());
0087     m_filterEffect->setKernelUnitLengthY(kernelUnitLengthY());
0088 
0089     SVGFEDiffuseLightingElement *nonConstThis = const_cast<SVGFEDiffuseLightingElement *>(this);
0090 
0091     RenderStyle *parentStyle = nonConstThis->styleForRenderer(parent()->renderer());
0092     RenderStyle *filterStyle = nonConstThis->resolveStyle(parentStyle);
0093 
0094     m_filterEffect->setLightingColor(filterStyle->svgStyle()->lightingColor());
0095     setStandardAttributes(m_filterEffect);
0096 
0097     parentStyle->deref(document()->renderArena());
0098     filterStyle->deref(document()->renderArena());
0099 
0100     updateLights();
0101     return m_filterEffect;
0102 }
0103 
0104 void SVGFEDiffuseLightingElement::updateLights() const
0105 {
0106     if (!m_filterEffect) {
0107         return;
0108     }
0109 
0110     SVGLightSource *light = 0;
0111     for (Node *n = firstChild(); n; n = n->nextSibling()) {
0112         if (n->hasTagName(SVGNames::feDistantLightTag) ||
0113                 n->hasTagName(SVGNames::fePointLightTag) ||
0114                 n->hasTagName(SVGNames::feSpotLightTag)) {
0115             SVGFELightElement *lightNode = static_cast<SVGFELightElement *>(n);
0116             light = lightNode->lightSource();
0117             break;
0118         }
0119     }
0120 
0121     m_filterEffect->setLightSource(light);
0122 }
0123 
0124 }
0125 
0126 #endif // ENABLE(SVG)
0127