download JclRTF.pas
Language: Delphi
LOC: 371
Project Info
XVCL
Server: SourceForge
Type: cvs
...rge\x\xvcl\xvcl\JCL\Source\
   _source_.dof
   DJCL50.dpk
   JCL.INC
   Jcl8087.pas
   JclAppInst.pas
   JclBase.pas
   JclCOM.pas
   JclComplex.pas
   JclCounter.pas
   JclDateTime.pas
   JclDebug.pas
   JclExcel.pas
   jclexpreval.pas
   JclFileUtils.pas
   JclGraphics.pas
   JclGraphUtils.pas
   JclHookExcept.pas
   JclIniFiles.pas
   JclLANMan.pas
   JclLocales.pas
   JclLogic.pas
   JclMapi.pas
   JclMath.pas
   JclMime.pas
   JclMiscel.pas
   JclMultimedia.pas
   JclNTFS.pas
   JclPeImage.pas
   JclPrint.pas
   JclRegistry.pas
   JclResources.pas
   JclRTF.pas
   JclRTTI.pas
   JclSecurity.pas
   JclShell.pas
   JclSscanf.pas
   JclStatistics.pas
   JclStrHashMap.pas
   JclStrings.pas
   JclSynch.pas
   JclSysInfo.pas
   JclSysUtils.pas
   JclUnicode.pas
   JclUnicode.rc
   JclUnitConv.pas
   JclWin32.pas
   LM.pas

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
{******************************************************************************}
{                                                                              }
{ Project JEDI Code Library (JCL)                                              }
{                                                                              }
{ The contents of this file are subject to the Mozilla Public License Version  }
{ 1.1 (the "License"); you may not use this file except in compliance with the }
{ License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ }
{                                                                              }
{ Software distributed under the License is distributed on an "AS IS" basis,   }
{ WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for }
{ the specific language governing rights and limitations under the License.    }
{                                                                              }
{ The Original Code is JclRTF.pas.                                             }
{                                                                              }
{ The Initial Developer of the Original Code is documented in the accompanying }
{ help file JCL.chm. Portions created by these individuals are Copyright (C)   }
{ 2000 of these individuals.                                                   }
{                                                                              }
{******************************************************************************}
{                                                                              }
{ Unit owner: Marcel van Brakel                                                }
{ Last modified: January 30, 2001                                              }
{                                                                              }
{******************************************************************************}

unit JclRTF;

{$I JCL.INC}

{$WEAKPACKAGEUNIT ON}

interface

uses
  Windows, Classes, Graphics, SysUtils;

type
  TJclRTFProducer = class (TObject)
  private
    FCodePage: Word;
    FColorTable: array of TColor;
    FFont: TFont;
    FFontChanged: Boolean;
    FFontCharsetTable: array of TFontCharset;
    FFontTable: array of string;
    FRTFStream: TStream;
    FText: string;
    function GetRTFStream: TStream;
    function GetRTFText: string;
    procedure FontChanged(Sender: TObject);
    function RTFHeader: string;
    procedure SetFont(const Value: TFont);
  protected
    function FindColor(Color: TColor): Integer;
    function FindFontNameCharset(const FontName: string; Charset: TFontCharset): Integer;
    procedure WriteChanges;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Clear(AllocSize: Integer {$IFDEF SUPPORTS_DEFAULTPARAMS} = 0 {$ENDIF});
    procedure SaveToFile(const FileName: TFileName);
    procedure SaveToStream(S: TStream);
    procedure WriteLn;
    procedure WriteParagraph(Alignment: TAlignment {$IFDEF SUPPORTS_DEFAULTPARAMS} = taLeftJustify {$ENDIF});
    procedure WriteRaw(const RawText: string);
    procedure WriteText(const Text: string);
    property CodePage: Word read FCodePage write FCodePage;
    property Font: TFont read FFont write SetFont;
    property RTFText: string read GetRTFText;
    property RTFStream: TStream read GetRTFStream;
  end;

