Exemple #1
0
        /// <summary>
        /// Performs syntax highlighting with the loaded Highlighter.
        /// </summary>
        public void Highlight()
        {
            if (HighlighterEnabled && Highlighter != null && !highlighting)
            {
                highlighting = true;
                int    selectionStart = SelectionStart;
                int    selectionLen   = SelectionLength;
                string rtf            = Rtf;
                rtf = Regex.Replace(rtf, @"\cf\d+ ?", string.Empty);
                // Get the end of the header so we don't accidental format it
                int headerEnd = GetRtfHeaderEnd();
                var table     = new RichColorTable();

                // Iterate highlighting profiles and apply them
                foreach (var profile in Highlighter.Profiles)
                {
                    var             regex       = new Regex(profile.Pattern, profile.RegexOptions);
                    MatchCollection MC          = regex.Matches(rtf, headerEnd);
                    bool            usingGroups = regex.GetGroupNames().Contains("Value");
                    if (MC.Count <= 0)
                    {
                        continue;
                    }

                    table.AddColors(profile.Color);
                    string colorID = @"\cf" + table.Colors.Count() + " ";

                    // Do this in reverse so the size of the text does not effect the insertions
                    for (int i = MC.Count - 1; i >= 0; i--)
                    {
                        int start  = usingGroups ? MC[i].Groups["Value"].Index : MC[i].Index;
                        int length = usingGroups ? MC[i].Groups["Value"].Length : MC[i].Length;
                        // So we dont get trailing color
                        var parentCol = GetParentColor(start, start - headerEnd, rtf);
                        rtf = rtf.Insert(start + length, parentCol);
                        // Insert color tag
                        rtf = rtf.Insert(start, colorID);
                    }
                }

                rtf             = table.InjectTable(rtf);
                Rtf             = rtf;
                SelectionStart  = selectionStart;
                SelectionLength = selectionLen;
                highlighting    = false;
            }
        }
        /// <summary>
        /// Gets the color table defined in rich text format.
        /// </summary>
        /// <returns>An empty table, if no table found.</returns>
        /// <param name="rtf">The RTF of a RichTextBox.</param>
        public static RichColorTable ExtractColorTable(string rtf)
        {
            string tableStr = Regex.Match(rtf, TABLE_PATTERN).Value;

            if (String.IsNullOrEmpty(tableStr))
            {
                return(Empty);
            }
            MatchCollection MC    = Regex.Matches(tableStr, TABLE_ELEMENT_PATTERN);
            var             table = new RichColorTable();

            foreach (Match match in MC)
            {
                int R = int.Parse(match.Groups["R"].Value);
                int G = int.Parse(match.Groups["G"].Value);
                int B = int.Parse(match.Groups["B"].Value);
                table.AddColors(Color.FromArgb(R, G, B));
            }

            return(table);
        }
 protected bool Equals(RichColorTable other)
 {
     return(Equals(colors, other.colors));
 }