/
uclipboard.pas
598 lines (555 loc) 路 15.5 KB
/
uclipboard.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// SPDX-License-Identifier: GPL-3.0-only
unit UClipboard;
{$mode objfpc}{$H+}
{$IFDEF DEBUG}
{ $DEFINE DEBUG_CLIPBOARD}
{$ENDIF}
{$DEFINE HTML_CLIPBOARD_FORMAT}
{$IFDEF DARWIN}
{$DEFINE TIFF_CLIPBOARD_FORMAT}
{$ELSE}
{$DEFINE BMP_CLIPBOARD_FORMAT}
{$DEFINE PNG_CLIPBOARD_FORMAT}
{$ENDIF}
{$IFDEF WINDOWS}
{$DEFINE PDN_CLIPBOARD_FORMAT}
{$ENDIF}
interface
uses
Classes, SysUtils, BGRABitmap;
procedure CopyToClipboard(bmp: TBGRABitmap);
function GetBitmapFromClipboard: TBGRABitmap;
function ClipboardContainsBitmap: boolean;
implementation
uses Dialogs, BGRABitmapTypes, Clipbrd, Graphics, LCLIntf, LCLType, GraphType
{$IFDEF PDN_CLIPBOARD_FORMAT}, math, BGRADNetDeserial{$ENDIF}
{$IFDEF BMP_CLIPBOARD_FORMAT}, FPWriteBMP{$ENDIF}
{$IFDEF HTML_CLIPBOARD_FORMAT}, UOnline{$ENDIF};
{$IFDEF DEBUG_CLIPBOARD}
const
moreMimeTypes : array[0..6] of string =
('image/x-xbitmap','image/gif','image/jpeg','image/pjpeg','image/png','image/x-png','image/tiff');
{$ENDIF}
{$IFDEF TIFF_CLIPBOARD_FORMAT}
var
tiffClipboardFormat: TClipboardFormat;
{$ENDIF}
{$IFDEF PNG_CLIPBOARD_FORMAT}
var
pngClipboardFormat: TClipboardFormat;
{$ENDIF}
{$IFDEF PDN_CLIPBOARD_FORMAT}
var
pdnClipboardFormat: TClipboardFormat;
function GetBitmapFromPaintDotNetMaskedSurface(deserial: TDotNetDeserialization): TBGRABitmap;
var width,height: integer;
dataObj: TSerializedArray;
data: pointer;
dataSize: longword;
mainObj: TSerializedClass;
segments,segment,poly: TSerializedArray;
point: TSerializedClass;
mask: TBGRABitmap;
polyPts: array of TPointF;
polyList: array of array of TPointF;
i: Integer;
j: Integer;
k: Integer;
minx,miny: double;
offset: TPointF;
begin
result := nil;
mainObj := deserial.FindClass('PaintDotNet.MaskedSurface');
try
width := StrToInt(deserial.GetSimpleField(mainObj,'surface\width'));
height := StrToInt(deserial.GetSimpleField(mainObj,'surface\height'));
dataObj := deserial.GetObjectField(mainObj, 'surface\scan0\chunk0') as TSerializedArray;
if dataObj <> nil then
begin
data := dataObj.ItemPtr[0];
dataSize := dataObj.ItemSize * dataObj.FieldCount;
result := TBGRABitmap.Create(width,height);
minx := 1e100;
miny := 1e100;
move(data^, result.Data^, min(dataSize, int64(result.NbPixels) * sizeof(TBGRAPixel)));
if result.LineOrder = riloBottomToTop then result.VerticalFlip;
mask := TBGRABitmap.Create(width,height, BGRABlack);
polyList := nil;
try
segments := deserial.GetObjectField(mainObj,'geometryMask\polygonList\segments') as TSerializedArray;
if segments <> nil then
begin
for i := 0 to segments.FieldCount-1 do
begin
segment := deserial.GetObjectField(segments,i) as TSerializedArray;
if segment <> nil then
for j := 0 to segment.FieldCount-1 do
begin
poly := deserial.GetObjectField(segment,j) as TSerializedArray;
if poly <> nil then
begin
polyPts := nil;
setlength(polyPts, poly.FieldCount);
for k := 0 to high(polyPts) do
begin
point := deserial.GetObjectField(poly,k) as TSerializedClass;
polyPts[k] := PointF(StrToFloat(point.FieldByNameAsString['_x']),
StrToFloat(point.FieldByNameAsString['_y']));
if polyPts[k].x < minx then minx := polyPts[k].x;
if polyPts[k].y < miny then miny := polyPts[k].y;
end;
setlength(polyList, length(polyList)+1);
polyList[high(polyList)] := polyPts;
end;
end;
end;
end;
offset := PointF(floor(minx)+0.5,floor(miny)+0.5);
for i := 0 to high(polyList) do
begin
polyPts := polyList[i];
for j := 0 to high(polyPts) do
polyPts[j] -= offset;
mask.FillPolyAntialias(polyPts,BGRAWhite);
end;
result.ApplyMask(mask);
finally
mask.Free;
end;
end;
except
on ex:Exception do
begin
//nothing
end;
end;
end;
{$ENDIF}
{$IFDEF HTML_CLIPBOARD_FORMAT}
var
htmlClipboardFormat: TClipboardFormat;
function WideStringToStr(data: string): string;
var
i: integer;
isWidestring: boolean;
w: WideString;
begin
isWidestring := (length(data)>0) and ((length(data) and 1) = 0);
i := 2;
while (i <= length(data)) do
begin
if data[i] <> #0 then
begin
isWidestring := false;
break;
end;
inc(i,2);
end;
if isWidestring then
begin
w := '';
setlength(w, length(data) div 2);
move(data[1],w[1],length(data));
result := UTF8Encode(w);
end
else result := data;
end;
function HtmlEntitiesToText(data: string): string;
var p,start: integer;
entity: string;
charcode,errpos: integer;
begin
p := 1;
while p <= length(data)-1 do
begin
if (data[p]='&') and (data[p+1] in ['#','a'..'z','A'..'Z']) then
begin
start := p;
inc(p);
while (p < length(data)) and (data[p+1] in['0'..'9','a'..'z','A'..'Z']) do inc(p);
entity := copy(data,start,p-start +1);
if (p < length(data)) and (data[p+1] = ';') then inc(p);
delete(data, start, p-start+1);
p := start;
case entity of
' ': entity := #160;
'<': entity := '<';
'>': entity := '>';
'&': entity := '&';
else
begin
if copy(entity,1,2)='&#' then
begin
val(copy(entity,3,length(entity)-2),charcode,errpos);
if (errpos = 0) and (charcode <= 127) then
entity := char(charcode);
end;
end;
end;
insert(entity,data,p);
inc(p,length(entity));
continue;
end;
inc(p);
end;
result := data;
end;
function GetBitmapFromTag(tokens: TStringList): TBGRABitmap;
var
i: Integer;
stream: TMemoryStream;
url: string;
begin
result := nil;
if tokens.Count > 0 then
begin
if UpperCase(tokens[0]) = 'IMG' then
begin
for i := 1 to tokens.count-3 do
if (UpperCase(tokens[i])='SRC')
and (tokens[i+1]='=') and (tokens[i+2][1] in ['''','"']) then
begin
url := HtmlEntitiesToText(copy(tokens[i+2],2,length(tokens[i+2])-2));
if copy(url,1,8) = 'https://' then
delete(url,5,1);
stream := TMemoryStream.Create;
try
MyHttpGet(url,stream);
stream.Position:= 0;
result := TBGRABitmap.Create(stream);
except on ex: exception do begin
ShowMessage(ex.Message);
end;
end;
stream.Free;
if result <> nil then exit;
end;
end;
end;
result := nil;
end;
function GetBitmapFromHtml(data: string): TBGRABitmap;
var
p: integer;
inTag, inComment: boolean;
tagTokens: TStringList;
inStr1, inStr2, inId, inNum: integer;
begin
result := nil;
data := WideStringToStr(data);
inTag := false;
inComment := false;
inStr1 := 0;
inStr2 := 0;
inId := 0;
inNum := 0;
tagTokens := TStringList.Create;
p := 1;
while p <= length(data) do
begin
if inComment then
begin
if data[p] = '-' then
begin
if copy(data,p,3) = '-->' then
begin
p += 3;
inComment:= false;
continue;
end;
end;
end else
if inStr1<>0 then
begin
if data[p] = '''' then
begin
tagTokens.add(copy(data,inStr1,p-inStr1+1));
inStr1 := 0;
end;
end else
if inStr2<>0 then
begin
if data[p] = '"' then
begin
tagTokens.add(copy(data,inStr2,p-inStr2+1));
inStr2 := 0;
end;
end else
begin
if inId<>0 then
begin
if not (data[p] in['A'..'Z','a'..'z',':','.']) then
begin
tagTokens.add(copy(data,inId,p-inId));
inId := 0;
end else
begin
inc(p);
continue;
end;
end;
if inNum<>0 then
begin
if not (data[p] in['0'..'9','.']) then
begin
tagTokens.add(copy(data,inNum,p-inNum));
inNum := 0;
end else
begin
inc(p);
continue;
end;
end;
if data[p]='<' then
begin
if copy(data,p,4) = '<!--' then
begin
p += 4;
inComment := true;
continue;
end else
inTag := true;
end else
if inTag then
begin
if data[p] = '''' then
inStr1 := p
else if data[p] = '"' then
inStr2 := p
else if data[p] in ['A'..'Z','a'..'z'] then
inId := p
else if data[p] in ['0'..'9','+','-'] then
inNum := p
else if data[p] = '>' then
begin
inTag := false;
result := GetBitmapFromTag(tagTokens);
tagTokens.clear;
if result <> nil then
begin
tagTokens.Free;
exit;
end;
end else
if data[p]>#32 then
tagTokens.Add(data[p]);
end;
end;
inc(p);
end;
tagTokens.Free;
end;
{$ENDIF}
function GetBitmapFromClipboard: TBGRABitmap;
var i: integer;
Stream: TMemoryStream;
data: string;
{$IFDEF DEBUG_CLIPBOARD}
j: integer;
pcf: TPredefinedClipboardFormat;
mime, str: string;
c: char;
prevCok: boolean;
{$ENDIF}
{$IFDEF PDN_CLIPBOARD_FORMAT}
deserial: TDotNetDeserialization;
{$ENDIF}
begin
result := nil;
{$IFDEF DEBUG_CLIPBOARD}
str := 'clipboard.FormatCount = '+inttostr(clipboard.FormatCount)+lineending;
for i := 0 to clipboard.FormatCount-1 do
begin
if str <> '' then str += ', ';
str := str + '#'+inttostr(clipboard.Formats[i])+'=';
mime := ClipboardFormatToMimeType(clipboard.Formats[i]);
if mime = '' then
for pcf := low(TPredefinedClipboardFormat) to high(TPredefinedClipboardFormat) do
if clipboard.Formats[i] = PredefinedClipboardFormat(pcf) then
mime := PredefinedClipboardMimeTypes[pcf];
if mime = '' then
for j := low(moreMimeTypes) to high(moreMimeTypes) do
if clipboard.Formats[j] = RegisterClipboardFormat(moreMimeTypes[j]) then
mime := moreMimeTypes[j];
str += mime;
stream := TMemoryStream.Create;
Clipboard.GetFormat(Clipboard.Formats[i],Stream);
str += '('+inttostr(stream.Size)+' bytes)';
if (mime = 'DataObject') or (mime = 'text/html') or (mime = 'HTML Format') or (mime = 'text/plain') then
begin
if stream.Size > 1024 then
setlength(data,1024) else
setlength(data,stream.size);
stream.Position:= 0;
stream.read(data[1],length(data));
str += '=[';
prevCok := false;
for j := 1 to length(data) do
begin
c := data[j];
if c in[#32..#126] then
begin
str+= c;
prevCok := true
end else
begin
if not (prevCOk and (c = #0)) then
str += ' '+inttohex(ord(c),2)+' ';
prevCok := false;
end;
end;
str += ']'+lineending;
end;
stream.Free;
end;
ShowMessage(str);
{$ENDIF}
{$IFDEF PDN_CLIPBOARD_FORMAT}
for i := 0 to clipboard.FormatCount-1 do
if Clipboard.Formats[i] = pdnClipboardFormat then
begin
Stream := TMemoryStream.Create;
Clipboard.GetFormat(Clipboard.Formats[i],Stream);
stream.Position := 0;
deserial := TDotNetDeserialization.Create;
deserial.LoadFromStream(stream);
Stream.Free;
try
result := GetBitmapFromPaintDotNetMaskedSurface(deserial);
except
end;
deserial.Free;
if result <> nil then exit;
end;
{$ENDIF}
{$IFDEF TIFF_CLIPBOARD_FORMAT}
for i := 0 to clipboard.FormatCount-1 do
if Clipboard.Formats[i] = tiffClipboardFormat then
begin
Stream := TMemoryStream.Create;
Clipboard.GetFormat(Clipboard.Formats[i],Stream);
Stream.Position := 0;
try
result := TBGRABitmap.Create;
result.LoadFromStream(Stream);
if result.Empty then result.AlphaFill(255);
except
on ex:exception do
begin
result := nil;
end;
end;
Stream.Free;
if result <> nil then exit;
end;
{$ENDIF}
{$IFDEF PNG_CLIPBOARD_FORMAT}
for i := 0 to clipboard.FormatCount-1 do
if Clipboard.Formats[i] = pngClipboardFormat then
begin
Stream := TMemoryStream.Create;
Clipboard.GetFormat(Clipboard.Formats[i],Stream);
Stream.Position := 0;
try
result := TBGRABitmap.Create;
result.LoadFromStream(Stream);
if result.Empty then result.AlphaFill(255);
except
on ex:exception do
begin
result := nil;
end;
end;
Stream.Free;
if result <> nil then exit;
end;
{$ENDIF}
for i := 0 to clipboard.FormatCount-1 do
if Clipboard.Formats[i] = htmlClipboardFormat then
begin
Stream := TMemoryStream.Create;
Clipboard.GetFormat(Clipboard.Formats[i],Stream);
data := '';
if stream.Size > 65536 then
setlength(data,65536) else
setlength(data,stream.size);
stream.Position:= 0;
stream.read(data[1],length(data));
Stream.Free;
try
result := GetBitmapFromHtml(data);
except
end;
if result <> nil then exit;
end;
for i := 0 to clipboard.FormatCount-1 do
if (Clipboard.Formats[i] = PredefinedClipboardFormat(pcfBitmap)) then
begin
Stream := TMemoryStream.Create;
Clipboard.GetFormat(Clipboard.Formats[i],Stream);
Stream.Position := 0;
try
result := TBGRABitmap.Create(Stream);
if result.Empty then result.AlphaFill(255);
except
on ex:exception do
begin
result := nil;
end;
end;
Stream.Free;
if result <> nil then exit;
end;
end;
function ClipboardContainsBitmap: boolean;
var temp: TBGRABitmap;
begin
temp := GetBitmapFromClipboard;
if (temp=nil) or (temp.Width = 0) or (temp.Height= 0) or temp.Empty then result := false
else result := true;
temp.Free;
end;
procedure CopyToClipboard(bmp: TBGRABitmap);
var
stream: TMemoryStream;
{$IFDEF BMP_CLIPBOARD_FORMAT}
bmpWriter: TFPWriterBMP;
{$ENDIF}
begin
Clipboard.Clear;
{$IFDEF BMP_CLIPBOARD_FORMAT}
stream := TMemoryStream.Create;
bmpWriter := TFPWriterBMP.Create;
bmpWriter.BitsPerPixel := 32;
bmp.SaveToStream(stream, bmpWriter);
bmpWriter.Free;
Clipboard.AddFormat(PredefinedClipboardFormat(pcfBitmap), stream);
stream.Free;
{$ENDIF}
{$IFDEF TIFF_CLIPBOARD_FORMAT}
stream := TMemoryStream.Create;
bmp.SaveToStreamAs(stream, ifTiff);
Clipboard.AddFormat(tiffClipboardFormat, stream);
stream.Free;
{$ENDIF}
{$IFDEF PNG_CLIPBOARD_FORMAT}
stream := TMemoryStream.Create;
bmp.SaveToStreamAs(stream, ifPng);
Clipboard.AddFormat(pngClipboardFormat, stream);
stream.Free;
{$ENDIF}
end;
initialization
{$IFDEF TIFF_CLIPBOARD_FORMAT}
tiffClipboardFormat := RegisterClipboardFormat({$IFDEF DARWIN}'public.tiff'{$ELSE}'image/tiff'{$ENDIF});
{$ENDIF}
{$IFDEF PNG_CLIPBOARD_FORMAT}
pngClipboardFormat := RegisterClipboardFormat({$IFDEF DARWIN}'public.png'{$ELSE}{$IFDEF WINDOWS}'PNG'{$ELSE}'image/png'{$ENDIF}{$ENDIF});
{$ENDIF}
{$IFDEF HTML_CLIPBOARD_FORMAT}
htmlClipboardFormat := RegisterClipboardFormat({$IFDEF DARWIN}'public.html'{$ELSE}'text/html'{$ENDIF});
{$ENDIF}
{$IFDEF PDN_CLIPBOARD_FORMAT}
pdnClipboardFormat := RegisterClipboardFormat('PaintDotNet.MaskedSurface');
{$ENDIF}
end.