Brush.jvm.cs: fixed transform methods, createContext
[mono.git] / mcs / class / System.Drawing / System.Drawing / Font.jvm.cs
1
2 using System.Runtime.Serialization;
3 using System.Runtime.InteropServices;
4 using System.ComponentModel;
5 using awt = java.awt;
6 using TextAttribute = java.awt.font.TextAttribute;
7
8 namespace System.Drawing {
9
10         public sealed class Font: IDisposable   
11         {
12                 const int DPI = 72; 
13                 private GraphicsUnit gUnit = GraphicsUnit.Point;
14                 awt.Font _jFont;
15
16                 internal awt.Font NativeObject {
17                         get {
18                                 return _jFont;
19                         }
20                 }
21
22                 public void Dispose()
23                 {
24
25                 }
26
27         private Font (SerializationInfo info, StreamingContext context)
28                 {
29                         
30                 }
31                 
32
33                 // FIXME: add this method when/if there will be resources needed to be disposed
34 //              ~Font()
35 //              {       
36 //                      
37 //              }
38
39                 internal float unitConversion(GraphicsUnit fromUnit, GraphicsUnit toUnit, float nSrc)
40                 {
41                         double inchs = 0;
42                         double nTrg = 0;                
43                         
44                         switch (fromUnit) {
45                         case GraphicsUnit.Display:
46                                 inchs = nSrc / 75f;
47                                 break;
48                         case GraphicsUnit.Document:
49                                 inchs = nSrc / 300f;
50                                 break;
51                         case GraphicsUnit.Inch:
52                                 inchs = nSrc;
53                                 break;
54                         case GraphicsUnit.Millimeter:
55                                 inchs = nSrc / 25.4f;
56                                 break;
57                         case GraphicsUnit.Pixel:                                
58                         case GraphicsUnit.World:
59                                 inchs = nSrc / Graphics.DefaultScreenResolution;
60                                 break;
61                         case GraphicsUnit.Point:
62                                 inchs = nSrc / 72f;
63                                 break;                          
64                         default:                
65                                 throw new ArgumentException("Invalid GraphicsUnit");                            
66                         }                       
67                         
68                         switch (toUnit) {
69                         case GraphicsUnit.Display:
70                                 nTrg = inchs * 75;
71                                 break;
72                         case GraphicsUnit.Document:
73                                 nTrg = inchs * 300;
74                                 break;
75                         case GraphicsUnit.Inch:
76                                 nTrg = inchs;
77                                 break;
78                         case GraphicsUnit.Millimeter:
79                                 nTrg = inchs * 25.4f;
80                                 break;
81                         case GraphicsUnit.Pixel:                                
82                         case GraphicsUnit.World:
83                                 nTrg = inchs * Graphics.DefaultScreenResolution;
84                                 break;
85                         case GraphicsUnit.Point:
86                                 nTrg = inchs * 72;
87                                 break;
88                         default:        
89                                 throw new ArgumentException("Invalid GraphicsUnit");                            
90                         }
91                         return (float)nTrg;     
92                 }
93
94 #if INTPTR_SUPPORT
95                 public IntPtr ToHfont ()
96                 {
97                         throw new NotImplementedException();
98                 }
99 #endif
100
101                 public Font(Font original, FontStyle style)
102                 {
103                         _jFont = original.NativeObject.deriveFont((int)style);
104                 }
105
106                 public Font(FontFamily family, float emSize)
107                         : this(family, emSize, FontStyle.Regular, GraphicsUnit.Point, (byte)0, false)
108                 {
109                 }
110
111                 public Font(FontFamily family, float emSize, FontStyle style)
112                         : this(family, emSize, style, GraphicsUnit.Point, (byte)0, false)
113                 {
114                 }
115                 public Font(FontFamily family, float emSize, GraphicsUnit unit)
116                         : this(family, emSize, FontStyle.Regular, unit, (byte)0, false)
117                 {
118                 }
119
120                 public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit)
121                         : this(family, emSize, style, unit, (byte)0, false)
122                 {
123                 }
124
125                 public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte charSet)
126                         : this(family, emSize, style, unit, charSet, false)
127                 {
128                 }
129                 
130                 public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical)
131                         :this(family.Name,emSize,style,unit,charSet,isVertical)
132                 {
133                 }
134
135                 public Font(string familyName, float emSize)
136                         : this(familyName, emSize, FontStyle.Regular, GraphicsUnit.Point, (byte)0, false)
137                 {
138                 }
139
140                 public Font(string familyName, float emSize, FontStyle style)
141                         : this(familyName, emSize, style, GraphicsUnit.Point, (byte)0, false)
142                 {
143                 }
144
145                 public Font(string familyName, float emSize, GraphicsUnit unit)
146                         : this(familyName, emSize, FontStyle.Regular, unit, (byte)0, false)
147                 {
148                 }
149                 
150                 public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit)
151                         : this(familyName, emSize, style, unit, (byte)0, false)
152                 {
153                 }
154                 
155                 public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte charSet)
156                         : this(familyName, emSize, style, unit, charSet, false)
157                 {
158                 }
159                 
160                 public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical)                  
161                 {
162                         //TODO: charset management
163                         gUnit = unit;
164                         java.util.Hashtable attribs = new java.util.Hashtable();
165                         attribs.put(TextAttribute.FAMILY, familyName/*TODO: family doungrade possibility*/);
166                         //init defaults
167                         attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
168
169                         if((style & FontStyle.Bold) != FontStyle.Regular)
170                                 attribs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
171                         if((style & FontStyle.Italic) != FontStyle.Regular)
172                                 attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
173                         if((style & FontStyle.Underline) != FontStyle.Regular)
174                                 attribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
175                         if((style & FontStyle.Strikeout) != FontStyle.Regular)
176                                 attribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
177
178                         float newSize = unitConversion(gUnit,GraphicsUnit.World,emSize);
179                         attribs.put(TextAttribute.SIZE,new java.lang.Float(newSize));
180
181                         #region OldStyleSwitch
182                         //                      switch(style)
183                         //                      {
184                         //                              case 0: // '\0'
185                         //                                      attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
186                         //                                      break;
187                         //
188                         //                              case 1: // '\001'
189                         //                                      attribs.put(TextAttribute.WEIGattribs, TextAttribute.WEIGattribs_BOLD);
190                         //                                      break;
191                         //
192                         //                              case 2: // '\002'
193                         //                                      attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
194                         //                                      break;
195                         //
196                         //                              case 3: // '\003'
197                         //                                      attribs.put(TextAttribute.WEIGattribs, TextAttribute.WEIGattribs_BOLD);
198                         //                                      attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
199                         //                                      break;
200                         //
201                         //                              case 4: // '\004'
202                         //                                      attribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
203                         //                                      break;
204                         //
205                         //                              case 5: // '\005'
206                         //                                      attribs.put(TextAttribute.WEIGattribs, TextAttribute.WEIGattribs_BOLD);
207                         //                                      attribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
208                         //                                      break;
209                         //
210                         //                              case 6: // '\006'
211                         //                                      attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
212                         //                                      attribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
213                         //                                      break;
214                         //
215                         //                              case 7: // '\007'
216                         //                                      attribs.put(TextAttribute.WEIGattribs, TextAttribute.WEIGattribs_BOLD);
217                         //                                      attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
218                         //                                      attribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
219                         //                                      break;
220                         //
221                         //                              case 8: // '\b'
222                         //                                      attribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
223                         //                                      break;
224                         //
225                         //                              case 9: // '\t'
226                         //                                      attribs.put(TextAttribute.WEIGattribs, TextAttribute.WEIGattribs_BOLD);
227                         //                                      attribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
228                         //                                      break;
229                         //
230                         //                              case 10: // '\n'
231                         //                                      attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
232                         //                                      attribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
233                         //                                      break;
234                         //
235                         //                              case 11: // '\013'
236                         //                                      attribs.put(TextAttribute.WEIGattribs, TextAttribute.WEIGattribs_BOLD);
237                         //                                      attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
238                         //                                      attribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
239                         //                                      break;
240                         //
241                         //                              case 12: // '\f'
242                         //                                      attribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
243                         //                                      attribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
244                         //                                      break;
245                         //
246                         //                              case 13: // '\r'
247                         //                                      attribs.put(TextAttribute.WEIGattribs, TextAttribute.WEIGattribs_BOLD);
248                         //                                      attribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
249                         //                                      attribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
250                         //                                      break;
251                         //
252                         //                              case 14: // '\016'
253                         //                                      attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
254                         //                                      attribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
255                         //                                      attribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
256                         //                                      break;
257                         //
258                         //                              case 15: // '\017'
259                         //                                      attribs.put(TextAttribute.WEIGattribs, TextAttribute.WEIGattribs_BOLD);
260                         //                                      attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
261                         //                                      attribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
262                         //                                      attribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
263                         //                                      break;
264                         //
265                         //                              default:
266                         //                                      attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
267                         //                                      break;
268                         //                      }
269                         #endregion
270                         //TODO: units conversion
271                         try
272                         {
273                                 _jFont = new awt.Font(attribs);
274                         }
275                         catch (Exception e)
276                         {
277 #if DEBUG
278                                 string mess = e.ToString();
279                                 Console.WriteLine(mess);
280 #endif
281                                 throw e;
282                         }
283                 }               
284                 
285                 public object Clone()
286                 {
287                         return new Font(this, Style);
288                 }
289                 
290                 
291                 
292                 public bool Bold {
293                         get {
294                                 return _jFont.isBold();
295                         }
296                 }
297                 
298                 
299                 public FontFamily FontFamily {
300                         get {                           
301                                 return new FontFamily(_jFont.getFamily());
302                         }
303                 }
304                 
305                 public byte GdiCharSet {
306                         get {
307                                 throw new NotSupportedException();
308                         }
309                 }
310                 
311                 public bool GdiVerticalFont {
312                         get {
313                                 throw new NotSupportedException();
314                         }
315                 }
316                 
317                 public int Height {
318                         get {
319                                 awt.Container c = new awt.Container();
320                                 return c.getFontMetrics(NativeObject).getHeight();
321                         }
322                 }
323
324                 public bool Italic {
325                         get {
326                                 return _jFont.isItalic();
327                         }
328                 }
329
330                 public string Name {
331                         get {
332                                 return _jFont.getName();
333                         }
334                 }
335
336                 public float Size {
337                         get {
338                                 return unitConversion(GraphicsUnit.World,gUnit,_jFont.getSize2D());
339                         }
340                 }
341
342                 
343                 public float SizeInPoints {
344                         get {
345                                 return unitConversion(GraphicsUnit.World,GraphicsUnit.Point,_jFont.getSize2D());
346                         }
347                 }
348
349                 
350                 public bool Strikeout {
351                         get {
352                                 try
353                                 {
354                                         if((java.lang.Boolean)_jFont.getAttributes().get(TextAttribute.STRIKETHROUGH) 
355                                                 == TextAttribute.STRIKETHROUGH_ON )
356                                                 return true;
357                                 }
358                                 catch
359                                 {
360                                 }
361                                 return false;
362                         }
363                 }
364                 
365                 public FontStyle Style {
366                         get {
367                                 FontStyle style = FontStyle.Regular;
368                                 if (Bold)
369                                         style |= FontStyle.Bold;
370                                 if (Italic)
371                                         style |= FontStyle.Italic;
372                                 if (Underline)
373                                         style |= FontStyle.Underline;
374                                 if (Strikeout)
375                                         style |= FontStyle.Strikeout;
376
377                                 return style;
378                         }
379                 }
380
381                 
382                 public bool Underline {
383                         get {
384                                 try
385                                 {
386                                         if((java.lang.Integer)_jFont.getAttributes().get(TextAttribute.UNDERLINE) 
387                                                 == TextAttribute.UNDERLINE_ON )
388                                                 return true;
389                                 }
390                                 catch
391                                 {
392                                 }
393                                 return false;
394                         }
395                 }
396
397                 [TypeConverter(typeof(FontConverter.FontUnitConverter))]
398                 public GraphicsUnit Unit {
399                         get {
400                                 return gUnit;
401                         }
402                 }
403                 
404                 public override System.String ToString()
405                 {
406                         return ("[Font: Name="+ Name +", Size="+ Size+", Style="+ Style  +", Units="+ Unit + "]");                      
407                 }
408         }
409 }