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.
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.
24 #include "core/svg/SVGMarkerElement.h"
27 #include "core/rendering/svg/RenderSVGResourceMarker.h"
28 #include "core/svg/SVGAngleTearOff.h"
29 #include "core/svg/SVGElementInstance.h"
33 template<> const SVGEnumerationStringEntries& getStaticStringEntries<SVGMarkerUnitsType>()
35 DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
36 if (entries.isEmpty()) {
37 entries.append(std::make_pair(SVGMarkerUnitsUserSpaceOnUse, "userSpaceOnUse"));
38 entries.append(std::make_pair(SVGMarkerUnitsStrokeWidth, "strokeWidth"));
44 inline SVGMarkerElement::SVGMarkerElement(Document& document)
45 : SVGElement(SVGNames::markerTag, document)
46 , SVGFitToViewBox(this)
47 , m_refX(SVGAnimatedLength::create(this, SVGNames::refXAttr, SVGLength::create(LengthModeWidth), AllowNegativeLengths))
48 , m_refY(SVGAnimatedLength::create(this, SVGNames::refXAttr, SVGLength::create(LengthModeWidth), AllowNegativeLengths))
49 , m_markerWidth(SVGAnimatedLength::create(this, SVGNames::markerWidthAttr, SVGLength::create(LengthModeWidth), ForbidNegativeLengths))
50 , m_markerHeight(SVGAnimatedLength::create(this, SVGNames::markerHeightAttr, SVGLength::create(LengthModeHeight), ForbidNegativeLengths))
51 , m_orientAngle(SVGAnimatedAngle::create(this))
52 , m_markerUnits(SVGAnimatedEnumeration<SVGMarkerUnitsType>::create(this, SVGNames::markerUnitsAttr, SVGMarkerUnitsStrokeWidth))
54 ScriptWrappable::init(this);
56 // Spec: If the markerWidth/markerHeight attribute is not specified, the effect is as if a value of "3" were specified.
57 m_markerWidth->setDefaultValueAsString("3");
58 m_markerHeight->setDefaultValueAsString("3");
60 addToPropertyMap(m_refX);
61 addToPropertyMap(m_refY);
62 addToPropertyMap(m_markerWidth);
63 addToPropertyMap(m_markerHeight);
64 addToPropertyMap(m_orientAngle);
65 addToPropertyMap(m_markerUnits);
68 PassRefPtr<SVGMarkerElement> SVGMarkerElement::create(Document& document)
70 return adoptRef(new SVGMarkerElement(document));
73 AffineTransform SVGMarkerElement::viewBoxToViewTransform(float viewWidth, float viewHeight) const
75 return SVGFitToViewBox::viewBoxToViewTransform(viewBox()->currentValue()->value(), preserveAspectRatio()->currentValue(), viewWidth, viewHeight);
78 bool SVGMarkerElement::isSupportedAttribute(const QualifiedName& attrName)
80 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
81 if (supportedAttributes.isEmpty()) {
82 SVGFitToViewBox::addSupportedAttributes(supportedAttributes);
83 supportedAttributes.add(SVGNames::markerUnitsAttr);
84 supportedAttributes.add(SVGNames::refXAttr);
85 supportedAttributes.add(SVGNames::refYAttr);
86 supportedAttributes.add(SVGNames::markerWidthAttr);
87 supportedAttributes.add(SVGNames::markerHeightAttr);
88 supportedAttributes.add(SVGNames::orientAttr);
90 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
93 void SVGMarkerElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
95 SVGParsingError parseError = NoError;
97 if (!isSupportedAttribute(name))
98 SVGElement::parseAttribute(name, value);
99 else if (name == SVGNames::markerUnitsAttr)
100 m_markerUnits->setBaseValueAsString(value, parseError);
101 else if (name == SVGNames::refXAttr)
102 m_refX->setBaseValueAsString(value, parseError);
103 else if (name == SVGNames::refYAttr)
104 m_refY->setBaseValueAsString(value, parseError);
105 else if (name == SVGNames::markerWidthAttr)
106 m_markerWidth->setBaseValueAsString(value, parseError);
107 else if (name == SVGNames::markerHeightAttr)
108 m_markerHeight->setBaseValueAsString(value, parseError);
109 else if (name == SVGNames::orientAttr)
110 m_orientAngle->setBaseValueAsString(value, parseError);
111 else if (SVGFitToViewBox::parseAttribute(name, value, document(), parseError)) {
113 ASSERT_NOT_REACHED();
115 reportAttributeParsingError(parseError, name, value);
118 void SVGMarkerElement::svgAttributeChanged(const QualifiedName& attrName)
120 if (!isSupportedAttribute(attrName)) {
121 SVGElement::svgAttributeChanged(attrName);
125 SVGElement::InvalidationGuard invalidationGuard(this);
127 if (attrName == SVGNames::refXAttr
128 || attrName == SVGNames::refYAttr
129 || attrName == SVGNames::markerWidthAttr
130 || attrName == SVGNames::markerHeightAttr)
131 updateRelativeLengthsInformation();
133 RenderSVGResourceContainer* renderer = toRenderSVGResourceContainer(this->renderer());
135 renderer->invalidateCacheAndMarkForLayout();
138 void SVGMarkerElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
140 SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
145 if (RenderObject* object = renderer())
146 object->setNeedsLayout();
149 void SVGMarkerElement::setOrientToAuto()
151 m_orientAngle->baseValue()->orientType()->setEnumValue(SVGMarkerOrientAuto);
152 invalidateSVGAttributes();
153 svgAttributeChanged(SVGNames::orientAttr);
156 void SVGMarkerElement::setOrientToAngle(PassRefPtr<SVGAngleTearOff> angle)
159 RefPtr<SVGAngle> target = angle->target();
160 m_orientAngle->baseValue()->newValueSpecifiedUnits(target->unitType(), target->valueInSpecifiedUnits());
161 invalidateSVGAttributes();
162 svgAttributeChanged(SVGNames::orientAttr);
165 RenderObject* SVGMarkerElement::createRenderer(RenderStyle*)
167 return new RenderSVGResourceMarker(this);
170 bool SVGMarkerElement::selfHasRelativeLengths() const
172 return m_refX->currentValue()->isRelative()
173 || m_refY->currentValue()->isRelative()
174 || m_markerWidth->currentValue()->isRelative()
175 || m_markerHeight->currentValue()->isRelative();