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 * Copyright (C) 2018-2019 Apple Inc. 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 "SVGMarkerElement.h"
25
26#include "RenderSVGResourceMarker.h"
27#include "SVGNames.h"
28#include <wtf/IsoMallocInlines.h>
29
30namespace WebCore {
31
32WTF_MAKE_ISO_ALLOCATED_IMPL(SVGMarkerElement);
33
34inline SVGMarkerElement::SVGMarkerElement(const QualifiedName& tagName, Document& document)
35 : SVGElement(tagName, document)
36 , SVGExternalResourcesRequired(this)
37 , SVGFitToViewBox(this)
38{
39 // Spec: If the markerWidth/markerHeight attribute is not specified, the effect is as if a value of "3" were specified.
40 ASSERT(hasTagName(SVGNames::markerTag));
41
42 static std::once_flag onceFlag;
43 std::call_once(onceFlag, [] {
44 PropertyRegistry::registerProperty<SVGNames::refXAttr, &SVGMarkerElement::m_refX>();
45 PropertyRegistry::registerProperty<SVGNames::refYAttr, &SVGMarkerElement::m_refY>();
46 PropertyRegistry::registerProperty<SVGNames::markerWidthAttr, &SVGMarkerElement::m_markerWidth>();
47 PropertyRegistry::registerProperty<SVGNames::markerHeightAttr, &SVGMarkerElement::m_markerHeight>();
48 PropertyRegistry::registerProperty<SVGNames::markerUnitsAttr, SVGMarkerUnitsType, &SVGMarkerElement::m_markerUnits>();
49 PropertyRegistry::registerProperty<SVGNames::orientAttr, &SVGMarkerElement::m_orientAngle, &SVGMarkerElement::m_orientType>();
50 });
51}
52
53Ref<SVGMarkerElement> SVGMarkerElement::create(const QualifiedName& tagName, Document& document)
54{
55 return adoptRef(*new SVGMarkerElement(tagName, document));
56}
57
58AffineTransform SVGMarkerElement::viewBoxToViewTransform(float viewWidth, float viewHeight) const
59{
60 return SVGFitToViewBox::viewBoxToViewTransform(viewBox(), preserveAspectRatio(), viewWidth, viewHeight);
61}
62
63void SVGMarkerElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
64{
65 if (name == SVGNames::markerUnitsAttr) {
66 auto propertyValue = SVGPropertyTraits<SVGMarkerUnitsType>::fromString(value);
67 if (propertyValue > 0)
68 m_markerUnits->setBaseValInternal<SVGMarkerUnitsType>(propertyValue);
69 return;
70 }
71
72 if (name == SVGNames::orientAttr) {
73 auto pair = SVGPropertyTraits<std::pair<SVGAngleValue, SVGMarkerOrientType>>::fromString(value);
74 m_orientAngle->setBaseValInternal(pair.first);
75 m_orientType->setBaseValInternal(pair.second);
76 return;
77 }
78
79 SVGParsingError parseError = NoError;
80
81 if (name == SVGNames::refXAttr)
82 m_refX->setBaseValInternal(SVGLengthValue::construct(LengthModeWidth, value, parseError));
83 else if (name == SVGNames::refYAttr)
84 m_refY->setBaseValInternal(SVGLengthValue::construct(LengthModeHeight, value, parseError));
85 else if (name == SVGNames::markerWidthAttr)
86 m_markerWidth->setBaseValInternal(SVGLengthValue::construct(LengthModeWidth, value, parseError));
87 else if (name == SVGNames::markerHeightAttr)
88 m_markerHeight->setBaseValInternal(SVGLengthValue::construct(LengthModeHeight, value, parseError));
89
90 reportAttributeParsingError(parseError, name, value);
91
92 SVGElement::parseAttribute(name, value);
93 SVGExternalResourcesRequired::parseAttribute(name, value);
94 SVGFitToViewBox::parseAttribute(name, value);
95}
96
97void SVGMarkerElement::svgAttributeChanged(const QualifiedName& attrName)
98{
99 if (PropertyRegistry::isKnownAttribute(attrName)) {
100 InstanceInvalidationGuard guard(*this);
101 if (PropertyRegistry::isAnimatedLengthAttribute(attrName))
102 updateRelativeLengthsInformation();
103 if (RenderObject* object = renderer())
104 object->setNeedsLayout();
105 return;
106 }
107
108 if (SVGFitToViewBox::isKnownAttribute(attrName)) {
109 if (RenderObject* object = renderer())
110 object->setNeedsLayout();
111 return;
112 }
113
114 SVGElement::svgAttributeChanged(attrName);
115 SVGExternalResourcesRequired::svgAttributeChanged(attrName);
116}
117
118void SVGMarkerElement::childrenChanged(const ChildChange& change)
119{
120 SVGElement::childrenChanged(change);
121
122 if (change.source == ChildChangeSource::Parser)
123 return;
124
125 if (RenderObject* object = renderer())
126 object->setNeedsLayout();
127}
128
129void SVGMarkerElement::setOrient(SVGMarkerOrientType orientType, const SVGAngleValue& angle)
130{
131 m_orientType->setBaseValInternal(orientType);
132 m_orientAngle->setBaseValInternal(angle);
133 m_orientAngle->baseVal()->commitChange();
134}
135
136void SVGMarkerElement::setOrientToAuto()
137{
138 setOrient(SVGMarkerOrientAuto, { });
139}
140
141void SVGMarkerElement::setOrientToAngle(SVGAngle& angle)
142{
143 setOrient(SVGMarkerOrientAngle, angle.value());
144}
145
146RenderPtr<RenderElement> SVGMarkerElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
147{
148 return createRenderer<RenderSVGResourceMarker>(*this, WTFMove(style));
149}
150
151bool SVGMarkerElement::selfHasRelativeLengths() const
152{
153 return refX().isRelative()
154 || refY().isRelative()
155 || markerWidth().isRelative()
156 || markerHeight().isRelative();
157}
158
159}
160