]> www.fi.muni.cz Git - evince.git/commitdiff
Add support for thumbnails in ps backend. Fixes bug #164755.
authorCarlos Garcia Campos <carlosgc@gnome.org>
Tue, 13 Feb 2007 15:32:24 +0000 (15:32 +0000)
committerCarlos Garcia Campos <carlosgc@src.gnome.org>
Tue, 13 Feb 2007 15:32:24 +0000 (15:32 +0000)
2007-02-13  Carlos Garcia Campos  <carlosgc@gnome.org>
* backend/ps/Makefile.am:
* backend/ps/ps-document.[ch]: (ps_document_init), (ps_document_dispose),
(document_load), (ps_document_load), (save_document),
(save_page_list), (ps_document_get_n_pages),
(ps_document_get_page_size), (ps_document_get_info),
(ps_interpreter_page_rendered), (ps_async_renderer_render_pixbuf),
(ps_interpreter_thumbnail_rendered), (ps_document_render_thumbnail),
(ps_document_thumbnails_get_thumbnail),
(ps_document_thumbnails_get_dimensions),
(ps_document_document_thumbnails_iface_init):
* backend/ps/ps.[ch]: (psgetpagebox):
* backend/ps/ps-interpreter.[ch]:
Add support for thumbnails in ps backend. Fixes bug #164755.

svn path=/trunk/; revision=2323

ChangeLog
backend/ps/Makefile.am
backend/ps/ps-document.c
backend/ps/ps-document.h
backend/ps/ps-interpreter.c [new file with mode: 0644]
backend/ps/ps-interpreter.h [new file with mode: 0644]
backend/ps/ps.c
backend/ps/ps.h
libdocument/ev-render-context.c

index dbb4df6a1c3c9a141e6e98987529ac70c6fb2ba7..ae157aadf2d05a35a28e6a2b944f8a4fca13b4c1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2007-02-13  Carlos Garcia Campos  <carlosgc@gnome.org>
+
+       * backend/ps/Makefile.am:
+       * backend/ps/ps-document.[ch]: (ps_document_init), (ps_document_dispose),
+       (document_load), (ps_document_load), (save_document),
+       (save_page_list), (ps_document_get_n_pages),
+       (ps_document_get_page_size), (ps_document_get_info),
+       (ps_interpreter_page_rendered), (ps_async_renderer_render_pixbuf),
+       (ps_interpreter_thumbnail_rendered), (ps_document_render_thumbnail),
+       (ps_document_thumbnails_get_thumbnail),
+       (ps_document_thumbnails_get_dimensions),
+       (ps_document_document_thumbnails_iface_init):
+       * backend/ps/ps.[ch]: (psgetpagebox):
+       * backend/ps/ps-interpreter.[ch]:
+
+       Add support for thumbnails in ps backend. Fixes bug #164755. 
+
 2007-02-13  Nickolay V. Shmyrev  <nshmyrev@yandex.ru>
 
        * NEWS:
index 9e813d69733bf4a109bee4aa8daea02c72b794ec..e443b4a957eaba0325f112aad152cf91d7a2d971 100644 (file)
@@ -15,6 +15,8 @@ libpsdocument_la_SOURCES =    \
        ps.h                    \
        ps-document.c           \
        ps-document.h           \
+       ps-interpreter.c        \
+       ps-interpreter.h        \
        gsdefaults.c            \
        gsdefaults.h
 
index 05533c37da32837b6e907663cedb972ec2aa7404..086f082125762db12612b7c11f1b694b4ae97700 100644 (file)
 
 #include <glib/gstdio.h>
 #include <glib/gi18n.h>
-#include <gtk/gtkwidget.h>
-#include <gtk/gtkwindow.h>
-#include <gdk/gdk.h>
-#include <gdk/gdkx.h>
+#include <gdk/gdkpixbuf.h>
 #include <string.h>
 #include <stdlib.h>
-#include <sys/wait.h>
-#include <errno.h>
 
 #include "ps-document.h"
+#include "ps-interpreter.h"
 #include "ps.h"
 #include "gstypes.h"
 #include "gsdefaults.h"
 #include "ev-file-exporter.h"
 #include "ev-async-renderer.h"
-
-#define MAX_BUFSIZE 1024
-
-/* structure to describe section of file to send to ghostscript */
-typedef struct {
-       FILE *fp;
-       glong begin;
-       guint len;
-       gboolean seek_needed;
-       gboolean close;
-} PSSection;
+#include "ev-document-thumbnails.h"
+#include "ev-document-misc.h"
 
 struct _PSDocument {
        GObject object;
 
-       GtkWidget *target_window;
-       GdkWindow *pstarget;
-       GdkPixmap *bpixmap;
-       glong message_window;          /* Used by ghostview to receive messages from app */
-
-       GPid interpreter_pid;               /* PID of interpreter, -1 if none  */
-       GIOChannel *interpreter_input;      /* stdin of interpreter            */
-       GIOChannel *interpreter_output;     /* stdout of interpreter           */
-       GIOChannel *interpreter_err;        /* stderr of interpreter           */
-       guint interpreter_input_id;
-       guint interpreter_output_id;
-       guint interpreter_error_id;
-
-       gboolean busy;                /* Is gs busy drawing? */
+       gchar *filename;
+       struct document *doc;
        gboolean structured_doc;
 
-       GQueue *ps_input;
-       gchar *input_buffer_ptr;
-       guint bytes_left;
-       guint buffer_bytes_left;
-
-       FILE *gs_psfile;              /* the currently loaded FILE */
-       gchar *gs_filename;           /* the currently loaded filename */
-       gchar *input_buffer;
-       gboolean send_filename_to_gs; /* True if gs should read from file directly */
-       struct document *doc;
+       PSInterpreter *gs;
 
+       /* Document Thumbnails */
+       PSInterpreter   *thumbs_gs;
+       GdkPixbuf       *thumbnail;
+       EvRenderContext *thumbs_rc;
+       GMutex          *thumbs_mutex;
+       GCond           *thumbs_cond;
+       
+       /* File exporter */
        gint  *ps_export_pagelist;
        gchar *ps_export_filename;
 };
 
 struct _PSDocumentClass {
        GObjectClass parent_class;
-       
-       GdkAtom gs_atom;
-       GdkAtom next_atom;
-       GdkAtom page_atom;
-       GdkAtom string_atom;
 }; 
 
-static void     ps_document_document_iface_init      (EvDocumentIface      *iface);
-static void     ps_document_file_exporter_iface_init (EvFileExporterIface  *iface);
-static void     ps_async_renderer_iface_init         (EvAsyncRendererIface *iface);
-
-static void     ps_interpreter_start                 (PSDocument           *gs);
-static void     ps_interpreter_stop                  (PSDocument           *gs);
-static void     ps_interpreter_failed                (PSDocument           *gs,
-                                                     const gchar          *msg);
-static gboolean ps_interpreter_is_ready              (PSDocument           *gs);
-
-static void     push_pixbuf                          (PSDocument           *gs);
+static void     ps_document_document_iface_init            (EvDocumentIface           *iface);
+static void     ps_document_file_exporter_iface_init       (EvFileExporterIface       *iface);
+static void     ps_async_renderer_iface_init               (EvAsyncRendererIface      *iface);
+static void     ps_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
 
+static void     ps_interpreter_page_rendered               (PSInterpreter             *gs,
+                                                           GdkPixbuf                 *pixbuf,
+                                                           PSDocument                *ps_document);
 
 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_DOCUMENT_THUMBNAILS,
+                                                       ps_document_document_thumbnails_iface_init);
                                 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
                                                        ps_document_file_exporter_iface_init);
                                 G_IMPLEMENT_INTERFACE (EV_TYPE_ASYNC_RENDERER,
                                                        ps_async_renderer_iface_init);
                         });
 
-static void
-ps_section_free (PSSection *section)
-{
-       if (!section)
-               return;
-
-       if (section->close && section->fp)
-               fclose (section->fp);
-
-       g_free (section);
-}
-
 /* PSDocument */
 static void
