]> www.fi.muni.cz Git - evince.git/blobdiff - ps/ps-document.c
Translation updated.
[evince.git] / ps / ps-document.c
index 4b2d4c43e36d23ef37e2466e49f3c8f39f028c9d..34579674c302add2e07d0964c4e537ee323c0638 100644 (file)
 #include "ps-document.h"
 #include "ev-debug.h"
 #include "gsdefaults.h"
-
-#ifdef HAVE_LOCALE_H
-#   include <locale.h>
-#endif
-
-/* if POSIX O_NONBLOCK is not available, use O_NDELAY */
-#if !defined(O_NONBLOCK) && defined(O_NDELAY)
-#   define O_NONBLOCK O_NDELAY
-#endif
+#include "ev-ps-exporter.h"
+#include "ev-async-renderer.h"
 
 #define MAX_BUFSIZE 1024
 
-#define PS_DOCUMENT_IS_COMPRESSED(gs)       (PS_DOCUMENT(gs)->gs_filename_unc != NULL)
-#define PS_DOCUMENT_GET_PS_FILE(gs)         (PS_DOCUMENT_IS_COMPRESSED(gs) ? \
-                                        PS_DOCUMENT(gs)->gs_filename_unc : \
-                                        PS_DOCUMENT(gs)->gs_filename)
-
-GCond* pixbuf_cond = NULL;
-GMutex* pixbuf_mutex = NULL;
-GdkPixbuf *current_pixbuf = NULL;
-
-enum {
-       PROP_0,
-       PROP_TITLE
-};
+#define PS_DOCUMENT_IS_COMPRESSED(gs) (PS_DOCUMENT(gs)->gs_filename_unc != NULL)
+#define PS_DOCUMENT_GET_PS_FILE(gs)   (PS_DOCUMENT_IS_COMPRESSED(gs) ? \
+                                       PS_DOCUMENT(gs)->gs_filename_unc : \
+                                       PS_DOCUMENT(gs)->gs_filename)
 
 /* structure to describe section of file to send to ghostscript */
-struct record_list {
-  FILE *fp;
-  long begin;
-  guint len;
-  gboolean seek_needed;
-  gboolean close;
-  struct record_list *next;
+struct record_list
+{
+       FILE *fp;
+       long begin;
+       guint len;
+       gboolean seek_needed;
+       gboolean close;
+       struct record_list *next;
 };
 
 static gboolean broken_pipe = FALSE;
 
 /* Forward declarations */
-static void ps_document_init(PSDocument * gs);
-static void ps_document_class_init(PSDocumentClass * klass);
-static void ps_document_finalize(GObject * object);
-static void send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close);
-static void close_pipe(int p[2]);
-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 gboolean ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data);
+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_ps_exporter_iface_init      (EvPSExporterIface    *iface);
+static void    ps_async_renderer_iface_init            (EvAsyncRendererIface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (PSDocument, ps_document, G_TYPE_OBJECT,
+                         {
+                                G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
+                                                       ps_document_document_iface_init);
+                                G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
+                                                       ps_document_ps_exporter_iface_init);
+                                G_IMPLEMENT_INTERFACE (EV_TYPE_ASYNC_RENDERER,
+                                                       ps_async_renderer_iface_init);
+                        });
 
 static GObjectClass *parent_class = NULL;
-
 static PSDocumentClass *gs_class = NULL;
 
 static void
@@ -106,11 +106,8 @@ ps_document_init (PSDocument *gs)
 {
        gs->bpixmap = NULL;
 
-       gs->current_page = 0;
        gs->interpreter_pid = -1;
 
-       gs->width = -1;
-       gs->height = -1;
        gs->busy = FALSE;
        gs->gs_filename = 0;
        gs->gs_filename_unc = 0;
@@ -122,7 +119,6 @@ ps_document_init (PSDocument *gs)
        gs->send_filename_to_gs = FALSE;
 
        gs->doc = NULL;
-       gs->loaded = FALSE;
 
        gs->interpreter_input = -1;
        gs->interpreter_output = -1;
@@ -137,47 +133,58 @@ ps_document_init (PSDocument *gs)
        gs->bytes_left = 0;
        gs->buffer_bytes_left = 0;
 
-       gs->zoom_factor = 1.0;
-
        gs->gs_status = _("No document loaded.");
 
-       pixbuf_cond = g_cond_new ();
-       pixbuf_mutex = g_mutex_new ();
+       gs->ps_export_pagelist = NULL;
+       gs->ps_export_filename = NULL;
 }
 
 static void
