]> www.fi.muni.cz Git - evince.git/blobdiff - ps/ps-document.c
Runtime check for gs interpreter. Fix for the bug #320868. Thanks to
[evince.git] / ps / ps-document.c
index 3f1999524137f84e221c3adc93849c67cacf274e..75009dc9844f54c4ed8ef6ede8240c26a19d36f9 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
  
  * Boston, MA 02111-1307, USA.
  */
  
-/*
-Ghostview interface to ghostscript
-
-When the GHOSTVIEW environment variable is set, ghostscript draws on
-an existing drawable rather than creating its own window.  Ghostscript
-can be directed to draw on either a window or a pixmap.
-
-Drawing on a Window
-
-The GHOSTVIEW environment variable contains the window id of the target
-window.  The window id is an integer.  Ghostscript will use the attributes
-of the window to obtain the width, height, colormap, screen, and visual of
-the window. The remainder of the information is gotten from the GHOSTVIEW
-property on that window.
-
-
-Drawing on a Pixmap
-
-The GHOSTVIEW environment variable contains a window id and a pixmap id.
-They are integers separated by white space.  Ghostscript will use the
-attributes of the window to obtain the colormap, screen, and visual to use.
-The width and height will be obtained from the pixmap. The remainder of the
-information, is gotten from the GHOSTVIEW property on the window.  In this
-case, the property is deleted when read.
-
-The GHOSTVIEW environment variable
-
-parameters:    window-id [pixmap-id]
-
-scanf format:  "%d %d"
-
-explanation of parameters:
-
-       window-id: tells ghostscript where to
-                   - read the GHOSTVIEW property
-                   - send events
-                   If pixmap-id is not present,
-                   ghostscript will draw on this window.
-
-       pixmap-id: If present, tells ghostscript that a pixmap will be used
-                   as the final destination for drawing.  The window will
-                   not be touched for drawing purposes.
-
-The GHOSTVIEW property
-
-type:  STRING
-
-parameters:
-
-    bpixmap orient llx lly urx ury xdpi ydpi [left bottom top right]
-
-scanf format: "%d %d %d %d %d %d %f %f %d %d %d %d"
-
-explanation of parameters:
-
-       bpixmap: pixmap id of the backing pixmap for the window.  If no
-               pixmap is to be used, this parameter should be zero.  This
-               parameter must be zero when drawing on a pixmap.
-
-       orient: orientation of the page.  The number represents clockwise
-               rotation of the paper in degrees.  Permitted values are
-               0, 90, 180, 270.
-
-       llx, lly, urx, ury: Bounding box of the drawable.  The bounding box
-               is specified in PostScript points in default user coordinates.
-
-       xdpi, ydpi: Resolution of window.  (This can be derived from the
-               other parameters, but not without roundoff error.  These
-               values are included to avoid this error.)
-
-       left, bottom, top, right: (optional)
-               Margins around the window.  The margins extend the imageable
-               area beyond the boundaries of the window.  This is primarily
-               used for popup zoom windows.  I have encountered several
-               instances of PostScript programs that position themselves
-               with respect to the imageable area.  The margins are specified
-               in PostScript points.  If omitted, the margins are assumed to
-               be 0.
-
-Events from ghostscript
-
-If the final destination is a pixmap, the client will get a property notify
-event when ghostscript reads the GHOSTVIEW property causing it to be deleted.
-
-Ghostscript sends events to the window where it read the GHOSTVIEW property.
-These events are of type ClientMessage.  The message_type is set to
-either PAGE or DONE.  The first long data value gives the window to be used
-to send replies to ghostscript.  The second long data value gives the primary
-drawable.  If rendering to a pixmap, it is the primary drawable.  If rendering
-to a window, the backing pixmap is the primary drawable.  If no backing pixmap
-is employed, then the window is the primary drawable.  This field is necessary
-to distinguish multiple ghostscripts rendering to separate pixmaps where the
-GHOSTVIEW property was placed on the same window.
-
-The PAGE message indicates that a "page" has completed.  Ghostscript will
-wait until it receives a ClientMessage whose message_type is NEXT before
-continuing.
-
-The DONE message indicates that ghostscript has finished processing.
-
-*/
-
 #include "config.h"
 #include <string.h>
 #include <stdlib.h>
 #include "config.h"
 #include <string.h>
 #include <stdlib.h>
@@ -134,10 +32,6 @@ The DONE message indicates that ghostscript has finished processing.
 #include <gdk/gdkx.h>
 #include <gdk/gdk.h>
 #include <glib/gi18n.h>
 #include <gdk/gdkx.h>
 #include <gdk/gdk.h>
 #include <glib/gi18n.h>
-#ifdef  HAVE_XINERAMA
-#   include <gdk/gdkx.h>
-#   include <X11/extensions/Xinerama.h>
-#endif /* HAVE_XINERAMA */
 #include <X11/Intrinsic.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <X11/Intrinsic.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -150,779 +44,785 @@ The DONE message indicates that ghostscript has finished processing.
 #include <math.h>
 
 #include "ps-document.h"
 #include <math.h>
 
 #include "ps-document.h"
+#include "ev-debug.h"
 #include "gsdefaults.h"
 #include "gsdefaults.h"
