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 stop_interpreter (gs);
183 G_OBJECT_CLASS (parent_class)->dispose (object);
187 ps_document_class_init(PSDocumentClass *klass)
189 GObjectClass *object_class;
191 object_class = (GObjectClass *) klass;
192 parent_class = g_type_class_peek_parent (klass);
195 object_class->dispose = ps_document_dispose;
197 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
198 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
199 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
200 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
204 push_pixbuf (PSDocument *gs)
210 cmap = gdk_window_get_colormap (gs->pstarget);
211 gdk_drawable_get_size (gs->bpixmap, &width, &height);
212 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
215 g_signal_emit_by_name (gs, "render_finished", pixbuf);
216 g_object_unref (pixbuf);
220 interpreter_failed (PSDocument *gs, char *msg)
222 LOG ("Interpreter failed %s", msg);
226 stop_interpreter (gs);
230 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
232 PSDocument *gs = (PSDocument *) data;
234 if(event->type != GDK_CLIENT_EVENT)
237 gs->message_window = event->client.data.l[0];
239 if (event->client.message_type == gs_class->page_atom) {
240 LOG ("GS rendered the document");
250 send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
252 struct record_list *ps_new;
254 if (gs->interpreter_input < 0) {
255 g_critical("No pipe to gs: error in send_ps().");
259 ps_new = g_new0 (struct record_list, 1);
260 ps_new->fp = gs->gs_psfile;
261 ps_new->begin = begin;
263 ps_new->seek_needed = TRUE;
264 ps_new->close = close;
267 if (gs->input_buffer == NULL) {
268 gs->input_buffer = g_malloc(MAX_BUFSIZE);
271 if (gs->ps_input == NULL) {
272 gs->input_buffer_ptr = gs->input_buffer;
273 gs->bytes_left = len;
274 gs->buffer_bytes_left = 0;
275 gs->ps_input = ps_new;
276 gs->interpreter_input_id = gdk_input_add
277 (gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
279 struct record_list *p = gs->ps_input;
280 while (p->next != NULL) {
288 get_xdpi (PSDocument *gs)
290 return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
294 get_ydpi (PSDocument *gs)
296 return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
300 setup_pixmap (PSDocument *gs, int page, double scale)
303 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
304 GdkColormap *colormap;
305 double width, height;
306 int pixmap_width, pixmap_height;
308 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
309 pixmap_width = width * scale + 0.5;
310 pixmap_height = height * scale + 0.5;
315 gdk_drawable_get_size (gs->bpixmap, &w, &h);
317 if (pixmap_width != w || h != pixmap_height) {
318 gdk_drawable_unref (gs->bpixmap);
320 stop_interpreter (gs);
325 LOG ("Create pixmap");
327 fill = gdk_gc_new (gs->pstarget);
328 colormap = gdk_drawable_get_colormap (gs->pstarget);
329 gdk_color_alloc (colormap, &white);
330 gdk_gc_set_foreground (fill, &white);
331 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
333 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
334 0, 0, pixmap_width, pixmap_height);
338 #define DEFAULT_PAGE_SIZE 1
341 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
347 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
348 int new_pagesize = -1;
350 g_return_if_fail (PS_IS_DOCUMENT (gs));
352 if (new_pagesize == -1) {
353 new_pagesize = DEFAULT_PAGE_SIZE;
355 /* If we have a document:
356 * We use -- the page size (if specified)
357 * or the doc. size (if specified)
358 * or the page bbox (if specified)
359 * or the bounding box
361 if ((page >= 0) && (gs->doc->numpages > page) &&
362 (gs->doc->pages) && (gs->doc->pages[page].size)) {
363 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
364 } else if (gs->doc->default_page_size != NULL) {
365 new_pagesize = gs->doc->default_page_size - gs->doc->size;
366 } else if ((page >= 0) &&
367 (gs->doc->numpages > page) &&
369 (gs->doc->pages[page].boundingbox[URX] >
370 gs->doc->pages[page].boundingbox[LLX]) &&
371 (gs->doc->pages[page].boundingbox[URY] >
372 gs->doc->pages[page].boundingbox[LLY])) {
374 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
375 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
381 /* Compute bounding box */
382 if (gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
385 (gs->doc->pages[page].boundingbox[URX] >
386 gs->doc->pages[page].boundingbox[LLX]) &&
387 (gs->doc->pages[page].boundingbox[URY] >
388 gs->doc->pages[page].boundingbox[LLY])) {
390 new_llx = gs->doc->pages[page].boundingbox[LLX];
391 new_lly = gs->doc->pages[page].boundingbox[LLY];
392 new_urx = gs->doc->pages[page].boundingbox[URX];
393 new_ury = gs->doc->pages[page].boundingbox[URY];
394 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
395 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
397 new_llx = gs->doc->boundingbox[LLX];
398 new_lly = gs->doc->boundingbox[LLY];
399 new_urx = gs->doc->boundingbox[URX];
400 new_ury = gs->doc->boundingbox[URY];
403 if (new_pagesize < 0)
404 new_pagesize = DEFAULT_PAGE_SIZE;
405 new_llx = new_lly = 0;
406 if (gs->doc && gs->doc->size &&
407 (new_pagesize < gs->doc->numsizes)) {
408 new_urx = gs->doc->size[new_pagesize].width;
409 new_ury = gs->doc->size[new_pagesize].height;
411 new_urx = papersizes[new_pagesize].width;
412 new_ury = papersizes[new_pagesize].height;
416 if (new_urx <= new_llx)
417 new_urx = papersizes[12].width;
418 if (new_ury <= new_lly)
419 new_ury = papersizes[12].height;
428 get_page_orientation (PSDocument *gs, int page)
432 orientation = GTK_GS_ORIENTATION_NONE;
434 if (gs->structured_doc) {
435 orientation = gs->doc->pages[page].orientation;
437 if (orientation == GTK_GS_ORIENTATION_NONE) {
438 orientation = gs->doc->default_page_orientation;
440 if (orientation == GTK_GS_ORIENTATION_NONE) {
441 orientation = gs->doc->orientation;
443 if (orientation == GTK_GS_ORIENTATION_NONE) {
444 orientation = GTK_GS_ORIENTATION_PORTRAIT;
446 if (gs->orientation != GTK_GS_ORIENTATION_NONE) {
447 orientation = gs->orientation;
454 setup_page (PSDocument *gs, int page, double scale)
457 int urx, ury, llx, lly, orientation;
458 char scaled_xdpi[G_ASCII_DTOSTR_BUF_SIZE];
459 char scaled_ydpi[G_ASCII_DTOSTR_BUF_SIZE];
461 LOG ("Setup the page");
463 get_page_box (gs, page, &urx, &ury, &llx, &lly);
464 orientation = get_page_orientation (gs, page);
465 g_ascii_dtostr (scaled_xdpi, G_ASCII_DTOSTR_BUF_SIZE, get_xdpi (gs) * scale);
466 g_ascii_dtostr (scaled_ydpi, G_ASCII_DTOSTR_BUF_SIZE, get_ydpi (gs) * scale);
468 g_snprintf (buf, 1024, "%ld %d %d %d %d %d %s %s %d %d %d %d",
469 0L, orientation * 90, llx, lly, urx, ury,
470 scaled_xdpi, scaled_ydpi,
472 LOG ("GS property %s", buf);
474 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
475 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
480 close_pipe (int p[2])
491 is_interpreter_ready (PSDocument *gs)
493 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
497 output (gpointer data, gint source, GdkInputCondition condition)
499 char buf[MAX_BUFSIZE + 1];
501 PSDocument *gs = PS_DOCUMENT(data);
503 if (source == gs->interpreter_output) {
504 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
505 if (bytes == 0) { /* EOF occurred */
506 close (gs->interpreter_output);
507 gs->interpreter_output = -1;
508 gdk_input_remove (gs->interpreter_output_id);
510 } else if (bytes == -1) {
512 interpreter_failed (gs, NULL);
515 if (gs->interpreter_err == -1) {
516 interpreter_failed (gs, NULL);
518 } else if (source == gs->interpreter_err) {
519 bytes = read (gs->interpreter_err, buf, MAX_BUFSIZE);
520 if (bytes == 0) { /* EOF occurred */
521 close (gs->interpreter_err);
522 gs->interpreter_err = -1;
523 gdk_input_remove (gs->interpreter_error_id);
525 } else if (bytes == -1) {
527 interpreter_failed (gs, NULL);
530 if (gs->interpreter_output == -1) {
531 interpreter_failed(gs, NULL);
548 input(gpointer data, gint source, GdkInputCondition condition)
550 PSDocument *gs = PS_DOCUMENT(data);
552 void (*oldsig) (int);
553 oldsig = signal(SIGPIPE, catchPipe);
558 if (gs->buffer_bytes_left == 0) {
559 /* Get a new section if required */
560 if (gs->ps_input && gs->bytes_left == 0) {
561 struct record_list *ps_old = gs->ps_input;
562 gs->ps_input = ps_old->next;
563 if (ps_old->close && NULL != ps_old->fp)
568 /* Have to seek at the beginning of each section */
569 if (gs->ps_input && gs->ps_input->seek_needed) {
570 fseek (gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
571 gs->ps_input->seek_needed = FALSE;
572 gs->bytes_left = gs->ps_input->len;
575 if (gs->bytes_left > MAX_BUFSIZE) {
576 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
577 MAX_BUFSIZE, gs->ps_input->fp);
578 } else if (gs->bytes_left > 0) {
579 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
580 gs->bytes_left, gs->ps_input->fp);
582 gs->buffer_bytes_left = 0;
584 if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
585 interpreter_failed (gs, NULL); /* Error occurred */
587 gs->input_buffer_ptr = gs->input_buffer;
588 gs->bytes_left -= gs->buffer_bytes_left;
591 if (gs->buffer_bytes_left > 0) {
592 bytes_written = write (gs->interpreter_input,
593 gs->input_buffer_ptr, gs->buffer_bytes_left);
596 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
598 interpreter_failed (gs, NULL);
599 } else if (bytes_written == -1) {
600 if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
601 interpreter_failed (gs, NULL); /* Something bad happened */
604 gs->buffer_bytes_left -= bytes_written;
605 gs->input_buffer_ptr += bytes_written;
608 } while (gs->ps_input && gs->buffer_bytes_left == 0);
610 signal (SIGPIPE, oldsig);
612 if (gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
613 if (gs->interpreter_input_id != 0) {
614 gdk_input_remove (gs->interpreter_input_id);
615 gs->interpreter_input_id = 0;
621 start_interpreter (PSDocument *gs)
623 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
624 int std_out[2]; /* pipe from interp stdout */
625 int std_err[2]; /* pipe from interp stderr */
628 #define NUM_GS_ARGS (NUM_ARGS - 20)
629 #define NUM_ALPHA_ARGS 10
631 char *argv[NUM_ARGS], *dir, *gv_env;
632 char **gs_args, **alpha_args = NULL;
635 LOG ("Start the interpreter");
640 stop_interpreter(gs);
642 /* set up the args... */
643 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
644 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
645 argv[argc] = gs_args[i];
648 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
649 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
650 argv[argc] = alpha_args[i];
653 argv[argc++] = "-dNOPAUSE";
654 argv[argc++] = "-dQUIET";
655 argv[argc++] = "-dSAFER";
657 /* set up the pipes */
658 if (gs->send_filename_to_gs) {
659 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
661 argv[argc++] = "quit";
668 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
669 if (pipe (std_in) == -1) {
670 g_critical ("Unable to open pipe to Ghostscript.");
675 if (pipe (std_out) == -1) {
680 if (pipe(std_err) == -1) {
682 close_pipe (std_out);
686 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
687 gdk_x11_drawable_get_xid (gs->pstarget),
688 gdk_x11_drawable_get_xid (gs->bpixmap));
689 LOG ("Launching ghostview with env %s", gv_env);
691 gs->interpreter_pid = fork ();
692 switch (gs->interpreter_pid) {
695 close_pipe (std_out);
696 close_pipe (std_err);
701 dup2 (std_out[1], 1);
705 dup2 (std_err[1], 2);
708 if (!gs->reading_from_pipe) {
709 if (gs->send_filename_to_gs) {
711 /* just in case gs tries to read from stdin */
712 stdinfd = open("/dev/null", O_RDONLY);
726 /* change to directory where the input file is. This helps
727 * with postscript-files which include other files using
729 dir = g_path_get_dirname (gs->gs_filename);
733 execvp (argv[0], argv);
736 g_critical ("Unable to execute [%s]\n", argv[0]);
737 g_strfreev (gs_args);
739 g_strfreev (alpha_args);
742 default: /* parent */
743 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
746 /* use non-blocking IO for pipe to ghostscript */
747 result = fcntl (std_in[1], F_GETFL, 0);
748 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
749 gs->interpreter_input = std_in[1];
751 gs->interpreter_input = -1;
755 gs->interpreter_output = std_out[0];
757 gs->interpreter_err = std_err[0];
758 gs->interpreter_output_id =
759 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
760 gs->interpreter_error_id =
761 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
769 stop_interpreter(PSDocument * gs)
771 if (gs->interpreter_pid > 0) {
773 LOG ("Stop the interpreter");
774 kill (gs->interpreter_pid, SIGTERM);
775 while ((wait(&status) == -1) && (errno == EINTR));
776 gs->interpreter_pid = -1;
778 gs->gs_status = _("Interpreter failed.");
782 if (gs->interpreter_input >= 0) {
783 close (gs->interpreter_input);
784 gs->interpreter_input = -1;
785 if (gs->interpreter_input_id != 0) {
786 gdk_input_remove(gs->interpreter_input_id);
787 gs->interpreter_input_id = 0;
789 while (gs->ps_input) {
790 struct record_list *ps_old = gs->ps_input;
791 gs->ps_input = gs->ps_input->next;
792 if (ps_old->close && NULL != ps_old->fp)
798 if (gs->interpreter_output >= 0) {
799 close (gs->interpreter_output);
800 gs->interpreter_output = -1;
801 if (gs->interpreter_output_id) {
802 gdk_input_remove (gs->interpreter_output_id);
803 gs->interpreter_output_id = 0;
807 if (gs->interpreter_err >= 0) {
808 close (gs->interpreter_err);
809 gs->interpreter_err = -1;
810 if (gs->interpreter_error_id) {
811 gdk_input_remove (gs->interpreter_error_id);
812 gs->interpreter_error_id = 0;
819 /* If file exists and is a regular file then return its length, else -1 */
821 file_length (const gchar * filename)
823 struct stat stat_rec;
825 if (filename && (stat (filename, &stat_rec) == 0) && S_ISREG (stat_rec.st_mode))
826 return stat_rec.st_size;
831 /* Test if file exists, is a regular file and its length is > 0 */
833 file_readable(const char *filename)
835 return (file_length (filename) > 0);
839 * Decompress gs->gs_filename if necessary
840 * Set gs->filename_unc to the name of the uncompressed file or NULL.
841 * Error reporting via signal 'interpreter_message'
842 * Return name of input file to use or NULL on error..
845 check_filecompressed (PSDocument * gs)
849 gchar *filename, *filename_unc, *filename_err, *cmdline;
855 if ((file = fopen(gs->gs_filename, "r")) &&
856 (fread (buf, sizeof(gchar), 3, file) == 3)) {
857 if ((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
858 /* file is gzipped or compressed */
859 cmd = gtk_gs_defaults_get_ungzip_cmd ();
860 } else if (strncmp (buf, "BZh", 3) == 0) {
861 /* file is compressed with bzip2 */
862 cmd = gtk_gs_defaults_get_unbzip2_cmd ();
870 return gs->gs_filename;
872 /* do the decompression */
873 filename = g_shell_quote (gs->gs_filename);
874 filename_unc = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
875 if ((fd = mkstemp (filename_unc)) < 0) {
876 g_free (filename_unc);
882 filename_err = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
883 if ((fd = mkstemp(filename_err)) < 0) {
884 g_free (filename_err);
885 g_free (filename_unc);
891 cmdline = g_strdup_printf ("%s %s >%s 2>%s", cmd,
892 filename, filename_unc, filename_err);
893 if (system (cmdline) == 0 &&
894 file_readable (filename_unc) &&
895 file_length (filename_err) == 0) {
896 /* sucessfully uncompressed file */
897 gs->gs_filename_unc = filename_unc;
900 g_snprintf (buf, 1024, _("Error while decompressing file %s:\n"),
902 interpreter_failed (gs, buf);
903 unlink (filename_unc);
904 g_free (filename_unc);
908 unlink (filename_err);
909 g_free (filename_err);
917 ps_document_enable_interpreter(PSDocument *gs)
919 g_return_val_if_fail (PS_IS_DOCUMENT (gs), FALSE);
921 if (!gs->gs_filename)
924 return start_interpreter (gs);
928 document_load (PSDocument *gs, const gchar *fname)
930 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
932 LOG ("Load the document");
939 /* prepare this document */
940 gs->structured_doc = FALSE;
941 gs->send_filename_to_gs = TRUE;
942 gs->gs_filename = g_strdup (fname);
944 if ((gs->reading_from_pipe = (strcmp (fname, "-") == 0))) {
945 gs->send_filename_to_gs = FALSE;
948 * We need to make sure that the file is loadable/exists!
949 * otherwise we want to exit without loading new stuff...
951 gchar *filename = NULL;
953 if (!file_readable(fname)) {
956 g_snprintf (buf, 1024, _("Cannot open file %s.\n"), fname);
957 interpreter_failed (gs, buf);
958 gs->gs_status = _("File is not readable.");
960 filename = check_filecompressed(gs);
963 if (!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
964 interpreter_failed (gs, NULL);
968 /* we grab the vital statistics!!! */
969 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
971 if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
972 (gs->doc->epsf && gs->doc->numpages > 1)) {
973 gs->structured_doc = TRUE;
974 gs->send_filename_to_gs = FALSE;
978 gs->gs_status = _("Document loaded.");
984 ps_document_next_page (PSDocument *gs)
988 LOG ("Make ghostscript render next page");
990 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
991 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
992 g_return_val_if_fail (gs->busy != TRUE, FALSE);
996 event.xclient.type = ClientMessage;
997 event.xclient.display = gdk_display;
998 event.xclient.window = gs->message_window;
999 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1000 event.xclient.format = 32;
1002 gdk_error_trap_push ();
1003 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1005 gdk_error_trap_pop ();
1011 render_page (PSDocument *gs, int page)
1013 g_return_val_if_fail(gs != NULL, FALSE);
1014 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1016 if(!gs->gs_filename) {
1020 if (gs->structured_doc && gs->doc) {
1021 LOG ("It's a structured document, let's send one page to gs");
1023 if (is_interpreter_ready (gs)) {
1024 ps_document_next_page (gs);
1026 ps_document_enable_interpreter (gs);
1027 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1028 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1031 send_ps (gs, gs->doc->pages[page].begin,
1032 gs->doc->pages[page].len, FALSE);
1034 /* Unstructured document
1036 * In the case of non structured documents,
1037 * GS read the PS from the actual file (via command
1038 * line. Hence, ggv only send a signal next page.
1039 * If ghostview is not running it is usually because
1040 * the last page of the file was displayed. In that
1041 * case, ggv restarts GS again and the first page is displayed.
1044 LOG ("It's an unstructured document, gs will just read the file");
1046 if (!is_interpreter_ready (gs)) {
1047 ps_document_enable_interpreter(gs);
1049 ps_document_next_page(gs);
1056 ps_document_load (EvDocument *document,
1063 filename = g_filename_from_uri (uri, NULL, error);
1067 result = document_load (PS_DOCUMENT (document), filename);
1069 g_set_error (error, G_FILE_ERROR,
1070 G_FILE_ERROR_FAILED,
1071 "Failed to load document '%s'\n",
1081 save_page_list (PSDocument *document, int *page_list, const char *filename)
1083 gboolean result = TRUE;
1084 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1088 pscopydoc (sink, document->gs_filename, document->doc, page_list);
1090 buf = gtk_gs_doc_sink_get_buffer (sink);
1092 f = fopen (filename, "w");
1101 gtk_gs_doc_sink_free (sink);
1108 ps_document_save (EvDocument *document,
1112 PSDocument *ps = PS_DOCUMENT (document);
1118 filename = g_filename_from_uri (uri, NULL, error);
1122 page_list = g_new0 (int, ps->doc->numpages);
1123 for (i = 0; i < ps->doc->numpages; i++) {
1127 result = save_page_list (ps, page_list, filename);
1136 ps_document_get_n_pages (EvDocument *document)
1138 PSDocument *ps = PS_DOCUMENT (document);
1140 g_return_val_if_fail (ps != NULL, -1);
1142 if (!ps->gs_filename || !ps->doc) {
1146 return ps->structured_doc ? ps->doc->numpages : 1;
1150 ps_document_get_page_size (EvDocument *document,
1155 PSDocument *gs = PS_DOCUMENT (document);
1157 int urx, ury, llx, lly, orientation;
1159 get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
1160 orientation = get_page_orientation (PS_DOCUMENT (document), page);
1162 switch (orientation) {
1163 case GTK_GS_ORIENTATION_PORTRAIT:
1164 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1165 w = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
1166 h = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
1168 case GTK_GS_ORIENTATION_LANDSCAPE:
1169 case GTK_GS_ORIENTATION_SEASCAPE:
1170 w = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
1171 h = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
1175 g_assert_not_reached ();
1189 ps_document_can_get_text (EvDocument *document)
1195 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer, int page, double scale)
1197 PSDocument *gs = PS_DOCUMENT (renderer);
1199 if (gs->pstarget == NULL) {
1202 widget = gtk_window_new (GTK_WINDOW_POPUP);
1203 gtk_widget_realize (widget);
1204 gs->pstarget = widget->window;
1206 g_assert (gs->pstarget != NULL);
1208 g_signal_connect (widget, "event",
1209 G_CALLBACK (ps_document_widget_event),
1213 setup_pixmap (gs, page, scale);
1214 setup_page (gs, page, scale);
1216 render_page (gs, page);
1219 static EvDocumentInfo *
1220 ps_document_get_info (EvDocument *document)
1222 EvDocumentInfo *info;
1223 PSDocument *ps = PS_DOCUMENT (document);
1225 info = g_new0 (EvDocumentInfo, 1);
1226 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
1227 EV_DOCUMENT_INFO_FORMAT |
1228 EV_DOCUMENT_INFO_CREATOR |
1229 EV_DOCUMENT_INFO_N_PAGES;
1230 info->title = g_strdup (ps->doc->title);
1231 info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
1232 : g_strdup (_("PostScript"));
1233 info->creator = g_strdup (ps->doc->creator);
1234 info->n_pages = ev_document_get_n_pages (document);
1239 static EvOrientation
1240 ps_document_get_orientation (EvDocument *document)
1242 EvOrientation orientation;
1243 PSDocument *ps = PS_DOCUMENT (document);
1245 g_return_val_if_fail (ps != NULL, EV_ORIENTATION_PORTRAIT);
1247 switch (ps->orientation) {
1248 case GTK_GS_ORIENTATION_PORTRAIT:
1249 orientation = EV_ORIENTATION_PORTRAIT;
1251 case GTK_GS_ORIENTATION_LANDSCAPE:
1252 orientation = EV_ORIENTATION_LANDSCAPE;
1254 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1255 orientation = EV_ORIENTATION_UPSIDEDOWN;
1257 case GTK_GS_ORIENTATION_SEASCAPE:
1258 orientation = EV_ORIENTATION_SEASCAPE;
1261 orientation = EV_ORIENTATION_PORTRAIT;
1269 ps_document_set_orientation (EvDocument *document, EvOrientation orientation)
1271 PSDocument *ps = PS_DOCUMENT (document);
1273 g_return_if_fail (ps != NULL);
1275 switch (orientation) {
1276 case EV_ORIENTATION_PORTRAIT:
1277 ps->orientation = GTK_GS_ORIENTATION_PORTRAIT;
1279 case EV_ORIENTATION_LANDSCAPE:
1280 ps->orientation = GTK_GS_ORIENTATION_LANDSCAPE;
1282 case EV_ORIENTATION_UPSIDEDOWN:
1283 ps->orientation = GTK_GS_ORIENTATION_UPSIDEDOWN;
1285 case EV_ORIENTATION_SEASCAPE:
1286 ps->orientation = GTK_GS_ORIENTATION_SEASCAPE;
1292 ps_document_document_iface_init (EvDocumentIface *iface)
1294 iface->load = ps_document_load;
1295 iface->save = ps_document_save;
1296 iface->can_get_text = ps_document_can_get_text;
1297 iface->get_n_pages = ps_document_get_n_pages;
1298 iface->get_page_size = ps_document_get_page_size;
1299 iface->get_info = ps_document_get_info;
1300 iface->set_orientation = ps_document_set_orientation;
1301 iface->get_orientation = ps_document_get_orientation;
1305 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
1307 iface->render_pixbuf = ps_async_renderer_render_pixbuf;
1311 ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
1312 int first_page, int last_page)
1314 PSDocument *document = PS_DOCUMENT (exporter);
1316 g_free (document->ps_export_pagelist);
1318 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1319 document->ps_export_filename = g_strdup (filename);
1323 ps_document_ps_export_do_page (EvPSExporter *exporter, int page)
1325 PSDocument *document = PS_DOCUMENT (exporter);
1327 document->ps_export_pagelist[page] = 1;
1331 ps_document_ps_export_end (EvPSExporter *exporter)
1333 PSDocument *document = PS_DOCUMENT (exporter);
1335 save_page_list (document, document->ps_export_pagelist,
1336 document->ps_export_filename);
1338 g_free (document->ps_export_pagelist);
1339 g_free (document->ps_export_filename);
1340 document->ps_export_pagelist = NULL;
1341 document->ps_export_filename = NULL;
1345 ps_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1347 iface->begin = ps_document_ps_export_begin;
1348 iface->do_page = ps_document_ps_export_do_page;
1349 iface->end = ps_document_ps_export_end;