Unit.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Namespace: System.Web.UI.WebControls
  23. * Struct: Unit
  24. *
  25. * Author: Gaurav Vaish
  26. * Maintainer: [email protected]
  27. * Contact: <[email protected]>, <[email protected]>
  28. * Implementation: yes
  29. * Status: 100%
  30. *
  31. * (C) Gaurav Vaish (2001)
  32. */
  33. using System;
  34. using System.Globalization;
  35. using System.ComponentModel;
  36. using System.Web;
  37. using System.Web.UI;
  38. namespace System.Web.UI.WebControls
  39. {
  40. [TypeConverter(typeof(UnitConverter))]
  41. public struct Unit
  42. {
  43. public static readonly Unit Empty = new Unit();
  44. private static int Min = -32768;
  45. private static int Max = +32767;
  46. private UnitType type;
  47. private double val;
  48. public static Unit Parse(string s)
  49. {
  50. return new Unit(s);
  51. }
  52. public static Unit Parse(string s, CultureInfo culture)
  53. {
  54. return new Unit(s, culture);
  55. }
  56. public static Unit Percentage(double n)
  57. {
  58. return new Unit (n, UnitType.Percentage);
  59. }
  60. public static Unit Pixel(int n)
  61. {
  62. return new Unit (n, UnitType.Pixel);
  63. }
  64. public static Unit Point(int n)
  65. {
  66. return new Unit(n, UnitType.Point);
  67. }
  68. public static bool operator ==(Unit left, Unit right)
  69. {
  70. return (left.type == right.type && left.val == right.val);
  71. }
  72. public static bool operator !=(Unit left, Unit right)
  73. {
  74. return !(left == right);
  75. }
  76. public static implicit operator Unit(int n)
  77. {
  78. return new Unit(n);
  79. }
  80. public Unit(double value)
  81. {
  82. if(value < Min || value > Max)
  83. {
  84. throw new ArgumentOutOfRangeException();
  85. }
  86. val = value;
  87. type = UnitType.Pixel;
  88. }
  89. public Unit(int value)
  90. {
  91. if(value < Min || value > Max)
  92. {
  93. throw new ArgumentOutOfRangeException();
  94. }
  95. val = value;
  96. type = UnitType.Pixel;
  97. }
  98. public Unit(string value): this(value, CultureInfo.CurrentCulture)
  99. {
  100. }
  101. public Unit(double value, UnitType type)
  102. {
  103. if(value < Min || value > Max)
  104. {
  105. throw new ArgumentOutOfRangeException();
  106. }
  107. val = value;
  108. this.type = type;
  109. }
  110. public Unit(string value, CultureInfo culture): this(value, culture, UnitType.Pixel)
  111. {
  112. }
  113. internal Unit(string value, CultureInfo culture, UnitType defType)
  114. {
  115. string valueTrim;
  116. if (value == null || (valueTrim = value.Trim ()).Length == 0) {
  117. val = 0;
  118. type = (UnitType)0;
  119. return;
  120. }
  121. if (culture == null)
  122. culture = CultureInfo.CurrentCulture;
  123. string strVal = valueTrim.ToLower ();
  124. int length = strVal.Length;
  125. char c;
  126. int start = -1;
  127. for (int i = 0; i < length; i++) {
  128. c = strVal [i];
  129. if( (c >= '0' && c <= '9') || (c == '-' || c == '.' || c == ',') )
  130. start = i;
  131. }
  132. if (start == -1)
  133. throw new ArgumentException("No digits in 'value'");
  134. start++;
  135. if (start < length) {
  136. type = GetTypeFromString (strVal.Substring (start).Trim ());
  137. val = 0;
  138. } else {
  139. type = defType;
  140. }
  141. try {
  142. string numbers = strVal.Substring (0, start);
  143. if (type == UnitType.Pixel)
  144. val = (double) Int32.Parse (numbers, culture);
  145. else
  146. val = (double) Single.Parse (numbers, culture);
  147. } catch (Exception) {
  148. throw new FormatException ("Error parsing " + value);
  149. }
  150. if (val < Min || val > Max)
  151. throw new ArgumentOutOfRangeException ();
  152. }
  153. private static UnitType GetTypeFromString(string s)
  154. {
  155. if(s == null || s.Length == 0)
  156. return UnitType.Pixel;
  157. switch(s)
  158. {
  159. case "px":
  160. return UnitType.Pixel;
  161. case "pt":
  162. return UnitType.Point;
  163. case "pc":
  164. return UnitType.Pica;
  165. case "in":
  166. return UnitType.Inch;
  167. case "mm":
  168. return UnitType.Mm;
  169. case "cm":
  170. return UnitType.Cm;
  171. case "%":
  172. return UnitType.Percentage;
  173. case "em":
  174. return UnitType.Em;
  175. case "ex":
  176. return UnitType.Ex;
  177. default:
  178. return UnitType.Pixel;
  179. }
  180. }
  181. private string GetStringFromPixel(UnitType ut)
  182. {
  183. switch(ut)
  184. {
  185. case UnitType.Pixel:
  186. return "px";
  187. case UnitType.Point:
  188. return "pt";
  189. case UnitType.Pica:
  190. return "pc";
  191. case UnitType.Inch:
  192. return "in";
  193. case UnitType.Mm:
  194. return "mm";
  195. case UnitType.Cm:
  196. return "cm";
  197. case UnitType.Percentage:
  198. return "%";
  199. case UnitType.Em:
  200. return "em";
  201. case UnitType.Ex:
  202. return "ex";
  203. default:
  204. return String.Empty;
  205. }
  206. }
  207. public bool IsEmpty
  208. {
  209. get
  210. {
  211. return (type == 0);
  212. }
  213. }
  214. public UnitType Type
  215. {
  216. get
  217. {
  218. if(IsEmpty)
  219. return UnitType.Pixel;
  220. return type;
  221. }
  222. }
  223. public double Value
  224. {
  225. get
  226. {
  227. return val;
  228. }
  229. }
  230. public override bool Equals(object obj)
  231. {
  232. if(obj != null && obj is Unit)
  233. {
  234. Unit that = (Unit)obj;
  235. return ( this.type == that.type && this.val == that.val );
  236. }
  237. return false;
  238. }
  239. public override int GetHashCode()
  240. {
  241. return ( (type.GetHashCode() << 2) | (val.GetHashCode()) );
  242. }
  243. public override string ToString()
  244. {
  245. if(IsEmpty)
  246. return String.Empty;
  247. return ( val.ToString() + GetStringFromPixel(type) );
  248. }
  249. public string ToString(CultureInfo culture)
  250. {
  251. if(IsEmpty)
  252. return String.Empty;
  253. return ( val.ToString(culture.NumberFormat) + GetStringFromPixel(type) );
  254. }
  255. }
  256. }