2010-06-03 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Drawing / System.Drawing / FontFamily.cs
blobfc56f466d91ec043b94bad258af0d3e7f5915d53
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 void refreshName()
54 StringBuilder sb;
56 if (nativeFontFamily == IntPtr.Zero)
57 return;
59 sb = new StringBuilder (GDIPlus.FACESIZE);
60 Status status = GDIPlus.GdipGetFamilyName (nativeFontFamily, sb, 0);
61 GDIPlus.CheckStatus (status);
62 name = sb.ToString();
65 ~FontFamily()
67 Dispose ();
70 internal IntPtr NativeObject
72 get
74 return nativeFontFamily;
78 public FontFamily (GenericFontFamilies genericFamily)
80 Status status;
81 switch (genericFamily) {
82 case GenericFontFamilies.SansSerif:
83 status = GDIPlus.GdipGetGenericFontFamilySansSerif (out nativeFontFamily);
84 break;
85 case GenericFontFamilies.Serif:
86 status = GDIPlus.GdipGetGenericFontFamilySerif (out nativeFontFamily);
87 break;
88 case GenericFontFamilies.Monospace:
89 default: // Undocumented default
90 status = GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
91 break;
93 GDIPlus.CheckStatus (status);
96 public FontFamily(string name) : this (name, null)
100 public FontFamily (string name, FontCollection fontCollection)
102 IntPtr handle = (fontCollection == null) ? IntPtr.Zero : fontCollection.nativeFontCollection;
103 Status status = GDIPlus.GdipCreateFontFamilyFromName (name, handle, out nativeFontFamily);
104 GDIPlus.CheckStatus (status);
107 public string Name {
108 get {
109 if (nativeFontFamily == IntPtr.Zero)
110 throw new ArgumentException ("Name", Locale.GetText ("Object was disposed."));
111 if (name == null)
112 refreshName ();
113 return name;
117 public static FontFamily GenericMonospace {
118 get { return new FontFamily (GenericFontFamilies.Monospace); }
121 public static FontFamily GenericSansSerif {
122 get { return new FontFamily (GenericFontFamilies.SansSerif); }
125 public static FontFamily GenericSerif {
126 get { return new FontFamily (GenericFontFamilies.Serif); }
129 public int GetCellAscent (FontStyle style)
131 short outProperty;
132 Status status = GDIPlus.GdipGetCellAscent (nativeFontFamily, (int)style, out outProperty);
133 GDIPlus.CheckStatus (status);
135 return (int) outProperty;
138 public int GetCellDescent (FontStyle style)
140 short outProperty;
141 Status status = GDIPlus.GdipGetCellDescent (nativeFontFamily, (int)style, out outProperty);
142 GDIPlus.CheckStatus (status);
144 return (int) outProperty;
147 public int GetEmHeight (FontStyle style)
149 short outProperty;
150 Status status = GDIPlus.GdipGetEmHeight (nativeFontFamily, (int)style, out outProperty);
151 GDIPlus.CheckStatus (status);
153 return (int) outProperty;
156 public int GetLineSpacing (FontStyle style)
158 short outProperty;
159 Status status = GDIPlus.GdipGetLineSpacing (nativeFontFamily, (int)style, out outProperty);
160 GDIPlus.CheckStatus (status);
162 return (int) outProperty;
165 [MonoDocumentationNote ("When used with libgdiplus this method always return true (styles are created on demand).")]
166 public bool IsStyleAvailable (FontStyle style)
168 bool outProperty;
169 Status status = GDIPlus.GdipIsStyleAvailable (nativeFontFamily, (int)style, out outProperty);
170 GDIPlus.CheckStatus (status);
172 return outProperty;
175 public void Dispose ()
177 if (nativeFontFamily != IntPtr.Zero) {
178 Status status = GDIPlus.GdipDeleteFontFamily (nativeFontFamily);
179 nativeFontFamily = IntPtr.Zero;
180 GC.SuppressFinalize (this);
181 // check the status code (throw) at the last step
182 GDIPlus.CheckStatus (status);
186 public override bool Equals (object obj)
188 FontFamily o = (obj as FontFamily);
189 if (o == null)
190 return false;
192 return (Name == o.Name);
195 public override int GetHashCode ()
197 return Name.GetHashCode ();
201 public static FontFamily[] Families {
202 get { return new InstalledFontCollection ().Families; }
205 public static FontFamily[] GetFamilies (Graphics graphics)
207 if (graphics == null)
208 throw new ArgumentNullException ("graphics");
210 InstalledFontCollection fntcol = new InstalledFontCollection ();
211 return fntcol.Families;
214 [MonoLimitation ("The language parameter is ignored. We always return the name using the default system language.")]
215 public string GetName (int language)
217 return Name;
220 public override string ToString ()
222 return String.Concat ("[FontFamily: Name=", Name, "]");