⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtftext.cs

📁 Gibphone is CSharp Program, it can tell you how to design p2p chat.
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using System.Drawing;

namespace GPCore.GibphoneControls
{
    /// <summary>
    /// Allows for conversion from RTF to plain text or HTML.
    /// </summary>
    public class RtfText
    {
        #region RtfText Members
        /// <summary>
        /// Gets the plain text equivalent of the rich text.
        /// </summary>
        public string Text
        {
            get { return gettextEx.Replace(this.rtf, ""); }
        }

        private string rtf = "";
        /// <summary>
        /// Gets or sets the rich text.
        /// </summary>
        public string Rtf
        {
            set { this.rtf = value; }
            get { return this.rtf; }
        }

        /// <summary>
        /// Gets the HTML equivalent of the rich text.
        /// </summary>
        
        public string Html
        {
            get { return this.toHtml(this.rtf); }
        }
        #endregion

        #region Regexes and String Constants
        private static Regex getfonttableEx = new Regex(@"\{\\fonttbl.*\}\}", RegexOptions.Singleline);
        private static Regex getcolortableEx = new Regex(@"\{\\colortbl.*;\}", RegexOptions.Singleline);
        private static Regex gettextEx = new Regex(@"[\{\\]+[\w\s;\}]*\s?", RegexOptions.Singleline);
        private static Regex getfonttableentriesEx = new Regex(@"\{\\f(?<FontNumber>\d+)[\w*\\]*\s?(?<FontName>(?:\s?\w*)*);\}", RegexOptions.Singleline);
        private static Regex getfontsizesEx = new Regex(@"\\fs(?<FontSize>\d+)\s?", RegexOptions.Singleline);
        private static Regex getcolortableentriesEx = new Regex(@"\\red(?<Red>\d+)\\green(?<Green>\d+)\\blue(?<Blue>\d+);", RegexOptions.Singleline);
        private static Regex getbasicformattingEx = new Regex(@"\\b0|\\b|\\ulnone|\\ul0|\\ul|\\i0|\\i", RegexOptions.Singleline);
        #endregion

        /// <summary>
        /// Creates a new RtfText
        /// </summary>
        /// <param name="rtf">The RTF text that you wish to translate</param>
        public RtfText(string rtf)
        {
            this.rtf = rtf;
        }

        private string toHtml(string input)
        {
			input = input.Replace("\\\\", "&slash;");
            input = input.Replace("<", "&lt;");
            input = input.Replace(">", "&gt;");
            input = input.Replace('"'.ToString(),"&quot;");
            input = this.replaceColorCodes(input);
            input = this.replaceFontSizeCodes(input);
            input = this.replaceFontCodes(input);
            input = this.replaceBasicFormatting(input);
			input = gettextEx.Replace(input, "");
			input = input.Replace("&slash;", "\\\\");
            return input;
        }

        private string replaceBasicFormatting(string input)
        {
			Regex regex = new Regex(@"\\ulnone\s?");
            input = regex.Replace(input, "</u>");
			regex = new Regex(@"\\ul0\s?");
			input = regex.Replace(input, "</u>");
			regex = new Regex(@"\\ul\s?");
            input = regex.Replace(input, "<u>");
			regex = new Regex(@"\\i0\s?");
            input = regex.Replace(input, "</i>");
			regex = new Regex(@"\\i\s?");
            input = regex.Replace(input, "<i>");
			regex = new Regex(@"\\b0\s?");
            input = regex.Replace(input, "</b>");
			regex = new Regex(@"\\strike0\s?");
			input = regex.Replace(input, "</strike>");
			regex = new Regex(@"\\strike\s?");
			input = regex.Replace(input, "<strike>");
			regex = new Regex(@"\\b\s?");
            input = regex.Replace(input, "<b>");
			regex = new Regex(@"\\line\s?");
            input = regex.Replace(input, "<br>");
            return input;
        }

        private string replaceColorCodes(string input)
        {
            input = input.Replace("\\cf0", "<font color=#000000>");
            int count = 1;

            MatchCollection mc = getcolortableentriesEx.Matches(input);
            input = getcolortableentriesEx.Replace(input, "");
            foreach (Match m in mc)
            {
                Color c = Color.FromArgb
                        (Convert.ToInt32(m.Groups["Red"].Value),
                        Convert.ToInt32(m.Groups["Green"].Value),
                        Convert.ToInt32(m.Groups["Blue"].Value));
                string tag = String.Format("<font color=\"{0}\">", ColorTranslator.ToHtml(c));
                string btag = String.Format("<font back=\"{0}\">", ColorTranslator.ToHtml(c));
				string scount =count.ToString();
				Regex regex = new Regex(@"\\cf" + scount + "\\s?");
                input = regex.Replace(input, tag);
				try
				{
					regex = new Regex(@"\\highlight" + scount + "\\s?");
				}
				catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); }
				//scount = @"\highlight" + scount + " ";
                //input = input.Replace(scount, btag);
				//scount = scount.Substring(0,scount.Length - 1) ;
				//input = input.Replace(scount, btag); 
				input = regex.Replace(input, btag);
				count++;
            }
            return input;
        }

        private string replaceFontSizeCodes(string input)
        {
            //MatchCollection mc = getfontsizesEx.Matches(input);
            //input = getfontsizesEx.Replace(input, "");
            input = getfontsizesEx.Replace(input, delegate(Match m)
            {
                return  String.Format("<font ptsize=\"{0}\">", (Convert.ToInt32(m.Groups["FontSize"].Value) / 2));
            });
            return input;
        }

        private string replaceFontCodes(string input)
        {
            MatchCollection mc = getfonttableentriesEx.Matches(input);
            input = getfonttableEx.Replace(input, "");
            foreach (Match m in mc)
            {
                string number = m.Groups["FontNumber"].Value;
                string face = m.Groups["FontName"].Value;

                string tag = String.Format("<font face=\"{0}\">", face);
                input = input.Replace("\\f" + number, tag);
            }
            return input;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -