KHTML
SVGAngle.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "config.h"
00024 #include "wtf/Platform.h"
00025
00026 #if ENABLE(SVG)
00027 #include "SVGAngle.h"
00028
00029 #include <math.h>
00030 #include <wtf/MathExtras.h>
00031
00032 namespace WebCore {
00033
00034 SVGAngle::SVGAngle()
00035 : RefCounted<SVGAngle>(0)
00036 , m_unitType(SVG_ANGLETYPE_UNKNOWN)
00037 , m_value(0)
00038 , m_valueInSpecifiedUnits(0)
00039 {
00040 }
00041
00042 SVGAngle::~SVGAngle()
00043 {
00044 }
00045
00046 SVGAngle::SVGAngleType SVGAngle::unitType() const
00047 {
00048 return m_unitType;
00049 }
00050
00051 void SVGAngle::setValue(float value)
00052 {
00053 m_value = value;
00054 }
00055
00056 float SVGAngle::value() const
00057 {
00058 return m_value;
00059 }
00060
00061
00062 void SVGAngle::calculate()
00063 {
00064 if (m_unitType == SVG_ANGLETYPE_GRAD)
00065 m_value = grad2deg(m_valueInSpecifiedUnits);
00066 else if (m_unitType == SVG_ANGLETYPE_RAD)
00067 m_value = rad2deg(m_valueInSpecifiedUnits);
00068 else if (m_unitType == SVG_ANGLETYPE_UNSPECIFIED || m_unitType == SVG_ANGLETYPE_DEG)
00069 m_value = m_valueInSpecifiedUnits;
00070 }
00071
00072 void SVGAngle::setValueInSpecifiedUnits(float valueInSpecifiedUnits)
00073 {
00074 m_valueInSpecifiedUnits = valueInSpecifiedUnits;
00075 calculate();
00076 }
00077
00078 float SVGAngle::valueInSpecifiedUnits() const
00079 {
00080 return m_valueInSpecifiedUnits;
00081 }
00082
00083 void SVGAngle::setValueAsString(const String& s)
00084 {
00085 m_valueAsString = s;
00086
00087 bool bOK;
00088 m_valueInSpecifiedUnits = m_valueAsString.toFloat(&bOK);
00089 m_unitType = SVG_ANGLETYPE_UNSPECIFIED;
00090
00091 if (!bOK) {
00092 if (m_valueAsString.endsWith("deg"))
00093 m_unitType = SVG_ANGLETYPE_DEG;
00094 else if (m_valueAsString.endsWith("grad"))
00095 m_unitType = SVG_ANGLETYPE_GRAD;
00096 else if (m_valueAsString.endsWith("rad"))
00097 m_unitType = SVG_ANGLETYPE_RAD;
00098 }
00099
00100 calculate();
00101 }
00102
00103 String SVGAngle::valueAsString() const
00104 {
00105 m_valueAsString = String::number(m_valueInSpecifiedUnits);
00106
00107 switch (m_unitType) {
00108 case SVG_ANGLETYPE_UNSPECIFIED:
00109 case SVG_ANGLETYPE_DEG:
00110 m_valueAsString += "deg";
00111 break;
00112 case SVG_ANGLETYPE_RAD:
00113 m_valueAsString += "rad";
00114 break;
00115 case SVG_ANGLETYPE_GRAD:
00116 m_valueAsString += "grad";
00117 break;
00118 case SVG_ANGLETYPE_UNKNOWN:
00119 break;
00120 }
00121
00122 return m_valueAsString;
00123 }
00124
00125 void SVGAngle::newValueSpecifiedUnits(unsigned short unitType, float valueInSpecifiedUnits)
00126 {
00127 m_unitType = (SVGAngleType)unitType;
00128 m_valueInSpecifiedUnits = valueInSpecifiedUnits;
00129 calculate();
00130 }
00131
00132 void SVGAngle::convertToSpecifiedUnits(unsigned short unitType)
00133 {
00134 if (m_unitType == unitType)
00135 return;
00136
00137 if (m_unitType == SVG_ANGLETYPE_DEG && unitType == SVG_ANGLETYPE_RAD)
00138 m_valueInSpecifiedUnits = deg2rad(m_valueInSpecifiedUnits);
00139 else if (m_unitType == SVG_ANGLETYPE_GRAD && unitType == SVG_ANGLETYPE_RAD)
00140 m_valueInSpecifiedUnits = grad2rad(m_valueInSpecifiedUnits);
00141 else if (m_unitType == SVG_ANGLETYPE_DEG && unitType == SVG_ANGLETYPE_GRAD)
00142 m_valueInSpecifiedUnits = deg2grad(m_valueInSpecifiedUnits);
00143 else if (m_unitType == SVG_ANGLETYPE_RAD && unitType == SVG_ANGLETYPE_GRAD)
00144 m_valueInSpecifiedUnits = rad2grad(m_valueInSpecifiedUnits);
00145 else if (m_unitType == SVG_ANGLETYPE_RAD && unitType == SVG_ANGLETYPE_DEG)
00146 m_valueInSpecifiedUnits = rad2deg(m_valueInSpecifiedUnits);
00147 else if (m_unitType == SVG_ANGLETYPE_GRAD && unitType == SVG_ANGLETYPE_DEG)
00148 m_valueInSpecifiedUnits = grad2deg(m_valueInSpecifiedUnits);
00149
00150 m_unitType = (SVGAngleType)unitType;
00151 }
00152
00153
00154 double SVGAngle::todeg(double rad)
00155 {
00156 return rad2deg(rad);
00157 }
00158
00159 double SVGAngle::torad(double deg)
00160 {
00161 return deg2rad(deg);
00162 }
00163
00164 double SVGAngle::shortestArcBisector(double angle1, double angle2)
00165 {
00166 double bisector = (angle1 + angle2) / 2;
00167
00168 if (fabs(angle1 - angle2) > 180)
00169 bisector += 180;
00170
00171 return bisector;
00172 }
00173
00174 }
00175
00176 #endif // ENABLE(SVG)