Improve use of NeverDestroyed
[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     static NeverDestroyed<AtomicString> s_identifier("SVGOrientType", AtomicString::ConstructFromLiteral);
94     return s_identifier;
95 }
96
97 const AtomicString& SVGMarkerElement::orientAngleIdentifier()
98 {
99     static NeverDestroyed<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 const auto supportedAttributes = makeNeverDestroyed([] {
111         HashSet<QualifiedName> set;
112         SVGLangSpace::addSupportedAttributes(set);
113         SVGExternalResourcesRequired::addSupportedAttributes(set);
114         SVGFitToViewBox::addSupportedAttributes(set);
115         set.add({
116             SVGNames::markerUnitsAttr,
117             SVGNames::refXAttr,
118             SVGNames::refYAttr,
119             SVGNames::markerWidthAttr,
120             SVGNames::markerHeightAttr,
121             SVGNames::orientAttr,
122         });
123         return set;
124     }());
125     return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
126 }
127
128 void SVGMarkerElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
129 {
130     if (name == SVGNames::markerUnitsAttr) {
131         auto propertyValue = SVGPropertyTraits<SVGMarkerUnitsType>::fromString(value);
132         if (propertyValue > 0)
133             setMarkerUnitsBaseValue(propertyValue);
134         return;
135     }
136
137     if (name == SVGNames::orientAttr) {
138         SVGAngleValue angle;
139         auto orientType = SVGPropertyTraits<SVGMarkerOrientType>::fromString(value, angle);
140         if (orientType > 0)
141             setOrientTypeBaseValue(orientType);
142         if (orientType == SVGMarkerOrientAngle)
143             setOrientAngleBaseValue(angle);
144         return;
145     }
146
147     SVGParsingError parseError = NoError;
148
149     if (name == SVGNames::refXAttr)
150         setRefXBaseValue(SVGLengthValue::construct(LengthModeWidth, value, parseError));
151     else if (name == SVGNames::refYAttr)
152         setRefYBaseValue(SVGLengthValue::construct(LengthModeHeight, value, parseError));
153     else if (name == SVGNames::markerWidthAttr)
154         setMarkerWidthBaseValue(SVGLengthValue::construct(LengthModeWidth, value, parseError));
155     else if (name == SVGNames::markerHeightAttr)
156         setMarkerHeightBaseValue(SVGLengthValue::construct(LengthModeHeight, value, parseError));
157
158     reportAttributeParsingError(parseError, name, value);
159
160     SVGElement::parseAttribute(name, value);
161     SVGExternalResourcesRequired::parseAttribute(name, value);
162     SVGFitToViewBox::parseAttribute(this, name, value);
163 }
164
165 void SVGMarkerElement::svgAttributeChanged(const QualifiedName& attrName)
166 {
167     if (!isSupportedAttribute(attrName)) {
168         SVGElement::svgAttributeChanged(attrName);
169         return;
170     }
171
172     InstanceInvalidationGuard guard(*this);
173     
174     if (attrName == SVGNames::refXAttr
175         || attrName == SVGNames::refYAttr
176         || attrName == SVGNames::markerWidthAttr
177         || attrName == SVGNames::markerHeightAttr)
178         updateRelativeLengthsInformation();
179
180     if (RenderObject* object = renderer())
181         object->setNeedsLayout();
182 }
183
184 void SVGMarkerElement::childrenChanged(const ChildChange& change)
185 {
186     SVGElement::childrenChanged(change);
187
188     if (change.source == ChildChangeSourceParser)
189         return;
190
191     if (RenderObject* object = renderer())
192         object->setNeedsLayout();
193 }
194
195 void SVGMarkerElement::setOrient(SVGMarkerOrientType orientType, const SVGAngleValue& angle)
196 {
197     setOrientTypeBaseValue(orientType);
198     setOrientAngleBaseValue(angle);
199
200     // Mark orientAttr dirty - the next XML DOM access of that attribute kicks in synchronization.
201     m_orientAngle.shouldSynchronize = true;
202     m_orientType.shouldSynchronize = true;
203     invalidateSVGAttributes();
204     svgAttributeChanged(orientAnglePropertyInfo()->attributeName);
205 }
206
207 void SVGMarkerElement::setOrientToAuto()
208 {
209     setOrient(SVGMarkerOrientAuto, { });
210 }
211
212 void SVGMarkerElement::setOrientToAngle(SVGAngle& angle)
213 {
214     setOrient(SVGMarkerOrientAngle, angle.propertyReference());
215 }
216
217 RenderPtr<RenderElement> SVGMarkerElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
218 {
219     return createRenderer<RenderSVGResourceMarker>(*this, WTFMove(style));
220 }
221
222 bool SVGMarkerElement::selfHasRelativeLengths() const
223 {
224     return refX().isRelative()
225         || refY().isRelative()
226         || markerWidth().isRelative()
227         || markerHeight().isRelative();
228 }
229
230 void SVGMarkerElement::synchronizeOrientType(SVGElement* contextElement)
231 {
232     ASSERT(contextElement);
233     SVGMarkerElement& ownerType = downcast<SVGMarkerElement>(*contextElement);
234     if (!ownerType.m_orientType.shouldSynchronize)
235         return;
236
237     static NeverDestroyed<AtomicString> autoString("auto", AtomicString::ConstructFromLiteral);
238     static NeverDestroyed<AtomicString> autoStartReverseString("auto-start-reverse", AtomicString::ConstructFromLiteral);
239
240     // If orient is not auto or auto-start-reverse, the previous call to synchronizeOrientAngle already set the orientAttr to the right angle.
241     if (ownerType.m_orientType.value == SVGMarkerOrientAuto) {
242         ownerType.m_orientType.synchronize(&ownerType, orientTypePropertyInfo()->attributeName, autoString);
243         return;
244     }
245
246     if (ownerType.m_orientType.value == SVGMarkerOrientAutoStartReverse)
247         ownerType.m_orientType.synchronize(&ownerType, orientTypePropertyInfo()->attributeName, autoStartReverseString);
248 }
249
250 Ref<SVGAnimatedProperty> SVGMarkerElement::lookupOrCreateOrientTypeWrapper(SVGElement* contextElement)
251 {
252     ASSERT(contextElement);
253     SVGMarkerElement& ownerType = downcast<SVGMarkerElement>(*contextElement);
254     return SVGAnimatedProperty::lookupOrCreateWrapper<SVGMarkerElement, SVGAnimatedEnumerationPropertyTearOff<SVGMarkerOrientType>, SVGMarkerOrientType>
255         (&ownerType, orientTypePropertyInfo(), ownerType.m_orientType.value);
256 }
257
258 SVGMarkerOrientType& SVGMarkerElement::orientType() const
259 {
260     if (auto wrapper = SVGAnimatedProperty::lookupWrapper<UseOwnerType, SVGAnimatedEnumeration>(this, orientTypePropertyInfo())) {
261         if (wrapper->isAnimating()) {
262             ASSERT(wrapper->currentAnimatedValue() < SVGMarkerOrientMax);
263             return reinterpret_cast<SVGMarkerOrientType&>(wrapper->currentAnimatedValue());
264         }
265     }
266     return m_orientType.value;
267 }
268
269 Ref<SVGAnimatedEnumerationPropertyTearOff<SVGMarkerOrientType>> SVGMarkerElement::orientTypeAnimated()
270 {
271     m_orientType.shouldSynchronize = true;
272     return static_reference_cast<SVGAnimatedEnumerationPropertyTearOff<SVGMarkerOrientType>>(lookupOrCreateOrientTypeWrapper(this));
273 }
274
275 }