-ps_document_init (PSDocument *gs)
+ps_document_init (PSDocument *ps_document)
 {
-       gs->bpixmap = NULL;
-       gs->interpreter_pid = -1;
-
-       gs->busy = FALSE;
-       gs->gs_filename = NULL;
-
-       gs->structured_doc = FALSE;
-       gs->send_filename_to_gs = FALSE;
-
-       gs->doc = NULL;
-
-       gs->interpreter_input = NULL;
-       gs->interpreter_output = NULL;
-       gs->interpreter_err = NULL;
-       gs->interpreter_input_id = 0;
-       gs->interpreter_output_id = 0;
-       gs->interpreter_error_id = 0;
-
-       gs->ps_input = g_queue_new ();
-       gs->input_buffer = NULL;
-       gs->input_buffer_ptr = NULL;
-       gs->bytes_left = 0;
-       gs->buffer_bytes_left = 0;
-
-       gs->ps_export_pagelist = NULL;
-       gs->ps_export_filename = NULL;
 }
 
 static void
 ps_document_dispose (GObject *object)
 {
-       PSDocument *gs = PS_DOCUMENT (object);
-       
-       if (gs->gs_psfile) {
-               fclose (gs->gs_psfile);
-               gs->gs_psfile = NULL;
-       }
+       PSDocument *ps_document = PS_DOCUMENT (object);
 
-       if (gs->gs_filename) {
-               g_free (gs->gs_filename);
-               gs->gs_filename = NULL;
+       if (ps_document->gs) {
+               g_object_unref (ps_document->gs);
+               ps_document->gs = NULL;
        }
 
-       if (gs->doc) {
-               psfree (gs->doc);
-               gs->doc = NULL;
+       if (ps_document->thumbs_gs) {
+               g_object_unref (ps_document->thumbs_gs);
+               ps_document->thumbs_gs = NULL;
        }
-
-       if (gs->bpixmap) {
-               g_object_unref (gs->bpixmap);
-               gs->bpixmap = NULL;
+       
+       if (ps_document->filename) {
+               g_free (ps_document->filename);
+               ps_document->filename = NULL;
        }
 
-       if (gs->input_buffer) {
-               g_free (gs->input_buffer);
-               gs->input_buffer = NULL;
+       if (ps_document->doc) {
+               psfree (ps_document->doc);
+               ps_document->doc = NULL;
        }
 
-       if (gs->target_window) {
-               gtk_widget_destroy (gs->target_window);
-               gs->target_window = NULL;
-               gs->pstarget = NULL;
+       if (ps_document->thumbnail) {
+               g_object_unref (ps_document->thumbnail);
+               ps_document->thumbnail = NULL;
        }
 
-       if (gs->ps_input) {
-               g_queue_foreach (gs->ps_input, (GFunc)ps_section_free, NULL);
-               g_queue_free (gs->ps_input);
-               gs->ps_input = NULL;
+       if (ps_document->thumbs_mutex) {
+               g_mutex_free (ps_document->thumbs_mutex);
+               ps_document->thumbs_mutex = NULL;
+       }
+       
+       if (ps_document->thumbs_cond) {
+               g_cond_free (ps_document->thumbs_cond);
+               ps_document->thumbs_cond = NULL;
        }
 
-       ps_interpreter_stop (gs);
+       if (ps_document->thumbs_rc) {
+               g_object_unref (ps_document->thumbs_rc);
+               ps_document->thumbs_rc = NULL;
+       }
 
        G_OBJECT_CLASS (ps_document_parent_class)->dispose (object);
 }
@@ -218,387 +148,15 @@ ps_document_class_init (PSDocumentClass *klass)
        object_class = G_OBJECT_CLASS (klass);
 
        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);
-}
-
-/* PSInterpreter */
-static gboolean
-ps_interpreter_input (GIOChannel   *io,
-                     GIOCondition condition,
-                     PSDocument  *gs)
-{
-       PSSection *section = NULL;
-
-       do {
-               if (gs->buffer_bytes_left == 0) {
-                       /* Get a new section if required */
-                       if (gs->bytes_left == 0) {
-                               ps_section_free (section);
-                               section = NULL;
-                               g_queue_pop_tail (gs->ps_input);
-                       }
-                       
-                       if (section == NULL) {
-                               section = g_queue_peek_tail (gs->ps_input);
-                       }
-
-                       /* Have to seek at the beginning of each section */
-                       if (section && section->seek_needed) {
-                               fseek (section->fp, section->begin, SEEK_SET);
-                               section->seek_needed = FALSE;
-                               gs->bytes_left = section->len;
-                       }
-
-                       if (gs->bytes_left > MAX_BUFSIZE) {
-                               gs->buffer_bytes_left = fread (gs->input_buffer, sizeof (char),
-                                                              MAX_BUFSIZE, section->fp);
-                       } else if (gs->bytes_left > 0) {
-                               gs->buffer_bytes_left = fread (gs->input_buffer, sizeof (char),
-                                                              gs->bytes_left, section->fp);
-                       } else {
-                               gs->buffer_bytes_left = 0;
-                       }
-                       
-                       if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
-                               ps_interpreter_failed (gs, NULL); /* Error occurred */
-                       }
-                       
-                       gs->input_buffer_ptr = gs->input_buffer;
-                       gs->bytes_left -= gs->buffer_bytes_left;
-               }
-
-               if (gs->buffer_bytes_left > 0) {
-                       GIOStatus status;
-                       gsize bytes_written;
-                       GError *error = NULL;
-
-                       status = g_io_channel_write_chars (gs->interpreter_input,
-                                                          gs->input_buffer_ptr,
-                                                          gs->buffer_bytes_left,
-                                                          &bytes_written,
-                                                          &error);
-                       switch (status) {
-                               case G_IO_STATUS_NORMAL:
-                                       gs->buffer_bytes_left -= bytes_written;
-                                       gs->input_buffer_ptr += bytes_written;
-
-                                       break;
-                               case G_IO_STATUS_ERROR:
-                                       ps_interpreter_failed (gs, error->message);
-                                       g_error_free (error);
-                                       
-                                       break;
-                               case G_IO_STATUS_AGAIN:
-                               default:
-                                       break;
-                       }
-               }
-       } while (!g_queue_is_empty (gs->ps_input) && gs->buffer_bytes_left == 0);
-
-       if (g_queue_is_empty (gs->ps_input) && gs->buffer_bytes_left == 0) {
-               GIOFlags flags;
-
-               flags = g_io_channel_get_flags (gs->interpreter_input);
-               
-               g_io_channel_set_flags (gs->interpreter_input,
-                                       flags & ~G_IO_FLAG_NONBLOCK, NULL);
-               g_io_channel_flush (gs->interpreter_input, NULL);
-               g_io_channel_set_flags (gs->interpreter_input,
-                                       flags | G_IO_FLAG_NONBLOCK, NULL);
-               
-               gs->interpreter_input_id = 0;
-               
-               return FALSE;
-       }
-       
-       return TRUE;
-}
-
-static gboolean
-ps_interpreter_output (GIOChannel   *io,
-                      GIOCondition condition,
-                      PSDocument  *gs)
-{
-       gchar buf[MAX_BUFSIZE + 1];
-       gsize bytes = 0;
-       GIOStatus status;
-       GError *error = NULL;
-
-       status = g_io_channel_read_chars (io, buf, MAX_BUFSIZE,
-                                         &bytes, &error);
-       switch (status) {
-               case G_IO_STATUS_NORMAL:
-                       if (bytes > 0) {
-                               buf[bytes] = '\0';
-                               g_print ("%s", buf);
-                       }
-                       break;
-               case G_IO_STATUS_EOF:
-                       g_io_channel_unref (gs->interpreter_output);
-                       gs->interpreter_output = NULL;
-                       gs->interpreter_output_id = 0;
-                       
-                       return FALSE;
-               case G_IO_STATUS_ERROR:
-                       ps_interpreter_failed (gs, error->message);
-                       g_error_free (error);
-                       gs->interpreter_output_id = 0;
-                       
-                       return FALSE;
-               default:
-                       break;
-       }
-       
-       if (!gs->interpreter_err) {
-               ps_interpreter_failed (gs, NULL);
-       }
-
-       return TRUE;
-}
-
-static gboolean
-ps_interpreter_error (GIOChannel   *io,
-                      GIOCondition condition,
-                      PSDocument  *gs)
-{
-       gchar buf[MAX_BUFSIZE + 1];
-       gsize bytes = 0;
-       GIOStatus status;
-       GError *error = NULL;
-
-       status = g_io_channel_read_chars (io, buf, MAX_BUFSIZE,
-                                         &bytes, &error);
-       switch (status) {
-               case G_IO_STATUS_NORMAL:
-                       if (bytes > 0) {
-                               buf[bytes] = '\0';
-                               g_print ("%s", buf);
-                       }
-                       
-                       break;
-               case G_IO_STATUS_EOF:
-                       g_io_channel_unref (gs->interpreter_err);
-                       gs->interpreter_err = NULL;
-                       gs->interpreter_error_id = 0;
-                       
-                       return FALSE;
-               case G_IO_STATUS_ERROR:
-                       ps_interpreter_failed (gs, error->message);
-                       g_error_free (error);
-                       gs->interpreter_error_id = 0;
-                       
-                       break;
-               default:
-                       break;
-       }
-       
-       if (!gs->interpreter_output) {
-               ps_interpreter_failed (gs, NULL);
-       }
-
-       return TRUE;
-}
-
-static void
-ps_interpreter_finished (GPid        pid,
-                        gint        status,
-                        PSDocument *gs)
-{
-       g_spawn_close_pid (gs->interpreter_pid);
-       gs->interpreter_pid = -1;
-       ps_interpreter_failed (gs, NULL);
-}
-
-#define NUM_ARGS    100
-#define NUM_GS_ARGS (NUM_ARGS - 20)
-#define NUM_ALPHA_ARGS 10
-
-static void
-setup_interpreter_env (gchar **envp)
-{
-       gint i;
-
-       for (i = 0; envp[i]; i++)
-               putenv (envp[i]);
-}
-
-static void
-ps_interpreter_start (PSDocument *gs)
-{
-       gchar *argv[NUM_ARGS], *dir, *gv_env, *gs_path;
-       gchar **gs_args, **alpha_args = NULL;
-       gchar **envp;
-       gint pin, pout, perr;
-       gint argc = 0, i;
-       GError *error = NULL;
-       
-       if (!gs->gs_filename)
-               return;
-
-       ps_interpreter_stop (gs);
-
-       dir = g_path_get_dirname (gs->gs_filename);
-
-       /* set up the args... */
-       gs_path = g_find_program_in_path ("gs");
-       gs_args = g_strsplit (gs_path, " ", NUM_GS_ARGS);
-       g_free (gs_path);
-       for (i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
-               argv[argc] = gs_args[i];
-       }
-
-       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";
-
-       if (gs->send_filename_to_gs) {
-               argv[argc++] = gs->gs_filename;
-               argv[argc++] = "-c";
-               argv[argc++] = "quit";
-       } else {
-               argv[argc++] = "-";
-       }
-
-       argv[argc++] = NULL;
-
-       gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld;DISPLAY=%s",
-                                 gdk_x11_drawable_get_xid (gs->pstarget),
-                                 gdk_x11_drawable_get_xid (gs->bpixmap),
-                                 gdk_display_get_name (gdk_drawable_get_display (gs->pstarget)));
-       envp = g_strsplit (gv_env, ";", 2);
-       g_free (gv_env);
-
-       if (g_spawn_async_with_pipes (dir, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
-                                     (GSpawnChildSetupFunc)setup_interpreter_env, envp,
-                                     &(gs->interpreter_pid),
-                                     &pin, &pout, &perr,
-                                     &error)) {
-               GIOFlags flags;
-
-               g_child_watch_add (gs->interpreter_pid,
-                                  (GChildWatchFunc)ps_interpreter_finished, 
-                                  gs);
-
-               gs->interpreter_input = g_io_channel_unix_new (pin);
-               g_io_channel_set_encoding (gs->interpreter_input, NULL, NULL);
-               flags = g_io_channel_get_flags (gs->interpreter_input);
-               g_io_channel_set_flags (gs->interpreter_input,
-                                       flags | G_IO_FLAG_NONBLOCK, NULL);
-
-               
-               gs->interpreter_output = g_io_channel_unix_new (pout);
-               flags = g_io_channel_get_flags (gs->interpreter_output);
-               g_io_channel_set_flags (gs->interpreter_output,
-                                       flags | G_IO_FLAG_NONBLOCK, NULL);
-               gs->interpreter_output_id =
-                       g_io_add_watch (gs->interpreter_output, G_IO_IN,
-                                       (GIOFunc)ps_interpreter_output,
-                                       gs);
-               
-               gs->interpreter_err = g_io_channel_unix_new (perr);
-               flags = g_io_channel_get_flags (gs->interpreter_err);
-               g_io_channel_set_flags (gs->interpreter_err,
-                                       flags | G_IO_FLAG_NONBLOCK, NULL);
-               gs->interpreter_error_id =
-                       g_io_add_watch (gs->interpreter_err, G_IO_IN,
-                                       (GIOFunc)ps_interpreter_error,
-                                       gs);
-       } else {
-               g_warning (error->message);
-               g_error_free (error);
-       }
-
-       g_free (dir);
-       g_strfreev (envp);
-       g_strfreev (gs_args);
-       g_strfreev (alpha_args);
-}
-
-static void
-ps_interpreter_stop (PSDocument *gs)
-{
-       if (gs->interpreter_pid > 0) {
-               gint status = 0;
-               
-               kill (gs->interpreter_pid, SIGTERM);
-               while ((wait (&status) == -1) && (errno == EINTR));
-               g_spawn_close_pid (gs->interpreter_pid);
-               gs->interpreter_pid = -1;
-       }
-
-       if (gs->interpreter_input) {
-               g_io_channel_unref (gs->interpreter_input);
-               gs->interpreter_input = NULL;
-
-               if (gs->interpreter_input_id > 0) {
-                       g_source_remove (gs->interpreter_input_id);
-                       gs->interpreter_input_id = 0;
-               }
-               
-               if (gs->ps_input) {
-                       g_queue_foreach (gs->ps_input, (GFunc)ps_section_free, NULL);
-                       g_queue_free (gs->ps_input);
-                       gs->ps_input = g_queue_new ();
-               }
-       }
-
-       if (gs->interpreter_output) {
-               g_io_channel_unref (gs->interpreter_output);
-               gs->interpreter_output = NULL;
-
-               if (gs->interpreter_output_id > 0) {
-                       g_source_remove (gs->interpreter_output_id);
-                       gs->interpreter_output_id = 0;
-               }
-       }
-
-       if (gs->interpreter_err) {
-               g_io_channel_unref (gs->interpreter_err);
-               gs->interpreter_err = NULL;
-               
-               if (gs->interpreter_error_id > 0) {
-                       g_source_remove (gs->interpreter_error_id);
-                       gs->interpreter_error_id = 0;
-               }
-       }
-
-       gs->busy = FALSE;
-}
-
-static void
-ps_interpreter_failed (PSDocument *gs, const char *msg)
-{
-       g_warning (msg ? msg : _("Interpreter failed."));
-       
-       push_pixbuf (gs);
-       ps_interpreter_stop (gs);
-}
-
-static gboolean
-ps_interpreter_is_ready (PSDocument *gs)
-{
-       return (gs->interpreter_pid != -1 &&
-               !gs->busy &&
-               (g_queue_is_empty (gs->ps_input)));
 }
 
 /* EvDocumentIface */
 static gboolean
-document_load (PSDocument *gs, const gchar *fname, GError **error)
+document_load (PSDocument *ps_document, const gchar *fname, GError **error)
 {
-       /* prepare this document */
-       gs->structured_doc = FALSE;
-       gs->send_filename_to_gs = TRUE;
-       gs->gs_filename = g_strdup (fname);
+       FILE *fd;
+       
+       ps_document->filename = g_strdup (fname);
 
        /*
         * We need to make sure that the file is loadable/exists!
@@ -615,11 +173,10 @@ document_load (PSDocument *gs, const gchar *fname, GError **error)
                             filename_dsp);
                g_free (filename_dsp);
                
-               ps_interpreter_failed (gs, NULL);
                return FALSE;
        }
 
-       if (!gs->gs_filename || (gs->gs_psfile = fopen (gs->gs_filename, "r")) == NULL) {
+       if ((fd = fopen (ps_document->filename, "r")) == NULL) {
                gchar *filename_dsp;
 
                filename_dsp = g_filename_display_name (fname);
@@ -630,22 +187,25 @@ document_load (PSDocument *gs, const gchar *fname, GError **error)
                             filename_dsp);
                g_free (filename_dsp);
                
-               ps_interpreter_failed (gs, NULL);
                return FALSE;
        }
        
        /* we grab the vital statistics!!! */
-       gs->doc = psscan (gs->gs_psfile, TRUE, gs->gs_filename);
-       if (!gs->doc)
+       ps_document->doc = psscan (fd, TRUE, ps_document->filename);
+       fclose (fd);
+       if (!ps_document->doc)
                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;
-       }
 
+       ps_document->structured_doc =
+               ((!ps_document->doc->epsf && ps_document->doc->numpages > 0) ||
+                (ps_document->doc->epsf && ps_document->doc->numpages > 1));
+
+       ps_document->gs = ps_interpreter_new (ps_document->filename,
+                                             ps_document->doc);
+       g_signal_connect (G_OBJECT (ps_document->gs), "page_rendered",
+                         G_CALLBACK (ps_interpreter_page_rendered),
+                         (gpointer) ps_document);
+       
        return TRUE;
 }
 
@@ -677,6 +237,7 @@ ps_document_load (EvDocument  *document,
                
                return FALSE;
        }
+       g_free (gs_path);
 
        result = document_load (PS_DOCUMENT (document), filename, error);
        if (!result && !(*error)) {
@@ -691,7 +252,6 @@ ps_document_load (EvDocument  *document,
                g_free (filename_dsp);
        }
        
-       g_free (gs_path);
        g_free (filename);
 
        return result;
@@ -705,11 +265,11 @@ save_document (PSDocument *document, const char *filename)
        FILE *f, *src_file;
        gchar *buf;
 
-       src_file = fopen (document->gs_filename, "r");
+       src_file = fopen (document->filename, "r");
        if (src_file) {
                struct stat stat_rec;
 
-               if (stat (document->gs_filename, &stat_rec) == 0) {
+               if (stat (document->filename, &stat_rec) == 0) {
                        pscopy (src_file, sink, 0, stat_rec.st_size - 1);
                }
 
@@ -744,7 +304,7 @@ save_page_list (PSDocument *document, int *page_list, const char *filename)
        FILE *f;
        gchar *buf;
 
-       pscopydoc (sink, document->gs_filename, 
+       pscopydoc (sink, document->filename, 
                   document->doc, page_list);
        
        buf = gtk_gs_doc_sink_get_buffer (sink);
@@ -789,99 +349,13 @@ ps_document_get_n_pages (EvDocument *document)
 {
        PSDocument *ps = PS_DOCUMENT (document);
 
-       if (!ps->gs_filename || !ps->doc) {
+       if (!ps->filename || !ps->doc) {
                return -1;
        }
 
        return ps->structured_doc ? ps->doc->numpages : 1;
 }
 
-#define DEFAULT_PAGE_SIZE 1
-
-static void
-get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
-{
-       gint new_llx = 0;
-       gint new_lly = 0;
-       gint new_urx = 0;
-       gint new_ury = 0;
-       GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
-       int new_pagesize = -1;
-
-       if (new_pagesize == -1) {
-               new_pagesize = DEFAULT_PAGE_SIZE;
-               if (gs->doc) {
-                       /* If we have a document:
-                        * We use -- the page size (if specified)       
-                        * or the doc. size (if specified)      
-                        * or the page bbox (if specified)      
-                        * or the bounding box  
-                        */
-                       if ((page >= 0) && (gs->doc->numpages > page) &&
-                           (gs->doc->pages) && (gs->doc->pages[page].size)) {
-                               new_pagesize = gs->doc->pages[page].size - gs->doc->size;
-                       } else if (gs->doc->default_page_size != NULL) {
-                               new_pagesize = gs->doc->default_page_size - gs->doc->size;
-                       } else if ((page >= 0) &&
-                                  (gs->doc->numpages > page) &&
-                                  (gs->doc->pages) &&
-                                  (gs->doc->pages[page].boundingbox[URX] >
-                                   gs->doc->pages[page].boundingbox[LLX]) &&
-                                  (gs->doc->pages[page].boundingbox[URY] >
-                                           gs->doc->pages[page].boundingbox[LLY])) {
-                               new_pagesize = -1;
-                       } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
-                                  (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
-                               new_pagesize = -1;
-                       }
-               }
-       }
-
-       /* Compute bounding box */
-       if (gs->doc && (gs->doc->epsf || new_pagesize == -1)) {    /* epsf or bbox */
-               if ((page >= 0) &&
-                   (gs->doc->pages) &&
-                   (gs->doc->pages[page].boundingbox[URX] >
-                    gs->doc->pages[page].boundingbox[LLX]) &&
-                   (gs->doc->pages[page].boundingbox[URY] >
-                     gs->doc->pages[page].boundingbox[LLY])) {
-                       /* use page bbox */
-                       new_llx = gs->doc->pages[page].boundingbox[LLX];
-                       new_lly = gs->doc->pages[page].boundingbox[LLY];
-                       new_urx = gs->doc->pages[page].boundingbox[URX];
-                       new_ury = gs->doc->pages[page].boundingbox[URY];
-               } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
-                          (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
-                       /* use doc bbox */
-                       new_llx = gs->doc->boundingbox[LLX];
-                       new_lly = gs->doc->boundingbox[LLY];
-                       new_urx = gs->doc->boundingbox[URX];
-                       new_ury = gs->doc->boundingbox[URY];
-               }
-       } else {
-               if (new_pagesize < 0)
-                       new_pagesize = DEFAULT_PAGE_SIZE;
-               new_llx = new_lly = 0;
-               if (gs->doc && gs->doc->size &&
-                   (new_pagesize < gs->doc->numsizes)) {
-                       new_urx = gs->doc->size[new_pagesize].width;
-                       new_ury = gs->doc->size[new_pagesize].height;
-               } else {
-                       new_urx = papersizes[new_pagesize].width;
-                       new_ury = papersizes[new_pagesize].height;
-               }
-       }
-
-       if (new_urx <= new_llx)
-               new_urx = papersizes[12].width;
-       if (new_ury <= new_lly)
-               new_ury = papersizes[12].height;
-
-       *urx = new_urx;
-       *ury = new_ury;
-       *llx = new_llx;
-       *lly = new_lly;
-}
 
 static void
 ps_document_get_page_size (EvDocument *document,
@@ -889,10 +363,10 @@ ps_document_get_page_size (EvDocument *document,
                           double     *width,
                           double     *height)
 {
-       PSDocument *gs = PS_DOCUMENT (document);
+       PSDocument *ps_document = PS_DOCUMENT (document);
        int urx, ury, llx, lly;
 
-       get_page_box (gs, page, &urx, &ury, &llx, &lly);
+       psgetpagebox (ps_document->doc, page, &urx, &ury, &llx, &lly);
 
        if (width) {
                *width = (urx - llx) + 0.5;
@@ -929,7 +403,7 @@ ps_document_get_info (EvDocument *document)
        info->creator = g_strdup (ps->doc->creator);
        info->n_pages = ev_document_get_n_pages (document);
        
-       get_page_box (PS_DOCUMENT (document), 0, &urx, &ury, &llx, &lly);
+       psgetpagebox (PS_DOCUMENT (document)->doc, 0, &urx, &ury, &llx, &lly);
 
        info->paper_width  = (urx - llx) / 72.0f * 25.4f;
        info->paper_height = (ury - lly) / 72.0f * 25.4f;
@@ -950,257 +424,144 @@ ps_document_document_iface_init (EvDocumentIface *iface)
 
 /* EvAsyncRendererIface */
 static void
-setup_page (PSDocument *gs, int page, double scale, int rotation)
+ps_interpreter_page_rendered (PSInterpreter *gs,
+                             GdkPixbuf     *pixbuf,
+                             PSDocument    *ps_document)
 {
-       gchar *buf;
-       char scaled_dpi[G_ASCII_DTOSTR_BUF_SIZE];       
-       int urx, ury, llx, lly;
-       PSDocumentClass *gs_class;
-
-       gs_class = PS_DOCUMENT_GET_CLASS (gs);
-
-       get_page_box (gs, page, &urx, &ury, &llx, &lly);
-       g_ascii_dtostr (scaled_dpi, G_ASCII_DTOSTR_BUF_SIZE, 72.0 * scale);
-
-       buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
-                              0L, rotation, llx, lly, urx, ury,
-                              scaled_dpi, scaled_dpi,
-                              0, 0, 0, 0);
-       
-       gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
-                            8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen (buf));
-       g_free (buf);
-       
-       gdk_flush ();
+       g_signal_emit_by_name (ps_document, "render_finished", pixbuf);
 }
 
 static void
-setup_pixmap (PSDocument *gs, int page, double scale, int rotation)
+ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer,
+                                gint             page,
+                                gdouble          scale,
+                                gint             rotation)
 {
-       GdkGC *fill;
-       GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF };   /* pixel, r, g, b */
-       GdkColormap *colormap;
-       double width, height;
-       int pixmap_width, pixmap_height;
-       
-       if (gs->pstarget == NULL)
-               return;
+       PSDocument *ps_document = PS_DOCUMENT (renderer);
 
-       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;
-       }
+       g_return_if_fail (PS_IS_INTERPRETER (ps_document->gs));
 
-       if (gs->bpixmap) {
-               gint w, h;
-
-               gdk_drawable_get_size (gs->bpixmap, &w, &h);
-
-               if (pixmap_width != w || h != pixmap_height) {
-                       g_object_unref (gs->bpixmap);
-                       gs->bpixmap = NULL;
-                       ps_interpreter_stop (gs);
-               }
-       }
-
-       if (!gs->bpixmap) {
-               fill = gdk_gc_new (gs->pstarget);
-               colormap = gdk_drawable_get_colormap (gs->pstarget);
-               gdk_colormap_alloc_color (colormap, &white, FALSE, TRUE);
-               gdk_gc_set_foreground (fill, &white);
-               gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
-                                             pixmap_height, -1);
-               gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
-                                   0, 0, pixmap_width, pixmap_height);
-       }
+       ps_interpreter_render_page (ps_document->gs, page, scale, rotation);
 }
 
 static void
-push_pixbuf (PSDocument *gs)
-{
-       GdkColormap *cmap;
-       GdkPixbuf *pixbuf;
-       gint width, height;
-
-       if (gs->pstarget == NULL)
-               return;
-
-       cmap = gdk_drawable_get_colormap (gs->pstarget);
-       gdk_drawable_get_size (gs->bpixmap, &width, &height);
-       pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
-                                              0, 0, 0, 0,
-                                              width, height);
-       g_signal_emit_by_name (gs, "render_finished", pixbuf);
-       g_object_unref (pixbuf);
-}
-
-static gboolean
-ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
+ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
 {
-       PSDocument *gs = (PSDocument *) data;
-       PSDocumentClass *gs_class;
-
-       if (event->type != GDK_CLIENT_EVENT)
-               return FALSE;
-
-       gs_class = PS_DOCUMENT_GET_CLASS (gs);
-       
-       gs->message_window = event->client.data.l[0];
-
-       if (event->client.message_type == gs_class->page_atom) {
-               gs->busy = FALSE;
-
-               push_pixbuf (gs);
-       }
-
-       return TRUE;
+       iface->render_pixbuf = ps_async_renderer_render_pixbuf;
 }
 
+/* EvDocumentThumbnailsIface */
 static void
-send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
+ps_interpreter_thumbnail_rendered (PSInterpreter *gs,
+                                  GdkPixbuf     *pixbuf,
+                                  PSDocument    *ps_document)
 {
-       PSSection *ps_new;
+       if (ps_document->thumbnail)
+               g_object_unref (ps_document->thumbnail);
+       ps_document->thumbnail = g_object_ref (pixbuf);
 
-       if (!gs->interpreter_input) {
-               g_critical ("No pipe to gs: error in send_ps().");
-               return;
-       }
-
-       ps_new = g_new0 (PSSection, 1);
-       ps_new->fp = gs->gs_psfile;
-       ps_new->begin = begin;
-       ps_new->len = len;
-       ps_new->seek_needed = TRUE;
-       ps_new->close = close;
-
-       if (gs->input_buffer == NULL) {
-               gs->input_buffer = g_malloc (MAX_BUFSIZE);
-       }
-
-       if (g_queue_is_empty (gs->ps_input)) {
-               gs->input_buffer_ptr = gs->input_buffer;
-               gs->bytes_left = len;
-               gs->buffer_bytes_left = 0;
-               g_queue_push_head (gs->ps_input, ps_new);
-               gs->interpreter_input_id =
-                       g_io_add_watch (gs->interpreter_input,
-                                       G_IO_OUT/* | G_IO_HUP | G_IO_ERR | G_IO_NVAL*/,
-                                       (GIOFunc)ps_interpreter_input,
-                                       gs);
-       } else {
-               g_queue_push_head (gs->ps_input, ps_new);
-       }
+       g_cond_broadcast (ps_document->thumbs_cond);
 }
 
-static void
-ps_document_next_page (PSDocument *gs)
+static gboolean
+ps_document_render_thumbnail (PSDocument *ps_document)
 {
-       XEvent      event;
-       GdkScreen  *screen;
-       GdkDisplay *display;
-       Display    *dpy;
-       PSDocumentClass *gs_class;
+       ps_interpreter_render_page (ps_document->thumbs_gs,
+                                   ps_document->thumbs_rc->page,
+                                   ps_document->thumbs_rc->scale,
+                                   ps_document->thumbs_rc->rotation);
 
-       g_assert (gs->interpreter_pid != 0);
-       g_assert (gs->busy != TRUE);
-
-       gs_class = PS_DOCUMENT_GET_CLASS (gs);
-       
-       gs->busy = TRUE;
-
-       screen = gtk_window_get_screen (GTK_WINDOW (gs->target_window));
-       display = gdk_screen_get_display (screen);
-       dpy = gdk_x11_display_get_xdisplay (display);
-
-       event.xclient.type = ClientMessage;
-       event.xclient.display = dpy;
-       event.xclient.window = gs->message_window;
-       event.xclient.message_type =
-               gdk_x11_atom_to_xatom_for_display (display,
-                                                  gs_class->next_atom);
-       event.xclient.format = 32;
-
-       gdk_error_trap_push ();
-       XSendEvent (dpy, gs->message_window, FALSE, 0, &event);
-       gdk_flush ();
-       gdk_error_trap_pop ();
+       return FALSE;
 }
 
-static gboolean
-render_page (PSDocument *gs, int page)
+static GdkPixbuf *
+ps_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
+                                     gint                  page,
+                                     gint                  rotation,
+                                     gint                  size,
+                                     gboolean              border)
 {
-       g_assert (gs != NULL);
+       PSDocument *ps_document;
+       GdkPixbuf  *pixbuf = NULL;
+       gdouble     page_width, page_height;
+       gdouble     scale;
 
-       if (!gs->gs_filename) {
-               return FALSE;
+       ps_document = PS_DOCUMENT (document_thumbnails);
+       
+       g_return_val_if_fail (ps_document->filename != NULL, NULL);
+       g_return_val_if_fail (ps_document->doc != NULL, NULL);
+               
+       if (!ps_document->thumbs_gs) {
+               ps_document->thumbs_gs = ps_interpreter_new (ps_document->filename,
+                                                            ps_document->doc);
+               g_signal_connect (G_OBJECT (ps_document->thumbs_gs), "page_rendered",
+                                 G_CALLBACK (ps_interpreter_thumbnail_rendered),
+                                 (gpointer) ps_document);
        }
 
-       if (gs->structured_doc && gs->doc) {
-               if (ps_interpreter_is_ready (gs)) {
-                       ps_document_next_page (gs);
-               } else {
-                       ps_interpreter_start (gs);
-                       send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
-                       send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
-               }
+       if (!ps_document->thumbs_mutex)
+               ps_document->thumbs_mutex = g_mutex_new ();
+       ps_document->thumbs_cond = g_cond_new ();
 
-               send_ps (gs, gs->doc->pages[page].begin,
-                        gs->doc->pages[page].len, FALSE);
+       ps_document_get_page_size (EV_DOCUMENT (ps_document), page,
+                                  &page_width, &page_height);
+       scale = size / page_width;
+       
+       if (!ps_document->thumbs_rc) {
+               ps_document->thumbs_rc = ev_render_context_new (rotation, page, scale);
        } 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 (!ps_interpreter_is_ready (gs)) {
-                       ps_interpreter_start (gs);
-               }
-               ps_document_next_page (gs);
+               ev_render_context_set_page (ps_document->thumbs_rc, page);
+               ev_render_context_set_scale (ps_document->thumbs_rc, scale);
+               ev_render_context_set_rotation (ps_document->thumbs_rc, rotation);
+       }
+
+       ev_document_doc_mutex_unlock ();
+       g_mutex_lock (ps_document->thumbs_mutex);
+       g_idle_add ((GSourceFunc)ps_document_render_thumbnail, ps_document);
+       g_cond_wait (ps_document->thumbs_cond, ps_document->thumbs_mutex);
+       g_cond_free (ps_document->thumbs_cond);
+       ps_document->thumbs_cond = NULL;
+       g_mutex_unlock (ps_document->thumbs_mutex);
+       ev_document_doc_mutex_lock ();
+       
+       pixbuf = ps_document->thumbnail;
+       ps_document->thumbnail = NULL;
+       
+       if (border) {
+               GdkPixbuf *border_pixbuf;
+               
+               border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, rotation, pixbuf);
+               g_object_unref (pixbuf);
+               pixbuf = border_pixbuf;
        }
 
-       return TRUE;
+       return pixbuf;
 }
 
 static void
-ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer,
-                                gint             page,
-                                gdouble          scale,
-                                gint             rotation)
+ps_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
+                                      gint                  page,
+                                      gint                  size,
+                                      gint                 *width,
+                                      gint                 *height)
 {
-       PSDocument *gs = PS_DOCUMENT (renderer);
-
-       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;
+       PSDocument *ps_document;
+       gdouble     page_width, page_height;
 
-               g_assert (gs->pstarget != NULL);
-
-               g_signal_connect (gs->target_window, "event",
-                                 G_CALLBACK (ps_document_widget_event),
-                                 gs);
-       }
-
-       setup_pixmap (gs, page, scale, rotation);
-       setup_page (gs, page, scale, rotation);
-
-       render_page (gs, page);
+       ps_document = PS_DOCUMENT (document_thumbnails);
+       
+       ps_document_get_page_size (EV_DOCUMENT (ps_document),
+                                  page,
+                                  &page_width, &page_height);
+       *width = size;
+       *height = (int) (size * page_height / page_width);
 }
 
 static void
-ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
+ps_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
 {
-       iface->render_pixbuf = ps_async_renderer_render_pixbuf;
+       iface->get_thumbnail = ps_document_thumbnails_get_thumbnail;
+       iface->get_dimensions = ps_document_thumbnails_get_dimensions;
 }
 
 /* EvFileExporterIface */
