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 LOG ("Get from drawable\n");
219 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
222 LOG ("Get from drawable done\n");
223 g_signal_emit_by_name (gs, "render_finished", pixbuf);
224 g_object_unref (pixbuf);
228 interpreter_failed (PSDocument *gs, char *msg)
230 LOG ("Interpreter failed %s", msg);
234 stop_interpreter (gs);
238 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
240 PSDocument *gs = (PSDocument *) data;
242 if(event->type != GDK_CLIENT_EVENT)
245 gs->message_window = event->client.data.l[0];
247 if (event->client.message_type == gs_class->page_atom) {
248 LOG ("GS rendered the document");
252 LOG ("Pixbuf pushed");
259 send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
261 struct record_list *ps_new;
263 if (gs->interpreter_input < 0) {
264 g_critical("No pipe to gs: error in send_ps().");
268 ps_new = g_new0 (struct record_list, 1);
269 ps_new->fp = gs->gs_psfile;
270 ps_new->begin = begin;
272 ps_new->seek_needed = TRUE;
273 ps_new->close = close;
276 if (gs->input_buffer == NULL) {
277 gs->input_buffer = g_malloc(MAX_BUFSIZE);
280 if (gs->ps_input == NULL) {
281 gs->input_buffer_ptr = gs->input_buffer;
282 gs->bytes_left = len;
283 gs->buffer_bytes_left = 0;
284 gs->ps_input = ps_new;
285 gs->interpreter_input_id = gdk_input_add
286 (gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
288 struct record_list *p = gs->ps_input;
289 while (p->next != NULL) {
297 get_xdpi (PSDocument *gs)
299 return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
303 get_ydpi (PSDocument *gs)
305 return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
309 setup_pixmap (PSDocument *gs, int page, double scale)
312 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
313 GdkColormap *colormap;
314 double width, height;
315 int pixmap_width, pixmap_height;
317 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
318 pixmap_width = width * scale + 0.5;
319 pixmap_height = height * scale + 0.5;
324 gdk_drawable_get_size (gs->bpixmap, &w, &h);
326 if (pixmap_width != w || h != pixmap_height) {
327 gdk_drawable_unref (gs->bpixmap);
329 stop_interpreter (gs);
334 LOG ("Create pixmap");
336 fill = gdk_gc_new (gs->pstarget);
337 colormap = gdk_drawable_get_colormap (gs->pstarget);
338 gdk_color_alloc (colormap, &white);
339 gdk_gc_set_foreground (fill, &white);
340 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
342 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
343 0, 0, pixmap_width, pixmap_height);
347 #define DEFAULT_PAGE_SIZE 1
350 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
356 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
357 int new_pagesize = -1;
359 g_return_if_fail (PS_IS_DOCUMENT (gs));
361 if (new_pagesize == -1) {
362 new_pagesize = DEFAULT_PAGE_SIZE;
364 /* If we have a document:
365 * We use -- the page size (if specified)
366 * or the doc. size (if specified)
367 * or the page bbox (if specified)
368 * or the bounding box
370 if ((page >= 0) && (gs->doc->numpages > page) &&
371 (gs->doc->pages) && (gs->doc->pages[page].size)) {
372 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
373 } else if (gs->doc->default_page_size != NULL) {
374 new_pagesize = gs->doc->default_page_size - gs->doc->size;
375 } else if ((page >= 0) &&
376 (gs->doc->numpages > page) &&
378 (gs->doc->pages[page].boundingbox[URX] >
379 gs->doc->pages[page].boundingbox[LLX]) &&
380 (gs->doc->pages[page].boundingbox[URY] >
381 gs->doc->pages[page].boundingbox[LLY])) {
383 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
384 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
390 /* Compute bounding box */
391 if (gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
394 (gs->doc->pages[page].boundingbox[URX] >
395 gs->doc->pages[page].boundingbox[LLX]) &&
396 (gs->doc->pages[page].boundingbox[URY] >
397 gs->doc->pages[page].boundingbox[LLY])) {
399 new_llx = gs->doc->pages[page].boundingbox[LLX];
400 new_lly = gs->doc->pages[page].boundingbox[LLY];
401 new_urx = gs->doc->pages[page].boundingbox[URX];
402 new_ury = gs->doc->pages[page].boundingbox[URY];
403 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
404 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
406 new_llx = gs->doc->boundingbox[LLX];
407 new_lly = gs->doc->boundingbox[LLY];
408 new_urx = gs->doc->boundingbox[URX];
409 new_ury = gs->doc->boundingbox[URY];
412 if (new_pagesize < 0)
413 new_pagesize = DEFAULT_PAGE_SIZE;
414 new_llx = new_lly = 0;
415 if (gs->doc && gs->doc->size &&
416 (new_pagesize < gs->doc->numsizes)) {
417 new_urx = gs->doc->size[new_pagesize].width;
418 new_ury = gs->doc->size[new_pagesize].height;
420 new_urx = papersizes[new_pagesize].width;
421 new_ury = papersizes[new_pagesize].height;
425 if (new_urx <= new_llx)
426 new_urx = papersizes[12].width;
427 if (new_ury <= new_lly)
428 new_ury = papersizes[12].height;
437 get_page_orientation (PSDocument *gs, int page)
441 orientation = GTK_GS_ORIENTATION_NONE;
443 if (gs->structured_doc) {
444 orientation = gs->doc->pages[page].orientation;
446 if (orientation == GTK_GS_ORIENTATION_NONE) {
447 orientation = gs->doc->default_page_orientation;
449 if (orientation == GTK_GS_ORIENTATION_NONE) {
450 orientation = gs->doc->orientation;
452 if (orientation == GTK_GS_ORIENTATION_NONE) {
453 orientation = GTK_GS_ORIENTATION_PORTRAIT;
455 if (gs->orientation != GTK_GS_ORIENTATION_NONE) {
456 orientation = gs->orientation;
463 setup_page (PSDocument *gs, int page, double scale)
466 int urx, ury, llx, lly, orientation;
467 char scaled_xdpi[G_ASCII_DTOSTR_BUF_SIZE];
468 char scaled_ydpi[G_ASCII_DTOSTR_BUF_SIZE];
470 LOG ("Setup the page");
472 get_page_box (gs, page, &urx, &ury, &llx, &lly);
473 orientation = get_page_orientation (gs, page);
474 g_ascii_dtostr (scaled_xdpi, G_ASCII_DTOSTR_BUF_SIZE, get_xdpi (gs) * scale);
475 g_ascii_dtostr (scaled_ydpi, G_ASCII_DTOSTR_BUF_SIZE, get_ydpi (gs) * scale);
477 buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
478 0L, orientation * 90, llx, lly, urx, ury,
479 scaled_xdpi, scaled_ydpi,
481 LOG ("GS property %s", buf);
483 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
484 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
491 close_pipe (int p[2])
502 is_interpreter_ready (PSDocument *gs)
504 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
508 output (gpointer data, gint source, GdkInputCondition condition)
510 char buf[MAX_BUFSIZE + 1];
512 PSDocument *gs = PS_DOCUMENT(data);
514 if (source == gs->interpreter_output) {
515 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
516 if (bytes == 0) { /* EOF occurred */
517 close (gs->interpreter_output);
518 gs->interpreter_output = -1;
519 gdk_input_remove (gs->interpreter_output_id);
521 } else if (bytes == -1) {
523 interpreter_failed (gs, NULL);
526 if (gs->interpreter_err == -1) {
527 interpreter_failed (gs, NULL);
529 } else if (source == gs->interpreter_err) {
530 bytes = read (gs->interpreter_err, buf, MAX_BUFSIZE);
531 if (bytes == 0) { /* EOF occurred */
532 close (gs->interpreter_err);
533 gs->interpreter_err = -1;
534 gdk_input_remove (gs->interpreter_error_id);
536 } else if (bytes == -1) {
538 interpreter_failed (gs, NULL);
541 if (gs->interpreter_output == -1) {
542 interpreter_failed(gs, NULL);
559 input(gpointer data, gint source, GdkInputCondition condition)
561 PSDocument *gs = PS_DOCUMENT(data);
563 void (*oldsig) (int);
564 oldsig = signal(SIGPIPE, catchPipe);
569 if (gs->buffer_bytes_left == 0) {
570 /* Get a new section if required */
571 if (gs->ps_input && gs->bytes_left == 0) {
572 struct record_list *ps_old = gs->ps_input;
573 gs->ps_input = ps_old->next;
574 if (ps_old->close && NULL != ps_old->fp)
579 /* Have to seek at the beginning of each section */
580 if (gs->ps_input && gs->ps_input->seek_needed) {
581 fseek (gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
582 gs->ps_input->seek_needed = FALSE;
583 gs->bytes_left = gs->ps_input->len;
586 if (gs->bytes_left > MAX_BUFSIZE) {
587 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
588 MAX_BUFSIZE, gs->ps_input->fp);
589 } else if (gs->bytes_left > 0) {
590 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
591 gs->bytes_left, gs->ps_input->fp);
593 gs->buffer_bytes_left = 0;
595 if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
596 interpreter_failed (gs, NULL); /* Error occurred */
598 gs->input_buffer_ptr = gs->input_buffer;
599 gs->bytes_left -= gs->buffer_bytes_left;
602 if (gs->buffer_bytes_left > 0) {
603 bytes_written = write (gs->interpreter_input,
604 gs->input_buffer_ptr, gs->buffer_bytes_left);
607 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
609 interpreter_failed (gs, NULL);
610 } else if (bytes_written == -1) {
611 if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
612 interpreter_failed (gs, NULL); /* Something bad happened */
615 gs->buffer_bytes_left -= bytes_written;
616 gs->input_buffer_ptr += bytes_written;
619 } while (gs->ps_input && gs->buffer_bytes_left == 0);
621 signal (SIGPIPE, oldsig);
623 if (gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
624 if (gs->interpreter_input_id != 0) {
625 gdk_input_remove (gs->interpreter_input_id);
626 gs->interpreter_input_id = 0;
632 start_interpreter (PSDocument *gs)
634 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
635 int std_out[2]; /* pipe from interp stdout */
636 int std_err[2]; /* pipe from interp stderr */
639 #define NUM_GS_ARGS (NUM_ARGS - 20)
640 #define NUM_ALPHA_ARGS 10
642 char *argv[NUM_ARGS], *dir, *gv_env;
643 char **gs_args, **alpha_args = NULL;
646 LOG ("Start the interpreter");
651 stop_interpreter(gs);
653 /* set up the args... */
654 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
655 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
656 argv[argc] = gs_args[i];
659 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
660 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
661 argv[argc] = alpha_args[i];
664 argv[argc++] = "-dNOPAUSE";
665 argv[argc++] = "-dQUIET";
666 argv[argc++] = "-dSAFER";
668 /* set up the pipes */
669 if (gs->send_filename_to_gs) {
670 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
672 argv[argc++] = "quit";
679 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
680 if (pipe (std_in) == -1) {
681 g_critical ("Unable to open pipe to Ghostscript.");
686 if (pipe (std_out) == -1) {
691 if (pipe(std_err) == -1) {
693 close_pipe (std_out);
697 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
698 gdk_x11_drawable_get_xid (gs->pstarget),
699 gdk_x11_drawable_get_xid (gs->bpixmap));
700 LOG ("Launching ghostview with env %s", gv_env);
702 gs->interpreter_pid = fork ();
703 switch (gs->interpreter_pid) {
706 close_pipe (std_out);
707 close_pipe (std_err);
712 dup2 (std_out[1], 1);
716 dup2 (std_err[1], 2);
719 if (!gs->reading_from_pipe) {
720 if (gs->send_filename_to_gs) {
722 /* just in case gs tries to read from stdin */
723 stdinfd = open("/dev/null", O_RDONLY);
737 /* change to directory where the input file is. This helps
738 * with postscript-files which include other files using
740 dir = g_path_get_dirname (gs->gs_filename);
744 execvp (argv[0], argv);
747 g_critical ("Unable to execute [%s]\n", argv[0]);
748 g_strfreev (gs_args);
750 g_strfreev (alpha_args);
753 default: /* parent */
754 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
757 /* use non-blocking IO for pipe to ghostscript */
758 result = fcntl (std_in[1], F_GETFL, 0);
759 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
760 gs->interpreter_input = std_in[1];
762 gs->interpreter_input = -1;
766 gs->interpreter_output = std_out[0];
768 gs->interpreter_err = std_err[0];
769 gs->interpreter_output_id =
770 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
771 gs->interpreter_error_id =
772 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
780 stop_interpreter(PSDocument * gs)
782 if (gs->interpreter_pid > 0) {
784 LOG ("Stop the interpreter");
785 kill (gs->interpreter_pid, SIGTERM);
786 while ((wait(&status) == -1) && (errno == EINTR));
787 gs->interpreter_pid = -1;
789 gs->gs_status = _("Interpreter failed.");
793 if (gs->interpreter_input >= 0) {
794 close (gs->interpreter_input);
795 gs->interpreter_input = -1;
796 if (gs->interpreter_input_id != 0) {
797 gdk_input_remove(gs->interpreter_input_id);
798 gs->interpreter_input_id = 0;
800 while (gs->ps_input) {
801 struct record_list *ps_old = gs->ps_input;
802 gs->ps_input = gs->ps_input->next;
803 if (ps_old->close && NULL != ps_old->fp)
809 if (gs->interpreter_output >= 0) {
810 close (gs->interpreter_output);
811 gs->interpreter_output = -1;
812 if (gs->interpreter_output_id) {
813 gdk_input_remove (gs->interpreter_output_id);
814 gs->interpreter_output_id = 0;
818 if (gs->interpreter_err >= 0) {
819 close (gs->interpreter_err);
820 gs->interpreter_err = -1;
821 if (gs->interpreter_error_id) {
822 gdk_input_remove (gs->interpreter_error_id);
823 gs->interpreter_error_id = 0;
830 /* If file exists and is a regular file then return its length, else -1 */
832 file_length (const gchar * filename)
834 struct stat stat_rec;
836 if (filename && (stat (filename, &stat_rec) == 0) && S_ISREG (stat_rec.st_mode))
837 return stat_rec.st_size;
842 /* Test if file exists, is a regular file and its length is > 0 */
844 file_readable(const char *filename)
846 return (file_length (filename) > 0);
850 * Decompress gs->gs_filename if necessary
851 * Set gs->filename_unc to the name of the uncompressed file or NULL.
852 * Error reporting via signal 'interpreter_message'
853 * Return name of input file to use or NULL on error..
856 check_filecompressed (PSDocument * gs)
860 gchar *filename, *filename_unc, *filename_err, *cmdline;
866 if ((file = fopen(gs->gs_filename, "r")) &&
867 (fread (buf, sizeof(gchar), 3, file) == 3)) {
868 if ((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
869 /* file is gzipped or compressed */
870 cmd = gtk_gs_defaults_get_ungzip_cmd ();
871 } else if (strncmp (buf, "BZh", 3) == 0) {
872 /* file is compressed with bzip2 */
873 cmd = gtk_gs_defaults_get_unbzip2_cmd ();
881 return gs->gs_filename;
883 /* do the decompression */
884 filename = g_shell_quote (gs->gs_filename);
885 filename_unc = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
886 if ((fd = mkstemp (filename_unc)) < 0) {
887 g_free (filename_unc);
893 filename_err = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
894 if ((fd = mkstemp(filename_err)) < 0) {
895 g_free (filename_err);
896 g_free (filename_unc);
902 cmdline = g_strdup_printf ("%s %s >%s 2>%s", cmd,
903 filename, filename_unc, filename_err);
904 if (system (cmdline) == 0 &&
905 file_readable (filename_unc) &&
906 file_length (filename_err) == 0) {
907 /* sucessfully uncompressed file */
908 gs->gs_filename_unc = filename_unc;
914 filename_dsp = g_filename_display_name (gs->gs_filename);
915 msg = g_strdup_printf (_("Error while decompressing file %s:\n"), filename_dsp);
916 g_free (filename_dsp);
918 interpreter_failed (gs, msg);
920 unlink (filename_unc);
921 g_free (filename_unc);
925 unlink (filename_err);
926 g_free (filename_err);
934 ps_document_enable_interpreter(PSDocument *gs)
936 g_return_val_if_fail (PS_IS_DOCUMENT (gs), FALSE);
938 if (!gs->gs_filename)
941 return start_interpreter (gs);
945 document_load (PSDocument *gs, const gchar *fname)
947 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
949 LOG ("Load the document");
956 /* prepare this document */
957 gs->structured_doc = FALSE;
958 gs->send_filename_to_gs = TRUE;
959 gs->gs_filename = g_strdup (fname);
961 if ((gs->reading_from_pipe = (strcmp (fname, "-") == 0))) {
962 gs->send_filename_to_gs = FALSE;
965 * We need to make sure that the file is loadable/exists!
966 * otherwise we want to exit without loading new stuff...
968 gchar *filename = NULL;
970 if (!file_readable(fname)) {
974 filename_dsp = g_filename_display_name (fname);
975 msg = g_strdup_printf (_("Cannot open file %s.\n"), filename_dsp);
976 g_free (filename_dsp);
978 interpreter_failed (gs, msg);
980 gs->gs_status = _("File is not readable.");
982 filename = check_filecompressed(gs);
985 if (!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
986 interpreter_failed (gs, NULL);
990 /* we grab the vital statistics!!! */
991 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
993 if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
994 (gs->doc->epsf && gs->doc->numpages > 1)) {
995 gs->structured_doc = TRUE;
996 gs->send_filename_to_gs = FALSE;
1000 gs->gs_status = _("Document loaded.");
1006 ps_document_next_page (PSDocument *gs)
1010 LOG ("Make ghostscript render next page");
1012 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
1013 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
1014 g_return_val_if_fail (gs->busy != TRUE, FALSE);
1018 event.xclient.type = ClientMessage;
1019 event.xclient.display = gdk_display;
1020 event.xclient.window = gs->message_window;
1021 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1022 event.xclient.format = 32;
1024 gdk_error_trap_push ();
1025 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1027 gdk_error_trap_pop ();
1033 render_page (PSDocument *gs, int page)
1035 g_return_val_if_fail(gs != NULL, FALSE);
1036 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1038 if(!gs->gs_filename) {
1042 if (gs->structured_doc && gs->doc) {
1043 LOG ("It's a structured document, let's send one page to gs");
1045 if (is_interpreter_ready (gs)) {
1046 ps_document_next_page (gs);
1048 ps_document_enable_interpreter (gs);
1049 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1050 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1053 send_ps (gs, gs->doc->pages[page].begin,
1054 gs->doc->pages[page].len, FALSE);
1056 /* Unstructured document
1058 * In the case of non structured documents,
1059 * GS read the PS from the actual file (via command
1060 * line. Hence, ggv only send a signal next page.
1061 * If ghostview is not running it is usually because
1062 * the last page of the file was displayed. In that
1063 * case, ggv restarts GS again and the first page is displayed.
1066 LOG ("It's an unstructured document, gs will just read the file");
1068 if (!is_interpreter_ready (gs)) {
1069 ps_document_enable_interpreter(gs);
1071 ps_document_next_page(gs);
1078 ps_document_load (EvDocument *document,
1085 filename = g_filename_from_uri (uri, NULL, error);
1089 result = document_load (PS_DOCUMENT (document), filename);
1091 gchar *filename_dsp;
1093 filename_dsp = g_filename_display_name (filename);
1095 g_set_error (error, G_FILE_ERROR,
1096 G_FILE_ERROR_FAILED,
1097 _("Failed to load document '%s'"),
1100 g_free (filename_dsp);
1109 save_page_list (PSDocument *document, int *page_list, const char *filename)
1111 gboolean result = TRUE;
1112 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1116 pscopydoc (sink, PS_DOCUMENT_GET_PS_FILE(document),
1117 document->doc, page_list);
1119 buf = gtk_gs_doc_sink_get_buffer (sink);
1121 f = fopen (filename, "w");
1130 gtk_gs_doc_sink_free (sink);
1137 ps_document_save (EvDocument *document,
1141 PSDocument *ps = PS_DOCUMENT (document);
1147 filename = g_filename_from_uri (uri, NULL, error);
1151 page_list = g_new0 (int, ps->doc->numpages);
1152 for (i = 0; i < ps->doc->numpages; i++) {
1156 result = save_page_list (ps, page_list, filename);
1165 ps_document_get_n_pages (EvDocument *document)
1167 PSDocument *ps = PS_DOCUMENT (document);
1169 g_return_val_if_fail (ps != NULL, -1);
1171 if (!ps->gs_filename || !ps->doc) {
1175 return ps->structured_doc ? ps->doc->numpages : 1;
1179 ps_document_get_page_size (EvDocument *document,
1184 PSDocument *gs = PS_DOCUMENT (document);
1186 int urx, ury, llx, lly, orientation;
1188 get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
1189 orientation = get_page_orientation (PS_DOCUMENT (document), page);
1191 switch (orientation) {
1192 case GTK_GS_ORIENTATION_PORTRAIT:
1193 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1194 w = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
1195 h = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
1197 case GTK_GS_ORIENTATION_LANDSCAPE:
1198 case GTK_GS_ORIENTATION_SEASCAPE:
1199 w = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
1200 h = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
1204 g_assert_not_reached ();
1218 ps_document_can_get_text (EvDocument *document)
1224 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer, int page, double scale)
1226 PSDocument *gs = PS_DOCUMENT (renderer);
1228 if (gs->pstarget == NULL) {
1229 gs->target_window = gtk_window_new (GTK_WINDOW_POPUP);
1230 gtk_widget_realize (gs->target_window);
1231 gs->pstarget = gs->target_window->window;
1233 g_assert (gs->pstarget != NULL);
1235 g_signal_connect (gs->target_window, "event",
1236 G_CALLBACK (ps_document_widget_event),
1240 setup_pixmap (gs, page, scale);
1241 setup_page (gs, page, scale);
1243 render_page (gs, page);
1246 static EvDocumentInfo *
1247 ps_document_get_info (EvDocument *document)
1249 EvDocumentInfo *info;
1250 PSDocument *ps = PS_DOCUMENT (document);
1252 info = g_new0 (EvDocumentInfo, 1);
1253 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
1254 EV_DOCUMENT_INFO_FORMAT |
1255 EV_DOCUMENT_INFO_CREATOR |
1256 EV_DOCUMENT_INFO_N_PAGES;
1257 info->title = g_strdup (ps->doc->title);
1258 info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
1259 : g_strdup (_("PostScript"));
1260 info->creator = g_strdup (ps->doc->creator);
1261 info->n_pages = ev_document_get_n_pages (document);
1267 ps_document_document_iface_init (EvDocumentIface *iface)
1269 iface->load = ps_document_load;
1270 iface->save = ps_document_save;
1271 iface->can_get_text = ps_document_can_get_text;
1272 iface->get_n_pages = ps_document_get_n_pages;
1273 iface->get_page_size = ps_document_get_page_size;
1274 iface->get_info = ps_document_get_info;
1278 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
1280 iface->render_pixbuf = ps_async_renderer_render_pixbuf;
1284 ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
1285 int first_page, int last_page,
1286 double width, double height, gboolean duplex)
1288 PSDocument *document = PS_DOCUMENT (exporter);
1290 g_free (document->ps_export_pagelist);
1292 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1293 document->ps_export_filename = g_strdup (filename);
1297 ps_document_ps_export_do_page (EvPSExporter *exporter, EvRenderContext *rc)
1299 PSDocument *document = PS_DOCUMENT (exporter);
1301 document->ps_export_pagelist[rc->page] = 1;
1305 ps_document_ps_export_end (EvPSExporter *exporter)
1307 PSDocument *document = PS_DOCUMENT (exporter);
1309 save_page_list (document, document->ps_export_pagelist,
1310 document->ps_export_filename);
1312 g_free (document->ps_export_pagelist);
1313 g_free (document->ps_export_filename);
1314 document->ps_export_pagelist = NULL;
1315 document->ps_export_filename = NULL;
1319 ps_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1321 iface->begin = ps_document_ps_export_begin;
1322 iface->do_page = ps_document_ps_export_do_page;
1323 iface->end = ps_document_ps_export_end;