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, CultureInfo culture, object value)
133 string fontFamily = value as string;
134 if (fontFamily == null)
135 return base.ConvertFrom (context, culture, value);
137 // MS creates a font from the given family with
139 return new Font (fontFamily, 8);
142 public override object CreateInstance (ITypeDescriptorContext context, IDictionary propertyValues)
148 bool vertical = false;
149 FontStyle style = FontStyle.Regular;
150 FontFamily fontFamily = null;
151 GraphicsUnit unit = GraphicsUnit.Point;
153 if ((value = propertyValues ["GdiCharSet"]) != null)
154 charSet = (byte) value;
156 if ((value = propertyValues ["Size"]) != null)
157 size = (float) value;
159 if ((value = propertyValues ["Unit"]) != null)
160 unit = (GraphicsUnit) value;
162 if ((value = propertyValues ["Name"]) != null)
163 name = (String) value;
165 if ((value = propertyValues ["GdiVerticalFont"]) != null)
166 vertical = (bool) value;
168 if ((value = propertyValues ["Bold"]) != null) {
169 bool bold = (bool) value;
171 style |= FontStyle.Bold;
174 if ((value = propertyValues ["Italic"]) != null) {
175 bool italic = (bool) value;
177 style |= FontStyle.Italic;
180 if ((value = propertyValues ["Strikeout"]) != null) {
181 bool strike = (bool) value;
183 style |= FontStyle.Strikeout;
186 if ((value = propertyValues ["Underline"]) != null) {
187 bool underline = (bool) value;
188 if (underline == true)
189 style |= FontStyle.Underline;
192 /* ?? Should default font be culture dependent ?? */
194 fontFamily = new FontFamily ("Tahoma");
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 ()) {
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 ()) {
218 // font family not found in private fonts also
219 if (fontFamily == null)
220 fontFamily = FontFamily.GenericSansSerif;
223 return new Font (fontFamily, size, style, unit, charSet, vertical);
226 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
231 public override PropertyDescriptorCollection GetProperties
232 (ITypeDescriptorContext context,
233 object value, Attribute [] attributes)
236 return TypeDescriptor.GetProperties (value, attributes);
238 return base.GetProperties (context, value, attributes);
241 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
246 public sealed class FontNameConverter : TypeConverter
253 public FontNameConverter ()
255 fonts = FontFamily.Families;
258 void IDisposable.Dispose ()
262 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
264 if (sourceType == typeof (string))
267 return base.CanConvertFrom (context, sourceType);
270 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
274 return base.ConvertFrom (context, culture, value);
277 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
279 string [] values = new string [fonts.Length];
280 for (int i = fonts.Length; i > 0;){
282 values [i] = fonts [i].Name;
285 return new TypeConverter.StandardValuesCollection (values);
288 public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
290 // We allow other values other than those in the font list.
294 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
296 // Yes, we support picking an element from the list.
301 public class FontUnitConverter : EnumConverter
303 public FontUnitConverter () : base (typeof (GraphicsUnit)) {}
305 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
307 return base.GetStandardValues (context);