Author: spouliot
Date: 2006-05-30 14:59:11 -0400 (Tue, 30 May 2006)
New Revision: 61277

Modified:
   trunk/mcs/class/System.Drawing/System.Drawing/ChangeLog
   trunk/mcs/class/System.Drawing/System.Drawing/Font.cs
   trunk/mcs/class/System.Drawing/System.Drawing/FontFamily.cs
   trunk/mcs/class/System.Drawing/System.Drawing/gdipFunctions.cs
Log:
2006-05-30  Sebastien Pouliot  <[EMAIL PROTECTED]> 

        * Font.cs: Change default charset to 1. Ensure we always call the
        CheckStatus method after calling GDI+. Added null checks to match MS
        exception handling.
        * FontFamily.cs: Avoid double-cast in Equals. Fixed Equals not compare
        pointers but names. Simplify the Generic* static properties by reusing
        the .ctor code. Fixed ToString to match MS output. Removed 
        GetStyleCheck as it failed when multiple styles were provided (flags).
        * gdipFunctions.cs: Convert Status.FontFamilyNotFound to an 
        ArgumentException.



Modified: trunk/mcs/class/System.Drawing/System.Drawing/ChangeLog
===================================================================
--- trunk/mcs/class/System.Drawing/System.Drawing/ChangeLog     2006-05-30 
18:54:32 UTC (rev 61276)
+++ trunk/mcs/class/System.Drawing/System.Drawing/ChangeLog     2006-05-30 
18:59:11 UTC (rev 61277)
@@ -1,3 +1,15 @@
+2006-05-30  Sebastien Pouliot  <[EMAIL PROTECTED]> 
+
+       * Font.cs: Change default charset to 1. Ensure we always call the
+       CheckStatus method after calling GDI+. Added null checks to match MS
+       exception handling.
+       * FontFamily.cs: Avoid double-cast in Equals. Fixed Equals not compare
+       pointers but names. Simplify the Generic* static properties by reusing
+       the .ctor code. Fixed ToString to match MS output. Removed 
+       GetStyleCheck as it failed when multiple styles were provided (flags).
+       * gdipFunctions.cs: Convert Status.FontFamilyNotFound to an 
+       ArgumentException.
+
 2006-05-29  Sebastien Pouliot  <[EMAIL PROTECTED]> 
 
        * ImageFormatConverter.cs: Changed ConvertFrom to work with both short

Modified: trunk/mcs/class/System.Drawing/System.Drawing/Font.cs
===================================================================
--- trunk/mcs/class/System.Drawing/System.Drawing/Font.cs       2006-05-30 
18:54:32 UTC (rev 61276)
+++ trunk/mcs/class/System.Drawing/System.Drawing/Font.cs       2006-05-30 
18:59:11 UTC (rev 61277)
@@ -48,10 +48,15 @@
                private string  systemFontName;
                private float _size;
 
-               private void CreateFont(string familyName, float emSize, 
FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical) {
-                        Status         status;                  
-                        FontFamily     family;                      
+               private const byte DefaultCharSet = 1;
 
+               private void CreateFont (string familyName, float emSize, 
FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical)
+               {
+#if ONLY_1_1
+                       if (familyName == null)
+                               throw new ArgumentNullException ("familyName");
+#endif
+                        FontFamily family;
                        // NOTE: If family name is null, empty or invalid,
                        // MS creates Microsoft Sans Serif font.
                        try {
@@ -62,7 +67,7 @@
                        }
 
                        setProperties (family, emSize, style, unit, charSet, 
isVertical);           
-                       status = GDIPlus.GdipCreateFont (family.NativeObject, 
emSize,  style, unit, out fontObject);
+                       Status status = GDIPlus.GdipCreateFont 
(family.NativeObject, emSize,  style, unit, out fontObject);
                        GDIPlus.CheckStatus (status);
                }
 
@@ -78,7 +83,7 @@
                        style = (FontStyle)info.GetValue("Style", 
typeof(FontStyle));
                        unit = (GraphicsUnit)info.GetValue("Unit", 
typeof(GraphicsUnit));
  
-                       CreateFont(name, size, style, unit, (byte)0, false);
+                       CreateFont(name, size, style, unit, DefaultCharSet, 
false);
                }
 
                void ISerializable.GetObjectData(SerializationInfo info, 
StreamingContext context)
@@ -97,9 +102,11 @@
                public void Dispose ()
                {
                        if (fontObject != IntPtr.Zero) {
-                               GDIPlus.CheckStatus (GDIPlus.GdipDeleteFont 
(fontObject));
+                               Status status = GDIPlus.GdipDeleteFont 
(fontObject);
                                fontObject = IntPtr.Zero;
                                GC.SuppressFinalize (this);
+                               // check the status code (throw) at the last 
step
+                               GDIPlus.CheckStatus (status);
                        }
                }
 
