Update To 11.40.268.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 "core/SVGNames.h"
27 #include "core/rendering/svg/RenderSVGResourceMarker.h"
28 #include "core/svg/SVGAngleTearOff.h"
29
30 namespace blink {
31
32 template<> const SVGEnumerationStringEntries& getStaticStringEntries<SVGMarkerUnitsType>()
33 {
34     DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
35     if (entries.isEmpty()) {
36         entries.append(std::make_pair(SVGMarkerUnitsUserSpaceOnUse, "userSpaceOnUse"));
37         entries.append(std::make_pair(SVGMarkerUnitsStrokeWidth, "strokeWidth"));
38     }
39     return entries;
40 }
41
42
43 inline SVGMarkerElement::SVGMarkerElement(Document& document)
44     : SVGElement(SVGNames::markerTag, document)
45     , SVGFitToViewBox(this)
46     , m_refX(SVGAnimatedLength::create(this, SVGNames::refXAttr, SVGLength::create(LengthModeWidth), AllowNegativeLengths))
47     , m_refY(SVGAnimatedLength::create(this, SVGNames::refYAttr, SVGLength::create(LengthModeHeight), AllowNegativeLengths))
48     , m_markerWidth(SVGAnimatedLength::create(this, SVGNames::markerWidthAttr, SVGLength::create(LengthModeWidth), ForbidNegativeLengths))
49     , m_markerHeight(SVGAnimatedLength::create(this, SVGNames::markerHeightAttr, SVGLength::create(LengthModeHeight), ForbidNegativeLengths))
50     , m_orientAngle(SVGAnimatedAngle::create(this))
51     , m_markerUnits(SVGAnimatedEnumeration<SVGMarkerUnitsType>::create(this, SVGNames::markerUnitsAttr, SVGMarkerUnitsStrokeWidth))
52 {
53     // Spec: If the markerWidth/markerHeight attribute is not specified, the effect is as if a value of "3" were specified.
54     m_markerWidth->setDefaultValueAsString("3");
55     m_markerHeight->setDefaultValueAsString("3");
56
57     addToPropertyMap(m_refX);
58     addToPropertyMap(m_refY);
59     addToPropertyMap(m_markerWidth);
60     addToPropertyMap(m_markerHeight);
61     addToPropertyMap(m_orientAngle);
62     addToPropertyMap(m_markerUnits);
63 }
64
65 DEFINE_NODE_FACTORY(SVGMarkerElement)
66
67 AffineTransform SVGMarkerElement::viewBoxToViewTransform(float viewWidth, float viewHeight) const
68 {
69     return SVGFitToViewBox::viewBoxToViewTransform(viewBox()->currentValue()->value(), preserveAspectRatio()->currentValue(), viewWidth, viewHeight);
70 }
71
72 bool SVGMarkerElement::isSupportedAttribute(const QualifiedName& attrName)
73 {
74     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
75     if (supportedAttributes.isEmpty()) {
76         SVGFitToViewBox::addSupportedAttributes(supportedAttributes);
77         supportedAttributes.add(SVGNames::markerUnitsAttr);
78         supportedAttributes.add(SVGNames::refXAttr);
79         supportedAttributes.add(SVGNames::refYAttr);
80         supportedAttributes.add(SVGNames::markerWidthAttr);
81         supportedAttributes.add(SVGNames::markerHeightAttr);
82         supportedAttributes.add(SVGNames::orientAttr);
83     }
84     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
85 }
86
87 void SVGMarkerElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
88 {
89     parseAttributeNew(name, value);
90 }
91
92 void SVGMarkerElement::svgAttributeChanged(const QualifiedName& attrName)
93 {
94     if (!isSupportedAttribute(attrName)) {
95         SVGElement::svgAttributeChanged(attrName);
96         return;
97     }
98
99     SVGElement::InvalidationGuard invalidationGuard(this);
100
101     if (attrName == SVGNames::refXAttr
102         || attrName == SVGNames::refYAttr
103         || attrName == SVGNames::markerWidthAttr
104         || attrName == SVGNames::markerHeightAttr)
105         updateRelativeLengthsInformation();
106
107     RenderSVGResourceContainer* renderer = toRenderSVGResourceContainer(this->renderer());
108     if (renderer)
109         renderer->invalidateCacheAndMarkForLayout();
110 }
111
112 void SVGMarkerElement::childrenChanged(const ChildrenChange& change)
113 {
114     SVGElement::childrenChanged(change);
115
116     if (change.byParser)
117         return;
118
119     if (RenderObject* object = renderer())
120         object->setNeedsLayoutAndFullPaintInvalidation();
121 }
122
123 void SVGMarkerElement::setOrientToAuto()
124 {
125     m_orientAngle->baseValue()->orientType()->setEnumValue(SVGMarkerOrientAuto);
126     invalidateSVGAttributes();
127     svgAttributeChanged(SVGNames::orientAttr);
128 }
129
130 void SVGMarkerElement::setOrientToAngle(PassRefPtr<SVGAngleTearOff> angle)
131 {
132     ASSERT(angle);
133     RefPtr<SVGAngle> target = angle->target();
134     m_orientAngle->baseValue()->newValueSpecifiedUnits(target->unitType(), target->valueInSpecifiedUnits());
135     invalidateSVGAttributes();
136     svgAttributeChanged(SVGNames::orientAttr);
137 }
138
139 RenderObject* SVGMarkerElement::createRenderer(RenderStyle*)
140 {
141     return new RenderSVGResourceMarker(this);
142 }
143
144 bool SVGMarkerElement::selfHasRelativeLengths() const
145 {
146     return m_refX->currentValue()->isRelative()
147         || m_refY->currentValue()->isRelative()
148         || m_markerWidth->currentValue()->isRelative()
149         || m_markerHeight->currentValue()->isRelative();
150 }
151
152 } // namespace blink