Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

RichText Builder (StringBuilder for RTF)

, 12 Nov 2008
RichText Builder - use in place of StringBuilder to output RTF.
namespace RTF
{
    using System.Drawing;
    using System.IO;
    using System.Text;

    public class RTFBuilderParser
    {
        #region Fields

        private const int Eof = -1;
        private readonly RTFBuilderbase _builder;

        #endregion

        #region Constructor

        public RTFBuilderParser(RTFBuilderbase builder)
        {
            this._builder = builder;
        }

        #endregion

        #region Public Methods

        public void AppendRTF(string rtf)
        {
            StringReader sr = new StringReader(rtf);
            int c = sr.Peek();
            if (c == '\\')
            {
                sr.Read();
            }

            using (this._builder.FormatLock())
            {
                while (c != Eof)
                {
                    switch (c)
                    {
                        case '\\':
                            c = this.TokenAction(sr);
                            break;
                        default:
                            c = this.ReadString(sr, c);
                            break;
                    }
                }
            }
        }

        #endregion

        #region Methods

        private void Action(string rtfcode)
        {
            switch (rtfcode)
            {
                case "b":
                    this._builder.FontStyleOR(FontStyle.Bold);
                    break;
                case "par":
                case "line":
                    this._builder.AppendLine();
                    break;
                case "bullet":
                    this._builder.Append('\u25A0');
                    break;
                case "lquote":
                case "rquote":
                    this._builder.Append("\'");
                    break;
                default:
                    break;
            }
        }

        private void Action(string rtfcode, int rtfparam)
        {
            switch (rtfcode)
            {
                case "b":
                    this._builder.FontStyleNOT(FontStyle.Bold);
                    break;
                case "fs":
                    this._builder.FontSize(rtfparam);
                    break;
                case "cf":
                    this._builder.ForeColor(rtfparam);
                    break;
                case "f":
                    this._builder.Font(rtfparam);
                    break;
                default:
                    break;
            }
        }

        private int ReadString(StringReader sr, int c)
        {
            StringBuilder sb = new StringBuilder();

            while (c != Eof)
            {
                if (c == '\\')
                {
                    int cc = sr.Peek();
                    if (cc == '\\' || cc == '}' || cc == '{')
                    {
                        c = cc;
                    }
                    else
                    {
                        break;
                    }
                }
                sb.Append((char) c);
                c = sr.Read();
                while (c == '\r' || c == '\n' || c == '\t' || c == '\0')
                {
                    c = sr.Read();
                }
            }

            if (sb.Length > 0)
            {
                this._builder.Append(sb.ToString());
            }
            return c;
        }


        /// <summary>
        /// runs when / found
        /// </summary>
        /// <param name="sr"></param>
        /// <returns></returns>
        private int TokenAction(StringReader sr)
        {
            StringBuilder sb = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            int c = sr.Read();
            while (char.IsLetter((char) c))
            {
                sb.Append((char) c);
                c = sr.Read();
            }
            while (char.IsDigit((char) c))
            {
                sb2.Append((char) c);
                c = sr.Read();
            }
            //if (c == ' ')
            //{
            //    c = sr.Read();
            //}
            if (sb.Length > 0)
            {
                if (sb2.Length > 0)
                {
                    int num;
                    if (int.TryParse(sb2.ToString(), out num))
                    {
                        this.Action(sb.ToString(), num);
                        return c;
                    }
                }
                this.Action(sb.ToString());
            }

            return c;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

seeblunt

Australia Australia
Interested in financial math and programming theory in general. Working on medical applications in spare time. Happy to get feedback.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 12 Nov 2008
Article Copyright 2008 by seeblunt
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid