-
Notifications
You must be signed in to change notification settings - Fork 5
/
FontConverter.cs
51 lines (44 loc) · 1.95 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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
namespace MusicBeePlugin
{
internal class FontConverter : JsonConverter<Font>
{
private static TypeConverter Converter { get; } = TypeDescriptor.GetConverter(typeof(Font));
public override void WriteJson(JsonWriter writer, Font value, JsonSerializer serializer)
{
writer.WriteValue(Converter.ConvertToInvariantString(value));
}
public override Font ReadJson(JsonReader reader, Type objectType, Font existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var str = serializer.Deserialize<string>(reader);
try
{
return (Font) Converter.ConvertFromInvariantString(str);
}
catch (Exception)
{
// Fallback to legacy
return ConvertFromLegacyString(str);
}
}
private static Font ConvertFromLegacyString(string str)
{
var m = Regex.Match(
str,
"^(?<Font>[\\w ]+),(?<Size>(\\d+(\\.\\d+)?))(,(?<Style>(R|[BIU]{1,3})))?$",
RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
if (!m.Success) throw new FormatException("Font settings are not properly formatted.");
if (m.Groups.Count < 4 || m.Groups[3].Value == "R")
return new Font(m.Groups["Font"].Value, float.Parse(m.Groups["Size"].Value));
var fs = (m.Groups[3].Value.Contains("B") ? FontStyle.Bold : FontStyle.Regular)
| (m.Groups[3].Value.Contains("I") ? FontStyle.Italic : FontStyle.Regular)
| (m.Groups[3].Value.Contains("U") ? FontStyle.Underline : FontStyle.Regular);
return new Font(m.Groups["Font"].Value, float.Parse(m.Groups["Size"].Value), fs,
GraphicsUnit.Point);
}
}
}