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 ()
50 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
52 if (sourceType == typeof (string))
55 return base.CanConvertFrom (context, sourceType);
58 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
60 if (destinationType == typeof (String))
63 if (destinationType == typeof (InstanceDescriptor))
66 return base.CanConvertTo (context, destinationType);
69 public override object ConvertTo (ITypeDescriptorContext context,
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);
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;
87 case GraphicsUnit.Document:
88 sb.Append ("doc"); break;
90 case GraphicsUnit.Point:
91 sb.Append ("pt"); break;
93 case GraphicsUnit.Inch:
94 sb.Append ("in"); break;
96 case GraphicsUnit.Millimeter:
97 sb.Append ("mm"); break;
99 case GraphicsUnit.Pixel:
100 sb.Append ("px"); break;
102 case GraphicsUnit.World:
103 sb.Append ("world"); break;
106 if (font.Style != FontStyle.Regular)
107 sb.Append (", style=").Append (font.Style);
109 return sb.ToString ();
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);
123 return base.ConvertTo (context, culture, value, destinationType);
126 public override object ConvertFrom (ITypeDescriptorContext context,
130 string fontFamily = value as string;
131 if (fontFamily == null)
132 return base.ConvertFrom (context, culture, value);
134 // MS creates a font from the given family with
136 return new Font (fontFamily, 8);
139 public override object CreateInstance (ITypeDescriptorContext context,
140 IDictionary propertyValues)
146 bool vertical = false;
147 FontStyle style = FontStyle.Regular;
148 FontFamily fontFamily = null;
149 GraphicsUnit unit = GraphicsUnit.Point;
151 if ((value = propertyValues ["GdiCharSet"]) != null)
152 charSet = (byte) value;
154 if ((value = propertyValues ["Size"]) != null)
155 size = (float) value;
157 if ((value = propertyValues ["Unit"]) != null)
158 unit = (GraphicsUnit) value;
160 if ((value = propertyValues ["Name"]) != null)
161 name = (String) value;
163 if ((value = propertyValues ["GdiVerticalFont"]) != null)
164 vertical = (bool) value;
166 if ((value = propertyValues ["Bold"]) != null) {
167 bool bold = (bool) value;
169 style |= FontStyle.Bold;
172 if ((value = propertyValues ["Italic"]) != null) {
173 bool italic = (bool) value;
175 style |= FontStyle.Italic;
178 if ((value = propertyValues ["Strikeout"]) != null) {
179 bool strike = (bool) value;
181 style |= FontStyle.Strikeout;
184 if ((value = propertyValues ["Underline"]) != null) {
185 bool underline = (bool) value;
186 if (underline == true)
187 style |= FontStyle.Underline;
190 /* ?? Should default font be culture dependent ?? */
192 fontFamily = new FontFamily ("Tahoma");
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 ()) {
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 ()) {
216 // font family not found in private fonts also
217 if (fontFamily == null)
218 fontFamily = FontFamily.GenericSansSerif;
221 return new Font (fontFamily, size, style, unit, charSet, vertical);
224 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
229 public override PropertyDescriptorCollection GetProperties
230 (ITypeDescriptorContext context,
231 object value, Attribute [] attributes)
234 return TypeDescriptor.GetProperties (value, attributes);
236 return base.GetProperties (context, value, attributes);
239 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
244 public sealed class FontNameConverter : TypeConverter
246 public FontNameConverter ()
250 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
252 if (sourceType == typeof (string))
255 return base.CanConvertFrom (context, sourceType);
259 public override object ConvertFrom (ITypeDescriptorContext context,
263 throw new NotImplementedException ();
267 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
269 throw new NotImplementedException ();
273 public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
275 throw new NotImplementedException ();
278 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
285 ~FontNameConverter ()
287 throw new NotImplementedException ();
292 public class FontUnitConverter : EnumConverter
\r
294 public FontUnitConverter () : base (typeof (GraphicsUnit))
\r
299 public override TypeConverter.StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
\r
301 throw new NotImplementedException ();