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;
71 /* structure to describe section of file to send to ghostscript */
78 struct record_list *next;
87 static gboolean broken_pipe = FALSE;
89 /* Forward declarations */
90 static void ps_document_init(PSDocument * gs);
91 static void ps_document_class_init(PSDocumentClass * klass);
92 static void send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close);
93 static void close_pipe(int p[2]);
94 static void output(gpointer data, gint source, GdkInputCondition condition);
95 static void input(gpointer data, gint source, GdkInputCondition condition);
96 static void stop_interpreter(PSDocument * gs);
97 static gint start_interpreter(PSDocument * gs);
98 static void ps_document_document_iface_init (EvDocumentIface *iface);
99 static void ps_document_ps_exporter_iface_init (EvPSExporterIface *iface);
100 static gboolean ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data);
102 static GObjectClass *parent_class = NULL;
104 static PSDocumentClass *gs_class = NULL;
107 ps_document_init (PSDocument *gs)
111 gs->interpreter_pid = -1;
115 gs->gs_filename_unc = 0;
119 gs->structured_doc = FALSE;
120 gs->reading_from_pipe = FALSE;
121 gs->send_filename_to_gs = FALSE;
126 gs->interpreter_input = -1;
127 gs->interpreter_output = -1;
128 gs->interpreter_err = -1;
129 gs->interpreter_input_id = 0;
130 gs->interpreter_output_id = 0;
131 gs->interpreter_error_id = 0;
134 gs->input_buffer = NULL;
135 gs->input_buffer_ptr = NULL;
137 gs->buffer_bytes_left = 0;
139 gs->gs_status = _("No document loaded.");
141 gs->ps_export_pagelist = NULL;
142 gs->ps_export_filename = NULL;
144 pixbuf_cond = g_cond_new ();
145 pixbuf_mutex = g_mutex_new ();
149 ps_document_dispose (GObject *object)
151 PSDocument *gs = PS_DOCUMENT (object);
153 g_return_if_fail (gs != NULL);
156 fclose (gs->gs_psfile);
157 gs->gs_psfile = NULL;
160 if (gs->gs_filename) {
161 g_free (gs->gs_filename);
162 gs->gs_filename = NULL;
170 if (gs->gs_filename_unc) {
171 unlink(gs->gs_filename_unc);
172 g_free(gs->gs_filename_unc);
173 gs->gs_filename_unc = NULL;
177 gdk_drawable_unref (gs->bpixmap);
180 if(gs->input_buffer) {
181 g_free(gs->input_buffer);
182 gs->input_buffer = NULL;
185 stop_interpreter (gs);
187 G_OBJECT_CLASS (parent_class)->dispose (object);
191 ps_document_class_init(PSDocumentClass *klass)
193 GObjectClass *object_class;
195 object_class = (GObjectClass *) klass;
196 parent_class = g_type_class_peek_parent (klass);
199 object_class->dispose = ps_document_dispose;
201 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
202 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
203 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
204 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
208 push_pixbuf (PSDocument *gs)
214 cmap = gdk_window_get_colormap (gs->pstarget);
215 gdk_drawable_get_size (gs->bpixmap, &width, &height);
216 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
219 g_mutex_lock (pixbuf_mutex);
220 current_pixbuf = pixbuf;
221 g_cond_signal (pixbuf_cond);
222 g_mutex_unlock (pixbuf_mutex);
227 interpreter_failed (PSDocument *gs, char *msg)
229 LOG ("Interpreter failed %s", msg);
233 stop_interpreter (gs);
237 interpreter_message (PSDocument *gs, char *msg)
239 /* FIXME maybe we should display a dialog */
244 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
246 PSDocument *gs = (PSDocument *) data;
248 if(event->type != GDK_CLIENT_EVENT)
251 gs->message_window = event->client.data.l[0];
253 if (event->client.message_type == gs_class->page_atom) {
254 LOG ("GS rendered the document");
264 send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close)
266 struct record_list *ps_new;
268 if(gs->interpreter_input < 0) {
269 g_critical("No pipe to gs: error in send_ps().");
273 ps_new = (struct record_list *) g_malloc(sizeof(struct record_list));
274 ps_new->fp = gs->gs_psfile;
275 ps_new->begin = begin;
277 ps_new->seek_needed = TRUE;
278 ps_new->close = close;
281 if(gs->input_buffer == NULL) {
282 gs->input_buffer = g_malloc(MAX_BUFSIZE);
285 if(gs->ps_input == NULL) {
286 gs->input_buffer_ptr = gs->input_buffer;
287 gs->bytes_left = len;
288 gs->buffer_bytes_left = 0;
289 gs->ps_input = ps_new;
290 gs->interpreter_input_id =
291 gdk_input_add(gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
294 struct record_list *p = gs->ps_input;
295 while(p->next != NULL) {
303 get_xdpi (PSDocument *gs)
305 return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
309 get_ydpi (PSDocument *gs)
311 return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
315 setup_pixmap (PSDocument *gs, int page, double scale)
318 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
319 GdkColormap *colormap;
320 double width, height;
321 int pixmap_width, pixmap_height;
323 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
324 pixmap_width = floor (width * scale);
325 pixmap_height = floor (height * scale);
330 gdk_drawable_get_size (gs->bpixmap, &w, &h);
332 if (pixmap_width != w || h != pixmap_height) {
333 gdk_drawable_unref (gs->bpixmap);
335 stop_interpreter (gs);
340 LOG ("Create pixmap");
342 fill = gdk_gc_new (gs->pstarget);
343 colormap = gdk_drawable_get_colormap (gs->pstarget);
344 gdk_color_alloc (colormap, &white);
345 gdk_gc_set_foreground (fill, &white);
346 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
348 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
349 0, 0, pixmap_width, pixmap_height);
353 #define DEFAULT_PAGE_SIZE 1
356 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
362 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes();
363 int new_pagesize = -1;
365 g_return_if_fail(PS_IS_DOCUMENT(gs));
367 if(new_pagesize == -1) {
368 new_pagesize = DEFAULT_PAGE_SIZE;
370 /* If we have a document:
371 We use -- the page size (if specified)
372 or the doc. size (if specified)
373 or the page bbox (if specified)
376 if((page >= 0) && (gs->doc->numpages > page) &&
377 (gs->doc->pages) && (gs->doc->pages[page].size)) {
378 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
380 else if(gs->doc->default_page_size != NULL) {
381 new_pagesize = gs->doc->default_page_size - gs->doc->size;
383 else if((page >= 0) &&
384 (gs->doc->numpages > page) &&
386 (gs->doc->pages[page].boundingbox[URX] >
387 gs->doc->pages[page].boundingbox[LLX]) &&
388 (gs->doc->pages[page].boundingbox[URY] >
389 gs->doc->pages[page].boundingbox[LLY])) {
392 else if((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
393 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
399 /* Compute bounding box */
400 if(gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
403 (gs->doc->pages[page].boundingbox[URX] >
404 gs->doc->pages[page].boundingbox[LLX])
405 && (gs->doc->pages[page].boundingbox[URY] >
406 gs->doc->pages[page].boundingbox[LLY])) {
408 new_llx = gs->doc->pages[page].boundingbox[LLX];
409 new_lly = gs->doc->pages[page].boundingbox[LLY];
410 new_urx = gs->doc->pages[page].boundingbox[URX];
411 new_ury = gs->doc->pages[page].boundingbox[URY];
413 else if((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
414 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
416 new_llx = gs->doc->boundingbox[LLX];
417 new_lly = gs->doc->boundingbox[LLY];
418 new_urx = gs->doc->boundingbox[URX];
419 new_ury = gs->doc->boundingbox[URY];
424 new_pagesize = DEFAULT_PAGE_SIZE;
425 new_llx = new_lly = 0;
426 if(gs->doc && gs->doc->size &&
427 (new_pagesize < gs->doc->numsizes)) {
428 new_urx = gs->doc->size[new_pagesize].width;
429 new_ury = gs->doc->size[new_pagesize].height;
432 new_urx = papersizes[new_pagesize].width;
433 new_ury = papersizes[new_pagesize].height;
437 if(new_urx <= new_llx)
438 new_urx = papersizes[12].width;
439 if(new_ury <= new_lly)
440 new_ury = papersizes[12].height;
449 get_page_orientation (PSDocument *gs, int page)
453 orientation = GTK_GS_ORIENTATION_NONE;
455 if (gs->structured_doc) {
456 orientation = gs->doc->pages[page].orientation;
458 if (orientation == GTK_GS_ORIENTATION_NONE) {
459 orientation = gs->doc->default_page_orientation;
461 if (orientation == GTK_GS_ORIENTATION_NONE) {
462 orientation = gs->doc->orientation;
464 if (orientation == GTK_GS_ORIENTATION_NONE) {
465 orientation = GTK_GS_ORIENTATION_PORTRAIT;
472 setup_page (PSDocument *gs, int page, double scale)
475 int urx, ury, llx, lly, orientation;
480 LOG ("Setup the page");
483 /* gs needs floating point parameters with '.' as decimal point
484 * while some (european) locales use ',' instead, so we set the
485 * locale for this snprintf to "C".
487 savelocale = setlocale (LC_NUMERIC, "C");
489 get_page_box (gs, page, &urx, &ury, &llx, &lly);
490 orientation = get_page_orientation (gs, page);
492 g_snprintf (buf, 1024, "%ld %d %d %d %d %d %f %f %d %d %d %d",
493 0L, orientation * 90, llx, lly, urx, ury,
494 get_xdpi (gs) * scale,
495 get_ydpi (gs) * scale,
497 LOG ("GS property %s", buf);
500 setlocale(LC_NUMERIC, savelocale);
502 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
503 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
508 close_pipe (int p[2])
519 is_interpreter_ready (PSDocument *gs)
521 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
525 output(gpointer data, gint source, GdkInputCondition condition)
527 char buf[MAX_BUFSIZE + 1], *msg;
529 PSDocument *gs = PS_DOCUMENT(data);
531 if(source == gs->interpreter_output) {
532 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
533 if(bytes == 0) { /* EOF occurred */
534 close(gs->interpreter_output);
535 gs->interpreter_output = -1;
536 gdk_input_remove(gs->interpreter_output_id);
539 else if(bytes == -1) {
541 interpreter_failed(gs, NULL);
544 if(gs->interpreter_err == -1) {
545 interpreter_failed(gs, NULL);
548 else if(source == gs->interpreter_err) {
549 bytes = read(gs->interpreter_err, buf, MAX_BUFSIZE);
550 if(bytes == 0) { /* EOF occurred */
551 close(gs->interpreter_err);
552 gs->interpreter_err = -1;
553 gdk_input_remove(gs->interpreter_error_id);
556 else if(bytes == -1) {
558 interpreter_failed(gs, NULL);
561 if(gs->interpreter_output == -1) {
562 interpreter_failed(gs, NULL);
568 interpreter_message (gs, msg);
579 input(gpointer data, gint source, GdkInputCondition condition)
581 PSDocument *gs = PS_DOCUMENT(data);
583 void (*oldsig) (int);
584 oldsig = signal(SIGPIPE, catchPipe);
589 if(gs->buffer_bytes_left == 0) {
590 /* Get a new section if required */
591 if(gs->ps_input && gs->bytes_left == 0) {
592 struct record_list *ps_old = gs->ps_input;
593 gs->ps_input = ps_old->next;
594 if(ps_old->close && NULL != ps_old->fp)
596 g_free((char *) ps_old);
598 /* Have to seek at the beginning of each section */
599 if(gs->ps_input && gs->ps_input->seek_needed) {
600 fseek(gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
601 gs->ps_input->seek_needed = FALSE;
602 gs->bytes_left = gs->ps_input->len;
605 if(gs->bytes_left > MAX_BUFSIZE) {
606 gs->buffer_bytes_left =
607 fread(gs->input_buffer, sizeof(char), MAX_BUFSIZE, gs->ps_input->fp);
609 else if(gs->bytes_left > 0) {
610 gs->buffer_bytes_left =
611 fread(gs->input_buffer,
612 sizeof(char), gs->bytes_left, gs->ps_input->fp);
615 gs->buffer_bytes_left = 0;
617 if(gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
618 interpreter_failed (gs, NULL); /* Error occurred */
620 gs->input_buffer_ptr = gs->input_buffer;
621 gs->bytes_left -= gs->buffer_bytes_left;
624 if(gs->buffer_bytes_left > 0) {
625 bytes_written = write(gs->interpreter_input,
626 gs->input_buffer_ptr, gs->buffer_bytes_left);
629 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
631 interpreter_failed (gs, NULL);
633 else if(bytes_written == -1) {
634 if((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
635 interpreter_failed (gs, NULL); /* Something bad happened */
639 gs->buffer_bytes_left -= bytes_written;
640 gs->input_buffer_ptr += bytes_written;
644 while(gs->ps_input && gs->buffer_bytes_left == 0);
646 signal(SIGPIPE, oldsig);
648 if(gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
649 if(gs->interpreter_input_id != 0) {
650 gdk_input_remove(gs->interpreter_input_id);
651 gs->interpreter_input_id = 0;
657 start_interpreter (PSDocument *gs)
659 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
660 int std_out[2]; /* pipe from interp stdout */
661 int std_err[2]; /* pipe from interp stderr */
664 #define NUM_GS_ARGS (NUM_ARGS - 20)
665 #define NUM_ALPHA_ARGS 10
667 char *argv[NUM_ARGS], *dir, *gv_env;
668 char **gs_args, **alpha_args = NULL;
671 LOG ("Start the interpreter");
676 stop_interpreter(gs);
678 /* set up the args... */
679 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
680 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
681 argv[argc] = gs_args[i];
684 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
685 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
686 argv[argc] = alpha_args[i];
689 argv[argc++] = "-dNOPAUSE";
690 argv[argc++] = "-dQUIET";
691 argv[argc++] = "-dSAFER";
693 /* set up the pipes */
694 if (gs->send_filename_to_gs) {
695 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
697 argv[argc++] = "quit";
704 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
705 if (pipe (std_in) == -1) {
706 g_critical ("Unable to open pipe to Ghostscript.");
711 if (pipe (std_out) == -1) {
716 if (pipe(std_err) == -1) {
718 close_pipe (std_out);
722 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
723 gdk_x11_drawable_get_xid (gs->pstarget),
724 gdk_x11_drawable_get_xid (gs->bpixmap));
725 LOG ("Launching ghostview with env %s", gv_env);
728 gs->interpreter_pid = fork ();
729 switch (gs->interpreter_pid) {
732 close_pipe (std_out);
733 close_pipe (std_err);
738 dup2 (std_out[1], 1);
742 dup2 (std_err[1], 2);
745 if (!gs->reading_from_pipe) {
746 if (gs->send_filename_to_gs) {
748 /* just in case gs tries to read from stdin */
749 stdinfd = open("/dev/null", O_RDONLY);
763 /* change to directory where the input file is. This helps
764 * with postscript-files which include other files using
766 dir = g_path_get_dirname (gs->gs_filename);
770 execvp (argv[0], argv);
773 g_critical ("Unable to execute [%s]\n", argv[0]);
774 g_strfreev (gs_args);
776 g_strfreev (alpha_args);
779 default: /* parent */
780 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
783 /* use non-blocking IO for pipe to ghostscript */
784 result = fcntl (std_in[1], F_GETFL, 0);
785 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
786 gs->interpreter_input = std_in[1];
788 gs->interpreter_input = -1;
792 gs->interpreter_output = std_out[0];
794 gs->interpreter_err = std_err[0];
795 gs->interpreter_output_id =
796 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
797 gs->interpreter_error_id =
798 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
806 stop_interpreter(PSDocument * gs)
808 if(gs->interpreter_pid > 0) {
810 LOG ("Stop the interpreter");
811 kill(gs->interpreter_pid, SIGTERM);
812 while((wait(&status) == -1) && (errno == EINTR)) ;
813 gs->interpreter_pid = -1;
815 gs->gs_status = _("Interpreter failed.");
819 if(gs->interpreter_input >= 0) {
820 close(gs->interpreter_input);
821 gs->interpreter_input = -1;
822 if(gs->interpreter_input_id != 0) {
823 gdk_input_remove(gs->interpreter_input_id);
824 gs->interpreter_input_id = 0;
826 while(gs->ps_input) {
827 struct record_list *ps_old = gs->ps_input;
828 gs->ps_input = gs->ps_input->next;
829 if(ps_old->close && NULL != ps_old->fp)
831 g_free((char *) ps_old);
835 if(gs->interpreter_output >= 0) {
836 close(gs->interpreter_output);
837 gs->interpreter_output = -1;
838 if(gs->interpreter_output_id) {
839 gdk_input_remove(gs->interpreter_output_id);
840 gs->interpreter_output_id = 0;
844 if(gs->interpreter_err >= 0) {
845 close(gs->interpreter_err);
846 gs->interpreter_err = -1;
847 if(gs->interpreter_error_id) {
848 gdk_input_remove(gs->interpreter_error_id);
849 gs->interpreter_error_id = 0;
856 /* If file exists and is a regular file then return its length, else -1 */
858 file_length(const gchar * filename)
860 struct stat stat_rec;
862 if(filename && (stat(filename, &stat_rec) == 0)
863 && S_ISREG(stat_rec.st_mode))
864 return stat_rec.st_size;
869 /* Test if file exists, is a regular file and its length is > 0 */
871 file_readable(const char *filename)
873 return (file_length(filename) > 0);
877 * Decompress gs->gs_filename if necessary
878 * Set gs->filename_unc to the name of the uncompressed file or NULL.
879 * Error reporting via signal 'interpreter_message'
880 * Return name of input file to use or NULL on error..
883 check_filecompressed(PSDocument * gs)
887 gchar *filename, *filename_unc, *filename_err, *cmdline;
893 if((file = fopen(gs->gs_filename, "r"))
894 && (fread(buf, sizeof(gchar), 3, file) == 3)) {
895 if((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
896 /* file is gzipped or compressed */
897 cmd = gtk_gs_defaults_get_ungzip_cmd();
899 else if(strncmp(buf, "BZh", 3) == 0) {
900 /* file is compressed with bzip2 */
901 cmd = gtk_gs_defaults_get_unbzip2_cmd();
908 return gs->gs_filename;
910 /* do the decompression */
911 filename = g_shell_quote(gs->gs_filename);
912 filename_unc = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
913 if((fd = mkstemp(filename_unc)) < 0) {
914 g_free(filename_unc);
919 filename_err = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
920 if((fd = mkstemp(filename_err)) < 0) {
921 g_free(filename_err);
922 g_free(filename_unc);
927 cmdline = g_strdup_printf("%s %s >%s 2>%s", cmd,
928 filename, filename_unc, filename_err);
929 if((system(cmdline) == 0)
930 && file_readable(filename_unc)
931 && (file_length(filename_err) == 0)) {
932 /* sucessfully uncompressed file */
933 gs->gs_filename_unc = filename_unc;
937 g_snprintf(buf, 1024, _("Error while decompressing file %s:\n"),
939 interpreter_failed (gs, buf);
940 unlink(filename_unc);
941 g_free(filename_unc);
944 unlink(filename_err);
945 g_free(filename_err);
952 ps_document_enable_interpreter(PSDocument * gs)
954 g_return_val_if_fail(gs != NULL, FALSE);
955 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
960 return start_interpreter(gs);
963 /* publicly accessible functions */
966 ps_document_get_type(void)
968 static GType gs_type = 0;
970 GTypeInfo gs_info = {
971 sizeof(PSDocumentClass),
972 (GBaseInitFunc) NULL,
973 (GBaseFinalizeFunc) NULL,
974 (GClassInitFunc) ps_document_class_init,
975 (GClassFinalizeFunc) NULL,
976 NULL, /* class_data */
979 (GInstanceInitFunc) ps_document_init
982 static const GInterfaceInfo document_info =
984 (GInterfaceInitFunc) ps_document_document_iface_init,
989 static const GInterfaceInfo ps_exporter_info =
991 (GInterfaceInitFunc) ps_document_ps_exporter_iface_init,
996 gs_type = g_type_register_static(G_TYPE_OBJECT,
997 "PSDocument", &gs_info, 0);
999 g_type_add_interface_static (gs_type,
1002 g_type_add_interface_static (gs_type,
1003 EV_TYPE_PS_EXPORTER,
1012 document_load(PSDocument * gs, const gchar * fname)
1014 g_return_val_if_fail(gs != NULL, FALSE);
1015 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1017 LOG ("Load the document");
1024 /* prepare this document */
1025 gs->structured_doc = FALSE;
1026 gs->send_filename_to_gs = TRUE;
1029 /* an absolute path */
1030 gs->gs_filename = g_strdup(fname);
1033 /* path relative to our cwd: make it absolute */
1034 gchar *cwd = g_get_current_dir();
1035 gs->gs_filename = g_strconcat(cwd, "/", fname, NULL);
1039 if((gs->reading_from_pipe = (strcmp(fname, "-") == 0))) {
1040 gs->send_filename_to_gs = FALSE;
1044 * We need to make sure that the file is loadable/exists!
1045 * otherwise we want to exit without loading new stuff...
1047 gchar *filename = NULL;
1049 if(!file_readable(fname)) {
1051 g_snprintf(buf, 1024, _("Cannot open file %s.\n"), fname);
1052 interpreter_failed (gs, buf);
1053 gs->gs_status = _("File is not readable.");
1056 filename = check_filecompressed(gs);
1059 if(!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
1060 interpreter_failed (gs, NULL);
1064 /* we grab the vital statistics!!! */
1065 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
1067 if(gs->doc == NULL) {
1068 /* File does not seem to be a Postscript one */
1070 g_snprintf(buf, 1024, _("Error while scanning file %s\n"), fname);
1071 interpreter_failed (gs, buf);
1072 gs->gs_status = _("The file is not a PostScript document.");
1076 if((!gs->doc->epsf && gs->doc->numpages > 0) ||
1077 (gs->doc->epsf && gs->doc->numpages > 1)) {
1078 gs->structured_doc = TRUE;
1079 gs->send_filename_to_gs = FALSE;
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 save_page_list (PSDocument *document, int *page_list, const char *filename)
1190 gboolean result = TRUE;
1191 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1195 pscopydoc (sink, document->gs_filename, document->doc, page_list);
1197 buf = gtk_gs_doc_sink_get_buffer (sink);
1199 f = fopen (filename, "w");
1208 gtk_gs_doc_sink_free (sink);
1215 ps_document_save (EvDocument *document,
1219 PSDocument *ps = PS_DOCUMENT (document);
1225 filename = g_filename_from_uri (uri, NULL, error);
1229 page_list = g_new0 (int, ps->doc->numpages);
1230 for (i = 0; i < ps->doc->numpages; i++) {
1234 result = save_page_list (ps, page_list, filename);
1243 ps_document_get_n_pages (EvDocument *document)
1245 PSDocument *ps = PS_DOCUMENT (document);
1247 g_return_val_if_fail (ps != NULL, -1);
1249 if (!ps->gs_filename || !ps->doc) {
1253 return ps->structured_doc ? ps->doc->numpages : 1;
1257 ps_document_get_page_size (EvDocument *document,
1262 PSDocument *gs = PS_DOCUMENT (document);
1264 int urx, ury, llx, lly, orientation;
1266 get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
1267 orientation = get_page_orientation (PS_DOCUMENT (document), page);
1269 switch (orientation) {
1270 case GTK_GS_ORIENTATION_PORTRAIT:
1271 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1272 w = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
1273 h = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
1275 case GTK_GS_ORIENTATION_LANDSCAPE:
1276 case GTK_GS_ORIENTATION_SEASCAPE:
1277 w = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
1278 h = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
1282 g_assert_not_reached ();
1296 ps_document_can_get_text (EvDocument *document)
1302 render_pixbuf_idle (PSRenderJob *job)
1304 PSDocument *gs = job->document;
1306 if (gs->pstarget == NULL) {
1309 widget = gtk_window_new (GTK_WINDOW_POPUP);
1310 gtk_widget_realize (widget);
1311 gs->pstarget = widget->window;
1313 g_assert (gs->pstarget != NULL);
1315 g_signal_connect (widget, "event",
1316 G_CALLBACK (ps_document_widget_event),
1320 setup_pixmap (gs, job->page, job->scale);
1321 setup_page (gs, job->page, job->scale);
1323 render_page (gs, job->page);
1329 ps_document_render_pixbuf (EvDocument *document, int page, double scale)
1336 job.document = PS_DOCUMENT (document);
1337 g_idle_add ((GSourceFunc)render_pixbuf_idle, &job);
1339 g_mutex_lock (pixbuf_mutex);
1340 while (!current_pixbuf)
1341 g_cond_wait (pixbuf_cond, pixbuf_mutex);
1342 pixbuf = current_pixbuf;
1343 current_pixbuf = NULL;
1344 g_mutex_unlock (pixbuf_mutex);
1346 LOG ("Pixbuf rendered %p\n", pixbuf);
1351 static EvDocumentInfo *
1352 ps_document_get_info (EvDocument *document)
1354 EvDocumentInfo *info;
1355 PSDocument *ps = PS_DOCUMENT (document);
1357 info = g_new0 (EvDocumentInfo, 1);
1358 info->fields_mask = EV_DOCUMENT_INFO_TITLE;
1359 info->title = g_strdup (ps->doc->title);
1365 ps_document_document_iface_init (EvDocumentIface *iface)
1367 iface->load = ps_document_load;
1368 iface->save = ps_document_save;
1369 iface->can_get_text = ps_document_can_get_text;
1370 iface->get_n_pages = ps_document_get_n_pages;
1371 iface->get_page_size = ps_document_get_page_size;
1372 iface->render_pixbuf = ps_document_render_pixbuf;
1373 iface->get_info = ps_document_get_info;
1377 ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
1378 int first_page, int last_page)
1380 PSDocument *document = PS_DOCUMENT (exporter);
1382 g_free (document->ps_export_pagelist);
1384 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1385 document->ps_export_filename = g_strdup (filename);
1389 ps_document_ps_export_do_page (EvPSExporter *exporter, int page)
1391 PSDocument *document = PS_DOCUMENT (exporter);
1393 document->ps_export_pagelist[page] = 1;
1397 ps_document_ps_export_end (EvPSExporter *exporter)
1399 PSDocument *document = PS_DOCUMENT (exporter);
1401 save_page_list (document, document->ps_export_pagelist,
1402 document->ps_export_filename);
1404 g_free (document->ps_export_pagelist);
1405 g_free (document->ps_export_filename);
1406 document->ps_export_pagelist = NULL;
1407 document->ps_export_filename = NULL;
1411 ps_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1413 iface->begin = ps_document_ps_export_begin;
1414 iface->do_page = ps_document_ps_export_do_page;
1415 iface->end = ps_document_ps_export_end;