• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 WebCore {
33 
SVGPatternElement(Document & document)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     ScriptWrappable::init(this);
48 
49     addToPropertyMap(m_x);
50     addToPropertyMap(m_y);
51     addToPropertyMap(m_width);
52     addToPropertyMap(m_height);
53     addToPropertyMap(m_patternTransform);
54     addToPropertyMap(m_patternUnits);
55     addToPropertyMap(m_patternContentUnits);
56 }
57 
DEFINE_NODE_FACTORY(SVGPatternElement)58 DEFINE_NODE_FACTORY(SVGPatternElement)
59 
60 bool SVGPatternElement::isSupportedAttribute(const QualifiedName& attrName)
61 {
62     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
63     if (supportedAttributes.isEmpty()) {
64         SVGURIReference::addSupportedAttributes(supportedAttributes);
65         SVGTests::addSupportedAttributes(supportedAttributes);
66         SVGFitToViewBox::addSupportedAttributes(supportedAttributes);
67         supportedAttributes.add(SVGNames::patternUnitsAttr);
68         supportedAttributes.add(SVGNames::patternContentUnitsAttr);
69         supportedAttributes.add(SVGNames::patternTransformAttr);
70         supportedAttributes.add(SVGNames::xAttr);
71         supportedAttributes.add(SVGNames::yAttr);
72         supportedAttributes.add(SVGNames::widthAttr);
73         supportedAttributes.add(SVGNames::heightAttr);
74     }
75     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
76 }
77 
parseAttribute(const QualifiedName & name,const AtomicString & value)78 void SVGPatternElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
79 {
80     SVGParsingError parseError = NoError;
81 
82     if (!isSupportedAttribute(name)) {
83         SVGElement::parseAttribute(name, value);
84     } else if (name == SVGNames::patternUnitsAttr) {
85         m_patternUnits->setBaseValueAsString(value, parseError);
86     } else if (name == SVGNames::patternContentUnitsAttr) {
87         m_patternContentUnits->setBaseValueAsString(value, parseError);
88     } else if (name == SVGNames::patternTransformAttr) {
89         m_patternTransform->setBaseValueAsString(value, parseError);
90     } else if (name == SVGNames::xAttr) {
91         m_x->setBaseValueAsString(value, parseError);
92     } else if (name == SVGNames::yAttr) {
93         m_y->setBaseValueAsString(value, parseError);
94     } else if (name == SVGNames::widthAttr) {
95         m_width->setBaseValueAsString(value, parseError);
96     } else if (name == SVGNames::heightAttr) {
97         m_height->setBaseValueAsString(value, parseError);
98     } else if (SVGURIReference::parseAttribute(name, value, parseError)) {
99     } else if (SVGTests::parseAttribute(name, value)) {
100     } else if (SVGFitToViewBox::parseAttribute(name, value, document(), parseError)) {
101     } else {
102         ASSERT_NOT_REACHED();
103     }
104 
105     reportAttributeParsingError(parseError, name, value);
106 }
107 
svgAttributeChanged(const QualifiedName & attrName)108 void SVGPatternElement::svgAttributeChanged(const QualifiedName& attrName)
109 {
110     if (!isSupportedAttribute(attrName)) {
111         SVGElement::svgAttributeChanged(attrName);
112         return;
113     }
114 
115     SVGElement::InvalidationGuard invalidationGuard(this);
116 
117     if (attrName == SVGNames::xAttr
118         || attrName == SVGNames::yAttr
119         || attrName == SVGNames::widthAttr
120         || attrName == SVGNames::heightAttr)
121         updateRelativeLengthsInformation();
122 
123     RenderSVGResourceContainer* renderer = toRenderSVGResourceContainer(this->renderer());
124     if (renderer)
125         renderer->invalidateCacheAndMarkForLayout();
126 }
127 
childrenChanged(bool changedByParser,Node * beforeChange,Node * afterChange,int childCountDelta)128 void SVGPatternElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
129 {
130     SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
131 
132     if (changedByParser)
133         return;
134 
135     if (RenderObject* object = renderer())
136         object->setNeedsLayoutAndFullPaintInvalidation();
137 }
138 
createRenderer(RenderStyle *)139 RenderObject* SVGPatternElement::createRenderer(RenderStyle*)
140 {
141     return new RenderSVGResourcePattern(this);
142 }
143 
setPatternAttributes(const SVGPatternElement * element,PatternAttributes & attributes)144 static void setPatternAttributes(const SVGPatternElement* element, PatternAttributes& attributes)
145 {
146     if (!attributes.hasX() && element->x()->isSpecified())
147         attributes.setX(element->x()->currentValue());
148 
149     if (!attributes.hasY() && element->y()->isSpecified())
150         attributes.setY(element->y()->currentValue());
151 
152     if (!attributes.hasWidth() && element->width()->isSpecified())
153         attributes.setWidth(element->width()->currentValue());
154 
155     if (!attributes.hasHeight() && element->height()->isSpecified())
156         attributes.setHeight(element->height()->currentValue());
157 
158     if (!attributes.hasViewBox() && element->viewBox()->isSpecified() && element->viewBox()->currentValue()->isValid())
159         attributes.setViewBox(element->viewBox()->currentValue()->value());
160 
161     if (!attributes.hasPreserveAspectRatio() && element->preserveAspectRatio()->isSpecified())
162         attributes.setPreserveAspectRatio(element->preserveAspectRatio()->currentValue());
163 
164     if (!attributes.hasPatternUnits() && element->patternUnits()->isSpecified())
165         attributes.setPatternUnits(element->patternUnits()->currentValue()->enumValue());
166 
167     if (!attributes.hasPatternContentUnits() && element->patternContentUnits()->isSpecified())
168         attributes.setPatternContentUnits(element->patternContentUnits()->currentValue()->enumValue());
169 
170     if (!attributes.hasPatternTransform() && element->patternTransform()->isSpecified()) {
171         AffineTransform transform;
172         element->patternTransform()->currentValue()->concatenate(transform);
173         attributes.setPatternTransform(transform);
174     }
175 
176     if (!attributes.hasPatternContentElement() && ElementTraversal::firstWithin(*element))
177         attributes.setPatternContentElement(element);
178 }
179 
collectPatternAttributes(PatternAttributes & attributes) const180 void SVGPatternElement::collectPatternAttributes(PatternAttributes& attributes) const
181 {
182     HashSet<const SVGPatternElement*> processedPatterns;
183     const SVGPatternElement* current = this;
184 
185     while (true) {
186         setPatternAttributes(current, attributes);
187         processedPatterns.add(current);
188 
189         // Respect xlink:href, take attributes from referenced element
190         Node* refNode = SVGURIReference::targetElementFromIRIString(current->hrefString(), treeScope());
191         if (isSVGPatternElement(refNode)) {
192             current = toSVGPatternElement(refNode);
193 
194             // Cycle detection
195             if (processedPatterns.contains(current))
196                 return;
197         } else {
198             return;
199         }
200     }
201 
202     ASSERT_NOT_REACHED();
203 }
204 
localCoordinateSpaceTransform(SVGElement::CTMScope) const205 AffineTransform SVGPatternElement::localCoordinateSpaceTransform(SVGElement::CTMScope) const
206 {
207     AffineTransform matrix;
208     m_patternTransform->currentValue()->concatenate(matrix);
209     return matrix;
210 }
211 
selfHasRelativeLengths() const212 bool SVGPatternElement::selfHasRelativeLengths() const
213 {
214     return m_x->currentValue()->isRelative()
215         || m_y->currentValue()->isRelative()
216         || m_width->currentValue()->isRelative()
217         || m_height->currentValue()->isRelative();
218 }
219 
220 }
221