/Source/FR_E_TXT.PAS

http://github.com/FastReports/FreeReport · Pascal · 152 lines · 120 code · 21 blank · 11 comment · 11 complexity · 338433b2f6fb3f85cacd4feac6b741ed MD5 · raw file

  1. {*****************************************}
  2. { }
  3. { FastReport v2.3 }
  4. { Text export filter }
  5. { }
  6. { Copyright (c) 1998-99 by Tzyganenko A. }
  7. { }
  8. {*****************************************}
  9. unit FR_E_TXT;
  10. interface
  11. {$I FR.inc}
  12. uses
  13. SysUtils, Windows, Messages, Classes, Graphics, Dialogs, FR_Class;
  14. type
  15. TfrTextExport = class(TComponent) // fake component
  16. end;
  17. TfrTextExportFilter = class(TfrExportFilter)
  18. public
  19. constructor Create(AStream: TStream); override;
  20. procedure OnEndPage; override;
  21. procedure OnBeginPage; override;
  22. procedure OnText(X, Y: Integer; const Text: String; View: TfrView); override;
  23. end;
  24. implementation
  25. uses FR_Utils, FR_Const;
  26. var
  27. UsedFont: Integer = 16;
  28. constructor TfrTextExportFilter.Create(AStream: TStream);
  29. var
  30. s: String;
  31. n: Integer;
  32. begin
  33. inherited;
  34. s := InputBox(LoadStr(SFilter), LoadStr(SFilterParam), '16');
  35. Val(s, UsedFont, n);
  36. end;
  37. procedure TfrTextExportFilter.OnEndPage;
  38. var
  39. i, n, x, tc1: Integer;
  40. p: PfrTextRec;
  41. s: String;
  42. function Dup(Count: Integer): String;
  43. var
  44. i: Integer;
  45. begin
  46. Result := '';
  47. for i := 1 to Count do
  48. Result := Result + ' ';
  49. end;
  50. begin
  51. n := Lines.Count - 1;
  52. while n >= 0 do
  53. begin
  54. if Lines[n] <> nil then break;
  55. Dec(n);
  56. end;
  57. for i := 0 to n do
  58. begin
  59. s := '';
  60. tc1 := 0;
  61. p := PfrTextRec(Lines[i]);
  62. while p <> nil do
  63. begin
  64. x := Round(p^.X / 6.5);
  65. s := s + Dup(x - tc1) + p^.Text;
  66. tc1 := x + Length(p^.Text);
  67. p := p^.Next;
  68. end;
  69. s := s + #13#10;
  70. Stream.Write(s[1], Length(s));
  71. end;
  72. s := #12#13#10;
  73. Stream.Write(s[1], Length(s));
  74. end;
  75. procedure TfrTextExportFilter.OnBeginPage;
  76. var
  77. i: Integer;
  78. begin
  79. ClearLines;
  80. for i := 0 to 200 do Lines.Add(nil);
  81. end;
  82. procedure TfrTextExportFilter.OnText(X, Y: Integer; const Text: String;
  83. View: TfrView);
  84. var
  85. p, p1, p2: PfrTextRec;
  86. begin
  87. if View = nil then Exit;
  88. Y := Round(Y / UsedFont);
  89. p1 := PfrTextRec(Lines[Y]);
  90. GetMem(p, SizeOf(TfrTextRec));
  91. FillChar(p^, SizeOf(TfrTextRec), 0);
  92. p^.Next := nil;
  93. p^.X := X;
  94. p^.Text := Text;
  95. if View is TfrMemoView then
  96. with View as TfrMemoView do
  97. begin
  98. p^.FontName := Font.Name;
  99. p^.FontSize := Font.Size;
  100. p^.FontStyle := frGetFontStyle(Font.Style);
  101. p^.FontColor := Font.Color;
  102. {$IFNDEF Delphi2}
  103. p^.FontCharset := Font.Charset;
  104. {$ENDIF}
  105. end;
  106. p^.FillColor := View.FillColor;
  107. if p1 = nil then
  108. Lines[Y] := TObject(p)
  109. else
  110. begin
  111. p2 := p1;
  112. while (p1 <> nil) and (p1^.X < p^.X) do
  113. begin
  114. p2 := p1;
  115. p1 := p1^.Next;
  116. end;
  117. if p2 <> p1 then
  118. begin
  119. p2^.Next := p;
  120. p^.Next := p1;
  121. end
  122. else
  123. begin
  124. Lines[Y] := TObject(p);
  125. p^.Next := p1;
  126. end;
  127. end;
  128. end;
  129. initialization
  130. frRegisterExportFilter(TfrTextExportFilter, LoadStr(STextFile) + ' (*.txt)', '*.txt');
  131. end.