File: c-sharp-ghost-api.md

package info (click to toggle)
ghostscript 10.0.0~dfsg-11%2Bdeb12u7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 98,252 kB
  • sloc: ansic: 891,828; python: 7,649; cpp: 6,493; cs: 6,209; sh: 6,043; java: 4,028; perl: 2,372; tcl: 1,639; makefile: 521; awk: 66; javascript: 43; yacc: 18
file content (603 lines) | stat: -rw-r--r-- 21,617 bytes parent folder | download
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
# GhostAPI

<div class="banner">
    <div class="c-sharp-text"></div>
    <div class="vendor-logo c-sharp-logo"></div>
</div>


## About

`GhostAPI` is the `C#` bridge into the Ghostscript `C` library.

`GhostAPI` contains some essential [structs and enums] as well as a static class for some [constants], finally it contains the main [GSAPI] class which holds the key methods which interface with the `C` library.

## Structs and Enums


### gsapi_revision_t

This `struct` is used to contain information pertinent to the version of Ghostscript.

<div class="tag structDefinition csharp"></div>

```
public struct gsapi_revision_t
{
    public IntPtr product;
    public IntPtr copyright;
    public int revision;
    public int revisiondate;
}
```


### gs_set_param_type


<div class="tag enumDefinition csharp"></div>

```
public enum gs_set_param_type
{
    gs_spt_invalid = -1,
    gs_spt_null =   0, /* void * is NULL */
    gs_spt_bool =   1, /* void * is NULL (false) or non-NULL (true) */
    gs_spt_int = 2, /* void * is a pointer to an int */
    gs_spt_float = 3, /* void * is a float * */
    gs_spt_name = 4, /* void * is a char * */
    gs_spt_string = 5, /* void * is a char * */
    gs_spt_long =   6, /* void * is a long * */
    gs_spt_i64 = 7, /* void * is an int64_t * */
    gs_spt_size_t = 8, /* void * is a size_t * */
    gs_spt_parsed = 9, /* void * is a pointer to a char * to be parsed */
    gs_spt_more_to_come = 1 << 31
};
```


### gsEncoding


<div class="tag enumDefinition csharp"></div>


```
public enum gsEncoding
{
    GS_ARG_ENCODING_LOCAL = 0,
    GS_ARG_ENCODING_UTF8 = 1,
    GS_ARG_ENCODING_UTF16LE = 2
};
```

## Constants

Constants are stored in the static class `gsConstants` for direct referencing.

### gsConstants


<div class="tag classDefinition csharp"></div>

```
static class gsConstants
{
    public const int E_QUIT = -101;
    public const int GS_READ_BUFFER = 32768;
    public const int DISPLAY_UNUSED_LAST = (1 << 7);
    public const int DISPLAY_COLORS_RGB = (1 << 2);
    public const int DISPLAY_DEPTH_8 = (1 << 11);
    public const int DISPLAY_LITTLEENDIAN = (1 << 16);
    public const int DISPLAY_BIGENDIAN = (0 << 16);
}
```

## GSAPI

Methods contained within are explained below.

`gsapi_run_*` and `gsapi_exit` methods return an `int` code which can be interpreted as follows:

| code | status |
|---|---|
|`0`| no error |
|`gsConstants.E_QUIT`| "quit" has been executed. This is not an error. [gsapi_exit] must be called next |
|`<0` | error |

>**NOTE**<br>
> For full detail on these return code please see:
https://www.ghostscript.com/doc/current/API.htm#return_codes
>

> **NOTE**<br>
> All [GSAPI] methods aside from [gsapi_revision] and [gsapi_new_instance] should pass an instance of Ghostscript as their first parameter with `IntPtr instance`
>

### gsapi_revision

This method returns the revision numbers and strings of the Ghostscript interpreter library; you should call it before any other interpreter library functions to make sure that the correct version of the Ghostscript interpreter has been loaded.



<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_revision(ref gsapi_revision_t vers,
                                                         int size);
```


> **NOTE**<br>
> The method should write to a reference variable which conforms to the struct [gsapi_revision_t].
>

### gsapi_new_instance

Creates a new instance of Ghostscript. This instance is passed to most other [GSAPI] methods. Unless Ghostscript has been compiled with the `GS_THREADSAFE` define, only one instance at a time is supported.

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_new_instance(out IntPtr pinstance,
                                                IntPtr caller_handle);
```


