KHtml

SVGFEDiffuseLightingElement.cpp
1 /*
2  Copyright (C) 2005 Oliver Hunt <[email protected]>
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 #if ENABLE(SVG) && ENABLE(SVG_FILTERS)
21 #include "SVGFEDiffuseLightingElement.h"
22 
23 #include "Attr.h"
24 #include "RenderObject.h"
25 #include "SVGColor.h"
26 #include "SVGFELightElement.h"
27 #include "SVGNames.h"
28 #include "SVGParserUtilities.h"
29 #include "SVGRenderStyle.h"
30 #include "SVGFEDiffuseLighting.h"
31 #include "SVGResourceFilter.h"
32 
33 namespace WebCore
34 {
35 
36 SVGFEDiffuseLightingElement::SVGFEDiffuseLightingElement(const QualifiedName &tagName, Document *doc)
37  : SVGFilterPrimitiveStandardAttributes(tagName, doc)
38  , m_diffuseConstant(1.0f)
39  , m_surfaceScale(1.0f)
40  , m_kernelUnitLengthX(0.0f)
41  , m_kernelUnitLengthY(0.0f)
42  , m_filterEffect(0)
43 {
44 }
45 
46 SVGFEDiffuseLightingElement::~SVGFEDiffuseLightingElement()
47 {
48  delete m_filterEffect;
49 }
50 
51 ANIMATED_PROPERTY_DEFINITIONS(SVGFEDiffuseLightingElement, String, String, string, In1, in1, SVGNames::inAttr, m_in1)
52 ANIMATED_PROPERTY_DEFINITIONS(SVGFEDiffuseLightingElement, float, Number, number, DiffuseConstant, diffuseConstant, SVGNames::diffuseConstantAttr, m_diffuseConstant)
53 ANIMATED_PROPERTY_DEFINITIONS(SVGFEDiffuseLightingElement, float, Number, number, SurfaceScale, surfaceScale, SVGNames::surfaceScaleAttr, m_surfaceScale)
54 ANIMATED_PROPERTY_DEFINITIONS_WITH_CUSTOM_IDENTIFIER(SVGFEDiffuseLightingElement, float, Number, number, KernelUnitLengthX, kernelUnitLengthX, SVGNames::kernelUnitLengthAttr, "kernelUnitLengthX", m_kernelUnitLengthX)
55 ANIMATED_PROPERTY_DEFINITIONS_WITH_CUSTOM_IDENTIFIER(SVGFEDiffuseLightingElement, float, Number, number, KernelUnitLengthY, kernelUnitLengthY, SVGNames::kernelUnitLengthAttr, "kernelUnitLengthY", m_kernelUnitLengthY)
56 
57 void SVGFEDiffuseLightingElement::parseMappedAttribute(MappedAttribute *attr)
58 {
59  const String &value = attr->value();
60  if (attr->name() == SVGNames::inAttr) {
61  setIn1BaseValue(value);
62  } else if (attr->name() == SVGNames::surfaceScaleAttr) {
63  setSurfaceScaleBaseValue(value.toFloat());
64  } else if (attr->name() == SVGNames::diffuseConstantAttr) {
65  setDiffuseConstantBaseValue(value.toInt());
66  } else if (attr->name() == SVGNames::kernelUnitLengthAttr) {
67  float x, y;
68  if (parseNumberOptionalNumber(value, x, y)) {
69  setKernelUnitLengthXBaseValue(x);
70  setKernelUnitLengthYBaseValue(y);
71  }
72  } else {
73  SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
74  }
75 }
76 
77 SVGFilterEffect *SVGFEDiffuseLightingElement::filterEffect(SVGResourceFilter *filter) const
78 {
79  if (!m_filterEffect) {
80  m_filterEffect = new SVGFEDiffuseLighting(filter);
81  }
82 
83  m_filterEffect->setIn(in1());
84  m_filterEffect->setDiffuseConstant(diffuseConstant());
85  m_filterEffect->setSurfaceScale(surfaceScale());
86  m_filterEffect->setKernelUnitLengthX(kernelUnitLengthX());
87  m_filterEffect->setKernelUnitLengthY(kernelUnitLengthY());
88 
89  SVGFEDiffuseLightingElement *nonConstThis = const_cast<SVGFEDiffuseLightingElement *>(this);
90 
91  RenderStyle *parentStyle = nonConstThis->styleForRenderer(parent()->renderer());
92  RenderStyle *filterStyle = nonConstThis->resolveStyle(parentStyle);
93 
94  m_filterEffect->setLightingColor(filterStyle->svgStyle()->lightingColor());
95  setStandardAttributes(m_filterEffect);
96 
97  parentStyle->deref(document()->renderArena());
98  filterStyle->deref(document()->renderArena());
99 
100  updateLights();
101  return m_filterEffect;
102 }
103 
104 void SVGFEDiffuseLightingElement::updateLights() const
105 {
106  if (!m_filterEffect) {
107  return;
108  }
109 
110  SVGLightSource *light = 0;
111  for (Node *n = firstChild(); n; n = n->nextSibling()) {
112  if (n->hasTagName(SVGNames::feDistantLightTag) ||
113  n->hasTagName(SVGNames::fePointLightTag) ||
114  n->hasTagName(SVGNames::feSpotLightTag)) {
115  SVGFELightElement *lightNode = static_cast<SVGFELightElement *>(n);
116  light = lightNode->lightSource();
117  break;
118  }
119  }
120 
121  m_filterEffect->setLightSource(light);
122 }
123 
124 }
125 
126 #endif // ENABLE(SVG)
127 
MESSAGECORE_EXPORT KMime::Content * firstChild(const KMime::Content *node)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 22:48:22 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.