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 parseAttributeNew(name, value);
81 void SVGPatternElement::svgAttributeChanged(const QualifiedName& attrName)
83 if (!isSupportedAttribute(attrName)) {
84 SVGElement::svgAttributeChanged(attrName);
88 SVGElement::InvalidationGuard invalidationGuard(this);
90 if (attrName == SVGNames::xAttr
91 || attrName == SVGNames::yAttr
92 || attrName == SVGNames::widthAttr
93 || attrName == SVGNames::heightAttr)
94 updateRelativeLengthsInformation();
96 RenderSVGResourceContainer* renderer = toRenderSVGResourceContainer(this->renderer());
98 renderer->invalidateCacheAndMarkForLayout();
101 void SVGPatternElement::childrenChanged(const ChildrenChange& change)
103 SVGElement::childrenChanged(change);
108 if (RenderObject* object = renderer())
109 object->setNeedsLayoutAndFullPaintInvalidation();
112 RenderObject* SVGPatternElement::createRenderer(RenderStyle*)
114 return new RenderSVGResourcePattern(this);
117 static void setPatternAttributes(const SVGPatternElement* element, PatternAttributes& attributes)
119 if (!attributes.hasX() && element->x()->isSpecified())
120 attributes.setX(element->x()->currentValue());
122 if (!attributes.hasY() && element->y()->isSpecified())
123 attributes.setY(element->y()->currentValue());
125 if (!attributes.hasWidth() && element->width()->isSpecified())
126 attributes.setWidth(element->width()->currentValue());
128 if (!attributes.hasHeight() && element->height()->isSpecified())
129 attributes.setHeight(element->height()->currentValue());
131 if (!attributes.hasViewBox() && element->viewBox()->isSpecified() && element->viewBox()->currentValue()->isValid())
132 attributes.setViewBox(element->viewBox()->currentValue()->value());
134 if (!attributes.hasPreserveAspectRatio() && element->preserveAspectRatio()->isSpecified())
135 attributes.setPreserveAspectRatio(element->preserveAspectRatio()->currentValue());
137 if (!attributes.hasPatternUnits() && element->patternUnits()->isSpecified())
138 attributes.setPatternUnits(element->patternUnits()->currentValue()->enumValue());
140 if (!attributes.hasPatternContentUnits() && element->patternContentUnits()->isSpecified())
141 attributes.setPatternContentUnits(element->patternContentUnits()->currentValue()->enumValue());
143 if (!attributes.hasPatternTransform() && element->patternTransform()->isSpecified()) {
144 AffineTransform transform;
145 element->patternTransform()->currentValue()->concatenate(transform);
146 attributes.setPatternTransform(transform);
149 if (!attributes.hasPatternContentElement() && ElementTraversal::firstWithin(*element))
150 attributes.setPatternContentElement(element);
153 void SVGPatternElement::collectPatternAttributes(PatternAttributes& attributes) const
155 WillBeHeapHashSet<RawPtrWillBeMember<const SVGPatternElement> > processedPatterns;
156 const SVGPatternElement* current = this;
159 setPatternAttributes(current, attributes);
160 processedPatterns.add(current);
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);
168 if (processedPatterns.contains(current))
175 ASSERT_NOT_REACHED();
178 AffineTransform SVGPatternElement::localCoordinateSpaceTransform(SVGElement::CTMScope) const
180 AffineTransform matrix;
181 m_patternTransform->currentValue()->concatenate(matrix);
185 bool SVGPatternElement::selfHasRelativeLengths() const
187 return m_x->currentValue()->isRelative()
188 || m_y->currentValue()->isRelative()
189 || m_width->currentValue()->isRelative()
190 || m_height->currentValue()->isRelative();