2 // System.Drawing.FontFamily.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Alexandre Pigolkine (pigolkine@gmx.de)
8 // Copyright (C) 2002/2004 Ximian, Inc http://www.ximian.com
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using System.Drawing.Text;
34 using System.Runtime.InteropServices;
36 namespace System.Drawing {
38 public sealed class FontFamily : MarshalByRefObject, IDisposable
41 static private FontFamily genericMonospace = null;
42 static private FontFamily genericSansSerif = null;
43 static private FontFamily genericSerif = null;
45 internal IntPtr nativeFontFamily = IntPtr.Zero;
47 internal FontFamily(IntPtr fntfamily)
49 nativeFontFamily = fntfamily;
53 internal void refreshName()
55 if (nativeFontFamily != IntPtr.Zero) {
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();
64 //Need to come back here, is Arial the right thing to do
65 internal FontFamily() : this ("Arial", null)
76 internal IntPtr NativeObject
80 return nativeFontFamily;
84 nativeFontFamily = value;
88 public FontFamily(GenericFontFamilies genericFamily)
91 switch (genericFamily)
93 case GenericFontFamilies.Monospace:
94 status = GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
95 GDIPlus.CheckStatus (status);
98 case GenericFontFamilies.SansSerif:
99 status = GDIPlus.GdipGetGenericFontFamilySansSerif (out nativeFontFamily);
100 GDIPlus.CheckStatus (status);
103 case GenericFontFamilies.Serif:
104 status = GDIPlus.GdipGetGenericFontFamilySerif (out nativeFontFamily);
105 GDIPlus.CheckStatus (status);
108 default: // Undocumented default
109 status = GDIPlus.GdipGetGenericFontFamilyMonospace (out nativeFontFamily);
110 GDIPlus.CheckStatus (status);
116 public FontFamily(string familyName) : this (familyName, null)
120 public FontFamily (string familyName, FontCollection collection)
123 if ( collection != null )
124 status = GDIPlus.GdipCreateFontFamilyFromName (familyName, collection.nativeFontCollection, out nativeFontFamily);
126 status = GDIPlus.GdipCreateFontFamilyFromName (familyName, IntPtr.Zero, out nativeFontFamily);
127 GDIPlus.CheckStatus (status);
140 public static FontFamily GenericMonospace
145 IntPtr generic = IntPtr.Zero;
146 Status status = GDIPlus.GdipGetGenericFontFamilyMonospace (out generic);
147 GDIPlus.CheckStatus (status);
148 FontFamily genericMonospace = new FontFamily (generic);
149 genericMonospace.refreshName ();
150 return genericMonospace;
154 public static FontFamily GenericSansSerif
158 IntPtr generic = IntPtr.Zero;
159 Status status = GDIPlus.GdipGetGenericFontFamilySansSerif (out generic);
160 GDIPlus.CheckStatus (status);
161 FontFamily genericSansSerif = new FontFamily (generic);
162 genericSansSerif.refreshName ();
163 return genericSansSerif;
167 public static FontFamily GenericSerif
171 IntPtr generic = IntPtr.Zero;
172 Status status = GDIPlus.GdipGetGenericFontFamilySerif (out generic);
173 GDIPlus.CheckStatus (status);
174 FontFamily genericSerif = new FontFamily (generic);
175 genericSerif.refreshName ();
181 //Need to check how to get the Flags attribute to read
182 //bitwise value of the enumeration
183 internal int GetStyleCheck(FontStyle style)
190 case FontStyle.Italic:
193 case FontStyle.Regular:
196 case FontStyle.Strikeout:
199 case FontStyle.Underline:
206 public int GetCellAscent (FontStyle style)
210 int styleCheck = GetStyleCheck (style);
211 status = GDIPlus.GdipGetCellAscent (nativeFontFamily, styleCheck, out outProperty);
212 GDIPlus.CheckStatus (status);
214 return (int) outProperty;
217 public int GetCellDescent (FontStyle style)
221 int styleCheck = GetStyleCheck (style);
222 status = GDIPlus.GdipGetCellDescent (nativeFontFamily, styleCheck, out outProperty);
223 GDIPlus.CheckStatus (status);
225 return (int) outProperty;
228 public int GetEmHeight (FontStyle style)
232 int styleCheck = GetStyleCheck (style);
233 status = GDIPlus.GdipGetEmHeight (nativeFontFamily, styleCheck, out outProperty);
234 GDIPlus.CheckStatus (status);
236 return (int) outProperty;
239 public int GetLineSpacing (FontStyle style)
243 int styleCheck = GetStyleCheck (style);
244 status = GDIPlus.GdipGetLineSpacing (nativeFontFamily, styleCheck, out outProperty);
245 GDIPlus.CheckStatus (status);
247 return (int) outProperty;
250 public bool IsStyleAvailable (FontStyle style)
254 int styleCheck = GetStyleCheck (style);
255 status = GDIPlus.GdipIsStyleAvailable (nativeFontFamily, styleCheck, out outProperty);
256 GDIPlus.CheckStatus (status);
261 public void Dispose ()
263 Status status = GDIPlus.GdipDeleteFontFamily (nativeFontFamily);
264 if ( status == Status.Ok )
265 nativeFontFamily = IntPtr.Zero;
268 public override bool Equals(object obj)
270 if (!(obj is FontFamily))
273 FontFamily o = (FontFamily) obj;
274 return (nativeFontFamily == o.nativeFontFamily);
277 public override int GetHashCode ()
279 return name.GetHashCode ();
283 public static FontFamily[] Families
287 return GetFamilies (null);
291 public static FontFamily[] GetFamilies (Graphics graphics)
293 InstalledFontCollection fntcol = new InstalledFontCollection ();
294 return fntcol.Families;
297 [MonoTODO ("We only support the default system language")]
298 public string GetName (int language)
303 public override string ToString ()
305 return "FontFamily :" + name;