From 81da0ba8e244a30e054045b3a6222213d6f1fb5d Mon Sep 17 00:00:00 2001 From: OBattler Date: Thu, 13 Aug 2020 02:53:16 +0200 Subject: [PATCH] PostScript Printer fixes. --- src/printer/prt_ps.c | 155 ++++++++++++++++++++----------------------- 1 file changed, 72 insertions(+), 83 deletions(-) diff --git a/src/printer/prt_ps.c b/src/printer/prt_ps.c index 7a3a0619b..03e7ea9b9 100644 --- a/src/printer/prt_ps.c +++ b/src/printer/prt_ps.c @@ -32,12 +32,14 @@ #include <86box/ui.h> #include <86box/prt_devs.h> + #ifdef _WIN32 # define GSDLLAPI __stdcall #else # define GSDLLAPI #endif + #define GS_ARG_ENCODING_UTF16LE 2 #define gs_error_Quit -101 @@ -46,31 +48,6 @@ #define POSTSCRIPT_BUFFER_LENGTH 65536 -typedef struct gsapi_revision_s { - const char *product; - const char *copyright; - long revision; - long revisiondate; -} gsapi_revision_t; - -static int (GSDLLAPI *gsapi_revision)(gsapi_revision_t *pr, int len); -static int (GSDLLAPI *gsapi_new_instance)(void **pinstance, void *caller_handle); -static void (GSDLLAPI *gsapi_delete_instance)(void *instance); -static int (GSDLLAPI *gsapi_set_arg_encoding)(void *instance, int encoding); -static int (GSDLLAPI *gsapi_init_with_args)(void *instance, int argc, char **argv); -static int (GSDLLAPI *gsapi_exit)(void *instance); - -static dllimp_t ghostscript_imports[] = { - { "gsapi_revision", &gsapi_revision }, - { "gsapi_new_instance", &gsapi_new_instance }, - { "gsapi_delete_instance", &gsapi_delete_instance }, - { "gsapi_set_arg_encoding", &gsapi_set_arg_encoding }, - { "gsapi_init_with_args", &gsapi_init_with_args }, - { "gsapi_exit", &gsapi_exit }, - { NULL, NULL } -}; - -static void *ghostscript_handle = NULL; typedef struct { @@ -98,12 +75,39 @@ typedef struct size_t buffer_pos; } ps_t; +typedef struct gsapi_revision_s { + const char *product; + const char *copyright; + long revision; + long revisiondate; +} gsapi_revision_t; + + +static int (GSDLLAPI *gsapi_revision)(gsapi_revision_t *pr, int len); +static int (GSDLLAPI *gsapi_new_instance)(void **pinstance, void *caller_handle); +static void (GSDLLAPI *gsapi_delete_instance)(void *instance); +static int (GSDLLAPI *gsapi_set_arg_encoding)(void *instance, int encoding); +static int (GSDLLAPI *gsapi_init_with_args)(void *instance, int argc, char **argv); +static int (GSDLLAPI *gsapi_exit)(void *instance); + +static dllimp_t ghostscript_imports[] = { + { "gsapi_revision", &gsapi_revision }, + { "gsapi_new_instance", &gsapi_new_instance }, + { "gsapi_delete_instance", &gsapi_delete_instance }, + { "gsapi_set_arg_encoding", &gsapi_set_arg_encoding }, + { "gsapi_init_with_args", &gsapi_init_with_args }, + { "gsapi_exit", &gsapi_exit }, + { NULL, NULL } +}; + +static void *ghostscript_handle = NULL; + + static void reset_ps(ps_t *dev) { - if (dev == NULL) { + if (dev == NULL) return; - } dev->ack = false; @@ -114,6 +118,7 @@ reset_ps(ps_t *dev) timer_disable(&dev->timeout_timer); } + static void pulse_timer(void *priv) { @@ -127,6 +132,7 @@ pulse_timer(void *priv) timer_disable(&dev->pulse_timer); } + static int convert_to_pdf(ps_t *dev) { @@ -153,42 +159,29 @@ convert_to_pdf(ps_t *dev) gsargv[8] = input_fn; code = gsapi_new_instance(&instance, dev); - if (code < 0) { + if (code < 0) return code; - } code = gsapi_set_arg_encoding(instance, GS_ARG_ENCODING_UTF16LE); - if (code == 0) { + if (code == 0) code = gsapi_init_with_args(instance, 9, (char **) gsargv); - } - if (code == 0 || code == gs_error_Quit) { + if (code == 0 || code == gs_error_Quit) code = gsapi_exit(instance); - } else { + else gsapi_exit(instance); - } gsapi_delete_instance(instance); - if (code == 0) { + if (code == 0) plat_remove(input_fn); - } else { + else plat_remove(output_fn); - } return code; } -static void -finish_document(ps_t *dev) -{ - if (ghostscript_handle != NULL) { - convert_to_pdf(dev); - } - - dev->filename[0] = 0; -} static void write_buffer(ps_t *dev, bool newline) @@ -196,22 +189,19 @@ write_buffer(ps_t *dev, bool newline) wchar_t path[1024]; FILE *fp; - if (dev->buffer[0] == 0) { + if (dev->buffer[0] == 0) return; - } - if (dev->filename[0] == 0) { + if (dev->filename[0] == 0) plat_tempfile(dev->filename, NULL, L".ps"); - } path[0] = 0; wcscat(path, dev->printer_path); wcscat(path, dev->filename); fp = plat_fopen(path, L"a"); - if (fp == NULL) { + if (fp == NULL) return; - } fseek(fp, 0, SEEK_END); @@ -221,36 +211,42 @@ write_buffer(ps_t *dev, bool newline) dev->buffer[0] = 0; dev->buffer_pos = 0; + + if (ghostscript_handle != NULL) + convert_to_pdf(dev); + + dev->filename[0] = 0; } + static void timeout_timer(void *priv) { ps_t *dev = (ps_t *) priv; write_buffer(dev, false); - finish_document(dev); timer_disable(&dev->timeout_timer); } + static void ps_write_data(uint8_t val, void *p) { ps_t *dev = (ps_t *) p; - if (dev == NULL) { + if (dev == NULL) return; - } dev->data = (char) val; } + static void process_data(ps_t *dev) { /* Check for non-printable characters */ - if (dev->data < 0x20 || dev->data == 0x7F) { + if ((dev->data < 0x20) || (dev->data == 0x7f)) { switch (dev->data) { /* The following characters are considered white-space by the PostScript specification */ @@ -268,7 +264,6 @@ process_data(ps_t *dev) /* Ctrl+D (0x04) marks the end of the document */ case '\4': write_buffer(dev, false); - finish_document(dev); return; /* Don't bother with the others */ @@ -278,32 +273,29 @@ process_data(ps_t *dev) } /* Flush the buffer if we have run to its end */ - if (dev->buffer_pos == POSTSCRIPT_BUFFER_LENGTH - 1) { + if (dev->buffer_pos == POSTSCRIPT_BUFFER_LENGTH - 1) write_buffer(dev, false); - dev->buffer_pos = 0; - } dev->buffer[dev->buffer_pos++] = dev->data; dev->buffer[dev->buffer_pos] = 0; } + static void ps_write_ctrl(uint8_t val, void *p) { ps_t *dev = (ps_t *) p; - if (dev == NULL) { + if (dev == NULL) return; - } dev->autofeed = val & 0x02 ? true : false; - if (val & 0x08) { + if (val & 0x08) dev->select = true; - } if ((val & 0x04) && !(dev->ctrl & 0x04)) { - // reset printer + /* Reset printer */ dev->select = false; reset_ps(dev); @@ -321,21 +313,20 @@ ps_write_ctrl(uint8_t val, void *p) dev->ctrl = val; } + static uint8_t ps_read_status(void *p) { ps_t *dev = (ps_t *) p; - uint8_t ret = 0x1f; + uint8_t ret = 0x9f; - ret |= 0x80; - - if (!dev->ack) { + if (!dev->ack) ret |= 0x40; - } return(ret); } + static void * ps_init(void *lpt) { @@ -351,23 +342,22 @@ ps_init(void *lpt) /* Try loading the DLL. */ ghostscript_handle = dynld_module(PATH_GHOSTSCRIPT_DLL, ghostscript_imports); - if (ghostscript_handle == NULL) { + if (ghostscript_handle == NULL) ui_msgbox_header(MBX_ERROR, (wchar_t *) IDS_2114, (wchar_t *) IDS_2132); - } else { - if (gsapi_revision(&rev, sizeof(rev)) == 0) { + else { + if (gsapi_revision(&rev, sizeof(rev)) == 0) pclog("Loaded %s, rev %ld (%ld)\n", rev.product, rev.revision, rev.revisiondate); - } else { + else { dynld_close(ghostscript_handle); ghostscript_handle = NULL; } } - // Cache print folder path + /* Cache print folder path. */ memset(dev->printer_path, 0x00, sizeof(dev->printer_path)); plat_append_filename(dev->printer_path, usr_path, L"printer"); - if (!plat_dir_check(dev->printer_path)) { + if (!plat_dir_check(dev->printer_path)) plat_dir_create(dev->printer_path); - } plat_path_slash(dev->printer_path); timer_add(&dev->pulse_timer, pulse_timer, dev, 0); @@ -376,19 +366,17 @@ ps_init(void *lpt) return(dev); } + static void ps_close(void *p) { ps_t *dev = (ps_t *) p; - if (dev == NULL) { + if (dev == NULL) return; - } - if (dev->buffer[0] != 0) { + if (dev->buffer[0] != 0) write_buffer(dev, false); - finish_document(dev); - } if (ghostscript_handle != NULL) { dynld_close(ghostscript_handle); @@ -398,6 +386,7 @@ ps_close(void *p) free(dev); } + const lpt_device_t lpt_prt_ps_device = { .name = "Generic PostScript printer", .init = ps_init, @@ -407,4 +396,4 @@ const lpt_device_t lpt_prt_ps_device = { .read_data = NULL, .read_status = ps_read_status, .read_ctrl = NULL -}; \ No newline at end of file +};