+#include "ev-ps-exporter.h"
+#include "ev-async-renderer.h"
 
 
-#ifdef HAVE_LOCALE_H
-#   include <locale.h>
-#endif
+#define MAX_BUFSIZE 1024
 
 
-/* if POSIX O_NONBLOCK is not available, use O_NDELAY */
-#if !defined(O_NONBLOCK) && defined(O_NDELAY)
-#   define O_NONBLOCK O_NDELAY
-#endif
+#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)
 
 
-#define PS_DOCUMENT_WATCH_INTERVAL 1000
-#define PS_DOCUMENT_WATCH_TIMEOUT  2
+/* 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;
+};
 
 
-#define MAX_BUFSIZE 1024
+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_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);
+                        });
 
 
-#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)
+static GObjectClass *parent_class = NULL;
+static PSDocumentClass *gs_class = NULL;
 
 
-enum { INTERPRETER_MESSAGE, INTERPRETER_ERROR, LAST_SIGNAL };
+static void
+ps_document_init (PSDocument *gs)
+{
+       gs->bpixmap = NULL;
 
 
-enum {
-       PROP_0,
-       PROP_TITLE
-};
+       gs->interpreter_pid = -1;
 
 
-/* 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;
-};
+       gs->busy = FALSE;
+       gs->gs_filename = 0;
+       gs->gs_filename_unc = 0;
 
 
-static gboolean broken_pipe = FALSE;
+       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_id = 0;
+       gs->interpreter_output_id = 0;
+       gs->interpreter_error_id = 0;
+
+       gs->ps_input = NULL;
+       gs->input_buffer = NULL;
+       gs->input_buffer_ptr = NULL;
+       gs->bytes_left = 0;
+       gs->buffer_bytes_left = 0;
+
+       gs->gs_status = _("No document loaded.");
+
+       gs->ps_export_pagelist = NULL;
+       gs->ps_export_filename = NULL;
+}
 
 static void
 
 static void
-catchPipe(int i)
+ps_document_dispose (GObject *object)
 {
 {
-  broken_pipe = True;
+       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) {
+               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;
+       }
+
+       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);
 }
 
 }
 
-/* Forward declarations */
-static void ps_document_init(PSDocument * gs);
-static void ps_document_class_init(PSDocumentClass * klass);
-static void ps_document_emit_error_msg(PSDocument * gs, const gchar * msg);
-static void ps_document_finalize(GObject * object);
-static void send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close);
-static void set_up_page(PSDocument * gs);
-static void close_pipe(int p[2]);
-static void interpreter_failed(PSDocument * gs);
-static float compute_xdpi(void);
-static float compute_ydpi(void);
-static gboolean compute_size(PSDocument * gs);
-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);
-gboolean computeSize(void);
-static gboolean ps_document_set_page_size(PSDocument * gs, gint new_pagesize, gint pageid);
-static void ps_document_document_iface_init (EvDocumentIface *iface);
+static void
+ps_document_class_init(PSDocumentClass *klass)
+{
+       GObjectClass *object_class;
 
 
-static GObjectClass *parent_class = NULL;
+       object_class = (GObjectClass *) klass;
+       parent_class = g_type_class_peek_parent (klass);
+       gs_class = klass;
 
 
-static PSDocumentClass *gs_class = NULL;
+       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);
+}
 
 static void
 
 static void
-ps_document_init(PSDocument * gs)
+push_pixbuf (PSDocument *gs)
 {
 {
-  gs->bpixmap = NULL;
-
-  gs->current_page = -2;
-  gs->disable_start = FALSE;
-  gs->interpreter_pid = -1;
-
-  gs->width = -1;
-  gs->height = -1;
-  gs->busy = FALSE;
-  gs->changed = FALSE;
-  gs->gs_scanstyle = 0;
-  gs->gs_filename = 0;
-  gs->gs_filename_dsc = 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->loaded = FALSE;
-
-  gs->interpreter_input = -1;
-  gs->interpreter_output = -1;
-  gs->interpreter_err = -1;
-  gs->interpreter_input_id = 0;
-  gs->interpreter_output_id = 0;
-  gs->interpreter_error_id = 0;
-
-  gs->ps_input = NULL;
-  gs->input_buffer = NULL;
-  gs->input_buffer_ptr = NULL;
-  gs->bytes_left = 0;
-  gs->buffer_bytes_left = 0;
-
-  gs->llx = 0;
-  gs->lly = 0;
-  gs->urx = 0;
-  gs->ury = 0;
-  gs->xdpi = compute_xdpi();
-  gs->ydpi = compute_ydpi();
-
-  gs->left_margin = 0;
-  gs->top_margin = 0;
-  gs->right_margin = 0;
-  gs->bottom_margin = 0;
-
-  /* Set user defined defaults */
-  gs->override_orientation = gtk_gs_defaults_get_override_orientation();
-  gs->fallback_orientation = gtk_gs_defaults_get_orientation();
-  gs->zoom_factor = gtk_gs_defaults_get_zoom_factor();
-  gs->default_size = gtk_gs_defaults_get_size();
-  gs->antialiased = gtk_gs_defaults_get_antialiased();
-  gs->override_size = gtk_gs_defaults_get_override_size();
-  gs->respect_eof = gtk_gs_defaults_get_respect_eof();
-  gs->zoom_mode = gtk_gs_defaults_get_zoom_mode();
-
-  gs->gs_status = _("No document loaded.");
+       GdkColormap *cmap;
+       GdkPixbuf *pixbuf;
+       int width, height;
+
+       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,
+                                               width, height);
+       LOG ("Get from drawable done\n");
+       g_signal_emit_by_name (gs, "render_finished", pixbuf);
+       g_object_unref (pixbuf);
 }
 
 static void
 }
 
 static void
-ps_document_set_property (GObject *object,
-                         guint prop_id,
-                         const GValue *value,
-                         GParamSpec *pspec)
+interpreter_failed (PSDocument *gs, char *msg)
 {
 {
-       switch (prop_id)
+       LOG ("Interpreter failed %s", msg);
 
 
-       {
-               case PROP_TITLE:
-                       /* read only */
-                       break;
-       }
+       push_pixbuf (gs);
+
+       stop_interpreter (gs);
 }
 
 }
 
-static void
-ps_document_get_property (GObject *object,
-                         guint prop_id,
-                         GValue *value,
-                         GParamSpec *pspec)
+static gboolean
+ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
 {
 {
-       PSDocument *ps = PS_DOCUMENT (object);
+       PSDocument *gs = (PSDocument *) data;
 
 
-       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(event->type != GDK_CLIENT_EVENT)
+               return FALSE;
+
+       gs->message_window = event->client.data.l[0];
+
+       if (event->client.message_type == gs_class->page_atom) {
+               LOG ("GS rendered the document");
+               gs->busy = FALSE;
+
+               push_pixbuf (gs);
+               LOG ("Pixbuf pushed");
        }
        }
+
+       return TRUE;
 }
 
 static void
 }
 
 static void
-ps_document_class_init(PSDocumentClass * klass)
+send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
 {
 {
-  GObjectClass *object_class;
+       struct record_list *ps_new;
 
 
-  object_class = (GObjectClass *) klass;
-  parent_class = gtk_type_class(gtk_widget_get_type());
-  gs_class = klass;
+       if (gs->interpreter_input < 0) {
+               g_critical("No pipe to gs: error in send_ps().");
+               return;
+       }
 
 
-  object_class->finalize = ps_document_finalize;
-  object_class->get_property = ps_document_get_property;
-  object_class->set_property = ps_document_set_property;
+       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;
 
 
-  /* Create atoms */
-  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);
+       if (gs->input_buffer == NULL) {
+               gs->input_buffer = g_malloc(MAX_BUFSIZE);
+       }
 
 
-  gtk_gs_defaults_load();
+       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;
+       }
+}
 
 
-  g_object_class_override_property (object_class, PROP_TITLE, "title");
+static float
+get_xdpi (PSDocument *gs)
+{
+       return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
 }
 
 }
 
