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(SVG_FILTERS)
23 #include "SVGFEDiffuseLightingElement.h"
24
25 #include "Attr.h"
26 #include "RenderObject.h"
27 #include "SVGColor.h"
28 #include "SVGFELightElement.h"
29 #include "SVGNames.h"
30 #include "SVGParserUtilities.h"
31 #include "SVGRenderStyle.h"
32 #include "SVGFEDiffuseLighting.h"
33 #include "SVGResourceFilter.h"
34
35 namespace WebCore {
36
37 char SVGKernelUnitLengthXIdentifier[] = "SVGKernelUnitLengthX";
38 char SVGKernelUnitLengthYIdentifier[] = "SVGKernelUnitLengthY";
39
SVGFEDiffuseLightingElement(const QualifiedName & tagName,Document * doc)40 SVGFEDiffuseLightingElement::SVGFEDiffuseLightingElement(const QualifiedName& tagName, Document* doc)
41 : SVGFilterPrimitiveStandardAttributes(tagName, doc)
42 , m_in1(this, SVGNames::inAttr)
43 , m_diffuseConstant(this, SVGNames::diffuseConstantAttr, 1.0f)
44 , m_surfaceScale(this, SVGNames::surfaceScaleAttr, 1.0f)
45 , m_kernelUnitLengthX(this, SVGNames::kernelUnitLengthAttr)
46 , m_kernelUnitLengthY(this, SVGNames::kernelUnitLengthAttr)
47 , m_filterEffect(0)
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
filterEffect(SVGResourceFilter * filter) const74 SVGFilterEffect* SVGFEDiffuseLightingElement::filterEffect(SVGResourceFilter* filter) const
75 {
76 ASSERT_NOT_REACHED();
77 return 0;
78 }
79
build(FilterBuilder * builder)80 bool SVGFEDiffuseLightingElement::build(FilterBuilder* builder)
81 {
82 FilterEffect* input1 = builder->getEffectById(in1());
83
84 if(!input1)
85 return false;
86
87 RefPtr<RenderStyle> filterStyle = styleForRenderer();
88 Color color = filterStyle->svgStyle()->lightingColor();
89
90 RefPtr<FilterEffect> addedEffect = FEDiffuseLighting::create(input1, color, surfaceScale(), diffuseConstant(),
91 kernelUnitLengthX(), kernelUnitLengthY(), findLights());
92 builder->add(result(), addedEffect.release());
93
94 return true;
95 }
96
findLights() const97 LightSource* SVGFEDiffuseLightingElement::findLights() const
98 {
99 LightSource* light = 0;
100 for (Node* n = firstChild(); n; n = n->nextSibling()) {
101 if (n->hasTagName(SVGNames::feDistantLightTag) ||
102 n->hasTagName(SVGNames::fePointLightTag) ||
103 n->hasTagName(SVGNames::feSpotLightTag)) {
104 SVGFELightElement* lightNode = static_cast<SVGFELightElement*>(n);
105 light = lightNode->lightSource();
106 break;
107 }
108 }
109
110 return light;
111 }
112
113 }
114
115 #endif // ENABLE(SVG)
116