FontUnit.cs 4.9 KB

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