• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 
SVGFEDiffuseLightingElement(const QualifiedName & tagName,Document * doc)41 SVGFEDiffuseLightingElement::SVGFEDiffuseLightingElement(const QualifiedName& tagName, Document* doc)
42     : SVGFilterPrimitiveStandardAttributes(tagName, doc)
43     , m_in1(this, SVGNames::inAttr)
44     , m_diffuseConstant(this, SVGNames::diffuseConstantAttr, 1.0f)
45     , m_surfaceScale(this, SVGNames::surfaceScaleAttr, 1.0f)
46     , m_kernelUnitLengthX(this, SVGNames::kernelUnitLengthAttr)
47     , m_kernelUnitLengthY(this, SVGNames::kernelUnitLengthAttr)
48 {
49 }
50 
~SVGFEDiffuseLightingElement()51 SVGFEDiffuseLightingElement::~SVGFEDiffuseLightingElement()
52 {
53 }
54 
parseMappedAttribute(MappedAttribute * attr)55 void SVGFEDiffuseLightingElement::parseMappedAttribute(MappedAttribute *attr)
56 {
57     const String& value = attr->value();
58     if (attr->name() == SVGNames::inAttr)
59         setIn1BaseValue(value);
60     else if (attr->name() == SVGNames::surfaceScaleAttr)
61         setSurfaceScaleBaseValue(value.toFloat());
62     else if (attr->name() == SVGNames::diffuseConstantAttr)
63         setDiffuseConstantBaseValue(value.toInt());
64     else if (attr->name() == SVGNames::kernelUnitLengthAttr) {
65         float x, y;
66         if (parseNumberOptionalNumber(value, x, y)) {
67             setKernelUnitLengthXBaseValue(x);
68             setKernelUnitLengthYBaseValue(y);
69         }
70     } else
71         SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
72 }
73 
build(SVGResourceFilter * filterResource)74 bool SVGFEDiffuseLightingElement::build(SVGResourceFilter* filterResource)
75 {
76     FilterEffect* input1 = filterResource->builder()->getEffectById(in1());
77 
78     if (!input1)
79         return false;
80 
81     RefPtr<RenderStyle> filterStyle = styleForRenderer();
82     Color color = filterStyle->svgStyle()->lightingColor();
83 
84     RefPtr<FilterEffect> effect = FEDiffuseLighting::create(input1, color, surfaceScale(), diffuseConstant(),
85                                             kernelUnitLengthX(), kernelUnitLengthY(), findLights());
86     filterResource->addFilterEffect(this, effect.release());
87 
88     return true;
89 }
90 
findLights() const91 LightSource* SVGFEDiffuseLightingElement::findLights() const
92 {
93     LightSource* light = 0;
94     for (Node* n = firstChild(); n; n = n->nextSibling()) {
95         if (n->hasTagName(SVGNames::feDistantLightTag) ||
96             n->hasTagName(SVGNames::fePointLightTag) ||
97             n->hasTagName(SVGNames::feSpotLightTag)) {
98             SVGFELightElement* lightNode = static_cast<SVGFELightElement*>(n);
99             light = lightNode->lightSource();
100             break;
101         }
102     }
103 
104     return light;
105 }
106 
107 }
108 
109 #endif // ENABLE(SVG)
110