2007-12-19 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / System.Drawing / FontFamily.cs
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)
11 //
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.
30 //
31
32 using System.Drawing.Text;
33 using System.Text;
34 using System.Runtime.InteropServices;
35
36 namespace System.Drawing {
37
38         public sealed class FontFamily : MarshalByRefObject, IDisposable 
39         {
40                 
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;
46                                 
47                 internal FontFamily(IntPtr fntfamily)
48                 {
49                         nativeFontFamily = fntfamily;           
50                 }
51                 
52                 internal void refreshName()
53                 {
54                         StringBuilder sb;
55
56                         if (nativeFontFamily == IntPtr.Zero)
57                                 return;
58
59                         sb = new StringBuilder (GDIPlus.FACESIZE);
60                         Status status = GDIPlus.GdipGetFamilyName (nativeFontFamily, sb, 0);
61                         GDIPlus.CheckStatus (status);
62                         name = sb.ToString();
63                 }
64                 
65                 ~FontFamily()
66                 {       
67                         Dispose ();
68                 }
69
70                 internal IntPtr NativeObject
71                 {            
72                         get     
73                         {
74                                 return nativeFontFamily;
75                         }
76                 }
77
78                 public FontFamily (GenericFontFamilies genericFamily) 
79                 {
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;
92                         }
93                         GDIPlus.CheckStatus (status);
94                 }
95                 
96                 public FontFamily(string familyName) : this (familyName, null)
97                 {                       
98                 }
99                 
100                 public FontFamily (string familyName, FontCollection collection) 
101                 {
102                         IntPtr handle = (collection == null) ? IntPtr.Zero : collection.nativeFontCollection;
103                         Status status = GDIPlus.GdipCreateFontFamilyFromName (familyName, handle, out nativeFontFamily);
104                         GDIPlus.CheckStatus (status);
105                 }
106                 
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;
114                         }
115                 }
116                 
117                 public static FontFamily GenericMonospace {
118                         get { return new FontFamily (GenericFontFamilies.Monospace); }
119                 }
120                 
121                 public static FontFamily GenericSansSerif {
122                         get { return new FontFamily (GenericFontFamilies.SansSerif); }
123                 }
124                 
125                 public static FontFamily GenericSerif {
126                         get { return new FontFamily (GenericFontFamilies.Serif); }
127                 }
128                 
129                 public int GetCellAscent (FontStyle style) 
130                 {
131                         short outProperty;
132                         Status status = GDIPlus.GdipGetCellAscent (nativeFontFamily, (int)style, out outProperty);
133                         GDIPlus.CheckStatus (status);
134
135                         return (int) outProperty;
136                 }
137                 
138                 public int GetCellDescent (FontStyle style) 
139                 {
140                         short outProperty;
141                         Status status = GDIPlus.GdipGetCellDescent (nativeFontFamily, (int)style, out outProperty);
142                         GDIPlus.CheckStatus (status);
143
144                         return (int) outProperty;
145                 }
146                 
147                 public int GetEmHeight (FontStyle style) 
148                 {
149                         short outProperty;
150                         Status status = GDIPlus.GdipGetEmHeight (nativeFontFamily, (int)style, out outProperty);
151                         GDIPlus.CheckStatus (status);
152
153                         return (int) outProperty;
154                 }
155                 
156                 public int GetLineSpacing (FontStyle style)
157                 {
158                         short outProperty;
159                         Status status = GDIPlus.GdipGetLineSpacing (nativeFontFamily, (int)style, out outProperty);
160                         GDIPlus.CheckStatus (status);   
161
162                         return (int) outProperty;
163                 }
164
165                 [MonoDocumentationNote ("When used with libgdiplus this method always return true (styles are created on demand).")]
166                 public bool IsStyleAvailable (FontStyle style)
167                 {
168                         bool outProperty;
169                         Status status = GDIPlus.GdipIsStyleAvailable (nativeFontFamily, (int)style, out outProperty);
170                         GDIPlus.CheckStatus (status);
171
172                         return outProperty;
173                 }
174                 
175                 public void Dispose ()
176                 {
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);
183                         }
184                 }               
185                 
186                 public override bool Equals (object obj)
187                 {
188                         FontFamily o = (obj as FontFamily);
189                         if (o == null)
190                                 return false;
191
192                         return (Name == o.Name);
193                 }
194                 
195                 public override int GetHashCode ()
196                 {
197                         return Name.GetHashCode ();                     
198                 }
199                         
200                         
201                 public static FontFamily[] Families {
202                         get { return new InstalledFontCollection ().Families; }
203                 }               
204                 
205                 public static FontFamily[] GetFamilies (Graphics graphics)
206                 {
207                         if (graphics == null)
208                                 throw new ArgumentNullException ("graphics");
209
210                         InstalledFontCollection fntcol = new InstalledFontCollection ();
211                         return fntcol.Families;                 
212                 }
213                 
214                 [MonoLimitation ("The language parameter is ignored. We always return the name using the default system language.")]
215                 public string GetName (int language)
216                 {
217                         return Name;
218                 }
219                 
220                 public override string ToString ()
221                 {
222                         return String.Concat ("[FontFamily: Name=", Name, "]");
223                 }
224         }
225 }
226