コード例 #1
0
        public string HighlightAsRtf(Dictionary <string, Color> colorDictionary, Color defaultColor, string rtfTemplate)
        {
            var highlightJobs         = ApplyDescriptors(SourceCode, 0, Descriptors);
            var rb                    = new RTFBuilder(rtfTemplate);
            var highlightedTextLength = ConvertHighlightJobsToRichTextFormat(rb, highlightJobs, 0, colorDictionary, defaultColor);
            var leftOver              = SourceCode.Length - highlightedTextLength;

            if (leftOver > 0)
            {
                rb.Append(SourceCode.Substring(highlightedTextLength, leftOver), defaultColor);
            }
            return(rb.ToString());
        }
コード例 #2
0
        // returns length of characters highlighted.
        private int ConvertHighlightJobsToRichTextFormat(RTFBuilder builder, IEnumerable <HighlightJob> jobs, int startIndex, Dictionary <string, Color> colorDictionary, Color parentColor)
        {
            var currentIndex = startIndex;

            foreach (var job in jobs)
            {
                CancellationToken?.ThrowIfCancellationRequested();
                if (job.StartIndex != currentIndex)
                {
                    builder.Append(SourceCode.Substring(currentIndex, job.StartIndex - currentIndex), parentColor);
                    currentIndex = job.StartIndex;
                }

                if (!colorDictionary.TryGetValue(job.ColorKey, out var color))
                {
                    color = parentColor;
                }

                if (job.ChildJobs != null)
                {
                    currentIndex += ConvertHighlightJobsToRichTextFormat(builder, job.ChildJobs, currentIndex, colorDictionary, color);
                    var leftOver = job.StartIndex + job.Length - currentIndex;
                    if (leftOver > 0)
                    {
                        builder.Append(SourceCode.Substring(currentIndex, leftOver), color);
                        currentIndex += leftOver;
                    }
                }
                else
                {
                    builder.Append(SourceCode.Substring(currentIndex, job.Length), color);
                    currentIndex += job.Length;
                }
            }
            return(currentIndex - startIndex);
        }