-/* Clean all memory and temporal files */
-static void
-ps_document_cleanup(PSDocument * gs)
+static float
+get_ydpi (PSDocument *gs)
 {
 {
-  g_return_if_fail(gs != NULL);
-  g_return_if_fail(GTK_IS_GS(gs));
-
-  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_dsc) {
-    unlink(gs->gs_filename_dsc);
-    g_free(gs->gs_filename_dsc);
-    gs->gs_filename_dsc = 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 = -1;
-  gs->loaded = FALSE;
-  gs->llx = 0;
-  gs->lly = 0;
-  gs->urx = 0;
-  gs->ury = 0;
-  set_up_page(gs);
+       return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
 }
 
 static void
 }
 
 static void
-ps_document_finalize(GObject * object)
+setup_pixmap (PSDocument *gs, int page, double scale, int rotation)
 {
 {
-  PSDocument *gs;
-
-  g_return_if_fail(object != NULL);
-  g_return_if_fail(GTK_IS_GS(object));
+       GdkGC *fill;
+       GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF };   /* pixel, r, g, b */
+       GdkColormap *colormap;
+       double width, height;
+       int pixmap_width, pixmap_height;
+
+       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;
+       }
 
 
-  gs = PS_DOCUMENT(object);
+       if(gs->bpixmap) {
+               int w, h;
 
 
-  ps_document_cleanup(gs);
+               gdk_drawable_get_size (gs->bpixmap, &w, &h);
 
 
-  if(gs->input_buffer) {
-    g_free(gs->input_buffer);
-    gs->input_buffer = NULL;
-  }
+               if (pixmap_width != w || h != pixmap_height) {
+                       gdk_drawable_unref (gs->bpixmap);
+                       gs->bpixmap = NULL;
+                       stop_interpreter (gs);
+               }
+       }
 
 
-  (*G_OBJECT_CLASS(parent_class)->finalize) (object);
+       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
 static void
-send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close)
+get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
 {
 {
-  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;
-  }
-}
+       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;
+                       }
+               }
+       }
 
 
-static gint
-ps_document_get_orientation(PSDocument * gs)
-{
-  g_return_val_if_fail(gs != NULL, -1);
-  g_return_val_if_fail(GTK_IS_GS(gs), -1);
-
-  if(gs->doc) {
-    if(gs->structured_doc) {
-      if(gs->doc->pages[MAX(gs->current_page, 0)].orientation !=
-         GTK_GS_ORIENTATION_NONE)
-        gs->real_orientation =
-          gs->doc->pages[MAX(gs->current_page, 0)].orientation;
-      else
-        gs->real_orientation = gs->doc->default_page_orientation;
-    }
-
-    if(gs->real_orientation == GTK_GS_ORIENTATION_NONE)
-      gs->real_orientation = gs->doc->orientation;
-  }
-
-  if(gs->override_orientation ||
-     gs->real_orientation == GTK_GS_ORIENTATION_NONE)
-    return gs->fallback_orientation;
-  else
-    return gs->real_orientation;
+       /* 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
 }
 
 static void
-set_up_page(PSDocument * gs)
+setup_page (PSDocument *gs, int page, double scale, int rotation)
 {
 {
-  guint orientation;
-  char buf[1024];
-  //GdkColormap *colormap;
-  GdkGC *fill;
-  GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF };   /* pixel, r, g, b */
-  GdkColormap *colormap;
-
-#ifdef HAVE_LOCALE_H
-  char *savelocale;
-#endif
-
-  if (gs->pstarget == NULL)
-    return;
-
-  /* Do we have to check if the actual geometry changed? */
-
-  stop_interpreter(gs);
-
-  orientation = ps_document_get_orientation(gs);
-
-  if(compute_size(gs)) {
-    gdk_flush();
-
-    /* clear new pixmap (set to white) */
-    fill = gdk_gc_new(gs->pstarget);
-    if(fill) {
-      colormap = gdk_drawable_get_colormap(gs->pstarget);
-      gdk_color_alloc (colormap, &white);
-      gdk_gc_set_foreground(fill, &white);
-
-      if(gs->width > 0 && gs->height > 0) {
-        if(gs->bpixmap) {
-          gdk_drawable_unref(gs->bpixmap);
-          gs->bpixmap = NULL;
-        }
-
-        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);
-      }
-      else {
-        gdk_draw_rectangle(gs->pstarget, fill, TRUE,
-                           0, 0, gs->width, gs->height);
-      }
-      gdk_gc_unref(fill);
-
-      gdk_flush();
-    }
-  }
-
-#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,
-             orientation * 90,
-             gs->llx,
-             gs->lly,
-             gs->urx,
-             gs->ury,
-             gs->xdpi * gs->zoom_factor,
-             gs->ydpi * gs->zoom_factor,
-             gs->left_margin,
-             gs->bottom_margin, gs->right_margin, gs->top_margin);
-
-#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, buf, strlen(buf));
-  gdk_flush();
+       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");
+
+       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);
+
+       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
 }
 
 static void
-close_pipe(int p[2])
+close_pipe (int p[2])
 {
 {
-  if(p[0] != -1)
-    close(p[0]);
-  if(p[1] != -1)
-    close(p[1]);
+       if (p[0] != -1) {
+               close (p[0]);
+       }
+       if (p[1] != -1) {
+               close (p[1]);
+       }
 }
 
 static gboolean
 }
 
 static gboolean
-is_interpreter_ready(PSDocument * gs)
+is_interpreter_ready (PSDocument *gs)
 {
 {
-  return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
+       return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
 }
 
 static void
 }
 
 static void
