Code Coverage Statistics for Source File
c:\Tools\SD3\src\Libraries\ICSharpCode.TextEditor\Project\Src\Util\RtfWriter.cs
|
Sequence Point Coverage
N/A
0 of 0
|
Branch Coverage
N/A
0 of 0
|
Lines
164
|
Highlight:
Uncovered Code
Covered Code
| L | V | Source |
|---|---|---|
1 |
// <file> |
|
2 |
// <copyright see="prj:///doc/copyright.txt"/> |
|
3 |
// <license see="prj:///doc/license.txt"/> |
|
4 |
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/> |
|
5 |
// <version>$Revision: 1965 $</version> |
|
6 |
// </file> |
|
7 |
||
8 |
using System; |
|
9 |
using System.Collections.Generic; |
|
10 |
using System.Drawing; |
|
11 |
using System.Text; |
|
12 |
||
13 |
using ICSharpCode.TextEditor.Document; |
|
14 |
||
15 |
namespace ICSharpCode.TextEditor.Util |
|
16 |
{ |
|
17 |
public class RtfWriter |
|
18 |
{ |
|
19 |
static Dictionary<string, int> colors; |
|
20 |
static int colorNum; |
|
21 |
static StringBuilder colorString; |
|
22 |
||
23 |
public static string GenerateRtf(TextArea textArea) |
|
24 |
{ |
|
25 |
colors = new Dictionary<string, int>(); |
|
26 |
colorNum = 0; |
|
27 |
colorString = new StringBuilder(); |
|
28 |
||
29 |
||
30 |
StringBuilder rtf = new StringBuilder(); |
|
31 |
||
32 |
rtf.Append(@"{\rtf1\ansi\ansicpg1252\deff0\deflang1031"); |
|
33 |
BuildFontTable(textArea.Document, rtf); |
|
34 |
rtf.Append('\n'); |
|
35 |
||
36 |
string fileContent = BuildFileContent(textArea); |
|
37 |
BuildColorTable(textArea.Document, rtf); |
|
38 |
rtf.Append('\n'); |
|
39 |
rtf.Append(@"\viewkind4\uc1\pard"); |
|
40 |
rtf.Append(fileContent); |
|
41 |
rtf.Append("}"); |
|
42 |
return rtf.ToString(); |
|
43 |
} |
|
44 |
||
45 |
static void BuildColorTable(IDocument doc, StringBuilder rtf) |
|
46 |
{ |
|
47 |
rtf.Append(@"{\colortbl ;"); |
|
48 |
rtf.Append(colorString.ToString()); |
|
49 |
rtf.Append("}"); |
|
50 |
} |
|
51 |
||
52 |
static void BuildFontTable(IDocument doc, StringBuilder rtf) |
|
53 |
{ |
|
54 |
rtf.Append(@"{\fonttbl"); |
|
55 |
rtf.Append(@"{\f0\fmodern\fprq1\fcharset0 " + doc.TextEditorProperties.Font.Name + ";}"); |
|
56 |
rtf.Append("}"); |
|
57 |
} |
|
58 |
||
59 |
static string BuildFileContent(TextArea textArea) |
|
60 |
{ |
|
61 |
StringBuilder rtf = new StringBuilder(); |
|
62 |
bool firstLine = true; |
|
63 |
Color curColor = Color.Black; |
|
64 |
bool oldItalic = false; |
|
65 |
bool oldBold = false; |
|
66 |
bool escapeSequence = false; |
|
67 |
||
68 |
foreach (ISelection selection in textArea.SelectionManager.SelectionCollection) { |
|
69 |
int selectionOffset = textArea.Document.PositionToOffset(selection.StartPosition); |
|
70 |
int selectionEndOffset = textArea.Document.PositionToOffset(selection.EndPosition); |
|
71 |
for (int i = selection.StartPosition.Y; i <= selection.EndPosition.Y; ++i) { |
|
72 |
LineSegment line = textArea.Document.GetLineSegment(i); |
|
73 |
int offset = line.Offset; |
|
74 |
if (line.Words == null) { |
|
75 |
continue; |
|
76 |
} |
|
77 |
||
78 |
foreach (TextWord word in line.Words) { |
|
79 |
switch (word.Type) { |
|
80 |
case TextWordType.Space: |
|
81 |
if (selection.ContainsOffset(offset)) { |
|
82 |
rtf.Append(' '); |
|
83 |
} |
|
84 |
++offset; |
|
85 |
break; |
|
86 |
||
87 |
case TextWordType.Tab: |
|
88 |
if (selection.ContainsOffset(offset)) { |
|
89 |
rtf.Append(@"\tab"); |
|
90 |
} |
|
91 |
++offset; |
|
92 |
escapeSequence = true; |
|
93 |
break; |
|
94 |
||
95 |
case TextWordType.Word: |
|
96 |
Color c = word.Color; |
|
97 |
||
98 |
if (offset + word.Word.Length > selectionOffset && offset < selectionEndOffset) { |
|
99 |
string colorstr = c.R + ", " + c.G + ", " + c.B; |
|
100 |
||
101 |
if (!colors.ContainsKey(colorstr)) { |
|
102 |
colors[colorstr] = ++colorNum; |
|
103 |
colorString.Append(@"\red" + c.R + @"\green" + c.G + @"\blue" + c.B + ";"); |
|
104 |
} |
|
105 |
if (c != curColor || firstLine) { |
|
106 |
rtf.Append(@"\cf" + colors[colorstr].ToString()); |
|
107 |
curColor = c; |
|
108 |
escapeSequence = true; |
|
109 |
} |
|
110 |
||
111 |
if (oldItalic != word.Italic) { |
|
112 |
if (word.Italic) { |
|
113 |
rtf.Append(@"\i"); |
|
114 |
} else { |
|
115 |
rtf.Append(@"\i0"); |
|
116 |
} |
|
117 |
oldItalic = word.Italic; |
|
118 |
escapeSequence = true; |
|
119 |
} |
|
120 |
||
121 |
if (oldBold != word.Bold) { |
|
122 |
if (word.Bold) { |
|
123 |
rtf.Append(@"\b"); |
|
124 |
} else { |
|
125 |
rtf.Append(@"\b0"); |
|
126 |
} |
|
127 |
oldBold = word.Bold; |
|
128 |
escapeSequence = true; |
|
129 |
} |
|
130 |
||
131 |
if (firstLine) { |
|
132 |
rtf.Append(@"\f0\fs" + (textArea.TextEditorProperties.Font.Size * 2)); |
|
133 |
firstLine = false; |
|
134 |
} |
|
135 |
if (escapeSequence) { |
|
136 |
rtf.Append(' '); |
|
137 |
escapeSequence = false; |
|
138 |
} |
|
139 |
string printWord; |
|
140 |
if (offset < selectionOffset) { |
|
141 |
printWord = word.Word.Substring(selectionOffset - offset); |
|
142 |
} else if (offset + word.Word.Length > selectionEndOffset) { |
|
143 |
printWord = word.Word.Substring(0, (offset + word.Word.Length) - selectionEndOffset); |
|
144 |
} else { |
|
145 |
printWord = word.Word; |
|
146 |
} |
|
147 |
||
148 |
rtf.Append(printWord.Replace(@"\", @"\\").Replace("{", "\\{").Replace("}", "\\}")); |
|
149 |
} |
|
150 |
offset += word.Length; |
|
151 |
break; |
|
152 |
} |
|
153 |
} |
|
154 |
if (offset < selectionEndOffset) { |
|
155 |
rtf.Append(@"\par"); |
|
156 |
} |
|
157 |
rtf.Append('\n'); |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
return rtf.ToString(); |
|
162 |
} |
|
163 |
} |
|
164 |
} |