2 Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4 Copyright (C) 2006 Samuel Weinig <sam.weinig@gmail.com>
6 This file is part of the KDE project
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.
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.
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.
26 #if ENABLE(SVG) && ENABLE(SVG_FILTERS)
27 #include "SVGFilterElement.h"
30 #include "SVGResourceFilter.h"
31 #include "SVGFilterPrimitiveStandardAttributes.h"
32 #include "SVGLength.h"
34 #include "SVGUnitTypes.h"
38 SVGFilterElement::SVGFilterElement(const QualifiedName
& tagName
, Document
* doc
)
39 : SVGStyledElement(tagName
, doc
)
42 , SVGExternalResourcesRequired()
43 , m_filterUnits(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX
)
44 , m_primitiveUnits(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE
)
45 , m_x(this, LengthModeWidth
)
46 , m_y(this, LengthModeHeight
)
47 , m_width(this, LengthModeWidth
)
48 , m_height(this, LengthModeHeight
)
52 // Spec: If the attribute is not specified, the effect is as if a value of "-10%" were specified.
53 setXBaseValue(SVGLength(this, LengthModeWidth
, "-10%"));
54 setYBaseValue(SVGLength(this, LengthModeHeight
, "-10%"));
56 // Spec: If the attribute is not specified, the effect is as if a value of "120%" were specified.
57 setWidthBaseValue(SVGLength(this, LengthModeWidth
, "120%"));
58 setHeightBaseValue(SVGLength(this, LengthModeHeight
, "120%"));
61 SVGFilterElement::~SVGFilterElement()
65 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterElement
, int, Enumeration
, enumeration
, FilterUnits
, filterUnits
, SVGNames::filterUnitsAttr
, m_filterUnits
)
66 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterElement
, int, Enumeration
, enumeration
, PrimitiveUnits
, primitiveUnits
, SVGNames::primitiveUnitsAttr
, m_primitiveUnits
)
67 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterElement
, SVGLength
, Length
, length
, X
, x
, SVGNames::xAttr
, m_x
)
68 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterElement
, SVGLength
, Length
, length
, Y
, y
, SVGNames::yAttr
, m_y
)
69 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterElement
, SVGLength
, Length
, length
, Width
, width
, SVGNames::widthAttr
, m_width
)
70 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterElement
, SVGLength
, Length
, length
, Height
, height
, SVGNames::heightAttr
, m_height
)
71 ANIMATED_PROPERTY_DEFINITIONS_WITH_CUSTOM_IDENTIFIER(SVGFilterElement
, long, Integer
, integer
, FilterResX
, filterResX
, SVGNames::filterResAttr
, "filterResX", m_filterResX
)
72 ANIMATED_PROPERTY_DEFINITIONS_WITH_CUSTOM_IDENTIFIER(SVGFilterElement
, long, Integer
, integer
, FilterResY
, filterResY
, SVGNames::filterResAttr
, "filterResY", m_filterResY
)
74 void SVGFilterElement::setFilterRes(unsigned long, unsigned long) const
78 void SVGFilterElement::parseMappedAttribute(MappedAttribute
* attr
)
80 const String
& value
= attr
->value();
81 if (attr
->name() == SVGNames::filterUnitsAttr
) {
82 if (value
== "userSpaceOnUse")
83 setFilterUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE
);
84 else if (value
== "objectBoundingBox")
85 setFilterUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX
);
86 } else if (attr
->name() == SVGNames::primitiveUnitsAttr
) {
87 if (value
== "userSpaceOnUse")
88 setPrimitiveUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE
);
89 else if (value
== "objectBoundingBox")
90 setPrimitiveUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX
);
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
));
100 if (SVGURIReference::parseMappedAttribute(attr
)) return;
101 if (SVGLangSpace::parseMappedAttribute(attr
)) return;
102 if (SVGExternalResourcesRequired::parseMappedAttribute(attr
)) return;
104 SVGStyledElement::parseMappedAttribute(attr
);
108 SVGResource
* SVGFilterElement::canvasResource()
114 m_filter
= new SVGResourceFilter();
116 bool filterBBoxMode
= filterUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX
;
117 m_filter
->setFilterBoundingBoxMode(filterBBoxMode
);
119 float _x
, _y
, _width
, _height
;
121 if (filterBBoxMode
) {
122 _x
= x().valueAsPercentage();
123 _y
= y().valueAsPercentage();
124 _width
= width().valueAsPercentage();
125 _height
= height().valueAsPercentage();
127 m_filter
->setXBoundingBoxMode(x().unitType() == LengthTypePercentage
);
128 m_filter
->setYBoundingBoxMode(y().unitType() == LengthTypePercentage
);
132 _width
= width().value();
133 _height
= height().value();
136 m_filter
->setFilterRect(FloatRect(_x
, _y
, _width
, _height
));
138 bool primitiveBBoxMode
= primitiveUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX
;
139 m_filter
->setEffectBoundingBoxMode(primitiveBBoxMode
);
141 // TODO : use switch/case instead?
142 m_filter
->clearEffects();
143 for (Node
* n
= firstChild(); n
!= 0; n
= n
->nextSibling()) {
144 SVGElement
* element
= 0;
145 if (n
->isSVGElement())
146 element
= static_cast<SVGElement
*>(n
);
147 if (element
&& element
->isFilterEffect()) {
148 SVGFilterPrimitiveStandardAttributes
* filterAttributes
= static_cast<SVGFilterPrimitiveStandardAttributes
*>(element
);
149 SVGFilterEffect
* filterEffect
= filterAttributes
->filterEffect(m_filter
.get());
153 m_filter
->addFilterEffect(filterEffect
);
157 return m_filter
.get();
162 #endif // ENABLE(SVG)