fprtfexport.pp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. unit fprtfexport;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, DB, fpdbexport;
  6. Type
  7. TRTFExportOption = (reHeaderRow,reHeaderLine,reTopLine,reBottomLine);
  8. TRTFExportOptions = Set of TrtfExportOption;
  9. { TRTFExportFormatSettings }
  10. TRTFExportFormatSettings = Class(TExportFormatSettings)
  11. Private
  12. FOptions : TRTFExportOptions;
  13. Public
  14. Constructor Create(DoInitSettings : Boolean); override;
  15. Procedure Assign(Source : TPersistent); override;
  16. Published
  17. // Properties
  18. Property Options : TRTFExportOptions Read FOptions Write FOptions;
  19. end;
  20. { TRTFExportFieldItem }
  21. TRTFExportFieldItem = Class(TExportFieldItem)
  22. private
  23. FLineAfter: Boolean;
  24. FLineBefore: Boolean;
  25. FWidth: Integer;
  26. FAlign: TAlignment;
  27. Public
  28. Procedure Assign(Source : TPersistent); override;
  29. Published
  30. Property Width : Integer Read FWidth Write FWidth;
  31. Property Align: TAlignment Read FAlign write FAlign;
  32. Property LineBefore : Boolean Read FLineBefore Write FLineBefore;
  33. Property LineAfter : Boolean Read FLineAfter Write FLineAfter;
  34. end;
  35. { TCustomRTFExporter }
  36. TCustomRTFExporter = Class(TCustomFileExporter)
  37. Private
  38. FCurrentRow : String;
  39. FEO : TRTFExportOptions;
  40. FTD : String; // Tabular(X) Table definition string
  41. FTH : String; // Table header row
  42. FTN : String; // Tabular environment name (for closing)
  43. function GetRTFFormatsettings: TRTFExportFormatSettings;
  44. function MakeCell(S: UTF8String; LineBefore, LineAfter: Boolean): string;
  45. procedure SetRTFFormatSettings(const AValue: TRTFExportFormatSettings);
  46. Protected
  47. function EscapeRTF(S: UTF8String): String;
  48. procedure OutputRow(const ARow: String); virtual;
  49. procedure OutputTableEnd; virtual;
  50. procedure OutputTableStart; virtual;
  51. procedure CloseDocument; virtual;
  52. procedure OpenDocument; virtual;
  53. Function CreateFormatSettings : TCustomExportFormatSettings; override;
  54. Procedure BuildDefaultFieldMap(AMap : TExportFields); override;
  55. Function CreateExportFields : TExportFields; override;
  56. Procedure DoDataHeader; override;
  57. Procedure DoDataFooter; override;
  58. Procedure DoBeforeExecute; override;
  59. Procedure DoAfterExecute; override;
  60. Procedure DoDataRowStart; override;
  61. Procedure ExportField(EF : TExportFieldItem); override;
  62. Procedure DoDataRowEnd; override;
  63. Public
  64. Property FormatSettings : TRTFExportFormatSettings Read GetRTFFormatsettings Write SetRTFFormatSettings;
  65. end;
  66. TRTFExporter = Class(TCustomRTFExporter)
  67. Published
  68. Property FileName;
  69. Property Dataset;
  70. Property ExportFields;
  71. Property FromCurrent;
  72. Property RestorePosition;
  73. Property FormatSettings;
  74. Property OnExportRow;
  75. end;
  76. Procedure RegisterRTFExporter;
  77. Procedure UnRegisterRTFExporter;
  78. Const
  79. SRTFExport = 'RTF export';
  80. SRTFExportExt = '.rtf';
  81. Resourcestring
  82. SRTFExportDescr = 'Export to RTF table';
  83. implementation
  84. procedure RegisterRTFExporter;
  85. begin
  86. ExportFormats.RegisterExportFormat(SRTFExport,SRTFExportDescr,SRTFExportExt,TRTFExporter);
  87. end;
  88. procedure UnRegisterRTFExporter;
  89. begin
  90. ExportFormats.UnRegisterExportFormat(SRTFExport);
  91. end;
  92. { TCustomRTFExporter }
  93. function TCustomRTFExporter.EscapeRTF(S: UTF8String): String;
  94. Const
  95. NeedEscape : TSysCharSet = ['{', '}', '\'];
  96. var
  97. SS : UnicodeString;
  98. Ch : UnicodeChar;
  99. begin
  100. SS:=UTF8Decode(S);
  101. Result:='';
  102. For Ch in SS do
  103. begin
  104. if CharInSet(Ch, NeedEscape) then
  105. Result:=Result+'\';
  106. if Ord(Ch)>255 then
  107. Result:=Result+'\u'+IntToStr(Ord(Ch))+'?'
  108. else
  109. Result:=Result+Utf8Encode(Ch);
  110. end;
  111. end;
  112. function TCustomRTFExporter.GetRTFFormatsettings: TRTFExportFormatSettings;
  113. begin
  114. Result:=TRTFExportFormatSettings(Inherited FormatSettings)
  115. end;
  116. procedure TCustomRTFExporter.OutputRow(const ARow: String);
  117. begin
  118. Writeln(TextFile,ARow);
  119. end;
  120. procedure TCustomRTFExporter.BuildDefaultFieldMap(AMap: TexportFields);
  121. Const
  122. FieldWidths : Array[TFieldType] of integer
  123. = (-1,0,3,10,5,
  124. 1,20,20,20,10,8,20,
  125. 0,0,10,0,0,0,0,
  126. 0,0,0,0,0,
  127. 0,0,0,0,0,
  128. 0,0,0,0,0,
  129. 0,0,0,0,0,0);
  130. Var
  131. I : Integer;
  132. FL : TRTFExportFieldItem;
  133. F : TField;
  134. W : Integer;
  135. begin
  136. inherited BuildDefaultFieldMap(AMap);
  137. For I:=0 to AMap.Count-1 do
  138. begin
  139. FL:=TRTFExportFieldItem(AMAP[i]);
  140. F:=Dataset.Fields[i];
  141. W:= FieldWidths[F.DataType];
  142. If (W>0) then
  143. FL.Width:=W
  144. else if (W=0) then
  145. begin
  146. if (F.DataType in StringFieldTypes) then
  147. FL.Width:=F.Size;
  148. end;
  149. If (F.DataType in IntFieldTypes) then
  150. Fl.Align:=taRightJustify;
  151. end;
  152. end;
  153. function TCustomRTFExporter.CreateExportFields: TexportFields;
  154. begin
  155. Result:=TexportFields.Create(TRTFExportFieldItem);
  156. end;
  157. procedure TCustomRTFExporter.DoDataHeader;
  158. Var
  159. I : Integer;
  160. B2 : Boolean;
  161. EF : TRTFExportFieldItem;
  162. begin
  163. B2:=reHeaderRow in FEO;
  164. If B2 then
  165. For I:=0 to ExportFields.Count-1 do
  166. begin
  167. EF:=TRTFExportFieldItem(ExportFields[i]);
  168. If EF.Enabled then
  169. begin
  170. FTH:=FTH+MakeCell(EF.ExportedName,EF.lineBefore,EF.LineAfter);
  171. end;
  172. end;
  173. OutPutTableStart;
  174. inherited DoDataHeader;
  175. end;
  176. procedure TCustomRTFExporter.DoDataFooter;
  177. begin
  178. OutPutTableEnd;
  179. Inherited DoDataFooter;
  180. end;
  181. procedure TCustomRTFExporter.OutputTableEnd;
  182. begin
  183. OutputRow('}');
  184. end;
  185. procedure TCustomRTFExporter.OutputTableStart;
  186. Var
  187. S : String;
  188. I : Integer;
  189. begin
  190. OutputRow('\par{');
  191. if (reHeaderLine in FEO) then
  192. S := '\trbrdrl\brdrs\brdrw1\trbrdrr\brdrs\brdrw1'
  193. else
  194. S := '';
  195. If reHeaderRow in FEO then
  196. begin
  197. OutputRow('{\b\trowd'+S+'\trbrdrh\brdrs\trbrdrv\brdrs');
  198. OutputRow(FTH);
  199. OutputRow('\row}');
  200. end;
  201. end;
  202. procedure TCustomRTFExporter.SetRTFFormatSettings(
  203. const AValue: TRTFExportFormatSettings);
  204. begin
  205. Inherited FormatSettings:=AValue
  206. end;
  207. function TCustomRTFExporter.CreateFormatSettings: TCustomExportFormatSettings;
  208. begin
  209. Result:=TRTFExportFormatSettings.Create(False);
  210. end;
  211. procedure TCustomRTFExporter.DoBeforeExecute;
  212. begin
  213. inherited DoBeforeExecute;
  214. OpenTextFile;
  215. FEO:=FormatSettings.Options;
  216. FTD:='';
  217. FTH:='';
  218. OpenDocument;
  219. end;
  220. procedure TCustomRTFExporter.DoAfterExecute;
  221. begin
  222. CloseDocument;
  223. CloseTextFile;
  224. inherited DoAfterExecute;
  225. end;
  226. procedure TCustomRTFExporter.DoDataRowStart;
  227. begin
  228. FCurrentRow:='';
  229. inherited DoDataRowStart;
  230. end;
  231. Function TCustomRTFExporter.MakeCell(S : UTF8String; LineBefore,LineAfter : Boolean) : string;
  232. begin
  233. Result:='\pard\intbl '+EscapeRTF(S)+'\cell';
  234. end;
  235. procedure TCustomRTFExporter.ExportField(EF: TExportFieldItem);
  236. Var
  237. S : String;
  238. RF : TRTFExportFieldItem;
  239. begin
  240. RF:=EF as TRTFExportFieldItem;
  241. S:=MakeCell(FormatField(EF.Field),RF.LineBefore,RF.LineAfter);
  242. FCurrentRow:=FCurrentRow+S;
  243. end;
  244. procedure TCustomRTFExporter.DoDataRowEnd;
  245. begin
  246. OutputRow('\trowd\trbrdrh\brdrs\trbrdrv\brdrs');
  247. OutputRow(FCurrentRow);
  248. OutputRow('\row');
  249. end;
  250. procedure TCustomRTFExporter.OpenDocument;
  251. begin
  252. OutputRow('{\rtf1');
  253. OutputRow('{\fonttbl');
  254. OutputRow('{\f0\fswiss Helvetica{\*\falt Arial};}');
  255. OutputRow('{\f1\fmodern Courier{\*\falt Courier New};}');
  256. OutputRow('{\f2\froman Times{\*\falt Times New Roman};}');
  257. OutputRow('}{\stylesheet');
  258. OutputRow('{\s1\li0\fi0\ql\sb240\sa60\keepn\f2\b\fs32 Section Title;}');
  259. OutputRow('{\s2\ql\sb30\sa30\keepn\b0\i0\scaps1\f1\fs28 Table Title;}');
  260. OutputRow('{\s3\li0\fi0\qc\sb240\sa60\keepn\f2\b\scaps1\fs28 Listing Title;}');
  261. OutputRow('{\s4\li30\fi30\ql\f2\fs24 Listing Contents;}');
  262. OutputRow('{\s5\li0\fi0\ql\sb240\sa60\keepn\f2\b\fs40 Chapter;}');
  263. OutputRow('{\s6\li0\fi0\ql\sb240\sa60\keepn\f2\b\fs32 Section;}');
  264. OutputRow('{\s7\li0\fi0\ql\sb240\sa60\keepn\f2\b\fs28 Subsection;}');
  265. OutputRow('{\s8\li0\fi0\ql\sb240\sa60\keepn\f2\b\fs24 Subsubsection;}');
  266. OutputRow('{\s9\li30\fi10\ql\sb60\keepn\f2\fs24 Description titles;}');
  267. OutputRow('{\s10\li30\fi30\ql\fs24 Description;}');
  268. OutputRow('{\s11\li0\fi0\ql\fs24 Source Example;}');
  269. OutputRow('}');
  270. end;
  271. procedure TCustomRTFExporter.CloseDocument;
  272. begin
  273. OutputRow('}');
  274. end;
  275. { TRTFExportFormatSettings }
  276. constructor TRTFExportFormatSettings.Create(DoInitSettings: Boolean);
  277. begin
  278. inherited Create(DoInitSettings);
  279. FOptions:=[reHeaderRow,reTopLine,reBottomLine]
  280. end;
  281. procedure TRTFExportFormatSettings.Assign(Source: TPersistent);
  282. Var
  283. FS : TRTFExportFormatSettings;
  284. begin
  285. If (Source is TRTFExportFormatSettings) then
  286. begin
  287. FS:=Source as TRTFExportFormatSettings;
  288. Options:=FS.OPtions;
  289. end;
  290. inherited Assign(Source);
  291. end;
  292. { TRTFExportFieldItem }
  293. procedure TRTFExportFieldItem.Assign(Source: TPersistent);
  294. Var
  295. Fi : TRTFExportFieldItem;
  296. begin
  297. If (Source is TRTFExportFieldItem) then
  298. begin
  299. FI:=Source as TRTFExportFieldItem;
  300. Width:=FI.Width;
  301. Align:=FI.Align;
  302. LineBefore:=FI.LineBefore;
  303. LineAfter:=FI.LineAfter;
  304. end;
  305. inherited Assign(Source);
  306. end;
  307. end.