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-2010 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 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
54 [NotifyParentProperty(true)]
55 [WebSysDescription ("")]
56 [WebCategory ("Font")]
59 if (!_owner.CheckBit((int)Style.Styles.FontBold))
62 return bag.GetBool("Font_Bold", false);
66 bag["Font_Bold"] = value;
67 _owner.SetBit ((int) Style.Styles.FontBold);
72 [NotifyParentProperty(true)]
73 [WebSysDescription ("")]
74 [WebCategory ("Font")]
77 if (!_owner.CheckBit ((int) Style.Styles.FontItalic))
80 return bag.GetBool("Font_Italic", false);
84 bag["Font_Italic"] = value;
85 _owner.SetBit ((int) Style.Styles.FontItalic);
89 [RefreshProperties (RefreshProperties.Repaint)]
91 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
92 [Editor("System.Drawing.Design.FontNameEditor, " + Consts.AssemblySystem_Drawing_Design, typeof(System.Drawing.Design.UITypeEditor))]
93 [NotifyParentProperty(true)]
94 [TypeConverter (typeof(System.Drawing.FontConverter.FontNameConverter))]
95 [WebSysDescription ("")]
96 [WebCategory ("Font")]
99 string [] names = Names;
101 if (names.Length == 0)
108 // Seems to be a special case in MS, removing the names from the bag when Name is set to empty,
109 // but not when setting Names to an empty array
110 if (value == string.Empty) {
116 throw new ArgumentNullException("value", "Font name cannot be null");
117 Names = new string[1] { value };
121 [RefreshProperties (RefreshProperties.Repaint)]
122 [Editor("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
123 [NotifyParentProperty(true)]
124 [TypeConverter(typeof(System.Web.UI.WebControls.FontNamesConverter))]
125 [WebSysDescription ("")]
126 [WebCategory ("Font")]
127 public string[] Names {
131 if (!_owner.CheckBit ((int) Style.Styles.FontNames))
132 return FontInfo.empty_names;
134 ret = (string[])bag["Font_Names"];
139 return FontInfo.empty_names;
144 bag.Remove ("Font_Names");
145 _owner.RemoveBit ((int) Style.Styles.FontNames);
147 bag ["Font_Names"] = value;
148 _owner.SetBit ((int) Style.Styles.FontNames);
153 [DefaultValue(false)]
154 [NotifyParentProperty(true)]
155 [WebSysDescription ("")]
156 [WebCategory ("Font")]
157 public bool Overline {
159 if (!_owner.CheckBit ((int) Style.Styles.FontOverline))
162 return bag.GetBool("Font_Overline", false);
166 bag["Font_Overline"] = value;
167 _owner.SetBit ((int) Style.Styles.FontOverline);
171 [RefreshProperties (RefreshProperties.Repaint)]
172 [DefaultValue(typeof (FontUnit), "")]
173 [NotifyParentProperty(true)]
174 [WebSysDescription ("")]
175 [WebCategory ("Font")]
176 public FontUnit Size {
178 if (!_owner.CheckBit ((int) Style.Styles.FontSize))
179 return FontUnit.Empty;
181 return (FontUnit)bag["Font_Size"];
185 if (value.Unit.Value < 0)
186 throw new ArgumentOutOfRangeException("Value", value.Unit.Value, "Font size cannot be negative");
188 bag["Font_Size"] = value;
189 _owner.SetBit ((int) Style.Styles.FontSize);
193 [DefaultValue(false)]
194 [NotifyParentProperty(true)]
195 [WebSysDescription ("")]
196 [WebCategory ("Font")]
197 public bool Strikeout {
199 if (!_owner.CheckBit ((int) Style.Styles.FontStrikeout))
202 return bag.GetBool("Font_Strikeout", false);
206 bag["Font_Strikeout"] = value;
207 _owner.SetBit ((int) Style.Styles.FontStrikeout);
211 [DefaultValue(false)]
212 [NotifyParentProperty(true)]
213 [WebSysDescription ("")]
214 [WebCategory ("Font")]
215 public bool Underline {
217 if (!_owner.CheckBit ((int) Style.Styles.FontUnderline))
220 return bag.GetBool("Font_Underline", false);
224 bag["Font_Underline"] = value;
225 _owner.SetBit ((int) Style.Styles.FontUnderline);
228 #endregion // Public Instance Properties
230 #region Public Instance Methods
231 public void CopyFrom (FontInfo f)
233 if (f == null || f.IsEmpty)
239 // MS stores the property in the bag if it's value is false
240 if (f._owner.CheckBit((int) Style.Styles.FontBold))
243 if (f._owner.CheckBit ((int) Style.Styles.FontItalic))
244 this.Italic = f.Italic;
246 // MS seems to have some weird behaviour, even if f's Name has been set to String.Empty we still get an empty array
247 this.Names = f.Names;
249 if (f._owner.CheckBit ((int) Style.Styles.FontOverline))
250 this.Overline = f.Overline;
252 if (f._owner.CheckBit ((int) Style.Styles.FontSize))
255 if (f._owner.CheckBit ((int) Style.Styles.FontStrikeout))
256 this.Strikeout = f.Strikeout;
258 if (f._owner.CheckBit ((int) Style.Styles.FontUnderline))
259 this.Underline = f.Underline;
262 public void MergeWith (FontInfo f)
264 if (!_owner.CheckBit ((int) Style.Styles.FontBold) && f._owner.CheckBit ((int) Style.Styles.FontBold))
267 if (!_owner.CheckBit ((int) Style.Styles.FontItalic) && f._owner.CheckBit ((int) Style.Styles.FontItalic))
268 this.Italic = f.Italic;
270 if (!_owner.CheckBit ((int) Style.Styles.FontNames) && f._owner.CheckBit ((int) Style.Styles.FontNames))
271 this.Names = f.Names;
273 if (!_owner.CheckBit ((int) Style.Styles.FontOverline) && f._owner.CheckBit ((int) Style.Styles.FontOverline))
274 this.Overline = f.Overline;
276 if (!_owner.CheckBit ((int) Style.Styles.FontSize) && f._owner.CheckBit ((int) Style.Styles.FontSize))
279 if (!_owner.CheckBit ((int) Style.Styles.FontStrikeout) && f._owner.CheckBit ((int) Style.Styles.FontStrikeout))
280 this.Strikeout = f.Strikeout;
282 if (!_owner.CheckBit ((int) Style.Styles.FontUnderline) && f._owner.CheckBit ((int) Style.Styles.FontUnderline))
283 this.Underline = f.Underline;
286 public bool ShouldSerializeNames ()
288 return (Names.Length != 0);
291 public override string ToString ()
293 if (this.Names.Length == 0)
294 return this.Size.ToString();
296 return this.Name + ", " + this.Size.ToString();
299 public void ClearDefaults ()
304 #endregion // Public Instance Methods
306 #region Private Methods
307 internal void Reset()
309 bag.Remove("Font_Bold");
310 bag.Remove("Font_Italic");
311 bag.Remove("Font_Names");
312 bag.Remove("Font_Overline");
313 bag.Remove("Font_Size");
314 bag.Remove("Font_Strikeout");
315 bag.Remove("Font_Underline");
316 _owner.RemoveBit ((int) Style.Styles.FontAll);
319 internal void FillStyleAttributes (CssStyleCollection attributes, bool alwaysRenderTextDecoration)
322 if(alwaysRenderTextDecoration)
323 attributes.Add (HtmlTextWriterStyle.TextDecoration, "none");
328 // Fonts are a bit weird
329 s = String.Join (",", Names);
331 attributes.Add (HtmlTextWriterStyle.FontFamily, s);
333 if (_owner.CheckBit ((int) Style.Styles.FontBold))
334 attributes.Add (HtmlTextWriterStyle.FontWeight, Bold ? "bold" : "normal");
336 if (_owner.CheckBit ((int) Style.Styles.FontItalic))
337 attributes.Add (HtmlTextWriterStyle.FontStyle, Italic ? "italic" : "normal");
340 attributes.Add (HtmlTextWriterStyle.FontSize, Size.ToString ());
342 // These styles are munged into a attribute decoration
344 bool hasTextDecoration = false;
346 if (_owner.CheckBit ((int) Style.Styles.FontOverline)) {
349 hasTextDecoration = true;
352 if (_owner.CheckBit ((int) Style.Styles.FontStrikeout)) {
354 s += "line-through ";
355 hasTextDecoration = true;
358 if (_owner.CheckBit ((int) Style.Styles.FontUnderline)) {
361 hasTextDecoration = true;
364 s = (s.Length > 0) ? s.Trim () : (alwaysRenderTextDecoration || hasTextDecoration) ? "none" : String.Empty;
366 attributes.Add (HtmlTextWriterStyle.TextDecoration, s);
368 #endregion // Private Methods
372 get { return !_owner.CheckBit ((int) Style.Styles.FontAll); }