@@ -28,10 +28,8 @@ public class RtfBuilder
2828 \cf2 = Green for added bytes
2929 /**/
3030
31- const string FONT_FAMILY_SIZE = @"{\rtf1\ansi\ansicpg1250\deff0\deflang1050{\fonttbl{\f0\fnil\fcharset238 Courier New;}}\fs17\r\n" ; // \r\n
31+ const string FONT_BASE = @"{\rtf1\ansi\ansicpg1250\deff0\deflang1050{\fonttbl{\f0\fnil\fcharset238 " ;
3232 const string START_COLORS = @"{\colortbl;" ;
33- const string EMPTY_FGCOLOR = @"\cf0" ; // \~
34- const string EMPTY_BGCOLOR = @"\chcbpat0 " ;
3533
3634 private readonly RtfColorManager _colorTableManager ;
3735 private readonly StringBuilder _contentTextRtf = new StringBuilder ( ) ;
@@ -41,7 +39,7 @@ public class RtfBuilder
4139 private bool _pageSetup = true ;
4240 private int _lastFontSize = 17 ;
4341 private int _originalFontSize = 0 ; // 17/2 = 8.5pt
44- private string _fontFamilyWithSize = "{ \\ rtf1 \\ ansi \\ ansicpg1250 \\ deff0 \\ deflang1050{ \\ fonttbl{ \\ f0 \\ fnil \\ fcharset238 _FONTNAME_;}}\\ fs_FONTSIZE_\r \n ";
42+ private string _fontFamWithSize = FONT_BASE + " _FONTNAME_;}}\\ fs_FONTSIZE_\r \n ";
4543
4644 /// <summary>
4745 ///
@@ -70,7 +68,7 @@ public RtfBuilder(Color[] colorIndexList, bool pageSetup = true, string fontName
7068 // 1st Font Index, (17/2 = 8.5pt)
7169 _lastFontSize = ( int ) Math . Round ( fontPtSize * 2 , 0 ) ;
7270
73- _fontFamilyWithSize = _fontFamilyWithSize . Replace ( "_FONTNAME_" , fontName ) . Replace ( "_FONTSIZE_" , _lastFontSize . ToString ( ) ) ;
71+ _fontFamWithSize = _fontFamWithSize . Replace ( "_FONTNAME_" , fontName ) . Replace ( "_FONTSIZE_" , _lastFontSize . ToString ( ) ) ;
7472 _colorTableManager = new RtfColorManager ( colorIndexList ) ;
7573 _pageSetup = pageSetup ;
7674 RtfSetupPage ( ) ;
@@ -151,6 +149,14 @@ public string GetDocument(bool clear = true)
151149
152150 return retVal ;
153151 }
152+ public int DocumentSize
153+ {
154+ get
155+ {
156+ // +1 because of the final '}' when GetDocument() is called.
157+ return _contentTextRtf . Length + 1 ;
158+ }
159+ }
154160 /// <summary>
155161 /// View existing RTF context data, which may or may not be complete.<br/>
156162 /// GetDocument(), will finalize the RTF document with any following RTF requirements.
@@ -175,7 +181,7 @@ internal void AddRTF(string text, bool endLine)
175181 if ( endLine )
176182 {
177183 _contentTextAscii . Append ( "\n " ) ;
178- _contentTextRtf . Append ( "\\ par" ) ; // \\par = RTF Line Feed, same as "\n"
184+ _contentTextRtf . Append ( $ "\\ par { Environment . NewLine } ") ; // \\par = RTF Line Feed, same as "\n"
179185 }
180186 }
181187 internal void AddText ( string text , Color bg , Color fg , bool endLine , double fontPtSize )
@@ -185,25 +191,26 @@ internal void AddText(string text, Color bg, Color fg, bool endLine, double font
185191 // if previous background color wasn't empty and now it is, we want to reset color to default.
186192 if ( ! _bgPrevColor . IsEmpty && bg . IsEmpty )
187193 {
188- _contentTextRtf . Append ( EMPTY_BGCOLOR ) ;
194+ _contentTextRtf . Append ( _colorTableManager . ResetColor ( Color_Appearance . Background ) ) ;
189195 _bgPrevColor = bg ;
190196 }
191197 // if previous foreground color wasn't empty and now it is, we want to reset color to default.
192198 if ( ! _fgPrevColor . IsEmpty && fg . IsEmpty )
193199 {
194- _contentTextRtf . Append ( EMPTY_FGCOLOR ) ;
200+ //_contentTextRtf.Append(EMPTY_FGCOLOR);
201+ _contentTextRtf . Append ( _colorTableManager . ResetColor ( Color_Appearance . Foreground ) ) ;
195202 _fgPrevColor = fg ;
196203 }
197204 // if background color isn't empty and previous background is different than new color, lets set color.
198205 if ( ! bg . IsEmpty && ! _bgPrevColor . Equals ( bg ) )
199206 {
200- _contentTextRtf . Append ( _colorTableManager . GetIndex ( bg , Color_Appearance . Background ) ) ;
207+ _contentTextRtf . Append ( _colorTableManager . GetCode ( bg , Color_Appearance . Background ) ) ;
201208 _bgPrevColor = bg ;
202209 }
203210 // if foregroundcolor isn't empty and previous foregroundis different than new color, lets set color.
204211 if ( ! fg . IsEmpty && ! _fgPrevColor . Equals ( fg ) )
205212 {
206- _contentTextRtf . Append ( _colorTableManager . GetIndex ( fg , Color_Appearance . Foreground ) ) ;
213+ _contentTextRtf . Append ( _colorTableManager . GetCode ( fg , Color_Appearance . Foreground ) ) ;
207214 _fgPrevColor = fg ;
208215 }
209216
@@ -219,7 +226,7 @@ internal void AddText(string text, Color bg, Color fg, bool endLine, double font
219226 if ( endLine )
220227 {
221228 _contentTextAscii . Append ( "\n " ) ;
222- _contentTextRtf . Append ( "\\ par" ) ; // \\par = RTF Line Feed, same as "\n"
229+ _contentTextRtf . Append ( $ "\\ par { Environment . NewLine } ") ; // \\par = RTF Line Feed, same as "\n"
223230 }
224231 }
225232 private int GetSize ( double fontPtSize ) => ( int ) Math . Round ( fontPtSize * 2 , 0 ) ;
@@ -239,7 +246,7 @@ private void SetSize(double fontPtSize, bool append = true)
239246 {
240247 _lastFontSize = getRtfSize ;
241248 if ( append )
242- _contentTextAscii . Append ( $ "\\ fs{ _lastFontSize } ") ;
249+ _contentTextAscii . Append ( $ "\\ fs{ _lastFontSize } { Environment . NewLine } ") ;
243250 }
244251 }
245252 }
@@ -256,8 +263,8 @@ private void RtfSetupPage()
256263
257264 if ( _pageSetup )
258265 {
259- _contentTextRtf . Append ( _fontFamilyWithSize ) ; // {\rtf1\ansi\deff0 and font table + default font
260- _contentTextRtf . Append ( START_COLORS ) ; // {\colortbl;
266+ _contentTextRtf . Append ( _fontFamWithSize ) ; // {\rtf1\ansi\deff0 and font table + default font
267+ _contentTextRtf . Append ( START_COLORS ) ; // {\colortbl;
261268
262269 foreach ( var colorIndex in _colorTableManager . ColorArray )
263270 {
@@ -266,7 +273,7 @@ private void RtfSetupPage()
266273 _contentTextRtf . Append ( $ "\\ red{ colorIndex . R } \\ green{ colorIndex . G } \\ blue{ colorIndex . B } ;") ;
267274 }
268275
269- _contentTextRtf . Append ( " }") ;
276+ _contentTextRtf . Append ( $ "}} { Environment . NewLine } ") ;
270277 }
271278 }
272279 private bool ValidFontSize ( double fontPtSize , out string msg )
0 commit comments