summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/Font.cs
blob: bb6da4f4a8a04f70aab68404afda9690522b7376 (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
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
using System;

namespace Xamarin.Forms
{
	[TypeConverter(typeof(FontTypeConverter))]
	public struct Font
	{
		public string FontFamily { get; private set; }

		public double FontSize { get; private set; }

		public NamedSize NamedSize { get; private set; }

		public FontAttributes FontAttributes { get; private set; }

		public bool IsDefault
		{
			get { return FontFamily == null && FontSize == 0 && NamedSize == NamedSize.Default && FontAttributes == FontAttributes.None; }
		}

		public bool UseNamedSize
		{
			get { return FontSize <= 0; }
		}

		public static Font Default
		{
			get { return default(Font); }
		}

		public Font WithSize(double size)
		{
			return new Font { FontFamily = FontFamily, FontSize = size, NamedSize = 0, FontAttributes = FontAttributes };
		}

		public Font WithSize(NamedSize size)
		{
			if (size <= 0)
				throw new ArgumentOutOfRangeException("size");

			return new Font { FontFamily = FontFamily, FontSize = 0, NamedSize = size, FontAttributes = FontAttributes };
		}

		public Font WithAttributes(FontAttributes fontAttributes)
		{
			return new Font { FontFamily = FontFamily, FontSize = FontSize, NamedSize = NamedSize, FontAttributes = fontAttributes };
		}

		public static Font OfSize(string name, double size)
		{
			var result = new Font { FontFamily = name, FontSize = size };
			return result;
		}

		public static Font OfSize(string name, NamedSize size)
		{
			var result = new Font { FontFamily = name, NamedSize = size };
			return result;
		}

		public static Font SystemFontOfSize(double size)
		{
			var result = new Font { FontSize = size };
			return result;
		}

		public static Font SystemFontOfSize(NamedSize size)
		{
			var result = new Font { NamedSize = size };
			return result;
		}

		public static Font SystemFontOfSize(double size, FontAttributes attributes)
		{
			var result = new Font { FontSize = size, FontAttributes = attributes };
			return result;
		}

		public static Font SystemFontOfSize(NamedSize size, FontAttributes attributes)
		{
			var result = new Font { NamedSize = size, FontAttributes = attributes };
			return result;
		}

		[Obsolete("BoldSystemFontOfSize is obsolete, please use SystemFontOfSize (double, FontAttributes)")]
		public static Font BoldSystemFontOfSize(double size)
		{
			var result = new Font { FontSize = size, FontAttributes = FontAttributes.Bold };
			return result;
		}

		[Obsolete("BoldSystemFontOfSize is obsolete, please use SystemFontOfSize (NamedSize, FontAttributes)")]
		public static Font BoldSystemFontOfSize(NamedSize size)
		{
			var result = new Font { NamedSize = size, FontAttributes = FontAttributes.Bold };
			return result;
		}

		bool Equals(Font other)
		{
			return string.Equals(FontFamily, other.FontFamily) && FontSize.Equals(other.FontSize) && NamedSize == other.NamedSize && FontAttributes == other.FontAttributes;
		}

		public override bool Equals(object obj)
		{
			if (ReferenceEquals(null, obj))
			{
				return false;
			}
			if (obj.GetType() != GetType())
			{
				return false;
			}
			return Equals((Font)obj);
		}

		public override int GetHashCode()
		{
			unchecked
			{
				int hashCode = FontFamily != null ? FontFamily.GetHashCode() : 0;
				hashCode = (hashCode * 397) ^ FontSize.GetHashCode();
				hashCode = (hashCode * 397) ^ NamedSize.GetHashCode();
				hashCode = (hashCode * 397) ^ FontAttributes.GetHashCode();

				return hashCode;
			}
		}

		public static bool operator ==(Font left, Font right)
		{
			return left.Equals(right);
		}

		public static bool operator !=(Font left, Font right)
		{
			return !left.Equals(right);
		}

		public override string ToString()
		{
			return string.Format("FontFamily: {0}, FontSize: {1}, NamedSize: {2}, FontAttributes: {3}", FontFamily, FontSize, NamedSize, FontAttributes);
		}
	}
}