FontUnit.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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(val.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)fs;
  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): Parse(s, CultureInfo.CurrentCulture)
  110. {
  111. }
  112. public static FontUnit Parse(string s, CultureInfo culture)
  113. {
  114. return new FontUnit(s, culture);
  115. }
  116. public static FontUnit Point(int n)
  117. {
  118. return new FontUnit(n);
  119. }
  120. public static bool operator ==(FontUnit left, FontUnit right)
  121. {
  122. return (left.type == right.type && left.val == right.val);
  123. }
  124. public static bool operator !=(FontUnit left, FontUnit right)
  125. {
  126. return !(left == right);
  127. }
  128. public static implicit operator FontUnit(int n)
  129. {
  130. return FontUnit.Point(n);
  131. }
  132. public override bool Equals(object obj)
  133. {
  134. if(obj!= null && obj is FontUnit)
  135. {
  136. FontUnit that = (FontUnit)obj;
  137. return (this.type == that.type && this.val == that.val);
  138. }
  139. return false;
  140. }
  141. public override int GetHashCode()
  142. {
  143. return ( (type.GetHashCode() << 2) | val.GetHashCode() );
  144. }
  145. public override string ToString(): ToString(CultureInfo.CurrentCulture)
  146. {
  147. }
  148. public override string ToString(CultureInfo culture)
  149. {
  150. if(IsEmpty)
  151. {
  152. return String.Empty;
  153. }
  154. //string strRepr = String.Empty;
  155. switch(type)
  156. {
  157. case FontSize.AsUnit: return val.ToString(culture);
  158. case FontSize.XXSmall: return "XX-Small";
  159. case FontSize.XSmall: return "X-Small";
  160. case FontSize.XLarge: return "X-Large";
  161. case FontSize.XXLarge: return "XX-Large";
  162. default: return PropertyConverter.EnumToString(typeof(FontSize), type);
  163. }
  164. }
  165. public bool IsEmpty
  166. {
  167. get
  168. {
  169. return (type == FontSize.NotSet);
  170. }
  171. }
  172. public FontSize Type
  173. {
  174. get
  175. {
  176. return type;
  177. }
  178. }
  179. public Unit Unit
  180. {
  181. get
  182. {
  183. return val;
  184. }
  185. }
  186. }
  187. }