> **NOTE**<br>
> The method returns a pointer which represents your instance of Ghostscript.
>

### gsapi_delete_instance

Destroy an instance of Ghostscript. Before you call this, Ghostscript must have finished. If Ghostscript has been initialised, you must call [gsapi_exit] beforehand.

<div class="tag methodDefinition csharp"></div>

```
public static extern void gsapi_delete_instance(IntPtr instance);
```

<div class="tag sampleCode csharp"></div>

```
GSAPI.gsapi_delete_instance(gsInstance);
gsInstance = IntPtr.Zero;
```

### gsapi_set_stdio_with_handle

Set the callback functions for `stdio`, together with the handle to use in the callback functions. The `stdin` callback function should return the number of characters read, 0 for EOF, or -1 for error. The `stdout` and `stderr` callback functions should return the number of characters written.



> **NOTE**<br>
> These callbacks do not affect output device I/O when using "%stdout" as the output file. In that case, device output will still be directed to the process "stdout" file descriptor, not to the `stdio` callback.
>

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_set_stdio_with_handle(IntPtr instance,
                                           gs_stdio_handler stdin,
                                           gs_stdio_handler stdout,
                                           gs_stdio_handler stderr,
                                                     IntPtr caller_handle);
```


### gsapi_set_stdio

Set the callback functions for `stdio`. The handle used in the callbacks will be taken from the value passed to [gsapi_new_instance]. Otherwise the behaviour of this function matches [gsapi_set_stdio_with_handle].

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_set_stdio_with_handle(IntPtr instance,
                                           gs_stdio_handler stdin,
                                           gs_stdio_handler stdout,
                                           gs_stdio_handler stderr);
```


### gsapi_set_poll_with_handle

Set the callback function for polling, together with the handle to pass to the callback function. This function will only be called if the Ghostscript interpreter was compiled with `CHECK_INTERRUPTS` as described in `gpcheck.h`.

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.

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.


<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_set_poll_with_handle(IntPtr instance,
                                             gsPollHandler pollfn,
                                                    IntPtr caller_handle);
```

### gsapi_set_poll

Set the callback function for polling. The handle passed to the callback function will be taken from the handle passed to [gsapi_new_instance]. Otherwise the behaviour of this function matches [gsapi_set_poll_with_handle].

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_set_poll(IntPtr instance,
                                 gsPollHandler pollfn);
```


### gsapi_set_display_callback

This call is deprecated; please use [gsapi_register_callout] to register a [callout handler] for the display device in preference.

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_set_display_callback(IntPtr pinstance,
                                                    IntPtr caller_handle);
```

### gsapi_register_callout

This call registers a [callout handler].

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_register_callout(IntPtr instance,
                                             gsCallOut callout,
                                                IntPtr callout_handle);
```

### gsapi_deregister_callout


This call deregisters a [callout handler] previously registered with [gsapi_register_callout]. All three arguments must match exactly for the [callout handler] to be deregistered.


<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_deregister_callout(IntPtr instance,
                                               gsCallOut callout,
                                                  IntPtr callout_handle);
```

### gsapi_set_arg_encoding

Set the encoding used for the interpretation of all subsequent arguments supplied via the `GhostAPI` interface on this instance. By default we expect args to be in encoding `0` (the 'local' encoding for this OS). On Windows this means "the currently selected codepage". On Linux this typically means `utf8`. This means that omitting to call this function will leave Ghostscript running exactly as it always has. Please note that use of the 'local' encoding is now deprecated and should be avoided in new code. This must be called after [gsapi_new_instance] and before [gsapi_init_with_args].

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_set_arg_encoding(IntPtr instance,
                                                   int encoding);
```

### gsapi_set_default_device_list

Set the string containing the list of default device names, for example "display x11alpha x11 bbox". Allows the calling application to influence which device(s) Ghostscript will try, in order, in its selection of the default device. This must be called after [gsapi_new_instance] and before [gsapi_init_with_args].

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_set_default_device_list(IntPtr instance,
                                                       IntPtr list,
                                                      ref int listlen);
```

### gsapi_get_default_device_list

Returns a pointer to the current default device string. This must be called after [gsapi_new_instance] and before [gsapi_init_with_args].

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_get_default_device_list(IntPtr instance,
                                                   ref IntPtr list,
                                                      ref int listlen);
```

