Example #1
0
        /// <summary>
        ///     Returns a <see cref="StyleRun" /> enumerable representing the individual character styling of the annotation text.
        /// </summary>
        /// <returns>
        ///     A <see cref="StyleRun" /> enumerable representing the individual character styling,
        ///     where the <see cref="StyleRun.Length" /> property of each run represents the number
        ///     of characters the run spans.
        /// </returns>
        public virtual IEnumerable <StyleRun> GetStyles()
        {
            CheckInvalid();

            // We need to translate the array Scintilla gives us representing the style of each text
            // byte into a list of style runs. Our run lengths, however, are measured in characters,
            // not bytes, so we need to also read the annotation text and adjust as necessary when we find
            // characters that span more than one byte.

            int length = _scintilla.DirectMessage(NativeMethods.SCI_ANNOTATIONGETTEXT, new IntPtr(_lineIndex), IntPtr.Zero).ToInt32();

            byte[] textBuffer   = new byte[length];
            byte[] stylesBuffer = new byte[length];

            unsafe
            {
                fixed(byte *bp = textBuffer)
                _scintilla.DirectMessage(NativeMethods.SCI_ANNOTATIONGETTEXT, new IntPtr(_lineIndex), new IntPtr(bp)).ToInt32();

                fixed(byte *bp = stylesBuffer)
                _scintilla.DirectMessage(NativeMethods.SCI_ANNOTATIONGETSTYLES, new IntPtr(_lineIndex), new IntPtr(bp)).ToInt32();
            }

            List <StyleRun> styles  = new List <StyleRun>();
            Decoder         decoder = _scintilla.Encoding.GetDecoder();
            StyleRun        sr      = new StyleRun()
            {
                Style = -1
            };
            int index = 0;
            int count = 1;

            while (index < stylesBuffer.Length)
            {
                if (sr.Style != stylesBuffer[index])
                {
                    // A new style has been encountered. Save the last one
                    // to the list we're building and start tracking a new one
                    if (sr.Length > 0)
                    {
                        styles.Add(sr);
                    }

                    sr       = new StyleRun();
                    sr.Style = stylesBuffer[index];
                }

                // At the end of this loop, the 'count' variable will tell us
                // how many bytes there are for one character.
                while (decoder.GetCharCount(textBuffer, index, count) != 1)
                {
                    count++;
                }

                sr.Length++;
                index += count;
                count  = 1;
            }

            // Add the last style run
            styles.Add(sr);

            return(styles.ToArray());
        }
Example #2
0
        /// <summary>
        ///     Returns a <see cref="StyleRun" /> enumerable representing the individual character styling of the annotation text.
        /// </summary>
        /// <returns>
        ///     A <see cref="StyleRun" /> enumerable representing the individual character styling,
        ///     where the <see cref="StyleRun.Length" /> property of each run represents the number
        ///     of characters the run spans.
        /// </returns>
        public virtual IEnumerable<StyleRun> GetStyles()
        {
            CheckInvalid();

            // We need to translate the array Scintilla gives us representing the style of each text
            // byte into a list of style runs. Our run lengths, however, are measured in characters,
            // not bytes, so we need to also read the annotation text and adjust as necessary when we find
            // characters that span more than one byte.

            int length = _scintilla.DirectMessage(NativeMethods.SCI_ANNOTATIONGETTEXT, new IntPtr(_lineIndex), IntPtr.Zero).ToInt32();
            byte[] textBuffer = new byte[length];
            byte[] stylesBuffer = new byte[length];

            unsafe
            {
                fixed (byte* bp = textBuffer)
                    _scintilla.DirectMessage(NativeMethods.SCI_ANNOTATIONGETTEXT, new IntPtr(_lineIndex), new IntPtr(bp)).ToInt32();
                fixed (byte* bp = stylesBuffer)
                    _scintilla.DirectMessage(NativeMethods.SCI_ANNOTATIONGETSTYLES, new IntPtr(_lineIndex), new IntPtr(bp)).ToInt32();
            }

            List<StyleRun> styles = new List<StyleRun>();
            Decoder decoder = _scintilla.Encoding.GetDecoder();
            StyleRun sr = new StyleRun() { Style = -1 };
            int index = 0;
            int count = 1;

            while (index < stylesBuffer.Length)
            {
                if (sr.Style != stylesBuffer[index])
                {
                    // A new style has been encountered. Save the last one
                    // to the list we're building and start tracking a new one
                    if (sr.Length > 0)
                        styles.Add(sr);

                    sr = new StyleRun();
                    sr.Style = stylesBuffer[index];
                }

                // At the end of this loop, the 'count' variable will tell us
                // how many bytes there are for one character.
                while (decoder.GetCharCount(textBuffer, index, count) != 1)
                    count++;

                sr.Length++;
                index += count;
                count = 1;
            }

            // Add the last style run
            styles.Add(sr);

            return styles.ToArray();
        }