public static bool Intersects(
            this IAttributedTextRun first,
            IAttributedTextRun second)
        {
            if (first == null || second == null)
                return false;

            if (first.Start < second.GetEnd())
                return first.GetEnd() > second.Start;

            return false;
        }
        public static bool Intersects(
            this IAttributedTextRun first,
            int start,
            int length)
        {
            if (first == null)
                return false;

            var end = start + length;
            if (first.Start < end)
                return first.GetEnd() > start;

            return false;
        }
Пример #3
0
        public static bool Intersects(
            this IAttributedTextRun first,
            IAttributedTextRun second)
        {
            if (first == null || second == null)
            {
                return(false);
            }

            if (first.Start < second.GetEnd())
            {
                return(first.GetEnd() > second.Start);
            }

            return(false);
        }
        public static IList<IAttributedTextRun> CalculatedIntersections(
            this IAttributedTextRun first,
            IAttributedTextRun second)
        {
            List<IAttributedTextRun> intersections = new List<IAttributedTextRun>();

            var combined = first.Attributes.Union(second.Attributes);
            if (first.Start == second.Start)
            {
                if (first.Length == second.Length)
                {
                    intersections.Add(new AttributedTextRun(first.Start, first.Length, combined));
                }
                else if (first.Length > second.Length)
                {
                    var start1 = first.Start;
                    var length1 = Math.Min(first.Length, second.Length);
                    var start2 = start1 + length1;
                    var length2 = Math.Max(first.Length, second.Length) - length1;

                    intersections.Add(new AttributedTextRun(start1, length1, combined));
                    intersections.Add(new AttributedTextRun(start2, length2, first.Attributes));
                }
                else if (first.Length < second.Length)
                {
                    var start1 = first.Start;
                    var length1 = Math.Min(first.Length, second.Length);
                    var start2 = start1 + length1;
                    var length2 = Math.Max(first.Length, second.Length) - length1;

                    intersections.Add(new AttributedTextRun(start1, length1, combined));
                    intersections.Add(new AttributedTextRun(start2, length2, second.Attributes));
                }
            }
            else if (first.GetEnd() == second.GetEnd())
            {
                if (first.Start < second.Start)
                {
                    var start1 = first.Start;
                    var length1 = second.Start - first.Start;
                    var start2 = start1 + length1;
                    var length2 = Math.Max(first.Length, second.Length) - length1;

                    intersections.Add(new AttributedTextRun(start1, length1, first.Attributes));
                    intersections.Add(new AttributedTextRun(start2, length2, combined));
                }
                else
                {
                    var start1 = second.Start;
                    var length1 = first.Start - second.Start;
                    var start2 = start1 + length1;
                    var length2 = Math.Max(first.Length, second.Length) - length1;

                    intersections.Add(new AttributedTextRun(start1, length1, second.Attributes));
                    intersections.Add(new AttributedTextRun(start2, length2, combined));
                }
            }
            else
            {
                if (first.Start < second.Start)
                {
                    var start1 = first.Start;
                    var length1 = second.Start - first.Start;
                    var start2 = start1 + length1;
                    var length2 = second.Length;
                    var start3 = start2 + length2;
                    var length3 = Math.Max(first.Length, second.Length) - (length1 + length2);

                    intersections.Add(new AttributedTextRun(start1, length1, first.Attributes));
                    intersections.Add(new AttributedTextRun(start2, length2, combined));
                    intersections.Add(new AttributedTextRun(start3, length3, first.GetEnd() > second.GetEnd() ? first.Attributes : second.Attributes));
                }
                else
                {
                    var start1 = second.Start;
                    var length1 = first.Start - second.Start;
                    var start2 = start1 + length1;
                    var length2 = first.Length;
                    var start3 = start2 + length2;
                    var length3 = Math.Max(first.Length, second.Length) - (length1 + length2);

                    intersections.Add(new AttributedTextRun(start1, length1, second.Attributes));
                    intersections.Add(new AttributedTextRun(start2, length2, combined));
                    intersections.Add(new AttributedTextRun(start3, length3, first.GetEnd() > second.GetEnd() ? first.Attributes : second.Attributes));
                }
            }

            return intersections;
        }