2 // System.Drawing.FontFamily.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Alexandre Pigolkine (pigolkine@gmx.de)
7 // Peter Dennis Bartok (pbartok@novell.com)
9 // Copyright (C) 2002/2004 Ximian, Inc http://www.ximian.com
10 // Copyright (C) 2004 - 2006 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;
42 //static private FontFamily genericSansSerif;
43 //static private FontFamily genericSerif;
45 private IntPtr nativeFontFamily
= IntPtr
.Zero
;
47 internal FontFamily(IntPtr fntfamily
)
49 nativeFontFamily
= fntfamily
;
52 internal void refreshName()
56 if (nativeFontFamily
== IntPtr
.Zero
)
59 sb
= new StringBuilder (GDIPlus
.FACESIZE
);
60 Status status
= GDIPlus
.GdipGetFamilyName (nativeFontFamily
, sb
, 0);
61 GDIPlus
.CheckStatus (status
);
70 internal IntPtr NativeObject
74 return nativeFontFamily
;
78 public FontFamily (GenericFontFamilies genericFamily
)
81 switch (genericFamily
) {
82 case GenericFontFamilies
.SansSerif
:
83 status
= GDIPlus
.GdipGetGenericFontFamilySansSerif (out nativeFontFamily
);
85 case GenericFontFamilies
.Serif
:
86 status
= GDIPlus
.GdipGetGenericFontFamilySerif (out nativeFontFamily
);
88 case GenericFontFamilies
.Monospace
:
89 default: // Undocumented default
90 status
= GDIPlus
.GdipGetGenericFontFamilyMonospace (out nativeFontFamily
);
93 GDIPlus
.CheckStatus (status
);
96 public FontFamily(string name
) : this (name
, null)
100 public FontFamily (string name
, FontCollection fontCollection
)
102 IntPtr handle
= (fontCollection
== null) ? IntPtr
.Zero
: fontCollection
.nativeFontCollection
;
103 Status status
= GDIPlus
.GdipCreateFontFamilyFromName (name
, handle
, out nativeFontFamily
);
104 GDIPlus
.CheckStatus (status
);
109 if (nativeFontFamily
== IntPtr
.Zero
)
110 throw new ArgumentException ("Name", Locale
.GetText ("Object was disposed."));
117 public static FontFamily GenericMonospace
{
118 get { return new FontFamily (GenericFontFamilies.Monospace); }
121 public static FontFamily GenericSansSerif
{
122 get { return new FontFamily (GenericFontFamilies.SansSerif); }
125 public static FontFamily GenericSerif
{
126 get { return new FontFamily (GenericFontFamilies.Serif); }
129 public int GetCellAscent (FontStyle style
)
132 Status status
= GDIPlus
.GdipGetCellAscent (nativeFontFamily
, (int)style
, out outProperty
);
133 GDIPlus
.CheckStatus (status
);
135 return (int) outProperty
;
138 public int GetCellDescent (FontStyle style
)
141 Status status
= GDIPlus
.GdipGetCellDescent (nativeFontFamily
, (int)style
, out outProperty
);
142 GDIPlus
.CheckStatus (status
);
144 return (int) outProperty
;
147 public int GetEmHeight (FontStyle style
)
150 Status status
= GDIPlus
.GdipGetEmHeight (nativeFontFamily
, (int)style
, out outProperty
);
151 GDIPlus
.CheckStatus (status
);
153 return (int) outProperty
;
156 public int GetLineSpacing (FontStyle style
)
159 Status status
= GDIPlus
.GdipGetLineSpacing (nativeFontFamily
, (int)style
, out outProperty
);
160 GDIPlus
.CheckStatus (status
);
162 return (int) outProperty
;
165 [MonoDocumentationNote ("When used with libgdiplus this method always return true (styles are created on demand).")]
166 public bool IsStyleAvailable (FontStyle style
)
169 Status status
= GDIPlus
.GdipIsStyleAvailable (nativeFontFamily
, (int)style
, out outProperty
);
170 GDIPlus
.CheckStatus (status
);
175 public void Dispose ()
177 if (nativeFontFamily
!= IntPtr
.Zero
) {
178 Status status
= GDIPlus
.GdipDeleteFontFamily (nativeFontFamily
);
179 nativeFontFamily
= IntPtr
.Zero
;
180 GC
.SuppressFinalize (this);
181 // check the status code (throw) at the last step
182 GDIPlus
.CheckStatus (status
);
186 public override bool Equals (object obj
)
188 FontFamily o
= (obj
as FontFamily
);
192 return (Name
== o
.Name
);
195 public override int GetHashCode ()
197 return Name
.GetHashCode ();
201 public static FontFamily
[] Families
{
202 get { return new InstalledFontCollection ().Families; }
205 public static FontFamily
[] GetFamilies (Graphics graphics
)
207 if (graphics
== null)
208 throw new ArgumentNullException ("graphics");
210 InstalledFontCollection fntcol
= new InstalledFontCollection ();
211 return fntcol
.Families
;
214 [MonoLimitation ("The language parameter is ignored. We always return the name using the default system language.")]
215 public string GetName (int language
)
220 public override string ToString ()
222 return String
.Concat ("[FontFamily: Name=", Name
, "]");