index debf17ea6aa64f8124aabb00f46f7b20e783b713..fb00f9fb2c96101b14bf09fda28481d9943105d5 100644 (file)
 #ifndef __PS_DOCUMENT_H__
 #define __PS_DOCUMENT_H__
 
+#include <glib-object.h>
+
 #include "ev-document.h"
 
 G_BEGIN_DECLS
 
 #define PS_TYPE_DOCUMENT           (ps_document_get_type())
-#define PS_DOCUMENT(obj)           GTK_CHECK_CAST (obj, PS_TYPE_DOCUMENT, PSDocument)
-#define PS_DOCUMENT_CLASS(klass)   GTK_CHECK_CLASS_CAST (klass, PS_TYPE_DOCUMENT, PSDocumentClass)
-#define PS_IS_DOCUMENT(obj)        GTK_CHECK_TYPE (obj, PS_TYPE_DOCUMENT)
+#define PS_DOCUMENT(obj)           (G_TYPE_CHECK_INSTANCE_CAST ((obj), PS_TYPE_DOCUMENT, PSDocument))
+#define PS_DOCUMENT_CLASS(klass)   (G_TYPE_CHECK_CLASS_CAST ((klass), PS_TYPE_DOCUMENT, PSDocumentClass))
+#define PS_IS_DOCUMENT(obj)        (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PS_TYPE_DOCUMENT))
 #define PS_DOCUMENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PS_TYPE_DOCUMENT, PSDocumentClass))
 
 typedef struct _PSDocument      PSDocument;
