KHtml

SVGAngle.cpp
1 /*
2  Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <[email protected]>
3  2004, 2005, 2006 Rob Buis <[email protected]>
4 
5  This file is part of the KDE project
6 
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.
11 
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.
16 
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.
21 */
22 
23 #include "wtf/Platform.h"
24 
25 #if ENABLE(SVG)
26 #include "SVGAngle.h"
27 
28 #include <math.h>
29 #include <wtf/MathExtras.h>
30 
31 namespace WebCore
32 {
33 
34 SVGAngle::SVGAngle()
35  : RefCounted<SVGAngle>(0)
36  , m_unitType(SVG_ANGLETYPE_UNKNOWN)
37  , m_value(0)
38  , m_valueInSpecifiedUnits(0)
39 {
40 }
41 
42 SVGAngle::~SVGAngle()
43 {
44 }
45 
46 SVGAngle::SVGAngleType SVGAngle::unitType() const
47 {
48  return m_unitType;
49 }
50 
51 void SVGAngle::setValue(float value)
52 {
53  m_value = value;
54 }
55 
56 float SVGAngle::value() const
57 {
58  return m_value;
59 }
60 
61 // calc m_value
62 void SVGAngle::calculate()
63 {
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;
70  }
71 }
72 
73 void SVGAngle::setValueInSpecifiedUnits(float valueInSpecifiedUnits)
74 {
75  m_valueInSpecifiedUnits = valueInSpecifiedUnits;
76  calculate();
77 }
78 
79 float SVGAngle::valueInSpecifiedUnits() const
80 {
81  return m_valueInSpecifiedUnits;
82 }
83 
84 void SVGAngle::setValueAsString(const String &s)
85 {
86  m_valueAsString = s;
87 
88  bool bOK;
89  m_valueInSpecifiedUnits = m_valueAsString.toFloat(&bOK);
90  m_unitType = SVG_ANGLETYPE_UNSPECIFIED;
91 
92  if (!bOK) {
93  if (m_valueAsString.endsWith("deg")) {
94  m_unitType = SVG_ANGLETYPE_DEG;
95  } else if (m_valueAsString.endsWith("grad")) {
96  m_unitType = SVG_ANGLETYPE_GRAD;
97  } else if (m_valueAsString.endsWith("rad")) {
98  m_unitType = SVG_ANGLETYPE_RAD;
99  }
100  }
101 
102  calculate();
103 }
104 
105 String SVGAngle::valueAsString() const
106 {
107  m_valueAsString = String::number(m_valueInSpecifiedUnits);
108 
109  switch (m_unitType) {
110  case SVG_ANGLETYPE_UNSPECIFIED:
111  case SVG_ANGLETYPE_DEG:
112  m_valueAsString += "deg";
113  break;
114  case SVG_ANGLETYPE_RAD:
115  m_valueAsString += "rad";
116  break;
117  case SVG_ANGLETYPE_GRAD:
118  m_valueAsString += "grad";
119  break;
120  case SVG_ANGLETYPE_UNKNOWN:
121  break;
122  }
123 
124  return m_valueAsString;
125 }
126 
127 void SVGAngle::newValueSpecifiedUnits(unsigned short unitType, float valueInSpecifiedUnits)
128 {
129  m_unitType = (SVGAngleType)unitType;
130  m_valueInSpecifiedUnits = valueInSpecifiedUnits;
131  calculate();
132 }
133 
134 void SVGAngle::convertToSpecifiedUnits(unsigned short unitType)
135 {
136  if (m_unitType == unitType) {
137  return;
138  }
139 
140  if (m_unitType == SVG_ANGLETYPE_DEG && unitType == SVG_ANGLETYPE_RAD) {
141  m_valueInSpecifiedUnits = deg2rad(m_valueInSpecifiedUnits);
142  } else if (m_unitType == SVG_ANGLETYPE_GRAD && unitType == SVG_ANGLETYPE_RAD) {
143  m_valueInSpecifiedUnits = grad2rad(m_valueInSpecifiedUnits);
144  } else if (m_unitType == SVG_ANGLETYPE_DEG && unitType == SVG_ANGLETYPE_GRAD) {
145  m_valueInSpecifiedUnits = deg2grad(m_valueInSpecifiedUnits);
146  } else if (m_unitType == SVG_ANGLETYPE_RAD && unitType == SVG_ANGLETYPE_GRAD) {
147  m_valueInSpecifiedUnits = rad2grad(m_valueInSpecifiedUnits);
148  } else if (m_unitType == SVG_ANGLETYPE_RAD && unitType == SVG_ANGLETYPE_DEG) {
149  m_valueInSpecifiedUnits = rad2deg(m_valueInSpecifiedUnits);
150  } else if (m_unitType == SVG_ANGLETYPE_GRAD && unitType == SVG_ANGLETYPE_DEG) {
151  m_valueInSpecifiedUnits = grad2deg(m_valueInSpecifiedUnits);
152  }
153 
154  m_unitType = (SVGAngleType)unitType;
155 }
156 
157 // Helpers
158 double SVGAngle::todeg(double rad)
159 {
160  return rad2deg(rad);
161 }
162 
163 double SVGAngle::torad(double deg)
164 {
165  return deg2rad(deg);
166 }
167 
168 double SVGAngle::shortestArcBisector(double angle1, double angle2)
169 {
170  double bisector = (angle1 + angle2) / 2;
171 
172  if (fabs(angle1 - angle2) > 180) {
173  bisector += 180;
174  }
175 
176  return bisector;
177 }
178 
179 }
180 
181 #endif // ENABLE(SVG)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 22:48:22 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.