-interpreter_failed(PSDocument * gs)
+output (gpointer data, gint source, GdkInputCondition condition)
 {
 {
-  stop_interpreter(gs);
+       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
 }
 
 static void
-output(gpointer data, gint source, GdkInputCondition condition)
+catchPipe (int i)
 {
 {
-  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);
-      return;
-    }
-    if(gs->interpreter_err == -1) {
-      stop_interpreter(gs);
-    }
-  }
-  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);
-      return;
-    }
-    if(gs->interpreter_output == -1) {
-      stop_interpreter(gs);
-    }
-  }
-  if(bytes > 0) {
-    buf[bytes] = '\0';
-    msg = g_strdup(buf);
-    ps_document_emit_error_msg (gs, msg);   
-  }
+       broken_pipe = True;
 }
 
 static void
 input(gpointer data, gint source, GdkInputCondition condition)
 {
 }
 
 static void
 input(gpointer data, gint source, GdkInputCondition condition)
 {
-  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((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); /* Error occurred */
-      }
-      gs->input_buffer_ptr = gs->input_buffer;
-      gs->bytes_left -= gs->buffer_bytes_left;
-    }
-
-    if(gs->buffer_bytes_left > 0) {
-      /* g_print (" writing: %s\n",gs->input_buffer_ptr); */
-
-      bytes_written = write(gs->interpreter_input,
-                            gs->input_buffer_ptr, gs->buffer_bytes_left);
-
-      if(broken_pipe) {
-        ps_document_emit_error_msg(gs, g_strdup(_("Broken pipe.")));
-        broken_pipe = FALSE;
-        interpreter_failed(gs);
-      }
-      else if(bytes_written == -1) {
-        if((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
-          interpreter_failed(gs);   /* 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
 }
 
 static int
-start_interpreter(PSDocument * gs)
+start_interpreter (PSDocument *gs)
 {
 {
-  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 */
+       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
 
 
 #define NUM_ARGS    100
 #define NUM_GS_ARGS (NUM_ARGS - 20)
 #define NUM_ALPHA_ARGS 10
 
-  char *argv[NUM_ARGS], *dir, *gv_env;
-  char **gs_args, **alpha_args = NULL;
-  int argc = 0, i;
-
-  if(!gs->gs_filename)
-    return 0;
-
-  stop_interpreter(gs);
-
-  if(gs->disable_start == TRUE)
-    return 0;
-
-  /* set up the args... */
-  gs_args = g_strsplit(gtk_gs_defaults_get_interpreter_cmd(), " ", NUM_GS_ARGS);
-  for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++)
-    argv[argc] = gs_args[i];
-
-  if(gs->antialiased) {
-    if(strlen(gtk_gs_defaults_get_alpha_parameters()) == 0)
-      alpha_args = g_strsplit(ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
-    else
-      alpha_args = g_strsplit(gtk_gs_defaults_get_alpha_parameters(),
-                              " ", NUM_ALPHA_ARGS);
-    for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++)
-      argv[argc] = alpha_args[i];
-  }
-  else
-    argv[argc++] = "-sDEVICE=x11";
-  argv[argc++] = "-dNOPAUSE";
-  argv[argc++] = "-dQUIET";
-  /* I assume we do _not_ want to change this... (: */
-  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;
-  }
-
-  gs->busy = TRUE;
-  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 = g_strdup_printf("GHOSTVIEW=%ld %ld",
-                             gdk_x11_drawable_get_xid(gs->pstarget),
-                            gdk_x11_drawable_get_xid(gs->bpixmap));
-    putenv(gv_env);
-
-    /* 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_print("Unable to execute [%s]\n", argv[0]);
-    g_strfreev(gs_args);
-    g_free(gv_env);
-    if(alpha_args)
-      g_strfreev(alpha_args);
-    _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);
-    break;
-  }
-  return TRUE;
+       char *argv[NUM_ARGS], *dir, *gv_env, *gs_path;
+       char **gs_args, **alpha_args = NULL;
+       int argc = 0, i;
+
+       LOG ("Start the interpreter");
+
+       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",
+                                 gdk_x11_drawable_get_xid (gs->pstarget),
+                                 gdk_x11_drawable_get_xid (gs->bpixmap));
+       LOG ("Launching ghostview with env %s", gv_env);
+
+       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]);
+                               }
+                       }
+
+                       putenv(gv_env);
+
+                       /* 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_free (gv_env);
+                       g_strfreev (alpha_args);
+                       _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);
+                       break;
+       }
+
+       return TRUE;
 }
 
 static void
 stop_interpreter(PSDocument * gs)
 {
 }
 
 static void
 stop_interpreter(PSDocument * gs)
 {
-  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) {
-      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
 }
 
 /* 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)
 {
 }
 
 /* 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);
 }
 
 /*
 }
 
 /*
@@ -932,754 +832,343 @@ file_readable(const char *filename)
  * Return name of input file to use or NULL on error..
  */
 static gchar *
  * 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);
-    ps_document_emit_error_msg(gs, buf);
-    if(file_length(filename_err) > 0) {
-      FILE *err;
-      if((err = fopen(filename_err, "r"))) {
-        /* print file to message window */
-        while(fgets(buf, 1024, err))
-          ps_document_emit_error_msg(gs, buf);
-        fclose(err);
-      }
-    }
-    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 ();
+               }
+       }
 
 
-/*
- * Check if gs->gs_filename or gs->gs_filename_unc is a pdf file and scan
- * pdf file if necessary.
- * Set gs->filename_dsc to the name of the dsc file or NULL.
- * Error reporting via signal 'interpreter_message'.
- */
-static gchar *
-check_pdf(PSDocument * gs)
-{
-  FILE *file;
-  gchar buf[1024], *filename;
-  int fd;
-
-  /* use uncompressed file as input if necessary */
-  filename = (gs->gs_filename_unc ? gs->gs_filename_unc : gs->gs_filename);
-
-  if((file = fopen(filename, "r"))
-     && (fread(buf, sizeof(char), 5, file) == 5)
-     && (strncmp(buf, "%PDF-", 5) == 0)) {
-    /* we found a PDF file */
-    gchar *fname, *filename_dsc, *filename_err, *cmd, *cmdline;
-    filename_dsc = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
-    if((fd = mkstemp(filename_dsc)) < 0) {
-      return NULL;
-    }
-    close(fd);
-    filename_err = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
-    if((fd = mkstemp(filename_err)) < 0) {
-      g_free(filename_dsc);
-      return NULL;
-    }
-    close(fd);
-    fname = g_shell_quote(filename);
-    cmd = g_strdup_printf(gtk_gs_defaults_get_dsc_cmd(), filename_dsc, fname);
-    g_free(fname);
-    /* this command (sometimes?) prints error messages to stdout! */
-    cmdline = g_strdup_printf("%s >%s 2>&1", cmd, filename_err);
-    g_free(cmd);
-
-    if((system(cmdline) == 0) && file_readable(filename_dsc)) {
-
-      /* success */
-      filename = gs->gs_filename_dsc = filename_dsc;
-
-      if(file_length(filename_err) > 0) {
-        gchar *err_msg = " ";
-        GtkWidget *dialog;
-        FILE *err;
-        GdkColor color;
-
-        if((err = fopen(filename_err, "r"))) {
-
-          /* print the content of the file to a message box */
-          while(fgets(buf, 1024, err))
-            err_msg = g_strconcat(err_msg, buf, NULL);
-
-          /* FIXME The dialog is not yet set to modal, difficult to 
-           * get the parent of the dialog box here 
-           */
-
-          dialog = gtk_message_dialog_new(NULL,
-                                          GTK_DIALOG_MODAL,
-                                          GTK_MESSAGE_WARNING,
-                                          GTK_BUTTONS_OK,
-                                          ("There was an error while scaning the file: %s \n%s"),
-                                          gs->gs_filename, err_msg);
-
-          gdk_color_parse("white", &color);
-          gtk_widget_modify_bg(GTK_WIDGET(dialog), GTK_STATE_NORMAL, &color);
-
-          g_signal_connect(G_OBJECT(dialog), "response",
-                           G_CALLBACK(gtk_widget_destroy), NULL);
-
-          gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
-          gtk_widget_show(dialog);
-          g_free(err_msg);
-        }
-      }
-
-    }
-    else {
-      /* report error */
-      g_snprintf(buf, 1024,
-                 _("Error while converting pdf file %s:\n"), filename);
-      ps_document_emit_error_msg(gs, buf);
-
-      if(file_length(filename_err) > 0) {
-        FILE *err;
-        if((err = fopen(filename_err, "r"))) {
-          /* print file to message window */
-          while(fgets(buf, 1024, err))
-            ps_document_emit_error_msg(gs, buf);
-        }
-      }
-      unlink(filename_dsc);
-      g_free(filename_dsc);
-      filename = NULL;
-    }
-    unlink(filename_err);
-    g_free(filename_err);
-    g_free(cmdline);
-  }
-  if(NULL != file)
-    fclose(file);
-  return filename;
-}
+       if (NULL != file)
+               fclose(file);
 
 
-#ifdef BROKEN_XINERAMA_PATCH_THAT_SHOULD_NOT_BE_USED
-/* never mind this patch: a properly working X server should take care of
-   calculating the proper values. */
-static float
-compute_xdpi(void)
-{
-#   ifndef HAVE_XINERAMA
-  return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
-#   else
-  Display *dpy;
-  dpy = (Display *) GDK_DISPLAY();
-  if(XineramaIsActive(dpy)) {
-    int num_heads;
-    XineramaScreenInfo *head_info;
-    head_info = (XineramaScreenInfo *) XineramaQueryScreens(dpy, &num_heads);
-    /* fake it with dimensions of the first head for now */
-    return 25.4 * head_info[0].width / gdk_screen_width_mm();
-  }
-  else {
-    return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
-  }
-#   endif
-  /* HAVE_XINERAMA */
-}
+       if (!cmd)
+               return gs->gs_filename;
 
 
-static float
-compute_ydpi(void)
-{
-#   ifndef HAVE_XINERAMA
-  return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
-#   else
-  Display *dpy;
-  dpy = (Display *) GDK_DISPLAY();
-  if(XineramaIsActive(dpy)) {
-    int num_heads;
-    XineramaScreenInfo *head_info;
-    head_info = (XineramaScreenInfo *) XineramaQueryScreens(dpy, &num_heads);
-    /* fake it with dimensions of the first head for now */
-    return 25.4 * head_info[0].height / gdk_screen_height_mm();
-  }
-  else {
-    return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
-  }
-#   endif
-  /* HAVE_XINERAMA */
-}
-#else
-static float
-compute_xdpi(void)
-{
-  return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
-}
+       /* 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);
+
+       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;
+       }
+       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;
+       }
 
 
-static float
-compute_ydpi(void)
-{
-  return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
-}
-#endif /* BROKEN_XINERAMA_PATCH_THAT_SHOULD_NOT_BE_USED */
+       unlink (filename_err);
+       g_free (filename_err);
+       g_free (cmdline);
+       g_free (filename);
 
 
-/* Compute new size of window, sets xdpi and ydpi if necessary.
- * returns True if new window size is different */
-static gboolean
-compute_size(PSDocument * gs)
-{
-  guint new_width = 1;
-  guint new_height = 1;
-  gboolean change = FALSE;
-  gint orientation;
-
-  /* width and height can be changed, calculate window size according */
-  /* to xpdi and ydpi */
-  orientation = ps_document_get_orientation(gs);
-
-  switch (orientation) {
-  case GTK_GS_ORIENTATION_PORTRAIT:
-  case GTK_GS_ORIENTATION_UPSIDEDOWN:
-    new_width = (gs->urx - gs->llx) / 72.0 * gs->xdpi + 0.5;
-    new_height = (gs->ury - gs->lly) / 72.0 * gs->ydpi + 0.5;
-    break;
-  case GTK_GS_ORIENTATION_LANDSCAPE:
-  case GTK_GS_ORIENTATION_SEASCAPE:
-    new_width = (gs->ury - gs->lly) / 72.0 * gs->xdpi + 0.5;
-    new_height = (gs->urx - gs->llx) / 72.0 * gs->ydpi + 0.5;
-    break;
-  }
-
-  change = (new_width != gs->width * gs->zoom_factor)
-    || (new_height != gs->height * gs->zoom_factor);
-  gs->width = (gint) (new_width * gs->zoom_factor);
-  gs->height = (gint) (new_height * gs->zoom_factor);
-
-  return (change);
+       return filename_unc;
 }
 
 static gint
 }
 
 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(GTK_IS_GS(gs), FALSE);
