-
Notifications
You must be signed in to change notification settings - Fork 1
/
RtfEditor.cs
250 lines (227 loc) · 9.54 KB
/
RtfEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//********************************************************************************************
//Author: Sergey Stoyan, CliverSoft.com
// http://cliversoft.com
// stoyan@cliversoft.com
// sergey.stoyan@gmail.com
// 17 February 2008
//Copyright: (C) 2008, Sergey Stoyan
//********************************************************************************************
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
namespace Cliver
{
/// <summary>
/// Edit rtf text
/// </summary>
internal class RtfEditor
{
StringBuilder rtf_head = null;
StringBuilder rtf_body = null;
List<Color> color_table = new List<Color>();
public RtfEditor(string rtf_source)
{
Match m;
int colortbl_start = rtf_source.IndexOf(@"{\colortbl");
if (colortbl_start < 0)
{
m = Regex.Match(rtf_source, @".*\\(?:fonttbl).*?(?'Open'\{).*?(?'-Open'\})}(?'Point'.*)", RegexOptions.Compiled | RegexOptions.Singleline);
colortbl_start = m.Groups["Point"].Index;
rtf_source = rtf_source.Insert(colortbl_start, @"{\colortbl;}");
}
colortbl_end = rtf_source.IndexOf('}', colortbl_start);
string colors = rtf_source.Substring(colortbl_start, colortbl_end - colortbl_start);
colors = colors.Substring(colors.IndexOf(";"));
m = Regex.Match(colors, @"(?'Color'.+?;)", RegexOptions.Compiled | RegexOptions.Singleline);
while (m.Success)
{
color_table.Add(Color.Empty);
m = m.NextMatch();
}
//m = Regex.Match(rtf_source, @".*\\(?:deff|fonttbl|filetbl|colortbl|stylesheet|listtables|revtbl).*?(?'Open'\{).*?(?'-Open'\})(?'Body'}.*)", RegexOptions.Compiled | RegexOptions.Singleline);
m = Regex.Match(rtf_source, @".*?{\\colortbl.*?}\r\n(?'Body'.*)", RegexOptions.Compiled | RegexOptions.Singleline);
Group g = m.Groups["Body"];
rtf_head = new StringBuilder(rtf_source, 0, g.Index, g.Index);
rtf_body = new StringBuilder(g.Value);
//int head_end = rtf_source.IndexOf("\r\n");
//rtf_head = new StringBuilder(rtf_source.Substring(0, head_end));
//rtf_body = new StringBuilder(rtf_source.Substring(head_end));
}
public string RtfSource
{
get
{
StringBuilder rtf_source = new StringBuilder();
return rtf_source.Append(rtf_head).Append(rtf_body).ToString();
}
}
int colortbl_end = -1;
int get_color_index(Color color)
{
int color_index = color_table.IndexOf(color);
if (color_index >= 0)
return color_index + 1;
string color_rtf_code = @"\red" + color.R.ToString() + @"\green" + color.G.ToString() + @"\blue" + color.B.ToString() + ";";
rtf_head.Insert(colortbl_end, color_rtf_code);
colortbl_end += color_rtf_code.Length;
color_table.Add(color);
return color_table.Count + 1;
}
int pass_tags(int rtf_start)
{
int body_length = rtf_body.Length;
for (int i = rtf_start; i < body_length; i++)
{
char c = rtf_body[i];
if (c == '\\')
{
if (i + 1 < body_length)
{
char c2 = rtf_body[i + 1];
if (c2 == '\\'
|| c2 == '{' || c == '}'
)
{//encoded char
return i;
}
else
{//tag
for (i++; i < body_length; i++)
{
c = rtf_body[i];
if (c == ' '//end of control word
|| (c != '\\' && (c < '0' || c > '9') && (c < 'A' || c > 'Z') && (c < 'a' || c > 'z'))//end of control symbol
)
break;
}
}
}
}
else
{
if (c == '{' || c == '}')// || c == '\r' || c == '\n')
{
}
else
return i;
}
}
return -1;
}
void insert_tag(int plain_start, int plain_end, string tag)
{
int rtf_start = 0;
int rtf_end = 0;
int current_plain_position = 0;
int body_length = rtf_body.Length;
for (int i = 0; i < body_length; i++)
{
char c = rtf_body[i];
if (current_plain_position == plain_start)
{
i = pass_tags(i);
rtf_start = i;
}
if (current_plain_position == plain_end)
{
rtf_end = i;
break;
}
i = pass_tags(i);
if (i < 0)
break;
current_plain_position++;
}
rtf_body.Insert(rtf_end, "}");
rtf_body.Insert(rtf_start, "{" + tag);
}
//public void SetBackColor(int plain_start, int plain_end, Color color)
//{
// string color_tag = @"\highlight" + get_color_index(color).ToString() + " ";
// insert(plain_start, "{" + color_tag);
// insert(plain_end, "}");
//}
//public void SetForeColor(int plain_start, int plain_end, Color color)
//{
// string color_tag = @"\cf" + get_color_index(color).ToString() + " ";
// insert(plain_start, "{" + color_tag);
// insert(plain_end, "}");
//}
public void InsertText(int plain_start, string text, Color fore_color, Color back_color)
{
string color_tags = @"\highlight" + get_color_index(back_color).ToString() + @"\cf" + get_color_index(fore_color).ToString() + " ";
insert_tag(plain_start, plain_start, color_tags + text);
}
internal void MarkCapture(int plain_start, string label_with_font_tags, Color label_fore_color, int plain_end, Color back_color)
{
string capture_tags = @"\highlight" + get_color_index(back_color).ToString() + " ";
string label_with_tags = @"\cf" + get_color_index(label_fore_color).ToString() + label_with_font_tags;
insert_tag(plain_start, plain_end, capture_tags + "{" + label_with_tags + "}");
}
/// <summary>
/// Analog string.IndexOf for StringBuilder
/// </summary>
/// <param name="str">string where to search</param>
/// <param name="value">string what to search</param>
/// <returns>-1 if not found or value is empty</returns>
static public int IndexOf(StringBuilder str, string value)
{
return RtfEditor.IndexOf(str, value, 0, str.Length);
}
/// <summary>
/// Analog string.IndexOf for StringBuilder
/// </summary>
/// <param name="str">string where to search</param>
/// <param name="value">string what to search</param>
/// <param name="start_index">where to start serch within str</param>
/// <returns>-1 if not found or value is empty</returns>
static public int IndexOf(StringBuilder str, string value, int start_index)
{
return RtfEditor.IndexOf(str, value, start_index, str.Length);
}
/// <summary>
/// Analog string.IndexOf for StringBuilder
/// </summary>
/// <param name="str">string where to search</param>
/// <param name="value">string what to search</param>
/// <param name="start_index">where to start serch within str</param>
/// <param name="count">the number of charakter positions to examine</param>
/// <returns>-1 if not found or value is empty</returns>
static public int IndexOf(StringBuilder str, string value, int start_index, int count)
{
if (string.IsNullOrEmpty(value))
return -1;
int length = str.Length;
if (start_index + count < length)
length = start_index + count;
for (int i = start_index; i < length; i++)
{
if (str[i] == value[0])
{
for (int j = 1; i + j < length; j++)
{
if (j == value.Length)
return i;
else if (str[i + j] != value[j])
break;
}
}
}
return -1;
}
static public bool IsEqualIgnoringCase(StringBuilder str, int start_index, string value)
{
if (str == null || string.IsNullOrEmpty(value))
return false;
if (start_index < 0)
return false;
if (start_index + value.Length > str.Length)
return false;
char [] cs = new char[value.Length];
str.CopyTo(start_index, cs, 0, value.Length);
string v = new string(cs);
return string.Compare(v, value, true) == 0;
}
}
}