コード例 #1
0
 private TextStyleRun(TextStyleRun run)
 {
     Flags = run.Flags;
     Start = run.Start;
     End   = run.End;
     Type  = run.Type;
 }
コード例 #2
0
ファイル: TextStyleRun.cs プロジェクト: nitanmarcel/Unigram
        public static IList <TextStyleRun> GetRuns(string text, IList <TextEntity> entities)
        {
            var runs         = new List <TextStyleRun>();
            var entitiesCopy = new List <TextEntity>(entities);

            entitiesCopy.Sort((x, y) =>
            {
                if (x.Offset > y.Offset)
                {
                    return(1);
                }
                else if (y.Offset > x.Offset)
                {
                    return(-1);
                }

                return(0);
            });

            for (int a = 0, N = entitiesCopy.Count; a < N; a++)
            {
                var entity = entitiesCopy[a];
                if (entity.Length <= 0 || entity.Offset < 0 || entity.Offset >= text.Length)
                {
                    continue;
                }
                else if (entity.Offset + entity.Length > text.Length)
                {
                    entity.Length = text.Length - entity.Offset;
                }

                var newRun = new TextStyleRun();
                newRun.Start = entity.Offset;
                newRun.End   = newRun.Start + entity.Length;

                if (entity.Type is TextEntityTypeStrikethrough)
                {
                    newRun.Flags = TextStyle.Strikethrough;
                }
                else if (entity.Type is TextEntityTypeUnderline)
                {
                    newRun.Flags = TextStyle.Underline;
                }
                //else if (entity.Type is TextEntityTypeBlockQuote)
                //{
                //    newRun.Flags = TextStyle.BlockQuote;
                //}
                else if (entity.Type is TextEntityTypeBold)
                {
                    newRun.Flags = TextStyle.Bold;
                }
                else if (entity.Type is TextEntityTypeItalic)
                {
                    newRun.Flags = TextStyle.Italic;
                }
                else if (entity.Type is TextEntityTypeCode or TextEntityTypePre or TextEntityTypePreCode)
                {
                    newRun.Flags = TextStyle.Monospace;
                    newRun.Type  = entity.Type;
                }
コード例 #3
0
 public TextStyleRun(TextStyleRun run)
 {
     Flags  = run.Flags;
     Start  = run.Start;
     End    = run.End;
     Entity = run.Entity;
 }
コード例 #4
0
 private void Merge(TextStyleRun run)
 {
     Flags |= run.Flags;
     if (Type == null && run.Type != null)
     {
         Type = run.Type;
     }
 }
コード例 #5
0
 private void Merge(TextStyleRun run)
 {
     Flags |= run.Flags;
     if (Entity == null && run.Entity != null)
     {
         Entity = run.Entity;
     }
 }
コード例 #6
0
ファイル: TextBlockHelper.cs プロジェクト: sp1ke77/Unigram
        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var sender   = d as TextBlock;
            var newValue = e.NewValue as string;

            var span = new Span();

            sender.Inlines.Clear();
            sender.Inlines.Add(span);

            var markdown = Client.Execute(new GetTextEntities(newValue)) as TextEntities;

            if (markdown == null)
            {
                return;
            }

            var entities = markdown.Entities;
            var text     = newValue;

            var runs     = TextStyleRun.GetRuns(text, entities);
            var previous = 0;

            foreach (var entity in runs)
            {
                if (entity.Offset > previous)
                {
                    span.Inlines.Add(new Run {
                        Text = text.Substring(previous, entity.Offset - previous)
                    });
                }

                if (entity.Length + entity.Offset > text.Length)
                {
                    previous = entity.Offset + entity.Length;
                    continue;
                }

                if (entity.HasFlag(TextStyle.Monospace))
                {
                    span.Inlines.Add(new Run {
                        Text = text.Substring(entity.Offset, entity.Length), FontFamily = new FontFamily("Consolas")
                    });
                }
                else
                {
                    var local = span;

                    if (entity.Type is TextEntityTypeTextUrl textUrl)
                    {
                        var hyperlink = new Hyperlink {
                            NavigateUri = new Uri(textUrl.Url)
                        };
                        span.Inlines.Add(hyperlink);
                        local = hyperlink;
                    }
                    else if (entity.Type is TextEntityTypeUrl url)
                    {
                        var data      = text.Substring(entity.Offset, entity.Length);
                        var hyperlink = new Hyperlink {
                            NavigateUri = new Uri(data)
                        };
                        span.Inlines.Add(hyperlink);
                        local = hyperlink;
                    }
                    else if (entity.Type is TextEntityTypeMention mention)
                    {
                        var data      = text.Substring(entity.Offset + 1, entity.Length - 1);
                        var hyperlink = new Hyperlink {
                            NavigateUri = new Uri("https://t.me/" + data)
                        };
                        span.Inlines.Add(hyperlink);
                        local = hyperlink;
                    }

                    var run = new Run {
                        Text = text.Substring(entity.Offset, entity.Length)
                    };

                    if (entity.HasFlag(TextStyle.Bold))
                    {
                        run.FontWeight = FontWeights.SemiBold;
                    }
                    if (entity.HasFlag(TextStyle.Italic))
                    {
                        run.FontStyle |= FontStyle.Italic;
                    }
                    if (entity.HasFlag(TextStyle.Underline))
                    {
                        run.TextDecorations |= TextDecorations.Underline;
                    }
                    if (entity.HasFlag(TextStyle.Strikethrough))
                    {
                        run.TextDecorations |= TextDecorations.Strikethrough;
                    }

                    local.Inlines.Add(run);
                }

                previous = entity.Offset + entity.Length;
            }

            if (text.Length > previous)
            {
                span.Inlines.Add(new Run {
                    Text = text.Substring(previous)
                });
            }

            //var previous = 0;
            //var index = markdown.IndexOf("**");
            //var next = index > -1 ? markdown.IndexOf("**", index + 2) : -1;

            //while (index > -1 && next > -1)
            //{
            //    if (index - previous > 0)
            //    {
            //        sender.Inlines.Add(new Run { Text = markdown.Substring(previous, index - previous) });
            //    }

            //    sender.Inlines.Add(new Run { Text = markdown.Substring(index + 2, next - index - 2), FontWeight = FontWeights.SemiBold });

            //    previous = next + 2;
            //    index = markdown.IndexOf("**", next + 2);
            //    next = index > -1 ? markdown.IndexOf("**", index + 2) : -1;
            //}

            //if (markdown.Length - previous > 0)
            //{
            //    sender.Inlines.Add(new Run { Text = markdown.Substring(previous, markdown.Length - previous) });
            //}
        }
コード例 #7
0
 private void Merge(TextStyleRun run)
 {
     Flags |= run.Flags;
     Type ??= run.Type;
 }
コード例 #8
0
        public static IList <TextStyleRun> GetRuns(string text, IList <TextEntity> entities)
        {
            var runs         = new List <TextStyleRun>();
            var entitiesCopy = new List <TextEntity>(entities);

            //Collections.sort(entitiesCopy, (o1, o2)-> {
            //    if (o1.offset > o2.offset)
            //    {
            //        return 1;
            //    }
            //    else if (o1.offset < o2.offset)
            //    {
            //        return -1;
            //    }
            //    return 0;
            //});
            for (int a = 0, N = entitiesCopy.Count; a < N; a++)
            {
                var entity = entitiesCopy[a];
                if (entity.Length <= 0 || entity.Offset < 0 || entity.Offset >= text.Length)
                {
                    continue;
                }
                else if (entity.Offset + entity.Length > text.Length)
                {
                    entity.Length = text.Length - entity.Offset;
                }

                var newRun = new TextStyleRun();
                newRun.Start = entity.Offset;
                newRun.End   = newRun.Start + entity.Length;

                if (entity.Type is TextEntityTypeStrikethrough)
                {
                    newRun.Flags = TextStyle.Strikethrough;
                }
                else if (entity.Type is TextEntityTypeUnderline)
                {
                    newRun.Flags = TextStyle.Underline;
                }
                //else if (entity.Type is TextEntityTypeBlockQuote)
                //{
                //    newRun.Flags = TextStyle.BlockQuote;
                //}
                else if (entity.Type is TextEntityTypeBold)
                {
                    newRun.Flags = TextStyle.Bold;
                }
                else if (entity.Type is TextEntityTypeItalic)
                {
                    newRun.Flags = TextStyle.Italic;
                }
                else if (entity.Type is TextEntityTypeCode || entity.Type is TextEntityTypePre || entity.Type is TextEntityTypePreCode)
                {
                    newRun.Flags = TextStyle.Monospace;
                }
                else if (entity.Type is TextEntityTypeMentionName)
                {
                    newRun.Flags = TextStyle.Mention;
                    newRun.Type  = entity.Type;
                }
                else
                {
                    newRun.Flags = TextStyle.Url;
                    newRun.Type  = entity.Type;
                }

                for (int b = 0, N2 = runs.Count; b < N2; b++)
                {
                    TextStyleRun run = runs[b];

                    if (newRun.Start > run.Start)
                    {
                        if (newRun.Start >= run.End)
                        {
                            continue;
                        }

                        if (newRun.End < run.End)
                        {
                            TextStyleRun r = new TextStyleRun(newRun);
                            r.Merge(run);
                            b++;
                            N2++;
                            runs.Insert(b, r);

                            r       = new TextStyleRun(run);
                            r.Start = newRun.End;
                            b++;
                            N2++;
                            runs.Insert(b, r);
                        }
                        else if (newRun.End >= run.End)
                        {
                            TextStyleRun r = new TextStyleRun(newRun);
                            r.Merge(run);
                            r.End = run.End;
                            b++;
                            N2++;
                            runs.Insert(b, r);
                        }

                        int temp = newRun.Start;
                        newRun.Start = run.End;
                        run.End      = temp;
                    }
                    else
                    {
                        if (run.Start >= newRun.End)
                        {
                            continue;
                        }
                        int temp = run.Start;
                        if (newRun.End == run.End)
                        {
                            run.Merge(newRun);
                        }
                        else if (newRun.End < run.End)
                        {
                            TextStyleRun r = new TextStyleRun(run);
                            r.Merge(newRun);
                            r.End = newRun.End;
                            b++;
                            N2++;
                            runs.Insert(b, r);

                            run.Start = newRun.End;
                        }
                        else
                        {
                            TextStyleRun r = new TextStyleRun(newRun);
                            r.Start = run.End;
                            b++;
                            N2++;
                            runs.Insert(b, r);

                            run.Merge(newRun);
                        }
                        newRun.End = temp;
                    }
                }
                if (newRun.Start < newRun.End)
                {
                    runs.Add(newRun);
                }
            }
            return(runs);
        }
コード例 #9
0
 private void Replace(TextStyleRun run)
 {
     Flags = run.Flags;
     Type  = run.Type;
 }
コード例 #10
0
 private void Replace(TextStyleRun run)
 {
     Flags  = run.Flags;
     Entity = run.Entity;
 }