diff --git a/backend/ps/ps-interpreter.c b/backend/ps/ps-interpreter.c
new file mode 100644 (file)
index 0000000..55f55d4
--- /dev/null
@@ -0,0 +1,814 @@
+/* this file is part of evince, a gnome document viewer
+ *
+ *  Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
+ *  Copyright 1998 - 2005 The Free Software Foundation
+ *
+ * Evince is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Evince is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include <glib/gstdio.h>
+#include <glib/gi18n.h>
+#include <gtk/gtkwidget.h>
+#include <gtk/gtkwindow.h>
+#include <gdk/gdk.h>
+#include <gdk/gdkx.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <errno.h>
+
+#include "ps-interpreter.h"
+#include "ps.h"
+
+#define MAX_BUFSIZE 1024
+
+enum {
+        PAGE_RENDERED,
+        LAST_SIGNAL
+};
+
+/* structure to describe section of file to send to ghostscript */
+typedef struct {
+        FILE *fp;
+        glong begin;
+        guint len;
+        gboolean seek_needed;
+        gboolean close;
+} PSSection;
+
+struct _PSInterpreter {
+        GObject object;
+
+        GtkWidget *target_window;
+        GdkWindow *pstarget;
+        GdkPixmap *bpixmap;
+        glong message_window;          /* Used by ghostview to receive messages from app */
+
+        GPid pid;               /* PID of interpreter, -1 if none  */
+        GIOChannel *input;      /* stdin of interpreter            */
+        GIOChannel *output;     /* stdout of interpreter           */
+        GIOChannel *error;        /* stderr of interpreter           */
+        guint input_id;
+        guint output_id;
+        guint error_id;
+
+        gboolean busy;                /* Is gs busy drawing? */
+        gboolean structured_doc;
+
+        GQueue *ps_input;
+        gchar *input_buffer_ptr;
+        guint bytes_left;
+        guint buffer_bytes_left;
+
+        FILE *psfile;              /* the currently loaded FILE */
+        gchar *psfilename;           /* the currently loaded filename */
+        gchar *input_buffer;
+        gboolean send_filename_to_gs; /* True if gs should read from file directly */
+        const struct document *doc;
+};
+
+struct _PSInterpreterClass {
+        GObjectClass parent_class;
+
+        void (* page_rendered) (PSInterpreter *gs,
+                                GdkPixbuf     *pixbuf);
+        
+        GdkAtom gs_atom;
+        GdkAtom next_atom;
+        GdkAtom page_atom;
+        GdkAtom string_atom;
+};
+
+static void     ps_interpreter_start    (PSInterpreter *gs);
+static void     ps_interpreter_stop     (PSInterpreter *gs);
+static void     ps_interpreter_failed   (PSInterpreter *gs,
+                                         const gchar   *msg);
+static gboolean ps_interpreter_is_ready (PSInterpreter *gs);
+
+static void     push_pixbuf             (PSInterpreter *gs);
+
+G_DEFINE_TYPE (PSInterpreter, ps_interpreter, G_TYPE_OBJECT)
+
+static guint gs_signals[LAST_SIGNAL];
+
+static void
+ps_section_free (PSSection *section)
+{
+        if (!section)
+                return;
+
+        if (section->close && section->fp)
+                fclose (section->fp);
+
+        g_free (section);
+}
+
+static void
+ps_interpreter_init (PSInterpreter *gs)
+{
+        gs->pid = -1;
+        gs->ps_input = g_queue_new ();
+}
+
+static void
+ps_interpreter_dispose (GObject *object)
+{
+        PSInterpreter *gs = PS_INTERPRETER (object);
+
+        gs->doc = NULL;
+        
+        if (gs->psfile) {
+                fclose (gs->psfile);
+                gs->psfile = NULL;
+        }
+
+        if (gs->psfilename) {
+                g_free (gs->psfilename);
+                gs->psfilename = NULL;
+        }
+
+        if (gs->bpixmap) {
+                g_object_unref (gs->bpixmap);
+                gs->bpixmap = NULL;
+        }
+
+        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;
+        }
+
+        if (gs->ps_input) {
+                g_queue_foreach (gs->ps_input, (GFunc)ps_section_free, NULL);
+                g_queue_free (gs->ps_input);
+                gs->ps_input = NULL;
+        }
+
+        ps_interpreter_stop (gs);
+
+        G_OBJECT_CLASS (ps_interpreter_parent_class)->dispose (object);
+}
+
+static void
+ps_interpreter_class_init (PSInterpreterClass *klass)
+{
+        GObjectClass *object_class;
+
+        object_class = G_OBJECT_CLASS (klass);
+
+        gs_signals[PAGE_RENDERED] =
+                g_signal_new ("page_rendered",
+                              PS_TYPE_INTERPRETER,
+                              G_SIGNAL_RUN_LAST,
+                              G_STRUCT_OFFSET (PSInterpreterClass, page_rendered),
+                              NULL, NULL,
+                              g_cclosure_marshal_VOID__OBJECT,
+                              G_TYPE_NONE,
+                              1,
+                              GDK_TYPE_PIXBUF);
+        
+        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);
+
+        object_class->dispose = ps_interpreter_dispose;
+}
+
+static gboolean
+ps_interpreter_input (GIOChannel    *io,
+                      GIOCondition   condition,
+                      PSInterpreter *gs)
+{
+        PSSection *section = NULL;
+
+        do {
+                if (gs->buffer_bytes_left == 0) {
+                        /* Get a new section if required */
+                        if (gs->bytes_left == 0) {
+                                ps_section_free (section);
+                                section = NULL;
+                                g_queue_pop_tail (gs->ps_input);
+                        }
+                        
+                        if (section == NULL) {
+                                section = g_queue_peek_tail (gs->ps_input);
+                        }
+
+                        /* Have to seek at the beginning of each section */
+                        if (section && section->seek_needed) {
+                                fseek (section->fp, section->begin, SEEK_SET);
+                                section->seek_needed = FALSE;
+                                gs->bytes_left = section->len;
+                        }
+
+                        if (gs->bytes_left > MAX_BUFSIZE) {
+                                gs->buffer_bytes_left = fread (gs->input_buffer, sizeof (char),
+                                                               MAX_BUFSIZE, section->fp);
+                        } else if (gs->bytes_left > 0) {
+                                gs->buffer_bytes_left = fread (gs->input_buffer, sizeof (char),
+                                                               gs->bytes_left, section->fp);
+                        } else {
+                                gs->buffer_bytes_left = 0;
+                        }
+                        
+                        if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
+                                ps_interpreter_failed (gs, NULL); /* Error occurred */
+                        }
+                        
+                        gs->input_buffer_ptr = gs->input_buffer;
+                        gs->bytes_left -= gs->buffer_bytes_left;
+                }
+
+                if (gs->buffer_bytes_left > 0) {
+                        GIOStatus status;
+                        gsize bytes_written;
+                        GError *error = NULL;
+
+                        status = g_io_channel_write_chars (gs->input,
+                                                           gs->input_buffer_ptr,
+                                                           gs->buffer_bytes_left,
+                                                           &bytes_written,
+                                                           &error);
+                        switch (status) {
+                        case G_IO_STATUS_NORMAL:
+                                gs->buffer_bytes_left -= bytes_written;
+                                gs->input_buffer_ptr += bytes_written;
+
+                                break;
+                        case G_IO_STATUS_ERROR:
+                                ps_interpreter_failed (gs, error->message);
+                                g_error_free (error);
+                                        
+                                break;
+                        case G_IO_STATUS_AGAIN:
+                        default:
+                                break;
+                        }
+                }
+        } while (!g_queue_is_empty (gs->ps_input) && gs->buffer_bytes_left == 0);
+
+        if (g_queue_is_empty (gs->ps_input) && gs->buffer_bytes_left == 0) {
+                GIOFlags flags;
+
+                flags = g_io_channel_get_flags (gs->input);
+                
+                g_io_channel_set_flags (gs->input,
+                                        flags & ~G_IO_FLAG_NONBLOCK, NULL);
+                g_io_channel_flush (gs->input, NULL);
+                g_io_channel_set_flags (gs->input,
+                                        flags | G_IO_FLAG_NONBLOCK, NULL);
+                
+                gs->input_id = 0;
+                
+                return FALSE;
+        }
+        
+        return TRUE;
+}
+
+static gboolean
+ps_interpreter_output (GIOChannel    *io,
+                       GIOCondition   condition,
+                       PSInterpreter *gs)
+{
+        gchar buf[MAX_BUFSIZE + 1];
+        gsize bytes = 0;
+        GIOStatus status;
+        GError *error = NULL;
+
+        status = g_io_channel_read_chars (io, buf, MAX_BUFSIZE,
+                                          &bytes, &error);
+        switch (status) {
+        case G_IO_STATUS_NORMAL:
+                if (bytes > 0) {
+                        buf[bytes] = '\0';
+                        g_print ("%s", buf);
+                }
+                break;
+        case G_IO_STATUS_EOF:
+                g_io_channel_unref (gs->output);
+                gs->output = NULL;
+                gs->output_id = 0;
+                        
+                return FALSE;
+        case G_IO_STATUS_ERROR:
+                ps_interpreter_failed (gs, error->message);
+                g_error_free (error);
+                gs->output_id = 0;
+                        
+                return FALSE;
+        default:
+                break;
+        }
+        
+        if (!gs->error) {
+                ps_interpreter_failed (gs, NULL);
+        }
+
+        return TRUE;
+}
+
+static gboolean
+ps_interpreter_error (GIOChannel    *io,
+                      GIOCondition   condition,
+                      PSInterpreter *gs)
+{
+        gchar buf[MAX_BUFSIZE + 1];
+        gsize bytes = 0;
+        GIOStatus status;
+        GError *error = NULL;
+
+        status = g_io_channel_read_chars (io, buf, MAX_BUFSIZE,
+                                          &bytes, &error);
+        switch (status) {
+        case G_IO_STATUS_NORMAL:
+                if (bytes > 0) {
+                        buf[bytes] = '\0';
+                        g_print ("%s", buf);
+                }
+                        
+                break;
+        case G_IO_STATUS_EOF:
+                g_io_channel_unref (gs->error);
+                gs->error = NULL;
+                gs->error_id = 0;
+                        
+                return FALSE;
+        case G_IO_STATUS_ERROR:
+                ps_interpreter_failed (gs, error->message);
+                g_error_free (error);
+                gs->error_id = 0;
+                        
+                break;
+        default:
+                break;
+        }
+        
+        if (!gs->output) {
+                ps_interpreter_failed (gs, NULL);
+        }
+
+        return TRUE;
+}
+
+static void
+ps_interpreter_finished (GPid           pid,
+                         gint           status,
+                         PSInterpreter *gs)
+{
+        g_spawn_close_pid (gs->pid);
+        gs->pid = -1;
+        ps_interpreter_failed (gs, NULL);
+}
+
+#define NUM_ARGS    100
+#define NUM_GS_ARGS (NUM_ARGS - 20)
+#define NUM_ALPHA_ARGS 10
+
+static void
+setup_interpreter_env (gchar **envp)
+{
+        gint i;
+
+        for (i = 0; envp[i]; i++)
+                putenv (envp[i]);
+}
+
+static void
+ps_interpreter_start (PSInterpreter *gs)
+{
+        gchar *argv[NUM_ARGS], *dir, *gv_env, *gs_path;
+        gchar **gs_args, **alpha_args = NULL;
+        gchar **envp;
+        gint pin, pout, perr;
+        gint argc = 0, i;
+        GError *error = NULL;
+
+        g_assert (gs->psfilename != NULL);
+
+        ps_interpreter_stop (gs);
+
+        dir = g_path_get_dirname (gs->psfilename);
+
+        /* 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";
+
+        if (gs->send_filename_to_gs) {
+                argv[argc++] = gs->psfilename;
+                argv[argc++] = "-c";
+                argv[argc++] = "quit";
+        } else {
+                argv[argc++] = "-";
+        }
+
+        argv[argc++] = NULL;
+
+        gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld;DISPLAY=%s",
+                                  gdk_x11_drawable_get_xid (gs->pstarget),
+                                  gdk_x11_drawable_get_xid (gs->bpixmap),
+                                  gdk_display_get_name (gdk_drawable_get_display (gs->pstarget)));
+        envp = g_strsplit (gv_env, ";", 2);
+        g_free (gv_env);
+
+        if (g_spawn_async_with_pipes (dir, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
+                                      (GSpawnChildSetupFunc)setup_interpreter_env, envp,
+                                      &(gs->pid), &pin, &pout, &perr,
+                                      &error)) {
+                GIOFlags flags;
+
+                g_child_watch_add (gs->pid,
+                                   (GChildWatchFunc)ps_interpreter_finished, 
+                                   gs);
+
+                gs->input = g_io_channel_unix_new (pin);
+                g_io_channel_set_encoding (gs->input, NULL, NULL);
+                flags = g_io_channel_get_flags (gs->input);
+                g_io_channel_set_flags (gs->input, flags | G_IO_FLAG_NONBLOCK, NULL);
+
+                
+                gs->output = g_io_channel_unix_new (pout);
+                flags = g_io_channel_get_flags (gs->output);
+                g_io_channel_set_flags (gs->output, flags | G_IO_FLAG_NONBLOCK, NULL);
+                gs->output_id = g_io_add_watch (gs->output, G_IO_IN,
+                                                (GIOFunc)ps_interpreter_output,
+                                                gs);
+                
+                gs->error = g_io_channel_unix_new (perr);
+                flags = g_io_channel_get_flags (gs->error);
+                g_io_channel_set_flags (gs->error, flags | G_IO_FLAG_NONBLOCK, NULL);
+                gs->error_id = g_io_add_watch (gs->error, G_IO_IN,
+                                               (GIOFunc)ps_interpreter_error,
+                                               gs);
+        } else {
+                g_warning (error->message);
+                g_error_free (error);
+        }
+
+        g_free (dir);
+        g_strfreev (envp);
+        g_strfreev (gs_args);
+        g_strfreev (alpha_args);
+}
+
+static void
+ps_interpreter_stop (PSInterpreter *gs)
+{
+        if (gs->pid > 0) {
+                gint status = 0;
+                
+                kill (gs->pid, SIGTERM);
+                while ((wait (&status) == -1) && (errno == EINTR));
+                g_spawn_close_pid (gs->pid);
+                gs->pid = -1;
+        }
+
+        if (gs->input) {
+                g_io_channel_unref (gs->input);
+                gs->input = NULL;
+
+                if (gs->input_id > 0) {
+                        g_source_remove (gs->input_id);
+                        gs->input_id = 0;
+                }
+                
+                if (gs->ps_input) {
+                        g_queue_foreach (gs->ps_input, (GFunc)ps_section_free, NULL);
+                        g_queue_free (gs->ps_input);
+                        gs->ps_input = g_queue_new ();
+                }
+        }
+
+        if (gs->output) {
+                g_io_channel_unref (gs->output);
+                gs->output = NULL;
+
+                if (gs->output_id > 0) {
+                        g_source_remove (gs->output_id);
+                        gs->output_id = 0;
+                }
+        }
+
+        if (gs->error) {
+                g_io_channel_unref (gs->error);
+                gs->error = NULL;
+                
+                if (gs->error_id > 0) {
+                        g_source_remove (gs->error_id);
+                        gs->error_id = 0;
+                }
+        }
+
+        gs->busy = FALSE;
+}
+
+static void
+ps_interpreter_failed (PSInterpreter *gs, const char *msg)
+{
+        g_warning (msg ? msg : _("Interpreter failed."));
+
+        push_pixbuf (gs);
+        ps_interpreter_stop (gs);
+}
+
+static gboolean
+ps_interpreter_is_ready (PSInterpreter *gs)
+{
+        return (gs->pid != -1 && !gs->busy &&
+                (g_queue_is_empty (gs->ps_input)));
+}
+
+static void
+setup_page (PSInterpreter *gs, int page, double scale, int rotation)
+{
+        gchar *buf;
+        char scaled_dpi[G_ASCII_DTOSTR_BUF_SIZE];       
+        int urx, ury, llx, lly;
+        PSInterpreterClass *gs_class = PS_INTERPRETER_GET_CLASS (gs);
+
+        psgetpagebox (gs->doc, page, &urx, &ury, &llx, &lly);
+        g_ascii_dtostr (scaled_dpi, G_ASCII_DTOSTR_BUF_SIZE, 72.0 * scale);
+
+        buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
+                               0L, rotation, llx, lly, urx, ury,
+                               scaled_dpi, scaled_dpi,
+                               0, 0, 0, 0);
+        
+        gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
+                             8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen (buf));
+        g_free (buf);
+        
+        gdk_flush ();
+}
+
+static void
+setup_pixmap (PSInterpreter *gs, int page, double scale, int rotation)
+{
+        GdkGC *fill;
+        GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF };   /* pixel, r, g, b */
+        GdkColormap *colormap;
+        double width, height;
+        int pixmap_width, pixmap_height;
+        int urx, ury, llx, lly;
+
+        psgetpagebox (gs->doc, page, &urx, &ury, &llx, &lly);
+        width = (urx - llx) + 0.5;
+        height = (ury - lly) + 0.5;
+
+        if (rotation == 90 || rotation == 270) {
+                pixmap_height = width * scale + 0.5;
+                pixmap_width = height * scale + 0.5;
+        } else {
+                pixmap_width = width * scale + 0.5;
+                pixmap_height = height * scale + 0.5;
+        }
+
+        if (gs->bpixmap) {
+                gint w, h;
+
+                gdk_drawable_get_size (gs->bpixmap, &w, &h);
+
+                if (pixmap_width != w || h != pixmap_height) {
+                        g_object_unref (gs->bpixmap);
+                        gs->bpixmap = NULL;
+                        ps_interpreter_stop (gs);
+                }
+        }
+
+        if (!gs->bpixmap) {
+                fill = gdk_gc_new (gs->pstarget);
+                colormap = gdk_drawable_get_colormap (gs->pstarget);
+                gdk_colormap_alloc_color (colormap, &white, FALSE, TRUE);
+                gdk_gc_set_foreground (fill, &white);
+                gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
+                                              pixmap_height, -1);
+                gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
+                                    0, 0, pixmap_width, pixmap_height);
+        }
+}
+
+static void
+push_pixbuf (PSInterpreter *gs)
+{
+        GdkColormap *cmap;
+        GdkPixbuf *pixbuf;
+        gint width, height;
+
+        if (gs->pstarget == NULL)
+                return;
+
+        cmap = gdk_drawable_get_colormap (gs->pstarget);
+        gdk_drawable_get_size (gs->bpixmap, &width, &height);
+        pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
+                                               0, 0, 0, 0,
+                                               width, height);
+        g_signal_emit (gs, gs_signals[PAGE_RENDERED], 0, pixbuf);
+        g_object_unref (pixbuf);
+}
+
+static gboolean
+ps_interpreter_widget_event (GtkWidget     *widget,
+                             GdkEvent      *event,
+                             PSInterpreter *gs)
+{
+        PSInterpreterClass *gs_class = PS_INTERPRETER_GET_CLASS (gs);
+
+        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) {
+                gs->busy = FALSE;
+
+                push_pixbuf (gs);
+        }
+
+        return TRUE;
+}
+
+static void
+send_ps (PSInterpreter *gs, glong begin, guint len, gboolean close)
+{
+        PSSection *ps_new;
+
+        g_assert (gs->psfile != NULL);
+        
+        if (!gs->input) {
+                g_critical ("No pipe to gs: error in send_ps().");
+                return;
+        }
+
+        ps_new = g_new0 (PSSection, 1);
+        ps_new->fp = gs->psfile;
+        ps_new->begin = begin;
+        ps_new->len = len;
+        ps_new->seek_needed = TRUE;
+        ps_new->close = close;
+
+        if (gs->input_buffer == NULL) {
+                gs->input_buffer = g_malloc (MAX_BUFSIZE);
+        }
+
+        if (g_queue_is_empty (gs->ps_input)) {
+                gs->input_buffer_ptr = gs->input_buffer;
+                gs->bytes_left = len;
+                gs->buffer_bytes_left = 0;
+                g_queue_push_head (gs->ps_input, ps_new);
+                gs->input_id = g_io_add_watch (gs->input, G_IO_OUT,
+                                               (GIOFunc)ps_interpreter_input,
+                                               gs);
+        } else {
+                g_queue_push_head (gs->ps_input, ps_new);
+        }
+}
+
+static void
+ps_interpreter_next_page (PSInterpreter *gs)
+{
+        XEvent              event;
+        GdkScreen          *screen;
+        GdkDisplay         *display;
+        Display            *dpy;
+        PSInterpreterClass *gs_class = PS_INTERPRETER_GET_CLASS (gs);
+
+        g_assert (gs->pid != 0);
+        g_assert (gs->busy != TRUE);
+
+        gs->busy = TRUE;
+
+        screen = gtk_window_get_screen (GTK_WINDOW (gs->target_window));
+        display = gdk_screen_get_display (screen);
+        dpy = gdk_x11_display_get_xdisplay (display);
+
+        event.xclient.type = ClientMessage;
+        event.xclient.display = dpy;
+        event.xclient.window = gs->message_window;
+        event.xclient.message_type =
+                gdk_x11_atom_to_xatom_for_display (display,
+                                                   gs_class->next_atom);
+        event.xclient.format = 32;
+
+        gdk_error_trap_push ();
+        XSendEvent (dpy, gs->message_window, FALSE, 0, &event);
+        gdk_flush ();
+        gdk_error_trap_pop ();
+}
+
+static void
+render_page (PSInterpreter *gs, gint page)
+{
+        if (gs->structured_doc && gs->doc) {
+                if (ps_interpreter_is_ready (gs)) {
+                        ps_interpreter_next_page (gs);
+                } else {
+                        ps_interpreter_start (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.
+                 */
+
+                if (!ps_interpreter_is_ready (gs)) {
+                        ps_interpreter_start (gs);
+                }
+                ps_interpreter_next_page (gs);
+        }
+}
+
+/* Public methods */
+PSInterpreter *
+ps_interpreter_new (const gchar           *filename,
+                    const struct document *doc)
+{
+        PSInterpreter *gs;
+
+        g_return_val_if_fail (filename != NULL, NULL);
+        g_return_val_if_fail (doc != NULL, NULL);
+
+        gs = PS_INTERPRETER (g_object_new (PS_TYPE_INTERPRETER, NULL));
+
+        gs->psfilename = g_strdup (filename);
+        gs->doc = doc;
+        gs->structured_doc = FALSE;
+        gs->send_filename_to_gs = TRUE;
+        gs->psfile = fopen (gs->psfilename, "r");
+
+        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;
+        }
+
+        return gs;
+}
+
+void
+ps_interpreter_render_page (PSInterpreter *gs,
+                            gint           page,
+                            gdouble        scale,
+                            gint           rotation)
+{
+        g_return_if_fail (PS_IS_INTERPRETER (gs));
+
+        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;
+
+                g_assert (gs->pstarget != NULL);
+
+                g_signal_connect (gs->target_window, "event",
+                                  G_CALLBACK (ps_interpreter_widget_event),
+                                  gs);
+        }
+
+        setup_pixmap (gs, page, scale, rotation);
+        setup_page (gs, page, scale, rotation);
+
+        render_page (gs, page);
+}
diff --git a/backend/ps/ps-interpreter.h b/backend/ps/ps-interpreter.h
new file mode 100644 (file)
index 0000000..150bfec
--- /dev/null
@@ -0,0 +1,49 @@
+/* this file is part of evince, a gnome document viewer
+ *
+ *  Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
+ *
+ * Evince is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Evince is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __PS_INTERPRETER_H__
+#define __PS_INTERPRETER_H__
+
+#include <glib-object.h>
+
+#include "ps.h"
+
+G_BEGIN_DECLS
+
+typedef struct _PSInterpreter        PSInterpreter;
+typedef struct _PSInterpreterClass   PSInterpreterClass;
+
+#define PS_TYPE_INTERPRETER              (ps_interpreter_get_type())
+#define PS_INTERPRETER(object)           (G_TYPE_CHECK_INSTANCE_CAST((object), PS_TYPE_INTERPRETER, PSInterpreter))
+#define PS_INTERPRETER_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST((klass), PS_TYPE_INTERPRETER, PSInterpreterClass))
+#define PS_IS_INTERPRETER(object)        (G_TYPE_CHECK_INSTANCE_TYPE((object), PS_TYPE_INTERPRETER))
+#define PS_IS_INTERPRETER_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE((klass), PS_TYPE_INTERPRETER))
+#define PS_INTERPRETER_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS((object), PS_TYPE_INTERPRETER, PSInterpreterClass))
+
+GType          ps_interpreter_get_type    (void) G_GNUC_CONST;
+PSInterpreter *ps_interpreter_new         (const gchar           *filename,
+                                          const struct document *doc);
+void           ps_interpreter_render_page (PSInterpreter         *gs,
+                                          gint                   page,
+                                          gdouble                scale,
+                                          gint                   rotation);
+
+G_END_DECLS
+
+#endif /* __PS_INTERPRETER_H__ */
index 37b7f7e536709b4c74e2dd29c124375e02603b9e..cccf241b0f660e2cb12a709465d33e251588657a 100644 (file)
@@ -1873,3 +1873,90 @@ static char *readline (fd, lineP, positionP, line_lenP)
 
    return(FD_BUF+FD_LINE_BEGIN);
 }
