fpcanvas.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. { TFPCustomInterpolation }
  139. TFPCustomInterpolation = class
  140. private
  141. fcanvas: TFPCustomCanvas;
  142. fimage: TFPCustomImage;
  143. protected
  144. procedure Initialize (aimage:TFPCustomImage; acanvas:TFPCustomCanvas); virtual;
  145. procedure Execute (x,y,w,h:integer); virtual; abstract;
  146. public
  147. property Canvas : TFPCustomCanvas read fcanvas;
  148. property Image : TFPCustomImage read fimage;
  149. end;
  150. { TFPBaseInterpolation }
  151. TFPBaseInterpolation = class (TFPCustomInterpolation)
  152. private
  153. xfactor, yfactor : double;
  154. xsupport,ysupport : double;
  155. tempimage : TFPCustomImage;
  156. procedure Horizontal (width : integer);
  157. procedure vertical (dx,dy,width,height: integer);
  158. protected
  159. procedure Execute (x,y,w,h:integer); override;
  160. function Filter (x : double) : double; virtual; abstract;
  161. function MaxSupport : double; virtual; abstract;
  162. end;
  163. { TMitchelInterpolation }
  164. TMitchelInterpolation = class (TFPBaseInterpolation)
  165. protected
  166. function Filter (x : double) : double; override;
  167. function MaxSupport : double; override;
  168. end;
  169. { TFPCustomCanvas }
  170. TFPCustomCanvas = class(TPersistent)
  171. private
  172. FClipping,
  173. FManageResources: boolean;
  174. FRemovingHelpers : boolean;
  175. FDefaultFont,
  176. FFont : TFPCustomFont;
  177. FDefaultBrush,
  178. FBrush : TFPCustomBrush;
  179. FDefaultPen,
  180. FPen : TFPCustomPen;
  181. FPenPos : TPoint;
  182. FClipRect : TRect;
  183. FHelpers : TList;
  184. FLocks : integer;
  185. FInterpolation : TFPCustomInterpolation;
  186. function AllowFont (AFont : TFPCustomFont) : boolean;
  187. function AllowBrush (ABrush : TFPCustomBrush) : boolean;
  188. function AllowPen (APen : TFPCustomPen) : boolean;
  189. function CreateDefaultFont : TFPCustomFont;
  190. function CreateDefaultPen : TFPCustomPen;
  191. function CreateDefaultBrush : TFPCustomBrush;
  192. procedure RemoveHelpers;
  193. function GetFont : TFPCustomFont;
  194. function GetBrush : TFPCustomBrush;
  195. function GetPen : TFPCustomPen;
  196. protected
  197. function DoCreateDefaultFont : TFPCustomFont; virtual; abstract;
  198. function DoCreateDefaultPen : TFPCustomPen; virtual; abstract;
  199. function DoCreateDefaultBrush : TFPCustomBrush; virtual; abstract;
  200. procedure SetFont (AValue:TFPCustomFont); virtual;
  201. procedure SetBrush (AValue:TFPCustomBrush); virtual;
  202. procedure SetPen (AValue:TFPCustomPen); virtual;
  203. function DoAllowFont (AFont : TFPCustomFont) : boolean; virtual;
  204. function DoAllowPen (APen : TFPCustomPen) : boolean; virtual;
  205. function DoAllowBrush (ABrush : TFPCustomBrush) : boolean; virtual;
  206. procedure SetColor (x,y:integer; const Value:TFPColor); Virtual; abstract;
  207. function GetColor (x,y:integer) : TFPColor; Virtual; abstract;
  208. procedure SetHeight (AValue : integer); virtual; abstract;
  209. function GetHeight : integer; virtual; abstract;
  210. procedure SetWidth (AValue : integer); virtual; abstract;
  211. function GetWidth : integer; virtual; abstract;
  212. function GetClipRect: TRect; virtual;
  213. procedure SetClipRect(const AValue: TRect); virtual;
  214. procedure SetPenPos(const AValue: TPoint); virtual;
  215. procedure DoLockCanvas; virtual;
  216. procedure DoUnlockCanvas; virtual;
  217. procedure DoTextOut (x,y:integer;text:string); virtual; abstract;
  218. procedure DoGetTextSize (text:string; var w,h:integer); virtual; abstract;
  219. function DoGetTextHeight (text:string) : integer; virtual; abstract;
  220. function DoGetTextWidth (text:string) : integer; virtual; abstract;
  221. procedure DoRectangle (Const Bounds:TRect); virtual; abstract;
  222. procedure DoRectangleFill (Const Bounds:TRect); virtual; abstract;
  223. procedure DoRectangleAndFill (Const Bounds:TRect); virtual;
  224. procedure DoEllipseFill (Const Bounds:TRect); virtual; abstract;
  225. procedure DoEllipse (Const Bounds:TRect); virtual; abstract;
  226. procedure DoEllipseAndFill (Const Bounds:TRect); virtual;
  227. procedure DoPolygonFill (const points:array of TPoint); virtual; abstract;
  228. procedure DoPolygon (const points:array of TPoint); virtual; abstract;
  229. procedure DoPolygonAndFill (const points:array of TPoint); virtual;
  230. procedure DoPolyline (const points:array of TPoint); virtual; abstract;
  231. procedure DoFloodFill (x,y:integer); virtual; abstract;
  232. procedure DoMoveTo (x,y:integer); virtual;
  233. procedure DoLineTo (x,y:integer); virtual;
  234. procedure DoLine (x1,y1,x2,y2:integer); virtual; abstract;
  235. procedure DoCopyRect (x,y:integer; canvas:TFPCustomCanvas; Const SourceRect:TRect); virtual; abstract;
  236. procedure DoDraw (x,y:integer; Const image:TFPCustomImage); virtual; abstract;
  237. procedure CheckHelper (AHelper:TFPCanvasHelper); virtual;
  238. procedure AddHelper (AHelper:TFPCanvasHelper);
  239. public
  240. constructor create;
  241. destructor destroy; override;
  242. procedure LockCanvas;
  243. procedure UnlockCanvas;
  244. function Locked: boolean;
  245. function CreateFont : TFPCustomFont;
  246. function CreatePen : TFPCustomPen;
  247. function CreateBrush : TFPCustomBrush;
  248. // using font
  249. procedure TextOut (x,y:integer;text:string);
  250. procedure GetTextSize (text:string; var w,h:integer);
  251. function GetTextHeight (text:string) : integer;
  252. function GetTextWidth (text:string) : integer;
  253. // using pen and brush
  254. procedure Ellipse (Const Bounds:TRect);
  255. procedure Ellipse (left,top,right,bottom:integer);
  256. procedure EllipseC (x,y:integer; rx,ry:longword);
  257. procedure Polygon (Const points:array of TPoint);
  258. procedure Polyline (Const points:array of TPoint);
  259. procedure Rectangle (Const Bounds:TRect);
  260. procedure Rectangle (left,top,right,bottom:integer);
  261. // using brush
  262. procedure FloodFill (x,y:integer);
  263. procedure Clear;
  264. // using pen
  265. procedure MoveTo (x,y:integer);
  266. procedure MoveTo (p:TPoint);
  267. procedure LineTo (x,y:integer);
  268. procedure LineTo (p:TPoint);
  269. procedure Line (x1,y1,x2,y2:integer);
  270. procedure Line (const p1,p2:TPoint);
  271. procedure Line (const points:TRect);
  272. // other procedures
  273. procedure CopyRect (x,y:integer; canvas:TFPCustomCanvas; SourceRect:TRect);
  274. procedure Draw (x,y:integer; image:TFPCustomImage);
  275. procedure StretchDraw (x,y,w,h:integer; source:TFPCustomImage);
  276. procedure Erase;virtual;
  277. // properties
  278. property Font : TFPCustomFont read GetFont write SetFont;
  279. property Pen : TFPCustomPen read GetPen write SetPen;
  280. property Brush : TFPCustomBrush read GetBrush write SetBrush;
  281. property Interpolation : TFPCustomInterpolation read FInterpolation write FInterpolation;
  282. property Colors [x,y:integer] : TFPColor read GetColor write SetColor;
  283. property ClipRect : TRect read GetClipRect write SetClipRect;
  284. property Clipping : boolean read FClipping write FClipping;
  285. property PenPos : TPoint read FPenPos write SetPenPos;
  286. property Height : integer read GetHeight write SetHeight;
  287. property Width : integer read GetWidth write SetWidth;
  288. property ManageResources: boolean read FManageResources write FManageResources;
  289. end;
  290. TFPCustomDrawFont = class (TFPCustomFont)
  291. private
  292. procedure DrawText (x,y:integer; text:string);
  293. procedure GetTextSize (text:string; var w,h:integer);
  294. function GetTextHeight (text:string) : integer;
  295. function GetTextWidth (text:string) : integer;
  296. protected
  297. procedure DoDrawText (x,y:integer; text:string); virtual; abstract;
  298. procedure DoGetTextSize (text:string; var w,h:integer); virtual; abstract;
  299. function DoGetTextHeight (text:string) : integer; virtual; abstract;
  300. function DoGetTextWidth (text:string) : integer; virtual; abstract;
  301. end;
  302. TFPEmptyFont = class (TFPCustomFont)
  303. end;
  304. TFPCustomDrawPen = class (TFPCustomPen)
  305. private
  306. procedure DrawLine (x1,y1,x2,y2:integer);
  307. procedure Polyline (const points:array of TPoint; close:boolean);
  308. procedure Ellipse (left,top, right,bottom:integer);
  309. procedure Rectangle (left,top, right,bottom:integer);
  310. protected
  311. procedure DoDrawLine (x1,y1,x2,y2:integer); virtual; abstract;
  312. procedure DoPolyline (const points:array of TPoint; close:boolean); virtual; abstract;
  313. procedure DoEllipse (left,top, right,bottom:integer); virtual; abstract;
  314. procedure DoRectangle (left,top, right,bottom:integer); virtual; abstract;
  315. end;
  316. TFPEmptyPen = class (TFPCustomPen)
  317. end;
  318. TFPCustomDrawBrush = class (TFPCustomBrush)
  319. private
  320. procedure Rectangle (left,top, right,bottom:integer);
  321. procedure FloodFill (x,y:integer);
  322. procedure Ellipse (left,top, right,bottom:integer);
  323. procedure Polygon (const points:array of TPoint);
  324. public
  325. procedure DoRectangle (left,top, right,bottom:integer); virtual; abstract;
  326. procedure DoEllipse (left,top, right,bottom:integer); virtual; abstract;
  327. procedure DoFloodFill (x,y:integer); virtual; abstract;
  328. procedure DoPolygon (const points:array of TPoint); virtual; abstract;
  329. end;
  330. TFPEmptyBrush = class (TFPCustomBrush)
  331. end;
  332. procedure DecRect (var rect : TRect; delta:integer);
  333. procedure IncRect (var rect : TRect; delta:integer);
  334. procedure DecRect (var rect : TRect);
  335. procedure IncRect (var rect : TRect);
  336. implementation
  337. uses clipping;
  338. const
  339. EFont = 'Font';
  340. EPen = 'Pen';
  341. EBrush = 'Brush';
  342. ErrAllocation = '%s %s be allocated.';
  343. ErrAlloc : array [boolean] of string = ('may not','must');
  344. ErrCouldNotCreate = 'Could not create a %s.';
  345. ErrNoLock = 'Canvas not locked.';
  346. procedure DecRect (var rect : TRect; delta:integer);
  347. begin
  348. with rect do
  349. begin
  350. left := left + delta;
  351. right := right - delta;
  352. top := top + delta;
  353. bottom := bottom - delta;
  354. end;
  355. end;
  356. procedure DecRect (var rect : trect);
  357. begin
  358. DecRect (rect, 1);
  359. end;
  360. procedure IncRect (var rect : trect);
  361. begin
  362. IncRect (rect, 1);
  363. end;
  364. procedure IncRect (var rect : TRect; delta:integer);
  365. begin
  366. with rect do
  367. begin
  368. left := left - delta;
  369. right := right + delta;
  370. top := top - delta;
  371. bottom := bottom + delta;
  372. end;
  373. end;
  374. {$i FPHelper.inc}
  375. {$i FPFont.inc}
  376. {$i FPPen.inc}
  377. {$i FPBrush.inc}
  378. {$i fpinterpolation.inc}
  379. {$i FPCanvas.inc}
  380. {$i FPCDrawH.inc}
  381. end.