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.
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.
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.
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.
24 #include "core/svg/SVGPatternElement.h"
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"
34 inline SVGPatternElement::SVGPatternElement(Document& document)
35 : SVGElement(SVGNames::patternTag, document)
36 , SVGURIReference(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))
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);
56 DEFINE_NODE_FACTORY(SVGPatternElement)
58 bool SVGPatternElement::isSupportedAttribute(const QualifiedName& attrName)
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);
73 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
76 void SVGPatternElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
78 SVGParsingError parseError = NoError;
80 if (!isSupportedAttribute(name)) {
81 SVGElement::parseAttribute(name, value);
82 } else if (name == SVGNames::patternUnitsAttr) {
83 m_patternUnits->setBaseValueAsString(value, parseError);
84 } else if (name == SVGNames::patternContentUnitsAttr) {
85 m_patternContentUnits->setBaseValueAsString(value, parseError);
86 } else if (name == SVGNames::patternTransformAttr) {
87 m_patternTransform->setBaseValueAsString(value, parseError);
88 } else if (name == SVGNames::xAttr) {
89 m_x->setBaseValueAsString(value, parseError);
90 } else if (name == SVGNames::yAttr) {
91 m_y->setBaseValueAsString(value, parseError);
92 } else if (name == SVGNames::widthAttr) {
93 m_width->setBaseValueAsString(value, parseError);
94 } else if (name == SVGNames::heightAttr) {
95 m_height->setBaseValueAsString(value, parseError);
96 } else if (SVGURIReference::parseAttribute(name, value, parseError)) {
97 } else if (SVGTests::parseAttribute(name, value)) {
98 } else if (SVGFitToViewBox::parseAttribute(name, value, document(), parseError)) {
100 ASSERT_NOT_REACHED();
103 reportAttributeParsingError(parseError, name, value);
106 void SVGPatternElement::svgAttributeChanged(const QualifiedName& attrName)
108 if (!isSupportedAttribute(attrName)) {
109 SVGElement::svgAttributeChanged(attrName);
113 SVGElement::InvalidationGuard invalidationGuard(this);
115 if (attrName == SVGNames::xAttr
116 || attrName == SVGNames::yAttr
117 || attrName == SVGNames::widthAttr
118 || attrName == SVGNames::heightAttr)
119 updateRelativeLengthsInformation();
121 RenderSVGResourceContainer* renderer = toRenderSVGResourceContainer(this->renderer());
123 renderer->invalidateCacheAndMarkForLayout();
126 void SVGPatternElement::childrenChanged(const ChildrenChange& change)
128 SVGElement::childrenChanged(change);
133 if (RenderObject* object = renderer())
134 object->setNeedsLayoutAndFullPaintInvalidation();
137 RenderObject* SVGPatternElement::createRenderer(RenderStyle*)
139 return new RenderSVGResourcePattern(this);
142 static void setPatternAttributes(const SVGPatternElement* element, PatternAttributes& attributes)
144 if (!attributes.hasX() && element->x()->isSpecified())
145 attributes.setX(element->x()->currentValue());
147 if (!attributes.hasY() && element->y()->isSpecified())
148 attributes.setY(element->y()->currentValue());
150 if (!attributes.hasWidth() && element->width()->isSpecified())
151 attributes.setWidth(element->width()->currentValue());
153 if (!attributes.hasHeight() && element->height()->isSpecified())
154 attributes.setHeight(element->height()->currentValue());
156 if (!attributes.hasViewBox() && element->viewBox()->isSpecified() && element->viewBox()->currentValue()->isValid())
157 attributes.setViewBox(element->viewBox()->currentValue()->value());
159 if (!attributes.hasPreserveAspectRatio() && element->preserveAspectRatio()->isSpecified())
160 attributes.setPreserveAspectRatio(element->preserveAspectRatio()->currentValue());
162 if (!attributes.hasPatternUnits() && element->patternUnits()->isSpecified())
163 attributes.setPatternUnits(element->patternUnits()->currentValue()->enumValue());
165 if (!attributes.hasPatternContentUnits() && element->patternContentUnits()->isSpecified())
166 attributes.setPatternContentUnits(element->patternContentUnits()->currentValue()->enumValue());
168 if (!attributes.hasPatternTransform() && element->patternTransform()->isSpecified()) {
169 AffineTransform transform;
170 element->patternTransform()->currentValue()->concatenate(transform);
171 attributes.setPatternTransform(transform);
174 if (!attributes.hasPatternContentElement() && ElementTraversal::firstWithin(*element))
175 attributes.setPatternContentElement(element);
178 void SVGPatternElement::collectPatternAttributes(PatternAttributes& attributes) const
180 WillBeHeapHashSet<RawPtrWillBeMember<const SVGPatternElement> > processedPatterns;
181 const SVGPatternElement* current = this;
184 setPatternAttributes(current, attributes);
185 processedPatterns.add(current);
187 // Respect xlink:href, take attributes from referenced element
188 Node* refNode = SVGURIReference::targetElementFromIRIString(current->hrefString(), treeScope());
189 if (isSVGPatternElement(refNode)) {
190 current = toSVGPatternElement(refNode);
193 if (processedPatterns.contains(current))
200 ASSERT_NOT_REACHED();
203 AffineTransform SVGPatternElement::localCoordinateSpaceTransform(SVGElement::CTMScope) const
205 AffineTransform matrix;
206 m_patternTransform->currentValue()->concatenate(matrix);
210 bool SVGPatternElement::selfHasRelativeLengths() const
212 return m_x->currentValue()->isRelative()
213 || m_y->currentValue()->isRelative()
214 || m_width->currentValue()->isRelative()
215 || m_height->currentValue()->isRelative();