Fixes GdipGet* signatures function calls
[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 //
8 // Copyright (C) 2002/2004 Ximian, Inc http://www.ximian.com
9 //
10 // Copyright (C) 2004 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 using System;
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 = null;
42                 static private FontFamily genericSansSerif = null;
43                 static private FontFamily genericSerif = null;
44                 private string name;
45                 internal IntPtr nativeFontFamily = IntPtr.Zero;
46                                 
47                 internal FontFamily(IntPtr fntfamily)
48                 {
49                         nativeFontFamily = fntfamily;           
50                         refreshName();                  
51                 }
52                 
53                 internal void refreshName()
54                 {
55                         if (nativeFontFamily != IntPtr.Zero) {
56                                 int language = 0;                       
57                                 StringBuilder sBuilder = new StringBuilder (GDIPlus.FACESIZE * UnicodeEncoding.CharSize);       
58                                 Status status = GDIPlus.GdipGetFamilyName (nativeFontFamily, sBuilder, language);
59                                 GDIPlus.CheckStatus (status);
60                                 name = sBuilder.ToString();                             
61                         }
62                 }
63                 
64                 //Need to come back here, is Arial the right thing to do
65                 internal FontFamily() : this ("Arial", null)
66                 {
67                                                                                 
68                 }
69                 
70                 
71                 ~FontFamily()
72                 {       
73                         Dispose ();
74                 }
75
76                 internal IntPtr NativeObject
77                 {            
78                         get     
79                         {
80                                 return nativeFontFamily;
81                         }
82                         set     
83                         {
84                                 nativeFontFamily = value;
85                         }
86                 }
87
88                 public FontFamily(GenericFontFamilies genericFamily) 
89                 {
90                         Status status;
91                         switch (genericFamily) 
92                         {
93                                 case GenericFontFamilies.Monospace:
94                                         status = GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
95                                         GDIPlus.CheckStatus (status);
96                                         refreshName ();
97                                         break;
98                                 case GenericFontFamilies.SansSerif:
99                                         status = GDIPlus.GdipGetGenericFontFamilySansSerif (out nativeFontFamily);
100                                         GDIPlus.CheckStatus (status);
101                                         refreshName ();
102                                         break;
103                                 case GenericFontFamilies.Serif:
104                                         status = GDIPlus.GdipGetGenericFontFamilySerif (out nativeFontFamily);
105                                         GDIPlus.CheckStatus (status);
106                                         refreshName ();
107                                         break;
108                                 default:        // Undocumented default 
109                                         status = GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
110                                         GDIPlus.CheckStatus (status);
111                                         refreshName ();
112                                         break;
113                         }
114                 }
115                 
116                 public FontFamily(string familyName) : this (familyName, null)
117                 {                       
118                 }
119                 
120                 public FontFamily (string familyName, FontCollection collection) 
121                 {
122                         Status status;
123                         if ( collection != null )
124                                 status = GDIPlus.GdipCreateFontFamilyFromName (familyName, collection.nativeFontCollection, out nativeFontFamily);
125                         else
126                                 status = GDIPlus.GdipCreateFontFamilyFromName (familyName, IntPtr.Zero, out nativeFontFamily);
127                         GDIPlus.CheckStatus (status);
128                         
129                         refreshName ();
130                 }
131                 
132                 public string Name 
133                 {
134                         get 
135                         {
136                                 return name;
137                         }
138                 }
139                 
140                 public static FontFamily GenericMonospace 
141                 {
142                         get 
143                         {
144                                 if (genericMonospace == null) 
145                                 {
146                                         IntPtr generic = IntPtr.Zero;
147                                         Status status = GDIPlus.GdipGetGenericFontFamilyMonospace (out generic);
148                                         GDIPlus.CheckStatus (status);
149                                         genericMonospace = new FontFamily (generic);                                            
150                                         genericMonospace.refreshName ();
151                                         
152                                 }
153                                 return genericMonospace;
154                         }
155                 }
156                 
157                 public static FontFamily GenericSansSerif 
158                 {
159                         get 
160                         {
161                                 if (genericSansSerif == null) 
162                                 {
163                                         IntPtr generic = IntPtr.Zero;
164                                         Status status = GDIPlus.GdipGetGenericFontFamilySansSerif (out generic);
165                                         GDIPlus.CheckStatus (status);
166                                         genericSansSerif = new FontFamily (generic);
167                                         genericSansSerif.refreshName ();
168                                         
169                                 }
170                                 return genericSansSerif;
171                         }
172                 }
173                 
174                 public static FontFamily GenericSerif 
175                 {
176                         get 
177                         {
178                                 if (genericSerif == null) 
179                                 {
180                                         IntPtr generic = IntPtr.Zero;
181                                         Status status = GDIPlus.GdipGetGenericFontFamilySerif (out generic);
182                                         GDIPlus.CheckStatus (status);
183                                         genericSerif = new FontFamily (generic);
184                                         genericSerif.refreshName ();
185                                 }
186                                 return genericSerif;
187                         }
188                 }
189                 
190                 //[MONO TODO]
191                 //Need to check how to get the Flags attribute to read 
192                 //bitwise value of the enumeration
193                 internal int GetStyleCheck(FontStyle style)
194                 {
195                         int styleCheck = 0 ;
196                         switch ( style) {
197                                 case FontStyle.Bold:
198                                         styleCheck = 1;
199                                         break;
200                                 case FontStyle.Italic:
201                                         styleCheck = 2;
202                                         break;
203                                 case FontStyle.Regular:
204                                         styleCheck = 0;
205                                         break;
206                                 case FontStyle.Strikeout:
207                                         styleCheck = 8;
208                                         break;
209                                 case FontStyle.Underline:
210                                         styleCheck = 4;
211                                         break;
212                         }
213                         return styleCheck;
214                 }
215
216                 public int GetCellAscent (FontStyle style) 
217                 {
218                         Status status;
219                         short outProperty;
220                         int styleCheck = GetStyleCheck (style);                         
221                         status = GDIPlus.GdipGetCellAscent (nativeFontFamily, styleCheck, out outProperty);
222                         GDIPlus.CheckStatus (status);
223
224                         return (int) outProperty;
225                 }
226                 
227                 public int GetCellDescent (FontStyle style) 
228                 {
229                         Status status;
230                         short outProperty;
231                         int styleCheck = GetStyleCheck (style);                         
232                         status = GDIPlus.GdipGetCellDescent (nativeFontFamily, styleCheck, out outProperty);
233                         GDIPlus.CheckStatus (status);
234
235                         return (int) outProperty;
236                 }
237                 
238                 public int GetEmHeight (FontStyle style) 
239                 {
240                         Status status;
241                         short outProperty;
242                         int styleCheck = GetStyleCheck (style);                         
243                         status = GDIPlus.GdipGetEmHeight (nativeFontFamily, styleCheck, out outProperty);
244                         GDIPlus.CheckStatus (status);
245
246                         return (int) outProperty;
247                 }
248                 
249                 public int GetLineSpacing (FontStyle style)
250                 {
251                         Status status;
252                         short outProperty;
253                         int styleCheck = GetStyleCheck (style);                         
254                         status = GDIPlus.GdipGetLineSpacing (nativeFontFamily, styleCheck, out outProperty);
255                         GDIPlus.CheckStatus (status);   
256
257                         return (int) outProperty;
258                 }
259                 
260                 public bool IsStyleAvailable (FontStyle style)
261                 {
262                         Status status;
263                         bool outProperty;
264                         int styleCheck = GetStyleCheck (style);                         
265                         status = GDIPlus.GdipIsStyleAvailable (nativeFontFamily, styleCheck, out outProperty);
266                         GDIPlus.CheckStatus (status);
267
268                         return outProperty;
269                 }
270                 
271                 public void Dispose ()
272                 {       
273                         Status status = GDIPlus.GdipDeleteFontFamily (nativeFontFamily);
274                         if ( status == Status.Ok ) 
275                                 nativeFontFamily = IntPtr.Zero;                                                         
276                 }               
277                 
278                 public override bool Equals(object obj)
279                 {
280                         if (!(obj is FontFamily))
281                                 return false;
282
283                         return (this == (FontFamily) obj);                      
284                 }
285                 
286                 public override int GetHashCode ()
287                 {
288                         return name.GetHashCode ();                     
289                 }
290                         
291                         
292                 public static FontFamily[] Families
293                 {
294                         get {
295                                 
296                                 return GetFamilies (null);
297                         }
298                 }               
299                 
300                 public static FontFamily[] GetFamilies (Graphics graphics)
301                 {
302                         InstalledFontCollection fntcol = new InstalledFontCollection ();
303                         return fntcol.Families;                 
304                 }
305                 
306                 [MonoTODO ("We only support the default system language")]
307                 public string GetName (int language)
308                 {
309                         return name;
310                 }
311                 
312                 public override string ToString ()
313                 {
314                         return "FontFamily :" + name;
315                 }
316
317         }
318 }
319