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

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