-ps_document_set_property (GObject *object,
-                         guint prop_id,
-                         const GValue *value,
-                         GParamSpec *pspec)
+ps_document_dispose (GObject *object)
 {
-       switch (prop_id)
+       PSDocument *gs = PS_DOCUMENT (object);
 
-       {
-               case PROP_TITLE:
-                       /* read only */
-                       break;
+       g_return_if_fail (gs != NULL);
+
+       if (gs->gs_psfile) {
+               fclose (gs->gs_psfile);
+               gs->gs_psfile = NULL;
        }
-}
 
-static void
-ps_document_get_property (GObject *object,
-                         guint prop_id,
-                         GValue *value,
-                         GParamSpec *pspec)
-{
-       PSDocument *ps = PS_DOCUMENT (object);
+       if (gs->gs_filename) {
+               g_free (gs->gs_filename);
+               gs->gs_filename = NULL;
+       }
 
-       switch (prop_id)
-       {
-               case PROP_TITLE:
-                       if (ps->doc) {
-                               g_value_set_string (value, ps->doc->title);
-                       } else {
-                               g_value_set_string (value, NULL);
-                       }
-                       break;
+       if (gs->doc) {
+               psfree (gs->doc);
+               gs->doc = NULL;
+       }
+
+       if (gs->gs_filename_unc) {
+               unlink(gs->gs_filename_unc);
+               g_free(gs->gs_filename_unc);
+               gs->gs_filename_unc = NULL;
+       }
+
+       if (gs->bpixmap) {
+               gdk_drawable_unref (gs->bpixmap);
+       }
+
+       if(gs->input_buffer) {
+               g_free(gs->input_buffer);
+               gs->input_buffer = NULL;
        }
+
+       if (gs->target_window) {
+               gtk_widget_destroy (gs->target_window);
+               gs->target_window = NULL;
+               gs->pstarget = NULL;
+       }
+
+       stop_interpreter (gs);
+
+       G_OBJECT_CLASS (parent_class)->dispose (object);
 }
 
 static void
@@ -189,16 +196,12 @@ ps_document_class_init(PSDocumentClass *klass)
        parent_class = g_type_class_peek_parent (klass);
        gs_class = klass;
 
-       object_class->finalize = ps_document_finalize;
-       object_class->get_property = ps_document_get_property;
-       object_class->set_property = ps_document_set_property;
+       object_class->dispose = ps_document_dispose;    
 
        klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
        klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
        klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
        klass->string_atom = gdk_atom_intern ("STRING", FALSE);
-
-       g_object_class_override_property (object_class, PROP_TITLE, "title");
 }
 
 static void
@@ -206,17 +209,20 @@ push_pixbuf (PSDocument *gs)
 {
        GdkColormap *cmap;
        GdkPixbuf *pixbuf;
+       int width, height;
+       
+       if (gs->pstarget == NULL)
+               return;
 
        cmap = gdk_window_get_colormap (gs->pstarget);
-       
+       gdk_drawable_get_size (gs->bpixmap, &width, &height);
+       LOG ("Get from drawable\n");
        pixbuf =  gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
                                                0, 0, 0, 0,
-                                               gs->width, gs->height);
-       g_mutex_lock (pixbuf_mutex);
-       current_pixbuf = pixbuf;
-       g_cond_signal (pixbuf_cond);
-       g_mutex_unlock (pixbuf_mutex);
-
+                                               width, height);
+       LOG ("Get from drawable done\n");
+       g_signal_emit_by_name (gs, "render_finished", pixbuf);
+       g_object_unref (pixbuf);
 }
 
 static void
@@ -229,41 +235,6 @@ interpreter_failed (PSDocument *gs, char *msg)
        stop_interpreter (gs);
 }
 
-static void
-ps_document_cleanup (PSDocument *gs)
-{
-       g_return_if_fail (gs != NULL);
-       g_return_if_fail (PS_IS_DOCUMENT (gs));
-
-       LOG ("Cleanup\n");
-
-       stop_interpreter (gs);
-
-       if (gs->gs_psfile) {
-               fclose (gs->gs_psfile);
-               gs->gs_psfile = NULL;
-       }
-
-       if (gs->gs_filename) {
-               g_free (gs->gs_filename);
-               gs->gs_filename = NULL;
-       }
-
-       if (gs->doc) {
-               psfree (gs->doc);
-               gs->doc = NULL;
-       }
-
-       if (gs->gs_filename_unc) {
-               unlink(gs->gs_filename_unc);
-               g_free(gs->gs_filename_unc);
-               gs->gs_filename_unc = NULL;
-       }
-
-       gs->current_page = 0;
-       gs->loaded = FALSE;
-}
-
 static gboolean
 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
 {
@@ -279,71 +250,48 @@ ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
                gs->busy = FALSE;
 
                push_pixbuf (gs);
+               LOG ("Pixbuf pushed");
        }
 
        return TRUE;
 }
 
 static void
