Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 #include "core/svg/SVGPatternElement.h"
25
26 #include "core/XLinkNames.h"
27 #include "core/dom/ElementTraversal.h"
28 #include "core/rendering/svg/RenderSVGResourcePattern.h"
29 #include "core/svg/PatternAttributes.h"
30 #include "platform/transforms/AffineTransform.h"
31
32 namespace blink {
33
34 inline SVGPatternElement::SVGPatternElement(Document& document)
35     : SVGElement(SVGNames::patternTag, document)
36     , SVGURIReference(this)
37     , SVGTests(this)
38     , SVGFitToViewBox(this)
39     , m_x(SVGAnimatedLength::create(this, SVGNames::xAttr, SVGLength::create(LengthModeWidth), AllowNegativeLengths))
40     , m_y(SVGAnimatedLength::create(this, SVGNames::yAttr, SVGLength::create(LengthModeHeight), AllowNegativeLengths))
41     , m_width(SVGAnimatedLength::create(this, SVGNames::widthAttr, SVGLength::create(LengthModeWidth), ForbidNegativeLengths))
42     , m_height(SVGAnimatedLength::create(this, SVGNames::heightAttr, SVGLength::create(LengthModeHeight), ForbidNegativeLengths))
43     , m_patternTransform(SVGAnimatedTransformList::create(this, SVGNames::patternTransformAttr, SVGTransformList::create()))
44     , m_patternUnits(SVGAnimatedEnumeration<SVGUnitTypes::SVGUnitType>::create(this, SVGNames::patternUnitsAttr, SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX))
45     , m_patternContentUnits(SVGAnimatedEnumeration<SVGUnitTypes::SVGUnitType>::create(this, SVGNames::patternContentUnitsAttr, SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE))
46 {
47     addToPropertyMap(m_x);
48     addToPropertyMap(m_y);
49     addToPropertyMap(m_width);
50     addToPropertyMap(m_height);
51     addToPropertyMap(m_patternTransform);
52     addToPropertyMap(m_patternUnits);
53     addToPropertyMap(m_patternContentUnits);
54 }
55
56 DEFINE_NODE_FACTORY(SVGPatternElement)
57
58 bool SVGPatternElement::isSupportedAttribute(const QualifiedName& attrName)
59 {
60     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
61     if (supportedAttributes.isEmpty()) {
62         SVGURIReference::addSupportedAttributes(supportedAttributes);
63         SVGTests::addSupportedAttributes(supportedAttributes);
64         SVGFitToViewBox::addSupportedAttributes(supportedAttributes);
65         supportedAttributes.add(SVGNames::patternUnitsAttr);
66         supportedAttributes.add(SVGNames::patternContentUnitsAttr);
67         supportedAttributes.add(SVGNames::patternTransformAttr);
68         supportedAttributes.add(SVGNames::xAttr);
69         supportedAttributes.add(SVGNames::yAttr);
70         supportedAttributes.add(SVGNames::widthAttr);
71         supportedAttributes.add(SVGNames::heightAttr);
72     }
73     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
74 }
75
76 void SVGPatternElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
77 {
78     parseAttributeNew(name, value);
79 }
80
81 void SVGPatternElement::svgAttributeChanged(const QualifiedName& attrName)
82 {
83     if (!isSupportedAttribute(attrName)) {
84         SVGElement::svgAttributeChanged(attrName);
85         return;
86     }
87
88     SVGElement::InvalidationGuard invalidationGuard(this);
89
90     if (attrName == SVGNames::xAttr
91         || attrName == SVGNames::yAttr
92         || attrName == SVGNames::widthAttr
93         || attrName == SVGNames::heightAttr)
94         updateRelativeLengthsInformation();
95
96     RenderSVGResourceContainer* renderer = toRenderSVGResourceContainer(this->renderer());
97     if (renderer)
98         renderer->invalidateCacheAndMarkForLayout();
99 }
100
101 void SVGPatternElement::childrenChanged(const ChildrenChange& change)
102 {
103     SVGElement::childrenChanged(change);
104
105     if (change.byParser)
106         return;
107
108     if (RenderObject* object = renderer())
109         object->setNeedsLayoutAndFullPaintInvalidation();
110 }
111
112 RenderObject* SVGPatternElement::createRenderer(RenderStyle*)
113 {
114     return new RenderSVGResourcePattern(this);
115 }
116
117 static void setPatternAttributes(const SVGPatternElement* element, PatternAttributes& attributes)
118 {
119     if (!attributes.hasX() && element->x()->isSpecified())
120         attributes.setX(element->x()->currentValue());
121
122     if (!attributes.hasY() && element->y()->isSpecified())
123         attributes.setY(element->y()->currentValue());
124
125     if (!attributes.hasWidth() && element->width()->isSpecified())
126         attributes.setWidth(element->width()->currentValue());
127
128     if (!attributes.hasHeight() && element->height()->isSpecified())
129         attributes.setHeight(element->height()->currentValue());
130
131     if (!attributes.hasViewBox() && element->viewBox()->isSpecified() && element->viewBox()->currentValue()->isValid())
132         attributes.setViewBox(element->viewBox()->currentValue()->value());
133
134     if (!attributes.hasPreserveAspectRatio() && element->preserveAspectRatio()->isSpecified())
135         attributes.setPreserveAspectRatio(element->preserveAspectRatio()->currentValue());
136
137     if (!attributes.hasPatternUnits() && element->patternUnits()->isSpecified())
138         attributes.setPatternUnits(element->patternUnits()->currentValue()->enumValue());
139
140     if (!attributes.hasPatternContentUnits() && element->patternContentUnits()->isSpecified())
141         attributes.setPatternContentUnits(element->patternContentUnits()->currentValue()->enumValue());
142
143     if (!attributes.hasPatternTransform() && element->patternTransform()->isSpecified()) {
144         AffineTransform transform;
145         element->patternTransform()->currentValue()->concatenate(transform);
146         attributes.setPatternTransform(transform);
147     }
148
149     if (!attributes.hasPatternContentElement() && ElementTraversal::firstWithin(*element))
150         attributes.setPatternContentElement(element);
151 }
152
153 void SVGPatternElement::collectPatternAttributes(PatternAttributes& attributes) const
154 {
155     WillBeHeapHashSet<RawPtrWillBeMember<const SVGPatternElement> > processedPatterns;
156     const SVGPatternElement* current = this;
157
158     while (true) {
159         setPatternAttributes(current, attributes);
160         processedPatterns.add(current);
161
162         // Respect xlink:href, take attributes from referenced element
163         Node* refNode = SVGURIReference::targetElementFromIRIString(current->hrefString(), treeScope());
164         if (isSVGPatternElement(refNode)) {
165             current = toSVGPatternElement(refNode);
166
167             // Cycle detection
168             if (processedPatterns.contains(current))
169                 return;
170         } else {
171             return;
172         }
173     }
174
175     ASSERT_NOT_REACHED();
176 }
177
178 AffineTransform SVGPatternElement::localCoordinateSpaceTransform(SVGElement::CTMScope) const
179 {
180     AffineTransform matrix;
181     m_patternTransform->currentValue()->concatenate(matrix);
182     return matrix;
183 }
184
185 bool SVGPatternElement::selfHasRelativeLengths() const
186 {
187     return m_x->currentValue()->isRelative()
188         || m_y->currentValue()->isRelative()
189         || m_width->currentValue()->isRelative()
190         || m_height->currentValue()->isRelative();
191 }
192
193 } // namespace blink