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;
85 static gboolean broken_pipe = FALSE;
87 /* Forward declarations */
88 static void ps_document_init(PSDocument * gs);
89 static void ps_document_class_init(PSDocumentClass * klass);
90 static void ps_document_finalize(GObject * object);
91 static void send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close);
92 static void close_pipe(int p[2]);
93 static void output(gpointer data, gint source, GdkInputCondition condition);
94 static void input(gpointer data, gint source, GdkInputCondition condition);
95 static void stop_interpreter(PSDocument * gs);
96 static gint start_interpreter(PSDocument * gs);
97 static void ps_document_document_iface_init (EvDocumentIface *iface);
98 static gboolean ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data);
100 static GObjectClass *parent_class = NULL;
102 static PSDocumentClass *gs_class = NULL;
105 ps_document_init (PSDocument *gs)
109 gs->current_page = 0;
110 gs->interpreter_pid = -1;
116 gs->gs_filename_unc = 0;
120 gs->structured_doc = FALSE;
121 gs->reading_from_pipe = FALSE;
122 gs->send_filename_to_gs = FALSE;
127 gs->interpreter_input = -1;
128 gs->interpreter_output = -1;
129 gs->interpreter_err = -1;
130 gs->interpreter_input_id = 0;
131 gs->interpreter_output_id = 0;
132 gs->interpreter_error_id = 0;
135 gs->input_buffer = NULL;
136 gs->input_buffer_ptr = NULL;
138 gs->buffer_bytes_left = 0;
140 gs->zoom_factor = 1.0;
142 gs->gs_status = _("No document loaded.");
144 pixbuf_cond = g_cond_new ();
145 pixbuf_mutex = g_mutex_new ();
149 ps_document_set_property (GObject *object,
164 ps_document_get_property (GObject *object,
169 PSDocument *ps = PS_DOCUMENT (object);
175 g_value_set_string (value, ps->doc->title);
177 g_value_set_string (value, NULL);
184 ps_document_class_init(PSDocumentClass *klass)
186 GObjectClass *object_class;
188 object_class = (GObjectClass *) klass;
189 parent_class = g_type_class_peek_parent (klass);
192 object_class->finalize = ps_document_finalize;
193 object_class->get_property = ps_document_get_property;
194 object_class->set_property = ps_document_set_property;
196 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
197 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
198 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
199 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
201 g_object_class_override_property (object_class, PROP_TITLE, "title");
205 push_pixbuf (PSDocument *gs)
210 cmap = gdk_window_get_colormap (gs->pstarget);
212 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
214 gs->width, gs->height);
215 g_mutex_lock (pixbuf_mutex);
216 current_pixbuf = pixbuf;
217 g_cond_signal (pixbuf_cond);
218 g_mutex_unlock (pixbuf_mutex);
223 interpreter_failed (PSDocument *gs, char *msg)
225 LOG ("Interpreter failed %s", msg);
229 stop_interpreter (gs);
233 ps_document_cleanup (PSDocument *gs)
235 g_return_if_fail (gs != NULL);
236 g_return_if_fail (PS_IS_DOCUMENT (gs));
240 stop_interpreter (gs);
243 fclose (gs->gs_psfile);
244 gs->gs_psfile = NULL;
247 if (gs->gs_filename) {
248 g_free (gs->gs_filename);
249 gs->gs_filename = NULL;
257 if (gs->gs_filename_unc) {
258 unlink(gs->gs_filename_unc);
259 g_free(gs->gs_filename_unc);
260 gs->gs_filename_unc = NULL;
263 gs->current_page = 0;
268 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
270 PSDocument *gs = (PSDocument *) data;
272 if(event->type != GDK_CLIENT_EVENT)
275 gs->message_window = event->client.data.l[0];
277 if (event->client.message_type == gs_class->page_atom) {
278 LOG ("GS rendered the document");
288 ps_document_finalize (GObject * object)
292 g_return_if_fail (object != NULL);
293 g_return_if_fail (PS_IS_DOCUMENT (object));
297 gs = PS_DOCUMENT (object);
299 ps_document_cleanup (gs);
300 stop_interpreter (gs);
302 if(gs->input_buffer) {
303 g_free(gs->input_buffer);
304 gs->input_buffer = NULL;
307 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
311 send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close)
313 struct record_list *ps_new;
315 if(gs->interpreter_input < 0) {
316 g_critical("No pipe to gs: error in send_ps().");
320 ps_new = (struct record_list *) g_malloc(sizeof(struct record_list));
321 ps_new->fp = gs->gs_psfile;
322 ps_new->begin = begin;
324 ps_new->seek_needed = TRUE;
325 ps_new->close = close;
328 if(gs->input_buffer == NULL) {
329 gs->input_buffer = g_malloc(MAX_BUFSIZE);
332 if(gs->ps_input == NULL) {
333 gs->input_buffer_ptr = gs->input_buffer;
334 gs->bytes_left = len;
335 gs->buffer_bytes_left = 0;
336 gs->ps_input = ps_new;
337 gs->interpreter_input_id =
338 gdk_input_add(gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
341 struct record_list *p = gs->ps_input;
342 while(p->next != NULL) {
350 get_xdpi (PSDocument *gs)
352 return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
356 get_ydpi (PSDocument *gs)
358 return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
362 setup_pixmap (PSDocument *gs)
365 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
366 GdkColormap *colormap;
368 LOG ("Create our internal pixmap");
371 gdk_drawable_unref(gs->bpixmap);
374 fill = gdk_gc_new (gs->pstarget);
375 colormap = gdk_drawable_get_colormap (gs->pstarget);
376 gdk_color_alloc (colormap, &white);
377 gdk_gc_set_foreground (fill, &white);
378 gs->bpixmap = gdk_pixmap_new (gs->pstarget, gs->width, gs->height, -1);
379 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
380 0, 0, gs->width, gs->height);
384 setup_page (PSDocument *gs)
391 LOG ("Setup the page");
394 /* gs needs floating point parameters with '.' as decimal point
395 * while some (european) locales use ',' instead, so we set the
396 * locale for this snprintf to "C".
398 savelocale = setlocale (LC_NUMERIC, "C");
401 g_snprintf (buf, 1024, "%ld %d %d %d %d %d %f %f %d %d %d %d",
402 0L, gs->orientation * 90, gs->llx, gs->lly, gs->urx, gs->ury,
403 get_xdpi (gs) * gs->zoom_factor,
404 get_ydpi (gs) * gs->zoom_factor,
406 LOG ("GS property %s", buf);
409 setlocale(LC_NUMERIC, savelocale);
411 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
412 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
417 close_pipe (int p[2])
428 is_interpreter_ready (PSDocument *gs)
430 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
434 output(gpointer data, gint source, GdkInputCondition condition)
436 char buf[MAX_BUFSIZE + 1], *msg;
438 PSDocument *gs = PS_DOCUMENT(data);
440 if(source == gs->interpreter_output) {
441 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
442 if(bytes == 0) { /* EOF occurred */
443 close(gs->interpreter_output);
444 gs->interpreter_output = -1;
445 gdk_input_remove(gs->interpreter_output_id);
448 else if(bytes == -1) {
450 interpreter_failed(gs, NULL);
453 if(gs->interpreter_err == -1) {
454 interpreter_failed(gs, NULL);
457 else if(source == gs->interpreter_err) {
458 bytes = read(gs->interpreter_err, buf, MAX_BUFSIZE);
459 if(bytes == 0) { /* EOF occurred */
460 close(gs->interpreter_err);
461 gs->interpreter_err = -1;
462 gdk_input_remove(gs->interpreter_error_id);
465 else if(bytes == -1) {
467 interpreter_failed(gs, NULL);
470 if(gs->interpreter_output == -1) {
471 interpreter_failed(gs, NULL);
477 interpreter_failed (gs, msg);
488 input(gpointer data, gint source, GdkInputCondition condition)
490 PSDocument *gs = PS_DOCUMENT(data);
492 void (*oldsig) (int);
493 oldsig = signal(SIGPIPE, catchPipe);
498 if(gs->buffer_bytes_left == 0) {
499 /* Get a new section if required */
500 if(gs->ps_input && gs->bytes_left == 0) {
501 struct record_list *ps_old = gs->ps_input;
502 gs->ps_input = ps_old->next;
503 if(ps_old->close && NULL != ps_old->fp)
505 g_free((char *) ps_old);
507 /* Have to seek at the beginning of each section */
508 if(gs->ps_input && gs->ps_input->seek_needed) {
509 fseek(gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
510 gs->ps_input->seek_needed = FALSE;
511 gs->bytes_left = gs->ps_input->len;
514 if(gs->bytes_left > MAX_BUFSIZE) {
515 gs->buffer_bytes_left =
516 fread(gs->input_buffer, sizeof(char), MAX_BUFSIZE, gs->ps_input->fp);
518 else if(gs->bytes_left > 0) {
519 gs->buffer_bytes_left =
520 fread(gs->input_buffer,
521 sizeof(char), gs->bytes_left, gs->ps_input->fp);
524 gs->buffer_bytes_left = 0;
526 if(gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
527 interpreter_failed (gs, NULL); /* Error occurred */
529 gs->input_buffer_ptr = gs->input_buffer;
530 gs->bytes_left -= gs->buffer_bytes_left;
533 if(gs->buffer_bytes_left > 0) {
534 bytes_written = write(gs->interpreter_input,
535 gs->input_buffer_ptr, gs->buffer_bytes_left);
538 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
540 interpreter_failed (gs, NULL);
542 else if(bytes_written == -1) {
543 if((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
544 interpreter_failed (gs, NULL); /* Something bad happened */
548 gs->buffer_bytes_left -= bytes_written;
549 gs->input_buffer_ptr += bytes_written;
553 while(gs->ps_input && gs->buffer_bytes_left == 0);
555 signal(SIGPIPE, oldsig);
557 if(gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
558 if(gs->interpreter_input_id != 0) {
559 gdk_input_remove(gs->interpreter_input_id);
560 gs->interpreter_input_id = 0;
566 start_interpreter (PSDocument *gs)
568 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
569 int std_out[2]; /* pipe from interp stdout */
570 int std_err[2]; /* pipe from interp stderr */
573 #define NUM_GS_ARGS (NUM_ARGS - 20)
574 #define NUM_ALPHA_ARGS 10
576 char *argv[NUM_ARGS], *dir, *gv_env;
577 char **gs_args, **alpha_args = NULL;
580 LOG ("Start the interpreter");
585 stop_interpreter(gs);
587 /* set up the args... */
588 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
589 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
590 argv[argc] = gs_args[i];
593 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
595 argv[argc++] = "-dNOPAUSE";
596 argv[argc++] = "-dQUIET";
597 argv[argc++] = "-dSAFER";
599 /* set up the pipes */
600 if (gs->send_filename_to_gs) {
601 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
603 argv[argc++] = "quit";
610 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
611 if (pipe (std_in) == -1) {
612 g_critical ("Unable to open pipe to Ghostscript.");
617 if (pipe (std_out) == -1) {
622 if (pipe(std_err) == -1) {
624 close_pipe (std_out);
628 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
629 gdk_x11_drawable_get_xid (gs->pstarget),
630 gdk_x11_drawable_get_xid (gs->bpixmap));
631 LOG ("Launching ghostview with env %s", gv_env);
634 gs->interpreter_pid = fork ();
635 switch (gs->interpreter_pid) {
638 close_pipe (std_out);
639 close_pipe (std_err);
644 dup2 (std_out[1], 1);
648 dup2 (std_err[1], 2);
651 if (!gs->reading_from_pipe) {
652 if (gs->send_filename_to_gs) {
654 /* just in case gs tries to read from stdin */
655 stdinfd = open("/dev/null", O_RDONLY);
669 /* change to directory where the input file is. This helps
670 * with postscript-files which include other files using
672 dir = g_path_get_dirname (gs->gs_filename);
676 execvp (argv[0], argv);
679 g_critical ("Unable to execute [%s]\n", argv[0]);
680 g_strfreev (gs_args);
682 g_strfreev (alpha_args);
685 default: /* parent */
686 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
689 /* use non-blocking IO for pipe to ghostscript */
690 result = fcntl (std_in[1], F_GETFL, 0);
691 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
692 gs->interpreter_input = std_in[1];
694 gs->interpreter_input = -1;
698 gs->interpreter_output = std_out[0];
700 gs->interpreter_err = std_err[0];
701 gs->interpreter_output_id =
702 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
703 gs->interpreter_error_id =
704 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
712 stop_interpreter(PSDocument * gs)
714 if(gs->interpreter_pid > 0) {
716 LOG ("Stop the interpreter");
717 kill(gs->interpreter_pid, SIGTERM);
718 while((wait(&status) == -1) && (errno == EINTR)) ;
719 gs->interpreter_pid = -1;
721 ps_document_cleanup(gs);
722 gs->gs_status = _("Interpreter failed.");
726 if(gs->interpreter_input >= 0) {
727 close(gs->interpreter_input);
728 gs->interpreter_input = -1;
729 if(gs->interpreter_input_id != 0) {
730 gdk_input_remove(gs->interpreter_input_id);
731 gs->interpreter_input_id = 0;
733 while(gs->ps_input) {
734 struct record_list *ps_old = gs->ps_input;
735 gs->ps_input = gs->ps_input->next;
736 if(ps_old->close && NULL != ps_old->fp)
738 g_free((char *) ps_old);
742 if(gs->interpreter_output >= 0) {
743 close(gs->interpreter_output);
744 gs->interpreter_output = -1;
745 if(gs->interpreter_output_id) {
746 gdk_input_remove(gs->interpreter_output_id);
747 gs->interpreter_output_id = 0;
751 if(gs->interpreter_err >= 0) {
752 close(gs->interpreter_err);
753 gs->interpreter_err = -1;
754 if(gs->interpreter_error_id) {
755 gdk_input_remove(gs->interpreter_error_id);
756 gs->interpreter_error_id = 0;
763 /* If file exists and is a regular file then return its length, else -1 */
765 file_length(const gchar * filename)
767 struct stat stat_rec;
769 if(filename && (stat(filename, &stat_rec) == 0)
770 && S_ISREG(stat_rec.st_mode))
771 return stat_rec.st_size;
776 /* Test if file exists, is a regular file and its length is > 0 */
778 file_readable(const char *filename)
780 return (file_length(filename) > 0);
784 * Decompress gs->gs_filename if necessary
785 * Set gs->filename_unc to the name of the uncompressed file or NULL.
786 * Error reporting via signal 'interpreter_message'
787 * Return name of input file to use or NULL on error..
790 check_filecompressed(PSDocument * gs)
794 gchar *filename, *filename_unc, *filename_err, *cmdline;
800 if((file = fopen(gs->gs_filename, "r"))
801 && (fread(buf, sizeof(gchar), 3, file) == 3)) {
802 if((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
803 /* file is gzipped or compressed */
804 cmd = gtk_gs_defaults_get_ungzip_cmd();
806 else if(strncmp(buf, "BZh", 3) == 0) {
807 /* file is compressed with bzip2 */
808 cmd = gtk_gs_defaults_get_unbzip2_cmd();
815 return gs->gs_filename;
817 /* do the decompression */
818 filename = g_shell_quote(gs->gs_filename);
819 filename_unc = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
820 if((fd = mkstemp(filename_unc)) < 0) {
821 g_free(filename_unc);
826 filename_err = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
827 if((fd = mkstemp(filename_err)) < 0) {
828 g_free(filename_err);
829 g_free(filename_unc);
834 cmdline = g_strdup_printf("%s %s >%s 2>%s", cmd,
835 filename, filename_unc, filename_err);
836 if((system(cmdline) == 0)
837 && file_readable(filename_unc)
838 && (file_length(filename_err) == 0)) {
839 /* sucessfully uncompressed file */
840 gs->gs_filename_unc = filename_unc;
844 g_snprintf(buf, 1024, _("Error while decompressing file %s:\n"),
846 interpreter_failed (gs, buf);
847 unlink(filename_unc);
848 g_free(filename_unc);
851 unlink(filename_err);
852 g_free(filename_err);
859 compute_dimensions (PSDocument *gs, int page)
861 GtkGSPaperSize *paper_sizes = gtk_gs_defaults_get_paper_sizes ();
862 int urx, ury, llx, lly;
866 g_return_if_fail (PS_IS_DOCUMENT (gs));
867 g_return_if_fail (gs->doc != NULL);
868 g_return_if_fail (page >= 0);
869 g_return_if_fail (gs->doc->numpages > page);
871 orientation = GTK_GS_ORIENTATION_NONE;
872 if (gs->structured_doc) {
873 orientation = gs->doc->pages[gs->current_page].orientation;
875 if (orientation == GTK_GS_ORIENTATION_NONE) {
876 orientation = GTK_GS_ORIENTATION_PORTRAIT;
879 if (gs->doc->pages && gs->doc->pages[page].size) {
882 page_size = gs->doc->pages[page].size - gs->doc->size;
884 urx = gs->doc->size[page_size].width;
885 ury = gs->doc->size[page_size].height;
886 } else if (gs->doc->pages &&
887 (gs->doc->pages[page].boundingbox[URX] >
888 gs->doc->pages[page].boundingbox[LLX]) &&
889 (gs->doc->pages[page].boundingbox[URY] >
890 gs->doc->pages[page].boundingbox[LLY])) {
891 llx = gs->doc->pages[page].boundingbox[LLX];
892 lly = gs->doc->pages[page].boundingbox[LLY];
893 urx = gs->doc->pages[page].boundingbox[URX];
894 ury = gs->doc->pages[page].boundingbox[URY];
895 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
896 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
897 llx = gs->doc->boundingbox[LLX];
898 lly = gs->doc->boundingbox[LLY];
899 urx = gs->doc->boundingbox[URX];
900 ury = gs->doc->boundingbox[URY];
904 urx = paper_sizes[12].width;
905 ury = paper_sizes[12].height;
908 switch (orientation) {
909 case GTK_GS_ORIENTATION_PORTRAIT:
910 case GTK_GS_ORIENTATION_UPSIDEDOWN:
911 width = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
912 height = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
914 case GTK_GS_ORIENTATION_LANDSCAPE:
915 case GTK_GS_ORIENTATION_SEASCAPE:
916 width = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
917 height = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
921 g_assert_not_reached ();
925 width = width * gs->zoom_factor;
926 height = height * gs->zoom_factor;
928 if (llx != gs->llx || lly != gs->lly ||
929 urx != gs->urx || ury != gs->ury ||
930 gs->width != width || gs->height != height ||
931 orientation != gs->orientation) {
938 gs->orientation = orientation;
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 gs_type = g_type_register_static(G_TYPE_OBJECT,
982 "PSDocument", &gs_info, 0);
984 g_type_add_interface_static (gs_type,
994 document_load(PSDocument * gs, const gchar * fname)
996 g_return_val_if_fail(gs != NULL, FALSE);
997 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
999 LOG ("Load the document");
1001 /* clean up previous document */
1002 ps_document_cleanup(gs);
1009 /* prepare this document */
1010 gs->structured_doc = FALSE;
1011 gs->send_filename_to_gs = TRUE;
1012 gs->current_page = 0;
1015 /* an absolute path */
1016 gs->gs_filename = g_strdup(fname);
1019 /* path relative to our cwd: make it absolute */
1020 gchar *cwd = g_get_current_dir();
1021 gs->gs_filename = g_strconcat(cwd, "/", fname, NULL);
1025 if((gs->reading_from_pipe = (strcmp(fname, "-") == 0))) {
1026 gs->send_filename_to_gs = FALSE;
1030 * We need to make sure that the file is loadable/exists!
1031 * otherwise we want to exit without loading new stuff...
1033 gchar *filename = NULL;
1035 if(!file_readable(fname)) {
1037 g_snprintf(buf, 1024, _("Cannot open file %s.\n"), fname);
1038 interpreter_failed (gs, buf);
1039 gs->gs_status = _("File is not readable.");
1042 filename = check_filecompressed(gs);
1045 if(!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
1046 interpreter_failed (gs, NULL);
1047 ps_document_cleanup(gs);
1051 /* we grab the vital statistics!!! */
1052 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
1054 g_object_notify (G_OBJECT (gs), "title");
1056 if(gs->doc == NULL) {
1057 /* File does not seem to be a Postscript one */
1059 g_snprintf(buf, 1024, _("Error while scanning file %s\n"), fname);
1060 interpreter_failed (gs, buf);
1061 ps_document_cleanup(gs);
1062 gs->gs_status = _("The file is not a PostScript document.");
1066 if((!gs->doc->epsf && gs->doc->numpages > 0) ||
1067 (gs->doc->epsf && gs->doc->numpages > 1)) {
1068 gs->structured_doc = TRUE;
1069 gs->send_filename_to_gs = FALSE;
1073 compute_dimensions (gs, gs->current_page);
1075 gs->gs_status = _("Document loaded.");
1082 ps_document_next_page (PSDocument *gs)
1086 LOG ("Make ghostscript render next page");
1088 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
1089 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
1090 g_return_val_if_fail (gs->busy != TRUE, FALSE);
1094 event.xclient.type = ClientMessage;
1095 event.xclient.display = gdk_display;
1096 event.xclient.window = gs->message_window;
1097 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1098 event.xclient.format = 32;
1100 gdk_error_trap_push ();
1101 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1103 gdk_error_trap_pop ();
1109 render_page (PSDocument *gs)
1111 g_return_val_if_fail(gs != NULL, FALSE);
1112 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1114 if(!gs->gs_filename) {
1118 if (gs->structured_doc && gs->doc) {
1119 LOG ("It's a structured document, let's send one page to gs");
1121 if (is_interpreter_ready (gs)) {
1122 ps_document_next_page (gs);
1124 ps_document_enable_interpreter (gs);
1125 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1126 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1129 send_ps (gs, gs->doc->pages[gs->current_page].begin,
1130 gs->doc->pages[gs->current_page].len, FALSE);
1132 /* Unstructured document
1134 * In the case of non structured documents,
1135 * GS read the PS from the actual file (via command
1136 * line. Hence, ggv only send a signal next page.
1137 * If ghostview is not running it is usually because
1138 * the last page of the file was displayed. In that
1139 * case, ggv restarts GS again and the first page is displayed.
1142 LOG ("It's an unstructured document, gs will just read the file");
1144 if (!is_interpreter_ready (gs)) {
1145 ps_document_enable_interpreter(gs);
1147 ps_document_next_page(gs);
1154 ps_document_load (EvDocument *document,
1161 filename = g_filename_from_uri (uri, NULL, error);
1165 result = document_load (PS_DOCUMENT (document), filename);
1167 g_set_error (error, G_FILE_ERROR,
1168 G_FILE_ERROR_FAILED,
1169 "Failed to load document '%s'\n",
1179 ps_document_save (EvDocument *document,
1183 g_warning ("ps_document_save not implemented"); /* FIXME */
1188 ps_document_get_n_pages (EvDocument *document)
1190 PSDocument *ps = PS_DOCUMENT (document);
1192 g_return_val_if_fail (ps != NULL, -1);
1194 if (!ps->gs_filename || !ps->doc) {
1198 return ps->structured_doc ? ps->doc->numpages : 1;
1202 ps_document_set_page (EvDocument *document,
1205 PSDocument *gs = PS_DOCUMENT (document);
1207 LOG ("Set document page %d\n", page);
1209 gs->current_page = page;
1210 compute_dimensions (gs, page);
1214 ps_document_get_page (EvDocument *document)
1216 PSDocument *ps = PS_DOCUMENT (document);
1218 g_return_val_if_fail (ps != NULL, -1);
1220 return ps->current_page;
1224 ps_document_set_scale (EvDocument *document,
1227 PSDocument *gs = PS_DOCUMENT (document);
1229 gs->zoom_factor = scale;
1230 compute_dimensions (gs, gs->current_page);
1234 ps_document_get_page_size (EvDocument *document,
1239 /* Post script documents never vary in size */
1241 PSDocument *gs = PS_DOCUMENT (document);
1248 *height = gs->height;
1253 ps_document_get_text (EvDocument *document, GdkRectangle *rect)
1255 g_warning ("ps_document_get_text not implemented"); /* FIXME ? */
1260 ps_document_get_link (EvDocument *document,
1268 render_pixbuf_idle (EvDocument *document)
1270 PSDocument *gs = PS_DOCUMENT (document);
1272 if (gs->pstarget == NULL) {
1275 widget = gtk_window_new (GTK_WINDOW_POPUP);
1276 gtk_widget_realize (widget);
1277 gs->pstarget = widget->window;
1279 g_assert (gs->pstarget != NULL);
1281 g_signal_connect (widget, "event",
1282 G_CALLBACK (ps_document_widget_event),
1287 stop_interpreter (gs);
1290 gs->changed = FALSE;
1293 render_page (PS_DOCUMENT (document));
1299 ps_document_render_pixbuf (EvDocument *document)
1303 g_idle_add ((GSourceFunc)render_pixbuf_idle, document);
1305 g_mutex_lock (pixbuf_mutex);
1306 while (!current_pixbuf)
1307 g_cond_wait (pixbuf_cond, pixbuf_mutex);
1308 pixbuf = current_pixbuf;
1309 current_pixbuf = NULL;
1310 g_mutex_unlock (pixbuf_mutex);
1312 LOG ("Pixbuf rendered %p\n", pixbuf);
1318 ps_document_document_iface_init (EvDocumentIface *iface)
1320 iface->load = ps_document_load;
1321 iface->save = ps_document_save;
1322 iface->get_text = ps_document_get_text;
1323 iface->get_link = ps_document_get_link;
1324 iface->get_n_pages = ps_document_get_n_pages;
1325 iface->set_page = ps_document_set_page;
1326 iface->get_page = ps_document_get_page;
1327 iface->set_scale = ps_document_set_scale;
1328 iface->get_page_size = ps_document_get_page_size;
1329 iface->render_pixbuf = ps_document_render_pixbuf;