2 // System.Drawing.FontConverter.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Ravindra (rkumar@novell.com)
9 // Copyright (C) 2002,2003 Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
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
42 namespace System.Drawing
44 public class FontConverter : TypeConverter
46 public FontConverter ()
55 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
57 if (sourceType == typeof (string))
60 return base.CanConvertFrom (context, sourceType);
63 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
65 if (destinationType == typeof (String))
68 if (destinationType == typeof (InstanceDescriptor))
71 return base.CanConvertTo (context, destinationType);
74 public override object ConvertTo (ITypeDescriptorContext context,
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);
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;
92 case GraphicsUnit.Document:
93 sb.Append ("doc"); break;
95 case GraphicsUnit.Point:
96 sb.Append ("pt"); break;
98 case GraphicsUnit.Inch:
99 sb.Append ("in"); break;
101 case GraphicsUnit.Millimeter:
102 sb.Append ("mm"); break;
104 case GraphicsUnit.Pixel:
105 sb.Append ("px"); break;
107 case GraphicsUnit.World:
108 sb.Append ("world"); break;
111 if (font.Style != FontStyle.Regular)
112 sb.Append (", style=").Append (font.Style);
114 return sb.ToString ();
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);
128 return base.ConvertTo (context, culture, value, destinationType);
131 public override object ConvertFrom (ITypeDescriptorContext context,
135 string fontFamily = value as string;
136 if (fontFamily == null)
137 return base.ConvertFrom (context, culture, value);
139 // MS creates a font from the given family with
141 return new Font (fontFamily, 8);
144 public override object CreateInstance (ITypeDescriptorContext context,
145 IDictionary propertyValues)
151 bool vertical = false;
152 FontStyle style = FontStyle.Regular;
153 FontFamily fontFamily = null;
154 GraphicsUnit unit = GraphicsUnit.Point;
156 if ((value = propertyValues ["GdiCharSet"]) != null)
157 charSet = (byte) value;
159 if ((value = propertyValues ["Size"]) != null)
160 size = (float) value;
162 if ((value = propertyValues ["Unit"]) != null)
163 unit = (GraphicsUnit) value;
165 if ((value = propertyValues ["Name"]) != null)
166 name = (String) value;
168 if ((value = propertyValues ["GdiVerticalFont"]) != null)
169 vertical = (bool) value;
171 if ((value = propertyValues ["Bold"]) != null) {
172 bool bold = (bool) value;
174 style |= FontStyle.Bold;
177 if ((value = propertyValues ["Italic"]) != null) {
178 bool italic = (bool) value;
180 style |= FontStyle.Italic;
183 if ((value = propertyValues ["Strikeout"]) != null) {
184 bool strike = (bool) value;
186 style |= FontStyle.Strikeout;
189 if ((value = propertyValues ["Underline"]) != null) {
190 bool underline = (bool) value;
191 if (underline == true)
192 style |= FontStyle.Underline;
195 /* ?? Should default font be culture dependent ?? */
197 fontFamily = new FontFamily ("Tahoma");
199 name = name.ToLower ();
200 FontCollection collection = new InstalledFontCollection ();
201 FontFamily [] installedFontList = collection.Families;
202 foreach (FontFamily font in installedFontList) {
203 if (name == font.Name.ToLower ()) {
209 // font family not found in installed fonts
210 if (fontFamily == null) {
211 collection = new PrivateFontCollection ();
212 FontFamily [] privateFontList = collection.Families;
213 foreach (FontFamily font in privateFontList) {
214 if (name == font.Name.ToLower ()) {
221 // font family not found in private fonts also
222 if (fontFamily == null)
223 fontFamily = FontFamily.GenericSansSerif;
226 return new Font (fontFamily, size, style, unit, charSet, vertical);
229 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
234 public override PropertyDescriptorCollection GetProperties
235 (ITypeDescriptorContext context,
236 object value, Attribute [] attributes)
239 return TypeDescriptor.GetProperties (value, attributes);
241 return base.GetProperties (context, value, attributes);
244 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
249 public sealed class FontNameConverter : TypeConverter
254 public FontNameConverter ()
258 void IDisposable.Dispose ()
\r
263 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
265 if (sourceType == typeof (string))
268 return base.CanConvertFrom (context, sourceType);
272 public override object ConvertFrom (ITypeDescriptorContext context,
276 throw new NotImplementedException ();
280 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
282 throw new NotImplementedException ();
286 public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
288 throw new NotImplementedException ();
291 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
297 public class FontUnitConverter : EnumConverter
\r
299 public FontUnitConverter () : base (typeof (GraphicsUnit))
\r
304 public override TypeConverter.StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
\r
306 throw new NotImplementedException ();