-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
FontConverter.cs
465 lines (386 loc) · 17.3 KB
/
FontConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.Drawing.Text;
using System.Globalization;
using System.Reflection;
using System.Text;
namespace System.Drawing
{
public class FontConverter : TypeConverter
{
private const string StylePrefix = "style=";
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
{
return (destinationType == typeof(string)) || (destinationType == typeof(InstanceDescriptor));
}
public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
{
if (value is Font font)
{
if (destinationType == typeof(string))
{
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
ValueStringBuilder sb = default;
sb.Append(font.Name);
sb.Append(culture.TextInfo.ListSeparator[0] + " ");
sb.Append(font.Size.ToString(culture.NumberFormat));
switch (font.Unit)
{
// MS throws ArgumentException, if unit is set
// to GraphicsUnit.Display
// Don't know what to append for GraphicsUnit.Display
case GraphicsUnit.Display:
sb.Append("display");
break;
case GraphicsUnit.Document:
sb.Append("doc");
break;
case GraphicsUnit.Point:
sb.Append("pt");
break;
case GraphicsUnit.Inch:
sb.Append("in");
break;
case GraphicsUnit.Millimeter:
sb.Append("mm");
break;
case GraphicsUnit.Pixel:
sb.Append("px");
break;
case GraphicsUnit.World:
sb.Append("world");
break;
}
if (font.Style != FontStyle.Regular)
{
sb.Append(culture.TextInfo.ListSeparator[0] + " style=");
sb.Append(font.Style.ToString());
}
return sb.ToString();
}
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo? met = typeof(Font).GetConstructor(new Type[] { typeof(string), typeof(float), typeof(FontStyle), typeof(GraphicsUnit) });
object[] args = new object[4];
args[0] = font.Name;
args[1] = font.Size;
args[2] = font.Style;
args[3] = font.Unit;
return new InstanceDescriptor(met, args);
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
if (!(value is string font))
{
return base.ConvertFrom(context, culture, value);
}
font = font.Trim();
// Expected string format: "name[, size[, units[, style=style1[, style2[...]]]]]"
// Example using 'vi-VN' culture: "Microsoft Sans Serif, 8,25pt, style=Italic, Bold"
if (font.Length == 0)
{
return null;
}
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
char separator = culture.TextInfo.ListSeparator[0]; // For vi-VN: ','
string fontName = font; // start with the assumption that only the font name was provided.
string? style = null;
string? sizeStr = null;
float fontSize = 8.25f;
FontStyle fontStyle = FontStyle.Regular;
GraphicsUnit units = GraphicsUnit.Point;
// Get the index of the first separator (would indicate the end of the name in the string).
int nameIndex = font.IndexOf(separator);
if (nameIndex < 0)
{
return new Font(fontName, fontSize, fontStyle, units);
}
// Some parameters are provided in addition to name.
fontName = font.Substring(0, nameIndex);
if (nameIndex < font.Length - 1)
{
// Get the style index (if any). The size is a bit problematic because it can be formatted differently
// depending on the culture, we'll parse it last.
int styleIndex = culture.CompareInfo.IndexOf(font, StylePrefix, CompareOptions.IgnoreCase);
if (styleIndex != -1)
{
// style found.
style = font.Substring(styleIndex, font.Length - styleIndex);
// Get the mid-substring containing the size information.
sizeStr = font.Substring(nameIndex + 1, styleIndex - nameIndex - 1);
}
else
{
// no style.
sizeStr = font.Substring(nameIndex + 1);
}
// Parse size.
(string? size, string? unit) unitTokens = ParseSizeTokens(sizeStr, separator);
if (unitTokens.size != null)
{
try
{
fontSize = (float)TypeDescriptor.GetConverter(typeof(float)).ConvertFromString(context, culture, unitTokens.size);
}
catch
{
// Exception from converter is too generic.
throw new ArgumentException(SR.Format(SR.TextParseFailedFormat, font, $"name{separator} size[units[{separator} style=style1[{separator} style2{separator} ...]]]"), nameof(value));
}
}
if (unitTokens.unit != null)
{
// ParseGraphicsUnits throws an ArgumentException if format is invalid.
units = ParseGraphicsUnits(unitTokens.unit);
}
if (style != null)
{
// Parse FontStyle
style = style.Substring(6); // style string always starts with style=
string[] styleTokens = style.Split(separator);
for (int tokenCount = 0; tokenCount < styleTokens.Length; tokenCount++)
{
string styleText = styleTokens[tokenCount];
styleText = styleText.Trim();
fontStyle |= (FontStyle)Enum.Parse(typeof(FontStyle), styleText, true);
// Enum.IsDefined doesn't do what we want on flags enums...
FontStyle validBits = FontStyle.Regular | FontStyle.Bold | FontStyle.Italic | FontStyle.Underline | FontStyle.Strikeout;
if ((fontStyle | validBits) != validBits)
{
throw new InvalidEnumArgumentException(nameof(style), (int)fontStyle, typeof(FontStyle));
}
}
}
}
return new Font(fontName, fontSize, fontStyle, units);
}
private (string?, string?) ParseSizeTokens(string text, char separator)
{
string? size = null;
string? units = null;
text = text.Trim();
int length = text.Length;
int splitPoint;
if (length > 0)
{
// text is expected to have a format like " 8,25pt, ". Leading and trailing spaces (trimmed above),
// last comma, unit and decimal value may not appear. We need to make it ####.##CC
for (splitPoint = 0; splitPoint < length; splitPoint++)
{
if (char.IsLetter(text[splitPoint]))
{
break;
}
}
char[] trimChars = new char[] { separator, ' ' };
if (splitPoint > 0)
{
size = text.Substring(0, splitPoint);
// Trimming spaces between size and units.
size = size.Trim(trimChars);
}
if (splitPoint < length)
{
units = text.Substring(splitPoint);
units = units.TrimEnd(trimChars);
}
}
return (size, units);
}
private GraphicsUnit ParseGraphicsUnits(string units) =>
units switch
{
"display" => GraphicsUnit.Display,
"doc" => GraphicsUnit.Document,
"pt" => GraphicsUnit.Point,
"in" => GraphicsUnit.Inch,
"mm" => GraphicsUnit.Millimeter,
"px" => GraphicsUnit.Pixel,
"world" => GraphicsUnit.World,
_ => throw new ArgumentException(SR.Format(SR.InvalidArgumentValueFontConverter, units), nameof(units)),
};
public override object CreateInstance(ITypeDescriptorContext? context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException(nameof(propertyValues));
}
object? value;
byte charSet = 1;
float size = 8;
string? name = null;
bool vertical = false;
FontStyle style = FontStyle.Regular;
FontFamily? fontFamily = null;
GraphicsUnit unit = GraphicsUnit.Point;
if ((value = propertyValues["GdiCharSet"]) != null)
charSet = (byte)value;
if ((value = propertyValues["Size"]) != null)
size = (float)value;
if ((value = propertyValues["Unit"]) != null)
unit = (GraphicsUnit)value;
if ((value = propertyValues["Name"]) != null)
name = (string)value;
if ((value = propertyValues["GdiVerticalFont"]) != null)
vertical = (bool)value;
if ((value = propertyValues["Bold"]) != null)
{
if ((bool)value == true)
style |= FontStyle.Bold;
}
if ((value = propertyValues["Italic"]) != null)
{
if ((bool)value == true)
style |= FontStyle.Italic;
}
if ((value = propertyValues["Strikeout"]) != null)
{
if ((bool)value == true)
style |= FontStyle.Strikeout;
}
if ((value = propertyValues["Underline"]) != null)
{
if ((bool)value == true)
style |= FontStyle.Underline;
}
if (name == null)
{
fontFamily = new FontFamily("Tahoma");
}
else
{
FontCollection collection = new InstalledFontCollection();
FontFamily[] installedFontList = collection.Families;
foreach (FontFamily font in installedFontList)
{
if (name.Equals(font.Name, StringComparison.OrdinalIgnoreCase))
{
fontFamily = font;
break;
}
}
// font family not found in installed fonts
if (fontFamily == null)
{
collection = new PrivateFontCollection();
FontFamily[] privateFontList = collection.Families;
foreach (FontFamily font in privateFontList)
{
if (name.Equals(font.Name, StringComparison.OrdinalIgnoreCase))
{
fontFamily = font;
break;
}
}
}
// font family not found in private fonts also
if (fontFamily == null)
fontFamily = FontFamily.GenericSansSerif;
}
return new Font(fontFamily, size, style, unit, charSet, vertical);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext? context) => true;
public override PropertyDescriptorCollection GetProperties(
ITypeDescriptorContext? context,
object? value,
Attribute[]? attributes)
{
if (value is not Font)
return base.GetProperties(context, value, attributes);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(value, attributes);
return props.Sort(new string[] { nameof(Font.Name), nameof(Font.Size), nameof(Font.Unit) });
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context) => true;
public sealed class FontNameConverter : TypeConverter, IDisposable
{
private readonly FontFamily[] _fonts;
public FontNameConverter()
{
_fonts = FontFamily.Families;
}
void IDisposable.Dispose()
{
}
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType)
{
return sourceType == typeof(string) ? true : base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
return value is string strValue ? MatchFontName(strValue, context) : base.ConvertFrom(context, culture, value);
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext? context)
{
string[] values = new string[_fonts.Length];
for (int i = 0; i < _fonts.Length; i++)
{
values[i] = _fonts[i].Name;
}
Array.Sort(values, Comparer.Default);
return new TypeConverter.StandardValuesCollection(values);
}
// We allow other values other than those in the font list.
public override bool GetStandardValuesExclusive(ITypeDescriptorContext? context) => false;
// Yes, we support picking an element from the list.
public override bool GetStandardValuesSupported(ITypeDescriptorContext? context) => true;
private string MatchFontName(string name, ITypeDescriptorContext? context)
{
// Try a partial match
string? bestMatch = null;
// setting fontName as nullable since IEnumerable.Current returned nullable in 3.0
foreach (string? fontName in GetStandardValues(context))
{
Debug.Assert(fontName != null);
if (fontName.Equals(name, StringComparison.InvariantCultureIgnoreCase))
{
// For an exact match, return immediately
return fontName;
}
if (fontName.StartsWith(name, StringComparison.InvariantCultureIgnoreCase))
{
if (bestMatch == null || fontName.Length <= bestMatch.Length)
{
bestMatch = fontName;
}
}
}
// No match... fall back on whatever was provided
return bestMatch ?? name;
}
}
public class FontUnitConverter : EnumConverter
{
public FontUnitConverter() : base(typeof(GraphicsUnit)) { }
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext? context)
{
// display graphic unit is not supported.
if (Values == null)
{
base.GetStandardValues(context); // sets "values"
Debug.Assert(Values != null);
ArrayList filteredValues = new ArrayList(Values);
filteredValues.Remove(GraphicsUnit.Display);
Values = new StandardValuesCollection(filteredValues);
}
return Values;
}
}
}
}