using System;
using System.Text;
using EColor = ElmSharp.Color;
namespace Xamarin.Forms.Platform.Tizen.Native
{
///
/// Represent a text with attributes applied.
///
public class Span
{
static EColor s_defaultLineColor = EColor.Black;
string _text;
///
/// Gets or sets the formatted text.
///
public FormattedString FormattedText { get; set; }
///
/// Gets or sets the text.
///
///
/// Setting Text to a non-null value will set the FormattedText property to null.
///
public string Text
{
get
{
if (FormattedText != null)
{
return FormattedText.ToString();
}
else
{
return _text;
}
}
set
{
if (value == null)
{
value = "";
}
else
{
FormattedText = null;
}
_text = value;
}
}
///
/// Gets or sets the color for the text.
///
public EColor ForegroundColor { get; set; }
///
/// Gets or sets the background color for the text.
///
public EColor BackgroundColor { get; set; }
///
/// Gets or sets the font family for the text.
///
public string FontFamily { get; set; }
///
/// Gets or sets the font attributes for the text.
/// See for information about FontAttributes.
///
public FontAttributes FontAttributes { get; set; }
///
/// Gets or sets the font size for the text.
///
public double FontSize { get; set; }
///
/// Gets or sets the font weight for the text.
///
public FontWeight FontWeight { get; set; }
///
/// Gets or sets the line break mode for the text.
/// See for information about LineBreakMode.
///
public LineBreakMode LineBreakMode { get; set; }
///
/// Gets or sets the horizontal alignment mode for the text.
/// See for information about TextAlignment.
///
public TextAlignment HorizontalTextAlignment { get; set; }
///
/// Gets or sets the vertical alignment mode for the text.
/// See for information about TextAlignment.
///
public TextAlignment VerticalTextAlignment { get; set; }
///
/// Gets or sets the value that indicates whether the text has underline.
///
public bool Underline { get; set; }
///
/// Gets or sets the value that indicates whether the text has strike line though it.
///
public bool Strikethrough { get; set; }
///
/// Create a new Span instance with default attributes.
///
public Span()
{
Text = "";
FontFamily = "";
FontSize = -1;
FontWeight = FontWeight.None;
FontAttributes = FontAttributes.None;
ForegroundColor = EColor.Default;
BackgroundColor = EColor.Default;
HorizontalTextAlignment = TextAlignment.None;
VerticalTextAlignment = TextAlignment.None;
LineBreakMode = LineBreakMode.None;
Underline = false;
Strikethrough = false;
}
///
/// This method return marked up text
///
internal string GetMarkupText()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("");
sb.Append(GetDecoratedText());
sb.Append("");
return sb.ToString();
}
///
/// This method return text decorated with markup if FormattedText is set or plain text otherwise.
///
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.ConvertToPixel(FontSize));
}
if ((FontAttributes & FontAttributes.Bold) != 0)
{
_formattingString.Append("font_weight=Bold ");
}
else
{
// FontWeight is only available in case of FontAttributes.Bold is not used.
switch (FontWeight)
{
case FontWeight.Bold:
_formattingString.Append("font_weight=Bold ");
break;
case FontWeight.SemiBold:
_formattingString.Append("font_weight=SemiBold ");
break;
case FontWeight.UltraBold:
_formattingString.Append("font_weight=UltraBold ");
break;
case FontWeight.Black:
_formattingString.Append("font_weight=Black ");
break;
case FontWeight.ExtraBlack:
_formattingString.Append("font_weight=ExtraBlack ");
break;
case FontWeight.Book:
_formattingString.Append("font_weight=Book ");
break;
case FontWeight.Light:
_formattingString.Append("font_weight=Light ");
break;
case FontWeight.Medium:
_formattingString.Append("font_weight=Medium ");
break;
case FontWeight.Normal:
_formattingString.Append("font_weight=Normal ");
break;
case FontWeight.Thin:
_formattingString.Append("font_weight=Thin ");
break;
case FontWeight.UltraLight:
_formattingString.Append("font_weight=UltraLight ");
break;
}
}
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 (VerticalTextAlignment)
{
case TextAlignment.Auto:
case TextAlignment.Start:
_formattingString.Append("valign=top ");
break;
case TextAlignment.End:
_formattingString.Append("valign=bottom ");
break;
case TextAlignment.Center:
_formattingString.Append("valign=middle ");
break;
case TextAlignment.None:
break;
}
switch (LineBreakMode)
{
case LineBreakMode.NoWrap:
_formattingString.Append("wrap=none");
break;
case LineBreakMode.CharacterWrap:
_formattingString.Append("wrap=char");
break;
case LineBreakMode.WordWrap:
_formattingString.Append("wrap=word");
break;
case LineBreakMode.MixedWrap:
_formattingString.Append("wrap=mixed");
break;
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.None:
break;
}
return _formattingString;
}
string ConvertTags(string text)
{
return text.Replace("&", "&")
.Replace("<", "<")
.Replace(">", ">")
.Replace(Environment.NewLine, "
");
}
public string GetStyle()
{
StringBuilder sb = new StringBuilder();
sb.Append("DEFAULT='");
PrepareFormattingString(sb);
sb.Append("'");
return sb.ToString();
}
///
/// Converts string value to Span.
///
/// The string text
public static implicit operator Span(string text)
{
return new Span { Text = text };
}
}
}