+
+#define DEFAULT_PAGE_SIZE 1
+
+void
+psgetpagebox (const struct document *doc, int page, int *urx, int *ury, int *llx, int *lly)
+{
+   gint new_llx = 0;
+   gint new_lly = 0;
+   gint new_urx = 0;
+   gint new_ury = 0;
+   GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
+   int new_pagesize = -1;
+
+   if (new_pagesize == -1) {
+      new_pagesize = DEFAULT_PAGE_SIZE;
+      if (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) && (doc->numpages > page) &&
+            (doc->pages) && (doc->pages[page].size)) {
+           new_pagesize = doc->pages[page].size - doc->size;
+        } else if (doc->default_page_size != NULL) {
+           new_pagesize = doc->default_page_size - doc->size;
+        } else if ((page >= 0) &&
+                   (doc->numpages > page) &&
+                   (doc->pages) &&
+                   (doc->pages[page].boundingbox[URX] >
+                    doc->pages[page].boundingbox[LLX]) &&
+                   (doc->pages[page].boundingbox[URY] >
+                    doc->pages[page].boundingbox[LLY])) {
+           new_pagesize = -1;
+        } else if ((doc->boundingbox[URX] > doc->boundingbox[LLX]) &&
+                   (doc->boundingbox[URY] > doc->boundingbox[LLY])) {
+           new_pagesize = -1;
+        }
+      }
+   }
+
+   /* Compute bounding box */
+   if (doc && (doc->epsf || new_pagesize == -1)) {    /* epsf or bbox */
+      if ((page >= 0) &&
+         (doc->pages) &&
+         (doc->pages[page].boundingbox[URX] >
+          doc->pages[page].boundingbox[LLX]) &&
+         (doc->pages[page].boundingbox[URY] >
+          doc->pages[page].boundingbox[LLY])) {
+         /* use page bbox */
+        new_llx = doc->pages[page].boundingbox[LLX];
+        new_lly = doc->pages[page].boundingbox[LLY];
+        new_urx = doc->pages[page].boundingbox[URX];
+        new_ury = doc->pages[page].boundingbox[URY];
+      } else if ((doc->boundingbox[URX] > doc->boundingbox[LLX]) &&
+                (doc->boundingbox[URY] > doc->boundingbox[LLY])) {
+        /* use doc bbox */
+        new_llx = doc->boundingbox[LLX];
+        new_lly = doc->boundingbox[LLY];
+        new_urx = doc->boundingbox[URX];
+        new_ury = doc->boundingbox[URY];
+      }
+   } else {
+      if (new_pagesize < 0)
+         new_pagesize = DEFAULT_PAGE_SIZE;
+      new_llx = new_lly = 0;
+      if (doc && doc->size &&
+         (new_pagesize < doc->numsizes)) {
+             new_urx = doc->size[new_pagesize].width;
+        new_ury = 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;
+}
index 5e27618bfd006141c5b418672e61c0179a347d68..cb520cecc91e998ca7240c0b2ac0cff8af69dcd4 100644 (file)
@@ -102,6 +102,9 @@ char *pscopyuntil(FILE * from, GtkGSDocSink * to, long begin, long end,
 void pscopydoc(GtkGSDocSink * dest_file, char *src_filename,
                struct document *d, int *pagelist);
 
+/* Get the page box */
+void psgetpagebox (const struct document *doc, int page, int *urx, int *ury, int *llx, int *lly);
+
 G_END_DECLS
 
 #endif /* __GGV_PS_H__ */
index a3969adad32f1ed85279a5e8609dd32a95506e21..0072e27955e85ff3dfb482a4e438977919da27fc 100644 (file)
@@ -1,3 +1,22 @@
+/* this file is part of evince, a gnome document viewer
+ *
+ *  Copyright (C) 2005 Jonathan Blandford <jrb@gnome.org>
+ *
+ * Evince is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Evince is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+ */
+
 #include "ev-render-context.h"
 
 static void ev_render_context_init       (EvRenderContext      *rc);