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