diff options
author | evrhel <ethanvrhel@gmail.com> | 2020-07-12 15:17:16 -0700 |
---|---|---|
committer | evrhel <ethanvrhel@gmail.com> | 2020-09-23 13:28:55 -0700 |
commit | 4283bf3be5ad3e3e077e6666b5b3135f04a1d430 (patch) | |
tree | 7855a42f62f9615016ef44465f0b2e3e7b210f65 | |
parent | e5af7e20dbc8f3164da386edf3dec3e5c8b3ba9d (diff) | |
download | ghostpdl-4283bf3be5ad3e3e077e6666b5b3135f04a1d430.tar.gz |
Fixed some bugs related to stdio
Fixed stdio bugs and added some more utility methods to make calling
ghostscript functions easier in Java.
-rw-r--r-- | demos/java/gsjava/src/com/artifex/gsjava/GSAPI.java | 71 | ||||
-rw-r--r-- | demos/java/gsjava/src/com/artifex/gsjava/Main.java | 56 |
2 files changed, 118 insertions, 9 deletions
diff --git a/demos/java/gsjava/src/com/artifex/gsjava/GSAPI.java b/demos/java/gsjava/src/com/artifex/gsjava/GSAPI.java index ab6005e32..8ca2c0af9 100644 --- a/demos/java/gsjava/src/com/artifex/gsjava/GSAPI.java +++ b/demos/java/gsjava/src/com/artifex/gsjava/GSAPI.java @@ -1,5 +1,7 @@ package com.artifex.gsjava; +import java.util.List; + import com.artifex.gsjava.callbacks.DisplayCallback; import com.artifex.gsjava.callbacks.ICalloutFunction; import com.artifex.gsjava.callbacks.IPollFunction; @@ -74,6 +76,41 @@ public class GSAPI { GS_ERROR_INVALIDCONTEXT = -29, GS_ERROR_INVALID = -30; + public static final int GS_COLORS_NATIVE = (1 << 0), + GS_COLORS_GRAY = (1 << 1), + GS_COLORS_RGB = (1 << 2), + GS_COLORS_CMYK = (1 << 3), + GS_DISPLAY_COLORS_SEPARATION = (1 << 19); + + public static final long GS_DISPLAY_COLORS_MASK = 0x8000fL; + + public static final int GS_DISPLAY_ALPHA_NONE = (0 << 4), + GS_DISPLAY_ALPHA_FIRST = (1 << 4), + GS_DISPLAY_ALPHA_LAST = (1 << 5), + GS_DIAPLAY_UNUSED_FIRST = (1 <<6), + GS_DISPLAY_UNUSED_LAST = (1 << 7); + + public static final long GS_DISPLAY_ALPGA_MASK = 0x00f0L; + + public static final int GS_DISPLAY_DEPTH_1 = (1 << 8), + GS_DISPLAY_DEPTH_2 = (1 << 9), + GS_DISPLAY_DEPTH_4 = (1 << 10), + GS_DISPLAY_DEPTH_8 = (1 << 11), + GS_DISPLAY_DEPTH_12 = (1 << 12), + GS_DISPLAY_DEPTH_16 = (1 << 3); + + public static final long GS_DISPLAY_DEPTH_MASK = 0xff00L; + + public static final int GS_DISPLAY_BIGENDIAN = (0 << 16), + GS_DISPLAY_LITTLEENDIAN = (1 << 16); + + public static final long GS_DISPLAY_ENDIAN_MASK = 0x00010000L; + + public static final int GS_DISPLAY_TOPFIRST = (0 << 17), + GS_DISPLAY_BOTTOMFIRST = (1 << 17); + + public static final long DISPLAY_FIRSTROW_MASK = 0x00020000L; + public static class Revision { public volatile byte[] product; public volatile byte[] copyright; @@ -129,10 +166,6 @@ public class GSAPI { public static native int gsapi_init_with_args(long instance, int argc, byte[][] argv); - public static int gsapi_init_with_args(long instance, String[] argv) { - return gsapi_init_with_args(instance, argv.length, StringUtil.to2DByteArray(argv)); - } - public static native int gsapi_run_string_begin(long instance, int userErrors, IntReference pExitCode); public static native int gsapi_run_string_continue(long instance, byte[] str, int length, int userErrors, @@ -148,4 +181,34 @@ public class GSAPI { public static native int gsapi_run_file(long instance, byte[] fileName, int userErrors, IntReference pExitCode); public static native int gsapi_exit(long instance); + + // Utility methods to make calling some native methods easier + + public static int gsapi_init_with_args(long instance, String[] argv) { + return gsapi_init_with_args(instance, argv.length, StringUtil.to2DByteArray(argv)); + } + + public static int gsapi_init_with_args(long instance, List<String> argv) { + return gsapi_init_with_args(instance, argv.toArray(new String[argv.size()])); + } + + public static int gsapi_run_string_continue(long instance, String str, int length, int userErrors, + IntReference pExitCode) { + return gsapi_run_string_continue(instance, StringUtil.toNullTerminatedByteArray(str.substring(0, length)), + length, userErrors, pExitCode); + } + + public static int gsapi_run_string_with_length(long instance, String str, int length, int userErrors, + IntReference pExitCode) { + return gsapi_run_string_with_length(instance, StringUtil.toNullTerminatedByteArray(str.substring(0, length)), + length, userErrors, pExitCode); + } + + public static int gsapi_run_string(long instance, String str, int userErrors, IntReference pExitCode) { + return gsapi_run_string(instance, StringUtil.toNullTerminatedByteArray(str), userErrors, pExitCode); + } + + public static int gsapi_run_file(long instance, String fileName, int userErrors, IntReference pExitCode) { + return gsapi_run_file(instance, StringUtil.toNullTerminatedByteArray(fileName), userErrors, pExitCode); + } } diff --git a/demos/java/gsjava/src/com/artifex/gsjava/Main.java b/demos/java/gsjava/src/com/artifex/gsjava/Main.java index 4cd02b91b..fae1c3ed4 100644 --- a/demos/java/gsjava/src/com/artifex/gsjava/Main.java +++ b/demos/java/gsjava/src/com/artifex/gsjava/Main.java @@ -2,18 +2,20 @@ package com.artifex.gsjava; import static com.artifex.gsjava.GSAPI.*; +import java.io.File; +import java.io.FileNotFoundException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import com.artifex.gsjava.GSAPI.Revision; import com.artifex.gsjava.callbacks.DisplayCallback; import com.artifex.gsjava.util.LongReference; -import com.artifex.gsjava.util.StringUtil; public class Main { - public static void main(String[] args) { - Revision revision = new Revision(); + public static void main(String[] args) throws FileNotFoundException { + final Revision revision = new Revision(); gsapi_revision(revision, 0); System.out.println("Product: " + revision.getProduct()); System.out.println("Copyright: " + revision.getCopyright()); @@ -28,7 +30,7 @@ public class Main { System.out.println("Instance address: 0x" + Long.toHexString(instanceRef.value)); System.out.println(); - StdIO stdio = new StdIO(); + final StdIO stdio = new StdIO(); code = gsapi_set_stdio_with_handle(instanceRef.value, stdio, stdio, stdio, GS_NULL); if (code != GS_ERROR_OK) { @@ -38,8 +40,52 @@ public class Main { return; } - String[] gargs = { "gs", "-h" }; + code = gsapi_set_arg_encoding(instanceRef.value, 1); + if (code != GS_ERROR_OK) { + System.err.println("Failed to set arg encoding (code = " + code + ")"); + gsapi_delete_instance(instanceRef.value); + instanceRef.value = GS_NULL; + return; + } + + final DisplayCallback displayCallback = new StandardDisplayCallback(); + code = gsapi_set_display_callback(instanceRef.value, displayCallback); + if (code != GS_ERROR_OK) { + System.err.println("Failed to display callback (code = " + code + ")"); + gsapi_delete_instance(instanceRef.value); + instanceRef.value = GS_NULL; + return; + } + + final int format = GS_COLORS_RGB | GS_DISPLAY_DEPTH_8 | GS_DISPLAY_LITTLEENDIAN; + + final File file = new File("Hello.pdf"); + if (!file.exists()) + throw new FileNotFoundException(file.getAbsolutePath()); + + final File ofile = new File("image.tiff"); + + + String[] gargs = { "gs", "-dNOPAUSE", "-dSAFER", + "-I%rom%Resource%/Init/", + "-dBATCH", "-r72", "-sDEVICE=display", + "-dDisplayFormat=" + format, + "-f", + file.getAbsolutePath() }; + + /*String[] gargs = { "gs", "-dNOPAUSE", "-dSAFER", + "-I%rom%Resource%/Init/", + "-dBATCH", "-r72", "-sDEVICE=tiff24nc", "-o", ofile.getAbsolutePath(), + "-f", + file.getAbsolutePath() };*/ + //final String[] gargs = { "gs", "-Z#", "-h" }; + System.out.println("args = " + Arrays.toString(gargs)); code = gsapi_init_with_args(instanceRef.value, gargs); + if (code != GS_ERROR_OK) { + System.err.println("Failed to gsapi_init_with_args (code = " + code + ")"); + } else { + System.out.println("gsapi_init_with_args success"); + } } else { System.err.println("Failed to create new instance"); |