summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Native/Span.cs
blob: 3b410ba1096df2fccc330fb529af02e93a7a2e7f (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System;
using System.Text;
using EColor = ElmSharp.Color;
using Specific = Xamarin.Forms.PlatformConfiguration.TizenSpecific;

namespace Xamarin.Forms.Platform.Tizen.Native
{
	/// <summary>
	/// Represent a text with attributes applied.
	/// </summary>
	public class Span
	{
		static EColor s_defaultLineColor = EColor.Black;
		string _text;

		/// <summary>
		/// Gets or sets the formatted text.
		/// </summary>
		public FormattedString FormattedText { get; set; }

		/// <summary>
		/// Gets or sets the text.
		/// </summary>
		/// <remarks>
		/// Setting Text to a non-null value will set the FormattedText property to null.
		/// </remarks>
		public string Text
		{
			get
			{
				if (FormattedText != null)
				{
					return FormattedText.ToString();
				}
				else
				{
					return _text;
				}
			}
			set
			{
				if (value == null)
				{
					value = "";
				}
				else
				{
					FormattedText = null;
				}
				_text = value;
			}
		}

		/// <summary>
		/// Gets or sets the color for the text.
		/// </summary>
		public EColor ForegroundColor { get; set; }

		/// <summary>
		/// Gets or sets the background color for the text.
		/// </summary>
		public EColor BackgroundColor { get; set; }

		/// <summary>
		/// Gets or sets the font family for the text.
		/// </summary>
		public string FontFamily { get; set; }

		/// <summary>
		/// Gets or sets the font attributes for the text.
		/// See <see cref="FontAttributes"/> for information about FontAttributes.
		/// </summary>
		public FontAttributes FontAttributes { get; set; }

		/// <summary>
		/// Gets or sets the font size for the text.
		/// </summary>
		public double FontSize { get; set; }

		/// <summary>
		/// Gets or sets the font weight for the text.
		/// </summary>
		public string FontWeight { get; set; }

		/// <summary>
		/// Gets or sets the line break mode for the text.
		/// See <see cref="LineBreakMode"/> for information about LineBreakMode.
		/// </summary>
		public LineBreakMode LineBreakMode { get; set; }

		/// <summary>
		/// Gets or sets the horizontal alignment mode for the text.
		/// See <see cref="TextAlignment"/> for information about TextAlignment.
		/// </summary>
		public TextAlignment HorizontalTextAlignment { get; set; }

		/// <summary>
		/// Gets or sets the value that indicates whether the text has underline.
		/// </summary>
		public bool Underline { get; set; }

		/// <summary>
		/// Gets or sets the value that indicates whether the text has strike line though it.
		/// </summary>
		public bool Strikethrough { get; set; }

		/// <summary>
		/// Create a new Span instance with default attributes.
		/// </summary>
		public Span()
		{
			Text = "";
			FontFamily = "";
			FontSize = -1;
			FontWeight = Specific.FontWeight.None;
			FontAttributes = FontAttributes.None;
			ForegroundColor = EColor.Default;
			BackgroundColor = EColor.Default;
			HorizontalTextAlignment = TextAlignment.None;
			LineBreakMode = LineBreakMode.None;
			Underline = false;
			Strikethrough = false;
		}

		/// <summary>
		/// This method return marked up text
		/// </summary>
		internal string GetMarkupText()
		{
			StringBuilder sb = new StringBuilder();

			sb.AppendFormat("<span ");

			sb = PrepareFormattingString(sb);

			sb.Append(">");

			sb.Append(GetDecoratedText());

			sb.Append("</span>");

			return sb.ToString();
		}

		/// <summary>
		/// This method return text decorated with markup if FormattedText is set or plain text otherwise.
		/// </summary>
		public string GetDecoratedText()
		{
			if (FormattedText != null)
			{
				return FormattedText.ToMarkupString();
			}
			else
			{
				return ConvertTags(Text);
			}
		}

		StringBuilder PrepareFormattingString(StringBuilder _formattingString)
		{
			if (!ForegroundColor.IsDefault)
			{
				_formattingString.AppendFormat("color={0} ", ForegroundColor.ToHex());
			}

			if (!BackgroundColor.IsDefault)
			{
				_formattingString.AppendFormat("backing_color={0} backing=on ", BackgroundColor.ToHex());
			}

			if (!string.IsNullOrEmpty(FontFamily))
			{
				_formattingString.AppendFormat("font={0} ", FontFamily);
			}

			if (FontSize != -1)
			{
				_formattingString.AppendFormat("font_size={0} ", Forms.ConvertToEflFontPoint(FontSize));
			}

			if ((FontAttributes & FontAttributes.Bold) != 0)
			{
				_formattingString.Append("font_weight=Bold ");
			}
			else
			{
				// FontWeight is only available in case of FontAttributes.Bold is not used.
				if(FontWeight != Specific.FontWeight.None)
				{
					_formattingString.AppendFormat("font_weight={0} ", FontWeight);
				}
			}

			if ((FontAttributes & FontAttributes.Italic) != 0)
			{
				_formattingString.Append("font_style=italic ");
			}

			if (Underline)
			{
				_formattingString.AppendFormat("underline=on underline_color={0} ",
					ForegroundColor.IsDefault ? s_defaultLineColor.ToHex() : ForegroundColor.ToHex());
			}

			if (Strikethrough)
			{
				_formattingString.AppendFormat("strikethrough=on strikethrough_color={0} ",
					ForegroundColor.IsDefault ? s_defaultLineColor.ToHex() : ForegroundColor.ToHex());
			}

			switch (HorizontalTextAlignment)
			{
				case TextAlignment.Auto:
					_formattingString.Append("align=auto ");
					break;

				case TextAlignment.Start:
					_formattingString.Append("align=left ");
					break;

				case TextAlignment.End:
					_formattingString.Append("align=right ");
					break;

				case TextAlignment.Center:
					_formattingString.Append("align=center ");
					break;

				case TextAlignment.None:
					break;
			}

			switch (LineBreakMode)
			{
				case LineBreakMode.HeadTruncation:
					_formattingString.Append("ellipsis=0.0");
					break;

				case LineBreakMode.MiddleTruncation:
					_formattingString.Append("ellipsis=0.5");
					break;

				case LineBreakMode.TailTruncation:
					_formattingString.Append("ellipsis=1.0");
					break;

				case LineBreakMode.NoWrap:
				case LineBreakMode.CharacterWrap:
				case LineBreakMode.WordWrap:
				case LineBreakMode.MixedWrap:
				case LineBreakMode.None:
					break;
			}

			return _formattingString;
		}

		string ConvertTags(string text)
		{
			return text.Replace("&", "&amp;")
					   .Replace("<", "&lt;")
					   .Replace(">", "&gt;")
					   .Replace(Environment.NewLine, "<br>");
		}

		public string GetStyle()
		{
			StringBuilder sb = new StringBuilder();

			sb.Append("DEFAULT='");

			PrepareFormattingString(sb);

			sb.Append("'");

			return sb.ToString();
		}

		/// <summary>
		/// Converts string value to Span.
		/// </summary>
		/// <param name="text">The string text</param>
		public static implicit operator Span(string text)
		{
			return new Span { Text = text };
		}
	}
}