-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSkiSys.GS_Api.pas
More file actions
2626 lines (2445 loc) · 85.1 KB
/
SkiSys.GS_Api.pas
File metadata and controls
2626 lines (2445 loc) · 85.1 KB
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{******************************************************************************}
{ }
{ Ghostscript API Wrapper: An extended Ghostscript API for Delphi }
{ to simplify use of Ghostscript. }
{ }
{ Copyright (c) 2021-2022 (SKI-Systems) }
{ Author: Jan Blumstengel }
{ }
{ https://github.com/SKI-Systems/Ghostscript-API-Wrapper }
{ }
{******************************************************************************}
{ }
{ This program is free software: you can redistribute it and/or modify }
{ it under the terms of the GNU Affero General Public License as }
{ published by the Free Software Foundation, either version 3 of the }
{ License, or (at your option) any later version. }
{ }
{ This program is distributed in the hope that it will be useful, }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the }
{ GNU Affero General Public License for more details. }
{ }
{ You should have received a copy of the GNU Affero General Public License }
{ along with this program. If not, see <https://www.gnu.org/licenses/>. }
{ }
{******************************************************************************}
// Main unit to implement the Wrapper units in delphi classes
unit SkiSys.GS_Api;
{$IFDEF FPC} //Free Pascal
{$MODE DELPHI}
{$H+}
{$ELSE} //Delphi
{$DEFINE DELPHI}
{$ENDIF}
interface
uses
SkiSys.GS_Dll, SkiSys.GS_Types, SkiSys.GS_Errors, SkiSys.GS_ParameterTypes,
SkiSys.GS_ParameterConst, SkiSys.GS_gdevdsp,
{$IFDEF FPC}
Classes, SysUtils, Graphics, Generics.Collections, LclIntf, LclType, GraphType
{$IFDEF MSWINDOWS}
, Windows
{$ENDIF}
{$IFDEF UNIX}
, Unix
{$ENDIF}
;
{$ENDIF}
{$IFDEF DELPHI}
System.Classes, System.SysUtils, WinApi.Windows, System.AnsiStrings,
System.Generics.Collections, Vcl.Graphics;
{$ENDIF}
{$MINENUMSIZE 4}
{$I+}
const
/// <summary>
/// The TGS_Api version
/// </summary>
API_VERSION = 1011;
/// <summary>
/// Minimum required Ghostscript version, which will be checked in
/// <see cref="SkiSys.GS_API|TGS_Revision.CheckRevision"/>
/// </summary>
MIN_GHOSTSCRIPT_REVISION = 9500;
type
TAnsiStringArray = array of AnsiString;
TGSEvent_Std = procedure(const AText: String) of object;
TNotifyFuncEvent = function(Sender: TObject): Integer of object;
/// <summary>
/// Will be raised when a not supported Ghostscript version is used.
/// </summary>
EInvalidGhostscriptVersionException = Exception;
{ EGS_ApiException }
/// <summary>
/// Exceptions raised by the TGS_Api
/// </summary>
EGS_ApiException = class(Exception)
private
const
// <1>ErrorCode , <2>FunctionName , <3>ErrorMessage
MessageFunctionFmt = 'Error(%d) on %s: %s';
MessageErrorCodeFmt = 'Error(%d) %s';
protected
FErrorCode: Integer;
FFunctionName: string;
FInternalMessage: string;
function GetMessage(AValue: string): string;
public
constructor Create(const Msg, AFunctionName: string; error: Integer); reintroduce;
constructor CreateFmt(const Msg, AFunctionName: string; error: Integer;
const Args: array of const); reintroduce;
end;
TGS_Api = class;
TGS_Display = class;
{ TStrSyncObject }
/// <summary>
/// Object to synchronize a TGSEvent_Std
/// </summary>
TStrSyncObject = class
private
FEvent: TGSEvent_Std;
FStr: string;
public
// constructor
constructor Create(AEvent: TGSEvent_Std; AStr: string);
/// <summary>
/// Method to execute the event synchronized
/// </summary>
procedure Execute;
end;
/// <summary>
/// Thread to run InitWithArgs as a thread with the arguments
/// </summary>
TGS_ApiThread = class(TThread)
private
FArgs: TStrings;
{$IFDEF DELPHI}[WEAK]{$ENDIF}FApi: TGS_Api;
protected
procedure Execute; override;
public
/// <summary>
/// Create the thread and start it
/// </summary>
constructor Create(AApi: TGS_Api; Args: TStrings);
// destructor
destructor Destroy; override;
end;
/// <summary>
/// Image record to store the image info
/// </summary>
TGS_ImageData = record
/// <summary>
/// Calculated bytes per image line
/// </summary>
ByteWidth: Integer;
/// <summary>
/// Ghostscript device pointer
/// </summary>
Device: Pointer;
/// <summary>
/// The Ghostscript display format, for further informations have a look at
/// the unit SkiSys.GS_gdevdsp
/// </summary>
Format: Cardinal;
/// <summary>
/// Image height
/// </summary>
Height: Integer;
/// <summary>
/// Pointer to the image data buffer
/// </summary>
ImageData: PByte;
/// <summary>
/// Ghostscript bytes per image line
/// </summary>
Raster: Integer;
/// <summary>
/// Image width
/// </summary>
Width: Integer;
/// <summary>
/// Set the values of TGS_ImageData
/// </summary>
procedure SetDataAndSize(Width, Height, Raster: Integer; Format: Cardinal;
PImage: PByte);
end;
{ TGS_Image }
/// <summary>
/// TGS_Image inherits from TBitmap to show a preview image
/// </summary>
TGS_Image = class({$IFDEF FPC}Graphics.{$ENDIF}TBitmap)
private
{$IFDEF DELPHI}[WEAK]{$ENDIF}FDisplay: TGS_Display;
FBmpInfoHeader: BITMAPINFOHEADER;
FBitsPerPixel: Integer;
FGS_Device: Pointer;
FGS_Format: TGSDisplayFormat;
FGS_ImageDataLoaded: Boolean;
FGS_Raster: Integer; // GS BytesPerScanLine
function GetGS_Format: Cardinal;
protected
/// <summary>
/// Converts the image data if needed
/// </summary>
function ConvertImageDataLine(ASrcLine, ADestLine: PByte): Boolean; virtual;
/// <summary>
/// convert data bytes from the native 555 format
/// </summary>
procedure ConvertNative555(ASrcBytes, ADestBytes: PByte); virtual;
/// <summary>
/// convert data bytes from the native 565 format
/// </summary>
procedure ConvertNative565(ASrcBytes, ADestBytes: PByte); virtual;
/// <summary>
/// calculate the bytes
/// </summary>
function GetBytesPerLine: Integer; virtual;
/// <summary>
/// calculate the bits per pixel for our formats
/// </summary>
function GetBitsPerPixel: Integer; virtual;
/// <summary>
/// get the gray colors from normal colors
/// </summary>
function GetGrayLuminance(AColor: TColor; const ALum: array of Byte): TColor;
/// <summary>
/// get the pointer to the image data line
/// </summary>
function GetScanLine(AIndex: Integer): Pointer;
/// <summary>
/// Get the PixelFormat
/// </summary>
function GetPixelFormatFromBits: TPixelFormat; virtual;
/// <summary>
/// Calls the Move method
/// </summary>
procedure MemCopy(Source, Dest: Pointer; ALength: Integer);
/// <summary>
/// Set the BmpInfoHeader from the format
/// </summary>
procedure SetBmpInfoHeader(AWidth, AHeight: Integer); virtual;
/// <summary>
/// Set the image in the bitmap
/// </summary>
procedure SetImageData(PImage: PByte); virtual;
/// <summary>
/// The BmpInfoHeader
/// </summary>
property BmpInfoHeader: BITMAPINFOHEADER read FBmpInfoHeader write FBmpInfoHeader;
public
constructor Create(ADisplays: TGS_Display; AWidth, AHeight, ARaster: Integer;
AFormat: Cardinal; PImage: PByte); reintroduce; overload;
constructor Create(ADisplays: TGS_Display; AData: TGS_ImageData); reintroduce; overload;
public (*** PROPERTIES ***)
/// <summary>
/// Pointer to the Ghostscript display device
/// </summary>
property GS_Device: Pointer read FGS_Device;
/// <summary>
/// The Ghostscript display format implemented in the unit SkiSys.GS_gdevdsp
/// </summary>
property GS_Format: Cardinal read GetGS_Format;
/// <summary>
/// Is the image buffer loaded into the bitmap
/// </summary>
property GS_ImageDataLoaded: Boolean read FGS_ImageDataLoaded;
/// <summary>
/// The raster of the bitmap (bytes per image line)
/// </summary>
property GS_Raster: Integer read FGS_Raster;
end;
/// <summary>
/// Imagelist for TGS_Image
/// </summary>
TGS_ImageList = class(TObjectList<TGS_Image>)
private
FDisplays: TGS_Display;
FImageData: TGS_ImageData;
public
/// <summary>
/// Create the image from the ImageData and add it to the list
/// </summary>
function AddFromImageData: Integer; virtual;
// constructor
constructor Create(ADisplays: TGS_Display);
/// <summary>
/// Initialize the ImgaeData when the device is open
/// </summary>
procedure InitImageData(ADevice: Pointer); virtual;
/// <summary>
/// Set the size and the buffer pointer for the image buffer. The image
/// buffer isn't filled at this moment.
/// </summary>
procedure SetDataAndSize(AWidth, AHeight, ARaster: Integer; AFormat: Cardinal;
PImage: PByte); virtual;
end;
{$REGION 'TGS_Display Events Types'}
TGS_DisplayEvent = function(ADevice: Pointer): Integer of object;
TGS_DisplayAdjustBandHeightEvent = function(ADevice: Pointer;
ABandHeight: Integer): Integer of object;
TGS_DisplayMemAllocEvent = function(ADevice: Pointer; ASize: SIZE_T): Integer of object;
TGS_DisplayMemFreeEvent = function(ADevice, AMem: Pointer): Integer of object;
TGS_DisplayPageEvent = function(ADevice: Pointer; ACopies, AFlush: Integer): Integer of object;
TGS_DisplayPresizeEvent = function(ADevice: Pointer;
AWidth, AHeight, ARaster: Integer;
AFormat: Cardinal): Integer of object;
TGS_DisplayRectangleRequestEvent = function(ADevice, AMemory: Pointer;
out ARaster, APlaneRaster: Integer;
out X, Y, W, H: Integer): Integer of object;
TGS_DisplaySeparationEvent = function(ADevice: Pointer; AComponent: Integer;
AComponentName: string;
C, M, Y, K: Word): Integer of object;
TGS_DisplaySizeEvent = function(ADevice: Pointer;
AWidth, AHeight, ARaster: Integer;
AFormat: Cardinal; PImage: PByte): Integer of object;
TGS_DisplayUpdateEvent = function(ADevice: Pointer; X, Y, W, H: Integer): Integer of object;
{$ENDREGION}
/// <summary>
/// The display class to create a preview
/// </summary>
TGS_Display = class
private
{$IFDEF DELPHI}[WEAK]{$ENDIF}FApi: TGS_Api;
FCallback: display_callback;
FDebug: Boolean;
FEventAdjustBandHeight: TGS_DisplayAdjustBandHeightEvent;
FEventClose: TGS_DisplayEvent;
FEventMemAlloc: TGS_DisplayMemAllocEvent;
FEventMemFree: TGS_DisplayMemFreeEvent;
FEventOpen: TGS_DisplayEvent;
FEventPage: TGS_DisplayPageEvent;
FEventPreclose: TGS_DisplayEvent;
FEventPresize: TGS_DisplayPresizeEvent;
FEventRectangleRequest: TGS_DisplayRectangleRequestEvent;
FEventSeparation: TGS_DisplaySeparationEvent;
FEventSize: TGS_DisplaySizeEvent;
FEventSync: TGS_DisplayEvent;
FEventUpdate: TGS_DisplayUpdateEvent;
protected
/// <summary>
/// Preview ImageList will be filled when the parameter -sDEVICE=display is set
/// </summary>
FImageList: TGS_ImageList;
/// <summary>
/// write a message to std_out, when Debug is true
/// </summary>
procedure DebugLog(AMessage: string); overload;
/// <summary>
/// write a message in the format to std_out, when Debug is true
/// </summary>
procedure DebugLogFmt(AFormat: string; const Args: array of const); overload;
/// <summary>
/// see description <see cref="SkiSys.GS_Api|TGS_Display.OnAdjustBandHeight" />
/// </summary>
function EventAdjustBandHeight(ADevice: Pointer; ABandHeight: Integer): Integer; virtual;
/// <summary>
/// see description <see cref="SkiSys.GS_Api|TGS_Display.OnClose"/>
/// </summary>
function EventClose(ADevice: Pointer): Integer; virtual;
/// <summary>
/// see description <see cref="SkiSys.GS_Api|TGS_Display.OnMemAlloc"/>
/// </summary>
procedure EventMemAlloc(ADevice: Pointer; ASize: SIZE_T); virtual;
/// <summary>
/// see description <see cref="SkiSys.GS_Api|TGS_Display.OnMemFree"/>
/// </summary>
function EventMemFree(ADevice, AMem: Pointer): Integer; virtual;
/// <summary>
/// see description <see cref="SkiSys.GS_Api|TGS_Display.OnOpen"/>
/// </summary>
function EventOpen(ADevice: Pointer): Integer; virtual;
/// <summary>
/// see description <see cref="SkiSys.GS_Api|TGS_Display.OnPage"/>
/// </summary>
function EventPage(ADevice: Pointer; ACopies, AFlush: Integer): Integer; virtual;
/// <summary>
/// see description <see cref="SkiSys.GS_Api|TGS_Display.OnPreclose"/>
/// </summary>
function EventPreclose(ADevice: Pointer): Integer; virtual;
/// <summary>
/// see description <see cref="SkiSys.GS_Api|TGS_Display.OnPresize"/>
/// </summary>
function EventPresize(ADevice: Pointer;
AWidth, AHeight, ARaster: Integer;
AFormat: Cardinal): Integer; virtual;
/// <summary>
/// see description <see cref="SkiSys.GS_Api|TGS_Display.OnRectangleRequest"/>
/// </summary>
function EventRectangleRequest(ADevice, AMemory: Pointer;
out ARaster, APlaneRaster: Integer;
out X, Y, W, H: Integer): Integer; virtual;
/// <summary>
/// see description <see cref="SkiSys.GS_Api|TGS_Display.OnSeparation"/>
/// </summary>
function EventSeparation(ADevice: Pointer; AComponent: Integer;
AComponentName: PAnsiChar; C, M, Y, K: Word): Integer; virtual;
/// <summary>
/// see description <see cref="SkiSys.GS_Api|TGS_Display.OnSize"/>
/// </summary>
function EventSize(ADevice: Pointer;
AWidth, AHeight, ARaster: Integer;
AFormat: Cardinal; PImage: PByte): Integer; virtual;
/// <summary>
/// see description <see cref="SkiSys.GS_Api|TGS_Display.OnSync"/>
/// </summary>
function EventSync(ADevice: Pointer): Integer; virtual;
/// <summary>
/// see description <see cref="SkiSys.GS_Api|TGS_Display.OnUpdate"/>
/// </summary>
function EventUpdate(ADevice: Pointer; X, Y, W, H: Integer): Integer; virtual;
/// <summary>
/// get the count of pages
/// </summary>
function GetPageCount: Integer; virtual;
/// <summary>
/// initialize the object
/// </summary>
procedure Init(AApi: TGS_Api); virtual;
public (*** PROPERTIES ***)
/// <summary>
/// Set Debug=True to receive debug messages of called events with values
/// </summary>
property Debug: Boolean read FDebug write FDebug;
/// <summary>
/// The page count
/// </summary>
property PageCount: Integer read GetPageCount;
public (*** EVENTS ***)
/// <summary>
/// When running in rectangle request mode the device first renders the
/// page to a display list internally. It can then be played back repeatedly
/// so that different regions (rectangles) of the page can be extracted in
/// sequence. A common use of this is to support banded operation, where
/// the page is divided into multiple non-overlapping bands of a fixed height.
/// The display device itself will pick an appropriate band height for it
/// to use. If this function pointer is left as NULL then this value will
/// be used unchanged. Otherwise, the proposed value will be offered to this
/// function. This function can override the choice of bandheight, by
/// returning the value that it would like to be used in preference.
/// In general, this figure should (as much as possible) only be adjusted
/// downwards. For example, a device targeting an inkjet printer with
/// 200 nozzles in the print head might like to extract bands that are a
/// multiple of 200 lines high. So the function might
/// return max(200, 200*(bandheight/200)). If the function returns 0,
/// then the existing value will be used unchanged.
/// Any size rectangle can be chosen with any size bandheight, so ultimately
/// the value chosen here will not matter much. It may make some small
/// difference in speed in some cases.
/// </summary>
property OnAdjustBandHeight: TGS_DisplayAdjustBandHeightEvent
read FEventAdjustBandHeight write FEventAdjustBandHeight;
/// <summary>
/// Device has been closed.
/// </summary>
/// <remarks>
/// This is the last event from this device.
/// </remarks>
property OnClose: TGS_DisplayEvent read FEventClose write FEventClose;
/// <summary>
/// New device has been opened
/// </summary>
/// <remarks>
/// This is the first event from this device.
/// </remarks>
property OnOpen: TGS_DisplayEvent read FEventOpen write FEventOpen;
/// <summary>
/// Allocate memory for bitmap
/// </summary>
/// <remarks>
/// This is provided in case you need to create memory in a special
/// way, e.g. shared. If the compiler option "USE_GSDisplayMemAlloc" is
/// not defined, the Ghostscript memory device allocates the bitmap.
/// This will only called to allocate the image buffer. The first row will
/// be placed at the address returned by display_memalloc.
///
/// In the event of this callback returning nil, Ghostscript will
/// look for a display_rectangle_request callback. If one is not
/// supplied, then this will be reported as memory exhaustion. If
/// one is supplied, then Ghostscript will switch to working in
/// rectangle request mode.
/// This event will only be called with the compiler option "USE_GSDisplayMemAlloc".
/// </remarks>
property OnMemAlloc: TGS_DisplayMemAllocEvent read FEventMemAlloc write FEventMemAlloc;
/// <summary>
/// Free memory for bitmap
/// If this is NULL, the Ghostscript memory device will free the bitmap buffer
/// </summary>
property OnMemFree: TGS_DisplayMemFreeEvent read FEventMemFree write FEventMemFree;
/// <summary>
/// showpage
/// If you want to pause on showpage, then don't return immediately
/// </summary>
property OnPage: TGS_DisplayPageEvent read FEventPage write FEventPage;
/// <summary>
/// Device is about to be closed.
/// </summary>
/// <remarks>
/// Device will not be closed until this function returns.
/// </remarks>
property OnPreclose: TGS_DisplayEvent read FEventPreclose write FEventPreclose;
/// <summary>
/// Device is about to be resized.
/// Resize will only occur if this function returns 0.
/// </summary>
/// <param name="ARaster">ARaster is byte count of a row.</param>
property OnPresize: TGS_DisplayPresizeEvent read FEventPresize write FEventPresize;
/// <summary>
/// If the display device chooses to use rectangle request mode, this function
/// will be called repeatedly to request a rectangle to render. Ghostscript
/// will render the rectangle, and call this function again. The implementer
/// is expected to handle the rectangle that has just been rendered, and to
/// return the details of another rectangle to render. This will continue
/// until a rectangle with zero height or width is returned, whereupon
/// Ghostscript will continue operation. <param/>
/// On entry, *raster and *plane_raster are set to the values expected by
/// the format in use. All the other pointers point to uninitialised values. <param/>
/// On exit, the values should be updated appropriately. The implementor is
/// expected to store the values returned so that the rendered output given
/// can be correctly interpreted when control returns to this function.
/// </summary>
/// <param name="AMemory">
/// should be updated to point to a block of memory to use for the rendered
/// output. Pixel ( *ox, *oy) is the first pixel represented in that block.
/// </param>
/// <param name="ARaster">
/// is the number of bytes difference between the address of component 0 of
/// Pixel( *ox, *oy) and the address of component 0 of Pixel( *ox, 1+``*oy``).
/// </param>
/// <param name="APlaneRaster">
/// is the number of bytes difference between the address of component 0 of
/// Pixel( *ox, *oy) and the address of component 1 of Pixel( *ox, *oy), if
/// in planar mode, 0 otherwise. *x, *y, *w and *h give the rectangle
/// requested within that memory block.
/// </param>
/// <remarks>
/// Any set of rectangles can be rendered with this method, so this can be
/// used to drive Ghostscript in various ways. Firstly, it is simple to
/// request a set of non-overlapping bands that cover the page, to drive
/// a printer. Alternatively, rectangles can be chosen to fill a given block
/// of memory to implement a window panning around a larger page. Either the
/// whole image could be redrawn each time, or smaller rectangles around the
/// edge of the panned area could be requested. The choice is down to the caller.
/// </remarks>
property OnRectangleRequest: TGS_DisplayRectangleRequestEvent
read FEventRectangleRequest write FEventRectangleRequest;
/// <summary>
/// When using DISPLAY_COLORS_SEPARATION, this function will be called once
/// for every separation component - first Cyan, Magenta, Yellow and
/// Black, then any spot colors used. The supplied c, m, y and k values
/// give the equivalent color for each spot. Each colorant value ranges
/// from 0 (for none) to 65535 (full).
/// In separation color mode you are expected to count the number of calls
/// you get to this function after each display_size to know how many colors
/// you are dealing with.
/// </summary>
property OnSeparation: TGS_DisplaySeparationEvent read FEventSeparation
write FEventSeparation;
/// <summary>
/// Device has been resized.
/// New pointer to raster returned in pimage
/// </summary>
property OnSize: TGS_DisplaySizeEvent read FEventSize write FEventSize;
/// <summary>
/// This function may be called periodically during display to flush the
/// page to the display.
/// </summary>
property OnSync: TGS_DisplayEvent read FEventSync write FEventSync;
/// <summary>
/// This function may get called repeatedly during rendering to indicate
/// that an area of the output has been updated. Certain types of rendering
/// will not see this function called back at all
/// (in particular files using transparency).
/// </summary>
property OnUpdate: TGS_DisplayUpdateEvent read FEventUpdate write FEventUpdate;
public (*** METHODS ***)
// constructur
constructor Create(AApi: TGS_Api);
// destructor
destructor Destroy; override;
/// <summary>
/// Clear the images
/// </summary>
procedure Clear;
/// <summary>
/// Get a preview page as a bitmap
/// </summary>
/// <param name="AIdx">The index of the page</param>
/// <returns>
/// Will return the image of the page.
/// Otherwise will return nil, when no image exists at the index.
/// </returns>
function GetPage(AIdx: Integer): TGS_Image;
end;
/// <summary>
/// Class to read the revision information from the dll
/// </summary>
TGS_Revision = class
private
{$IFDEF DELPHI}[WEAK]{$ENDIF}FApi: TGS_API;
FCopyright: string;
FProduct: string;
FRevision: Integer;
FRevisionStr: string;
FRevisionDate: Integer;
protected (*** PROTECTED METHODS ***)
/// <summary>
/// Fills the RevisionStr after GetRevision is called
/// </summary>
function GetRevisonStr: string; virtual;
public (*** PUBLIC PROPERTIES ***)
/// <summary>
/// The product string of Ghostscript
/// </summary>
property Product: string read FProduct;
/// <summary>
/// The copyright of Ghostscript
/// </summary>
property Copyright: string read FCopyright;
/// <summary>
/// Revison as an integer
/// </summary>
property Revision: Integer read FRevision;
/// <summary>
/// Revision as a string
/// </summary>
property RevisionStr: string read FRevisionStr;
/// <summary>
/// Revison date as an integer in the format yyyymmdd
/// </summary>
property RevisionDate: Integer read FRevisionDate;
public (*** PUBLIC METHODS ***)
/// <summary>
/// create the object and read the revision data from the dll
/// </summary>
constructor Create(AApi: TGS_API); overload;
/// <summary>
/// Check the revision and return a warning on StdOut, when a higher version
/// is used. When the minimum supported version of Ghostscript is used, then
/// raise an EInvalidGhostscriptVersion.
/// You can deactivate the check with the compiler switch DONT_CHECK_GS_REVISION
/// </summary>
procedure CheckRevision; virtual;
/// <summary>
/// Read the revision data from the dll and fill the fields
/// </summary>
procedure GetRevision(AApi: TGS_API); virtual;
end;
{ TGS_Api }
/// <summary>
/// Base API to use the Ghostscript dll functions
/// </summary>
TGS_Api = class(TObject)
private
FArgumentEncoding: GS_ARG_ENCODING;
FDebug: Boolean;
FDebugLastCmdArgs: string;
FDebugParams: TGSDebugParams;
FDebugShowCmdArgs: Boolean;
FDefaultDeviceList: TStringList;
FDllPath: string;
FEventAfterExecute: TNotifyEvent;
FEventAfterInitWithArgs: TNotifyEvent;
FEventPoll: TNotifyFuncEvent;
FEventStdError: TGSEvent_Std;
FEventStdIn: TGSEvent_Std;
FEventStdOut: TGSEvent_Std;
FExit: Boolean; // was gsapi_exit allready called
FInitWithArgs: Boolean;
FInstance: Pointer; // ghostscript instance pointer
FLastError: string;
FLastErrors: TStringList; // list of errors occured during the process
FLastErrorCode: Integer;
FLogStdIn: TStringList; // log StdIn
FLogStdOut: TStringList; // log StdOut
FNoExit: Boolean;
FRevision: TGS_Revision;
FThread: TGS_ApiThread;
FThreadUsed: Boolean;
function GetDefaultDeviceList: TStrings;
function GetLastErrors: string;
function GetLogStdIn: TStrings;
function GetLogStdOut: TStrings;
function GetThreadRunning: Boolean;
/// <summary>
/// Register all callouts to communicate with the Ghostscript library
/// </summary>
procedure SetCallouts;
procedure SetDefaultDeviceList(ADeviceList: TStrings);
/// <summary>
/// Set FLastErrorCode and the internal Log. (Will not call OnStdError)
/// </summary>
procedure SetLastErrorInternal(AText: string; AErrorCode: Integer = -1);
protected
/// <summary>
/// Call event <see cref="SkiSys.GS_Api|TGS_Api.OnAfterExecute"/>
/// </summary>
procedure AfterExecute; virtual;
/// <summary>
/// Call event <see cref="SkiSys.GS_Api|TGS_Api.OnAfterInitWithArgs"/>
/// </summary>
procedure AfterInitWithArgs; virtual;
/// <summary>
/// Check if run_string* operations can be executed
/// </summary>
function CheckRunString: Boolean; virtual;
/// <summary>
/// Check if a gsapi_* method returns gs_error_Quit, if yes gsapi_exit
/// will be called
/// </summary>
function CheckResult(AResult: Integer): Integer; virtual;
/// <summary>
/// This function make sure that gsapi_exit is called at the right time
/// to avoid errors.
/// </summary>
/// <remarks>
/// The gsapi_run_* functions are like gs_main_run_* except that the
/// error_object is omitted. If these functions return (smaller)= -100, either quit
/// or a fatal error has occured. You must call gsapi_exit() next.
/// The only exception is gsapi_run_string_continue() which will
/// return gs_error_NeedInput if all is well. See below for return codes.
/// </remarks>
function CheckRunResult(AResult: Integer): Integer; virtual;
/// <summary>
/// Clear the internal log vars
/// </summary>
procedure ClearInternalLog; virtual;
/// <summary>
/// Write a log when the API is in Debug Mode
/// </summary>
procedure DebugLog(AText: string); virtual;
/// <summary>
/// Will fire the given event and the string synchronized, when the api is
/// execute in a thread
/// </summary>
procedure FireEvent(AEvent: TGSEvent_Std; AStr: string); virtual;
/// <summary>
/// Free the gs_instance
/// </summary>
procedure FreeGSInstance; virtual;
/// <summary>
/// Convert a TStrings object to a TAnsiStringArray
/// </summary>
function GetAnsiStrArray(AStrings: TStrings): TAnsiStringArray; overload; virtual;
/// <summary>
/// Convert a TAnsiStringArray to a PArgv Pointer
/// </summary>
function GetPAnsiCharArray(AAnsiStrings: TAnsiStringArray): PArgv; virtual;
/// <summary>
/// Is called when the object will be created and initialize the API and the DLL
/// </summary>
procedure Init(ADllPath: string); virtual;
/// <summary>
/// Get a new Instance Pointer for API calls. This method has to be called
/// everytime before InitWithArgs.(To avoid an fatal error from gsapi at 2nd call)
/// </summary>
procedure InitGSInstance; virtual;
/// <summary>
/// Initialize the log vars to store the Ghostscript output
/// </summary>
procedure InitInternalLog; virtual;
/// <summary>
/// Main method of Ghostscript lirary to execute commands using pointers
/// </summary>
function InitWithArgs(AArgs: PArgv): Boolean; overload; virtual;
/// <summary>
/// The callback function for polling. See full description at the event
/// <see cref="SkiSys.GS_Api|TGS_Api.OnPoll"/>
/// </summary>
function Poll: Integer; virtual;
/// <summary>
/// check the param for whitespaces and set quotes if needed
/// </summary>
function QuoteCmdParameter(const AParam: string): string;
/// <summary>
/// Set the default values of the class
/// </summary>
procedure SetDefaultValues; virtual;
/// <summary>
/// Set the last error from the API and the Ghostscript library
/// </summary>
procedure SetLastError(AText: string; AErrorCode: Integer = -1); overload; virtual;
/// <summary>
/// Set the last error from the API and the Ghostscript library
/// </summary>
procedure SetLastError(AText: string; AErrorCode: gs_error_type); overload; virtual;
/// <summary>
/// Set the last error code from the API and the Ghostscript library
/// </summary>
procedure SetLastErrorCode(ACode: gs_error_type); virtual;
/// <summary>
/// Set parameters for the InitWithArgs procedure
/// </summary>
procedure SetParams(AList: TStringList); virtual;
/// <summary>
/// Set the StdError and call the OnStdError Event
/// </summary>
procedure StdError(AText: string); virtual;
/// <summary>
/// Set the StdIn and call the OnStdIn Event
/// </summary>
procedure StdIn(AText: string); virtual;
/// <summary>
/// Set the StdOut, filter some informations and call the OnStdOut Event
/// </summary>
procedure StdOut(AText: string); virtual;
/// <summary>
/// Calls StdOut method with a linebreak at the end
/// </summary>
procedure StdOutLine(AText: string); virtual;
/// <summary>
/// Will be called after InitWithArgs and InitWithArgsStart was executed
/// </summary>
procedure ThreadFinished(Sender: TObject); virtual;
public (*** PUBLIC PROPERTIES and VARS ***)
/// <summary>
/// Will be used when the device is set to "display" to create a preview,
/// but some options are not availible with this device.
/// </summary>
GSDisplay: TGS_Display;
/// <summary>
/// Set the argument encoding for Ghostscript
/// </summary>
property ArgumentEncoding: GS_ARG_ENCODING read FArgumentEncoding
write FArgumentEncoding;
/// <summary>
/// Set Debug to True to get extended informations about which Params will be
/// set for convert operations and other details
/// </summary>
property Debug: Boolean read FDebug write FDebug;
/// <summary>
/// Get the last command line arguments
/// </summary>
property DebugLastCmdArgs: string read FDebugLastCmdArgs;
/// <summary>
/// Ghostscript Debug Parameters
/// </summary>
property DebugParams: TGSDebugParams read FDebugParams write FDebugParams;
/// <summary>
/// Show the command line args in StdOut
/// </summary>
property DebugShowCmdArgs: Boolean read FDebugShowCmdArgs
write FDebugShowCmdArgs;
/// <summary>
/// Get's and Sets a DefaultDevice list for Ghostscript
/// </summary>
property DefaultDeviceList: TStrings read GetDefaultDeviceList
write SetDefaultDeviceList;
/// <summary>
/// Returns True if Exit was called.
/// </summary>
property GSExit: Boolean read FExit;
/// <summary>
/// returns LastError from the API or Ghostscript
/// </summary>
property LastError: string read FLastError;
/// <summary>
/// The LastErrors, we have to read the Error from StdError and we can get
/// more as one. So we put them all in a list.
/// </summary>
property LastErrors: string read GetLastErrors;
/// <summary>
/// LastErrorCode from Ghostscript
/// </summary>
property LastErrorCode: Integer read FLastErrorCode;
/// <summary>
/// Is currently a thread running
/// </summary>
property ThreadRunning: Boolean read GetThreadRunning;
/// <summary>
/// Can be used to turn off the automatic Exit(gsapi_exit) call after
/// InitWithArgs, but you have to call Exit by your self. A saver method
/// is to use the event <see cref="SkiSys.GS_Api|TGS_Api.OnAfterInitWithArgs"/>.
/// </summary>
property NoExit: Boolean read FNoExit write FNoExit;
/// <summary>
/// The event will be called after an operation is finished. (after Exit)
/// </summary>
property OnAfterExecute: TNotifyEvent read FEventAfterExecute
write FEventAfterExecute;
/// <summary>
/// The event will be called after InitWithArgs and before Exit.
/// You can use it to perform Run* operations.
/// </summary>
property OnAfterInitWithArgs: TNotifyEvent read FEventAfterInitWithArgs
write FEventAfterInitWithArgs;
/// <summary>
/// The callback function for polling.
/// The polling function should return zero if all is well, and return
/// negative if it wants Ghostscript to abort. This is often used for
/// checking for a user cancel. This can also be used for handling window
/// events or cooperative multitasking.
/// </summary>
/// <remarks>
/// The polling function is called very frequently during interpretation
/// and rendering so it must be fast. If the function is slow, then using a
/// counter to return 0 immediately some number of times can be used to
/// reduce the performance impact.
/// </remarks>
property OnPoll: TNotifyFuncEvent read FEventPoll write FEventPoll;
/// <summary>
/// StdError from the Ghostscript library
/// </summary>
property OnStdError: TGSEvent_Std read FEventStdError write FEventStdError;
/// <summary>
/// StdIn from the Ghostscript library
/// </summary>
property OnStdIn: TGSEvent_Std read FEventStdIn write FEventStdIn;
/// <summary>
/// StdOut from the Ghostscript library. This event will be also used for
/// debug informations.
/// </summary>
property OnStdOut: TGSEvent_Std read FEventStdOut write FEventStdOut;
/// <summary>
/// The revision information of the Ghostscript DLL
/// </summary>
property Revision: TGS_Revision read FRevision;
/// <summary>
/// The API stores the full informations of the StdOut here
/// </summary>
property StdInLog: TStrings read GetLogStdIn;
/// <summary>
/// The API stores the full informations of the StdOut here
/// </summary>
property StdOutLog: TStrings read GetLogStdOut;
public (*** PUBLIC METHODS ***)
/// <summary>
/// default constructor
/// </summary>
constructor Create; overload;
/// <summary>
/// constructor with the path to the dll
/// </summary>
constructor Create(ADllPath: string); overload;
/// <summary>
/// destructor
/// </summary>
destructor Destroy; override;
/// <summary>
/// call <see cref="SkiSys.GS_Dll|gsapi_exit" />
/// </summary>
procedure Exit; virtual;
/// <summary>
/// Main method to execute Ghostscript commands
/// </summary>
function InitWithArgs(AStrings: TStrings): Boolean; overload; virtual;
/// <summary>
/// Main method to execute Ghostscript commands in a thread. This API will
/// only execute one thread at a time. After the thread is finished the
/// OnAfterExecute event is called.
/// </summary>
procedure InitWithArgsStart(AStrings: TStrings);
/// <summary>
/// Checks if AResult = AError
/// </summary>
function IsError(AResult: Integer; AError: gs_error_type): Boolean;
/// <summary>
/// Executes gsapi_run_file without any file checks
/// </summary>
/// <remarks>
/// All Run* operations should be called after InitWidthArgs and before Exit.
/// For this operations you have to set NoExit to true and to call Exit by
/// your self or use the event <see cref="SkiSys.GS_Api|TGS_Api.OnAfterInitWithArgs"/>
/// </remarks>
function RunFile(AFile: string; AUserErrors: Integer; out AExitCode: Integer): Integer;
/// <summary>
/// Executes gsapi_run_string
/// </summary>
/// <remarks>
/// All Run* operations should be called after InitWidthArgs and before Exit.
/// For this operations you have to set NoExit to true and to call Exit by
/// your self or use the event <see cref="SkiSys.GS_Api|TGS_Api.OnAfterInitWithArgs"/>
/// </remarks>
function RunString(AStr: string; AUserErrors: Integer; out AExitCode: Integer): Integer;
/// <summary>
/// Executes gsapi_run_begin
/// This method have to be called before RunStringContinue. Make sure
/// to call RunStringEnd at the end, otherwise Ghostscript is waiting for
/// more input.
/// </summary>
/// <remarks>
/// All Run* operations should be called after InitWidthArgs and before Exit.
/// For this operations you have to set NoExit to true and to call Exit by
/// your self or use the event <see cref="SkiSys.GS_Api|TGS_Api.OnAfterInitWithArgs"/>
/// </remarks>
function RunStringBegin(AUserErrors: Integer; out AExitCode: Integer): Integer;
/// <summary>
/// Executes gsapi_run_string_continue
/// This method have to be called after RunStringBegin
/// </summary>
/// <remarks>
/// All Run* operations should be called after InitWidthArgs and before Exit.
/// For this operations you have to set NoExit to true and to call Exit by
/// your self or use the event <see cref="SkiSys.GS_Api|TGS_Api.OnAfterInitWithArgs"/>
/// </remarks>
function RunStringContinue(AStr: string; ALength: Cardinal;