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-file-exporter.h"
50 #include "ev-async-renderer.h"
52 #define MAX_BUFSIZE 1024
54 #define PS_DOCUMENT_IS_COMPRESSED(gs) (PS_DOCUMENT(gs)->gs_filename_unc != NULL)
55 #define PS_DOCUMENT_GET_PS_FILE(gs) (PS_DOCUMENT_IS_COMPRESSED(gs) ? \
56 PS_DOCUMENT(gs)->gs_filename_unc : \
57 PS_DOCUMENT(gs)->gs_filename)
59 /* structure to describe section of file to send to ghostscript */
67 struct record_list *next;
70 static gboolean broken_pipe = FALSE;
72 /* Forward declarations */
73 static void ps_document_init (PSDocument *gs);
74 static void ps_document_class_init (PSDocumentClass *klass);
75 static void send_ps (PSDocument *gs,
79 static void output (gpointer data,
81 GdkInputCondition condition);
82 static void input (gpointer data,
84 GdkInputCondition condition);
85 static void stop_interpreter (PSDocument *gs);
86 static gint start_interpreter (PSDocument *gs);
87 static void ps_document_document_iface_init (EvDocumentIface *iface);
88 static void ps_document_file_exporter_iface_init (EvFileExporterIface *iface);
89 static void ps_async_renderer_iface_init (EvAsyncRendererIface *iface);
91 G_DEFINE_TYPE_WITH_CODE (PSDocument, ps_document, G_TYPE_OBJECT,
93 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
94 ps_document_document_iface_init);
95 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
96 ps_document_file_exporter_iface_init);
97 G_IMPLEMENT_INTERFACE (EV_TYPE_ASYNC_RENDERER,
98 ps_async_renderer_iface_init);
101 static GObjectClass *parent_class = NULL;
102 static PSDocumentClass *gs_class = NULL;
105 ps_document_init (PSDocument *gs)
109 gs->interpreter_pid = -1;
113 gs->gs_filename_unc = 0;
117 gs->structured_doc = FALSE;
118 gs->reading_from_pipe = FALSE;
119 gs->send_filename_to_gs = FALSE;
123 gs->interpreter_input = -1;
124 gs->interpreter_output = -1;
125 gs->interpreter_err = -1;
126 gs->interpreter_input_id = 0;
127 gs->interpreter_output_id = 0;
128 gs->interpreter_error_id = 0;
131 gs->input_buffer = NULL;
132 gs->input_buffer_ptr = NULL;
134 gs->buffer_bytes_left = 0;
136 gs->gs_status = _("No document loaded.");
138 gs->ps_export_pagelist = NULL;
139 gs->ps_export_filename = NULL;
143 ps_document_dispose (GObject *object)
145 PSDocument *gs = PS_DOCUMENT (object);
147 g_return_if_fail (gs != NULL);
150 fclose (gs->gs_psfile);
151 gs->gs_psfile = NULL;
154 if (gs->gs_filename) {
155 g_free (gs->gs_filename);
156 gs->gs_filename = NULL;
164 if (gs->gs_filename_unc) {
165 unlink(gs->gs_filename_unc);
166 g_free(gs->gs_filename_unc);
167 gs->gs_filename_unc = NULL;
171 gdk_drawable_unref (gs->bpixmap);
174 if(gs->input_buffer) {
175 g_free(gs->input_buffer);
176 gs->input_buffer = NULL;
179 if (gs->target_window) {
180 gtk_widget_destroy (gs->target_window);
181 gs->target_window = 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 if (gs->pstarget == NULL)
217 cmap = gdk_window_get_colormap (gs->pstarget);
218 gdk_drawable_get_size (gs->bpixmap, &width, &height);
219 LOG ("Get from drawable\n");
220 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
223 LOG ("Get from drawable done\n");
224 g_signal_emit_by_name (gs, "render_finished", pixbuf);
225 g_object_unref (pixbuf);
229 interpreter_failed (PSDocument *gs, char *msg)
231 LOG ("Interpreter failed %s", msg);
235 stop_interpreter (gs);
239 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
241 PSDocument *gs = (PSDocument *) data;
243 if(event->type != GDK_CLIENT_EVENT)
246 gs->message_window = event->client.data.l[0];
248 if (event->client.message_type == gs_class->page_atom) {
249 LOG ("GS rendered the document");
253 LOG ("Pixbuf pushed");
260 send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
262 struct record_list *ps_new;
264 if (gs->interpreter_input < 0) {
265 g_critical("No pipe to gs: error in send_ps().");
269 ps_new = g_new0 (struct record_list, 1);
270 ps_new->fp = gs->gs_psfile;
271 ps_new->begin = begin;
273 ps_new->seek_needed = TRUE;
274 ps_new->close = close;
277 if (gs->input_buffer == NULL) {
278 gs->input_buffer = g_malloc(MAX_BUFSIZE);
281 if (gs->ps_input == NULL) {
282 gs->input_buffer_ptr = gs->input_buffer;
283 gs->bytes_left = len;
284 gs->buffer_bytes_left = 0;
285 gs->ps_input = ps_new;
286 gs->interpreter_input_id = gdk_input_add
287 (gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
289 struct record_list *p = gs->ps_input;
290 while (p->next != NULL) {
298 setup_pixmap (PSDocument *gs, int page, double scale, int rotation)
301 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
302 GdkColormap *colormap;
303 double width, height;
304 int pixmap_width, pixmap_height;
306 if (gs->pstarget == NULL)
309 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
311 if (rotation == 90 || rotation == 270) {
312 pixmap_height = width * scale + 0.5;
313 pixmap_width = height * scale + 0.5;
315 pixmap_width = width * scale + 0.5;
316 pixmap_height = height * scale + 0.5;
322 gdk_drawable_get_size (gs->bpixmap, &w, &h);
324 if (pixmap_width != w || h != pixmap_height) {
325 gdk_drawable_unref (gs->bpixmap);
327 stop_interpreter (gs);
332 LOG ("Create pixmap");
334 fill = gdk_gc_new (gs->pstarget);
335 colormap = gdk_drawable_get_colormap (gs->pstarget);
336 gdk_color_alloc (colormap, &white);
337 gdk_gc_set_foreground (fill, &white);
338 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
340 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
341 0, 0, pixmap_width, pixmap_height);
345 #define DEFAULT_PAGE_SIZE 1
348 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
354 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
355 int new_pagesize = -1;
357 g_return_if_fail (PS_IS_DOCUMENT (gs));
359 if (new_pagesize == -1) {
360 new_pagesize = DEFAULT_PAGE_SIZE;
362 /* If we have a document:
363 * We use -- the page size (if specified)
364 * or the doc. size (if specified)
365 * or the page bbox (if specified)
366 * or the bounding box
368 if ((page >= 0) && (gs->doc->numpages > page) &&
369 (gs->doc->pages) && (gs->doc->pages[page].size)) {
370 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
371 } else if (gs->doc->default_page_size != NULL) {
372 new_pagesize = gs->doc->default_page_size - gs->doc->size;
373 } else if ((page >= 0) &&
374 (gs->doc->numpages > page) &&
376 (gs->doc->pages[page].boundingbox[URX] >
377 gs->doc->pages[page].boundingbox[LLX]) &&
378 (gs->doc->pages[page].boundingbox[URY] >
379 gs->doc->pages[page].boundingbox[LLY])) {
381 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
382 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
388 /* Compute bounding box */
389 if (gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
392 (gs->doc->pages[page].boundingbox[URX] >
393 gs->doc->pages[page].boundingbox[LLX]) &&
394 (gs->doc->pages[page].boundingbox[URY] >
395 gs->doc->pages[page].boundingbox[LLY])) {
397 new_llx = gs->doc->pages[page].boundingbox[LLX];
398 new_lly = gs->doc->pages[page].boundingbox[LLY];
399 new_urx = gs->doc->pages[page].boundingbox[URX];
400 new_ury = gs->doc->pages[page].boundingbox[URY];
401 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
402 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
404 new_llx = gs->doc->boundingbox[LLX];
405 new_lly = gs->doc->boundingbox[LLY];
406 new_urx = gs->doc->boundingbox[URX];
407 new_ury = gs->doc->boundingbox[URY];
410 if (new_pagesize < 0)
411 new_pagesize = DEFAULT_PAGE_SIZE;
412 new_llx = new_lly = 0;
413 if (gs->doc && gs->doc->size &&
414 (new_pagesize < gs->doc->numsizes)) {
415 new_urx = gs->doc->size[new_pagesize].width;
416 new_ury = gs->doc->size[new_pagesize].height;
418 new_urx = papersizes[new_pagesize].width;
419 new_ury = papersizes[new_pagesize].height;
423 if (new_urx <= new_llx)
424 new_urx = papersizes[12].width;
425 if (new_ury <= new_lly)
426 new_ury = papersizes[12].height;
435 setup_page (PSDocument *gs, int page, double scale, int rotation)
438 char scaled_dpi[G_ASCII_DTOSTR_BUF_SIZE];
439 int urx, ury, llx, lly;
441 LOG ("Setup the page");
443 get_page_box (gs, page, &urx, &ury, &llx, &lly);
444 g_ascii_dtostr (scaled_dpi, G_ASCII_DTOSTR_BUF_SIZE, 72.0 * scale);
446 buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
447 0L, rotation, llx, lly, urx, ury,
448 scaled_dpi, scaled_dpi,
450 LOG ("GS property %s", buf);
452 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
453 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
460 close_pipe (int p[2])
471 is_interpreter_ready (PSDocument *gs)
473 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
477 output (gpointer data, gint source, GdkInputCondition condition)
479 char buf[MAX_BUFSIZE + 1];
481 PSDocument *gs = PS_DOCUMENT(data);
483 if (source == gs->interpreter_output) {
484 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
485 if (bytes == 0) { /* EOF occurred */
486 close (gs->interpreter_output);
487 gs->interpreter_output = -1;
488 gdk_input_remove (gs->interpreter_output_id);
490 } else if (bytes == -1) {
492 interpreter_failed (gs, NULL);
495 if (gs->interpreter_err == -1) {
496 interpreter_failed (gs, NULL);
498 } else if (source == gs->interpreter_err) {
499 bytes = read (gs->interpreter_err, buf, MAX_BUFSIZE);
500 if (bytes == 0) { /* EOF occurred */
501 close (gs->interpreter_err);
502 gs->interpreter_err = -1;
503 gdk_input_remove (gs->interpreter_error_id);
505 } else if (bytes == -1) {
507 interpreter_failed (gs, NULL);
510 if (gs->interpreter_output == -1) {
511 interpreter_failed(gs, NULL);
528 input(gpointer data, gint source, GdkInputCondition condition)
530 PSDocument *gs = PS_DOCUMENT(data);
532 void (*oldsig) (int);
533 oldsig = signal(SIGPIPE, catchPipe);
538 if (gs->buffer_bytes_left == 0) {
539 /* Get a new section if required */
540 if (gs->ps_input && gs->bytes_left == 0) {
541 struct record_list *ps_old = gs->ps_input;
542 gs->ps_input = ps_old->next;
543 if (ps_old->close && NULL != ps_old->fp)
548 /* Have to seek at the beginning of each section */
549 if (gs->ps_input && gs->ps_input->seek_needed) {
550 fseek (gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
551 gs->ps_input->seek_needed = FALSE;
552 gs->bytes_left = gs->ps_input->len;
555 if (gs->bytes_left > MAX_BUFSIZE) {
556 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
557 MAX_BUFSIZE, gs->ps_input->fp);
558 } else if (gs->bytes_left > 0) {
559 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
560 gs->bytes_left, gs->ps_input->fp);
562 gs->buffer_bytes_left = 0;
564 if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
565 interpreter_failed (gs, NULL); /* Error occurred */
567 gs->input_buffer_ptr = gs->input_buffer;
568 gs->bytes_left -= gs->buffer_bytes_left;
571 if (gs->buffer_bytes_left > 0) {
572 bytes_written = write (gs->interpreter_input,
573 gs->input_buffer_ptr, gs->buffer_bytes_left);
576 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
578 interpreter_failed (gs, NULL);
579 } else if (bytes_written == -1) {
580 if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
581 interpreter_failed (gs, NULL); /* Something bad happened */
584 gs->buffer_bytes_left -= bytes_written;
585 gs->input_buffer_ptr += bytes_written;
588 } while (gs->ps_input && gs->buffer_bytes_left == 0);
590 signal (SIGPIPE, oldsig);
592 if (gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
593 if (gs->interpreter_input_id != 0) {
594 gdk_input_remove (gs->interpreter_input_id);
595 gs->interpreter_input_id = 0;
601 start_interpreter (PSDocument *gs)
603 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
604 int std_out[2]; /* pipe from interp stdout */
605 int std_err[2]; /* pipe from interp stderr */
608 #define NUM_GS_ARGS (NUM_ARGS - 20)
609 #define NUM_ALPHA_ARGS 10
611 char *argv[NUM_ARGS], *dir, *gv_env, *gs_path;
612 char **gs_args, **alpha_args = NULL;
615 LOG ("Start the interpreter");
620 stop_interpreter(gs);
622 /* set up the args... */
623 gs_path = g_find_program_in_path ("gs");
624 gs_args = g_strsplit (gs_path, " ", NUM_GS_ARGS);
626 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
627 argv[argc] = gs_args[i];
630 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
631 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
632 argv[argc] = alpha_args[i];
635 argv[argc++] = "-dNOPAUSE";
636 argv[argc++] = "-dQUIET";
637 argv[argc++] = "-dSAFER";
639 /* set up the pipes */
640 if (gs->send_filename_to_gs) {
641 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
643 argv[argc++] = "quit";
650 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
651 if (pipe (std_in) == -1) {
652 g_critical ("Unable to open pipe to Ghostscript.");
657 if (pipe (std_out) == -1) {
662 if (pipe(std_err) == -1) {
664 close_pipe (std_out);
668 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
669 gdk_x11_drawable_get_xid (gs->pstarget),
670 gdk_x11_drawable_get_xid (gs->bpixmap));
671 LOG ("Launching ghostview with env %s", gv_env);
673 gs->interpreter_pid = fork ();
674 switch (gs->interpreter_pid) {
677 close_pipe (std_out);
678 close_pipe (std_err);
683 dup2 (std_out[1], 1);
687 dup2 (std_err[1], 2);
690 if (!gs->reading_from_pipe) {
691 if (gs->send_filename_to_gs) {
693 /* just in case gs tries to read from stdin */
694 stdinfd = open("/dev/null", O_RDONLY);
708 /* change to directory where the input file is. This helps
709 * with postscript-files which include other files using
711 dir = g_path_get_dirname (gs->gs_filename);
715 execvp (argv[0], argv);
718 g_critical ("Unable to execute [%s]\n", argv[0]);
719 g_strfreev (gs_args);
721 g_strfreev (alpha_args);
724 default: /* parent */
725 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
728 /* use non-blocking IO for pipe to ghostscript */
729 result = fcntl (std_in[1], F_GETFL, 0);
730 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
731 gs->interpreter_input = std_in[1];
733 gs->interpreter_input = -1;
737 gs->interpreter_output = std_out[0];
739 gs->interpreter_err = std_err[0];
740 gs->interpreter_output_id =
741 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
742 gs->interpreter_error_id =
743 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
751 stop_interpreter(PSDocument * gs)
753 if (gs->interpreter_pid > 0) {
755 LOG ("Stop the interpreter");
756 kill (gs->interpreter_pid, SIGTERM);
757 while ((wait(&status) == -1) && (errno == EINTR));
758 gs->interpreter_pid = -1;
760 gs->gs_status = _("Interpreter failed.");
764 if (gs->interpreter_input >= 0) {
765 close (gs->interpreter_input);
766 gs->interpreter_input = -1;
767 if (gs->interpreter_input_id != 0) {
768 gdk_input_remove(gs->interpreter_input_id);
769 gs->interpreter_input_id = 0;
771 while (gs->ps_input) {
772 struct record_list *ps_old = gs->ps_input;
773 gs->ps_input = gs->ps_input->next;
774 if (ps_old->close && NULL != ps_old->fp)
780 if (gs->interpreter_output >= 0) {
781 close (gs->interpreter_output);
782 gs->interpreter_output = -1;
783 if (gs->interpreter_output_id) {
784 gdk_input_remove (gs->interpreter_output_id);
785 gs->interpreter_output_id = 0;
789 if (gs->interpreter_err >= 0) {
790 close (gs->interpreter_err);
791 gs->interpreter_err = -1;
792 if (gs->interpreter_error_id) {
793 gdk_input_remove (gs->interpreter_error_id);
794 gs->interpreter_error_id = 0;
801 /* If file exists and is a regular file then return its length, else -1 */
803 file_length (const gchar * filename)
805 struct stat stat_rec;
807 if (filename && (stat (filename, &stat_rec) == 0) && S_ISREG (stat_rec.st_mode))
808 return stat_rec.st_size;
813 /* Test if file exists, is a regular file and its length is > 0 */
815 file_readable(const char *filename)
817 return (file_length (filename) > 0);
821 * Decompress gs->gs_filename if necessary
822 * Set gs->filename_unc to the name of the uncompressed file or NULL.
823 * Error reporting via signal 'interpreter_message'
824 * Return name of input file to use or NULL on error..
827 check_filecompressed (PSDocument * gs)
831 gchar *filename, *filename_unc, *filename_err, *cmdline;
837 if ((file = fopen(gs->gs_filename, "r")) &&
838 (fread (buf, sizeof(gchar), 3, file) == 3)) {
839 if ((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
840 /* file is gzipped or compressed */
841 cmd = gtk_gs_defaults_get_ungzip_cmd ();
842 } else if (strncmp (buf, "BZh", 3) == 0) {
843 /* file is compressed with bzip2 */
844 cmd = gtk_gs_defaults_get_unbzip2_cmd ();
852 return gs->gs_filename;
854 /* do the decompression */
855 filename = g_shell_quote (gs->gs_filename);
856 filename_unc = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
857 if ((fd = mkstemp (filename_unc)) < 0) {
858 g_free (filename_unc);
864 filename_err = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
865 if ((fd = mkstemp(filename_err)) < 0) {
866 g_free (filename_err);
867 g_free (filename_unc);
873 cmdline = g_strdup_printf ("%s %s >%s 2>%s", cmd,
874 filename, filename_unc, filename_err);
875 if (system (cmdline) == 0 &&
876 file_readable (filename_unc) &&
877 file_length (filename_err) == 0) {
878 /* sucessfully uncompressed file */
879 gs->gs_filename_unc = filename_unc;
885 filename_dsp = g_filename_display_name (gs->gs_filename);
886 msg = g_strdup_printf (_("Error while decompressing file “%s”:\n"), filename_dsp);
887 g_free (filename_dsp);
889 interpreter_failed (gs, msg);
891 unlink (filename_unc);
892 g_free (filename_unc);
896 unlink (filename_err);
897 g_free (filename_err);
905 ps_document_enable_interpreter(PSDocument *gs)
907 g_return_val_if_fail (PS_IS_DOCUMENT (gs), FALSE);
909 if (!gs->gs_filename)
912 return start_interpreter (gs);
916 document_load (PSDocument *gs, const gchar *fname)
918 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
920 LOG ("Load the document");
927 /* prepare this document */
928 gs->structured_doc = FALSE;
929 gs->send_filename_to_gs = TRUE;
930 gs->gs_filename = g_strdup (fname);
932 if ((gs->reading_from_pipe = (strcmp (fname, "-") == 0))) {
933 gs->send_filename_to_gs = FALSE;
936 * We need to make sure that the file is loadable/exists!
937 * otherwise we want to exit without loading new stuff...
939 gchar *filename = NULL;
941 if (!file_readable(fname)) {
945 filename_dsp = g_filename_display_name (fname);
946 msg = g_strdup_printf (_("Cannot open file “%s”.\n"), filename_dsp);
947 g_free (filename_dsp);
949 interpreter_failed (gs, msg);
951 gs->gs_status = _("File is not readable.");
953 filename = check_filecompressed(gs);
956 if (!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
957 interpreter_failed (gs, NULL);
961 /* we grab the vital statistics!!! */
962 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
964 if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
965 (gs->doc->epsf && gs->doc->numpages > 1)) {
966 gs->structured_doc = TRUE;
967 gs->send_filename_to_gs = FALSE;
971 gs->gs_status = _("Document loaded.");
977 ps_document_next_page (PSDocument *gs)
981 LOG ("Make ghostscript render next page");
983 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
984 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
985 g_return_val_if_fail (gs->busy != TRUE, FALSE);
989 event.xclient.type = ClientMessage;
990 event.xclient.display = gdk_display;
991 event.xclient.window = gs->message_window;
992 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
993 event.xclient.format = 32;
995 gdk_error_trap_push ();
996 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
998 gdk_error_trap_pop ();
1004 render_page (PSDocument *gs, int page)
1006 g_return_val_if_fail(gs != NULL, FALSE);
1007 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1009 if(!gs->gs_filename) {
1013 if (gs->structured_doc && gs->doc) {
1014 LOG ("It's a structured document, let's send one page to gs");
1016 if (is_interpreter_ready (gs)) {
1017 ps_document_next_page (gs);
1019 ps_document_enable_interpreter (gs);
1020 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1021 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1024 send_ps (gs, gs->doc->pages[page].begin,
1025 gs->doc->pages[page].len, FALSE);
1027 /* Unstructured document
1029 * In the case of non structured documents,
1030 * GS read the PS from the actual file (via command
1031 * line. Hence, ggv only send a signal next page.
1032 * If ghostview is not running it is usually because
1033 * the last page of the file was displayed. In that
1034 * case, ggv restarts GS again and the first page is displayed.
1037 LOG ("It's an unstructured document, gs will just read the file");
1039 if (!is_interpreter_ready (gs)) {
1040 ps_document_enable_interpreter(gs);
1042 ps_document_next_page(gs);
1049 ps_document_load (EvDocument *document,
1057 filename = g_filename_from_uri (uri, NULL, error);
1061 gs_path = g_find_program_in_path ("gs");
1063 gchar *filename_dsp;
1064 filename_dsp = g_filename_display_name (filename);
1068 _("Failed to load document “%s”. Ghostscript interpreter was not found in path"),
1070 g_free (filename_dsp);
1073 result = document_load (PS_DOCUMENT (document), filename);
1075 gchar *filename_dsp;
1076 filename_dsp = g_filename_display_name (filename);
1078 g_set_error (error, G_FILE_ERROR,
1079 G_FILE_ERROR_FAILED,
1080 _("Failed to load document “%s”"),
1082 g_free (filename_dsp);
1092 save_document (PSDocument *document, const char *filename)
1094 gboolean result = TRUE;
1095 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1099 src_file = fopen (PS_DOCUMENT_GET_PS_FILE(document), "r");
1101 struct stat stat_rec;
1103 if (stat (PS_DOCUMENT_GET_PS_FILE(document), &stat_rec) == 0) {
1104 pscopy (src_file, sink, 0, stat_rec.st_size - 1);
1110 buf = gtk_gs_doc_sink_get_buffer (sink);
1115 f = fopen (filename, "w");
1124 gtk_gs_doc_sink_free (sink);
1131 save_page_list (PSDocument *document, int *page_list, const char *filename)
1133 gboolean result = TRUE;
1134 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1138 pscopydoc (sink, PS_DOCUMENT_GET_PS_FILE(document),
1139 document->doc, page_list);
1141 buf = gtk_gs_doc_sink_get_buffer (sink);
1143 f = fopen (filename, "w");
1152 gtk_gs_doc_sink_free (sink);
1159 ps_document_save (EvDocument *document,
1163 PSDocument *ps = PS_DOCUMENT (document);
1167 filename = g_filename_from_uri (uri, NULL, error);
1171 result = save_document (ps, filename);
1179 ps_document_get_n_pages (EvDocument *document)
1181 PSDocument *ps = PS_DOCUMENT (document);
1183 g_return_val_if_fail (ps != NULL, -1);
1185 if (!ps->gs_filename || !ps->doc) {
1189 return ps->structured_doc ? ps->doc->numpages : 1;
1193 ps_document_get_page_size (EvDocument *document,
1198 PSDocument *gs = PS_DOCUMENT (document);
1199 int urx, ury, llx, lly;
1201 get_page_box (gs, page, &urx, &ury, &llx, &lly);
1204 *width = (urx - llx) + 0.5;
1208 *height = (ury - lly) + 0.5;
1213 ps_document_can_get_text (EvDocument *document)
1219 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer, int page, double scale, int rotation)
1221 PSDocument *gs = PS_DOCUMENT (renderer);
1223 if (gs->pstarget == NULL) {
1224 gs->target_window = gtk_window_new (GTK_WINDOW_POPUP);
1225 gtk_widget_realize (gs->target_window);
1226 gs->pstarget = gs->target_window->window;
1228 g_assert (gs->pstarget != NULL);
1230 g_signal_connect (gs->target_window, "event",
1231 G_CALLBACK (ps_document_widget_event),
1235 setup_pixmap (gs, page, scale, rotation);
1236 setup_page (gs, page, scale, rotation);
1238 render_page (gs, page);
1241 static EvDocumentInfo *
1242 ps_document_get_info (EvDocument *document)
1244 EvDocumentInfo *info;
1245 PSDocument *ps = PS_DOCUMENT (document);
1246 int urx, ury, llx, lly;
1248 info = g_new0 (EvDocumentInfo, 1);
1249 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
1250 EV_DOCUMENT_INFO_FORMAT |
1251 EV_DOCUMENT_INFO_CREATOR |
1252 EV_DOCUMENT_INFO_N_PAGES |
1253 EV_DOCUMENT_INFO_PAPER_SIZE;
1255 info->title = g_strdup (ps->doc->title);
1256 info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
1257 : g_strdup (_("PostScript"));
1258 info->creator = g_strdup (ps->doc->creator);
1259 info->n_pages = ev_document_get_n_pages (document);
1261 get_page_box (PS_DOCUMENT (document), 0, &urx, &ury, &llx, &lly);
1263 info->paper_width = (urx - llx) / 72.0f * 25.4f;
1264 info->paper_height = (ury - lly) / 72.0f * 25.4f;
1270 ps_document_document_iface_init (EvDocumentIface *iface)
1272 iface->load = ps_document_load;
1273 iface->save = ps_document_save;
1274 iface->can_get_text = ps_document_can_get_text;
1275 iface->get_n_pages = ps_document_get_n_pages;
1276 iface->get_page_size = ps_document_get_page_size;
1277 iface->get_info = ps_document_get_info;
1281 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
1283 iface->render_pixbuf = ps_async_renderer_render_pixbuf;
1287 ps_document_file_exporter_format_supported (EvFileExporter *exporter,
1288 EvFileExporterFormat format)
1290 return (format == EV_FILE_FORMAT_PS);
1294 ps_document_file_exporter_begin (EvFileExporter *exporter,
1295 EvFileExporterFormat format,
1296 const char *filename,
1303 PSDocument *document = PS_DOCUMENT (exporter);
1305 if (document->structured_doc) {
1306 g_free (document->ps_export_pagelist);
1308 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1311 document->ps_export_filename = g_strdup (filename);
1315 ps_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
1317 PSDocument *document = PS_DOCUMENT (exporter);
1319 if (document->structured_doc) {
1320 document->ps_export_pagelist[rc->page] = 1;
1325 ps_document_file_exporter_end (EvFileExporter *exporter)
1327 PSDocument *document = PS_DOCUMENT (exporter);
1329 if (!document->structured_doc) {
1330 save_document (document, document->ps_export_filename);
1332 save_page_list (document, document->ps_export_pagelist,
1333 document->ps_export_filename);
1334 g_free (document->ps_export_pagelist);
1335 g_free (document->ps_export_filename);
1336 document->ps_export_pagelist = NULL;
1337 document->ps_export_filename = NULL;
1342 ps_document_file_exporter_iface_init (EvFileExporterIface *iface)
1344 iface->format_supported = ps_document_file_exporter_format_supported;
1345 iface->begin = ps_document_file_exporter_begin;
1346 iface->do_page = ps_document_file_exporter_do_page;
1347 iface->end = ps_document_file_exporter_end;