Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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
24 #include "core/svg/SVGMarkerElement.h"
25
26 #include "SVGNames.h"
27 #include "core/rendering/svg/RenderSVGResourceMarker.h"
28 #include "core/svg/SVGAngleTearOff.h"
29 #include "core/svg/SVGElementInstance.h"
30
31 namespace WebCore {
32
33 template<> const SVGEnumerationStringEntries& getStaticStringEntries<SVGMarkerUnitsType>()
34 {
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"));
39     }
40     return entries;
41 }
42
43
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))
53 {
54     ScriptWrappable::init(this);
55
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");
59
60     addToPropertyMap(m_refX);
61     addToPropertyMap(m_refY);
62     addToPropertyMap(m_markerWidth);
63     addToPropertyMap(m_markerHeight);
64     addToPropertyMap(m_orientAngle);
65     addToPropertyMap(m_markerUnits);
66 }
67
68 PassRefPtr<SVGMarkerElement> SVGMarkerElement::create(Document& document)
69 {
70     return adoptRef(new SVGMarkerElement(document));
71 }
72
73 AffineTransform SVGMarkerElement::viewBoxToViewTransform(float viewWidth, float viewHeight) const
74 {
75     return SVGFitToViewBox::viewBoxToViewTransform(viewBox()->currentValue()->value(), preserveAspectRatio()->currentValue(), viewWidth, viewHeight);
76 }
77
78 bool SVGMarkerElement::isSupportedAttribute(const QualifiedName& attrName)
79 {
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);
89     }
90     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
91 }
92
93 void SVGMarkerElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
94 {
95     SVGParsingError parseError = NoError;
96
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)) {
112     } else
113         ASSERT_NOT_REACHED();
114
115     reportAttributeParsingError(parseError, name, value);
116 }
117
118 void SVGMarkerElement::svgAttributeChanged(const QualifiedName& attrName)
119 {
120     if (!isSupportedAttribute(attrName)) {
121         SVGElement::svgAttributeChanged(attrName);
122         return;
123     }
124
125     SVGElement::InvalidationGuard invalidationGuard(this);
126
127     if (attrName == SVGNames::refXAttr
128         || attrName == SVGNames::refYAttr
129         || attrName == SVGNames::markerWidthAttr
130         || attrName == SVGNames::markerHeightAttr)
131         updateRelativeLengthsInformation();
132
133     RenderSVGResourceContainer* renderer = toRenderSVGResourceContainer(this->renderer());
134     if (renderer)
135         renderer->invalidateCacheAndMarkForLayout();
136 }
137
138 void SVGMarkerElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
139 {
140     SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
141
142     if (changedByParser)
143         return;
144
145     if (RenderObject* object = renderer())
146         object->setNeedsLayout();
147 }
148
149 void SVGMarkerElement::setOrientToAuto()
150 {
151     m_orientAngle->baseValue()->orientType()->setEnumValue(SVGMarkerOrientAuto);
152     invalidateSVGAttributes();
153     svgAttributeChanged(SVGNames::orientAttr);
154 }
155
156 void SVGMarkerElement::setOrientToAngle(PassRefPtr<SVGAngleTearOff> angle)
157 {
158     ASSERT(angle);
159     RefPtr<SVGAngle> target = angle->target();
160     m_orientAngle->baseValue()->newValueSpecifiedUnits(target->unitType(), target->valueInSpecifiedUnits());
161     invalidateSVGAttributes();
162     svgAttributeChanged(SVGNames::orientAttr);
163 }
164
165 RenderObject* SVGMarkerElement::createRenderer(RenderStyle*)
166 {
167     return new RenderSVGResourceMarker(this);
168 }
169
170 bool SVGMarkerElement::selfHasRelativeLengths() const
171 {
172     return m_refX->currentValue()->isRelative()
173         || m_refY->currentValue()->isRelative()
174         || m_markerWidth->currentValue()->isRelative()
175         || m_markerHeight->currentValue()->isRelative();
176 }
177
178 }