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->interpreter_pid = -1;
119 gs->gs_filename_unc = 0;
123 gs->structured_doc = FALSE;
124 gs->reading_from_pipe = FALSE;
125 gs->send_filename_to_gs = FALSE;
130 gs->interpreter_input = -1;
131 gs->interpreter_output = -1;
132 gs->interpreter_err = -1;
133 gs->interpreter_input_id = 0;
134 gs->interpreter_output_id = 0;
135 gs->interpreter_error_id = 0;
138 gs->input_buffer = NULL;
139 gs->input_buffer_ptr = NULL;
141 gs->buffer_bytes_left = 0;
143 gs->gs_status = _("No document loaded.");
145 pixbuf_cond = g_cond_new ();
146 pixbuf_mutex = g_mutex_new ();
150 ps_document_set_property (GObject *object,
165 ps_document_get_property (GObject *object,
170 PSDocument *ps = PS_DOCUMENT (object);
176 g_value_set_string (value, ps->doc->title);
178 g_value_set_string (value, NULL);
185 ps_document_class_init(PSDocumentClass *klass)
187 GObjectClass *object_class;
189 object_class = (GObjectClass *) klass;
190 parent_class = g_type_class_peek_parent (klass);
193 object_class->finalize = ps_document_finalize;
194 object_class->get_property = ps_document_get_property;
195 object_class->set_property = ps_document_set_property;
197 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
198 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
199 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
200 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
202 g_object_class_override_property (object_class, PROP_TITLE, "title");
206 push_pixbuf (PSDocument *gs)
212 cmap = gdk_window_get_colormap (gs->pstarget);
213 gdk_drawable_get_size (gs->bpixmap, &width, &height);
214 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
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;
269 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
271 PSDocument *gs = (PSDocument *) data;
273 if(event->type != GDK_CLIENT_EVENT)
276 gs->message_window = event->client.data.l[0];
278 if (event->client.message_type == gs_class->page_atom) {
279 LOG ("GS rendered the document");
289 ps_document_finalize (GObject * object)
293 g_return_if_fail (object != NULL);
294 g_return_if_fail (PS_IS_DOCUMENT (object));
298 gs = PS_DOCUMENT (object);
300 ps_document_cleanup (gs);
301 stop_interpreter (gs);
303 if(gs->input_buffer) {
304 g_free(gs->input_buffer);
305 gs->input_buffer = NULL;
308 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
312 send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close)
314 struct record_list *ps_new;
316 if(gs->interpreter_input < 0) {
317 g_critical("No pipe to gs: error in send_ps().");
321 ps_new = (struct record_list *) g_malloc(sizeof(struct record_list));
322 ps_new->fp = gs->gs_psfile;
323 ps_new->begin = begin;
325 ps_new->seek_needed = TRUE;
326 ps_new->close = close;
329 if(gs->input_buffer == NULL) {
330 gs->input_buffer = g_malloc(MAX_BUFSIZE);
333 if(gs->ps_input == NULL) {
334 gs->input_buffer_ptr = gs->input_buffer;
335 gs->bytes_left = len;
336 gs->buffer_bytes_left = 0;
337 gs->ps_input = ps_new;
338 gs->interpreter_input_id =
339 gdk_input_add(gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
342 struct record_list *p = gs->ps_input;
343 while(p->next != NULL) {
351 get_xdpi (PSDocument *gs)
353 return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
357 get_ydpi (PSDocument *gs)
359 return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
363 setup_pixmap (PSDocument *gs, int page, double scale)
366 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
367 GdkColormap *colormap;
368 double width, height;
369 int pixmap_width, pixmap_height;
371 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
372 pixmap_width = floor (width * scale);
373 pixmap_height = floor (height * scale);
375 g_print ("%f %f %f %d %d\n", width, height, scale, pixmap_width, pixmap_height);
380 gdk_drawable_get_size (gs->bpixmap, &w, &h);
382 if (pixmap_width != w || h != pixmap_height) {
383 gdk_drawable_unref (gs->bpixmap);
385 stop_interpreter (gs);
390 LOG ("Create pixmap");
392 fill = gdk_gc_new (gs->pstarget);
393 colormap = gdk_drawable_get_colormap (gs->pstarget);
394 gdk_color_alloc (colormap, &white);
395 gdk_gc_set_foreground (fill, &white);
396 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
398 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
399 0, 0, pixmap_width, pixmap_height);
403 #define DEFAULT_PAGE_SIZE 1
406 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
412 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes();
413 int new_pagesize = -1;
415 g_return_if_fail(PS_IS_DOCUMENT(gs));
417 if(new_pagesize == -1) {
418 new_pagesize = DEFAULT_PAGE_SIZE;
420 /* If we have a document:
421 We use -- the page size (if specified)
422 or the doc. size (if specified)
423 or the page bbox (if specified)
426 if((page >= 0) && (gs->doc->numpages > page) &&
427 (gs->doc->pages) && (gs->doc->pages[page].size)) {
428 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
430 else if(gs->doc->default_page_size != NULL) {
431 new_pagesize = gs->doc->default_page_size - gs->doc->size;
433 else if((page >= 0) &&
434 (gs->doc->numpages > page) &&
436 (gs->doc->pages[page].boundingbox[URX] >
437 gs->doc->pages[page].boundingbox[LLX]) &&
438 (gs->doc->pages[page].boundingbox[URY] >
439 gs->doc->pages[page].boundingbox[LLY])) {
442 else if((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
443 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
449 /* Compute bounding box */
450 if(gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
453 (gs->doc->pages[page].boundingbox[URX] >
454 gs->doc->pages[page].boundingbox[LLX])
455 && (gs->doc->pages[page].boundingbox[URY] >
456 gs->doc->pages[page].boundingbox[LLY])) {
458 new_llx = gs->doc->pages[page].boundingbox[LLX];
459 new_lly = gs->doc->pages[page].boundingbox[LLY];
460 new_urx = gs->doc->pages[page].boundingbox[URX];
461 new_ury = gs->doc->pages[page].boundingbox[URY];
463 else if((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
464 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
466 new_llx = gs->doc->boundingbox[LLX];
467 new_lly = gs->doc->boundingbox[LLY];
468 new_urx = gs->doc->boundingbox[URX];
469 new_ury = gs->doc->boundingbox[URY];
474 new_pagesize = DEFAULT_PAGE_SIZE;
475 new_llx = new_lly = 0;
476 if(gs->doc && gs->doc->size &&
477 (new_pagesize < gs->doc->numsizes)) {
478 new_urx = gs->doc->size[new_pagesize].width;
479 new_ury = gs->doc->size[new_pagesize].height;
482 new_urx = papersizes[new_pagesize].width;
483 new_ury = papersizes[new_pagesize].height;
487 if(new_urx <= new_llx)
488 new_urx = papersizes[12].width;
489 if(new_ury <= new_lly)
490 new_ury = papersizes[12].height;
499 get_page_orientation (PSDocument *gs, int page)
503 orientation = GTK_GS_ORIENTATION_NONE;
505 if (gs->structured_doc) {
506 orientation = gs->doc->pages[page].orientation;
508 if (orientation == GTK_GS_ORIENTATION_NONE) {
509 orientation = GTK_GS_ORIENTATION_PORTRAIT;
516 setup_page (PSDocument *gs, int page, double scale)
519 int urx, ury, llx, lly, orientation;
524 LOG ("Setup the page");
527 /* gs needs floating point parameters with '.' as decimal point
528 * while some (european) locales use ',' instead, so we set the
529 * locale for this snprintf to "C".
531 savelocale = setlocale (LC_NUMERIC, "C");
533 get_page_box (gs, page, &urx, &ury, &llx, &lly);
534 orientation = get_page_orientation (gs, page);
536 g_snprintf (buf, 1024, "%ld %d %d %d %d %d %f %f %d %d %d %d",
537 0L, orientation * 90, llx, lly, urx, ury,
538 get_xdpi (gs) * scale,
539 get_ydpi (gs) * scale,
541 LOG ("GS property %s", buf);
544 setlocale(LC_NUMERIC, savelocale);
546 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
547 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
552 close_pipe (int p[2])
563 is_interpreter_ready (PSDocument *gs)
565 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
569 output(gpointer data, gint source, GdkInputCondition condition)
571 char buf[MAX_BUFSIZE + 1], *msg;
573 PSDocument *gs = PS_DOCUMENT(data);
575 if(source == gs->interpreter_output) {
576 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
577 if(bytes == 0) { /* EOF occurred */
578 close(gs->interpreter_output);
579 gs->interpreter_output = -1;
580 gdk_input_remove(gs->interpreter_output_id);
583 else if(bytes == -1) {
585 interpreter_failed(gs, NULL);
588 if(gs->interpreter_err == -1) {
589 interpreter_failed(gs, NULL);
592 else if(source == gs->interpreter_err) {
593 bytes = read(gs->interpreter_err, buf, MAX_BUFSIZE);
594 if(bytes == 0) { /* EOF occurred */
595 close(gs->interpreter_err);
596 gs->interpreter_err = -1;
597 gdk_input_remove(gs->interpreter_error_id);
600 else if(bytes == -1) {
602 interpreter_failed(gs, NULL);
605 if(gs->interpreter_output == -1) {
606 interpreter_failed(gs, NULL);
612 interpreter_failed (gs, msg);
623 input(gpointer data, gint source, GdkInputCondition condition)
625 PSDocument *gs = PS_DOCUMENT(data);
627 void (*oldsig) (int);
628 oldsig = signal(SIGPIPE, catchPipe);
633 if(gs->buffer_bytes_left == 0) {
634 /* Get a new section if required */
635 if(gs->ps_input && gs->bytes_left == 0) {
636 struct record_list *ps_old = gs->ps_input;
637 gs->ps_input = ps_old->next;
638 if(ps_old->close && NULL != ps_old->fp)
640 g_free((char *) ps_old);
642 /* Have to seek at the beginning of each section */
643 if(gs->ps_input && gs->ps_input->seek_needed) {
644 fseek(gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
645 gs->ps_input->seek_needed = FALSE;
646 gs->bytes_left = gs->ps_input->len;
649 if(gs->bytes_left > MAX_BUFSIZE) {
650 gs->buffer_bytes_left =
651 fread(gs->input_buffer, sizeof(char), MAX_BUFSIZE, gs->ps_input->fp);
653 else if(gs->bytes_left > 0) {
654 gs->buffer_bytes_left =
655 fread(gs->input_buffer,
656 sizeof(char), gs->bytes_left, gs->ps_input->fp);
659 gs->buffer_bytes_left = 0;
661 if(gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
662 interpreter_failed (gs, NULL); /* Error occurred */
664 gs->input_buffer_ptr = gs->input_buffer;
665 gs->bytes_left -= gs->buffer_bytes_left;
668 if(gs->buffer_bytes_left > 0) {
669 bytes_written = write(gs->interpreter_input,
670 gs->input_buffer_ptr, gs->buffer_bytes_left);
673 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
675 interpreter_failed (gs, NULL);
677 else if(bytes_written == -1) {
678 if((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
679 interpreter_failed (gs, NULL); /* Something bad happened */
683 gs->buffer_bytes_left -= bytes_written;
684 gs->input_buffer_ptr += bytes_written;
688 while(gs->ps_input && gs->buffer_bytes_left == 0);
690 signal(SIGPIPE, oldsig);
692 if(gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
693 if(gs->interpreter_input_id != 0) {
694 gdk_input_remove(gs->interpreter_input_id);
695 gs->interpreter_input_id = 0;
701 start_interpreter (PSDocument *gs)
703 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
704 int std_out[2]; /* pipe from interp stdout */
705 int std_err[2]; /* pipe from interp stderr */
708 #define NUM_GS_ARGS (NUM_ARGS - 20)
709 #define NUM_ALPHA_ARGS 10
711 char *argv[NUM_ARGS], *dir, *gv_env;
712 char **gs_args, **alpha_args = NULL;
715 LOG ("Start the interpreter");
720 stop_interpreter(gs);
722 /* set up the args... */
723 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
724 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
725 argv[argc] = gs_args[i];
728 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
729 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
730 argv[argc] = alpha_args[i];
733 argv[argc++] = "-dNOPAUSE";
734 argv[argc++] = "-dQUIET";
735 argv[argc++] = "-dSAFER";
737 /* set up the pipes */
738 if (gs->send_filename_to_gs) {
739 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
741 argv[argc++] = "quit";
748 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
749 if (pipe (std_in) == -1) {
750 g_critical ("Unable to open pipe to Ghostscript.");
755 if (pipe (std_out) == -1) {
760 if (pipe(std_err) == -1) {
762 close_pipe (std_out);
766 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
767 gdk_x11_drawable_get_xid (gs->pstarget),
768 gdk_x11_drawable_get_xid (gs->bpixmap));
769 LOG ("Launching ghostview with env %s", gv_env);
772 gs->interpreter_pid = fork ();
773 switch (gs->interpreter_pid) {
776 close_pipe (std_out);
777 close_pipe (std_err);
782 dup2 (std_out[1], 1);
786 dup2 (std_err[1], 2);
789 if (!gs->reading_from_pipe) {
790 if (gs->send_filename_to_gs) {
792 /* just in case gs tries to read from stdin */
793 stdinfd = open("/dev/null", O_RDONLY);
807 /* change to directory where the input file is. This helps
808 * with postscript-files which include other files using
810 dir = g_path_get_dirname (gs->gs_filename);
814 execvp (argv[0], argv);
817 g_critical ("Unable to execute [%s]\n", argv[0]);
818 g_strfreev (gs_args);
820 g_strfreev (alpha_args);
823 default: /* parent */
824 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
827 /* use non-blocking IO for pipe to ghostscript */
828 result = fcntl (std_in[1], F_GETFL, 0);
829 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
830 gs->interpreter_input = std_in[1];
832 gs->interpreter_input = -1;
836 gs->interpreter_output = std_out[0];
838 gs->interpreter_err = std_err[0];
839 gs->interpreter_output_id =
840 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
841 gs->interpreter_error_id =
842 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
850 stop_interpreter(PSDocument * gs)
852 if(gs->interpreter_pid > 0) {
854 LOG ("Stop the interpreter");
855 kill(gs->interpreter_pid, SIGTERM);
856 while((wait(&status) == -1) && (errno == EINTR)) ;
857 gs->interpreter_pid = -1;
859 ps_document_cleanup(gs);
860 gs->gs_status = _("Interpreter failed.");
864 if(gs->interpreter_input >= 0) {
865 close(gs->interpreter_input);
866 gs->interpreter_input = -1;
867 if(gs->interpreter_input_id != 0) {
868 gdk_input_remove(gs->interpreter_input_id);
869 gs->interpreter_input_id = 0;
871 while(gs->ps_input) {
872 struct record_list *ps_old = gs->ps_input;
873 gs->ps_input = gs->ps_input->next;
874 if(ps_old->close && NULL != ps_old->fp)
876 g_free((char *) ps_old);
880 if(gs->interpreter_output >= 0) {
881 close(gs->interpreter_output);
882 gs->interpreter_output = -1;
883 if(gs->interpreter_output_id) {
884 gdk_input_remove(gs->interpreter_output_id);
885 gs->interpreter_output_id = 0;
889 if(gs->interpreter_err >= 0) {
890 close(gs->interpreter_err);
891 gs->interpreter_err = -1;
892 if(gs->interpreter_error_id) {
893 gdk_input_remove(gs->interpreter_error_id);
894 gs->interpreter_error_id = 0;
901 /* If file exists and is a regular file then return its length, else -1 */
903 file_length(const gchar * filename)
905 struct stat stat_rec;
907 if(filename && (stat(filename, &stat_rec) == 0)
908 && S_ISREG(stat_rec.st_mode))
909 return stat_rec.st_size;
914 /* Test if file exists, is a regular file and its length is > 0 */
916 file_readable(const char *filename)
918 return (file_length(filename) > 0);
922 * Decompress gs->gs_filename if necessary
923 * Set gs->filename_unc to the name of the uncompressed file or NULL.
924 * Error reporting via signal 'interpreter_message'
925 * Return name of input file to use or NULL on error..
928 check_filecompressed(PSDocument * gs)
932 gchar *filename, *filename_unc, *filename_err, *cmdline;
938 if((file = fopen(gs->gs_filename, "r"))
939 && (fread(buf, sizeof(gchar), 3, file) == 3)) {
940 if((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
941 /* file is gzipped or compressed */
942 cmd = gtk_gs_defaults_get_ungzip_cmd();
944 else if(strncmp(buf, "BZh", 3) == 0) {
945 /* file is compressed with bzip2 */
946 cmd = gtk_gs_defaults_get_unbzip2_cmd();
953 return gs->gs_filename;
955 /* do the decompression */
956 filename = g_shell_quote(gs->gs_filename);
957 filename_unc = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
958 if((fd = mkstemp(filename_unc)) < 0) {
959 g_free(filename_unc);
964 filename_err = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
965 if((fd = mkstemp(filename_err)) < 0) {
966 g_free(filename_err);
967 g_free(filename_unc);
972 cmdline = g_strdup_printf("%s %s >%s 2>%s", cmd,
973 filename, filename_unc, filename_err);
974 if((system(cmdline) == 0)
975 && file_readable(filename_unc)
976 && (file_length(filename_err) == 0)) {
977 /* sucessfully uncompressed file */
978 gs->gs_filename_unc = filename_unc;
982 g_snprintf(buf, 1024, _("Error while decompressing file %s:\n"),
984 interpreter_failed (gs, buf);
985 unlink(filename_unc);
986 g_free(filename_unc);
989 unlink(filename_err);
990 g_free(filename_err);
997 ps_document_enable_interpreter(PSDocument * gs)
999 g_return_val_if_fail(gs != NULL, FALSE);
1000 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1002 if(!gs->gs_filename)
1005 return start_interpreter(gs);
1008 /* publicly accessible functions */
1011 ps_document_get_type(void)
1013 static GType gs_type = 0;
1015 GTypeInfo gs_info = {
1016 sizeof(PSDocumentClass),
1017 (GBaseInitFunc) NULL,
1018 (GBaseFinalizeFunc) NULL,
1019 (GClassInitFunc) ps_document_class_init,
1020 (GClassFinalizeFunc) NULL,
1021 NULL, /* class_data */
1023 0, /* n_preallocs */
1024 (GInstanceInitFunc) ps_document_init
1027 static const GInterfaceInfo document_info =
1029 (GInterfaceInitFunc) ps_document_document_iface_init,
1034 gs_type = g_type_register_static(G_TYPE_OBJECT,
1035 "PSDocument", &gs_info, 0);
1037 g_type_add_interface_static (gs_type,
1047 document_load(PSDocument * gs, const gchar * fname)
1049 g_return_val_if_fail(gs != NULL, FALSE);
1050 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1052 LOG ("Load the document");
1054 /* clean up previous document */
1055 ps_document_cleanup(gs);
1062 /* prepare this document */
1063 gs->structured_doc = FALSE;
1064 gs->send_filename_to_gs = TRUE;
1067 /* an absolute path */
1068 gs->gs_filename = g_strdup(fname);
1071 /* path relative to our cwd: make it absolute */
1072 gchar *cwd = g_get_current_dir();
1073 gs->gs_filename = g_strconcat(cwd, "/", fname, NULL);
1077 if((gs->reading_from_pipe = (strcmp(fname, "-") == 0))) {
1078 gs->send_filename_to_gs = FALSE;
1082 * We need to make sure that the file is loadable/exists!
1083 * otherwise we want to exit without loading new stuff...
1085 gchar *filename = NULL;
1087 if(!file_readable(fname)) {
1089 g_snprintf(buf, 1024, _("Cannot open file %s.\n"), fname);
1090 interpreter_failed (gs, buf);
1091 gs->gs_status = _("File is not readable.");
1094 filename = check_filecompressed(gs);
1097 if(!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
1098 interpreter_failed (gs, NULL);
1099 ps_document_cleanup(gs);
1103 /* we grab the vital statistics!!! */
1104 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
1106 g_object_notify (G_OBJECT (gs), "title");
1108 if(gs->doc == NULL) {
1109 /* File does not seem to be a Postscript one */
1111 g_snprintf(buf, 1024, _("Error while scanning file %s\n"), fname);
1112 interpreter_failed (gs, buf);
1113 ps_document_cleanup(gs);
1114 gs->gs_status = _("The file is not a PostScript document.");
1118 if((!gs->doc->epsf && gs->doc->numpages > 0) ||
1119 (gs->doc->epsf && gs->doc->numpages > 1)) {
1120 gs->structured_doc = TRUE;
1121 gs->send_filename_to_gs = FALSE;
1126 gs->gs_status = _("Document loaded.");
1133 ps_document_next_page (PSDocument *gs)
1137 LOG ("Make ghostscript render next page");
1139 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
1140 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
1141 g_return_val_if_fail (gs->busy != TRUE, FALSE);
1145 event.xclient.type = ClientMessage;
1146 event.xclient.display = gdk_display;
1147 event.xclient.window = gs->message_window;
1148 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1149 event.xclient.format = 32;
1151 gdk_error_trap_push ();
1152 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1154 gdk_error_trap_pop ();
1160 render_page (PSDocument *gs, int page)
1162 g_return_val_if_fail(gs != NULL, FALSE);
1163 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1165 if(!gs->gs_filename) {
1169 if (gs->structured_doc && gs->doc) {
1170 LOG ("It's a structured document, let's send one page to gs");
1172 if (is_interpreter_ready (gs)) {
1173 ps_document_next_page (gs);
1175 ps_document_enable_interpreter (gs);
1176 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1177 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1180 send_ps (gs, gs->doc->pages[page].begin,
1181 gs->doc->pages[page].len, FALSE);
1183 /* Unstructured document
1185 * In the case of non structured documents,
1186 * GS read the PS from the actual file (via command
1187 * line. Hence, ggv only send a signal next page.
1188 * If ghostview is not running it is usually because
1189 * the last page of the file was displayed. In that
1190 * case, ggv restarts GS again and the first page is displayed.
1193 LOG ("It's an unstructured document, gs will just read the file");
1195 if (!is_interpreter_ready (gs)) {
1196 ps_document_enable_interpreter(gs);
1198 ps_document_next_page(gs);
1205 ps_document_load (EvDocument *document,
1212 filename = g_filename_from_uri (uri, NULL, error);
1216 result = document_load (PS_DOCUMENT (document), filename);
1218 g_set_error (error, G_FILE_ERROR,
1219 G_FILE_ERROR_FAILED,
1220 "Failed to load document '%s'\n",
1230 ps_document_save (EvDocument *document,
1234 g_warning ("ps_document_save not implemented"); /* FIXME */
1239 ps_document_get_n_pages (EvDocument *document)
1241 PSDocument *ps = PS_DOCUMENT (document);
1243 g_return_val_if_fail (ps != NULL, -1);
1245 if (!ps->gs_filename || !ps->doc) {
1249 return ps->structured_doc ? ps->doc->numpages : 1;
1253 ps_document_get_page_size (EvDocument *document,
1258 PSDocument *gs = PS_DOCUMENT (document);
1260 int urx, ury, llx, lly, orientation;
1262 get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
1263 orientation = get_page_orientation (PS_DOCUMENT (document), page);
1265 switch (orientation) {
1266 case GTK_GS_ORIENTATION_PORTRAIT:
1267 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1268 w = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
1269 h = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
1271 case GTK_GS_ORIENTATION_LANDSCAPE:
1272 case GTK_GS_ORIENTATION_SEASCAPE:
1273 w = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
1274 h = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
1278 g_assert_not_reached ();
1292 ps_document_get_text (EvDocument *document, int page, EvRectangle *rect)
1294 g_warning ("ps_document_get_text not implemented"); /* FIXME ? */
1299 render_pixbuf_idle (PSRenderJob *job)
1301 PSDocument *gs = job->document;
1303 if (gs->pstarget == NULL) {
1306 widget = gtk_window_new (GTK_WINDOW_POPUP);
1307 gtk_widget_realize (widget);
1308 gs->pstarget = widget->window;
1310 g_assert (gs->pstarget != NULL);
1312 g_signal_connect (widget, "event",
1313 G_CALLBACK (ps_document_widget_event),
1317 setup_pixmap (gs, job->page, job->scale);
1318 setup_page (gs, job->page, job->scale);
1320 render_page (gs, job->page);
1326 ps_document_render_pixbuf (EvDocument *document, int page, double scale)
1333 job.document = PS_DOCUMENT (document);
1334 g_idle_add ((GSourceFunc)render_pixbuf_idle, &job);
1336 g_mutex_lock (pixbuf_mutex);
1337 while (!current_pixbuf)
1338 g_cond_wait (pixbuf_cond, pixbuf_mutex);
1339 pixbuf = current_pixbuf;
1340 current_pixbuf = NULL;
1341 g_mutex_unlock (pixbuf_mutex);
1343 LOG ("Pixbuf rendered %p\n", pixbuf);
1349 ps_document_document_iface_init (EvDocumentIface *iface)
1351 iface->load = ps_document_load;
1352 iface->save = ps_document_save;
1353 iface->get_text = ps_document_get_text;
1354 iface->get_n_pages = ps_document_get_n_pages;
1355 iface->get_page_size = ps_document_get_page_size;
1356 iface->render_pixbuf = ps_document_render_pixbuf;