-ps_document_finalize (GObject * object)
+send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
 {
-       PSDocument *gs;
-
-       g_return_if_fail (object != NULL);
-       g_return_if_fail (PS_IS_DOCUMENT (object));
-
-       LOG ("Finalize");
+       struct record_list *ps_new;
 
-       gs = PS_DOCUMENT (object);
+       if (gs->interpreter_input < 0) {
+               g_critical("No pipe to gs: error in send_ps().");
+               return;
+       }
 
-       ps_document_cleanup (gs);
-       stop_interpreter (gs);
+       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;
 
-       if(gs->input_buffer) {
-               g_free(gs->input_buffer);
-               gs->input_buffer = NULL;
+       if (gs->input_buffer == NULL) {
+               gs->input_buffer = g_malloc(MAX_BUFSIZE);
        }
 
-       (*G_OBJECT_CLASS (parent_class)->finalize) (object);
-}
-
-static void
-send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close)
-{
-  struct record_list *ps_new;
-
-  if(gs->interpreter_input < 0) {
-    g_critical("No pipe to gs: error in send_ps().");
-    return;
-  }
-
-  ps_new = (struct record_list *) g_malloc(sizeof(struct record_list));
-  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;
-
-  if(gs->input_buffer == NULL) {
-    gs->input_buffer = g_malloc(MAX_BUFSIZE);
-  }
-
-  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);
-  }
-  else {
-    struct record_list *p = gs->ps_input;
-    while(p->next != NULL) {
-      p = p->next;
-    }
-    p->next = ps_new;
-  }
+       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);
+       } else {
+               struct record_list *p = gs->ps_input;
+               while (p->next != NULL) {
+                       p = p->next;
+               }
+               p->next = ps_new;
+       }
 }
 
 static float
@@ -359,57 +307,166 @@ get_ydpi (PSDocument *gs)
 }
 
 static void
-setup_pixmap (PSDocument *gs)
+setup_pixmap (PSDocument *gs, int page, double scale, int rotation)
 {
        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;
 
-        LOG ("Create our internal pixmap");
+       ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
+
+       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->bpixmap) {
-               gdk_drawable_unref(gs->bpixmap);
+               int w, h;
+
+               gdk_drawable_get_size (gs->bpixmap, &w, &h);
+
+               if (pixmap_width != w || h != pixmap_height) {
+                       gdk_drawable_unref (gs->bpixmap);
+                       gs->bpixmap = NULL;
+                       stop_interpreter (gs);
+               }
        }
 
-       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, gs->width, gs->height, -1);
-       gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
-                            0, 0, gs->width, gs->height);
+       if (!gs->bpixmap) {
+               LOG ("Create pixmap");
+
+               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);
+       }
 }
 
+#define DEFAULT_PAGE_SIZE 1
+
 static void
-setup_page (PSDocument *gs)
+get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
 {
-       char buf[1024];
-#ifdef HAVE_LOCALE_H
-       char *savelocale;
-#endif
+       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;
+
+       g_return_if_fail (PS_IS_DOCUMENT (gs));
+
+       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]) &&
+                                  (gs->doc->pages[page].boundingbox[URY] >
+                                           gs->doc->pages[page].boundingbox[LLY])) {
+                               new_pagesize = -1;
+                       } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
+                                  (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
+                               new_pagesize = -1;
+                       }
+               }
+       }
+
+       /* Compute bounding box */
+       if (gs->doc && (gs->doc->epsf || new_pagesize == -1)) {    /* epsf or bbox */
+               if ((page >= 0) &&
+                   (gs->doc->pages) &&
+                   (gs->doc->pages[page].boundingbox[URX] >
+                    gs->doc->pages[page].boundingbox[LLX]) &&
+                   (gs->doc->pages[page].boundingbox[URY] >
+                     gs->doc->pages[page].boundingbox[LLY])) {
+                       /* use page bbox */
+                       new_llx = gs->doc->pages[page].boundingbox[LLX];
+                       new_lly = gs->doc->pages[page].boundingbox[LLY];
+                       new_urx = gs->doc->pages[page].boundingbox[URX];
+                       new_ury = gs->doc->pages[page].boundingbox[URY];
+               } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
+                          (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
+                       /* use doc bbox */
+                       new_llx = gs->doc->boundingbox[LLX];
+                       new_lly = gs->doc->boundingbox[LLY];
+                       new_urx = gs->doc->boundingbox[URX];
+                       new_ury = gs->doc->boundingbox[URY];
+               }
+       } else {
+               if (new_pagesize < 0)
+                       new_pagesize = DEFAULT_PAGE_SIZE;
+               new_llx = new_lly = 0;
+               if (gs->doc && gs->doc->size &&
+                   (new_pagesize < gs->doc->numsizes)) {
+                       new_urx = gs->doc->size[new_pagesize].width;
+                       new_ury = gs->doc->size[new_pagesize].height;
+               } else {
+                       new_urx = papersizes[new_pagesize].width;
+                       new_ury = papersizes[new_pagesize].height;
+               }
+       }
+
+       if (new_urx <= new_llx)
+               new_urx = papersizes[12].width;
+       if (new_ury <= new_lly)
+               new_ury = papersizes[12].height;
+
+       *urx = new_urx;
+       *ury = new_ury;
+       *llx = new_llx;
+       *lly = new_lly;
+}
+
+static void
+setup_page (PSDocument *gs, int page, double scale, int rotation)
+{
+       gchar *buf;
+       char scaled_xdpi[G_ASCII_DTOSTR_BUF_SIZE];      
+       char scaled_ydpi[G_ASCII_DTOSTR_BUF_SIZE];
+       int urx, ury, llx, lly;
 
        LOG ("Setup the page");
 
-#ifdef HAVE_LOCALE_H
-       /* gs needs floating point parameters with '.' as decimal point
-        * while some (european) locales use ',' instead, so we set the 
-        * locale for this snprintf to "C".
-        */
-       savelocale = setlocale (LC_NUMERIC, "C");
-#endif
-
-       g_snprintf (buf, 1024, "%ld %d %d %d %d %d %f %f %d %d %d %d",
-                   0L, gs->orientation * 90, gs->llx, gs->lly, gs->urx, gs->ury,
-                   get_xdpi (gs) * gs->zoom_factor,
-                   get_ydpi (gs) * gs->zoom_factor,
-                   0, 0, 0, 0);
+       get_page_box (gs, page, &urx, &ury, &llx, &lly);
+       g_ascii_dtostr (scaled_xdpi, G_ASCII_DTOSTR_BUF_SIZE, get_xdpi (gs) * scale);
+       g_ascii_dtostr (scaled_ydpi, G_ASCII_DTOSTR_BUF_SIZE, get_ydpi (gs) * scale);
+
+       buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
+                              0L, rotation, llx, lly, urx, ury,
+                              scaled_xdpi, scaled_ydpi,
+                              0, 0, 0, 0);
        LOG ("GS property %s", buf);
 
