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->page_x_offset = 0;
141 gs->page_y_offset = 0;
142 gs->zoom_factor = 1.0;
144 gs->gs_status = _("No document loaded.");
146 pixbuf_cond = g_cond_new ();
147 pixbuf_mutex = g_mutex_new ();
151 ps_document_set_property (GObject *object,
166 ps_document_get_property (GObject *object,
171 PSDocument *ps = PS_DOCUMENT (object);
177 g_value_set_string (value, ps->doc->title);
179 g_value_set_string (value, NULL);
186 ps_document_class_init(PSDocumentClass *klass)
188 GObjectClass *object_class;
190 object_class = (GObjectClass *) klass;
191 parent_class = g_type_class_peek_parent (klass);
194 object_class->finalize = ps_document_finalize;
195 object_class->get_property = ps_document_get_property;
196 object_class->set_property = ps_document_set_property;
198 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
199 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
200 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
201 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
203 g_object_class_override_property (object_class, PROP_TITLE, "title");
207 push_pixbuf (PSDocument *gs)
212 cmap = gdk_window_get_colormap (gs->pstarget);
214 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
216 gs->width, gs->height);
217 g_mutex_lock (pixbuf_mutex);
218 current_pixbuf = pixbuf;
219 g_cond_signal (pixbuf_cond);
220 g_mutex_unlock (pixbuf_mutex);
225 interpreter_failed (PSDocument *gs, char *msg)
227 LOG ("Interpreter failed %s", msg);
231 stop_interpreter (gs);
235 ps_document_cleanup (PSDocument *gs)
237 g_return_if_fail (gs != NULL);
238 g_return_if_fail (PS_IS_DOCUMENT (gs));
242 stop_interpreter (gs);
245 fclose (gs->gs_psfile);
246 gs->gs_psfile = NULL;
249 if (gs->gs_filename) {
250 g_free (gs->gs_filename);
251 gs->gs_filename = NULL;
259 if (gs->gs_filename_unc) {
260 unlink(gs->gs_filename_unc);
261 g_free(gs->gs_filename_unc);
262 gs->gs_filename_unc = NULL;
265 gs->current_page = 0;
270 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
272 PSDocument *gs = (PSDocument *) data;
274 if(event->type != GDK_CLIENT_EVENT)
277 gs->message_window = event->client.data.l[0];
279 if (event->client.message_type == gs_class->page_atom) {
280 LOG ("GS rendered the document");
290 ps_document_finalize (GObject * object)
294 g_return_if_fail (object != NULL);
295 g_return_if_fail (PS_IS_DOCUMENT (object));
299 gs = PS_DOCUMENT (object);
301 ps_document_cleanup (gs);
302 stop_interpreter (gs);
304 if(gs->input_buffer) {
305 g_free(gs->input_buffer);
306 gs->input_buffer = NULL;
309 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
313 send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close)
315 struct record_list *ps_new;
317 if(gs->interpreter_input < 0) {
318 g_critical("No pipe to gs: error in send_ps().");
322 ps_new = (struct record_list *) g_malloc(sizeof(struct record_list));
323 ps_new->fp = gs->gs_psfile;
324 ps_new->begin = begin;
326 ps_new->seek_needed = TRUE;
327 ps_new->close = close;
330 if(gs->input_buffer == NULL) {
331 gs->input_buffer = g_malloc(MAX_BUFSIZE);
334 if(gs->ps_input == NULL) {
335 gs->input_buffer_ptr = gs->input_buffer;
336 gs->bytes_left = len;
337 gs->buffer_bytes_left = 0;
338 gs->ps_input = ps_new;
339 gs->interpreter_input_id =
340 gdk_input_add(gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
343 struct record_list *p = gs->ps_input;
344 while(p->next != NULL) {
352 get_xdpi (PSDocument *gs)
354 return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
358 get_ydpi (PSDocument *gs)
360 return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
364 setup_pixmap (PSDocument *gs)
367 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
368 GdkColormap *colormap;
370 LOG ("Create our internal pixmap");
373 gdk_drawable_unref(gs->bpixmap);
376 fill = gdk_gc_new (gs->pstarget);
377 colormap = gdk_drawable_get_colormap (gs->pstarget);
378 gdk_color_alloc (colormap, &white);
379 gdk_gc_set_foreground (fill, &white);
380 gs->bpixmap = gdk_pixmap_new (gs->pstarget, gs->width, gs->height, -1);
381 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
382 0, 0, gs->width, gs->height);
386 setup_page (PSDocument *gs)
393 LOG ("Setup the page");
396 /* gs needs floating point parameters with '.' as decimal point
397 * while some (european) locales use ',' instead, so we set the
398 * locale for this snprintf to "C".
400 savelocale = setlocale (LC_NUMERIC, "C");
403 g_snprintf (buf, 1024, "%ld %d %d %d %d %d %f %f %d %d %d %d",
404 0L, gs->orientation * 90, gs->llx, gs->lly, gs->urx, gs->ury,
405 get_xdpi (gs) * gs->zoom_factor,
406 get_ydpi (gs) * gs->zoom_factor,
408 LOG ("GS property %s", buf);
411 setlocale(LC_NUMERIC, savelocale);
413 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
414 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
419 close_pipe (int p[2])
430 is_interpreter_ready (PSDocument *gs)
432 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
436 output(gpointer data, gint source, GdkInputCondition condition)
438 char buf[MAX_BUFSIZE + 1], *msg;
440 PSDocument *gs = PS_DOCUMENT(data);
442 if(source == gs->interpreter_output) {
443 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
444 if(bytes == 0) { /* EOF occurred */
445 close(gs->interpreter_output);
446 gs->interpreter_output = -1;
447 gdk_input_remove(gs->interpreter_output_id);
450 else if(bytes == -1) {
452 interpreter_failed(gs, NULL);
455 if(gs->interpreter_err == -1) {
456 interpreter_failed(gs, NULL);
459 else if(source == gs->interpreter_err) {
460 bytes = read(gs->interpreter_err, buf, MAX_BUFSIZE);
461 if(bytes == 0) { /* EOF occurred */
462 close(gs->interpreter_err);
463 gs->interpreter_err = -1;
464 gdk_input_remove(gs->interpreter_error_id);
467 else if(bytes == -1) {
469 interpreter_failed(gs, NULL);
472 if(gs->interpreter_output == -1) {
473 interpreter_failed(gs, NULL);
479 interpreter_failed (gs, msg);
490 input(gpointer data, gint source, GdkInputCondition condition)
492 PSDocument *gs = PS_DOCUMENT(data);
494 void (*oldsig) (int);
495 oldsig = signal(SIGPIPE, catchPipe);
500 if(gs->buffer_bytes_left == 0) {
501 /* Get a new section if required */
502 if(gs->ps_input && gs->bytes_left == 0) {
503 struct record_list *ps_old = gs->ps_input;
504 gs->ps_input = ps_old->next;
505 if(ps_old->close && NULL != ps_old->fp)
507 g_free((char *) ps_old);
509 /* Have to seek at the beginning of each section */
510 if(gs->ps_input && gs->ps_input->seek_needed) {
511 fseek(gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
512 gs->ps_input->seek_needed = FALSE;
513 gs->bytes_left = gs->ps_input->len;
516 if(gs->bytes_left > MAX_BUFSIZE) {
517 gs->buffer_bytes_left =
518 fread(gs->input_buffer, sizeof(char), MAX_BUFSIZE, gs->ps_input->fp);
520 else if(gs->bytes_left > 0) {
521 gs->buffer_bytes_left =
522 fread(gs->input_buffer,
523 sizeof(char), gs->bytes_left, gs->ps_input->fp);
526 gs->buffer_bytes_left = 0;
528 if(gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
529 interpreter_failed (gs, NULL); /* Error occurred */
531 gs->input_buffer_ptr = gs->input_buffer;
532 gs->bytes_left -= gs->buffer_bytes_left;
535 if(gs->buffer_bytes_left > 0) {
536 bytes_written = write(gs->interpreter_input,
537 gs->input_buffer_ptr, gs->buffer_bytes_left);
540 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
542 interpreter_failed (gs, NULL);
544 else if(bytes_written == -1) {
545 if((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
546 interpreter_failed (gs, NULL); /* Something bad happened */
550 gs->buffer_bytes_left -= bytes_written;
551 gs->input_buffer_ptr += bytes_written;
555 while(gs->ps_input && gs->buffer_bytes_left == 0);
557 signal(SIGPIPE, oldsig);
559 if(gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
560 if(gs->interpreter_input_id != 0) {
561 gdk_input_remove(gs->interpreter_input_id);
562 gs->interpreter_input_id = 0;
568 start_interpreter (PSDocument *gs)
570 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
571 int std_out[2]; /* pipe from interp stdout */
572 int std_err[2]; /* pipe from interp stderr */
575 #define NUM_GS_ARGS (NUM_ARGS - 20)
576 #define NUM_ALPHA_ARGS 10
578 char *argv[NUM_ARGS], *dir, *gv_env;
579 char **gs_args, **alpha_args = NULL;
582 LOG ("Start the interpreter");
587 stop_interpreter(gs);
589 /* set up the args... */
590 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
591 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
592 argv[argc] = gs_args[i];
595 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
597 argv[argc++] = "-dNOPAUSE";
598 argv[argc++] = "-dQUIET";
599 argv[argc++] = "-dSAFER";
601 /* set up the pipes */
602 if (gs->send_filename_to_gs) {
603 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
605 argv[argc++] = "quit";
612 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
613 if (pipe (std_in) == -1) {
614 g_critical ("Unable to open pipe to Ghostscript.");
619 if (pipe (std_out) == -1) {
624 if (pipe(std_err) == -1) {
626 close_pipe (std_out);
630 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
631 gdk_x11_drawable_get_xid (gs->pstarget),
632 gdk_x11_drawable_get_xid (gs->bpixmap));
633 LOG ("Launching ghostview with env %s", gv_env);
636 gs->interpreter_pid = fork ();
637 switch (gs->interpreter_pid) {
640 close_pipe (std_out);
641 close_pipe (std_err);
646 dup2 (std_out[1], 1);
650 dup2 (std_err[1], 2);
653 if (!gs->reading_from_pipe) {
654 if (gs->send_filename_to_gs) {
656 /* just in case gs tries to read from stdin */
657 stdinfd = open("/dev/null", O_RDONLY);
671 /* change to directory where the input file is. This helps
672 * with postscript-files which include other files using
674 dir = g_path_get_dirname (gs->gs_filename);
678 execvp (argv[0], argv);
681 g_critical ("Unable to execute [%s]\n", argv[0]);
682 g_strfreev (gs_args);
684 g_strfreev (alpha_args);
687 default: /* parent */
688 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
691 /* use non-blocking IO for pipe to ghostscript */
692 result = fcntl (std_in[1], F_GETFL, 0);
693 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
694 gs->interpreter_input = std_in[1];
696 gs->interpreter_input = -1;
700 gs->interpreter_output = std_out[0];
702 gs->interpreter_err = std_err[0];
703 gs->interpreter_output_id =
704 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
705 gs->interpreter_error_id =
706 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
714 stop_interpreter(PSDocument * gs)
716 if(gs->interpreter_pid > 0) {
718 LOG ("Stop the interpreter");
719 kill(gs->interpreter_pid, SIGTERM);
720 while((wait(&status) == -1) && (errno == EINTR)) ;
721 gs->interpreter_pid = -1;
723 ps_document_cleanup(gs);
724 gs->gs_status = _("Interpreter failed.");
728 if(gs->interpreter_input >= 0) {
729 close(gs->interpreter_input);
730 gs->interpreter_input = -1;
731 if(gs->interpreter_input_id != 0) {
732 gdk_input_remove(gs->interpreter_input_id);
733 gs->interpreter_input_id = 0;
735 while(gs->ps_input) {
736 struct record_list *ps_old = gs->ps_input;
737 gs->ps_input = gs->ps_input->next;
738 if(ps_old->close && NULL != ps_old->fp)
740 g_free((char *) ps_old);
744 if(gs->interpreter_output >= 0) {
745 close(gs->interpreter_output);
746 gs->interpreter_output = -1;
747 if(gs->interpreter_output_id) {
748 gdk_input_remove(gs->interpreter_output_id);
749 gs->interpreter_output_id = 0;
753 if(gs->interpreter_err >= 0) {
754 close(gs->interpreter_err);
755 gs->interpreter_err = -1;
756 if(gs->interpreter_error_id) {
757 gdk_input_remove(gs->interpreter_error_id);
758 gs->interpreter_error_id = 0;
765 /* If file exists and is a regular file then return its length, else -1 */
767 file_length(const gchar * filename)
769 struct stat stat_rec;
771 if(filename && (stat(filename, &stat_rec) == 0)
772 && S_ISREG(stat_rec.st_mode))
773 return stat_rec.st_size;
778 /* Test if file exists, is a regular file and its length is > 0 */
780 file_readable(const char *filename)
782 return (file_length(filename) > 0);
786 * Decompress gs->gs_filename if necessary
787 * Set gs->filename_unc to the name of the uncompressed file or NULL.
788 * Error reporting via signal 'interpreter_message'
789 * Return name of input file to use or NULL on error..
792 check_filecompressed(PSDocument * gs)
796 gchar *filename, *filename_unc, *filename_err, *cmdline;
802 if((file = fopen(gs->gs_filename, "r"))
803 && (fread(buf, sizeof(gchar), 3, file) == 3)) {
804 if((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
805 /* file is gzipped or compressed */
806 cmd = gtk_gs_defaults_get_ungzip_cmd();
808 else if(strncmp(buf, "BZh", 3) == 0) {
809 /* file is compressed with bzip2 */
810 cmd = gtk_gs_defaults_get_unbzip2_cmd();
817 return gs->gs_filename;
819 /* do the decompression */
820 filename = g_shell_quote(gs->gs_filename);
821 filename_unc = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
822 if((fd = mkstemp(filename_unc)) < 0) {
823 g_free(filename_unc);
828 filename_err = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
829 if((fd = mkstemp(filename_err)) < 0) {
830 g_free(filename_err);
831 g_free(filename_unc);
836 cmdline = g_strdup_printf("%s %s >%s 2>%s", cmd,
837 filename, filename_unc, filename_err);
838 if((system(cmdline) == 0)
839 && file_readable(filename_unc)
840 && (file_length(filename_err) == 0)) {
841 /* sucessfully uncompressed file */
842 gs->gs_filename_unc = filename_unc;
846 g_snprintf(buf, 1024, _("Error while decompressing file %s:\n"),
848 interpreter_failed (gs, buf);
849 unlink(filename_unc);
850 g_free(filename_unc);
853 unlink(filename_err);
854 g_free(filename_err);
861 compute_dimensions (PSDocument *gs, int page)
863 GtkGSPaperSize *paper_sizes = gtk_gs_defaults_get_paper_sizes ();
864 int urx, ury, llx, lly;
868 g_return_if_fail (PS_IS_DOCUMENT (gs));
869 g_return_if_fail (gs->doc != NULL);
870 g_return_if_fail (page >= 0);
871 g_return_if_fail (gs->doc->numpages > page);
873 orientation = GTK_GS_ORIENTATION_NONE;
874 if (gs->structured_doc) {
875 orientation = gs->doc->pages[gs->current_page].orientation;
877 if (orientation == GTK_GS_ORIENTATION_NONE) {
878 orientation = GTK_GS_ORIENTATION_PORTRAIT;
881 if (gs->doc->pages && gs->doc->pages[page].size) {
884 page_size = gs->doc->pages[page].size - gs->doc->size;
886 urx = gs->doc->size[page_size].width;
887 ury = gs->doc->size[page_size].height;
888 } else if (gs->doc->pages &&
889 (gs->doc->pages[page].boundingbox[URX] >
890 gs->doc->pages[page].boundingbox[LLX]) &&
891 (gs->doc->pages[page].boundingbox[URY] >
892 gs->doc->pages[page].boundingbox[LLY])) {
893 llx = gs->doc->pages[page].boundingbox[LLX];
894 lly = gs->doc->pages[page].boundingbox[LLY];
895 urx = gs->doc->pages[page].boundingbox[URX];
896 ury = gs->doc->pages[page].boundingbox[URY];
897 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
898 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
899 llx = gs->doc->boundingbox[LLX];
900 lly = gs->doc->boundingbox[LLY];
901 urx = gs->doc->boundingbox[URX];
902 ury = gs->doc->boundingbox[URY];
906 urx = paper_sizes[12].width;
907 ury = paper_sizes[12].height;
910 switch (orientation) {
911 case GTK_GS_ORIENTATION_PORTRAIT:
912 case GTK_GS_ORIENTATION_UPSIDEDOWN:
913 width = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
914 height = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
916 case GTK_GS_ORIENTATION_LANDSCAPE:
917 case GTK_GS_ORIENTATION_SEASCAPE:
918 width = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
919 height = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
923 g_assert_not_reached ();
927 width = width * gs->zoom_factor;
928 height = height * gs->zoom_factor;
930 if (llx != gs->llx || lly != gs->lly ||
931 urx != gs->urx || ury != gs->ury ||
932 gs->width != width || gs->height != height ||
933 orientation != gs->orientation) {
940 gs->orientation = orientation;
946 ps_document_enable_interpreter(PSDocument * gs)
948 g_return_val_if_fail(gs != NULL, FALSE);
949 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
954 return start_interpreter(gs);
957 /* publicly accessible functions */
960 ps_document_get_type(void)
962 static GType gs_type = 0;
964 GTypeInfo gs_info = {
965 sizeof(PSDocumentClass),
966 (GBaseInitFunc) NULL,
967 (GBaseFinalizeFunc) NULL,
968 (GClassInitFunc) ps_document_class_init,
969 (GClassFinalizeFunc) NULL,
970 NULL, /* class_data */
973 (GInstanceInitFunc) ps_document_init
976 static const GInterfaceInfo document_info =
978 (GInterfaceInitFunc) ps_document_document_iface_init,
983 gs_type = g_type_register_static(G_TYPE_OBJECT,
984 "PSDocument", &gs_info, 0);
986 g_type_add_interface_static (gs_type,
996 document_load(PSDocument * gs, const gchar * fname)
998 g_return_val_if_fail(gs != NULL, FALSE);
999 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1001 LOG ("Load the document");
1003 /* clean up previous document */
1004 ps_document_cleanup(gs);
1011 /* prepare this document */
1012 gs->structured_doc = FALSE;
1013 gs->send_filename_to_gs = TRUE;
1014 gs->current_page = 0;
1017 /* an absolute path */
1018 gs->gs_filename = g_strdup(fname);
1021 /* path relative to our cwd: make it absolute */
1022 gchar *cwd = g_get_current_dir();
1023 gs->gs_filename = g_strconcat(cwd, "/", fname, NULL);
1027 if((gs->reading_from_pipe = (strcmp(fname, "-") == 0))) {
1028 gs->send_filename_to_gs = FALSE;
1032 * We need to make sure that the file is loadable/exists!
1033 * otherwise we want to exit without loading new stuff...
1035 gchar *filename = NULL;
1037 if(!file_readable(fname)) {
1039 g_snprintf(buf, 1024, _("Cannot open file %s.\n"), fname);
1040 interpreter_failed (gs, buf);
1041 gs->gs_status = _("File is not readable.");
1044 filename = check_filecompressed(gs);
1047 if(!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
1048 interpreter_failed (gs, NULL);
1049 ps_document_cleanup(gs);
1053 /* we grab the vital statistics!!! */
1054 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
1056 g_object_notify (G_OBJECT (gs), "title");
1058 if(gs->doc == NULL) {
1059 /* File does not seem to be a Postscript one */
1061 g_snprintf(buf, 1024, _("Error while scanning file %s\n"), fname);
1062 interpreter_failed (gs, buf);
1063 ps_document_cleanup(gs);
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;
1075 compute_dimensions (gs, gs->current_page);
1077 gs->gs_status = _("Document loaded.");
1084 ps_document_next_page (PSDocument *gs)
1088 LOG ("Make ghostscript render next page");
1090 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
1091 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
1092 g_return_val_if_fail (gs->busy != TRUE, FALSE);
1096 event.xclient.type = ClientMessage;
1097 event.xclient.display = gdk_display;
1098 event.xclient.window = gs->message_window;
1099 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1100 event.xclient.format = 32;
1102 gdk_error_trap_push ();
1103 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1105 gdk_error_trap_pop ();
1111 render_page (PSDocument *gs)
1113 g_return_val_if_fail(gs != NULL, FALSE);
1114 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1116 if(!gs->gs_filename) {
1120 if (gs->structured_doc && gs->doc) {
1121 LOG ("It's a structured document, let's send one page to gs");
1123 if (is_interpreter_ready (gs)) {
1124 ps_document_next_page (gs);
1126 ps_document_enable_interpreter (gs);
1127 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1128 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1131 send_ps (gs, gs->doc->pages[gs->current_page].begin,
1132 gs->doc->pages[gs->current_page].len, FALSE);
1134 /* Unstructured document
1136 * In the case of non structured documents,
1137 * GS read the PS from the actual file (via command
1138 * line. Hence, ggv only send a signal next page.
1139 * If ghostview is not running it is usually because
1140 * the last page of the file was displayed. In that
1141 * case, ggv restarts GS again and the first page is displayed.
1144 LOG ("It's an unstructured document, gs will just read the file");
1146 if (!is_interpreter_ready (gs)) {
1147 ps_document_enable_interpreter(gs);
1149 ps_document_next_page(gs);
1156 ps_document_load (EvDocument *document,
1163 filename = g_filename_from_uri (uri, NULL, error);
1167 result = document_load (PS_DOCUMENT (document), filename);
1169 g_set_error (error, G_FILE_ERROR,
1170 G_FILE_ERROR_FAILED,
1171 "Failed to load document '%s'\n",
1181 ps_document_save (EvDocument *document,
1185 g_warning ("ps_document_save not implemented"); /* FIXME */
1190 ps_document_get_n_pages (EvDocument *document)
1192 PSDocument *ps = PS_DOCUMENT (document);
1194 g_return_val_if_fail (ps != NULL, -1);
1196 if (!ps->gs_filename || !ps->doc) {
1200 return ps->structured_doc ? ps->doc->numpages : 1;
1204 ps_document_set_page (EvDocument *document,
1207 PSDocument *gs = PS_DOCUMENT (document);
1209 LOG ("Set document page %d\n", page);
1211 gs->current_page = page - 1;
1212 compute_dimensions (gs, page);
1216 ps_document_get_page (EvDocument *document)
1218 PSDocument *ps = PS_DOCUMENT (document);
1220 g_return_val_if_fail (ps != NULL, -1);
1222 return ps->current_page + 1;
1226 ps_document_set_scale (EvDocument *document,
1229 PSDocument *gs = PS_DOCUMENT (document);
1231 gs->zoom_factor = scale;
1232 compute_dimensions (gs, gs->current_page);
1236 ps_document_set_page_offset (EvDocument *document,
1240 PSDocument *gs = PS_DOCUMENT (document);
1242 gs->page_x_offset = x;
1243 gs->page_y_offset = y;
1247 ps_document_get_page_size (EvDocument *document,
1252 /* Post script documents never vary in size */
1254 PSDocument *gs = PS_DOCUMENT (document);
1261 *height = gs->height;
1266 ps_document_get_text (EvDocument *document, GdkRectangle *rect)
1268 g_warning ("ps_document_get_text not implemented"); /* FIXME ? */
1273 ps_document_get_link (EvDocument *document,
1281 render_pixbuf_idle (EvDocument *document)
1283 PSDocument *gs = PS_DOCUMENT (document);
1285 if (gs->pstarget == NULL) {
1288 widget = gtk_window_new (GTK_WINDOW_POPUP);
1289 gtk_widget_realize (widget);
1290 gs->pstarget = widget->window;
1292 g_assert (gs->pstarget != NULL);
1294 g_signal_connect (widget, "event",
1295 G_CALLBACK (ps_document_widget_event),
1300 stop_interpreter (gs);
1303 gs->changed = FALSE;
1306 render_page (PS_DOCUMENT (document));
1312 ps_document_render_pixbuf (EvDocument *document)
1316 g_idle_add ((GSourceFunc)render_pixbuf_idle, document);
1318 g_mutex_lock (pixbuf_mutex);
1319 while (!current_pixbuf)
1320 g_cond_wait (pixbuf_cond, pixbuf_mutex);
1321 pixbuf = current_pixbuf;
1322 current_pixbuf = NULL;
1323 g_mutex_unlock (pixbuf_mutex);
1325 LOG ("Pixbuf rendered %p\n", pixbuf);
1331 ps_document_document_iface_init (EvDocumentIface *iface)
1333 iface->load = ps_document_load;
1334 iface->save = ps_document_save;
1335 iface->get_text = ps_document_get_text;
1336 iface->get_link = ps_document_get_link;
1337 iface->get_n_pages = ps_document_get_n_pages;
1338 iface->set_page = ps_document_set_page;
1339 iface->get_page = ps_document_get_page;
1340 iface->set_scale = ps_document_set_scale;
1341 iface->set_page_offset = ps_document_set_page_offset;
1342 iface->get_page_size = ps_document_get_page_size;
1343 iface->render_pixbuf = ps_document_render_pixbuf;