fpcanvas.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2003 by the Free Pascal development team
  4. Basic canvas definitions.
  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. {$mode objfpc}{$h+}
  12. unit FPCanvas;
  13. interface
  14. uses sysutils, classes, FPImage;
  15. const
  16. PatternBitCount = sizeof(longword) * 8;
  17. type
  18. TFPCanvasException = class (Exception);
  19. TFPPenException = class (TFPCanvasException);
  20. TFPBrushException = class (TFPCanvasException);
  21. TFPFontException = class (TFPCanvasException);
  22. TFPCustomCanvas = class;
  23. { TFPCanvasHelper }
  24. TFPCanvasHelper = class(TPersistent)
  25. private
  26. FDelayAllocate: boolean;
  27. FFPColor : TFPColor;
  28. FAllocated,
  29. FFixedCanvas : boolean;
  30. FCanvas : TFPCustomCanvas;
  31. FFlags : word;
  32. FOnChange: TNotifyEvent;
  33. FOnChanging: TNotifyEvent;
  34. procedure NotifyCanvas;
  35. protected
  36. // flags 0-15 are reserved for FPCustomCanvas
  37. function GetAllocated: boolean; virtual;
  38. procedure SetFlags (index:integer; AValue:boolean); virtual;
  39. function GetFlags (index:integer) : boolean; virtual;
  40. procedure CheckAllocated (ValueNeeded:boolean);
  41. procedure SetFixedCanvas (AValue : boolean);
  42. procedure DoAllocateResources; virtual;
  43. procedure DoDeAllocateResources; virtual;
  44. procedure DoCopyProps (From:TFPCanvasHelper); virtual;
  45. procedure SetFPColor (const AValue:TFPColor); virtual;
  46. procedure Changing; dynamic;
  47. procedure Changed; dynamic;
  48. Procedure Lock;
  49. Procedure UnLock;
  50. public
  51. constructor Create; virtual;
  52. destructor Destroy; override;
  53. // prepare helper for use
  54. procedure AllocateResources (ACanvas : TFPCustomCanvas;
  55. CanDelay: boolean = true);
  56. // free all resource used by this helper
  57. procedure DeallocateResources;
  58. property Allocated : boolean read GetAllocated;
  59. // properties cannot be changed when allocated
  60. property FixedCanvas : boolean read FFixedCanvas;
  61. // Canvas for which the helper is allocated
  62. property Canvas : TFPCustomCanvas read FCanvas;
  63. // color of the helper
  64. property FPColor : TFPColor read FFPColor Write SetFPColor;
  65. property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
  66. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  67. property DelayAllocate: boolean read FDelayAllocate write FDelayAllocate;
  68. end;
  69. TFPCustomFont = class (TFPCanvasHelper)
  70. private
  71. FName : string;
  72. FSize : integer;
  73. protected
  74. procedure DoCopyProps (From:TFPCanvasHelper); override;
  75. procedure SetName (AValue:string); virtual;
  76. procedure SetSize (AValue:integer); virtual;
  77. public
  78. function CopyFont : TFPCustomFont;
  79. // Creates a copy of the font with all properties the same, but not allocated
  80. procedure GetTextSize (text:string; var w,h:integer);
  81. function GetTextHeight (text:string) : integer;
  82. function GetTextWidth (text:string) : integer;
  83. property Name : string read FName write SetName;
  84. property Size : integer read FSize write SetSize;
  85. property Bold : boolean index 5 read GetFlags write SetFlags;
  86. property Italic : boolean index 6 read GetFlags write SetFlags;
  87. property Underline : boolean index 7 read GetFlags write SetFlags;
  88. property StrikeTrough : boolean index 8 read GetFlags write SetFlags;
  89. end;
  90. TFPCustomFontClass = class of TFPCustomFont;
  91. TFPPenStyle = (psSolid, psDash, psDot, psDashDot, psDashDotDot, psinsideFrame, psPattern,psClear);
  92. TFPPenStyleSet = set of TFPPenStyle;
  93. TFPPenMode = (pmBlack, pmWhite, pmNop, pmNot, pmCopy, pmNotCopy,
  94. pmMergePenNot, pmMaskPenNot, pmMergeNotPen, pmMaskNotPen, pmMerge,
  95. pmNotMerge, pmMask, pmNotMask, pmXor, pmNotXor);
  96. TPenPattern = Longword;
  97. TFPCustomPen = class (TFPCanvasHelper)
  98. private
  99. FStyle : TFPPenStyle;
  100. FWidth : Integer;
  101. FMode : TFPPenMode;
  102. FPattern : longword;
  103. protected
  104. procedure DoCopyProps (From:TFPCanvasHelper); override;
  105. procedure SetMode (AValue : TFPPenMode); virtual;
  106. procedure SetWidth (AValue : Integer); virtual;
  107. procedure SetStyle (AValue : TFPPenStyle); virtual;
  108. procedure SetPattern (AValue : longword); virtual;
  109. public
  110. function CopyPen : TFPCustomPen;
  111. // Creates a copy of the pen with all properties the same, but not allocated
  112. property Style : TFPPenStyle read FStyle write SetStyle;
  113. property Width : Integer read FWidth write SetWidth;
  114. property Mode : TFPPenMode read FMode write SetMode;
  115. property Pattern : longword read FPattern write SetPattern;
  116. end;
  117. TFPCustomPenClass = class of TFPCustomPen;
  118. TFPBrushStyle = (bsSolid, bsClear, bsHorizontal, bsVertical, bsFDiagonal,
  119. bsBDiagonal, bsCross, bsDiagCross, bsImage, bsPattern);
  120. TBrushPattern = array[0..PatternBitCount-1] of TPenPattern;
  121. PBrushPattern = ^TBrushPattern;
  122. TFPCustomBrush = class (TFPCanvasHelper)
  123. private
  124. FStyle : TFPBrushStyle;
  125. FImage : TFPCustomImage;
  126. FPattern : TBrushPattern;
  127. protected
  128. procedure SetStyle (AValue : TFPBrushStyle); virtual;
  129. procedure SetImage (AValue : TFPCustomImage); virtual;
  130. procedure DoCopyProps (From:TFPCanvasHelper); override;
  131. public
  132. function CopyBrush : TFPCustomBrush;
  133. property Style : TFPBrushStyle read FStyle write SetStyle;
  134. property Image : TFPCustomImage read FImage write SetImage;
  135. property Pattern : TBrushPattern read FPattern write FPattern;
  136. end;
  137. TFPCustomBrushClass = class of TFPCustomBrush;
  138. { TFPCustomCanvas }
  139. TFPCustomCanvas = class(TPersistent)
  140. private
  141. FClipping,
  142. FManageResources: boolean;
  143. FRemovingHelpers : boolean;
  144. FDefaultFont,
  145. FFont : TFPCustomFont;
  146. FDefaultBrush,
  147. FBrush : TFPCustomBrush;
  148. FDefaultPen,
  149. FPen : TFPCustomPen;
  150. FPenPos : TPoint;
  151. FClipRect : TRect;
  152. FHelpers : TList;
  153. FLocks : integer;
  154. function AllowFont (AFont : TFPCustomFont) : boolean;
  155. function AllowBrush (ABrush : TFPCustomBrush) : boolean;
  156. function AllowPen (APen : TFPCustomPen) : boolean;
  157. function CreateDefaultFont : TFPCustomFont;
  158. function CreateDefaultPen : TFPCustomPen;
  159. function CreateDefaultBrush : TFPCustomBrush;
  160. procedure RemoveHelpers;
  161. function GetFont : TFPCustomFont;
  162. function GetBrush : TFPCustomBrush;
  163. function GetPen : TFPCustomPen;
  164. protected
  165. function DoCreateDefaultFont : TFPCustomFont; virtual; abstract;
  166. function DoCreateDefaultPen : TFPCustomPen; virtual; abstract;
  167. function DoCreateDefaultBrush : TFPCustomBrush; virtual; abstract;
  168. procedure SetFont (AValue:TFPCustomFont); virtual;
  169. procedure SetBrush (AValue:TFPCustomBrush); virtual;
  170. procedure SetPen (AValue:TFPCustomPen); virtual;
  171. function DoAllowFont (AFont : TFPCustomFont) : boolean; virtual;
  172. function DoAllowPen (APen : TFPCustomPen) : boolean; virtual;
  173. function DoAllowBrush (ABrush : TFPCustomBrush) : boolean; virtual;
  174. procedure SetColor (x,y:integer; const Value:TFPColor); Virtual; abstract;
  175. function GetColor (x,y:integer) : TFPColor; Virtual; abstract;
  176. procedure SetHeight (AValue : integer); virtual; abstract;
  177. function GetHeight : integer; virtual; abstract;
  178. procedure SetWidth (AValue : integer); virtual; abstract;
  179. function GetWidth : integer; virtual; abstract;
  180. function GetClipRect: TRect; virtual;
  181. procedure SetClipRect(const AValue: TRect); virtual;
  182. procedure SetPenPos(const AValue: TPoint); virtual;
  183. procedure DoLockCanvas; virtual;
  184. procedure DoUnlockCanvas; virtual;
  185. procedure DoTextOut (x,y:integer;text:string); virtual; abstract;
  186. procedure DoGetTextSize (text:string; var w,h:integer); virtual; abstract;
  187. function DoGetTextHeight (text:string) : integer; virtual; abstract;
  188. function DoGetTextWidth (text:string) : integer; virtual; abstract;
  189. procedure DoRectangle (Const Bounds:TRect); virtual; abstract;
  190. procedure DoRectangleFill (Const Bounds:TRect); virtual; abstract;
  191. procedure DoRectangleAndFill (Const Bounds:TRect); virtual;
  192. procedure DoEllipseFill (Const Bounds:TRect); virtual; abstract;
  193. procedure DoEllipse (Const Bounds:TRect); virtual; abstract;
  194. procedure DoEllipseAndFill (Const Bounds:TRect); virtual;
  195. procedure DoPolygonFill (const points:array of TPoint); virtual; abstract;
  196. procedure DoPolygon (const points:array of TPoint); virtual; abstract;
  197. procedure DoPolygonAndFill (const points:array of TPoint); virtual;
  198. procedure DoPolyline (const points:array of TPoint); virtual; abstract;
  199. procedure DoFloodFill (x,y:integer); virtual; abstract;
  200. procedure DoMoveTo (x,y:integer); virtual;
  201. procedure DoLineTo (x,y:integer); virtual;
  202. procedure DoLine (x1,y1,x2,y2:integer); virtual; abstract;
  203. procedure DoCopyRect (x,y:integer; canvas:TFPCustomCanvas; Const SourceRect:TRect); virtual; abstract;
  204. procedure DoDraw (x,y:integer; Const image:TFPCustomImage); virtual; abstract;
  205. procedure CheckHelper (AHelper:TFPCanvasHelper); virtual;
  206. procedure AddHelper (AHelper:TFPCanvasHelper);
  207. public
  208. constructor create;
  209. destructor destroy; override;
  210. procedure LockCanvas;
  211. procedure UnlockCanvas;
  212. function Locked: boolean;
  213. function CreateFont : TFPCustomFont;
  214. function CreatePen : TFPCustomPen;
  215. function CreateBrush : TFPCustomBrush;
  216. // using font
  217. procedure TextOut (x,y:integer;text:string);
  218. procedure GetTextSize (text:string; var w,h:integer);
  219. function GetTextHeight (text:string) : integer;
  220. function GetTextWidth (text:string) : integer;
  221. // using pen and brush
  222. procedure Ellipse (Const Bounds:TRect);
  223. procedure Ellipse (left,top,right,bottom:integer);
  224. procedure EllipseC (x,y:integer; rx,ry:longword);
  225. procedure Polygon (Const points:array of TPoint);
  226. procedure Polyline (Const points:array of TPoint);
  227. procedure Rectangle (Const Bounds:TRect);
  228. procedure Rectangle (left,top,right,bottom:integer);
  229. // using brush
  230. procedure FloodFill (x,y:integer);
  231. procedure Clear;
  232. // using pen
  233. procedure MoveTo (x,y:integer);
  234. procedure MoveTo (p:TPoint);
  235. procedure LineTo (x,y:integer);
  236. procedure LineTo (p:TPoint);
  237. procedure Line (x1,y1,x2,y2:integer);
  238. procedure Line (const p1,p2:TPoint);
  239. procedure Line (const points:TRect);
  240. // other procedures
  241. procedure CopyRect (x,y:integer; canvas:TFPCustomCanvas; SourceRect:TRect);
  242. procedure Draw (x,y:integer; image:TFPCustomImage);
  243. procedure Erase;virtual;
  244. // properties
  245. property Font : TFPCustomFont read GetFont write SetFont;
  246. property Pen : TFPCustomPen read GetPen write SetPen;
  247. property Brush : TFPCustomBrush read GetBrush write SetBrush;
  248. property Colors [x,y:integer] : TFPColor read GetColor write SetColor;
  249. property ClipRect : TRect read GetClipRect write SetClipRect;
  250. property Clipping : boolean read FClipping write FClipping;
  251. property PenPos : TPoint read FPenPos write SetPenPos;
  252. property Height : integer read GetHeight write SetHeight;
  253. property Width : integer read GetWidth write SetWidth;
  254. property ManageResources: boolean read FManageResources write FManageResources;
  255. end;
  256. TFPCustomDrawFont = class (TFPCustomFont)
  257. private
  258. procedure DrawText (x,y:integer; text:string);
  259. procedure GetTextSize (text:string; var w,h:integer);
  260. function GetTextHeight (text:string) : integer;
  261. function GetTextWidth (text:string) : integer;
  262. protected
  263. procedure DoDrawText (x,y:integer; text:string); virtual; abstract;
  264. procedure DoGetTextSize (text:string; var w,h:integer); virtual; abstract;
  265. function DoGetTextHeight (text:string) : integer; virtual; abstract;
  266. function DoGetTextWidth (text:string) : integer; virtual; abstract;
  267. end;
  268. TFPEmptyFont = class (TFPCustomFont)
  269. end;
  270. TFPCustomDrawPen = class (TFPCustomPen)
  271. private
  272. procedure DrawLine (x1,y1,x2,y2:integer);
  273. procedure Polyline (const points:array of TPoint; close:boolean);
  274. procedure Ellipse (left,top, right,bottom:integer);
  275. procedure Rectangle (left,top, right,bottom:integer);
  276. protected
  277. procedure DoDrawLine (x1,y1,x2,y2:integer); virtual; abstract;
  278. procedure DoPolyline (const points:array of TPoint; close:boolean); virtual; abstract;
  279. procedure DoEllipse (left,top, right,bottom:integer); virtual; abstract;
  280. procedure DoRectangle (left,top, right,bottom:integer); virtual; abstract;
  281. end;
  282. TFPEmptyPen = class (TFPCustomPen)
  283. end;
  284. TFPCustomDrawBrush = class (TFPCustomBrush)
  285. private
  286. procedure Rectangle (left,top, right,bottom:integer);
  287. procedure FloodFill (x,y:integer);
  288. procedure Ellipse (left,top, right,bottom:integer);
  289. procedure Polygon (const points:array of TPoint);
  290. public
  291. procedure DoRectangle (left,top, right,bottom:integer); virtual; abstract;
  292. procedure DoEllipse (left,top, right,bottom:integer); virtual; abstract;
  293. procedure DoFloodFill (x,y:integer); virtual; abstract;
  294. procedure DoPolygon (const points:array of TPoint); virtual; abstract;
  295. end;
  296. TFPEmptyBrush = class (TFPCustomBrush)
  297. end;
  298. procedure DecRect (var rect : TRect; delta:integer);
  299. procedure IncRect (var rect : TRect; delta:integer);
  300. procedure DecRect (var rect : TRect);
  301. procedure IncRect (var rect : TRect);
  302. implementation
  303. uses clipping;
  304. const
  305. EFont = 'Font';
  306. EPen = 'Pen';
  307. EBrush = 'Brush';
  308. ErrAllocation = '%s %s be allocated.';
  309. ErrAlloc : array [boolean] of string = ('may not','must');
  310. ErrCouldNotCreate = 'Could not create a %s.';
  311. ErrNoLock = 'Canvas not locked.';
  312. procedure DecRect (var rect : TRect; delta:integer);
  313. begin
  314. with rect do
  315. begin
  316. left := left + delta;
  317. right := right - delta;
  318. top := top + delta;
  319. bottom := bottom - delta;
  320. end;
  321. end;
  322. procedure DecRect (var rect : trect);
  323. begin
  324. DecRect (rect, 1);
  325. end;
  326. procedure IncRect (var rect : trect);
  327. begin
  328. IncRect (rect, 1);
  329. end;
  330. procedure IncRect (var rect : TRect; delta:integer);
  331. begin
  332. with rect do
  333. begin
  334. left := left - delta;
  335. right := right + delta;
  336. top := top - delta;
  337. bottom := bottom + delta;
  338. end;
  339. end;
  340. {$i FPHelper.inc}
  341. {$i FPFont.inc}
  342. {$i FPPen.inc}
  343. {$i FPBrush.inc}
  344. {$i FPCanvas.inc}
  345. {$i FPCDrawH.inc}
  346. end.