2
0

FontUnit.cs 4.8 KB

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