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