FontInfo.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Dennis Bartok ([email protected])
  24. //
  25. //
  26. using System.ComponentModel;
  27. namespace System.Web.UI.WebControls
  28. {
  29. [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
  30. public sealed class FontInfo
  31. {
  32. [Flags]
  33. internal enum FontStyles
  34. {
  35. None = 0,
  36. Bold = 0x0001,
  37. Italic = 0x0002,
  38. Names = 0x0004,
  39. Overline = 0x0008,
  40. Size = 0x0010,
  41. Strikeout = 0x0020,
  42. Underline = 0x0040
  43. }
  44. #region Fields
  45. private static string[] empty_names = new string[0];
  46. private FontStyles fontstyles;
  47. private StateBag bag;
  48. #endregion // Fields
  49. #region Constructors
  50. private FontInfo()
  51. {
  52. }
  53. internal FontInfo(Style owner)
  54. {
  55. this.bag = owner.ViewState;
  56. }
  57. #endregion // Constructors
  58. #region Public Instance Properties
  59. [Bindable(true)]
  60. [DefaultValue(false)]
  61. [NotifyParentProperty(true)]
  62. [WebSysDescription ("")]
  63. [WebCategory ("Font")]
  64. public bool Bold
  65. {
  66. get
  67. {
  68. if ((fontstyles & FontStyles.Bold) == 0)
  69. {
  70. return false;
  71. }
  72. return bag.GetBool("Font_Bold", false);
  73. }
  74. set
  75. {
  76. fontstyles |= FontStyles.Bold;
  77. bag["Font_Bold"] = value;
  78. }
  79. }
  80. [Bindable(true)]
  81. [DefaultValue(false)]
  82. [NotifyParentProperty(true)]
  83. [WebSysDescription ("")]
  84. [WebCategory ("Font")]
  85. public bool Italic
  86. {
  87. get
  88. {
  89. if ((fontstyles & FontStyles.Italic) == 0)
  90. {
  91. return false;
  92. }
  93. return bag.GetBool("Font_Italic", false);
  94. }
  95. set
  96. {
  97. fontstyles |= FontStyles.Italic;
  98. bag["Font_Italic"] = value;
  99. }
  100. }
  101. [Bindable(true)]
  102. [DefaultValue("")]
  103. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  104. [Editor("System.Drawing.Design.FontNameEditor, " + Consts.AssemblySystem_Drawing_Design, typeof(System.Drawing.Design.UITypeEditor))]
  105. [NotifyParentProperty(true)]
  106. [TypeConverter (typeof(System.Drawing.FontConverter.FontNameConverter))]
  107. [WebSysDescription ("")]
  108. [WebCategory ("Font")]
  109. public string Name
  110. {
  111. get
  112. {
  113. string[] names;
  114. if ((fontstyles & FontStyles.Names) == 0)
  115. {
  116. return string.Empty;
  117. }
  118. names = (string[])bag["Font_Names"];
  119. if (names.Length == 0)
  120. {
  121. return string.Empty;
  122. }
  123. return names[0];
  124. }
  125. set
  126. {
  127. // Seems to be a special case in MS, removing the names from the bag when Name is set to empty,
  128. // but not when setting Names to an empty array
  129. if (value == string.Empty) {
  130. bag.Remove("Font_Names");
  131. return;
  132. }
  133. if (value == null) {
  134. throw new ArgumentNullException("value", "Font name cannot be null");
  135. }
  136. Names = new string[1] { value };
  137. }
  138. }
  139. [Editor("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  140. [NotifyParentProperty(true)]
  141. [TypeConverter(typeof(System.Web.UI.WebControls.FontNamesConverter))]
  142. [WebSysDescription ("")]
  143. [WebCategory ("Font")]
  144. public string[] Names
  145. {
  146. get
  147. {
  148. string[] ret;
  149. if ((fontstyles & FontStyles.Names) == 0)
  150. {
  151. return FontInfo.empty_names;
  152. }
  153. ret = (string[])bag["Font_Names"];
  154. if (ret != null) {
  155. return ret;
  156. }
  157. return FontInfo.empty_names;
  158. }
  159. set
  160. {
  161. fontstyles |= FontStyles.Names;
  162. bag["Font_Names"] = value;
  163. }
  164. }
  165. [Bindable(true)]
  166. [DefaultValue(false)]
  167. [NotifyParentProperty(true)]
  168. [WebSysDescription ("")]
  169. [WebCategory ("Font")]
  170. public bool Overline
  171. {
  172. get
  173. {
  174. if ((fontstyles & FontStyles.Overline) == 0)
  175. {
  176. return false;
  177. }
  178. return bag.GetBool("Font_Overline", false);
  179. }
  180. set
  181. {
  182. fontstyles |= FontStyles.Overline;
  183. bag["Font_Overline"] = value;
  184. }
  185. }
  186. [Bindable(true)]
  187. [DefaultValue(typeof (FontUnit), "")]
  188. [NotifyParentProperty(true)]
  189. [WebSysDescription ("")]
  190. [WebCategory ("Font")]
  191. public FontUnit Size
  192. {
  193. get
  194. {
  195. if ((fontstyles & FontStyles.Size) == 0)
  196. {
  197. return FontUnit.Empty;
  198. }
  199. return (FontUnit)bag["Font_Size"];
  200. }
  201. set
  202. {
  203. if (value.Unit.Value < 0)
  204. {
  205. throw new ArgumentOutOfRangeException("Value", value.Unit.Value, "Font size cannot be negative");
  206. }
  207. fontstyles |= FontStyles.Size;
  208. bag["Font_Size"] = value;
  209. }
  210. }
  211. [Bindable(true)]
  212. [DefaultValue(false)]
  213. [NotifyParentProperty(true)]
  214. [WebSysDescription ("")]
  215. [WebCategory ("Font")]
  216. public bool Strikeout
  217. {
  218. get
  219. {
  220. if ((fontstyles & FontStyles.Strikeout) == 0)
  221. {
  222. return false;
  223. }
  224. return bag.GetBool("Font_Strikeout", false);
  225. }
  226. set
  227. {
  228. fontstyles |= FontStyles.Strikeout;
  229. bag["Font_Strikeout"] = value;
  230. }
  231. }
  232. [Bindable(true)]
  233. [DefaultValue(false)]
  234. [NotifyParentProperty(true)]
  235. [WebSysDescription ("")]
  236. [WebCategory ("Font")]
  237. public bool Underline
  238. {
  239. get
  240. {
  241. if ((fontstyles & FontStyles.Underline) == 0)
  242. {
  243. return false;
  244. }
  245. return bag.GetBool("Font_Underline", false);
  246. }
  247. set
  248. {
  249. fontstyles |= FontStyles.Underline;
  250. bag["Font_Underline"] = value;
  251. }
  252. }
  253. #endregion // Public Instance Properties
  254. #region Public Instance Methods
  255. public void CopyFrom(FontInfo f)
  256. {
  257. this.Reset();
  258. // MS does not store the property in the bag if it's value is false
  259. if (((f.fontstyles & FontStyles.Bold) != 0) && f.Bold)
  260. {
  261. this.Bold = true;
  262. }
  263. if (((f.fontstyles & FontStyles.Italic) != 0) && f.Italic)
  264. {
  265. this.Italic = true;
  266. }
  267. // MS seems to have some weird behaviour, even if f's Name has been set to String.Empty we still get an empty array
  268. if (((f.fontstyles & FontStyles.Names) != 0))
  269. {
  270. this.Names = f.Names;
  271. }
  272. if (((f.fontstyles & FontStyles.Overline) != 0) && f.Overline)
  273. {
  274. this.Overline = true;
  275. }
  276. if (((f.fontstyles & FontStyles.Size) != 0) && (f.Size != FontUnit.Empty))
  277. {
  278. this.Size = f.Size;
  279. }
  280. if (((f.fontstyles & FontStyles.Strikeout) != 0) && f.Strikeout)
  281. {
  282. this.Strikeout = true;
  283. }
  284. if (((f.fontstyles & FontStyles.Underline) != 0) && f.Underline)
  285. {
  286. this.Underline = true;
  287. }
  288. }
  289. public void MergeWith(FontInfo f)
  290. {
  291. if (((fontstyles & FontStyles.Bold) == 0) && ((f.fontstyles & FontStyles.Bold) != 0) && f.Bold)
  292. {
  293. this.Bold = true;
  294. }
  295. if (((fontstyles & FontStyles.Italic) == 0) && ((f.fontstyles & FontStyles.Italic) != 0) && f.Italic)
  296. {
  297. this.Italic = true;
  298. }
  299. if (((fontstyles & FontStyles.Names) == 0) && ((f.fontstyles & FontStyles.Names) != 0))
  300. {
  301. this.Names = f.Names;
  302. }
  303. if (((fontstyles & FontStyles.Overline) == 0) && ((f.fontstyles & FontStyles.Overline) != 0) && f.Overline)
  304. {
  305. this.Overline = true;
  306. }
  307. if (((fontstyles & FontStyles.Size) == 0) && ((f.fontstyles & FontStyles.Size) != 0) && (f.Size != FontUnit.Empty))
  308. {
  309. this.Size = f.Size;
  310. }
  311. if (((fontstyles & FontStyles.Strikeout) == 0) && ((f.fontstyles & FontStyles.Strikeout) != 0) && f.Strikeout)
  312. {
  313. this.Strikeout = true;
  314. }
  315. if (((fontstyles & FontStyles.Underline) == 0) && ((f.fontstyles & FontStyles.Underline) != 0) && f.Underline)
  316. {
  317. this.Underline = true;
  318. }
  319. }
  320. [MonoTODO]
  321. public bool ShouldSerializeNames()
  322. {
  323. throw new NotImplementedException("Microsoft Internal, not sure what to do");
  324. }
  325. public override string ToString()
  326. {
  327. if (this.Names.Length == 0)
  328. {
  329. return string.Empty;
  330. }
  331. return this.Name + ", " + this.Size.ToString();
  332. }
  333. #endregion // Public Instance Methods
  334. #region Private Methods
  335. internal void Reset()
  336. {
  337. bag.Remove("Font_Bold");
  338. bag.Remove("Font_Italic");
  339. bag.Remove("Font_Names");
  340. bag.Remove("Font_Overline");
  341. bag.Remove("Font_Size");
  342. bag.Remove("Font_Strikeout");
  343. bag.Remove("Font_Underline");
  344. fontstyles = FontStyles.None;
  345. }
  346. internal void LoadViewState() {
  347. fontstyles = FontStyles.None;
  348. if (bag["Font_Bold"] != null)
  349. {
  350. fontstyles |= FontStyles.Bold;
  351. }
  352. if (bag["Font_Italic"] != null)
  353. {
  354. fontstyles |= FontStyles.Italic;
  355. }
  356. if (bag["Font_Names"] != null)
  357. {
  358. fontstyles |= FontStyles.Names;
  359. }
  360. if (bag["Font_Overline"] != null)
  361. {
  362. fontstyles |= FontStyles.Overline;
  363. }
  364. if (bag["Font_Size"] != null)
  365. {
  366. fontstyles |= FontStyles.Size;
  367. }
  368. if (bag["Font_Strikeout"] != null)
  369. {
  370. fontstyles |= FontStyles.Strikeout;
  371. }
  372. if (bag["Font_Underline"] != null)
  373. {
  374. fontstyles |= FontStyles.Underline;
  375. }
  376. }
  377. #endregion // Private Methods
  378. }
  379. }