FontInfo.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. using System.Security.Permissions;
  28. namespace System.Web.UI.WebControls {
  29. // CAS
  30. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  31. // attributes
  32. [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
  33. public sealed class FontInfo
  34. {
  35. [Flags]
  36. internal enum FontStyles
  37. {
  38. None = 0,
  39. Bold = 0x0001,
  40. Italic = 0x0002,
  41. Names = 0x0004,
  42. Overline = 0x0008,
  43. Size = 0x0010,
  44. Strikeout = 0x0020,
  45. Underline = 0x0040
  46. }
  47. #region Fields
  48. private static string[] empty_names = new string[0];
  49. private FontStyles fontstyles;
  50. private StateBag bag;
  51. #endregion // Fields
  52. #region Constructors
  53. internal FontInfo(Style owner)
  54. {
  55. this.bag = owner.ViewState;
  56. }
  57. #endregion // Constructors
  58. #region Public Instance Properties
  59. #if ONLY_1_1
  60. [Bindable(true)]
  61. #endif
  62. [DefaultValue(false)]
  63. [NotifyParentProperty(true)]
  64. [WebSysDescription ("")]
  65. [WebCategory ("Font")]
  66. public bool Bold
  67. {
  68. get
  69. {
  70. if ((fontstyles & FontStyles.Bold) == 0)
  71. {
  72. return false;
  73. }
  74. return bag.GetBool("Font_Bold", false);
  75. }
  76. set
  77. {
  78. fontstyles |= FontStyles.Bold;
  79. bag["Font_Bold"] = value;
  80. }
  81. }
  82. #if ONLY_1_1
  83. [Bindable(true)]
  84. #endif
  85. [DefaultValue(false)]
  86. [NotifyParentProperty(true)]
  87. [WebSysDescription ("")]
  88. [WebCategory ("Font")]
  89. public bool Italic
  90. {
  91. get
  92. {
  93. if ((fontstyles & FontStyles.Italic) == 0)
  94. {
  95. return false;
  96. }
  97. return bag.GetBool("Font_Italic", false);
  98. }
  99. set
  100. {
  101. fontstyles |= FontStyles.Italic;
  102. bag["Font_Italic"] = value;
  103. }
  104. }
  105. #if NET_2_0
  106. [RefreshProperties (RefreshProperties.Repaint)]
  107. #else
  108. [Bindable(true)]
  109. #endif
  110. [DefaultValue("")]
  111. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  112. [Editor("System.Drawing.Design.FontNameEditor, " + Consts.AssemblySystem_Drawing_Design, typeof(System.Drawing.Design.UITypeEditor))]
  113. [NotifyParentProperty(true)]
  114. [TypeConverter (typeof(System.Drawing.FontConverter.FontNameConverter))]
  115. [WebSysDescription ("")]
  116. [WebCategory ("Font")]
  117. public string Name
  118. {
  119. get
  120. {
  121. string[] names;
  122. if ((fontstyles & FontStyles.Names) == 0)
  123. {
  124. return string.Empty;
  125. }
  126. names = (string[])bag["Font_Names"];
  127. if (names.Length == 0)
  128. {
  129. return string.Empty;
  130. }
  131. return names[0];
  132. }
  133. set
  134. {
  135. // Seems to be a special case in MS, removing the names from the bag when Name is set to empty,
  136. // but not when setting Names to an empty array
  137. if (value == string.Empty) {
  138. bag.Remove("Font_Names");
  139. return;
  140. }
  141. if (value == null) {
  142. throw new ArgumentNullException("value", "Font name cannot be null");
  143. }
  144. Names = new string[1] { value };
  145. }
  146. }
  147. #if NET_2_0
  148. [RefreshProperties (RefreshProperties.Repaint)]
  149. #endif
  150. [Editor("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  151. [NotifyParentProperty(true)]
  152. [TypeConverter(typeof(System.Web.UI.WebControls.FontNamesConverter))]
  153. [WebSysDescription ("")]
  154. [WebCategory ("Font")]
  155. public string[] Names
  156. {
  157. get
  158. {
  159. string[] ret;
  160. if ((fontstyles & FontStyles.Names) == 0)
  161. {
  162. return FontInfo.empty_names;
  163. }
  164. ret = (string[])bag["Font_Names"];
  165. if (ret != null) {
  166. return ret;
  167. }
  168. return FontInfo.empty_names;
  169. }
  170. set
  171. {
  172. fontstyles |= FontStyles.Names;
  173. bag["Font_Names"] = value;
  174. }
  175. }
  176. #if ONLY_1_1
  177. [Bindable(true)]
  178. #endif
  179. [DefaultValue(false)]
  180. [NotifyParentProperty(true)]
  181. [WebSysDescription ("")]
  182. [WebCategory ("Font")]
  183. public bool Overline
  184. {
  185. get
  186. {
  187. if ((fontstyles & FontStyles.Overline) == 0)
  188. {
  189. return false;
  190. }
  191. return bag.GetBool("Font_Overline", false);
  192. }
  193. set
  194. {
  195. fontstyles |= FontStyles.Overline;
  196. bag["Font_Overline"] = value;
  197. }
  198. }
  199. #if NET_2_0
  200. [RefreshProperties (RefreshProperties.Repaint)]
  201. #else
  202. [Bindable(true)]
  203. #endif
  204. [DefaultValue(typeof (FontUnit), "")]
  205. [NotifyParentProperty(true)]
  206. [WebSysDescription ("")]
  207. [WebCategory ("Font")]
  208. public FontUnit Size
  209. {
  210. get
  211. {
  212. if ((fontstyles & FontStyles.Size) == 0)
  213. {
  214. return FontUnit.Empty;
  215. }
  216. return (FontUnit)bag["Font_Size"];
  217. }
  218. set
  219. {
  220. if (value.Unit.Value < 0)
  221. {
  222. throw new ArgumentOutOfRangeException("Value", value.Unit.Value, "Font size cannot be negative");
  223. }
  224. fontstyles |= FontStyles.Size;
  225. bag["Font_Size"] = value;
  226. }
  227. }
  228. #if ONLY_1_1
  229. [Bindable(true)]
  230. #endif
  231. [DefaultValue(false)]
  232. [NotifyParentProperty(true)]
  233. [WebSysDescription ("")]
  234. [WebCategory ("Font")]
  235. public bool Strikeout
  236. {
  237. get
  238. {
  239. if ((fontstyles & FontStyles.Strikeout) == 0)
  240. {
  241. return false;
  242. }
  243. return bag.GetBool("Font_Strikeout", false);
  244. }
  245. set
  246. {
  247. fontstyles |= FontStyles.Strikeout;
  248. bag["Font_Strikeout"] = value;
  249. }
  250. }
  251. #if ONLY_1_1
  252. [Bindable(true)]
  253. #endif
  254. [DefaultValue(false)]
  255. [NotifyParentProperty(true)]
  256. [WebSysDescription ("")]
  257. [WebCategory ("Font")]
  258. public bool Underline
  259. {
  260. get
  261. {
  262. if ((fontstyles & FontStyles.Underline) == 0)
  263. {
  264. return false;
  265. }
  266. return bag.GetBool("Font_Underline", false);
  267. }
  268. set
  269. {
  270. fontstyles |= FontStyles.Underline;
  271. bag["Font_Underline"] = value;
  272. }
  273. }
  274. #endregion // Public Instance Properties
  275. #region Public Instance Methods
  276. public void CopyFrom(FontInfo f)
  277. {
  278. if (f == this)
  279. return;
  280. this.Reset();
  281. // MS does not store the property in the bag if it's value is false
  282. if (((f.fontstyles & FontStyles.Bold) != 0) && f.Bold)
  283. {
  284. this.Bold = true;
  285. }
  286. if (((f.fontstyles & FontStyles.Italic) != 0) && f.Italic)
  287. {
  288. this.Italic = true;
  289. }
  290. // MS seems to have some weird behaviour, even if f's Name has been set to String.Empty we still get an empty array
  291. if (((f.fontstyles & FontStyles.Names) != 0))
  292. {
  293. this.Names = f.Names;
  294. }
  295. if (((f.fontstyles & FontStyles.Overline) != 0) && f.Overline)
  296. {
  297. this.Overline = true;
  298. }
  299. if (((f.fontstyles & FontStyles.Size) != 0) && (f.Size != FontUnit.Empty))
  300. {
  301. this.Size = f.Size;
  302. }
  303. if (((f.fontstyles & FontStyles.Strikeout) != 0) && f.Strikeout)
  304. {
  305. this.Strikeout = true;
  306. }
  307. if (((f.fontstyles & FontStyles.Underline) != 0) && f.Underline)
  308. {
  309. this.Underline = true;
  310. }
  311. }
  312. public void MergeWith(FontInfo f)
  313. {
  314. if (((fontstyles & FontStyles.Bold) == 0) && ((f.fontstyles & FontStyles.Bold) != 0) && f.Bold)
  315. {
  316. this.Bold = true;
  317. }
  318. if (((fontstyles & FontStyles.Italic) == 0) && ((f.fontstyles & FontStyles.Italic) != 0) && f.Italic)
  319. {
  320. this.Italic = true;
  321. }
  322. if (((fontstyles & FontStyles.Names) == 0) && ((f.fontstyles & FontStyles.Names) != 0))
  323. {
  324. this.Names = f.Names;
  325. }
  326. if (((fontstyles & FontStyles.Overline) == 0) && ((f.fontstyles & FontStyles.Overline) != 0) && f.Overline)
  327. {
  328. this.Overline = true;
  329. }
  330. if (((fontstyles & FontStyles.Size) == 0) && ((f.fontstyles & FontStyles.Size) != 0) && (f.Size != FontUnit.Empty))
  331. {
  332. this.Size = f.Size;
  333. }
  334. if (((fontstyles & FontStyles.Strikeout) == 0) && ((f.fontstyles & FontStyles.Strikeout) != 0) && f.Strikeout)
  335. {
  336. this.Strikeout = true;
  337. }
  338. if (((fontstyles & FontStyles.Underline) == 0) && ((f.fontstyles & FontStyles.Underline) != 0) && f.Underline)
  339. {
  340. this.Underline = true;
  341. }
  342. }
  343. public bool ShouldSerializeNames()
  344. {
  345. return (Names.Length != 0);
  346. }
  347. public override string ToString()
  348. {
  349. if (this.Names.Length == 0)
  350. {
  351. return this.Size.ToString();
  352. }
  353. return this.Name + ", " + this.Size.ToString();
  354. }
  355. #endregion // Public Instance Methods
  356. #region Private Methods
  357. internal void Reset()
  358. {
  359. bag.Remove("Font_Bold");
  360. bag.Remove("Font_Italic");
  361. bag.Remove("Font_Names");
  362. bag.Remove("Font_Overline");
  363. bag.Remove("Font_Size");
  364. bag.Remove("Font_Strikeout");
  365. bag.Remove("Font_Underline");
  366. fontstyles = FontStyles.None;
  367. }
  368. internal void LoadViewState() {
  369. fontstyles = FontStyles.None;
  370. if (bag["Font_Bold"] != null)
  371. {
  372. fontstyles |= FontStyles.Bold;
  373. }
  374. if (bag["Font_Italic"] != null)
  375. {
  376. fontstyles |= FontStyles.Italic;
  377. }
  378. if (bag["Font_Names"] != null)
  379. {
  380. fontstyles |= FontStyles.Names;
  381. }
  382. if (bag["Font_Overline"] != null)
  383. {
  384. fontstyles |= FontStyles.Overline;
  385. }
  386. if (bag["Font_Size"] != null)
  387. {
  388. fontstyles |= FontStyles.Size;
  389. }
  390. if (bag["Font_Strikeout"] != null)
  391. {
  392. fontstyles |= FontStyles.Strikeout;
  393. }
  394. if (bag["Font_Underline"] != null)
  395. {
  396. fontstyles |= FontStyles.Underline;
  397. }
  398. }
  399. #endregion // Private Methods
  400. internal bool IsEmpty {
  401. get {
  402. return fontstyles == FontStyles.None;
  403. }
  404. }
  405. }
  406. }