- Implemented Font.ToHfont method
[mono.git] / mcs / class / System.Drawing / System.Drawing / Font.cs
1 //
2 // System.Drawing.Fonts.cs
3 //
4 // (C) 2004 Ximian, Inc.  http://www.ximian.com
5 //
6 // Authors: 
7
8
9 using System.Runtime.Serialization;
10 using System.Runtime.InteropServices;
11 using System.ComponentModel;
12
13 namespace System.Drawing {
14
15         [Serializable]
16         [ComVisible (true)]
17         [Editor ("System.Drawing.Design.FontEditor, " + Consts.AssemblySystem_Drawing_Design, typeof (System.Drawing.Design.UITypeEditor))]
18         [TypeConverter(typeof(FontConverter))]
19         public sealed class Font : MarshalByRefObject, ISerializable, ICloneable, IDisposable
20         {
21                 IntPtr  fontObject = IntPtr.Zero;
22                 
23         private Font (SerializationInfo info, StreamingContext context)
24                 {
25                 }
26
27                 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
28                 {
29                 }
30                 
31                 ~Font()
32                 {       
33                         Dispose ();
34                 }
35
36                 public void Dispose ()
37                 {
38                         if (fontObject!=IntPtr.Zero){                           
39                                 GDIPlus.GdipDeleteFont(fontObject);                     
40                                 GC.SuppressFinalize(this);
41                         }
42                 }               
43                 
44                 internal void unitConversion(GraphicsUnit fromUnit, GraphicsUnit toUnit, float nSrc, out float nTrg)
45                 {
46                         float inchs = 0;
47                         nTrg = 0;               
48                         
49                         switch (fromUnit) {
50                         case GraphicsUnit.Display:
51                                 inchs = nSrc / 75f;
52                                 break;
53                         case GraphicsUnit.Document:
54                                 inchs = nSrc / 300f;
55                                 break;
56                         case GraphicsUnit.Inch:
57                                 inchs = nSrc;
58                                 break;
59                         case GraphicsUnit.Millimeter:
60                                 inchs = nSrc / 25.4f;
61                                 break;
62                         case GraphicsUnit.Pixel:                                
63                         case GraphicsUnit.World:
64                                 inchs = nSrc / Graphics.systemDpiX;
65                                 break;
66                         case GraphicsUnit.Point:
67                                 inchs = nSrc / 72f;
68                                 break;                          
69                         default:                
70                                 throw new ArgumentException("Invalid GraphicsUnit");                            
71                         }                       
72                         
73                         switch (toUnit) {
74                         case GraphicsUnit.Display:
75                                 nTrg = inchs * 75;
76                                 break;
77                         case GraphicsUnit.Document:
78                                 nTrg = inchs * 300;
79                                 break;
80                         case GraphicsUnit.Inch:
81                                 nTrg = inchs;
82                                 break;
83                         case GraphicsUnit.Millimeter:
84                                 nTrg = inchs * 25.4f;
85                                 break;
86                         case GraphicsUnit.Pixel:                                
87                         case GraphicsUnit.World:
88                                 nTrg = inchs * Graphics.systemDpiX;
89                                 break;
90                         case GraphicsUnit.Point:
91                                 nTrg = inchs * 72;
92                                 break;
93                         default:        
94                                 throw new ArgumentException("Invalid GraphicsUnit");                            
95                         }                               
96                 }
97                 
98                 internal void setProperties(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical)                  
99                 {                       
100                         _name=family.Name;
101                         _fontFamily = family;
102                         _size = emSize;                 
103                         _unit = unit;
104                         _style = style;
105                         _gdiCharSet = charSet;
106                         _gdiVerticalFont = isVertical;
107                         
108                         unitConversion(unit, GraphicsUnit.Point, emSize, out  _sizeInPoints);
109                                                 
110                         _bold = _italic = _strikeout = _underline = false;
111
112                         if ((style & FontStyle.Bold) == FontStyle.Bold)
113                                 _bold = true;
114                                 
115                         if ((style & FontStyle.Italic) == FontStyle.Italic)
116                                _italic = true;
117
118                         if ((style & FontStyle.Strikeout) == FontStyle.Strikeout)
119                                 _strikeout = true;
120
121                         if ((style & FontStyle.Underline) == FontStyle.Underline)
122                                 _underline = true;                  
123                 }
124
125                 public static Font FromHfont(IntPtr font)
126                 {
127                         // FIXME: 
128                         Font result = new Font("Arial", (float)12.0, FontStyle.Regular);
129                         return result;
130                 }
131
132                 public IntPtr ToHfont ()
133                 {
134                         IntPtr                  Hfont;
135                         System.OperatingSystem  osInfo = System.Environment.OSVersion;
136
137                         // Sanity. Should we throw an exception?
138                         if (fontObject==IntPtr.Zero){                           
139                                 return(IntPtr.Zero);
140                         }
141
142                         if ((int)osInfo.Platform==128) {
143                                 // If we're on Unix we use our private gdiplus API
144                                 GDIPlus.GdipGetHfont(fontObject, out Hfont);
145                         } else {
146                                 // This needs testing, but I don't have a working win32 mono
147                                 // environment. 
148                                 LOGFONTA        lf       = new LOGFONTA();
149
150                                 GDIPlus.GdipGetLogFontA(fontObject, IntPtr.Zero, ref lf);
151                                 Hfont=GDIPlus.CreateFontIndirectA(ref lf);
152                         }
153                         return(Hfont);
154                 }
155
156                 public Font(Font original, FontStyle style)
157                 {
158                         setProperties(original.FontFamily, original.Size, style, original.Unit, 
159                                 original.GdiCharSet, original.GdiVerticalFont);
160                         
161                         GDIPlus.GdipCreateFont(_fontFamily.NativeObject,        Size,  Style,   Unit,  out fontObject);                                 
162                 }
163
164                 public Font(FontFamily family, float emSize)
165                         : this(family, emSize, FontStyle.Regular, GraphicsUnit.Point, (byte)0, false)
166                 {
167                 }
168
169                 public Font(FontFamily family, float emSize, FontStyle style)
170                         : this(family, emSize, style, GraphicsUnit.Point, (byte)0, false)
171                 {
172                 }
173
174                 public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit)
175                         : this(family, emSize, style, unit, (byte)0, false)
176                 {
177                 }
178
179                 public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte charSet)
180                         : this(family, emSize, style, unit, charSet, false)
181                 {
182                 }
183                 
184                 public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical)
185                 {       
186                         setProperties(family, emSize, style, unit, charSet, isVertical);                
187                         GDIPlus.GdipCreateFont(family.NativeObject,     emSize,  style,   unit,  out fontObject);                                       
188                 }
189
190                 public Font(string familyName, float emSize)
191                         : this(familyName, emSize, FontStyle.Regular, GraphicsUnit.Point, (byte)0, false)
192                 {
193                 }
194
195                 public Font(string familyName, float emSize, FontStyle style)
196                         : this(familyName, emSize, style, GraphicsUnit.Point, (byte)0, false)
197                 {
198                 }
199                 
200                 public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit)
201                         : this(familyName, emSize, style, unit, (byte)0, false)
202                 {
203                 }
204                 
205                 public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte charSet)
206                         : this(familyName, emSize, style, unit, charSet, false)
207                 {
208                 }
209                 
210                 public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical)                  
211                 {
212                         FontFamily family = new FontFamily(familyName);                 
213                         setProperties(family, emSize, style, unit, charSet, isVertical);
214                         
215                         GDIPlus.GdipCreateFont(family.NativeObject,     emSize,  style,   unit,  out fontObject);                                       
216                 }               
217                 
218                 public object Clone()
219                 {
220                         return new Font(this, Style);
221                 }
222                 
223                 internal IntPtr NativeObject{            
224                         get{
225                                         return fontObject;
226                         }
227                         set     {
228                                         fontObject = value;
229                         }
230                 }
231                 
232                 private bool _bold;
233                 public bool Bold {
234                         get {
235                                 return _bold;
236                         }
237                 }
238                 
239                 private FontFamily _fontFamily;
240                 public FontFamily FontFamily {
241                         get {
242                                 return _fontFamily;
243                         }
244                 }
245                 
246                 private byte _gdiCharSet;
247                 public byte GdiCharSet {
248                         get {
249                                 return _gdiCharSet;
250                         }
251                 }
252                 
253                 private bool _gdiVerticalFont;
254                 public bool GdiVerticalFont {
255                         get {
256                                 return _gdiVerticalFont;
257                         }
258                 }
259                 
260                 private int _height;
261                 public int Height {
262                         get {
263                                 return _height;
264                         }
265                 }
266
267                 private bool _italic;
268                 public bool Italic {
269                         get {
270                                 return _italic;
271                         }
272                 }
273
274                 private string _name;
275                 public string Name {
276                         get {
277                                 return _name;
278                         }
279                 }
280
281                 private float _size;
282                 public float Size {
283                         get {
284                                 return _size;
285                         }
286                 }
287
288                 private float _sizeInPoints;
289                 public float SizeInPoints {
290                         get {
291                                 return _sizeInPoints;
292                         }
293                 }
294
295                 private bool _strikeout;
296                 public bool Strikeout {
297                         get {
298                                 return _strikeout;
299                         }
300                 }
301                 
302                 private FontStyle _style;
303                 public FontStyle Style {
304                         get {
305                                 return _style;
306                         }
307                 }
308
309                 private bool _underline;
310                 public bool Underline {
311                         get {
312                                 return _underline;
313                         }
314                 }
315
316                 private GraphicsUnit _unit;
317                 public GraphicsUnit Unit {
318                         get {
319                                 return _unit;
320                         }
321                 }
322                 
323                 public override System.String ToString()
324                 {
325                         return ("[Font: Name="+ _name +", Size="+ _size+", Style="+ _style  +", Units="+ _unit  +", GdiCharSet="+ _gdiCharSet
326                                 +", GdiVerticalFont="+ _gdiVerticalFont + "]");                 
327                 }
328         }
329 }