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