source: PrintPreview/UVectorCanvas.pas

Last change on this file was 455, checked in by chronos, 11 years ago
  • Modified: PrintPreview should use TVectorCanvas instead of raster TCanvas dependent on DPI of printer. But some font function as TextWidth and TextHeight cannot be easily implemented without use of OS API.
File size: 8.2 KB
Line 
1unit UVectorCanvas;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, Graphics, GraphType, GraphMath, types, LCLType, fpvectorial,
9 Contnrs;
10
11type
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
133implementation
134
135{ TElementBitmap }
136
137constructor TElementBitmap.Create;
138begin
139 Bitmap := TBitmap.Create;
140end;
141
142destructor TElementBitmap.Destroy;
143begin
144 Bitmap.Free;
145 inherited Destroy;
146end;
147
148procedure TElementBitmap.Render(Canvas: TCanvas);
149begin
150 inherited Render(Canvas);
151 Canvas.CopyRect(DestRect, Bitmap.Canvas, Rect(0, 0, Bitmap.Width, Bitmap.Height));
152end;
153
154{ TElementText }
155
156procedure TElementText.Render(Canvas: TCanvas);
157begin
158 inherited Render(Canvas);
159 Canvas.Font.Assign(Font);
160 Canvas.TextOut(Position.X, Position.Y, Text);
161end;
162
163constructor TElementText.Create;
164begin
165 Font := TFont.Create;
166end;
167
168destructor TElementText.Destroy;
169begin
170 Font.Free;
171 inherited Destroy;
172end;
173
174{ TVectorPen }
175
176procedure TVectorPen.SetColor(AValue: TColor);
177begin
178 if FColor = AValue then Exit;
179 FColor := AValue;
180end;
181
182procedure TVectorPen.SetPosition(AValue: TPoint);
183begin
184 if FPosition=AValue then Exit;
185 FPosition:=AValue;
186end;
187
188procedure TVectorPen.SetStyle(AValue: TPenStyle);
189begin
190 if FStyle=AValue then Exit;
191 FStyle:=AValue;
192end;
193
194{ TElementFillRect }
195
196procedure TElementFillRect.Render(Canvas: TCanvas);
197begin
198 inherited Render(Canvas);
199 Canvas.FillRect(Dimension);
200end;
201
202{ TVectorBrush }
203
204procedure TVectorBrush.SetColor(AValue: TColor);
205begin
206 if FColor=AValue then Exit;
207 FColor:=AValue;
208end;
209
210procedure TVectorBrush.SetStyle(AValue: TBrushStyle);
211begin
212 if FStyle=AValue then Exit;
213 FStyle:=AValue;
214end;
215
216{ TElementFont }
217
218constructor TElementFont.Create;
219begin
220 Font := TFont.Create;
221end;
222
223destructor TElementFont.Destroy;
224begin
225 Font.Free;
226 inherited Destroy;
227end;
228
229{ TElementLine }
230
231procedure TElementLine.AddPoint(X, Y: Integer);
232begin
233 SetLength(Points, Length(Points) + 1);
234 Points[High(Points)] := Point(X, Y);
235end;
236
237procedure TElementLine.Render(Canvas: TCanvas);
238var
239 I: Integer;
240begin
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);
245end;
246
247{ TCanvasElement }
248
249procedure TCanvasElement.Render(Canvas: TCanvas);
250begin
251
252end;
253
254{ TVectorCanvas }
255
256procedure TVectorCanvas.SetFont(AValue: TFont);
257begin
258 if FFont = AValue then Exit;
259 FFont := AValue;
260end;
261
262procedure TVectorCanvas.SetPen(AValue: TVectorPen);
263begin
264 if FPen=AValue then Exit;
265 FPen:=AValue;
266end;
267
268procedure TVectorCanvas.Clear;
269begin
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;
274end;
275
276procedure TVectorCanvas.SetBrush(AValue: TVectorBrush);
277begin
278 if FBrush=AValue then Exit;
279 FBrush:=AValue;
280end;
281
282function TVectorCanvas.GetHeight: Integer;
283begin
284 Result := FSize.Y;
285end;
286
287function TVectorCanvas.GetWidth: Integer;
288begin
289 Result := FSize.X;
290end;
291
292procedure TVectorCanvas.Line(x1, y1, x2, y2: Integer);
293begin
294 with TElementLine(Elements[Elements.Add(TElementLine.Create)]) do begin
295 Color := Pen.Color;
296 AddPoint(X1, Y1);
297 AddPoint(X2, Y2);
298 end;
299end;
300
301procedure TVectorCanvas.TextOut(X, Y: Integer; Text: string);
302var
303 TextElement: TElementText;
304begin
305 TextElement := TElementText(Elements[Elements.Add(TElementText.Create)]);
306 TextElement.Text := Text;
307 TextElement.Position := Point(X, Y);
308 TextElement.Font.Assign(Font);
309end;
310
311procedure TVectorCanvas.MoveTo(X, Y: Integer);
312begin
313 FPen.Position := Point(X, Y);
314end;
315
316procedure TVectorCanvas.LineTo(X, Y: Integer);
317begin
318 Line(FPen.Position.X, FPen.Position.Y, X, Y);
319 FPen.Position := Point(X, Y);
320end;
321
322function TVectorCanvas.TextWidth(Text: string): Integer;
323begin
324 Result := Abs(FFont.Height * Length(Text));
325end;
326
327function TVectorCanvas.TextHeight(Text: string): Integer;
328begin
329 Result := Abs(FFont.Height);
330end;
331
332procedure TVectorCanvas.CopyRect(Dest: TRect; SrcCanvas: TCanvas; Source: TRect
333 );
334begin
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;
341end;
342
343procedure TVectorCanvas.SetSize(Value: TPoint);
344begin
345 FSize := Value;
346end;
347
348constructor TVectorCanvas.Create;
349begin
350 inherited;
351 Elements := TObjectList.Create;
352 Brush := TVectorBrush.Create;
353 Pen := TVectorPen.Create;
354 Font := TFont.Create;
355end;
356
357destructor TVectorCanvas.Destroy;
358begin
359 Font.Free;
360 Pen.Free;
361 Brush.Free;
362 Elements.Free;
363 inherited;
364end;
365
366procedure TVectorCanvas.Render(Canvas: TCanvas);
367var
368 I: Integer;
369begin
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);
375end;
376
377end.
378
379
Note: See TracBrowser for help on using the repository browser.