Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / svg / SVGPatternElement.cpp
blob4fe6c38eb3ed9439b9439bb10300f113c9bd5026
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.
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.
22 #include "config.h"
23 #include "core/svg/SVGPatternElement.h"
25 #include "core/XLinkNames.h"
26 #include "core/dom/ElementTraversal.h"
27 #include "core/layout/svg/LayoutSVGResourcePattern.h"
28 #include "core/svg/PatternAttributes.h"
29 #include "platform/transforms/AffineTransform.h"
31 namespace blink {
33 inline SVGPatternElement::SVGPatternElement(Document& document)
34 : SVGElement(SVGNames::patternTag, document)
35 , SVGURIReference(this)
36 , SVGTests(this)
37 , SVGFitToViewBox(this)
38 , m_x(SVGAnimatedLength::create(this, SVGNames::xAttr, SVGLength::create(SVGLengthMode::Width), AllowNegativeLengths))
39 , m_y(SVGAnimatedLength::create(this, SVGNames::yAttr, SVGLength::create(SVGLengthMode::Height), AllowNegativeLengths))
40 , m_width(SVGAnimatedLength::create(this, SVGNames::widthAttr, SVGLength::create(SVGLengthMode::Width), ForbidNegativeLengths))
41 , m_height(SVGAnimatedLength::create(this, SVGNames::heightAttr, SVGLength::create(SVGLengthMode::Height), ForbidNegativeLengths))
42 , m_patternTransform(SVGAnimatedTransformList::create(this, SVGNames::patternTransformAttr, SVGTransformList::create()))
43 , m_patternUnits(SVGAnimatedEnumeration<SVGUnitTypes::SVGUnitType>::create(this, SVGNames::patternUnitsAttr, SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX))
44 , m_patternContentUnits(SVGAnimatedEnumeration<SVGUnitTypes::SVGUnitType>::create(this, SVGNames::patternContentUnitsAttr, SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE))
46 addToPropertyMap(m_x);
47 addToPropertyMap(m_y);
48 addToPropertyMap(m_width);
49 addToPropertyMap(m_height);
50 addToPropertyMap(m_patternTransform);
51 addToPropertyMap(m_patternUnits);
52 addToPropertyMap(m_patternContentUnits);
55 DEFINE_TRACE(SVGPatternElement)
57 visitor->trace(m_x);
58 visitor->trace(m_y);
59 visitor->trace(m_width);
60 visitor->trace(m_height);
61 visitor->trace(m_patternTransform);
62 visitor->trace(m_patternUnits);
63 visitor->trace(m_patternContentUnits);
64 SVGElement::trace(visitor);
65 SVGURIReference::trace(visitor);
66 SVGTests::trace(visitor);
67 SVGFitToViewBox::trace(visitor);
70 DEFINE_NODE_FACTORY(SVGPatternElement)
72 void SVGPatternElement::svgAttributeChanged(const QualifiedName& attrName)
74 bool isLengthAttr = attrName == SVGNames::xAttr
75 || attrName == SVGNames::yAttr
76 || attrName == SVGNames::widthAttr
77 || attrName == SVGNames::heightAttr;
79 if (isLengthAttr
80 || attrName == SVGNames::patternUnitsAttr
81 || attrName == SVGNames::patternContentUnitsAttr
82 || attrName == SVGNames::patternTransformAttr
83 || SVGFitToViewBox::isKnownAttribute(attrName)
84 || SVGURIReference::isKnownAttribute(attrName)
85 || SVGTests::isKnownAttribute(attrName)) {
86 SVGElement::InvalidationGuard invalidationGuard(this);
88 if (isLengthAttr)
89 updateRelativeLengthsInformation();
91 LayoutSVGResourceContainer* layoutObject = toLayoutSVGResourceContainer(this->layoutObject());
92 if (layoutObject)
93 layoutObject->invalidateCacheAndMarkForLayout();
95 return;
98 SVGElement::svgAttributeChanged(attrName);
101 void SVGPatternElement::childrenChanged(const ChildrenChange& change)
103 SVGElement::childrenChanged(change);
105 if (change.byParser)
106 return;
108 if (LayoutObject* object = layoutObject())
109 object->setNeedsLayoutAndFullPaintInvalidation(LayoutInvalidationReason::ChildChanged);
112 LayoutObject* SVGPatternElement::createLayoutObject(const ComputedStyle&)
114 return new LayoutSVGResourcePattern(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;
158 while (true) {
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());
165 // Only consider attached SVG pattern elements.
166 if (!isSVGPatternElement(refNode) || !refNode->layoutObject())
167 break;
169 current = toSVGPatternElement(refNode);
171 // Cycle detection
172 if (processedPatterns.contains(current))
173 break;
177 AffineTransform SVGPatternElement::localCoordinateSpaceTransform(SVGElement::CTMScope) const
179 AffineTransform matrix;
180 m_patternTransform->currentValue()->concatenate(matrix);
181 return matrix;
184 bool SVGPatternElement::selfHasRelativeLengths() const
186 return m_x->currentValue()->isRelative()
187 || m_y->currentValue()->isRelative()
188 || m_width->currentValue()->isRelative()
189 || m_height->currentValue()->isRelative();
192 } // namespace blink