2011-05-09 Rob Buis <rbuis@rim.com>
[WebKit-https.git] / Source / WebCore / svg / SVGPatternElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
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 #if ENABLE(SVG)
25 #include "SVGPatternElement.h"
26
27 #include "AffineTransform.h"
28 #include "Attribute.h"
29 #include "Document.h"
30 #include "FloatConversion.h"
31 #include "GraphicsContext.h"
32 #include "ImageBuffer.h"
33 #include "PatternAttributes.h"
34 #include "RenderSVGContainer.h"
35 #include "RenderSVGResourcePattern.h"
36 #include "SVGNames.h"
37 #include "SVGRenderSupport.h"
38 #include "SVGSVGElement.h"
39 #include "SVGStyledTransformableElement.h"
40 #include "SVGTransformable.h"
41 #include "SVGUnitTypes.h"
42
43 namespace WebCore {
44
45 // Animated property definitions
46 DEFINE_ANIMATED_LENGTH(SVGPatternElement, SVGNames::xAttr, X, x)
47 DEFINE_ANIMATED_LENGTH(SVGPatternElement, SVGNames::yAttr, Y, y)
48 DEFINE_ANIMATED_LENGTH(SVGPatternElement, SVGNames::widthAttr, Width, width)
49 DEFINE_ANIMATED_LENGTH(SVGPatternElement, SVGNames::heightAttr, Height, height)
50 DEFINE_ANIMATED_ENUMERATION(SVGPatternElement, SVGNames::patternUnitsAttr, PatternUnits, patternUnits)
51 DEFINE_ANIMATED_ENUMERATION(SVGPatternElement, SVGNames::patternContentUnitsAttr, PatternContentUnits, patternContentUnits)
52 DEFINE_ANIMATED_TRANSFORM_LIST(SVGPatternElement, SVGNames::patternTransformAttr, PatternTransform, patternTransform) 
53 DEFINE_ANIMATED_STRING(SVGPatternElement, XLinkNames::hrefAttr, Href, href)
54 DEFINE_ANIMATED_BOOLEAN(SVGPatternElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
55 DEFINE_ANIMATED_RECT(SVGPatternElement, SVGNames::viewBoxAttr, ViewBox, viewBox)
56 DEFINE_ANIMATED_PRESERVEASPECTRATIO(SVGPatternElement, SVGNames::preserveAspectRatioAttr, PreserveAspectRatio, preserveAspectRatio) 
57
58 inline SVGPatternElement::SVGPatternElement(const QualifiedName& tagName, Document* document)
59     : SVGStyledElement(tagName, document)
60     , m_x(LengthModeWidth)
61     , m_y(LengthModeHeight)
62     , m_width(LengthModeWidth)
63     , m_height(LengthModeHeight)
64     , m_patternUnits(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
65     , m_patternContentUnits(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE)
66 {
67     ASSERT(hasTagName(SVGNames::patternTag));
68 }
69
70 PassRefPtr<SVGPatternElement> SVGPatternElement::create(const QualifiedName& tagName, Document* document)
71 {
72     return adoptRef(new SVGPatternElement(tagName, document));
73 }
74
75 void SVGPatternElement::parseMappedAttribute(Attribute* attr)
76 {
77     if (attr->name() == SVGNames::patternUnitsAttr) {
78         if (attr->value() == "userSpaceOnUse")
79             setPatternUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE);
80         else if (attr->value() == "objectBoundingBox")
81             setPatternUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
82     } else if (attr->name() == SVGNames::patternContentUnitsAttr) {
83         if (attr->value() == "userSpaceOnUse")
84             setPatternContentUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE);
85         else if (attr->value() == "objectBoundingBox")
86             setPatternContentUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
87     } else if (attr->name() == SVGNames::patternTransformAttr) {
88         SVGTransformList newList;
89         if (!SVGTransformable::parseTransformAttribute(newList, attr->value()))
90             newList.clear();
91
92         detachAnimatedPatternTransformListWrappers(newList.size());
93         setPatternTransformBaseValue(newList);
94     } else if (attr->name() == SVGNames::xAttr)
95         setXBaseValue(SVGLength(LengthModeWidth, attr->value()));
96     else if (attr->name() == SVGNames::yAttr)
97         setYBaseValue(SVGLength(LengthModeHeight, attr->value()));
98     else if (attr->name() == SVGNames::widthAttr) {
99         setWidthBaseValue(SVGLength(LengthModeWidth, attr->value()));
100         if (widthBaseValue().value(this) < 0.0)
101             document()->accessSVGExtensions()->reportError("A negative value for pattern attribute <width> is not allowed");
102     } else if (attr->name() == SVGNames::heightAttr) {
103         setHeightBaseValue(SVGLength(LengthModeHeight, attr->value()));
104         if (heightBaseValue().value(this) < 0.0)
105             document()->accessSVGExtensions()->reportError("A negative value for pattern attribute <height> is not allowed");
106     } else {
107         if (SVGURIReference::parseMappedAttribute(attr))
108             return;
109         if (SVGTests::parseMappedAttribute(attr))
110             return;
111         if (SVGLangSpace::parseMappedAttribute(attr))
112             return;
113         if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
114             return;
115         if (SVGFitToViewBox::parseMappedAttribute(document(), attr))
116             return;
117
118         SVGStyledElement::parseMappedAttribute(attr);
119     }
120 }
121
122 void SVGPatternElement::svgAttributeChanged(const QualifiedName& attrName)
123 {
124     SVGStyledElement::svgAttributeChanged(attrName);
125
126     bool invalidateClients = false;
127     if (attrName == SVGNames::xAttr
128         || attrName == SVGNames::yAttr
129         || attrName == SVGNames::widthAttr
130         || attrName == SVGNames::heightAttr) {
131         invalidateClients = true;
132         updateRelativeLengthsInformation();
133     }
134
135     RenderObject* object = renderer();
136     if (!object)
137         return;
138
139     if (invalidateClients
140         || attrName == SVGNames::patternUnitsAttr
141         || attrName == SVGNames::patternContentUnitsAttr
142         || attrName == SVGNames::patternTransformAttr
143         || SVGURIReference::isKnownAttribute(attrName)
144         || SVGTests::isKnownAttribute(attrName)
145         || SVGLangSpace::isKnownAttribute(attrName)
146         || SVGExternalResourcesRequired::isKnownAttribute(attrName)
147         || SVGFitToViewBox::isKnownAttribute(attrName)
148         || SVGStyledElement::isKnownAttribute(attrName))
149         object->setNeedsLayout(true);
150 }
151
152 void SVGPatternElement::synchronizeProperty(const QualifiedName& attrName)
153 {
154     SVGStyledElement::synchronizeProperty(attrName);
155
156     if (attrName == anyQName()) {
157         synchronizePatternUnits();
158         synchronizePatternContentUnits();
159         synchronizePatternTransform();
160         synchronizeX();
161         synchronizeY();
162         synchronizeWidth();
163         synchronizeHeight();
164         synchronizeExternalResourcesRequired();
165         synchronizeViewBox();
166         synchronizePreserveAspectRatio();
167         synchronizeHref();
168         SVGTests::synchronizeProperties(this, attrName);
169         return;
170     }
171
172     if (attrName == SVGNames::patternUnitsAttr)
173         synchronizePatternUnits();
174     else if (attrName == SVGNames::patternContentUnitsAttr)
175         synchronizePatternContentUnits();
176     else if (attrName == SVGNames::patternTransformAttr)
177         synchronizePatternTransform();
178     else if (attrName == SVGNames::xAttr)
179         synchronizeX();
180     else if (attrName == SVGNames::yAttr)
181         synchronizeY();
182     else if (attrName == SVGNames::widthAttr)
183         synchronizeWidth();
184     else if (attrName == SVGNames::heightAttr)
185         synchronizeHeight();
186     else if (SVGExternalResourcesRequired::isKnownAttribute(attrName))
187         synchronizeExternalResourcesRequired();
188     else if (attrName == SVGNames::viewBoxAttr)
189         synchronizeViewBox();
190     else if (attrName == SVGNames::preserveAspectRatioAttr)
191         synchronizePreserveAspectRatio();
192     else if (SVGURIReference::isKnownAttribute(attrName))
193         synchronizeHref();
194     else if (SVGTests::isKnownAttribute(attrName))
195         SVGTests::synchronizeProperties(this, attrName);
196 }
197
198 AttributeToPropertyTypeMap& SVGPatternElement::attributeToPropertyTypeMap()
199 {
200     DEFINE_STATIC_LOCAL(AttributeToPropertyTypeMap, s_attributeToPropertyTypeMap, ());
201     return s_attributeToPropertyTypeMap;
202 }
203
204 void SVGPatternElement::fillAttributeToPropertyTypeMap()
205 {
206     AttributeToPropertyTypeMap& attributeToPropertyTypeMap = this->attributeToPropertyTypeMap();
207
208     SVGStyledElement::fillPassedAttributeToPropertyTypeMap(attributeToPropertyTypeMap);
209     attributeToPropertyTypeMap.set(SVGNames::xAttr, AnimatedLength);
210     attributeToPropertyTypeMap.set(SVGNames::yAttr, AnimatedLength);
211     attributeToPropertyTypeMap.set(SVGNames::widthAttr, AnimatedLength);
212     attributeToPropertyTypeMap.set(SVGNames::heightAttr, AnimatedLength);
213     attributeToPropertyTypeMap.set(SVGNames::patternUnitsAttr, AnimatedEnumeration);
214     attributeToPropertyTypeMap.set(SVGNames::patternContentUnitsAttr, AnimatedEnumeration);
215     attributeToPropertyTypeMap.set(SVGNames::patternTransformAttr, AnimatedTransformList);
216     attributeToPropertyTypeMap.set(XLinkNames::hrefAttr, AnimatedString);
217     attributeToPropertyTypeMap.set(SVGNames::viewBoxAttr, AnimatedRect);
218     attributeToPropertyTypeMap.set(SVGNames::preserveAspectRatioAttr, AnimatedPreserveAspectRatio);
219 }
220
221 void SVGPatternElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
222 {
223     SVGStyledElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
224
225     if (changedByParser)
226         return;
227
228     if (RenderObject* object = renderer())
229         object->setNeedsLayout(true);
230 }
231
232 RenderObject* SVGPatternElement::createRenderer(RenderArena* arena, RenderStyle*)
233 {
234     return new (arena) RenderSVGResourcePattern(this);
235 }
236
237 void SVGPatternElement::collectPatternAttributes(PatternAttributes& attributes) const
238 {
239     HashSet<const SVGPatternElement*> processedPatterns;
240
241     const SVGPatternElement* current = this;
242     while (current) {
243         if (!attributes.hasX() && current->hasAttribute(SVGNames::xAttr))
244             attributes.setX(current->x());
245
246         if (!attributes.hasY() && current->hasAttribute(SVGNames::yAttr))
247             attributes.setY(current->y());
248
249         if (!attributes.hasWidth() && current->hasAttribute(SVGNames::widthAttr))
250             attributes.setWidth(current->width());
251
252         if (!attributes.hasHeight() && current->hasAttribute(SVGNames::heightAttr))
253             attributes.setHeight(current->height());
254
255         if (!attributes.hasViewBox() && current->hasAttribute(SVGNames::viewBoxAttr))
256             attributes.setViewBox(current->viewBox());
257
258         if (!attributes.hasPreserveAspectRatio() && current->hasAttribute(SVGNames::preserveAspectRatioAttr))
259             attributes.setPreserveAspectRatio(current->preserveAspectRatio());
260
261         if (!attributes.hasBoundingBoxMode() && current->hasAttribute(SVGNames::patternUnitsAttr))
262             attributes.setBoundingBoxMode(current->patternUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
263
264         if (!attributes.hasBoundingBoxModeContent() && current->hasAttribute(SVGNames::patternContentUnitsAttr))
265             attributes.setBoundingBoxModeContent(current->patternContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
266
267         if (!attributes.hasPatternTransform() && current->hasAttribute(SVGNames::patternTransformAttr)) {
268             AffineTransform transform;
269             current->patternTransform().concatenate(transform);
270             attributes.setPatternTransform(transform);
271         }
272
273         if (!attributes.hasPatternContentElement() && current->hasChildNodes())
274             attributes.setPatternContentElement(current);
275
276         processedPatterns.add(current);
277
278         // Respect xlink:href, take attributes from referenced element
279         Node* refNode = treeScope()->getElementById(SVGURIReference::getTarget(current->href()));
280         if (refNode && refNode->hasTagName(SVGNames::patternTag)) {
281             current = static_cast<const SVGPatternElement*>(const_cast<const Node*>(refNode));
282
283             // Cycle detection
284             if (processedPatterns.contains(current)) {
285                 current = 0;
286                 break;
287             }
288         } else
289             current = 0;
290     }
291 }
292
293 bool SVGPatternElement::selfHasRelativeLengths() const
294 {
295     return x().isRelative()
296         || y().isRelative()
297         || width().isRelative()
298         || height().isRelative();
299 }
300
301 }
302
303 #endif // ENABLE(SVG)