Copied remotely
[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                                 name = name.ToLower ();
179                                 FontCollection collection = new InstalledFontCollection ();
180                                 FontFamily [] installedFontList = collection.Families;
181                                 foreach (FontFamily font in installedFontList) {
182                                         if (name == font.Name.ToLower ()) {
183                                                 fontFamily = font;
184                                                 break;
185                                         }
186                                 }
187
188                                 // font family not found in installed fonts
189                                 if (fontFamily == null) {
190                                         collection = new PrivateFontCollection ();
191                                         FontFamily [] privateFontList = collection.Families;
192                                         foreach (FontFamily font in privateFontList) {
193                                                 if (name == font.Name.ToLower ()) {
194                                                         fontFamily = font;
195                                                         break;
196                                                 }
197                                         }
198                                 }
199
200                                 // font family not found in private fonts also
201                                 if (fontFamily == null)
202                                         fontFamily = FontFamily.GenericSansSerif;
203                         }
204
205                         return new Font (fontFamily, size, style, unit, charSet, vertical);
206                 }
207
208                 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
209                 {
210                         return true;
211                 }
212
213                 public override PropertyDescriptorCollection GetProperties
214                                                 (ITypeDescriptorContext context,
215                                                 object value, Attribute [] attributes)
216                 {
217                         if (value is Font)
218                                 return TypeDescriptor.GetProperties (value, attributes);
219
220                         return base.GetProperties (context, value, attributes);
221                 }
222
223                 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
224                 {
225                         return true;
226                 }
227
228                 public sealed class FontNameConverter : TypeConverter
229                 {
230                         public FontNameConverter ()
231                         {
232                         }
233
234                         public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
235                         {
236                                 if (sourceType == typeof (string))
237                                         return true;
238
239                                 return base.CanConvertFrom (context, sourceType);
240                         }
241
242                         [MonoTODO]
243                         public override object ConvertFrom (ITypeDescriptorContext context,
244                                                             CultureInfo culture,
245                                                             object value)
246                         {
247                                 throw new NotImplementedException ();
248                         }
249
250                         [MonoTODO]
251                         public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
252                         {
253                                 throw new NotImplementedException ();
254                         }
255
256                         [MonoTODO]
257                         public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
258                         {
259                                 throw new NotImplementedException ();
260                         }
261
262                         public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
263                         {
264                                 return true;
265                         }
266
267                         [MonoTODO]
268                         ~FontNameConverter ()
269                         {
270                                 throw new NotImplementedException ();
271                         }
272                 }
273
274                 public class FontUnitConverter : EnumConverter
275                 {
276                         public FontUnitConverter () : base (typeof (GraphicsUnit))
277                         {
278                         }
279
280                         [MonoTODO]
281                         public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
282                         {
283                                 throw new NotImplementedException ();
284                         }
285                 }
286         }
287 }