public static int GetEnd(this IAttributedTextRun run)
        {
            if (run == null)
                return 0;

            return run.Start + run.Length;
        }
        private void WriteRun(
            IAttributedTextRun run,
            TextWriter writer)
        {
            if (run.Attributes != null)
            {
                var attributes = run.Attributes;

                writer.Write($"<{XmlNames.Run}");

                writer.Write($" {XmlNames.Start}=\"");
                writer.Write(run.Start.ToString(CultureInfo.InvariantCulture));
                writer.Write("\"");

                writer.Write($" {XmlNames.Length}=\"");
                writer.Write(run.Length.ToString(CultureInfo.InvariantCulture));
                writer.Write("\"");

                foreach (var entry in run.Attributes)
                {
                    Write(attributes, entry.Key, null, writer);
                }

                writer.Write(" />");
            }
        }
        public static bool IntersectsExactly(
            this IAttributedTextRun first,
            IAttributedTextRun second)
        {
            if (first == null || second == null)
                return false;

            return first.Start == second.Start && first.Length == second.Length;
        }
Esempio n. 4
0
        public static int GetEnd(this IAttributedTextRun run)
        {
            if (run == null)
            {
                return(0);
            }

            return(run.Start + run.Length);
        }
        public static bool IntersectsExactly(
            this IAttributedTextRun first,
            int start,
            int length)
        {
            if (first == null)
                return false;

            return first.Start == start && first.Length == length;
        }
Esempio n. 6
0
 private static void HandleRun(
     NSMutableAttributedString attributedString,
     IAttributedTextRun section,
     string contextFontName  = null,
     float contextFontSize   = 12f,
     string contextFontColor = null,
     bool coreTextCompatible = false)
 {
     AddAttributes(attributedString, section.Attributes, section.Start, section.Length, contextFontName, contextFontSize, contextFontColor, coreTextCompatible);
 }
Esempio n. 7
0
        public static void Optimize(this List <IAttributedTextRun> runs, int textLength)
        {
            // Loop through the runs and make sure that they don't extend beyond the bounds of the text.
            for (int i = 0; i < runs.Count; i++)
            {
                var run = runs[i];
                var end = run.GetEnd();

                if (run.Start < 0 || end > textLength)
                {
                    var start     = Math.Max(run.Start, 0);
                    var maxLength = textLength - start;
                    var length    = Math.Min(run.Length, maxLength);
                    if (length > 0)
                    {
                        runs[i] = new AttributedTextRun(start, length, run.Attributes);
                    }
                    else
                    {
                        runs.RemoveAt(i--);
                    }
                }
            }

            runs.Sort(AttributedTextRunComparer.Instance);

            // Loop through the runs and join the ones that overlap.
            IAttributedTextRun previous = null;

            for (int i = 0; i < runs.Count; i++)
            {
                var run = runs[i];

                if (previous != null)
                {
                    if (previous.IntersectsExactly(run))
                    {
                        var combined = previous.Attributes.Union(run.Attributes);
                        run = runs[i - 1] = new AttributedTextRun(run.Start, run.Length, combined);
                        runs.RemoveAt(i--);
                    }
                    else if (previous.Intersects(run))
                    {
                        var intersections = previous.CalculatedIntersections(run);
                        runs.RemoveAt(i--);
                        runs.RemoveAt(i);
                        runs.InsertRange(i++, intersections);
                        run = runs[i];
                        runs.Sort(AttributedTextRunComparer.Instance);
                    }
                }

                previous = run;
            }
        }
        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 void AddRun(IAttributedTextRun run)
        {
            if (_runs == null)
            {
                _runs = new List <IAttributedTextRun> {
                    run
                };
                return;
            }

            _runs.Add(run);
            _runs = this.OptimizeRuns();
        }
        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;
        }
Esempio n. 11
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;
        }
        private static void ApplyFormattingToSpan(Span span, IAttributedTextRun textRun)
        {
            var attributes = textRun.Attributes;

            var fontName = attributes.GetFontName();

            if (fontName != null)
            {
                span.FontFamily = new Windows.Media.FontFamily(fontName);
            }

            if (attributes.GetBold())
            {
                span.FontWeight = FontWeights.Bold;
            }

            if (attributes.GetItalic())
            {
                span.FontStyle = FontStyles.Italic;
            }

            if (attributes.GetUnderline())
            {
                span.TextDecorations.Add(TextDecorations.Underline);
            }

            var foregroundColor = attributes.GetForegroundColor()?.ParseAsInts()?.ToColor();

            if (foregroundColor != null)
            {
                var brush = new SolidColorBrush((Windows.Media.Color)foregroundColor);
                span.Foreground = brush;
            }

            var backgroundColor = attributes.GetBackgroundColor()?.ParseAsInts()?.ToColor();

            if (backgroundColor != null)
            {
                var brush = new SolidColorBrush((Windows.Media.Color)backgroundColor);
                span.Background = brush;
            }

            if (attributes.GetSubscript())
            {
                span.BaselineAlignment = BaselineAlignment.Subscript;
            }

            if (attributes.GetSuperscript())
            {
                span.BaselineAlignment = BaselineAlignment.Superscript;
            }

            if (attributes.GetStrikethrough())
            {
                span.TextDecorations.Add(TextDecorations.Strikethrough);
            }

            /*if (attributes.GetUnorderedList())
             * {
             *  var bulletSpan = new BulletSpan();
             *  spannableString.SetSpan(bulletSpan, start, end, SpanTypes.ExclusiveExclusive);
             * }*/
        }
Esempio n. 14
0
        private static void HandleFormatRun(ISpannable spannableString, IAttributedTextRun run)
        {
            var attributes = run.Attributes;
            var start      = run.Start;
            var end        = start + run.Length;

            var fontName = attributes.GetFontName();

            if (fontName != null)
            {
                var typefaceSpan = new TypefaceSpan(fontName);
                spannableString.SetSpan(typefaceSpan, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetBold() && attributes.GetItalic())
            {
                var span = new StyleSpan(TypefaceStyle.BoldItalic);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }
            else if (attributes.GetBold())
            {
                var span = new StyleSpan(TypefaceStyle.Bold);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }
            else if (attributes.GetItalic())
            {
                var span = new StyleSpan(TypefaceStyle.Italic);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetUnderline())
            {
                var span = new UnderlineSpan();
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            var foregroundColor = attributes.GetForegroundColor()?.ParseAsInts()?.ToColor();

            if (foregroundColor != null)
            {
                var span = new ForegroundColorSpan((global::Android.Graphics.Color)foregroundColor);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            var backgroundColor = attributes.GetBackgroundColor()?.ParseAsInts()?.ToColor();

            if (backgroundColor != null)
            {
                var span = new BackgroundColorSpan((global::Android.Graphics.Color)backgroundColor);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetSubscript())
            {
                var span = new SubscriptSpan();
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetSuperscript())
            {
                var span = new SuperscriptSpan();
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetStrikethrough())
            {
                var span = new StrikethroughSpan();
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetUnorderedList())
            {
                var bulletSpan = new BulletSpan();
                spannableString.SetSpan(bulletSpan, start, end, SpanTypes.ExclusiveExclusive);
            }
        }