### gsapi_init_with_args

To initialise the interpreter, pass your `instance` of Ghostscript, your argument count, `argc`, and your argument variables, `argv`.

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_init_with_args(IntPtr instance,
                                                 int argc,
                                              IntPtr argv);
```

### gsapi\_run\_\*

If these functions return `<= -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.

There is a 64 KB length limit on any buffer submitted to a `gsapi_run_*` function for processing. If you have more than 65535 bytes of input then you must split it into smaller pieces and submit each in a separate [gsapi_run_string_continue] call.

### gsapi_run_string_begin

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_run_string_begin(IntPtr instance,
                                                   int usererr,
                                               ref int exitcode);
```

### gsapi_run_string_continue

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_run_string_continue(IntPtr instance,
                                                   IntPtr command,
                                                      int count,
                                                      int usererr,
                                                  ref int exitcode);
```

### gsapi_run_string_with_length

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_run_string_with_length(IntPtr instance,
                                                      IntPtr command,
                                                        uint length,
                                                         int usererr,
                                                     ref int exitcode);
```

### gsapi_run_string


<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_run_string(IntPtr instance,
                                          IntPtr command,
                                             int usererr,
                                         ref int exitcode);
```

### gsapi_run_string_end

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_run_string_end(IntPtr instance,
                                                 int usererr,
                                             ref int exitcode);
```

### gsapi_run_file

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_run_file(IntPtr instance,
                                        IntPtr filename,
                                           int usererr,
                                       ref int exitcode);
```

### gsapi_exit

Exit the interpreter. This must be called on shutdown if [gsapi_init_with_args] has been called, and just before [gsapi_delete_instance].

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_exit(IntPtr instance);
```

### gsapi_set_param

Sets a parameter.

Broadly, this is equivalent to setting a parameter using -d, -s or -p on the command line. This call cannot be made during a [gsapi_run_string] operation.

Parameters in this context are not the same as 'arguments' as processed by [gsapi_init_with_args], but often the same thing can be achieved. For example, with [gsapi_init_with_args], we can pass "-r200" to change the resolution. Broadly the same thing can be achieved by using [gsapi_set_param] to set a parsed value of "<</HWResolution [ 200.0 200.0 ]>>".

Internally, when we set a parameter, we perform an `initgraphics` operation. This means that using [gsapi_set_param] other than at the start of a page is likely to give unexpected results.

Attempting to set a parameter that the device does not recognise will be silently ignored, and that parameter will not be found in subsequent [gsapi_get_param] calls.


<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_set_param(IntPtr instance,
                                         IntPtr param,
                                         IntPtr value,
                              gs_set_param_type type);
```


> **NOTE**<br>
> The `type` argument, as a [gs_set_param_type], dictates the kind of object that the `value` argument points to.
>

> **NOTE**<br>
> For more on the C implementation of parameters see: [Ghostscript parameters in C].
>

### gsapi_get_param

Retrieve the current value of a parameter.

If an error occurs, the return value is negative. Otherwise the return value is the number of bytes required for storage of the value. Call once with value `NULL` to get the number of bytes required, then call again with value pointing to at least the required number of bytes where the value will be copied out. Note that the caller is required to know the type of value in order to get it. For all types other than [gs_spt_string], [gs_spt_name], and [gs_spt_parsed] knowing the type means you already know the size required.

This call retrieves parameters/values that have made it to the device. Thus, any values set using [gs_spt_more_to_come] without a following call omitting that flag will not be retrieved. Similarly, attempting to get a parameter before [gsapi_init_with_args] has been called will not list any, even if [gsapi_set_param] has been used.

Attempting to read a parameter that is not set will return `gs_error_undefined` (-21). Note that calling [gsapi_set_param] followed by [gsapi_get_param] may not find the value, if the device did not recognise the key as being one of its configuration keys.

For the C documentation please refer to [Ghostscript get_param].


<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_get_param(IntPtr instance,
                                         IntPtr param,
                                         IntPtr value,
                              gs_set_param_type type);
