Support external keyboard function (SelectAll, Cut, Copy, Paste)
[framework/web/webkit-efl.git] / Source / WebCore / svg / SVGLength.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4  * Copyright (C) 2007 Apple Inc. 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 #if ENABLE(SVG)
25 #include "SVGLength.h"
26
27 #include "CSSHelper.h"
28 #include "CSSPrimitiveValue.h"
29 #include "ExceptionCode.h"
30 #include "FloatConversion.h"
31 #include "SVGNames.h"
32 #include "SVGParserUtilities.h"
33
34 #include <wtf/MathExtras.h>
35 #include <wtf/text/WTFString.h>
36
37 namespace WebCore {
38
39 // Helper functions
40 static inline unsigned int storeUnit(SVGLengthMode mode, SVGLengthType type)
41 {
42     return (mode << 4) | type;
43 }
44
45 static inline SVGLengthMode extractMode(unsigned int unit)
46 {
47     unsigned int mode = unit >> 4;    
48     return static_cast<SVGLengthMode>(mode);
49 }
50
51 static inline SVGLengthType extractType(unsigned int unit)
52 {
53     unsigned int mode = unit >> 4;
54     unsigned int type = unit ^ (mode << 4);
55     return static_cast<SVGLengthType>(type);
56 }
57
58 static inline String lengthTypeToString(SVGLengthType type)
59 {
60     switch (type) {
61     case LengthTypeUnknown:
62     case LengthTypeNumber:
63         return "";    
64     case LengthTypePercentage:
65         return "%";
66     case LengthTypeEMS:
67         return "em";
68     case LengthTypeEXS:
69         return "ex";
70     case LengthTypePX:
71         return "px";
72     case LengthTypeCM:
73         return "cm";
74     case LengthTypeMM:
75         return "mm";
76     case LengthTypeIN:
77         return "in";
78     case LengthTypePT:
79         return "pt";
80     case LengthTypePC:
81         return "pc";
82     }
83
84     ASSERT_NOT_REACHED();
85     return String();
86 }
87
88 inline SVGLengthType stringToLengthType(const UChar*& ptr, const UChar* end)
89 {
90     if (ptr == end)
91         return LengthTypeNumber;
92
93     const UChar firstChar = *ptr;
94
95     if (++ptr == end)
96         return firstChar == '%' ? LengthTypePercentage : LengthTypeUnknown;
97
98     const UChar secondChar = *ptr;
99
100     if (++ptr != end)
101         return LengthTypeUnknown;
102
103     if (firstChar == 'e' && secondChar == 'm')
104         return LengthTypeEMS;
105     if (firstChar == 'e' && secondChar == 'x')
106         return LengthTypeEXS;
107     if (firstChar == 'p' && secondChar == 'x')
108         return LengthTypePX;
109     if (firstChar == 'c' && secondChar == 'm')
110         return LengthTypeCM;
111     if (firstChar == 'm' && secondChar == 'm')
112         return LengthTypeMM;
113     if (firstChar == 'i' && secondChar == 'n')
114         return LengthTypeIN;
115     if (firstChar == 'p' && secondChar == 't')
116         return LengthTypePT;
117     if (firstChar == 'p' && secondChar == 'c')
118         return LengthTypePC;
119
120     return LengthTypeUnknown;
121 }
122
123 SVGLength::SVGLength(SVGLengthMode mode, const String& valueAsString)
124     : m_valueInSpecifiedUnits(0)
125     , m_unit(storeUnit(mode, LengthTypeNumber))
126 {
127     ExceptionCode ec = 0;
128     setValueAsString(valueAsString, ec);
129 }
130
131 SVGLength::SVGLength(const SVGLengthContext& context, float value, SVGLengthMode mode, SVGLengthType unitType)
132     : m_valueInSpecifiedUnits(0)
133     , m_unit(storeUnit(mode, unitType))
134 {
135     ExceptionCode ec = 0;
136     setValue(value, context, ec);
137     ASSERT(!ec);
138 }
139
140 SVGLength::SVGLength(const SVGLength& other)
141     : m_valueInSpecifiedUnits(other.m_valueInSpecifiedUnits)
142     , m_unit(other.m_unit)
143 {
144 }
145
146 void SVGLength::setValueAsString(const String& valueAsString, SVGLengthMode mode, ExceptionCode& ec)
147 {
148     m_valueInSpecifiedUnits = 0;
149     m_unit = storeUnit(mode, LengthTypeNumber);
150     setValueAsString(valueAsString, ec);
151 }
152
153 bool SVGLength::operator==(const SVGLength& other) const
154 {
155     return m_unit == other.m_unit
156         && m_valueInSpecifiedUnits == other.m_valueInSpecifiedUnits;
157 }
158
159 bool SVGLength::operator!=(const SVGLength& other) const
160 {
161     return !operator==(other);
162 }
163
164 SVGLength SVGLength::construct(SVGLengthMode mode, const String& valueAsString, SVGParsingError& parseError, SVGLengthNegativeValuesMode negativeValuesMode)
165 {
166     ExceptionCode ec = 0;
167     SVGLength length(mode);
168
169     length.setValueAsString(valueAsString, ec);
170
171     if (ec)
172         parseError = ParsingAttributeFailedError;
173     else if (negativeValuesMode == ForbidNegativeLengths && length.valueInSpecifiedUnits() < 0)
174         parseError = NegativeValueForbiddenError;
175
176     return length;
177 }
178
179 SVGLengthType SVGLength::unitType() const
180 {
181     return extractType(m_unit);
182 }
183
184 SVGLengthMode SVGLength::unitMode() const
185 {
186     return extractMode(m_unit);
187 }
188
189 float SVGLength::value(const SVGLengthContext& context) const
190 {
191     ExceptionCode ec = 0;
192     return value(context, ec);
193 }
194
195 float SVGLength::value(const SVGLengthContext& context, ExceptionCode& ec) const
196 {
197     return context.convertValueToUserUnits(m_valueInSpecifiedUnits, extractMode(m_unit), extractType(m_unit), ec);
198 }
199
200 void SVGLength::setValue(const SVGLengthContext& context, float value, SVGLengthMode mode, SVGLengthType unitType, ExceptionCode& ec)
201 {
202     m_unit = storeUnit(mode, unitType);
203     setValue(value, context, ec);
204 }
205
206 void SVGLength::setValue(float value, const SVGLengthContext& context, ExceptionCode& ec)
207 {
208     // 100% = 100.0 instead of 1.0 for historical reasons, this could eventually be changed
209     if (extractType(m_unit) == LengthTypePercentage)
210         value = value / 100;
211
212     float convertedValue = context.convertValueFromUserUnits(value, extractMode(m_unit), extractType(m_unit), ec);
213     if (!ec)
214         m_valueInSpecifiedUnits = convertedValue;
215 }
216 float SVGLength::valueAsPercentage() const
217 {
218     // 100% = 100.0 instead of 1.0 for historical reasons, this could eventually be changed
219     if (extractType(m_unit) == LengthTypePercentage)
220         return m_valueInSpecifiedUnits / 100;
221
222     return m_valueInSpecifiedUnits;
223 }
224
225 void SVGLength::setValueAsString(const String& string, ExceptionCode& ec)
226 {
227     if (string.isEmpty())
228         return;
229
230     float convertedNumber = 0;
231     const UChar* ptr = string.characters();
232     const UChar* end = ptr + string.length();
233
234     if (!parseNumber(ptr, end, convertedNumber, false)) {
235         ec = SYNTAX_ERR;
236         return;
237     }
238
239     SVGLengthType type = stringToLengthType(ptr, end);
240     ASSERT(ptr <= end);
241     if (type == LengthTypeUnknown) {
242         ec = SYNTAX_ERR;
243         return;
244     }
245
246     m_unit = storeUnit(extractMode(m_unit), type);
247     m_valueInSpecifiedUnits = convertedNumber;
248 }
249
250 String SVGLength::valueAsString() const
251 {
252     return String::number(m_valueInSpecifiedUnits) + lengthTypeToString(extractType(m_unit));
253 }
254
255 void SVGLength::newValueSpecifiedUnits(unsigned short type, float value, ExceptionCode& ec)
256 {
257     if (type == LengthTypeUnknown || type > LengthTypePC) {
258         ec = NOT_SUPPORTED_ERR;
259         return;
260     }
261
262     m_unit = storeUnit(extractMode(m_unit), static_cast<SVGLengthType>(type));
263     m_valueInSpecifiedUnits = value;
264 }
265
266 void SVGLength::convertToSpecifiedUnits(unsigned short type, const SVGLengthContext& context, ExceptionCode& ec)
267 {
268     if (type == LengthTypeUnknown || type > LengthTypePC) {
269         ec = NOT_SUPPORTED_ERR;
270         return;
271     }
272
273     float valueInUserUnits = value(context, ec);
274     if (ec)
275         return;
276
277     unsigned int originalUnitAndType = m_unit;    
278     m_unit = storeUnit(extractMode(m_unit), static_cast<SVGLengthType>(type));
279     setValue(valueInUserUnits, context, ec);
280     if (!ec)
281         return;
282
283     // Eventually restore old unit and type
284     m_unit = originalUnitAndType;
285 }
286
287 SVGLength SVGLength::fromCSSPrimitiveValue(CSSPrimitiveValue* value)
288 {
289     ASSERT(value);
290
291     SVGLengthType svgType;
292     switch (value->primitiveType()) {
293     case CSSPrimitiveValue::CSS_NUMBER:
294         svgType = LengthTypeNumber;
295         break;
296     case CSSPrimitiveValue::CSS_PERCENTAGE:
297         svgType = LengthTypePercentage;
298         break;
299     case CSSPrimitiveValue::CSS_EMS:
300         svgType = LengthTypeEMS;
301         break;
302     case CSSPrimitiveValue::CSS_EXS:
303         svgType = LengthTypeEXS;
304         break;
305     case CSSPrimitiveValue::CSS_PX:
306         svgType = LengthTypePX;
307         break;
308     case CSSPrimitiveValue::CSS_CM:
309         svgType = LengthTypeCM;
310         break;
311     case CSSPrimitiveValue::CSS_MM:
312         svgType = LengthTypeMM;
313         break;
314     case CSSPrimitiveValue::CSS_IN:
315         svgType = LengthTypeIN;
316         break;
317     case CSSPrimitiveValue::CSS_PT:
318         svgType = LengthTypePT;
319         break;
320     case CSSPrimitiveValue::CSS_PC:
321         svgType = LengthTypePC;
322         break;
323     case CSSPrimitiveValue::CSS_UNKNOWN:
324     default:
325         svgType = LengthTypeUnknown;
326         break;
327     };
328
329     if (svgType == LengthTypeUnknown)
330         return SVGLength();
331
332     ExceptionCode ec = 0;
333     SVGLength length;
334     length.newValueSpecifiedUnits(svgType, value->getFloatValue(), ec);
335     if (ec)    
336         return SVGLength();
337
338     return length;
339 }
340
341 PassRefPtr<CSSPrimitiveValue> SVGLength::toCSSPrimitiveValue(const SVGLength& length)
342 {
343     CSSPrimitiveValue::UnitTypes cssType = CSSPrimitiveValue::CSS_UNKNOWN;
344     switch (length.unitType()) {
345     case LengthTypeUnknown:
346         break;
347     case LengthTypeNumber:
348         cssType = CSSPrimitiveValue::CSS_NUMBER;
349         break;
350     case LengthTypePercentage:
351         cssType = CSSPrimitiveValue::CSS_PERCENTAGE;
352         break;
353     case LengthTypeEMS:
354         cssType = CSSPrimitiveValue::CSS_EMS;
355         break;
356     case LengthTypeEXS:
357         cssType = CSSPrimitiveValue::CSS_EXS;
358         break;
359     case LengthTypePX:
360         cssType = CSSPrimitiveValue::CSS_PX;
361         break;
362     case LengthTypeCM:
363         cssType = CSSPrimitiveValue::CSS_CM;
364         break;
365     case LengthTypeMM:
366         cssType = CSSPrimitiveValue::CSS_MM;
367         break;
368     case LengthTypeIN:
369         cssType = CSSPrimitiveValue::CSS_IN;
370         break;
371     case LengthTypePT:
372         cssType = CSSPrimitiveValue::CSS_PT;
373         break;
374     case LengthTypePC:
375         cssType = CSSPrimitiveValue::CSS_PC;
376         break;
377     };
378
379     return CSSPrimitiveValue::create(length.valueInSpecifiedUnits(), cssType);
380 }
381
382 SVGLengthMode SVGLength::lengthModeForAnimatedLengthAttribute(const QualifiedName& attrName)
383 {
384     typedef HashMap<QualifiedName, SVGLengthMode> LengthModeForLengthAttributeMap;
385     DEFINE_STATIC_LOCAL(LengthModeForLengthAttributeMap, s_lengthModeMap, ());
386     
387     if (s_lengthModeMap.isEmpty()) {
388         s_lengthModeMap.set(SVGNames::xAttr, LengthModeWidth);
389         s_lengthModeMap.set(SVGNames::yAttr, LengthModeHeight);
390         s_lengthModeMap.set(SVGNames::cxAttr, LengthModeWidth);
391         s_lengthModeMap.set(SVGNames::cyAttr, LengthModeHeight);
392         s_lengthModeMap.set(SVGNames::dxAttr, LengthModeWidth);
393         s_lengthModeMap.set(SVGNames::dyAttr, LengthModeHeight);
394         s_lengthModeMap.set(SVGNames::fxAttr, LengthModeWidth);
395         s_lengthModeMap.set(SVGNames::fyAttr, LengthModeHeight);
396         s_lengthModeMap.set(SVGNames::rAttr, LengthModeOther);
397         s_lengthModeMap.set(SVGNames::widthAttr, LengthModeWidth);
398         s_lengthModeMap.set(SVGNames::heightAttr, LengthModeHeight);
399         s_lengthModeMap.set(SVGNames::x1Attr, LengthModeWidth);
400         s_lengthModeMap.set(SVGNames::x2Attr, LengthModeWidth);
401         s_lengthModeMap.set(SVGNames::y1Attr, LengthModeHeight);
402         s_lengthModeMap.set(SVGNames::y2Attr, LengthModeHeight);
403         s_lengthModeMap.set(SVGNames::refXAttr, LengthModeWidth);
404         s_lengthModeMap.set(SVGNames::refYAttr, LengthModeHeight);
405         s_lengthModeMap.set(SVGNames::markerWidthAttr, LengthModeWidth);
406         s_lengthModeMap.set(SVGNames::markerHeightAttr, LengthModeHeight);        
407         s_lengthModeMap.set(SVGNames::textLengthAttr, LengthModeWidth);
408         s_lengthModeMap.set(SVGNames::startOffsetAttr, LengthModeWidth);
409     }
410     
411     if (s_lengthModeMap.contains(attrName))
412         return s_lengthModeMap.get(attrName);
413     
414     return LengthModeOther;
415 }
416
417 }
418
419 #endif