1 /*
2 Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005, 2006 Rob Buis <buis@kde.org>
4
5 This file is part of the KDE project
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21 */
22
23 #include "config.h"
24
25 #if ENABLE(SVG) && ENABLE(FILTERS)
26 #include "SVGFilterPrimitiveStandardAttributes.h"
27
28 #include "FilterEffect.h"
29 #include "MappedAttribute.h"
30 #include "SVGLength.h"
31 #include "SVGNames.h"
32 #include "SVGStyledElement.h"
33 #include "SVGUnitTypes.h"
34
35 namespace WebCore {
36
37 char SVGFilterPrimitiveStandardAttributesIdentifierIdentifier[] = "SVGFilterPrimitiveStandardAttributesIdentifier";
38
SVGFilterPrimitiveStandardAttributes(const QualifiedName & tagName,Document * doc)39 SVGFilterPrimitiveStandardAttributes::SVGFilterPrimitiveStandardAttributes(const QualifiedName& tagName, Document* doc)
40 : SVGStyledElement(tagName, doc)
41 , m_x(this, SVGNames::xAttr, LengthModeWidth, "0%")
42 , m_y(this, SVGNames::yAttr, LengthModeHeight, "0%")
43 , m_width(this, SVGNames::widthAttr, LengthModeWidth, "100%")
44 , m_height(this, SVGNames::heightAttr, LengthModeHeight, "100%")
45 , m_result(this, SVGNames::resultAttr)
46 {
47 // Spec: If the x/y attribute is not specified, the effect is as if a value of "0%" were specified.
48 // Spec: If the width/height attribute is not specified, the effect is as if a value of "100%" were specified.
49 }
50
~SVGFilterPrimitiveStandardAttributes()51 SVGFilterPrimitiveStandardAttributes::~SVGFilterPrimitiveStandardAttributes()
52 {
53 }
54
parseMappedAttribute(MappedAttribute * attr)55 void SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(MappedAttribute* attr)
56 {
57 const AtomicString& value = attr->value();
58 if (attr->name() == SVGNames::xAttr)
59 setXBaseValue(SVGLength(LengthModeWidth, value));
60 else if (attr->name() == SVGNames::yAttr)
61 setYBaseValue(SVGLength(LengthModeHeight, value));
62 else if (attr->name() == SVGNames::widthAttr)
63 setWidthBaseValue(SVGLength(LengthModeWidth, value));
64 else if (attr->name() == SVGNames::heightAttr)
65 setHeightBaseValue(SVGLength(LengthModeHeight, value));
66 else if (attr->name() == SVGNames::resultAttr)
67 setResultBaseValue(value);
68 else
69 return SVGStyledElement::parseMappedAttribute(attr);
70 }
71
setStandardAttributes(SVGResourceFilter * resourceFilter,FilterEffect * filterEffect) const72 void SVGFilterPrimitiveStandardAttributes::setStandardAttributes(SVGResourceFilter* resourceFilter, FilterEffect* filterEffect) const
73 {
74 ASSERT(filterEffect);
75 if (!filterEffect)
76 return;
77
78 ASSERT(resourceFilter);
79
80 float _x, _y, _width, _height;
81
82 if (this->hasAttribute(SVGNames::xAttr))
83 filterEffect->setHasX(true);
84 if (this->hasAttribute(SVGNames::yAttr))
85 filterEffect->setHasY(true);
86 if (this->hasAttribute(SVGNames::widthAttr))
87 filterEffect->setHasWidth(true);
88 if (this->hasAttribute(SVGNames::heightAttr))
89 filterEffect->setHasHeight(true);
90
91 if (resourceFilter->effectBoundingBoxMode()) {
92 _x = x().valueAsPercentage();
93 _y = y().valueAsPercentage();
94 _width = width().valueAsPercentage();
95 _height = height().valueAsPercentage();
96 } else {
97 // We need to resolve any percentages in filter rect space.
98 if (x().unitType() == LengthTypePercentage) {
99 filterEffect->setXBoundingBoxMode(true);
100 _x = x().valueAsPercentage();
101 } else {
102 filterEffect->setXBoundingBoxMode(false);
103 _x = x().value(this);
104 }
105
106 if (y().unitType() == LengthTypePercentage) {
107 filterEffect->setYBoundingBoxMode(true);
108 _y = y().valueAsPercentage();
109 } else {
110 filterEffect->setYBoundingBoxMode(false);
111 _y = y().value(this);
112 }
113
114 if (width().unitType() == LengthTypePercentage) {
115 filterEffect->setWidthBoundingBoxMode(true);
116 _width = width().valueAsPercentage();
117 } else {
118 filterEffect->setWidthBoundingBoxMode(false);
119 _width = width().value(this);
120 }
121
122 if (height().unitType() == LengthTypePercentage) {
123 filterEffect->setHeightBoundingBoxMode(true);
124 _height = height().valueAsPercentage();
125 } else {
126 filterEffect->setHeightBoundingBoxMode(false);
127 _height = height().value(this);
128 }
129 }
130
131 filterEffect->setSubRegion(FloatRect(_x, _y, _width, _height));
132 }
133
134 }
135
136 #endif // ENABLE(SVG)
137