From 2d1cbc42b4fb22539659e4f621d2d8545d907adb Mon Sep 17 00:00:00 2001 From: Carlos Garcia Campos Date: Tue, 13 Feb 2007 15:32:24 +0000 Subject: [PATCH] Add support for thumbnails in ps backend. Fixes bug #164755. 2007-02-13 Carlos Garcia Campos * 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 | 17 + backend/ps/Makefile.am | 2 + backend/ps/ps-document.c | 997 ++++++-------------------------- backend/ps/ps-document.h | 8 +- backend/ps/ps-interpreter.c | 814 ++++++++++++++++++++++++++ backend/ps/ps-interpreter.h | 49 ++ backend/ps/ps.c | 87 +++ backend/ps/ps.h | 3 + libdocument/ev-render-context.c | 19 + 9 files changed, 1175 insertions(+), 821 deletions(-) create mode 100644 backend/ps/ps-interpreter.c create mode 100644 backend/ps/ps-interpreter.h diff --git a/ChangeLog b/ChangeLog index dbb4df6a..ae157aad 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2007-02-13 Carlos Garcia Campos + + * 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 * NEWS: diff --git a/backend/ps/Makefile.am b/backend/ps/Makefile.am index 9e813d69..e443b4a9 100644 --- a/backend/ps/Makefile.am +++ b/backend/ps/Makefile.am @@ -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 diff --git a/backend/ps/ps-document.c b/backend/ps/ps-document.c index 05533c37..086f0821 100644 --- a/backend/ps/ps-document.c +++ b/backend/ps/ps-document.c @@ -26,186 +26,116 @@ #include #include -#include -#include -#include -#include +#include #include #include -#include -#include #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 */ diff --git a/backend/ps/ps-document.h b/backend/ps/ps-document.h index debf17ea..fb00f9fb 100644 --- a/backend/ps/ps-document.h +++ b/backend/ps/ps-document.h @@ -24,14 +24,16 @@ #ifndef __PS_DOCUMENT_H__ #define __PS_DOCUMENT_H__ +#include + #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 index 00000000..55f55d40 --- /dev/null +++ b/backend/ps/ps-interpreter.c @@ -0,0 +1,814 @@ +/* this file is part of evince, a gnome document viewer + * + * Copyright (C) 2007 Carlos Garcia Campos + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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 index 00000000..150bfec8 --- /dev/null +++ b/backend/ps/ps-interpreter.h @@ -0,0 +1,49 @@ +/* this file is part of evince, a gnome document viewer + * + * Copyright (C) 2007 Carlos Garcia Campos + * + * 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 + +#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__ */ diff --git a/backend/ps/ps.c b/backend/ps/ps.c index 37b7f7e5..cccf241b 100644 --- a/backend/ps/ps.c +++ b/backend/ps/ps.c @@ -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; +} diff --git a/backend/ps/ps.h b/backend/ps/ps.h index 5e27618b..cb520cec 100644 --- a/backend/ps/ps.h +++ b/backend/ps/ps.h @@ -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__ */ diff --git a/libdocument/ev-render-context.c b/libdocument/ev-render-context.c index a3969ada..0072e279 100644 --- a/libdocument/ev-render-context.c +++ b/libdocument/ev-render-context.c @@ -1,3 +1,22 @@ +/* this file is part of evince, a gnome document viewer + * + * Copyright (C) 2005 Jonathan Blandford + * + * 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); -- 2.43.5