Example #1
0
        /////////////////////////////////////////////////////////////////////////////////

        public static string Join(string delimiter, TaggedTextCollection lines)
        {
            StringBuilder sb = new StringBuilder();

            foreach (TaggedText t in lines)
            {
                if (sb.Length == 0)
                {
                    sb.Append(t.Text);
                }
                else
                {
                    sb.Append(delimiter).Append(t.Text);
                }
            }

            return(sb.ToString());
        }
Example #2
0
        /////////////////////////////////////////////////////////////////////////////////

        public static TaggedTextCollection SplitTextInLines(string text,
                                                            ref int maxLineWidth)
        {
            int lineWrapAt = maxLineWidth;

            maxLineWidth = 0;
            TaggedTextCollection lines = new TaggedTextCollection();

            if (text == null)
            {
                return(lines);
            }

            // Add CRLF delimitted chunks of text to lines collection.
            // 'start' points to beginning of next line
            // 'end' points to next CRLF or end of text

            const string delimiter = "\n";
            int          start     = 0;

            while (start < text.Length)
            {
                // Find next CRLF (if any)
                //
                int end       = text.IndexOf(delimiter, start);
                int skipChars = delimiter.Length;

                // Calculate length of the chunk between line 'start' and next CRLF
                // (or text length if CRLF is not found).
                //
                int len = end < 0 ? text.Length - start : end - start;

                if (lineWrapAt > 0 && len > lineWrapAt)
                {
                    skipChars = 0;

                    // If line should wrap, find space between words
                    //
                    for (len = lineWrapAt; len > 0; --len)
                    {
                        char c = text[start + len];

                        // Skip all (found) spaces (between words) backwards
                        //
                        if (char.IsWhiteSpace(c))
                        {
                            ++skipChars;

                            while (len > 0 &&
                                   char.IsWhiteSpace(text[start + len - 1]))
                            {
                                ++skipChars;
                                --len;
                            }
                        }

                        // If found space between words or other delimiter character
                        //
                        if (skipChars > 0 || c == '\\' || c == '/')
                        {
                            break;
                        }
                    }

                    if (len <= 0)         // not found any space between words
                    {
                        len = lineWrapAt; // reset to original 'wrap at' position
                    }
                }

                maxLineWidth = Math.Max(len, maxLineWidth);

                // Add chunk to lines colletion
                //
                lines.Add(TaggedText.ClearFromControlCharacters(
                              text.Substring(start, len)));

                // Move 'start' at the beginning of next line (i.e. skip CRLF)
                //
                start += len + skipChars;
            }

            return(lines);
        }