testfppdf.lpr 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. { This program generates a multi-page PDF document and tests various
  2. functionality on each of the 5 pages.
  3. You can also specify to generate single pages by using the -p <n>
  4. command line parameter.
  5. eg: testfppdf -p 1
  6. testfppdf -p 2
  7. Use -h to see more command line parameter options.
  8. }
  9. program testfppdf;
  10. {$mode objfpc}{$H+}
  11. {$codepage utf8}
  12. uses
  13. {$ifdef unix}cwstring,{$endif} // required for UnicodeString handling.
  14. classes,
  15. sysutils,
  16. custapp,
  17. fpimage,
  18. fpreadjpeg,
  19. fppdf,
  20. fpparsettf;
  21. type
  22. TPDFTestApp = class(TCustomApplication)
  23. private
  24. Fpg: integer;
  25. FFontCompression: boolean;
  26. FDoc: TPDFDocument;
  27. function SetUpDocument: TPDFDocument;
  28. procedure SaveDocument(D: TPDFDocument);
  29. procedure EmptyPage;
  30. procedure SimpleText(D: TPDFDocument; APage: integer);
  31. procedure SimpleLinesRaw(D: TPDFDocument; APage: integer);
  32. procedure SimpleLines(D: TPDFDocument; APage: integer);
  33. procedure SimpleImage(D: TPDFDocument; APage: integer);
  34. procedure SimpleShapes(D: TPDFDocument; APage: integer);
  35. procedure SampleMatrixTransform(D: TPDFDocument; APage: integer);
  36. protected
  37. procedure DoRun; override;
  38. public
  39. procedure WriteHelp;
  40. end;
  41. var
  42. Application: TPDFTestApp;
  43. function TPDFTestApp.SetUpDocument: TPDFDocument;
  44. var
  45. P: TPDFPage;
  46. S: TPDFSection;
  47. i: integer;
  48. lPageCount: integer;
  49. lOpts: TPDFOptions;
  50. begin
  51. Result := TPDFDocument.Create(Nil);
  52. Result.Infos.Title := Application.Title;
  53. Result.Infos.Author := 'Graeme Geldenhuys';
  54. Result.Infos.Producer := 'fpGUI Toolkit 0.8';
  55. Result.Infos.ApplicationName := ApplicationName;
  56. Result.Infos.CreationDate := Now;
  57. lOpts := [];
  58. if FFontCompression then
  59. Include(lOpts, poCompressFonts);
  60. Result.Options := lOpts;
  61. Result.StartDocument;
  62. S := Result.Sections.AddSection; // we always need at least one section
  63. lPageCount := 6;
  64. if Fpg <> -1 then
  65. lPageCount := 1;
  66. for i := 1 to lPageCount do
  67. begin
  68. P := Result.Pages.AddPage;
  69. P.PaperType := ptA4;
  70. P.UnitOfMeasure := uomMillimeters;
  71. S.AddPage(P); // Add the Page to the Section
  72. end;
  73. end;
  74. procedure TPDFTestApp.SaveDocument(D : TPDFDocument);
  75. var
  76. F: TFileStream;
  77. begin
  78. F := TFileStream.Create('test.pdf',fmCreate);
  79. try
  80. D.SaveToStream(F);
  81. Writeln('Document used ',D.ObjectCount,' PDF objects/commands');
  82. finally
  83. F.Free;
  84. end;
  85. end;
  86. procedure TPDFTestApp.EmptyPage;
  87. var
  88. D: TPDFDocument;
  89. begin
  90. D := SetupDocument;
  91. try
  92. SaveDocument(D);
  93. finally
  94. D.Free;
  95. end;
  96. end;
  97. { all units of measure are in millimeters }
  98. procedure TPDFTestApp.SimpleText(D: TPDFDocument; APage: integer);
  99. var
  100. P : TPDFPage;
  101. FtTitle, FtText1, FtText2, FtText3: integer;
  102. begin
  103. P := D.Pages[APage];
  104. // create the fonts to be used (use one of the 14 Adobe PDF standard fonts)
  105. FtTitle := D.AddFont('Helvetica', clRed);
  106. FtText1 := D.AddFont('FreeSans.ttf', 'FreeSans', clGreen); // TODO: this color value means nothing - not used at all
  107. FtText2 := D.AddFont('Times-BoldItalic', clBlack);
  108. // FtText3 := D.AddFont('arial.ttf', 'Arial', clBlack);
  109. FtText3 := FtText1; // to reduce font dependecies, but above works too if you have arial.ttf available
  110. { Page title }
  111. P.SetFont(FtTitle, 23);
  112. P.SetColor(clBlack, false);
  113. P.WriteText(25, 20, 'Sample Text');
  114. // -----------------------------------
  115. // Write text using PDF standard fonts
  116. P.SetFont(FtTitle, 12);
  117. P.SetColor(clBlue, false);
  118. P.WriteText(25, 50, '(25mm,50mm) Helvetica: The quick brown fox jumps over the lazy dog.');
  119. P.SetFont(ftText2,16);
  120. P.SetColor($c00000, false);
  121. P.WriteText(60, 100, '(60mm,100mm) Times-BoldItalic: Big text at absolute position');
  122. // -----------------------------------
  123. // TrueType testing purposes
  124. P.SetFont(ftText3, 13);
  125. P.SetColor(clBlack, false);
  126. P.WriteUTF8Text(15, 120, 'Languages: English: Hello, World!');
  127. P.WriteUTF8Text(40, 130, 'Greek: Γειά σου κόσμος');
  128. P.WriteUTF8Text(40, 140, 'Polish: Witaj świecie');
  129. P.WriteUTF8Text(40, 150, 'Portuguese: Olá mundo');
  130. P.WriteUTF8Text(40, 160, 'Russian: Здравствуйте мир');
  131. P.WriteUTF8Text(40, 170, 'Vietnamese: Xin chào thế giới');
  132. P.SetFont(ftText1, 13);
  133. P.WriteUTF8Text(15, 185, 'Box Drawing: ╠ ╣ ╦ ╩ ├ ┤ ┬ ┴');
  134. P.WriteUTF8Text(15, 200, 'Typography: “What’s wrong?”');
  135. P.WriteUTF8Text(40, 210, '£17.99 vs £17·99');
  136. P.WriteUTF8Text(40, 220, '€17.99 vs €17·99');
  137. P.WriteUTF8Text(40, 230, 'OK then… êçèûÎÐð£¢ß');
  138. P.WriteUTF8Text(25, 280, 'B субботу двадцать третьего мая приезжает твоя любимая теща.');
  139. end;
  140. procedure TPDFTestApp.SimpleLinesRaw(D: TPDFDocument; APage: integer);
  141. var
  142. P: TPDFPage;
  143. FtTitle: integer;
  144. lPt1, lPt2: TPDFCoord;
  145. begin
  146. P:=D.Pages[APage];
  147. // create the fonts to be used (use one of the 14 Adobe PDF standard fonts)
  148. FtTitle := D.AddFont('Helvetica', clBlack);
  149. { Page title }
  150. P.SetFont(FtTitle,23);
  151. P.SetColor(clBlack, False);
  152. P.WriteText(25, 20, 'Sample Line Drawing (DrawLine)');
  153. P.SetColor(clBlack, True);
  154. P.SetPenStyle(ppsSolid);
  155. lPt1.X := 30; lPt1.Y := 100;
  156. lPt2.X := 150; lPt2.Y := 150;
  157. P.DrawLine(lPt1, lPt2, 0.2);
  158. P.SetColor(clBlue, True);
  159. P.SetPenStyle(ppsDash);
  160. lPt1.X := 50; lPt1.Y := 70;
  161. lPt2.X := 180; lPt2.Y := 100;
  162. P.DrawLine(lPt1, lPt2, 0.1);
  163. { we can also use coordinates directly, without TPDFCoord variables }
  164. P.SetColor(clRed, True);
  165. P.SetPenStyle(ppsDashDot);
  166. P.DrawLine(40, 140, 160, 80, 1);
  167. P.SetColor(clBlack, True);
  168. P.SetPenStyle(ppsDashDotDot);
  169. P.DrawLine(60, 50, 60, 120, 1.5);
  170. P.SetColor(clBlack, True);
  171. P.SetPenStyle(ppsDot);
  172. P.DrawLine(10, 80, 130, 130, 0.5);
  173. end;
  174. procedure TPDFTestApp.SimpleLines(D: TPDFDocument; APage: integer);
  175. var
  176. P: TPDFPage;
  177. FtTitle: integer;
  178. TsThinBlack, TsThinBlue, TsThick, TsThinRed, TsThinBlackDot: Integer;
  179. lPt1, lPt2: TPDFCoord;
  180. begin
  181. P:=D.Pages[APage];
  182. // create the fonts to be used (use one of the 14 Adobe PDF standard fonts)
  183. FtTitle := D.AddFont('Helvetica', clRed);
  184. { Page title }
  185. P.SetFont(FtTitle,23);
  186. P.SetColor(clBlack, false);
  187. P.WriteText(25, 20, 'Sample Line Drawing (DrawLineStyle)');
  188. // write the text at position 100 mm from left and 120 mm from top
  189. TsThinBlack := D.AddLineStyleDef(0.2, clBlack, ppsSolid);
  190. TsThinBlue := D.AddLineStyleDef(0.1, clBlue, ppsDash);
  191. TsThinRed := D.AddLineStyleDef(1, clRed, ppsDashDot);
  192. TsThick := D.AddLineStyleDef(1.5, clBlack, ppsDashDotDot);
  193. TsThinBlackDot := D.AddLineStyleDef(0.5, clBlack, ppsDot);
  194. lPt1.X := 30; lPt1.Y := 100;
  195. lPt2.X := 150; lPt2.Y := 150;
  196. P.DrawLineStyle(lPt1, lPt2, tsThinBlack);
  197. lPt1.X := 50; lPt1.Y := 70;
  198. lPt2.X := 180; lPt2.Y := 100;
  199. P.DrawLineStyle(lPt1, lPt2, tsThinBlue);
  200. { we can also use coordinates directly, without TPDFCoord variables }
  201. P.DrawLineStyle(40, 140, 160, 80, tsThinRed);
  202. P.DrawLineStyle(60, 50, 60, 120, tsThick);
  203. P.DrawLineStyle(10, 80, 130, 130, tsThinBlackDot);
  204. end;
  205. procedure TPDFTestApp.SimpleImage(D: TPDFDocument; APage: integer);
  206. Var
  207. P: TPDFPage;
  208. FtTitle: integer;
  209. IDX: Integer;
  210. W, H: Integer;
  211. begin
  212. P := D.Pages[APage];
  213. // create the fonts to be used (use one of the 14 Adobe PDF standard fonts)
  214. FtTitle := D.AddFont('Helvetica', clBlack);
  215. { Page title }
  216. P.SetFont(FtTitle,23);
  217. P.SetColor(clBlack, false);
  218. P.WriteText(25, 20, 'Sample Image Support');
  219. P.SetFont(FtTitle,10);
  220. P.SetColor(clBlack, false);
  221. IDX := D.Images.AddFromFile('poppy.jpg',False);
  222. W := D.Images[IDX].Width;
  223. H := D.Images[IDX].Height;
  224. { scalled down image (small) }
  225. P.DrawImage(25, 100, W div 2, H div 2, IDX); // left-bottom coordinate of image
  226. P.WriteText(90, 75, '[Scaled image]');
  227. { large image }
  228. P.DrawImage(35, 190, W, H, IDX); // left-bottom coordinate of image
  229. P.WriteText(160, 150, '[Default size]');
  230. end;
  231. procedure TPDFTestApp.SimpleShapes(D: TPDFDocument; APage: integer);
  232. var
  233. P: TPDFPage;
  234. FtTitle: integer;
  235. lPt1: TPDFCoord;
  236. begin
  237. P:=D.Pages[APage];
  238. // create the fonts to be used (use one of the 14 Adobe PDF standard fonts)
  239. FtTitle := D.AddFont('Helvetica', clBlack);
  240. { Page title }
  241. P.SetFont(FtTitle,23);
  242. P.SetColor(clBlack);
  243. P.WriteText(25, 20, 'Basic Shapes');
  244. // ========== Rectangles ============
  245. { PDF origin coordinate is Bottom-Left, and we want to use Image Coordinate of Top-Left }
  246. lPt1.X := 30;
  247. lPt1.Y := 60+20; // origin + height
  248. P.SetColor(clRed, true);
  249. P.SetColor($37b344, false); // some green color
  250. P.DrawRect(lPt1.X, lPt1.Y, 40, 20, 3, true, true);
  251. lPt1.X := 20;
  252. lPt1.Y := 50+20; // origin + height
  253. P.SetColor(clBlue, true);
  254. P.SetColor($b737b3, false); // some purple color
  255. P.DrawRect(lPt1.X, lPt1.Y, 40, 20, 1, true, true);
  256. P.SetPenStyle(ppsDashDot);
  257. P.SetColor(clBlue, true);
  258. P.DrawRect(110, 70+20 {origin+height}, 40, 20, 1, false, true);
  259. P.SetPenStyle(ppsDash);
  260. P.SetColor($37b344, true); // some green color
  261. P.DrawRect(100, 60+20 {origin+height}, 40, 20, 2, false, true);
  262. P.SetPenStyle(ppsSolid);
  263. P.SetColor($b737b3, true); // some purple color
  264. P.DrawRect(90, 50+20 {origin+height}, 40, 20, 4, false, true);
  265. // ========== Ellipses ============
  266. P.SetPenStyle(ppsSolid);
  267. P.SetColor($c00000, True);
  268. P.DrawEllipse(60, 150, -40, 20, 3, False, True);
  269. lPt1.X := 60;
  270. lPt1.Y := 150;
  271. P.SetColor(clBlue, true);
  272. P.SetColor($b737b3, false); // some purple color
  273. P.DrawEllipse(lPt1, 10, 10, 1, True, True);
  274. P.SetPenStyle(ppsDashDot);
  275. P.SetColor($b737b3, True);
  276. P.DrawEllipse(140, 150, 35, 20, 1, False, True);
  277. // ========== Lines Pen Styles ============
  278. P.SetPenStyle(ppsSolid);
  279. P.SetColor(clBlack, True);
  280. P.DrawLine(30, 200, 70, 200, 1);
  281. P.SetPenStyle(ppsDash);
  282. P.SetColor(clBlack, True);
  283. P.DrawLine(30, 210, 70, 210, 1);
  284. P.SetPenStyle(ppsDot);
  285. P.SetColor(clBlack, True);
  286. P.DrawLine(30, 220, 70, 220, 1);
  287. P.SetPenStyle(ppsDashDot);
  288. P.SetColor(clBlack, True);
  289. P.DrawLine(30, 230, 70, 230, 1);
  290. P.SetPenStyle(ppsDashDotDot);
  291. P.SetColor(clBlack, True);
  292. P.DrawLine(30, 240, 70, 240, 1);
  293. // ========== Line Attribute ============
  294. P.SetPenStyle(ppsSolid);
  295. P.SetColor(clBlack, True);
  296. P.DrawLine(100, 170, 140, 170, 0.2);
  297. P.DrawLine(100, 180, 140, 180, 0.3);
  298. P.DrawLine(100, 190, 140, 190, 0.5);
  299. P.DrawLine(100, 200, 140, 200, 1);
  300. P.SetColor(clRed, True);
  301. P.DrawLine(100, 210, 140, 210, 2);
  302. P.SetColor($37b344, True);
  303. P.DrawLine(100, 220, 140, 220, 3);
  304. P.SetColor(clBlue, True);
  305. P.DrawLine(100, 230, 140, 230, 4);
  306. P.SetColor($b737b3, True);
  307. P.DrawLine(100, 240, 140, 240, 5);
  308. end;
  309. procedure TPDFTestApp.SampleMatrixTransform(D: TPDFDocument; APage: integer);
  310. var
  311. P: TPDFPage;
  312. FtTitle: integer;
  313. procedure OutputSample;
  314. var
  315. b: boolean;
  316. begin
  317. b := P.Matrix._11 = -1;
  318. P.SetFont(FtTitle, 10);
  319. P.WriteText(10, 10, 'Matrix transform: ' + BoolToStr(b, True));
  320. P.DrawLine(0, 0, 100, 100, 1);
  321. P.WriteText(100, 100, '(line end point)');
  322. end;
  323. begin
  324. P:=D.Pages[APage];
  325. // create the fonts to be used (use one of the 14 Adobe PDF standard fonts)
  326. FtTitle := D.AddFont('Helvetica', clBlack);
  327. { Page title }
  328. P.SetFont(FtTitle,23);
  329. P.SetColor(clBlack);
  330. P.WriteText(75, 20, 'Matrix Transform');
  331. OutputSample;
  332. // enables Cartesian coordinate system for the page
  333. P.Matrix.SetYScalation(1);
  334. P.Matrix.SetYTranslation(0);
  335. OutputSample;
  336. end;
  337. { TPDFTestApp }
  338. procedure TPDFTestApp.DoRun;
  339. var
  340. ErrorMsg: String;
  341. v: integer;
  342. begin
  343. inherited DoRun;
  344. // quick check parameters
  345. ErrorMsg := CheckOptions('hp:f:', '');
  346. if ErrorMsg <> '' then
  347. begin
  348. WriteLn('ERROR: ' + ErrorMsg);
  349. Writeln('');
  350. Terminate;
  351. Exit;
  352. end;
  353. // parse parameters
  354. if HasOption('h', '') then
  355. begin
  356. WriteHelp;
  357. Terminate;
  358. Exit;
  359. end;
  360. Fpg := -1;
  361. if HasOption('p', '') then
  362. begin
  363. Fpg := StrToInt(GetOptionValue('p', ''));
  364. if (Fpg < 1) or (Fpg > 5) then
  365. begin
  366. Writeln('Error in -p parameter. Valid range is 1-5.');
  367. Writeln('');
  368. Terminate;
  369. Exit;
  370. end;
  371. end;
  372. FFontCompression := True;
  373. if HasOption('f', '') then
  374. begin
  375. v := StrToInt(GetOptionValue('f', ''));
  376. if (v <> 0) and (v <> 1) then
  377. begin
  378. Writeln('Error in -f parameter. Valid range is 0-1.');
  379. Writeln('');
  380. Terminate;
  381. Exit;
  382. end;
  383. FFontCompression := (v = 1);
  384. end;
  385. FDoc := SetupDocument;
  386. try
  387. FDoc.FontDirectory := 'fonts';
  388. if Fpg = -1 then
  389. begin
  390. SimpleText(FDoc, 0);
  391. SimpleShapes(FDoc, 1);
  392. SimpleLines(FDoc, 2);
  393. SimpleLinesRaw(FDoc, 3);
  394. SimpleImage(FDoc, 4);
  395. SampleMatrixTransform(FDoc, 5);
  396. end
  397. else
  398. begin
  399. case Fpg of
  400. 1: SimpleText(FDoc, 0);
  401. 2: SimpleShapes(FDoc, 0);
  402. 3: SimpleLines(FDoc, 0);
  403. 4: SimpleLinesRaw(FDoc, 0);
  404. 5: SimpleImage(FDoc, 0);
  405. 6: SampleMatrixTransform(FDoc, 0);
  406. end;
  407. end;
  408. SaveDocument(FDoc);
  409. finally
  410. FDoc.Free;
  411. end;
  412. // stop program loop
  413. Terminate;
  414. end;
  415. procedure TPDFTestApp.WriteHelp;
  416. begin
  417. writeln('Usage:');
  418. writeln(' -h Show this help.');
  419. writeln(' -p <n> Generate only one page. Valid range is 1-5.' + LineEnding +
  420. ' If this option is not specified, then all 5 pages are' + LineEnding +
  421. ' generated.');
  422. writeln(' -f <0|1> Toggle embedded font compression. A value of 0' + LineEnding +
  423. ' disables compression. A value of 1 enables compression.');
  424. writeln('');
  425. end;
  426. begin
  427. Application := TPDFTestApp.Create(nil);
  428. Application.Title := 'fpPDF Test Application';
  429. Application.Run;
  430. Application.Free;
  431. end.