Mark tests as not working under TARGET_JVM
[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         [Serializable]
10         public sealed class Font: MarshalByRefObject, ISerializable, ICloneable, IDisposable {
11
12                 #region variables
13
14                 const byte DEFAULT_CHARSET = 1;
15
16                 private readonly GraphicsUnit _gUnit = GraphicsUnit.Point;
17                 private readonly FontFamily _fontFamily;
18                 private readonly awt.Font _jFont;
19                 private readonly byte _charset;
20
21 #if NET_2_0
22                 private readonly string _systemFontName;
23 #endif
24
25                 #endregion
26
27                 internal awt.Font NativeObject {
28                         get {
29                                 return _jFont;
30                         }
31                 }
32
33                 #region ISerializable
34
35                 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
36                         info.AddValue("Name", Name);
37                         info.AddValue("Size", Size);
38                         info.AddValue("Style", Style, typeof(FontStyle));
39                         info.AddValue("Unit", Unit, typeof(GraphicsUnit));
40                 }
41
42                 #endregion
43
44                 #region ctors
45
46                 private Font (SerializationInfo info, StreamingContext context)
47                         : this(
48                         info.GetString("Name"), 
49                         info.GetSingle("Size"), 
50                         (FontStyle)info.GetValue("Style", typeof(FontStyle)), 
51                         (GraphicsUnit)info.GetValue("Unit", typeof(GraphicsUnit)) ) {
52                 }
53
54                 public Font(Font original, FontStyle style) {
55                         _jFont = original.NativeObject.deriveFont( DeriveStyle(original.NativeObject.getAttributes(), style, true) );
56                         _gUnit = original._gUnit;
57                         _fontFamily = original._fontFamily;
58                         _charset = original._charset;
59                 }
60
61                 public Font(FontFamily family, float emSize)
62                         : this(family, emSize, FontStyle.Regular, GraphicsUnit.Point, DEFAULT_CHARSET, false) {
63                 }
64
65                 public Font(FontFamily family, float emSize, FontStyle style)
66                         : this(family, emSize, style, GraphicsUnit.Point, DEFAULT_CHARSET, false) {
67                 }
68                 public Font(FontFamily family, float emSize, GraphicsUnit unit)
69                         : this(family, emSize, FontStyle.Regular, unit, DEFAULT_CHARSET, false) {
70                 }
71
72                 public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit)
73                         : this(family, emSize, style, unit, DEFAULT_CHARSET, false) {
74                 }
75
76                 public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte charSet)
77                         : this(family, emSize, style, unit, charSet, false) {
78                 }
79                 
80                 public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical) {
81                         if (family == null)
82                                 throw new ArgumentNullException("family");
83
84                         _gUnit = unit;
85                         _fontFamily = family;
86                         _charset = charSet;
87
88                         java.util.Hashtable attribs = new java.util.Hashtable();
89                         attribs.put(TextAttribute.FAMILY, family.Name/*TODO: family doungrade possibility*/);
90                         //init defaults
91                         attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
92
93                         float newSize = emSize * Graphics.UnitConversion[ (int)_gUnit ];
94                         attribs.put(TextAttribute.SIZE, new java.lang.Float(newSize));
95
96                         DeriveStyle(attribs, style, false);
97
98                         _jFont = family.FamilyFont.deriveFont(attribs);
99                 }
100
101                 public Font(string familyName, float emSize)
102                         : this(familyName, emSize, FontStyle.Regular, GraphicsUnit.Point, (byte)0, false) {
103                 }
104
105                 public Font(string familyName, float emSize, FontStyle style)
106                         : this(familyName, emSize, style, GraphicsUnit.Point, (byte)0, false) {
107                 }
108
109                 public Font(string familyName, float emSize, GraphicsUnit unit)
110                         : this(familyName, emSize, FontStyle.Regular, unit, (byte)0, false) {
111                 }
112                 
113                 public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit)
114                         : this(familyName, emSize, style, unit, (byte)0, false) {
115                 }
116                 
117                 public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte charSet)
118                         : this(familyName, emSize, style, unit, charSet, false) {
119                 }
120                 
121                 public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical)
122                         : this (GetFontFamily (familyName), emSize, style, unit, charSet, isVertical) {
123                 }
124
125 #if NET_2_0
126                 internal Font (string familyName, float emSize, string systemName)
127                         : this (familyName, emSize) {
128                         _systemFontName = systemName;
129                 }
130 #endif
131
132                 static FontFamily GetFontFamily (string familyName) {
133 #if ONLY_1_1
134                         if (familyName == null)
135                                 throw new ArgumentNullException ("familyName");
136 #endif
137                         // NOTE: If family name is null, empty or invalid,
138                         // MS creates Microsoft Sans Serif font.
139                         try {
140                                 return new FontFamily (familyName);
141                         }
142                         catch {
143                                 return FontFamily.GenericSansSerif;
144                         }
145                 }
146
147                 #endregion
148                 
149                 #region IDisposable members
150
151                 public void Dispose() {
152                 }
153
154                 #endregion
155
156                 #region ICloneable
157
158                 public object Clone() {
159                         return (Font)MemberwiseClone();
160                 }
161
162                 #endregion
163                 
164 #if INTPTR_SUPPORT
165                 [MonoTODO]
166                 public IntPtr ToHfont ()
167                 {
168                         throw new NotImplementedException();
169                 }
170 #endif
171                 
172                 #region public properties
173
174                 public bool Bold {
175                         get {
176                                 return _jFont.isBold();
177                         }
178                 }
179                 
180                 public FontFamily FontFamily {
181                         get {                           
182                                 return _fontFamily;
183                         }
184                 }
185                 
186                 public byte GdiCharSet {
187                         get {
188                                 return _charset;
189                         }
190                 }
191                 
192                 public bool GdiVerticalFont {
193                         get {
194                                 return Name.StartsWith("@");
195                         }
196                 }
197                 
198                 public int Height {
199                         get {
200                                 return FontFamily.Container.getFontMetrics(NativeObject).getHeight();
201                         }
202                 }
203
204                 public bool Italic {
205                         get {
206                                 return _jFont.isItalic();
207                         }
208                 }
209
210                 public string Name {
211                         get {
212                                 return _jFont.getName();
213                         }
214                 }
215
216                 public float Size {
217                         get {
218                                 return SizeInPoints / Graphics.UnitConversion[ (int)_gUnit ];
219                         }
220                 }
221                 
222                 public float SizeInPoints {
223                         get {
224                                 return _jFont.getSize2D();
225                         }
226                 }
227                 
228                 public bool Strikeout {
229                         get {
230                                 try {
231                                         if((java.lang.Boolean)_jFont.getAttributes().get(TextAttribute.STRIKETHROUGH) 
232                                                 == TextAttribute.STRIKETHROUGH_ON )
233                                                 return true;
234                                 }
235                                 catch {
236                                 }
237                                 return false;
238                         }
239                 }
240                 
241                 public FontStyle Style {
242                         get {
243                                 FontStyle style = FontStyle.Regular;
244                                 if (Bold)
245                                         style |= FontStyle.Bold;
246                                 if (Italic)
247                                         style |= FontStyle.Italic;
248                                 if (Underline)
249                                         style |= FontStyle.Underline;
250                                 if (Strikeout)
251                                         style |= FontStyle.Strikeout;
252
253                                 return style;
254                         }
255                 }
256                 
257                 public bool Underline {
258                         get {
259                                 try {
260                                         if((java.lang.Integer)_jFont.getAttributes().get(TextAttribute.UNDERLINE) 
261                                                 == TextAttribute.UNDERLINE_ON )
262                                                 return true;
263                                 }
264                                 catch {
265                                 }
266                                 return false;
267                         }
268                 }
269
270                 [TypeConverter(typeof(FontConverter.FontUnitConverter))]
271                 public GraphicsUnit Unit {
272                         get {
273                                 return _gUnit;
274                         }
275                 }
276
277 #if NET_2_0
278                 [Browsable (false)]
279                 public bool IsSystemFont {
280                         get {
281                                 return !string.IsNullOrEmpty (_systemFontName);
282                         }
283                 }
284
285                 [Browsable (false)]
286                 public string SystemFontName {
287                         get {
288                                 return _systemFontName;
289                         }
290                 }
291 #endif
292
293                 #endregion
294                 
295                 public override System.String ToString() {
296                         return ("[Font: Name="+ Name +", Size="+ Size +", Style="+ Style +", Units="+ Unit +"]");                       
297                 }
298
299                 static internal java.util.Map DeriveStyle(java.util.Map attribs, FontStyle style, bool createNew) {
300                         java.util.Map newAttribs;
301                         if (createNew) {
302                                 newAttribs = new java.util.Hashtable( attribs.size() );
303                                 object [] keys = attribs.keySet().toArray();
304                                 for (int i=0; i < keys.Length; i++)
305                                         newAttribs.put( keys[i], attribs.get( keys[i] ) );
306                         }
307                         else
308                                 newAttribs = attribs;
309
310                         //Bold
311                         if((style & FontStyle.Bold) == FontStyle.Bold)
312                                 newAttribs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
313                         else
314                                 newAttribs.remove(TextAttribute.WEIGHT);
315
316                         //Italic
317                         if((style & FontStyle.Italic) == FontStyle.Italic)
318                                 newAttribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
319                         else
320                                 newAttribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
321
322                         //Underline
323                         if((style & FontStyle.Underline) == FontStyle.Underline)
324                                 newAttribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
325                         else
326                                 newAttribs.remove(TextAttribute.UNDERLINE);
327
328                         //Strikeout
329                         if((style & FontStyle.Strikeout) == FontStyle.Strikeout)
330                                 newAttribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
331                         else
332                                 newAttribs.remove(TextAttribute.STRIKETHROUGH);
333
334                         return newAttribs;
335                 }
336         }
337 }