summaryrefslogtreecommitdiff
path: root/doc/src/LanguageBindingsJava.rst
blob: a17d52d2607f1424aa29684f90c374900566545f (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
.. title:: Java

.. meta::
   :description: The Ghostscript documentation
   :keywords: Ghostscript, documentation, ghostpdl



Introduction
==============

In the ``GhostPDL`` repository sample Java projects can be found in ``/demos/java``.

Within this location the following folders are of relevance:

- :ref:`jni<jni: Building the Java Native Interface>` ``jni``
- :ref:`gsjava<java gsjava>` ``gsjava``
- :ref:`gstest<java gstest>` ``gstest``
- :ref:`gsviewer<java gsviewer>` ``gsviewer``



Platform & setup
=======================


Ghostscript should be built as a shared library for your platform.

See :ref:`Building Ghostscript<Building Ghostscript>`.



jni: Building the Java Native Interface
--------------------------------------------------

Before building the JNI ensure that Ghostscript has already been built for your platform and that you have JDK installed.

The JNI is for use in the Java interface, this object must be placed somewhere on your Java ``PATH``. On Windows, the DLL can be placed in the working directory, next to ``gsjava.jar``.



.. list-table::
   :header-rows: 1

   * - Platform
     - JNI file
   * - Windows
     - ``gs_jni.dll``
   * - MacOS
     - ``gs_jni.dylib``
   * - Linux / OpenBSD
     - ``gs_jni.so``


Preparing your include folder
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The build scripts require the header ``jni.h``, which defines all JNI functions, and ``jni_md.h``, which defines all system-specific integer types. The build scripts expect an include folder relative to their location which contain these header files from your system.

These headers are typically found in the following directories:


.. list-table::
   :header-rows: 1

   * - Platform
     - jni.h
     - jni_md.h
   * - Windows
     - C:\Program Files\Java\<JDK Install>\include\jni.h
     - C:\Program Files\Java\<JDK Install>\include\win32\jni_md.h
   * - MacOS
     - /Library/Java/JavaVirtualMachines/<JDK Install>/Contents/Home/include/jni.h
     - /Library/Java/JavaVirtualMachines/<JDK Install>/Contents/Home/include/darwin/jni_md.h
   * - Linux
     - /lib/jvm/<JDK Install>/include/jni.h
     - /lib/jvm/<JDK Install>/include/linux/jni_md.h



Once your ``include`` folder has been located folder you can copy it and place it in your ``ghostpdl/demos/java/jni/gs_jni`` folder.

Your build scripts should now be ready to run as they will be able to find the required JNI header files in their own relative include folder.

Building on Windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The ``jni`` folder contains a Visual Studio Solution file ``/jni/gs_jni/gs_jni.sln`` which you should use to build the required JNI ``gs_jni.dll`` library file.

With the project open in Visual Studio, select the required architecture from the drop down - then right click on 'gs_jni' in the solution explorer and choose "Build".


Building on MacOS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

On your command line, navigate to ``ghostpdl/demos/java/jni/gs_jni`` and ensure that the build script is executable and then run it, with:

.. code-block:: bash

   chmod +x build_darwin.sh
   ./build_darwin.sh


Building on Linux
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

On your command line, navigate to ``ghostpdl/demos/java/jni/gs_jni`` and ensure that the build script is executable and then run it, with:

.. code-block:: bash

   chmod +x build_linux.sh
   ./build_linux.sh



gsjava: Building the JAR
---------------------------------

``gsjava.jar`` is the Java library which contains classes and interfaces which enable API calls required to use Ghostscript.

Assuming that the JAR for your project has been built and properly linked with your own project then the Ghostscript API should be available by importing the required classes within your project's ``.java`` files.



Building with the command line
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Navigate to ``ghostpdl/demos/java/gsjava`` and use the following:


.. list-table::
   :header-rows: 1

   * - Platform
     - Run file
   * - Windows
     - build_win32.bat
   * - MacOS
     - build_darwin.sh
   * - Linux
     - build_linux.sh

.. note::

   ``gsjava`` has a dependency on ``jni``, please ensure that :ref:`gs_jni<jni: Building the Java Native Interface>` is able to be built beforehand.



Building with Eclipse
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Alternatively you can use Eclipse_ to build the JAR file.

Using Eclipse_ import the source folder ``gsjava`` as a project and select ``Export > Java > JAR File`` as shown in the screenshot example below:


.. note we embedd the image with raw HTML because Sphinx is incapable of doing percentage style widths ... :(

.. raw:: html

   <img src="_static/export-jar.png" width=70%/>




Linking the JAR
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The built JAR should be properly linked within your project Java Build Path as follows:


.. raw:: html

   <img src="_static/linking-jar.png" width=80%/>



Demo projects
==================

.. _java gstest:

gstest
----------------------------------------

Use this to quickly test Ghostscript methods.

This project can be opened in Eclipse and used to test the Ghostscript API. The sample here simply sets up an instance of Ghostscript and then sets and gets some parameters accordingly.



.. _java gsviewer:

gsviewer
------------------------------------

A handy file viewer.

This project can be used to test the Ghostscript API alongside a UI which handles opening PostScript and PDF files. The sample application here allows for file browsing and Ghostscript file viewing.

Below is a screenshot of the sample application with a PDF open:


.. raw:: html

   <img src="_static/gsviewer.png" width=100%/>



To run the project navigate to the ``demos/java/gsviewer`` location and ensure that the required libraries are in the directory:


.. list-table::
   :header-rows: 1
   :widths: 15 70 15

   * - Platform
     - Ghostscript library file
     - JNI library file
   * - Windows
     - ``gpdldll64.dll``
     - ``gs_jni.dll``
   * - MacOS
     - ``libgpdl.dylib``
     - ``gs_jni.dylib``
   * - Linux / OpenBSD
     - ``libgpdl.so`` (this may have been built as ``libgs.so``,

       so it should be copied into this directory and

       renamed to ``libgpdl.so``)
     - ``gs_jni.so``


Building on Windows
~~~~~~~~~~~~~~~~~~~~~~~

Run the ``build_win32.bat`` script.

Running on Windows
~~~~~~~~~~~~~~~~~~~~~~~

To run, open ``gsviewer.jar`` either through File Explorer or in the command line through the following command:


.. code-block:: bash

   java -jar gsviewer.jar


Building on MacOS
~~~~~~~~~~~~~~~~~~~~~~~

On your command line, navigate to ``ghostpdl/demos/java/gsviewer`` and ensure that the build script is executable and then run it, with:

.. code-block:: bash

   chmod +x build_darwin.sh
   ./build_darwin.sh

This will automatically ``build gs_jni.dylib`` (in the ``ghostpdl/demos/java/jni/gs_jni/`` location) and ``gsjava.jar`` ``gsviewer.jar`` in the ``gsviewer`` directory.


Running on MacOS
~~~~~~~~~~~~~~~~~~~~~~~

Ensure that the Ghostscript library exists in the ``gsviewer`` directory. (Copy and move the built library from ``ghostpdl/sobin`` as required).

Ensure that the run script is executable and then run it, with:

.. code-block:: bash

   chmod +x start_darwin.sh
   ./start_darwin.sh


Building on Linux
~~~~~~~~~~~~~~~~~~~~~~~

On your command line, navigate to ``ghostpdl/demos/java/gsviewer`` and ensure that the build script is executable and then run it, with:

.. code-block:: bash

   chmod +x build_linux.sh
   ./build_linux.sh

This will automatically build ``gs_jni.so`` (in the ``ghostpdl/demos/java/jni/gs_jni/`` location) and ``gsjava.jar`` ``gsviewer.jar`` in the ``gsviewer`` directory.


.. note::

   On Linux, when using OpenJDK, the property "assistive_technologies" may need to be modified for the Java code to build. It can be modified by editing the "accessibility.properties" file. This is located at:

   ``/etc/java-8-openjdk/accessibility.properties``


Running on Linux
~~~~~~~~~~~~~~~~~~~~~~~

Ensure that the Ghostscript library exists in the ``gsviewer`` directory. (Copy and move the built library from ``ghostpdl/sobin`` as required).

Ensure that the run script is executable and then run it, with:

.. code-block:: bash

   chmod +x start_linux.sh
   ./start_linux.sh



.. _java gsjava:

Using the Java library
==================================


gsjava
----------

There are two main classes within the ``gsjava.jar`` library to consider:


GSAPI & GSInstance
---------------------

:title:`GSAPI` is the main Ghostscript API class which bridges into the :ref:`Ghostscript C library<API.htm exported functions>`.

:title:`GSInstance` is a wrapper class for :title:`GSAPI` which encapsulates an instance of Ghostscript and allows for simpler API calls.


**Sample code:**


.. code-block:: java

   // to use GSAPI
   import static com.artifex.gsjava.GSAPI.*;

   // to use GSInstance
   import com.artifex.gsjava.GSInstance;


GSAPI
--------


.. _java gsapi_revision:

``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.


.. code-block:: java

   public static native int gsapi_revision(GSAPI.Revision revision,
                                                      int len);

.. note::

   The method should write to a reference variable which conforms to the class ``GSAPI.Revision``.

``GSAPI.Revision``
""""""""""""""""""""""

This class is used to store information about Ghostscript and provides handy getters for the product and the copyright information.



.. code-block:: java

   public static class Revision {
       public volatile byte[] product;
       public volatile byte[] copyright;
       public volatile long revision;
       public volatile long revisionDate;

       public Revision() {
           this.product = null;
           this.copyright = null;
           this.revision = 0L;
           this.revisionDate = 0L;
       }

       /**
        * Returns the product information as a String.
        *
        * @return The product information.
        */
       public String getProduct() {
           return new String(product);
       }

       /**
        * Returns the copyright information as a String.
        *
        * @return The copyright information.
        */
       public String getCopyright() {
           return new String(copyright);
       }
   }


.. _java gsapi_new_instance:

``gsapi_new_instance``
~~~~~~~~~~~~~~~~~~~~~~~

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

.. code-block:: java

   public static native int gsapi_new_instance(Reference<Long> instance,
                                                          long callerHandle);

.. note::

   The method returns a reference which represents your instance of Ghostscript.


.. _java gsapi_delete_instance:


``gsapi_delete_instance``
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Destroy an instance of Ghostscript. Before you call this, Ghostscript must have finished. If Ghostscript has been initialised, you should call :ref:`gsapi_exit<java gsapi_exit>` beforehand.


.. code-block:: java

   public static native void gsapi_delete_instance(long instance);



.. _java gsapi_set_stdio_with_handle:

``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.


.. code-block:: java

   public static native int gsapi_set_stdio_with_handle(long instance,
                                              IStdInFunction stdin,
                                             IStdOutFunction stdout,
                                             IStdErrFunction stderr,
                                                        long callerHandle);


.. _java gsapi_set_stdio:

``gsapi_set_stdio``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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


.. code-block:: java

   public static native int gsapi_set_stdio(long instance,
                                  IStdInFunction stdin,
                                 IStdOutFunction stdout,
                                 IStdErrFunction stderr);




.. _java gsapi_set_poll_with_handle:

``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.

.. code-block:: java

   public static native int gsapi_set_poll_with_handle(long instance,
                                              IPollFunction pollfun,
                                                       long callerHandle);



.. _java gsapi_set_poll:

``gsapi_set_poll``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Set the callback function for polling. The handle passed to the callback function will be taken from the handle passed to :ref:`gsapi_new_instance<java gsapi_new_instance>`. Otherwise the behaviour of this function matches :ref:`gsapi_set_poll_with_handle<java gsapi_set_poll_with_handle>`.

.. code-block:: java

   public static native int gsapi_set_poll(long instance,
                                  IPollFunction pollfun);


.. _java gsapi_set_display_callback:

``gsapi_set_display_callback``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This call is deprecated; please use :ref:`gsapi_register_callout<java gsapi_register_callout>` to register a callout handler for the display device in preference.


.. code-block:: java

   public static native int gsapi_set_display_callback(long instance,
                                            DisplayCallback displayCallback);


.. _java gsapi_register_callout:

``gsapi_register_callout``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This call registers a callout handler.

.. code-block:: java

   public static native int gsapi_register_callout(long instance,
                                       ICalloutFunction callout,
                                                   long calloutHandle);


.. _java gsapi_deregister_callout:

``gsapi_deregister_callout``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This call deregisters a callout handler previously registered with :ref:`gsapi_register_callout<java gsapi_register_callout>`. All three arguments must match exactly for the callout handler to be deregistered.

.. code-block:: java

   public static native void gsapi_deregister_callout(long instance,
                                          ICalloutFunction callout,
                                                      long calloutHandle);


.. _java gsapi_set_arg_encoding:

``gsapi_set_arg_encoding``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Set the encoding used for the interpretation of all subsequent arguments supplied via the GSAPI_ 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". 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 :ref:`gsapi_new_instance<java gsapi_new_instance>` and before :ref:`gsapi_init_with_args<java gsapi_init_with_args>`.


.. code-block:: java

   public static native int gsapi_set_arg_encoding(long instance,
                                                    int encoding);


.. _java gsapi_set_default_device_list:

``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 :ref:`gsapi_new_instance<java gsapi_new_instance>` and before :ref:`gsapi_init_with_args<java gsapi_init_with_args>`.


.. code-block:: java

   public static native int gsapi_set_default_device_list(long instance,
                                                        byte[] list,
                                                           int listlen);


.. _java gsapi_get_default_device_list:

``gsapi_get_default_device_list``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Returns a pointer to the current default device string. This must be called after :ref:`gsapi_new_instance<java gsapi_new_instance>` and before :ref:`gsapi_init_with_args<java gsapi_init_with_args>`.


.. code-block:: java

   public static native int gsapi_get_default_device_list(long instance,
                                             Reference<byte[]> list,
                                            Reference<Integer> listlen);


.. _java gsapi_init_with_args:


``gsapi_init_with_args``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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


.. code-block:: java

   public static native int gsapi_init_with_args(long instance,
                                                  int argc,
                                             byte[][] argv);

.. note::

   There are also simpler utility methods which eliminates the need to send through your argument count and allows for simpler ``String`` passing for your argument array.

   *Utility methods*:

   .. code-block:: java

      public static int gsapi_init_with_args(long instance,
                                         String[] argv);

      public static int gsapi_init_with_args(long instance,
                                     List<String> argv);




.. _java gsapi_run_asterisk:

``gsapi_run_*``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If these functions return ``<= -100``, either quit or a fatal error has occured. You must call :ref:`java gsapi_exit<gsapi_exit>` next. The only exception is :ref:`gsapi_run_string_continue<java 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 :ref:`gsapi_run_string_continue<java gsapi_run_string_continue>` call.


.. _java gsapi_run_string_begin:


``gsapi_run_string_begin``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


.. code-block:: java

   public static native int gsapi_run_string_begin(long instance,
                                                    int userErrors,
                                     Reference<Integer> pExitCode);

.. _java gsapi_run_string_continue:

``gsapi_run_string_continue``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: java

   public static native int gsapi_run_string_continue(long instance,
                                                    byte[] str,
                                                        int length,
                                                        int userErrors,
                                        Reference<Integer> pExitCode);


.. note::

   There is a simpler utility method which allows for simpler ``String`` passing for the ``str`` argument.

   *Utility method*:

   .. code-block:: java

      public static int gsapi_run_string_continue(long instance,
                                                String str,
                                                   int length,
                                                   int userErrors,
                                    Reference<Integer> pExitCode);


.. _java gsapi_run_string_with_length:

``gsapi_run_string_with_length``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: java

   public static native int gsapi_run_string_with_length(long instance,
                                                       byte[] str,
                                                          int length,
                                                          int userErrors,
                                           Reference<Integer> pExitCode);

.. note::

   There is a simpler utility method which allows for simpler ``String`` passing for the ``str`` argument.

   *Utility method*:

   .. code-block:: java

      public static int gsapi_run_string_with_length(long instance,
                                                    String str,
                                                       int length,
                                                       int userErrors,
                                       Reference<Integer> pExitCode);


.. _java gsapi_run_string:


``gsapi_run_string``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: java

   public static native int gsapi_run_string(long instance,
                                           byte[] str,
                                              int userErrors,
                               Reference<Integer> pExitCode);


.. note::

   There is a simpler utility method which allows for simpler ``String`` passing for the ``str`` argument.

   *Utility method*:

   .. code-block:: java

      public static int gsapi_run_string(long instance,
                                         String str,
                                            int userErrors,
                             Reference<Integer> pExitCode);


.. _java gsapi_run_string_end:


``gsapi_run_string_end``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: java

   public static native int gsapi_run_string_end(long instance,
                                                  int userErrors,
                                   Reference<Integer> pExitCode);


.. _java gsapi_run_file:

``gsapi_run_file``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: java

   public static native int gsapi_run_file(long instance,
                                         byte[] fileName,
                                            int userErrors,
                             Reference<Integer> pExitCode);

.. note::

   There is a simpler utility method which allows for simpler ``String`` passing for the ``fileName`` argument.

   *Utility method*:

   .. code-block:: java

      public static int gsapi_run_file(long instance,
                                     String fileName,
                                        int userErrors,
                         Reference<Integer> pExitCode);


.. _java gsapi_exit:


``gsapi_exit``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Exit the interpreter. This must be called on shutdown if :ref:`gsapi_init_with_args<java gsapi_init_with_args>` has been called, and just before :ref:`gsapi_delete_instance<java gsapi_delete_instance>`.

.. code-block:: java

   public static native int gsapi_exit(long instance);



.. _java gsapi_set_param:

``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 :ref:`gsapi_run_string<java gsapi_run_string>` operation.

Parameters in this context are not the same as 'arguments' as processed by :ref:`gsapi_init_with_args<java gsapi_init_with_args>`, but often the same thing can be achieved. For example, with :ref:`gsapi_init_with_args<java gsapi_init_with_args>`, we can pass "-r200" to change the resolution. Broadly the same thing can be achieved by using :ref:`gsapi_set_param<java 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 :ref:`gsapi_set_param<java 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 :ref:`gsapi_get_param<java gsapi_get_param>`  calls.


.. code-block:: java

   public static native int gsapi_set_param(long instance,
                                           byte[] param,
                                           Object value,
                                              int paramType);

.. note::

   For more on the C implementation of parameters see: :ref:`Ghostscript parameters in C<Use_Setting Parameters>`.

   There are also simpler utility methods which allows for simpler String passing for your arguments.

   *Utility methods*:


   .. code-block:: java

      public static int gsapi_set_param(long instance,
                                        String param,
                                        String value,
                                           int paramType);

      public static int gsapi_set_param(long instance,
                                      String param,
                                      Object value,
                                         int paramType);


.. _java gsapi_get_param:

``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 :ref:`gsapi_init_with_args<java gsapi_init_with_args>` has been called will not list any, even if :ref:`gsapi_set_param<java gsapi_set_param>` has been used.

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

For further documentation please refer to :ref:`the C API<API_gsapi_get_param>`.


.. code-block:: java

   public static native int gsapi_get_param(long instance,
                                          byte[] param,
                                            long value,
                                             int paramType);
.. note::

   There is a simpler utility method which allows for simpler ``String`` passing for the ``param`` argument.

   *Utility method*:

   .. code-block:: java

      public static int gsapi_get_param(long instance,
                                       String param,
                                         long value,
                                          int paramType);


.. _java gsapi_enumerate_params:

``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 :ref:`gsapi_enumerate_params<java gsapi_enumerate_params>`. If type is non ``NULL`` then the pointer type will be updated to have the type of the parameter.

.. note::

  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 :ref:`gsapi_init_with_args<java gsapi_init_with_args>` has been called will not list any, even if :ref:`gsapi_set_param<java gsapi_set_param>` has been used.


.. code-block:: java

   public static native int gsapi_enumerate_params(long instance,
                                        Reference<Long> iter,
                                      Reference<byte[]> key,
                                     Reference<Integer> paramType);


.. _java gsapi_add_control_path:

``gsapi_add_control_path``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Add a (case sensitive) path to one of the lists of :ref:`permitted paths<Use Safer>` for file access.


.. code-block:: java

   public static native int gsapi_add_control_path(long instance,
                                                      int type,
                                                   byte[] path);

.. note::
   There is a simpler utility method which allows for simpler ``String`` passing for the ``path`` argument.

   *Utility method*:

   .. code-block:: java

      public static int gsapi_add_control_path(long instance,
                                                int type,
                                             String path);



.. _java gsapi_remove_control_path:

``gsapi_remove_control_path``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Remove a (case sensitive) path from one of the lists of :ref:`permitted paths<Use Safer>` for file access.


.. code-block:: java

   public static native int gsapi_remove_control_path(long instance,
                                                        int type,
                                                     byte[] path);
.. note::

   There is a simpler utility method which allows for simpler ``String`` passing for the ``path`` argument.

   *Utility method*:

   .. code-block:: java

      public static int gsapi_remove_control_path(long instance,
                                                   int type,
                                                String path);



.. _java gsapi_purge_control_paths:

``gsapi_purge_control_paths``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Clear all the paths from one of the lists of :ref:`permitted paths<Use Safer>` for file access.

.. code-block:: java

   public static native void gsapi_purge_control_paths(long instance,
                                                        int type);


.. _java gsapi_activate_path_control:

``gsapi_activate_path_control``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enable/Disable path control (i.e. whether paths are checked against :ref:`permitted paths<Use Safer>` before access is granted).

.. code-block:: java

   public static native void gsapi_activate_path_control(long instance,
                                                      boolean enable);


.. _java gsapi_is_path_control_active:


``gsapi_is_path_control_active``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Query whether path control is activated or not.


.. code-block:: java

   public static native boolean gsapi_is_path_control_active(long instance);



Callback & Callout interfaces
--------------------------------------

``gsjava.jar`` also defines some functional interfaces for callbacks & callouts with package ``com.artifex.gsjava.callback`` which are defined as follows.


``IStdInFunction``
~~~~~~~~~~~~~~~~~~~~~

.. code-block:: java

   public interface IStdInFunction {
       /**
        * @param callerHandle The caller handle.
        * @param buf A string represented by a byte array.
        * @param len The number of bytes to read.
        * @return The number of bytes read, must be <code>len</code>/
        */
       public int onStdIn(long callerHandle,
                        byte[] buf,
                           int len);
   }


``IStdOutFunction``
~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: java

   public interface IStdOutFunction {
       /**
        * Called when something should be written to the standard
        * output stream.
        *
        * @param callerHandle The caller handle.
        * @param str The string represented by a byte array to write.
        * @param len The number of bytes to write.
        * @return The number of bytes written, must be <code>len</code>.
        */
       public int onStdOut(long callerHandle,
                         byte[] str,
                            int len);
   }


``IStdErrFunction``
~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: java

   public interface IStdErrFunction {
       /**
        * Called when something should be written to the standard error stream.
        *
        * @param callerHandle The caller handle.
        * @param str The string represented by a byte array to write.
        * @param len The length of bytes to be written.
        * @return The amount of bytes written, must be <code>len</code>.
        */
       public int onStdErr(long callerHandle,
                         byte[] str,
                            int len);
   }


``IPollFunction``
~~~~~~~~~~~~~~~~~~

.. code-block:: java

   public interface IPollFunction {
       public int onPoll(long callerHandle);
   }


``ICalloutFunction``
~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: java

   public interface ICalloutFunction {
       public int onCallout(long instance,
                            long calloutHandle,
                          byte[] deviceName,
                             int id,
                             int size,
                            long data);
   }



GSInstance
---------------

This is a utility class which makes Ghostscript calls easier by storing a Ghostscript instance and, optionally, a caller handle. Essentially the class acts as a handy wrapper for the standard GSAPI_ methods.


Constructors
~~~~~~~~~~~~~~~~~~~~

.. code-block:: java

   public GSInstance() throws IllegalStateException;
   public GSInstance(long callerHandle) throws IllegalStateException;


``delete_instance``
~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_delete_instance<java gsapi_delete_instance>`.

.. code-block:: java

   public void delete_instance();


``set_stdio``
~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_set_stdio<java gsapi_set_stdio>`.

.. code-block:: java

   public int set_stdio(IStdInFunction stdin,
                       IStdOutFunction stdout,
                       IStdErrFunction stderr);

``set_poll``
~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_set_poll<java gsapi_set_poll>`.

.. code-block:: java

   public int set_poll(IPollFunction pollfun);


``set_display_callback``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_set_display_callback<java gsapi_set_display_callback>`.

.. code-block:: java

   public int set_display_callback(DisplayCallback displaycallback);


``register_callout``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_register_callout<java gsapi_register_callout>`.

.. code-block:: java

   public int register_callout(ICalloutFunction callout);


``deregister_callout``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_deregister_callout<java gsapi_deregister_callout>`.

.. code-block:: java

   public void deregister_callout(ICalloutFunction callout);


``set_arg_encoding``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_set_arg_encoding<java gsapi_set_arg_encoding>`.

.. code-block:: java

   public int set_arg_encoding(int encoding);


``set_default_device_list``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_set_default_device_list<java gsapi_set_default_device_list>`.

.. code-block:: java

   public int set_default_device_list(byte[] list,
                                         int listlen);


``get_default_device_list``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Wraps :ref:`gsapi_get_default_device_list<java gsapi_get_default_device_list>`.

.. code-block:: java

   public int get_default_device_list(Reference<byte[]> list,
                                      Reference<Integer> listlen);

``init_with_args``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_init_with_args<java gsapi_init_with_args>`.

.. code-block:: java

   public int init_with_args(int argc,
                        byte[][] argv);

   public int init_with_args(String[] argv);

   public int init_with_args(List<String> argv);



``run_string_begin``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_run_string_begin<java gsapi_run_string_begin>`.

.. code-block:: java

   public int run_string_begin(int userErrors,
                Reference<Integer> pExitCode);

``run_string_continue``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_run_string_continue<java gsapi_run_string_continue>`.

.. code-block:: java

   public int run_string_continue(byte[] str,
                                     int length,
                                     int userErrors,
                      Reference<Integer> pExitCode);

   public int run_string_continue(String str,
                                     int length,
                                     int userErrors,
                      Reference<Integer> pExitCode);


``run_string``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_run_string<java gsapi_run_string>`.


.. code-block:: java

   public int run_string(byte[] str,
                            int userErrors,
             Reference<Integer> pExitCode);

   public int run_string(String str,
                            int userErrors,
             Reference<Integer> pExitCode);


``run_file``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_run_file<java gsapi_run_file>`.


.. code-block:: java

   public int run_file(byte[] fileName,
                          int userErrors,
           Reference<Integer> pExitCode);

   public int run_file(String filename,
                          int userErrors,
           Reference<Integer> pExitCode);


``exit``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_exit<java gsapi_exit>`.

.. code-block:: java

   public int exit();


``set_param``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_set_param<java gsapi_set_param>`.

.. code-block:: java

   public int set_param(byte[] param,
                         Object value,
                            int paramType);

   public int set_param(String param,
                        String value,
                           int paramType);

   public int set_param(String param,
                        Object value,
                           int paramType);


``get_param``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_get_param<java gsapi_get_param>`.


.. code-block:: java

   public int get_param(byte[] param,
                          long value,
                           int paramType);

   public int get_param(String param,
                          long value,
                           int paramType);

``enumerate_params``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_enumerate_params<java gsapi_enumerate_params>`.

.. code-block:: java

   public int enumerate_params(Reference<Long> iter,
                             Reference<byte[]> key,
                            Reference<Integer> paramType);



``add_control_path``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_add_control_path<java gsapi_add_control_path>`.


.. code-block:: java

   public int add_control_path(int type,
                            byte[] path);

   public int add_control_path(int type,
                            String path);


``remove_control_path``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_remove_control_path<java gsapi_remove_control_path>`.


.. code-block:: java

   public int remove_control_path(int type,
                               byte[] path);

   public int remove_control_path(int type,
                               String path);

``purge_control_paths``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_purge_control_paths<java gsapi_purge_control_paths>`.


.. code-block:: java

   public void purge_control_paths(int type);


``activate_path_control``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_activate_path_control<java gsapi_activate_path_control>`.

.. code-block:: java

   public void activate_path_control(boolean enable);


``is_path_control_active``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wraps :ref:`gsapi_is_path_control_active<java gsapi_is_path_control_active>`.

.. code-block:: java

   public boolean is_path_control_active();


Utility classes
------------------

The ``com.artifex.gsjava.util`` package contains a set of classes and interfaces which are used throughout the API.

``com.artifex.gsjava.util.Reference``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

``Reference<T>`` is used in many of the Ghostscript calls, it stores a reference to a generic value of type ``T``. This class exists to emulate pointers being passed to a native function. Its value can be fetched with ``getValue()`` and set with ``setValue(T value)``.


.. code-block:: java

   public class Reference<T> {

       private volatile T value;

       public Reference() {
           this(null);
       }

       public Reference(T value) {
           this.value = value;
       }

       public void setValue(T value) {
           this.value = value;
       }

       public T getValue() {
           return value;
       }
       ...
   }

.. External links

.. _Eclipse: https://www.eclipse.org/eclipseide/