+       g_return_val_if_fail (PS_IS_DOCUMENT (gs), FALSE);
 
 
-  if(!gs->gs_filename)
-    return 0;
+       if (!gs->gs_filename)
+               return 0;
 
 
-  gs->disable_start = FALSE;
-  
-  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;
+       }
 
 
-/*
- * Show error message -> send signal "interpreter_message"
- */
-static void
-ps_document_emit_error_msg(PSDocument * gs, const gchar * msg)
-{
-  gdk_pointer_ungrab(GDK_CURRENT_TIME);
-  if(strstr(msg, "Error:")) {
-    gs->gs_status = _("File is not a valid PostScript document.");
-    ps_document_cleanup(gs);
-  }
-}
+       /* 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;
+               }
+       }
 
 
-static gboolean
-document_load(PSDocument * gs, const gchar * fname)
-{
-  g_return_val_if_fail(gs != NULL, FALSE);
-  g_return_val_if_fail(GTK_IS_GS(gs), FALSE);
-
-  /* clean up previous document */
-  ps_document_cleanup(gs);
-
-  if(fname == NULL) {
-    gs->gs_status = "";
-    return FALSE;
-  }
-
-  /* prepare this document */
-
-  /* default values: no dsc information available  */
-  gs->structured_doc = FALSE;
-  gs->send_filename_to_gs = TRUE;
-  gs->current_page = -2;
-  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);
-      ps_document_emit_error_msg(gs, buf);
-      gs->gs_status = _("File is not readable.");
-    }
-    else {
-      filename = check_filecompressed(gs);
-      if(filename)
-        filename = check_pdf(gs);
-    }
-
-    if(!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
-      ps_document_cleanup(gs);
-      return FALSE;
-    }
-
-    /* we grab the vital statistics!!! */
-    gs->doc = psscan(gs->gs_psfile, gs->respect_eof, 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);
-      ps_document_emit_error_msg(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;
-    }
-
-    /* We have to set up the orientation of the document */
-
-
-    /* orientation can only be portrait, and landscape or none.
-       This is the document default. A document can have
-       pages in landscape and some in portrait */
-    if(gs->override_orientation) {
-      /* If the orientation should be override... 
-         then gs->orientation has already the correct
-         value (it was set when the widget was created */
-      /* So do nothing */
-
-    }
-    else {
-      /* Otherwise, set the proper orientation for the doc */
-      gs->real_orientation = gs->doc->orientation;
-    }
-  }
-  ps_document_set_page_size(gs, -1, gs->current_page);
-  gs->loaded = TRUE;
-
-  gs->gs_status = _("Document loaded.");
-
-  return gs->loaded;
-}
+       gs->gs_status = _("Document loaded.");
 
 
+       return TRUE;
+}
 
 static gboolean
 
 static gboolean
