Apricot.Balloon.GetInspectorSize C# (CSharp) Method

GetInspectorSize() private method

private GetInspectorSize ( Entry entry ) : Size
entry Entry
return System.Windows.Size
        private Size GetInspectorSize(Entry entry)
        {
            const double space = 10;
            Size imageSize = new Size(32, 32);
            Size iconSize = new Size(6, 6);
            string title = String.IsNullOrEmpty(entry.Title) && entry.Resource != null ? entry.Resource.ToString() : entry.Title;
            Size inspectorSize = new Size(this.baseWidth - 12, imageSize.Height / 2);

            if (!String.IsNullOrEmpty(title))
            {
                double iconWidth = entry.HasSimilarEntries ? (space - iconSize.Width) / 2 + iconSize.Width : 0;
                double titleWidth = inspectorSize.Width - 30 - imageSize.Width - iconWidth;
                Dictionary<int, int> dictionary = new Dictionary<int, int>();
                StringBuilder lineStringBuilder = new StringBuilder();
                int lines = 0;
                bool isBreaked = false;
                bool isReseted = true;

                foreach (System.Text.RegularExpressions.Match match in System.Text.RegularExpressions.Regex.Matches(title, @"[\p{IsBasicLatin}-[\s]]+\s?"))
                {
                    dictionary.Add(match.Index, match.Length);
                }

                for (int i = 0; i < title.Length; i++)
                {
                    int length;

                    if (dictionary.TryGetValue(i, out length) && Math.Ceiling(new FormattedText(String.Concat(lineStringBuilder.ToString(), title.Substring(i, length)), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, this.linkBrush).WidthIncludingTrailingWhitespace) > titleWidth && !isReseted)
                    {
                        lineStringBuilder.Clear();
                        lines++;
                        isBreaked = true;
                    }

                    lineStringBuilder.Append(title[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)
                        {
                            lineStringBuilder.Remove(0, lineStringBuilder.Length);
                        }

                        lines++;
                        isBreaked = true;
                        isReseted = true;
                    }
                    else if (Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, this.linkBrush).WidthIncludingTrailingWhitespace) > titleWidth)
                    {
                        if (lineStringBuilder.Length - 1 > 0)
                        {
                            lineStringBuilder.Remove(0, lineStringBuilder.Length - 1);
                        }

                        lines++;
                        isBreaked = true;
                        isReseted = true;
                    }
                    else
                    {
                        isReseted = false;
                    }
                }

                if (lineStringBuilder.Length > 0 || isBreaked)
                {
                    lines++;
                }

                inspectorSize.Height += this.lineHeight * lines;
            }

            if (entry.Resource != null && entry.Resource.IsAbsoluteUri)
            {
                string authority = entry.Resource.Authority;
                StringBuilder lineStringBuilder = new StringBuilder();
                int lines = 0;

                for (int i = 0; i < authority.Length; i++)
                {
                    int safeLineLength = lineStringBuilder.Length;

                    lineStringBuilder.Append(authority[i]);

                    if (Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, this.linkBrush).WidthIncludingTrailingWhitespace) > inspectorSize.Width - 30 - imageSize.Width)
                    {
                        if (safeLineLength > 0)
                        {
                            lineStringBuilder.Remove(0, safeLineLength);
                        }

                        lines++;
                    }
                }

                if (lineStringBuilder.Length > 0)
                {
                    lines++;
                }

                inspectorSize.Height += this.lineHeight * lines;
            }

            if (!String.IsNullOrEmpty(entry.Author))
            {
                Dictionary<int, int> dictionary = new Dictionary<int, int>();
                StringBuilder lineStringBuilder = new StringBuilder();
                int lines = 0;
                bool isBreaked = false;

                foreach (System.Text.RegularExpressions.Match match in System.Text.RegularExpressions.Regex.Matches(entry.Author, @"[\p{IsBasicLatin}-[\s]]+\s?"))
                {
                    dictionary.Add(match.Index, match.Length);
                }

                for (int i = 0; i < entry.Author.Length; i++)
                {
                    int length;

                    if (dictionary.TryGetValue(i, out length) && Math.Ceiling(new FormattedText(String.Concat(lineStringBuilder.ToString(), entry.Author.Substring(i, length)), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, this.linkBrush).WidthIncludingTrailingWhitespace) > inspectorSize.Width - 30 - imageSize.Width)
                    {
                        lineStringBuilder.Clear();
                        lines++;
                        isBreaked = true;
                    }

                    lineStringBuilder.Append(entry.Author[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)
                        {
                            lineStringBuilder.Remove(0, lineStringBuilder.Length);
                        }

                        lines++;
                        isBreaked = true;
                    }
                    else if (lineStringBuilder.Length > 0 && Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, this.linkBrush).WidthIncludingTrailingWhitespace) > inspectorSize.Width - 30 - imageSize.Width)
                    {
                        if (lineStringBuilder.Length - 1 > 0)
                        {
                            lineStringBuilder.Remove(0, lineStringBuilder.Length - 1);
                        }

                        lines++;
                        isBreaked = true;
                    }
                }

                if (lineStringBuilder.Length > 0 || isBreaked)
                {
                    lines++;
                }

                inspectorSize.Height += this.lineHeight * lines;
            }

            if (entry.Modified.Ticks > 0)
            {
                string modified = entry.Modified.ToString("G", System.Globalization.CultureInfo.CurrentCulture);
                Dictionary<int, int> dictionary = new Dictionary<int, int>();
                StringBuilder lineStringBuilder = new StringBuilder();
                int lines = 0;

                foreach (System.Text.RegularExpressions.Match match in System.Text.RegularExpressions.Regex.Matches(modified, @"\S+\s?"))
                {
                    dictionary.Add(match.Index, match.Length);
                }

                for (int i = 0; i < modified.Length; i++)
                {
                    int length;

                    if (dictionary.TryGetValue(i, out length) && Math.Ceiling(new FormattedText(String.Concat(lineStringBuilder.ToString(), modified.Substring(i, length)), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, this.textBrush).WidthIncludingTrailingWhitespace) > inspectorSize.Width - 30 - imageSize.Width)
                    {
                        lineStringBuilder.Clear();
                        lines++;
                    }

                    int safeLineLength = lineStringBuilder.Length;

                    lineStringBuilder.Append(modified[i]);

                    if (Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, this.textBrush).WidthIncludingTrailingWhitespace) > inspectorSize.Width - 30 - imageSize.Width)
                    {
                        if (safeLineLength > 0)
                        {
                            lineStringBuilder.Remove(0, safeLineLength);
                        }

                        lines++;
                    }
                }

                if (lineStringBuilder.Length > 0)
                {
                    lines++;
                }

                inspectorSize.Height += this.lineHeight * lines;
            }

            if (entry.Score.HasValue)
            {
                string score = entry.Score.Value.ToString("F3", System.Globalization.CultureInfo.CurrentCulture);
                StringBuilder lineStringBuilder = new StringBuilder();
                int lines = 0;

                for (int i = 0; i < score.Length; i++)
                {
                    int safeLineLength = lineStringBuilder.Length;

                    lineStringBuilder.Append(score[i]);

                    if (space + Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, this.textBrush).WidthIncludingTrailingWhitespace) > inspectorSize.Width - 30 - imageSize.Width)
                    {
                        if (safeLineLength > 0)
                        {
                            lineStringBuilder.Remove(0, safeLineLength);
                        }

                        lines++;
                    }
                }

                if (lineStringBuilder.Length > 0)
                {
                    lines++;
                }

                inspectorSize.Height += this.lineHeight * lines;
            }

            if (entry.HasTags)
            {
                inspectorSize.Height += this.lineHeight;
            }

            if (inspectorSize.Height < imageSize.Height)
            {
                inspectorSize.Height = imageSize.Height;
            }

            return inspectorSize;
        }