public static RuleText ValidateDefaultRule(this RichTextBlock richTextBlock, RuleText currentRule)
 {
     if (currentRule == null)
     {
         currentRule             = new RuleText();
         currentRule.IsUnMatched = true;
     }
     if (!currentRule.FontStyleSet)
     {
         currentRule.FontStyle = richTextBlock.FontStyle;
     }
     if (!currentRule.FontWeightSet)
     {
         currentRule.FontWeight = richTextBlock.FontWeight;
     }
     if (currentRule.FontFamily == null)
     {
         currentRule.FontFamily = richTextBlock.FontFamily;
     }
     if (!currentRule.FontStretchSet)
     {
         currentRule.FontStretch = richTextBlock.FontStretch;
     }
     if (currentRule.Background == null)
     {
         currentRule.Background = richTextBlock.Background;
     }
     if (currentRule.Foreground == null)
     {
         currentRule.Foreground = richTextBlock.Foreground;
     }
     if (!currentRule.FontSizeSet)
     {
         currentRule.FontSize = richTextBlock.FontSize;
     }
     return(currentRule);
 }
        public static double[] GetCharWidths(this FormattedText formattedText, RichTextBlock richTextBlock, RuleText rule)
        {
            richTextBlock.ValidateDefaultRule(rule);
            var text   = formattedText.Text;
            var result = new double[text.Length];

            for (int i = 0; i < text.Length; i++)
            {
                var ft = new FormattedText(text[i].ToString(), CultureInfo.CurrentUICulture, richTextBlock.FlowDirection, new Typeface(rule.FontFamily, rule.FontStyle, rule.FontWeight, rule.FontStretch), rule.FontSize, rule.Foreground);
                result[i] = ft.Width;
            }
            return(result);
        }
 public static FormattedText BuildFormattedText(this RichTextBlock richTextBlock, string value, RuleText currentRule = null)
 {
     if (currentRule == null)
     {
         currentRule = richTextBlock.BuildDefaultRule();
     }
     else
     {
         richTextBlock.ValidateDefaultRule(currentRule);
     }
     return(new FormattedText(value, CultureInfo.InvariantCulture,
                              richTextBlock.FlowDirection,
                              new Typeface(currentRule.FontFamily, currentRule.FontStyle,
                                           currentRule.FontWeight, currentRule.FontStretch),
                              currentRule.FontSize, currentRule.Foreground));
 }
        public List <RuleText> ParserRule(ref string text, Func <string, List <RuleText> > indentParser)
        {
            var start = Rule.Start;
            var end   = Rule.End;

            BackStart = Rule.Start;
            BackEnd   = Rule.End;
            if (!string.IsNullOrEmpty(start))
            {
                RegexInvalidateChar.ForEach(o => start = start.Replace(o, @"\" + o));
            }
            if (!string.IsNullOrEmpty(end))
            {
                RegexInvalidateChar.ForEach(o => end = end.Replace(o, @"\" + o));
            }
            string match = "";

            if (Rule.Regex != null)
            {
                match = Rule.Regex;
            }
            else
            {
                match = $"(?<={start}).*?(?={end})";
            }
            var r = new List <RuleText>();

            try
            {
                if (Regex.IsMatch(text, match))
                {
                    var result = Regex.Matches(text, match);
                    foreach (Match rc in result)
                    {
                        var rt = new RuleText();
                        rt.IsUnMatched    = false;
                        rt.OffsetWithMark = rc.Index - (BackStart == null ? 0 : BackStart.Length);
                        rt.FontStyle      = Rule.FontStyle;
                        rt.FontSize       = Rule.FontSize;
                        rt.Foreground     = Rule.Foreground;
                        rt.Background     = Rule.Background;
                        rt.Value          = rc.Value;
                        var tempValue = rc.Value.TrimStart(SpaceChar);
                        rt.Offset         = rc.Index + rc.Value.Length - tempValue.Length;
                        rt.Length         = rc.Length;
                        rt.LengthWithMark = rc.Index + rc.Length + (BackEnd == null ? 0 : BackEnd.Length);
                        if (rc.Value != text)
                        {
                            var rule = indentParser(rc.Value);
                            if (rule.Any())
                            {
                                rt.Childs = rule;
                            }
                        }

                        r.Add(rt);
                        text = text.Substring(0, rt.OffsetWithMark) +
                               "".PadLeft(rt.LengthWithMark - rt.OffsetWithMark, SpaceChar) +
                               text.Substring(rt.LengthWithMark, text.Length - rt.LengthWithMark);
                    }
                }
            }
            catch
            {
            }


            return(r);
        }