@@ -247,14 +254,11 @@
 
                public IntPtr ToHfont ()
                {
+                       if (fontObject == IntPtr.Zero)
+                               throw new ArgumentException (Locale.GetText 
("Object has been disposed."));
+
                        IntPtr Hfont;
                        OperatingSystem osInfo = Environment.OSVersion;
-
-                       // Sanity. Should we throw an exception?
-                       if (fontObject == IntPtr.Zero) {
-                               return IntPtr.Zero;
-                       }
-
                        if ((int) osInfo.Platform == 128 || (int) 
osInfo.Platform == 4) {
                                return fontObject;
                        } else {
@@ -290,27 +294,27 @@
                }
 
                public Font (FontFamily family, float emSize,  GraphicsUnit 
unit)
-                       : this (family, emSize, FontStyle.Regular, unit, 
(byte)0, false)
+                       : this (family, emSize, FontStyle.Regular, unit, 
DefaultCharSet, false)
                {
                }
 
                public Font (string familyName, float emSize,  GraphicsUnit 
unit)
-                       : this (new FontFamily (familyName), emSize, 
FontStyle.Regular, unit, (byte)0, false)
+                       : this (new FontFamily (familyName), emSize, 
FontStyle.Regular, unit, DefaultCharSet, false)
                {
                }
 
                public Font (FontFamily family, float emSize)
-                       : this (family, emSize, FontStyle.Regular, 
GraphicsUnit.Point, (byte)0, false)
+                       : this (family, emSize, FontStyle.Regular, 
GraphicsUnit.Point, DefaultCharSet, false)
                {
                }
 
                public Font (FontFamily family, float emSize, FontStyle style)
-                       : this (family, emSize, style, GraphicsUnit.Point, 
(byte)0, false)
+                       : this (family, emSize, style, GraphicsUnit.Point, 
DefaultCharSet, false)
                {
                }
 
                public Font (FontFamily family, float emSize, FontStyle style, 
GraphicsUnit unit)
-                       : this (family, emSize, style, unit, (byte)0, false)
+                       : this (family, emSize, style, unit, DefaultCharSet, 
false)
                {
                }
 
@@ -322,7 +326,9 @@
                public Font (FontFamily family, float emSize, FontStyle style,
                                GraphicsUnit unit, byte charSet, bool 
isVertical)
                {
-                       // MS does not accept null family
+                       if (family == null)
+                               throw new ArgumentNullException ("family");
+
                        Status status;
                        setProperties (family, emSize, style, unit, charSet, 
isVertical);               
                        status = GDIPlus.GdipCreateFont (family.NativeObject, 
emSize,  style,   unit,  out fontObject);
@@ -330,17 +336,17 @@
                }
 
                public Font (string familyName, float emSize)
-                       : this (familyName, emSize, FontStyle.Regular, 
GraphicsUnit.Point, (byte)0, false)
+                       : this (familyName, emSize, FontStyle.Regular, 
GraphicsUnit.Point, DefaultCharSet, false)
                {
                }
 
                public Font (string familyName, float emSize, FontStyle style)
-                       : this (familyName, emSize, style, GraphicsUnit.Point, 
(byte)0, false)
+                       : this (familyName, emSize, style, GraphicsUnit.Point, 
DefaultCharSet, false)
                {
                }
 
                public Font (string familyName, float emSize, FontStyle style, 
GraphicsUnit unit)
-                       : this (familyName, emSize, style, unit, (byte)0, false)
+                       : this (familyName, emSize, style, unit, 
DefaultCharSet, false)
                {
                }
 
@@ -362,11 +368,8 @@
 
                internal IntPtr NativeObject {            
                        get {
-                                       return fontObject;
+                               return fontObject;
                        }
-                       set {
-                                       fontObject = value;
-                       }
                }
 
 #if NET_2_0
@@ -543,7 +546,8 @@
                {
                        IntPtr newObject;
                        LOGFONT o = (LOGFONT)lf;
-                       GDIPlus.GdipCreateFontFromLogfont (hdc, ref o, out 
newObject);
+                       Status status = GDIPlus.GdipCreateFontFromLogfont (hdc, 
ref o, out newObject);
+                       GDIPlus.CheckStatus (status);
                        return new Font (newObject, "Microsoft Sans Serif", 
FontStyle.Regular, 10);
                }
 
@@ -642,15 +646,16 @@
                public float GetHeight (Graphics graphics)
                {
                        float size;
-                       
-                       GDIPlus.GdipGetFontHeight (fontObject, 
graphics.NativeObject, out size);
+                       Status status = GDIPlus.GdipGetFontHeight (fontObject, 
graphics.NativeObject, out size);
+                       GDIPlus.CheckStatus (status);
                        return size;
                }
 
                public float GetHeight (float dpi)
                {
                        float size;
-                       GDIPlus.GdipGetFontHeightGivenDPI (fontObject, dpi, out 
size);
+                       Status status = GDIPlus.GdipGetFontHeightGivenDPI 
(fontObject, dpi, out size);
+                       GDIPlus.CheckStatus (status);
                        return size;
                }
 

Modified: trunk/mcs/class/System.Drawing/System.Drawing/FontFamily.cs
===================================================================
--- trunk/mcs/class/System.Drawing/System.Drawing/FontFamily.cs 2006-05-30 
18:54:32 UTC (rev 61276)
+++ trunk/mcs/class/System.Drawing/System.Drawing/FontFamily.cs 2006-05-30 
18:59:11 UTC (rev 61277)
@@ -42,7 +42,7 @@
                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)
                {
@@ -57,19 +57,12 @@
                        if (nativeFontFamily == IntPtr.Zero)
                                return;
 
-                       sb = new StringBuilder(GDIPlus.FACESIZE);
+                       sb = new StringBuilder (GDIPlus.FACESIZE);
                        Status status = GDIPlus.GdipGetFamilyName 
(nativeFontFamily, sb, 0);
                        GDIPlus.CheckStatus (status);
                        name = sb.ToString();
                }
                
