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.
24 #include "wtf/Platform.h"
30 #include <wtf/MathExtras.h>
35 : RefCounted
<SVGAngle
>(0)
36 , m_unitType(SVG_ANGLETYPE_UNKNOWN
)
38 , m_valueInSpecifiedUnits(0)
46 SVGAngle::SVGAngleType
SVGAngle::unitType() const
51 void SVGAngle::setValue(float value
)
56 float SVGAngle::value() const
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
;
78 float SVGAngle::valueInSpecifiedUnits() const
80 return m_valueInSpecifiedUnits
;
83 void SVGAngle::setValueAsString(const String
& s
)
88 m_valueInSpecifiedUnits
= m_valueAsString
.toFloat(&bOK
);
89 m_unitType
= SVG_ANGLETYPE_UNSPECIFIED
;
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
;
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";
112 case SVG_ANGLETYPE_RAD
:
113 m_valueAsString
+= "rad";
115 case SVG_ANGLETYPE_GRAD
:
116 m_valueAsString
+= "grad";
118 case SVG_ANGLETYPE_UNKNOWN
:
122 return m_valueAsString
;
125 void SVGAngle::newValueSpecifiedUnits(unsigned short unitType
, float valueInSpecifiedUnits
)
127 m_unitType
= (SVGAngleType
)unitType
;
128 m_valueInSpecifiedUnits
= valueInSpecifiedUnits
;
132 void SVGAngle::convertToSpecifiedUnits(unsigned short unitType
)
134 if (m_unitType
== unitType
)
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
;
154 double SVGAngle::todeg(double rad
)
159 double SVGAngle::torad(double deg
)
164 double SVGAngle::shortestArcBisector(double angle1
, double angle2
)
166 double bisector
= (angle1
+ angle2
) / 2;
168 if (fabs(angle1
- angle2
) > 180)
176 #endif // ENABLE(SVG)