2 // System.Drawing.FontFamily.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Alexandre Pigolkine (pigolkine@gmx.de)
7 // Peter Dennis Bartok (pbartok@novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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;
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;
45 private IntPtr nativeFontFamily = IntPtr.Zero;
47 internal FontFamily(IntPtr fntfamily)
49 nativeFontFamily = fntfamily;
52 internal void refreshName()
56 if (nativeFontFamily == IntPtr.Zero)
59 sb = new StringBuilder (GDIPlus.FACESIZE);
60 Status status = GDIPlus.GdipGetFamilyName (nativeFontFamily, sb, 0);
61 GDIPlus.CheckStatus (status);
70 internal IntPtr NativeObject
74 return nativeFontFamily;
78 // For CoreFX compatibility
79 internal IntPtr NativeFamily
83 return nativeFontFamily;
87 public FontFamily (GenericFontFamilies genericFamily)
90 switch (genericFamily) {
91 case GenericFontFamilies.SansSerif:
92 status = GDIPlus.GdipGetGenericFontFamilySansSerif (out nativeFontFamily);
94 case GenericFontFamilies.Serif:
95 status = GDIPlus.GdipGetGenericFontFamilySerif (out nativeFontFamily);
97 case GenericFontFamilies.Monospace:
98 default: // Undocumented default
99 status = GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
102 GDIPlus.CheckStatus (status);
105 public FontFamily(string name) : this (name, null)
109 public FontFamily (string name, FontCollection fontCollection)
111 IntPtr handle = (fontCollection == null) ? IntPtr.Zero : fontCollection.nativeFontCollection;
112 Status status = GDIPlus.GdipCreateFontFamilyFromName (name, handle, out nativeFontFamily);
113 GDIPlus.CheckStatus (status);
118 if (nativeFontFamily == IntPtr.Zero)
119 throw new ArgumentException ("Name", Locale.GetText ("Object was disposed."));
126 public static FontFamily GenericMonospace {
127 get { return new FontFamily (GenericFontFamilies.Monospace); }
130 public static FontFamily GenericSansSerif {
131 get { return new FontFamily (GenericFontFamilies.SansSerif); }
134 public static FontFamily GenericSerif {
135 get { return new FontFamily (GenericFontFamilies.Serif); }
138 public int GetCellAscent (FontStyle style)
141 Status status = GDIPlus.GdipGetCellAscent (nativeFontFamily, (int)style, out outProperty);
142 GDIPlus.CheckStatus (status);
144 return (int) outProperty;
147 public int GetCellDescent (FontStyle style)
150 Status status = GDIPlus.GdipGetCellDescent (nativeFontFamily, (int)style, out outProperty);
151 GDIPlus.CheckStatus (status);
153 return (int) outProperty;
156 public int GetEmHeight (FontStyle style)
159 Status status = GDIPlus.GdipGetEmHeight (nativeFontFamily, (int)style, out outProperty);
160 GDIPlus.CheckStatus (status);
162 return (int) outProperty;
165 public int GetLineSpacing (FontStyle style)
168 Status status = GDIPlus.GdipGetLineSpacing (nativeFontFamily, (int)style, out outProperty);
169 GDIPlus.CheckStatus (status);
171 return (int) outProperty;
174 [MonoDocumentationNote ("When used with libgdiplus this method always return true (styles are created on demand).")]
175 public bool IsStyleAvailable (FontStyle style)
178 Status status = GDIPlus.GdipIsStyleAvailable (nativeFontFamily, (int)style, out outProperty);
179 GDIPlus.CheckStatus (status);
184 public void Dispose ()
186 if (nativeFontFamily != IntPtr.Zero) {
187 Status status = GDIPlus.GdipDeleteFontFamily (nativeFontFamily);
188 nativeFontFamily = IntPtr.Zero;
189 GC.SuppressFinalize (this);
190 // check the status code (throw) at the last step
191 GDIPlus.CheckStatus (status);
195 public override bool Equals (object obj)
197 FontFamily o = (obj as FontFamily);
201 return (Name == o.Name);
204 public override int GetHashCode ()
206 return Name.GetHashCode ();
210 public static FontFamily[] Families {
211 get { return new InstalledFontCollection ().Families; }
214 public static FontFamily[] GetFamilies (Graphics graphics)
216 if (graphics == null)
217 throw new ArgumentNullException ("graphics");
219 InstalledFontCollection fntcol = new InstalledFontCollection ();
220 return fntcol.Families;
223 [MonoLimitation ("The language parameter is ignored. We always return the name using the default system language.")]
224 public string GetName (int language)
229 public override string ToString ()
231 return String.Concat ("[FontFamily: Name=", Name, "]");