don't discard iframe children.
[kdelibs.git] / khtml / svg / SVGAngle.cpp
blobfd93daa72c8fa9cfa9e30005c3ef455433ed4e76
1 /*
2 Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005, 2006 Rob Buis <buis@kde.org>
5 This file is part of the KDE project
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
23 #include "config.h"
24 #include "wtf/Platform.h"
26 #if ENABLE(SVG)
27 #include "SVGAngle.h"
29 #include <math.h>
30 #include <wtf/MathExtras.h>
32 namespace WebCore {
34 SVGAngle::SVGAngle()
35 : RefCounted<SVGAngle>(0)
36 , m_unitType(SVG_ANGLETYPE_UNKNOWN)
37 , m_value(0)
38 , m_valueInSpecifiedUnits(0)
42 SVGAngle::~SVGAngle()
46 SVGAngle::SVGAngleType SVGAngle::unitType() const
48 return m_unitType;
51 void SVGAngle::setValue(float value)
53 m_value = value;
56 float SVGAngle::value() const
58 return m_value;
61 // calc m_value
62 void SVGAngle::calculate()
64 if (m_unitType == SVG_ANGLETYPE_GRAD)
65 m_value = grad2deg(m_valueInSpecifiedUnits);
66 else if (m_unitType == SVG_ANGLETYPE_RAD)
67 m_value = rad2deg(m_valueInSpecifiedUnits);
68 else if (m_unitType == SVG_ANGLETYPE_UNSPECIFIED || m_unitType == SVG_ANGLETYPE_DEG)
69 m_value = m_valueInSpecifiedUnits;
72 void SVGAngle::setValueInSpecifiedUnits(float valueInSpecifiedUnits)
74 m_valueInSpecifiedUnits = valueInSpecifiedUnits;
75 calculate();
78 float SVGAngle::valueInSpecifiedUnits() const
80 return m_valueInSpecifiedUnits;
83 void SVGAngle::setValueAsString(const String& s)
85 m_valueAsString = s;
87 bool bOK;
88 m_valueInSpecifiedUnits = m_valueAsString.toFloat(&bOK);
89 m_unitType = SVG_ANGLETYPE_UNSPECIFIED;
91 if (!bOK) {
92 if (m_valueAsString.endsWith("deg"))
93 m_unitType = SVG_ANGLETYPE_DEG;
94 else if (m_valueAsString.endsWith("grad"))
95 m_unitType = SVG_ANGLETYPE_GRAD;
96 else if (m_valueAsString.endsWith("rad"))
97 m_unitType = SVG_ANGLETYPE_RAD;
100 calculate();
103 String SVGAngle::valueAsString() const
105 m_valueAsString = String::number(m_valueInSpecifiedUnits);
107 switch (m_unitType) {
108 case SVG_ANGLETYPE_UNSPECIFIED:
109 case SVG_ANGLETYPE_DEG:
110 m_valueAsString += "deg";
111 break;
112 case SVG_ANGLETYPE_RAD:
113 m_valueAsString += "rad";
114 break;
115 case SVG_ANGLETYPE_GRAD:
116 m_valueAsString += "grad";
117 break;
118 case SVG_ANGLETYPE_UNKNOWN:
119 break;
122 return m_valueAsString;
125 void SVGAngle::newValueSpecifiedUnits(unsigned short unitType, float valueInSpecifiedUnits)
127 m_unitType = (SVGAngleType)unitType;
128 m_valueInSpecifiedUnits = valueInSpecifiedUnits;
129 calculate();
132 void SVGAngle::convertToSpecifiedUnits(unsigned short unitType)
134 if (m_unitType == unitType)
135 return;
137 if (m_unitType == SVG_ANGLETYPE_DEG && unitType == SVG_ANGLETYPE_RAD)
138 m_valueInSpecifiedUnits = deg2rad(m_valueInSpecifiedUnits);
139 else if (m_unitType == SVG_ANGLETYPE_GRAD && unitType == SVG_ANGLETYPE_RAD)
140 m_valueInSpecifiedUnits = grad2rad(m_valueInSpecifiedUnits);
141 else if (m_unitType == SVG_ANGLETYPE_DEG && unitType == SVG_ANGLETYPE_GRAD)
142 m_valueInSpecifiedUnits = deg2grad(m_valueInSpecifiedUnits);
143 else if (m_unitType == SVG_ANGLETYPE_RAD && unitType == SVG_ANGLETYPE_GRAD)
144 m_valueInSpecifiedUnits = rad2grad(m_valueInSpecifiedUnits);
145 else if (m_unitType == SVG_ANGLETYPE_RAD && unitType == SVG_ANGLETYPE_DEG)
146 m_valueInSpecifiedUnits = rad2deg(m_valueInSpecifiedUnits);
147 else if (m_unitType == SVG_ANGLETYPE_GRAD && unitType == SVG_ANGLETYPE_DEG)
148 m_valueInSpecifiedUnits = grad2deg(m_valueInSpecifiedUnits);
150 m_unitType = (SVGAngleType)unitType;
153 // Helpers
154 double SVGAngle::todeg(double rad)
156 return rad2deg(rad);
159 double SVGAngle::torad(double deg)
161 return deg2rad(deg);
164 double SVGAngle::shortestArcBisector(double angle1, double angle2)
166 double bisector = (angle1 + angle2) / 2;
168 if (fabs(angle1 - angle2) > 180)
169 bisector += 180;
171 return bisector;
176 #endif // ENABLE(SVG)