-#ifdef HAVE_LOCALE_H
-       setlocale(LC_NUMERIC, savelocale);
-#endif
        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 ();
 }
 
@@ -431,135 +488,127 @@ is_interpreter_ready (PSDocument *gs)
 }
 
 static void
-output(gpointer data, gint source, GdkInputCondition condition)
+output (gpointer data, gint source, GdkInputCondition condition)
 {
-  char buf[MAX_BUFSIZE + 1], *msg;
-  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(bytes > 0) {
-    buf[bytes] = '\0';
-    msg = g_strdup(buf);
-    interpreter_failed (gs, msg);   
-  }
+       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 (bytes > 0) {
+               buf[bytes] = '\0';
+               printf ("%s", buf);
+       }
 }
 
 static void
-catchPipe(int i)
+catchPipe (int i)
 {
-  broken_pipe = True;
+       broken_pipe = True;
 }
 
 static void
 input(gpointer data, gint source, GdkInputCondition condition)
 {
-  PSDocument *gs = PS_DOCUMENT(data);
-  int bytes_written;
-  void (*oldsig) (int);
-  oldsig = signal(SIGPIPE, catchPipe);
-
-  LOG ("Input");
-
-  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((char *) 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;
-      }
-
-      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;
-    }
-
-    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);
-
-  signal(SIGPIPE, oldsig);
-
-  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;
-    }
-  }
+       PSDocument *gs = PS_DOCUMENT(data);
+       int bytes_written;
+       void (*oldsig) (int);
+       oldsig = signal(SIGPIPE, catchPipe);
+
+       LOG ("Input");
+
+       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;
+                       }
+
+                       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;
+               }
+
+               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);
+
+       signal (SIGPIPE, oldsig);
+
+       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;
+               }
+       }
 }
 
 static int
@@ -573,7 +622,7 @@ start_interpreter (PSDocument *gs)
 #define NUM_GS_ARGS (NUM_ARGS - 20)
 #define NUM_ALPHA_ARGS 10
 
-       char *argv[NUM_ARGS], *dir, *gv_env;
+       char *argv[NUM_ARGS], *dir, *gv_env, *gs_path;
        char **gs_args, **alpha_args = NULL;
        int argc = 0, i;
 
@@ -585,12 +634,17 @@ start_interpreter (PSDocument *gs)
        stop_interpreter(gs);
 
        /* set up the args... */
-       gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_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";
@@ -630,7 +684,6 @@ start_interpreter (PSDocument *gs)
                                  gdk_x11_drawable_get_xid (gs->bpixmap));
        LOG ("Launching ghostview with env %s", gv_env);
 
