/// <summary>
 /// Creates a new RichTextColorizer instance.
 /// </summary>
 public RichTextColorizer(RichTextModel richTextModel)
 {
     if (richTextModel == null)
     {
         throw new ArgumentNullException("richTextModel");
     }
     this.richTextModel = richTextModel;
 }
 /// <summary>
 /// Creates a new RichTextModelWriter that inserts into document, starting at insertionOffset.
 /// </summary>
 public RichTextModelWriter(RichTextModel richTextModel, IDocument document, int insertionOffset)
     : base(new DocumentTextWriter(document, insertionOffset))
 {
     if (richTextModel == null)
     {
         throw new ArgumentNullException("richTextModel");
     }
     this.richTextModel      = richTextModel;
     this.documentTextWriter = (DocumentTextWriter)base.textWriter;
     currentColor            = richTextModel.GetHighlightingAt(Math.Max(0, insertionOffset - 1));
 }
        /// <summary>
        /// Creates a <see cref="RichTextModel"/> that stores the highlighting of this line.
        /// </summary>
        public RichTextModel ToRichTextModel()
        {
            var builder     = new RichTextModel();
            int startOffset = DocumentLine.Offset;

            foreach (HighlightedSection section in Sections)
            {
                builder.ApplyHighlighting(section.Offset - startOffset, section.Length, section.Color);
            }
            return(builder);
        }
示例#4
0
        /// <summary>
        /// Creates a substring of this rich text.
        /// </summary>
        public RichText Substring(int offset, int length)
        {
            if (offset == 0 && length == this.Length)
            {
                return(this);
            }
            string          newText = text.Substring(offset, length);
            RichTextModel   model   = ToRichTextModel();
            OffsetChangeMap map     = new OffsetChangeMap(2);

            map.Add(new OffsetChangeMapEntry(offset + length, text.Length - offset - length, 0));
            map.Add(new OffsetChangeMapEntry(0, offset, 0));
            model.UpdateOffsets(map);
            return(new RichText(newText, model));
        }
示例#5
0
        /// <summary>
        /// Concatenates the specified rich texts.
        /// </summary>
        public static RichText Concat(params RichText[] texts)
        {
            if (texts == null || texts.Length == 0)
            {
                return(Empty);
            }
            else if (texts.Length == 1)
            {
                return(texts[0]);
            }
            string        newText = string.Concat(texts.Select(txt => txt.text));
            RichTextModel model   = texts[0].ToRichTextModel();
            int           offset  = texts[0].Length;

            for (int i = 1; i < texts.Length; i++)
            {
                model.Append(offset, texts[i].stateChangeOffsets, texts[i].stateChanges);
                offset += texts[i].Length;
            }
            return(new RichText(newText, model));
        }
示例#6
0
 /// <summary>
 /// Creates a RichText instance with the given text and RichTextModel.
 /// </summary>
 /// <param name="text">
 /// The text to use in this RichText instance.
 /// </param>
 /// <param name="model">
 /// The model that contains the formatting to use for this RichText instance.
 /// <c>model.DocumentLength</c> should correspond to <c>text.Length</c>.
 /// This parameter may be null, in which case the RichText instance just holds plain text.
 /// </param>
 public RichText(string text, RichTextModel model = null)
 {
     if (text == null)
     {
         throw new ArgumentNullException("text");
     }
     this.text = text;
     if (model != null)
     {
         var sections = model.GetHighlightedSections(0, text.Length).ToArray();
         stateChangeOffsets = new int[sections.Length];
         stateChanges       = new HighlightingColor[sections.Length];
         for (int i = 0; i < sections.Length; i++)
         {
             stateChangeOffsets[i] = sections[i].Offset;
             stateChanges[i]       = sections[i].Color;
         }
     }
     else
     {
         stateChangeOffsets = new int[] { 0 };
         stateChanges       = new HighlightingColor[] { HighlightingColor.Empty };
     }
 }