function JclRTFToPlainText(RtfStream: TCustomMemoryStream; TextStream: TStream; var CodePage: Word): Boolean; overload;
function JclRTFToPlainText(const RtfFileName, TextFileName: TFileName; var CodePage: Word): Boolean; overload;
function JclRTFToPlainText(RtfStream: TCustomMemoryStream; TextStream: TStream): Boolean; overload;
function JclRTFToPlainText(const RtfFileName, TextFileName: TFileName): Boolean; overload;

implementation

uses
  Consts,
  JclBase, JclFileUtils, JclLogic, JclStrings, JclSysUtils;

//==============================================================================
// TJclRTFProducer
//==============================================================================

procedure TJclRTFProducer.Clear(AllocSize: Integer);
begin
  FCodePage := GetACP;
  FFontChanged := True;
  SetLength(FText, AllocSize);
  FText := '';
  FColorTable := nil;
  FFontTable := nil;
  FFontCharsetTable := nil;
  FreeAndNil(FRTFStream);
end;

//------------------------------------------------------------------------------

constructor TJclRTFProducer.Create;
begin
  inherited Create;
  FFont := TFont.Create;
  FFont.OnChange := FontChanged;
  Clear(0);
end;

//------------------------------------------------------------------------------

destructor TJclRTFProducer.Destroy;
begin
  FreeAndNil(FFont);
  FreeAndNil(FRTFStream);
  inherited;
end;

//------------------------------------------------------------------------------

function TJclRTFProducer.FindColor(Color: TColor): Integer;
var
  I: Integer;
begin
  Result := -1;
  for I := Low(FColorTable) to High(FColorTable) do
    if Color = FColorTable[I] then
    begin
      Result := I;
      Break;
    end;
  if Result = -1 then
  begin
    Result := Length(FColorTable);
    SetLength(FColorTable, Result + 1);
    FColorTable[Result] := Color;
  end;
end;

//------------------------------------------------------------------------------

function TJclRTFProducer.FindFontNameCharset(const FontName: string; Charset: TFontCharset): Integer;
var
  I: Integer;
begin
  Result := -1;
  for I := Low(FFontTable) to High(FFontTable) do
    if StrSame(FFontTable[I], FontName) and (FFontCharsetTable[I] = Charset) then
    begin
      Result := I;
      Break;
    end;
  if Result = -1 then
  begin
    Result := Length(FFontTable);
    SetLength(FFontTable, Result + 1);
    SetLength(FFontCharsetTable, Result + 1);
    FFontTable[Result] := FontName;
    FFontCharsetTable[Result] := Charset;
  end;
end;

//------------------------------------------------------------------------------

procedure TJclRTFProducer.FontChanged(Sender: TObject);
begin
  FFontChanged := True;
end;

//------------------------------------------------------------------------------

function TJclRTFProducer.GetRTFStream: TStream;
begin
  FreeAndNil(FRTFStream);
  FRTFStream := TStringStream.Create(RTFText);
  Result := FRTFStream;
end;

//------------------------------------------------------------------------------

function TJclRTFProducer.GetRTFText: string;
begin
  Result := RTFHeader + FText + '\par }';
end;

//------------------------------------------------------------------------------

function TJclRTFProducer.RTFHeader: string;
var
  I: Integer;

  function FontString(Index: Integer): string;
  begin
    Result := Format('{\f%d\fcharset%d %s;}', [Index, FFontCharsetTable[Index], FFontTable[Index]]);
  end;

  function ColorString(Index: Integer): string;
  var
    Color: TColorRef;
  begin
    Color := ColorToRGB(FColorTable[Index]);
    Result := Format('\red%d\green%d\blue%d;', [GetRValue(Color), GetGValue(Color), GetBValue(Color)]);
  end;

begin
  Result := Format('{\rtf1\ansi\ansicpg%d\deff0\deftab720', [FCodePage]);
  Result := Result + '{\fonttbl';
  for I := 0 to Length(FFontTable) - 1 do
    Result := Result + FontString(I);
  Result := Result + '}';
  Result := Result + '{\colortbl';
  for I := 0 to Length(FColorTable) - 1 do
    Result := Result + ColorString(I);
  Result := Result + '}';
