1 /* Ghostscript widget for GTK/GNOME
3 * Copyright (C) 1998 - 2005 the Free Software Foundation
5 * Authors: Jonathan Blandford, Jaka Mocnik
7 * Based on code by: Federico Mena (Quartic), Szekeres Istvan (Pista)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
30 #include <gtk/gtkobject.h>
31 #include <gdk/gdkprivate.h>
34 #include <glib/gi18n.h>
35 #include <X11/Intrinsic.h>
41 #include <sys/types.h>
46 #include "ps-document.h"
48 #include "gsdefaults.h"
49 #include "ev-ps-exporter.h"
55 /* if POSIX O_NONBLOCK is not available, use O_NDELAY */
56 #if !defined(O_NONBLOCK) && defined(O_NDELAY)
57 # define O_NONBLOCK O_NDELAY
60 #define MAX_BUFSIZE 1024
62 #define PS_DOCUMENT_IS_COMPRESSED(gs) (PS_DOCUMENT(gs)->gs_filename_unc != NULL)
63 #define PS_DOCUMENT_GET_PS_FILE(gs) (PS_DOCUMENT_IS_COMPRESSED(gs) ? \
64 PS_DOCUMENT(gs)->gs_filename_unc : \
65 PS_DOCUMENT(gs)->gs_filename)
67 GCond* pixbuf_cond = NULL;
68 GMutex* pixbuf_mutex = NULL;
69 GdkPixbuf *current_pixbuf = NULL;
71 /* structure to describe section of file to send to ghostscript */
78 struct record_list *next;
87 static gboolean broken_pipe = FALSE;
89 /* Forward declarations */
90 static void ps_document_init(PSDocument * gs);
91 static void ps_document_class_init(PSDocumentClass * klass);
92 static void send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close);
93 static void close_pipe(int p[2]);
94 static void output(gpointer data, gint source, GdkInputCondition condition);
95 static void input(gpointer data, gint source, GdkInputCondition condition);
96 static void stop_interpreter(PSDocument * gs);
97 static gint start_interpreter(PSDocument * gs);
98 static void ps_document_document_iface_init (EvDocumentIface *iface);
99 static void ps_document_ps_exporter_iface_init (EvPSExporterIface *iface);
100 static gboolean ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data);
102 static GObjectClass *parent_class = NULL;
104 static PSDocumentClass *gs_class = NULL;
107 ps_document_init (PSDocument *gs)
111 gs->interpreter_pid = -1;
115 gs->gs_filename_unc = 0;
119 gs->structured_doc = FALSE;
120 gs->reading_from_pipe = FALSE;
121 gs->send_filename_to_gs = FALSE;
126 gs->interpreter_input = -1;
127 gs->interpreter_output = -1;
128 gs->interpreter_err = -1;
129 gs->interpreter_input_id = 0;
130 gs->interpreter_output_id = 0;
131 gs->interpreter_error_id = 0;
134 gs->input_buffer = NULL;
135 gs->input_buffer_ptr = NULL;
137 gs->buffer_bytes_left = 0;
139 gs->gs_status = _("No document loaded.");
141 gs->ps_export_pagelist = NULL;
142 gs->ps_export_filename = NULL;
144 pixbuf_cond = g_cond_new ();
145 pixbuf_mutex = g_mutex_new ();
149 ps_document_dispose (GObject *object)
151 PSDocument *gs = PS_DOCUMENT (object);
153 g_return_if_fail (gs != NULL);
156 fclose (gs->gs_psfile);
157 gs->gs_psfile = NULL;
160 if (gs->gs_filename) {
161 g_free (gs->gs_filename);
162 gs->gs_filename = NULL;
170 if (gs->gs_filename_unc) {
171 unlink(gs->gs_filename_unc);
172 g_free(gs->gs_filename_unc);
173 gs->gs_filename_unc = NULL;
177 gdk_drawable_unref (gs->bpixmap);
180 if(gs->input_buffer) {
181 g_free(gs->input_buffer);
182 gs->input_buffer = NULL;
185 stop_interpreter (gs);
187 G_OBJECT_CLASS (parent_class)->dispose (object);
191 ps_document_class_init(PSDocumentClass *klass)
193 GObjectClass *object_class;
195 object_class = (GObjectClass *) klass;
196 parent_class = g_type_class_peek_parent (klass);
199 object_class->dispose = ps_document_dispose;
201 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
202 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
203 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
204 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
208 push_pixbuf (PSDocument *gs)
214 cmap = gdk_window_get_colormap (gs->pstarget);
215 gdk_drawable_get_size (gs->bpixmap, &width, &height);
216 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
219 g_mutex_lock (pixbuf_mutex);
220 current_pixbuf = pixbuf;
221 g_cond_signal (pixbuf_cond);
222 g_mutex_unlock (pixbuf_mutex);
227 interpreter_failed (PSDocument *gs, char *msg)
229 LOG ("Interpreter failed %s", msg);
233 stop_interpreter (gs);
237 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
239 PSDocument *gs = (PSDocument *) data;
241 if(event->type != GDK_CLIENT_EVENT)
244 gs->message_window = event->client.data.l[0];
246 if (event->client.message_type == gs_class->page_atom) {
247 LOG ("GS rendered the document");
257 send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close)
259 struct record_list *ps_new;
261 if(gs->interpreter_input < 0) {
262 g_critical("No pipe to gs: error in send_ps().");
266 ps_new = (struct record_list *) g_malloc(sizeof(struct record_list));
267 ps_new->fp = gs->gs_psfile;
268 ps_new->begin = begin;
270 ps_new->seek_needed = TRUE;
271 ps_new->close = close;
274 if(gs->input_buffer == NULL) {
275 gs->input_buffer = g_malloc(MAX_BUFSIZE);
278 if(gs->ps_input == NULL) {
279 gs->input_buffer_ptr = gs->input_buffer;
280 gs->bytes_left = len;
281 gs->buffer_bytes_left = 0;
282 gs->ps_input = ps_new;
283 gs->interpreter_input_id =
284 gdk_input_add(gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
287 struct record_list *p = gs->ps_input;
288 while(p->next != NULL) {
296 get_xdpi (PSDocument *gs)
298 return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
302 get_ydpi (PSDocument *gs)
304 return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
308 setup_pixmap (PSDocument *gs, int page, double scale)
311 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
312 GdkColormap *colormap;
313 double width, height;
314 int pixmap_width, pixmap_height;
316 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
317 pixmap_width = floor (width * scale);
318 pixmap_height = floor (height * scale);
323 gdk_drawable_get_size (gs->bpixmap, &w, &h);
325 if (pixmap_width != w || h != pixmap_height) {
326 gdk_drawable_unref (gs->bpixmap);
328 stop_interpreter (gs);
333 LOG ("Create pixmap");
335 fill = gdk_gc_new (gs->pstarget);
336 colormap = gdk_drawable_get_colormap (gs->pstarget);
337 gdk_color_alloc (colormap, &white);
338 gdk_gc_set_foreground (fill, &white);
339 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
341 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
342 0, 0, pixmap_width, pixmap_height);
346 #define DEFAULT_PAGE_SIZE 1
349 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
355 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes();
356 int new_pagesize = -1;
358 g_return_if_fail(PS_IS_DOCUMENT(gs));
360 if(new_pagesize == -1) {
361 new_pagesize = DEFAULT_PAGE_SIZE;
363 /* If we have a document:
364 We use -- the page size (if specified)
365 or the doc. size (if specified)
366 or the page bbox (if specified)
369 if((page >= 0) && (gs->doc->numpages > page) &&
370 (gs->doc->pages) && (gs->doc->pages[page].size)) {
371 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
373 else if(gs->doc->default_page_size != NULL) {
374 new_pagesize = gs->doc->default_page_size - gs->doc->size;
376 else if((page >= 0) &&
377 (gs->doc->numpages > page) &&
379 (gs->doc->pages[page].boundingbox[URX] >
380 gs->doc->pages[page].boundingbox[LLX]) &&
381 (gs->doc->pages[page].boundingbox[URY] >
382 gs->doc->pages[page].boundingbox[LLY])) {
385 else if((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
386 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
392 /* Compute bounding box */
393 if(gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
396 (gs->doc->pages[page].boundingbox[URX] >
397 gs->doc->pages[page].boundingbox[LLX])
398 && (gs->doc->pages[page].boundingbox[URY] >
399 gs->doc->pages[page].boundingbox[LLY])) {
401 new_llx = gs->doc->pages[page].boundingbox[LLX];
402 new_lly = gs->doc->pages[page].boundingbox[LLY];
403 new_urx = gs->doc->pages[page].boundingbox[URX];
404 new_ury = gs->doc->pages[page].boundingbox[URY];
406 else if((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
407 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
409 new_llx = gs->doc->boundingbox[LLX];
410 new_lly = gs->doc->boundingbox[LLY];
411 new_urx = gs->doc->boundingbox[URX];
412 new_ury = gs->doc->boundingbox[URY];
417 new_pagesize = DEFAULT_PAGE_SIZE;
418 new_llx = new_lly = 0;
419 if(gs->doc && gs->doc->size &&
420 (new_pagesize < gs->doc->numsizes)) {
421 new_urx = gs->doc->size[new_pagesize].width;
422 new_ury = gs->doc->size[new_pagesize].height;
425 new_urx = papersizes[new_pagesize].width;
426 new_ury = papersizes[new_pagesize].height;
430 if(new_urx <= new_llx)
431 new_urx = papersizes[12].width;
432 if(new_ury <= new_lly)
433 new_ury = papersizes[12].height;
442 get_page_orientation (PSDocument *gs, int page)
446 orientation = GTK_GS_ORIENTATION_NONE;
448 if (gs->structured_doc) {
449 orientation = gs->doc->pages[page].orientation;
451 if (orientation == GTK_GS_ORIENTATION_NONE) {
452 orientation = gs->doc->default_page_orientation;
454 if (orientation == GTK_GS_ORIENTATION_NONE) {
455 orientation = gs->doc->orientation;
457 if (orientation == GTK_GS_ORIENTATION_NONE) {
458 orientation = GTK_GS_ORIENTATION_PORTRAIT;
465 setup_page (PSDocument *gs, int page, double scale)
468 int urx, ury, llx, lly, orientation;
473 LOG ("Setup the page");
476 /* gs needs floating point parameters with '.' as decimal point
477 * while some (european) locales use ',' instead, so we set the
478 * locale for this snprintf to "C".
480 savelocale = setlocale (LC_NUMERIC, "C");
482 get_page_box (gs, page, &urx, &ury, &llx, &lly);
483 orientation = get_page_orientation (gs, page);
485 g_snprintf (buf, 1024, "%ld %d %d %d %d %d %f %f %d %d %d %d",
486 0L, orientation * 90, llx, lly, urx, ury,
487 get_xdpi (gs) * scale,
488 get_ydpi (gs) * scale,
490 LOG ("GS property %s", buf);
493 setlocale(LC_NUMERIC, savelocale);
495 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
496 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
501 close_pipe (int p[2])
512 is_interpreter_ready (PSDocument *gs)
514 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
518 output(gpointer data, gint source, GdkInputCondition condition)
520 char buf[MAX_BUFSIZE + 1];
522 PSDocument *gs = PS_DOCUMENT(data);
524 if(source == gs->interpreter_output) {
525 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
526 if(bytes == 0) { /* EOF occurred */
527 close(gs->interpreter_output);
528 gs->interpreter_output = -1;
529 gdk_input_remove(gs->interpreter_output_id);
532 else if(bytes == -1) {
534 interpreter_failed(gs, NULL);
537 if(gs->interpreter_err == -1) {
538 interpreter_failed(gs, NULL);
541 else if(source == gs->interpreter_err) {
542 bytes = read(gs->interpreter_err, buf, MAX_BUFSIZE);
543 if(bytes == 0) { /* EOF occurred */
544 close(gs->interpreter_err);
545 gs->interpreter_err = -1;
546 gdk_input_remove(gs->interpreter_error_id);
549 else if(bytes == -1) {
551 interpreter_failed(gs, NULL);
554 if(gs->interpreter_output == -1) {
555 interpreter_failed(gs, NULL);
571 input(gpointer data, gint source, GdkInputCondition condition)
573 PSDocument *gs = PS_DOCUMENT(data);
575 void (*oldsig) (int);
576 oldsig = signal(SIGPIPE, catchPipe);
581 if(gs->buffer_bytes_left == 0) {
582 /* Get a new section if required */
583 if(gs->ps_input && gs->bytes_left == 0) {
584 struct record_list *ps_old = gs->ps_input;
585 gs->ps_input = ps_old->next;
586 if(ps_old->close && NULL != ps_old->fp)
588 g_free((char *) ps_old);
590 /* Have to seek at the beginning of each section */
591 if(gs->ps_input && gs->ps_input->seek_needed) {
592 fseek(gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
593 gs->ps_input->seek_needed = FALSE;
594 gs->bytes_left = gs->ps_input->len;
597 if(gs->bytes_left > MAX_BUFSIZE) {
598 gs->buffer_bytes_left =
599 fread(gs->input_buffer, sizeof(char), MAX_BUFSIZE, gs->ps_input->fp);
601 else if(gs->bytes_left > 0) {
602 gs->buffer_bytes_left =
603 fread(gs->input_buffer,
604 sizeof(char), gs->bytes_left, gs->ps_input->fp);
607 gs->buffer_bytes_left = 0;
609 if(gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
610 interpreter_failed (gs, NULL); /* Error occurred */
612 gs->input_buffer_ptr = gs->input_buffer;
613 gs->bytes_left -= gs->buffer_bytes_left;
616 if(gs->buffer_bytes_left > 0) {
617 bytes_written = write(gs->interpreter_input,
618 gs->input_buffer_ptr, gs->buffer_bytes_left);
621 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
623 interpreter_failed (gs, NULL);
625 else if(bytes_written == -1) {
626 if((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
627 interpreter_failed (gs, NULL); /* Something bad happened */
631 gs->buffer_bytes_left -= bytes_written;
632 gs->input_buffer_ptr += bytes_written;
636 while(gs->ps_input && gs->buffer_bytes_left == 0);
638 signal(SIGPIPE, oldsig);
640 if(gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
641 if(gs->interpreter_input_id != 0) {
642 gdk_input_remove(gs->interpreter_input_id);
643 gs->interpreter_input_id = 0;
649 start_interpreter (PSDocument *gs)
651 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
652 int std_out[2]; /* pipe from interp stdout */
653 int std_err[2]; /* pipe from interp stderr */
656 #define NUM_GS_ARGS (NUM_ARGS - 20)
657 #define NUM_ALPHA_ARGS 10
659 char *argv[NUM_ARGS], *dir, *gv_env;
660 char **gs_args, **alpha_args = NULL;
663 LOG ("Start the interpreter");
668 stop_interpreter(gs);
670 /* set up the args... */
671 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
672 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
673 argv[argc] = gs_args[i];
676 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
677 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
678 argv[argc] = alpha_args[i];
681 argv[argc++] = "-dNOPAUSE";
682 argv[argc++] = "-dQUIET";
683 argv[argc++] = "-dSAFER";
685 /* set up the pipes */
686 if (gs->send_filename_to_gs) {
687 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
689 argv[argc++] = "quit";
696 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
697 if (pipe (std_in) == -1) {
698 g_critical ("Unable to open pipe to Ghostscript.");
703 if (pipe (std_out) == -1) {
708 if (pipe(std_err) == -1) {
710 close_pipe (std_out);
714 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
715 gdk_x11_drawable_get_xid (gs->pstarget),
716 gdk_x11_drawable_get_xid (gs->bpixmap));
717 LOG ("Launching ghostview with env %s", gv_env);
720 gs->interpreter_pid = fork ();
721 switch (gs->interpreter_pid) {
724 close_pipe (std_out);
725 close_pipe (std_err);
730 dup2 (std_out[1], 1);
734 dup2 (std_err[1], 2);
737 if (!gs->reading_from_pipe) {
738 if (gs->send_filename_to_gs) {
740 /* just in case gs tries to read from stdin */
741 stdinfd = open("/dev/null", O_RDONLY);
755 /* change to directory where the input file is. This helps
756 * with postscript-files which include other files using
758 dir = g_path_get_dirname (gs->gs_filename);
762 execvp (argv[0], argv);
765 g_critical ("Unable to execute [%s]\n", argv[0]);
766 g_strfreev (gs_args);
768 g_strfreev (alpha_args);
771 default: /* parent */
772 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
775 /* use non-blocking IO for pipe to ghostscript */
776 result = fcntl (std_in[1], F_GETFL, 0);
777 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
778 gs->interpreter_input = std_in[1];
780 gs->interpreter_input = -1;
784 gs->interpreter_output = std_out[0];
786 gs->interpreter_err = std_err[0];
787 gs->interpreter_output_id =
788 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
789 gs->interpreter_error_id =
790 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
798 stop_interpreter(PSDocument * gs)
800 if(gs->interpreter_pid > 0) {
802 LOG ("Stop the interpreter");
803 kill(gs->interpreter_pid, SIGTERM);
804 while((wait(&status) == -1) && (errno == EINTR)) ;
805 gs->interpreter_pid = -1;
807 gs->gs_status = _("Interpreter failed.");
811 if(gs->interpreter_input >= 0) {
812 close(gs->interpreter_input);
813 gs->interpreter_input = -1;
814 if(gs->interpreter_input_id != 0) {
815 gdk_input_remove(gs->interpreter_input_id);
816 gs->interpreter_input_id = 0;
818 while(gs->ps_input) {
819 struct record_list *ps_old = gs->ps_input;
820 gs->ps_input = gs->ps_input->next;
821 if(ps_old->close && NULL != ps_old->fp)
823 g_free((char *) ps_old);
827 if(gs->interpreter_output >= 0) {
828 close(gs->interpreter_output);
829 gs->interpreter_output = -1;
830 if(gs->interpreter_output_id) {
831 gdk_input_remove(gs->interpreter_output_id);
832 gs->interpreter_output_id = 0;
836 if(gs->interpreter_err >= 0) {
837 close(gs->interpreter_err);
838 gs->interpreter_err = -1;
839 if(gs->interpreter_error_id) {
840 gdk_input_remove(gs->interpreter_error_id);
841 gs->interpreter_error_id = 0;
848 /* If file exists and is a regular file then return its length, else -1 */
850 file_length(const gchar * filename)
852 struct stat stat_rec;
854 if(filename && (stat(filename, &stat_rec) == 0)
855 && S_ISREG(stat_rec.st_mode))
856 return stat_rec.st_size;
861 /* Test if file exists, is a regular file and its length is > 0 */
863 file_readable(const char *filename)
865 return (file_length(filename) > 0);
869 * Decompress gs->gs_filename if necessary
870 * Set gs->filename_unc to the name of the uncompressed file or NULL.
871 * Error reporting via signal 'interpreter_message'
872 * Return name of input file to use or NULL on error..
875 check_filecompressed(PSDocument * gs)
879 gchar *filename, *filename_unc, *filename_err, *cmdline;
885 if((file = fopen(gs->gs_filename, "r"))
886 && (fread(buf, sizeof(gchar), 3, file) == 3)) {
887 if((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
888 /* file is gzipped or compressed */
889 cmd = gtk_gs_defaults_get_ungzip_cmd();
891 else if(strncmp(buf, "BZh", 3) == 0) {
892 /* file is compressed with bzip2 */
893 cmd = gtk_gs_defaults_get_unbzip2_cmd();
900 return gs->gs_filename;
902 /* do the decompression */
903 filename = g_shell_quote(gs->gs_filename);
904 filename_unc = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
905 if((fd = mkstemp(filename_unc)) < 0) {
906 g_free(filename_unc);
911 filename_err = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
912 if((fd = mkstemp(filename_err)) < 0) {
913 g_free(filename_err);
914 g_free(filename_unc);
919 cmdline = g_strdup_printf("%s %s >%s 2>%s", cmd,
920 filename, filename_unc, filename_err);
921 if((system(cmdline) == 0)
922 && file_readable(filename_unc)
923 && (file_length(filename_err) == 0)) {
924 /* sucessfully uncompressed file */
925 gs->gs_filename_unc = filename_unc;
929 g_snprintf(buf, 1024, _("Error while decompressing file %s:\n"),
931 interpreter_failed (gs, buf);
932 unlink(filename_unc);
933 g_free(filename_unc);
936 unlink(filename_err);
937 g_free(filename_err);
944 ps_document_enable_interpreter(PSDocument * gs)
946 g_return_val_if_fail(gs != NULL, FALSE);
947 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
952 return start_interpreter(gs);
955 /* publicly accessible functions */
958 ps_document_get_type(void)
960 static GType gs_type = 0;
962 GTypeInfo gs_info = {
963 sizeof(PSDocumentClass),
964 (GBaseInitFunc) NULL,
965 (GBaseFinalizeFunc) NULL,
966 (GClassInitFunc) ps_document_class_init,
967 (GClassFinalizeFunc) NULL,
968 NULL, /* class_data */
971 (GInstanceInitFunc) ps_document_init
974 static const GInterfaceInfo document_info =
976 (GInterfaceInitFunc) ps_document_document_iface_init,
981 static const GInterfaceInfo ps_exporter_info =
983 (GInterfaceInitFunc) ps_document_ps_exporter_iface_init,
988 gs_type = g_type_register_static(G_TYPE_OBJECT,
989 "PSDocument", &gs_info, 0);
991 g_type_add_interface_static (gs_type,
994 g_type_add_interface_static (gs_type,
1004 document_load(PSDocument * gs, const gchar * fname)
1006 g_return_val_if_fail(gs != NULL, FALSE);
1007 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1009 LOG ("Load the document");
1016 /* prepare this document */
1017 gs->structured_doc = FALSE;
1018 gs->send_filename_to_gs = TRUE;
1021 /* an absolute path */
1022 gs->gs_filename = g_strdup(fname);
1025 /* path relative to our cwd: make it absolute */
1026 gchar *cwd = g_get_current_dir();
1027 gs->gs_filename = g_strconcat(cwd, "/", fname, NULL);
1031 if((gs->reading_from_pipe = (strcmp(fname, "-") == 0))) {
1032 gs->send_filename_to_gs = FALSE;
1036 * We need to make sure that the file is loadable/exists!
1037 * otherwise we want to exit without loading new stuff...
1039 gchar *filename = NULL;
1041 if(!file_readable(fname)) {
1043 g_snprintf(buf, 1024, _("Cannot open file %s.\n"), fname);
1044 interpreter_failed (gs, buf);
1045 gs->gs_status = _("File is not readable.");
1048 filename = check_filecompressed(gs);
1051 if(!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
1052 interpreter_failed (gs, NULL);
1056 /* we grab the vital statistics!!! */
1057 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
1059 if(gs->doc == NULL) {
1060 /* File does not seem to be a Postscript one */
1062 g_snprintf(buf, 1024, _("Error while scanning file %s\n"), fname);
1063 interpreter_failed (gs, buf);
1064 gs->gs_status = _("The file is not a PostScript document.");
1068 if((!gs->doc->epsf && gs->doc->numpages > 0) ||
1069 (gs->doc->epsf && gs->doc->numpages > 1)) {
1070 gs->structured_doc = TRUE;
1071 gs->send_filename_to_gs = FALSE;
1076 gs->gs_status = _("Document loaded.");
1083 ps_document_next_page (PSDocument *gs)
1087 LOG ("Make ghostscript render next page");
1089 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
1090 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
1091 g_return_val_if_fail (gs->busy != TRUE, FALSE);
1095 event.xclient.type = ClientMessage;
1096 event.xclient.display = gdk_display;
1097 event.xclient.window = gs->message_window;
1098 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1099 event.xclient.format = 32;
1101 gdk_error_trap_push ();
1102 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1104 gdk_error_trap_pop ();
1110 render_page (PSDocument *gs, int page)
1112 g_return_val_if_fail(gs != NULL, FALSE);
1113 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1115 if(!gs->gs_filename) {
1119 if (gs->structured_doc && gs->doc) {
1120 LOG ("It's a structured document, let's send one page to gs");
1122 if (is_interpreter_ready (gs)) {
1123 ps_document_next_page (gs);
1125 ps_document_enable_interpreter (gs);
1126 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1127 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1130 send_ps (gs, gs->doc->pages[page].begin,
1131 gs->doc->pages[page].len, FALSE);
1133 /* Unstructured document
1135 * In the case of non structured documents,
1136 * GS read the PS from the actual file (via command
1137 * line. Hence, ggv only send a signal next page.
1138 * If ghostview is not running it is usually because
1139 * the last page of the file was displayed. In that
1140 * case, ggv restarts GS again and the first page is displayed.
1143 LOG ("It's an unstructured document, gs will just read the file");
1145 if (!is_interpreter_ready (gs)) {
1146 ps_document_enable_interpreter(gs);
1148 ps_document_next_page(gs);
1155 ps_document_load (EvDocument *document,
1162 filename = g_filename_from_uri (uri, NULL, error);
1166 result = document_load (PS_DOCUMENT (document), filename);
1168 g_set_error (error, G_FILE_ERROR,
1169 G_FILE_ERROR_FAILED,
1170 "Failed to load document '%s'\n",
1180 save_page_list (PSDocument *document, int *page_list, const char *filename)
1182 gboolean result = TRUE;
1183 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1187 pscopydoc (sink, document->gs_filename, document->doc, page_list);
1189 buf = gtk_gs_doc_sink_get_buffer (sink);
1191 f = fopen (filename, "w");
1200 gtk_gs_doc_sink_free (sink);
1207 ps_document_save (EvDocument *document,
1211 PSDocument *ps = PS_DOCUMENT (document);
1217 filename = g_filename_from_uri (uri, NULL, error);
1221 page_list = g_new0 (int, ps->doc->numpages);
1222 for (i = 0; i < ps->doc->numpages; i++) {
1226 result = save_page_list (ps, page_list, filename);
1235 ps_document_get_n_pages (EvDocument *document)
1237 PSDocument *ps = PS_DOCUMENT (document);
1239 g_return_val_if_fail (ps != NULL, -1);
1241 if (!ps->gs_filename || !ps->doc) {
1245 return ps->structured_doc ? ps->doc->numpages : 1;
1249 ps_document_get_page_size (EvDocument *document,
1254 PSDocument *gs = PS_DOCUMENT (document);
1256 int urx, ury, llx, lly, orientation;
1258 get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
1259 orientation = get_page_orientation (PS_DOCUMENT (document), page);
1261 switch (orientation) {
1262 case GTK_GS_ORIENTATION_PORTRAIT:
1263 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1264 w = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
1265 h = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
1267 case GTK_GS_ORIENTATION_LANDSCAPE:
1268 case GTK_GS_ORIENTATION_SEASCAPE:
1269 w = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
1270 h = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
1274 g_assert_not_reached ();
1288 ps_document_can_get_text (EvDocument *document)
1294 render_pixbuf_idle (PSRenderJob *job)
1296 PSDocument *gs = job->document;
1298 if (gs->pstarget == NULL) {
1301 widget = gtk_window_new (GTK_WINDOW_POPUP);
1302 gtk_widget_realize (widget);
1303 gs->pstarget = widget->window;
1305 g_assert (gs->pstarget != NULL);
1307 g_signal_connect (widget, "event",
1308 G_CALLBACK (ps_document_widget_event),
1312 setup_pixmap (gs, job->page, job->scale);
1313 setup_page (gs, job->page, job->scale);
1315 render_page (gs, job->page);
1321 ps_document_render_pixbuf (EvDocument *document, int page, double scale)
1328 job.document = PS_DOCUMENT (document);
1329 g_idle_add ((GSourceFunc)render_pixbuf_idle, &job);
1331 g_mutex_lock (pixbuf_mutex);
1332 while (!current_pixbuf)
1333 g_cond_wait (pixbuf_cond, pixbuf_mutex);
1334 pixbuf = current_pixbuf;
1335 current_pixbuf = NULL;
1336 g_mutex_unlock (pixbuf_mutex);
1338 LOG ("Pixbuf rendered %p\n", pixbuf);
1343 static EvDocumentInfo *
1344 ps_document_get_info (EvDocument *document)
1346 EvDocumentInfo *info;
1347 PSDocument *ps = PS_DOCUMENT (document);
1349 info = g_new0 (EvDocumentInfo, 1);
1350 info->fields_mask = EV_DOCUMENT_INFO_TITLE;
1351 info->title = g_strdup (ps->doc->title);
1357 ps_document_document_iface_init (EvDocumentIface *iface)
1359 iface->load = ps_document_load;
1360 iface->save = ps_document_save;
1361 iface->can_get_text = ps_document_can_get_text;
1362 iface->get_n_pages = ps_document_get_n_pages;
1363 iface->get_page_size = ps_document_get_page_size;
1364 iface->render_pixbuf = ps_document_render_pixbuf;
1365 iface->get_info = ps_document_get_info;
1369 ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
1370 int first_page, int last_page)
1372 PSDocument *document = PS_DOCUMENT (exporter);
1374 g_free (document->ps_export_pagelist);
1376 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1377 document->ps_export_filename = g_strdup (filename);
1381 ps_document_ps_export_do_page (EvPSExporter *exporter, int page)
1383 PSDocument *document = PS_DOCUMENT (exporter);
1385 document->ps_export_pagelist[page] = 1;
1389 ps_document_ps_export_end (EvPSExporter *exporter)
1391 PSDocument *document = PS_DOCUMENT (exporter);
1393 save_page_list (document, document->ps_export_pagelist,
1394 document->ps_export_filename);
1396 g_free (document->ps_export_pagelist);
1397 g_free (document->ps_export_filename);
1398 document->ps_export_pagelist = NULL;
1399 document->ps_export_filename = NULL;
1403 ps_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1405 iface->begin = ps_document_ps_export_begin;
1406 iface->do_page = ps_document_ps_export_do_page;
1407 iface->end = ps_document_ps_export_end;