KHtml

SVGFilterElement.cpp
1 /*
2  Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <[email protected]>
3  Copyright (C) 2004, 2005, 2006 Rob Buis <[email protected]>
4  Copyright (C) 2006 Samuel Weinig <[email protected]>
5 
6  This file is part of the KDE project
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to
20  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  Boston, MA 02110-1301, USA.
22 */
23 
24 #if ENABLE(SVG) && ENABLE(SVG_FILTERS)
25 #include "SVGFilterElement.h"
26 
27 #include "Attr.h"
28 #include "SVGFilterPrimitiveStandardAttributes.h"
29 #include "SVGLength.h"
30 #include "SVGNames.h"
31 #include "SVGUnitTypes.h"
32 
33 namespace WebCore
34 {
35 
36 SVGFilterElement::SVGFilterElement(const QualifiedName &tagName, Document *doc)
37  : SVGStyledElement(tagName, doc)
38  , SVGURIReference()
39  , SVGLangSpace()
40  , SVGExternalResourcesRequired()
41  , m_filterUnits(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
42  , m_primitiveUnits(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE)
43  , m_x(this, LengthModeWidth)
44  , m_y(this, LengthModeHeight)
45  , m_width(this, LengthModeWidth)
46  , m_height(this, LengthModeHeight)
47  , m_filterResX(0)
48  , m_filterResY(0)
49 {
50  // Spec: If the attribute is not specified, the effect is as if a value of "-10%" were specified.
51  setXBaseValue(SVGLength(this, LengthModeWidth, "-10%"));
52  setYBaseValue(SVGLength(this, LengthModeHeight, "-10%"));
53 
54  // Spec: If the attribute is not specified, the effect is as if a value of "120%" were specified.
55  setWidthBaseValue(SVGLength(this, LengthModeWidth, "120%"));
56  setHeightBaseValue(SVGLength(this, LengthModeHeight, "120%"));
57 }
58 
59 SVGFilterElement::~SVGFilterElement()
60 {
61 }
62 
63 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterElement, int, Enumeration, enumeration, FilterUnits, filterUnits, SVGNames::filterUnitsAttr, m_filterUnits)
64 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterElement, int, Enumeration, enumeration, PrimitiveUnits, primitiveUnits, SVGNames::primitiveUnitsAttr, m_primitiveUnits)
65 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterElement, SVGLength, Length, length, X, x, SVGNames::xAttr, m_x)
66 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterElement, SVGLength, Length, length, Y, y, SVGNames::yAttr, m_y)
67 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterElement, SVGLength, Length, length, Width, width, SVGNames::widthAttr, m_width)
68 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterElement, SVGLength, Length, length, Height, height, SVGNames::heightAttr, m_height)
69 ANIMATED_PROPERTY_DEFINITIONS_WITH_CUSTOM_IDENTIFIER(SVGFilterElement, long, Integer, integer, FilterResX, filterResX, SVGNames::filterResAttr, "filterResX", m_filterResX)
70 ANIMATED_PROPERTY_DEFINITIONS_WITH_CUSTOM_IDENTIFIER(SVGFilterElement, long, Integer, integer, FilterResY, filterResY, SVGNames::filterResAttr, "filterResY", m_filterResY)
71 
72 void SVGFilterElement::setFilterRes(unsigned long, unsigned long) const
73 {
74 }
75 
76 void SVGFilterElement::parseMappedAttribute(MappedAttribute *attr)
77 {
78  const String &value = attr->value();
79  if (attr->name() == SVGNames::filterUnitsAttr) {
80  if (value == "userSpaceOnUse") {
81  setFilterUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE);
82  } else if (value == "objectBoundingBox") {
83  setFilterUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
84  }
85  } else if (attr->name() == SVGNames::primitiveUnitsAttr) {
86  if (value == "userSpaceOnUse") {
87  setPrimitiveUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE);
88  } else if (value == "objectBoundingBox") {
89  setPrimitiveUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
90  }
91  } else if (attr->name() == SVGNames::xAttr) {
92  setXBaseValue(SVGLength(this, LengthModeWidth, value));
93  } else if (attr->name() == SVGNames::yAttr) {
94  setYBaseValue(SVGLength(this, LengthModeHeight, value));
95  } else if (attr->name() == SVGNames::widthAttr) {
96  setWidthBaseValue(SVGLength(this, LengthModeWidth, value));
97  } else if (attr->name() == SVGNames::heightAttr) {
98  setHeightBaseValue(SVGLength(this, LengthModeHeight, value));
99  } else {
100  if (SVGURIReference::parseMappedAttribute(attr)) {
101  return;
102  }
103  if (SVGLangSpace::parseMappedAttribute(attr)) {
104  return;
105  }
106  if (SVGExternalResourcesRequired::parseMappedAttribute(attr)) {
107  return;
108  }
109 
110  SVGStyledElement::parseMappedAttribute(attr);
111  }
112 }
113 
114 SVGResource *SVGFilterElement::canvasResource()
115 {
116  if (!attached()) {
117  return 0;
118  }
119 
120  if (!m_filter) {
121  m_filter = new SVGResourceFilter();
122  }
123 
124  bool filterBBoxMode = filterUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX;
125  m_filter->setFilterBoundingBoxMode(filterBBoxMode);
126 
127  float _x, _y, _width, _height;
128 
129  if (filterBBoxMode) {
130  _x = x().valueAsPercentage();
131  _y = y().valueAsPercentage();
132  _width = width().valueAsPercentage();
133  _height = height().valueAsPercentage();
134  } else {
135  m_filter->setXBoundingBoxMode(x().unitType() == LengthTypePercentage);
136  m_filter->setYBoundingBoxMode(y().unitType() == LengthTypePercentage);
137 
138  _x = x().value();
139  _y = y().value();
140  _width = width().value();
141  _height = height().value();
142  }
143 
144  m_filter->setFilterRect(FloatRect(_x, _y, _width, _height));
145 
146  bool primitiveBBoxMode = primitiveUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX;
147  m_filter->setEffectBoundingBoxMode(primitiveBBoxMode);
148 
149  // TODO : use switch/case instead?
150  m_filter->clearEffects();
151  for (Node *n = firstChild(); n != 0; n = n->nextSibling()) {
152  SVGElement *element = 0;
153  if (n->isSVGElement()) {
154  element = static_cast<SVGElement *>(n);
155  }
156  if (element && element->isFilterEffect()) {
157  SVGFilterPrimitiveStandardAttributes *filterAttributes = static_cast<SVGFilterPrimitiveStandardAttributes *>(element);
158  SVGFilterEffect *filterEffect = filterAttributes->filterEffect(m_filter.get());
159  if (!filterEffect) {
160  continue;
161  }
162 
163  m_filter->addFilterEffect(filterEffect);
164  }
165  }
166 
167  return m_filter.get();
168 }
169 
170 }
171 
172 #endif // ENABLE(SVG)
173 
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 Tue Oct 26 2021 22:48:10 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.