private Size GetMessageSize(Message message)
{
Size messageSize = new Size(0, 0);
double x = 0;
bool isBreaked = true;
int lineLength = 0;
bool isReseted = true;
foreach (object o in message)
{
string inline = o as string;
Brush brush = this.textBrush;
Dictionary<int, int> dictionary = new Dictionary<int, int>();
StringBuilder lineStringBuilder = new StringBuilder();
if (inline == null)
{
Entry entry = o as Entry;
if (entry == null)
{
inline = o.ToString();
}
else
{
inline = entry.Title;
brush = this.linkBrush;
}
}
foreach (System.Text.RegularExpressions.Match match in System.Text.RegularExpressions.Regex.Matches(inline, @"[\p{IsBasicLatin}-[\s]]+\s?"))
{
dictionary.Add(match.Index, match.Length);
}
for (int i = 0; i < inline.Length; i++)
{
int length;
if (dictionary.TryGetValue(i, out length) && x + Math.Ceiling(new FormattedText(String.Concat(lineStringBuilder.ToString(), inline.Substring(i, length)), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush).WidthIncludingTrailingWhitespace) > this.maxMessageSize.Width && !isReseted)
{
if (lineStringBuilder.Length > 0)
{
FormattedText formattedText = new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush);
if (x + Math.Ceiling(formattedText.WidthIncludingTrailingWhitespace - formattedText.OverhangLeading - formattedText.OverhangTrailing) > messageSize.Width)
{
messageSize.Width = x + Math.Ceiling(formattedText.WidthIncludingTrailingWhitespace - formattedText.OverhangLeading - formattedText.OverhangTrailing);
}
}
lineStringBuilder.Clear();
messageSize.Height += this.lineHeight;
x = 0;
isBreaked = true;
}
lineStringBuilder.Append(inline[i]);
if (lineStringBuilder.ToString().EndsWith(Environment.NewLine, StringComparison.Ordinal))
{
lineStringBuilder.Remove(lineStringBuilder.ToString().LastIndexOf(Environment.NewLine, StringComparison.Ordinal), Environment.NewLine.Length);
if (lineStringBuilder.Length > 0)
{
FormattedText formattedText = new FormattedText(lineStringBuilder.ToString().Substring(0, lineStringBuilder.Length), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush);
if (x + Math.Ceiling(formattedText.WidthIncludingTrailingWhitespace) > messageSize.Width)
{
messageSize.Width = x + Math.Ceiling(formattedText.WidthIncludingTrailingWhitespace);
}
lineStringBuilder.Remove(0, lineStringBuilder.Length);
}
messageSize.Height += this.lineHeight;
x = 0;
isBreaked = true;
isReseted = true;
}
else if (x + Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush).WidthIncludingTrailingWhitespace) > this.maxMessageSize.Width)
{
if (lineStringBuilder.Length - 1 > 0)
{
FormattedText formattedText = new FormattedText(lineStringBuilder.ToString().Substring(0, lineStringBuilder.Length - 1), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush);
if (x + Math.Ceiling(formattedText.WidthIncludingTrailingWhitespace) > messageSize.Width)
{
messageSize.Width = x + Math.Ceiling(formattedText.WidthIncludingTrailingWhitespace);
}
lineStringBuilder.Remove(0, lineStringBuilder.Length - 1);
}
messageSize.Height += this.lineHeight;
x = 0;
isBreaked = true;
isReseted = true;
}
else
{
isReseted = false;
}
}
if (lineStringBuilder.Length > 0)
{
x += Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, brush).WidthIncludingTrailingWhitespace);
if (x > messageSize.Width)
{
messageSize.Width = x;
}
isBreaked = false;
isReseted = false;
}
lineLength = lineStringBuilder.Length;
}
if (isBreaked || lineLength > 0)
{
messageSize.Height += this.lineHeight;
}
if (message.HasAttachments)
{
messageSize.Height += this.lineHeight * message.Attachments.Count;
}
return messageSize;
}