-
Notifications
You must be signed in to change notification settings - Fork 0
/
digit_utils.pas
387 lines (329 loc) · 11.3 KB
/
digit_utils.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
(*******************************************************************************
** DigIt **
** **
** (s) 2023 Massimo Magnano **
** **
********************************************************************************
** Utils Functions **
*******************************************************************************)
unit DigIt_Utils;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, ComCtrls, DOM, XMLConf, Laz2_DOM, Laz_XMLStreaming, Laz2_XMLCfg,
FPImage, Menus, DigIt_types, BGRAPapers;
resourcestring
rsDestination_Default = 'Save as Files';
rsVertical = ' Vertical';
rsHorizontal = ' Horizontal';
function CreateXMLWriter(ADoc: TDOMDocument; const Path: string; Append: Boolean; var DestroyDriver: boolean): TWriter;
function CreateXMLReader(ADoc: TDOMDocument; const Path: string; var DestroyDriver: boolean): TReader;
procedure WritePersistentToXMLConfig(XMLConfig: TXMLConfig; const Path, AName: string; AData: TPersistent);
procedure ReadPersistentFromXMLConfig(XMLConfig: TXMLConfig; const Path, AName: string;
AData: TPersistent; OnFindComponentClass: TFindComponentClassEvent=nil);
procedure BuildPaperSizesMenu(ResUnit: TResolutionUnit;
AOwner: TComponent; menuPaperSizes: TMenu; menuOnClick: TNotifyEvent;
VImageIndex, HImageIndex: Integer);
procedure PaperSizesMenuTag_decode(ATag:Integer; var ResUnit: TResolutionUnit; var Paper: TPaperSize);
function PaperSizesMenuTag_encode(ResUnit: TResolutionUnit; vert: Boolean; pIndex, iIndex: Byte): Integer;
procedure BuildSourcesMenu(AOwner: TComponent; menuSources: TMenu; menuOnClick: TNotifyEvent);
procedure BuildDestinationsMenu(AOwner: TComponent; menuDestinations: TMenu; menuOnClick: TNotifyEvent);
function FindMenuItemByTag(AMenu: TMenu; ATag: PtrInt): TMenuItem;
procedure GetThumnailSize(thumbWidth, thumbHeight, imgWidth, imgHeight:Integer;
var newWidth, newHeight:Integer);
implementation
uses Digit_Bridge_Intf, Digit_Bridge_Impl;
type
//Workaround class so we can write a TPersistent using TWriter.WriteRootComponent
TPersistentComponent = class(TComponent)
protected
rData:TPersistent;
published
property Data:TPersistent read rData write rData;
end;
function CreateXMLWriter(ADoc: TDOMDocument; const Path: string; Append: Boolean; var DestroyDriver: boolean): TWriter;
var
Driver: TAbstractObjectWriter;
begin
Driver:=TXMLObjectWriter.Create(ADoc,Path,Append);
DestroyDriver:=true;
Result:=TWriter.Create(Driver);
end;
function CreateXMLReader(ADoc: TDOMDocument; const Path: string; var DestroyDriver: boolean): TReader;
var
p: Pointer;
Driver: TAbstractObjectReader;
DummyStream: TMemoryStream;
begin
DummyStream:=TMemoryStream.Create;
try
Result:=TReader.Create(DummyStream,256);
DestroyDriver:=false;
// hack to set a write protected variable.
// DestroyDriver:=true; TReader will free it
Driver:=TXMLObjectReader.Create(ADoc,Path);
p:=@Result.Driver;
Result.Driver.Free;
TAbstractObjectReader(p^):=Driver;
finally
DummyStream.Free;
end;
end;
procedure WritePersistentToXMLConfig(XMLConfig: TXMLConfig; const Path, AName: string; AData: TPersistent);
var
Writer: TWriter;
DestroyDriver: boolean;
tt:TPersistentComponent;
begin
Writer:=nil;
DestroyDriver:=false;
try
tt:=TPersistentComponent.Create(nil);
tt.Name:=AName;
tt.Data :=AData;
Writer:=CreateXMLWriter(XMLConfig.Document,Path,false,DestroyDriver);
XMLConfig.Modified:=true;
Writer.WriteRootComponent(tt);
XMLConfig.Flush;
finally
if DestroyDriver and (Writer<>nil)
then Writer.Driver.Free;
Writer.Free;
tt.Free;
end;
end;
procedure ReadPersistentFromXMLConfig(XMLConfig: TXMLConfig; const Path, AName: string;
AData: TPersistent; OnFindComponentClass: TFindComponentClassEvent=nil);
var
DestroyDriver: Boolean;
Reader: TReader;
IsInherited: Boolean;
AClassName: String;
AClass: TComponentClass;
tt:TPersistentComponent;
begin
if (AData=nil)
then raise Exception.Create('ReadPersistentFromXMLConfig AData=nil');
Reader:=nil;
try
Reader:=CreateXMLReader(XMLConfig.Document,Path,DestroyDriver);
Reader.OnFindComponentClass:=OnFindComponentClass;
// get root class
AClassName:=(Reader.Driver as TXMLObjectReader).GetRootClassName(IsInherited);
if (AClassName<>'TPersistentComponent')
then raise Exception.Create('Invalid ClassName '+AClassName+' in XML (maybe TPersistentComponent)');
tt :=TPersistentComponent.Create(nil);
tt.Name:=AName;
tt.Data:=AData;
Reader.ReadRootComponent(tt);
finally
if DestroyDriver
then Reader.Driver.Free;
Reader.Free;
tt.Free;
end;
end;
procedure BuildPaperSizesMenu(ResUnit: TResolutionUnit; AOwner: TComponent; menuPaperSizes: TMenu;
menuOnClick: TNotifyEvent; VImageIndex, HImageIndex: Integer);
var
p,
i :Integer;
newItem, newItem2 :TMenuItem;
curPapers: array of TPaperSizes;
u: String[5];
preCaption: String;
begin
if ResUnit=ruPixelsPerInch
then begin
curPapers:=PaperSizes_inch;
u:=' in';
end
else begin
curPapers:=PaperSizes_cm;
u:=' cm';
ResUnit:=ruPixelsPerCentimeter;
end;
//Vertical
newItem :=TMenuItem.Create(AOwner);
newItem.Caption:= rsVertical;
newItem.ImageIndex:=VImageIndex;
menuPaperSizes.Items.Add(newItem);
for p:=Low(curPapers) to High(curPapers) do
begin
newItem :=TMenuItem.Create(AOwner);
newItem.Caption:=PaperSizes_Names[p];
newItem.Tag:=p;
//newItem.ImageIndex:=VImageIndex;
menuPaperSizes.Items.Add(newItem);
for i:=Low(curPapers[p]) to High(curPapers[p]) do
begin
newItem2 :=TMenuItem.Create(AOwner);
preCaption :=curPapers[p][i].name;
if (preCaption<>'')
then preCaption :=preCaption+' - ';
newItem2.Caption:=preCaption+
FloatToStrF(curPapers[p][i].w, ffFixed, 15, 2)+' x '+
FloatToStrF(curPapers[p][i].h, ffFixed, 15, 2)+u;
newItem2.OnClick:=menuOnClick;
newItem2.Tag:=PaperSizesMenuTag_encode(ResUnit, True, p, i);
newItem.Add(newItem2);
end;
end;
menuPaperSizes.Items.AddSeparator;
//Horizontal
newItem :=TMenuItem.Create(AOwner);
newItem.Caption:= rsHorizontal;
newItem.ImageIndex:=HImageIndex;
menuPaperSizes.Items.Add(newItem);
for p:=Low(curPapers) to High(curPapers) do
begin
newItem :=TMenuItem.Create(AOwner);
newItem.Caption:=PaperSizes_Names[p];
newItem.Tag:=p;
//newItem.ImageIndex:=HImageIndex;
menuPaperSizes.Items.Add(newItem);
for i:=Low(curPapers[p]) to High(curPapers[p]) do
begin
newItem2 :=TMenuItem.Create(AOwner);
preCaption :=curPapers[p][i].name;
if (preCaption<>'')
then preCaption :=preCaption+' - ';
newItem2.Caption:=preCaption+
FloatToStrF(curPapers[p][i].h, ffFixed, 15, 2)+' x '+
FloatToStrF(curPapers[p][i].w, ffFixed, 15, 2)+u;
newItem2.OnClick:=menuOnClick;
newItem2.Tag:=PaperSizesMenuTag_encode(ResUnit, False, p, i);
newItem.Add(newItem2);
end;
end;
end;
procedure PaperSizesMenuTag_decode(ATag: Integer; var ResUnit: TResolutionUnit; var Paper: TPaperSize);
var
p, i :Integer;
vert :Boolean;
t :Single;
begin
// ResUnit - vert(1bit) - pIndex(8bit) - iIndex(8bit)
i :=(ATag and $00FF);
p :=(ATag and $FF00) shr 8;
vert :=Boolean((ATag and $10000) shr 16);
ResUnit :=TResolutionUnit((ATag and $E0000) shr 17);
if (ResUnit=ruPixelsPerCentimeter)
then Paper :=PaperSizes_cm[p][i]
else Paper :=PaperSizes_inch[p][i];
if not(vert) then
begin
//Swap w and h
t :=Paper.h;
Paper.h:=Paper.w;
Paper.w:=t;
end;
end;
function PaperSizesMenuTag_encode(ResUnit: TResolutionUnit; vert: Boolean; pIndex, iIndex: Byte): Integer;
begin
// ResUnit - vert(1bit) - pIndex(8bit) - iIndex(8bit)
Result :=(Byte(ResUnit) shl 17) or (Byte(vert) shl 16) or (pIndex shl 8) or iIndex;
end;
procedure BuildSourcesMenu(AOwner: TComponent; menuSources: TMenu; menuOnClick: TNotifyEvent);
var
i, res :Integer;
newItem :TMenuItem;
curSource:PSourceInfo;
curTitle:PChar;
begin
for i:=0 to theBridge.SourcesImpl.Count-1 do
begin
curSource:= theBridge.SourcesImpl.Data[i];
if (curSource<>nil) then
begin
curTitle:= '';
newItem:= TMenuItem.Create(AOwner);
res:= curSource^.Inst.UI_Title(curTitle);
if (res >0 ) and (curTitle <> '')
then begin
newItem.Caption:= curTitle;
StrDispose(curTitle);
end
else newItem.Caption:= theBridge.SourcesImpl.Key[i];
newItem.ImageIndex:= curSource^.Inst.UI_ImageIndex;
newItem.Tag:= i;
newItem.OnClick:= menuOnClick;
newItem.Enabled:= curSource^.Inst.Enabled;
menuSources.Items.Add(newItem);
end;
end;
end;
procedure BuildDestinationsMenu(AOwner: TComponent; menuDestinations: TMenu; menuOnClick: TNotifyEvent);
var
i, res :Integer;
newItem :TMenuItem;
//curDestination:PDestinationInfo;
curTitle:PChar;
begin
//Add SaveAsFile Destination
newItem:= TMenuItem.Create(AOwner);
newItem.Caption:= rsDestination_Default;
newItem.ImageIndex:= 6;
newItem.Tag:= -1;
newItem.OnClick:= menuOnClick;
menuDestinations.Items.Add(newItem);
{ #note -oMaxM : Not enabled for now until I figure out how to pass the image data and make the thumbnails }
(*
for i:=0 to theBridge.DestinationsImpl.Count-1 do
begin
curDestination:= theBridge.DestinationsImpl.Data[i];
if (curDestination<>nil) then
begin
curTitle:= '';
newItem:= TMenuItem.Create(AOwner);
res:= curDestination^.Inst.UI_Title(curTitle);
if (res >0 ) and (curTitle <> '')
then begin
newItem.Caption:= curTitle;
StrDispose(curTitle);
end
else newItem.Caption:= theBridge.DestinationsImpl.Key[i];
newItem.ImageIndex:= curDestination^.Inst.UI_ImageIndex;
newItem.Tag:= i;
newItem.OnClick:= menuOnClick;
newItem.Enabled:= curDestination^.Inst.Enabled;
menuDestinations.Items.Add(newItem);
end;
end;
*)
end;
function FindMenuItemByTag(AMenu: TMenu; ATag: PtrInt): TMenuItem;
var
i: Integer;
begin
Result:= Nil;
//No Need to recurvilly find in subitems
for i:=0 to AMenu.Items.Count-1 do
if (AMenu.Items[i].Tag = ATag)
then begin
Result:= AMenu.Items[i];
break;
end;
end;
procedure GetThumnailSize(thumbWidth, thumbHeight, imgWidth, imgHeight:Integer;
var newWidth, newHeight:Integer);
var
rW, rH:Single;
begin
if (thumbWidth=0)
then rW:=1
else rW := imgWidth / thumbWidth;
if (thumbHeight=0)
then rH:=1
else rH := imgHeight / thumbHeight;
if (rW > rH)
then begin
newHeight := round(imgHeight / rW);
newWidth := thumbWidth;
end
else begin
newWidth := round(imgWidth / rH);
newHeight := thumbHeight;
end;
end;
end.