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"
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_ps_exporter_iface_init (EvPSExporterIface *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_PS_EXPORTER,
96 ps_document_ps_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;
141 gs->orientation = GTK_GS_ORIENTATION_NONE;
145 ps_document_dispose (GObject *object)
147 PSDocument *gs = PS_DOCUMENT (object);
149 g_return_if_fail (gs != NULL);
152 fclose (gs->gs_psfile);
153 gs->gs_psfile = NULL;
156 if (gs->gs_filename) {
157 g_free (gs->gs_filename);
158 gs->gs_filename = NULL;
166 if (gs->gs_filename_unc) {
167 unlink(gs->gs_filename_unc);
168 g_free(gs->gs_filename_unc);
169 gs->gs_filename_unc = NULL;
173 gdk_drawable_unref (gs->bpixmap);
176 if(gs->input_buffer) {
177 g_free(gs->input_buffer);
178 gs->input_buffer = NULL;
181 if (gs->target_window) {
182 gtk_widget_destroy (gs->target_window);
183 gs->target_window = NULL;
187 stop_interpreter (gs);
189 G_OBJECT_CLASS (parent_class)->dispose (object);
193 ps_document_class_init(PSDocumentClass *klass)
195 GObjectClass *object_class;
197 object_class = (GObjectClass *) klass;
198 parent_class = g_type_class_peek_parent (klass);
201 object_class->dispose = ps_document_dispose;
203 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
204 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
205 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
206 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
210 push_pixbuf (PSDocument *gs)
216 cmap = gdk_window_get_colormap (gs->pstarget);
217 gdk_drawable_get_size (gs->bpixmap, &width, &height);
218 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
221 g_signal_emit_by_name (gs, "render_finished", pixbuf);
222 g_object_unref (pixbuf);
226 interpreter_failed (PSDocument *gs, char *msg)
228 LOG ("Interpreter failed %s", msg);
232 stop_interpreter (gs);
236 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
238 PSDocument *gs = (PSDocument *) data;
240 if(event->type != GDK_CLIENT_EVENT)
243 gs->message_window = event->client.data.l[0];
245 if (event->client.message_type == gs_class->page_atom) {
246 LOG ("GS rendered the document");
256 send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
258 struct record_list *ps_new;
260 if (gs->interpreter_input < 0) {
261 g_critical("No pipe to gs: error in send_ps().");
265 ps_new = g_new0 (struct record_list, 1);
266 ps_new->fp = gs->gs_psfile;
267 ps_new->begin = begin;
269 ps_new->seek_needed = TRUE;
270 ps_new->close = close;
273 if (gs->input_buffer == NULL) {
274 gs->input_buffer = g_malloc(MAX_BUFSIZE);
277 if (gs->ps_input == NULL) {
278 gs->input_buffer_ptr = gs->input_buffer;
279 gs->bytes_left = len;
280 gs->buffer_bytes_left = 0;
281 gs->ps_input = ps_new;
282 gs->interpreter_input_id = gdk_input_add
283 (gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
285 struct record_list *p = gs->ps_input;
286 while (p->next != NULL) {
294 get_xdpi (PSDocument *gs)
296 return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
300 get_ydpi (PSDocument *gs)
302 return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
306 setup_pixmap (PSDocument *gs, int page, double scale)
309 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
310 GdkColormap *colormap;
311 double width, height;
312 int pixmap_width, pixmap_height;
314 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
315 pixmap_width = width * scale + 0.5;
316 pixmap_height = height * scale + 0.5;
321 gdk_drawable_get_size (gs->bpixmap, &w, &h);
323 if (pixmap_width != w || h != pixmap_height) {
324 gdk_drawable_unref (gs->bpixmap);
326 stop_interpreter (gs);
331 LOG ("Create pixmap");
333 fill = gdk_gc_new (gs->pstarget);
334 colormap = gdk_drawable_get_colormap (gs->pstarget);
335 gdk_color_alloc (colormap, &white);
336 gdk_gc_set_foreground (fill, &white);
337 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
339 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
340 0, 0, pixmap_width, pixmap_height);
344 #define DEFAULT_PAGE_SIZE 1
347 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
353 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
354 int new_pagesize = -1;
356 g_return_if_fail (PS_IS_DOCUMENT (gs));
358 if (new_pagesize == -1) {
359 new_pagesize = DEFAULT_PAGE_SIZE;
361 /* If we have a document:
362 * We use -- the page size (if specified)
363 * or the doc. size (if specified)
364 * or the page bbox (if specified)
365 * or the bounding box
367 if ((page >= 0) && (gs->doc->numpages > page) &&
368 (gs->doc->pages) && (gs->doc->pages[page].size)) {
369 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
370 } else if (gs->doc->default_page_size != NULL) {
371 new_pagesize = gs->doc->default_page_size - gs->doc->size;
372 } else if ((page >= 0) &&
373 (gs->doc->numpages > page) &&
375 (gs->doc->pages[page].boundingbox[URX] >
376 gs->doc->pages[page].boundingbox[LLX]) &&
377 (gs->doc->pages[page].boundingbox[URY] >
378 gs->doc->pages[page].boundingbox[LLY])) {
380 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
381 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
387 /* Compute bounding box */
388 if (gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
391 (gs->doc->pages[page].boundingbox[URX] >
392 gs->doc->pages[page].boundingbox[LLX]) &&
393 (gs->doc->pages[page].boundingbox[URY] >
394 gs->doc->pages[page].boundingbox[LLY])) {
396 new_llx = gs->doc->pages[page].boundingbox[LLX];
397 new_lly = gs->doc->pages[page].boundingbox[LLY];
398 new_urx = gs->doc->pages[page].boundingbox[URX];
399 new_ury = gs->doc->pages[page].boundingbox[URY];
400 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
401 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
403 new_llx = gs->doc->boundingbox[LLX];
404 new_lly = gs->doc->boundingbox[LLY];
405 new_urx = gs->doc->boundingbox[URX];
406 new_ury = gs->doc->boundingbox[URY];
409 if (new_pagesize < 0)
410 new_pagesize = DEFAULT_PAGE_SIZE;
411 new_llx = new_lly = 0;
412 if (gs->doc && gs->doc->size &&
413 (new_pagesize < gs->doc->numsizes)) {
414 new_urx = gs->doc->size[new_pagesize].width;
415 new_ury = gs->doc->size[new_pagesize].height;
417 new_urx = papersizes[new_pagesize].width;
418 new_ury = papersizes[new_pagesize].height;
422 if (new_urx <= new_llx)
423 new_urx = papersizes[12].width;
424 if (new_ury <= new_lly)
425 new_ury = papersizes[12].height;
434 get_page_orientation (PSDocument *gs, int page)
438 orientation = GTK_GS_ORIENTATION_NONE;
440 if (gs->structured_doc) {
441 orientation = gs->doc->pages[page].orientation;
443 if (orientation == GTK_GS_ORIENTATION_NONE) {
444 orientation = gs->doc->default_page_orientation;
446 if (orientation == GTK_GS_ORIENTATION_NONE) {
447 orientation = gs->doc->orientation;
449 if (orientation == GTK_GS_ORIENTATION_NONE) {
450 orientation = GTK_GS_ORIENTATION_PORTRAIT;
452 if (gs->orientation != GTK_GS_ORIENTATION_NONE) {
453 orientation = gs->orientation;
460 setup_page (PSDocument *gs, int page, double scale)
463 int urx, ury, llx, lly, orientation;
464 char scaled_xdpi[G_ASCII_DTOSTR_BUF_SIZE];
465 char scaled_ydpi[G_ASCII_DTOSTR_BUF_SIZE];
467 LOG ("Setup the page");
469 get_page_box (gs, page, &urx, &ury, &llx, &lly);
470 orientation = get_page_orientation (gs, page);
471 g_ascii_dtostr (scaled_xdpi, G_ASCII_DTOSTR_BUF_SIZE, get_xdpi (gs) * scale);
472 g_ascii_dtostr (scaled_ydpi, G_ASCII_DTOSTR_BUF_SIZE, get_ydpi (gs) * scale);
474 buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
475 0L, orientation * 90, llx, lly, urx, ury,
476 scaled_xdpi, scaled_ydpi,
478 LOG ("GS property %s", buf);
480 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
481 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
488 close_pipe (int p[2])
499 is_interpreter_ready (PSDocument *gs)
501 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
505 output (gpointer data, gint source, GdkInputCondition condition)
507 char buf[MAX_BUFSIZE + 1];
509 PSDocument *gs = PS_DOCUMENT(data);
511 if (source == gs->interpreter_output) {
512 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
513 if (bytes == 0) { /* EOF occurred */
514 close (gs->interpreter_output);
515 gs->interpreter_output = -1;
516 gdk_input_remove (gs->interpreter_output_id);
518 } else if (bytes == -1) {
520 interpreter_failed (gs, NULL);
523 if (gs->interpreter_err == -1) {
524 interpreter_failed (gs, NULL);
526 } else if (source == gs->interpreter_err) {
527 bytes = read (gs->interpreter_err, buf, MAX_BUFSIZE);
528 if (bytes == 0) { /* EOF occurred */
529 close (gs->interpreter_err);
530 gs->interpreter_err = -1;
531 gdk_input_remove (gs->interpreter_error_id);
533 } else if (bytes == -1) {
535 interpreter_failed (gs, NULL);
538 if (gs->interpreter_output == -1) {
539 interpreter_failed(gs, NULL);
556 input(gpointer data, gint source, GdkInputCondition condition)
558 PSDocument *gs = PS_DOCUMENT(data);
560 void (*oldsig) (int);
561 oldsig = signal(SIGPIPE, catchPipe);
566 if (gs->buffer_bytes_left == 0) {
567 /* Get a new section if required */
568 if (gs->ps_input && gs->bytes_left == 0) {
569 struct record_list *ps_old = gs->ps_input;
570 gs->ps_input = ps_old->next;
571 if (ps_old->close && NULL != ps_old->fp)
576 /* Have to seek at the beginning of each section */
577 if (gs->ps_input && gs->ps_input->seek_needed) {
578 fseek (gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
579 gs->ps_input->seek_needed = FALSE;
580 gs->bytes_left = gs->ps_input->len;
583 if (gs->bytes_left > MAX_BUFSIZE) {
584 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
585 MAX_BUFSIZE, gs->ps_input->fp);
586 } else if (gs->bytes_left > 0) {
587 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
588 gs->bytes_left, gs->ps_input->fp);
590 gs->buffer_bytes_left = 0;
592 if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
593 interpreter_failed (gs, NULL); /* Error occurred */
595 gs->input_buffer_ptr = gs->input_buffer;
596 gs->bytes_left -= gs->buffer_bytes_left;
599 if (gs->buffer_bytes_left > 0) {
600 bytes_written = write (gs->interpreter_input,
601 gs->input_buffer_ptr, gs->buffer_bytes_left);
604 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
606 interpreter_failed (gs, NULL);
607 } else if (bytes_written == -1) {
608 if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
609 interpreter_failed (gs, NULL); /* Something bad happened */
612 gs->buffer_bytes_left -= bytes_written;
613 gs->input_buffer_ptr += bytes_written;
616 } while (gs->ps_input && gs->buffer_bytes_left == 0);
618 signal (SIGPIPE, oldsig);
620 if (gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
621 if (gs->interpreter_input_id != 0) {
622 gdk_input_remove (gs->interpreter_input_id);
623 gs->interpreter_input_id = 0;
629 start_interpreter (PSDocument *gs)
631 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
632 int std_out[2]; /* pipe from interp stdout */
633 int std_err[2]; /* pipe from interp stderr */
636 #define NUM_GS_ARGS (NUM_ARGS - 20)
637 #define NUM_ALPHA_ARGS 10
639 char *argv[NUM_ARGS], *dir, *gv_env;
640 char **gs_args, **alpha_args = NULL;
643 LOG ("Start the interpreter");
648 stop_interpreter(gs);
650 /* set up the args... */
651 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
652 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
653 argv[argc] = gs_args[i];
656 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
657 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
658 argv[argc] = alpha_args[i];
661 argv[argc++] = "-dNOPAUSE";
662 argv[argc++] = "-dQUIET";
663 argv[argc++] = "-dSAFER";
665 /* set up the pipes */
666 if (gs->send_filename_to_gs) {
667 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
669 argv[argc++] = "quit";
676 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
677 if (pipe (std_in) == -1) {
678 g_critical ("Unable to open pipe to Ghostscript.");
683 if (pipe (std_out) == -1) {
688 if (pipe(std_err) == -1) {
690 close_pipe (std_out);
694 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
695 gdk_x11_drawable_get_xid (gs->pstarget),
696 gdk_x11_drawable_get_xid (gs->bpixmap));
697 LOG ("Launching ghostview with env %s", gv_env);
699 gs->interpreter_pid = fork ();
700 switch (gs->interpreter_pid) {
703 close_pipe (std_out);
704 close_pipe (std_err);
709 dup2 (std_out[1], 1);
713 dup2 (std_err[1], 2);
716 if (!gs->reading_from_pipe) {
717 if (gs->send_filename_to_gs) {
719 /* just in case gs tries to read from stdin */
720 stdinfd = open("/dev/null", O_RDONLY);
734 /* change to directory where the input file is. This helps
735 * with postscript-files which include other files using
737 dir = g_path_get_dirname (gs->gs_filename);
741 execvp (argv[0], argv);
744 g_critical ("Unable to execute [%s]\n", argv[0]);
745 g_strfreev (gs_args);
747 g_strfreev (alpha_args);
750 default: /* parent */
751 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
754 /* use non-blocking IO for pipe to ghostscript */
755 result = fcntl (std_in[1], F_GETFL, 0);
756 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
757 gs->interpreter_input = std_in[1];
759 gs->interpreter_input = -1;
763 gs->interpreter_output = std_out[0];
765 gs->interpreter_err = std_err[0];
766 gs->interpreter_output_id =
767 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
768 gs->interpreter_error_id =
769 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
777 stop_interpreter(PSDocument * gs)
779 if (gs->interpreter_pid > 0) {
781 LOG ("Stop the interpreter");
782 kill (gs->interpreter_pid, SIGTERM);
783 while ((wait(&status) == -1) && (errno == EINTR));
784 gs->interpreter_pid = -1;
786 gs->gs_status = _("Interpreter failed.");
790 if (gs->interpreter_input >= 0) {
791 close (gs->interpreter_input);
792 gs->interpreter_input = -1;
793 if (gs->interpreter_input_id != 0) {
794 gdk_input_remove(gs->interpreter_input_id);
795 gs->interpreter_input_id = 0;
797 while (gs->ps_input) {
798 struct record_list *ps_old = gs->ps_input;
799 gs->ps_input = gs->ps_input->next;
800 if (ps_old->close && NULL != ps_old->fp)
806 if (gs->interpreter_output >= 0) {
807 close (gs->interpreter_output);
808 gs->interpreter_output = -1;
809 if (gs->interpreter_output_id) {
810 gdk_input_remove (gs->interpreter_output_id);
811 gs->interpreter_output_id = 0;
815 if (gs->interpreter_err >= 0) {
816 close (gs->interpreter_err);
817 gs->interpreter_err = -1;
818 if (gs->interpreter_error_id) {
819 gdk_input_remove (gs->interpreter_error_id);
820 gs->interpreter_error_id = 0;
827 /* If file exists and is a regular file then return its length, else -1 */
829 file_length (const gchar * filename)
831 struct stat stat_rec;
833 if (filename && (stat (filename, &stat_rec) == 0) && S_ISREG (stat_rec.st_mode))
834 return stat_rec.st_size;
839 /* Test if file exists, is a regular file and its length is > 0 */
841 file_readable(const char *filename)
843 return (file_length (filename) > 0);
847 * Decompress gs->gs_filename if necessary
848 * Set gs->filename_unc to the name of the uncompressed file or NULL.
849 * Error reporting via signal 'interpreter_message'
850 * Return name of input file to use or NULL on error..
853 check_filecompressed (PSDocument * gs)
857 gchar *filename, *filename_unc, *filename_err, *cmdline;
863 if ((file = fopen(gs->gs_filename, "r")) &&
864 (fread (buf, sizeof(gchar), 3, file) == 3)) {
865 if ((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
866 /* file is gzipped or compressed */
867 cmd = gtk_gs_defaults_get_ungzip_cmd ();
868 } else if (strncmp (buf, "BZh", 3) == 0) {
869 /* file is compressed with bzip2 */
870 cmd = gtk_gs_defaults_get_unbzip2_cmd ();
878 return gs->gs_filename;
880 /* do the decompression */
881 filename = g_shell_quote (gs->gs_filename);
882 filename_unc = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
883 if ((fd = mkstemp (filename_unc)) < 0) {
884 g_free (filename_unc);
890 filename_err = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
891 if ((fd = mkstemp(filename_err)) < 0) {
892 g_free (filename_err);
893 g_free (filename_unc);
899 cmdline = g_strdup_printf ("%s %s >%s 2>%s", cmd,
900 filename, filename_unc, filename_err);
901 if (system (cmdline) == 0 &&
902 file_readable (filename_unc) &&
903 file_length (filename_err) == 0) {
904 /* sucessfully uncompressed file */
905 gs->gs_filename_unc = filename_unc;
911 utf8 = g_locale_to_utf8 (gs->gs_filename, -1, NULL, NULL, NULL);
914 msg = g_strdup_printf (_("Error while decompressing file %s:\n"), utf8);
916 msg = g_strdup (_("Error while decompressing file\n"));
921 interpreter_failed (gs, msg);
923 unlink (filename_unc);
924 g_free (filename_unc);
928 unlink (filename_err);
929 g_free (filename_err);
937 ps_document_enable_interpreter(PSDocument *gs)
939 g_return_val_if_fail (PS_IS_DOCUMENT (gs), FALSE);
941 if (!gs->gs_filename)
944 return start_interpreter (gs);
948 document_load (PSDocument *gs, const gchar *fname)
950 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
952 LOG ("Load the document");
959 /* prepare this document */
960 gs->structured_doc = FALSE;
961 gs->send_filename_to_gs = TRUE;
962 gs->gs_filename = g_strdup (fname);
964 if ((gs->reading_from_pipe = (strcmp (fname, "-") == 0))) {
965 gs->send_filename_to_gs = FALSE;
968 * We need to make sure that the file is loadable/exists!
969 * otherwise we want to exit without loading new stuff...
971 gchar *filename = NULL;
973 if (!file_readable(fname)) {
977 utf8 = g_locale_to_utf8 (fname, -1, NULL, NULL, NULL);
980 msg = g_strdup_printf (_("Cannot open file %s.\n"), utf8);
982 msg = g_strdup (_("Cannot open file.\n"));
987 interpreter_failed (gs, msg);
989 gs->gs_status = _("File is not readable.");
991 filename = check_filecompressed(gs);
994 if (!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
995 interpreter_failed (gs, NULL);
999 /* we grab the vital statistics!!! */
1000 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
1002 if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
1003 (gs->doc->epsf && gs->doc->numpages > 1)) {
1004 gs->structured_doc = TRUE;
1005 gs->send_filename_to_gs = FALSE;
1009 gs->gs_status = _("Document loaded.");
1015 ps_document_next_page (PSDocument *gs)
1019 LOG ("Make ghostscript render next page");
1021 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
1022 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
1023 g_return_val_if_fail (gs->busy != TRUE, FALSE);
1027 event.xclient.type = ClientMessage;
1028 event.xclient.display = gdk_display;
1029 event.xclient.window = gs->message_window;
1030 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1031 event.xclient.format = 32;
1033 gdk_error_trap_push ();
1034 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1036 gdk_error_trap_pop ();
1042 render_page (PSDocument *gs, int page)
1044 g_return_val_if_fail(gs != NULL, FALSE);
1045 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1047 if(!gs->gs_filename) {
1051 if (gs->structured_doc && gs->doc) {
1052 LOG ("It's a structured document, let's send one page to gs");
1054 if (is_interpreter_ready (gs)) {
1055 ps_document_next_page (gs);
1057 ps_document_enable_interpreter (gs);
1058 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1059 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1062 send_ps (gs, gs->doc->pages[page].begin,
1063 gs->doc->pages[page].len, FALSE);
1065 /* Unstructured document
1067 * In the case of non structured documents,
1068 * GS read the PS from the actual file (via command
1069 * line. Hence, ggv only send a signal next page.
1070 * If ghostview is not running it is usually because
1071 * the last page of the file was displayed. In that
1072 * case, ggv restarts GS again and the first page is displayed.
1075 LOG ("It's an unstructured document, gs will just read the file");
1077 if (!is_interpreter_ready (gs)) {
1078 ps_document_enable_interpreter(gs);
1080 ps_document_next_page(gs);
1087 ps_document_load (EvDocument *document,
1094 filename = g_filename_from_uri (uri, NULL, error);
1098 result = document_load (PS_DOCUMENT (document), filename);
1102 utf8 = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
1105 g_set_error (error, G_FILE_ERROR,
1106 G_FILE_ERROR_FAILED,
1107 _("Failed to load document '%s'"),
1110 g_set_error (error, G_FILE_ERROR,
1111 G_FILE_ERROR_FAILED,
1112 _("Failed to load document"));
1124 save_page_list (PSDocument *document, int *page_list, const char *filename)
1126 gboolean result = TRUE;
1127 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1131 pscopydoc (sink, PS_DOCUMENT_GET_PS_FILE(document),
1132 document->doc, page_list);
1134 buf = gtk_gs_doc_sink_get_buffer (sink);
1136 f = fopen (filename, "w");
1145 gtk_gs_doc_sink_free (sink);
1152 ps_document_save (EvDocument *document,
1156 PSDocument *ps = PS_DOCUMENT (document);
1162 filename = g_filename_from_uri (uri, NULL, error);
1166 page_list = g_new0 (int, ps->doc->numpages);
1167 for (i = 0; i < ps->doc->numpages; i++) {
1171 result = save_page_list (ps, page_list, filename);
1180 ps_document_get_n_pages (EvDocument *document)
1182 PSDocument *ps = PS_DOCUMENT (document);
1184 g_return_val_if_fail (ps != NULL, -1);
1186 if (!ps->gs_filename || !ps->doc) {
1190 return ps->structured_doc ? ps->doc->numpages : 1;
1194 ps_document_get_page_size (EvDocument *document,
1199 PSDocument *gs = PS_DOCUMENT (document);
1201 int urx, ury, llx, lly, orientation;
1203 get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
1204 orientation = get_page_orientation (PS_DOCUMENT (document), page);
1206 switch (orientation) {
1207 case GTK_GS_ORIENTATION_PORTRAIT:
1208 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1209 w = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
1210 h = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
1212 case GTK_GS_ORIENTATION_LANDSCAPE:
1213 case GTK_GS_ORIENTATION_SEASCAPE:
1214 w = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
1215 h = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
1219 g_assert_not_reached ();
1233 ps_document_can_get_text (EvDocument *document)
1239 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer, int page, double scale)
1241 PSDocument *gs = PS_DOCUMENT (renderer);
1243 if (gs->pstarget == NULL) {
1244 gs->target_window = gtk_window_new (GTK_WINDOW_POPUP);
1245 gtk_widget_realize (gs->target_window);
1246 gs->pstarget = gs->target_window->window;
1248 g_assert (gs->pstarget != NULL);
1250 g_signal_connect (gs->target_window, "event",
1251 G_CALLBACK (ps_document_widget_event),
1255 setup_pixmap (gs, page, scale);
1256 setup_page (gs, page, scale);
1258 render_page (gs, page);
1261 static EvDocumentInfo *
1262 ps_document_get_info (EvDocument *document)
1264 EvDocumentInfo *info;
1265 PSDocument *ps = PS_DOCUMENT (document);
1267 info = g_new0 (EvDocumentInfo, 1);
1268 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
1269 EV_DOCUMENT_INFO_FORMAT |
1270 EV_DOCUMENT_INFO_CREATOR |
1271 EV_DOCUMENT_INFO_N_PAGES;
1272 info->title = g_strdup (ps->doc->title);
1273 info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
1274 : g_strdup (_("PostScript"));
1275 info->creator = g_strdup (ps->doc->creator);
1276 info->n_pages = ev_document_get_n_pages (document);
1282 ps_document_document_iface_init (EvDocumentIface *iface)
1284 iface->load = ps_document_load;
1285 iface->save = ps_document_save;
1286 iface->can_get_text = ps_document_can_get_text;
1287 iface->get_n_pages = ps_document_get_n_pages;
1288 iface->get_page_size = ps_document_get_page_size;
1289 iface->get_info = ps_document_get_info;
1293 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
1295 iface->render_pixbuf = ps_async_renderer_render_pixbuf;
1299 ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
1300 int first_page, int last_page,
1301 double width, double height, gboolean duplex)
1303 PSDocument *document = PS_DOCUMENT (exporter);
1305 g_free (document->ps_export_pagelist);
1307 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1308 document->ps_export_filename = g_strdup (filename);
1312 ps_document_ps_export_do_page (EvPSExporter *exporter, EvRenderContext *rc)
1314 PSDocument *document = PS_DOCUMENT (exporter);
1316 document->ps_export_pagelist[rc->page] = 1;
1320 ps_document_ps_export_end (EvPSExporter *exporter)
1322 PSDocument *document = PS_DOCUMENT (exporter);
1324 save_page_list (document, document->ps_export_pagelist,
1325 document->ps_export_filename);
1327 g_free (document->ps_export_pagelist);
1328 g_free (document->ps_export_filename);
1329 document->ps_export_pagelist = NULL;
1330 document->ps_export_filename = NULL;
1334 ps_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1336 iface->begin = ps_document_ps_export_begin;
1337 iface->do_page = ps_document_ps_export_do_page;
1338 iface->end = ps_document_ps_export_end;