fix leak in FontFamily
[mono-project.git] / mcs / class / System.Drawing / System.Drawing / FontFamily.cs
blob79b63b9b5f92ab391ffb66b5cc1df7fdef6337ce
1 //
2 // System.Drawing.FontFamily.cs
3 //
4 // Author:
5 // Dennis Hayes (dennish@Raytek.com)
6 // Alexandre Pigolkine (pigolkine@gmx.de)
7 // Peter Dennis Bartok (pbartok@novell.com)
8 //
9 // Copyright (C) 2002/2004 Ximian, Inc http://www.ximian.com
10 // Copyright (C) 2004 - 2006 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Drawing.Text;
33 using System.Text;
34 using System.Runtime.InteropServices;
36 namespace System.Drawing {
38 public sealed class FontFamily : MarshalByRefObject, IDisposable
41 //static private FontFamily genericMonospace;
42 //static private FontFamily genericSansSerif;
43 //static private FontFamily genericSerif;
44 private string name;
45 private IntPtr nativeFontFamily = IntPtr.Zero;
47 internal FontFamily(IntPtr fntfamily)
49 nativeFontFamily = fntfamily;
52 internal unsafe void refreshName()
54 if (nativeFontFamily == IntPtr.Zero)
55 return;
57 char* namePtr = stackalloc char[GDIPlus.FACESIZE];
58 Status status = GDIPlus.GdipGetFamilyName (nativeFontFamily, (IntPtr)namePtr, 0);
59 GDIPlus.CheckStatus (status);
60 name = Marshal.PtrToStringUni((IntPtr)namePtr);
63 ~FontFamily()
65 Dispose ();
68 internal IntPtr NativeObject
70 get
72 return nativeFontFamily;
76 // For CoreFX compatibility
77 internal IntPtr NativeFamily
79 get
81 return nativeFontFamily;
85 public FontFamily (GenericFontFamilies genericFamily)
87 Status status;
88 switch (genericFamily) {
89 case GenericFontFamilies.SansSerif:
90 status = GDIPlus.GdipGetGenericFontFamilySansSerif (out nativeFontFamily);
91 break;
92 case GenericFontFamilies.Serif:
93 status = GDIPlus.GdipGetGenericFontFamilySerif (out nativeFontFamily);
94 break;
95 case GenericFontFamilies.Monospace:
96 default: // Undocumented default
97 status = GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
98 break;
100 GDIPlus.CheckStatus (status);
103 public FontFamily(string name) : this (name, null)
107 public FontFamily (string name, FontCollection fontCollection)
109 IntPtr handle = (fontCollection == null) ? IntPtr.Zero : fontCollection._nativeFontCollection;
110 Status status = GDIPlus.GdipCreateFontFamilyFromName (name, handle, out nativeFontFamily);
111 GDIPlus.CheckStatus (status);
114 public string Name {
115 get {
116 if (nativeFontFamily == IntPtr.Zero)
117 throw new ArgumentException ("Name", Locale.GetText ("Object was disposed."));
118 if (name == null)
119 refreshName ();
120 return name;
124 public static FontFamily GenericMonospace {
125 get { return new FontFamily (GenericFontFamilies.Monospace); }
128 public static FontFamily GenericSansSerif {
129 get { return new FontFamily (GenericFontFamilies.SansSerif); }
132 public static FontFamily GenericSerif {
133 get { return new FontFamily (GenericFontFamilies.Serif); }
136 public int GetCellAscent (FontStyle style)
138 short outProperty;
139 Status status = GDIPlus.GdipGetCellAscent (nativeFontFamily, (int)style, out outProperty);
140 GDIPlus.CheckStatus (status);
142 return (int) outProperty;
145 public int GetCellDescent (FontStyle style)
147 short outProperty;
148 Status status = GDIPlus.GdipGetCellDescent (nativeFontFamily, (int)style, out outProperty);
149 GDIPlus.CheckStatus (status);
151 return (int) outProperty;
154 public int GetEmHeight (FontStyle style)
156 short outProperty;
157 Status status = GDIPlus.GdipGetEmHeight (nativeFontFamily, (int)style, out outProperty);
158 GDIPlus.CheckStatus (status);
160 return (int) outProperty;
163 public int GetLineSpacing (FontStyle style)
165 short outProperty;
166 Status status = GDIPlus.GdipGetLineSpacing (nativeFontFamily, (int)style, out outProperty);
167 GDIPlus.CheckStatus (status);
169 return (int) outProperty;
172 [MonoDocumentationNote ("When used with libgdiplus this method always return true (styles are created on demand).")]
173 public bool IsStyleAvailable (FontStyle style)
175 bool outProperty;
176 Status status = GDIPlus.GdipIsStyleAvailable (nativeFontFamily, (int)style, out outProperty);
177 GDIPlus.CheckStatus (status);
179 return outProperty;
182 public void Dispose ()
184 if (nativeFontFamily != IntPtr.Zero) {
185 Status status = GDIPlus.GdipDeleteFontFamily (nativeFontFamily);
186 nativeFontFamily = IntPtr.Zero;
187 GC.SuppressFinalize (this);
188 // check the status code (throw) at the last step
189 GDIPlus.CheckStatus (status);
193 public override bool Equals (object obj)
195 FontFamily o = (obj as FontFamily);
196 if (o == null)
197 return false;
199 return (Name == o.Name);
202 public override int GetHashCode ()
204 return Name.GetHashCode ();
208 public static FontFamily[] Families {
209 get { return new InstalledFontCollection ().Families; }
212 public static FontFamily[] GetFamilies (Graphics graphics)
214 if (graphics == null)
215 throw new ArgumentNullException ("graphics");
217 InstalledFontCollection fntcol = new InstalledFontCollection ();
218 return fntcol.Families;
221 [MonoLimitation ("The language parameter is ignored. We always return the name using the default system language.")]
222 public string GetName (int language)
224 return Name;
227 public override string ToString ()
229 return String.Concat ("[FontFamily: Name=", Name, "]");