Alsing.Windows.Forms.SyntaxBox.EditViewControl.CopyAsRTF C# (CSharp) Method

CopyAsRTF() protected method

protected CopyAsRTF ( ) : void
return void
        protected void CopyAsRTF()
        {
            TextStyle[] styles = Document.Parser.SyntaxDefinition.Styles;
            Document.ParseAll(true);
            int r1 = Selection.LogicalBounds.FirstRow;
            int r2 = Selection.LogicalBounds.LastRow;
            int c1 = Selection.LogicalBounds.FirstColumn;
            int c2 = Selection.LogicalBounds.LastColumn;

            var sb = new StringBuilder();
            sb.Append(@"{\rtf1\ansi\ansicpg1252\deff0\deflang1053{\fonttbl{\f0\fmodern\fprq1\fcharset0 " + FontName +
                      @";}}");
            sb.Append(@"{\colortbl ;");

            foreach (TextStyle ts in styles)
            {
                sb.AppendFormat("\\red{0}\\green{1}\\blue{2};", ts.ForeColor.R,
                                ts.ForeColor.G, ts.ForeColor.B);
                sb.AppendFormat("\\red{0}\\green{1}\\blue{2};", ts.BackColor.R,
                                ts.BackColor.G, ts.BackColor.B);
            }

            sb.Append(@";}");
            sb.Append(@"\viewkind4\uc1\pard\f0\fs20");


            bool Done = false;
            for (int i = r1; i <= r2; i++)
            {
                Row row = Document[i];


                foreach (Word w in row)
                {
                    if (i == r1 && w.Column + w.Text.Length < c1)
                        continue;

                    bool IsFirst = (i == r1 && w.Column <= c1 && w.Column + w.Text.Length
                                                                 > c1);
                    bool IsLast = (i == r2 && w.Column < c2 && w.Column + w.Text.Length >
                                                               c2);


                    if (w.Type == WordType.Word && w.Style != null)
                    {
                        int clrindex = Array.IndexOf(styles, w.Style);
                        clrindex *= 2;
                        clrindex++;

                        sb.Append("{\\cf" + clrindex.ToString
                                                (CultureInfo.InvariantCulture));
                        if (!w.Style.Transparent)
                        {
                            sb.Append("\\highlight" + (clrindex + 1).ToString
                                                          (CultureInfo.InvariantCulture));
                        }
                        sb.Append(" ");
                    }

                    if (w.Style != null)
                    {
                        if (w.Style.Bold)
                            sb.Append(@"\b ");
                        if (w.Style.Underline)
                            sb.Append(@"\ul ");
                        if (w.Style.Italic)
                            sb.Append(@"\i ");
                    }
                    string wordtext = w.Text;

                    if (IsLast)
                        wordtext = wordtext.Substring(0, c2 - w.Column);

                    if (IsFirst)
                        wordtext = wordtext.Substring(c1 - w.Column);


                    wordtext =
                        wordtext.Replace(@"\", @" \ \ ").Replace(@"
        }
        ", @" \
      }
      ").
                            Replace(@"
      {
        ", @" \
        {
          ");

                    sb.Append(wordtext);

                    if (w.Style != null)
                    {
                        if (w.Style.Bold) sb.Append(@"\b0 ");
                        if (w.Style.Underline)
                            sb.Append(@"\ul0 ");
                        if (w.Style.Italic) sb.Append(@"\i0 ");
                    }

                    if (w.Type == WordType.Word && w.Style != null)
                    {
                        sb.Append("}");
                    }

                    if (IsLast)
                    {
                        Done = true;
                        break;
                    }
                }
                if (Done) break;

                sb.Append(@"\par");
            }


            var da = new DataObject();
            da.SetData(DataFormats.Rtf, sb.ToString());
            string s = Selection.Text;
            da.SetData(DataFormats.Text, s);
            Clipboard.SetDataObject(da);

            var ea = new CopyEventArgs {Text = s};
            OnClipboardUpdated(ea);
        }