FontUnit.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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: FontUnit
  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.Collections;
  35. using System.ComponentModel;
  36. using System.Globalization;
  37. using System.Web;
  38. using System.Web.UI;
  39. namespace System.Web.UI.WebControls
  40. {
  41. [TypeConverter(typeof(FontUnitConverter))]
  42. public struct FontUnit
  43. {
  44. public static readonly FontUnit Empty = new FontUnit();
  45. public static readonly FontUnit Large = new FontUnit(FontSize.Large);
  46. public static readonly FontUnit Larger = new FontUnit(FontSize.Larger);
  47. public static readonly FontUnit Medium = new FontUnit(FontSize.Medium);
  48. public static readonly FontUnit Small = new FontUnit(FontSize.Small);
  49. public static readonly FontUnit Smaller = new FontUnit(FontSize.Smaller);
  50. public static readonly FontUnit XLarge = new FontUnit(FontSize.XLarge);
  51. public static readonly FontUnit XSmall = new FontUnit(FontSize.XSmall);
  52. public static readonly FontUnit XXLarge = new FontUnit(FontSize.XXLarge);
  53. public static readonly FontUnit XXSmall = new FontUnit(FontSize.XXSmall);
  54. private FontSize type;
  55. private Unit val;
  56. private static Hashtable sizeTable;
  57. static FontUnit ()
  58. {
  59. sizeTable = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
  60. CaseInsensitiveComparer.Default);
  61. sizeTable.Add ("smaller", 2);
  62. sizeTable.Add ("larger", 3);
  63. sizeTable.Add ("xx-small", 4);
  64. sizeTable.Add ("xxsmall", 4);
  65. sizeTable.Add ("x-small", 5);
  66. sizeTable.Add ("xsmall", 5);
  67. sizeTable.Add ("small", 6);
  68. sizeTable.Add ("medium", 7);
  69. sizeTable.Add ("large", 8);
  70. sizeTable.Add ("xlarge", 9);
  71. sizeTable.Add ("x-large", 9);
  72. sizeTable.Add ("xxlarge", 10);
  73. sizeTable.Add ("xx-large", 10);
  74. }
  75. public FontUnit(FontSize type)
  76. {
  77. if(!Enum.IsDefined(typeof(FontSize), type))
  78. throw new ArgumentException();
  79. this.type = type;
  80. if(this.type == FontSize.AsUnit)
  81. {
  82. val = Unit.Point(10);
  83. } else
  84. {
  85. val = Unit.Empty;
  86. }
  87. }
  88. public FontUnit(int value)
  89. {
  90. type = FontSize.AsUnit;
  91. val = Unit.Point(value);
  92. }
  93. public FontUnit(string value): this(value, CultureInfo.CurrentCulture)
  94. {
  95. }
  96. public FontUnit(Unit value)
  97. {
  98. if(value.IsEmpty)
  99. {
  100. type = FontSize.NotSet;
  101. val = Unit.Empty;
  102. } else
  103. {
  104. type = FontSize.AsUnit;
  105. val = value;
  106. }
  107. }
  108. public FontUnit(string value, CultureInfo culture)
  109. {
  110. type = FontSize.NotSet;
  111. val = Unit.Empty;
  112. if(value != null && value != String.Empty)
  113. {
  114. string low = value.ToLower(culture);
  115. int size = GetTypeFromString(low);
  116. if (size != -1)
  117. {
  118. type = (FontSize)size;
  119. } else {
  120. val = new Unit(value, culture, UnitType.Point);
  121. type = FontSize.AsUnit;
  122. }
  123. }
  124. }
  125. private static int GetTypeFromString(string strVal)
  126. {
  127. if (!(sizeTable.ContainsKey (strVal)))
  128. return -1;
  129. return (int) sizeTable [strVal];
  130. }
  131. public static FontUnit Parse(string s)
  132. {
  133. return Parse(s, CultureInfo.CurrentCulture);
  134. }
  135. public static FontUnit Parse(string s, CultureInfo culture)
  136. {
  137. return new FontUnit(s, culture);
  138. }
  139. public static FontUnit Point(int n)
  140. {
  141. return new FontUnit(n);
  142. }
  143. public static bool operator ==(FontUnit left, FontUnit right)
  144. {
  145. return (left.type == right.type && left.val == right.val);
  146. }
  147. public static bool operator !=(FontUnit left, FontUnit right)
  148. {
  149. return !(left == right);
  150. }
  151. public static implicit operator FontUnit(int n)
  152. {
  153. return FontUnit.Point(n);
  154. }
  155. public override bool Equals(object obj)
  156. {
  157. if(obj!= null && obj is FontUnit)
  158. {
  159. FontUnit that = (FontUnit)obj;
  160. return (this.type == that.type && this.val == that.val);
  161. }
  162. return false;
  163. }
  164. public override int GetHashCode()
  165. {
  166. return ( (type.GetHashCode() << 2) | val.GetHashCode() );
  167. }
  168. public override string ToString()
  169. {
  170. return ToString(CultureInfo.CurrentCulture);
  171. }
  172. public string ToString(CultureInfo culture)
  173. {
  174. if(IsEmpty)
  175. {
  176. return String.Empty;
  177. }
  178. //string strRepr = String.Empty;
  179. switch(type)
  180. {
  181. case FontSize.AsUnit: return val.ToString(culture);
  182. case FontSize.XXSmall: return "XX-Small";
  183. case FontSize.XSmall: return "X-Small";
  184. case FontSize.XLarge: return "X-Large";
  185. case FontSize.XXLarge: return "XX-Large";
  186. default: return PropertyConverter.EnumToString(typeof(FontSize), type);
  187. }
  188. }
  189. public bool IsEmpty
  190. {
  191. get
  192. {
  193. return (type == FontSize.NotSet);
  194. }
  195. }
  196. public FontSize Type
  197. {
  198. get
  199. {
  200. return type;
  201. }
  202. }
  203. public Unit Unit
  204. {
  205. get
  206. {
  207. return val;
  208. }
  209. }
  210. }
  211. }