Exemplo n.º 1
0
        /// <summary>
        /// Creates a new instance of BaseHighlighter
        /// </summary>
        /// <param name="document">Document to highlight</param>
        public BaseHighlighter(ITextDocument document)
        {
            this.document = document;

            // Much faster to keep a worker range rather than to keep requesting one from
            // the document
            this.workerRange = document.GetRange(0, 0);

            this._highlights = new List<Tuple<Regex, Action<ITextCharacterFormat>>>();

            // Add highlighting rules
            AddHighlightRules();
        }
        private static void ForEachLinkInDocument(ITextDocument document, Action <ITextRange> action)
        {
            var range = document.GetRange(0, 0);

            range.SetIndex(TextRangeUnit.Character, -1, false);

            // Handle link at the very end of the document where GetIndex fails to detect
            range.Expand(TextRangeUnit.Link);
            if (!string.IsNullOrEmpty(range.Link))
            {
                action?.Invoke(range);
            }

            var nextIndex = range.GetIndex(TextRangeUnit.Link);

            while (nextIndex != 0 && nextIndex != 1)
            {
                range.Move(TextRangeUnit.Link, -1);

                var linkRange = range.GetClone();
                linkRange.Expand(TextRangeUnit.Link);

                // Adjacent links have the same index. Manually check each link with Collapse and Expand.
                var previousStart    = linkRange.StartPosition;
                var hasAdjacentToken = true;
                while (hasAdjacentToken)
                {
                    action?.Invoke(linkRange);

                    linkRange.Collapse(false);
                    linkRange.Expand(TextRangeUnit.Link);
                    hasAdjacentToken = !string.IsNullOrEmpty(linkRange.Link) && linkRange.StartPosition != previousStart;
                    previousStart    = linkRange.StartPosition;
                }

                nextIndex = range.GetIndex(TextRangeUnit.Link);
            }
        }
        public static void ToHtml(ITextDocument doc)
        {
            // Takes a RichEditBox control and returns a
            // simple HTML-formatted version of its contents
            string     strHTML, strFont, strColour, strBold, strFntName;
            float      shtSize;
            int        lngOriginalStart, lngOriginalLength;
            int        intCount = 0;
            ITextRange tr       = doc.GetRange(0, 9999999);

            // If nothing in the box, exit
            if (tr.Length == 0)
            {
                //App.Current.Exit();
            }

            // Store original selections, then select first character
            lngOriginalStart  = 0;
            lngOriginalLength = tr.Length;
            tr.SetRange(0, 1);

            // Add HTML header
            strHTML = "<html>";

            // Set up initial parameters
            strColour  = tr.CharacterFormat.ForegroundColor.ToString();
            strFont    = tr.CharacterFormat.FontStyle.ToString();
            shtSize    = tr.CharacterFormat.Size;
            strBold    = tr.CharacterFormat.Bold.ToString();
            strFntName = tr.CharacterFormat.Name;
            Debug.WriteLine("Colour: " + strColour);

            // Include first 'style' parameters in the HTML
            strHTML += "<span style=\"font-family:" + strFntName + "; font-size: " + shtSize + "pt; color: #" + strColour.Substring(3) + "\">";

            // Include bold tag, if required
            if (strBold.ToLower() == "on")
            {
                strHTML += "<b>";
            }
            // Include italic tag, if required
            if (strFont.ToLower() == "italic")
            {
                strHTML += "<i>";
            }

            // Finally, add our first character
            strHTML += tr.Character;

            // Loop around all remaining characters
            for (intCount = 2; intCount < lngOriginalLength; intCount++)
            {
                // Select current character
                tr.SetRange(intCount - 1, intCount + 1);

                //     If this is a line break, add HTML tag
                if (tr.Character == Convert.ToChar(13))
                {
                    strHTML += "<br />";
                }

                //    ' Check/implement any changes in style
                if (tr.CharacterFormat.ForegroundColor.ToString() != strColour || tr.CharacterFormat.Name != strFntName || tr.CharacterFormat.Size != shtSize)
                {
                    strHTML += "</span><span style=\"font-family: " + tr.CharacterFormat.Name + "; font size: " + tr.CharacterFormat.Size + "pt; color: #" + tr.CharacterFormat.ForegroundColor.ToString().Substring(3) + "\">";
                }

                //     Check for bold changes
                if (tr.CharacterFormat.Bold.ToString().ToLower() != strBold.ToLower())
                {
                    if (tr.CharacterFormat.Bold.ToString().ToLower() != "on")
                    {
                        strHTML += "</b>";
                    }
                    else
                    {
                        strHTML += "<b>";
                    }
                }

                //    Check for italic changes
                if (tr.CharacterFormat.FontStyle.ToString().ToLower() != strFont.ToLower())
                {
                    if (tr.CharacterFormat.FontStyle.ToString().ToLower() != "italic")
                    {
                        strHTML += "</i>";
                    }
                    else
                    {
                        strHTML += "<i>";
                    }
                }

                //    ' Add the actual character
                strHTML += tr.Character;

                //    ' Update variables with current style
                strColour  = tr.CharacterFormat.ForegroundColor.ToString();
                strFont    = tr.CharacterFormat.FontStyle.ToString();
                shtSize    = tr.CharacterFormat.Size;
                strFntName = tr.CharacterFormat.Name;
                strBold    = tr.CharacterFormat.Bold.ToString();
            }

            // Close off any open bold/italic tags
            if (strBold == "on")
            {
                strHTML += "";
            }
            if (strFont.ToLower() == "italic")
            {
                strHTML += "";
            }

            // Terminate outstanding HTML tags
            strHTML += "</span></html>";

            //' Restore original RichTextBox selection
            tr.SetRange(lngOriginalStart, lngOriginalLength);

            doc.SetText(TextSetOptions.FormatRtf, strHTML);
        }
        public static void SetRangeColor(this ITextDocument document, int start, int end, Color color)
        {
            ITextRange range = document.GetRange(start, end);

            range.CharacterFormat.ForegroundColor = color;
        }
 public static ITextRange GetRangeAt(this ITextDocument document, int position)
 {
     return(document.GetRange(position, position));
 }