1/*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifndef SVGMarkerElement_h
22#define SVGMarkerElement_h
23
24#include "bindings/v8/ExceptionState.h"
25#include "core/svg/SVGAnimatedAngle.h"
26#include "core/svg/SVGAnimatedBoolean.h"
27#include "core/svg/SVGAnimatedEnumeration.h"
28#include "core/svg/SVGAnimatedLength.h"
29#include "core/svg/SVGAnimatedPreserveAspectRatio.h"
30#include "core/svg/SVGAnimatedRect.h"
31#include "core/svg/SVGElement.h"
32#include "core/svg/SVGExternalResourcesRequired.h"
33#include "core/svg/SVGFitToViewBox.h"
34
35namespace WebCore {
36
37enum SVGMarkerUnitsType {
38    SVGMarkerUnitsUnknown = 0,
39    SVGMarkerUnitsUserSpaceOnUse,
40    SVGMarkerUnitsStrokeWidth
41};
42
43enum SVGMarkerOrientType {
44    SVGMarkerOrientUnknown = 0,
45    SVGMarkerOrientAuto,
46    SVGMarkerOrientAngle
47};
48
49template<>
50struct SVGPropertyTraits<SVGMarkerUnitsType> {
51    static unsigned highestEnumValue() { return SVGMarkerUnitsStrokeWidth; }
52
53    static String toString(SVGMarkerUnitsType type)
54    {
55        switch (type) {
56        case SVGMarkerUnitsUnknown:
57            return emptyString();
58        case SVGMarkerUnitsUserSpaceOnUse:
59            return "userSpaceOnUse";
60        case SVGMarkerUnitsStrokeWidth:
61            return "strokeWidth";
62        }
63
64        ASSERT_NOT_REACHED();
65        return emptyString();
66    }
67
68    static SVGMarkerUnitsType fromString(const String& value)
69    {
70        if (value == "userSpaceOnUse")
71            return SVGMarkerUnitsUserSpaceOnUse;
72        if (value == "strokeWidth")
73            return SVGMarkerUnitsStrokeWidth;
74        return SVGMarkerUnitsUnknown;
75    }
76};
77
78template<>
79struct SVGPropertyTraits<SVGMarkerOrientType> {
80    static unsigned highestEnumValue() { return SVGMarkerOrientAngle; }
81
82    // toString is not needed, synchronizeOrientType() handles this on its own.
83
84    static SVGMarkerOrientType fromString(const String& value, SVGAngle& angle)
85    {
86        if (value == "auto")
87            return SVGMarkerOrientAuto;
88
89        TrackExceptionState exceptionState;
90        angle.setValueAsString(value, exceptionState);
91        if (!exceptionState.hadException())
92            return SVGMarkerOrientAngle;
93        return SVGMarkerOrientUnknown;
94    }
95};
96
97class SVGMarkerElement FINAL : public SVGElement,
98                               public SVGExternalResourcesRequired,
99                               public SVGFitToViewBox {
100public:
101    // Forward declare enumerations in the W3C naming scheme, for IDL generation.
102    enum {
103        SVG_MARKERUNITS_UNKNOWN = SVGMarkerUnitsUnknown,
104        SVG_MARKERUNITS_USERSPACEONUSE = SVGMarkerUnitsUserSpaceOnUse,
105        SVG_MARKERUNITS_STROKEWIDTH = SVGMarkerUnitsStrokeWidth
106    };
107
108    enum {
109        SVG_MARKER_ORIENT_UNKNOWN = SVGMarkerOrientUnknown,
110        SVG_MARKER_ORIENT_AUTO = SVGMarkerOrientAuto,
111        SVG_MARKER_ORIENT_ANGLE = SVGMarkerOrientAngle
112    };
113
114    static PassRefPtr<SVGMarkerElement> create(Document&);
115
116    AffineTransform viewBoxToViewTransform(float viewWidth, float viewHeight) const;
117
118    void setOrientToAuto();
119    void setOrientToAngle(const SVGAngle&);
120
121    static const SVGPropertyInfo* orientTypePropertyInfo();
122
123    // Custom 'orientType' property.
124    static void synchronizeOrientType(SVGElement* contextElement);
125    static PassRefPtr<SVGAnimatedProperty> lookupOrCreateOrientTypeWrapper(SVGElement* contextElement);
126    SVGMarkerOrientType& orientTypeCurrentValue() const { return m_orientType.value; }
127    SVGMarkerOrientType& orientTypeBaseValue() const { return m_orientType.value; }
128    void setOrientTypeBaseValue(const SVGMarkerOrientType& type) { m_orientType.value = type; }
129    PassRefPtr<SVGAnimatedEnumerationPropertyTearOff<SVGMarkerOrientType> > orientType();
130
131private:
132    explicit SVGMarkerElement(Document&);
133
134    virtual bool needsPendingResourceHandling() const { return false; }
135
136    bool isSupportedAttribute(const QualifiedName&);
137    virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
138    virtual void svgAttributeChanged(const QualifiedName&);
139    virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0);
140
141    virtual RenderObject* createRenderer(RenderStyle*);
142    virtual bool rendererIsNeeded(const RenderStyle&) { return true; }
143
144    virtual bool selfHasRelativeLengths() const;
145
146    void synchronizeOrientType();
147
148    static const AtomicString& orientTypeIdentifier();
149    static const AtomicString& orientAngleIdentifier();
150
151    mutable SVGSynchronizableAnimatedProperty<SVGMarkerOrientType> m_orientType;
152
153    BEGIN_DECLARE_ANIMATED_PROPERTIES(SVGMarkerElement)
154        DECLARE_ANIMATED_LENGTH(RefX, refX)
155        DECLARE_ANIMATED_LENGTH(RefY, refY)
156        DECLARE_ANIMATED_LENGTH(MarkerWidth, markerWidth)
157        DECLARE_ANIMATED_LENGTH(MarkerHeight, markerHeight)
158        DECLARE_ANIMATED_ENUMERATION(MarkerUnits, markerUnits, SVGMarkerUnitsType)
159        DECLARE_ANIMATED_ANGLE(OrientAngle, orientAngle)
160        DECLARE_ANIMATED_BOOLEAN(ExternalResourcesRequired, externalResourcesRequired)
161        DECLARE_ANIMATED_RECT(ViewBox, viewBox)
162        DECLARE_ANIMATED_PRESERVEASPECTRATIO(PreserveAspectRatio, preserveAspectRatio)
163    END_DECLARE_ANIMATED_PROPERTIES
164};
165
166DEFINE_NODE_TYPE_CASTS(SVGMarkerElement, hasTagName(SVGNames::markerTag));
167
168}
169
170#endif
171