/* Ghostscript widget for GTK/GNOME
- *
+ *
* Copyright (C) 1998 - 2005 the Free Software Foundation
- *
- * Authors: Jonathan Blandford, Jaka Mocnik
- *
+ *
+ * Authors: Jonathan Blandford, Jaka Mocnik, Carlos Garcia Campos
+ *
* Based on code by: Federico Mena (Quartic), Szekeres Istvan (Pista)
*
* This library is free software; you can redistribute it and/or
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
-
+
#include "config.h"
+
+#include <glib/gstdio.h>
+#include <glib/gi18n.h>
+#include <gtk/gtkwidget.h>
+#include <gtk/gtkwindow.h>
+#include <gdk/gdk.h>
+#include <gdk/gdkx.h>
#include <string.h>
#include <stdlib.h>
-#include <signal.h>
-#include <gtk/gtk.h>
-#include <gtk/gtkobject.h>
-#include <gdk/gdkprivate.h>
-#include <gdk/gdkx.h>
-#include <gdk/gdk.h>
-#include <glib/gi18n.h>
-#include <X11/Intrinsic.h>
+#include <sys/wait.h>
#include <unistd.h>
-#include <fcntl.h>
-#include <stdlib.h>
#include <errno.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <stdio.h>
-#include <math.h>
#include "ps-document.h"
+#include "ps.h"
+#include "gstypes.h"
#include "gsdefaults.h"
#include "ev-file-exporter.h"
#include "ev-async-renderer.h"
PS_DOCUMENT(gs)->gs_filename)
/* structure to describe section of file to send to ghostscript */
-struct record_list
-{
+typedef struct {
FILE *fp;
- long begin;
+ glong begin;
guint len;
gboolean seek_needed;
gboolean close;
- struct record_list *next;
+} PSSection;
+
+struct _PSDocument {
+ GObject object;
+
+ GtkWidget *target_window;
+ GdkWindow *pstarget;
+ GdkPixmap *bpixmap;
+ glong message_window; /* Used by ghostview to receive messages from app */
+
+ GPid interpreter_pid; /* PID of interpreter, -1 if none */
+ GIOChannel *interpreter_input; /* stdin of interpreter */
+ GIOChannel *interpreter_output; /* stdout of interpreter */
+ GIOChannel *interpreter_err; /* stderr of interpreter */
+ guint interpreter_input_id;
+ guint interpreter_output_id;
+ guint interpreter_error_id;
+
+ gboolean busy; /* Is gs busy drawing? */
+ gboolean structured_doc;
+
+ GQueue *ps_input;
+ gchar *input_buffer_ptr;
+ guint bytes_left;
+ guint buffer_bytes_left;
+
+ FILE *gs_psfile; /* the currently loaded FILE */
+ gchar *gs_filename; /* the currently loaded filename */
+ gchar *gs_filename_unc; /* Uncompressed file */
+ gchar *input_buffer;
+ gboolean send_filename_to_gs; /* True if gs should read from file directly */
+ struct document *doc;
+
+ gint *ps_export_pagelist;
+ gchar *ps_export_filename;
+
+ const gchar *gs_status; /* PSDocument status */
};
-static gboolean broken_pipe = FALSE;
-
-/* Forward declarations */
-static void ps_document_init (PSDocument *gs);
-static void ps_document_class_init (PSDocumentClass *klass);
-static void send_ps (PSDocument *gs,
- long begin,
- unsigned int len,
- gboolean close);
-static void output (gpointer data,
- gint source,
- GdkInputCondition condition);
-static void input (gpointer data,
- gint source,
- GdkInputCondition condition);
-static void stop_interpreter (PSDocument *gs);
-static gint start_interpreter (PSDocument *gs);
-static void ps_document_document_iface_init (EvDocumentIface *iface);
-static void ps_document_file_exporter_iface_init (EvFileExporterIface *iface);
-static void ps_async_renderer_iface_init (EvAsyncRendererIface *iface);
+struct _PSDocumentClass {
+ GObjectClass parent_class;
+
+ GdkAtom gs_atom;
+ GdkAtom next_atom;
+ GdkAtom page_atom;
+ GdkAtom string_atom;
+};
+
+static void ps_document_document_iface_init (EvDocumentIface *iface);
+static void ps_document_file_exporter_iface_init (EvFileExporterIface *iface);
+static void ps_async_renderer_iface_init (EvAsyncRendererIface *iface);
+
+static void ps_interpreter_start (PSDocument *gs);
+static void ps_interpreter_stop (PSDocument *gs);
+static void ps_interpreter_failed (PSDocument *gs,
+ const gchar *msg);
+static gboolean ps_interpreter_is_ready (PSDocument *gs);
+
+static void push_pixbuf (PSDocument *gs);
+
G_DEFINE_TYPE_WITH_CODE (PSDocument, ps_document, G_TYPE_OBJECT,
{
ps_async_renderer_iface_init);
});
-static GObjectClass *parent_class = NULL;
-static PSDocumentClass *gs_class = NULL;
+static void
+ps_section_free (PSSection *section)
+{
+ if (!section)
+ return;
+
+ if (section->close && section->fp)
+ fclose (section->fp);
+ g_free (section);
+}
+
+/* PSDocument */
static void
ps_document_init (PSDocument *gs)
{
gs->bpixmap = NULL;
-
gs->interpreter_pid = -1;
gs->busy = FALSE;
gs->gs_filename = 0;
gs->gs_filename_unc = 0;
- broken_pipe = FALSE;
-
gs->structured_doc = FALSE;
- gs->reading_from_pipe = FALSE;
gs->send_filename_to_gs = FALSE;
gs->doc = NULL;
- gs->interpreter_input = -1;
- gs->interpreter_output = -1;
- gs->interpreter_err = -1;
+ gs->interpreter_input = NULL;
+ gs->interpreter_output = NULL;
+ gs->interpreter_err = NULL;
gs->interpreter_input_id = 0;
gs->interpreter_output_id = 0;
gs->interpreter_error_id = 0;
- gs->ps_input = NULL;
+ gs->ps_input = g_queue_new ();
gs->input_buffer = NULL;
gs->input_buffer_ptr = NULL;
gs->bytes_left = 0;
ps_document_dispose (GObject *object)
{
PSDocument *gs = PS_DOCUMENT (object);
-
- g_return_if_fail (gs != NULL);
-
+
if (gs->gs_psfile) {
fclose (gs->gs_psfile);
gs->gs_psfile = NULL;
}
if (gs->gs_filename_unc) {
- unlink(gs->gs_filename_unc);
+ g_unlink(gs->gs_filename_unc);
g_free(gs->gs_filename_unc);
gs->gs_filename_unc = NULL;
}
if (gs->bpixmap) {
- gdk_drawable_unref (gs->bpixmap);
+ g_object_unref (gs->bpixmap);
+ gs->bpixmap = NULL;
}
- if(gs->input_buffer) {
- g_free(gs->input_buffer);
+ if (gs->input_buffer) {
+ g_free (gs->input_buffer);
gs->input_buffer = NULL;
}
gs->pstarget = NULL;
}
- stop_interpreter (gs);
+ if (gs->ps_input) {
+ g_queue_foreach (gs->ps_input, (GFunc)ps_section_free, NULL);
+ g_queue_free (gs->ps_input);
+ gs->ps_input = NULL;
+ }
+
+ ps_interpreter_stop (gs);
- G_OBJECT_CLASS (parent_class)->dispose (object);
+ G_OBJECT_CLASS (ps_document_parent_class)->dispose (object);
}
static void
-ps_document_class_init(PSDocumentClass *klass)
+ps_document_class_init (PSDocumentClass *klass)
{
GObjectClass *object_class;
- object_class = (GObjectClass *) klass;
- parent_class = g_type_class_peek_parent (klass);
- gs_class = klass;
+ object_class = G_OBJECT_CLASS (klass);
- object_class->dispose = ps_document_dispose;
+ object_class->dispose = ps_document_dispose;
klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
klass->string_atom = gdk_atom_intern ("STRING", FALSE);
}
-static void
-push_pixbuf (PSDocument *gs)
+/* PSInterpreter */
+static gboolean
+ps_interpreter_input (GIOChannel *io,
+ GIOCondition condition,
+ PSDocument *gs)
{
- GdkColormap *cmap;
- GdkPixbuf *pixbuf;
- int width, height;
-
- if (gs->pstarget == NULL)
- return;
+ PSSection *section = NULL;
- cmap = gdk_window_get_colormap (gs->pstarget);
- gdk_drawable_get_size (gs->bpixmap, &width, &height);
- pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
- 0, 0, 0, 0,
- width, height);
- g_signal_emit_by_name (gs, "render_finished", pixbuf);
- g_object_unref (pixbuf);
-}
+ do {
+ if (gs->buffer_bytes_left == 0) {
+ /* Get a new section if required */
+ if (gs->bytes_left == 0) {
+ ps_section_free (section);
+ section = NULL;
+ g_queue_pop_tail (gs->ps_input);
+ }
+
+ if (section == NULL) {
+ section = g_queue_peek_tail (gs->ps_input);
+ }
-static void
-interpreter_failed (PSDocument *gs, char *msg)
-{
- push_pixbuf (gs);
+ /* Have to seek at the beginning of each section */
+ if (section && section->seek_needed) {
+ fseek (section->fp, section->begin, SEEK_SET);
+ section->seek_needed = FALSE;
+ gs->bytes_left = section->len;
+ }
- stop_interpreter (gs);
-}
+ if (gs->bytes_left > MAX_BUFSIZE) {
+ gs->buffer_bytes_left = fread (gs->input_buffer, sizeof (char),
+ MAX_BUFSIZE, section->fp);
+ } else if (gs->bytes_left > 0) {
+ gs->buffer_bytes_left = fread (gs->input_buffer, sizeof (char),
+ gs->bytes_left, section->fp);
+ } else {
+ gs->buffer_bytes_left = 0;
+ }
+
+ if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
+ ps_interpreter_failed (gs, NULL); /* Error occurred */
+ }
+
+ gs->input_buffer_ptr = gs->input_buffer;
+ gs->bytes_left -= gs->buffer_bytes_left;
+ }
-static gboolean
-ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
-{
- PSDocument *gs = (PSDocument *) data;
+ if (gs->buffer_bytes_left > 0) {
+ GIOStatus status;
+ gsize bytes_written;
+ GError *error = NULL;
+
+ status = g_io_channel_write_chars (gs->interpreter_input,
+ gs->input_buffer_ptr,
+ gs->buffer_bytes_left,
+ &bytes_written,
+ &error);
+ switch (status) {
+ case G_IO_STATUS_NORMAL:
+ gs->buffer_bytes_left -= bytes_written;
+ gs->input_buffer_ptr += bytes_written;
+
+ break;
+ case G_IO_STATUS_ERROR:
+ ps_interpreter_failed (gs, error->message);
+ g_error_free (error);
+
+ break;
+ case G_IO_STATUS_AGAIN:
+ default:
+ break;
+ }
+ }
+ } while (!g_queue_is_empty (gs->ps_input) && gs->buffer_bytes_left == 0);
- if(event->type != GDK_CLIENT_EVENT)
+ if (g_queue_is_empty (gs->ps_input) && gs->buffer_bytes_left == 0) {
+ GIOFlags flags;
+
+ flags = g_io_channel_get_flags (gs->interpreter_input);
+
+ g_io_channel_set_flags (gs->interpreter_input,
+ flags & ~G_IO_FLAG_NONBLOCK, NULL);
+ g_io_channel_flush (gs->interpreter_input, NULL);
+ g_io_channel_set_flags (gs->interpreter_input,
+ flags | G_IO_FLAG_NONBLOCK, NULL);
+
+ gs->interpreter_input_id = 0;
+
return FALSE;
+ }
+
+ return TRUE;
+}
- gs->message_window = event->client.data.l[0];
+static gboolean
+ps_interpreter_output (GIOChannel *io,
+ GIOCondition condition,
+ PSDocument *gs)
+{
+ gchar buf[MAX_BUFSIZE + 1];
+ gsize bytes = 0;
+ GIOStatus status;
+ GError *error = NULL;
+
+ status = g_io_channel_read_chars (io, buf, MAX_BUFSIZE,
+ &bytes, &error);
+ switch (status) {
+ case G_IO_STATUS_NORMAL:
+ if (bytes > 0) {
+ buf[bytes] = '\0';
+ g_print ("%s", buf);
+ }
+ break;
+ case G_IO_STATUS_EOF:
+ g_io_channel_unref (gs->interpreter_output);
+ gs->interpreter_output = NULL;
+ gs->interpreter_output_id = 0;
+
+ return FALSE;
+ case G_IO_STATUS_ERROR:
+ ps_interpreter_failed (gs, error->message);
+ g_error_free (error);
+ gs->interpreter_output_id = 0;
+
+ return FALSE;
+ default:
+ break;
+ }
+
+ if (!gs->interpreter_err) {
+ ps_interpreter_failed (gs, NULL);
+ }
- if (event->client.message_type == gs_class->page_atom) {
- gs->busy = FALSE;
+ return TRUE;
+}
- push_pixbuf (gs);
+static gboolean
+ps_interpreter_error (GIOChannel *io,
+ GIOCondition condition,
+ PSDocument *gs)
+{
+ gchar buf[MAX_BUFSIZE + 1];
+ gsize bytes = 0;
+ GIOStatus status;
+ GError *error = NULL;
+
+ status = g_io_channel_read_chars (io, buf, MAX_BUFSIZE,
+ &bytes, &error);
+ switch (status) {
+ case G_IO_STATUS_NORMAL:
+ if (bytes > 0) {
+ buf[bytes] = '\0';
+ g_print ("%s", buf);
+ }
+
+ break;
+ case G_IO_STATUS_EOF:
+ g_io_channel_unref (gs->interpreter_err);
+ gs->interpreter_err = NULL;
+ gs->interpreter_error_id = 0;
+
+ return FALSE;
+ case G_IO_STATUS_ERROR:
+ ps_interpreter_failed (gs, error->message);
+ g_error_free (error);
+ gs->interpreter_error_id = 0;
+
+ break;
+ default:
+ break;
+ }
+
+ if (!gs->interpreter_output) {
+ ps_interpreter_failed (gs, NULL);
}
return TRUE;
}
+#define NUM_ARGS 100
+#define NUM_GS_ARGS (NUM_ARGS - 20)
+#define NUM_ALPHA_ARGS 10
+
static void
-send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
+setup_interpreter_env (gchar **envp)
{
- struct record_list *ps_new;
+ gint i;
+
+ for (i = 0; envp[i]; i++)
+ putenv (envp[i]);
+}
- if (gs->interpreter_input < 0) {
- g_critical("No pipe to gs: error in send_ps().");
+static void
+ps_interpreter_start (PSDocument *gs)
+{
+ gchar *argv[NUM_ARGS], *dir, *gv_env, *gs_path;
+ gchar **gs_args, **alpha_args = NULL;
+ gchar **envp;
+ gint pin, pout, perr;
+ gint argc = 0, i;
+ GError *error = NULL;
+
+ if (!gs->gs_filename)
return;
+
+ ps_interpreter_stop (gs);
+
+ dir = g_path_get_dirname (gs->gs_filename);
+
+ /* set up the args... */
+ gs_path = g_find_program_in_path ("gs");
+ gs_args = g_strsplit (gs_path, " ", NUM_GS_ARGS);
+ g_free (gs_path);
+ for (i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
+ argv[argc] = gs_args[i];
}
- ps_new = g_new0 (struct record_list, 1);
- ps_new->fp = gs->gs_psfile;
- ps_new->begin = begin;
- ps_new->len = len;
- ps_new->seek_needed = TRUE;
- ps_new->close = close;
- ps_new->next = NULL;
+ alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
+ for (i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
+ argv[argc] = alpha_args[i];
+ }
- if (gs->input_buffer == NULL) {
- gs->input_buffer = g_malloc(MAX_BUFSIZE);
+ argv[argc++] = "-dNOPAUSE";
+ argv[argc++] = "-dQUIET";
+ argv[argc++] = "-dSAFER";
+
+ if (gs->send_filename_to_gs) {
+ argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
+ argv[argc++] = "-c";
+ argv[argc++] = "quit";
+ } else {
+ argv[argc++] = "-";
}
- if (gs->ps_input == NULL) {
- gs->input_buffer_ptr = gs->input_buffer;
- gs->bytes_left = len;
- gs->buffer_bytes_left = 0;
- gs->ps_input = ps_new;
- gs->interpreter_input_id = gdk_input_add
- (gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
+ argv[argc++] = NULL;
+
+ gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld;DISPLAY=%s",
+ gdk_x11_drawable_get_xid (gs->pstarget),
+ gdk_x11_drawable_get_xid (gs->bpixmap),
+ gdk_display_get_name (gdk_drawable_get_display (gs->pstarget)));
+ envp = g_strsplit (gv_env, ";", 2);
+ g_free (gv_env);
+
+ if (g_spawn_async_with_pipes (dir, argv, NULL, 0,
+ (GSpawnChildSetupFunc)setup_interpreter_env, envp,
+ &(gs->interpreter_pid),
+ &pin, &pout, &perr,
+ &error)) {
+ GIOFlags flags;
+
+ gs->interpreter_input = g_io_channel_unix_new (pin);
+ g_io_channel_set_encoding (gs->interpreter_input, NULL, NULL);
+ flags = g_io_channel_get_flags (gs->interpreter_input);
+ g_io_channel_set_flags (gs->interpreter_input,
+ flags | G_IO_FLAG_NONBLOCK, NULL);
+
+
+ gs->interpreter_output = g_io_channel_unix_new (pout);
+ flags = g_io_channel_get_flags (gs->interpreter_output);
+ g_io_channel_set_flags (gs->interpreter_output,
+ flags | G_IO_FLAG_NONBLOCK, NULL);
+ gs->interpreter_output_id =
+ g_io_add_watch (gs->interpreter_output, G_IO_IN,
+ (GIOFunc)ps_interpreter_output,
+ gs);
+
+ gs->interpreter_err = g_io_channel_unix_new (perr);
+ flags = g_io_channel_get_flags (gs->interpreter_err);
+ g_io_channel_set_flags (gs->interpreter_err,
+ flags | G_IO_FLAG_NONBLOCK, NULL);
+ gs->interpreter_error_id =
+ g_io_add_watch (gs->interpreter_err, G_IO_IN,
+ (GIOFunc)ps_interpreter_error,
+ gs);
} else {
- struct record_list *p = gs->ps_input;
- while (p->next != NULL) {
- p = p->next;
- }
- p->next = ps_new;
+ g_warning (error->message);
+ g_error_free (error);
}
+
+ g_free (dir);
+ g_strfreev (envp);
+ g_strfreev (gs_args);
+ g_strfreev (alpha_args);
}
static void
-setup_pixmap (PSDocument *gs, int page, double scale, int rotation)
+ps_interpreter_stop (PSDocument *gs)
{
- GdkGC *fill;
- GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
- GdkColormap *colormap;
- double width, height;
- int pixmap_width, pixmap_height;
-
- if (gs->pstarget == NULL)
- return;
+ if (gs->interpreter_pid > 0) {
+ gint status = 0;
+
+ kill (gs->interpreter_pid, SIGTERM);
+ while ((wait (&status) == -1) && (errno == EINTR));
+ g_spawn_close_pid (gs->interpreter_pid);
+ gs->interpreter_pid = -1;
+
+ if (status == 1) {
+ gs->gs_status = _("Interpreter failed.");
+ }
+ }
- ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
+ if (gs->interpreter_input) {
+ g_io_channel_unref (gs->interpreter_input);
+ gs->interpreter_input = NULL;
- if (rotation == 90 || rotation == 270) {
- pixmap_height = width * scale + 0.5;
- pixmap_width = height * scale + 0.5;
- } else {
- pixmap_width = width * scale + 0.5;
- pixmap_height = height * scale + 0.5;
+ if (gs->interpreter_input_id > 0) {
+ g_source_remove (gs->interpreter_input_id);
+ gs->interpreter_input_id = 0;
+ }
+
+ if (gs->ps_input) {
+ g_queue_foreach (gs->ps_input, (GFunc)ps_section_free, NULL);
+ g_queue_free (gs->ps_input);
+ gs->ps_input = g_queue_new ();
+ }
}
- if(gs->bpixmap) {
- int w, h;
+ if (gs->interpreter_output) {
+ g_io_channel_unref (gs->interpreter_output);
+ gs->interpreter_output = NULL;
- gdk_drawable_get_size (gs->bpixmap, &w, &h);
+ if (gs->interpreter_output_id > 0) {
+ g_source_remove (gs->interpreter_output_id);
+ gs->interpreter_output_id = 0;
+ }
+ }
- if (pixmap_width != w || h != pixmap_height) {
- gdk_drawable_unref (gs->bpixmap);
- gs->bpixmap = NULL;
- stop_interpreter (gs);
+ if (gs->interpreter_err) {
+ g_io_channel_unref (gs->interpreter_err);
+ gs->interpreter_err = NULL;
+
+ if (gs->interpreter_error_id > 0) {
+ g_source_remove (gs->interpreter_error_id);
+ gs->interpreter_error_id = 0;
}
}
- if (!gs->bpixmap) {
+ gs->busy = FALSE;
+}
- fill = gdk_gc_new (gs->pstarget);
- colormap = gdk_drawable_get_colormap (gs->pstarget);
- gdk_color_alloc (colormap, &white);
- gdk_gc_set_foreground (fill, &white);
- gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
- pixmap_height, -1);
- gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
- 0, 0, pixmap_width, pixmap_height);
- }
+static void
+ps_interpreter_failed (PSDocument *gs, const char *msg)
+{
+ if (msg)
+ g_warning (msg);
+
+ push_pixbuf (gs);
+ ps_interpreter_stop (gs);
}
-#define DEFAULT_PAGE_SIZE 1
+static gboolean
+ps_interpreter_is_ready (PSDocument *gs)
+{
+ return (gs->interpreter_pid != -1 &&
+ !gs->busy &&
+ (g_queue_is_empty (gs->ps_input)));
+}
-static void
-get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
+/* EvDocumentIface */
+/*
+ * Decompress gs->gs_filename if necessary
+ * Set gs->filename_unc to the name of the uncompressed file or NULL.
+ * Error reporting via signal 'interpreter_message'
+ * Return name of input file to use or NULL on error..
+ */
+static const gchar *
+check_filecompressed (PSDocument * gs)
{
- gint new_llx = 0;
- gint new_lly = 0;
- gint new_urx = 0;
- gint new_ury = 0;
- GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
- int new_pagesize = -1;
+ FILE *file;
+ gchar buf[1024];
+ gchar *filename, *filename_unc, *filename_err, *cmdline;
+ const gchar *cmd;
+ int fd;
- g_return_if_fail (PS_IS_DOCUMENT (gs));
+ cmd = NULL;
- if (new_pagesize == -1) {
- new_pagesize = DEFAULT_PAGE_SIZE;
- if (gs->doc) {
- /* If we have a document:
- * We use -- the page size (if specified)
- * or the doc. size (if specified)
- * or the page bbox (if specified)
- * or the bounding box
- */
- if ((page >= 0) && (gs->doc->numpages > page) &&
- (gs->doc->pages) && (gs->doc->pages[page].size)) {
- new_pagesize = gs->doc->pages[page].size - gs->doc->size;
- } else if (gs->doc->default_page_size != NULL) {
- new_pagesize = gs->doc->default_page_size - gs->doc->size;
- } else if ((page >= 0) &&
- (gs->doc->numpages > page) &&
+ if ((file = fopen(gs->gs_filename, "r")) &&
+ (fread (buf, sizeof(gchar), 3, file) == 3)) {
+ if ((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
+ /* file is gzipped or compressed */
+ cmd = gtk_gs_defaults_get_ungzip_cmd ();
+ } else if (strncmp (buf, "BZh", 3) == 0) {
+ /* file is compressed with bzip2 */
+ cmd = gtk_gs_defaults_get_unbzip2_cmd ();
+ }
+ }
+
+ if (NULL != file)
+ fclose(file);
+
+ if (!cmd)
+ return gs->gs_filename;
+
+ /* do the decompression */
+ filename = g_shell_quote (gs->gs_filename);
+ filename_unc = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
+ if ((fd = g_mkstemp (filename_unc)) < 0) {
+ g_free (filename_unc);
+ g_free (filename);
+ return NULL;
+ }
+ close (fd);
+
+ filename_err = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
+ if ((fd = g_mkstemp (filename_err)) < 0) {
+ g_free (filename_err);
+ g_free (filename_unc);
+ g_free (filename);
+ return NULL;
+ }
+ close (fd);
+
+ cmdline = g_strdup_printf ("%s %s >%s 2>%s", cmd,
+ filename, filename_unc, filename_err);
+
+ if (system (cmdline) == 0 &&
+ g_file_test (filename_unc, G_FILE_TEST_IS_REGULAR)) {
+ /* sucessfully uncompressed file */
+ gs->gs_filename_unc = filename_unc;
+ } else {
+ gchar *filename_dsp;
+ gchar *msg;
+
+ /* report error */
+ filename_dsp = g_filename_display_name (gs->gs_filename);
+ msg = g_strdup_printf (_("Error while decompressing file “%s”:\n"),
+ filename_dsp);
+ g_free (filename_dsp);
+
+ ps_interpreter_failed (gs, msg);
+ g_free (msg);
+ g_unlink (filename_unc);
+ g_free (filename_unc);
+ filename_unc = NULL;
+ }
+
+ g_unlink (filename_err);
+ g_free (filename_err);
+ g_free (cmdline);
+ g_free (filename);
+
+ return filename_unc;
+}
+
+static gboolean
+document_load (PSDocument *gs, const gchar *fname)
+{
+ const gchar *filename;
+
+ if (fname == NULL) {
+ gs->gs_status = "";
+ return FALSE;
+ }
+
+ /* prepare this document */
+ gs->structured_doc = FALSE;
+ gs->send_filename_to_gs = TRUE;
+ gs->gs_filename = g_strdup (fname);
+
+ /*
+ * We need to make sure that the file is loadable/exists!
+ * otherwise we want to exit without loading new stuff...
+ */
+ if (!g_file_test (fname, G_FILE_TEST_IS_REGULAR)) {
+ gchar *filename_dsp;
+ gchar *msg;
+
+ filename_dsp = g_filename_display_name (fname);
+ msg = g_strdup_printf (_("Cannot open file “%s”.\n"), filename_dsp);
+ g_free (filename_dsp);
+
+ ps_interpreter_failed (gs, msg);
+ g_free (msg);
+ gs->gs_status = _("File is not readable.");
+
+ return FALSE;
+ }
+
+ filename = check_filecompressed (gs);
+
+ if (!filename || (gs->gs_psfile = fopen (filename, "r")) == NULL) {
+ ps_interpreter_failed (gs, NULL);
+ return FALSE;
+ }
+
+ /* we grab the vital statistics!!! */
+ gs->doc = psscan (gs->gs_psfile, TRUE, filename);
+
+ if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
+ (gs->doc->epsf && gs->doc->numpages > 1)) {
+ gs->structured_doc = TRUE;
+ gs->send_filename_to_gs = FALSE;
+ }
+
+ gs->gs_status = _("Document loaded.");
+
+ return TRUE;
+}
+
+static gboolean
+ps_document_load (EvDocument *document,
+ const char *uri,
+ GError **error)
+{
+ char *filename;
+ char *gs_path;
+ gboolean result;
+
+ filename = g_filename_from_uri (uri, NULL, error);
+ if (!filename)
+ return FALSE;
+
+ gs_path = g_find_program_in_path ("gs");
+ if (!gs_path) {
+ gchar *filename_dsp;
+
+ filename_dsp = g_filename_display_name (filename);
+ g_set_error (error,
+ G_FILE_ERROR,
+ G_FILE_ERROR_NOENT,
+ _("Failed to load document “%s”. Ghostscript interpreter was not found in path"),
+ filename);
+ g_free (filename_dsp);
+ g_free (filename);
+
+ return FALSE;
+ }
+
+ result = document_load (PS_DOCUMENT (document), filename);
+ if (!result) {
+ gchar *filename_dsp;
+
+ filename_dsp = g_filename_display_name (filename);
+ g_set_error (error, G_FILE_ERROR,
+ G_FILE_ERROR_FAILED,
+ _("Failed to load document “%s”"),
+ filename_dsp);
+ g_free (filename_dsp);
+ }
+
+ g_free (gs_path);
+ g_free (filename);
+
+ return result;
+}
+
+static gboolean
+save_document (PSDocument *document, const char *filename)
+{
+ gboolean result = TRUE;
+ GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
+ FILE *f, *src_file;
+ gchar *buf;
+
+ src_file = fopen (PS_DOCUMENT_GET_PS_FILE(document), "r");
+ if (src_file) {
+ struct stat stat_rec;
+
+ if (stat (PS_DOCUMENT_GET_PS_FILE(document), &stat_rec) == 0) {
+ pscopy (src_file, sink, 0, stat_rec.st_size - 1);
+ }
+
+ fclose (src_file);
+ }
+
+ buf = gtk_gs_doc_sink_get_buffer (sink);
+ if (buf == NULL) {
+ return FALSE;
+ }
+
+ f = fopen (filename, "w");
+ if (f) {
+ fputs (buf, f);
+ fclose (f);
+ } else {
+ result = FALSE;
+ }
+
+ g_free (buf);
+ gtk_gs_doc_sink_free (sink);
+ g_free (sink);
+
+ return result;
+}
+
+static gboolean
+save_page_list (PSDocument *document, int *page_list, const char *filename)
+{
+ gboolean result = TRUE;
+ GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
+ FILE *f;
+ gchar *buf;
+
+ pscopydoc (sink, PS_DOCUMENT_GET_PS_FILE(document),
+ document->doc, page_list);
+
+ buf = gtk_gs_doc_sink_get_buffer (sink);
+
+ f = fopen (filename, "w");
+ if (f) {
+ fputs (buf, f);
+ fclose (f);
+ } else {
+ result = FALSE;
+ }
+
+ g_free (buf);
+ gtk_gs_doc_sink_free (sink);
+ g_free (sink);
+
+ return result;
+}
+
+static gboolean
+ps_document_save (EvDocument *document,
+ const char *uri,
+ GError **error)
+{
+ PSDocument *ps = PS_DOCUMENT (document);
+ gboolean result;
+ char *filename;
+
+ filename = g_filename_from_uri (uri, NULL, error);
+ if (!filename)
+ return FALSE;
+
+ result = save_document (ps, filename);
+
+ g_free (filename);
+
+ return result;
+}
+
+static int
+ps_document_get_n_pages (EvDocument *document)
+{
+ PSDocument *ps = PS_DOCUMENT (document);
+
+ if (!ps->gs_filename || !ps->doc) {
+ return -1;
+ }
+
+ return ps->structured_doc ? ps->doc->numpages : 1;
+}
+
+#define DEFAULT_PAGE_SIZE 1
+
+static void
+get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
+{
+ gint new_llx = 0;
+ gint new_lly = 0;
+ gint new_urx = 0;
+ gint new_ury = 0;
+ GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
+ int new_pagesize = -1;
+
+ if (new_pagesize == -1) {
+ new_pagesize = DEFAULT_PAGE_SIZE;
+ if (gs->doc) {
+ /* If we have a document:
+ * We use -- the page size (if specified)
+ * or the doc. size (if specified)
+ * or the page bbox (if specified)
+ * or the bounding box
+ */
+ if ((page >= 0) && (gs->doc->numpages > page) &&
+ (gs->doc->pages) && (gs->doc->pages[page].size)) {
+ new_pagesize = gs->doc->pages[page].size - gs->doc->size;
+ } else if (gs->doc->default_page_size != NULL) {
+ new_pagesize = gs->doc->default_page_size - gs->doc->size;
+ } else if ((page >= 0) &&
+ (gs->doc->numpages > page) &&
(gs->doc->pages) &&
(gs->doc->pages[page].boundingbox[URX] >
gs->doc->pages[page].boundingbox[LLX]) &&
}
static void
-setup_page (PSDocument *gs, int page, double scale, int rotation)
+ps_document_get_page_size (EvDocument *document,
+ int page,
+ double *width,
+ double *height)
{
- gchar *buf;
- char scaled_dpi[G_ASCII_DTOSTR_BUF_SIZE];
+ PSDocument *gs = PS_DOCUMENT (document);
int urx, ury, llx, lly;
get_page_box (gs, page, &urx, &ury, &llx, &lly);
- g_ascii_dtostr (scaled_dpi, G_ASCII_DTOSTR_BUF_SIZE, 72.0 * scale);
- buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
- 0L, rotation, llx, lly, urx, ury,
- scaled_dpi, scaled_dpi,
- 0, 0, 0, 0);
-
- gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
- 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
- g_free (buf);
-
- gdk_flush ();
-}
-
-static void
-close_pipe (int p[2])
-{
- if (p[0] != -1) {
- close (p[0]);
- }
- if (p[1] != -1) {
- close (p[1]);
- }
-}
-
-static gboolean
-is_interpreter_ready (PSDocument *gs)
-{
- return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
-}
-
-static void
-output (gpointer data, gint source, GdkInputCondition condition)
-{
- char buf[MAX_BUFSIZE + 1];
- guint bytes = 0;
- PSDocument *gs = PS_DOCUMENT(data);
-
- if (source == gs->interpreter_output) {
- bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
- if (bytes == 0) { /* EOF occurred */
- close (gs->interpreter_output);
- gs->interpreter_output = -1;
- gdk_input_remove (gs->interpreter_output_id);
- return;
- } else if (bytes == -1) {
- /* trouble... */
- interpreter_failed (gs, NULL);
- return;
- }
- if (gs->interpreter_err == -1) {
- interpreter_failed (gs, NULL);
- }
- } else if (source == gs->interpreter_err) {
- bytes = read (gs->interpreter_err, buf, MAX_BUFSIZE);
- if (bytes == 0) { /* EOF occurred */
- close (gs->interpreter_err);
- gs->interpreter_err = -1;
- gdk_input_remove (gs->interpreter_error_id);
- return;
- } else if (bytes == -1) {
- /* trouble... */
- interpreter_failed (gs, NULL);
- return;
- }
- if (gs->interpreter_output == -1) {
- interpreter_failed(gs, NULL);
- }
+ if (width) {
+ *width = (urx - llx) + 0.5;
}
- if (bytes > 0) {
- buf[bytes] = '\0';
- printf ("%s", buf);
+ if (height) {
+ *height = (ury - lly) + 0.5;
}
}
-static void
-catchPipe (int i)
+static gboolean
+ps_document_can_get_text (EvDocument *document)
{
- broken_pipe = True;
+ return FALSE;
}
-static void
-input(gpointer data, gint source, GdkInputCondition condition)
+static EvDocumentInfo *
+ps_document_get_info (EvDocument *document)
{
- PSDocument *gs = PS_DOCUMENT(data);
- int bytes_written;
- void (*oldsig) (int);
- oldsig = signal(SIGPIPE, catchPipe);
-
- do {
- if (gs->buffer_bytes_left == 0) {
- /* Get a new section if required */
- if (gs->ps_input && gs->bytes_left == 0) {
- struct record_list *ps_old = gs->ps_input;
- gs->ps_input = ps_old->next;
- if (ps_old->close && NULL != ps_old->fp)
- fclose (ps_old->fp);
- g_free (ps_old);
- }
-
- /* Have to seek at the beginning of each section */
- if (gs->ps_input && gs->ps_input->seek_needed) {
- fseek (gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
- gs->ps_input->seek_needed = FALSE;
- gs->bytes_left = gs->ps_input->len;
- }
+ EvDocumentInfo *info;
+ PSDocument *ps = PS_DOCUMENT (document);
+ int urx, ury, llx, lly;
- if (gs->bytes_left > MAX_BUFSIZE) {
- gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
- MAX_BUFSIZE, gs->ps_input->fp);
- } else if (gs->bytes_left > 0) {
- gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
- gs->bytes_left, gs->ps_input->fp);
- } else {
- gs->buffer_bytes_left = 0;
- }
- if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
- interpreter_failed (gs, NULL); /* Error occurred */
- }
- gs->input_buffer_ptr = gs->input_buffer;
- gs->bytes_left -= gs->buffer_bytes_left;
- }
+ info = g_new0 (EvDocumentInfo, 1);
+ info->fields_mask = EV_DOCUMENT_INFO_TITLE |
+ EV_DOCUMENT_INFO_FORMAT |
+ EV_DOCUMENT_INFO_CREATOR |
+ EV_DOCUMENT_INFO_N_PAGES |
+ EV_DOCUMENT_INFO_PAPER_SIZE;
- if (gs->buffer_bytes_left > 0) {
- bytes_written = write (gs->interpreter_input,
- gs->input_buffer_ptr, gs->buffer_bytes_left);
-
- if (broken_pipe) {
- interpreter_failed (gs, g_strdup(_("Broken pipe.")));
- broken_pipe = FALSE;
- interpreter_failed (gs, NULL);
- } else if (bytes_written == -1) {
- if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
- interpreter_failed (gs, NULL); /* Something bad happened */
- }
- } else {
- gs->buffer_bytes_left -= bytes_written;
- gs->input_buffer_ptr += bytes_written;
- }
- }
- } while (gs->ps_input && gs->buffer_bytes_left == 0);
+ info->title = g_strdup (ps->doc->title);
+ info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
+ : g_strdup (_("PostScript"));
+ info->creator = g_strdup (ps->doc->creator);
+ info->n_pages = ev_document_get_n_pages (document);
+
+ get_page_box (PS_DOCUMENT (document), 0, &urx, &ury, &llx, &lly);
- signal (SIGPIPE, oldsig);
+ info->paper_width = (urx - llx) / 72.0f * 25.4f;
+ info->paper_height = (ury - lly) / 72.0f * 25.4f;
- if (gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
- if (gs->interpreter_input_id != 0) {
- gdk_input_remove (gs->interpreter_input_id);
- gs->interpreter_input_id = 0;
- }
- }
+ return info;
}
-static int
-start_interpreter (PSDocument *gs)
+static void
+ps_document_document_iface_init (EvDocumentIface *iface)
{
- int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
- int std_out[2]; /* pipe from interp stdout */
- int std_err[2]; /* pipe from interp stderr */
-
-#define NUM_ARGS 100
-#define NUM_GS_ARGS (NUM_ARGS - 20)
-#define NUM_ALPHA_ARGS 10
-
- char *argv[NUM_ARGS], *dir, *gv_env, *gs_path;
- char **gs_args, **alpha_args = NULL;
- char **gv_env_vars = NULL;
- int argc = 0, i;
-
- if(!gs->gs_filename)
- return 0;
-
- stop_interpreter(gs);
-
- /* set up the args... */
- gs_path = g_find_program_in_path ("gs");
- gs_args = g_strsplit (gs_path, " ", NUM_GS_ARGS);
- g_free (gs_path);
- for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
- argv[argc] = gs_args[i];
- }
-
- alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
- for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
- argv[argc] = alpha_args[i];
- }
-
- argv[argc++] = "-dNOPAUSE";
- argv[argc++] = "-dQUIET";
- argv[argc++] = "-dSAFER";
-
- /* set up the pipes */
- if (gs->send_filename_to_gs) {
- argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
- argv[argc++] = "-c";
- argv[argc++] = "quit";
- } else {
- argv[argc++] = "-";
- }
-
- argv[argc++] = NULL;
-
- if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
- if (pipe (std_in) == -1) {
- g_critical ("Unable to open pipe to Ghostscript.");
- return -1;
- }
- }
-
- if (pipe (std_out) == -1) {
- close_pipe (std_in);
- return -1;
- }
-
- if (pipe(std_err) == -1) {
- close_pipe (std_in);
- close_pipe (std_out);
- return -1;
- }
-
- gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld;DISPLAY=%s",
- gdk_x11_drawable_get_xid (gs->pstarget),
- gdk_x11_drawable_get_xid (gs->bpixmap),
- gdk_display_get_name (gdk_drawable_get_display (gs->pstarget)));
-
- gs->interpreter_pid = fork ();
- switch (gs->interpreter_pid) {
- case -1: /* error */
- close_pipe (std_in);
- close_pipe (std_out);
- close_pipe (std_err);
- return -2;
- break;
- case 0: /* child */
- close (std_out[0]);
- dup2 (std_out[1], 1);
- close (std_out[1]);
-
- close (std_err[0]);
- dup2 (std_err[1], 2);
- close (std_err[1]);
-
- if (!gs->reading_from_pipe) {
- if (gs->send_filename_to_gs) {
- int stdinfd;
- /* just in case gs tries to read from stdin */
- stdinfd = open("/dev/null", O_RDONLY);
- if (stdinfd != 0) {
- dup2(stdinfd, 0);
- close(stdinfd);
- }
- } else {
- close (std_in[1]);
- dup2 (std_in[0], 0);
- close (std_in[0]);
- }
- }
-
- gv_env_vars = g_strsplit (gv_env, ";", -1);
- g_free (gv_env);
- for (i = 0; gv_env_vars[i]; i++) {
- putenv (gv_env_vars[i]);
- }
-
- /* change to directory where the input file is. This helps
- * with postscript-files which include other files using
- * a relative path */
- dir = g_path_get_dirname (gs->gs_filename);
- chdir (dir);
- g_free (dir);
-
- execvp (argv[0], argv);
-
- /* Notify error */
- g_critical ("Unable to execute [%s]\n", argv[0]);
- g_strfreev (gs_args);
- g_strfreev (alpha_args);
- g_strfreev (gv_env_vars);
- _exit (1);
- break;
- default: /* parent */
- if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
- int result;
- close (std_in[0]);
- /* use non-blocking IO for pipe to ghostscript */
- result = fcntl (std_in[1], F_GETFL, 0);
- fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
- gs->interpreter_input = std_in[1];
- } else {
- gs->interpreter_input = -1;
- }
- close (std_out[1]);
-
- gs->interpreter_output = std_out[0];
- close (std_err[1]);
- gs->interpreter_err = std_err[0];
- gs->interpreter_output_id =
- gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
- gs->interpreter_error_id =
- gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
-
- g_free (gv_env);
- g_free (gs_args);
- g_free (alpha_args);
- break;
- }
-
- return TRUE;
+ iface->load = ps_document_load;
+ iface->save = ps_document_save;
+ iface->can_get_text = ps_document_can_get_text;
+ iface->get_n_pages = ps_document_get_n_pages;
+ iface->get_page_size = ps_document_get_page_size;
+ iface->get_info = ps_document_get_info;
}
+/* EvAsyncRendererIface */
static void
-stop_interpreter(PSDocument * gs)
+setup_page (PSDocument *gs, int page, double scale, int rotation)
{
- if (gs->interpreter_pid > 0) {
- int status = 0;
- kill (gs->interpreter_pid, SIGTERM);
- while ((wait(&status) == -1) && (errno == EINTR));
- gs->interpreter_pid = -1;
- if (status == 1) {
- gs->gs_status = _("Interpreter failed.");
- }
- }
-
- if (gs->interpreter_input >= 0) {
- close (gs->interpreter_input);
- gs->interpreter_input = -1;
- if (gs->interpreter_input_id != 0) {
- gdk_input_remove(gs->interpreter_input_id);
- gs->interpreter_input_id = 0;
- }
- while (gs->ps_input) {
- struct record_list *ps_old = gs->ps_input;
- gs->ps_input = gs->ps_input->next;
- if (ps_old->close && NULL != ps_old->fp)
- fclose (ps_old->fp);
- g_free (ps_old);
- }
- }
-
- if (gs->interpreter_output >= 0) {
- close (gs->interpreter_output);
- gs->interpreter_output = -1;
- if (gs->interpreter_output_id) {
- gdk_input_remove (gs->interpreter_output_id);
- gs->interpreter_output_id = 0;
- }
- }
-
- if (gs->interpreter_err >= 0) {
- close (gs->interpreter_err);
- gs->interpreter_err = -1;
- if (gs->interpreter_error_id) {
- gdk_input_remove (gs->interpreter_error_id);
- gs->interpreter_error_id = 0;
- }
- }
-
- gs->busy = FALSE;
-}
+ gchar *buf;
+ char scaled_dpi[G_ASCII_DTOSTR_BUF_SIZE];
+ int urx, ury, llx, lly;
+ PSDocumentClass *gs_class;
-/* If file exists and is a regular file then return its length, else -1 */
-static gint
-file_length (const gchar * filename)
-{
- struct stat stat_rec;
+ gs_class = PS_DOCUMENT_GET_CLASS (gs);
- if (filename && (stat (filename, &stat_rec) == 0) && S_ISREG (stat_rec.st_mode))
- return stat_rec.st_size;
- else
- return -1;
-}
+ get_page_box (gs, page, &urx, &ury, &llx, &lly);
+ g_ascii_dtostr (scaled_dpi, G_ASCII_DTOSTR_BUF_SIZE, 72.0 * scale);
-/* Test if file exists, is a regular file and its length is > 0 */
-static gboolean
-file_readable(const char *filename)
-{
- return (file_length (filename) > 0);
+ buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
+ 0L, rotation, llx, lly, urx, ury,
+ scaled_dpi, scaled_dpi,
+ 0, 0, 0, 0);
+
+ gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
+ 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen (buf));
+ g_free (buf);
+
+ gdk_flush ();
}
-/*
- * Decompress gs->gs_filename if necessary
- * Set gs->filename_unc to the name of the uncompressed file or NULL.
- * Error reporting via signal 'interpreter_message'
- * Return name of input file to use or NULL on error..
- */
-static gchar *
-check_filecompressed (PSDocument * gs)
+static void
+setup_pixmap (PSDocument *gs, int page, double scale, int rotation)
{
- FILE *file;
- gchar buf[1024];
- gchar *filename, *filename_unc, *filename_err, *cmdline;
- const gchar *cmd;
- int fd;
+ GdkGC *fill;
+ GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
+ GdkColormap *colormap;
+ double width, height;
+ int pixmap_width, pixmap_height;
+
+ if (gs->pstarget == NULL)
+ return;
- cmd = NULL;
+ ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
- if ((file = fopen(gs->gs_filename, "r")) &&
- (fread (buf, sizeof(gchar), 3, file) == 3)) {
- if ((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
- /* file is gzipped or compressed */
- cmd = gtk_gs_defaults_get_ungzip_cmd ();
- } else if (strncmp (buf, "BZh", 3) == 0) {
- /* file is compressed with bzip2 */
- cmd = gtk_gs_defaults_get_unbzip2_cmd ();
- }
+ if (rotation == 90 || rotation == 270) {
+ pixmap_height = width * scale + 0.5;
+ pixmap_width = height * scale + 0.5;
+ } else {
+ pixmap_width = width * scale + 0.5;
+ pixmap_height = height * scale + 0.5;
}
- if (NULL != file)
- fclose(file);
-
- if (!cmd)
- return gs->gs_filename;
+ if (gs->bpixmap) {
+ gint w, h;
- /* do the decompression */
- filename = g_shell_quote (gs->gs_filename);
- filename_unc = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
- if ((fd = mkstemp (filename_unc)) < 0) {
- g_free (filename_unc);
- g_free (filename);
- return NULL;
- }
- close (fd);
+ gdk_drawable_get_size (gs->bpixmap, &w, &h);
- filename_err = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
- if ((fd = mkstemp(filename_err)) < 0) {
- g_free (filename_err);
- g_free (filename_unc);
- g_free (filename);
- return NULL;
+ if (pixmap_width != w || h != pixmap_height) {
+ g_object_unref (gs->bpixmap);
+ gs->bpixmap = NULL;
+ ps_interpreter_stop (gs);
+ }
}
- close (fd);
-
- cmdline = g_strdup_printf ("%s %s >%s 2>%s", cmd,
- filename, filename_unc, filename_err);
- if (system (cmdline) == 0 &&
- file_readable (filename_unc) &&
- file_length (filename_err) == 0) {
- /* sucessfully uncompressed file */
- gs->gs_filename_unc = filename_unc;
- } else {
- gchar *filename_dsp;
- gchar *msg;
- /* report error */
- filename_dsp = g_filename_display_name (gs->gs_filename);
- msg = g_strdup_printf (_("Error while decompressing file “%s”:\n"), filename_dsp);
- g_free (filename_dsp);
-
- interpreter_failed (gs, msg);
- g_free (msg);
- unlink (filename_unc);
- g_free (filename_unc);
- filename_unc = NULL;
+ if (!gs->bpixmap) {
+ fill = gdk_gc_new (gs->pstarget);
+ colormap = gdk_drawable_get_colormap (gs->pstarget);
+ gdk_colormap_alloc_color (colormap, &white, FALSE, TRUE);
+ gdk_gc_set_foreground (fill, &white);
+ gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
+ pixmap_height, -1);
+ gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
+ 0, 0, pixmap_width, pixmap_height);
}
-
- unlink (filename_err);
- g_free (filename_err);
- g_free (cmdline);
- g_free (filename);
-
- return filename_unc;
}
-static gint
-ps_document_enable_interpreter(PSDocument *gs)
+static void
+push_pixbuf (PSDocument *gs)
{
- g_return_val_if_fail (PS_IS_DOCUMENT (gs), FALSE);
+ GdkColormap *cmap;
+ GdkPixbuf *pixbuf;
+ gint width, height;
- if (!gs->gs_filename)
- return 0;
+ if (gs->pstarget == NULL)
+ return;
- return start_interpreter (gs);
+ cmap = gdk_drawable_get_colormap (gs->pstarget);
+ gdk_drawable_get_size (gs->bpixmap, &width, &height);
+ pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
+ 0, 0, 0, 0,
+ width, height);
+ g_signal_emit_by_name (gs, "render_finished", pixbuf);
+ g_object_unref (pixbuf);
}
static gboolean
-document_load (PSDocument *gs, const gchar *fname)
+ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
{
- g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
+ PSDocument *gs = (PSDocument *) data;
+ PSDocumentClass *gs_class;
- if (fname == NULL) {
- gs->gs_status = "";
+ if (event->type != GDK_CLIENT_EVENT)
return FALSE;
- }
-
- /* prepare this document */
- gs->structured_doc = FALSE;
- gs->send_filename_to_gs = TRUE;
- gs->gs_filename = g_strdup (fname);
- if ((gs->reading_from_pipe = (strcmp (fname, "-") == 0))) {
- gs->send_filename_to_gs = FALSE;
- } else {
- /*
- * We need to make sure that the file is loadable/exists!
- * otherwise we want to exit without loading new stuff...
- */
- gchar *filename = NULL;
+ gs_class = PS_DOCUMENT_GET_CLASS (gs);
+
+ gs->message_window = event->client.data.l[0];
- if (!file_readable(fname)) {
- gchar *filename_dsp;
- gchar *msg;
+ if (event->client.message_type == gs_class->page_atom) {
+ gs->busy = FALSE;
- filename_dsp = g_filename_display_name (fname);
- msg = g_strdup_printf (_("Cannot open file “%s”.\n"), filename_dsp);
- g_free (filename_dsp);
-
- interpreter_failed (gs, msg);
- g_free (msg);
- gs->gs_status = _("File is not readable.");
- } else {
- filename = check_filecompressed(gs);
- }
+ push_pixbuf (gs);
+ }
- if (!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
- interpreter_failed (gs, NULL);
- return FALSE;
- }
+ return TRUE;
+}
- /* we grab the vital statistics!!! */
- gs->doc = psscan(gs->gs_psfile, TRUE, filename);
+static void
+send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
+{
+ PSSection *ps_new;
- if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
- (gs->doc->epsf && gs->doc->numpages > 1)) {
- gs->structured_doc = TRUE;
- gs->send_filename_to_gs = FALSE;
- }
+ if (!gs->interpreter_input) {
+ g_critical ("No pipe to gs: error in send_ps().");
+ return;
}
- gs->gs_status = _("Document loaded.");
+ ps_new = g_new0 (PSSection, 1);
+ ps_new->fp = gs->gs_psfile;
+ ps_new->begin = begin;
+ ps_new->len = len;
+ ps_new->seek_needed = TRUE;
+ ps_new->close = close;
- return TRUE;
+ if (gs->input_buffer == NULL) {
+ gs->input_buffer = g_malloc (MAX_BUFSIZE);
+ }
+
+ if (g_queue_is_empty (gs->ps_input)) {
+ gs->input_buffer_ptr = gs->input_buffer;
+ gs->bytes_left = len;
+ gs->buffer_bytes_left = 0;
+ g_queue_push_head (gs->ps_input, ps_new);
+ gs->interpreter_input_id =
+ g_io_add_watch (gs->interpreter_input,
+ G_IO_OUT/* | G_IO_HUP | G_IO_ERR | G_IO_NVAL*/,
+ (GIOFunc)ps_interpreter_input,
+ gs);
+ } else {
+ g_queue_push_head (gs->ps_input, ps_new);
+ }
}
-static gboolean
+static void
ps_document_next_page (PSDocument *gs)
{
XEvent event;
GdkScreen *screen;
GdkDisplay *display;
Display *dpy;
+ PSDocumentClass *gs_class;
- g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
- g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
- g_return_val_if_fail (gs->busy != TRUE, FALSE);
+ g_assert (gs->interpreter_pid != 0);
+ g_assert (gs->busy != TRUE);
+ gs_class = PS_DOCUMENT_GET_CLASS (gs);
+
gs->busy = TRUE;
screen = gtk_window_get_screen (GTK_WINDOW (gs->target_window));
XSendEvent (dpy, gs->message_window, FALSE, 0, &event);
gdk_flush ();
gdk_error_trap_pop ();
-
- return TRUE;
}
static gboolean
render_page (PSDocument *gs, int page)
{
- g_return_val_if_fail(gs != NULL, FALSE);
- g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
+ g_assert (gs != NULL);
- if(!gs->gs_filename) {
+ if (!gs->gs_filename) {
return FALSE;
}
if (gs->structured_doc && gs->doc) {
-
- if (is_interpreter_ready (gs)) {
+ if (ps_interpreter_is_ready (gs)) {
ps_document_next_page (gs);
} else {
- ps_document_enable_interpreter (gs);
+ ps_interpreter_start (gs);
send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
}
* case, ggv restarts GS again and the first page is displayed.
*/
- if (!is_interpreter_ready (gs)) {
- ps_document_enable_interpreter(gs);
+ if (!ps_interpreter_is_ready (gs)) {
+ ps_interpreter_start (gs);
}
- ps_document_next_page(gs);
+ ps_document_next_page (gs);
}
return TRUE;
}
-static gboolean
-ps_document_load (EvDocument *document,
- const char *uri,
- GError **error)
-{
- char *filename;
- char *gs_path;
- gboolean result;
-
- filename = g_filename_from_uri (uri, NULL, error);
- if (!filename)
- return FALSE;
-
- gs_path = g_find_program_in_path ("gs");
- if (!gs_path) {
- gchar *filename_dsp;
- filename_dsp = g_filename_display_name (filename);
- g_set_error(error,
- G_FILE_ERROR,
- G_FILE_ERROR_NOENT,
- _("Failed to load document “%s”. Ghostscript interpreter was not found in path"),
- filename);
- g_free (filename_dsp);
- result = FALSE;
- } else {
- result = document_load (PS_DOCUMENT (document), filename);
- if (!result) {
- gchar *filename_dsp;
- filename_dsp = g_filename_display_name (filename);
-
- g_set_error (error, G_FILE_ERROR,
- G_FILE_ERROR_FAILED,
- _("Failed to load document “%s”"),
- filename_dsp);
- g_free (filename_dsp);
- }
- g_free (gs_path);
- }
- g_free (filename);
-
- return result;
-}
-
-static gboolean
-save_document (PSDocument *document, const char *filename)
-{
- gboolean result = TRUE;
- GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
- FILE *f, *src_file;
- gchar *buf;
-
- src_file = fopen (PS_DOCUMENT_GET_PS_FILE(document), "r");
- if (src_file) {
- struct stat stat_rec;
-
- if (stat (PS_DOCUMENT_GET_PS_FILE(document), &stat_rec) == 0) {
- pscopy (src_file, sink, 0, stat_rec.st_size - 1);
- }
-
- fclose (src_file);
- }
-
- buf = gtk_gs_doc_sink_get_buffer (sink);
- if (buf == NULL) {
- return FALSE;
- }
-
- f = fopen (filename, "w");
- if (f) {
- fputs (buf, f);
- fclose (f);
- } else {
- result = FALSE;
- }
-
- g_free (buf);
- gtk_gs_doc_sink_free (sink);
- g_free (sink);
-
- return result;
-}
-
-static gboolean
-save_page_list (PSDocument *document, int *page_list, const char *filename)
-{
- gboolean result = TRUE;
- GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
- FILE *f;
- gchar *buf;
-
- pscopydoc (sink, PS_DOCUMENT_GET_PS_FILE(document),
- document->doc, page_list);
-
- buf = gtk_gs_doc_sink_get_buffer (sink);
-
- f = fopen (filename, "w");
- if (f) {
- fputs (buf, f);
- fclose (f);
- } else {
- result = FALSE;
- }
-
- g_free (buf);
- gtk_gs_doc_sink_free (sink);
- g_free (sink);
-
- return result;
-}
-
-static gboolean
-ps_document_save (EvDocument *document,
- const char *uri,
- GError **error)
-{
- PSDocument *ps = PS_DOCUMENT (document);
- gboolean result;
- char *filename;
-
- filename = g_filename_from_uri (uri, NULL, error);
- if (!filename)
- return FALSE;
-
- result = save_document (ps, filename);
-
- g_free (filename);
-
- return result;
-}
-
-static int
-ps_document_get_n_pages (EvDocument *document)
-{
- PSDocument *ps = PS_DOCUMENT (document);
-
- g_return_val_if_fail (ps != NULL, -1);
-
- if (!ps->gs_filename || !ps->doc) {
- return -1;
- }
-
- return ps->structured_doc ? ps->doc->numpages : 1;
-}
-
-static void
-ps_document_get_page_size (EvDocument *document,
- int page,
- double *width,
- double *height)
-{
- PSDocument *gs = PS_DOCUMENT (document);
- int urx, ury, llx, lly;
-
- get_page_box (gs, page, &urx, &ury, &llx, &lly);
-
- if (width) {
- *width = (urx - llx) + 0.5;
- }
-
- if (height) {
- *height = (ury - lly) + 0.5;
- }
-}
-
-static gboolean
-ps_document_can_get_text (EvDocument *document)
-{
- return FALSE;
-}
-
static void
-ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer, int page, double scale, int rotation)
+ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer,
+ gint page,
+ gdouble scale,
+ gint rotation)
{
PSDocument *gs = PS_DOCUMENT (renderer);
render_page (gs, page);
}
-static EvDocumentInfo *
-ps_document_get_info (EvDocument *document)
-{
- EvDocumentInfo *info;
- PSDocument *ps = PS_DOCUMENT (document);
- int urx, ury, llx, lly;
-
- info = g_new0 (EvDocumentInfo, 1);
- info->fields_mask = EV_DOCUMENT_INFO_TITLE |
- EV_DOCUMENT_INFO_FORMAT |
- EV_DOCUMENT_INFO_CREATOR |
- EV_DOCUMENT_INFO_N_PAGES |
- EV_DOCUMENT_INFO_PAPER_SIZE;
-
- info->title = g_strdup (ps->doc->title);
- info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
- : g_strdup (_("PostScript"));
- info->creator = g_strdup (ps->doc->creator);
- info->n_pages = ev_document_get_n_pages (document);
-
- get_page_box (PS_DOCUMENT (document), 0, &urx, &ury, &llx, &lly);
-
- info->paper_width = (urx - llx) / 72.0f * 25.4f;
- info->paper_height = (ury - lly) / 72.0f * 25.4f;
-
- return info;
-}
-
-static void
-ps_document_document_iface_init (EvDocumentIface *iface)
-{
- iface->load = ps_document_load;
- iface->save = ps_document_save;
- iface->can_get_text = ps_document_can_get_text;
- iface->get_n_pages = ps_document_get_n_pages;
- iface->get_page_size = ps_document_get_page_size;
- iface->get_info = ps_document_get_info;
-}
-
static void
ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
{
iface->render_pixbuf = ps_async_renderer_render_pixbuf;
}
+/* EvFileExporterIface */
static gboolean
ps_document_file_exporter_format_supported (EvFileExporter *exporter,
EvFileExporterFormat format)