Replace WTF::move with WTFMove
[WebKit-https.git] / Source / WebCore / svg / SVGMarkerElement.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 2009-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 #include "SVGMarkerElement.h"
24
25 #include "RenderSVGResourceMarker.h"
26 #include "SVGFitToViewBox.h"
27 #include "SVGNames.h"
28 #include <wtf/NeverDestroyed.h>
29
30 namespace WebCore {
31  
32 // Define custom animated property 'orientType'.
33 const SVGPropertyInfo* SVGMarkerElement::orientTypePropertyInfo()
34 {
35     static const SVGPropertyInfo* s_propertyInfo = 0;
36     if (!s_propertyInfo) {
37         s_propertyInfo = new SVGPropertyInfo(AnimatedEnumeration,
38                                              PropertyIsReadWrite,
39                                              SVGNames::orientAttr,
40                                              orientTypeIdentifier(),
41                                              &SVGMarkerElement::synchronizeOrientType,
42                                              &SVGMarkerElement::lookupOrCreateOrientTypeWrapper);
43     }
44     return s_propertyInfo;
45 }
46
47 // Animated property definitions
48 DEFINE_ANIMATED_LENGTH(SVGMarkerElement, SVGNames::refXAttr, RefX, refX)
49 DEFINE_ANIMATED_LENGTH(SVGMarkerElement, SVGNames::refYAttr, RefY, refY)
50 DEFINE_ANIMATED_LENGTH(SVGMarkerElement, SVGNames::markerWidthAttr, MarkerWidth, markerWidth)
51 DEFINE_ANIMATED_LENGTH(SVGMarkerElement, SVGNames::markerHeightAttr, MarkerHeight, markerHeight)
52 DEFINE_ANIMATED_ENUMERATION(SVGMarkerElement, SVGNames::markerUnitsAttr, MarkerUnits, markerUnits, SVGMarkerUnitsType)
53 DEFINE_ANIMATED_ANGLE_AND_ENUMERATION(SVGMarkerElement, SVGNames::orientAttr, orientAngleIdentifier(), OrientAngle, orientAngle)
54 DEFINE_ANIMATED_BOOLEAN(SVGMarkerElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
55 DEFINE_ANIMATED_RECT(SVGMarkerElement, SVGNames::viewBoxAttr, ViewBox, viewBox)
56 DEFINE_ANIMATED_PRESERVEASPECTRATIO(SVGMarkerElement, SVGNames::preserveAspectRatioAttr, PreserveAspectRatio, preserveAspectRatio)
57
58 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGMarkerElement)
59     REGISTER_LOCAL_ANIMATED_PROPERTY(refX)
60     REGISTER_LOCAL_ANIMATED_PROPERTY(refY)
61     REGISTER_LOCAL_ANIMATED_PROPERTY(markerWidth)
62     REGISTER_LOCAL_ANIMATED_PROPERTY(markerHeight)
63     REGISTER_LOCAL_ANIMATED_PROPERTY(markerUnits)
64     REGISTER_LOCAL_ANIMATED_PROPERTY(orientAngle)
65     REGISTER_LOCAL_ANIMATED_PROPERTY(orientType)
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 END_REGISTER_ANIMATED_PROPERTIES
71
72 inline SVGMarkerElement::SVGMarkerElement(const QualifiedName& tagName, Document& document)
73     : SVGElement(tagName, document)
74     , m_refX(LengthModeWidth)
75     , m_refY(LengthModeHeight)
76     , m_markerWidth(LengthModeWidth, "3")
77     , m_markerHeight(LengthModeHeight, "3") 
78     , m_markerUnits(SVGMarkerUnitsStrokeWidth)
79     , m_orientType(SVGMarkerOrientAngle)
80 {
81     // Spec: If the markerWidth/markerHeight attribute is not specified, the effect is as if a value of "3" were specified.
82     ASSERT(hasTagName(SVGNames::markerTag));
83     registerAnimatedPropertiesForSVGMarkerElement();
84 }
85
86 Ref<SVGMarkerElement> SVGMarkerElement::create(const QualifiedName& tagName, Document& document)
87 {
88     return adoptRef(*new SVGMarkerElement(tagName, document));
89 }
90
91 const AtomicString& SVGMarkerElement::orientTypeIdentifier()
92 {
93     DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGOrientType", AtomicString::ConstructFromLiteral));
94     return s_identifier;
95 }
96
97 const AtomicString& SVGMarkerElement::orientAngleIdentifier()
98 {
99     DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGOrientAngle", AtomicString::ConstructFromLiteral));
100     return s_identifier;
101 }
102
103 AffineTransform SVGMarkerElement::viewBoxToViewTransform(float viewWidth, float viewHeight) const
104 {
105     return SVGFitToViewBox::viewBoxToViewTransform(viewBox(), preserveAspectRatio(), viewWidth, viewHeight);
106 }
107
108 bool SVGMarkerElement::isSupportedAttribute(const QualifiedName& attrName)
109 {
110     static NeverDestroyed<HashSet<QualifiedName>> supportedAttributes;
111     if (supportedAttributes.get().isEmpty()) {
112         SVGLangSpace::addSupportedAttributes(supportedAttributes);
113         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
114         SVGFitToViewBox::addSupportedAttributes(supportedAttributes);
115         supportedAttributes.get().add(SVGNames::markerUnitsAttr);
116         supportedAttributes.get().add(SVGNames::refXAttr);
117         supportedAttributes.get().add(SVGNames::refYAttr);
118         supportedAttributes.get().add(SVGNames::markerWidthAttr);
119         supportedAttributes.get().add(SVGNames::markerHeightAttr);
120         supportedAttributes.get().add(SVGNames::orientAttr);
121     }
122     return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
123 }
124
125 void SVGMarkerElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
126 {
127     if (name == SVGNames::markerUnitsAttr) {
128         auto propertyValue = SVGPropertyTraits<SVGMarkerUnitsType>::fromString(value);
129         if (propertyValue > 0)
130             setMarkerUnitsBaseValue(propertyValue);
131         return;
132     }
133
134     if (name == SVGNames::orientAttr) {
135         SVGAngle angle;
136         auto orientType = SVGPropertyTraits<SVGMarkerOrientType>::fromString(value, angle);
137         if (orientType > 0)
138             setOrientTypeBaseValue(orientType);
139         if (orientType == SVGMarkerOrientAngle)
140             setOrientAngleBaseValue(angle);
141         return;
142     }
143
144     SVGParsingError parseError = NoError;
145
146     if (name == SVGNames::refXAttr)
147         setRefXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
148     else if (name == SVGNames::refYAttr)
149         setRefYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
150     else if (name == SVGNames::markerWidthAttr)
151         setMarkerWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
152     else if (name == SVGNames::markerHeightAttr)
153         setMarkerHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
154
155     reportAttributeParsingError(parseError, name, value);
156
157     SVGElement::parseAttribute(name, value);
158     SVGExternalResourcesRequired::parseAttribute(name, value);
159     SVGFitToViewBox::parseAttribute(this, name, value);
160 }
161
162 void SVGMarkerElement::svgAttributeChanged(const QualifiedName& attrName)
163 {
164     if (!isSupportedAttribute(attrName)) {
165         SVGElement::svgAttributeChanged(attrName);
166         return;
167     }
168
169     InstanceInvalidationGuard guard(*this);
170     
171     if (attrName == SVGNames::refXAttr
172         || attrName == SVGNames::refYAttr
173         || attrName == SVGNames::markerWidthAttr
174         || attrName == SVGNames::markerHeightAttr)
175         updateRelativeLengthsInformation();
176
177     if (RenderObject* object = renderer())
178         object->setNeedsLayout();
179 }
180
181 void SVGMarkerElement::childrenChanged(const ChildChange& change)
182 {
183     SVGElement::childrenChanged(change);
184
185     if (change.source == ChildChangeSourceParser)
186         return;
187
188     if (RenderObject* object = renderer())
189         object->setNeedsLayout();
190 }
191
192 void SVGMarkerElement::setOrientToAuto()
193 {
194     setOrientTypeBaseValue(SVGMarkerOrientAuto);
195     setOrientAngleBaseValue(SVGAngle());
196  
197     // Mark orientAttr dirty - the next XML DOM access of that attribute kicks in synchronization.
198     m_orientAngle.shouldSynchronize = true;
199     m_orientType.shouldSynchronize = true;
200     invalidateSVGAttributes();
201     svgAttributeChanged(orientAnglePropertyInfo()->attributeName);
202 }
203
204 void SVGMarkerElement::setOrientToAngle(const SVGAngle& angle)
205 {
206     setOrientTypeBaseValue(SVGMarkerOrientAngle);
207     setOrientAngleBaseValue(angle);
208
209     // Mark orientAttr dirty - the next XML DOM access of that attribute kicks in synchronization.
210     m_orientAngle.shouldSynchronize = true;
211     m_orientType.shouldSynchronize = true;
212     invalidateSVGAttributes();
213     svgAttributeChanged(orientAnglePropertyInfo()->attributeName);
214 }
215
216 RenderPtr<RenderElement> SVGMarkerElement::createElementRenderer(Ref<RenderStyle>&& style, const RenderTreePosition&)
217 {
218     return createRenderer<RenderSVGResourceMarker>(*this, WTFMove(style));
219 }
220
221 bool SVGMarkerElement::selfHasRelativeLengths() const
222 {
223     return refX().isRelative()
224         || refY().isRelative()
225         || markerWidth().isRelative()
226         || markerHeight().isRelative();
227 }
228
229 void SVGMarkerElement::synchronizeOrientType(SVGElement* contextElement)
230 {
231     ASSERT(contextElement);
232     SVGMarkerElement& ownerType = downcast<SVGMarkerElement>(*contextElement);
233     if (!ownerType.m_orientType.shouldSynchronize)
234         return;
235
236     // If orient is not auto, the previous call to synchronizeOrientAngle already set the orientAttr to the right angle.
237     if (ownerType.m_orientType.value != SVGMarkerOrientAuto)
238         return;
239
240     DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, autoString, ("auto", AtomicString::ConstructFromLiteral));
241     ownerType.m_orientType.synchronize(&ownerType, orientTypePropertyInfo()->attributeName, autoString);
242 }
243
244 Ref<SVGAnimatedProperty> SVGMarkerElement::lookupOrCreateOrientTypeWrapper(SVGElement* contextElement)
245 {
246     ASSERT(contextElement);
247     SVGMarkerElement& ownerType = downcast<SVGMarkerElement>(*contextElement);
248     return SVGAnimatedProperty::lookupOrCreateWrapper<SVGMarkerElement, SVGAnimatedEnumerationPropertyTearOff<SVGMarkerOrientType>, SVGMarkerOrientType>
249         (&ownerType, orientTypePropertyInfo(), ownerType.m_orientType.value);
250 }
251
252 SVGMarkerOrientType& SVGMarkerElement::orientType() const
253 {
254     if (SVGAnimatedEnumeration* wrapper = SVGAnimatedProperty::lookupWrapper<UseOwnerType, SVGAnimatedEnumeration>(this, orientTypePropertyInfo())) {
255         if (wrapper->isAnimating()) {
256             ASSERT(wrapper->currentAnimatedValue() < SVGMarkerOrientMax);
257             return reinterpret_cast<SVGMarkerOrientType&>(wrapper->currentAnimatedValue());
258         }
259     }
260     return m_orientType.value;
261 }
262
263 Ref<SVGAnimatedEnumerationPropertyTearOff<SVGMarkerOrientType>> SVGMarkerElement::orientTypeAnimated()
264 {
265     m_orientType.shouldSynchronize = true;
266     return static_reference_cast<SVGAnimatedEnumerationPropertyTearOff<SVGMarkerOrientType>>(lookupOrCreateOrientTypeWrapper(this));
267 }
268
269 }