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;
40 namespace System.Drawing
42 public class FontConverter : TypeConverter
44 public FontConverter ()
48 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
50 if (sourceType == typeof (string))
53 return base.CanConvertFrom (context, sourceType);
56 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
58 if (destinationType == typeof (String))
61 return base.CanConvertTo (context, destinationType);
64 public override object ConvertTo (ITypeDescriptorContext context,
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);
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;
82 case GraphicsUnit.Document:
83 sb.Append ("doc"); break;
85 case GraphicsUnit.Point:
86 sb.Append ("pt"); break;
88 case GraphicsUnit.Inch:
89 sb.Append ("in"); break;
91 case GraphicsUnit.Millimeter:
92 sb.Append ("mm"); break;
94 case GraphicsUnit.Pixel:
95 sb.Append ("px"); break;
97 case GraphicsUnit.World:
98 sb.Append ("world"); break;
101 if (font.Style != FontStyle.Regular)
102 sb.Append (", style=").Append (font.Style);
104 return sb.ToString ();
107 return base.ConvertTo (context, culture, value, destinationType);
110 public override object ConvertFrom (ITypeDescriptorContext context,
114 string fontFamily = value as string;
115 if (fontFamily == null)
116 return base.ConvertFrom (context, culture, value);
118 // MS creates a font from the given family with
120 return new Font (fontFamily, 8);
123 public override object CreateInstance (ITypeDescriptorContext context,
124 IDictionary propertyValues)
130 bool vertical = false;
131 FontStyle style = FontStyle.Regular;
132 FontFamily fontFamily = null;
133 GraphicsUnit unit = GraphicsUnit.Point;
135 if ((value = propertyValues ["GdiCharSet"]) != null)
136 charSet = (byte) value;
138 if ((value = propertyValues ["Size"]) != null)
139 size = (float) value;
141 if ((value = propertyValues ["Unit"]) != null)
142 unit = (GraphicsUnit) value;
144 if ((value = propertyValues ["Name"]) != null)
145 name = (String) value;
147 if ((value = propertyValues ["GdiVerticalFont"]) != null)
148 vertical = (bool) value;
150 if ((value = propertyValues ["Bold"]) != null) {
151 bool bold = (bool) value;
153 style |= FontStyle.Bold;
156 if ((value = propertyValues ["Italic"]) != null) {
157 bool italic = (bool) value;
159 style |= FontStyle.Italic;
162 if ((value = propertyValues ["Strikeout"]) != null) {
163 bool strike = (bool) value;
165 style |= FontStyle.Strikeout;
168 if ((value = propertyValues ["Underline"]) != null) {
169 bool underline = (bool) value;
170 if (underline == true)
171 style |= FontStyle.Underline;
174 /* ?? Should default font be culture dependent ?? */
176 fontFamily = new FontFamily ("Tahoma");
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 ()) {
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 ()) {
200 // font family not found in private fonts also
201 if (fontFamily == null)
202 fontFamily = FontFamily.GenericSansSerif;
205 return new Font (fontFamily, size, style, unit, charSet, vertical);
208 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
213 public override PropertyDescriptorCollection GetProperties
214 (ITypeDescriptorContext context,
215 object value, Attribute [] attributes)
218 return TypeDescriptor.GetProperties (value, attributes);
220 return base.GetProperties (context, value, attributes);
223 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
228 public sealed class FontNameConverter : TypeConverter
230 public FontNameConverter ()
234 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
236 if (sourceType == typeof (string))
239 return base.CanConvertFrom (context, sourceType);
243 public override object ConvertFrom (ITypeDescriptorContext context,
247 throw new NotImplementedException ();
251 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
253 throw new NotImplementedException ();
257 public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
259 throw new NotImplementedException ();
262 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
268 ~FontNameConverter ()
270 throw new NotImplementedException ();
274 public class FontUnitConverter : EnumConverter
276 public FontUnitConverter () : base (typeof (GraphicsUnit))
281 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
283 throw new NotImplementedException ();