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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Dennis Bartok (pbartok@novell.com)
27 using System.ComponentModel;
28 using System.Security.Permissions;
30 namespace System.Web.UI.WebControls {
33 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
35 [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
36 public sealed class FontInfo
39 private static string[] empty_names = new string[0];
45 internal FontInfo(Style owner)
48 this.bag = owner.ViewState;
50 #endregion // Constructors
52 #region Public Instance Properties
57 [NotifyParentProperty(true)]
58 [WebSysDescription ("")]
59 [WebCategory ("Font")]
64 if (!_owner.CheckBit((int)Style.Styles.FontBold))
69 return bag.GetBool("Font_Bold", false);
74 bag["Font_Bold"] = value;
75 _owner.SetBit ((int) Style.Styles.FontBold);
83 [NotifyParentProperty(true)]
84 [WebSysDescription ("")]
85 [WebCategory ("Font")]
90 if (!_owner.CheckBit ((int) Style.Styles.FontItalic))
95 return bag.GetBool("Font_Italic", false);
100 bag["Font_Italic"] = value;
101 _owner.SetBit ((int) Style.Styles.FontItalic);
106 [RefreshProperties (RefreshProperties.Repaint)]
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")]
121 string [] names = Names;
123 if (names.Length == 0)
131 // Seems to be a special case in MS, removing the names from the bag when Name is set to empty,
132 // but not when setting Names to an empty array
133 if (value == string.Empty) {
139 throw new ArgumentNullException("value", "Font name cannot be null");
141 Names = new string[1] { value };
146 [RefreshProperties (RefreshProperties.Repaint)]
148 [Editor("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
149 [NotifyParentProperty(true)]
150 [TypeConverter(typeof(System.Web.UI.WebControls.FontNamesConverter))]
151 [WebSysDescription ("")]
152 [WebCategory ("Font")]
153 public string[] Names
159 if (!_owner.CheckBit ((int) Style.Styles.FontNames))
161 return FontInfo.empty_names;
164 ret = (string[])bag["Font_Names"];
169 return FontInfo.empty_names;
175 bag.Remove ("Font_Names");
176 _owner.RemoveBit ((int) Style.Styles.FontNames);
179 bag ["Font_Names"] = value;
180 _owner.SetBit ((int) Style.Styles.FontNames);
188 [DefaultValue(false)]
189 [NotifyParentProperty(true)]
190 [WebSysDescription ("")]
191 [WebCategory ("Font")]
196 if (!_owner.CheckBit ((int) Style.Styles.FontOverline))
201 return bag.GetBool("Font_Overline", false);
206 bag["Font_Overline"] = value;
207 _owner.SetBit ((int) Style.Styles.FontOverline);
212 [RefreshProperties (RefreshProperties.Repaint)]
216 [DefaultValue(typeof (FontUnit), "")]
217 [NotifyParentProperty(true)]
218 [WebSysDescription ("")]
219 [WebCategory ("Font")]
224 if (!_owner.CheckBit ((int) Style.Styles.FontSize))
226 return FontUnit.Empty;
229 return (FontUnit)bag["Font_Size"];
234 if (value.Unit.Value < 0)
236 throw new ArgumentOutOfRangeException("Value", value.Unit.Value, "Font size cannot be negative");
238 bag["Font_Size"] = value;
239 _owner.SetBit ((int) Style.Styles.FontSize);
246 [DefaultValue(false)]
247 [NotifyParentProperty(true)]
248 [WebSysDescription ("")]
249 [WebCategory ("Font")]
250 public bool Strikeout
254 if (!_owner.CheckBit ((int) Style.Styles.FontStrikeout))
259 return bag.GetBool("Font_Strikeout", false);
264 bag["Font_Strikeout"] = value;
265 _owner.SetBit ((int) Style.Styles.FontStrikeout);
272 [DefaultValue(false)]
273 [NotifyParentProperty(true)]
274 [WebSysDescription ("")]
275 [WebCategory ("Font")]
276 public bool Underline
280 if (!_owner.CheckBit ((int) Style.Styles.FontUnderline))
285 return bag.GetBool("Font_Underline", false);
290 bag["Font_Underline"] = value;
291 _owner.SetBit ((int) Style.Styles.FontUnderline);
294 #endregion // Public Instance Properties
296 #region Public Instance Methods
297 public void CopyFrom(FontInfo f)
299 //Methods CopyFrom and MergeWith behave differently between 1.1 and 2.0
300 if (f == null || f.IsEmpty)
307 // MS stores the property in the bag if it's value is false
308 if (f._owner.CheckBit((int) Style.Styles.FontBold)) {
312 if (f._owner.CheckBit ((int) Style.Styles.FontItalic)) {
313 this.Italic = f.Italic;
316 // MS seems to have some weird behaviour, even if f's Name has been set to String.Empty we still get an empty array
317 this.Names = f.Names;
319 if (f._owner.CheckBit ((int) Style.Styles.FontOverline)) {
320 this.Overline = f.Overline;
323 if (f._owner.CheckBit ((int) Style.Styles.FontSize)) {
327 if (f._owner.CheckBit ((int) Style.Styles.FontStrikeout)) {
328 this.Strikeout = f.Strikeout;
331 if (f._owner.CheckBit ((int) Style.Styles.FontUnderline)) {
332 this.Underline = f.Underline;
335 // MS does not store the property in the bag if it's value is false
336 if ((f._owner.CheckBit ((int) Style.Styles.FontBold)) && f.Bold)
341 if ((f._owner.CheckBit ((int) Style.Styles.FontItalic)) && f.Italic)
346 // MS seems to have some weird behaviour, even if f's Name has been set to String.Empty we still get an empty array
347 this.Names = f.Names;
349 if ((f._owner.CheckBit ((int) Style.Styles.FontOverline)) && f.Overline)
351 this.Overline = true;
354 if ((f._owner.CheckBit ((int) Style.Styles.FontSize)) && (f.Size != FontUnit.Empty))
359 if ((f._owner.CheckBit ((int) Style.Styles.FontStrikeout)) && f.Strikeout)
361 this.Strikeout = true;
364 if ((f._owner.CheckBit ((int) Style.Styles.FontUnderline)) && f.Underline)
366 this.Underline = true;
371 public void MergeWith(FontInfo f)
373 //Methods CopyFrom and MergeWith behave differently between 1.1 and 2.0
375 if (!_owner.CheckBit ((int) Style.Styles.FontBold) && f._owner.CheckBit ((int) Style.Styles.FontBold)) {
379 if (!_owner.CheckBit ((int) Style.Styles.FontItalic) && f._owner.CheckBit ((int) Style.Styles.FontItalic)) {
380 this.Italic = f.Italic;
383 if (!_owner.CheckBit ((int) Style.Styles.FontNames) && f._owner.CheckBit ((int) Style.Styles.FontNames)) {
384 this.Names = f.Names;
387 if (!_owner.CheckBit ((int) Style.Styles.FontOverline) && f._owner.CheckBit ((int) Style.Styles.FontOverline)) {
388 this.Overline = f.Overline;
391 if (!_owner.CheckBit ((int) Style.Styles.FontSize) && f._owner.CheckBit ((int) Style.Styles.FontSize)) {
395 if (!_owner.CheckBit ((int) Style.Styles.FontStrikeout) && f._owner.CheckBit ((int) Style.Styles.FontStrikeout)) {
396 this.Strikeout = f.Strikeout;
399 if (!_owner.CheckBit ((int) Style.Styles.FontUnderline) && f._owner.CheckBit ((int) Style.Styles.FontUnderline)) {
400 this.Underline = f.Underline;
403 if (!_owner.CheckBit ((int) Style.Styles.FontBold) && f._owner.CheckBit ((int) Style.Styles.FontBold) && f.Bold)
408 if (!_owner.CheckBit ((int) Style.Styles.FontItalic) && f._owner.CheckBit ((int) Style.Styles.FontItalic) && f.Italic)
413 if (!_owner.CheckBit ((int) Style.Styles.FontNames) && f._owner.CheckBit ((int) Style.Styles.FontNames))
415 this.Names = f.Names;
418 if (!_owner.CheckBit ((int) Style.Styles.FontOverline) && f._owner.CheckBit ((int) Style.Styles.FontOverline) && f.Overline)
420 this.Overline = true;
423 if (!_owner.CheckBit ((int) Style.Styles.FontSize) && f._owner.CheckBit ((int) Style.Styles.FontSize))
428 if (!_owner.CheckBit ((int) Style.Styles.FontStrikeout) && f._owner.CheckBit ((int) Style.Styles.FontStrikeout))
430 this.Strikeout = true;
433 if (!_owner.CheckBit ((int) Style.Styles.FontUnderline) && f._owner.CheckBit ((int) Style.Styles.FontUnderline) && f.Underline)
435 this.Underline = true;
440 public bool ShouldSerializeNames()
442 return (Names.Length != 0);
445 public override string ToString()
447 if (this.Names.Length == 0)
449 return this.Size.ToString();
452 return this.Name + ", " + this.Size.ToString();
456 public void ClearDefaults () {
461 #endregion // Public Instance Methods
463 #region Private Methods
464 internal void Reset()
466 bag.Remove("Font_Bold");
467 bag.Remove("Font_Italic");
468 bag.Remove("Font_Names");
469 bag.Remove("Font_Overline");
470 bag.Remove("Font_Size");
471 bag.Remove("Font_Strikeout");
472 bag.Remove("Font_Underline");
473 _owner.RemoveBit ((int) Style.Styles.FontAll);
477 internal void FillStyleAttributes (CssStyleCollection attributes, bool alwaysRenderTextDecoration) {
479 if(alwaysRenderTextDecoration)
480 attributes.Add (HtmlTextWriterStyle.TextDecoration, "none");
485 // Fonts are a bit weird
486 s = String.Join (",", Names);
488 attributes.Add (HtmlTextWriterStyle.FontFamily, s);
491 if (_owner.CheckBit ((int) Style.Styles.FontBold)) {
492 attributes.Add (HtmlTextWriterStyle.FontWeight, Bold ? "bold" : "normal");
495 if (_owner.CheckBit ((int) Style.Styles.FontItalic)) {
496 attributes.Add (HtmlTextWriterStyle.FontStyle, Italic ? "italic" : "normal");
500 attributes.Add (HtmlTextWriterStyle.FontSize, Size.ToString ());
503 // These styles are munged into a attribute decoration
505 bool hasTextDecoration = false;
507 if (_owner.CheckBit ((int) Style.Styles.FontOverline)) {
510 hasTextDecoration = true;
513 if (_owner.CheckBit ((int) Style.Styles.FontStrikeout)) {
515 s += "line-through ";
516 hasTextDecoration = true;
519 if (_owner.CheckBit ((int) Style.Styles.FontUnderline)) {
522 hasTextDecoration = true;
525 s = (s.Length > 0) ? s.Trim () : (alwaysRenderTextDecoration || hasTextDecoration) ? "none" : "";
527 attributes.Add (HtmlTextWriterStyle.TextDecoration, s);
530 #endregion // Private Methods
535 return !_owner.CheckBit ((int) Style.Styles.FontAll);