Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test.cs « System.Windows.Forms.RTF « Managed.Windows.Forms « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c62cdb90c494cfadbcd55fa265d07d32dc780afd (plain)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Windows.Forms;
using System.Text;
using System.Threading;
using System.Windows.Forms.RTF;
using System.IO;

namespace TextTestClass {
	public class Test {
		static Test	test;
		int		skip_width;
		int		skip_count;
		private string rtf_string = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs17 testing 123testiong\\par\r\n}";
		private string rtf_string2 =	"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fswiss\\fcharset0 Arial;}{\\f1\\fmodern\\fprq1\\fcharset0 Courier;}{\\f2\\fswiss\\fprq2\\fcharset0 Arial;}}\r\n" + 
			"{\\colortbl ;\\red255\\green0\\blue0;\\red0\\green0\\blue0;}\r\n" + 
			"{\\*\\generator Msftedit 5.41.15.1507;}\\viewkind4\\uc1\\pard\\f0\\fs20 I am in Arial 10pt\\par\r\n" + 
			"\\fs24 I am in Arial 12pt\\par\r\n" +
			"\\f1 I am in Courier 12pt\\par\r\n" + 
			"\\cf1 I am in Courier 12pt Red\\par\r\n" + 
			"\\cf2\\f2\\fs20 I am in Arial 10pt\\par\r\n" +
			"\\b I am in Arial 10pt Italic\\cf0\\b0\\f0\\par\r\n" +
			"}";
		private string rtf_string3 = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fswiss\\fcharset0 Arial;}{" + 
			"\\f1\\fmodern\\fprq1\\fcharset0 Courier;}{\\f2\\fswiss\\fprq2\\fcharset0 Arial;}{\\f3\\fni" +
			"l\\fcharset0 Impact;}{\\f4\\fnil\\fcharset0 Arial Unicode MS;}{\\f5\\fnil\\fcharset136 Arial Unicode MS;}{\\f6\\fnil\\fcharset0 MS" +
			" Shell Dlg;}}" +
			"{\\colortbl ;\\red255\\green0\\blue0;\\red0\\green0\\blue0;}" +
			"{\\*\\generator Msftedit 5.41.15.1507;}\\viewkind4\\uc1\\pard\\f0\\fs20 I am in Arial 1" +
			"0pt\\par" +
			"\\fs24 I am in Arial 12pt\\par" +
			"\\f1 I am in Courier 12pt\\par" +
			"\\cf1 I am in Courier 12pt Red\\par" +
			"\\cf2\\f2\\fs20 I am in Arial 10pt\\par" +
			"\\b I am in Arial 10pt Bold\\par" +
			"\\i I am in Arial 10pt Bold Italic\\par" +
			"\\ul I am in Arial 10pt Bold Italic Underline\\par" +
			"\\ulnone\\b0\\i0\\strike I am in Arial 10pt Strikethrough\\par" +
			"\\cf0\\strike0\\f3\\fs23 Some cyrilic character: \\u1034?\\par" +
			"And 5 CJK characters: \\f4\\fs21\\u23854?\\u23854?\\u23854?\\u23854?\\u23854?\\f5\\fs17\\par" + 
			"Some special chars:\\par" +
			"\\tab Tilde: ~\\par" +
			"\\tab Questionmark:?\\par" +
			"\\tab Yen: \\f5\\u165?\\f6\\fs17\\par" +
			"\\tab Umlaut: \\'e4\\par" +
			"\\f0\\fs20\\par" +
			"}";

		TextMap text;

		public Test() {
			MemoryStream	stream;
			RTF		rtf;
			byte[]		buffer;

			text = new TextMap();
			TextMap.SetupStandardTable(text.Table);

			buffer = new byte[rtf_string.Length];
			for (int i = 0; i < buffer.Length; i++) {
				buffer[i] = (byte)rtf_string[i];
			}
			stream = new MemoryStream(buffer);
			rtf = new RTF(stream);

			skip_width = 0;
			skip_count = 0;

			rtf.ClassCallback[TokenClass.Text] = new ClassDelegate(HandleText);
			rtf.ClassCallback[TokenClass.Control] = new ClassDelegate(HandleControl);

			rtf.Read();

			stream.Close();
		}

		void HandleControl(RTF rtf) {
			switch(rtf.Major) {
				case Major.Unicode: {
					switch(rtf.Minor) {
						case Minor.UnicodeCharBytes: {
							skip_width = rtf.Param;
							break;
						}

						case Minor.UnicodeChar: {
							Console.Write("[Unicode {0:X4}]", rtf.Param);
							skip_count += skip_width;
							break;
						}
					}
					break;
				}

				case Major.Destination: {
					Console.Write("[Got Destination control {0}]", rtf.Minor);
					rtf.SkipGroup();
					break;
				}

				case Major.CharAttr: {
					switch(rtf.Minor) {
						case Minor.ForeColor: {
							System.Windows.Forms.RTF.Color	color;
							int	num;

							color = System.Windows.Forms.RTF.Color.GetColor(rtf, rtf.Param);
							if (color != null) {
								if (color.Red == -1 && color.Green == -1 && color.Blue == -1) {
									Console.Write("[Default Color]");
								} else {
									Console.Write("[Color {0} [{1:X2}{2:X2}{3:X}]]", rtf.Param, color.Red, color.Green, color.Blue);
								}
							}
							break;
						}

						case Minor.FontSize: {
							Console.Write("[Fontsize {0}]", rtf.Param);
							break;
						}

						case Minor.FontNum: {
							System.Windows.Forms.RTF.Font	font;

							font = System.Windows.Forms.RTF.Font.GetFont(rtf, rtf.Param);
							if (font != null) {
								Console.Write("[Font {0} [{1}]]", rtf.Param, font.Name);
							}
							break;
						}

						case Minor.Plain: {
							Console.Write("[Normal]");
							break;
						}

						case Minor.Bold: {
							if (rtf.Param == RTF.NoParam) {
								Console.Write("[Bold]");
							} else {
								Console.Write("[NoBold]");
							}
							break;
						}

						case Minor.Italic: {
							if (rtf.Param == RTF.NoParam) {
								Console.Write("[Italic]");
							} else {
								Console.Write("[NoItalic]");
							}
							break;
						}

						case Minor.StrikeThru: {
							if (rtf.Param == RTF.NoParam) {
								Console.Write("[StrikeThru]");
							} else {
								Console.Write("[NoStrikeThru]");
							}
							break;
						}

						case Minor.Underline: {
							if (rtf.Param == RTF.NoParam) {
								Console.Write("[Underline]");
							} else {
								Console.Write("[NoUnderline]");
							}
							break;
						}

						case Minor.NoUnderline: {
							Console.Write("[NoUnderline]");
							break;
						}
					}
					break;
				}

				case Major.SpecialChar: {
					Console.Write("[Got SpecialChar control {0}]", rtf.Minor);
					SpecialChar(rtf);
					break;
				}
			}
		}

		void SpecialChar(RTF rtf) {
			switch(rtf.Minor) {
				case Minor.Page:
				case Minor.Sect:
				case Minor.Row:
				case Minor.Line:
				case Minor.Par: {
					Console.Write("\n");
					break;
				}

				case Minor.Cell: {
					Console.Write(" ");
					break;
				}

				case Minor.NoBrkSpace: {
					Console.Write(" ");
					break;
				}

				case Minor.Tab: {
					Console.Write("\t");
					break;
				}

				case Minor.NoBrkHyphen: {
					Console.Write("-");
					break;
				}

				case Minor.Bullet: {
					Console.Write("*");
					break;
				}

				case Minor.EmDash: {
					Console.Write("—");
					break;
				}

				case Minor.EnDash: {
					Console.Write("–");
					break;
				}

				case Minor.LQuote: {
					Console.Write("‘");
					break;
				}

				case Minor.RQuote: {
					Console.Write("’");
					break;
				}

				case Minor.LDblQuote: {
					Console.Write("“");
					break;
				}

				case Minor.RDblQuote: {
					Console.Write("”");
					break;
				}

				default: {
					rtf.SkipGroup();
					break;
				}
			}
		}


		void HandleText(RTF rtf) {
			if (skip_count > 0) {
				skip_count--;
				return;
			}
			if ((StandardCharCode)rtf.Minor != StandardCharCode.nothing) {
				Console.Write("{0}", text[(StandardCharCode)rtf.Minor]);
			} else {
				if ((int)rtf.Major > 31 && (int)rtf.Major < 128) {
					Console.Write("{0}", (char)rtf.Major);
				} else {
					Console.Write("[Literal:0x{0:X2}]", (int)rtf.Major);
				}
			}
		}

		public static void Main() {
			test = new Test();
		}
	}
}