-
Notifications
You must be signed in to change notification settings - Fork 0
/
u_FolderSVGForm.pas
355 lines (297 loc) · 11.4 KB
/
u_FolderSVGForm.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
unit u_FolderSVGForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.AppEvnts, u_BaseDockForm,
Vcl.ComCtrls, Vcl.ToolWin, System.ImageList, Vcl.ImgList, Vcl.WinXCtrls,
Vcl.Menus, System.Actions, Vcl.ActnList;
type
TFormSvgFolder = class(TBaseDockForm)
Panel1: TPanel;
PageScroller1: TPageScroller;
cb_SizeSVG: TComboBox;
ToolBar1: TToolBar;
tbRootfolder: TToolButton;
ToolButton5: TToolButton;
tbCopySVGFile: TToolButton;
tbImportSVGFiles: TToolButton;
il_Folder: TImageList;
splitFolders: TSplitView;
treeFolders: TTreeView;
splFolders: TSplitter;
il_DirSVG: TImageList;
lv_DirSVG: TListView;
tbApply: TToolButton;
tbRefreshFolder: TToolButton;
pmFolder: TPopupMenu;
miApplyTo: TMenuItem;
miCopy: TMenuItem;
miSep1: TMenuItem;
miRefresh: TMenuItem;
miImportAll: TMenuItem;
miRootfolder: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure treeFoldersCollapsing(Sender: TObject; Node: TTreeNode;
var AllowCollapse: Boolean);
procedure treeFoldersExpanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
procedure treeFoldersChange(Sender: TObject; Node: TTreeNode);
procedure lv_DirSVGCustomDrawItem(Sender: TCustomListView; Item: TListItem;
State: TCustomDrawState; var DefaultDraw: Boolean);
procedure cb_SizeSVGChange(Sender: TObject);
procedure aCopyExecute(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
SvgFolder:string;
procedure ResetTree;
procedure ReadFolderTree(const ParentDirectory: string);
function ReadDirectoryNames(const ParentDirectory: string; DirectoryList: TTreeNode): Integer;
procedure ReadSVGFolder(strPath: string; ListView: TListView);
end;
var
FormSvgFolder: TFormSvgFolder;
implementation
{$R *.dfm}
uses u_MainForm, Winapi.ShellAPI, SVG, System.Types, Winapi.GDIPAPI, Winapi.GDIPOBJ,
u_TtfForm, Clipbrd, math;
procedure TFormSvgFolder.aCopyExecute(Sender: TObject);
var Img: TSVG;
RF:TRectF;
dy:single;
begin
try
Img := TSVG.Create;
Img.LoadFromFile(lv_DirSVG.Selected.SubItems[0]);
rf:=FormMain.SvgSize(Img);
if rf.CenterPoint.y > 0 then
dy := FormMain.dyTTF.FontFace.ascent
else
dy := 0;
if Img.ViewBox.Width <> Img.ViewBox.Height then
begin
Img.ViewBox.Width := max(Img.ViewBox.Height, Img.ViewBox.Width);
Img.ViewBox.Height := Img.ViewBox.Width
end;
if Img.ViewBox.Width=0 then
Img.ViewBox.Create(0,dy,FormMain.dyTTF.FontFace.units_per_em,FormMain.dyTTF.FontFace.units_per_em+dy)
else
Img.ViewBox.Create(Img.ViewBox.Left, Img.ViewBox.Top
+ dy /FormMain.dyTTF.FontFace.units_per_em * Img.ViewBox.Height,
Img.ViewBox.Left+Img.ViewBox.Height,
Img.ViewBox.Top + Img.ViewBox.Height
+ dy / FormMain.dyTTF.FontFace.units_per_em * Img.ViewBox.Height);
Clipboard.AsText := FormMain.FixedSVG(Img, 0, 0);
finally
Img.Free;
end;
end;
procedure TFormSvgFolder.cb_SizeSVGChange(Sender: TObject);
var
tmp:TImageList;
begin
lv_DirSVG.OnCustomDrawItem := nil;
tmp:=TImageList.Create(self);
tmp.Width := sz[cb_SizeSVG.ItemIndex]+16;
tmp.Height := sz[cb_SizeSVG.ItemIndex]+16;
lv_DirSVG.LargeImages := tmp;
il_DirSVG.free;
il_DirSVG:= tmp;
if lv_DirSVG.Selected<> nil then
lv_DirSVG.Selected.MakeVisible(False);
lv_DirSVG.OnCustomDrawItem := lv_DirSVGCustomDrawItem;
end;
procedure TFormSvgFolder.FormCreate(Sender: TObject);
begin
ResetTree;
cb_SizeSVGChange(Nil)
end;
procedure TFormSvgFolder.ResetTree;
begin
splitFolders.Opened := treeFolders.Items.Count >1;
splFolders.Visible := treeFolders.Items.Count >1;
splFolders.Left := 500
end;
procedure TFormSvgFolder.treeFoldersChange(Sender: TObject; Node: TTreeNode);
begin
ReadSVGFolder(PAnsiString(Node.Data)^, lv_DirSVG);
end;
procedure TFormSvgFolder.treeFoldersCollapsing(Sender: TObject; Node: TTreeNode;
var AllowCollapse: Boolean);
begin
AllowCollapse := Node.Level>0;
end;
procedure TFormSvgFolder.treeFoldersExpanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
var
n: TTreeNode;
s:string;
i:integer;
begin
for i:= 0 to Node.Count-1 do
if Node.item[i].Count=0 then
ReadDirectoryNames(PAnsiString(Node.item[i].Data)^ , Node.item[i]);
end;
procedure TFormSvgFolder.lv_DirSVGCustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
R:TRect;
Img:TSVG;
RF:TRectF;
Kf,Kx,dy:single;
BMP: TBitmap;
Bs: TGPRectF;
Graphics:TGPGraphics;
s:string;
begin
R := Item.DisplayRect(drIcon);
if R.Width = 0 then exit;
kf := sz[cb_SizeSVG.ItemIndex] / FormMain.dyTTF.FontFace.units_per_em;
with Sender.Canvas do
begin
Pen.Style := psDot;
Pen.Color := clRed;
MoveTo(r.Left+8,r.Top);LineTo(r.Left+8,r.Bottom);
MoveTo(r.Left,r.Top + round(FormMain.dyTTF.FontFace.ascent* kf));LineTo(r.Right-8,r.Top+ round(FormMain.dyTTF.FontFace.ascent* kf));
Pen.Color := clBlue;
MoveTo(r.Left,r.Top);LineTo(r.Right-8,r.Top);
MoveTo(r.Left,r.Top + sz[cb_SizeSVG.ItemIndex] );LineTo(r.Right-8,r.Top+ sz[cb_SizeSVG.ItemIndex]);
Pen.Color := clSilver;
MoveTo(r.Left,r.Top + round((FormMain.dyTTF.FontFace.ascent - FormMain.dyTTF.FontFace.Cap_Height)* kf));LineTo(r.Right-8,r.Top+ round((FormMain.dyTTF.FontFace.ascent-FormMain.dyTTF.FontFace.Cap_Height) * kf));
MoveTo(r.Left,r.Top + round((FormMain.dyTTF.FontFace.ascent-FormMain.dyTTF.FontFace.X_Height)* kf));LineTo(r.Right-8,r.Top+ round((FormMain.dyTTF.FontFace.ascent-FormMain.dyTTF.FontFace.X_Height)* kf));
end;
Img := TSVG.Create;
try
try
Img.LoadFromFile(Item.SubItems[0]);
except
// Img.LoadFromText('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><text style="font-size:16;stroke:none;fill:red" left="10" top="50">ERROR!</text></svg>')
Img.LoadFromText('<?xml version="1.0" encoding="UTF-8"?>'
+'<svg width="256" height="256" version="1.1" viewBox="0 0 256 256" xml:space="preserve" xmlns="http://www.w3.org/2000/svg">'
+'<g transform="translate(1 1) scale(3)">'
+' <path d="m45 88h41c3 0 5-3 4-6l-41-78c-2-3-6-3-7 0l-41 78c-1 3 0.6 6 4 6h41z" fill="#d60000" stroke-linecap="round"/>'
+' <path d="m45 64v0c-2 0-3-1-3-3l-3-26c-0.2-3 2-6 6-6h0c3 0 6 3 6 6l-3 26c-0.07 2-1 3-3 3z" fill="#fff" stroke-linecap="round"/>'
+' <circle cx="45" cy="74" r="5" fill="#fff"/></g></svg>');
end;
rf:=FormMain.SvgSize(Img);
if FormTTF.chbOutline.Checked then
formMain.AddOutline(Img);
if rf.CenterPoint.y < 0 then
dy := -FormMain.dyTTF.FontFace.ascent
else
dy := 0;
if Img.ViewBox.Width=0 then
Img.ViewBox.Create(0,dy,FormMain.dyTTF.FontFace.units_per_em,FormMain.dyTTF.FontFace.units_per_em+dy)
else
Img.ViewBox.Create(Img.ViewBox.Left,Img.ViewBox.Top + dy /FormMain.dyTTF.FontFace.units_per_em *Img.ViewBox.Height, Img.ViewBox.Left+Img.ViewBox.Height, Img.ViewBox.Top + Img.ViewBox.Height+ dy /FormMain.dyTTF.FontFace.units_per_em *Img.ViewBox.Height);
Img.PaintTo(Sender.Canvas.Handle, r.Left+8, r.Top, sz[cb_SizeSVG.ItemIndex], sz[cb_SizeSVG.ItemIndex]);
s := ExtractFileName(Item.SubItems[0]);
if (copy(s,1,2)='U+') and (StrToIntDef('$'+copy(s+'----',3,4),-1)>0) then
Sender.Canvas.Font.Style := [fsBold];
finally
img.free;
end;
end;
function TFormSvgFolder.ReadDirectoryNames(const ParentDirectory: string;
DirectoryList: TTreeNode): Integer;
var
Status: Integer;
SearchRec: TSearchRec;
FileInfo: SHFILEINFO;
Icon: TIcon;
begin
Icon := TIcon.Create;
if DirectoryList.Text='' then
begin
DirectoryList.Text := ExtractFileName(ParentDirectory);
SHGetFileInfo(PChar(ParentDirectory), 0, FileInfo, SizeOf(FileInfo), SHGFI_DISPLAYNAME);
if FileInfo.szDisplayName<>'' then
DirectoryList.Text := FileInfo.szDisplayName;
//Get The Icon That Represents The File
SHGetFileInfo(PChar(ParentDirectory), 0, FileInfo, SizeOf(FileInfo), SHGFI_ICON or SHGFI_SMALLICON);
icon.Handle := FileInfo.hIcon;
// il_Folder.Width := icon.Width;
// il_Folder.Height := icon.Height;
DirectoryList.ImageIndex := il_Folder.AddIcon(Icon);
DirectoryList.SelectedIndex := DirectoryList.ImageIndex;
DirectoryList.Data := NewStr(ParentDirectory);
// Destroy the Icon
DestroyIcon(FileInfo.hIcon);
end;
Result := 1;
Status := FindFirst(ExcludeTrailingPathDelimiter(ParentDirectory)+'\*.*', faDirectory or faVolumeID , SearchRec);
try
while Status = 0 do
begin
if (SearchRec.Name<>'..') and (SearchRec.Name<>'.') and
((SearchRec.Attr and FaDirectory = FaDirectory) or (SearchRec.Attr and FaVolumeId = FaVolumeID))
then
with treeFolders.Items.AddChild(DirectoryList,SearchRec.Name) do
begin
Inc(Result);
// Text := ExtractFileName(SearchRec.Name);
SHGetFileInfo(PChar(IncludeTrailingPathDelimiter(ParentDirectory)+SearchRec.Name), 0, FileInfo, SizeOf(FileInfo), SHGFI_DISPLAYNAME);
Text := FileInfo.szDisplayName;
//Get The Icon That Represents The File
SHGetFileInfo(PChar(IncludeTrailingPathDelimiter(ParentDirectory)+SearchRec.Name), 0, FileInfo, SizeOf(FileInfo), SHGFI_ICON or SHGFI_SMALLICON);
icon.Handle := FileInfo.hIcon;
ImageIndex := il_Folder.AddIcon(Icon);
SelectedIndex := ImageIndex;
Data := NewStr(IncludeTrailingPathDelimiter(ParentDirectory)+SearchRec.Name);
// Destroy the Icon
DestroyIcon(FileInfo.hIcon);
// Inc(Result, ReadDirectoryNames(ParentDirectory+'\'+SearchRec.Name, treeFolders.Items.AddChild(DirectoryList,'')));
end;
Status := FindNext(SearchRec);
end;
finally
FindClose(SearchRec);
end;
Icon.Free;
end;
procedure TFormSvgFolder.ReadFolderTree(const ParentDirectory: string);
begin
treeFolders.Items.Clear;
treeFolders.Items.BeginUpdate;
ReadDirectoryNames(ParentDirectory, treeFolders.Items.AddFirst(nil,''));
treeFolders.Items[0].Expand(False);
treeFolders.Items[0].Selected := True;
treeFolders.Items.EndUpdate;
ResetTree;
end;
procedure TFormSvgFolder.ReadSVGFolder(strPath: string; ListView: TListView);
var
i: Integer;
SearchRec: TSearchRec;
ListItem: TListItem;
OldEvent:procedure;
begin
SvgFolder := strPath;
Caption := 'SVG Folder: '+ strPath;
ListView.OnCustomDrawItem := nil;
ListView.Items.BeginUpdate;
ListView.Clear;
try
// search for the first file
i := FindFirst(strPath + '\*.svg', faAnyFile, SearchRec);
while i = 0 do
begin
with ListView do
begin
if ((SearchRec.Attr and FaDirectory <> FaDirectory) and
(SearchRec.Attr and FaVolumeId <> FaVolumeID)) then
begin
ListItem := ListView.Items.Add;
Listitem.Caption := ExtractFileName(SearchRec.Name);
ListItem.SubItems.Add(strPath + '\' + SearchRec.Name);
end;
end;
i := FindNext(SearchRec);
end;
finally
ListView.Items.EndUpdate;
ListView.OnCustomDrawItem := lv_DirSVGCustomDrawItem;
end;
end;
end.