* FontConverter.cs: Implemented the missing functions.
[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
40 namespace System.Drawing
41 {
42         public class FontConverter : TypeConverter
43         {
44                 public FontConverter ()
45                 {
46                 }
47
48                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
49                 {
50                         if (sourceType == typeof (string))
51                                 return true;
52
53                         return base.CanConvertFrom (context, sourceType);
54                 }
55
56                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
57                 {
58                         if (destinationType == typeof (String))
59                                 return true;
60
61                         return base.CanConvertTo (context, destinationType);
62                 }
63
64                 public override object ConvertTo (ITypeDescriptorContext context,
65                                                   CultureInfo culture,
66                                                   object value,
67                                                   Type destinationType)
68                 {
69                         if ((destinationType == typeof (string)) && (value is Font)) {
70                                 Font font = (Font) value;
71                                 StringBuilder sb = new StringBuilder ();
72                                 sb.Append (font.Name).Append (", ");
73                                 sb.Append (font.Size);
74
75                                 switch (font.Unit) {
76                                 // MS throws ArgumentException, if unit is set 
77                                 // to GraphicsUnit.Display
78                                 // Don't know what to append for GraphicsUnit.Display
79                                 case GraphicsUnit.Display:
80                                         sb.Append ("display"); break;
81
82                                 case GraphicsUnit.Document:
83                                         sb.Append ("doc"); break;
84
85                                 case GraphicsUnit.Point:
86                                         sb.Append ("pt"); break;
87
88                                 case GraphicsUnit.Inch:
89                                         sb.Append ("in"); break;
90
91                                 case GraphicsUnit.Millimeter:
92                                         sb.Append ("mm"); break;
93
94                                 case GraphicsUnit.Pixel:
95                                         sb.Append ("px"); break;
96
97                                 case GraphicsUnit.World:
98                                         sb.Append ("world"); break;
99                                 }
100
101                                 if (font.Style != FontStyle.Regular)
102                                         sb.Append (", style=").Append (font.Style);
103
104                                 return sb.ToString ();
105                         }
106
107                         return base.ConvertTo (context, culture, value, destinationType);
108                 }
109
110                 public override object ConvertFrom (ITypeDescriptorContext context,
111                                                     CultureInfo culture,
112                                                     object value)
113                 {
114                         string fontFamily = value as string;
115                         if (fontFamily == null)
116                                 return base.ConvertFrom (context, culture, value);
117
118                         // MS creates a font from the given family with
119                         // emSize = 8.
120                         return new Font (fontFamily, 8);
121                 }
122
123                 public override object CreateInstance (ITypeDescriptorContext context,
124                                                        IDictionary propertyValues)
125                 {
126                         Object value;
127                         byte charSet = 1;
128                         float size = 8;
129                         String name = null;
130                         bool vertical = false;
131                         FontStyle style = FontStyle.Regular;
132                         FontFamily fontFamily = null;
133                         GraphicsUnit unit = GraphicsUnit.Point;
134
135                         if ((value = propertyValues ["GdiCharSet"]) != null)
136                                 charSet = (byte) value;
137
138                         if ((value = propertyValues ["Size"]) != null)
139                                 size = (float) value;
140
141                         if ((value = propertyValues ["Unit"]) != null)
142                                 unit = (GraphicsUnit) value;
143
144                         if ((value = propertyValues ["Name"]) != null)
145                                 name = (String) value;
146
147                         if ((value = propertyValues ["GdiVerticalFont"]) != null)
148                                 vertical = (bool) value;
149
150                         if ((value = propertyValues ["Bold"]) != null) {
151                                 bool bold = (bool) value;
152                                 if (bold == true)
153                                         style |= FontStyle.Bold;
154                         }
155
156                         if ((value = propertyValues ["Italic"]) != null) {
157                                 bool italic = (bool) value;
158                                 if (italic == true)
159                                         style |= FontStyle.Italic;
160                         }
161
162                         if ((value = propertyValues ["Strikeout"]) != null) {
163                                 bool strike = (bool) value;
164                                 if (strike == true)
165                                         style |= FontStyle.Strikeout;
166                         }
167
168                         if ((value = propertyValues ["Underline"]) != null) {
169                                 bool underline = (bool) value;
170                                 if (underline == true)
171                                         style |= FontStyle.Underline;
172                         }
173
174                         /* ?? Should default font be culture dependent ?? */
175                         if (name == null)
176                                 fontFamily = new FontFamily ("Tahoma");
177                         else {
178                                 FontCollection collection = new InstalledFontCollection ();
179                                 FontFamily [] installedFontList = collection.Families;
180                                 foreach (FontFamily font in installedFontList) {
181                                         if (name == font.Name) {
182                                                 fontFamily = font;
183                                                 break;
184                                         }
185                                 }
186
187                                 // font family not found in installed fonts
188                                 if (fontFamily == null) {
189                                         collection = new PrivateFontCollection ();
190                                         FontFamily [] privateFontList = collection.Families;
191                                         foreach (FontFamily font in privateFontList) {
192                                                 if (name == font.Name) {
193                                                         fontFamily = font;
194                                                         break;
195                                                 }
196                                         }
197                                 }
198
199                                 // font family not found in private fonts also
200                                 fontFamily = FontFamily.GenericSansSerif;
201                         }
202
203                         return new Font (fontFamily, size, style, unit, charSet, vertical);
204                 }
205
206                 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
207                 {
208                         return true;
209                 }
210
211                 public override PropertyDescriptorCollection GetProperties
212                                                 (ITypeDescriptorContext context,
213                                                 object value, Attribute [] attributes)
214                 {
215                         if (value is Font)
216                                 return TypeDescriptor.GetProperties (value, attributes);
217
218                         return base.GetProperties (context, value, attributes);
219                 }
220
221                 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
222                 {
223                         return true;
224                 }
225
226                 public sealed class FontNameConverter : TypeConverter
227                 {
228                         public FontNameConverter ()
229                         {
230                         }
231
232                         public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
233                         {
234                                 if (sourceType == typeof (string))
235                                         return true;
236
237                                 return base.CanConvertFrom (context, sourceType);
238                         }
239
240                         [MonoTODO]
241                         public override object ConvertFrom (ITypeDescriptorContext context,
242                                                             CultureInfo culture,
243                                                             object value)
244                         {
245                                 throw new NotImplementedException ();
246                         }
247
248                         [MonoTODO]
249                         public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
250                         {
251                                 throw new NotImplementedException ();
252                         }
253
254                         [MonoTODO]
255                         public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
256                         {
257                                 throw new NotImplementedException ();
258                         }
259
260                         public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
261                         {
262                                 return true;
263                         }
264
265                         [MonoTODO]
266                         ~FontNameConverter ()
267                         {
268                                 throw new NotImplementedException ();
269                         }
270                 }
271
272                 public class FontUnitConverter : EnumConverter
273                 {
274                         public FontUnitConverter () : base (typeof (GraphicsUnit))
275                         {
276                         }
277
278                         [MonoTODO]
279                         public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
280                         {
281                                 throw new NotImplementedException ();
282                         }
283                 }
284         }
285 }