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 = gs->doc->default_page_orientation;
514 if (orientation == GTK_GS_ORIENTATION_NONE) {
515 orientation = gs->doc->orientation;
517 if (orientation == GTK_GS_ORIENTATION_NONE) {
518 orientation = GTK_GS_ORIENTATION_PORTRAIT;
525 setup_page (PSDocument *gs, int page, double scale)
528 int urx, ury, llx, lly, orientation;
533 LOG ("Setup the page");
536 /* gs needs floating point parameters with '.' as decimal point
537 * while some (european) locales use ',' instead, so we set the
538 * locale for this snprintf to "C".
540 savelocale = setlocale (LC_NUMERIC, "C");
542 get_page_box (gs, page, &urx, &ury, &llx, &lly);
543 orientation = get_page_orientation (gs, page);
545 g_snprintf (buf, 1024, "%ld %d %d %d %d %d %f %f %d %d %d %d",
546 0L, orientation * 90, llx, lly, urx, ury,
547 get_xdpi (gs) * scale,
548 get_ydpi (gs) * scale,
550 LOG ("GS property %s", buf);
553 setlocale(LC_NUMERIC, savelocale);
555 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
556 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
561 close_pipe (int p[2])
572 is_interpreter_ready (PSDocument *gs)
574 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
578 output(gpointer data, gint source, GdkInputCondition condition)
580 char buf[MAX_BUFSIZE + 1], *msg;
582 PSDocument *gs = PS_DOCUMENT(data);
584 if(source == gs->interpreter_output) {
585 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
586 if(bytes == 0) { /* EOF occurred */
587 close(gs->interpreter_output);
588 gs->interpreter_output = -1;
589 gdk_input_remove(gs->interpreter_output_id);
592 else if(bytes == -1) {
594 interpreter_failed(gs, NULL);
597 if(gs->interpreter_err == -1) {
598 interpreter_failed(gs, NULL);
601 else if(source == gs->interpreter_err) {
602 bytes = read(gs->interpreter_err, buf, MAX_BUFSIZE);
603 if(bytes == 0) { /* EOF occurred */
604 close(gs->interpreter_err);
605 gs->interpreter_err = -1;
606 gdk_input_remove(gs->interpreter_error_id);
609 else if(bytes == -1) {
611 interpreter_failed(gs, NULL);
614 if(gs->interpreter_output == -1) {
615 interpreter_failed(gs, NULL);
621 interpreter_failed (gs, msg);
632 input(gpointer data, gint source, GdkInputCondition condition)
634 PSDocument *gs = PS_DOCUMENT(data);
636 void (*oldsig) (int);
637 oldsig = signal(SIGPIPE, catchPipe);
642 if(gs->buffer_bytes_left == 0) {
643 /* Get a new section if required */
644 if(gs->ps_input && gs->bytes_left == 0) {
645 struct record_list *ps_old = gs->ps_input;
646 gs->ps_input = ps_old->next;
647 if(ps_old->close && NULL != ps_old->fp)
649 g_free((char *) ps_old);
651 /* Have to seek at the beginning of each section */
652 if(gs->ps_input && gs->ps_input->seek_needed) {
653 fseek(gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
654 gs->ps_input->seek_needed = FALSE;
655 gs->bytes_left = gs->ps_input->len;
658 if(gs->bytes_left > MAX_BUFSIZE) {
659 gs->buffer_bytes_left =
660 fread(gs->input_buffer, sizeof(char), MAX_BUFSIZE, gs->ps_input->fp);
662 else if(gs->bytes_left > 0) {
663 gs->buffer_bytes_left =
664 fread(gs->input_buffer,
665 sizeof(char), gs->bytes_left, gs->ps_input->fp);
668 gs->buffer_bytes_left = 0;
670 if(gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
671 interpreter_failed (gs, NULL); /* Error occurred */
673 gs->input_buffer_ptr = gs->input_buffer;
674 gs->bytes_left -= gs->buffer_bytes_left;
677 if(gs->buffer_bytes_left > 0) {
678 bytes_written = write(gs->interpreter_input,
679 gs->input_buffer_ptr, gs->buffer_bytes_left);
682 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
684 interpreter_failed (gs, NULL);
686 else if(bytes_written == -1) {
687 if((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
688 interpreter_failed (gs, NULL); /* Something bad happened */
692 gs->buffer_bytes_left -= bytes_written;
693 gs->input_buffer_ptr += bytes_written;
697 while(gs->ps_input && gs->buffer_bytes_left == 0);
699 signal(SIGPIPE, oldsig);
701 if(gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
702 if(gs->interpreter_input_id != 0) {
703 gdk_input_remove(gs->interpreter_input_id);
704 gs->interpreter_input_id = 0;
710 start_interpreter (PSDocument *gs)
712 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
713 int std_out[2]; /* pipe from interp stdout */
714 int std_err[2]; /* pipe from interp stderr */
717 #define NUM_GS_ARGS (NUM_ARGS - 20)
718 #define NUM_ALPHA_ARGS 10
720 char *argv[NUM_ARGS], *dir, *gv_env;
721 char **gs_args, **alpha_args = NULL;
724 LOG ("Start the interpreter");
729 stop_interpreter(gs);
731 /* set up the args... */
732 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
733 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
734 argv[argc] = gs_args[i];
737 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
738 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
739 argv[argc] = alpha_args[i];
742 argv[argc++] = "-dNOPAUSE";
743 argv[argc++] = "-dQUIET";
744 argv[argc++] = "-dSAFER";
746 /* set up the pipes */
747 if (gs->send_filename_to_gs) {
748 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
750 argv[argc++] = "quit";
757 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
758 if (pipe (std_in) == -1) {
759 g_critical ("Unable to open pipe to Ghostscript.");
764 if (pipe (std_out) == -1) {
769 if (pipe(std_err) == -1) {
771 close_pipe (std_out);
775 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
776 gdk_x11_drawable_get_xid (gs->pstarget),
777 gdk_x11_drawable_get_xid (gs->bpixmap));
778 LOG ("Launching ghostview with env %s", gv_env);
781 gs->interpreter_pid = fork ();
782 switch (gs->interpreter_pid) {
785 close_pipe (std_out);
786 close_pipe (std_err);
791 dup2 (std_out[1], 1);
795 dup2 (std_err[1], 2);
798 if (!gs->reading_from_pipe) {
799 if (gs->send_filename_to_gs) {
801 /* just in case gs tries to read from stdin */
802 stdinfd = open("/dev/null", O_RDONLY);
816 /* change to directory where the input file is. This helps
817 * with postscript-files which include other files using
819 dir = g_path_get_dirname (gs->gs_filename);
823 execvp (argv[0], argv);
826 g_critical ("Unable to execute [%s]\n", argv[0]);
827 g_strfreev (gs_args);
829 g_strfreev (alpha_args);
832 default: /* parent */
833 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
836 /* use non-blocking IO for pipe to ghostscript */
837 result = fcntl (std_in[1], F_GETFL, 0);
838 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
839 gs->interpreter_input = std_in[1];
841 gs->interpreter_input = -1;
845 gs->interpreter_output = std_out[0];
847 gs->interpreter_err = std_err[0];
848 gs->interpreter_output_id =
849 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
850 gs->interpreter_error_id =
851 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
859 stop_interpreter(PSDocument * gs)
861 if(gs->interpreter_pid > 0) {
863 LOG ("Stop the interpreter");
864 kill(gs->interpreter_pid, SIGTERM);
865 while((wait(&status) == -1) && (errno == EINTR)) ;
866 gs->interpreter_pid = -1;
868 ps_document_cleanup(gs);
869 gs->gs_status = _("Interpreter failed.");
873 if(gs->interpreter_input >= 0) {
874 close(gs->interpreter_input);
875 gs->interpreter_input = -1;
876 if(gs->interpreter_input_id != 0) {
877 gdk_input_remove(gs->interpreter_input_id);
878 gs->interpreter_input_id = 0;
880 while(gs->ps_input) {
881 struct record_list *ps_old = gs->ps_input;
882 gs->ps_input = gs->ps_input->next;
883 if(ps_old->close && NULL != ps_old->fp)
885 g_free((char *) ps_old);
889 if(gs->interpreter_output >= 0) {
890 close(gs->interpreter_output);
891 gs->interpreter_output = -1;
892 if(gs->interpreter_output_id) {
893 gdk_input_remove(gs->interpreter_output_id);
894 gs->interpreter_output_id = 0;
898 if(gs->interpreter_err >= 0) {
899 close(gs->interpreter_err);
900 gs->interpreter_err = -1;
901 if(gs->interpreter_error_id) {
902 gdk_input_remove(gs->interpreter_error_id);
903 gs->interpreter_error_id = 0;
910 /* If file exists and is a regular file then return its length, else -1 */
912 file_length(const gchar * filename)
914 struct stat stat_rec;
916 if(filename && (stat(filename, &stat_rec) == 0)
917 && S_ISREG(stat_rec.st_mode))
918 return stat_rec.st_size;
923 /* Test if file exists, is a regular file and its length is > 0 */
925 file_readable(const char *filename)
927 return (file_length(filename) > 0);
931 * Decompress gs->gs_filename if necessary
932 * Set gs->filename_unc to the name of the uncompressed file or NULL.
933 * Error reporting via signal 'interpreter_message'
934 * Return name of input file to use or NULL on error..
937 check_filecompressed(PSDocument * gs)
941 gchar *filename, *filename_unc, *filename_err, *cmdline;
947 if((file = fopen(gs->gs_filename, "r"))
948 && (fread(buf, sizeof(gchar), 3, file) == 3)) {
949 if((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
950 /* file is gzipped or compressed */
951 cmd = gtk_gs_defaults_get_ungzip_cmd();
953 else if(strncmp(buf, "BZh", 3) == 0) {
954 /* file is compressed with bzip2 */
955 cmd = gtk_gs_defaults_get_unbzip2_cmd();
962 return gs->gs_filename;
964 /* do the decompression */
965 filename = g_shell_quote(gs->gs_filename);
966 filename_unc = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
967 if((fd = mkstemp(filename_unc)) < 0) {
968 g_free(filename_unc);
973 filename_err = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
974 if((fd = mkstemp(filename_err)) < 0) {
975 g_free(filename_err);
976 g_free(filename_unc);
981 cmdline = g_strdup_printf("%s %s >%s 2>%s", cmd,
982 filename, filename_unc, filename_err);
983 if((system(cmdline) == 0)
984 && file_readable(filename_unc)
985 && (file_length(filename_err) == 0)) {
986 /* sucessfully uncompressed file */
987 gs->gs_filename_unc = filename_unc;
991 g_snprintf(buf, 1024, _("Error while decompressing file %s:\n"),
993 interpreter_failed (gs, buf);
994 unlink(filename_unc);
995 g_free(filename_unc);
998 unlink(filename_err);
999 g_free(filename_err);
1002 return filename_unc;
1006 ps_document_enable_interpreter(PSDocument * gs)
1008 g_return_val_if_fail(gs != NULL, FALSE);
1009 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1011 if(!gs->gs_filename)
1014 return start_interpreter(gs);
1017 /* publicly accessible functions */
1020 ps_document_get_type(void)
1022 static GType gs_type = 0;
1024 GTypeInfo gs_info = {
1025 sizeof(PSDocumentClass),
1026 (GBaseInitFunc) NULL,
1027 (GBaseFinalizeFunc) NULL,
1028 (GClassInitFunc) ps_document_class_init,
1029 (GClassFinalizeFunc) NULL,
1030 NULL, /* class_data */
1032 0, /* n_preallocs */
1033 (GInstanceInitFunc) ps_document_init
1036 static const GInterfaceInfo document_info =
1038 (GInterfaceInitFunc) ps_document_document_iface_init,
1043 static const GInterfaceInfo ps_exporter_info =
1045 (GInterfaceInitFunc) ps_document_ps_exporter_iface_init,
1050 gs_type = g_type_register_static(G_TYPE_OBJECT,
1051 "PSDocument", &gs_info, 0);
1053 g_type_add_interface_static (gs_type,
1056 g_type_add_interface_static (gs_type,
1057 EV_TYPE_PS_EXPORTER,
1066 document_load(PSDocument * gs, const gchar * fname)
1068 g_return_val_if_fail(gs != NULL, FALSE);
1069 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1071 LOG ("Load the document");
1073 /* clean up previous document */
1074 ps_document_cleanup(gs);
1081 /* prepare this document */
1082 gs->structured_doc = FALSE;
1083 gs->send_filename_to_gs = TRUE;
1086 /* an absolute path */
1087 gs->gs_filename = g_strdup(fname);
1090 /* path relative to our cwd: make it absolute */
1091 gchar *cwd = g_get_current_dir();
1092 gs->gs_filename = g_strconcat(cwd, "/", fname, NULL);
1096 if((gs->reading_from_pipe = (strcmp(fname, "-") == 0))) {
1097 gs->send_filename_to_gs = FALSE;
1101 * We need to make sure that the file is loadable/exists!
1102 * otherwise we want to exit without loading new stuff...
1104 gchar *filename = NULL;
1106 if(!file_readable(fname)) {
1108 g_snprintf(buf, 1024, _("Cannot open file %s.\n"), fname);
1109 interpreter_failed (gs, buf);
1110 gs->gs_status = _("File is not readable.");
1113 filename = check_filecompressed(gs);
1116 if(!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
1117 interpreter_failed (gs, NULL);
1118 ps_document_cleanup(gs);
1122 /* we grab the vital statistics!!! */
1123 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
1125 g_object_notify (G_OBJECT (gs), "title");
1127 if(gs->doc == NULL) {
1128 /* File does not seem to be a Postscript one */
1130 g_snprintf(buf, 1024, _("Error while scanning file %s\n"), fname);
1131 interpreter_failed (gs, buf);
1132 ps_document_cleanup(gs);
1133 gs->gs_status = _("The file is not a PostScript document.");
1137 if((!gs->doc->epsf && gs->doc->numpages > 0) ||
1138 (gs->doc->epsf && gs->doc->numpages > 1)) {
1139 gs->structured_doc = TRUE;
1140 gs->send_filename_to_gs = FALSE;
1145 gs->gs_status = _("Document loaded.");
1152 ps_document_next_page (PSDocument *gs)
1156 LOG ("Make ghostscript render next page");
1158 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
1159 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
1160 g_return_val_if_fail (gs->busy != TRUE, FALSE);
1164 event.xclient.type = ClientMessage;
1165 event.xclient.display = gdk_display;
1166 event.xclient.window = gs->message_window;
1167 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1168 event.xclient.format = 32;
1170 gdk_error_trap_push ();
1171 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1173 gdk_error_trap_pop ();
1179 render_page (PSDocument *gs, int page)
1181 g_return_val_if_fail(gs != NULL, FALSE);
1182 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1184 if(!gs->gs_filename) {
1188 if (gs->structured_doc && gs->doc) {
1189 LOG ("It's a structured document, let's send one page to gs");
1191 if (is_interpreter_ready (gs)) {
1192 ps_document_next_page (gs);
1194 ps_document_enable_interpreter (gs);
1195 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1196 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1199 send_ps (gs, gs->doc->pages[page].begin,
1200 gs->doc->pages[page].len, FALSE);
1202 /* Unstructured document
1204 * In the case of non structured documents,
1205 * GS read the PS from the actual file (via command
1206 * line. Hence, ggv only send a signal next page.
1207 * If ghostview is not running it is usually because
1208 * the last page of the file was displayed. In that
1209 * case, ggv restarts GS again and the first page is displayed.
1212 LOG ("It's an unstructured document, gs will just read the file");
1214 if (!is_interpreter_ready (gs)) {
1215 ps_document_enable_interpreter(gs);
1217 ps_document_next_page(gs);
1224 ps_document_load (EvDocument *document,
1231 filename = g_filename_from_uri (uri, NULL, error);
1235 result = document_load (PS_DOCUMENT (document), filename);
1237 g_set_error (error, G_FILE_ERROR,
1238 G_FILE_ERROR_FAILED,
1239 "Failed to load document '%s'\n",
1249 save_page_list (PSDocument *document, int *page_list, const char *filename)
1251 gboolean result = TRUE;
1252 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1256 pscopydoc (sink, document->gs_filename, document->doc, page_list);
1258 buf = gtk_gs_doc_sink_get_buffer (sink);
1260 f = fopen (filename, "w");
1269 gtk_gs_doc_sink_free (sink);
1276 ps_document_save (EvDocument *document,
1280 PSDocument *ps = PS_DOCUMENT (document);
1286 filename = g_filename_from_uri (uri, NULL, error);
1290 page_list = g_new0 (int, ps->doc->numpages);
1291 for (i = 0; i < ps->doc->numpages; i++) {
1295 result = save_page_list (ps, page_list, filename);
1304 ps_document_get_n_pages (EvDocument *document)
1306 PSDocument *ps = PS_DOCUMENT (document);
1308 g_return_val_if_fail (ps != NULL, -1);
1310 if (!ps->gs_filename || !ps->doc) {
1314 return ps->structured_doc ? ps->doc->numpages : 1;
1318 ps_document_get_page_size (EvDocument *document,
1323 PSDocument *gs = PS_DOCUMENT (document);
1325 int urx, ury, llx, lly, orientation;
1327 get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
1328 orientation = get_page_orientation (PS_DOCUMENT (document), page);
1330 switch (orientation) {
1331 case GTK_GS_ORIENTATION_PORTRAIT:
1332 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1333 w = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
1334 h = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
1336 case GTK_GS_ORIENTATION_LANDSCAPE:
1337 case GTK_GS_ORIENTATION_SEASCAPE:
1338 w = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
1339 h = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
1343 g_assert_not_reached ();
1357 ps_document_can_get_text (EvDocument *document)
1363 render_pixbuf_idle (PSRenderJob *job)
1365 PSDocument *gs = job->document;
1367 if (gs->pstarget == NULL) {
1370 widget = gtk_window_new (GTK_WINDOW_POPUP);
1371 gtk_widget_realize (widget);
1372 gs->pstarget = widget->window;
1374 g_assert (gs->pstarget != NULL);
1376 g_signal_connect (widget, "event",
1377 G_CALLBACK (ps_document_widget_event),
1381 setup_pixmap (gs, job->page, job->scale);
1382 setup_page (gs, job->page, job->scale);
1384 render_page (gs, job->page);
1390 ps_document_render_pixbuf (EvDocument *document, int page, double scale)
1397 job.document = PS_DOCUMENT (document);
1398 g_idle_add ((GSourceFunc)render_pixbuf_idle, &job);
1400 g_mutex_lock (pixbuf_mutex);
1401 while (!current_pixbuf)
1402 g_cond_wait (pixbuf_cond, pixbuf_mutex);
1403 pixbuf = current_pixbuf;
1404 current_pixbuf = NULL;
1405 g_mutex_unlock (pixbuf_mutex);
1407 LOG ("Pixbuf rendered %p\n", pixbuf);
1413 ps_document_document_iface_init (EvDocumentIface *iface)
1415 iface->load = ps_document_load;
1416 iface->save = ps_document_save;
1417 iface->can_get_text = ps_document_can_get_text;
1418 iface->get_n_pages = ps_document_get_n_pages;
1419 iface->get_page_size = ps_document_get_page_size;
1420 iface->render_pixbuf = ps_document_render_pixbuf;
1424 ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename)
1426 PSDocument *document = PS_DOCUMENT (exporter);
1428 g_free (document->ps_export_pagelist);
1430 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1431 document->ps_export_filename = g_strdup (filename);
1435 ps_document_ps_export_do_page (EvPSExporter *exporter, int page)
1437 PSDocument *document = PS_DOCUMENT (exporter);
1439 document->ps_export_pagelist[page] = 1;
1443 ps_document_ps_export_end (EvPSExporter *exporter)
1445 PSDocument *document = PS_DOCUMENT (exporter);
1447 save_page_list (document, document->ps_export_pagelist,
1448 document->ps_export_filename);
1450 g_free (document->ps_export_pagelist);
1451 g_free (document->ps_export_filename);
1452 document->ps_export_pagelist = NULL;
1453 document->ps_export_filename = NULL;
1457 ps_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1459 iface->begin = ps_document_ps_export_begin;
1460 iface->do_page = ps_document_ps_export_do_page;
1461 iface->end = ps_document_ps_export_end;