1 | unit UVectorCanvas;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Graphics, GraphType, GraphMath, types, LCLType, fpvectorial,
|
---|
9 | Contnrs;
|
---|
10 |
|
---|
11 | type
|
---|
12 |
|
---|
13 | { TCanvasElement }
|
---|
14 |
|
---|
15 | TCanvasElement = class
|
---|
16 | procedure Render(Canvas: TCanvas); virtual;
|
---|
17 | end;
|
---|
18 |
|
---|
19 | { TElementLine }
|
---|
20 |
|
---|
21 | TElementLine = class(TCanvasElement)
|
---|
22 | Points: array of TPoint;
|
---|
23 | Color: TColor;
|
---|
24 | procedure AddPoint(X, Y: Integer);
|
---|
25 | procedure Render(Canvas: TCanvas); override;
|
---|
26 | end;
|
---|
27 |
|
---|
28 | { TElementText }
|
---|
29 |
|
---|
30 | TElementText = class(TCanvasElement)
|
---|
31 | Text: string;
|
---|
32 | Position: TPoint;
|
---|
33 | Font: TFont;
|
---|
34 | procedure Render(Canvas: TCanvas); override;
|
---|
35 | constructor Create;
|
---|
36 | destructor Destroy; override;
|
---|
37 | end;
|
---|
38 |
|
---|
39 | { TElementFont }
|
---|
40 |
|
---|
41 | TElementFont = class(TCanvasElement)
|
---|
42 | Font: TFont;
|
---|
43 | constructor Create;
|
---|
44 | destructor Destroy; override;
|
---|
45 | end;
|
---|
46 |
|
---|
47 | { TElementBitmap }
|
---|
48 |
|
---|
49 | TElementBitmap = class(TCanvasElement)
|
---|
50 | Bitmap: TBitmap;
|
---|
51 | DestRect: TRect;
|
---|
52 | constructor Create;
|
---|
53 | destructor Destroy; override;
|
---|
54 | procedure Render(Canvas: TCanvas); override;
|
---|
55 | end;
|
---|
56 |
|
---|
57 | { TElementFillRect }
|
---|
58 |
|
---|
59 | TElementFillRect = class(TCanvasElement)
|
---|
60 | Dimension: TRect;
|
---|
61 | Color: TColor;
|
---|
62 | procedure Render(Canvas: TCanvas); override;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | { TVectorBrush }
|
---|
66 |
|
---|
67 | TVectorBrush = class
|
---|
68 | private
|
---|
69 | FColor: TColor;
|
---|
70 | FStyle: TBrushStyle;
|
---|
71 | procedure SetColor(AValue: TColor);
|
---|
72 | procedure SetStyle(AValue: TBrushStyle);
|
---|
73 | published
|
---|
74 | property Color: TColor read FColor write SetColor;
|
---|
75 | property Style: TBrushStyle read FStyle write SetStyle;
|
---|
76 | end;
|
---|
77 |
|
---|
78 | { TVectorPen }
|
---|
79 |
|
---|
80 | TVectorPen = class
|
---|
81 | private
|
---|
82 | FColor: TColor;
|
---|
83 | FPosition: TPoint;
|
---|
84 | FStyle: TPenStyle;
|
---|
85 | procedure SetColor(AValue: TColor);
|
---|
86 | procedure SetPosition(AValue: TPoint);
|
---|
87 | procedure SetStyle(AValue: TPenStyle);
|
---|
88 | public
|
---|
89 | property Position: TPoint read FPosition write SetPosition;
|
---|
90 | property Color: TColor read FColor write SetColor;
|
---|
91 | property Style: TPenStyle read FStyle write SetStyle;
|
---|
92 | end;
|
---|
93 |
|
---|
94 | { TVectorCanvas }
|
---|
95 |
|
---|
96 | TVectorCanvas = class
|
---|
97 | private
|
---|
98 | FBrush: TVectorBrush;
|
---|
99 | FFont: TFont;
|
---|
100 | FPen: TVectorPen;
|
---|
101 | FSize: TPoint;
|
---|
102 | function GetHeight: Integer;
|
---|
103 | function GetWidth: Integer;
|
---|
104 | procedure SetBrush(AValue: TVectorBrush);
|
---|
105 | procedure SetFont(AValue: TFont);
|
---|
106 | procedure SetPen(AValue: TVectorPen);
|
---|
107 | procedure SetSize(Value: TPoint);
|
---|
108 | protected
|
---|
109 | public
|
---|
110 | Elements: TObjectList; // TObjectList<TCanvasElement>
|
---|
111 | procedure Clear;
|
---|
112 |
|
---|
113 | // TCanvas compatibility
|
---|
114 | procedure Line(x1, y1, x2, y2: Integer);
|
---|
115 | procedure TextOut(X, Y: Integer; Text: string);
|
---|
116 | procedure MoveTo(X, Y: Integer);
|
---|
117 | procedure LineTo(X, Y: Integer);
|
---|
118 | function TextWidth(Text: string): Integer;
|
---|
119 | function TextHeight(Text: string): Integer;
|
---|
120 | procedure CopyRect(Dest: TRect; SrcCanvas: TCanvas; Source: TRect);
|
---|
121 |
|
---|
122 | constructor Create;
|
---|
123 | destructor Destroy; override;
|
---|
124 | procedure Render(Canvas: TCanvas);
|
---|
125 | property Pen: TVectorPen read FPen write SetPen;
|
---|
126 | property Brush: TVectorBrush read FBrush write SetBrush;
|
---|
127 | property Font: TFont read FFont write SetFont;
|
---|
128 | property Width: Integer read GetWidth;
|
---|
129 | property Height: Integer read GetHeight;
|
---|
130 | property Size: TPoint read FSize write SetSize;
|
---|
131 | end;
|
---|
132 |
|
---|
133 | implementation
|
---|
134 |
|
---|
135 | { TElementBitmap }
|
---|
136 |
|
---|
137 | constructor TElementBitmap.Create;
|
---|
138 | begin
|
---|
139 | Bitmap := TBitmap.Create;
|
---|
140 | end;
|
---|
141 |
|
---|
142 | destructor TElementBitmap.Destroy;
|
---|
143 | begin
|
---|
144 | Bitmap.Free;
|
---|
145 | inherited Destroy;
|
---|
146 | end;
|
---|
147 |
|
---|
148 | procedure TElementBitmap.Render(Canvas: TCanvas);
|
---|
149 | begin
|
---|
150 | inherited Render(Canvas);
|
---|
151 | Canvas.CopyRect(DestRect, Bitmap.Canvas, Rect(0, 0, Bitmap.Width, Bitmap.Height));
|
---|
152 | end;
|
---|
153 |
|
---|
154 | { TElementText }
|
---|
155 |
|
---|
156 | procedure TElementText.Render(Canvas: TCanvas);
|
---|
157 | begin
|
---|
158 | inherited Render(Canvas);
|
---|
159 | Canvas.Font.Assign(Font);
|
---|
160 | Canvas.TextOut(Position.X, Position.Y, Text);
|
---|
161 | end;
|
---|
162 |
|
---|
163 | constructor TElementText.Create;
|
---|
164 | begin
|
---|
165 | Font := TFont.Create;
|
---|
166 | end;
|
---|
167 |
|
---|
168 | destructor TElementText.Destroy;
|
---|
169 | begin
|
---|
170 | Font.Free;
|
---|
171 | inherited Destroy;
|
---|
172 | end;
|
---|
173 |
|
---|
174 | { TVectorPen }
|
---|
175 |
|
---|
176 | procedure TVectorPen.SetColor(AValue: TColor);
|
---|
177 | begin
|
---|
178 | if FColor = AValue then Exit;
|
---|
179 | FColor := AValue;
|
---|
180 | end;
|
---|
181 |
|
---|
182 | procedure TVectorPen.SetPosition(AValue: TPoint);
|
---|
183 | begin
|
---|
184 | if FPosition=AValue then Exit;
|
---|
185 | FPosition:=AValue;
|
---|
186 | end;
|
---|
187 |
|
---|
188 | procedure TVectorPen.SetStyle(AValue: TPenStyle);
|
---|
189 | begin
|
---|
190 | if FStyle=AValue then Exit;
|
---|
191 | FStyle:=AValue;
|
---|
192 | end;
|
---|
193 |
|
---|
194 | { TElementFillRect }
|
---|
195 |
|
---|
196 | procedure TElementFillRect.Render(Canvas: TCanvas);
|
---|
197 | begin
|
---|
198 | inherited Render(Canvas);
|
---|
199 | Canvas.FillRect(Dimension);
|
---|
200 | end;
|
---|
201 |
|
---|
202 | { TVectorBrush }
|
---|
203 |
|
---|
204 | procedure TVectorBrush.SetColor(AValue: TColor);
|
---|
205 | begin
|
---|
206 | if FColor=AValue then Exit;
|
---|
207 | FColor:=AValue;
|
---|
208 | end;
|
---|
209 |
|
---|
210 | procedure TVectorBrush.SetStyle(AValue: TBrushStyle);
|
---|
211 | begin
|
---|
212 | if FStyle=AValue then Exit;
|
---|
213 | FStyle:=AValue;
|
---|
214 | end;
|
---|
215 |
|
---|
216 | { TElementFont }
|
---|
217 |
|
---|
218 | constructor TElementFont.Create;
|
---|
219 | begin
|
---|
220 | Font := TFont.Create;
|
---|
221 | end;
|
---|
222 |
|
---|
223 | destructor TElementFont.Destroy;
|
---|
224 | begin
|
---|
225 | Font.Free;
|
---|
226 | inherited Destroy;
|
---|
227 | end;
|
---|
228 |
|
---|
229 | { TElementLine }
|
---|
230 |
|
---|
231 | procedure TElementLine.AddPoint(X, Y: Integer);
|
---|
232 | begin
|
---|
233 | SetLength(Points, Length(Points) + 1);
|
---|
234 | Points[High(Points)] := Point(X, Y);
|
---|
235 | end;
|
---|
236 |
|
---|
237 | procedure TElementLine.Render(Canvas: TCanvas);
|
---|
238 | var
|
---|
239 | I: Integer;
|
---|
240 | begin
|
---|
241 | inherited Render(Canvas);
|
---|
242 | for I := 0 to High(Points) do
|
---|
243 | if I = 0 then Canvas.MoveTo(Points[I].X, Points[I].Y)
|
---|
244 | else Canvas.LineTo(Points[I].X, Points[I].Y);
|
---|
245 | end;
|
---|
246 |
|
---|
247 | { TCanvasElement }
|
---|
248 |
|
---|
249 | procedure TCanvasElement.Render(Canvas: TCanvas);
|
---|
250 | begin
|
---|
251 |
|
---|
252 | end;
|
---|
253 |
|
---|
254 | { TVectorCanvas }
|
---|
255 |
|
---|
256 | procedure TVectorCanvas.SetFont(AValue: TFont);
|
---|
257 | begin
|
---|
258 | if FFont = AValue then Exit;
|
---|
259 | FFont := AValue;
|
---|
260 | end;
|
---|
261 |
|
---|
262 | procedure TVectorCanvas.SetPen(AValue: TVectorPen);
|
---|
263 | begin
|
---|
264 | if FPen=AValue then Exit;
|
---|
265 | FPen:=AValue;
|
---|
266 | end;
|
---|
267 |
|
---|
268 | procedure TVectorCanvas.Clear;
|
---|
269 | begin
|
---|
270 | with TElementFillRect(Elements[Elements.Add(TElementFillRect.Create)]) do begin
|
---|
271 | Color := Brush.Color;
|
---|
272 | Dimension := Rect(0, 0, FSize.X, FSize.Y);
|
---|
273 | end;
|
---|
274 | end;
|
---|
275 |
|
---|
276 | procedure TVectorCanvas.SetBrush(AValue: TVectorBrush);
|
---|
277 | begin
|
---|
278 | if FBrush=AValue then Exit;
|
---|
279 | FBrush:=AValue;
|
---|
280 | end;
|
---|
281 |
|
---|
282 | function TVectorCanvas.GetHeight: Integer;
|
---|
283 | begin
|
---|
284 | Result := FSize.Y;
|
---|
285 | end;
|
---|
286 |
|
---|
287 | function TVectorCanvas.GetWidth: Integer;
|
---|
288 | begin
|
---|
289 | Result := FSize.X;
|
---|
290 | end;
|
---|
291 |
|
---|
292 | procedure TVectorCanvas.Line(x1, y1, x2, y2: Integer);
|
---|
293 | begin
|
---|
294 | with TElementLine(Elements[Elements.Add(TElementLine.Create)]) do begin
|
---|
295 | Color := Pen.Color;
|
---|
296 | AddPoint(X1, Y1);
|
---|
297 | AddPoint(X2, Y2);
|
---|
298 | end;
|
---|
299 | end;
|
---|
300 |
|
---|
301 | procedure TVectorCanvas.TextOut(X, Y: Integer; Text: string);
|
---|
302 | var
|
---|
303 | TextElement: TElementText;
|
---|
304 | begin
|
---|
305 | TextElement := TElementText(Elements[Elements.Add(TElementText.Create)]);
|
---|
306 | TextElement.Text := Text;
|
---|
307 | TextElement.Position := Point(X, Y);
|
---|
308 | TextElement.Font.Assign(Font);
|
---|
309 | end;
|
---|
310 |
|
---|
311 | procedure TVectorCanvas.MoveTo(X, Y: Integer);
|
---|
312 | begin
|
---|
313 | FPen.Position := Point(X, Y);
|
---|
314 | end;
|
---|
315 |
|
---|
316 | procedure TVectorCanvas.LineTo(X, Y: Integer);
|
---|
317 | begin
|
---|
318 | Line(FPen.Position.X, FPen.Position.Y, X, Y);
|
---|
319 | FPen.Position := Point(X, Y);
|
---|
320 | end;
|
---|
321 |
|
---|
322 | function TVectorCanvas.TextWidth(Text: string): Integer;
|
---|
323 | begin
|
---|
324 | Result := Abs(FFont.Height * Length(Text));
|
---|
325 | end;
|
---|
326 |
|
---|
327 | function TVectorCanvas.TextHeight(Text: string): Integer;
|
---|
328 | begin
|
---|
329 | Result := Abs(FFont.Height);
|
---|
330 | end;
|
---|
331 |
|
---|
332 | procedure TVectorCanvas.CopyRect(Dest: TRect; SrcCanvas: TCanvas; Source: TRect
|
---|
333 | );
|
---|
334 | begin
|
---|
335 | with TElementBitmap(Elements[Elements.Add(TElementBitmap.Create)]) do begin
|
---|
336 | Bitmap.SetSize(Dest.Right - Dest.Left, Dest.Bottom - Dest.Top);
|
---|
337 | Bitmap.Canvas.CopyRect(Rect(0, 0, Dest.Right - Dest.Left, Dest.Bottom - Dest.Top),
|
---|
338 | SrcCanvas, Source);
|
---|
339 | DestRect := Dest;
|
---|
340 | end;
|
---|
341 | end;
|
---|
342 |
|
---|
343 | procedure TVectorCanvas.SetSize(Value: TPoint);
|
---|
344 | begin
|
---|
345 | FSize := Value;
|
---|
346 | end;
|
---|
347 |
|
---|
348 | constructor TVectorCanvas.Create;
|
---|
349 | begin
|
---|
350 | inherited;
|
---|
351 | Elements := TObjectList.Create;
|
---|
352 | Brush := TVectorBrush.Create;
|
---|
353 | Pen := TVectorPen.Create;
|
---|
354 | Font := TFont.Create;
|
---|
355 | end;
|
---|
356 |
|
---|
357 | destructor TVectorCanvas.Destroy;
|
---|
358 | begin
|
---|
359 | Font.Free;
|
---|
360 | Pen.Free;
|
---|
361 | Brush.Free;
|
---|
362 | Elements.Free;
|
---|
363 | inherited;
|
---|
364 | end;
|
---|
365 |
|
---|
366 | procedure TVectorCanvas.Render(Canvas: TCanvas);
|
---|
367 | var
|
---|
368 | I: Integer;
|
---|
369 | begin
|
---|
370 | //Size := Point(Canvas.Width, Canvas.Height);
|
---|
371 | Pen.Position := Point(0, 0);
|
---|
372 | with Canvas do
|
---|
373 | for I := 0 to Elements.Count - 1 do
|
---|
374 | TCanvasElement(Elements[I]).Render(Canvas);
|
---|
375 | end;
|
---|
376 |
|
---|
377 | end.
|
---|
378 |
|
---|
379 |
|
---|