KHTML
SVGAngle.cpp
Go to the documentation of this file.
00001 /* 00002 Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org> 00003 2004, 2005, 2006 Rob Buis <buis@kde.org> 00004 00005 This file is part of the KDE project 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 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 // calc m_value 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 // Helpers 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)
KDE 4.6 API Reference