forked from synopse/mORMot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SynFPCMetaFile.pas
422 lines (349 loc) · 11.1 KB
/
SynFPCMetaFile.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
unit SynFPCMetaFile;
{$ifndef FPC}
unit for FPC under Windows only !
not needed for Delphi / VCL
{$endif}
{$mode objfpc}{$H+}
interface
uses
Windows,
Classes,
SysUtils,
Graphics;
type
TMetafile = class;
{ TMetafileCanvas }
TMetafileCanvas = class(TCanvas)
private
FMetafile: TMetafile;
public
constructor Create(AMetafile: TMetafile; ReferenceDevice: HDC);
constructor CreateWithComment(AMetafile: TMetafile; ReferenceDevice: HDC;
const CreatedBy, Description: string);
destructor Destroy; override;
end;
{ TMetafile }
TMetafile = class(TGraphic)
private
FImageHandle: HENHMETAFILE;
FImageMMWidth: Integer; // are in 0.01 mm logical pixels
FImageMMHeight: Integer; // are in 0.01 mm logical pixels
FImagePxWidth: Integer; // in device pixels
FImagePxHeight: Integer; // in device pixels
procedure DeleteImage;
function GetAuthor: String;
function GetDescription: String;
function GetEmpty: Boolean; override;
function GetHandle: HENHMETAFILE;
function GetMMHeight: Integer;
function GetMMWidth: Integer;
procedure SetHandle(Value: HENHMETAFILE);
procedure SetMMHeight(Value: Integer);
procedure SetMMWidth(Value: Integer);
protected
procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
function GetHeight: Integer; override;
function GetWidth: Integer; override;
procedure SetHeight(Value: Integer); override;
procedure SetWidth(Value: Integer); override;
function GetTransparent: Boolean; override;
procedure SetTransparent(Value: Boolean); override;
public
constructor Create; override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure Clear;
procedure LoadFromFile(const Filename: String); override;
procedure LoadFromStream(Stream: TStream); override;
procedure SaveToFile(const Filename: String); override;
procedure SaveToStream(Stream: TStream); override;
function ReleaseHandle: HENHMETAFILE;
property Handle: HENHMETAFILE read GetHandle write SetHandle;
property Empty: boolean read GetEmpty;
property CreatedBy: String read GetAuthor;
property Description: String read GetDescription;
property MMWidth: Integer read GetMMWidth write SetMMWidth;
property MMHeight: Integer read GetMMHeight write SetMMHeight;
end;
const
EMR_HEADER = 1;
EMR_POLYBEZIER = 2;
EMR_POLYGON = 3;
EMR_POLYLINE = 4;
EMR_SETWINDOWEXTEX = 9;
EMR_SETWINDOWORGEX = 10;
EMR_SETVIEWPORTEXTEX = 11;
EMR_SETVIEWPORTORGEX = 12;
EMR_SETBKMODE = 18;
EMR_SETTEXTALIGN = 22;
EMR_SETTEXTCOLOR = 24;
EMR_SETBKCOLOR = 25;
EMR_OFFSETCLIPRGN = 26;
EMR_MOVETOEX = 27;
EMR_EXCLUDECLIPRECT = 29;
EMR_INTERSECTCLIPRECT = 30;
EMR_SAVEDC = 33;
EMR_RESTOREDC = 34;
EMR_SETWORLDTRANSFORM = 35;
EMR_SELECTOBJECT = 37;
EMR_CREATEPEN = 38;
EMR_CREATEBRUSHINDIRECT = 39;
EMR_DELETEOBJECT = 40;
EMR_ELLIPSE = 42;
EMR_RECTANGLE = 43;
EMR_ROUNDRECT = 44;
EMR_LINETO = 54;
EMR_SELECTCLIPPATH = 67;
EMR_EXTSELECTCLIPRGN = 75;
EMR_BITBLT = 76;
EMR_STRETCHBLT = 77;
EMR_STRETCHDIBITS = 81;
EMR_EXTCREATEFONTINDIRECTW = 82;
EMR_EXTTEXTOUTW = 84;
EMR_POLYBEZIER16 = 85;
EMR_POLYGON16 = 86;
EMR_POLYLINE16 = 87;
implementation
{ TMetafile }
procedure TMetafile.DeleteImage;
begin
if FImageHandle <> 0 then
DeleteEnhMetafile(FImageHandle);
FImageHandle := 0;
end;
function TMetafile.GetAuthor: String;
var
NC: Integer;
begin
Result := '';
if FImageHandle = 0 then Exit;
NC := GetEnhMetafileDescription(FImageHandle, 0, nil);
if NC <= 0 then Exit
else begin
SetLength(Result, NC);
GetEnhMetafileDescription(FImageHandle, NC, PChar(Result));
SetLength(Result, StrLen(PChar(Result)) );
end;
end;
function TMetafile.GetDescription: String;
var
NC: Integer;
begin
Result := '';
if FImageHandle = 0 then Exit;
NC := GetEnhMetafileDescription(FImageHandle, 0, nil);
if NC <= 0 then Exit
else begin
SetLength(Result, NC);
GetEnhMetafileDescription(FImageHandle, NC, PChar(Result));
Delete(Result, 1, StrLen(PChar(Result))+1);
SetLength(Result, StrLen(PChar(Result)));
end;
end;
function TMetafile.GetEmpty: Boolean;
begin
Result := (FImageHandle = 0);
end;
function TMetafile.GetHandle: HENHMETAFILE;
begin
Result := FImageHandle
end;
function TMetafile.GetMMHeight: Integer;
begin
Result := FImageMMHeight;
end;
function TMetafile.GetMMWidth: Integer;
begin
Result := FImageMMWidth;
end;
procedure TMetafile.SetHandle(Value: HENHMETAFILE);
var
EnhHeader: TEnhMetaHeader;
begin
if (Value <> 0) and (GetEnhMetafileHeader(Value, sizeof(EnhHeader), @EnhHeader) = 0) then
raise EInvalidImage.Create('Invalid Metafile');;
if FImageHandle <> 0 then DeleteImage;
FImageHandle := Value;
FImagePxWidth := 0;
FImagePxHeight := 0;
FImageMMWidth := EnhHeader.rclFrame.Right - EnhHeader.rclFrame.Left;
FImageMMHeight := EnhHeader.rclFrame.Bottom - EnhHeader.rclFrame.Top;
end;
procedure TMetafile.SetMMHeight(Value: Integer);
begin
FImagePxHeight := 0;
if FImageMMHeight <> Value then FImageMMHeight := Value;
end;
procedure TMetafile.SetMMWidth(Value: Integer);
begin
FImagePxWidth := 0;
if FImageMMWidth <> Value then FImageMMWidth := Value;
end;
procedure TMetafile.Draw(ACanvas: TCanvas; const Rect: TRect);
var
RT: TRect;
begin
if FImageHandle = 0 then Exit;
RT := Rect;
PlayEnhMetaFile(ACanvas.Handle, FImageHandle, RT);
end;
function TMetafile.GetHeight: Integer;
var
EMFHeader: TEnhMetaHeader;
begin
if FImageHandle = 0 then
Result := FImagePxHeight
else begin // convert 0.01mm units to device pixels
GetEnhMetaFileHeader(FImageHandle, Sizeof(EMFHeader), @EMFHeader);
Result := MulDiv(FImageMMHeight, // metafile height in 0.01mm
EMFHeader.szlDevice.cy, // device height in pixels
EMFHeader.szlMillimeters.cy*100); // device height in mm
end
end;
function TMetafile.GetWidth: Integer;
var
EMFHeader: TEnhMetaHeader;
begin
if FImageHandle = 0 then
Result := FImagePxWidth
else begin // convert 0.01mm units to device pixels
GetEnhMetaFileHeader(FImageHandle, Sizeof(EMFHeader), @EMFHeader);
Result := MulDiv(FImageMMWidth, // metafile width in 0.01mm
EMFHeader.szlDevice.cx, // device width in pixels
EMFHeader.szlMillimeters.cx*100); // device width in 0.01mm
end
end;
procedure TMetafile.SetHeight(Value: Integer);
var
EMFHeader: TEnhMetaHeader;
begin
if FImageHandle = 0 then
FImagePxHeight := Value
else begin // convert device pixels to 0.01mm units
GetEnhMetaFileHeader(FImageHandle, Sizeof(EMFHeader), @EMFHeader);
MMHeight := MulDiv(Value, // metafile height in pixels
EMFHeader.szlMillimeters.cy*100, // device height in 0.01mm
EMFHeader.szlDevice.cy); // device height in pixels
end
end;
procedure TMetafile.SetWidth(Value: Integer);
var
EMFHeader: TEnhMetaHeader;
begin
if FImageHandle = 0 then
FImagePxWidth := Value
else begin // convert device pixels to 0.01mm units
GetEnhMetaFileHeader(FImageHandle, Sizeof(EMFHeader), @EMFHeader);
MMWidth := MulDiv(Value, // metafile width in pixels
EMFHeader.szlMillimeters.cx*100, // device width in mm
EMFHeader.szlDevice.cx); // device width in pixels
end
end;
constructor TMetafile.Create;
begin
inherited Create;
FImageHandle := 0;
end;
destructor TMetafile.Destroy;
begin
DeleteImage;
inherited Destroy;
end;
procedure TMetafile.Assign(Source: TPersistent);
begin
if (Source = nil) or (Source is TMetafile) then begin
if FImageHandle <> 0 then DeleteImage;
if Assigned(Source) then begin
FImageHandle := TMetafile(Source).Handle;
FImageMMWidth := TMetafile(Source).MMWidth;
FImageMMHeight := TMetafile(Source).MMHeight;
FImagePxWidth := TMetafile(Source).Width;
FImagePxHeight := TMetafile(Source).Height;
end
end
else
inherited Assign(Source);
end;
procedure TMetafile.Clear;
begin
DeleteImage;
end;
procedure TMetafile.LoadFromFile(const Filename: String);
begin
raise EComponentError.Create('Not Implemented');
end;
procedure TMetafile.SaveToFile(const Filename: String);
begin
raise EComponentError.Create('Not Implemented');
end;
procedure TMetafile.LoadFromStream(Stream: TStream);
begin
raise EComponentError.Create('Not Implemented');
end;
procedure TMetafile.SaveToStream(Stream: TStream);
begin
raise EComponentError.Create('Not Implemented');
end;
function TMetafile.ReleaseHandle: HENHMETAFILE;
begin
DeleteImage;
Result := FImageHandle;
FImageHandle := 0;
end;
function TMetafile.GetTransparent: Boolean;
begin // not implemented
result:=false;
end;
procedure TMetafile.SetTransparent(Value: Boolean);
begin // not implemented
end;
{ TMetafileCanvas }
constructor TMetafileCanvas.Create(AMetafile: TMetafile; ReferenceDevice: HDC);
begin
CreateWithComment(AMetafile, ReferenceDevice, AMetafile.CreatedBy,
AMetafile.Description);
end;
constructor TMetafileCanvas.CreateWithComment(AMetafile: TMetafile;
ReferenceDevice: HDC; const CreatedBy, Description: String);
var
RefDC: HDC;
R: TRect;
Temp: HDC;
P: PChar;
begin
inherited Create;
FMetafile := AMetafile;
if ReferenceDevice = 0 then RefDC := GetDC(0)
else RefDC := ReferenceDevice;
try
if FMetafile.MMWidth = 0 then begin
if FMetafile.Width = 0 then //if no width get RefDC height
FMetafile.MMWidth := GetDeviceCaps(RefDC, HORZSIZE)*100
else FMetafile.MMWidth := MulDiv(FMetafile.Width, //else convert
GetDeviceCaps(RefDC, HORZSIZE)*100, GetDeviceCaps(RefDC, HORZRES));
end;
if FMetafile.MMHeight = 0 then begin
if FMetafile.Height = 0 then //if no height get RefDC height
FMetafile.MMHeight := GetDeviceCaps(RefDC, VERTSIZE)*100
else FMetafile.MMHeight := MulDiv(FMetafile.Height, //else convert
GetDeviceCaps(RefDC, VERTSIZE)*100, GetDeviceCaps(RefDC, VERTRES));
end;
R := Rect(0,0,FMetafile.MMWidth,FMetafile.MMHeight);
//lpDescription stores both author and description
if (Length(CreatedBy) > 0) or (Length(Description) > 0) then
P := PChar(CreatedBy+#0+Description+#0#0)
else
P := nil;
Temp := CreateEnhMetafile(RefDC, nil, @R, P);
if Temp = 0 then raise EOutOfResources.Create('Out of Resources');;
Handle := Temp;
finally
if ReferenceDevice = 0 then ReleaseDC(0, RefDC);
end;
end;
destructor TMetafileCanvas.Destroy;
begin
FMetafile.Handle := CloseEnhMetafile(Handle);
inherited Destroy;
end;
end.