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;
143 ps_document_dispose (GObject *object)
145 PSDocument *gs = PS_DOCUMENT (object);
147 g_return_if_fail (gs != NULL);
150 fclose (gs->gs_psfile);
151 gs->gs_psfile = NULL;
154 if (gs->gs_filename) {
155 g_free (gs->gs_filename);
156 gs->gs_filename = NULL;
164 if (gs->gs_filename_unc) {
165 unlink(gs->gs_filename_unc);
166 g_free(gs->gs_filename_unc);
167 gs->gs_filename_unc = NULL;
171 gdk_drawable_unref (gs->bpixmap);
174 if(gs->input_buffer) {
175 g_free(gs->input_buffer);
176 gs->input_buffer = NULL;
179 if (gs->target_window) {
180 gtk_widget_destroy (gs->target_window);
181 gs->target_window = NULL;
185 stop_interpreter (gs);
187 G_OBJECT_CLASS (parent_class)->dispose (object);
191 ps_document_class_init(PSDocumentClass *klass)
193 GObjectClass *object_class;
195 object_class = (GObjectClass *) klass;
196 parent_class = g_type_class_peek_parent (klass);
199 object_class->dispose = ps_document_dispose;
201 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
202 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
203 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
204 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
208 push_pixbuf (PSDocument *gs)
214 cmap = gdk_window_get_colormap (gs->pstarget);
215 gdk_drawable_get_size (gs->bpixmap, &width, &height);
216 LOG ("Get from drawable\n");
217 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
220 LOG ("Get from drawable done\n");
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");
250 LOG ("Pixbuf pushed");
257 send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
259 struct record_list *ps_new;
261 if (gs->interpreter_input < 0) {
262 g_critical("No pipe to gs: error in send_ps().");
266 ps_new = g_new0 (struct record_list, 1);
267 ps_new->fp = gs->gs_psfile;
268 ps_new->begin = begin;
270 ps_new->seek_needed = TRUE;
271 ps_new->close = close;
274 if (gs->input_buffer == NULL) {
275 gs->input_buffer = g_malloc(MAX_BUFSIZE);
278 if (gs->ps_input == NULL) {
279 gs->input_buffer_ptr = gs->input_buffer;
280 gs->bytes_left = len;
281 gs->buffer_bytes_left = 0;
282 gs->ps_input = ps_new;
283 gs->interpreter_input_id = gdk_input_add
284 (gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
286 struct record_list *p = gs->ps_input;
287 while (p->next != NULL) {
295 get_xdpi (PSDocument *gs)
297 return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
301 get_ydpi (PSDocument *gs)
303 return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
307 setup_pixmap (PSDocument *gs, int page, double scale, int rotation)
310 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
311 GdkColormap *colormap;
312 double width, height;
313 int pixmap_width, pixmap_height;
315 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
317 if (rotation == 90 || rotation == 270) {
318 pixmap_height = width * scale + 0.5;
319 pixmap_width = height * scale + 0.5;
321 pixmap_width = width * scale + 0.5;
322 pixmap_height = height * scale + 0.5;
328 gdk_drawable_get_size (gs->bpixmap, &w, &h);
330 if (pixmap_width != w || h != pixmap_height) {
331 gdk_drawable_unref (gs->bpixmap);
333 stop_interpreter (gs);
338 LOG ("Create pixmap");
340 fill = gdk_gc_new (gs->pstarget);
341 colormap = gdk_drawable_get_colormap (gs->pstarget);
342 gdk_color_alloc (colormap, &white);
343 gdk_gc_set_foreground (fill, &white);
344 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
346 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
347 0, 0, pixmap_width, pixmap_height);
351 #define DEFAULT_PAGE_SIZE 1
354 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
360 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
361 int new_pagesize = -1;
363 g_return_if_fail (PS_IS_DOCUMENT (gs));
365 if (new_pagesize == -1) {
366 new_pagesize = DEFAULT_PAGE_SIZE;
368 /* If we have a document:
369 * We use -- the page size (if specified)
370 * or the doc. size (if specified)
371 * or the page bbox (if specified)
372 * or the bounding box
374 if ((page >= 0) && (gs->doc->numpages > page) &&
375 (gs->doc->pages) && (gs->doc->pages[page].size)) {
376 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
377 } else if (gs->doc->default_page_size != NULL) {
378 new_pagesize = gs->doc->default_page_size - gs->doc->size;
379 } else if ((page >= 0) &&
380 (gs->doc->numpages > page) &&
382 (gs->doc->pages[page].boundingbox[URX] >
383 gs->doc->pages[page].boundingbox[LLX]) &&
384 (gs->doc->pages[page].boundingbox[URY] >
385 gs->doc->pages[page].boundingbox[LLY])) {
387 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
388 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
394 /* Compute bounding box */
395 if (gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
398 (gs->doc->pages[page].boundingbox[URX] >
399 gs->doc->pages[page].boundingbox[LLX]) &&
400 (gs->doc->pages[page].boundingbox[URY] >
401 gs->doc->pages[page].boundingbox[LLY])) {
403 new_llx = gs->doc->pages[page].boundingbox[LLX];
404 new_lly = gs->doc->pages[page].boundingbox[LLY];
405 new_urx = gs->doc->pages[page].boundingbox[URX];
406 new_ury = gs->doc->pages[page].boundingbox[URY];
407 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
408 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
410 new_llx = gs->doc->boundingbox[LLX];
411 new_lly = gs->doc->boundingbox[LLY];
412 new_urx = gs->doc->boundingbox[URX];
413 new_ury = gs->doc->boundingbox[URY];
416 if (new_pagesize < 0)
417 new_pagesize = DEFAULT_PAGE_SIZE;
418 new_llx = new_lly = 0;
419 if (gs->doc && gs->doc->size &&
420 (new_pagesize < gs->doc->numsizes)) {
421 new_urx = gs->doc->size[new_pagesize].width;
422 new_ury = gs->doc->size[new_pagesize].height;
424 new_urx = papersizes[new_pagesize].width;
425 new_ury = papersizes[new_pagesize].height;
429 if (new_urx <= new_llx)
430 new_urx = papersizes[12].width;
431 if (new_ury <= new_lly)
432 new_ury = papersizes[12].height;
441 setup_page (PSDocument *gs, int page, double scale, int rotation)
444 char scaled_xdpi[G_ASCII_DTOSTR_BUF_SIZE];
445 char scaled_ydpi[G_ASCII_DTOSTR_BUF_SIZE];
446 int urx, ury, llx, lly;
448 LOG ("Setup the page");
450 get_page_box (gs, page, &urx, &ury, &llx, &lly);
451 g_ascii_dtostr (scaled_xdpi, G_ASCII_DTOSTR_BUF_SIZE, get_xdpi (gs) * scale);
452 g_ascii_dtostr (scaled_ydpi, G_ASCII_DTOSTR_BUF_SIZE, get_ydpi (gs) * scale);
454 buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
455 0L, rotation, llx, lly, urx, ury,
456 scaled_xdpi, scaled_ydpi,
458 LOG ("GS property %s", buf);
460 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
461 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
468 close_pipe (int p[2])
479 is_interpreter_ready (PSDocument *gs)
481 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
485 output (gpointer data, gint source, GdkInputCondition condition)
487 char buf[MAX_BUFSIZE + 1];
489 PSDocument *gs = PS_DOCUMENT(data);
491 if (source == gs->interpreter_output) {
492 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
493 if (bytes == 0) { /* EOF occurred */
494 close (gs->interpreter_output);
495 gs->interpreter_output = -1;
496 gdk_input_remove (gs->interpreter_output_id);
498 } else if (bytes == -1) {
500 interpreter_failed (gs, NULL);
503 if (gs->interpreter_err == -1) {
504 interpreter_failed (gs, NULL);
506 } else if (source == gs->interpreter_err) {
507 bytes = read (gs->interpreter_err, buf, MAX_BUFSIZE);
508 if (bytes == 0) { /* EOF occurred */
509 close (gs->interpreter_err);
510 gs->interpreter_err = -1;
511 gdk_input_remove (gs->interpreter_error_id);
513 } else if (bytes == -1) {
515 interpreter_failed (gs, NULL);
518 if (gs->interpreter_output == -1) {
519 interpreter_failed(gs, NULL);
536 input(gpointer data, gint source, GdkInputCondition condition)
538 PSDocument *gs = PS_DOCUMENT(data);
540 void (*oldsig) (int);
541 oldsig = signal(SIGPIPE, catchPipe);
546 if (gs->buffer_bytes_left == 0) {
547 /* Get a new section if required */
548 if (gs->ps_input && gs->bytes_left == 0) {
549 struct record_list *ps_old = gs->ps_input;
550 gs->ps_input = ps_old->next;
551 if (ps_old->close && NULL != ps_old->fp)
556 /* Have to seek at the beginning of each section */
557 if (gs->ps_input && gs->ps_input->seek_needed) {
558 fseek (gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
559 gs->ps_input->seek_needed = FALSE;
560 gs->bytes_left = gs->ps_input->len;
563 if (gs->bytes_left > MAX_BUFSIZE) {
564 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
565 MAX_BUFSIZE, gs->ps_input->fp);
566 } else if (gs->bytes_left > 0) {
567 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
568 gs->bytes_left, gs->ps_input->fp);
570 gs->buffer_bytes_left = 0;
572 if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
573 interpreter_failed (gs, NULL); /* Error occurred */
575 gs->input_buffer_ptr = gs->input_buffer;
576 gs->bytes_left -= gs->buffer_bytes_left;
579 if (gs->buffer_bytes_left > 0) {
580 bytes_written = write (gs->interpreter_input,
581 gs->input_buffer_ptr, gs->buffer_bytes_left);
584 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
586 interpreter_failed (gs, NULL);
587 } else if (bytes_written == -1) {
588 if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
589 interpreter_failed (gs, NULL); /* Something bad happened */
592 gs->buffer_bytes_left -= bytes_written;
593 gs->input_buffer_ptr += bytes_written;
596 } while (gs->ps_input && gs->buffer_bytes_left == 0);
598 signal (SIGPIPE, oldsig);
600 if (gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
601 if (gs->interpreter_input_id != 0) {
602 gdk_input_remove (gs->interpreter_input_id);
603 gs->interpreter_input_id = 0;
609 start_interpreter (PSDocument *gs)
611 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
612 int std_out[2]; /* pipe from interp stdout */
613 int std_err[2]; /* pipe from interp stderr */
616 #define NUM_GS_ARGS (NUM_ARGS - 20)
617 #define NUM_ALPHA_ARGS 10
619 char *argv[NUM_ARGS], *dir, *gv_env;
620 char **gs_args, **alpha_args = NULL;
623 LOG ("Start the interpreter");
628 stop_interpreter(gs);
630 /* set up the args... */
631 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
632 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
633 argv[argc] = gs_args[i];
636 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
637 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
638 argv[argc] = alpha_args[i];
641 argv[argc++] = "-dNOPAUSE";
642 argv[argc++] = "-dQUIET";
643 argv[argc++] = "-dSAFER";
645 /* set up the pipes */
646 if (gs->send_filename_to_gs) {
647 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
649 argv[argc++] = "quit";
656 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
657 if (pipe (std_in) == -1) {
658 g_critical ("Unable to open pipe to Ghostscript.");
663 if (pipe (std_out) == -1) {
668 if (pipe(std_err) == -1) {
670 close_pipe (std_out);
674 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
675 gdk_x11_drawable_get_xid (gs->pstarget),
676 gdk_x11_drawable_get_xid (gs->bpixmap));
677 LOG ("Launching ghostview with env %s", gv_env);
679 gs->interpreter_pid = fork ();
680 switch (gs->interpreter_pid) {
683 close_pipe (std_out);
684 close_pipe (std_err);
689 dup2 (std_out[1], 1);
693 dup2 (std_err[1], 2);
696 if (!gs->reading_from_pipe) {
697 if (gs->send_filename_to_gs) {
699 /* just in case gs tries to read from stdin */
700 stdinfd = open("/dev/null", O_RDONLY);
714 /* change to directory where the input file is. This helps
715 * with postscript-files which include other files using
717 dir = g_path_get_dirname (gs->gs_filename);
721 execvp (argv[0], argv);
724 g_critical ("Unable to execute [%s]\n", argv[0]);
725 g_strfreev (gs_args);
727 g_strfreev (alpha_args);
730 default: /* parent */
731 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
734 /* use non-blocking IO for pipe to ghostscript */
735 result = fcntl (std_in[1], F_GETFL, 0);
736 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
737 gs->interpreter_input = std_in[1];
739 gs->interpreter_input = -1;
743 gs->interpreter_output = std_out[0];
745 gs->interpreter_err = std_err[0];
746 gs->interpreter_output_id =
747 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
748 gs->interpreter_error_id =
749 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
757 stop_interpreter(PSDocument * gs)
759 if (gs->interpreter_pid > 0) {
761 LOG ("Stop the interpreter");
762 kill (gs->interpreter_pid, SIGTERM);
763 while ((wait(&status) == -1) && (errno == EINTR));
764 gs->interpreter_pid = -1;
766 gs->gs_status = _("Interpreter failed.");
770 if (gs->interpreter_input >= 0) {
771 close (gs->interpreter_input);
772 gs->interpreter_input = -1;
773 if (gs->interpreter_input_id != 0) {
774 gdk_input_remove(gs->interpreter_input_id);
775 gs->interpreter_input_id = 0;
777 while (gs->ps_input) {
778 struct record_list *ps_old = gs->ps_input;
779 gs->ps_input = gs->ps_input->next;
780 if (ps_old->close && NULL != ps_old->fp)
786 if (gs->interpreter_output >= 0) {
787 close (gs->interpreter_output);
788 gs->interpreter_output = -1;
789 if (gs->interpreter_output_id) {
790 gdk_input_remove (gs->interpreter_output_id);
791 gs->interpreter_output_id = 0;
795 if (gs->interpreter_err >= 0) {
796 close (gs->interpreter_err);
797 gs->interpreter_err = -1;
798 if (gs->interpreter_error_id) {
799 gdk_input_remove (gs->interpreter_error_id);
800 gs->interpreter_error_id = 0;
807 /* If file exists and is a regular file then return its length, else -1 */
809 file_length (const gchar * filename)
811 struct stat stat_rec;
813 if (filename && (stat (filename, &stat_rec) == 0) && S_ISREG (stat_rec.st_mode))
814 return stat_rec.st_size;
819 /* Test if file exists, is a regular file and its length is > 0 */
821 file_readable(const char *filename)
823 return (file_length (filename) > 0);
827 * Decompress gs->gs_filename if necessary
828 * Set gs->filename_unc to the name of the uncompressed file or NULL.
829 * Error reporting via signal 'interpreter_message'
830 * Return name of input file to use or NULL on error..
833 check_filecompressed (PSDocument * gs)
837 gchar *filename, *filename_unc, *filename_err, *cmdline;
843 if ((file = fopen(gs->gs_filename, "r")) &&
844 (fread (buf, sizeof(gchar), 3, file) == 3)) {
845 if ((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
846 /* file is gzipped or compressed */
847 cmd = gtk_gs_defaults_get_ungzip_cmd ();
848 } else if (strncmp (buf, "BZh", 3) == 0) {
849 /* file is compressed with bzip2 */
850 cmd = gtk_gs_defaults_get_unbzip2_cmd ();
858 return gs->gs_filename;
860 /* do the decompression */
861 filename = g_shell_quote (gs->gs_filename);
862 filename_unc = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
863 if ((fd = mkstemp (filename_unc)) < 0) {
864 g_free (filename_unc);
870 filename_err = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
871 if ((fd = mkstemp(filename_err)) < 0) {
872 g_free (filename_err);
873 g_free (filename_unc);
879 cmdline = g_strdup_printf ("%s %s >%s 2>%s", cmd,
880 filename, filename_unc, filename_err);
881 if (system (cmdline) == 0 &&
882 file_readable (filename_unc) &&
883 file_length (filename_err) == 0) {
884 /* sucessfully uncompressed file */
885 gs->gs_filename_unc = filename_unc;
891 filename_dsp = g_filename_display_name (gs->gs_filename);
892 msg = g_strdup_printf (_("Error while decompressing file %s:\n"), filename_dsp);
893 g_free (filename_dsp);
895 interpreter_failed (gs, msg);
897 unlink (filename_unc);
898 g_free (filename_unc);
902 unlink (filename_err);
903 g_free (filename_err);
911 ps_document_enable_interpreter(PSDocument *gs)
913 g_return_val_if_fail (PS_IS_DOCUMENT (gs), FALSE);
915 if (!gs->gs_filename)
918 return start_interpreter (gs);
922 document_load (PSDocument *gs, const gchar *fname)
924 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
926 LOG ("Load the document");
933 /* prepare this document */
934 gs->structured_doc = FALSE;
935 gs->send_filename_to_gs = TRUE;
936 gs->gs_filename = g_strdup (fname);
938 if ((gs->reading_from_pipe = (strcmp (fname, "-") == 0))) {
939 gs->send_filename_to_gs = FALSE;
942 * We need to make sure that the file is loadable/exists!
943 * otherwise we want to exit without loading new stuff...
945 gchar *filename = NULL;
947 if (!file_readable(fname)) {
951 filename_dsp = g_filename_display_name (fname);
952 msg = g_strdup_printf (_("Cannot open file %s.\n"), filename_dsp);
953 g_free (filename_dsp);
955 interpreter_failed (gs, msg);
957 gs->gs_status = _("File is not readable.");
959 filename = check_filecompressed(gs);
962 if (!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
963 interpreter_failed (gs, NULL);
967 /* we grab the vital statistics!!! */
968 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
970 if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
971 (gs->doc->epsf && gs->doc->numpages > 1)) {
972 gs->structured_doc = TRUE;
973 gs->send_filename_to_gs = FALSE;
977 gs->gs_status = _("Document loaded.");
983 ps_document_next_page (PSDocument *gs)
987 LOG ("Make ghostscript render next page");
989 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
990 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
991 g_return_val_if_fail (gs->busy != TRUE, FALSE);
995 event.xclient.type = ClientMessage;
996 event.xclient.display = gdk_display;
997 event.xclient.window = gs->message_window;
998 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
999 event.xclient.format = 32;
1001 gdk_error_trap_push ();
1002 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1004 gdk_error_trap_pop ();
1010 render_page (PSDocument *gs, int page)
1012 g_return_val_if_fail(gs != NULL, FALSE);
1013 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1015 if(!gs->gs_filename) {
1019 if (gs->structured_doc && gs->doc) {
1020 LOG ("It's a structured document, let's send one page to gs");
1022 if (is_interpreter_ready (gs)) {
1023 ps_document_next_page (gs);
1025 ps_document_enable_interpreter (gs);
1026 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1027 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1030 send_ps (gs, gs->doc->pages[page].begin,
1031 gs->doc->pages[page].len, FALSE);
1033 /* Unstructured document
1035 * In the case of non structured documents,
1036 * GS read the PS from the actual file (via command
1037 * line. Hence, ggv only send a signal next page.
1038 * If ghostview is not running it is usually because
1039 * the last page of the file was displayed. In that
1040 * case, ggv restarts GS again and the first page is displayed.
1043 LOG ("It's an unstructured document, gs will just read the file");
1045 if (!is_interpreter_ready (gs)) {
1046 ps_document_enable_interpreter(gs);
1048 ps_document_next_page(gs);
1055 ps_document_load (EvDocument *document,
1062 filename = g_filename_from_uri (uri, NULL, error);
1066 result = document_load (PS_DOCUMENT (document), filename);
1068 gchar *filename_dsp;
1070 filename_dsp = g_filename_display_name (filename);
1072 g_set_error (error, G_FILE_ERROR,
1073 G_FILE_ERROR_FAILED,
1074 _("Failed to load document '%s'"),
1077 g_free (filename_dsp);
1086 save_page_list (PSDocument *document, int *page_list, const char *filename)
1088 gboolean result = TRUE;
1089 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1093 pscopydoc (sink, PS_DOCUMENT_GET_PS_FILE(document),
1094 document->doc, page_list);
1096 buf = gtk_gs_doc_sink_get_buffer (sink);
1098 f = fopen (filename, "w");
1107 gtk_gs_doc_sink_free (sink);
1114 ps_document_save (EvDocument *document,
1118 PSDocument *ps = PS_DOCUMENT (document);
1124 filename = g_filename_from_uri (uri, NULL, error);
1128 page_list = g_new0 (int, ps->doc->numpages);
1129 for (i = 0; i < ps->doc->numpages; i++) {
1133 result = save_page_list (ps, page_list, filename);
1142 ps_document_get_n_pages (EvDocument *document)
1144 PSDocument *ps = PS_DOCUMENT (document);
1146 g_return_val_if_fail (ps != NULL, -1);
1148 if (!ps->gs_filename || !ps->doc) {
1152 return ps->structured_doc ? ps->doc->numpages : 1;
1156 ps_document_get_page_size (EvDocument *document,
1161 PSDocument *gs = PS_DOCUMENT (document);
1162 int urx, ury, llx, lly;
1164 get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
1167 *width = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
1171 *height = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
1176 ps_document_can_get_text (EvDocument *document)
1182 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer, int page, double scale, int rotation)
1184 PSDocument *gs = PS_DOCUMENT (renderer);
1186 if (gs->pstarget == NULL) {
1187 gs->target_window = gtk_window_new (GTK_WINDOW_POPUP);
1188 gtk_widget_realize (gs->target_window);
1189 gs->pstarget = gs->target_window->window;
1191 g_assert (gs->pstarget != NULL);
1193 g_signal_connect (gs->target_window, "event",
1194 G_CALLBACK (ps_document_widget_event),
1198 setup_pixmap (gs, page, scale, rotation);
1199 setup_page (gs, page, scale, rotation);
1201 render_page (gs, page);
1204 static EvDocumentInfo *
1205 ps_document_get_info (EvDocument *document)
1207 EvDocumentInfo *info;
1208 PSDocument *ps = PS_DOCUMENT (document);
1210 info = g_new0 (EvDocumentInfo, 1);
1211 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
1212 EV_DOCUMENT_INFO_FORMAT |
1213 EV_DOCUMENT_INFO_CREATOR |
1214 EV_DOCUMENT_INFO_N_PAGES;
1215 info->title = g_strdup (ps->doc->title);
1216 info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
1217 : g_strdup (_("PostScript"));
1218 info->creator = g_strdup (ps->doc->creator);
1219 info->n_pages = ev_document_get_n_pages (document);
1225 ps_document_document_iface_init (EvDocumentIface *iface)
1227 iface->load = ps_document_load;
1228 iface->save = ps_document_save;
1229 iface->can_get_text = ps_document_can_get_text;
1230 iface->get_n_pages = ps_document_get_n_pages;
1231 iface->get_page_size = ps_document_get_page_size;
1232 iface->get_info = ps_document_get_info;
1236 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
1238 iface->render_pixbuf = ps_async_renderer_render_pixbuf;
1242 ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
1243 int first_page, int last_page,
1244 double width, double height, gboolean duplex)
1246 PSDocument *document = PS_DOCUMENT (exporter);
1248 g_free (document->ps_export_pagelist);
1250 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1251 document->ps_export_filename = g_strdup (filename);
1255 ps_document_ps_export_do_page (EvPSExporter *exporter, EvRenderContext *rc)
1257 PSDocument *document = PS_DOCUMENT (exporter);
1259 document->ps_export_pagelist[rc->page] = 1;
1263 ps_document_ps_export_end (EvPSExporter *exporter)
1265 PSDocument *document = PS_DOCUMENT (exporter);
1267 save_page_list (document, document->ps_export_pagelist,
1268 document->ps_export_filename);
1270 g_free (document->ps_export_pagelist);
1271 g_free (document->ps_export_filename);
1272 document->ps_export_pagelist = NULL;
1273 document->ps_export_filename = NULL;
1277 ps_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1279 iface->begin = ps_document_ps_export_begin;
1280 iface->do_page = ps_document_ps_export_do_page;
1281 iface->end = ps_document_ps_export_end;