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>
50 #include "ps-document.h"
52 #include "gsdefaults.h"
53 #include "ev-ps-exporter.h"
54 #include "ev-async-renderer.h"
56 #define MAX_BUFSIZE 1024
58 #define PS_DOCUMENT_IS_COMPRESSED(gs) (PS_DOCUMENT(gs)->gs_filename_unc != NULL)
59 #define PS_DOCUMENT_GET_PS_FILE(gs) (PS_DOCUMENT_IS_COMPRESSED(gs) ? \
60 PS_DOCUMENT(gs)->gs_filename_unc : \
61 PS_DOCUMENT(gs)->gs_filename)
63 /* structure to describe section of file to send to ghostscript */
71 struct record_list *next;
74 static gboolean broken_pipe = FALSE;
76 /* Forward declarations */
77 static void ps_document_init (PSDocument *gs);
78 static void ps_document_class_init (PSDocumentClass *klass);
79 static void send_ps (PSDocument *gs,
83 static void output (gpointer data,
85 GdkInputCondition condition);
86 static void input (gpointer data,
88 GdkInputCondition condition);
89 static void stop_interpreter (PSDocument *gs);
90 static gint start_interpreter (PSDocument *gs);
91 static void ps_document_document_iface_init (EvDocumentIface *iface);
92 static void ps_document_ps_exporter_iface_init (EvPSExporterIface *iface);
93 static void ps_async_renderer_iface_init (EvAsyncRendererIface *iface);
95 G_DEFINE_TYPE_WITH_CODE (PSDocument, ps_document, G_TYPE_OBJECT,
97 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
98 ps_document_document_iface_init);
99 G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
100 ps_document_ps_exporter_iface_init);
101 G_IMPLEMENT_INTERFACE (EV_TYPE_ASYNC_RENDERER,
102 ps_async_renderer_iface_init);
105 static GObjectClass *parent_class = NULL;
106 static PSDocumentClass *gs_class = NULL;
109 ps_document_init (PSDocument *gs)
113 gs->interpreter_pid = -1;
117 gs->gs_filename_unc = 0;
121 gs->structured_doc = FALSE;
122 gs->reading_from_pipe = FALSE;
123 gs->send_filename_to_gs = FALSE;
127 gs->interpreter_input = -1;
128 gs->interpreter_output = -1;
129 gs->interpreter_err = -1;
130 gs->interpreter_input_id = 0;
131 gs->interpreter_output_id = 0;
132 gs->interpreter_error_id = 0;
135 gs->input_buffer = NULL;
136 gs->input_buffer_ptr = NULL;
138 gs->buffer_bytes_left = 0;
140 gs->gs_status = _("No document loaded.");
142 gs->ps_export_pagelist = NULL;
143 gs->ps_export_filename = NULL;
145 gs->orientation = GTK_GS_ORIENTATION_NONE;
149 ps_document_dispose (GObject *object)
151 PSDocument *gs = PS_DOCUMENT (object);
153 g_return_if_fail (gs != NULL);
156 fclose (gs->gs_psfile);
157 gs->gs_psfile = NULL;
160 if (gs->gs_filename) {
161 g_free (gs->gs_filename);
162 gs->gs_filename = NULL;
170 if (gs->gs_filename_unc) {
171 unlink(gs->gs_filename_unc);
172 g_free(gs->gs_filename_unc);
173 gs->gs_filename_unc = NULL;
177 gdk_drawable_unref (gs->bpixmap);
180 if(gs->input_buffer) {
181 g_free(gs->input_buffer);
182 gs->input_buffer = NULL;
185 stop_interpreter (gs);
187 G_OBJECT_CLASS (parent_class)->dispose (object);
191 ps_document_class_init(PSDocumentClass *klass)
193 GObjectClass *object_class;
195 object_class = (GObjectClass *) klass;
196 parent_class = g_type_class_peek_parent (klass);
199 object_class->dispose = ps_document_dispose;
201 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
202 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
203 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
204 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
208 push_pixbuf (PSDocument *gs)
214 cmap = gdk_window_get_colormap (gs->pstarget);
215 gdk_drawable_get_size (gs->bpixmap, &width, &height);
216 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
219 g_signal_emit_by_name (gs, "render_finished", pixbuf);
220 g_object_unref (pixbuf);
224 interpreter_failed (PSDocument *gs, char *msg)
226 LOG ("Interpreter failed %s", msg);
230 stop_interpreter (gs);
234 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
236 PSDocument *gs = (PSDocument *) data;
238 if(event->type != GDK_CLIENT_EVENT)
241 gs->message_window = event->client.data.l[0];
243 if (event->client.message_type == gs_class->page_atom) {
244 LOG ("GS rendered the document");
254 send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
256 struct record_list *ps_new;
258 if (gs->interpreter_input < 0) {
259 g_critical("No pipe to gs: error in send_ps().");
263 ps_new = g_new0 (struct record_list, 1);
264 ps_new->fp = gs->gs_psfile;
265 ps_new->begin = begin;
267 ps_new->seek_needed = TRUE;
268 ps_new->close = close;
271 if (gs->input_buffer == NULL) {
272 gs->input_buffer = g_malloc(MAX_BUFSIZE);
275 if (gs->ps_input == NULL) {
276 gs->input_buffer_ptr = gs->input_buffer;
277 gs->bytes_left = len;
278 gs->buffer_bytes_left = 0;
279 gs->ps_input = ps_new;
280 gs->interpreter_input_id = gdk_input_add
281 (gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
283 struct record_list *p = gs->ps_input;
284 while (p->next != NULL) {
292 get_xdpi (PSDocument *gs)
294 return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
298 get_ydpi (PSDocument *gs)
300 return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
304 setup_pixmap (PSDocument *gs, int page, double scale)
307 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
308 GdkColormap *colormap;
309 double width, height;
310 int pixmap_width, pixmap_height;
312 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
313 pixmap_width = width * scale + 0.5;
314 pixmap_height = height * scale + 0.5;
319 gdk_drawable_get_size (gs->bpixmap, &w, &h);
321 if (pixmap_width != w || h != pixmap_height) {
322 gdk_drawable_unref (gs->bpixmap);
324 stop_interpreter (gs);
329 LOG ("Create pixmap");
331 fill = gdk_gc_new (gs->pstarget);
332 colormap = gdk_drawable_get_colormap (gs->pstarget);
333 gdk_color_alloc (colormap, &white);
334 gdk_gc_set_foreground (fill, &white);
335 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
337 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
338 0, 0, pixmap_width, pixmap_height);
342 #define DEFAULT_PAGE_SIZE 1
345 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
351 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
352 int new_pagesize = -1;
354 g_return_if_fail (PS_IS_DOCUMENT (gs));
356 if (new_pagesize == -1) {
357 new_pagesize = DEFAULT_PAGE_SIZE;
359 /* If we have a document:
360 * We use -- the page size (if specified)
361 * or the doc. size (if specified)
362 * or the page bbox (if specified)
363 * or the bounding box
365 if ((page >= 0) && (gs->doc->numpages > page) &&
366 (gs->doc->pages) && (gs->doc->pages[page].size)) {
367 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
368 } else if (gs->doc->default_page_size != NULL) {
369 new_pagesize = gs->doc->default_page_size - gs->doc->size;
370 } else if ((page >= 0) &&
371 (gs->doc->numpages > page) &&
373 (gs->doc->pages[page].boundingbox[URX] >
374 gs->doc->pages[page].boundingbox[LLX]) &&
375 (gs->doc->pages[page].boundingbox[URY] >
376 gs->doc->pages[page].boundingbox[LLY])) {
378 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
379 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
385 /* Compute bounding box */
386 if (gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
389 (gs->doc->pages[page].boundingbox[URX] >
390 gs->doc->pages[page].boundingbox[LLX]) &&
391 (gs->doc->pages[page].boundingbox[URY] >
392 gs->doc->pages[page].boundingbox[LLY])) {
394 new_llx = gs->doc->pages[page].boundingbox[LLX];
395 new_lly = gs->doc->pages[page].boundingbox[LLY];
396 new_urx = gs->doc->pages[page].boundingbox[URX];
397 new_ury = gs->doc->pages[page].boundingbox[URY];
398 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
399 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
401 new_llx = gs->doc->boundingbox[LLX];
402 new_lly = gs->doc->boundingbox[LLY];
403 new_urx = gs->doc->boundingbox[URX];
404 new_ury = gs->doc->boundingbox[URY];
407 if (new_pagesize < 0)
408 new_pagesize = DEFAULT_PAGE_SIZE;
409 new_llx = new_lly = 0;
410 if (gs->doc && gs->doc->size &&
411 (new_pagesize < gs->doc->numsizes)) {
412 new_urx = gs->doc->size[new_pagesize].width;
413 new_ury = gs->doc->size[new_pagesize].height;
415 new_urx = papersizes[new_pagesize].width;
416 new_ury = papersizes[new_pagesize].height;
420 if (new_urx <= new_llx)
421 new_urx = papersizes[12].width;
422 if (new_ury <= new_lly)
423 new_ury = papersizes[12].height;
432 get_page_orientation (PSDocument *gs, int page)
436 orientation = GTK_GS_ORIENTATION_NONE;
438 if (gs->structured_doc) {
439 orientation = gs->doc->pages[page].orientation;
441 if (orientation == GTK_GS_ORIENTATION_NONE) {
442 orientation = gs->doc->default_page_orientation;
444 if (orientation == GTK_GS_ORIENTATION_NONE) {
445 orientation = gs->doc->orientation;
447 if (orientation == GTK_GS_ORIENTATION_NONE) {
448 orientation = GTK_GS_ORIENTATION_PORTRAIT;
450 if (gs->orientation != GTK_GS_ORIENTATION_NONE) {
451 orientation = gs->orientation;
458 setup_page (PSDocument *gs, int page, double scale)
461 int urx, ury, llx, lly, orientation;
466 LOG ("Setup the page");
469 /* gs needs floating point parameters with '.' as decimal point
470 * while some (european) locales use ',' instead, so we set the
471 * locale for this snprintf to "C".
473 savelocale = setlocale (LC_NUMERIC, "C");
475 get_page_box (gs, page, &urx, &ury, &llx, &lly);
476 orientation = get_page_orientation (gs, page);
478 g_snprintf (buf, 1024, "%ld %d %d %d %d %d %f %f %d %d %d %d",
479 0L, orientation * 90, llx, lly, urx, ury,
480 get_xdpi (gs) * scale,
481 get_ydpi (gs) * scale,
483 LOG ("GS property %s", buf);
486 setlocale(LC_NUMERIC, savelocale);
488 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
489 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
494 close_pipe (int p[2])
505 is_interpreter_ready (PSDocument *gs)
507 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
511 output (gpointer data, gint source, GdkInputCondition condition)
513 char buf[MAX_BUFSIZE + 1];
515 PSDocument *gs = PS_DOCUMENT(data);
517 if (source == gs->interpreter_output) {
518 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
519 if (bytes == 0) { /* EOF occurred */
520 close (gs->interpreter_output);
521 gs->interpreter_output = -1;
522 gdk_input_remove (gs->interpreter_output_id);
524 } else if (bytes == -1) {
526 interpreter_failed (gs, NULL);
529 if (gs->interpreter_err == -1) {
530 interpreter_failed (gs, NULL);
532 } else if (source == gs->interpreter_err) {
533 bytes = read (gs->interpreter_err, buf, MAX_BUFSIZE);
534 if (bytes == 0) { /* EOF occurred */
535 close (gs->interpreter_err);
536 gs->interpreter_err = -1;
537 gdk_input_remove (gs->interpreter_error_id);
539 } else if (bytes == -1) {
541 interpreter_failed (gs, NULL);
544 if (gs->interpreter_output == -1) {
545 interpreter_failed(gs, NULL);
562 input(gpointer data, gint source, GdkInputCondition condition)
564 PSDocument *gs = PS_DOCUMENT(data);
566 void (*oldsig) (int);
567 oldsig = signal(SIGPIPE, catchPipe);
572 if (gs->buffer_bytes_left == 0) {
573 /* Get a new section if required */
574 if (gs->ps_input && gs->bytes_left == 0) {
575 struct record_list *ps_old = gs->ps_input;
576 gs->ps_input = ps_old->next;
577 if (ps_old->close && NULL != ps_old->fp)
582 /* Have to seek at the beginning of each section */
583 if (gs->ps_input && gs->ps_input->seek_needed) {
584 fseek (gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
585 gs->ps_input->seek_needed = FALSE;
586 gs->bytes_left = gs->ps_input->len;
589 if (gs->bytes_left > MAX_BUFSIZE) {
590 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
591 MAX_BUFSIZE, gs->ps_input->fp);
592 } else if (gs->bytes_left > 0) {
593 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
594 gs->bytes_left, gs->ps_input->fp);
596 gs->buffer_bytes_left = 0;
598 if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
599 interpreter_failed (gs, NULL); /* Error occurred */
601 gs->input_buffer_ptr = gs->input_buffer;
602 gs->bytes_left -= gs->buffer_bytes_left;
605 if (gs->buffer_bytes_left > 0) {
606 bytes_written = write (gs->interpreter_input,
607 gs->input_buffer_ptr, gs->buffer_bytes_left);
610 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
612 interpreter_failed (gs, NULL);
613 } else if (bytes_written == -1) {
614 if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
615 interpreter_failed (gs, NULL); /* Something bad happened */
618 gs->buffer_bytes_left -= bytes_written;
619 gs->input_buffer_ptr += bytes_written;
622 } while (gs->ps_input && gs->buffer_bytes_left == 0);
624 signal (SIGPIPE, oldsig);
626 if (gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
627 if (gs->interpreter_input_id != 0) {
628 gdk_input_remove (gs->interpreter_input_id);
629 gs->interpreter_input_id = 0;
635 start_interpreter (PSDocument *gs)
637 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
638 int std_out[2]; /* pipe from interp stdout */
639 int std_err[2]; /* pipe from interp stderr */
642 #define NUM_GS_ARGS (NUM_ARGS - 20)
643 #define NUM_ALPHA_ARGS 10
645 char *argv[NUM_ARGS], *dir, *gv_env;
646 char **gs_args, **alpha_args = NULL;
649 LOG ("Start the interpreter");
654 stop_interpreter(gs);
656 /* set up the args... */
657 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
658 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
659 argv[argc] = gs_args[i];
662 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
663 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
664 argv[argc] = alpha_args[i];
667 argv[argc++] = "-dNOPAUSE";
668 argv[argc++] = "-dQUIET";
669 argv[argc++] = "-dSAFER";
671 /* set up the pipes */
672 if (gs->send_filename_to_gs) {
673 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
675 argv[argc++] = "quit";
682 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
683 if (pipe (std_in) == -1) {
684 g_critical ("Unable to open pipe to Ghostscript.");
689 if (pipe (std_out) == -1) {
694 if (pipe(std_err) == -1) {
696 close_pipe (std_out);
700 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
701 gdk_x11_drawable_get_xid (gs->pstarget),
702 gdk_x11_drawable_get_xid (gs->bpixmap));
703 LOG ("Launching ghostview with env %s", gv_env);
705 gs->interpreter_pid = fork ();
706 switch (gs->interpreter_pid) {
709 close_pipe (std_out);
710 close_pipe (std_err);
715 dup2 (std_out[1], 1);
719 dup2 (std_err[1], 2);
722 if (!gs->reading_from_pipe) {
723 if (gs->send_filename_to_gs) {
725 /* just in case gs tries to read from stdin */
726 stdinfd = open("/dev/null", O_RDONLY);
740 /* change to directory where the input file is. This helps
741 * with postscript-files which include other files using
743 dir = g_path_get_dirname (gs->gs_filename);
747 execvp (argv[0], argv);
750 g_critical ("Unable to execute [%s]\n", argv[0]);
751 g_strfreev (gs_args);
753 g_strfreev (alpha_args);
756 default: /* parent */
757 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
760 /* use non-blocking IO for pipe to ghostscript */
761 result = fcntl (std_in[1], F_GETFL, 0);
762 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
763 gs->interpreter_input = std_in[1];
765 gs->interpreter_input = -1;
769 gs->interpreter_output = std_out[0];
771 gs->interpreter_err = std_err[0];
772 gs->interpreter_output_id =
773 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
774 gs->interpreter_error_id =
775 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
783 stop_interpreter(PSDocument * gs)
785 if (gs->interpreter_pid > 0) {
787 LOG ("Stop the interpreter");
788 kill (gs->interpreter_pid, SIGTERM);
789 while ((wait(&status) == -1) && (errno == EINTR));
790 gs->interpreter_pid = -1;
792 gs->gs_status = _("Interpreter failed.");
796 if (gs->interpreter_input >= 0) {
797 close (gs->interpreter_input);
798 gs->interpreter_input = -1;
799 if (gs->interpreter_input_id != 0) {
800 gdk_input_remove(gs->interpreter_input_id);
801 gs->interpreter_input_id = 0;
803 while (gs->ps_input) {
804 struct record_list *ps_old = gs->ps_input;
805 gs->ps_input = gs->ps_input->next;
806 if (ps_old->close && NULL != ps_old->fp)
812 if (gs->interpreter_output >= 0) {
813 close (gs->interpreter_output);
814 gs->interpreter_output = -1;
815 if (gs->interpreter_output_id) {
816 gdk_input_remove (gs->interpreter_output_id);
817 gs->interpreter_output_id = 0;
821 if (gs->interpreter_err >= 0) {
822 close (gs->interpreter_err);
823 gs->interpreter_err = -1;
824 if (gs->interpreter_error_id) {
825 gdk_input_remove (gs->interpreter_error_id);
826 gs->interpreter_error_id = 0;
833 /* If file exists and is a regular file then return its length, else -1 */
835 file_length (const gchar * filename)
837 struct stat stat_rec;
839 if (filename && (stat (filename, &stat_rec) == 0) && S_ISREG (stat_rec.st_mode))
840 return stat_rec.st_size;
845 /* Test if file exists, is a regular file and its length is > 0 */
847 file_readable(const char *filename)
849 return (file_length (filename) > 0);
853 * Decompress gs->gs_filename if necessary
854 * Set gs->filename_unc to the name of the uncompressed file or NULL.
855 * Error reporting via signal 'interpreter_message'
856 * Return name of input file to use or NULL on error..
859 check_filecompressed (PSDocument * gs)
863 gchar *filename, *filename_unc, *filename_err, *cmdline;
869 if ((file = fopen(gs->gs_filename, "r")) &&
870 (fread (buf, sizeof(gchar), 3, file) == 3)) {
871 if ((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
872 /* file is gzipped or compressed */
873 cmd = gtk_gs_defaults_get_ungzip_cmd ();
874 } else if (strncmp (buf, "BZh", 3) == 0) {
875 /* file is compressed with bzip2 */
876 cmd = gtk_gs_defaults_get_unbzip2_cmd ();
884 return gs->gs_filename;
886 /* do the decompression */
887 filename = g_shell_quote (gs->gs_filename);
888 filename_unc = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
889 if ((fd = mkstemp (filename_unc)) < 0) {
890 g_free (filename_unc);
896 filename_err = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
897 if ((fd = mkstemp(filename_err)) < 0) {
898 g_free (filename_err);
899 g_free (filename_unc);
905 cmdline = g_strdup_printf ("%s %s >%s 2>%s", cmd,
906 filename, filename_unc, filename_err);
907 if (system (cmdline) == 0 &&
908 file_readable (filename_unc) &&
909 file_length (filename_err) == 0) {
910 /* sucessfully uncompressed file */
911 gs->gs_filename_unc = filename_unc;
914 g_snprintf (buf, 1024, _("Error while decompressing file %s:\n"),
916 interpreter_failed (gs, buf);
917 unlink (filename_unc);
918 g_free (filename_unc);
922 unlink (filename_err);
923 g_free (filename_err);
931 ps_document_enable_interpreter(PSDocument *gs)
933 g_return_val_if_fail (PS_IS_DOCUMENT (gs), FALSE);
935 if (!gs->gs_filename)
938 return start_interpreter (gs);
942 document_load (PSDocument *gs, const gchar *fname)
944 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
946 LOG ("Load the document");
953 /* prepare this document */
954 gs->structured_doc = FALSE;
955 gs->send_filename_to_gs = TRUE;
956 gs->gs_filename = g_strdup (fname);
958 if ((gs->reading_from_pipe = (strcmp (fname, "-") == 0))) {
959 gs->send_filename_to_gs = FALSE;
962 * We need to make sure that the file is loadable/exists!
963 * otherwise we want to exit without loading new stuff...
965 gchar *filename = NULL;
967 if (!file_readable(fname)) {
970 g_snprintf (buf, 1024, _("Cannot open file %s.\n"), fname);
971 interpreter_failed (gs, buf);
972 gs->gs_status = _("File is not readable.");
974 filename = check_filecompressed(gs);
977 if (!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
978 interpreter_failed (gs, NULL);
982 /* we grab the vital statistics!!! */
983 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
985 if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
986 (gs->doc->epsf && gs->doc->numpages > 1)) {
987 gs->structured_doc = TRUE;
988 gs->send_filename_to_gs = FALSE;
992 gs->gs_status = _("Document loaded.");
998 ps_document_next_page (PSDocument *gs)
1002 LOG ("Make ghostscript render next page");
1004 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
1005 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
1006 g_return_val_if_fail (gs->busy != TRUE, FALSE);
1010 event.xclient.type = ClientMessage;
1011 event.xclient.display = gdk_display;
1012 event.xclient.window = gs->message_window;
1013 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1014 event.xclient.format = 32;
1016 gdk_error_trap_push ();
1017 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1019 gdk_error_trap_pop ();
1025 render_page (PSDocument *gs, int page)
1027 g_return_val_if_fail(gs != NULL, FALSE);
1028 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1030 if(!gs->gs_filename) {
1034 if (gs->structured_doc && gs->doc) {
1035 LOG ("It's a structured document, let's send one page to gs");
1037 if (is_interpreter_ready (gs)) {
1038 ps_document_next_page (gs);
1040 ps_document_enable_interpreter (gs);
1041 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1042 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1045 send_ps (gs, gs->doc->pages[page].begin,
1046 gs->doc->pages[page].len, FALSE);
1048 /* Unstructured document
1050 * In the case of non structured documents,
1051 * GS read the PS from the actual file (via command
1052 * line. Hence, ggv only send a signal next page.
1053 * If ghostview is not running it is usually because
1054 * the last page of the file was displayed. In that
1055 * case, ggv restarts GS again and the first page is displayed.
1058 LOG ("It's an unstructured document, gs will just read the file");
1060 if (!is_interpreter_ready (gs)) {
1061 ps_document_enable_interpreter(gs);
1063 ps_document_next_page(gs);
1070 ps_document_load (EvDocument *document,
1077 filename = g_filename_from_uri (uri, NULL, error);
1081 result = document_load (PS_DOCUMENT (document), filename);
1083 g_set_error (error, G_FILE_ERROR,
1084 G_FILE_ERROR_FAILED,
1085 "Failed to load document '%s'\n",
1095 save_page_list (PSDocument *document, int *page_list, const char *filename)
1097 gboolean result = TRUE;
1098 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1102 pscopydoc (sink, document->gs_filename, document->doc, page_list);
1104 buf = gtk_gs_doc_sink_get_buffer (sink);
1106 f = fopen (filename, "w");
1115 gtk_gs_doc_sink_free (sink);
1122 ps_document_save (EvDocument *document,
1126 PSDocument *ps = PS_DOCUMENT (document);
1132 filename = g_filename_from_uri (uri, NULL, error);
1136 page_list = g_new0 (int, ps->doc->numpages);
1137 for (i = 0; i < ps->doc->numpages; i++) {
1141 result = save_page_list (ps, page_list, filename);
1150 ps_document_get_n_pages (EvDocument *document)
1152 PSDocument *ps = PS_DOCUMENT (document);
1154 g_return_val_if_fail (ps != NULL, -1);
1156 if (!ps->gs_filename || !ps->doc) {
1160 return ps->structured_doc ? ps->doc->numpages : 1;
1164 ps_document_get_page_size (EvDocument *document,
1169 PSDocument *gs = PS_DOCUMENT (document);
1171 int urx, ury, llx, lly, orientation;
1173 get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
1174 orientation = get_page_orientation (PS_DOCUMENT (document), page);
1176 switch (orientation) {
1177 case GTK_GS_ORIENTATION_PORTRAIT:
1178 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1179 w = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
1180 h = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
1182 case GTK_GS_ORIENTATION_LANDSCAPE:
1183 case GTK_GS_ORIENTATION_SEASCAPE:
1184 w = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
1185 h = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
1189 g_assert_not_reached ();
1203 ps_document_can_get_text (EvDocument *document)
1209 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer, int page, double scale)
1211 PSDocument *gs = PS_DOCUMENT (renderer);
1213 if (gs->pstarget == NULL) {
1216 widget = gtk_window_new (GTK_WINDOW_POPUP);
1217 gtk_widget_realize (widget);
1218 gs->pstarget = widget->window;
1220 g_assert (gs->pstarget != NULL);
1222 g_signal_connect (widget, "event",
1223 G_CALLBACK (ps_document_widget_event),
1227 setup_pixmap (gs, page, scale);
1228 setup_page (gs, page, scale);
1230 render_page (gs, page);
1233 static EvDocumentInfo *
1234 ps_document_get_info (EvDocument *document)
1236 EvDocumentInfo *info;
1237 PSDocument *ps = PS_DOCUMENT (document);
1239 info = g_new0 (EvDocumentInfo, 1);
1240 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
1241 EV_DOCUMENT_INFO_FORMAT |
1242 EV_DOCUMENT_INFO_CREATOR |
1243 EV_DOCUMENT_INFO_N_PAGES;
1244 info->title = g_strdup (ps->doc->title);
1245 info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
1246 : g_strdup (_("PostScript"));
1247 info->creator = g_strdup (ps->doc->creator);
1248 info->n_pages = ev_document_get_n_pages (document);
1253 static EvOrientation
1254 ps_document_get_orientation (EvDocument *document)
1256 EvOrientation orientation;
1257 PSDocument *ps = PS_DOCUMENT (document);
1259 g_return_val_if_fail (ps != NULL, EV_ORIENTATION_PORTRAIT);
1261 switch (ps->orientation) {
1262 case GTK_GS_ORIENTATION_PORTRAIT:
1263 orientation = EV_ORIENTATION_PORTRAIT;
1265 case GTK_GS_ORIENTATION_LANDSCAPE:
1266 orientation = EV_ORIENTATION_LANDSCAPE;
1268 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1269 orientation = EV_ORIENTATION_UPSIDEDOWN;
1271 case GTK_GS_ORIENTATION_SEASCAPE:
1272 orientation = EV_ORIENTATION_SEASCAPE;
1275 orientation = EV_ORIENTATION_PORTRAIT;
1283 ps_document_set_orientation (EvDocument *document, EvOrientation orientation)
1285 PSDocument *ps = PS_DOCUMENT (document);
1287 g_return_if_fail (ps != NULL);
1289 switch (orientation) {
1290 case EV_ORIENTATION_PORTRAIT:
1291 ps->orientation = GTK_GS_ORIENTATION_PORTRAIT;
1293 case EV_ORIENTATION_LANDSCAPE:
1294 ps->orientation = GTK_GS_ORIENTATION_LANDSCAPE;
1296 case EV_ORIENTATION_UPSIDEDOWN:
1297 ps->orientation = GTK_GS_ORIENTATION_UPSIDEDOWN;
1299 case EV_ORIENTATION_SEASCAPE:
1300 ps->orientation = GTK_GS_ORIENTATION_SEASCAPE;
1306 ps_document_document_iface_init (EvDocumentIface *iface)
1308 iface->load = ps_document_load;
1309 iface->save = ps_document_save;
1310 iface->can_get_text = ps_document_can_get_text;
1311 iface->get_n_pages = ps_document_get_n_pages;
1312 iface->get_page_size = ps_document_get_page_size;
1313 iface->get_info = ps_document_get_info;
1314 iface->set_orientation = ps_document_set_orientation;
1315 iface->get_orientation = ps_document_get_orientation;
1319 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
1321 iface->render_pixbuf = ps_async_renderer_render_pixbuf;
1325 ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
1326 int first_page, int last_page)
1328 PSDocument *document = PS_DOCUMENT (exporter);
1330 g_free (document->ps_export_pagelist);
1332 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1333 document->ps_export_filename = g_strdup (filename);
1337 ps_document_ps_export_do_page (EvPSExporter *exporter, int page)
1339 PSDocument *document = PS_DOCUMENT (exporter);
1341 document->ps_export_pagelist[page] = 1;
1345 ps_document_ps_export_end (EvPSExporter *exporter)
1347 PSDocument *document = PS_DOCUMENT (exporter);
1349 save_page_list (document, document->ps_export_pagelist,
1350 document->ps_export_filename);
1352 g_free (document->ps_export_pagelist);
1353 g_free (document->ps_export_filename);
1354 document->ps_export_pagelist = NULL;
1355 document->ps_export_filename = NULL;
1359 ps_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1361 iface->begin = ps_document_ps_export_begin;
1362 iface->do_page = ps_document_ps_export_do_page;
1363 iface->end = ps_document_ps_export_end;