Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FontUnit.cs « System.Web.UI.WebControls « System.Web « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b9242e892c4da33e870b1474730f591b09247c9f (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
 * Namespace: System.Web.UI.WebControls
 * Struct:    FontUnit
 *
 * Author:  Gaurav Vaish
 * Maintainer: gvaish@iitk.ac.in
 * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
 * Implementation: yes
 * Status:  100%
 *
 * (C) Gaurav Vaish (2001)
 */

using System;
using System.Globalization;
using System.Web;
using System.Web.UI;

namespace System.Web.UI.WebControls
{
	public struct FontUnit
	{
		public static readonly FontUnit Empty   = new FontUnit();
		public static readonly FontUnit Large   = new FontUnit(FontSize.Large);
		public static readonly FontUnit Larger  = new FontUnit(FontSize.Larger);
		public static readonly FontUnit Medium  = new FontUnit(FontSize.Medium);
		public static readonly FontUnit Small   = new FontUnit(FontSize.Small);
		public static readonly FontUnit Smaller = new FontUnit(FontSize.Smaller);
		public static readonly FontUnit XLarge  = new FontUnit(FontSize.XLarge);
		public static readonly FontUnit XSmall  = new FontUnit(FontSize.XSmall);
		public static readonly FontUnit XXLarge = new FontUnit(FontSize.XXLarge);
		public static readonly FontUnit XXSmall = new FontUnit(FontSize.XXSmall);

		private FontSize type;
		private Unit     val;

		public FontUnit(FontSize type)
		{
			if(!Enum.IsDefined(typeof(FontSize), type))
				throw new ArgumentException();
			this.type = type;
			if(this.type == FontSize.AsUnit)
			{
				val = Unit.Point(10);
			} else
			{
				val = Unit.Empty;
			}
		}

		public FontUnit(int value)
		{
			type = FontSize.AsUnit;
			val = Unit.Point(value);
		}

		public FontUnit(string value): this(value, CultureInfo.CurrentCulture)
		{
		}

		public FontUnit(Unit value)
		{
			if(value.IsEmpty)
			{
				type = FontSize.NotSet;
				val  = Unit.Empty;
			} else
			{
				type = FontSize.AsUnit;
				val  = value;
			}
		}

		public FontUnit(string value, CultureInfo culture)
		{
			type = FontSize.NotSet;
			val  = Unit.Empty;
			if(value != null && value != String.Empty)
			{
				string low = value.ToLower(culture);
				int index = GetTypeFromString(low);
				if( index != -1)
				{
					type = (FontSize)index;
					return;
				} else
				{
					val = new Unit(value, culture, UnitType.Point);
					type = FontSize.AsUnit;
				}
			}
		}

		private int GetTypeFromString(string strVal)
		{
			string[] values = {
				"smaller",
				"larger",
				"xx-small",
				"x-small",
				"small",
				"medium",
				"large",
				"xlarge",
				"xxlarge"
			};
			int i = 0;
			foreach(string valType in values)
			{
				if(strVal == valType)
				{
					return (i + 2);
				}
				i++;
			}
			return -1;
		}

		public static FontUnit Parse(string s)
		{
			return Parse(s, CultureInfo.CurrentCulture);
		}

		public static FontUnit Parse(string s, CultureInfo culture)
		{
			return new FontUnit(s, culture);
		}

		public static FontUnit Point(int n)
		{
			return new FontUnit(n);
		}

		public static bool operator ==(FontUnit left, FontUnit right)
		{
			return (left.type == right.type && left.val == right.val);
		}

		public static bool operator !=(FontUnit left, FontUnit right)
		{
			return !(left == right);
		}

		public static implicit operator FontUnit(int n)
		{
			return FontUnit.Point(n);
		}

		public override bool Equals(object obj)
		{
			if(obj!= null && obj is FontUnit)
			{
				FontUnit that = (FontUnit)obj;
				return (this.type == that.type && this.val == that.val);
			}
			return false;
		}

		public override int GetHashCode()
		{
			return ( (type.GetHashCode() << 2) | val.GetHashCode() );
		}

		public override string ToString()
		{
			return ToString(CultureInfo.CurrentCulture);
		}

		public string ToString(CultureInfo culture)
		{
			if(IsEmpty)
			{
				return String.Empty;
			}
			//string strRepr = String.Empty;
			switch(type)
			{
				case FontSize.AsUnit:  return val.ToString(culture);
				case FontSize.XXSmall: return "XX-Small";
				case FontSize.XSmall:  return "X-Small";
				case FontSize.XLarge:  return "X-Large";
				case FontSize.XXLarge: return "XX-Large";
				default:               return PropertyConverter.EnumToString(typeof(FontSize), type);
			}
		}

		public bool IsEmpty
		{
			get
			{
				return (type == FontSize.NotSet);
			}
		}

		public FontSize Type
		{
			get
			{
				return type;
			}
		}

		public Unit Unit
		{
			get
			{
				return val;
			}
		}
	}
}