aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/OpenGL/GLBackend/Font/Font.cs
blob: 4ff3bb15b90aede995454739be67286f333df009 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;


namespace go.GLBackend
{
	public class Font
	{
		public Font ()
		{
		}

		public Font (string name, FontSlant fs, FontWeight fw)
		{
			_name = name;
			switch (fs) {
			case FontSlant.Italic:
				Style |= FontStyle.Italic;
				break;
			case FontSlant.Oblique:
				Style |= FontStyle.Oblique;
				break;
			}
			if (fw == FontWeight.Bold)
				Style |= FontStyle.Bold;
		}

		string _name = "droid";
		FontStyle _style = FontStyle.Normal;
		FontFlag _flags = FontFlag.None;

		int _size = 10;

		public string Name {
			get { return _name; }
			set { _name = value; }
		}
		public FontStyle Style {
			get { return _style; }
			set { _style = value; }
		}
		public FontFlag Flags {
			get { return _flags; }
			set { _flags = value; }
		}
		public FontSlant Slant {
			get{ 
				if ((Style & FontStyle.Italic) == FontStyle.Italic)
					return FontSlant.Italic;
				if ((Style & FontStyle.Oblique) == FontStyle.Oblique)
					return FontSlant.Oblique;
				return FontSlant.Normal;
			}
		}
		public FontWeight Wheight {
			get{ return (Style & FontStyle.Bold) == FontStyle.Bold ? FontWeight.Bold : FontWeight.Normal; }
		}
		public int Size {
			get { return _size; }
			set { _size = value; }
		}

		#region Operators
		public static implicit operator string(Font c)
		{
			return c.ToString();
		}
		public static implicit operator Font(string s)
		{
			Font f = new Font ();

			if (!string.IsNullOrEmpty (s)) {
				f.Name = "";
				string[] attribs = s.TrimStart().TrimEnd().Split(',');
				for (int i = 0; i < attribs.Length; i++) {
					FontFlag fl;
					FontStyle fs;
					int sz;
					if (Enum.TryParse<FontFlag> (attribs [i], true, out fl))
						f.Flags |= fl;
					else if (Enum.TryParse<FontStyle> (attribs [i], true, out fs))
						f.Style |= fs;
					else if (int.TryParse (attribs [i], out sz))
						f.Size = sz;
					else
						f.Name += attribs[i] + " ";
				}
				f.Name = f.Name.Trim();
			}

			return f;
		}
		#endregion
		public string FontFaceString
		{
			get {
				string tmp = Name;
				if (Style != FontStyle.Normal)
					tmp += "," + Style.ToString ();
				if (Flags != FontFlag.None)
					tmp += "," + Flags.ToString ();
				return tmp;
			}
		}
		public override string ToString()
		{
			return string.Format("{0},{1}", FontFaceString, _size);
		}

		public static object Parse(string s)
		{
			return (Font)s;
		}
	}
}