-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
FontAttributes.cs
80 lines (70 loc) · 2.91 KB
/
FontAttributes.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
#nullable disable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/FontAttributes.xml" path="Type[@FullName='Microsoft.Maui.Controls.FontAttributes']/Docs/*" />
[Flags]
public enum FontAttributes
{
/// <include file="../../docs/Microsoft.Maui.Controls/FontAttributes.xml" path="//Member[@MemberName='None']/Docs/*" />
None = 0,
/// <include file="../../docs/Microsoft.Maui.Controls/FontAttributes.xml" path="//Member[@MemberName='Bold']/Docs/*" />
Bold = 1 << 0,
/// <include file="../../docs/Microsoft.Maui.Controls/FontAttributes.xml" path="//Member[@MemberName='Italic']/Docs/*" />
Italic = 1 << 1
}
/// <include file="../../docs/Microsoft.Maui.Controls/FontAttributesConverter.xml" path="Type[@FullName='Microsoft.Maui.Controls.FontAttributesConverter']/Docs/*" />
public sealed class FontAttributesConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
=> sourceType == typeof(string);
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
=> destinationType == typeof(string);
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var strValue = value?.ToString();
if (string.IsNullOrEmpty(strValue))
return FontAttributes.None;
FontAttributes attributes = FontAttributes.None;
strValue = strValue.Trim();
if (strValue.IndexOf(",", StringComparison.Ordinal) != -1)
{ //Xaml
foreach (var part in strValue.Split(','))
attributes |= ParseSingleAttribute(part, strValue);
}
else
{ //CSS or single value
foreach (var part in strValue.Split(' '))
attributes |= ParseSingleAttribute(part, strValue);
}
return attributes;
}
static FontAttributes ParseSingleAttribute(string part, string originalvalue)
{
part = part.Trim();
if (Enum.TryParse(part, true, out FontAttributes attr))
return attr;
if (part.Equals("normal", StringComparison.OrdinalIgnoreCase))
return FontAttributes.None;
if (part.Equals("oblique", StringComparison.OrdinalIgnoreCase))
return FontAttributes.Italic;
throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", originalvalue, typeof(FontAttributes)));
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is not FontAttributes attr)
throw new NotSupportedException();
if (attr == FontAttributes.None)
return "";
var parts = new List<string>();
if ((attr & FontAttributes.Bold) == FontAttributes.Bold)
parts.Add(nameof(FontAttributes.Bold));
if ((attr & FontAttributes.Italic) == FontAttributes.Italic)
parts.Add(nameof(FontAttributes.Italic));
return string.Join("' ", parts);
}
}
}