-
Notifications
You must be signed in to change notification settings - Fork 2
/
DiagramReader.dpr
180 lines (171 loc) · 5.86 KB
/
DiagramReader.dpr
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
library DiagramReader;
uses
FastMM4,
Windows,
SysUtils,
Math,
Types,
Graphics,
MemUtils,
MathUtils,
AnalyzerPlugins in 'AnalyzerPlugins.pas',
DiagramBase in 'DiagramBase.pas',
LineObject in 'LineObject.pas',
PictureObject in 'PictureObject.pas',
ShapeObject in 'ShapeObject.pas',
GroupObject in 'GroupObject.pas',
FormsDummy in 'FormsDummy.pas',
TemplateObjects in 'TemplateObjects.pas';
{$R *.RES}
function LibGetLoadFilter: PChar; stdcall;
begin
Result:=PChar(rsDiagramFileFilter+'|'+rsTemplatePaletteFilter);
end;
function LibLoadScaleImage(FileName,FileType: PChar; var Image: TImageContainer): Integer; stdcall;
var
Design : TDiagramContainer;
Y, Scale : Integer;
Bitmap, ScaleBitmap : TBitmap;
CanvasInfo : TCanvasInfo;
Templates : TTemplateSheet;
Background : TRectangleObject;
AutoSize : Boolean;
BackgroundColor : TColor;
Size : TPoint;
begin
AutoSize:=(Image.Width<0) or (Image.Height<0);
if not AutoSize then Size:=Point(Image.Width,Image.Height);
Result:=0;
Design:=TDiagramContainer.Create;
try
try
// Load design
if FileType='DDT' then
begin
Templates:=TTemplateSheet.Create;
try
Templates.LoadFromFile(FileName);
Design.Add(TDiagramPage.Create);
Design.Pages[0].Add(TDiagramLayer.Create);
Templates.CopyToPage(Design.Pages[0]);
finally
Templates.Free;
end;
BackgroundColor:=clBtnFace;
end
else
begin
Design.LoadFromFile(FileName);
if AutoSize then BackgroundColor:=clWhite
else BackgroundColor:=$faffff;
end;
// Create background rectangle
Background:=TRectangleObject.Create;
Background.Properties[opFillColor]:=BackgroundColor;
Background.Properties[opLineColor]:=BackgroundColor;
if Design.Count>0 then Background.Position:=Rect(0,0,Design.Pages[0].Width,Design.Pages[0].Height)
else Result:=3;
Design.Stencil.Insert(0,Background);
except
if FileExists(FileName) then Result:=3
else Result:=2;
end;
ZeroMem(Image,SizeOf(Image));
if Result=0 then
begin
ZeroMem(CanvasInfo,SizeOf(CanvasInfo));
CanvasInfo.DrawMode:=dmPreview;
CanvasInfo.DefaultFont:=TFont.Create;
try
if Design.DefaultFontName='' then
begin
CanvasInfo.DefaultFont.Name:='Arial';
CanvasInfo.DefaultFont.Size:=10;
end
else
begin
CanvasInfo.DefaultFont.Name:=Design.DefaultFontName;
CanvasInfo.DefaultFont.Size:=Design.DefaultFontSize;
end;
CanvasInfo.Container:=Design;
Scale:=1;
if AutoSize then // Scale using screen DPI
begin
CanvasInfo.Scale:=FloatPoint(GetDeviceCaps(GetDC(0),LOGPIXELSX)/DesignerDPI,GetDeviceCaps(GetDC(0),LOGPIXELSY)/DesignerDPI);
Image.PixelFormat:=$03;
end
else // Use specified size
begin
if Size.X<40 then Scale:=4
else if Size.X<200 then Scale:=2; // Do antialiasing
CanvasInfo.Scale.X:=Scale*Min(Size.X/Design.Pages[0].Width,Size.Y/Design.Pages[0].Height);
CanvasInfo.Scale.Y:=CanvasInfo.Scale.X;
Image.PixelFormat:=$04;
end;
Image.Width:=Ceil(Design.Pages[0].Width*CanvasInfo.Scale.X);
Image.Height:=Ceil(Design.Pages[0].Height*CanvasInfo.Scale.Y);
if (Image.Width=0) or (Image.Height=0) then Result:=3
else
begin
Bitmap:=TBitmap.Create;
try
Bitmap.PixelFormat:=pf24bit;
Bitmap.Width:=Image.Width;
Bitmap.Height:=Image.Height;
Design.Pages[0].Draw(Bitmap.Canvas,CanvasInfo);
if not AutoSize then
begin
Image.Width:=Size.X;
Image.Height:=Size.X;
ScaleBitmap:=TBitmap.Create;
ScaleBitmap.PixelFormat:=pf32bit;
ScaleBitmap.Width:=Image.Width;
ScaleBitmap.Height:=Image.Height;
if Scale>1 then SetStretchBltMode(ScaleBitmap.Canvas.Handle,STRETCH_HALFTONE);
StretchBlt(ScaleBitmap.Canvas.Handle,
(ScaleBitmap.Width-Bitmap.Width div Scale) div 2,
(ScaleBitmap.Height-Bitmap.Height div Scale) div 2,
Bitmap.Width div Scale,Bitmap.Height div Scale,
Bitmap.Canvas.Handle,0,0,Bitmap.Width,Bitmap.Height,
SRCCOPY);
Bitmap.Free;
Bitmap:=ScaleBitmap;
end;
Image.BytesPerLine:=Image.Width*Image.PixelFormat;
GetMem(Image.Map,Image.Height*Image.BytesPerLine);
for Y:=0 to Image.Height-1 do Move(Bitmap.ScanLine[Y]^,Image.Map^[Y*Image.BytesPerLine],Image.BytesPerLine);
finally
Bitmap.Free;
end;
end;
finally
CanvasInfo.DefaultFont.Free;
end;
end;
finally
Design.Free;
end;
end;
function LibLoadImage(FileName,FileType: PChar; var Image: TImageContainer): Integer; stdcall;
begin
Image.Width:=-1;
Image.Height:=-1;
result:=LibLoadScaleImage(FileName,FileType,Image);
end;
function LibFreeImage(Image: PImageContainer): Integer; stdcall;
begin
if Assigned(Image) then
begin
if Assigned(Image.Map) then FreeMem(Image.Map);
if Assigned(Image.Palette) then FreeMem(Image.Palette);
Result:=0;
end
else Result:=1;
end;
exports
LibGetLoadFilter name '?LibGetLoadFilter@@YGPADXZ',
LibGetLoadFilter name '?LibGetSaveFilter@@YGPADXZ', // To compensate for bug in old ImageDLLDoader
LibLoadImage name '?LibLoadImage@@YGHPAD0PAUTImageContainer@@@Z',
LibLoadScaleImage,
LibFreeImage name '?LibFreeImage@@YGHPAUTImageContainer@@@Z';
end.