New test.
[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 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                 static FontFamily GetFontFamily (string familyName) {
126 #if ONLY_1_1
127                         if (familyName == null)
128                                 throw new ArgumentNullException ("familyName");
129 #endif
130                         // NOTE: If family name is null, empty or invalid,
131                         // MS creates Microsoft Sans Serif font.
132                         try {
133                                 return new FontFamily (familyName);
134                         }
135                         catch {
136                                 return FontFamily.GenericSansSerif;
137                         }
138                 }
139
140                 #endregion
141                 
142                 #region IDisposable members
143
144                 public void Dispose() {
145                 }
146
147                 #endregion
148
149                 #region ICloneable
150
151                 public object Clone() {
152                         return (Font)MemberwiseClone();
153                 }
154
155                 #endregion
156                 
157 #if INTPTR_SUPPORT
158                 [MonoTODO]
159                 public IntPtr ToHfont ()
160                 {
161                         throw new NotImplementedException();
162                 }
163 #endif
164                 
165                 #region public properties
166
167                 public bool Bold {
168                         get {
169                                 return _jFont.isBold();
170                         }
171                 }
172                 
173                 public FontFamily FontFamily {
174                         get {                           
175                                 return _fontFamily;
176                         }
177                 }
178                 
179                 public byte GdiCharSet {
180                         get {
181                                 return _charset;
182                         }
183                 }
184                 
185                 public bool GdiVerticalFont {
186                         get {
187                                 return Name.StartsWith("@");
188                         }
189                 }
190                 
191                 public int Height {
192                         get {
193                                 return FontFamily.Container.getFontMetrics(NativeObject).getHeight();
194                         }
195                 }
196
197                 public bool Italic {
198                         get {
199                                 return _jFont.isItalic();
200                         }
201                 }
202
203                 public string Name {
204                         get {
205                                 return _jFont.getName();
206                         }
207                 }
208
209                 public float Size {
210                         get {
211                                 return SizeInPoints / Graphics.UnitConversion[ (int)_gUnit ];
212                         }
213                 }
214                 
215                 public float SizeInPoints {
216                         get {
217                                 return _jFont.getSize2D();
218                         }
219                 }
220                 
221                 public bool Strikeout {
222                         get {
223                                 try {
224                                         if((java.lang.Boolean)_jFont.getAttributes().get(TextAttribute.STRIKETHROUGH) 
225                                                 == TextAttribute.STRIKETHROUGH_ON )
226                                                 return true;
227                                 }
228                                 catch {
229                                 }
230                                 return false;
231                         }
232                 }
233                 
234                 public FontStyle Style {
235                         get {
236                                 FontStyle style = FontStyle.Regular;
237                                 if (Bold)
238                                         style |= FontStyle.Bold;
239                                 if (Italic)
240                                         style |= FontStyle.Italic;
241                                 if (Underline)
242                                         style |= FontStyle.Underline;
243                                 if (Strikeout)
244                                         style |= FontStyle.Strikeout;
245
246                                 return style;
247                         }
248                 }
249                 
250                 public bool Underline {
251                         get {
252                                 try {
253                                         if((java.lang.Integer)_jFont.getAttributes().get(TextAttribute.UNDERLINE) 
254                                                 == TextAttribute.UNDERLINE_ON )
255                                                 return true;
256                                 }
257                                 catch {
258                                 }
259                                 return false;
260                         }
261                 }
262
263                 [TypeConverter(typeof(FontConverter.FontUnitConverter))]
264                 public GraphicsUnit Unit {
265                         get {
266                                 return _gUnit;
267                         }
268                 }
269
270 #if NET_2_0
271                 [Browsable (false)]
272                 public bool IsSystemFont {
273                         get {
274                                 if (systemFontName == null)
275                                         return false;
276
277                                 return StringComparer.InvariantCulture.Compare (systemFontName, string.Empty) != 0;
278                         }
279                 }
280
281                 [Browsable (false)]
282                 public string SystemFontName {
283                         get {
284                                 return systemFontName;
285                         }
286                 }
287
288                 internal string SysFontName {
289                         set {
290                                 systemFontName = value;
291                         }
292                 }
293 #endif
294
295                 #endregion
296                 
297                 public override System.String ToString() {
298                         return ("[Font: Name="+ Name +", Size="+ Size +", Style="+ Style +", Units="+ Unit +"]");                       
299                 }
300
301                 static internal java.util.Map DeriveStyle(java.util.Map attribs, FontStyle style, bool createNew) {
302                         java.util.Map newAttribs;
303                         if (createNew) {
304                                 newAttribs = new java.util.Hashtable( attribs.size() );
305                                 object [] keys = attribs.keySet().toArray();
306                                 for (int i=0; i < keys.Length; i++)
307                                         newAttribs.put( keys[i], attribs.get( keys[i] ) );
308                         }
309                         else
310                                 newAttribs = attribs;
311
312                         //Bold
313                         if((style & FontStyle.Bold) == FontStyle.Bold)
314                                 newAttribs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
315                         else
316                                 newAttribs.remove(TextAttribute.WEIGHT);
317
318                         //Italic
319                         if((style & FontStyle.Italic) == FontStyle.Italic)
320                                 newAttribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
321                         else
322                                 newAttribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
323
324                         //Underline
325                         if((style & FontStyle.Underline) == FontStyle.Underline)
326                                 newAttribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
327                         else
328                                 newAttribs.remove(TextAttribute.UNDERLINE);
329
330                         //Strikeout
331                         if((style & FontStyle.Strikeout) == FontStyle.Strikeout)
332                                 newAttribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
333                         else
334                                 newAttribs.remove(TextAttribute.STRIKETHROUGH);
335
336                         return newAttribs;
337                 }
338         }
339 }