pscanvas.pp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2003 by the Free Pascal development team
  4. TPostScriptCanvas implementation.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. { ---------------------------------------------------------------------
  12. This code is heavily based on Tony Maro's initial TPostScriptCanvas
  13. implementation in the LCL, but was adapted to work with the custom
  14. canvas code and to work with streams instead of strings.
  15. ---------------------------------------------------------------------}
  16. {$mode objfpc}
  17. {$H+}
  18. unit pscanvas;
  19. interface
  20. uses
  21. Classes, SysUtils,fpimage,fpcanvas;
  22. type
  23. TPostScript = class;
  24. TPSPaintType = (ptColored, ptUncolored);
  25. TPSTileType = (ttConstant, ttNoDistortion, ttFast);
  26. TPostScriptCanvas = class; // forward reference
  27. {Remember, modifying a pattern affects that pattern for the ENTIRE document!}
  28. TPSPattern = class(TFPCanvasHelper)
  29. private
  30. FStream : TMemoryStream;
  31. FPatternCanvas : TPostScriptCanvas;
  32. FOldName: String;
  33. FOnChange: TNotifyEvent;
  34. FBBox: TRect;
  35. FName: String;
  36. FPaintType: TPSPaintType;
  37. FPostScript: TStringList;
  38. FTilingType: TPSTileType;
  39. FXStep: Real;
  40. FYStep: Real;
  41. function GetpostScript: TStringList;
  42. procedure SetBBox(const AValue: TRect);
  43. procedure SetName(const AValue: String);
  44. procedure SetPaintType(const AValue: TPSPaintType);
  45. procedure SetTilingType(const AValue: TPSTileType);
  46. procedure SetXStep(const AValue: Real);
  47. procedure SetYStep(const AValue: Real);
  48. protected
  49. public
  50. constructor Create;
  51. destructor Destroy; override;
  52. procedure Changed;
  53. property BBox: TRect read FBBox write SetBBox;
  54. property PaintType: TPSPaintType read FPaintType write SetPaintType;
  55. property TilingType: TPSTileType read FTilingType write SetTilingType;
  56. property XStep: Real read FXStep write SetXStep;
  57. property YStep: Real read FYStep write SetYStep;
  58. property Name: String read FName write SetName;
  59. property GetPS: TStringList read GetPostscript;
  60. property OldName: string read FOldName write FOldName; // used when notifying that name changed
  61. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  62. Property PatternCanvas : TPostScriptCanvas Read FPatternCanvas;
  63. end;
  64. PPSPattern = ^TPSPattern; // used for array
  65. { Pen and brush object both right now...}
  66. TPSPen = class(TFPCustomPen)
  67. private
  68. FPattern: TPSPattern;
  69. procedure SetPattern(const AValue: TPSPattern);
  70. public
  71. destructor Destroy; override;
  72. property Pattern: TPSPattern read FPattern write SetPattern;
  73. function AsString: String;
  74. end;
  75. TPSBrush = Class(TFPCustomBrush)
  76. Private
  77. Function GetAsString : String;
  78. Public
  79. Property AsString : String Read GetAsString;
  80. end;
  81. TPSFont = Class(TFPCustomFont)
  82. end;
  83. { Custom canvas-like object that handles postscript code }
  84. TPostScriptCanvas = class(TFPCustomCanvas)
  85. private
  86. FHeight,FWidth : Integer;
  87. FStream : TStream;
  88. FLineSpacing: Integer;
  89. LastX: Integer;
  90. LastY: Integer;
  91. function TranslateY(Ycoord: Integer): Integer; // Y axis is backwards in postscript
  92. procedure AddFill;
  93. procedure ResetPos; // reset back to last moveto location
  94. procedure SetWidth (AValue : integer); override;
  95. function GetWidth : integer; override;
  96. procedure SetHeight (AValue : integer); override;
  97. function GetHeight : integer; override;
  98. Protected
  99. Procedure WritePS(Const Cmd : String);
  100. Procedure WritePS(Const Fmt : String; Args : Array of Const);
  101. procedure DrawRectangle(const Bounds: TRect; DoFill : Boolean);
  102. procedure DrawEllipse(const Bounds: TRect; DoFill : Boolean);
  103. public
  104. constructor Create(AStream : TStream);
  105. destructor Destroy; override;
  106. function DoCreateDefaultFont : TFPCustomFont; override;
  107. function DoCreateDefaultPen : TFPCustomPen; override;
  108. function DoCreateDefaultBrush : TFPCustomBrush; override;
  109. property LineSpacing: Integer read FLineSpacing write FLineSpacing;
  110. Procedure DoMoveTo(X1,Y1 : Integer); override;
  111. Procedure DoLineTo(X1,Y1 : Integer); override;
  112. Procedure DoLine(X1,Y1,X2,Y2 : Integer); override;
  113. Procedure DoRectangle(Const Bounds : TRect); override;
  114. Procedure DoRectangleFill(Const Bounds : TRect); override;
  115. procedure DoPolyline(Const Points: Array of TPoint); override;
  116. procedure DoEllipse(const Bounds: TRect); override;
  117. procedure DoEllipseFill(const Bounds: TRect); override;
  118. procedure DoPie(x,y,awidth,aheight,angle1,angle2 : Integer);
  119. //procedure Pie(x,y,width,height,SX,SY,EX,EY : Integer);
  120. procedure Writeln(AString: String);
  121. procedure TextOut(X,Y: Integer; const Text: String);
  122. //procedure Chord(x,y,width,height,angle1,angle2 : Integer);
  123. //procedure Chord(x,y,width,height,SX,SY,EX,EY : Integer);
  124. //procedure PolyBezier(Points: PPoint; NumPts: Integer;
  125. // Filled: boolean{$IFDEF VER1_1} = False{$ENDIF};
  126. // Continuous: boolean{$IFDEF VER1_1} = False{$ENDIF});
  127. //procedure PolyBezier(const Points: array of TPoint;
  128. // Filled: boolean{$IFDEF VER1_1} = False{$ENDIF};
  129. // Continuous: boolean{$IFDEF VER1_1} = False{$ENDIF});
  130. //procedure PolyBezier(const Points: array of TPoint);
  131. //procedure Polygon(const Points: array of TPoint;
  132. // Winding: Boolean{$IFDEF VER1_1} = False{$ENDIF};
  133. // StartIndex: Integer{$IFDEF VER1_1} = 0{$ENDIF};
  134. // NumPts: Integer {$IFDEF VER1_1} = -1{$ENDIF});
  135. //procedure Polygon(Points: PPoint; NumPts: Integer;
  136. // Winding: boolean{$IFDEF VER1_1} = False{$ENDIF});
  137. //Procedure Polygon(const Points: array of TPoint);
  138. //Procedure FillRect(const Rect : TRect);
  139. //procedure FloodFill(X, Y: Integer; FillColor: TFPColor; FillStyle: TFillStyle);
  140. //Procedure RoundRect(X1, Y1, X2, Y2: Integer; RX,RY : Integer);
  141. //Procedure RoundRect(const Rect : TRect; RX,RY : Integer);
  142. Property Stream : TStream read FStream;
  143. end;
  144. { Encapsulates ALL the postscript and uses the TPostScriptCanvas object for a single page }
  145. TPostScript = class(TComponent)
  146. private
  147. FDocStarted : Boolean;
  148. FCreator : String;
  149. FStream : TStream;
  150. FCanvas: TPostScriptCanvas;
  151. FHeight: Integer;
  152. FLineSpacing: Integer;
  153. FPageNumber: Integer;
  154. FTitle: String;
  155. FWidth: Integer;
  156. FPatterns: TList; // array of pointers to pattern objects
  157. procedure SetHeight(const AValue: Integer);
  158. procedure SetLineSpacing(const AValue: Integer);
  159. procedure SetWidth(const AValue: Integer);
  160. procedure UpdateBoundingBox;
  161. procedure PatternChanged(Sender: TObject);
  162. procedure InsertPattern(APattern: TPSPattern); // adds the pattern to the postscript
  163. Procedure SetStream (Value : TStream);
  164. Function GetCreator : String;
  165. Protected
  166. Procedure WritePS(Const Cmd : String);
  167. Procedure WritePS(Const Fmt : String; Args : Array of Const);
  168. Procedure WriteDocumentHeader; virtual;
  169. Procedure WriteStandardFont; virtual;
  170. Procedure WritePage; virtual;
  171. Procedure FreePatterns;
  172. Procedure CheckStream;
  173. public
  174. Constructor Create(AOwner : TComponent);
  175. destructor Destroy; override;
  176. procedure AddPattern(APSPattern: TPSPattern);
  177. function FindPattern(AName: String): TPSPattern;
  178. function DelPattern(AName: String): Boolean;
  179. function NewPattern(AName: String): TPSPattern;
  180. property Canvas: TPostScriptCanvas read FCanvas;
  181. property Height: Integer read FHeight write SetHeight;
  182. property Width: Integer read FWidth write SetWidth;
  183. property PageNumber: Integer read FPageNumber;
  184. property Title: String read FTitle write FTitle;
  185. property LineSpacing: Integer read FLineSpacing write SetLineSpacing;
  186. procedure BeginDoc;
  187. procedure NewPage;
  188. procedure EndDoc;
  189. Property Stream : TStream Read FStream Write SetStream;
  190. Property Creator : String Read GetCreator Write FCreator;
  191. end;
  192. implementation
  193. Resourcestring
  194. SErrNoStreamAssigned = 'Invalid operation: No stream assigned';
  195. SErrDocumentAlreadyStarted = 'Cannot start document twice.';
  196. { TPostScriptCanvas ----------------------------------------------------------}
  197. Procedure TPostScriptCanvas.WritePS(const Cmd : String);
  198. begin
  199. If length(Cmd)>0 then
  200. FStream.Write(Cmd[1],Length(Cmd));
  201. FStream.Write(LineEnding,SizeOf(LineEnding));
  202. end;
  203. Procedure TPostScriptCanvas.WritePS(Const Fmt : String; Args : Array of Const);
  204. begin
  205. WritePS(Format(Fmt,Args));
  206. end;
  207. { Y coords in postscript are backwards... }
  208. function TPostScriptCanvas.TranslateY(Ycoord: Integer): Integer;
  209. begin
  210. Result:=Height-Ycoord;
  211. end;
  212. { Adds a fill finishing line to any path we desire to fill }
  213. procedure TPostScriptCanvas.AddFill;
  214. begin
  215. WritePs('gsave '+(Brush as TPSBrush).AsString+' fill grestore');
  216. end;
  217. { Return to last moveto location }
  218. procedure TPostScriptCanvas.ResetPos;
  219. begin
  220. WritePS(inttostr(LastX)+' '+inttostr(TranslateY(LastY))+' moveto');
  221. end;
  222. constructor TPostScriptCanvas.Create(AStream : TStream);
  223. begin
  224. inherited create;
  225. FStream:=AStream;
  226. Height := 792; // length of page in points at 72 ppi
  227. { // Choose a standard font in case the user doesn't
  228. FFontFace := 'AvantGarde-Book';
  229. SetFontSize(10);
  230. FLineSpacing := MPostScript.LineSpacing;
  231. end;
  232. FPen := TPSPen.Create;
  233. FPen.Width := 1;
  234. FPen.FPColor := 0;
  235. FPen.OnChange := @PenChanged;
  236. FBrush := TPSPen.Create;
  237. FBrush.Width := 1;
  238. FBrush.FPColor := -1;
  239. // don't notify us that the brush changed...
  240. }
  241. end;
  242. destructor TPostScriptCanvas.Destroy;
  243. begin
  244. {
  245. FPostScript.Free;
  246. FPen.Free;
  247. FBrush.Free;
  248. }
  249. inherited Destroy;
  250. end;
  251. procedure TPostScriptCanvas.SetWidth (AValue : integer);
  252. begin
  253. FWidth:=AValue;
  254. end;
  255. function TPostScriptCanvas.GetWidth : integer;
  256. begin
  257. Result:=FWidth;
  258. end;
  259. procedure TPostScriptCanvas.SetHeight (AValue : integer);
  260. begin
  261. FHeight:=AValue;
  262. end;
  263. function TPostScriptCanvas.GetHeight : integer;
  264. begin
  265. Result:=FHeight;
  266. end;
  267. { Move draw location }
  268. procedure TPostScriptCanvas.DoMoveTo(X1, Y1: Integer);
  269. var
  270. Y: Integer;
  271. begin
  272. Y := TranslateY(Y1);
  273. WritePS(inttostr(X1)+' '+inttostr(Y)+' moveto');
  274. LastX := X1;
  275. LastY := Y1;
  276. end;
  277. { Draw a line from current location to these coords }
  278. procedure TPostScriptCanvas.DoLineTo(X1, Y1: Integer);
  279. var
  280. Y: Integer;
  281. begin
  282. Y := TranslateY(Y1);
  283. WritePS(inttostr(X1)+' '+inttostr(Y)+' lineto');
  284. LastX := X1;
  285. LastY := Y1;
  286. end;
  287. procedure TPostScriptCanvas.DoLine(X1, Y1, X2, Y2: Integer);
  288. var
  289. Y12, Y22: Integer;
  290. begin
  291. Y12 := TranslateY(Y1);
  292. Y22 := TranslateY(Y2);
  293. WritePS('newpath '+inttostr(X1)+' '+inttostr(Y12)+' moveto '+
  294. inttostr(X2)+' '+inttostr(Y22)+' lineto closepath stroke');
  295. // go back to last moveto position
  296. ResetPos;
  297. end;
  298. { Draw a rectangle }
  299. procedure TPostScriptCanvas.DoRectangleFill(const Bounds: TRect);
  300. begin
  301. DrawRectangle(Bounds,true)
  302. end;
  303. procedure TPostScriptCanvas.DoRectangle(const Bounds: TRect);
  304. begin
  305. DrawRectangle(Bounds,False);
  306. end;
  307. procedure TPostScriptCanvas.DrawRectangle(const Bounds: TRect; DoFill : Boolean);
  308. var
  309. Y12, Y22: Integer;
  310. begin
  311. Y12 := TranslateY(Bounds.Top);
  312. Y22 := TranslateY(Bounds.Bottom);
  313. WritePS('stroke newpath');
  314. With Bounds do
  315. begin
  316. WritePS(inttostr(Left)+' '+inttostr(Y12)+' moveto');
  317. WritePS(inttostr(Right)+' '+inttostr(Y12)+' lineto');
  318. WritePS(inttostr(Right)+' '+inttostr(Y22)+' lineto');
  319. WritePS(inttostr(Left)+' '+inttostr(Y22)+' lineto');
  320. end;
  321. WritePS('closepath');
  322. If DoFill and (Brush.Style<>bsClear) then
  323. AddFill;
  324. WritePS('stroke');
  325. ResetPos;
  326. end;
  327. { Draw a series of lines }
  328. procedure TPostScriptCanvas.DoPolyline(Const Points: Array of TPoint);
  329. var
  330. i : Longint;
  331. begin
  332. MoveTo(Points[0].X, Points[0].Y);
  333. For i := 1 to High(Points) do
  334. LineTo(Points[i].X, Points[i].Y);
  335. ResetPos;
  336. end;
  337. { This was a pain to figure out... }
  338. procedure TPostScriptCanvas.DoEllipse(Const Bounds : TRect);
  339. begin
  340. DrawEllipse(Bounds,False);
  341. end;
  342. procedure TPostScriptCanvas.DoEllipseFill(Const Bounds : TRect);
  343. begin
  344. DrawEllipse(Bounds,true);
  345. end;
  346. procedure TPostScriptCanvas.DrawEllipse(Const Bounds : TRect; DoFill : Boolean);
  347. var
  348. radius: Integer;
  349. YRatio: Real;
  350. centerX, centerY: Integer;
  351. begin
  352. // set radius to half the width
  353. With Bounds do
  354. begin
  355. radius := (Right-Left) div 2;
  356. if radius <1 then
  357. exit;
  358. YRatio := (Bottom - Top) / (Right-Left);
  359. // find center
  360. CenterX := (Right+Left) div 2;
  361. CenterY := (Top+Bottom) div 2;
  362. end;
  363. WritePS('newpath '+inttostr(CenterX)+' '+inttostr(TranslateY(CenterY))+' translate');
  364. // move to edge
  365. WritePS(inttostr(radius)+' 0 moveto');
  366. // now draw it
  367. WritePS('gsave 1 '+format('%.3f',[YRatio])+' scale');
  368. WritePS('0 0 '+inttostr(radius)+' 0 360 arc');
  369. if DoFill and (Brush.Style<>bsClear) then
  370. AddFill;
  371. // reset scale for drawing line thickness so it doesn't warp
  372. YRatio := 1 / YRatio;
  373. WritePS('1 '+format('%.2f',[YRatio])+' scale stroke grestore');
  374. // move origin back
  375. WritePS(inttostr(-CenterX)+' '+inttostr(-TranslateY(CenterY))+' translate closepath stroke');
  376. ResetPos;
  377. end;
  378. procedure TPostScriptCanvas.DoPie(x, y, AWidth, AHeight, angle1, angle2: Integer);
  379. begin
  380. // set zero at center
  381. WritePS('newpath '+inttostr(X)+' '+inttostr(TranslateY(Y))+' translate');
  382. // scale it
  383. WritePS('gsave '+inttostr(AWidth)+' '+inttostr(Aheight)+' scale');
  384. //WritePS('gsave 1 1 scale');
  385. // draw line to edge
  386. WritePS('0 0 moveto');
  387. WritePS('0 0 1 '+inttostr(angle1)+' '+inttostr(angle2)+' arc closepath');
  388. if Brush.Style<>bsClear then
  389. AddFill;
  390. // reset scale so we don't change the line thickness
  391. // adding 0.01 to compensate for scaling error - there may be a deeper problem here...
  392. WritePS(format('%.6f',[(real(1) / X)+0.01])+' '+format('%.6f',[(real(1) / Y)+0.01])+' scale stroke grestore');
  393. // close out and return origin
  394. WritePS(inttostr(-X)+' '+inttostr(-TranslateY(Y))+' translate closepath stroke');
  395. resetpos;
  396. end;
  397. { Writes text with a carriage return }
  398. procedure TPostScriptCanvas.Writeln(AString: String);
  399. begin
  400. TextOut(LastX, LastY, AString);
  401. LastY := LastY+Font.Size+FLineSpacing;
  402. MoveTo(LastX, LastY);
  403. end;
  404. { Output text, restoring draw location }
  405. procedure TPostScriptCanvas.TextOut(X, Y: Integer; const Text: String);
  406. var
  407. Y1: Integer;
  408. begin
  409. Y1 := TranslateY(Y);
  410. WritePS(inttostr(X)+' '+inttostr(Y1)+' moveto');
  411. WritePS('('+Text+') show');
  412. ResetPos; // move back to last moveto location
  413. end;
  414. function TPostScriptCanvas.DoCreateDefaultFont : TFPCustomFont;
  415. begin
  416. Result:=TPSFont.Create;
  417. end;
  418. function TPostScriptCanvas.DoCreateDefaultPen : TFPCustomPen;
  419. begin
  420. Result:=TPSPen.Create;
  421. end;
  422. function TPostScriptCanvas.DoCreateDefaultBrush : TFPCustomBrush;
  423. begin
  424. Result:=TPSBrush.Create;
  425. end;
  426. { TPostScript -------------------------------------------------------------- }
  427. procedure TPostScript.SetHeight(const AValue: Integer);
  428. begin
  429. if FHeight=AValue then exit;
  430. FHeight:=AValue;
  431. UpdateBoundingBox;
  432. // filter down to the canvas height property
  433. if assigned(FCanvas) then
  434. FCanvas.Height := FHeight;
  435. end;
  436. procedure TPostScript.SetLineSpacing(const AValue: Integer);
  437. begin
  438. if FLineSpacing=AValue then exit;
  439. FLineSpacing:=AValue;
  440. // filter down to the canvas
  441. if assigned(FCanvas) then FCanvas.LineSpacing := AValue;
  442. end;
  443. procedure TPostScript.SetWidth(const AValue: Integer);
  444. begin
  445. if FWidth=AValue then exit;
  446. FWidth:=AValue;
  447. UpdateBoundingBox;
  448. end;
  449. { Take our sizes and change the boundingbox line }
  450. procedure TPostScript.UpdateBoundingBox;
  451. begin
  452. {
  453. // need to not hard-link this to line 1
  454. FDocument[1] := '%%BoundingBox: 0 0 '+inttostr(FWidth)+' '+inttostr(FHeight);
  455. }
  456. end;
  457. { Pattern changed so update the postscript code }
  458. procedure TPostScript.PatternChanged(Sender: TObject);
  459. begin
  460. // called anytime a pattern changes. Update the postscript code.
  461. // look for and delete the current postscript code for this pattern
  462. // then paste the pattern back into the code before the first page
  463. InsertPattern(Sender As TPSPattern);
  464. end;
  465. { Places a pattern definition into the bottom of the header in postscript }
  466. procedure TPostScript.InsertPattern(APattern: TPSPattern);
  467. var
  468. I, J: Integer;
  469. MyStrings: TStringList;
  470. begin
  471. { I := 0;
  472. if FDocument.Count < 1 then begin
  473. // added pattern when no postscript exists - this shouldn't happen
  474. raise exception.create('Pattern inserted with no postscript existing');
  475. exit;
  476. end;
  477. for I := 0 to FDocument.count - 1 do begin
  478. if (FDocument[I] = '%%Page: 1 1') then begin
  479. // found it!
  480. // insert into just before that
  481. MyStrings := APattern.GetPS;
  482. for J := 0 to MyStrings.Count - 1 do begin
  483. FDocument.Insert(I-1+J, MyStrings[j]);
  484. end;
  485. exit;
  486. end;
  487. end;
  488. }
  489. end;
  490. constructor TPostScript.Create(AOwner : TComponent);
  491. begin
  492. inherited create(AOwner);
  493. // Set some defaults
  494. FHeight := 792; // 11 inches at 72 dpi
  495. FWidth := 612; // 8 1/2 inches at 72 dpi
  496. end;
  497. Procedure TPostScript.WritePS(const Cmd : String);
  498. begin
  499. If length(Cmd)>0 then
  500. FStream.Write(Cmd[1],Length(Cmd));
  501. FStream.Write(LineEnding,SizeOf(LineEnding));
  502. end;
  503. Procedure TPostScript.WritePS(Const Fmt : String; Args : Array of Const);
  504. begin
  505. WritePS(Format(Fmt,Args));
  506. end;
  507. Procedure TPostScript.WriteDocumentHeader;
  508. begin
  509. WritePS('%!PS-Adobe-3.0');
  510. WritePS('%%BoundingBox: 0 0 612 792');
  511. WritePS('%%Creator: '+Creator);
  512. WritePS('%%Title: '+FTitle);
  513. WritePS('%%Pages: (atend)');
  514. WritePS('%%PageOrder: Ascend');
  515. WriteStandardFont;
  516. end;
  517. Procedure TPostScript.WriteStandardFont;
  518. begin
  519. // Choose a standard font in case the user doesn't
  520. WritePS('/AvantGarde-Book findfont');
  521. WritePS('10 scalefont');
  522. WritePS('setfont');
  523. end;
  524. Procedure TPostScript.FreePatterns;
  525. Var
  526. i : Integer;
  527. begin
  528. If Assigned(FPatterns) then
  529. begin
  530. For I:=0 to FPatterns.Count-1 do
  531. TObject(FPatterns[i]).Free;
  532. FreeAndNil(FPatterns);
  533. end;
  534. end;
  535. destructor TPostScript.Destroy;
  536. begin
  537. Stream:=Nil;
  538. FreePatterns;
  539. inherited Destroy;
  540. end;
  541. { add a pattern to the array }
  542. procedure TPostScript.AddPattern(APSPattern: TPSPattern);
  543. begin
  544. If Not Assigned(FPatterns) then
  545. FPatterns:=Tlist.Create;
  546. FPatterns.Add(APSPattern);
  547. end;
  548. { Find a pattern object by it's name }
  549. function TPostScript.FindPattern(AName: String): TPSPattern;
  550. var
  551. I: Integer;
  552. begin
  553. Result := nil;
  554. If Assigned(FPatterns) then
  555. begin
  556. I:=Fpatterns.Count-1;
  557. While (Result=Nil) and (I>=0) do
  558. if TPSPattern(FPatterns[I]).Name = AName then
  559. result := TPSPattern(FPatterns[i])
  560. else
  561. Dec(i)
  562. end;
  563. end;
  564. function TPostScript.DelPattern(AName: String): Boolean;
  565. begin
  566. // can't do that yet...
  567. Result:=false;
  568. end;
  569. { Create a new pattern and inserts it into the array for safe keeping }
  570. function TPostScript.NewPattern(AName: String): TPSPattern;
  571. var
  572. MyPattern: TPSPattern;
  573. begin
  574. MyPattern := TPSPattern.Create;
  575. AddPattern(MyPattern);
  576. MyPattern.Name := AName;
  577. MyPattern.OnChange := @PatternChanged;
  578. MyPattern.OldName := '';
  579. // add this to the postscript now...
  580. InsertPattern(MyPattern);
  581. result := MyPattern;
  582. end;
  583. { Start a new document }
  584. procedure TPostScript.BeginDoc;
  585. var
  586. I: Integer;
  587. begin
  588. CheckStream;
  589. If FDocStarted then
  590. Raise Exception.Create(SErrDocumentAlreadyStarted);
  591. FCanvas:=TPostScriptCanvas.Create(FStream);
  592. FCanvas.Height:=Self.Height;
  593. FCanvas.Width:=Self.width;
  594. FreePatterns;
  595. WriteDocumentHeader;
  596. // start our first page
  597. FPageNumber := 1;
  598. WritePage;
  599. UpdateBoundingBox;
  600. end;
  601. Procedure TPostScript.WritePage;
  602. begin
  603. WritePS('%%Page: '+inttostr(FPageNumber)+' '+inttostr(FPageNumber));
  604. WritePS('newpath');
  605. end;
  606. { Copy current page into the postscript and start a new one }
  607. procedure TPostScript.NewPage;
  608. begin
  609. // dump the current page into our postscript first
  610. // put end page definition...
  611. WritePS('stroke');
  612. WritePS('showpage');
  613. FPageNumber := FPageNumber+1;
  614. WritePage;
  615. end;
  616. { Finish off the document }
  617. procedure TPostScript.EndDoc;
  618. begin
  619. // Start printing the document after closing out the pages
  620. WritePS('stroke');
  621. WritePS('showpage');
  622. WritePS('%%Pages: '+inttostr(FPageNumber));
  623. // okay, the postscript is all ready, so dump it to the text file
  624. // or to the printer
  625. FDocStarted:=False;
  626. FreeAndNil(FCanvas);
  627. end;
  628. Function TPostScript.GetCreator : String;
  629. begin
  630. If (FCreator='') then
  631. Result:=ClassName
  632. else
  633. Result:=FCreator;
  634. end;
  635. Procedure TPostScript.SetStream (Value : TStream);
  636. begin
  637. if (FStream<>Value) then
  638. begin
  639. If (FStream<>Nil) and FDocStarted then
  640. EndDoc;
  641. FStream:=Value;
  642. FDocStarted:=False;
  643. end;
  644. end;
  645. Procedure TPostScript.CheckStream;
  646. begin
  647. If Not Assigned(FStream) then
  648. Raise Exception.Create(SErrNoStreamAssigned);
  649. end;
  650. { TPSPen }
  651. procedure TPSPen.SetPattern(const AValue: TPSPattern);
  652. begin
  653. if FPattern<>AValue then
  654. begin
  655. FPattern:=AValue;
  656. // NotifyCanvas;
  657. end;
  658. end;
  659. destructor TPSPen.Destroy;
  660. begin
  661. // Do NOT free the pattern object from here...
  662. inherited Destroy;
  663. end;
  664. { Return the pen definition as a postscript string }
  665. function TPSPen.AsString: String;
  666. begin
  667. Result:='';
  668. if FPattern <> nil then
  669. begin
  670. if FPattern.PaintType = ptColored then
  671. Result:='/Pattern setcolorspace '+FPattern.Name+' setcolor '
  672. else
  673. begin
  674. Result:='[/Pattern /DeviceRGB] setcolorspace '+inttostr(FPColor.Red)+' '+inttostr(FPColor.Green)+' '+
  675. inttostr(FPColor.Blue)+' '+FPattern.Name+' setcolor ';
  676. end;
  677. end
  678. else // no pattern do this:
  679. Result:=inttostr(FPColor.Red)+' '+inttostr(FPColor.Green)+' '+
  680. inttostr(FPColor.Blue)+' setrgbcolor ';
  681. Result := Result + format('%f',[Width])+' setlinewidth ';
  682. end;
  683. { TPSPattern }
  684. { Returns the pattern definition as postscript }
  685. function TPSPattern.GetpostScript: TStringList;
  686. var
  687. I: Integer;
  688. S : String;
  689. begin
  690. // If nothing in the canvas, error
  691. if FStream.Size=0 then
  692. raise exception.create('Empty pattern');
  693. FPostScript.Clear;
  694. With FPostScript do
  695. begin
  696. add('%% PATTERN '+FName);
  697. add('/'+FName+'proto 12 dict def '+FName+'proto begin');
  698. add('/PatternType 1 def');
  699. add(Format('/PaintType %d def',[ord(FPaintType)+1]));
  700. add(Format('/TilingType %d def',[ord(FTilingType)+1]));
  701. add('/BBox ['+inttostr(FBBox.Left)+' '+inttostr(FBBox.Top)+' '+inttostr(FBBox.Right)+' '+inttostr(FBBox.Bottom)+'] def');
  702. add('/XStep '+format('%f',[FXStep])+' def');
  703. add('/YStep '+format('%f',[FYstep])+' def');
  704. add('/PaintProc { begin');
  705. // insert the canvas
  706. SetLength(S,FStream.Size);
  707. FStream.Seek(0,soFromBeginning);
  708. FStream.Read(S[1],FStream.Size);
  709. Add(S);
  710. // add support for custom matrix later
  711. add('end } def end '+FName+'proto [1 0 0 1 0 0] makepattern /'+FName+' exch def');
  712. add('%% END PATTERN '+FName);
  713. end;
  714. Result := FPostScript;
  715. end;
  716. procedure TPSPattern.SetBBox(const AValue: TRect);
  717. begin
  718. { if FBBox<>AValue then
  719. begin
  720. FBBox:=AValue;
  721. FPatternCanvas.Height := FBBox.Bottom - FBBox.Top;
  722. // NotifyCanvas;
  723. end;
  724. }
  725. end;
  726. procedure TPSPattern.SetName(const AValue: String);
  727. begin
  728. FOldName := FName;
  729. if (FName<>AValue) then
  730. begin
  731. FName:=AValue;
  732. // NotifyCanvas;
  733. end;
  734. end;
  735. procedure TPSPattern.Changed;
  736. begin
  737. if Assigned(FOnChange) then FOnChange(Self);
  738. end;
  739. procedure TPSPattern.SetPaintType(const AValue: TPSPaintType);
  740. begin
  741. if FPaintType=AValue then exit;
  742. FPaintType:=AValue;
  743. changed;
  744. end;
  745. procedure TPSPattern.SetTilingType(const AValue: TPSTileType);
  746. begin
  747. if FTilingType=AValue then exit;
  748. FTilingType:=AValue;
  749. changed;
  750. end;
  751. procedure TPSPattern.SetXStep(const AValue: Real);
  752. begin
  753. if FXStep=AValue then exit;
  754. FXStep:=AValue;
  755. changed;
  756. end;
  757. procedure TPSPattern.SetYStep(const AValue: Real);
  758. begin
  759. if FYStep=AValue then exit;
  760. FYStep:=AValue;
  761. changed;
  762. end;
  763. constructor TPSPattern.Create;
  764. begin
  765. FPostScript := TStringList.Create;
  766. FPaintType := ptColored;
  767. FTilingType := ttConstant;
  768. FStream:=TmemoryStream.Create;
  769. FPatternCanvas := TPostScriptCanvas.Create(FStream);
  770. FName := 'Pattern1';
  771. end;
  772. destructor TPSPattern.Destroy;
  773. begin
  774. FPostScript.Free;
  775. FPatternCanvas.Free;
  776. FStream.Free;
  777. inherited Destroy;
  778. end;
  779. { ---------------------------------------------------------------------
  780. TPSBrush
  781. ---------------------------------------------------------------------}
  782. Function TPSBrush.GetAsString : String;
  783. begin
  784. Result:='';
  785. end;
  786. end.