04f8c087e025ee757b8d13644f666063ff3cdf22
[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                 // For CoreFX compatibility
79                 internal IntPtr NativeFamily
80                 {            
81                         get     
82                         {
83                                 return nativeFontFamily;
84                         }
85                 }
86
87                 public FontFamily (GenericFontFamilies genericFamily) 
88                 {
89                         Status status;
90                         switch (genericFamily) {
91                                 case GenericFontFamilies.SansSerif:
92                                         status = GDIPlus.GdipGetGenericFontFamilySansSerif (out nativeFontFamily);
93                                         break;
94                                 case GenericFontFamilies.Serif:
95                                         status = GDIPlus.GdipGetGenericFontFamilySerif (out nativeFontFamily);
96                                         break;
97                                 case GenericFontFamilies.Monospace:
98                                 default:        // Undocumented default 
99                                         status = GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
100                                         break;
101                         }
102                         GDIPlus.CheckStatus (status);
103                 }
104                 
105                 public FontFamily(string name) : this (name, null)
106                 {                       
107                 }
108
109                 public FontFamily (string name, FontCollection fontCollection) 
110                 {
111                         IntPtr handle = (fontCollection == null) ? IntPtr.Zero : fontCollection.nativeFontCollection;
112                         Status status = GDIPlus.GdipCreateFontFamilyFromName (name, handle, out nativeFontFamily);
113                         GDIPlus.CheckStatus (status);
114                 }
115                 
116                 public string Name {
117                         get {
118                                 if (nativeFontFamily == IntPtr.Zero)
119                                         throw new ArgumentException ("Name", Locale.GetText ("Object was disposed."));
120                                 if (name == null)
121                                         refreshName ();
122                                 return name;
123                         }
124                 }
125                 
126                 public static FontFamily GenericMonospace {
127                         get { return new FontFamily (GenericFontFamilies.Monospace); }
128                 }
129                 
130                 public static FontFamily GenericSansSerif {
131                         get { return new FontFamily (GenericFontFamilies.SansSerif); }
132                 }
133                 
134                 public static FontFamily GenericSerif {
135                         get { return new FontFamily (GenericFontFamilies.Serif); }
136                 }
137                 
138                 public int GetCellAscent (FontStyle style) 
139                 {
140                         short outProperty;
141                         Status status = GDIPlus.GdipGetCellAscent (nativeFontFamily, (int)style, out outProperty);
142                         GDIPlus.CheckStatus (status);
143
144                         return (int) outProperty;
145                 }
146                 
147                 public int GetCellDescent (FontStyle style) 
148                 {
149                         short outProperty;
150                         Status status = GDIPlus.GdipGetCellDescent (nativeFontFamily, (int)style, out outProperty);
151                         GDIPlus.CheckStatus (status);
152
153                         return (int) outProperty;
154                 }
155                 
156                 public int GetEmHeight (FontStyle style) 
157                 {
158                         short outProperty;
159                         Status status = GDIPlus.GdipGetEmHeight (nativeFontFamily, (int)style, out outProperty);
160                         GDIPlus.CheckStatus (status);
161
162                         return (int) outProperty;
163                 }
164                 
165                 public int GetLineSpacing (FontStyle style)
166                 {
167                         short outProperty;
168                         Status status = GDIPlus.GdipGetLineSpacing (nativeFontFamily, (int)style, out outProperty);
169                         GDIPlus.CheckStatus (status);   
170
171                         return (int) outProperty;
172                 }
173
174                 [MonoDocumentationNote ("When used with libgdiplus this method always return true (styles are created on demand).")]
175                 public bool IsStyleAvailable (FontStyle style)
176                 {
177                         bool outProperty;
178                         Status status = GDIPlus.GdipIsStyleAvailable (nativeFontFamily, (int)style, out outProperty);
179                         GDIPlus.CheckStatus (status);
180
181                         return outProperty;
182                 }
183                 
184                 public void Dispose ()
185                 {
186                         if (nativeFontFamily != IntPtr.Zero) {
187                                 Status status = GDIPlus.GdipDeleteFontFamily (nativeFontFamily);
188                                 nativeFontFamily = IntPtr.Zero;
189                                 GC.SuppressFinalize (this);
190                                 // check the status code (throw) at the last step
191                                 GDIPlus.CheckStatus (status);
192                         }
193                 }               
194                 
195                 public override bool Equals (object obj)
196                 {
197                         FontFamily o = (obj as FontFamily);
198                         if (o == null)
199                                 return false;
200
201                         return (Name == o.Name);
202                 }
203                 
204                 public override int GetHashCode ()
205                 {
206                         return Name.GetHashCode ();                     
207                 }
208                         
209                         
210                 public static FontFamily[] Families {
211                         get { return new InstalledFontCollection ().Families; }
212                 }               
213                 
214                 public static FontFamily[] GetFamilies (Graphics graphics)
215                 {
216                         if (graphics == null)
217                                 throw new ArgumentNullException ("graphics");
218
219                         InstalledFontCollection fntcol = new InstalledFontCollection ();
220                         return fntcol.Families;                 
221                 }
222                 
223                 [MonoLimitation ("The language parameter is ignored. We always return the name using the default system language.")]
224                 public string GetName (int language)
225                 {
226                         return Name;
227                 }
228                 
229                 public override string ToString ()
230                 {
231                         return String.Concat ("[FontFamily: Name=", Name, "]");
232                 }
233         }
234 }
235