2006-04-28 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / class / System.Drawing / System.Drawing / FontConverter.cs
1 //
2 // System.Drawing.FontConverter.cs
3 //
4 // Authors:
5 //      Dennis Hayes (dennish@Raytek.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //      Ravindra (rkumar@novell.com)
8 //
9 // Copyright (C) 2002,2003 Ximian, Inc.  http://www.ximian.com
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Text;
35 using System.Collections;
36 using System.ComponentModel;
37 using System.Globalization;
38 using System.Drawing.Text;
39 using System.ComponentModel.Design.Serialization;\r
40 using System.Reflection;\r
41
42 namespace System.Drawing
43 {
44         public class FontConverter : TypeConverter
45         {
46                 public FontConverter ()
47                 {
48                 }
49
50                 ~FontConverter ()
51                 {
52                                 
53                 }       
54
55                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
56                 {
57                         if (sourceType == typeof (string))
58                                 return true;
59
60                         return base.CanConvertFrom (context, sourceType);
61                 }
62
63                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
64                 {
65                         if (destinationType == typeof (String))
66                                 return true;
67
68                         if (destinationType == typeof (InstanceDescriptor))
69                                 return true;
70
71                         return base.CanConvertTo (context, destinationType);
72                 }
73
74                 public override object ConvertTo (ITypeDescriptorContext context,
75                         CultureInfo culture,
76                         object value,
77                         Type destinationType)
78                 {
79                         if ((destinationType == typeof (string)) && (value is Font)) {
80                                 Font font = (Font) value;
81                                 StringBuilder sb = new StringBuilder ();
82                                 sb.Append (font.Name).Append (", ");
83                                 sb.Append (font.Size);
84
85                                 switch (font.Unit) {
86                                         // MS throws ArgumentException, if unit is set 
87                                         // to GraphicsUnit.Display
88                                         // Don't know what to append for GraphicsUnit.Display
89                                 case GraphicsUnit.Display:
90                                         sb.Append ("display"); break;
91
92                                 case GraphicsUnit.Document:
93                                         sb.Append ("doc"); break;
94
95                                 case GraphicsUnit.Point:
96                                         sb.Append ("pt"); break;
97
98                                 case GraphicsUnit.Inch:
99                                         sb.Append ("in"); break;
100
101                                 case GraphicsUnit.Millimeter:
102                                         sb.Append ("mm"); break;
103
104                                 case GraphicsUnit.Pixel:
105                                         sb.Append ("px"); break;
106
107                                 case GraphicsUnit.World:
108                                         sb.Append ("world"); break;
109                                 }
110
111                                 if (font.Style != FontStyle.Regular)
112                                         sb.Append (", style=").Append (font.Style);
113
114                                 return sb.ToString ();
115                         }
116
117                         if ((destinationType == typeof (InstanceDescriptor)) && (value is Font)) {
118                                 Font font = (Font) value;
119                                 ConstructorInfo met = typeof(Font).GetConstructor (new Type[] {typeof(string), typeof(float), typeof(FontStyle), typeof(GraphicsUnit)});\r
120                                 object[] args = new object[4];
121                                 args [0] = font.Name;
122                                 args [1] = font.Size;
123                                 args [2] = font.Style;\r
124                                 args [3] = font.Unit;
125                                 return new InstanceDescriptor (met, args);
126                         }
127                         
128                         return base.ConvertTo (context, culture, value, destinationType);
129                 }
130
131                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
132                 {
133                         string fontFamily = value as string;
134                         if (fontFamily == null)
135                                 return base.ConvertFrom (context, culture, value);
136
137                         // MS creates a font from the given family with
138                         // emSize = 8.
139                         return new Font (fontFamily, 8);
140                 }
141
142                 public override object CreateInstance (ITypeDescriptorContext context, IDictionary propertyValues)
143                 {
144                         Object value;
145                         byte charSet = 1;
146                         float size = 8;
147                         String name = null;
148                         bool vertical = false;
149                         FontStyle style = FontStyle.Regular;
150                         FontFamily fontFamily = null;
151                         GraphicsUnit unit = GraphicsUnit.Point;
152
153                         if ((value = propertyValues ["GdiCharSet"]) != null)
154                                 charSet = (byte) value;
155
156                         if ((value = propertyValues ["Size"]) != null)
157                                 size = (float) value;
158
159                         if ((value = propertyValues ["Unit"]) != null)
160                                 unit = (GraphicsUnit) value;
161
162                         if ((value = propertyValues ["Name"]) != null)
163                                 name = (String) value;
164
165                         if ((value = propertyValues ["GdiVerticalFont"]) != null)
166                                 vertical = (bool) value;
167
168                         if ((value = propertyValues ["Bold"]) != null) {
169                                 bool bold = (bool) value;
170                                 if (bold == true)
171                                         style |= FontStyle.Bold;
172                         }
173
174                         if ((value = propertyValues ["Italic"]) != null) {
175                                 bool italic = (bool) value;
176                                 if (italic == true)
177                                         style |= FontStyle.Italic;
178                         }
179
180                         if ((value = propertyValues ["Strikeout"]) != null) {
181                                 bool strike = (bool) value;
182                                 if (strike == true)
183                                         style |= FontStyle.Strikeout;
184                         }
185
186                         if ((value = propertyValues ["Underline"]) != null) {
187                                 bool underline = (bool) value;
188                                 if (underline == true)
189                                         style |= FontStyle.Underline;
190                         }
191
192                         /* ?? Should default font be culture dependent ?? */
193                         if (name == null)
194                                 fontFamily = new FontFamily ("Tahoma");
195                         else {
196                                 name = name.ToLower ();
197                                 FontCollection collection = new InstalledFontCollection ();
198                                 FontFamily [] installedFontList = collection.Families;
199                                 foreach (FontFamily font in installedFontList) {
200                                         if (name == font.Name.ToLower ()) {
201                                                 fontFamily = font;
202                                                 break;
203                                         }
204                                 }
205
206                                 // font family not found in installed fonts
207                                 if (fontFamily == null) {
208                                         collection = new PrivateFontCollection ();
209                                         FontFamily [] privateFontList = collection.Families;
210                                         foreach (FontFamily font in privateFontList) {
211                                                 if (name == font.Name.ToLower ()) {
212                                                         fontFamily = font;
213                                                         break;
214                                                 }
215                                         }
216                                 }
217
218                                 // font family not found in private fonts also
219                                 if (fontFamily == null)
220                                         fontFamily = FontFamily.GenericSansSerif;
221                         }
222
223                         return new Font (fontFamily, size, style, unit, charSet, vertical);
224                 }
225
226                 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
227                 {
228                         return true;
229                 }
230
231                 public override PropertyDescriptorCollection GetProperties
232                         (ITypeDescriptorContext context,
233                         object value, Attribute [] attributes)
234                 {
235                         if (value is Font)
236                                 return TypeDescriptor.GetProperties (value, attributes);
237
238                         return base.GetProperties (context, value, attributes);
239                 }
240
241                 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
242                 {
243                         return true;
244                 }
245
246                 public sealed class FontNameConverter : TypeConverter
247 #if NET_2_0
248                 , IDisposable           
249 #endif
250                 {
251                         FontFamily [] fonts;
252                         
253                         public FontNameConverter ()
254                         {
255                                 fonts = FontFamily.Families;
256                         }       
257 #if NET_2_0                     
258                         void IDisposable.Dispose ()
259                         {
260                         }
261 #endif
262                         public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
263                         {
264                                 if (sourceType == typeof (string))
265                                         return true;
266
267                                 return base.CanConvertFrom (context, sourceType);
268                         }
269
270                         public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
271                         {
272                                 if (value is string)
273                                         return value;
274                                 return base.ConvertFrom (context, culture, value);
275                         }
276
277                         public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
278                         {
279                                 string [] values = new string [fonts.Length];
280                                 for (int i = fonts.Length; i > 0;){
281                                         i--;
282                                         values [i] = fonts [i].Name;
283                                 }
284                                 
285                                 return new TypeConverter.StandardValuesCollection (values);
286                         }
287
288                         public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
289                         {
290                                 // We allow other values other than those in the font list.
291                                 return false;
292                         }
293
294                         public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
295                         {
296                                 // Yes, we support picking an element from the list. 
297                                 return true;
298                         }
299                 }
300
301                 public class FontUnitConverter : EnumConverter
302                 {
303                         public FontUnitConverter () : base (typeof (GraphicsUnit)) {}
304                         
305                         public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
306                         {
307                                 return base.GetStandardValues (context);
308                         }
309                                 
310                 }
311         }
312 }