1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/*
* Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
* Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef SVGLength_h
#define SVGLength_h
#include "AnimationUtilities.h"
#include "SVGLengthContext.h"
#include "SVGParsingError.h"
#include "SVGPropertyTraits.h"
namespace WebCore {
class CSSPrimitiveValue;
class QualifiedName;
typedef int ExceptionCode;
enum SVGLengthNegativeValuesMode {
AllowNegativeLengths,
ForbidNegativeLengths
};
class SVGLength {
WTF_MAKE_FAST_ALLOCATED;
public:
// Forward declare these enums in the w3c naming scheme, for IDL generation
enum {
SVG_LENGTHTYPE_UNKNOWN = LengthTypeUnknown,
SVG_LENGTHTYPE_NUMBER = LengthTypeNumber,
SVG_LENGTHTYPE_PERCENTAGE = LengthTypePercentage,
SVG_LENGTHTYPE_EMS = LengthTypeEMS,
SVG_LENGTHTYPE_EXS = LengthTypeEXS,
SVG_LENGTHTYPE_PX = LengthTypePX,
SVG_LENGTHTYPE_CM = LengthTypeCM,
SVG_LENGTHTYPE_MM = LengthTypeMM,
SVG_LENGTHTYPE_IN = LengthTypeIN,
SVG_LENGTHTYPE_PT = LengthTypePT,
SVG_LENGTHTYPE_PC = LengthTypePC
};
// FIXME: Once all SVGLength users use Length internally, we make this a wrapper for Length.
SVGLength(SVGLengthMode = LengthModeOther, const String& valueAsString = String());
SVGLength(const SVGLengthContext&, float, SVGLengthMode = LengthModeOther, SVGLengthType = LengthTypeNumber);
SVGLength(const SVGLength&);
SVGLengthType unitType() const;
SVGLengthMode unitMode() const;
bool operator==(const SVGLength&) const;
bool operator!=(const SVGLength&) const;
static SVGLength construct(SVGLengthMode, const String&, SVGParsingError&, SVGLengthNegativeValuesMode = AllowNegativeLengths);
float value(const SVGLengthContext&) const;
float value(const SVGLengthContext&, ExceptionCode&) const;
void setValue(float, const SVGLengthContext&, ExceptionCode&);
void setValue(const SVGLengthContext&, float, SVGLengthMode, SVGLengthType, ExceptionCode&);
float valueInSpecifiedUnits() const { return m_valueInSpecifiedUnits; }
void setValueInSpecifiedUnits(float value) { m_valueInSpecifiedUnits = value; }
float valueAsPercentage() const;
String valueAsString() const;
void setValueAsString(const String&, ExceptionCode&);
void setValueAsString(const String&, SVGLengthMode, ExceptionCode&);
void newValueSpecifiedUnits(unsigned short, float valueInSpecifiedUnits, ExceptionCode&);
void convertToSpecifiedUnits(unsigned short, const SVGLengthContext&, ExceptionCode&);
// Helper functions
inline bool isRelative() const
{
SVGLengthType type = unitType();
return type == LengthTypePercentage || type == LengthTypeEMS || type == LengthTypeEXS;
}
bool isZero() const
{
return !m_valueInSpecifiedUnits;
}
static SVGLength fromCSSPrimitiveValue(const CSSPrimitiveValue&);
static Ref<CSSPrimitiveValue> toCSSPrimitiveValue(const SVGLength&);
static SVGLengthMode lengthModeForAnimatedLengthAttribute(const QualifiedName&);
SVGLength blend(const SVGLength& from, float progress) const
{
SVGLengthType toType = unitType();
SVGLengthType fromType = from.unitType();
if ((from.isZero() && isZero())
|| fromType == LengthTypeUnknown
|| toType == LengthTypeUnknown
|| (!from.isZero() && fromType != LengthTypePercentage && toType == LengthTypePercentage)
|| (!isZero() && fromType == LengthTypePercentage && toType != LengthTypePercentage)
|| (!from.isZero() && !isZero() && (fromType == LengthTypeEMS || fromType == LengthTypeEXS) && fromType != toType))
return *this;
SVGLength length;
ExceptionCode ec = 0;
if (fromType == LengthTypePercentage || toType == LengthTypePercentage) {
float fromPercent = from.valueAsPercentage() * 100;
float toPercent = valueAsPercentage() * 100;
length.newValueSpecifiedUnits(LengthTypePercentage, WebCore::blend(fromPercent, toPercent, progress), ec);
if (ec)
return SVGLength();
return length;
}
if (fromType == toType || from.isZero() || isZero() || fromType == LengthTypeEMS || fromType == LengthTypeEXS) {
float fromValue = from.valueInSpecifiedUnits();
float toValue = valueInSpecifiedUnits();
if (isZero())
length.newValueSpecifiedUnits(fromType, WebCore::blend(fromValue, toValue, progress), ec);
else
length.newValueSpecifiedUnits(toType, WebCore::blend(fromValue, toValue, progress), ec);
if (ec)
return SVGLength();
return length;
}
ASSERT(!isRelative());
ASSERT(!from.isRelative());
SVGLengthContext nonRelativeLengthContext(nullptr);
float fromValueInUserUnits = nonRelativeLengthContext.convertValueToUserUnits(from.valueInSpecifiedUnits(), from.unitMode(), fromType, ec);
if (ec)
return SVGLength();
float fromValue = nonRelativeLengthContext.convertValueFromUserUnits(fromValueInUserUnits, unitMode(), toType, ec);
if (ec)
return SVGLength();
float toValue = valueInSpecifiedUnits();
length.newValueSpecifiedUnits(toType, WebCore::blend(fromValue, toValue, progress), ec);
if (ec)
return SVGLength();
return length;
}
private:
float m_valueInSpecifiedUnits;
unsigned int m_unit;
};
template<>
struct SVGPropertyTraits<SVGLength> {
static SVGLength initialValue() { return SVGLength(); }
static String toString(const SVGLength& type) { return type.valueAsString(); }
};
} // namespace WebCore
#endif // SVGLength_h
|