-ps_document_next_page(PSDocument * gs)
+ps_document_next_page (PSDocument *gs)
 {
 {
-  XEvent event;
-
-  g_return_val_if_fail(gs != NULL, FALSE);
-  g_return_val_if_fail(GTK_IS_GS(gs), FALSE);
+       XEvent event;
 
 
-  if(gs->interpreter_pid == 0) {    /* no interpreter active */
-    return FALSE;
-  }
+       LOG ("Make ghostscript render next page");
 
 
-  if(gs->busy) {                /* interpreter is busy */
-    return FALSE;
-  }
+       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);
 
 
-  gs->busy = TRUE;
+       gs->busy = TRUE;
 
 
-  event.xclient.type = ClientMessage;
-  event.xclient.display = gdk_display;
-  event.xclient.window = gs->message_window;
-  event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
-  event.xclient.format = 32;
+       event.xclient.type = ClientMessage;
+       event.xclient.display = gdk_display;
+       event.xclient.window = gs->message_window;
+       event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
+       event.xclient.format = 32;
 
 
-  gdk_error_trap_push();
-  XSendEvent(gdk_display, gs->message_window, FALSE, 0, &event);
-  gdk_flush();
-  gdk_error_trap_pop();
+       gdk_error_trap_push ();
+       XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
+       gdk_flush ();
+       gdk_error_trap_pop ();
 
 
-  return TRUE;
+       return TRUE;
 }
 
 }
 
-static gint
-ps_document_get_current_page(PSDocument * gs)
+static gboolean
+render_page (PSDocument *gs, int page)
 {
 {
-  g_return_val_if_fail(gs != NULL, -1);
-  g_return_val_if_fail(GTK_IS_GS(gs), -1);
+       g_return_val_if_fail(gs != NULL, FALSE);
+       g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
 
 
-  return gs->current_page;
-}
+       if(!gs->gs_filename) {
+               return FALSE;
+       }
 
 
-static gint
-ps_document_get_page_count(PSDocument * gs)
-{
-  if(!gs->gs_filename)
-    return 0;
-
-  if(gs->doc) {
-    if(gs->structured_doc)
-      return gs->doc->numpages;
-    else
-      return G_MAXINT;
-  }
-  else
-    return 0;
+       if (gs->structured_doc && gs->doc) {
+               LOG ("It's a structured document, let's send one page to gs");
+
+               if (is_interpreter_ready (gs)) {
+                       ps_document_next_page (gs);
+               } else {
+                       ps_document_enable_interpreter (gs);
+                       send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
+                       send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
+               }
+
+               send_ps (gs, gs->doc->pages[page].begin,
+                        gs->doc->pages[page].len, FALSE);
+       } else {
+               /* Unstructured document
+                *
+                * In the case of non structured documents,
+                * GS read the PS from the  actual file (via command
+                * line. Hence, ggv only send a signal next page.
+                * If ghostview is not running it is usually because
+                * the last page of the file was displayed. In that
+                * case, ggv restarts GS again and the first page is displayed.
+                */
+
+               LOG ("It's an unstructured document, gs will just read the file");
+
+               if (!is_interpreter_ready (gs)) {
+                       ps_document_enable_interpreter(gs);
+               }
+               ps_document_next_page(gs);
+       }
+
+       return TRUE;
 }
 
 static gboolean
 }
 
 static gboolean