-       gs->busy = TRUE;
        gs->interpreter_pid = fork ();
        switch (gs->interpreter_pid) {
                case -1:                     /* error */
@@ -711,73 +764,71 @@ start_interpreter (PSDocument *gs)
 static void
 stop_interpreter(PSDocument * gs)
 {
-  if(gs->interpreter_pid > 0) {
-    int status = 0;
-    LOG ("Stop the interpreter");
-    kill(gs->interpreter_pid, SIGTERM);
-    while((wait(&status) == -1) && (errno == EINTR)) ;
-    gs->interpreter_pid = -1;
-    if(status == 1) {
-      ps_document_cleanup(gs);
-      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((char *) 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;
+       if (gs->interpreter_pid > 0) {
+               int status = 0;
+               LOG ("Stop the interpreter");
+               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;
 }
 
 /* If file exists and is a regular file then return its length, else -1 */
 static gint
-file_length(const gchar * filename)
+file_length (const gchar * filename)
 {
-  struct stat stat_rec;
+       struct stat stat_rec;
 
-  if(filename && (stat(filename, &stat_rec) == 0)
-     && S_ISREG(stat_rec.st_mode))
-    return stat_rec.st_size;
-  else
-    return -1;
+       if (filename && (stat (filename, &stat_rec) == 0) && S_ISREG (stat_rec.st_mode))
+               return stat_rec.st_size;
+       else
+               return -1;
 }
 
 /* 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);
+       return (file_length (filename) > 0);
 }
 
 /*
@@ -787,296 +838,154 @@ file_readable(const char *filename)
  * Return name of input file to use or NULL on error..
  */
 static gchar *
-check_filecompressed(PSDocument * gs)
+check_filecompressed (PSDocument * gs)
 {
-  FILE *file;
-  gchar buf[1024];
-  gchar *filename, *filename_unc, *filename_err, *cmdline;
-  const gchar *cmd;
-  int fd;
-
-  cmd = NULL;
-
-  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(), "/ggvXXXXXX", NULL);
-  if((fd = mkstemp(filename_unc)) < 0) {
-    g_free(filename_unc);
-    g_free(filename);
-    return NULL;
-  }
-  close(fd);
-  filename_err = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
-  if((fd = 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)
-     && file_readable(filename_unc)
-     && (file_length(filename_err) == 0)) {
-    /* sucessfully uncompressed file */
-    gs->gs_filename_unc = filename_unc;
-  }
-  else {
-    /* report error */
-    g_snprintf(buf, 1024, _("Error while decompressing file %s:\n"),
-               gs->gs_filename);
-    interpreter_failed (gs, buf);
-    unlink(filename_unc);
-    g_free(filename_unc);
-    filename_unc = NULL;
-  }
-  unlink(filename_err);
-  g_free(filename_err);
-  g_free(cmdline);
-  g_free(filename);
-  return filename_unc;
-}
+       FILE *file;
+       gchar buf[1024];
+       gchar *filename, *filename_unc, *filename_err, *cmdline;
+       const gchar *cmd;
+       int fd;
+
+       cmd = NULL;
+
+       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 ();
+               }
+       }
 
-static void
-compute_dimensions (PSDocument *gs, int page)
-{
-       GtkGSPaperSize *paper_sizes = gtk_gs_defaults_get_paper_sizes ();
-       int urx, ury, llx, lly;
-       int width, height;
-       int orientation;
+       if (NULL != file)
+               fclose(file);
 
-       g_return_if_fail (PS_IS_DOCUMENT (gs));
-       g_return_if_fail (gs->doc != NULL);
-       g_return_if_fail (page >= 0);
-       g_return_if_fail (gs->doc->numpages > page);
+       if (!cmd)
+               return gs->gs_filename;
 
-       orientation = GTK_GS_ORIENTATION_NONE;
-       if (gs->structured_doc) {
-               orientation = gs->doc->pages[gs->current_page].orientation;
+       /* 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;
        }
-       if (orientation == GTK_GS_ORIENTATION_NONE) {
-               orientation = GTK_GS_ORIENTATION_PORTRAIT;
+       close (fd);
+
+       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 (gs->doc->pages && gs->doc->pages[page].size) {
-               int page_size;
-
-               page_size = gs->doc->pages[page].size - gs->doc->size;
-               llx = lly = 0;
-               urx = gs->doc->size[page_size].width;
-               ury = gs->doc->size[page_size].height;
-       } else if (gs->doc->pages &&
-                   (gs->doc->pages[page].boundingbox[URX] >
-                    gs->doc->pages[page].boundingbox[LLX]) &&
-                   (gs->doc->pages[page].boundingbox[URY] >
-                    gs->doc->pages[page].boundingbox[LLY])) {
-               llx = gs->doc->pages[page].boundingbox[LLX];
-               lly = gs->doc->pages[page].boundingbox[LLY];
-               urx = gs->doc->pages[page].boundingbox[URX];
-               ury = gs->doc->pages[page].boundingbox[URY];
-       } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
-                   (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
-               llx = gs->doc->boundingbox[LLX];
-               lly = gs->doc->boundingbox[LLY];
-               urx = gs->doc->boundingbox[URX];
-               ury = gs->doc->boundingbox[URY];
+       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 {
-               /* Fallback to A4 */
-               llx = lly = 0;
-               urx = paper_sizes[12].width;
-               ury = paper_sizes[12].height;
+               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;
        }
 
-       switch (orientation) {
-               case GTK_GS_ORIENTATION_PORTRAIT:
-               case GTK_GS_ORIENTATION_UPSIDEDOWN:
-                       width = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
-                       height = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
-                       break;
-               case GTK_GS_ORIENTATION_LANDSCAPE:
-               case GTK_GS_ORIENTATION_SEASCAPE:
-                       width = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
-                       height = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
-                       break;
-               default:
-                       width = height = 0;
-                       g_assert_not_reached ();
-                       break;
-       }
+       unlink (filename_err);
+       g_free (filename_err);
+       g_free (cmdline);
+       g_free (filename);
 
-       width = width * gs->zoom_factor;
-       height = height * gs->zoom_factor;
-
-       if (llx != gs->llx || lly != gs->lly ||
-           urx != gs->urx || ury != gs->ury ||
-           gs->width != width || gs->height != height ||
-           orientation != gs->orientation) {
-               gs->llx = llx;
-               gs->lly = lly;
-               gs->urx = urx;
-               gs->ury = ury;
-               gs->width = width;
-               gs->height = height;
-               gs->orientation = orientation;
-               gs->changed = TRUE;
-       }
+       return filename_unc;
 }
 
 static gint
-ps_document_enable_interpreter(PSDocument * gs)
+ps_document_enable_interpreter(PSDocument *gs)
 {
-  g_return_val_if_fail(gs != NULL, FALSE);
-  g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
+       g_return_val_if_fail (PS_IS_DOCUMENT (gs), FALSE);
 
-  if(!gs->gs_filename)
-    return 0;
+       if (!gs->gs_filename)
+               return 0;
 
-  return start_interpreter(gs);
+       return start_interpreter (gs);
 }
 
-/* publicly accessible functions */
-
-GType
-ps_document_get_type(void)
+static gboolean
+document_load (PSDocument *gs, const gchar *fname)
 {
-  static GType gs_type = 0;
-  if(!gs_type) {
-    GTypeInfo gs_info = {
-      sizeof(PSDocumentClass),
-      (GBaseInitFunc) NULL,
-      (GBaseFinalizeFunc) NULL,
-      (GClassInitFunc) ps_document_class_init,
-      (GClassFinalizeFunc) NULL,
-      NULL,                     /* class_data */
-      sizeof(PSDocument),
-      0,                        /* n_preallocs */
-      (GInstanceInitFunc) ps_document_init
-    };
-
-    static const GInterfaceInfo document_info =
-    {
-        (GInterfaceInitFunc) ps_document_document_iface_init,
-        NULL,
-        NULL
-    };
-
-    gs_type = g_type_register_static(G_TYPE_OBJECT,
-                                     "PSDocument", &gs_info, 0);
-
-    g_type_add_interface_static (gs_type,
-                                 EV_TYPE_DOCUMENT,
-                                 &document_info);
-  }
-  return gs_type;
+       g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
 
+       LOG ("Load the document");
 
-}
+       if (fname == NULL) {
+               gs->gs_status = "";
+               return FALSE;
+       }
 
-static gboolean
-document_load(PSDocument * gs, const gchar * fname)
-{
-  g_return_val_if_fail(gs != NULL, FALSE);
-  g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
-
-  LOG ("Load the document");
-
-  /* clean up previous document */
-  ps_document_cleanup(gs);
-
-  if(fname == NULL) {
-    gs->gs_status = "";
-    return FALSE;
-  }
-
-  /* prepare this document */
-  gs->structured_doc = FALSE;
-  gs->send_filename_to_gs = TRUE;
-  gs->current_page = 0;
-  gs->loaded = FALSE;
-  if(*fname == '/') {
-    /* an absolute path */
-    gs->gs_filename = g_strdup(fname);
-  }
-  else {
-    /* path relative to our cwd: make it absolute */
-    gchar *cwd = g_get_current_dir();
-    gs->gs_filename = g_strconcat(cwd, "/", fname, NULL);
-    g_free(cwd);
-  }
-
-  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;
-
-    if(!file_readable(fname)) {
-      gchar buf[1024];
-      g_snprintf(buf, 1024, _("Cannot open file %s.\n"), fname);
-      interpreter_failed (gs, buf);
-      gs->gs_status = _("File is not readable.");
-    }
-    else {
-      filename = check_filecompressed(gs);
-    }
-
-    if(!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
-      interpreter_failed (gs, NULL);
-      ps_document_cleanup(gs);
-      return FALSE;
-    }
-
-    /* we grab the vital statistics!!! */
-    gs->doc = psscan(gs->gs_psfile, TRUE, filename);
-
-    g_object_notify (G_OBJECT (gs), "title");
-
-    if(gs->doc == NULL) {
-      /* File does not seem to be a Postscript one */
-      gchar buf[1024];
-      g_snprintf(buf, 1024, _("Error while scanning file %s\n"), fname);
-      interpreter_failed (gs, buf);
-      ps_document_cleanup(gs);
-      gs->gs_status = _("The file is not a PostScript document.");
-      return FALSE;
-    }
-
-    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->loaded = TRUE;
-  compute_dimensions (gs, gs->current_page);
-
-  gs->gs_status = _("Document loaded.");
-
-  return gs->loaded;
-}
+       /* 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;
+
+               if (!file_readable(fname)) {
+                       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);
+                       
+                       interpreter_failed (gs, msg);
+                       g_free (msg);
+                       gs->gs_status = _("File is not readable.");
+               } else {
+                       filename = check_filecompressed(gs);
+               }
+
+               if (!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
+                       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_next_page (PSDocument *gs)
@@ -1106,7 +1015,7 @@ ps_document_next_page (PSDocument *gs)
 }
 
 static gboolean
-render_page (PSDocument *gs)
+render_page (PSDocument *gs, int page)
 {
        g_return_val_if_fail(gs != NULL, FALSE);
        g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
@@ -1126,8 +1035,8 @@ render_page (PSDocument *gs)
                        send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
                }
 
-               send_ps (gs, gs->doc->pages[gs->current_page].begin,
-                        gs->doc->pages[gs->current_page].len, FALSE);
+               send_ps (gs, gs->doc->pages[page].begin,
+                        gs->doc->pages[page].len, FALSE);
        } else {
                /* Unstructured document
                 *
@@ -1155,163 +1064,212 @@ ps_document_load (EvDocument  *document,
                  const char  *uri,
                  GError     **error)
 {
-       gboolean result;
        char *filename;
+       char *gs_path;
+       gboolean result;
 
        filename = g_filename_from_uri (uri, NULL, error);
        if (!filename)
                return FALSE;
 
-       result = document_load (PS_DOCUMENT (document), filename);
-       if (!result) {
-               g_set_error (error, G_FILE_ERROR,
-                            G_FILE_ERROR_FAILED,
-                            "Failed to load document '%s'\n",
-                            uri);
+       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
-ps_document_save (EvDocument  *document,
-                 const char  *uri,
-                 GError     **error)
+save_document (PSDocument *document, const char *filename)
 {
-       g_warning ("ps_document_save not implemented"); /* FIXME */
-       return TRUE;
-}
+       gboolean result = TRUE;
+       GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
+       FILE *f, *src_file;
+       gchar *buf;
 
-static int
-ps_document_get_n_pages (EvDocument  *document)
-{
-       PSDocument *ps = PS_DOCUMENT (document);
+       src_file = fopen (PS_DOCUMENT_GET_PS_FILE(document), "r");
+       if (src_file) {
+               struct stat stat_rec;
 
-       g_return_val_if_fail (ps != NULL, -1);
+               if (stat (PS_DOCUMENT_GET_PS_FILE(document), &stat_rec) == 0) {
+                       pscopy (src_file, sink, 0, stat_rec.st_size - 1);
+               }
 
-       if (!ps->gs_filename || !ps->doc) {
-               return -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;
        }
 
-       return ps->structured_doc ? ps->doc->numpages : 1;
+       g_free (buf);
+       gtk_gs_doc_sink_free (sink);
+       g_free (sink);
+
+       return result;
 }
 
-static void
-ps_document_set_page (EvDocument  *document,
-                      int          page)
+static gboolean
+save_page_list (PSDocument *document, int *page_list, const char *filename)
 {
-       PSDocument *gs = PS_DOCUMENT (document);
+       gboolean result = TRUE;
+       GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
+       FILE *f;
+       gchar *buf;
 
-       LOG ("Set document page %d\n", page);
+       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);
 
-       gs->current_page = page;
-       compute_dimensions (gs, page);
+       return result;
 }
 
-static int
-ps_document_get_page (EvDocument  *document)
+static gboolean
+ps_document_save (EvDocument  *document,
+                 const char  *uri,
+                 GError     **error)
 {
        PSDocument *ps = PS_DOCUMENT (document);
+       gboolean result;
+       char *filename;
 
-       g_return_val_if_fail (ps != NULL, -1);
+       filename = g_filename_from_uri (uri, NULL, error);
+       if (!filename)
+               return FALSE;
+
+       result = save_document (ps, filename);
 
-       return ps->current_page;
+       g_free (filename);
+
+       return result;
 }
 
-static void
-ps_document_set_scale (EvDocument  *document,
-                       double       scale)
+static int
+ps_document_get_n_pages (EvDocument  *document)
 {
-       PSDocument *gs = PS_DOCUMENT (document);
+       PSDocument *ps = PS_DOCUMENT (document);
+
+       g_return_val_if_fail (ps != NULL, -1);
 
-       gs->zoom_factor = scale;
-       compute_dimensions (gs, gs->current_page);
+       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,
-                          int          *width,
-                          int          *height)
+                          double       *width,
+                          double       *height)
 {
-       /* Post script documents never vary in size */
-
        PSDocument *gs = PS_DOCUMENT (document);
+       int urx, ury, llx, lly;
+
+       get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
 
        if (width) {
-               *width = gs->width;
+               *width = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
        }
 
        if (height) {
-               *height = gs->height;
+               *height = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
        }
 }
 
