This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 // (C) 2002/2004 Ximian, Inc
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 using System;
33 using System.Drawing.Text;
34 using System.Text;
35 using System.Runtime.InteropServices;
36
37 namespace System.Drawing {
38
39         public sealed class FontFamily : MarshalByRefObject, IDisposable 
40         {
41                 
42                 static private FontFamily genericMonospace = null;
43                 static private FontFamily genericSansSerif = null;
44                 static private FontFamily genericSerif = null;
45                 private string name;
46                 internal IntPtr nativeFontFamily = IntPtr.Zero;
47                                 
48                 internal FontFamily(IntPtr fntfamily)
49                 {
50                         nativeFontFamily = fntfamily;           
51                         refreshName();                  
52                 }
53                 
54                 internal void refreshName()
55                 {
56                         if (nativeFontFamily != IntPtr.Zero) {
57                                 int language = 0;                       
58                                 StringBuilder sBuilder = new StringBuilder (GDIPlus.FACESIZE * UnicodeEncoding.CharSize);       
59                                 Status status = GDIPlus.GdipGetFamilyName (nativeFontFamily, sBuilder, language);
60                                 GDIPlus.CheckStatus (status);
61                                 name = sBuilder.ToString();                             
62                         }
63                 }
64                 
65                 //Need to come back here, is Arial the right thing to do
66                 internal FontFamily() : this ("Arial", null)
67                 {
68                                                                                 
69                 }
70                 
71                 
72                 ~FontFamily()
73                 {       
74                         Dispose ();
75                 }
76
77                 internal IntPtr NativeObject
78                 {            
79                         get     
80                         {
81                                 return nativeFontFamily;
82                         }
83                         set     
84                         {
85                                 nativeFontFamily = value;
86                         }
87                 }
88
89                 public FontFamily(GenericFontFamilies genericFamily) 
90                 {
91                         Status status;
92                         switch (genericFamily) 
93                         {
94                                 case GenericFontFamilies.Monospace:
95                                         status = GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
96                                         GDIPlus.CheckStatus (status);
97                                         refreshName ();
98                                         break;
99                                 case GenericFontFamilies.SansSerif:
100                                         status = GDIPlus.GdipGetGenericFontFamilySansSerif (out nativeFontFamily);
101                                         GDIPlus.CheckStatus (status);
102                                         refreshName ();
103                                         break;
104                                 case GenericFontFamilies.Serif:
105                                         status = GDIPlus.GdipGetGenericFontFamilySerif (out nativeFontFamily);
106                                         GDIPlus.CheckStatus (status);
107                                         refreshName ();
108                                         break;
109                                 default:        // Undocumented default 
110                                         status = GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
111                                         GDIPlus.CheckStatus (status);
112                                         refreshName ();
113                                         break;
114                         }
115                 }
116                 
117                 public FontFamily(string familyName) : this (familyName, null)
118                 {                       
119                 }
120                 
121                 public FontFamily (string familyName, FontCollection collection) 
122                 {
123                         Status status;
124                         if ( collection != null )
125                                 status = GDIPlus.GdipCreateFontFamilyFromName (familyName, collection.nativeFontCollection, out nativeFontFamily);
126                         else
127                                 status = GDIPlus.GdipCreateFontFamilyFromName (familyName, IntPtr.Zero, out nativeFontFamily);
128                         GDIPlus.CheckStatus (status);
129                         
130                         refreshName ();
131                 }
132                 
133                 public string Name 
134                 {
135                         get 
136                         {
137                                 return name;
138                         }
139                 }
140                 
141                 public static FontFamily GenericMonospace 
142                 {
143                         get 
144                         {
145                                 if (genericMonospace == null) 
146                                 {
147                                         lock (typeof (FontFamily))
148                                         {
149                                                 IntPtr generic = IntPtr.Zero;
150                                                 Status status = GDIPlus.GdipGetGenericFontFamilyMonospace (out generic);
151                                                 GDIPlus.CheckStatus (status);
152                                                 genericMonospace = new FontFamily (generic);
153                                                 genericMonospace.refreshName ();
154                                         }
155                                 }
156                                 return genericMonospace;
157                         }
158                 }
159                 
160                 public static FontFamily GenericSansSerif 
161                 {
162                         get 
163                         {
164                                 if (genericSansSerif == null) 
165                                 {
166                                         lock (typeof (FontFamily))
167                                         {
168                                                 IntPtr generic = IntPtr.Zero;
169                                                 Status status = GDIPlus.GdipGetGenericFontFamilySansSerif (out generic);
170                                                 GDIPlus.CheckStatus (status);
171                                                 genericSansSerif = new FontFamily (generic);
172                                                 genericSansSerif.refreshName ();
173                                         }
174                                 }
175                                 return genericSansSerif;
176                         }
177                 }
178                 
179                 public static FontFamily GenericSerif 
180                 {
181                         get 
182                         {
183                                 if (genericSerif == null) 
184                                 {
185                                         lock (typeof (FontFamily))
186                                         {
187                                                 IntPtr generic = IntPtr.Zero;
188                                                 Status status = GDIPlus.GdipGetGenericFontFamilySerif (out generic);
189                                                 GDIPlus.CheckStatus (status);
190                                                 genericSerif = new FontFamily (generic);
191                                                 genericSerif.refreshName ();
192                                         }
193                                 }
194                                 return genericSerif;
195                         }
196                 }
197                 
198                 //[MONO TODO]
199                 //Need to check how to get the Flags attribute to read 
200                 //bitwise value of the enumeration
201                 internal int GetStyleCheck(FontStyle style)
202                 {
203                         int styleCheck = 0 ;
204                         switch ( style) {
205                                 case FontStyle.Bold:
206                                         styleCheck = 1;
207                                         break;
208                                 case FontStyle.Italic:
209                                         styleCheck = 2;
210                                         break;
211                                 case FontStyle.Regular:
212                                         styleCheck = 0;
213                                         break;
214                                 case FontStyle.Strikeout:
215                                         styleCheck = 8;
216                                         break;
217                                 case FontStyle.Underline:
218                                         styleCheck = 4;
219                                         break;
220                         }
221                         return styleCheck;
222                 }
223
224                 public int GetCellAscent (FontStyle style) 
225                 {
226                         Status status;
227                         uint outProperty;
228                         int styleCheck = GetStyleCheck (style);                         
229                         status = GDIPlus.GdipGetCellAscent (nativeFontFamily, styleCheck, out outProperty);
230                         GDIPlus.CheckStatus (status);
231
232                         return (int) outProperty;
233                 }
234                 
235                 public int GetCellDescent (FontStyle style) 
236                 {
237                         Status status;
238                         uint outProperty;
239                         int styleCheck = GetStyleCheck (style);                         
240                         status = GDIPlus.GdipGetCellDescent (nativeFontFamily, styleCheck, out outProperty);
241                         GDIPlus.CheckStatus (status);
242
243                         return (int) outProperty;
244                 }
245                 
246                 public int GetEmHeight (FontStyle style) 
247                 {
248                         Status status;
249                         uint outProperty;
250                         int styleCheck = GetStyleCheck (style);                         
251                         status = GDIPlus.GdipGetEmHeight (nativeFontFamily, styleCheck, out outProperty);
252                         GDIPlus.CheckStatus (status);
253
254                         return (int) outProperty;
255                 }
256                 
257                 public int GetLineSpacing (FontStyle style)
258                 {
259                         Status status;
260                         uint outProperty;
261                         int styleCheck = GetStyleCheck (style);                         
262                         status = GDIPlus.GdipGetLineSpacing (nativeFontFamily, styleCheck, out outProperty);
263                         GDIPlus.CheckStatus (status);
264
265                         return (int) outProperty;
266                 }
267                 
268                 public bool IsStyleAvailable (FontStyle style)
269                 {
270                         Status status;
271                         bool outProperty;
272                         int styleCheck = GetStyleCheck (style);                         
273                         status = GDIPlus.GdipIsStyleAvailable (nativeFontFamily, styleCheck, out outProperty);
274                         GDIPlus.CheckStatus (status);
275
276                         return outProperty;
277                 }
278                 
279                 public void Dispose ()
280                 {       
281                         lock (this)
282                         {
283                                 Status status = GDIPlus.GdipDeleteFontFamily (nativeFontFamily);
284                                 if ( status == Status.Ok ) 
285                                         nativeFontFamily = IntPtr.Zero;                                                 
286                         }
287                 }
288                 
289                 
290                 public override bool Equals(object obj)
291                 {
292                         if (!(obj is FontFamily))
293                                 return false;
294
295                         return (this == (FontFamily) obj);                      
296                 }
297                 
298                 public override int GetHashCode ()
299                 {
300                         return name.GetHashCode ();                     
301                 }
302                         
303                         
304                 public FontFamily[] Families
305                 {
306                         get {
307                                 
308                                 return GetFamilies (null);
309                         }
310                 }               
311                 
312                 public static FontFamily[] GetFamilies (Graphics graphics)
313                 {
314                         InstalledFontCollection fntcol = new InstalledFontCollection ();
315                         return fntcol.Families;                 
316                 }
317                 
318                 [MonoTODO ("We only support the default system language")]
319                 public string GetName (int language)
320                 {
321                         return name;
322                 }
323                 
324                 public override string ToString ()
325                 {
326                         return "FontFamily :" + name;
327                 }
328
329         }
330 }
331