aboutsummaryrefslogtreecommitdiffstatsci
path: root/Drawing2D/src/Font.cs
blob: e39f91259d4f74d3d99f3e4487f59a7b662b5674 (plain)
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
// Copyright (c) 2013-2021  Bruyère Jean-Philippe jp_bruyere@hotmail.com
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)

using System;
using Drawing2D;

namespace Crow
{
	public enum FontStyle
	{
		Normal,
		Bold,
		Italic,
		Underlined
	}
	public class Font
	{
		#region CTOR
		public Font ()
		{
		}
		#endregion

		string _name = "sans";
		int _size = 10;
		FontStyle _style = FontStyle.Normal;

		public string Name {
			get { return _name; }
			set { _name = value; }
		}
		public int Size {
			get { return _size; }
			set { _size = value; }
		}
		public FontStyle Style {
			get { return _style; }
			set { _style = value; }
		}

		public FontSlant Slant {
			get{
				switch (Style) {
				case FontStyle.Italic:
					return FontSlant.Italic;
				default:
					return FontSlant.Normal;
				}
			}
		}
		public FontWeight Wheight {
			get{
				switch (Style) {
				case FontStyle.Bold:
					return FontWeight.Bold;
				default:
					return FontWeight.Normal;
				}
			}
		}

		#region Operators
		public static implicit operator string(Font c) => c.ToString();

		public static implicit operator Font(string s) => (Font)Parse(s);
		#endregion

		public override string ToString () =>
			_style == FontStyle.Normal ? $"{_name},{_size}" : $"{_name} {_style},{_size}";

		public static Font Parse(string s)
		{
			Font f = new Font ();
			ReadOnlySpan<char> tmp = s.AsSpan ().Trim ();
			if (tmp.Length > 0) {
				int ioc = tmp.IndexOf (',');

				if (ioc >= 0) {
					f.Size = int.Parse (tmp.Slice (ioc + 1).Trim ());
					tmp = tmp.Slice (0, ioc).TrimEnd ();
				}

				ioc = tmp.IndexOf (' ');

				if (ioc < 0)
					f.Name = tmp.ToString ();
				else {
					f.Name = tmp.Slice (0, ioc).ToString ();
					f.Style = EnumsNET.Enums.Parse<FontStyle> (tmp.Slice (ioc + 1).ToString (), true);
				}
			}
			return f;
		}
	}
}