end;

//------------------------------------------------------------------------------

procedure TJclRTFProducer.SaveToFile(const FileName: TFileName);
var
  S: TFileStream;
begin
  S := TFileStream.Create(FileName, fmCreate);
  try
    SaveToStream(S);
  finally
    S.Free;
  end;
end;

//------------------------------------------------------------------------------

procedure TJclRTFProducer.SaveToStream(S: TStream);
var
  LocalText: string;
begin
  LocalText := RTFText;
  S.WriteBuffer(Pointer(LocalText)^, Length(LocalText) + 1);
end;

//------------------------------------------------------------------------------

procedure TJclRTFProducer.SetFont(const Value: TFont);
begin
  FFont.Assign(Value);
end;

//------------------------------------------------------------------------------

procedure TJclRTFProducer.WriteChanges;
const
  FontStyleStrs: array [TFontStyle] of PChar = ('\b', '\i', '\ul', '\strike');
var
  ColorIndex, FontIndex: Integer;
  S: string;
  FS: TFontStyle;
begin
  if FFontChanged then
  begin
    ColorIndex := FindColor(FFont.Color);
    FontIndex := FindFontNameCharset(FFont.Name, FFont.Charset);
    S := Format('\plain\f%d\fs%d\cf%d', [FontIndex, FFont.Size * 2, ColorIndex]);
    for FS := Low(FS) to High(FS) do
      if FS in FFont.Style then
        S := S + FontStyleStrs[FS];
    S := S + ' ';
    FText := FText + S;
    FFontChanged := False;
  end;
end;

//------------------------------------------------------------------------------

procedure TJclRTFProducer.WriteLn;
begin
  WriteText(AnsiLineFeed);
end;

//------------------------------------------------------------------------------

procedure TJclRTFProducer.WriteParagraph(Alignment: TAlignment);
var
  S: string;
begin
  S := '\pard';
  case Alignment of
    taLeftJustify:
      S := S + ' ';
    taRightJustify:
      S := S + '\qr ';
    taCenter:
      S := S + '\qc ';
  end;
  FText := FText + S;
end;

//------------------------------------------------------------------------------

procedure TJclRTFProducer.WriteRaw(const RawText: string);
begin
  FText := FText + RawText;
end;

//------------------------------------------------------------------------------

procedure TJclRTFProducer.WriteText(const Text: string);
var
  I: Integer;
  S: string;
  C: Char;
