28
28
using System.Collections.Generic;
29
using Mono.TextEditor.Highlighting;
30
31
namespace Mono.TextEditor.Utils
32
33
public static class RtfWriter
34
static string CreateColorTable (List<Gdk.Color> colorList)
35
static string CreateColorTable (List<Cairo.Color> colorList)
36
37
var colorTable = new StringBuilder ();
37
38
colorTable.Append (@"{\colortbl ;");
38
39
for (int i = 0; i < colorList.Count; i++) {
39
Gdk.Color color = colorList [i];
40
var color = colorList [i];
40
41
colorTable.Append (@"\red");
41
colorTable.Append (color.Red / 256);
42
colorTable.Append ((int)(255 * color.R));
42
43
colorTable.Append (@"\green");
43
colorTable.Append (color.Green / 256);
44
colorTable.Append ((int)(255 * color.G));
44
45
colorTable.Append (@"\blue");
45
colorTable.Append (color.Blue / 256);
46
colorTable.Append ((int)(255 * color.B));
46
47
colorTable.Append (";");
48
49
colorTable.Append ("}");
52
53
public static string GenerateRtf (TextEditorData data)
54
return GenerateRtf (data.Document, data.Document.SyntaxMode, data.ColorStyle, data.Options);
57
public static string GenerateRtf (TextDocument doc, Mono.TextEditor.Highlighting.ISyntaxMode mode, Mono.TextEditor.Highlighting.ColorScheme style, ITextEditorOptions options)
55
return GenerateRtf (ColoredSegment.GetChunks (data, new TextSegment (0, data.Length)), data.ColorStyle, data.Options);
58
static void AppendRtfText (StringBuilder rtfText, string text, ref bool appendSpace)
60
foreach (char ch in text) {
63
rtfText.Append (@"\\");
66
rtfText.Append (@"\{");
69
rtfText.Append (@"\}");
72
rtfText.Append (@"\tab");
81
int unicodeCh = (int)ch;
82
if (0x7F < unicodeCh && unicodeCh <= 0xFF) {
83
rtfText.Append(@"\u" + unicodeCh);
84
} else if (0xFF < unicodeCh && unicodeCh <= 0x8000) {
85
rtfText.Append(@"\uc1\u" + unicodeCh + "*");
86
} else if (0x8000 < unicodeCh && unicodeCh <= 0xFFFF) {
87
rtfText.Append(@"\uc1\u" + (unicodeCh - 0x10000) + "*");
95
internal static string GenerateRtf (List<List<ColoredSegment>> chunks, Mono.TextEditor.Highlighting.ColorScheme style, ITextEditorOptions options)
59
97
var rtfText = new StringBuilder ();
60
var colorList = new List<Gdk.Color> ();
98
var colorList = new List<Cairo.Color> ();
62
var selection = new TextSegment (0, doc.TextLength);
63
int startLineNumber = doc.OffsetToLineNumber (selection.Offset);
64
int endLineNumber = doc.OffsetToLineNumber (selection.EndOffset);
66
100
bool isItalic = false;
67
101
bool isBold = false;
68
102
int curColor = -1;
69
foreach (var line in doc.GetLinesBetween (startLineNumber, endLineNumber)) {
103
foreach (var line in chunks) {
70
104
bool appendSpace = false;
71
foreach (var chunk in mode.GetChunks (style, line, line.Offset, line.Length)) {
72
int start = System.Math.Max (selection.Offset, chunk.Offset);
73
int end = System.Math.Min (chunk.EndOffset, selection.EndOffset);
74
var chunkStyle = style.GetChunkStyle (chunk);
76
if (isBold != chunkStyle.Bold) {
77
rtfText.Append (chunkStyle.Bold ? @"\b" : @"\b0");
78
isBold = chunkStyle.Bold;
81
if (isItalic != chunkStyle.Italic) {
82
rtfText.Append (chunkStyle.Italic ? @"\i" : @"\i0");
83
isItalic = chunkStyle.Italic;
86
if (!colorList.Contains (chunkStyle.Color))
87
colorList.Add (chunkStyle.Color);
88
int color = colorList.IndexOf (chunkStyle.Color);
89
if (curColor != color) {
91
rtfText.Append (@"\cf" + (curColor + 1));
94
for (int i = start; i < end; i++) {
95
char ch = doc.GetCharAt (i);
99
rtfText.Append (@"\\");
102
rtfText.Append (@"\{");
105
rtfText.Append (@"\}");
108
rtfText.Append (@"\tab");
113
rtfText.Append (' ');
105
foreach (var chunk in line) {
106
var chunkStyle = style.GetChunkStyle (chunk.Style);
107
if (isBold != (chunkStyle.FontWeight == Xwt.Drawing.FontWeight.Bold)) {
108
isBold = chunkStyle.FontWeight == Xwt.Drawing.FontWeight.Bold;
109
rtfText.Append (isBold ? @"\b" : @"\b0");
112
if (isItalic != (chunkStyle.FontStyle == Xwt.Drawing.FontStyle.Italic)) {
113
isItalic = chunkStyle.FontStyle == Xwt.Drawing.FontStyle.Italic;
114
rtfText.Append (isItalic ? @"\i" : @"\i0");
117
var foreground = style.GetForeground (chunkStyle);
118
if (!colorList.Contains (foreground))
119
colorList.Add (foreground);
120
int color = colorList.IndexOf (foreground);
121
if (curColor != color) {
123
rtfText.Append (@"\cf" + (curColor + 1));
126
AppendRtfText (rtfText, chunk.Text, ref appendSpace);
122
rtfText.Append (@"\par");
123
rtfText.AppendLine ();
128
rtfText.AppendLine (@"\line");
126
131
var rtf = new StringBuilder();
128
rtf.Append (@"{\rtf1\ansi\deff0\adeflang1025");
131
rtf.Append (@"{\fonttbl");
133
rtf.Append (@"{\f0\fnil\fprq1\fcharset128 " + options.Font.Family + ";}");
133
rtf.AppendLine (@"{\rtf1\ansi\deff0\adeflang1025");
134
rtf.AppendLine (@"{\fonttbl");
135
rtf.AppendLine (@"{\f0\fnil\fprq1\fcharset128 " + options.Font.Family + ";}");
136
rtf.AppendLine ("}");
137
137
rtf.Append (CreateColorTable (colorList));
139
rtf.Append (@"\viewkind4\uc1\pard");
138
rtf.AppendLine (@"\viewkind4\uc1\pard");
139
rtf.AppendLine (@"\f0");
143
141
string fontName = options.Font.ToString ();
144
142
double fontSize = Double.Parse (fontName.Substring (fontName.LastIndexOf (' ') + 1), System.Globalization.CultureInfo.InvariantCulture) * 2;
145
143
rtf.Append (@"\fs");
146
144
rtf.Append (fontSize);
147
145
} catch (Exception) {};
148
rtf.Append (@"\cf1");
146
rtf.AppendLine (@"\cf1");
149
147
rtf.Append (rtfText.ToString ());
151
149
return rtf.ToString ();