Ejemplo n.º 1
0
        internal static string GenerateRtf(List <List <ClipboardColoredText> > chunks, MonoDevelop.Ide.Editor.Highlighting.EditorTheme style, ITextEditorOptions options)
        {
            var rtfText   = new StringBuilder();
            var colorList = new List <Cairo.Color> ();

            bool isItalic = false;
            bool isBold   = false;
            int  curColor = -1;

            foreach (var line in chunks)
            {
                bool appendSpace = false;
                foreach (var chunk in line)
                {
                    var chunkStyle = style.GetChunkStyle(chunk.ScopeStack);
                    if (isBold != (chunkStyle.FontWeight == Xwt.Drawing.FontWeight.Bold))
                    {
                        isBold = chunkStyle.FontWeight == Xwt.Drawing.FontWeight.Bold;
                        rtfText.Append(isBold ? @"\b" : @"\b0");
                        appendSpace = true;
                    }
                    if (isItalic != (chunkStyle.FontStyle == Xwt.Drawing.FontStyle.Italic))
                    {
                        isItalic = chunkStyle.FontStyle == Xwt.Drawing.FontStyle.Italic;
                        rtfText.Append(isItalic ? @"\i" : @"\i0");
                        appendSpace = true;
                    }
                    var foreground = chunkStyle.Foreground;
                    if (!colorList.Contains(foreground))
                    {
                        colorList.Add(foreground);
                    }
                    int color = colorList.IndexOf(foreground);
                    if (curColor != color)
                    {
                        curColor = color;
                        rtfText.Append(@"\cf").Append(curColor + 1);
                        appendSpace = true;
                    }
                    AppendRtfText(rtfText, chunk.Text, ref appendSpace);
                }
                rtfText.AppendLine(@"\line");
            }

            var rtf = new StringBuilder();

            rtf.AppendLine(@"{\rtf1\ansi\deff0\adeflang1025");
            rtf.AppendLine(@"{\fonttbl");
            rtf.Append(@"{\f0\fnil\fprq1\fcharset128 ").Append(options.Font.Family).AppendLine(";}");
            rtf.AppendLine("}");
            rtf.Append(CreateColorTable(colorList));
            rtf.AppendLine(@"\viewkind4\uc1\pard");
            rtf.AppendLine(@"\f0");
            try {
                string fontName = options.Font.ToString();
                double fontSize = Double.Parse(fontName.Substring(fontName.LastIndexOf(' ') + 1), System.Globalization.CultureInfo.InvariantCulture) * 2;
                rtf.Append(@"\fs");
                rtf.Append(fontSize);
            } catch (Exception) {};
            rtf.AppendLine(@"\cf1");
            rtf.Append(rtfText.ToString());
            rtf.Append("}");
            return(rtf.ToString());
        }