Exemple #1
0
 void __check_sanity__()
 {
     if ('a' is T)
     {
         char ch;
         for (int i = _GapPos; i < _GapPos + _GapLen; i++)
         {
             ch = (char)_Data.GetValue(i);
             if (ch != INSANITY)
             {
                 __dump__("##SANITY CHECK##");
                 DebugUtl.Fail("SplitArray lost sanity!! (_Data[" + i + "] is " + (int)(char)_Data.GetValue(i) + ")");
             }
         }
     }
 }
        /// <summary>
        /// Determines where a screen line should be wrapped at.
        /// </summary>
        /// <param name="doc">The document currently rendering.</param>
        /// <param name="index">
        ///   The index of character which is to be drawn over the right edge of text area.
        /// </param>
        /// <returns>The index of the character which starts the next screen line.</returns>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="doc"/> is null.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        ///		<paramref name="index"/> is less than 0 or greater than length of the document.
        /// </exception>
        /// <seealso cref="IWordProc.HandleWordWrapping"/>
        public virtual int HandleWordWrapping(Document doc, int index)
        {
            if (doc == null)
            {
                throw new ArgumentNullException("doc");
            }
            if (index < 0 || doc.Length < index)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            int i;
            int startIndex;

            if (doc.Length <= index)
            {
                return(index);
            }

            // execute EOL hanging
            if (EnableEolHanging &&
                IsEolCode(doc, index))
            {
                if (doc[index] == '\r' &&
                    index + 1 < doc.Length && doc[index + 1] == '\n')
                {
                    index += 2;
                }
                else
                {
                    index += 1;
                }
                return(index);
            }

            // execute 'hanging'
            startIndex = index;
            for (i = 0; i < KinsokuDepth; i++)
            {
                // execute character hanging
                if (EnableCharacterHanging &&
                    index < doc.Length &&
                    0 <= Array.BinarySearch(CharsToBeHanged, doc[index]))
                {
                    index = doc.NextGraphemeClusterIndex(index);
                    continue;
                }

                break;
            }
            if (KinsokuDepth <= i || doc.Length <= index)
            {
                index = startIndex;                 // reached maximum depth of kinsoku-shori
            }

            // restrict characters starting a line
            startIndex = index;
            for (i = 0; i < KinsokuDepth; i++)
            {
                // execute line-head 'oidashi'
                if (EnableLineHeadRestriction &&
                    1 <= index &&
                    0 <= Array.BinarySearch(CharsForbiddenToStartLine, doc[index]))
                {
                    index = doc.PrevGraphemeClusterIndex(index);
                    continue;
                }

                break;
            }
            if (KinsokuDepth <= i || index <= 0)
            {
                index = startIndex;                 // reached maximum depth of kinsoku-shori
            }

            // wrap words
            if (EnableWordWrap &&
                index < doc.Length &&
                (IsAlphabet(doc, index) || IsDigit(doc, index)))
            {
                int wordBegin = doc.WordProc.PrevWordStart(doc, index);
                int wordEnd   = doc.WordProc.NextWordEnd(doc, index);
                if (wordBegin <= index && index < wordEnd)
                {
                    index = wordBegin;
                }
            }

            // restrict characters to end line
            startIndex = index;
            for (i = 0; i < KinsokuDepth; i++)
            {
                // execute line-end 'oidashi'
                if (EnableLineEndRestriction &&
                    1 <= index &&
                    0 <= Array.BinarySearch(CharsForbiddenToEndLine, doc[index - 1]))
                {
                    index = doc.PrevGraphemeClusterIndex(index);
                    continue;
                }

                break;
            }
            if (KinsokuDepth <= i || index <= 0)
            {
                index = startIndex;                 // reached maximum depth of kinsoku-shori
            }

            // correct index if its forbidden position
            if (doc.IsNotDividableIndex(index) && 0 < index)
            {
                DebugUtl.Assert(index + 1 < doc.Length);
                DebugUtl.Fail(String.Format(
                                  "kinsoku-shori resulted in forbidden index; between U+{0:X4} and U+{1:X4}", doc[index], doc[index + 1]
                                  ));
                index = doc.PrevGraphemeClusterIndex(index);
            }
            return(index);
        }