1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Globalization;
|
---|
4 | using System.Text.RegularExpressions;
|
---|
5 |
|
---|
6 | using SharpVectors.Dom.Css;
|
---|
7 |
|
---|
8 | namespace SharpVectors.Dom.Svg
|
---|
9 | {
|
---|
10 | public sealed class SvgNumber : ISvgNumber
|
---|
11 | {
|
---|
12 | #region Private Fields
|
---|
13 |
|
---|
14 | private double _value;
|
---|
15 |
|
---|
16 | private static string numberPattern = @"(?<number>(\+|-)?\d*\.?\d+((e|E)(\+|-)?\d+)?)";
|
---|
17 | private static Regex reNumber = new Regex("^" + numberPattern + "$");
|
---|
18 |
|
---|
19 | private static Regex reUnit = new Regex("[a-z]+$");
|
---|
20 |
|
---|
21 | #endregion
|
---|
22 |
|
---|
23 | #region Constructors and Destructor
|
---|
24 |
|
---|
25 | public SvgNumber(float val)
|
---|
26 | {
|
---|
27 | _value = val;
|
---|
28 | }
|
---|
29 |
|
---|
30 | public SvgNumber(string str)
|
---|
31 | {
|
---|
32 | _value = SvgNumber.ParseNumber(str);
|
---|
33 | }
|
---|
34 |
|
---|
35 | #endregion
|
---|
36 |
|
---|
37 | #region Public Static Properties
|
---|
38 |
|
---|
39 | public static NumberFormatInfo Format
|
---|
40 | {
|
---|
41 | get
|
---|
42 | {
|
---|
43 | return CssNumber.Format;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | #endregion
|
---|
48 |
|
---|
49 | #region Public Static Methods
|
---|
50 |
|
---|
51 | public static string ScientificToDec(string sc)
|
---|
52 | {
|
---|
53 | if (sc.IndexOfAny(new char[]{'e','E'})>-1)
|
---|
54 | {
|
---|
55 | sc = sc.Trim();
|
---|
56 | // remove the unit
|
---|
57 | Match match = reUnit.Match(sc);
|
---|
58 | return SvgNumber.ParseNumber(sc.Substring(0, sc.Length - match.Length)).ToString(Format) + match.Value;
|
---|
59 | }
|
---|
60 | else
|
---|
61 | {
|
---|
62 | return sc;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public static double ParseNumber(string str)
|
---|
67 | {
|
---|
68 | try
|
---|
69 | {
|
---|
70 | return Double.Parse(str, SvgNumber.Format);
|
---|
71 | }
|
---|
72 | catch (Exception e)
|
---|
73 | {
|
---|
74 | throw new DomException(DomExceptionType.SyntaxErr,
|
---|
75 | "Input string was not in a correct format: " + str, e);
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | //public static double ParseNumber(string str)
|
---|
80 | //{
|
---|
81 | // double val;
|
---|
82 | // int index = str.IndexOfAny(new Char[] { 'E', 'e' });
|
---|
83 | // if (index > -1)
|
---|
84 | // {
|
---|
85 | // double number = SvgNumber.ParseNumber(str.Substring(0, index));
|
---|
86 | // double power = SvgNumber.ParseNumber(str.Substring(index + 1));
|
---|
87 |
|
---|
88 | // val = Math.Pow(10, power) * number;
|
---|
89 | // }
|
---|
90 | // else
|
---|
91 | // {
|
---|
92 | // try
|
---|
93 | // {
|
---|
94 | // val = Double.Parse(str, SvgNumber.Format);
|
---|
95 | // }
|
---|
96 | // catch (Exception e)
|
---|
97 | // {
|
---|
98 | // throw new DomException(DomExceptionType.SyntaxErr,
|
---|
99 | // "Input string was not in a correct format: " + str, e);
|
---|
100 | // }
|
---|
101 | // }
|
---|
102 |
|
---|
103 | // return val;
|
---|
104 | //}
|
---|
105 |
|
---|
106 | //public static float ParseNumber(string str)
|
---|
107 | //{
|
---|
108 | // float val;
|
---|
109 | // int index = str.IndexOfAny(new Char[]{'E','e'});
|
---|
110 | // if (index>-1)
|
---|
111 | // {
|
---|
112 | // float number = SvgNumber.ParseNumber(str.Substring(0, index));
|
---|
113 | // float power = SvgNumber.ParseNumber(str.Substring(index+1));
|
---|
114 |
|
---|
115 | // val = (float) Math.Pow(10, power) * number;
|
---|
116 | // }
|
---|
117 | // else
|
---|
118 | // {
|
---|
119 | // try
|
---|
120 | // {
|
---|
121 | // val = Single.Parse(str, SvgNumber.Format);
|
---|
122 | // }
|
---|
123 | // catch(Exception e)
|
---|
124 | // {
|
---|
125 | // throw new DomException(DomExceptionType.SyntaxErr,
|
---|
126 | // "Input string was not in a correct format: " + str, e);
|
---|
127 | // }
|
---|
128 | // }
|
---|
129 |
|
---|
130 | // return val;
|
---|
131 | //}
|
---|
132 |
|
---|
133 | public static double CalcAngleDiff(double a1, double a2)
|
---|
134 | {
|
---|
135 | while(a1 < 0) a1 += 360;
|
---|
136 | a1 %= 360;
|
---|
137 |
|
---|
138 | while(a2 < 0) a2 += 360;
|
---|
139 | a2 %= 360;
|
---|
140 |
|
---|
141 | double diff = (a1 - a2);
|
---|
142 |
|
---|
143 | while(diff<0) diff += 360;
|
---|
144 | diff %= 360;
|
---|
145 |
|
---|
146 | return diff;
|
---|
147 | }
|
---|
148 |
|
---|
149 | public static double CalcAngleBisection(double a1, double a2)
|
---|
150 | {
|
---|
151 | double diff = CalcAngleDiff(a1, a2);
|
---|
152 | double bisect = a1 - diff / 2F;
|
---|
153 |
|
---|
154 | while (bisect < 0)
|
---|
155 | bisect += 360;
|
---|
156 |
|
---|
157 | bisect %= 360;
|
---|
158 | return bisect;
|
---|
159 | }
|
---|
160 |
|
---|
161 | #endregion
|
---|
162 |
|
---|
163 | #region ISvgNumber Nembers
|
---|
164 |
|
---|
165 | public double Value
|
---|
166 | {
|
---|
167 | get
|
---|
168 | {
|
---|
169 | return _value;
|
---|
170 | }
|
---|
171 | set
|
---|
172 | {
|
---|
173 | this._value = value;
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | #endregion
|
---|
178 | }
|
---|
179 | }
|
---|