-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRTFDirectEdit.cs
More file actions
1171 lines (1062 loc) · 44.2 KB
/
RTFDirectEdit.cs
File metadata and controls
1171 lines (1062 loc) · 44.2 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------
// <copyright file="RTFDirectEdit.cs" company="NoteFly">
// NoteFly a note application.
// Copyright (C) 2012-2015 Tom
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// </copyright>
//-----------------------------------------------------------------------
namespace NoteFly
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
/// <summary>
/// Allows RTF text to be edited if it's normal text without formatting.
/// </summary>
public class RTFDirectEdit
{
/// <summary>
/// The RTF start tag
/// </summary>
private const string RTF1DOCTAG = @"{\rtf1";
/// <summary>
/// The RTF document viewkind tag
/// </summary>
private const string VIEWKINDTAG = @"\viewkind";
/// <summary>
/// The RTF colortable tag
/// </summary>
private const string COLORTBLTAG = @"{\colortbl ";
/// <summary>
/// The RTF color format tag
/// </summary>
private const string COLORITEMTAG = @"\cf";
/// <summary>
/// RTF tab character
/// </summary>
private const string RTFTABTAG = @"\tab";
/// <summary>
/// RTF bold character
/// </summary>
private const string RTFBOLDTAG = @"\b";
/// <summary>
/// RTF bold end character
/// </summary>
private const string RTFBOLDENDTAG = @"\b0";
/// <summary>
/// RTF italic tag
/// </summary>
private const string RTFITALICTAG = @"\i";
/// <summary>
/// RTF italic endtag
/// </summary>
private const string RTFITALICENDTAG = @"\i0";
/// <summary>
/// RTF strike tag
/// </summary>
private const string RTFSTRIKETAG = @"\strike";
/// <summary>
/// RTF strike endtag
/// </summary>
private const string RTFSTRIKEENDTAG = @"\strike0";
/// <summary>
/// RTF underline tag
/// </summary>
private const string RTFUNDERLINETAG = @"\ul";
/// <summary>
/// RTF enderline endtag
/// </summary>
private const string RTFUNDERLINEENDTAG = @"\ulnone";
/// <summary>
/// Maximum length of a RTF tag
/// </summary>
private const int MAXLENRTFTTAG = 10;
/// <summary>
/// List with colortable coloritems
/// </summary>
private List<Color> colortblitems = new List<Color>();
/// <summary>
/// Are we at current position in the RTF document buzy with RTF code format or text content.
/// </summary>
private bool rtfformat = true;
/// <summary>
/// Different length between orginal RTF and new RTF code length.
/// </summary>
private int drtflen = 0;
/// <summary>
/// Array with hexcharacters (lowercase alpha).
/// </summary>
private char[] hexchars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
/// <summary>
/// Make text bold.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="textpos">The text position.</param>
/// <param name="sellentext">The selected length to make bold.</param>
/// <returns>The rtf stream with new bold formatting.</returns>
public string AddBoldTagInRTF(string rtf, int textpos, int sellentext)
{
return this.SetTagInRTF(rtf, textpos, sellentext, RTFBOLDTAG, RTFBOLDENDTAG);
}
/// <summary>
/// Make text italic.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="textpos">The text position.</param>
/// <param name="sellentext">The selected length to make italic.</param>
/// <returns>The rtf stream with new italic formatting.</returns>
public string AddItalicTagInRTF(string rtf, int textpos, int sellentext)
{
return this.SetTagInRTF(rtf, textpos, sellentext, RTFITALICTAG, RTFITALICENDTAG);
}
/// <summary>
/// Make text underline.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="textpos">The text position.</param>
/// <param name="sellentext">The selected length to make underline.</param>
/// <returns>The rtf stream with new underline formatting.</returns>
public string AddUnderlineTagInRTF(string rtf, int textpos, int sellentext)
{
return this.SetTagInRTF(rtf, textpos, sellentext, RTFUNDERLINETAG, RTFUNDERLINEENDTAG);
}
/// <summary>
/// Make text striketought.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="textpos">The text position.</param>
/// <param name="sellentext">The selected length to make striketought.</param>
/// <returns>The rtf stream with new striketought formatting.</returns>
public string AddStrikeTagInRTF(string rtf, int textpos, int sellentext)
{
return this.SetTagInRTF(rtf, textpos, sellentext, RTFSTRIKETAG, RTFSTRIKEENDTAG);
}
/// <summary>
/// Remove text bold.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="textpos">The text position.</param>
/// <param name="sellentext">The selected length to remove bold formatting from.</param>
/// <returns>The rtf stream without bold formatting.</returns>
public string RemoveBoldTagsInRTF(string rtf, int textpos, int sellentext)
{
return this.RemoveTagsInRTF(rtf, textpos, sellentext, RTFBOLDTAG, RTFBOLDENDTAG);
}
/// <summary>
/// Remove italic text.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="textpos">The text position.</param>
/// <param name="sellentext">The selected length to remove italic formatting from.</param>
/// <returns>The new rtf stream without italic formatting.</returns>
public string RemoveItalicTagsInRTF(string rtf, int textpos, int sellentext)
{
return this.RemoveTagsInRTF(rtf, textpos, sellentext, RTFITALICTAG, RTFITALICENDTAG);
}
/// <summary>
/// Remove striketought text.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="textpos">The text position.</param>
/// <param name="sellentext">The select text length.</param>
/// <returns>The new rtf stream without striketought formatting.</returns>
public string RemoveStrikeTagsInRTF(string rtf, int textpos, int sellentext)
{
return this.RemoveTagsInRTF(rtf, textpos, sellentext, RTFSTRIKETAG, RTFSTRIKEENDTAG);
}
/// <summary>
/// Remove underline text.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="textpos">The text position.</param>
/// <param name="sellentext">The selected text length.</param>
/// <returns>The new rtf stream.</returns>
public string RemoveUnderlineTagsInRTF(string rtf, int textpos, int sellentext)
{
return this.RemoveTagsInRTF(rtf, textpos, sellentext, RTFUNDERLINETAG, RTFUNDERLINEENDTAG);
}
/// <summary>
/// Set all rtf text to a particulair color.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="newclr">The color to use for all text in the rtf stream.</param>
/// <param name="textlength">The length of the text.</param>
/// <returns>The new rtf stream.</returns>
public string SetColorAllRTF(string rtf, Color newclr, int textlength)
{
return this.SetColorInRTF(rtf, newclr, 0, textlength);
}
/// <summary>
/// Set the font size in RTF.
/// </summary>
/// <param name="rtf">The RTF stream.</param>
/// <param name="textpos">The text position.</param>
/// <param name="seltextlen">The selected text length.</param>
/// <param name="fontsize">The new font size.</param>
/// <returns></returns>
public string SetSizeTagInRtf(string rtf, int textpos, int seltextlen, int fontsize)
{
string fontsizestarttag = "\fs" + fontsize.ToString();
string fontsizeendtag = "\fs12"; // TODO find the font size of before and set that back.
this.SetTagInRTF(rtf, textpos, seltextlen, fontsizestarttag, fontsizeendtag);
return rtf;
}
/// <summary>
/// The color of text in the RTF stream,
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="newclr">The color to give the text.</param>
/// <param name="textpos">The text position.</param>
/// <param name="textselectedlength">The text length to color.</param>
/// <returns>The new RTF stream.</returns>
public string SetColorInRTF(string rtf, Color newclr, int textpos, int textselectedlength)
{
const int INITEXTRACAP = 32;
StringBuilder sbrtfnew = new StringBuilder(rtf, rtf.Length + INITEXTRACAP);
this.rtfformat = true;
this.drtflen = 0;
int prevnrcoloritem = 1;
int currenttextpos = 0;
string insertcoloritemrtf = null;
bool overridecoloritem = false;
int rtflevel = 0;
bool textposdone = false;
bool isspecchar = false;
int speccharrtfpos = 0;
int posstartcolortbl = this.FindPosStartColortbl(sbrtfnew.ToString());
if (posstartcolortbl < 0)
{
// create colortbl, because it doesn't exist.
sbrtfnew = this.CreateColortbl(sbrtfnew, newclr);
posstartcolortbl = this.FindPosStartColortbl(sbrtfnew.ToString());
if (posstartcolortbl < 0)
{
Log.Write(LogType.exception, "Cannot create colortbl.");
return sbrtfnew.ToString();
}
}
this.ParserColorTbl(sbrtfnew.ToString(), posstartcolortbl);
for (int i = this.FindStartPosDoc(rtf); i < rtf.Length; ++i)
{
if (this.CheckRTFNestedLevel(rtf, i, rtflevel) != 0)
{
this.rtfformat = false;
continue;
}
if (rtf[i] == '\\')
{
speccharrtfpos = 0;
this.rtfformat = true;
bool isescapedslash = this.IsRTFSlash(rtf, i);
bool istabtag = this.IsRTFTab(rtf, i);
isspecchar = false;
isspecchar = this.IsRTFCodepointChar(rtf, i);
if (!isspecchar)
{
isspecchar = this.IsRTFUnicodeChar(rtf, i);
}
if (istabtag || isescapedslash)
{
// A tab is 1 character in text and is 4 characters "\tab" in the RTF stream.
// An escaped slash is 1 charater in text and is 2 characters "\\" in the RTF stream.
currenttextpos += 1;
}
else if (i + COLORITEMTAG.Length < rtf.Length && !isspecchar)
{
if (rtf.Substring(i, COLORITEMTAG.Length).Equals(COLORITEMTAG, StringComparison.Ordinal))
{
int numlen = this.GetLenDigit(rtf, i + COLORITEMTAG.Length);
if (numlen <= 0)
{
Log.Write(LogType.exception, "RTF: \\cf without number.");
return sbrtfnew.ToString();
}
if (!overridecoloritem)
{
string snr = rtf.Substring(i + COLORITEMTAG.Length, numlen);
prevnrcoloritem = this.IntParseFast(snr);
}
else if (overridecoloritem)
{
int posstartremove = i + this.drtflen + insertcoloritemrtf.Length;
int totallencftag = COLORITEMTAG.Length + numlen;
if (sbrtfnew[posstartremove + totallencftag] == ' ')
{
totallencftag += 1; // +1 for space
}
drtflen -= totallencftag;
try
{
sbrtfnew.Remove(posstartremove, totallencftag);
}
catch (ArgumentOutOfRangeException argoutrangexc)
{
Log.Write(LogType.exception, argoutrangexc.Message);
return sbrtfnew.ToString();
}
}
}
}
}
currenttextpos = this.NextTextChar(currenttextpos);
if (isspecchar)
{
if (speccharrtfpos == 0)
{
currenttextpos++;
}
speccharrtfpos++;
}
this.rtfformat = this.InRTFFormat(rtf, i, this.rtfformat);
if (textpos == currenttextpos && !textposdone)
{
// add begintag \cfX
textposdone = true;
int nrcoloritem = 1;
nrcoloritem = this.GetNrcoloritem(sbrtfnew.ToString(), newclr);
if (nrcoloritem < 0)
{
// newclr does not exist as coloritem in colortbl.
sbrtfnew = this.AddColorItem(sbrtfnew, posstartcolortbl, newclr);
// new nr coloritem
nrcoloritem = this.GetNrcoloritem(sbrtfnew.ToString(), newclr);
if (nrcoloritem < 0)
{
Log.Write(LogType.exception, "Can't add coloritem to colortbl.");
return sbrtfnew.ToString();
}
}
insertcoloritemrtf = COLORITEMTAG + nrcoloritem + " ";
this.drtflen = sbrtfnew.Length - rtf.Length;
int poscoloritem = i + this.drtflen + 1; // / +1 for space required
try
{
sbrtfnew.Insert(poscoloritem, insertcoloritemrtf);
}
catch (ArgumentOutOfRangeException argoutrangeexc)
{
Log.Write(LogType.exception, argoutrangeexc.Message);
return sbrtfnew.ToString();
}
//Log.Write(LogType.info, "this.drtflen=" + this.drtflen);
overridecoloritem = true;
}
else if (textpos + textselectedlength == currenttextpos)
{
// add endtag \cfX
string rtfprevinsertcoloritem = COLORITEMTAG + prevnrcoloritem + " ";
this.drtflen = sbrtfnew.Length - rtf.Length;
int possetbackcoloritem = i + this.drtflen + 1 + 2; // +1 for space, +2 for enters
try
{
sbrtfnew.Insert(possetbackcoloritem, rtfprevinsertcoloritem);
}
catch (ArgumentOutOfRangeException argoutrangeexc)
{
Log.Write(LogType.exception, argoutrangeexc.Message);
return sbrtfnew.ToString();
}
overridecoloritem = false;
break;
}
}
return sbrtfnew.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="nrtextchar"></param>
/// <returns></returns>
private int NextTextChar(int nrtextchar) {
if (!this.rtfformat)
{
if (nrtextchar < int.MaxValue)
{
nrtextchar++;
}
}
return nrtextchar;
}
/// <summary>
/// Remove all RTF tags in selected text.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="textpos">The text position.</param>
/// <param name="sellentext">The selected text length.</param>
/// <param name="starttag">The RTF starttag to remove.</param>
/// <param name="endtag">The RTF endtag to remove. Should be of the same kind as RTF starttag.</param>
/// <returns>The new rtf stream.</returns>
private string RemoveTagsInRTF(string rtf, int textpos, int sellentext, string starttag, string endtag)
{
this.rtfformat = true;
this.drtflen = 0;
StringBuilder newrtf = new StringBuilder(rtf);
int rtflevel = 0;
int currenttextpos = 0;
bool tagopen = false;
bool donebeginpos = false;
bool doneendpos = false;
bool isspecchar = false;
int speccharrtfpos = 0;
for (int i = this.FindStartPosDoc(rtf); i < rtf.Length && currenttextpos <= (textpos + sellentext + 1); ++i)
{
if (this.CheckRTFNestedLevel(rtf, i, rtflevel) == 0)
{
if (rtf[i] == '\\')
{
speccharrtfpos = 0;
this.rtfformat = true;
isspecchar = this.IsRTFCodepointChar(rtf, i);
if (!isspecchar)
{
isspecchar = this.IsRTFUnicodeChar(rtf, i);
}
if (this.IsRTFTab(rtf, i))
{
currenttextpos++;
}
tagopen = this.CheckTagOpened(tagopen, rtf, i, starttag, endtag);
}
}
else
{
this.rtfformat = false;
}
if (!this.rtfformat)
{
if (currenttextpos < int.MaxValue)
{
currenttextpos++;
}
}
if (isspecchar)
{
if (speccharrtfpos == 0)
{
currenttextpos++;
}
speccharrtfpos++;
}
this.rtfformat = this.InRTFFormat(rtf, i, this.rtfformat);
if (rtf.Length > (i + starttag.Length) && rtf.Length > (i + endtag.Length))
{
if (currenttextpos >= textpos && currenttextpos <= (textpos + sellentext))
{
newrtf = this.RemoveTag(newrtf, rtf, i, starttag, endtag);
tagopen = this.CheckTagOpened(tagopen, rtf, i, starttag, endtag);
}
// is begin?
if (currenttextpos == textpos && !donebeginpos)
{
if (tagopen)
{
if (rtf[i] == '\\')
{
newrtf.Insert(i + this.drtflen + 1, endtag);
this.drtflen += endtag.Length;
}
else
{
newrtf.Insert(i + this.drtflen + 1, endtag + " ");
this.drtflen += endtag.Length + 1;
}
}
donebeginpos = true;
}
else if (currenttextpos == (textpos + sellentext + 1) && !doneendpos)
{
// need to be last charcter if has RTFformat on this nrtextchar, that why i + this.drtflen here below and: nrtextchar == (textpos + sellentext + 1)
if (tagopen)
{
if (rtf[i] == '\\')
{
newrtf.Insert(i + this.drtflen, starttag);
this.drtflen += starttag.Length;
}
else
{
newrtf.Insert(i + this.drtflen, starttag + " ");
this.drtflen += starttag.Length + 1;
}
}
doneendpos = true;
}
}
}
return newrtf.ToString();
}
/// <summary>
/// Check if the RTF tag is open.
/// </summary>
/// <param name="tagopen">True if tag open.</param>
/// <param name="rtf">The rtf stream.</param>
/// <param name="i">Position in the rtf stream.</param>
/// <param name="starttag">RTF start tag.</param>
/// <param name="endtag">RTF end tag.</param>
/// <returns>True if tag still in open state.</returns>
private bool CheckTagOpened(bool tagopen, string rtf, int i, string starttag, string endtag)
{
if (rtf.Length > (i + starttag.Length) && rtf.Length > (i + endtag.Length))
{
if (rtf.Substring(i, endtag.Length).Equals(endtag, StringComparison.Ordinal))
{
tagopen = false;
}
else if (rtf.Substring(i, starttag.Length).Equals(starttag, StringComparison.Ordinal))
{
tagopen = true;
}
}
return tagopen;
}
/// <summary>
/// Remove RTF tags, between position i for length of sellentext.
/// </summary>
/// <param name="newrtf">The new rtf stream.</param>
/// <param name="rtf">The rtf stream.</param>
/// <param name="i">Position the rtf stream.</param>
/// <param name="starttag">RTF start tag.</param>
/// <param name="endtag">RTF end tag.</param>
/// <returns>The new rtf stream as stringbuilder.</returns>
private StringBuilder RemoveTag(StringBuilder newrtf, string rtf, int i, string starttag, string endtag)
{
int postag = i + this.drtflen;
if (rtf.Substring(i, endtag.Length).Equals(endtag, StringComparison.Ordinal))
{
if (rtf[i + endtag.Length] == ' ')
{
bool rtfformatbefore = false;
int n = i - 1;
while (rtf[n] != ' ' && rtf[n] != ' ' && rtf[n] != '\r')
{
if (rtf[n] == '\\')
{
rtfformatbefore = true;
}
n--;
if (n < i - MAXLENRTFTTAG)
{
break;
}
}
if (!rtfformatbefore)
{
newrtf.Remove(postag, endtag.Length + 1); // +1 for space
this.drtflen -= endtag.Length + 1;
}
else
{
newrtf.Remove(postag, endtag.Length); // +1 for space
this.drtflen -= endtag.Length;
}
}
else
{
newrtf.Remove(postag, endtag.Length);
this.drtflen -= endtag.Length;
}
}
else if (rtf.Substring(i, starttag.Length).Equals(starttag, StringComparison.Ordinal))
{
if (rtf[i + starttag.Length] == ' ')
{
newrtf.Remove(postag, starttag.Length + 1); // +1 for space
this.drtflen -= starttag.Length + 1;
}
else
{
newrtf.Remove(postag, starttag.Length);
this.drtflen -= starttag.Length;
}
}
return newrtf;
}
/// <summary>
/// Add a RTF start- and endtag to the RTF stream
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="textpos">The text position where to add the starttag</param>
/// <param name="sellentext">The length of the text after the starttag to add the RTF end tag.</param>
/// <param name="starttag">The RTF start tag to add.</param>
/// <param name="endtag">The RTF end tag to add.</param>
/// <returns>The new rtf stream.</returns>
private string SetTagInRTF(string rtf, int textpos, int sellentext, string starttag, string endtag)
{
this.rtfformat = true;
this.drtflen = 0;
StringBuilder newrtf = new StringBuilder(rtf, rtf.Length + starttag.Length + endtag.Length);
int rtflevel = 0;
int nrtextchar = 0;
bool isspecchar = false;
bool textstarttagdone = false;
int speccharrtfpos = 0;
bool tagopen = false;
for (int i = this.FindStartPosDoc(rtf); i < rtf.Length; ++i)
{
if (this.CheckRTFNestedLevel(rtf, i, rtflevel) == 0)
{
if (rtf[i] == '\\')
{
this.rtfformat = true;
isspecchar = this.IsRTFUnicodeChar(rtf, i);
if (this.IsRTFTab(rtf, i))
{
nrtextchar++;
}
if (rtf.Length > i + starttag.Length && rtf.Length > i + endtag.Length)
{
if (nrtextchar >= textpos && nrtextchar < (textpos + sellentext))
{
newrtf = this.RemoveTag(newrtf, rtf, i, starttag, endtag);
}
}
}
}
else
{
this.rtfformat = false;
}
nrtextchar = this.NextTextChar(nrtextchar);
if (isspecchar)
{
if (speccharrtfpos == 0)
{
nrtextchar++;
}
else if (speccharrtfpos >= 3)
{
this.rtfformat = false;
isspecchar = false;
speccharrtfpos = 0;
}
speccharrtfpos++;
}
this.rtfformat = this.InRTFFormat(rtf, i, this.rtfformat);
if (isspecchar)
{
if (speccharrtfpos == 0)
{
nrtextchar++;
}
else if (speccharrtfpos >= 3)
{
this.rtfformat = false;
isspecchar = false;
speccharrtfpos = 0;
}
speccharrtfpos++;
}
tagopen = this.CheckTagOpened(tagopen, rtf, i, starttag, endtag);
if (textstarttagdone)
{
if ((textpos + sellentext) == nrtextchar)
{
textstarttagdone = false;
// add end
int postag = i + this.drtflen + 1; // +1 for space
if (i + 1 < rtf.Length)
{
if (rtf[i + 1] == '\\')
{
newrtf.Insert(postag, endtag);
}
else
{
newrtf.Insert(postag, endtag + " ");
this.drtflen += 1;
}
}
else
{
newrtf.Insert(postag, endtag + " ");
this.drtflen += 1;
}
}
}
else
{
if (textpos == nrtextchar)
{
if (!this.rtfformat)
{
if (!tagopen)
{
textstarttagdone = true;
// add begin
newrtf.Insert(i + this.drtflen + 1, starttag + " ");
this.drtflen = newrtf.Length - rtf.Length;
tagopen = true;
}
}
}
}
}
return newrtf.ToString();
}
/// <summary>
/// Figure out if the RTF stream is at position pos still in RTF format mode or
/// is displaying the text from the RTF stream.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="pos">The position to check.</param>
/// <param name="rtfformat">The current rtf status, in rtf formating if true</param>
/// <returns>True if document at pos is in rtf formating mode.</returns>
private bool InRTFFormat(string rtf, int pos, bool rtfformat)
{
if (rtf[pos] == ' ' || rtf[pos] == '\r' || rtf[pos] == '\n')
{
rtfformat = false;
}
return rtfformat;
}
/// <summary>
/// Is there a RTF tab code at a position in a RTF stream.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="i">Position with the rtf stream to look for a tab rtf character.</param>
/// <returns>True if rtf at position i is RTF tab code.</returns>
private bool IsRTFTab(string rtf, int i)
{
if (i + RTFTABTAG.Length < rtf.Length)
{
if (rtf.Substring(i, RTFTABTAG.Length).Equals(RTFTABTAG, StringComparison.Ordinal))
{
return true;
}
}
return false;
}
/// <summary>
///
/// </summary>
/// <param name="rtf"></param>
/// <param name="pos"></param>
/// <returns></returns>
private bool IsRTFSlash(string rtf, int pos)
{
const string ESCAPEDSLASH = "\\\\";
if (pos + ESCAPEDSLASH.Length > rtf.Length)
{
return false;
}
if (!rtf.Substring(pos, ESCAPEDSLASH.Length).Equals(ESCAPEDSLASH, StringComparison.Ordinal))
{
return false;
}
int slashesbeforepos = 0;
for (int i = pos; rtf[i] == '\\' && i >= 0; --i)
{
slashesbeforepos++;
}
if (slashesbeforepos != 0 && slashesbeforepos % 2 != 0)
{
return false;
}
return true;
}
/// <summary>
/// Find the start of the RTF document content.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <returns>The position after viewkind rtftag in the rtf stream.</returns>
private int FindStartPosDoc(string rtf)
{
int posstartbody = rtf.IndexOf(VIEWKINDTAG);
if (posstartbody < 0)
{
if (rtf.StartsWith(RTF1DOCTAG))
{
posstartbody = RTF1DOCTAG.Length;
}
else
{
Log.Write(LogType.exception, "RTF content body not found.");
return 0;
}
}
else
{
posstartbody += VIEWKINDTAG.Length;
}
return posstartbody;
}
/// <summary>
/// Create a colortable in the RTF code stream.
/// </summary>
/// <param name="newrtf">The current new RTF code.</param>
/// <param name="color">The color to add to the color table RTF stream.</param>
/// <returns>StringBuilder of colortbl.</returns>
private StringBuilder CreateColortbl(StringBuilder newrtf, Color color)
{
const string ENDFONTTBL = "}}\r\n";
string rtfcolorvalue = "\\red" + color.R + "\\green" + color.G + "\\blue" + color.B + ";";
int posendfonttbl = newrtf.ToString().IndexOf(ENDFONTTBL);
newrtf.Insert(posendfonttbl + ENDFONTTBL.Length, COLORTBLTAG + ";" + rtfcolorvalue + "}\r\n");
return newrtf;
}
/// <summary>
/// Check if rtf contains a new level or depth document level has been gone up.
/// Escaped { and } characters are not a new RTF depth level.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="pos">The position in RTF</param>
/// <param name="rtfcurrentnestedlevel">Current RTF depth level.</param>
/// <returns>New RTF depth level.</returns>
private int CheckRTFNestedLevel(string rtf, int pos, int rtfcurrentnestedlevel)
{
if (rtf[pos] == '{')
{
if (pos - 1 >= 0)
{
if (rtf[pos - 1] != '\\')
{
if (rtfcurrentnestedlevel < int.MaxValue)
{
rtfcurrentnestedlevel++;
}
}
}
else
{
if (rtfcurrentnestedlevel < int.MaxValue)
{
rtfcurrentnestedlevel++;
}
}
}
else if (rtf[pos] == '}')
{
if (pos - 1 >= 0)
{
if (rtf[pos - 1] != '\\')
{
if (rtfcurrentnestedlevel > 0)
{
rtfcurrentnestedlevel--;
}
}
}
else
{
if (rtfcurrentnestedlevel > 0)
{
rtfcurrentnestedlevel--;
}
}
}
return rtfcurrentnestedlevel;
}
/// <summary>
/// Is the RTF part at current position a unicode character in RTF escaping.
/// </summary>
/// <param name="rtf">The rtf stream.</param>
/// <param name="pos">The RTF position in where the special character starts</param>
/// <returns>True if there is a special character RTF code at position i.</returns>
private bool IsRTFUnicodeChar(string rtf, int pos)
{
const int RTFUNICODECHARMINLEN = 5; // \u519?
if (pos + RTFUNICODECHARMINLEN > rtf.Length)
{
return false;
}
bool isunicodechar = false;
if (rtf.Substring(pos, 2).Equals(@"\u", StringComparison.Ordinal) && rtf[pos + 2] != 'c')
{
// An 16 bits unicode character and NOT RTF unicode control character e.g. \uc1.
// \uc1 keyword represents the number of bytes(1) corresponding to a given \uN Unicode character.
isunicodechar = true;
int i = 2;
char rtfchar = rtf[pos + i];
while (i < RTFUNICODECHARMINLEN || rtfchar != '?')
{
if (!Char.IsDigit(rtfchar))
{
Log.Write(LogType.info, "rtfchar not digit.");
isunicodechar = false;
break;
}
++i;
rtfchar = rtf[pos + i];
}
}
return isunicodechar;
}
/// <summary>
/// Is the RTF part at current position a codepoint in RTF escaping.
/// </summary>
/// <param name="rtf"></param>
/// <param name="pos"></param>
/// <returns></returns>
private bool IsRTFCodepointChar(string rtf, int pos)
{
const int RTFCODEPOINTMAXHEXLEN = 4;
if (pos + RTFCODEPOINTMAXHEXLEN > rtf.Length)