fpcanvas.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2003 by the Free Pascal development team
  5. Basic canvas definitions.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$mode objfpc}{$h+}
  13. unit FPCanvas;
  14. interface
  15. uses sysutils, classes, FPImage;
  16. type
  17. TFPCanvasException = class (Exception);
  18. TFPPenException = class (TFPCanvasException);
  19. TFPBrushException = class (TFPCanvasException);
  20. TFPFontException = class (TFPCanvasException);
  21. TFPCustomCanvas = class;
  22. TFPCanvasHelper = class
  23. private
  24. FColor : TFPColor;
  25. FAllocated,
  26. FFixedCanvas : boolean;
  27. FCanvas : TFPCustomCanvas;
  28. FFlags : word;
  29. function GetAllocated : boolean;
  30. procedure NotifyCanvas;
  31. protected
  32. // flags 0-15 are reserved for FPCustomCanvas
  33. procedure SetFlags (index:integer; AValue:boolean);
  34. function GetFlags (index:integer) : boolean;
  35. procedure CheckAllocated (ValueNeeded:boolean);
  36. procedure SetFixedCanvas (AValue : boolean);
  37. procedure DoAllocateResources; virtual;
  38. procedure DoDeAllocateResources; virtual;
  39. procedure DoCopyProps (From:TFPCanvasHelper); virtual;
  40. procedure SetColor (AValue:TFPColor); virtual;
  41. public
  42. constructor Create; virtual;
  43. destructor destroy; override;
  44. // prepare helper for use
  45. procedure AllocateResources (ACanvas : TFPCustomCanvas);
  46. // free all resource used bby this helper
  47. procedure DeallocateResources;
  48. property Allocated : boolean read GetAllocated;
  49. // properties cannot be changed when allocated
  50. property FixedCanvas : boolean read FFixedCanvas;
  51. // Canvas for which the helper is allocated
  52. property Canvas : TFPCustomCanvas read FCanvas;
  53. // color of the helper
  54. property Color : TFPColor read FColor Write SetColor;
  55. end;
  56. TFPCustomFont = class (TFPCanvasHelper)
  57. private
  58. FName : string;
  59. FSize : integer;
  60. protected
  61. procedure DoCopyProps (From:TFPCanvasHelper); override;
  62. procedure SetName (AValue:string); virtual;
  63. procedure SetSize (AValue:integer); virtual;
  64. public
  65. function CopyFont : TFPCustomFont;
  66. // Creates a copy of the font with all properties the same, but not allocated
  67. procedure GetTextSize (text:string; var w,h:integer);
  68. function GetTextHeight (text:string) : integer;
  69. function GetTextWidth (text:string) : integer;
  70. property Name : string read FName write SetName;
  71. property Size : integer read FSize write SetSize;
  72. property Bold : boolean index 5 read GetFlags write SetFlags;
  73. property Italic : boolean index 6 read GetFlags write SetFlags;
  74. property Underline : boolean index 7 read GetFlags write SetFlags;
  75. property StrikeTrough : boolean index 8 read GetFlags write SetFlags;
  76. end;
  77. TFPCustomFontClass = class of TFPCustomFont;
  78. TFPPenStyle = (psClear, psSolid, psDash, psDot, psDashDot, psDashDotDot, psPattern);
  79. TFPPenStyleSet = set of TFPPenStyle;
  80. TFPPenMode = (pmCopy, pmAnd, pmOr, pmXor);
  81. TFPCustomPen = class (TFPCanvasHelper)
  82. private
  83. FStyle : TFPPenStyle;
  84. FWidth : byte;
  85. FMode : TFPPenMode;
  86. FPattern : longword;
  87. protected
  88. procedure DoCopyProps (From:TFPCanvasHelper); override;
  89. procedure SetMode (AValue : TFPPenMode); virtual;
  90. procedure SetWidth (AValue : byte); virtual;
  91. procedure SetStyle (AValue : TFPPenStyle); virtual;
  92. procedure SetPattern (AValue : longword); virtual;
  93. public
  94. function CopyPen : TFPCustomPen;
  95. // Creates a copy of the font with all properties the same, but not allocated
  96. property Style : TFPPenStyle read FStyle write SetStyle;
  97. property Width : byte read FWidth write SetWidth;
  98. property Mode : TFPPenMode read FMode write SetMode;
  99. property Pattern : longword read FPattern write SetPattern;
  100. end;
  101. TFPCustomPenClass = class of TFPCustomPen;
  102. TFPBrushStyle = (bsClear, bsSolid, bsDiagonal, bsFDiagonal, bsCross,bsDiagCross,
  103. bsHorizontal, bsVertical, bsImage, bsPattern);
  104. TFPCustomBrush = class (TFPCanvasHelper)
  105. private
  106. FStyle : TFPBrushStyle;
  107. FImage : TFPCustomImage;
  108. protected
  109. procedure SetStyle (AValue : TFPBrushStyle); virtual;
  110. procedure SetImage (AValue : TFPCustomImage); virtual;
  111. procedure DoCopyProps (From:TFPCanvasHelper); override;
  112. public
  113. function CopyBrush : TFPCustomBrush;
  114. property Style : TFPBrushStyle read FStyle write SetStyle;
  115. property Image : TFPCustomImage read FImage write SetImage;
  116. end;
  117. TFPCustomBrushClass = class of TFPCustomBrush;
  118. TFPCustomCanvas = class
  119. private
  120. FClipping,
  121. FRemovingHelpers : boolean;
  122. FDefaultFont,
  123. FFont : TFPCustomFont;
  124. FDefaultBrush,
  125. FBrush : TFPCustomBrush;
  126. FDefaultPen,
  127. FPen : TFPCustomPen;
  128. FCurrent : TPoint;
  129. FClipRect : TRect;
  130. FHelpers : TList;
  131. FLocks : integer;
  132. function AllowFont (AFont : TFPCustomFont) : boolean;
  133. function AllowBrush (ABrush : TFPCustomBrush) : boolean;
  134. function AllowPen (APen : TFPCustomPen) : boolean;
  135. function CreateDefaultFont : TFPCustomFont;
  136. function CreateDefaultPen : TFPCustomPen;
  137. function CreateDefaultBrush : TFPCustomBrush;
  138. procedure RemoveHelpers;
  139. function GetFont : TFPCustomFont;
  140. function GetBrush : TFPCustomBrush;
  141. function GetPen : TFPCustomPen;
  142. protected
  143. function DoCreateDefaultFont : TFPCustomFont; virtual; abstract;
  144. function DoCreateDefaultPen : TFPCustomPen; virtual; abstract;
  145. function DoCreateDefaultBrush : TFPCustomBrush; virtual; abstract;
  146. procedure SetFont (AValue:TFPCustomFont); virtual;
  147. procedure SetBrush (AValue:TFPCustomBrush); virtual;
  148. procedure SetPen (AValue:TFPCustomPen); virtual;
  149. function DoAllowFont (AFont : TFPCustomFont) : boolean; virtual;
  150. function DoAllowPen (APen : TFPCustomPen) : boolean; virtual;
  151. function DoAllowBrush (ABrush : TFPCustomBrush) : boolean; virtual;
  152. procedure SetColor (x,y:integer; Value:TFPColor); Virtual; abstract;
  153. function GetColor (x,y:integer) : TFPColor; Virtual; abstract;
  154. procedure SetHeight (AValue : integer); virtual; abstract;
  155. function GetHeight : integer; virtual; abstract;
  156. procedure SetWidth (AValue : integer); virtual; abstract;
  157. function GetWidth : integer; virtual; abstract;
  158. procedure DoLockCanvas; virtual;
  159. procedure DoUnlockCanvas; virtual;
  160. procedure DoTextOut (x,y:integer;text:string); virtual; abstract;
  161. procedure DoGetTextSize (text:string; var w,h:integer); virtual; abstract;
  162. function DoGetTextHeight (text:string) : integer; virtual; abstract;
  163. function DoGetTextWidth (text:string) : integer; virtual; abstract;
  164. procedure DoRectangle (Const Bounds:TRect); virtual; abstract;
  165. procedure DoRectangleFill (Const Bounds:TRect); virtual; abstract;
  166. procedure DoRectangleAndFill (Const Bounds:TRect); virtual;
  167. procedure DoEllipseFill (Const Bounds:TRect); virtual; abstract;
  168. procedure DoEllipse (Const Bounds:TRect); virtual; abstract;
  169. procedure DoEllipseAndFill (Const Bounds:TRect); virtual;
  170. procedure DoPolygonFill (const points:array of TPoint); virtual; abstract;
  171. procedure DoPolygon (const points:array of TPoint); virtual; abstract;
  172. procedure DoPolygonAndFill (const points:array of TPoint); virtual;
  173. procedure DoPolyline (const points:array of TPoint); virtual; abstract;
  174. procedure DoFloodFill (x,y:integer); virtual; abstract;
  175. procedure DoMoveTo (x,y:integer); virtual;
  176. procedure DoLineTo (x,y:integer); virtual;
  177. procedure DoLine (x1,y1,x2,y2:integer); virtual; abstract;
  178. procedure DoCopyRect (x,y:integer; canvas:TFPCustomCanvas; SourceRect:TRect); virtual; abstract;
  179. procedure DoDraw (x,y:integer; image:TFPCustomImage); virtual; abstract;
  180. procedure CheckHelper (AHelper:TFPCanvasHelper); virtual;
  181. procedure AddHelper (AHelper:TFPCanvasHelper);
  182. public
  183. constructor create;
  184. destructor destroy; override;
  185. procedure LockCanvas;
  186. procedure UnlockCanvas;
  187. function CreateFont : TFPCustomFont;
  188. function CreatePen : TFPCustomPen;
  189. function CreateBrush : TFPCustomBrush;
  190. // using font
  191. procedure TextOut (x,y:integer;text:string);
  192. procedure GetTextSize (text:string; var w,h:integer);
  193. function GetTextHeight (text:string) : integer;
  194. function GetTextWidth (text:string) : integer;
  195. // using pen and brush
  196. procedure Ellipse (Const Bounds:TRect);
  197. procedure Ellipse (left,top,right,bottom:integer);
  198. procedure Polygon (Const points:array of TPoint);
  199. procedure Polyline (Const points:array of TPoint);
  200. procedure Rectangle (Const Bounds:TRect);
  201. procedure Rectangle (left,top,right,bottom:integer);
  202. // using brush
  203. procedure FloodFill (x,y:integer);
  204. procedure Clear;
  205. // using pen
  206. procedure MoveTo (x,y:integer);
  207. procedure MoveTo (p:TPoint);
  208. procedure LineTo (x,y:integer);
  209. procedure LineTo (p:TPoint);
  210. procedure Line (x1,y1,x2,y2:integer);
  211. procedure Line (p1,p2:TPoint);
  212. procedure Line (const points:TRect);
  213. // other procedures
  214. procedure CopyRect (x,y:integer; canvas:TFPCustomCanvas; SourceRect:TRect);
  215. procedure Draw (x,y:integer; image:TFPCustomImage);
  216. // properties
  217. property Font : TFPCustomFont read GetFont write SetFont;
  218. property Pen : TFPCustomPen read GetPen write SetPen;
  219. property Brush : TFPCustomBrush read GetBrush write SetBrush;
  220. property Colors [x,y:integer] : TFPColor read GetColor write SetColor;
  221. property ClipRect : TRect read FClipRect write FClipRect;
  222. property Clipping : boolean read FClipping write FClipping;
  223. property PenPos : TPoint read FCurrent write FCurrent;
  224. property Height : integer read GetHeight write SetHeight;
  225. property Width : integer read GetWidth write SetWidth;
  226. end;
  227. TFPCustomDrawFont = class (TFPCustomFont)
  228. private
  229. procedure DrawText (x,y:integer; text:string);
  230. procedure GetTextSize (text:string; var w,h:integer);
  231. function GetTextHeight (text:string) : integer;
  232. function GetTextWidth (text:string) : integer;
  233. protected
  234. procedure DoDrawText (x,y:integer; text:string); virtual; abstract;
  235. procedure DoGetTextSize (text:string; var w,h:integer); virtual; abstract;
  236. function DoGetTextHeight (text:string) : integer; virtual; abstract;
  237. function DoGetTextWidth (text:string) : integer; virtual; abstract;
  238. end;
  239. TFPEmptyFont = class (TFPCustomFont)
  240. end;
  241. TFPCustomDrawPen = class (TFPCustomPen)
  242. private
  243. procedure DrawLine (x1,y1,x2,y2:integer);
  244. procedure Polyline (points:array of TPoint; close:boolean);
  245. procedure Ellipse (left,top, right,bottom:integer);
  246. procedure Rectangle (left,top, right,bottom:integer);
  247. protected
  248. procedure DoDrawLine (x1,y1,x2,y2:integer); virtual; abstract;
  249. procedure DoPolyline (points:array of TPoint; close:boolean); virtual; abstract;
  250. procedure DoEllipse (left,top, right,bottom:integer); virtual; abstract;
  251. procedure DoRectangle (left,top, right,bottom:integer); virtual; abstract;
  252. end;
  253. TFPEmptyPen = class (TFPCustomPen)
  254. end;
  255. TFPCustomDrawBrush = class (TFPCustomBrush)
  256. private
  257. procedure Rectangle (left,top, right,bottom:integer);
  258. procedure FloodFill (x,y:integer);
  259. procedure Ellipse (left,top, right,bottom:integer);
  260. procedure Polygon (points:array of TPoint);
  261. public
  262. procedure DoRectangle (left,top, right,bottom:integer); virtual; abstract;
  263. procedure DoEllipse (left,top, right,bottom:integer); virtual; abstract;
  264. procedure DoFloodFill (x,y:integer); virtual; abstract;
  265. procedure DoPolygon (points:array of TPoint); virtual; abstract;
  266. end;
  267. TFPEmptyBrush = class (TFPCustomBrush)
  268. end;
  269. implementation
  270. uses clipping;
  271. const
  272. EFont = 'Font';
  273. EPen = 'Pen';
  274. EBrush = 'Brush';
  275. ErrAllocation = '%s %s be allocated.';
  276. ErrAlloc : array [boolean] of string = ('may not','must');
  277. ErrCouldNotCreate = 'Could not create a %s.';
  278. ErrNoLock = 'Canvas not locked.';
  279. {$i FPHelper.inc}
  280. {$i FPFont.inc}
  281. {$i FPPen.inc}
  282. {$i FPBrush.inc}
  283. {$i FPCanvas.inc}
  284. {$i FPCDrawH.inc}
  285. end.