FontUnit.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.Globalization;
  15. using System.Web;
  16. using System.Web.UI;
  17. namespace System.Web.UI.WebControls
  18. {
  19. public struct FontUnit
  20. {
  21. public static readonly FontUnit Empty = new FontUnit();
  22. public static readonly FontUnit Large = new FontUnit(FontSize.Large);
  23. public static readonly FontUnit Larger = new FontUnit(FontSize.Larger);
  24. public static readonly FontUnit Medium = new FontUnit(FontSize.Medium);
  25. public static readonly FontUnit Small = new FontUnit(FontSize.Small);
  26. public static readonly FontUnit Smaller = new FontUnit(FontSize.Smaller);
  27. public static readonly FontUnit XLarge = new FontUnit(FontSize.XLarge);
  28. public static readonly FontUnit XSmall = new FontUnit(FontSize.XSmall);
  29. public static readonly FontUnit XXLarge = new FontUnit(FontSize.XXLarge);
  30. public static readonly FontUnit XXSmall = new FontUnit(FontSize.XXSmall);
  31. private FontSize type;
  32. private Unit val;
  33. public FontUnit(FontSize type)
  34. {
  35. if(!Enum.IsDefined(typeof(FontSize), type))
  36. throw new ArgumentException();
  37. this.type = type;
  38. if(this.type == FontSize.AsUnit)
  39. {
  40. val = Unit.Point(10);
  41. } else
  42. {
  43. val = Unit.Empty;
  44. }
  45. }
  46. public FontUnit(int value)
  47. {
  48. type = FontSize.AsUnit;
  49. val = Unit.Point(value);
  50. }
  51. public FontUnit(string value): this(value, CultureInfo.CurrentCulture)
  52. {
  53. }
  54. public FontUnit(Unit value)
  55. {
  56. if(value.IsEmpty)
  57. {
  58. type = FontSize.NotSet;
  59. val = Unit.Empty;
  60. } else
  61. {
  62. type = FontSize.AsUnit;
  63. val = value;
  64. }
  65. }
  66. public FontUnit(string value, CultureInfo culture)
  67. {
  68. type = FontSize.NotSet;
  69. val = Unit.Empty;
  70. if(value != null && value != String.Empty)
  71. {
  72. string low = value.ToLower(culture);
  73. int index = GetTypeFromString(low);
  74. if( index != -1)
  75. {
  76. type = (FontSize)index;
  77. return;
  78. } else
  79. {
  80. val = new Unit(value, culture, UnitType.Point);
  81. type = FontSize.AsUnit;
  82. }
  83. }
  84. }
  85. private int GetTypeFromString(string strVal)
  86. {
  87. string[] values = {
  88. "smaller",
  89. "larger",
  90. "xx-small",
  91. "x-small",
  92. "small",
  93. "medium",
  94. "large",
  95. "xlarge",
  96. "xxlarge"
  97. };
  98. int i = 0;
  99. foreach(string valType in values)
  100. {
  101. if(strVal == valType)
  102. {
  103. return (i + 2);
  104. }
  105. i++;
  106. }
  107. return -1;
  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. }