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"
54 /* if POSIX O_NONBLOCK is not available, use O_NDELAY */
55 #if !defined(O_NONBLOCK) && defined(O_NDELAY)
56 # define O_NONBLOCK O_NDELAY
59 #define MAX_BUFSIZE 1024
61 #define PS_DOCUMENT_IS_COMPRESSED(gs) (PS_DOCUMENT(gs)->gs_filename_unc != NULL)
62 #define PS_DOCUMENT_GET_PS_FILE(gs) (PS_DOCUMENT_IS_COMPRESSED(gs) ? \
63 PS_DOCUMENT(gs)->gs_filename_unc : \
64 PS_DOCUMENT(gs)->gs_filename)
66 GCond* pixbuf_cond = NULL;
67 GMutex* pixbuf_mutex = NULL;
68 GdkPixbuf *current_pixbuf = NULL;
75 /* structure to describe section of file to send to ghostscript */
82 struct record_list *next;
91 static gboolean broken_pipe = FALSE;
93 /* Forward declarations */
94 static void ps_document_init(PSDocument * gs);
95 static void ps_document_class_init(PSDocumentClass * klass);
96 static void ps_document_finalize(GObject * object);
97 static void send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close);
98 static void close_pipe(int p[2]);
99 static void output(gpointer data, gint source, GdkInputCondition condition);
100 static void input(gpointer data, gint source, GdkInputCondition condition);
101 static void stop_interpreter(PSDocument * gs);
102 static gint start_interpreter(PSDocument * gs);
103 static void ps_document_document_iface_init (EvDocumentIface *iface);
104 static gboolean ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data);
106 static GObjectClass *parent_class = NULL;
108 static PSDocumentClass *gs_class = NULL;
111 ps_document_init (PSDocument *gs)
115 gs->current_page = 0;
116 gs->interpreter_pid = -1;
122 gs->gs_filename_unc = 0;
126 gs->structured_doc = FALSE;
127 gs->reading_from_pipe = FALSE;
128 gs->send_filename_to_gs = FALSE;
133 gs->interpreter_input = -1;
134 gs->interpreter_output = -1;
135 gs->interpreter_err = -1;
136 gs->interpreter_input_id = 0;
137 gs->interpreter_output_id = 0;
138 gs->interpreter_error_id = 0;
141 gs->input_buffer = NULL;
142 gs->input_buffer_ptr = NULL;
144 gs->buffer_bytes_left = 0;
146 gs->zoom_factor = 1.0;
148 gs->gs_status = _("No document loaded.");
150 pixbuf_cond = g_cond_new ();
151 pixbuf_mutex = g_mutex_new ();
155 ps_document_set_property (GObject *object,
170 ps_document_get_property (GObject *object,
175 PSDocument *ps = PS_DOCUMENT (object);
181 g_value_set_string (value, ps->doc->title);
183 g_value_set_string (value, NULL);
190 ps_document_class_init(PSDocumentClass *klass)
192 GObjectClass *object_class;
194 object_class = (GObjectClass *) klass;
195 parent_class = g_type_class_peek_parent (klass);
198 object_class->finalize = ps_document_finalize;
199 object_class->get_property = ps_document_get_property;
200 object_class->set_property = ps_document_set_property;
202 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
203 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
204 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
205 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
207 g_object_class_override_property (object_class, PROP_TITLE, "title");
211 push_pixbuf (PSDocument *gs)
216 cmap = gdk_window_get_colormap (gs->pstarget);
218 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
220 gs->width, gs->height);
221 g_mutex_lock (pixbuf_mutex);
222 current_pixbuf = pixbuf;
223 g_cond_signal (pixbuf_cond);
224 g_mutex_unlock (pixbuf_mutex);
229 interpreter_failed (PSDocument *gs, char *msg)
231 LOG ("Interpreter failed %s", msg);
235 stop_interpreter (gs);
239 ps_document_cleanup (PSDocument *gs)
241 g_return_if_fail (gs != NULL);
242 g_return_if_fail (PS_IS_DOCUMENT (gs));
246 stop_interpreter (gs);
249 fclose (gs->gs_psfile);
250 gs->gs_psfile = NULL;
253 if (gs->gs_filename) {
254 g_free (gs->gs_filename);
255 gs->gs_filename = NULL;
263 if (gs->gs_filename_unc) {
264 unlink(gs->gs_filename_unc);
265 g_free(gs->gs_filename_unc);
266 gs->gs_filename_unc = NULL;
269 gs->current_page = 0;
274 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
276 PSDocument *gs = (PSDocument *) data;
278 if(event->type != GDK_CLIENT_EVENT)
281 gs->message_window = event->client.data.l[0];
283 if (event->client.message_type == gs_class->page_atom) {
284 LOG ("GS rendered the document");
294 ps_document_finalize (GObject * object)
298 g_return_if_fail (object != NULL);
299 g_return_if_fail (PS_IS_DOCUMENT (object));
303 gs = PS_DOCUMENT (object);
305 ps_document_cleanup (gs);
306 stop_interpreter (gs);
308 if(gs->input_buffer) {
309 g_free(gs->input_buffer);
310 gs->input_buffer = NULL;
313 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
317 send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close)
319 struct record_list *ps_new;
321 if(gs->interpreter_input < 0) {
322 g_critical("No pipe to gs: error in send_ps().");
326 ps_new = (struct record_list *) g_malloc(sizeof(struct record_list));
327 ps_new->fp = gs->gs_psfile;
328 ps_new->begin = begin;
330 ps_new->seek_needed = TRUE;
331 ps_new->close = close;
334 if(gs->input_buffer == NULL) {
335 gs->input_buffer = g_malloc(MAX_BUFSIZE);
338 if(gs->ps_input == NULL) {
339 gs->input_buffer_ptr = gs->input_buffer;
340 gs->bytes_left = len;
341 gs->buffer_bytes_left = 0;
342 gs->ps_input = ps_new;
343 gs->interpreter_input_id =
344 gdk_input_add(gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
347 struct record_list *p = gs->ps_input;
348 while(p->next != NULL) {
356 get_xdpi (PSDocument *gs)
358 return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
362 get_ydpi (PSDocument *gs)
364 return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
368 setup_pixmap (PSDocument *gs)
371 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
372 GdkColormap *colormap;
374 LOG ("Create our internal pixmap");
377 gdk_drawable_unref(gs->bpixmap);
380 fill = gdk_gc_new (gs->pstarget);
381 colormap = gdk_drawable_get_colormap (gs->pstarget);
382 gdk_color_alloc (colormap, &white);
383 gdk_gc_set_foreground (fill, &white);
384 gs->bpixmap = gdk_pixmap_new (gs->pstarget, gs->width, gs->height, -1);
385 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
386 0, 0, gs->width, gs->height);
390 setup_page (PSDocument *gs, double scale)
397 LOG ("Setup the page");
400 /* gs needs floating point parameters with '.' as decimal point
401 * while some (european) locales use ',' instead, so we set the
402 * locale for this snprintf to "C".
404 savelocale = setlocale (LC_NUMERIC, "C");
407 g_snprintf (buf, 1024, "%ld %d %d %d %d %d %f %f %d %d %d %d",
408 0L, gs->orientation * 90, gs->llx, gs->lly, gs->urx, gs->ury,
409 get_xdpi (gs) * scale,
410 get_ydpi (gs) * scale,
412 LOG ("GS property %s", buf);
415 setlocale(LC_NUMERIC, savelocale);
417 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
418 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
423 close_pipe (int p[2])
434 is_interpreter_ready (PSDocument *gs)
436 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
440 output(gpointer data, gint source, GdkInputCondition condition)
442 char buf[MAX_BUFSIZE + 1], *msg;
444 PSDocument *gs = PS_DOCUMENT(data);
446 if(source == gs->interpreter_output) {
447 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
448 if(bytes == 0) { /* EOF occurred */
449 close(gs->interpreter_output);
450 gs->interpreter_output = -1;
451 gdk_input_remove(gs->interpreter_output_id);
454 else if(bytes == -1) {
456 interpreter_failed(gs, NULL);
459 if(gs->interpreter_err == -1) {
460 interpreter_failed(gs, NULL);
463 else if(source == gs->interpreter_err) {
464 bytes = read(gs->interpreter_err, buf, MAX_BUFSIZE);
465 if(bytes == 0) { /* EOF occurred */
466 close(gs->interpreter_err);
467 gs->interpreter_err = -1;
468 gdk_input_remove(gs->interpreter_error_id);
471 else if(bytes == -1) {
473 interpreter_failed(gs, NULL);
476 if(gs->interpreter_output == -1) {
477 interpreter_failed(gs, NULL);
483 interpreter_failed (gs, msg);
494 input(gpointer data, gint source, GdkInputCondition condition)
496 PSDocument *gs = PS_DOCUMENT(data);
498 void (*oldsig) (int);
499 oldsig = signal(SIGPIPE, catchPipe);
504 if(gs->buffer_bytes_left == 0) {
505 /* Get a new section if required */
506 if(gs->ps_input && gs->bytes_left == 0) {
507 struct record_list *ps_old = gs->ps_input;
508 gs->ps_input = ps_old->next;
509 if(ps_old->close && NULL != ps_old->fp)
511 g_free((char *) ps_old);
513 /* Have to seek at the beginning of each section */
514 if(gs->ps_input && gs->ps_input->seek_needed) {
515 fseek(gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
516 gs->ps_input->seek_needed = FALSE;
517 gs->bytes_left = gs->ps_input->len;
520 if(gs->bytes_left > MAX_BUFSIZE) {
521 gs->buffer_bytes_left =
522 fread(gs->input_buffer, sizeof(char), MAX_BUFSIZE, gs->ps_input->fp);
524 else if(gs->bytes_left > 0) {
525 gs->buffer_bytes_left =
526 fread(gs->input_buffer,
527 sizeof(char), gs->bytes_left, gs->ps_input->fp);
530 gs->buffer_bytes_left = 0;
532 if(gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
533 interpreter_failed (gs, NULL); /* Error occurred */
535 gs->input_buffer_ptr = gs->input_buffer;
536 gs->bytes_left -= gs->buffer_bytes_left;
539 if(gs->buffer_bytes_left > 0) {
540 bytes_written = write(gs->interpreter_input,
541 gs->input_buffer_ptr, gs->buffer_bytes_left);
544 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
546 interpreter_failed (gs, NULL);
548 else if(bytes_written == -1) {
549 if((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
550 interpreter_failed (gs, NULL); /* Something bad happened */
554 gs->buffer_bytes_left -= bytes_written;
555 gs->input_buffer_ptr += bytes_written;
559 while(gs->ps_input && gs->buffer_bytes_left == 0);
561 signal(SIGPIPE, oldsig);
563 if(gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
564 if(gs->interpreter_input_id != 0) {
565 gdk_input_remove(gs->interpreter_input_id);
566 gs->interpreter_input_id = 0;
572 start_interpreter (PSDocument *gs)
574 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
575 int std_out[2]; /* pipe from interp stdout */
576 int std_err[2]; /* pipe from interp stderr */
579 #define NUM_GS_ARGS (NUM_ARGS - 20)
580 #define NUM_ALPHA_ARGS 10
582 char *argv[NUM_ARGS], *dir, *gv_env;
583 char **gs_args, **alpha_args = NULL;
586 LOG ("Start the interpreter");
591 stop_interpreter(gs);
593 /* set up the args... */
594 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
595 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
596 argv[argc] = gs_args[i];
599 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
601 argv[argc++] = "-dNOPAUSE";
602 argv[argc++] = "-dQUIET";
603 argv[argc++] = "-dSAFER";
605 /* set up the pipes */
606 if (gs->send_filename_to_gs) {
607 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
609 argv[argc++] = "quit";
616 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
617 if (pipe (std_in) == -1) {
618 g_critical ("Unable to open pipe to Ghostscript.");
623 if (pipe (std_out) == -1) {
628 if (pipe(std_err) == -1) {
630 close_pipe (std_out);
634 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
635 gdk_x11_drawable_get_xid (gs->pstarget),
636 gdk_x11_drawable_get_xid (gs->bpixmap));
637 LOG ("Launching ghostview with env %s", gv_env);
640 gs->interpreter_pid = fork ();
641 switch (gs->interpreter_pid) {
644 close_pipe (std_out);
645 close_pipe (std_err);
650 dup2 (std_out[1], 1);
654 dup2 (std_err[1], 2);
657 if (!gs->reading_from_pipe) {
658 if (gs->send_filename_to_gs) {
660 /* just in case gs tries to read from stdin */
661 stdinfd = open("/dev/null", O_RDONLY);
675 /* change to directory where the input file is. This helps
676 * with postscript-files which include other files using
678 dir = g_path_get_dirname (gs->gs_filename);
682 execvp (argv[0], argv);
685 g_critical ("Unable to execute [%s]\n", argv[0]);
686 g_strfreev (gs_args);
688 g_strfreev (alpha_args);
691 default: /* parent */
692 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
695 /* use non-blocking IO for pipe to ghostscript */
696 result = fcntl (std_in[1], F_GETFL, 0);
697 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
698 gs->interpreter_input = std_in[1];
700 gs->interpreter_input = -1;
704 gs->interpreter_output = std_out[0];
706 gs->interpreter_err = std_err[0];
707 gs->interpreter_output_id =
708 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
709 gs->interpreter_error_id =
710 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
718 stop_interpreter(PSDocument * gs)
720 if(gs->interpreter_pid > 0) {
722 LOG ("Stop the interpreter");
723 kill(gs->interpreter_pid, SIGTERM);
724 while((wait(&status) == -1) && (errno == EINTR)) ;
725 gs->interpreter_pid = -1;
727 ps_document_cleanup(gs);
728 gs->gs_status = _("Interpreter failed.");
732 if(gs->interpreter_input >= 0) {
733 close(gs->interpreter_input);
734 gs->interpreter_input = -1;
735 if(gs->interpreter_input_id != 0) {
736 gdk_input_remove(gs->interpreter_input_id);
737 gs->interpreter_input_id = 0;
739 while(gs->ps_input) {
740 struct record_list *ps_old = gs->ps_input;
741 gs->ps_input = gs->ps_input->next;
742 if(ps_old->close && NULL != ps_old->fp)
744 g_free((char *) ps_old);
748 if(gs->interpreter_output >= 0) {
749 close(gs->interpreter_output);
750 gs->interpreter_output = -1;
751 if(gs->interpreter_output_id) {
752 gdk_input_remove(gs->interpreter_output_id);
753 gs->interpreter_output_id = 0;
757 if(gs->interpreter_err >= 0) {
758 close(gs->interpreter_err);
759 gs->interpreter_err = -1;
760 if(gs->interpreter_error_id) {
761 gdk_input_remove(gs->interpreter_error_id);
762 gs->interpreter_error_id = 0;
769 /* If file exists and is a regular file then return its length, else -1 */
771 file_length(const gchar * filename)
773 struct stat stat_rec;
775 if(filename && (stat(filename, &stat_rec) == 0)
776 && S_ISREG(stat_rec.st_mode))
777 return stat_rec.st_size;
782 /* Test if file exists, is a regular file and its length is > 0 */
784 file_readable(const char *filename)
786 return (file_length(filename) > 0);
790 * Decompress gs->gs_filename if necessary
791 * Set gs->filename_unc to the name of the uncompressed file or NULL.
792 * Error reporting via signal 'interpreter_message'
793 * Return name of input file to use or NULL on error..
796 check_filecompressed(PSDocument * gs)
800 gchar *filename, *filename_unc, *filename_err, *cmdline;
806 if((file = fopen(gs->gs_filename, "r"))
807 && (fread(buf, sizeof(gchar), 3, file) == 3)) {
808 if((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
809 /* file is gzipped or compressed */
810 cmd = gtk_gs_defaults_get_ungzip_cmd();
812 else if(strncmp(buf, "BZh", 3) == 0) {
813 /* file is compressed with bzip2 */
814 cmd = gtk_gs_defaults_get_unbzip2_cmd();
821 return gs->gs_filename;
823 /* do the decompression */
824 filename = g_shell_quote(gs->gs_filename);
825 filename_unc = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
826 if((fd = mkstemp(filename_unc)) < 0) {
827 g_free(filename_unc);
832 filename_err = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
833 if((fd = mkstemp(filename_err)) < 0) {
834 g_free(filename_err);
835 g_free(filename_unc);
840 cmdline = g_strdup_printf("%s %s >%s 2>%s", cmd,
841 filename, filename_unc, filename_err);
842 if((system(cmdline) == 0)
843 && file_readable(filename_unc)
844 && (file_length(filename_err) == 0)) {
845 /* sucessfully uncompressed file */
846 gs->gs_filename_unc = filename_unc;
850 g_snprintf(buf, 1024, _("Error while decompressing file %s:\n"),
852 interpreter_failed (gs, buf);
853 unlink(filename_unc);
854 g_free(filename_unc);
857 unlink(filename_err);
858 g_free(filename_err);
865 compute_dimensions (PSDocument *gs, int page)
867 GtkGSPaperSize *paper_sizes = gtk_gs_defaults_get_paper_sizes ();
868 int urx, ury, llx, lly;
872 g_return_if_fail (PS_IS_DOCUMENT (gs));
873 g_return_if_fail (gs->doc != NULL);
874 g_return_if_fail (page >= 0);
875 g_return_if_fail (gs->doc->numpages > page);
877 orientation = GTK_GS_ORIENTATION_NONE;
878 if (gs->structured_doc) {
879 orientation = gs->doc->pages[gs->current_page].orientation;
881 if (orientation == GTK_GS_ORIENTATION_NONE) {
882 orientation = GTK_GS_ORIENTATION_PORTRAIT;
885 if (gs->doc->pages && gs->doc->pages[page].size) {
888 page_size = gs->doc->pages[page].size - gs->doc->size;
890 urx = gs->doc->size[page_size].width;
891 ury = gs->doc->size[page_size].height;
892 } else if (gs->doc->pages &&
893 (gs->doc->pages[page].boundingbox[URX] >
894 gs->doc->pages[page].boundingbox[LLX]) &&
895 (gs->doc->pages[page].boundingbox[URY] >
896 gs->doc->pages[page].boundingbox[LLY])) {
897 llx = gs->doc->pages[page].boundingbox[LLX];
898 lly = gs->doc->pages[page].boundingbox[LLY];
899 urx = gs->doc->pages[page].boundingbox[URX];
900 ury = gs->doc->pages[page].boundingbox[URY];
901 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
902 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
903 llx = gs->doc->boundingbox[LLX];
904 lly = gs->doc->boundingbox[LLY];
905 urx = gs->doc->boundingbox[URX];
906 ury = gs->doc->boundingbox[URY];
910 urx = paper_sizes[12].width;
911 ury = paper_sizes[12].height;
914 switch (orientation) {
915 case GTK_GS_ORIENTATION_PORTRAIT:
916 case GTK_GS_ORIENTATION_UPSIDEDOWN:
917 width = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
918 height = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
920 case GTK_GS_ORIENTATION_LANDSCAPE:
921 case GTK_GS_ORIENTATION_SEASCAPE:
922 width = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
923 height = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
927 g_assert_not_reached ();
931 width = width * gs->zoom_factor;
932 height = height * gs->zoom_factor;
934 if (llx != gs->llx || lly != gs->lly ||
935 urx != gs->urx || ury != gs->ury ||
936 gs->width != width || gs->height != height ||
937 orientation != gs->orientation) {
944 gs->orientation = orientation;
950 ps_document_enable_interpreter(PSDocument * gs)
952 g_return_val_if_fail(gs != NULL, FALSE);
953 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
958 return start_interpreter(gs);
961 /* publicly accessible functions */
964 ps_document_get_type(void)
966 static GType gs_type = 0;
968 GTypeInfo gs_info = {
969 sizeof(PSDocumentClass),
970 (GBaseInitFunc) NULL,
971 (GBaseFinalizeFunc) NULL,
972 (GClassInitFunc) ps_document_class_init,
973 (GClassFinalizeFunc) NULL,
974 NULL, /* class_data */
977 (GInstanceInitFunc) ps_document_init
980 static const GInterfaceInfo document_info =
982 (GInterfaceInitFunc) ps_document_document_iface_init,
987 gs_type = g_type_register_static(G_TYPE_OBJECT,
988 "PSDocument", &gs_info, 0);
990 g_type_add_interface_static (gs_type,
1000 document_load(PSDocument * gs, const gchar * fname)
1002 g_return_val_if_fail(gs != NULL, FALSE);
1003 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1005 LOG ("Load the document");
1007 /* clean up previous document */
1008 ps_document_cleanup(gs);
1015 /* prepare this document */
1016 gs->structured_doc = FALSE;
1017 gs->send_filename_to_gs = TRUE;
1018 gs->current_page = 0;
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);
1053 ps_document_cleanup(gs);
1057 /* we grab the vital statistics!!! */
1058 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
1060 g_object_notify (G_OBJECT (gs), "title");
1062 if(gs->doc == NULL) {
1063 /* File does not seem to be a Postscript one */
1065 g_snprintf(buf, 1024, _("Error while scanning file %s\n"), fname);
1066 interpreter_failed (gs, buf);
1067 ps_document_cleanup(gs);
1068 gs->gs_status = _("The file is not a PostScript document.");
1072 if((!gs->doc->epsf && gs->doc->numpages > 0) ||
1073 (gs->doc->epsf && gs->doc->numpages > 1)) {
1074 gs->structured_doc = TRUE;
1075 gs->send_filename_to_gs = FALSE;
1079 compute_dimensions (gs, gs->current_page);
1081 gs->gs_status = _("Document loaded.");
1088 ps_document_next_page (PSDocument *gs)
1092 LOG ("Make ghostscript render next page");
1094 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
1095 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
1096 g_return_val_if_fail (gs->busy != TRUE, FALSE);
1100 event.xclient.type = ClientMessage;
1101 event.xclient.display = gdk_display;
1102 event.xclient.window = gs->message_window;
1103 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1104 event.xclient.format = 32;
1106 gdk_error_trap_push ();
1107 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1109 gdk_error_trap_pop ();
1115 render_page (PSDocument *gs, int page)
1117 g_return_val_if_fail(gs != NULL, FALSE);
1118 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1120 if(!gs->gs_filename) {
1124 if (gs->structured_doc && gs->doc) {
1125 LOG ("It's a structured document, let's send one page to gs");
1127 if (is_interpreter_ready (gs)) {
1128 ps_document_next_page (gs);
1130 ps_document_enable_interpreter (gs);
1131 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1132 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1135 send_ps (gs, gs->doc->pages[page].begin,
1136 gs->doc->pages[page].len, FALSE);
1138 /* Unstructured document
1140 * In the case of non structured documents,
1141 * GS read the PS from the actual file (via command
1142 * line. Hence, ggv only send a signal next page.
1143 * If ghostview is not running it is usually because
1144 * the last page of the file was displayed. In that
1145 * case, ggv restarts GS again and the first page is displayed.
1148 LOG ("It's an unstructured document, gs will just read the file");
1150 if (!is_interpreter_ready (gs)) {
1151 ps_document_enable_interpreter(gs);
1153 ps_document_next_page(gs);
1160 ps_document_load (EvDocument *document,
1167 filename = g_filename_from_uri (uri, NULL, error);
1171 result = document_load (PS_DOCUMENT (document), filename);
1173 g_set_error (error, G_FILE_ERROR,
1174 G_FILE_ERROR_FAILED,
1175 "Failed to load document '%s'\n",
1185 ps_document_save (EvDocument *document,
1189 g_warning ("ps_document_save not implemented"); /* FIXME */
1194 ps_document_get_n_pages (EvDocument *document)
1196 PSDocument *ps = PS_DOCUMENT (document);
1198 g_return_val_if_fail (ps != NULL, -1);
1200 if (!ps->gs_filename || !ps->doc) {
1204 return ps->structured_doc ? ps->doc->numpages : 1;
1208 ps_document_get_page_size (EvDocument *document,
1213 /* Post script documents never vary in size */
1215 PSDocument *gs = PS_DOCUMENT (document);
1217 compute_dimensions (gs, page);
1224 *height = gs->height;
1229 ps_document_get_text (EvDocument *document, int page, EvRectangle *rect)
1231 g_warning ("ps_document_get_text not implemented"); /* FIXME ? */
1236 render_pixbuf_idle (PSRenderJob *job)
1238 PSDocument *gs = job->document;
1240 if (gs->pstarget == NULL) {
1243 widget = gtk_window_new (GTK_WINDOW_POPUP);
1244 gtk_widget_realize (widget);
1245 gs->pstarget = widget->window;
1247 g_assert (gs->pstarget != NULL);
1249 g_signal_connect (widget, "event",
1250 G_CALLBACK (ps_document_widget_event),
1255 stop_interpreter (gs);
1257 setup_page (gs, job->scale);
1258 gs->changed = FALSE;
1261 render_page (gs, job->page);
1267 ps_document_render_pixbuf (EvDocument *document, int page, double scale)
1274 job.document = PS_DOCUMENT (document);
1275 g_idle_add ((GSourceFunc)render_pixbuf_idle, &job);
1277 g_mutex_lock (pixbuf_mutex);
1278 while (!current_pixbuf)
1279 g_cond_wait (pixbuf_cond, pixbuf_mutex);
1280 pixbuf = current_pixbuf;
1281 current_pixbuf = NULL;
1282 g_mutex_unlock (pixbuf_mutex);
1284 LOG ("Pixbuf rendered %p\n", pixbuf);
1290 ps_document_document_iface_init (EvDocumentIface *iface)
1292 iface->load = ps_document_load;
1293 iface->save = ps_document_save;
1294 iface->get_text = ps_document_get_text;
1295 iface->get_n_pages = ps_document_get_n_pages;
1296 iface->get_page_size = ps_document_get_page_size;
1297 iface->render_pixbuf = ps_document_render_pixbuf;