-ps_document_goto_page(PSDocument * gs, gint page)
+ps_document_load (EvDocument  *document,
+                 const char  *uri,
+                 GError     **error)
 {
 {
-  g_return_val_if_fail(gs != NULL, FALSE);
-  g_return_val_if_fail(GTK_IS_GS(gs), FALSE);
-
-  if(!gs->gs_filename) {
-    return FALSE;
-  }
-
-  /* range checking... */
-  if(page < 0)
-    page = 0;
-
-  if(gs->structured_doc && gs->doc) {
-    if(page >= gs->doc->numpages)
-      page = gs->doc->numpages - 1;
-
-    if(page == gs->current_page && !gs->changed)
-      return TRUE;
-
-    gs->current_page = page;
-
-    if(gs->doc->pages[page].orientation != NONE &&
-       !gs->override_orientation &&
-       gs->doc->pages[page].orientation != gs->real_orientation) {
-      gs->real_orientation = gs->doc->pages[page].orientation;
-      gs->changed = TRUE;
-    }
-
-    ps_document_set_page_size(gs, -1, page);
-
-    gs->changed = FALSE;
-
-    if(is_interpreter_ready(gs)) {
-      ps_document_next_page(gs);
-    }
-    else {
-      ps_document_enable_interpreter(gs);
-      send_ps(gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
-      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);
-  }
-  else {
-    /* Unstructured document */
-    /* In the case of non structured documents,
-       GS read the PS from the  actual file (via command
-       line. Hence, ggv only send a signal next page.
-       If ghostview is not running it is usually because
-       the last page of the file was displayed. In that
-       case, ggv restarts GS again and the first page is displayed.
-     */
-    if(page == gs->current_page && !gs->changed)
-      return TRUE;
-
-    ps_document_set_page_size(gs, -1, page);
-
-    if(!is_interpreter_ready(gs))
-      ps_document_enable_interpreter(gs);
-
-    gs->current_page = page;
-
-    ps_document_next_page(gs);
-  }
-  return TRUE;
+       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;
 }
 
 }
 
-/*
- * set pagesize sets the size from
- * if new_pagesize is -1, then it is set to either
- *  a) the default settings of pageid, if they exist, or if pageid != -1.
- *  b) the default setting of the document, if it exists.
- *  c) the default setting of the widget.
- * otherwise, the new_pagesize is used as the pagesize
- */
 static gboolean
 static gboolean
-ps_document_set_page_size(PSDocument * gs, gint new_pagesize, gint pageid)
+save_document (PSDocument *document, const char *filename)
 {
 {
-  gint new_llx = 0;
-  gint new_lly = 0;
-  gint new_urx = 0;
-  gint new_ury = 0;
-  GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes();
-
-  g_return_val_if_fail(gs != NULL, FALSE);
-  g_return_val_if_fail(GTK_IS_GS(gs), FALSE);
-
-  if(new_pagesize == -1) {
-    if(gs->default_size > 0)
-      new_pagesize = gs->default_size;
-    if(!gs->override_size && 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((pageid >= 0) && (gs->doc->numpages > pageid) &&
-         (gs->doc->pages) && (gs->doc->pages[pageid].size)) {
-        new_pagesize = gs->doc->pages[pageid].size - gs->doc->size;
-      }
-      else if(gs->doc->default_page_size != NULL) {
-        new_pagesize = gs->doc->default_page_size - gs->doc->size;
-      }
-      else if((pageid >= 0) &&
-              (gs->doc->numpages > pageid) &&
-              (gs->doc->pages) &&
-              (gs->doc->pages[pageid].boundingbox[URX] >
-               gs->doc->pages[pageid].boundingbox[LLX]) &&
-              (gs->doc->pages[pageid].boundingbox[URY] >
-               gs->doc->pages[pageid].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 && !gs->override_size) || new_pagesize == -1)) {    /* epsf or bbox */
-    if((pageid >= 0) &&
-       (gs->doc->pages) &&
-       (gs->doc->pages[pageid].boundingbox[URX] >
-        gs->doc->pages[pageid].boundingbox[LLX])
-       && (gs->doc->pages[pageid].boundingbox[URY] >
-           gs->doc->pages[pageid].boundingbox[LLY])) {
-      /* use page bbox */
-      new_llx = gs->doc->pages[pageid].boundingbox[LLX];
-      new_lly = gs->doc->pages[pageid].boundingbox[LLY];
-      new_urx = gs->doc->pages[pageid].boundingbox[URX];
-      new_ury = gs->doc->pages[pageid].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 = gs->default_size;
-    new_llx = new_lly = 0;
-    if(gs->doc && !gs->override_size && 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;
-
-  /* If bounding box changed, setup for new size. */
-  /* ps_document_disable_interpreter (gs); */
-  if((new_llx != gs->llx) || (new_lly != gs->lly) ||
-     (new_urx != gs->urx) || (new_ury != gs->ury)) {
-    gs->llx = new_llx;
-    gs->lly = new_lly;
-    gs->urx = new_urx;
-    gs->ury = new_ury;
-    gs->changed = TRUE;
-  }
-
-  if(gs->changed) {
-    set_up_page(gs);
-    return TRUE;
-  }
-
-  return FALSE;
-}
+       gboolean result = TRUE;
+       GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
+       FILE *f, *src_file;
+       gchar *buf;
 
 
-static gfloat
-ps_document_zoom_to_fit(PSDocument * gs, gboolean fit_width)
-{
-  gint new_y;
-  gfloat new_zoom;
-  guint avail_w, avail_h;
+       src_file = fopen (PS_DOCUMENT_GET_PS_FILE(document), "r");
+       if (src_file) {
+               struct stat stat_rec;
 
 
-  g_return_val_if_fail(gs != NULL, 0.0);
-  g_return_val_if_fail(GTK_IS_GS(gs), 0.0);
+               if (stat (PS_DOCUMENT_GET_PS_FILE(document), &stat_rec) == 0) {
+                       pscopy (src_file, sink, 0, stat_rec.st_size - 1);
+               }
 
 
-  avail_w = (gs->avail_w > 0) ? gs->avail_w : gs->width;
-  avail_h = (gs->avail_h > 0) ? gs->avail_h : gs->height;
+               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;
+       }
 
 
-  new_zoom = ((gfloat) avail_w) / ((gfloat) gs->width) * gs->zoom_factor;
-  if(!fit_width) {
-    new_y = new_zoom * ((gfloat) gs->height) / gs->zoom_factor;
-    if(new_y > avail_h)
-      new_zoom = ((gfloat) avail_h) / ((gfloat) gs->height) * gs->zoom_factor;
-  }
+       g_free (buf);
+       gtk_gs_doc_sink_free (sink);
+       g_free (sink);
 
 
-  return new_zoom;
+       return result;
 }
 
 }
 
-static void
-ps_document_set_zoom(PSDocument * gs, gfloat zoom)
+static gboolean
+save_page_list (PSDocument *document, int *page_list, const char *filename)
 {
 {
-  g_return_if_fail(gs != NULL);
-  g_return_if_fail(GTK_IS_GS(gs));
-
-  switch (gs->zoom_mode) {
-  case GTK_GS_ZOOM_FIT_WIDTH:
-    zoom = ps_document_zoom_to_fit(gs, TRUE);
-    break;
-  case GTK_GS_ZOOM_FIT_PAGE:
-    zoom = ps_document_zoom_to_fit(gs, FALSE);
-    break;
-  case GTK_GS_ZOOM_ABSOLUTE:
-  default:
-    break;
-  }
-
-  if(fabs(gs->zoom_factor - zoom) > 0.001) {
-    gs->zoom_factor = zoom;
-    set_up_page(gs);
-    gs->changed = TRUE;
-  }
-
-  ps_document_goto_page(gs, gs->current_page);
+       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
 }
 
 static gboolean
-ps_document_load (EvDocument  *document,
+ps_document_save (EvDocument  *document,
                  const char  *uri,
                  GError     **error)
 {
                  const char  *uri,
                  GError     **error)
 {
+       PSDocument *ps = PS_DOCUMENT (document);
        gboolean result;
        char *filename;
 
        gboolean result;
        char *filename;
 
@@ -1687,7 +1176,7 @@ ps_document_load (EvDocument  *document,
        if (!filename)
                return FALSE;
 
        if (!filename)
                return FALSE;
 
-       result = document_load (PS_DOCUMENT (document), filename);
+       result = save_document (ps, filename);
 
        g_free (filename);
 
 
        g_free (filename);
 
@@ -1697,127 +1186,150 @@ ps_document_load (EvDocument  *document,
 static int
 ps_document_get_n_pages (EvDocument  *document)
 {
 static int
 ps_document_get_n_pages (EvDocument  *document)
 {
-       return ps_document_get_page_count (PS_DOCUMENT (document));
-}
+       PSDocument *ps = PS_DOCUMENT (document);
 
 
-static void
-ps_document_set_page (EvDocument  *document,
-                      int          page)
-{
-       ps_document_goto_page (PS_DOCUMENT (document), page);
-}
+       g_return_val_if_fail (ps != NULL, -1);
 
 
-static int
-ps_document_get_page (EvDocument  *document)
-{
-       return ps_document_get_current_page (PS_DOCUMENT (document));
+       if (!ps->gs_filename || !ps->doc) {
+               return -1;
+       }
+
+       return ps->structured_doc ? ps->doc->numpages : 1;
 }
 
 }
 
-static gboolean
-ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
+static void
+ps_document_get_page_size (EvDocument   *document,
+                          int           page,
+                          double       *width,
+                          double       *height)
 {
 {
-       PSDocument *gs = (PSDocument *) data;
+       PSDocument *gs = PS_DOCUMENT (document);
+       int urx, ury, llx, lly;
 
 
-       if(event->type != GDK_CLIENT_EVENT)
-               return FALSE;
+       get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
 
 
-       if (event->client.message_type == gs_class->page_atom) {
-               gs->busy = FALSE;
-               ev_document_changed (EV_DOCUMENT (gs));
+       if (width) {
+               *width = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
        }
 
        }
 
-       return TRUE;
+       if (height) {
+               *height = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
+       }
+}
+
+static gboolean
+ps_document_can_get_text (EvDocument *document)
+{
+       return FALSE;
 }
 
 static void
 }
 
 static void
-ps_document_set_target (EvDocument  *document,
-                       GdkDrawable *target)
+ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer, int page, double scale, int rotation)
 {
 {
-       PSDocument *gs = PS_DOCUMENT (document);
-       GtkWidget *widget;
-       gpointer data;
+       PSDocument *gs = PS_DOCUMENT (renderer);
 
 
-       gs->pstarget = target;
+       if (gs->pstarget == NULL) {
+               gs->target_window = gtk_window_new (GTK_WINDOW_POPUP);
+               gtk_widget_realize (gs->target_window);
+               gs->pstarget = gs->target_window->window;
 
 
-       if (gs->pstarget) {
-               gdk_window_get_user_data (gs->pstarget, &data);
-               g_return_if_fail (GTK_IS_WIDGET (data));
+               g_assert (gs->pstarget != NULL);
 
 
-               widget = GTK_WIDGET (data);
-               g_signal_connect (widget, "event",
-                                 G_CALLBACK (ps_document_widget_event),
-                                 document);
+               g_signal_connect (gs->target_window, "event",
+                                 G_CALLBACK (ps_document_widget_event),
+                                 gs);
        }
 
        }
 
-       ps_document_goto_page (gs, gs->current_page);
+       setup_pixmap (gs, page, scale, rotation);
+       setup_page (gs, page, scale, rotation);
+
+       render_page (gs, page);
+}
+
+static EvDocumentInfo *
+ps_document_get_info (EvDocument *document)
+{
+       EvDocumentInfo *info;
+       PSDocument *ps = PS_DOCUMENT (document);
+
+       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
 }
 
 static void
-ps_document_set_scale (EvDocument  *document,
-                       double       scale)
+ps_document_document_iface_init (EvDocumentIface *iface)
 {
 {
-       ps_document_set_zoom (PS_DOCUMENT (document), scale);
+       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
 }
 
 static void
-ps_document_set_page_offset (EvDocument  *document,
-                             int          x,
-                             int          y)
+ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
 {
 {
+       iface->render_pixbuf = ps_async_renderer_render_pixbuf;
 }
 
 static void
 }
 
 static void
-ps_document_get_page_size (EvDocument   *document,
-                           int          *width,
-                           int          *height)
+ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
+                            int first_page, int last_page,
+                             double width, double height, gboolean duplex)
 {
 {
-       PSDocument *gs = PS_DOCUMENT (document);
+       PSDocument *document = PS_DOCUMENT (exporter);
 
 
-       if (width) {
-               *width = gs->width;
+       if (document->structured_doc) {
+               g_free (document->ps_export_pagelist);
+       
+               document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
        }
 
        }
 
-       if (height) {
-               *height = gs->height;
-       }
+       document->ps_export_filename = g_strdup (filename);
 }
 
 static void
 }
 
 static void
-ps_document_render (EvDocument  *document,
-                   int          clip_x,
-                   int          clip_y,
-                   int          clip_width,
-                   int          clip_height)
+ps_document_ps_export_do_page (EvPSExporter *exporter, EvRenderContext *rc)
 {
 {
-       PSDocument *gs = PS_DOCUMENT (document);
-       GdkGC *gc;
-
-       if (gs->pstarget == NULL ||
-           gs->bpixmap == NULL) {
-               return;
+       PSDocument *document = PS_DOCUMENT (exporter);
+       
+       if (document->structured_doc) {
+               document->ps_export_pagelist[rc->page] = 1;
        }
        }
+}
 
 
-       gc = gdk_gc_new (gs->pstarget);
-
-       gdk_draw_drawable (gs->pstarget, gc,
-                          gs->bpixmap,
-                          clip_x, clip_y,
-                          clip_x, clip_y,
-                          clip_width, clip_height);
-
-       g_object_unref (gc);
+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
 }
 
 static void
-ps_document_document_iface_init (EvDocumentIface *iface)
+ps_document_ps_exporter_iface_init (EvPSExporterIface *iface)
 {
 {
-       iface->load = ps_document_load;
-       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->set_target = ps_document_set_target;
-       iface->set_page_offset = ps_document_set_page_offset;
-       iface->get_page_size = ps_document_get_page_size;
-       iface->render = ps_document_render;
+       iface->begin = ps_document_ps_export_begin;
+       iface->do_page = ps_document_ps_export_do_page;
+       iface->end = ps_document_ps_export_end;
 }
 }