-static char *
-ps_document_get_text (EvDocument *document, GdkRectangle *rect)
+static gboolean
+ps_document_can_get_text (EvDocument *document)
 {
-       g_warning ("ps_document_get_text not implemented"); /* FIXME ? */
-       return NULL;
+       return FALSE;
 }
 
-static EvLink *
-ps_document_get_link (EvDocument *document,
-                     int         x,
-                     int         y)
+static void
+ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer, int page, double scale, int rotation)
 {
-       return NULL;
-}
-
-static gboolean
-render_pixbuf_idle (EvDocument *document)
-{
-       PSDocument *gs = PS_DOCUMENT (document);
+       PSDocument *gs = PS_DOCUMENT (renderer);
 
        if (gs->pstarget == NULL) {
-               GtkWidget *widget;
-
-               widget = gtk_window_new (GTK_WINDOW_POPUP);
-               gtk_widget_realize (widget);
-               gs->pstarget = widget->window;
+               gs->target_window = gtk_window_new (GTK_WINDOW_POPUP);
+               gtk_widget_realize (gs->target_window);
+               gs->pstarget = gs->target_window->window;
 
                g_assert (gs->pstarget != NULL);
 
-               g_signal_connect (widget, "event",
+               g_signal_connect (gs->target_window, "event",
                                  G_CALLBACK (ps_document_widget_event),
                                  gs);
        }
 
-       if (gs->changed) {
-               stop_interpreter (gs);
-               setup_pixmap (gs);
-               setup_page (gs);
-               gs->changed = FALSE;
-       }
+       setup_pixmap (gs, page, scale, rotation);
+       setup_page (gs, page, scale, rotation);
 
-       render_page (PS_DOCUMENT (document));
-
-       return FALSE;
+       render_page (gs, page);
 }
 
-static GdkPixbuf *
-ps_document_render_pixbuf (EvDocument *document)
+static EvDocumentInfo *
+ps_document_get_info (EvDocument *document)
 {
-       GdkPixbuf *pixbuf;
-
-       g_idle_add ((GSourceFunc)render_pixbuf_idle, document);
-
-       g_mutex_lock (pixbuf_mutex);
-       while (!current_pixbuf)
-               g_cond_wait (pixbuf_cond, pixbuf_mutex);
-       pixbuf = current_pixbuf;
-       current_pixbuf = NULL;
-       g_mutex_unlock (pixbuf_mutex);
-
-       LOG ("Pixbuf rendered %p\n", pixbuf);
+       EvDocumentInfo *info;
+       PSDocument *ps = PS_DOCUMENT (document);
 
-       return pixbuf;
+       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;
+       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);
+
+       return info;
 }
 
 static void
