1 /* Ghostscript widget for GTK/GNOME
3 * Copyright (C) 1998 - 2005 the Free Software Foundation
5 * Authors: Jonathan Blandford, Jaka Mocnik
7 * Based on code by: Federico Mena (Quartic), Szekeres Istvan (Pista)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
30 #include <gtk/gtkobject.h>
31 #include <gdk/gdkprivate.h>
34 #include <glib/gi18n.h>
35 #include <X11/Intrinsic.h>
41 #include <sys/types.h>
46 #include "ps-document.h"
48 #include "gsdefaults.h"
49 #include "ev-ps-exporter.h"
55 /* if POSIX O_NONBLOCK is not available, use O_NDELAY */
56 #if !defined(O_NONBLOCK) && defined(O_NDELAY)
57 # define O_NONBLOCK O_NDELAY
60 #define MAX_BUFSIZE 1024
62 #define PS_DOCUMENT_IS_COMPRESSED(gs) (PS_DOCUMENT(gs)->gs_filename_unc != NULL)
63 #define PS_DOCUMENT_GET_PS_FILE(gs) (PS_DOCUMENT_IS_COMPRESSED(gs) ? \
64 PS_DOCUMENT(gs)->gs_filename_unc : \
65 PS_DOCUMENT(gs)->gs_filename)
67 GCond* pixbuf_cond = NULL;
68 GMutex* pixbuf_mutex = NULL;
69 GdkPixbuf *current_pixbuf = NULL;
76 /* structure to describe section of file to send to ghostscript */
83 struct record_list *next;
92 static gboolean broken_pipe = FALSE;
94 /* Forward declarations */
95 static void ps_document_init(PSDocument * gs);
96 static void ps_document_class_init(PSDocumentClass * klass);
97 static void ps_document_finalize(GObject * object);
98 static void send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close);
99 static void close_pipe(int p[2]);
100 static void output(gpointer data, gint source, GdkInputCondition condition);
101 static void input(gpointer data, gint source, GdkInputCondition condition);
102 static void stop_interpreter(PSDocument * gs);
103 static gint start_interpreter(PSDocument * gs);
104 static void ps_document_document_iface_init (EvDocumentIface *iface);
105 static void ps_document_ps_exporter_iface_init (EvPSExporterIface *iface);
106 static gboolean ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data);
108 static GObjectClass *parent_class = NULL;
110 static PSDocumentClass *gs_class = NULL;
113 ps_document_init (PSDocument *gs)
117 gs->interpreter_pid = -1;
121 gs->gs_filename_unc = 0;
125 gs->structured_doc = FALSE;
126 gs->reading_from_pipe = FALSE;
127 gs->send_filename_to_gs = FALSE;
132 gs->interpreter_input = -1;
133 gs->interpreter_output = -1;
134 gs->interpreter_err = -1;
135 gs->interpreter_input_id = 0;
136 gs->interpreter_output_id = 0;
137 gs->interpreter_error_id = 0;
140 gs->input_buffer = NULL;
141 gs->input_buffer_ptr = NULL;
143 gs->buffer_bytes_left = 0;
145 gs->gs_status = _("No document loaded.");
147 gs->ps_export_pagelist = NULL;
148 gs->ps_export_filename = NULL;
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)
217 cmap = gdk_window_get_colormap (gs->pstarget);
218 gdk_drawable_get_size (gs->bpixmap, &width, &height);
219 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
222 g_mutex_lock (pixbuf_mutex);
223 current_pixbuf = pixbuf;
224 g_cond_signal (pixbuf_cond);
225 g_mutex_unlock (pixbuf_mutex);
230 interpreter_failed (PSDocument *gs, char *msg)
232 LOG ("Interpreter failed %s", msg);
236 stop_interpreter (gs);
240 ps_document_cleanup (PSDocument *gs)
242 g_return_if_fail (gs != NULL);
243 g_return_if_fail (PS_IS_DOCUMENT (gs));
247 stop_interpreter (gs);
250 fclose (gs->gs_psfile);
251 gs->gs_psfile = NULL;
254 if (gs->gs_filename) {
255 g_free (gs->gs_filename);
256 gs->gs_filename = NULL;
264 if (gs->gs_filename_unc) {
265 unlink(gs->gs_filename_unc);
266 g_free(gs->gs_filename_unc);
267 gs->gs_filename_unc = NULL;
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, int page, double scale)
371 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
372 GdkColormap *colormap;
373 double width, height;
374 int pixmap_width, pixmap_height;
376 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
377 pixmap_width = floor (width * scale);
378 pixmap_height = floor (height * scale);
383 gdk_drawable_get_size (gs->bpixmap, &w, &h);
385 if (pixmap_width != w || h != pixmap_height) {
386 gdk_drawable_unref (gs->bpixmap);
388 stop_interpreter (gs);
393 LOG ("Create pixmap");
395 fill = gdk_gc_new (gs->pstarget);
396 colormap = gdk_drawable_get_colormap (gs->pstarget);
397 gdk_color_alloc (colormap, &white);
398 gdk_gc_set_foreground (fill, &white);
399 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
401 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
402 0, 0, pixmap_width, pixmap_height);
406 #define DEFAULT_PAGE_SIZE 1
409 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
415 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes();
416 int new_pagesize = -1;
418 g_return_if_fail(PS_IS_DOCUMENT(gs));
420 if(new_pagesize == -1) {
421 new_pagesize = DEFAULT_PAGE_SIZE;
423 /* If we have a document:
424 We use -- the page size (if specified)
425 or the doc. size (if specified)
426 or the page bbox (if specified)
429 if((page >= 0) && (gs->doc->numpages > page) &&
430 (gs->doc->pages) && (gs->doc->pages[page].size)) {
431 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
433 else if(gs->doc->default_page_size != NULL) {
434 new_pagesize = gs->doc->default_page_size - gs->doc->size;
436 else if((page >= 0) &&
437 (gs->doc->numpages > page) &&
439 (gs->doc->pages[page].boundingbox[URX] >
440 gs->doc->pages[page].boundingbox[LLX]) &&
441 (gs->doc->pages[page].boundingbox[URY] >
442 gs->doc->pages[page].boundingbox[LLY])) {
445 else if((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
446 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
452 /* Compute bounding box */
453 if(gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
456 (gs->doc->pages[page].boundingbox[URX] >
457 gs->doc->pages[page].boundingbox[LLX])
458 && (gs->doc->pages[page].boundingbox[URY] >
459 gs->doc->pages[page].boundingbox[LLY])) {
461 new_llx = gs->doc->pages[page].boundingbox[LLX];
462 new_lly = gs->doc->pages[page].boundingbox[LLY];
463 new_urx = gs->doc->pages[page].boundingbox[URX];
464 new_ury = gs->doc->pages[page].boundingbox[URY];
466 else if((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
467 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
469 new_llx = gs->doc->boundingbox[LLX];
470 new_lly = gs->doc->boundingbox[LLY];
471 new_urx = gs->doc->boundingbox[URX];
472 new_ury = gs->doc->boundingbox[URY];
477 new_pagesize = DEFAULT_PAGE_SIZE;
478 new_llx = new_lly = 0;
479 if(gs->doc && gs->doc->size &&
480 (new_pagesize < gs->doc->numsizes)) {
481 new_urx = gs->doc->size[new_pagesize].width;
482 new_ury = gs->doc->size[new_pagesize].height;
485 new_urx = papersizes[new_pagesize].width;
486 new_ury = papersizes[new_pagesize].height;
490 if(new_urx <= new_llx)
491 new_urx = papersizes[12].width;
492 if(new_ury <= new_lly)
493 new_ury = papersizes[12].height;
502 get_page_orientation (PSDocument *gs, int page)
506 orientation = GTK_GS_ORIENTATION_NONE;
508 if (gs->structured_doc) {
509 orientation = gs->doc->pages[page].orientation;
511 if (orientation == GTK_GS_ORIENTATION_NONE) {
512 orientation = GTK_GS_ORIENTATION_PORTRAIT;
519 setup_page (PSDocument *gs, int page, double scale)
522 int urx, ury, llx, lly, orientation;
527 LOG ("Setup the page");
530 /* gs needs floating point parameters with '.' as decimal point
531 * while some (european) locales use ',' instead, so we set the
532 * locale for this snprintf to "C".
534 savelocale = setlocale (LC_NUMERIC, "C");
536 get_page_box (gs, page, &urx, &ury, &llx, &lly);
537 orientation = get_page_orientation (gs, page);
539 g_snprintf (buf, 1024, "%ld %d %d %d %d %d %f %f %d %d %d %d",
540 0L, orientation * 90, llx, lly, urx, ury,
541 get_xdpi (gs) * scale,
542 get_ydpi (gs) * scale,
544 LOG ("GS property %s", buf);
547 setlocale(LC_NUMERIC, savelocale);
549 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
550 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
555 close_pipe (int p[2])
566 is_interpreter_ready (PSDocument *gs)
568 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
572 output(gpointer data, gint source, GdkInputCondition condition)
574 char buf[MAX_BUFSIZE + 1], *msg;
576 PSDocument *gs = PS_DOCUMENT(data);
578 if(source == gs->interpreter_output) {
579 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
580 if(bytes == 0) { /* EOF occurred */
581 close(gs->interpreter_output);
582 gs->interpreter_output = -1;
583 gdk_input_remove(gs->interpreter_output_id);
586 else if(bytes == -1) {
588 interpreter_failed(gs, NULL);
591 if(gs->interpreter_err == -1) {
592 interpreter_failed(gs, NULL);
595 else if(source == gs->interpreter_err) {
596 bytes = read(gs->interpreter_err, buf, MAX_BUFSIZE);
597 if(bytes == 0) { /* EOF occurred */
598 close(gs->interpreter_err);
599 gs->interpreter_err = -1;
600 gdk_input_remove(gs->interpreter_error_id);
603 else if(bytes == -1) {
605 interpreter_failed(gs, NULL);
608 if(gs->interpreter_output == -1) {
609 interpreter_failed(gs, NULL);
615 interpreter_failed (gs, msg);
626 input(gpointer data, gint source, GdkInputCondition condition)
628 PSDocument *gs = PS_DOCUMENT(data);
630 void (*oldsig) (int);
631 oldsig = signal(SIGPIPE, catchPipe);
636 if(gs->buffer_bytes_left == 0) {
637 /* Get a new section if required */
638 if(gs->ps_input && gs->bytes_left == 0) {
639 struct record_list *ps_old = gs->ps_input;
640 gs->ps_input = ps_old->next;
641 if(ps_old->close && NULL != ps_old->fp)
643 g_free((char *) ps_old);
645 /* Have to seek at the beginning of each section */
646 if(gs->ps_input && gs->ps_input->seek_needed) {
647 fseek(gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
648 gs->ps_input->seek_needed = FALSE;
649 gs->bytes_left = gs->ps_input->len;
652 if(gs->bytes_left > MAX_BUFSIZE) {
653 gs->buffer_bytes_left =
654 fread(gs->input_buffer, sizeof(char), MAX_BUFSIZE, gs->ps_input->fp);
656 else if(gs->bytes_left > 0) {
657 gs->buffer_bytes_left =
658 fread(gs->input_buffer,
659 sizeof(char), gs->bytes_left, gs->ps_input->fp);
662 gs->buffer_bytes_left = 0;
664 if(gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
665 interpreter_failed (gs, NULL); /* Error occurred */
667 gs->input_buffer_ptr = gs->input_buffer;
668 gs->bytes_left -= gs->buffer_bytes_left;
671 if(gs->buffer_bytes_left > 0) {
672 bytes_written = write(gs->interpreter_input,
673 gs->input_buffer_ptr, gs->buffer_bytes_left);
676 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
678 interpreter_failed (gs, NULL);
680 else if(bytes_written == -1) {
681 if((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
682 interpreter_failed (gs, NULL); /* Something bad happened */
686 gs->buffer_bytes_left -= bytes_written;
687 gs->input_buffer_ptr += bytes_written;
691 while(gs->ps_input && gs->buffer_bytes_left == 0);
693 signal(SIGPIPE, oldsig);
695 if(gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
696 if(gs->interpreter_input_id != 0) {
697 gdk_input_remove(gs->interpreter_input_id);
698 gs->interpreter_input_id = 0;
704 start_interpreter (PSDocument *gs)
706 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
707 int std_out[2]; /* pipe from interp stdout */
708 int std_err[2]; /* pipe from interp stderr */
711 #define NUM_GS_ARGS (NUM_ARGS - 20)
712 #define NUM_ALPHA_ARGS 10
714 char *argv[NUM_ARGS], *dir, *gv_env;
715 char **gs_args, **alpha_args = NULL;
718 LOG ("Start the interpreter");
723 stop_interpreter(gs);
725 /* set up the args... */
726 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
727 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
728 argv[argc] = gs_args[i];
731 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
732 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
733 argv[argc] = alpha_args[i];
736 argv[argc++] = "-dNOPAUSE";
737 argv[argc++] = "-dQUIET";
738 argv[argc++] = "-dSAFER";
740 /* set up the pipes */
741 if (gs->send_filename_to_gs) {
742 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
744 argv[argc++] = "quit";
751 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
752 if (pipe (std_in) == -1) {
753 g_critical ("Unable to open pipe to Ghostscript.");
758 if (pipe (std_out) == -1) {
763 if (pipe(std_err) == -1) {
765 close_pipe (std_out);
769 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
770 gdk_x11_drawable_get_xid (gs->pstarget),
771 gdk_x11_drawable_get_xid (gs->bpixmap));
772 LOG ("Launching ghostview with env %s", gv_env);
775 gs->interpreter_pid = fork ();
776 switch (gs->interpreter_pid) {
779 close_pipe (std_out);
780 close_pipe (std_err);
785 dup2 (std_out[1], 1);
789 dup2 (std_err[1], 2);
792 if (!gs->reading_from_pipe) {
793 if (gs->send_filename_to_gs) {
795 /* just in case gs tries to read from stdin */
796 stdinfd = open("/dev/null", O_RDONLY);
810 /* change to directory where the input file is. This helps
811 * with postscript-files which include other files using
813 dir = g_path_get_dirname (gs->gs_filename);
817 execvp (argv[0], argv);
820 g_critical ("Unable to execute [%s]\n", argv[0]);
821 g_strfreev (gs_args);
823 g_strfreev (alpha_args);
826 default: /* parent */
827 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
830 /* use non-blocking IO for pipe to ghostscript */
831 result = fcntl (std_in[1], F_GETFL, 0);
832 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
833 gs->interpreter_input = std_in[1];
835 gs->interpreter_input = -1;
839 gs->interpreter_output = std_out[0];
841 gs->interpreter_err = std_err[0];
842 gs->interpreter_output_id =
843 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
844 gs->interpreter_error_id =
845 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
853 stop_interpreter(PSDocument * gs)
855 if(gs->interpreter_pid > 0) {
857 LOG ("Stop the interpreter");
858 kill(gs->interpreter_pid, SIGTERM);
859 while((wait(&status) == -1) && (errno == EINTR)) ;
860 gs->interpreter_pid = -1;
862 ps_document_cleanup(gs);
863 gs->gs_status = _("Interpreter failed.");
867 if(gs->interpreter_input >= 0) {
868 close(gs->interpreter_input);
869 gs->interpreter_input = -1;
870 if(gs->interpreter_input_id != 0) {
871 gdk_input_remove(gs->interpreter_input_id);
872 gs->interpreter_input_id = 0;
874 while(gs->ps_input) {
875 struct record_list *ps_old = gs->ps_input;
876 gs->ps_input = gs->ps_input->next;
877 if(ps_old->close && NULL != ps_old->fp)
879 g_free((char *) ps_old);
883 if(gs->interpreter_output >= 0) {
884 close(gs->interpreter_output);
885 gs->interpreter_output = -1;
886 if(gs->interpreter_output_id) {
887 gdk_input_remove(gs->interpreter_output_id);
888 gs->interpreter_output_id = 0;
892 if(gs->interpreter_err >= 0) {
893 close(gs->interpreter_err);
894 gs->interpreter_err = -1;
895 if(gs->interpreter_error_id) {
896 gdk_input_remove(gs->interpreter_error_id);
897 gs->interpreter_error_id = 0;
904 /* If file exists and is a regular file then return its length, else -1 */
906 file_length(const gchar * filename)
908 struct stat stat_rec;
910 if(filename && (stat(filename, &stat_rec) == 0)
911 && S_ISREG(stat_rec.st_mode))
912 return stat_rec.st_size;
917 /* Test if file exists, is a regular file and its length is > 0 */
919 file_readable(const char *filename)
921 return (file_length(filename) > 0);
925 * Decompress gs->gs_filename if necessary
926 * Set gs->filename_unc to the name of the uncompressed file or NULL.
927 * Error reporting via signal 'interpreter_message'
928 * Return name of input file to use or NULL on error..
931 check_filecompressed(PSDocument * gs)
935 gchar *filename, *filename_unc, *filename_err, *cmdline;
941 if((file = fopen(gs->gs_filename, "r"))
942 && (fread(buf, sizeof(gchar), 3, file) == 3)) {
943 if((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
944 /* file is gzipped or compressed */
945 cmd = gtk_gs_defaults_get_ungzip_cmd();
947 else if(strncmp(buf, "BZh", 3) == 0) {
948 /* file is compressed with bzip2 */
949 cmd = gtk_gs_defaults_get_unbzip2_cmd();
956 return gs->gs_filename;
958 /* do the decompression */
959 filename = g_shell_quote(gs->gs_filename);
960 filename_unc = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
961 if((fd = mkstemp(filename_unc)) < 0) {
962 g_free(filename_unc);
967 filename_err = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
968 if((fd = mkstemp(filename_err)) < 0) {
969 g_free(filename_err);
970 g_free(filename_unc);
975 cmdline = g_strdup_printf("%s %s >%s 2>%s", cmd,
976 filename, filename_unc, filename_err);
977 if((system(cmdline) == 0)
978 && file_readable(filename_unc)
979 && (file_length(filename_err) == 0)) {
980 /* sucessfully uncompressed file */
981 gs->gs_filename_unc = filename_unc;
985 g_snprintf(buf, 1024, _("Error while decompressing file %s:\n"),
987 interpreter_failed (gs, buf);
988 unlink(filename_unc);
989 g_free(filename_unc);
992 unlink(filename_err);
993 g_free(filename_err);
1000 ps_document_enable_interpreter(PSDocument * gs)
1002 g_return_val_if_fail(gs != NULL, FALSE);
1003 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1005 if(!gs->gs_filename)
1008 return start_interpreter(gs);
1011 /* publicly accessible functions */
1014 ps_document_get_type(void)
1016 static GType gs_type = 0;
1018 GTypeInfo gs_info = {
1019 sizeof(PSDocumentClass),
1020 (GBaseInitFunc) NULL,
1021 (GBaseFinalizeFunc) NULL,
1022 (GClassInitFunc) ps_document_class_init,
1023 (GClassFinalizeFunc) NULL,
1024 NULL, /* class_data */
1026 0, /* n_preallocs */
1027 (GInstanceInitFunc) ps_document_init
1030 static const GInterfaceInfo document_info =
1032 (GInterfaceInitFunc) ps_document_document_iface_init,
1037 static const GInterfaceInfo ps_exporter_info =
1039 (GInterfaceInitFunc) ps_document_ps_exporter_iface_init,
1044 gs_type = g_type_register_static(G_TYPE_OBJECT,
1045 "PSDocument", &gs_info, 0);
1047 g_type_add_interface_static (gs_type,
1050 g_type_add_interface_static (gs_type,
1051 EV_TYPE_PS_EXPORTER,
1060 document_load(PSDocument * gs, const gchar * fname)
1062 g_return_val_if_fail(gs != NULL, FALSE);
1063 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1065 LOG ("Load the document");
1067 /* clean up previous document */
1068 ps_document_cleanup(gs);
1075 /* prepare this document */
1076 gs->structured_doc = FALSE;
1077 gs->send_filename_to_gs = TRUE;
1080 /* an absolute path */
1081 gs->gs_filename = g_strdup(fname);
1084 /* path relative to our cwd: make it absolute */
1085 gchar *cwd = g_get_current_dir();
1086 gs->gs_filename = g_strconcat(cwd, "/", fname, NULL);
1090 if((gs->reading_from_pipe = (strcmp(fname, "-") == 0))) {
1091 gs->send_filename_to_gs = FALSE;
1095 * We need to make sure that the file is loadable/exists!
1096 * otherwise we want to exit without loading new stuff...
1098 gchar *filename = NULL;
1100 if(!file_readable(fname)) {
1102 g_snprintf(buf, 1024, _("Cannot open file %s.\n"), fname);
1103 interpreter_failed (gs, buf);
1104 gs->gs_status = _("File is not readable.");
1107 filename = check_filecompressed(gs);
1110 if(!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
1111 interpreter_failed (gs, NULL);
1112 ps_document_cleanup(gs);
1116 /* we grab the vital statistics!!! */
1117 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
1119 g_object_notify (G_OBJECT (gs), "title");
1121 if(gs->doc == NULL) {
1122 /* File does not seem to be a Postscript one */
1124 g_snprintf(buf, 1024, _("Error while scanning file %s\n"), fname);
1125 interpreter_failed (gs, buf);
1126 ps_document_cleanup(gs);
1127 gs->gs_status = _("The file is not a PostScript document.");
1131 if((!gs->doc->epsf && gs->doc->numpages > 0) ||
1132 (gs->doc->epsf && gs->doc->numpages > 1)) {
1133 gs->structured_doc = TRUE;
1134 gs->send_filename_to_gs = FALSE;
1139 gs->gs_status = _("Document loaded.");
1146 ps_document_next_page (PSDocument *gs)
1150 LOG ("Make ghostscript render next page");
1152 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
1153 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
1154 g_return_val_if_fail (gs->busy != TRUE, FALSE);
1158 event.xclient.type = ClientMessage;
1159 event.xclient.display = gdk_display;
1160 event.xclient.window = gs->message_window;
1161 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1162 event.xclient.format = 32;
1164 gdk_error_trap_push ();
1165 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1167 gdk_error_trap_pop ();
1173 render_page (PSDocument *gs, int page)
1175 g_return_val_if_fail(gs != NULL, FALSE);
1176 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1178 if(!gs->gs_filename) {
1182 if (gs->structured_doc && gs->doc) {
1183 LOG ("It's a structured document, let's send one page to gs");
1185 if (is_interpreter_ready (gs)) {
1186 ps_document_next_page (gs);
1188 ps_document_enable_interpreter (gs);
1189 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1190 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1193 send_ps (gs, gs->doc->pages[page].begin,
1194 gs->doc->pages[page].len, FALSE);
1196 /* Unstructured document
1198 * In the case of non structured documents,
1199 * GS read the PS from the actual file (via command
1200 * line. Hence, ggv only send a signal next page.
1201 * If ghostview is not running it is usually because
1202 * the last page of the file was displayed. In that
1203 * case, ggv restarts GS again and the first page is displayed.
1206 LOG ("It's an unstructured document, gs will just read the file");
1208 if (!is_interpreter_ready (gs)) {
1209 ps_document_enable_interpreter(gs);
1211 ps_document_next_page(gs);
1218 ps_document_load (EvDocument *document,
1225 filename = g_filename_from_uri (uri, NULL, error);
1229 result = document_load (PS_DOCUMENT (document), filename);
1231 g_set_error (error, G_FILE_ERROR,
1232 G_FILE_ERROR_FAILED,
1233 "Failed to load document '%s'\n",
1243 save_page_list (PSDocument *document, int *page_list, const char *filename)
1245 gboolean result = TRUE;
1246 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1250 pscopydoc (sink, document->gs_filename, document->doc, page_list);
1252 buf = gtk_gs_doc_sink_get_buffer (sink);
1254 f = fopen (filename, "w");
1263 gtk_gs_doc_sink_free (sink);
1270 ps_document_save (EvDocument *document,
1274 PSDocument *ps = PS_DOCUMENT (document);
1280 filename = g_filename_from_uri (uri, NULL, error);
1284 page_list = g_new0 (int, ps->doc->numpages);
1285 for (i = 0; i < ps->doc->numpages; i++) {
1289 result = save_page_list (ps, page_list, filename);
1298 ps_document_get_n_pages (EvDocument *document)
1300 PSDocument *ps = PS_DOCUMENT (document);
1302 g_return_val_if_fail (ps != NULL, -1);
1304 if (!ps->gs_filename || !ps->doc) {
1308 return ps->structured_doc ? ps->doc->numpages : 1;
1312 ps_document_get_page_size (EvDocument *document,
1317 PSDocument *gs = PS_DOCUMENT (document);
1319 int urx, ury, llx, lly, orientation;
1321 get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
1322 orientation = get_page_orientation (PS_DOCUMENT (document), page);
1324 switch (orientation) {
1325 case GTK_GS_ORIENTATION_PORTRAIT:
1326 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1327 w = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
1328 h = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
1330 case GTK_GS_ORIENTATION_LANDSCAPE:
1331 case GTK_GS_ORIENTATION_SEASCAPE:
1332 w = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
1333 h = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
1337 g_assert_not_reached ();
1351 ps_document_can_get_text (EvDocument *document)
1357 render_pixbuf_idle (PSRenderJob *job)
1359 PSDocument *gs = job->document;
1361 if (gs->pstarget == NULL) {
1364 widget = gtk_window_new (GTK_WINDOW_POPUP);
1365 gtk_widget_realize (widget);
1366 gs->pstarget = widget->window;
1368 g_assert (gs->pstarget != NULL);
1370 g_signal_connect (widget, "event",
1371 G_CALLBACK (ps_document_widget_event),
1375 setup_pixmap (gs, job->page, job->scale);
1376 setup_page (gs, job->page, job->scale);
1378 render_page (gs, job->page);
1384 ps_document_render_pixbuf (EvDocument *document, int page, double scale)
1391 job.document = PS_DOCUMENT (document);
1392 g_idle_add ((GSourceFunc)render_pixbuf_idle, &job);
1394 g_mutex_lock (pixbuf_mutex);
1395 while (!current_pixbuf)
1396 g_cond_wait (pixbuf_cond, pixbuf_mutex);
1397 pixbuf = current_pixbuf;
1398 current_pixbuf = NULL;
1399 g_mutex_unlock (pixbuf_mutex);
1401 LOG ("Pixbuf rendered %p\n", pixbuf);
1407 ps_document_document_iface_init (EvDocumentIface *iface)
1409 iface->load = ps_document_load;
1410 iface->save = ps_document_save;
1411 iface->can_get_text = ps_document_can_get_text;
1412 iface->get_n_pages = ps_document_get_n_pages;
1413 iface->get_page_size = ps_document_get_page_size;
1414 iface->render_pixbuf = ps_document_render_pixbuf;
1418 ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename)
1420 PSDocument *document = PS_DOCUMENT (exporter);
1422 g_free (document->ps_export_pagelist);
1424 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1425 document->ps_export_filename = g_strdup (filename);
1429 ps_document_ps_export_do_page (EvPSExporter *exporter, int page)
1431 PSDocument *document = PS_DOCUMENT (exporter);
1433 document->ps_export_pagelist[page] = 1;
1437 ps_document_ps_export_end (EvPSExporter *exporter)
1439 PSDocument *document = PS_DOCUMENT (exporter);
1441 save_page_list (document, document->ps_export_pagelist,
1442 document->ps_export_filename);
1444 g_free (document->ps_export_pagelist);
1445 g_free (document->ps_export_filename);
1446 document->ps_export_pagelist = NULL;
1447 document->ps_export_filename = NULL;
1451 ps_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1453 iface->begin = ps_document_ps_export_begin;
1454 iface->do_page = ps_document_ps_export_do_page;
1455 iface->end = ps_document_ps_export_end;