Replace WTF::move with WTFMove
[WebKit-https.git] / Source / WebCore / 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  * Copyright (C) 2014 Adobe Systems Incorporated. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include "config.h"
24 #include "SVGPatternElement.h"
25
26 #include "AffineTransform.h"
27 #include "Document.h"
28 #include "FloatConversion.h"
29 #include "GraphicsContext.h"
30 #include "ImageBuffer.h"
31 #include "PatternAttributes.h"
32 #include "RenderSVGContainer.h"
33 #include "RenderSVGResourcePattern.h"
34 #include "SVGFitToViewBox.h"
35 #include "SVGGraphicsElement.h"
36 #include "SVGNames.h"
37 #include "SVGRenderSupport.h"
38 #include "SVGTransformable.h"
39 #include "XLinkNames.h"
40 #include <wtf/NeverDestroyed.h>
41
42 namespace WebCore {
43
44 // Animated property definitions
45 DEFINE_ANIMATED_LENGTH(SVGPatternElement, SVGNames::xAttr, X, x)
46 DEFINE_ANIMATED_LENGTH(SVGPatternElement, SVGNames::yAttr, Y, y)
47 DEFINE_ANIMATED_LENGTH(SVGPatternElement, SVGNames::widthAttr, Width, width)
48 DEFINE_ANIMATED_LENGTH(SVGPatternElement, SVGNames::heightAttr, Height, height)
49 DEFINE_ANIMATED_ENUMERATION(SVGPatternElement, SVGNames::patternUnitsAttr, PatternUnits, patternUnits, SVGUnitTypes::SVGUnitType)
50 DEFINE_ANIMATED_ENUMERATION(SVGPatternElement, SVGNames::patternContentUnitsAttr, PatternContentUnits, patternContentUnits, SVGUnitTypes::SVGUnitType)
51 DEFINE_ANIMATED_TRANSFORM_LIST(SVGPatternElement, SVGNames::patternTransformAttr, PatternTransform, patternTransform) 
52 DEFINE_ANIMATED_STRING(SVGPatternElement, XLinkNames::hrefAttr, Href, href)
53 DEFINE_ANIMATED_BOOLEAN(SVGPatternElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
54 DEFINE_ANIMATED_RECT(SVGPatternElement, SVGNames::viewBoxAttr, ViewBox, viewBox)
55 DEFINE_ANIMATED_PRESERVEASPECTRATIO(SVGPatternElement, SVGNames::preserveAspectRatioAttr, PreserveAspectRatio, preserveAspectRatio) 
56
57 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGPatternElement)
58     REGISTER_LOCAL_ANIMATED_PROPERTY(x)
59     REGISTER_LOCAL_ANIMATED_PROPERTY(y)
60     REGISTER_LOCAL_ANIMATED_PROPERTY(width)
61     REGISTER_LOCAL_ANIMATED_PROPERTY(height)
62     REGISTER_LOCAL_ANIMATED_PROPERTY(patternUnits)
63     REGISTER_LOCAL_ANIMATED_PROPERTY(patternContentUnits)
64     REGISTER_LOCAL_ANIMATED_PROPERTY(patternTransform)
65     REGISTER_LOCAL_ANIMATED_PROPERTY(href)
66     REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
67     REGISTER_LOCAL_ANIMATED_PROPERTY(viewBox)
68     REGISTER_LOCAL_ANIMATED_PROPERTY(preserveAspectRatio) 
69     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGElement)
70     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGTests)
71 END_REGISTER_ANIMATED_PROPERTIES
72
73 inline SVGPatternElement::SVGPatternElement(const QualifiedName& tagName, Document& document)
74     : SVGElement(tagName, document)
75     , m_x(LengthModeWidth)
76     , m_y(LengthModeHeight)
77     , m_width(LengthModeWidth)
78     , m_height(LengthModeHeight)
79     , m_patternUnits(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
80     , m_patternContentUnits(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE)
81 {
82     ASSERT(hasTagName(SVGNames::patternTag));
83     registerAnimatedPropertiesForSVGPatternElement();
84 }
85
86 Ref<SVGPatternElement> SVGPatternElement::create(const QualifiedName& tagName, Document& document)
87 {
88     return adoptRef(*new SVGPatternElement(tagName, document));
89 }
90
91 bool SVGPatternElement::isSupportedAttribute(const QualifiedName& attrName)
92 {
93     static NeverDestroyed<HashSet<QualifiedName>> supportedAttributes;
94     if (supportedAttributes.get().isEmpty()) {
95         SVGURIReference::addSupportedAttributes(supportedAttributes);
96         SVGTests::addSupportedAttributes(supportedAttributes);
97         SVGLangSpace::addSupportedAttributes(supportedAttributes);
98         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
99         SVGFitToViewBox::addSupportedAttributes(supportedAttributes);
100         supportedAttributes.get().add(SVGNames::patternUnitsAttr);
101         supportedAttributes.get().add(SVGNames::patternContentUnitsAttr);
102         supportedAttributes.get().add(SVGNames::patternTransformAttr);
103         supportedAttributes.get().add(SVGNames::xAttr);
104         supportedAttributes.get().add(SVGNames::yAttr);
105         supportedAttributes.get().add(SVGNames::widthAttr);
106         supportedAttributes.get().add(SVGNames::heightAttr);
107     }
108     return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
109 }
110
111 void SVGPatternElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
112 {
113     if (name == SVGNames::patternUnitsAttr) {
114         auto propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(value);
115         if (propertyValue > 0)
116             setPatternUnitsBaseValue(propertyValue);
117         return;
118     }
119     if (name == SVGNames::patternContentUnitsAttr) {
120         auto propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(value);
121         if (propertyValue > 0)
122             setPatternContentUnitsBaseValue(propertyValue);
123         return;
124     }
125     if (name == SVGNames::patternTransformAttr) {
126         SVGTransformList newList;
127         newList.parse(value);
128         detachAnimatedPatternTransformListWrappers(newList.size());
129         setPatternTransformBaseValue(newList);
130         return;
131     }
132
133     SVGParsingError parseError = NoError;
134
135     if (name == SVGNames::xAttr)
136         setXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
137     else if (name == SVGNames::yAttr)
138         setYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
139     else if (name == SVGNames::widthAttr)
140         setWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError, ForbidNegativeLengths));
141     else if (name == SVGNames::heightAttr)
142         setHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError, ForbidNegativeLengths));
143
144     reportAttributeParsingError(parseError, name, value);
145
146     SVGElement::parseAttribute(name, value);
147     SVGURIReference::parseAttribute(name, value);
148     SVGTests::parseAttribute(name, value);
149     SVGExternalResourcesRequired::parseAttribute(name, value);
150     SVGFitToViewBox::parseAttribute(this, name, value);
151 }
152
153 void SVGPatternElement::svgAttributeChanged(const QualifiedName& attrName)
154 {
155     if (!isSupportedAttribute(attrName)) {
156         SVGElement::svgAttributeChanged(attrName);
157         return;
158     }
159
160     InstanceInvalidationGuard guard(*this);
161
162     if (attrName == SVGNames::xAttr
163         || attrName == SVGNames::yAttr
164         || attrName == SVGNames::widthAttr
165         || attrName == SVGNames::heightAttr) {
166         invalidateSVGPresentationAttributeStyle();
167         return;
168     }
169
170     if (RenderObject* object = renderer())
171         object->setNeedsLayout();
172 }
173
174 void SVGPatternElement::childrenChanged(const ChildChange& change)
175 {
176     SVGElement::childrenChanged(change);
177
178     if (change.source == ChildChangeSourceParser)
179         return;
180
181     if (RenderObject* object = renderer())
182         object->setNeedsLayout();
183 }
184
185 RenderPtr<RenderElement> SVGPatternElement::createElementRenderer(Ref<RenderStyle>&& style, const RenderTreePosition&)
186 {
187     return createRenderer<RenderSVGResourcePattern>(*this, WTFMove(style));
188 }
189
190 void SVGPatternElement::collectPatternAttributes(PatternAttributes& attributes) const
191 {
192     if (!attributes.hasX() && hasAttribute(SVGNames::xAttr))
193         attributes.setX(x());
194
195     if (!attributes.hasY() && hasAttribute(SVGNames::yAttr))
196         attributes.setY(y());
197
198     if (!attributes.hasWidth() && hasAttribute(SVGNames::widthAttr))
199         attributes.setWidth(width());
200
201     if (!attributes.hasHeight() && hasAttribute(SVGNames::heightAttr))
202         attributes.setHeight(height());
203
204     if (!attributes.hasViewBox() && hasAttribute(SVGNames::viewBoxAttr) && viewBoxIsValid())
205         attributes.setViewBox(viewBox());
206
207     if (!attributes.hasPreserveAspectRatio() && hasAttribute(SVGNames::preserveAspectRatioAttr))
208         attributes.setPreserveAspectRatio(preserveAspectRatio());
209
210     if (!attributes.hasPatternUnits() && hasAttribute(SVGNames::patternUnitsAttr))
211         attributes.setPatternUnits(patternUnits());
212
213     if (!attributes.hasPatternContentUnits() && hasAttribute(SVGNames::patternContentUnitsAttr))
214         attributes.setPatternContentUnits(patternContentUnits());
215
216     if (!attributes.hasPatternTransform() && hasAttribute(SVGNames::patternTransformAttr)) {
217         AffineTransform transform;
218         patternTransform().concatenate(transform);
219         attributes.setPatternTransform(transform);
220     }
221
222     if (!attributes.hasPatternContentElement() && childElementCount())
223         attributes.setPatternContentElement(this);
224 }
225
226 AffineTransform SVGPatternElement::localCoordinateSpaceTransform(SVGLocatable::CTMScope) const
227 {
228     AffineTransform matrix;
229     patternTransform().concatenate(matrix);
230     return matrix;
231 }
232
233 }