@@ -1319,12 +1277,65 @@ ps_document_document_iface_init (EvDocumentIface *iface)
 {
        iface->load = ps_document_load;
        iface->save = ps_document_save;
-       iface->get_text = ps_document_get_text;
-       iface->get_link = ps_document_get_link;
+       iface->can_get_text = ps_document_can_get_text;
        iface->get_n_pages = ps_document_get_n_pages;
-       iface->set_page = ps_document_set_page;
-       iface->get_page = ps_document_get_page;
-       iface->set_scale = ps_document_set_scale;
        iface->get_page_size = ps_document_get_page_size;
-       iface->render_pixbuf = ps_document_render_pixbuf;
+       iface->get_info = ps_document_get_info;
+}
+
+static void
+ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
+{
+       iface->render_pixbuf = ps_async_renderer_render_pixbuf;
+}
+
+static void
+ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
+                            int first_page, int last_page,
+                             double width, double height, gboolean duplex)
+{
+       PSDocument *document = PS_DOCUMENT (exporter);
+
+       if (document->structured_doc) {
+               g_free (document->ps_export_pagelist);
+       
+               document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
+       }
+
+       document->ps_export_filename = g_strdup (filename);
+}
+
+static void
+ps_document_ps_export_do_page (EvPSExporter *exporter, EvRenderContext *rc)
+{
+       PSDocument *document = PS_DOCUMENT (exporter);
+       
+       if (document->structured_doc) {
+               document->ps_export_pagelist[rc->page] = 1;
+       }
+}
+
+static void
+ps_document_ps_export_end (EvPSExporter *exporter)
+{
+       PSDocument *document = PS_DOCUMENT (exporter);
+
+       if (!document->structured_doc) {
+               save_document (document, document->ps_export_filename);
+       } else {
+               save_page_list (document, document->ps_export_pagelist,
+                               document->ps_export_filename);
+               g_free (document->ps_export_pagelist);
+               g_free (document->ps_export_filename);  
+               document->ps_export_pagelist = NULL;
+               document->ps_export_filename = NULL;
+       }
+}
+
+static void
+ps_document_ps_exporter_iface_init (EvPSExporterIface *iface)
+{
+       iface->begin = ps_document_ps_export_begin;
+       iface->do_page = ps_document_ps_export_do_page;
+       iface->end = ps_document_ps_export_end;
 }