1/*
2 * Copyright (C) 2004, 2005, 2006, 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 SVGLength_h
22#define SVGLength_h
23
24#if ENABLE(SVG)
25#include "ExceptionCode.h"
26#include "SVGPropertyTraits.h"
27
28namespace WebCore {
29
30class CSSPrimitiveValue;
31
32enum SVGLengthType {
33    LengthTypeUnknown = 0,
34    LengthTypeNumber = 1,
35    LengthTypePercentage = 2,
36    LengthTypeEMS = 3,
37    LengthTypeEXS = 4,
38    LengthTypePX = 5,
39    LengthTypeCM = 6,
40    LengthTypeMM = 7,
41    LengthTypeIN = 8,
42    LengthTypePT = 9,
43    LengthTypePC = 10
44};
45
46enum SVGLengthMode {
47    LengthModeWidth = 0,
48    LengthModeHeight,
49    LengthModeOther
50};
51
52class SVGElement;
53
54class SVGLength {
55public:
56    // Forward declare these enums in the w3c naming scheme, for IDL generation
57    enum {
58        SVG_LENGTHTYPE_UNKNOWN = LengthTypeUnknown,
59        SVG_LENGTHTYPE_NUMBER = LengthTypeNumber,
60        SVG_LENGTHTYPE_PERCENTAGE = LengthTypePercentage,
61        SVG_LENGTHTYPE_EMS = LengthTypeEMS,
62        SVG_LENGTHTYPE_EXS = LengthTypeEXS,
63        SVG_LENGTHTYPE_PX = LengthTypePX,
64        SVG_LENGTHTYPE_CM = LengthTypeCM,
65        SVG_LENGTHTYPE_MM = LengthTypeMM,
66        SVG_LENGTHTYPE_IN = LengthTypeIN,
67        SVG_LENGTHTYPE_PT = LengthTypePT,
68        SVG_LENGTHTYPE_PC = LengthTypePC
69    };
70
71    SVGLength(SVGLengthMode mode = LengthModeOther, const String& valueAsString = String());
72    SVGLength(const SVGLength&);
73
74    SVGLengthType unitType() const;
75
76    bool operator==(const SVGLength&) const;
77    bool operator!=(const SVGLength&) const;
78
79    float value(const SVGElement* context) const;
80    float value(const SVGElement* context, ExceptionCode&) const;
81    void setValue(float, const SVGElement* context, ExceptionCode&);
82
83    float valueInSpecifiedUnits() const { return m_valueInSpecifiedUnits; }
84    void setValueInSpecifiedUnits(float value) { m_valueInSpecifiedUnits = value; }
85
86    float valueAsPercentage() const;
87
88    String valueAsString() const;
89    void setValueAsString(const String&, ExceptionCode&);
90
91    void newValueSpecifiedUnits(unsigned short, float valueInSpecifiedUnits, ExceptionCode&);
92    void convertToSpecifiedUnits(unsigned short, const SVGElement* context, ExceptionCode&);
93
94    // Helper functions
95    inline bool isRelative() const
96    {
97        SVGLengthType type = unitType();
98        return type == LengthTypePercentage || type == LengthTypeEMS || type == LengthTypeEXS;
99    }
100
101    static SVGLength fromCSSPrimitiveValue(CSSPrimitiveValue*);
102    static PassRefPtr<CSSPrimitiveValue> toCSSPrimitiveValue(const SVGLength&);
103
104private:
105    bool determineViewport(const SVGElement* context, float& width, float& height) const;
106
107    float convertValueFromPercentageToUserUnits(float value, const SVGElement* context, ExceptionCode&) const;
108    float convertValueFromUserUnitsToPercentage(float value, const SVGElement* context, ExceptionCode&) const;
109
110    float convertValueFromUserUnitsToEMS(float value, const SVGElement* context, ExceptionCode&) const;
111    float convertValueFromEMSToUserUnits(float value, const SVGElement* context, ExceptionCode&) const;
112
113    float convertValueFromUserUnitsToEXS(float value, const SVGElement* context, ExceptionCode&) const;
114    float convertValueFromEXSToUserUnits(float value, const SVGElement* context, ExceptionCode&) const;
115
116private:
117    float m_valueInSpecifiedUnits;
118    unsigned int m_unit;
119};
120
121template<>
122struct SVGPropertyTraits<SVGLength> {
123    static SVGLength initialValue() { return SVGLength(); }
124    static String toString(const SVGLength& type) { return type.valueAsString(); }
125};
126
127
128} // namespace WebCore
129
130#endif // ENABLE(SVG)
131#endif // SVGLength_h
132