```

### gsapi_enumerate_params

Enumerate the current parameters. Call repeatedly to list out the current parameters.

The first call should have `iter` = NULL. Subsequent calls should pass the same pointer in so the iterator can be updated. Negative return codes indicate error, 0 success, and 1 indicates that there are no more keys to read. On success, key will be updated to point to a null terminated string with the key name that is guaranteed to be valid until the next call to [gsapi_enumerate_params]. If `type` is non NULL then the pointer `type` will be updated to have the `type` of the parameter.

> **NOTE**<br>
> Only one enumeration can happen at a time. Starting a second enumeration will reset the first.
>

The enumeration only returns parameters/values that have made it to the device. Thus, any values set using the [gs_spt_more_to_come] without a following call omitting that flag will not be retrieved. Similarly, attempting to enumerate parameters before [gsapi_init_with_args] has been called will not list any, even if [gsapi_set_param] has been used.

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_enumerate_params(IntPtr instance,
                                            out IntPtr iter,
                                            out IntPtr key,
                                                IntPtr type);
```

### gsapi_add_control_path

Add a (case sensitive) path to one of the lists of [permitted paths] for file access.

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_add_control_path(IntPtr instance,
                                                   int type,
                                                IntPtr path);
```

### gsapi_remove_control_path

Remove a (case sensitive) path from one of the lists of [permitted paths] for file access.

<div class="tag methodDefinition csharp"></div>

```
public static extern int gsapi_remove_control_path(IntPtr instance,
                                                      int type,
                                                   IntPtr path);
```

### gsapi_purge_control_paths

Clear all the paths from one of the lists of [permitted paths] for file access.
<div class="tag methodDefinition csharp"></div>

```
public static extern void gsapi_purge_control_paths(IntPtr instance,
                                                       int type);
```

### gsapi_activate_path_control

Enable/Disable path control (i.e. whether paths are checked against [permitted paths] before access is granted).

<div class="tag methodDefinition csharp"></div>

```
public static extern void gsapi_activate_path_control(IntPtr instance,
                                                         int enable);
```

### gsapi_is_path_control_active


Query whether path control is activated or not.

<div class="tag methodDefinition csharp"></div>


```
public static extern int gsapi_is_path_control_active(IntPtr instance);
```



## Callback and Callout prototypes

[GSAPI] also defines some prototype pointers which are defined as follows.

### gs_stdio_handler

<div class="tag callbackDefinition csharp"></div>

```
/* Callback proto for stdio */
public delegate int gs_stdio_handler(IntPtr caller_handle,
                                     IntPtr buffer,
                                        int len);
```


### gsPollHandler

<div class="tag callbackDefinition csharp"></div>

```
/* Callback proto for poll function */
public delegate int gsPollHandler(IntPtr caller_handle);
```


### gsCallOut

<div class="tag callbackDefinition csharp"></div>

```
/* Callout proto */
public delegate int gsCallOut(IntPtr callout_handle,
                              IntPtr device_name,
                                 int id,
                                 int size,
                              IntPtr data);

```


[structs and enums]: #structs-and-enums
[constants]: #constants
[GSAPI]: #gsapi

[Ghostscript parameters in C]: https://www.ghostscript.com/doc/current/Use.htm#Parameters
[Ghostscript get_param]: https://www.ghostscript.com/doc/current/API.htm#get_param
[gsapi_revision_t]: #gsapi_revision_t

[gsapi_revision]: #gsapi_revision
[gsapi_exit]: #gsapi_exit
[gsapi_new_instance]: #gsapi_new_instance
[gsapi_set_stdio_with_handle]: #gsapi_set_stdio_with_handle
[gsapi_set_poll_with_handle]: #gsapi_set_poll_with_handle
[gsapi_register_callout]: #gsapi_register_callout
[gsapi_init_with_args]: #gsapi_init_with_args
[gsapi_delete_instance]: #gsapi_delete_instance
[gsapi_exit]: #gsapi_exit
[gsapi_run_string]: #gsapi_run_string
[gsapi_run_string_continue]: #gsapi_run_string_continue
[gs_set_param_type]: #gs_set_param_type
[gs_spt_more_to_come]: #gs_set_param_type
[gs_spt_string]: #gs_set_param_type
[gs_spt_name]: #gs_set_param_type
[gs_spt_parsed]: #gs_set_param_type
[gsapi_set_param]: #gsapi_set_param
[gsapi_get_param]: #gsapi_get_param
[gsapi_enumerate_params]: #gsapi_enumerate_params

[permitted paths]: https://ghostscript.com/doc/current/Use.htm#Safer

[callout handler]: #gscallout