rptttf.pp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. unit rptttf;
  2. {$mode objfpc}{$H+}
  3. {$I demos.inc}
  4. {.$define gdebugframes}
  5. interface
  6. uses
  7. Classes,
  8. SysUtils,
  9. fpreport,
  10. udapp;
  11. type
  12. { TTTFDemo }
  13. TTTFDemo = class(TReportDemoApp)
  14. private
  15. lReportData: TFPReportUserData;
  16. sl: TStringList;
  17. procedure GetReportDataFirst(Sender: TObject);
  18. procedure GetReportDataValue(Sender: TObject; const AValueName: String; var AValue: Variant);
  19. procedure GetReportDataEOF(Sender: TObject; var IsEOF: Boolean);
  20. procedure GetReportFieldNames(Sender: TObject; List: TStrings);
  21. Protected
  22. procedure InitialiseData; override;
  23. procedure CreateReportDesign; override;
  24. public
  25. constructor Create(AOwner : TComponent); override;
  26. destructor Destroy; override;
  27. Class function Description : string; override;
  28. end;
  29. implementation
  30. { TTTFDemo }
  31. procedure TTTFDemo.GetReportDataFirst(Sender: TObject);
  32. begin
  33. {$IFDEF gdebug}
  34. writeln('GetReportDataFirst');
  35. {$ENDIF}
  36. end;
  37. procedure TTTFDemo.GetReportDataValue(Sender: TObject; const AValueName: String; var AValue: Variant);
  38. begin
  39. {$IFDEF gdebug}
  40. writeln(Format('GetReportDataValue - %d', [lReportData.RecNo]));
  41. {$ENDIF}
  42. if AValueName = 'country' then
  43. begin
  44. AValue := sl.Names[lReportData.RecNo-1];
  45. end
  46. else if AValueName = 'population' then
  47. begin
  48. AValue := sl.Values[sl.Names[lReportData.RecNo-1]];
  49. end;
  50. end;
  51. procedure TTTFDemo.GetReportDataEOF(Sender: TObject; var IsEOF: Boolean);
  52. begin
  53. {$IFDEF gdebug}
  54. writeln(Format('GetReportDataEOF - %d', [lReportData.RecNo]));
  55. {$ENDIF}
  56. if lReportData.RecNo > sl.Count then
  57. IsEOF := True
  58. else
  59. IsEOF := False;
  60. end;
  61. procedure TTTFDemo.GetReportFieldNames(Sender: TObject; List: TStrings);
  62. begin
  63. {$IFDEF gdebug}
  64. writeln('********** GetReportFieldNames');
  65. {$ENDIF}
  66. List.Add('country');
  67. List.Add('population');
  68. end;
  69. procedure TTTFDemo.InitialiseData;
  70. begin
  71. sl := TStringList.Create;
  72. {$I countries.inc}
  73. sl.Sort;
  74. end;
  75. procedure TTTFDemo.CreateReportDesign;
  76. var
  77. p: TFPReportPage;
  78. TitleBand: TFPReportTitleBand;
  79. DataBand: TFPReportDataBand;
  80. GroupHeader: TFPReportGroupHeaderBand;
  81. Memo: TFPReportMemo;
  82. PageFooter: TFPReportPageFooterBand;
  83. begin
  84. Inherited;
  85. rpt.Author := 'Graeme Geldenhuys';
  86. rpt.Title := 'FPReport Demo 5 - TrueType Fonts';
  87. rpt.TwoPass := True;
  88. p := TFPReportPage.Create(rpt);
  89. p.Orientation := poPortrait;
  90. p.PageSize.PaperName := 'A4';
  91. { page margins }
  92. p.Margins.Left := 30;
  93. p.Margins.Top := 20;
  94. p.Margins.Right := 30;
  95. p.Margins.Bottom := 20;
  96. p.Data := lReportData;
  97. p.Font.Name := 'LiberationSans';
  98. TitleBand := TFPReportTitleBand.Create(p);
  99. TitleBand.Layout.Height := 40;
  100. {$IFDEF gdebugframes}
  101. TitleBand.Frame.Shape := fsRectangle;
  102. TitleBand.Frame.BackgroundColor := clReportTitleSummary;
  103. {$ENDIF}
  104. Memo := TFPReportMemo.Create(TitleBand);
  105. Memo.Layout.Left := 5;
  106. Memo.Layout.Top := 0;
  107. Memo.Layout.Width := 140;
  108. Memo.Layout.Height := 15;
  109. Memo.Text := 'Country and Population as of 2014';
  110. Memo.TextAlignment.Vertical := tlCenter;
  111. Memo.TextAlignment.Horizontal := taCentered;
  112. Memo.UseParentFont := False;
  113. Memo.Font.Color := clWhite;
  114. Memo.Font.Name := 'Ubuntu'; // our custom TTF font
  115. Memo.Font.Size := 24;
  116. Memo.Frame.Shape := fsRectangle;
  117. Memo.Frame.BackgroundColor := TFPReportColor($01579B);
  118. Memo := TFPReportMemo.Create(TitleBand);
  119. Memo.Layout.Left := 5;
  120. Memo.Layout.Top := 15;
  121. Memo.Layout.Width := 140;
  122. Memo.Layout.Height := 8;
  123. Memo.Text := 'Developed for the <font color="#ffffff" bgcolor="#2E7D32"><a href="http://www.freepascal.org">Free Pascal</a></font> project';
  124. Memo.TextAlignment.Vertical := tlCenter;
  125. Memo.TextAlignment.Horizontal := taCentered;
  126. Memo.UseParentFont := False;
  127. Memo.Font.Color := clGray;
  128. Memo.Font.Name := 'Ubuntu'; // our custom TTF font
  129. Memo.Options := [moAllowHTML, moDisableWordWrap];
  130. GroupHeader := TFPReportGroupHeaderBand.Create(p);
  131. GroupHeader.Layout.Height := 15;
  132. GroupHeader.Data := lReportData;
  133. GroupHeader.GroupCondition := '[copy(country,1,1)]';
  134. GroupHeader.Frame.BackgroundColor := clYellow; // this has no affect on rendered PDF because here Shape = fsNone
  135. GroupHeader.Frame.Color := TFPReportColor($01579B);
  136. GroupHeader.Frame.Lines := [flBottom];
  137. {$ifdef gdebugframes}
  138. GroupHeader.Frame.Shape := fsRectangle;
  139. GroupHeader.Frame.BackgroundColor := clGroupHeaderFooter;
  140. {$endif}
  141. Memo := TFPReportMemo.Create(GroupHeader);
  142. Memo.Layout.Left := 0;
  143. Memo.Layout.Top := 5;
  144. Memo.Layout.Width := 7;
  145. Memo.Layout.Height := 8;
  146. Memo.Text := '[copy(country,1,1)]';
  147. Memo.TextAlignment.Vertical := tlCenter;
  148. Memo.TextAlignment.Horizontal := taCentered;
  149. Memo.UseParentFont := False;
  150. Memo.Font.Size := 18;
  151. Memo.Font.Color := TFPReportColor($01579B);
  152. DataBand := TFPReportDataBand.Create(p);
  153. DataBand.Layout.Height := 8;
  154. {$ifdef gdebugframes}
  155. DataBand.Frame.Shape := fsRectangle;
  156. DataBand.Frame.BackgroundColor := clDataBand;
  157. {$endif}
  158. Memo := TFPReportMemo.Create(DataBand);
  159. Memo.Layout.Left := 15;
  160. Memo.Layout.Top := 0;
  161. Memo.Layout.Width := 50;
  162. Memo.Layout.Height := 5;
  163. Memo.Text := '<a href="http://en.wikipedia.org/wiki/[country]">[country]</a>';
  164. Memo.Options := [moAllowHTML, moDisableWordWrap];
  165. Memo.TextAlignment.Vertical := tlCenter;
  166. Memo.LinkColor := TFPReportColor($2E7D32);
  167. Memo := TFPReportMemo.Create(DataBand);
  168. Memo.Layout.Left := 105;
  169. Memo.Layout.Top := 0;
  170. Memo.Layout.Width := 30;
  171. Memo.Layout.Height := 5;
  172. Memo.Text := '[formatfloat(''#,##0'', StrToFloat(population))]';
  173. Memo.TextAlignment.Vertical := tlCenter;
  174. Memo.TextAlignment.Horizontal := taRightJustified;
  175. PageFooter := TFPReportPageFooterBand.Create(p);
  176. PageFooter.Layout.Height := 20;
  177. {$ifdef gdebugframes}
  178. PageFooter.Frame.Shape := fsRectangle;
  179. PageFooter.Frame.BackgroundColor := clPageHeaderFooter;
  180. {$endif}
  181. Memo := TFPReportMemo.Create(PageFooter);
  182. Memo.Layout.Left := 125;
  183. Memo.Layout.Top := 13;
  184. Memo.Layout.Width := 30;
  185. Memo.Layout.Height := 5;
  186. Memo.Text := 'Page [PageNo] of [PAGECOUNT]';
  187. Memo.UseParentFont := False;
  188. Memo.Font.Name := 'DejaVuSans'; // our custom TTF font
  189. Memo.Font.Size := 10;
  190. end;
  191. constructor TTTFDemo.Create(AOwner : TComponent);
  192. begin
  193. Inherited;
  194. lReportData := TFPReportUserData.Create(nil);
  195. lReportData.OnGetValue := @GetReportDataValue;
  196. lReportData.OnGetEOF := @GetReportDataEOF;
  197. lReportData.OnFirst := @GetReportDataFirst;
  198. lReportData.OnGetNames := @GetReportFieldNames;
  199. end;
  200. destructor TTTFDemo.Destroy;
  201. begin
  202. FreeAndNil(lReportData);
  203. FreeAndNil(sl);
  204. inherited Destroy;
  205. end;
  206. class function TTTFDemo.Description: string;
  207. begin
  208. Result:='Demo showing TrueType Font support';
  209. end;
  210. end.