public string GetHtml(Range r)
        {
            this.tb = r.tb;
            Dictionary <StyleIndex, object> styles = new Dictionary <StyleIndex, object>();
            StringBuilder sb             = new StringBuilder();
            StringBuilder tempSB         = new StringBuilder();
            StyleIndex    currentStyleId = StyleIndex.None;

            r.Normalize();
            int currentLine = r.Start.iLine;

            styles[currentStyleId] = null;
            //
            if (UseOriginalFont)
            {
                sb.AppendFormat("<font style=\"font-family: {0}, monospace; font-size: {1}pt; line-height: {2}px;\">",
                                r.tb.Font.Name, r.tb.Font.SizeInPoints, r.tb.CharHeight);
            }

            //
            if (IncludeLineNumbers)
            {
                tempSB.AppendFormat("<span class=lineNumber>{0}</span>  ", currentLine + 1);
            }
            //
            bool hasNonSpace = false;

            foreach (Place p in r)
            {
                InternalChar c = r.tb[p.iLine][p.iChar];
                if (c.style != currentStyleId)
                {
                    Flush(sb, tempSB, currentStyleId);
                    currentStyleId         = c.style;
                    styles[currentStyleId] = null;
                }

                if (p.iLine != currentLine)
                {
                    for (int i = currentLine; i < p.iLine; i++)
                    {
                        tempSB.Append(UseBr ? "<br>" : "\r\n");
                        if (IncludeLineNumbers)
                        {
                            tempSB.AppendFormat("<span class=lineNumber>{0}</span>  ", i + 2);
                        }
                    }
                    currentLine = p.iLine;
                    hasNonSpace = false;
                }
                switch (c.c)
                {
                case ' ':
                    if ((hasNonSpace || !UseForwardNbsp) && !UseNbsp)
                    {
                        goto default;
                    }

                    tempSB.Append("&nbsp;");
                    break;

                case '<':
                    tempSB.Append("&lt;");
                    break;

                case '>':
                    tempSB.Append("&gt;");
                    break;

                case '&':
                    tempSB.Append("&amp;");
                    break;

                default:
                    hasNonSpace = true;
                    tempSB.Append(c.c);
                    break;
                }
            }
            Flush(sb, tempSB, currentStyleId);

            if (UseOriginalFont)
            {
                sb.Append("</font>");
            }

            //build styles
            if (UseStyleTag)
            {
                tempSB.Length = 0;
                tempSB.Append("<style type=\"text/css\">");
                foreach (var styleId in styles.Keys)
                {
                    tempSB.AppendFormat(".fctb{0}{{ {1} }}\r\n", GetStyleName(styleId), GetCss(styleId));
                }
                tempSB.Append("</style>");

                sb.Insert(0, tempSB.ToString());
            }

            if (IncludeLineNumbers)
            {
                sb.Insert(0, LineNumbersCSS);
            }

            return(sb.ToString());
        }
        public string GetRtf(Range r)
        {
            this.tb = r.tb;
            var styles         = new Dictionary <StyleIndex, object>();
            var sb             = new StringBuilder();
            var tempSB         = new StringBuilder();
            var currentStyleId = StyleIndex.None;

            r.Normalize();
            int currentLine = r.Start.iLine;

            styles[currentStyleId] = null;
            colourTable.Clear();
            //
            var lineNumberColor = GetColourTableNumber(r.tb.LineNumberColour);

            if (IncludeLineNumbers)
            {
                tempSB.AppendFormat(@"{{\cf{1} {0}}}\tab", currentLine + 1, lineNumberColor);
            }
            //
            foreach (Place p in r)
            {
                InternalChar c = r.tb[p.iLine][p.iChar];
                if (c.style != currentStyleId)
                {
                    Flush(sb, tempSB, currentStyleId);
                    currentStyleId         = c.style;
                    styles[currentStyleId] = null;
                }

                if (p.iLine != currentLine)
                {
                    for (int i = currentLine; i < p.iLine; i++)
                    {
                        tempSB.AppendLine(@"\line");
                        if (IncludeLineNumbers)
                        {
                            tempSB.AppendFormat(@"{{\cf{1} {0}}}\tab", i + 2, lineNumberColor);
                        }
                    }
                    currentLine = p.iLine;
                }
                switch (c.c)
                {
                case '\\':
                    tempSB.Append(@"\\");
                    break;

                case '{':
                    tempSB.Append(@"\{");
                    break;

                case '}':
                    tempSB.Append(@"\}");
                    break;

                default:
                    var ch   = c.c;
                    var code = (int)ch;
                    if (code < 128)
                    {
                        tempSB.Append(c.c);
                    }
                    else
                    {
                        tempSB.AppendFormat(@"{{\u{0}}}", code);
                    }
                    break;
                }
            }
            Flush(sb, tempSB, currentStyleId);

            //build color table
            var list = new SortedList <int, Color>();

            foreach (var pair in colourTable)
            {
                list.Add(pair.Value, pair.Key);
            }

            tempSB.Length = 0;
            tempSB.AppendFormat(@"{{\colortbl;");

            foreach (var pair in list)
            {
                tempSB.Append(GetColourAsString(pair.Value) + ";");
            }
            tempSB.AppendLine("}");

            //
            if (UseOriginalFont)
            {
                sb.Insert(0, string.Format(@"{{\fonttbl{{\f0\fmodern {0};}}}}{{\fs{1} ",
                                           tb.Font.Name, (int)(2 * tb.Font.SizeInPoints), tb.CharHeight));
                sb.AppendLine(@"}");
            }

            sb.Insert(0, tempSB.ToString());

            sb.Insert(0, @"{\rtf1\ud\deff0");
            sb.AppendLine(@"}");

            return(sb.ToString());
        }