* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Drawing / System.Drawing / FontFamily.cs
index f63f50667eedee2074cee49623567057cdae03d8..9591cf18fd0429df52f58f22cbb184ffaacee699 100644 (file)
@@ -4,9 +4,31 @@
 // Author:
 //   Dennis Hayes (dennish@Raytek.com)
 //   Alexandre Pigolkine (pigolkine@gmx.de)
-// (C) 2002/2004 Ximian, Inc
+//   Peter Dennis Bartok (pbartok@novell.com)
 //
-using System;
+// Copyright (C) 2002/2004 Ximian, Inc http://www.ximian.com
+// Copyright (C) 2004 - 2006 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System.Drawing.Text;
 using System.Text;
 using System.Runtime.InteropServices;
@@ -16,33 +38,28 @@ namespace System.Drawing {
        public sealed class FontFamily : MarshalByRefObject, IDisposable 
        {
                
-               static private FontFamily genericMonospace = null;
-               static private FontFamily genericSansSerif = null;
-               static private FontFamily genericSerif = null;
+               static private FontFamily genericMonospace;
+               static private FontFamily genericSansSerif;
+               static private FontFamily genericSerif;
                private string name;
-               internal IntPtr nativeFontFamily = IntPtr.Zero;
+               private IntPtr nativeFontFamily = IntPtr.Zero;
                                
                internal FontFamily(IntPtr fntfamily)
                {
                        nativeFontFamily = fntfamily;           
-                       refreshName();                  
                }
                
                internal void refreshName()
                {
-                       if (nativeFontFamily != IntPtr.Zero) {
-                               int language = 0;                       
-                               StringBuilder sBuilder = new StringBuilder (GDIPlus.FACESIZE * UnicodeEncoding.CharSize);       
-                               Status status = GDIPlus.GdipGetFamilyName (nativeFontFamily, sBuilder, language);
-                               GDIPlus.CheckStatus (status);
-                               name = sBuilder.ToString();                             
-                       }
-               }
-               
-               //Need to come back here, is Arial the right thing to do
-               internal FontFamily() : this ("Arial", null)
-               {
-                                                                               
+                       StringBuilder sb;
+
+                       if (nativeFontFamily == IntPtr.Zero)
+                               return;
+
+                       sb = new StringBuilder (GDIPlus.FACESIZE);
+                       Status status = GDIPlus.GdipGetFamilyName (nativeFontFamily, sb, 0);
+                       GDIPlus.CheckStatus (status);
+                       name = sb.ToString();
                }
                
                ~FontFamily()
@@ -56,38 +73,24 @@ namespace System.Drawing {
                        {
                                return nativeFontFamily;
                        }
-                       set     
-                       {
-                               nativeFontFamily = value;
-                       }
                }
 
-               public FontFamily(GenericFontFamilies genericFamily) 
+               public FontFamily (GenericFontFamilies genericFamily) 
                {
                        Status status;
-                       switch (genericFamily) 
-                       {
-                               case GenericFontFamilies.Monospace:
-                                       status = GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
-                                       GDIPlus.CheckStatus (status);
-                                       refreshName ();
-                                       break;
+                       switch (genericFamily) {
                                case GenericFontFamilies.SansSerif:
                                        status = GDIPlus.GdipGetGenericFontFamilySansSerif (out nativeFontFamily);
-                                       GDIPlus.CheckStatus (status);
-                                       refreshName ();
                                        break;
                                case GenericFontFamilies.Serif:
                                        status = GDIPlus.GdipGetGenericFontFamilySerif (out nativeFontFamily);
-                                       GDIPlus.CheckStatus (status);
-                                       refreshName ();
                                        break;
+                               case GenericFontFamilies.Monospace:
                                default:        // Undocumented default 
                                        status = GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
-                                       GDIPlus.CheckStatus (status);
-                                       refreshName ();
                                        break;
                        }
+                       GDIPlus.CheckStatus (status);
                }
                
                public FontFamily(string familyName) : this (familyName, null)
@@ -96,104 +99,37 @@ namespace System.Drawing {
                
                public FontFamily (string familyName, FontCollection collection) 
                {
-                       Status status;
-                       if ( collection != null )
-                               status = GDIPlus.GdipCreateFontFamilyFromName (familyName, collection.nativeFontCollection, out nativeFontFamily);
-                       else
-                               status = GDIPlus.GdipCreateFontFamilyFromName (familyName, IntPtr.Zero, out nativeFontFamily);
+                       IntPtr handle = (collection == null) ? IntPtr.Zero : collection.nativeFontCollection;
+                       Status status = GDIPlus.GdipCreateFontFamilyFromName (familyName, handle, out nativeFontFamily);
                        GDIPlus.CheckStatus (status);
-                       
-                       refreshName ();
                }
                
-               public string Name 
-               {
-                       get 
-                       {
+               public string Name {
+                       get {
+                               if (nativeFontFamily == IntPtr.Zero)
+                                       throw new ArgumentException ("Name", Locale.GetText ("Object was disposed."));
+                               if (name == null)
+                                       refreshName ();
                                return name;
                        }
                }
                
-               public static FontFamily GenericMonospace 
-               {
-                       get 
-                       {
-                               if (genericMonospace == null) 
-                               {
-                                       IntPtr generic = IntPtr.Zero;
-                                       Status status = GDIPlus.GdipGetGenericFontFamilyMonospace (out generic);
-                                       GDIPlus.CheckStatus (status);
-                                       genericMonospace = new FontFamily (generic);
-                                       genericMonospace.refreshName ();
-                               }
-                               return genericMonospace;
-                       }
+               public static FontFamily GenericMonospace {
+                       get { return new FontFamily (GenericFontFamilies.Monospace); }
                }
                
-               public static FontFamily GenericSansSerif 
-               {
-                       get 
-                       {
-                               if (genericSansSerif == null) 
-                               {
-                                       IntPtr generic = IntPtr.Zero;
-                                       Status status = GDIPlus.GdipGetGenericFontFamilySansSerif (out generic);
-                                       GDIPlus.CheckStatus (status);
-                                       genericSansSerif = new FontFamily (generic);
-                                       genericSansSerif.refreshName ();
-                               }
-                               return genericSansSerif;
-                       }
+               public static FontFamily GenericSansSerif {
+                       get { return new FontFamily (GenericFontFamilies.SansSerif); }
                }
                
-               public static FontFamily GenericSerif 
-               {
-                       get 
-                       {
-                               if (genericSerif == null) 
-                               {
-                                       IntPtr generic = IntPtr.Zero;
-                                       Status status = GDIPlus.GdipGetGenericFontFamilySerif (out generic);
-                                       GDIPlus.CheckStatus (status);
-                                       genericSerif = new FontFamily (generic);
-                                       genericSerif.refreshName ();
-                               }
-                               return genericSerif;
-                       }
+               public static FontFamily GenericSerif {
+                       get { return new FontFamily (GenericFontFamilies.Serif); }
                }
                
-               //[MONO TODO]
-               //Need to check how to get the Flags attribute to read 
-               //bitwise value of the enumeration
-               internal int GetStyleCheck(FontStyle style)
-               {
-                       int styleCheck = 0 ;
-                       switch ( style) {
-                               case FontStyle.Bold:
-                                       styleCheck = 1;
-                                       break;
-                               case FontStyle.Italic:
-                                       styleCheck = 2;
-                                       break;
-                               case FontStyle.Regular:
-                                       styleCheck = 0;
-                                       break;
-                               case FontStyle.Strikeout:
-                                       styleCheck = 8;
-                                       break;
-                               case FontStyle.Underline:
-                                       styleCheck = 4;
-                                       break;
-                       }
-                       return styleCheck;
-               }
-
                public int GetCellAscent (FontStyle style) 
                {
-                       Status status;
-                       uint outProperty;
-                       int styleCheck = GetStyleCheck (style);                         
-                       status = GDIPlus.GdipGetCellAscent (nativeFontFamily, styleCheck, out outProperty);
+                       short outProperty;
+                       Status status = GDIPlus.GdipGetCellAscent (nativeFontFamily, (int)style, out outProperty);
                        GDIPlus.CheckStatus (status);
 
                        return (int) outProperty;
@@ -201,10 +137,8 @@ namespace System.Drawing {
                
                public int GetCellDescent (FontStyle style) 
                {
-                       Status status;
-                       uint outProperty;
-                       int styleCheck = GetStyleCheck (style);                         
-                       status = GDIPlus.GdipGetCellDescent (nativeFontFamily, styleCheck, out outProperty);
+                       short outProperty;
+                       Status status = GDIPlus.GdipGetCellDescent (nativeFontFamily, (int)style, out outProperty);
                        GDIPlus.CheckStatus (status);
 
                        return (int) outProperty;
@@ -212,10 +146,8 @@ namespace System.Drawing {
                
                public int GetEmHeight (FontStyle style) 
                {
-                       Status status;
-                       uint outProperty;
-                       int styleCheck = GetStyleCheck (style);                         
-                       status = GDIPlus.GdipGetEmHeight (nativeFontFamily, styleCheck, out outProperty);
+                       short outProperty;
+                       Status status = GDIPlus.GdipGetEmHeight (nativeFontFamily, (int)style, out outProperty);
                        GDIPlus.CheckStatus (status);
 
                        return (int) outProperty;
@@ -223,58 +155,56 @@ namespace System.Drawing {
                
                public int GetLineSpacing (FontStyle style)
                {
-                       Status status;
-                       uint outProperty;
-                       int styleCheck = GetStyleCheck (style);                         
-                       status = GDIPlus.GdipGetLineSpacing (nativeFontFamily, styleCheck, out outProperty);
-                       GDIPlus.CheckStatus (status);
+                       short outProperty;
+                       Status status = GDIPlus.GdipGetLineSpacing (nativeFontFamily, (int)style, out outProperty);
+                       GDIPlus.CheckStatus (status);   
 
                        return (int) outProperty;
                }
-               
+
+               // note: libgdiplus always returns true
                public bool IsStyleAvailable (FontStyle style)
                {
-                       Status status;
                        bool outProperty;
-                       int styleCheck = GetStyleCheck (style);                         
-                       status = GDIPlus.GdipIsStyleAvailable (nativeFontFamily, styleCheck, out outProperty);
+                       Status status = GDIPlus.GdipIsStyleAvailable (nativeFontFamily, (int)style, out outProperty);
                        GDIPlus.CheckStatus (status);
 
                        return outProperty;
                }
                
                public void Dispose ()
-               {       
+               {
                        Status status = GDIPlus.GdipDeleteFontFamily (nativeFontFamily);
-                       if ( status == Status.Ok ) 
-                               nativeFontFamily = IntPtr.Zero;                                                 
-               }
-               
+                       nativeFontFamily = IntPtr.Zero;
+                       GC.SuppressFinalize (this);
+                       // check the status code (throw) at the last step
+                       GDIPlus.CheckStatus (status);
+               }               
                
-               public override bool Equals(object obj)
+               public override bool Equals (object obj)
                {
-                       if (!(obj is FontFamily))
+                       FontFamily o = (obj as FontFamily);
+                       if (o == null)
                                return false;
 
-                       return (this == (FontFamily) obj);                      
+                       return (Name == o.Name);
                }
                
                public override int GetHashCode ()
                {
-                       return name.GetHashCode ();                     
+                       return Name.GetHashCode ();                     
                }
                        
                        
-               public FontFamily[] Families
-               {
-                       get {
-                               
-                               return GetFamilies (null);
-                       }
+               public static FontFamily[] Families {
+                       get { return new InstalledFontCollection ().Families; }
                }               
                
                public static FontFamily[] GetFamilies (Graphics graphics)
                {
+                       if (graphics == null)
+                               throw new ArgumentNullException ("graphics");
+
                        InstalledFontCollection fntcol = new InstalledFontCollection ();
                        return fntcol.Families;                 
                }
@@ -282,14 +212,13 @@ namespace System.Drawing {
                [MonoTODO ("We only support the default system language")]
                public string GetName (int language)
                {
-                       return name;
+                       return Name;
                }
                
                public override string ToString ()
                {
-                       return "FontFamily :" + name;
+                       return String.Concat ("[FontFamily: Name=", Name, "]");
                }
-
        }
 }