begin
  SetLength(S, Length(Text));
  S := '';
  WriteChanges;
  for I := 1 to Length(Text) do
  begin
    C := Text[I];
    case C of
      AnsiLineFeed:
        S := S + '\par ' + AnsiCrLf;
      #32..#127:
        case C of
          '{', '}', '\':
            S := '\' + C;
        else
          S := S + C;
        end;
      #128..#255:
        S := S + LowerCase(Format('\''%.2x', [Byte(C)]));
    end;
  end;
  FText := FText + S;
end;

//==============================================================================
// RTF to Plain text conversion
//==============================================================================

function JclRTFToPlainText(RtfStream: TCustomMemoryStream; TextStream: TStream; var CodePage: Word): Boolean;
var
  P, EndText, StartText, StartParamText: PChar;
  BracketLevel, HexCharsNeeded, HexValue: Integer;
  SlashParam: Boolean;
  C: Char;

  procedure ProcessParam;
  var
    CodePageNum: array[0..4] of Char;
  begin
    if StartParamText <> nil then
    begin
      if StrLComp(StartParamText, '\par', P - StartParamText) = 0 then
        TextStream.WriteBuffer(AnsiCrLf, Length(AnsiCrLf));
      if (CodePage = 0) and (StrLComp('\ansicpg', StartParamText, 8) = 0) then
      begin
        Inc(StartParamText, 8);
        StrLCopy(CodePageNum, StartParamText, Min(P - StartParamText, 4));
        CodePage := StrToIntDef(CodePageNum, 0);
      end;
    end;
    StartParamText := nil;
  end;

  procedure MarkTextStart;
  begin
    if (StartText = nil) and (BracketLevel = 1) then
      StartText := P;
  end;

  procedure WriteChar(C: Char);
  begin
    TextStream.WriteBuffer(C, 1);
  end;

  procedure WriteText;
  begin
    if StartText <> nil then
      TextStream.WriteBuffer(StartText^, P - StartText);
    StartText := nil;
  end;

begin
  P := RtfStream.Memory;
  Result := (P <> nil) or (P^ <> '{');
  CodePage := 0;
  if not Result then
    Exit;
  StartText := nil;
  StartParamText := nil;
  EndText := P + RtfStream.Size;
  Inc(P);
  BracketLevel := 1;
  HexCharsNeeded := 0;
  SlashParam := False;
  HexValue := 0;
  while P < EndText do
  begin
    C := P^;
    if HexCharsNeeded > 0 then
    begin
      C := UpCase(C);
      if not (C in AnsiHexDigits) then
      begin
        Result := False;
        Break;
      end;
      HexValue := HexValue shl 4;
      case C of
        '0'..'9':
          Inc(HexValue, Ord(C) - Ord('0'));
        'A'..'F':
          Inc(HexValue, Ord(C) - Ord('A') + 10);
      end;
      Dec(HexCharsNeeded);
      if HexCharsNeeded = 0 then
        WriteChar(Chr(HexValue));
    end
    else
      case C of
        '{':
          begin
            if SlashParam then
            begin
              WriteChar(C);
              StartParamText := nil;
            end
            else
            begin
              ProcessParam;
              Inc(BracketLevel);
            end;
            SlashParam := False;
          end;
        '}':
          begin
            if SlashParam then
            begin
              WriteChar(C);
              StartParamText := nil;
            end
            else
            begin
              ProcessParam;
              Dec(BracketLevel);
              if BracketLevel = 0 then
                Break;
            end;
            SlashParam := False;
          end;
        '\':
          begin
            if SlashParam then
            begin
              WriteChar(C);
              SlashParam := False;
              StartParamText := nil;
            end
            else
            begin
              WriteText;
              ProcessParam;
              StartParamText := P;
              SlashParam := True;
            end;
          end;
        AnsiCarriageReturn, AnsiLineFeed:
          begin
            WriteText;
            ProcessParam;
            SlashParam := False;
          end;
        AnsiSpace:
          begin
            if StartParamText <> nil then
              ProcessParam
            else
              MarkTextStart;
            SlashParam := False;
          end;
        AnsiSingleQuote:
          begin
            if SlashParam then
            begin
              HexCharsNeeded := 2;
              HexValue := 0;
            end;
            SlashParam := False;
          end;
      else
        if StartParamText = nil then
          MarkTextStart;
        SlashParam := False;
      end;
    Inc(P);
  end;
end;

//------------------------------------------------------------------------------

function JclRTFToPlainText(const RtfFileName, TextFileName: TFileName; var CodePage: Word): Boolean;
var
  S: TJclFileMappingStream;
  D: TFileStream;
begin
  S := TJclFileMappingStream.Create(RtfFileName, fmOpenRead or fmShareDenyWrite);
  try
    D := TFileStream.Create(TextFileName, fmCreate);
    try
      Result := JclRTFToPlainText(S, D, CodePage);
    finally
      D.Free;
    end;
  finally
    S.Free;
  end;
end;

//------------------------------------------------------------------------------

function JclRTFToPlainText(RtfStream: TCustomMemoryStream; TextStream: TStream): Boolean;
var
  C: Word;
begin
  Result := JclRTFToPlainText(RtfStream, TextStream, C);
end;

//------------------------------------------------------------------------------

function JclRTFToPlainText(const RtfFileName, TextFileName: TFileName): Boolean;
var
  C: Word;
begin
  Result := JclRTFToPlainText(RtfFileName, TextFileName, C);
end;

end.

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us