1 /*
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) 2009 Dirk Schulze <krit@webkit.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #include "config.h"
23
24 #include "core/svg/SVGFilterPrimitiveStandardAttributes.h"
25
26 #include "core/SVGNames.h"
27 #include "platform/graphics/filters/FilterEffect.h"
28 #include "core/rendering/svg/RenderSVGResourceFilterPrimitive.h"
29 #include "core/svg/SVGLength.h"
30
31 namespace WebCore {
32
SVGFilterPrimitiveStandardAttributes(const QualifiedName & tagName,Document & document)33 SVGFilterPrimitiveStandardAttributes::SVGFilterPrimitiveStandardAttributes(const QualifiedName& tagName, Document& document)
34 : SVGElement(tagName, document)
35 , m_x(SVGAnimatedLength::create(this, SVGNames::xAttr, SVGLength::create(LengthModeWidth), AllowNegativeLengths))
36 , m_y(SVGAnimatedLength::create(this, SVGNames::yAttr, SVGLength::create(LengthModeHeight), AllowNegativeLengths))
37 , m_width(SVGAnimatedLength::create(this, SVGNames::widthAttr, SVGLength::create(LengthModeWidth), ForbidNegativeLengths))
38 , m_height(SVGAnimatedLength::create(this, SVGNames::heightAttr, SVGLength::create(LengthModeHeight), ForbidNegativeLengths))
39 , m_result(SVGAnimatedString::create(this, SVGNames::resultAttr, SVGString::create()))
40 {
41 // Spec: If the x/y attribute is not specified, the effect is as if a value of "0%" were specified.
42 m_x->setDefaultValueAsString("0%");
43 m_y->setDefaultValueAsString("0%");
44
45 // Spec: If the width/height attribute is not specified, the effect is as if a value of "100%" were specified.
46 m_width->setDefaultValueAsString("100%");
47 m_height->setDefaultValueAsString("100%");
48
49 addToPropertyMap(m_x);
50 addToPropertyMap(m_y);
51 addToPropertyMap(m_width);
52 addToPropertyMap(m_height);
53 addToPropertyMap(m_result);
54 }
55
isSupportedAttribute(const QualifiedName & attrName)56 bool SVGFilterPrimitiveStandardAttributes::isSupportedAttribute(const QualifiedName& attrName)
57 {
58 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
59 if (supportedAttributes.isEmpty()) {
60 supportedAttributes.add(SVGNames::xAttr);
61 supportedAttributes.add(SVGNames::yAttr);
62 supportedAttributes.add(SVGNames::widthAttr);
63 supportedAttributes.add(SVGNames::heightAttr);
64 supportedAttributes.add(SVGNames::resultAttr);
65 }
66 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
67 }
68
parseAttribute(const QualifiedName & name,const AtomicString & value)69 void SVGFilterPrimitiveStandardAttributes::parseAttribute(const QualifiedName& name, const AtomicString& value)
70 {
71 SVGParsingError parseError = NoError;
72
73 if (!isSupportedAttribute(name))
74 SVGElement::parseAttribute(name, value);
75 else if (name == SVGNames::xAttr)
76 m_x->setBaseValueAsString(value, parseError);
77 else if (name == SVGNames::yAttr)
78 m_y->setBaseValueAsString(value, parseError);
79 else if (name == SVGNames::widthAttr)
80 m_width->setBaseValueAsString(value, parseError);
81 else if (name == SVGNames::heightAttr)
82 m_height->setBaseValueAsString(value, parseError);
83 else if (name == SVGNames::resultAttr)
84 m_result->setBaseValueAsString(value, parseError);
85 else
86 ASSERT_NOT_REACHED();
87
88 reportAttributeParsingError(parseError, name, value);
89 }
90
setFilterEffectAttribute(FilterEffect *,const QualifiedName &)91 bool SVGFilterPrimitiveStandardAttributes::setFilterEffectAttribute(FilterEffect*, const QualifiedName&)
92 {
93 // When all filters support this method, it will be changed to a pure virtual method.
94 ASSERT_NOT_REACHED();
95 return false;
96 }
97
svgAttributeChanged(const QualifiedName & attrName)98 void SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(const QualifiedName& attrName)
99 {
100 if (!isSupportedAttribute(attrName)) {
101 SVGElement::svgAttributeChanged(attrName);
102 return;
103 }
104
105 SVGElement::InvalidationGuard invalidationGuard(this);
106 invalidate();
107 }
108
childrenChanged(bool changedByParser,Node * beforeChange,Node * afterChange,int childCountDelta)109 void SVGFilterPrimitiveStandardAttributes::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
110 {
111 SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
112
113 if (!changedByParser)
114 invalidate();
115 }
116
setStandardAttributes(FilterEffect * filterEffect) const117 void SVGFilterPrimitiveStandardAttributes::setStandardAttributes(FilterEffect* filterEffect) const
118 {
119 ASSERT(filterEffect);
120 if (!filterEffect)
121 return;
122
123 if (hasAttribute(SVGNames::xAttr))
124 filterEffect->setHasX(true);
125 if (hasAttribute(SVGNames::yAttr))
126 filterEffect->setHasY(true);
127 if (hasAttribute(SVGNames::widthAttr))
128 filterEffect->setHasWidth(true);
129 if (hasAttribute(SVGNames::heightAttr))
130 filterEffect->setHasHeight(true);
131 }
132
createRenderer(RenderStyle *)133 RenderObject* SVGFilterPrimitiveStandardAttributes::createRenderer(RenderStyle*)
134 {
135 return new RenderSVGResourceFilterPrimitive(this);
136 }
137
rendererIsNeeded(const RenderStyle & style)138 bool SVGFilterPrimitiveStandardAttributes::rendererIsNeeded(const RenderStyle& style)
139 {
140 if (isSVGFilterElement(parentNode()))
141 return SVGElement::rendererIsNeeded(style);
142
143 return false;
144 }
145
primitiveAttributeChanged(const QualifiedName & attribute)146 void SVGFilterPrimitiveStandardAttributes::primitiveAttributeChanged(const QualifiedName& attribute)
147 {
148 if (RenderObject* primitiveRenderer = renderer())
149 static_cast<RenderSVGResourceFilterPrimitive*>(primitiveRenderer)->primitiveAttributeChanged(attribute);
150 }
151
invalidateFilterPrimitiveParent(SVGElement * element)152 void invalidateFilterPrimitiveParent(SVGElement* element)
153 {
154 if (!element)
155 return;
156
157 ContainerNode* parent = element->parentNode();
158
159 if (!parent)
160 return;
161
162 RenderObject* renderer = parent->renderer();
163 if (!renderer || !renderer->isSVGResourceFilterPrimitive())
164 return;
165
166 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer, false);
167 }
168
169 }
170