2 // System.Drawing.FontFamily.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Alexandre Pigolkine (pigolkine@gmx.de)
7 // (C) 2002/2004 Ximian, Inc
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
33 using System.Drawing.Text;
35 using System.Runtime.InteropServices;
37 namespace System.Drawing {
39 public sealed class FontFamily : MarshalByRefObject, IDisposable
42 static private FontFamily genericMonospace = null;
43 static private FontFamily genericSansSerif = null;
44 static private FontFamily genericSerif = null;
46 internal IntPtr nativeFontFamily = IntPtr.Zero;
48 internal FontFamily(IntPtr fntfamily)
50 nativeFontFamily = fntfamily;
54 internal void refreshName()
56 if (nativeFontFamily != IntPtr.Zero) {
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();
65 //Need to come back here, is Arial the right thing to do
66 internal FontFamily() : this ("Arial", null)
77 internal IntPtr NativeObject
81 return nativeFontFamily;
85 nativeFontFamily = value;
89 public FontFamily(GenericFontFamilies genericFamily)
92 switch (genericFamily)
94 case GenericFontFamilies.Monospace:
95 status = GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
96 GDIPlus.CheckStatus (status);
99 case GenericFontFamilies.SansSerif:
100 status = GDIPlus.GdipGetGenericFontFamilySansSerif (out nativeFontFamily);
101 GDIPlus.CheckStatus (status);
104 case GenericFontFamilies.Serif:
105 status = GDIPlus.GdipGetGenericFontFamilySerif (out nativeFontFamily);
106 GDIPlus.CheckStatus (status);
109 default: // Undocumented default
110 status = GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
111 GDIPlus.CheckStatus (status);
117 public FontFamily(string familyName) : this (familyName, null)
121 public FontFamily (string familyName, FontCollection collection)
124 if ( collection != null )
125 status = GDIPlus.GdipCreateFontFamilyFromName (familyName, collection.nativeFontCollection, out nativeFontFamily);
127 status = GDIPlus.GdipCreateFontFamilyFromName (familyName, IntPtr.Zero, out nativeFontFamily);
128 GDIPlus.CheckStatus (status);
141 public static FontFamily GenericMonospace
145 if (genericMonospace == null)
147 lock (typeof (FontFamily))
149 IntPtr generic = IntPtr.Zero;
150 Status status = GDIPlus.GdipGetGenericFontFamilyMonospace (out generic);
151 GDIPlus.CheckStatus (status);
152 genericMonospace = new FontFamily (generic);
153 genericMonospace.refreshName ();
156 return genericMonospace;
160 public static FontFamily GenericSansSerif
164 if (genericSansSerif == null)
166 lock (typeof (FontFamily))
168 IntPtr generic = IntPtr.Zero;
169 Status status = GDIPlus.GdipGetGenericFontFamilySansSerif (out generic);
170 GDIPlus.CheckStatus (status);
171 genericSansSerif = new FontFamily (generic);
172 genericSansSerif.refreshName ();
175 return genericSansSerif;
179 public static FontFamily GenericSerif
183 if (genericSerif == null)
185 lock (typeof (FontFamily))
187 IntPtr generic = IntPtr.Zero;
188 Status status = GDIPlus.GdipGetGenericFontFamilySerif (out generic);
189 GDIPlus.CheckStatus (status);
190 genericSerif = new FontFamily (generic);
191 genericSerif.refreshName ();
199 //Need to check how to get the Flags attribute to read
200 //bitwise value of the enumeration
201 internal int GetStyleCheck(FontStyle style)
208 case FontStyle.Italic:
211 case FontStyle.Regular:
214 case FontStyle.Strikeout:
217 case FontStyle.Underline:
224 public int GetCellAscent (FontStyle style)
228 int styleCheck = GetStyleCheck (style);
229 status = GDIPlus.GdipGetCellAscent (nativeFontFamily, styleCheck, out outProperty);
230 GDIPlus.CheckStatus (status);
232 return (int) outProperty;
235 public int GetCellDescent (FontStyle style)
239 int styleCheck = GetStyleCheck (style);
240 status = GDIPlus.GdipGetCellDescent (nativeFontFamily, styleCheck, out outProperty);
241 GDIPlus.CheckStatus (status);
243 return (int) outProperty;
246 public int GetEmHeight (FontStyle style)
250 int styleCheck = GetStyleCheck (style);
251 status = GDIPlus.GdipGetEmHeight (nativeFontFamily, styleCheck, out outProperty);
252 GDIPlus.CheckStatus (status);
254 return (int) outProperty;
257 public int GetLineSpacing (FontStyle style)
261 int styleCheck = GetStyleCheck (style);
262 status = GDIPlus.GdipGetLineSpacing (nativeFontFamily, styleCheck, out outProperty);
263 GDIPlus.CheckStatus (status);
265 return (int) outProperty;
268 public bool IsStyleAvailable (FontStyle style)
272 int styleCheck = GetStyleCheck (style);
273 status = GDIPlus.GdipIsStyleAvailable (nativeFontFamily, styleCheck, out outProperty);
274 GDIPlus.CheckStatus (status);
279 public void Dispose ()
283 Status status = GDIPlus.GdipDeleteFontFamily (nativeFontFamily);
284 if ( status == Status.Ok )
285 nativeFontFamily = IntPtr.Zero;
290 public override bool Equals(object obj)
292 if (!(obj is FontFamily))
295 return (this == (FontFamily) obj);
298 public override int GetHashCode ()
300 return name.GetHashCode ();
304 public FontFamily[] Families
308 return GetFamilies (null);
312 public static FontFamily[] GetFamilies (Graphics graphics)
314 InstalledFontCollection fntcol = new InstalledFontCollection ();
315 return fntcol.Families;
318 [MonoTODO ("We only support the default system language")]
319 public string GetName (int language)
324 public override string ToString ()
326 return "FontFamily :" + name;