PostScript Printer fixes.

This commit is contained in:
OBattler
2020-08-13 02:53:16 +02:00
parent 44d11e0091
commit 81da0ba8e2

View File

@@ -32,12 +32,14 @@
#include <86box/ui.h> #include <86box/ui.h>
#include <86box/prt_devs.h> #include <86box/prt_devs.h>
#ifdef _WIN32 #ifdef _WIN32
# define GSDLLAPI __stdcall # define GSDLLAPI __stdcall
#else #else
# define GSDLLAPI # define GSDLLAPI
#endif #endif
#define GS_ARG_ENCODING_UTF16LE 2 #define GS_ARG_ENCODING_UTF16LE 2
#define gs_error_Quit -101 #define gs_error_Quit -101
@@ -46,31 +48,6 @@
#define POSTSCRIPT_BUFFER_LENGTH 65536 #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 typedef struct
{ {
@@ -98,12 +75,39 @@ typedef struct
size_t buffer_pos; size_t buffer_pos;
} ps_t; } 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 static void
reset_ps(ps_t *dev) reset_ps(ps_t *dev)
{ {
if (dev == NULL) { if (dev == NULL)
return; return;
}
dev->ack = false; dev->ack = false;
@@ -114,6 +118,7 @@ reset_ps(ps_t *dev)
timer_disable(&dev->timeout_timer); timer_disable(&dev->timeout_timer);
} }
static void static void
pulse_timer(void *priv) pulse_timer(void *priv)
{ {
@@ -127,6 +132,7 @@ pulse_timer(void *priv)
timer_disable(&dev->pulse_timer); timer_disable(&dev->pulse_timer);
} }
static int static int
convert_to_pdf(ps_t *dev) convert_to_pdf(ps_t *dev)
{ {
@@ -153,42 +159,29 @@ convert_to_pdf(ps_t *dev)
gsargv[8] = input_fn; gsargv[8] = input_fn;
code = gsapi_new_instance(&instance, dev); code = gsapi_new_instance(&instance, dev);
if (code < 0) { if (code < 0)
return code; return code;
}
code = gsapi_set_arg_encoding(instance, GS_ARG_ENCODING_UTF16LE); 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); 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); code = gsapi_exit(instance);
} else { else
gsapi_exit(instance); gsapi_exit(instance);
}
gsapi_delete_instance(instance); gsapi_delete_instance(instance);
if (code == 0) { if (code == 0)
plat_remove(input_fn); plat_remove(input_fn);
} else { else
plat_remove(output_fn); plat_remove(output_fn);
}
return code; return code;
} }
static void
finish_document(ps_t *dev)
{
if (ghostscript_handle != NULL) {
convert_to_pdf(dev);
}
dev->filename[0] = 0;
}
static void static void
write_buffer(ps_t *dev, bool newline) write_buffer(ps_t *dev, bool newline)
@@ -196,22 +189,19 @@ write_buffer(ps_t *dev, bool newline)
wchar_t path[1024]; wchar_t path[1024];
FILE *fp; FILE *fp;
if (dev->buffer[0] == 0) { if (dev->buffer[0] == 0)
return; return;
}
if (dev->filename[0] == 0) { if (dev->filename[0] == 0)
plat_tempfile(dev->filename, NULL, L".ps"); plat_tempfile(dev->filename, NULL, L".ps");
}
path[0] = 0; path[0] = 0;
wcscat(path, dev->printer_path); wcscat(path, dev->printer_path);
wcscat(path, dev->filename); wcscat(path, dev->filename);
fp = plat_fopen(path, L"a"); fp = plat_fopen(path, L"a");
if (fp == NULL) { if (fp == NULL)
return; return;
}
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
@@ -221,36 +211,42 @@ write_buffer(ps_t *dev, bool newline)
dev->buffer[0] = 0; dev->buffer[0] = 0;
dev->buffer_pos = 0; dev->buffer_pos = 0;
if (ghostscript_handle != NULL)
convert_to_pdf(dev);
dev->filename[0] = 0;
} }
static void static void
timeout_timer(void *priv) timeout_timer(void *priv)
{ {
ps_t *dev = (ps_t *) priv; ps_t *dev = (ps_t *) priv;
write_buffer(dev, false); write_buffer(dev, false);
finish_document(dev);
timer_disable(&dev->timeout_timer); timer_disable(&dev->timeout_timer);
} }
static void static void
ps_write_data(uint8_t val, void *p) ps_write_data(uint8_t val, void *p)
{ {
ps_t *dev = (ps_t *) p; ps_t *dev = (ps_t *) p;
if (dev == NULL) { if (dev == NULL)
return; return;
}
dev->data = (char) val; dev->data = (char) val;
} }
static void static void
process_data(ps_t *dev) process_data(ps_t *dev)
{ {
/* Check for non-printable characters */ /* Check for non-printable characters */
if (dev->data < 0x20 || dev->data == 0x7F) { if ((dev->data < 0x20) || (dev->data == 0x7f)) {
switch (dev->data) { switch (dev->data) {
/* The following characters are considered white-space /* The following characters are considered white-space
by the PostScript specification */ by the PostScript specification */
@@ -268,7 +264,6 @@ process_data(ps_t *dev)
/* Ctrl+D (0x04) marks the end of the document */ /* Ctrl+D (0x04) marks the end of the document */
case '\4': case '\4':
write_buffer(dev, false); write_buffer(dev, false);
finish_document(dev);
return; return;
/* Don't bother with the others */ /* 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 */ /* 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); write_buffer(dev, false);
dev->buffer_pos = 0;
}
dev->buffer[dev->buffer_pos++] = dev->data; dev->buffer[dev->buffer_pos++] = dev->data;
dev->buffer[dev->buffer_pos] = 0; dev->buffer[dev->buffer_pos] = 0;
} }
static void static void
ps_write_ctrl(uint8_t val, void *p) ps_write_ctrl(uint8_t val, void *p)
{ {
ps_t *dev = (ps_t *) p; ps_t *dev = (ps_t *) p;
if (dev == NULL) { if (dev == NULL)
return; return;
}
dev->autofeed = val & 0x02 ? true : false; dev->autofeed = val & 0x02 ? true : false;
if (val & 0x08) { if (val & 0x08)
dev->select = true; dev->select = true;
}
if ((val & 0x04) && !(dev->ctrl & 0x04)) { if ((val & 0x04) && !(dev->ctrl & 0x04)) {
// reset printer /* Reset printer */
dev->select = false; dev->select = false;
reset_ps(dev); reset_ps(dev);
@@ -321,21 +313,20 @@ ps_write_ctrl(uint8_t val, void *p)
dev->ctrl = val; dev->ctrl = val;
} }
static uint8_t static uint8_t
ps_read_status(void *p) ps_read_status(void *p)
{ {
ps_t *dev = (ps_t *) 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; ret |= 0x40;
}
return(ret); return(ret);
} }
static void * static void *
ps_init(void *lpt) ps_init(void *lpt)
{ {
@@ -351,23 +342,22 @@ ps_init(void *lpt)
/* Try loading the DLL. */ /* Try loading the DLL. */
ghostscript_handle = dynld_module(PATH_GHOSTSCRIPT_DLL, ghostscript_imports); 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); ui_msgbox_header(MBX_ERROR, (wchar_t *) IDS_2114, (wchar_t *) IDS_2132);
} else { else {
if (gsapi_revision(&rev, sizeof(rev)) == 0) { if (gsapi_revision(&rev, sizeof(rev)) == 0)
pclog("Loaded %s, rev %ld (%ld)\n", rev.product, rev.revision, rev.revisiondate); pclog("Loaded %s, rev %ld (%ld)\n", rev.product, rev.revision, rev.revisiondate);
} else { else {
dynld_close(ghostscript_handle); dynld_close(ghostscript_handle);
ghostscript_handle = NULL; ghostscript_handle = NULL;
} }
} }
// Cache print folder path /* Cache print folder path. */
memset(dev->printer_path, 0x00, sizeof(dev->printer_path)); memset(dev->printer_path, 0x00, sizeof(dev->printer_path));
plat_append_filename(dev->printer_path, usr_path, L"printer"); 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_dir_create(dev->printer_path);
}
plat_path_slash(dev->printer_path); plat_path_slash(dev->printer_path);
timer_add(&dev->pulse_timer, pulse_timer, dev, 0); timer_add(&dev->pulse_timer, pulse_timer, dev, 0);
@@ -376,19 +366,17 @@ ps_init(void *lpt)
return(dev); return(dev);
} }
static void static void
ps_close(void *p) ps_close(void *p)
{ {
ps_t *dev = (ps_t *) p; ps_t *dev = (ps_t *) p;
if (dev == NULL) { if (dev == NULL)
return; return;
}
if (dev->buffer[0] != 0) { if (dev->buffer[0] != 0)
write_buffer(dev, false); write_buffer(dev, false);
finish_document(dev);
}
if (ghostscript_handle != NULL) { if (ghostscript_handle != NULL) {
dynld_close(ghostscript_handle); dynld_close(ghostscript_handle);
@@ -398,6 +386,7 @@ ps_close(void *p)
free(dev); free(dev);
} }
const lpt_device_t lpt_prt_ps_device = { const lpt_device_t lpt_prt_ps_device = {
.name = "Generic PostScript printer", .name = "Generic PostScript printer",
.init = ps_init, .init = ps_init,
@@ -407,4 +396,4 @@ const lpt_device_t lpt_prt_ps_device = {
.read_data = NULL, .read_data = NULL,
.read_status = ps_read_status, .read_status = ps_read_status,
.read_ctrl = NULL .read_ctrl = NULL
}; };