FontInfo.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. //Methods CopyFrom and MergeWith behave differently between 1.1 and 2.0
  279. if (f == null || f.IsEmpty)
  280. return;
  281. if (f == this)
  282. return;
  283. #if NET_2_0
  284. // MS stores the property in the bag if it's value is false
  285. if ((f.fontstyles & FontStyles.Bold) != 0) {
  286. this.Bold = f.Bold;
  287. }
  288. if ((f.fontstyles & FontStyles.Italic) != 0) {
  289. this.Italic = f.Italic;
  290. }
  291. // MS seems to have some weird behaviour, even if f's Name has been set to String.Empty we still get an empty array
  292. if ((f.fontstyles & FontStyles.Names) != 0) {
  293. this.Names = f.Names;
  294. }
  295. if ((f.fontstyles & FontStyles.Overline) != 0) {
  296. this.Overline = f.Overline;
  297. }
  298. if ((f.fontstyles & FontStyles.Size) != 0) {
  299. this.Size = f.Size;
  300. }
  301. if ((f.fontstyles & FontStyles.Strikeout) != 0) {
  302. this.Strikeout = f.Strikeout;
  303. }
  304. if ((f.fontstyles & FontStyles.Underline) != 0) {
  305. this.Underline = f.Underline;
  306. }
  307. #else
  308. // MS does not store the property in the bag if it's value is false
  309. if (((f.fontstyles & FontStyles.Bold) != 0) && f.Bold)
  310. {
  311. this.Bold = true;
  312. }
  313. if (((f.fontstyles & FontStyles.Italic) != 0) && f.Italic)
  314. {
  315. this.Italic = true;
  316. }
  317. // MS seems to have some weird behaviour, even if f's Name has been set to String.Empty we still get an empty array
  318. if (((f.fontstyles & FontStyles.Names) != 0))
  319. {
  320. this.Names = f.Names;
  321. }
  322. if (((f.fontstyles & FontStyles.Overline) != 0) && f.Overline)
  323. {
  324. this.Overline = true;
  325. }
  326. if (((f.fontstyles & FontStyles.Size) != 0) && (f.Size != FontUnit.Empty))
  327. {
  328. this.Size = f.Size;
  329. }
  330. if (((f.fontstyles & FontStyles.Strikeout) != 0) && f.Strikeout)
  331. {
  332. this.Strikeout = true;
  333. }
  334. if (((f.fontstyles & FontStyles.Underline) != 0) && f.Underline)
  335. {
  336. this.Underline = true;
  337. }
  338. #endif
  339. }
  340. public void MergeWith(FontInfo f)
  341. {
  342. //Methods CopyFrom and MergeWith behave differently between 1.1 and 2.0
  343. #if NET_2_0
  344. if (((fontstyles & FontStyles.Bold) == 0) && ((f.fontstyles & FontStyles.Bold) != 0)) {
  345. this.Bold = f.Bold;
  346. }
  347. if (((fontstyles & FontStyles.Italic) == 0) && ((f.fontstyles & FontStyles.Italic) != 0)) {
  348. this.Italic = f.Italic;
  349. }
  350. if (((fontstyles & FontStyles.Names) == 0) && ((f.fontstyles & FontStyles.Names) != 0)) {
  351. this.Names = f.Names;
  352. }
  353. if (((fontstyles & FontStyles.Overline) == 0) && ((f.fontstyles & FontStyles.Overline) != 0)) {
  354. this.Overline = f.Overline;
  355. }
  356. if (((fontstyles & FontStyles.Size) == 0) && ((f.fontstyles & FontStyles.Size) != 0)) {
  357. this.Size = f.Size;
  358. }
  359. if (((fontstyles & FontStyles.Strikeout) == 0) && ((f.fontstyles & FontStyles.Strikeout) != 0)) {
  360. this.Strikeout = f.Strikeout;
  361. }
  362. if (((fontstyles & FontStyles.Underline) == 0) && ((f.fontstyles & FontStyles.Underline) != 0)) {
  363. this.Underline = f.Underline;
  364. }
  365. #else
  366. if (((fontstyles & FontStyles.Bold) == 0) && ((f.fontstyles & FontStyles.Bold) != 0) && f.Bold)
  367. {
  368. this.Bold = true;
  369. }
  370. if (((fontstyles & FontStyles.Italic) == 0) && ((f.fontstyles & FontStyles.Italic) != 0) && f.Italic)
  371. {
  372. this.Italic = true;
  373. }
  374. if (((fontstyles & FontStyles.Names) == 0) && ((f.fontstyles & FontStyles.Names) != 0))
  375. {
  376. this.Names = f.Names;
  377. }
  378. if (((fontstyles & FontStyles.Overline) == 0) && ((f.fontstyles & FontStyles.Overline) != 0) && f.Overline)
  379. {
  380. this.Overline = true;
  381. }
  382. if (((fontstyles & FontStyles.Size) == 0) && ((f.fontstyles & FontStyles.Size) != 0) && (f.Size != FontUnit.Empty))
  383. {
  384. this.Size = f.Size;
  385. }
  386. if (((fontstyles & FontStyles.Strikeout) == 0) && ((f.fontstyles & FontStyles.Strikeout) != 0) && f.Strikeout)
  387. {
  388. this.Strikeout = true;
  389. }
  390. if (((fontstyles & FontStyles.Underline) == 0) && ((f.fontstyles & FontStyles.Underline) != 0) && f.Underline)
  391. {
  392. this.Underline = true;
  393. }
  394. #endif
  395. }
  396. public bool ShouldSerializeNames()
  397. {
  398. return (Names.Length != 0);
  399. }
  400. public override string ToString()
  401. {
  402. if (this.Names.Length == 0)
  403. {
  404. return this.Size.ToString();
  405. }
  406. return this.Name + ", " + this.Size.ToString();
  407. }
  408. #if NET_2_0
  409. public void ClearDefaults () {
  410. Reset ();
  411. }
  412. #endif
  413. #endregion // Public Instance Methods
  414. #region Private Methods
  415. internal void Reset()
  416. {
  417. bag.Remove("Font_Bold");
  418. bag.Remove("Font_Italic");
  419. bag.Remove("Font_Names");
  420. bag.Remove("Font_Overline");
  421. bag.Remove("Font_Size");
  422. bag.Remove("Font_Strikeout");
  423. bag.Remove("Font_Underline");
  424. fontstyles = FontStyles.None;
  425. }
  426. internal void LoadViewState() {
  427. fontstyles = FontStyles.None;
  428. if (bag["Font_Bold"] != null)
  429. {
  430. fontstyles |= FontStyles.Bold;
  431. }
  432. if (bag["Font_Italic"] != null)
  433. {
  434. fontstyles |= FontStyles.Italic;
  435. }
  436. if (bag["Font_Names"] != null)
  437. {
  438. fontstyles |= FontStyles.Names;
  439. }
  440. if (bag["Font_Overline"] != null)
  441. {
  442. fontstyles |= FontStyles.Overline;
  443. }
  444. if (bag["Font_Size"] != null)
  445. {
  446. fontstyles |= FontStyles.Size;
  447. }
  448. if (bag["Font_Strikeout"] != null)
  449. {
  450. fontstyles |= FontStyles.Strikeout;
  451. }
  452. if (bag["Font_Underline"] != null)
  453. {
  454. fontstyles |= FontStyles.Underline;
  455. }
  456. }
  457. #endregion // Private Methods
  458. internal bool IsEmpty {
  459. get {
  460. return fontstyles == FontStyles.None;
  461. }
  462. }
  463. }
  464. }