-               //Need to come back here, is Arial the right thing to do
-               internal FontFamily() : this ("Arial", null)
-               {
-                                                                               
-               }
-               
-               
                ~FontFamily()
                {       
                        Dispose ();
@@ -81,22 +74,12 @@
                        {
                                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);
@@ -107,6 +90,7 @@
                                        GDIPlus.CheckStatus (status);
                                        refreshName ();
                                        break;
+                               case GenericFontFamilies.Monospace:
                                default:        // Undocumented default 
                                        status = 
GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
                                        GDIPlus.CheckStatus (status);
@@ -121,96 +105,37 @@
                
                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."));
                                return name;
                        }
                }
                
-               public static FontFamily GenericMonospace 
-               {
-                       get 
-                       {
-                               
-                               IntPtr generic = IntPtr.Zero;
-                               Status status = 
GDIPlus.GdipGetGenericFontFamilyMonospace (out generic);
-                               GDIPlus.CheckStatus (status);
-                               FontFamily genericMonospace = new FontFamily 
(generic);                                         
-                               genericMonospace.refreshName ();
-                               return genericMonospace;
-                       }
+               public static FontFamily GenericMonospace {
+                       get { return new FontFamily 
(GenericFontFamilies.Monospace); }
                }
                
-               public static FontFamily GenericSansSerif 
-               {
-                       get 
-                       {
-                               IntPtr generic = IntPtr.Zero;
-                               Status status = 
GDIPlus.GdipGetGenericFontFamilySansSerif (out generic);
-                               GDIPlus.CheckStatus (status);
-                               FontFamily genericSansSerif = new FontFamily 
(generic);
-                               genericSansSerif.refreshName ();
-                               return genericSansSerif;
-                       }
+               public static FontFamily GenericSansSerif {
+                       get { return new FontFamily 
(GenericFontFamilies.SansSerif); }
                }
                
-               public static FontFamily GenericSerif 
-               {
-                       get 
-                       {
-                               IntPtr generic = IntPtr.Zero;
-                               Status status = 
GDIPlus.GdipGetGenericFontFamilySerif (out generic);
-                               GDIPlus.CheckStatus (status);
-                               FontFamily 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;
                        short outProperty;
-                       int styleCheck = GetStyleCheck (style);                 
        
-                       status = GDIPlus.GdipGetCellAscent (nativeFontFamily, 
styleCheck, out outProperty);
+                       Status status = GDIPlus.GdipGetCellAscent 
(nativeFontFamily, (int)style, out outProperty);
                        GDIPlus.CheckStatus (status);
 
                        return (int) outProperty;
@@ -218,10 +143,8 @@
                
                public int GetCellDescent (FontStyle style) 
                {
-                       Status status;
                        short outProperty;
-                       int styleCheck = GetStyleCheck (style);                 
        
-                       status = GDIPlus.GdipGetCellDescent (nativeFontFamily, 
styleCheck, out outProperty);
+                       Status status = GDIPlus.GdipGetCellDescent 
(nativeFontFamily, (int)style, out outProperty);
                        GDIPlus.CheckStatus (status);
 
                        return (int) outProperty;
@@ -229,10 +152,8 @@
                
                public int GetEmHeight (FontStyle style) 
                {
-                       Status status;
                        short outProperty;
-                       int styleCheck = GetStyleCheck (style);                 
        
-                       status = GDIPlus.GdipGetEmHeight (nativeFontFamily, 
styleCheck, out outProperty);
+                       Status status = GDIPlus.GdipGetEmHeight 
(nativeFontFamily, (int)style, out outProperty);
                        GDIPlus.CheckStatus (status);
 
                        return (int) outProperty;
@@ -240,59 +161,56 @@
                
                public int GetLineSpacing (FontStyle style)
                {
-                       Status status;
                        short outProperty;
-                       int styleCheck = GetStyleCheck (style);                 
        
-                       status = GDIPlus.GdipGetLineSpacing (nativeFontFamily, 
styleCheck, out 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;                 
                                        
-                       GC.SuppressFinalize(this);
+                       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;
-                               
-                       FontFamily o = (FontFamily) obj;
-                       return (nativeFontFamily == o.nativeFontFamily);        
                
+
+                       return (Name == o.Name);
                }
                
                public override int GetHashCode ()
                {
-                       return name.GetHashCode ();                     
+                       return Name.GetHashCode ();                     
                }
                        
                        
-               public static 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;                 
                }
@@ -300,14 +218,13 @@
                [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, "]");
                }
-
        }
 }
 

Modified: trunk/mcs/class/System.Drawing/System.Drawing/gdipFunctions.cs
===================================================================
--- trunk/mcs/class/System.Drawing/System.Drawing/gdipFunctions.cs      
2006-05-30 18:54:32 UTC (rev 61276)
+++ trunk/mcs/class/System.Drawing/System.Drawing/gdipFunctions.cs      
2006-05-30 18:59:11 UTC (rev 61277)
@@ -204,6 +204,9 @@
                                case Status.WrongState:
                                        throw new ArgumentException 
("Properties not set properly.");
 
+                               case Status.FontFamilyNotFound:
+                                       throw new ArgumentException 
("FontFamily wasn't found.");
+
                                default:
                                        throw new Exception ("Unknown Error.");
                        }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to