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);
600 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
601 argv[argc] = alpha_args[i];
604 argv[argc++] = "-dNOPAUSE";
605 argv[argc++] = "-dQUIET";
606 argv[argc++] = "-dSAFER";
608 /* set up the pipes */
609 if (gs->send_filename_to_gs) {
610 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
612 argv[argc++] = "quit";
619 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
620 if (pipe (std_in) == -1) {
621 g_critical ("Unable to open pipe to Ghostscript.");
626 if (pipe (std_out) == -1) {
631 if (pipe(std_err) == -1) {
633 close_pipe (std_out);
637 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
638 gdk_x11_drawable_get_xid (gs->pstarget),
639 gdk_x11_drawable_get_xid (gs->bpixmap));
640 LOG ("Launching ghostview with env %s", gv_env);
643 gs->interpreter_pid = fork ();
644 switch (gs->interpreter_pid) {
647 close_pipe (std_out);
648 close_pipe (std_err);
653 dup2 (std_out[1], 1);
657 dup2 (std_err[1], 2);
660 if (!gs->reading_from_pipe) {
661 if (gs->send_filename_to_gs) {
663 /* just in case gs tries to read from stdin */
664 stdinfd = open("/dev/null", O_RDONLY);
678 /* change to directory where the input file is. This helps
679 * with postscript-files which include other files using
681 dir = g_path_get_dirname (gs->gs_filename);
685 execvp (argv[0], argv);
688 g_critical ("Unable to execute [%s]\n", argv[0]);
689 g_strfreev (gs_args);
691 g_strfreev (alpha_args);
694 default: /* parent */
695 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
698 /* use non-blocking IO for pipe to ghostscript */
699 result = fcntl (std_in[1], F_GETFL, 0);
700 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
701 gs->interpreter_input = std_in[1];
703 gs->interpreter_input = -1;
707 gs->interpreter_output = std_out[0];
709 gs->interpreter_err = std_err[0];
710 gs->interpreter_output_id =
711 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
712 gs->interpreter_error_id =
713 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
721 stop_interpreter(PSDocument * gs)
723 if(gs->interpreter_pid > 0) {
725 LOG ("Stop the interpreter");
726 kill(gs->interpreter_pid, SIGTERM);
727 while((wait(&status) == -1) && (errno == EINTR)) ;
728 gs->interpreter_pid = -1;
730 ps_document_cleanup(gs);
731 gs->gs_status = _("Interpreter failed.");
735 if(gs->interpreter_input >= 0) {
736 close(gs->interpreter_input);
737 gs->interpreter_input = -1;
738 if(gs->interpreter_input_id != 0) {
739 gdk_input_remove(gs->interpreter_input_id);
740 gs->interpreter_input_id = 0;
742 while(gs->ps_input) {
743 struct record_list *ps_old = gs->ps_input;
744 gs->ps_input = gs->ps_input->next;
745 if(ps_old->close && NULL != ps_old->fp)
747 g_free((char *) ps_old);
751 if(gs->interpreter_output >= 0) {
752 close(gs->interpreter_output);
753 gs->interpreter_output = -1;
754 if(gs->interpreter_output_id) {
755 gdk_input_remove(gs->interpreter_output_id);
756 gs->interpreter_output_id = 0;
760 if(gs->interpreter_err >= 0) {
761 close(gs->interpreter_err);
762 gs->interpreter_err = -1;
763 if(gs->interpreter_error_id) {
764 gdk_input_remove(gs->interpreter_error_id);
765 gs->interpreter_error_id = 0;
772 /* If file exists and is a regular file then return its length, else -1 */
774 file_length(const gchar * filename)
776 struct stat stat_rec;
778 if(filename && (stat(filename, &stat_rec) == 0)
779 && S_ISREG(stat_rec.st_mode))
780 return stat_rec.st_size;
785 /* Test if file exists, is a regular file and its length is > 0 */
787 file_readable(const char *filename)
789 return (file_length(filename) > 0);
793 * Decompress gs->gs_filename if necessary
794 * Set gs->filename_unc to the name of the uncompressed file or NULL.
795 * Error reporting via signal 'interpreter_message'
796 * Return name of input file to use or NULL on error..
799 check_filecompressed(PSDocument * gs)
803 gchar *filename, *filename_unc, *filename_err, *cmdline;
809 if((file = fopen(gs->gs_filename, "r"))
810 && (fread(buf, sizeof(gchar), 3, file) == 3)) {
811 if((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
812 /* file is gzipped or compressed */
813 cmd = gtk_gs_defaults_get_ungzip_cmd();
815 else if(strncmp(buf, "BZh", 3) == 0) {
816 /* file is compressed with bzip2 */
817 cmd = gtk_gs_defaults_get_unbzip2_cmd();
824 return gs->gs_filename;
826 /* do the decompression */
827 filename = g_shell_quote(gs->gs_filename);
828 filename_unc = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
829 if((fd = mkstemp(filename_unc)) < 0) {
830 g_free(filename_unc);
835 filename_err = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
836 if((fd = mkstemp(filename_err)) < 0) {
837 g_free(filename_err);
838 g_free(filename_unc);
843 cmdline = g_strdup_printf("%s %s >%s 2>%s", cmd,
844 filename, filename_unc, filename_err);
845 if((system(cmdline) == 0)
846 && file_readable(filename_unc)
847 && (file_length(filename_err) == 0)) {
848 /* sucessfully uncompressed file */
849 gs->gs_filename_unc = filename_unc;
853 g_snprintf(buf, 1024, _("Error while decompressing file %s:\n"),
855 interpreter_failed (gs, buf);
856 unlink(filename_unc);
857 g_free(filename_unc);
860 unlink(filename_err);
861 g_free(filename_err);
868 compute_dimensions (PSDocument *gs, int page)
870 GtkGSPaperSize *paper_sizes = gtk_gs_defaults_get_paper_sizes ();
871 int urx, ury, llx, lly;
875 g_return_if_fail (PS_IS_DOCUMENT (gs));
876 g_return_if_fail (gs->doc != NULL);
877 g_return_if_fail (page >= 0);
878 g_return_if_fail (gs->doc->numpages > page);
880 orientation = GTK_GS_ORIENTATION_NONE;
881 if (gs->structured_doc) {
882 orientation = gs->doc->pages[gs->current_page].orientation;
884 if (orientation == GTK_GS_ORIENTATION_NONE) {
885 orientation = GTK_GS_ORIENTATION_PORTRAIT;
888 if (gs->doc->pages && gs->doc->pages[page].size) {
891 page_size = gs->doc->pages[page].size - gs->doc->size;
893 urx = gs->doc->size[page_size].width;
894 ury = gs->doc->size[page_size].height;
895 } else if (gs->doc->pages &&
896 (gs->doc->pages[page].boundingbox[URX] >
897 gs->doc->pages[page].boundingbox[LLX]) &&
898 (gs->doc->pages[page].boundingbox[URY] >
899 gs->doc->pages[page].boundingbox[LLY])) {
900 llx = gs->doc->pages[page].boundingbox[LLX];
901 lly = gs->doc->pages[page].boundingbox[LLY];
902 urx = gs->doc->pages[page].boundingbox[URX];
903 ury = gs->doc->pages[page].boundingbox[URY];
904 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
905 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
906 llx = gs->doc->boundingbox[LLX];
907 lly = gs->doc->boundingbox[LLY];
908 urx = gs->doc->boundingbox[URX];
909 ury = gs->doc->boundingbox[URY];
913 urx = paper_sizes[12].width;
914 ury = paper_sizes[12].height;
917 switch (orientation) {
918 case GTK_GS_ORIENTATION_PORTRAIT:
919 case GTK_GS_ORIENTATION_UPSIDEDOWN:
920 width = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
921 height = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
923 case GTK_GS_ORIENTATION_LANDSCAPE:
924 case GTK_GS_ORIENTATION_SEASCAPE:
925 width = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
926 height = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
930 g_assert_not_reached ();
934 width = width * gs->zoom_factor;
935 height = height * gs->zoom_factor;
937 if (llx != gs->llx || lly != gs->lly ||
938 urx != gs->urx || ury != gs->ury ||
939 gs->width != width || gs->height != height ||
940 orientation != gs->orientation) {
947 gs->orientation = orientation;
953 ps_document_enable_interpreter(PSDocument * gs)
955 g_return_val_if_fail(gs != NULL, FALSE);
956 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
961 return start_interpreter(gs);
964 /* publicly accessible functions */
967 ps_document_get_type(void)
969 static GType gs_type = 0;
971 GTypeInfo gs_info = {
972 sizeof(PSDocumentClass),
973 (GBaseInitFunc) NULL,
974 (GBaseFinalizeFunc) NULL,
975 (GClassInitFunc) ps_document_class_init,
976 (GClassFinalizeFunc) NULL,
977 NULL, /* class_data */
980 (GInstanceInitFunc) ps_document_init
983 static const GInterfaceInfo document_info =
985 (GInterfaceInitFunc) ps_document_document_iface_init,
990 gs_type = g_type_register_static(G_TYPE_OBJECT,
991 "PSDocument", &gs_info, 0);
993 g_type_add_interface_static (gs_type,
1003 document_load(PSDocument * gs, const gchar * fname)
1005 g_return_val_if_fail(gs != NULL, FALSE);
1006 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1008 LOG ("Load the document");
1010 /* clean up previous document */
1011 ps_document_cleanup(gs);
1018 /* prepare this document */
1019 gs->structured_doc = FALSE;
1020 gs->send_filename_to_gs = TRUE;
1021 gs->current_page = 0;
1024 /* an absolute path */
1025 gs->gs_filename = g_strdup(fname);
1028 /* path relative to our cwd: make it absolute */
1029 gchar *cwd = g_get_current_dir();
1030 gs->gs_filename = g_strconcat(cwd, "/", fname, NULL);
1034 if((gs->reading_from_pipe = (strcmp(fname, "-") == 0))) {
1035 gs->send_filename_to_gs = FALSE;
1039 * We need to make sure that the file is loadable/exists!
1040 * otherwise we want to exit without loading new stuff...
1042 gchar *filename = NULL;
1044 if(!file_readable(fname)) {
1046 g_snprintf(buf, 1024, _("Cannot open file %s.\n"), fname);
1047 interpreter_failed (gs, buf);
1048 gs->gs_status = _("File is not readable.");
1051 filename = check_filecompressed(gs);
1054 if(!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
1055 interpreter_failed (gs, NULL);
1056 ps_document_cleanup(gs);
1060 /* we grab the vital statistics!!! */
1061 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
1063 g_object_notify (G_OBJECT (gs), "title");
1065 if(gs->doc == NULL) {
1066 /* File does not seem to be a Postscript one */
1068 g_snprintf(buf, 1024, _("Error while scanning file %s\n"), fname);
1069 interpreter_failed (gs, buf);
1070 ps_document_cleanup(gs);
1071 gs->gs_status = _("The file is not a PostScript document.");
1075 if((!gs->doc->epsf && gs->doc->numpages > 0) ||
1076 (gs->doc->epsf && gs->doc->numpages > 1)) {
1077 gs->structured_doc = TRUE;
1078 gs->send_filename_to_gs = FALSE;
1082 compute_dimensions (gs, gs->current_page);
1084 gs->gs_status = _("Document loaded.");
1091 ps_document_next_page (PSDocument *gs)
1095 LOG ("Make ghostscript render next page");
1097 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
1098 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
1099 g_return_val_if_fail (gs->busy != TRUE, FALSE);
1103 event.xclient.type = ClientMessage;
1104 event.xclient.display = gdk_display;
1105 event.xclient.window = gs->message_window;
1106 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1107 event.xclient.format = 32;
1109 gdk_error_trap_push ();
1110 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1112 gdk_error_trap_pop ();
1118 render_page (PSDocument *gs, int page)
1120 g_return_val_if_fail(gs != NULL, FALSE);
1121 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1123 if(!gs->gs_filename) {
1127 if (gs->structured_doc && gs->doc) {
1128 LOG ("It's a structured document, let's send one page to gs");
1130 if (is_interpreter_ready (gs)) {
1131 ps_document_next_page (gs);
1133 ps_document_enable_interpreter (gs);
1134 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1135 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1138 send_ps (gs, gs->doc->pages[page].begin,
1139 gs->doc->pages[page].len, FALSE);
1141 /* Unstructured document
1143 * In the case of non structured documents,
1144 * GS read the PS from the actual file (via command
1145 * line. Hence, ggv only send a signal next page.
1146 * If ghostview is not running it is usually because
1147 * the last page of the file was displayed. In that
1148 * case, ggv restarts GS again and the first page is displayed.
1151 LOG ("It's an unstructured document, gs will just read the file");
1153 if (!is_interpreter_ready (gs)) {
1154 ps_document_enable_interpreter(gs);
1156 ps_document_next_page(gs);
1163 ps_document_load (EvDocument *document,
1170 filename = g_filename_from_uri (uri, NULL, error);
1174 result = document_load (PS_DOCUMENT (document), filename);
1176 g_set_error (error, G_FILE_ERROR,
1177 G_FILE_ERROR_FAILED,
1178 "Failed to load document '%s'\n",
1188 ps_document_save (EvDocument *document,
1192 g_warning ("ps_document_save not implemented"); /* FIXME */
1197 ps_document_get_n_pages (EvDocument *document)
1199 PSDocument *ps = PS_DOCUMENT (document);
1201 g_return_val_if_fail (ps != NULL, -1);
1203 if (!ps->gs_filename || !ps->doc) {
1207 return ps->structured_doc ? ps->doc->numpages : 1;
1211 ps_document_get_page_size (EvDocument *document,
1216 /* Post script documents never vary in size */
1218 PSDocument *gs = PS_DOCUMENT (document);
1220 compute_dimensions (gs, page);
1227 *height = gs->height;
1232 ps_document_get_text (EvDocument *document, int page, EvRectangle *rect)
1234 g_warning ("ps_document_get_text not implemented"); /* FIXME ? */
1239 render_pixbuf_idle (PSRenderJob *job)
1241 PSDocument *gs = job->document;
1243 if (gs->pstarget == NULL) {
1246 widget = gtk_window_new (GTK_WINDOW_POPUP);
1247 gtk_widget_realize (widget);
1248 gs->pstarget = widget->window;
1250 g_assert (gs->pstarget != NULL);
1252 g_signal_connect (widget, "event",
1253 G_CALLBACK (ps_document_widget_event),
1258 stop_interpreter (gs);
1260 setup_page (gs, job->scale);
1261 gs->changed = FALSE;
1264 render_page (gs, job->page);
1270 ps_document_render_pixbuf (EvDocument *document, int page, double scale)
1277 job.document = PS_DOCUMENT (document);
1278 g_idle_add ((GSourceFunc)render_pixbuf_idle, &job);
1280 g_mutex_lock (pixbuf_mutex);
1281 while (!current_pixbuf)
1282 g_cond_wait (pixbuf_cond, pixbuf_mutex);
1283 pixbuf = current_pixbuf;
1284 current_pixbuf = NULL;
1285 g_mutex_unlock (pixbuf_mutex);
1287 LOG ("Pixbuf rendered %p\n", pixbuf);
1293 ps_document_document_iface_init (EvDocumentIface *iface)
1295 iface->load = ps_document_load;
1296 iface->save = ps_document_save;
1297 iface->get_text = ps_document_get_text;
1298 iface->get_n_pages = ps_document_get_n_pages;
1299 iface->get_page_size = ps_document_get_page_size;
1300 iface->render_pixbuf = ps_document_render_pixbuf;