Home | History | Annotate | Download | only in svg
      1 /*
      2     Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann (at) kde.org>
      3                   2004, 2005, 2006 Rob Buis <buis (at) kde.org>
      4 
      5     This library is free software; you can redistribute it and/or
      6     modify it under the terms of the GNU Library General Public
      7     License as published by the Free Software Foundation; either
      8     version 2 of the License, or (at your option) any later version.
      9 
     10     This library is distributed in the hope that it will be useful,
     11     but WITHOUT ANY WARRANTY; without even the implied warranty of
     12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13     Library General Public License for more details.
     14 
     15     You should have received a copy of the GNU Library General Public License
     16     along with this library; see the file COPYING.LIB.  If not, write to
     17     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18     Boston, MA 02110-1301, USA.
     19 */
     20 
     21 #include "config.h"
     22 #include "SVGAngle.h"
     23 
     24 #if ENABLE(SVG)
     25 
     26 #include <wtf/MathExtras.h>
     27 
     28 namespace WebCore {
     29 
     30 SVGAngle::SVGAngle()
     31     : m_unitType(SVG_ANGLETYPE_UNKNOWN)
     32     , m_value(0)
     33     , m_valueInSpecifiedUnits(0)
     34 {
     35 }
     36 
     37 SVGAngle::~SVGAngle()
     38 {
     39 }
     40 
     41 SVGAngle::SVGAngleType SVGAngle::unitType() const
     42 {
     43     return m_unitType;
     44 }
     45 
     46 void SVGAngle::setValue(float value)
     47 {
     48     m_value = value;
     49 }
     50 
     51 float SVGAngle::value() const
     52 {
     53     return m_value;
     54 }
     55 
     56 // calc m_value
     57 void SVGAngle::calculate()
     58 {
     59     if (m_unitType == SVG_ANGLETYPE_GRAD)
     60         m_value = grad2deg(m_valueInSpecifiedUnits);
     61     else if (m_unitType == SVG_ANGLETYPE_RAD)
     62         m_value = rad2deg(m_valueInSpecifiedUnits);
     63     else if (m_unitType == SVG_ANGLETYPE_UNSPECIFIED || m_unitType == SVG_ANGLETYPE_DEG)
     64         m_value = m_valueInSpecifiedUnits;
     65 }
     66 
     67 void SVGAngle::setValueInSpecifiedUnits(float valueInSpecifiedUnits)
     68 {
     69     m_valueInSpecifiedUnits = valueInSpecifiedUnits;
     70     calculate();
     71 }
     72 
     73 float SVGAngle::valueInSpecifiedUnits() const
     74 {
     75     return m_valueInSpecifiedUnits;
     76 }
     77 
     78 void SVGAngle::setValueAsString(const String& s)
     79 {
     80     m_valueAsString = s;
     81 
     82     bool bOK;
     83     m_valueInSpecifiedUnits = m_valueAsString.toFloat(&bOK);
     84     m_unitType = SVG_ANGLETYPE_UNSPECIFIED;
     85 
     86     if (!bOK) {
     87         if (m_valueAsString.endsWith("deg"))
     88             m_unitType = SVG_ANGLETYPE_DEG;
     89         else if (m_valueAsString.endsWith("grad"))
     90             m_unitType = SVG_ANGLETYPE_GRAD;
     91         else if (m_valueAsString.endsWith("rad"))
     92             m_unitType = SVG_ANGLETYPE_RAD;
     93     }
     94 
     95     calculate();
     96 }
     97 
     98 String SVGAngle::valueAsString() const
     99 {
    100     m_valueAsString = String::number(m_valueInSpecifiedUnits);
    101 
    102     switch (m_unitType) {
    103         case SVG_ANGLETYPE_UNSPECIFIED:
    104         case SVG_ANGLETYPE_DEG:
    105             m_valueAsString += "deg";
    106             break;
    107         case SVG_ANGLETYPE_RAD:
    108             m_valueAsString += "rad";
    109             break;
    110         case SVG_ANGLETYPE_GRAD:
    111             m_valueAsString += "grad";
    112             break;
    113         case SVG_ANGLETYPE_UNKNOWN:
    114             break;
    115     }
    116 
    117     return m_valueAsString;
    118 }
    119 
    120 void SVGAngle::newValueSpecifiedUnits(unsigned short unitType, float valueInSpecifiedUnits)
    121 {
    122     m_unitType = (SVGAngleType)unitType;
    123     m_valueInSpecifiedUnits = valueInSpecifiedUnits;
    124     calculate();
    125 }
    126 
    127 void SVGAngle::convertToSpecifiedUnits(unsigned short unitType)
    128 {
    129     if (m_unitType == unitType)
    130         return;
    131 
    132     if (m_unitType == SVG_ANGLETYPE_DEG && unitType == SVG_ANGLETYPE_RAD)
    133         m_valueInSpecifiedUnits = deg2rad(m_valueInSpecifiedUnits);
    134     else if (m_unitType == SVG_ANGLETYPE_GRAD && unitType == SVG_ANGLETYPE_RAD)
    135         m_valueInSpecifiedUnits = grad2rad(m_valueInSpecifiedUnits);
    136     else if (m_unitType == SVG_ANGLETYPE_DEG && unitType == SVG_ANGLETYPE_GRAD)
    137         m_valueInSpecifiedUnits = deg2grad(m_valueInSpecifiedUnits);
    138     else if (m_unitType == SVG_ANGLETYPE_RAD && unitType == SVG_ANGLETYPE_GRAD)
    139         m_valueInSpecifiedUnits = rad2grad(m_valueInSpecifiedUnits);
    140     else if (m_unitType == SVG_ANGLETYPE_RAD && unitType == SVG_ANGLETYPE_DEG)
    141         m_valueInSpecifiedUnits = rad2deg(m_valueInSpecifiedUnits);
    142     else if (m_unitType == SVG_ANGLETYPE_GRAD && unitType == SVG_ANGLETYPE_DEG)
    143         m_valueInSpecifiedUnits = grad2deg(m_valueInSpecifiedUnits);
    144 
    145     m_unitType = (SVGAngleType)unitType;
    146 }
    147 
    148 }
    149 
    150 #endif // ENABLE(SVG)
    151