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"
47 #include "gsdefaults.h"
48 #include "ev-file-exporter.h"
49 #include "ev-async-renderer.h"
51 #define MAX_BUFSIZE 1024
53 #define PS_DOCUMENT_IS_COMPRESSED(gs) (PS_DOCUMENT(gs)->gs_filename_unc != NULL)
54 #define PS_DOCUMENT_GET_PS_FILE(gs) (PS_DOCUMENT_IS_COMPRESSED(gs) ? \
55 PS_DOCUMENT(gs)->gs_filename_unc : \
56 PS_DOCUMENT(gs)->gs_filename)
58 /* structure to describe section of file to send to ghostscript */
66 struct record_list *next;
69 static gboolean broken_pipe = FALSE;
71 /* Forward declarations */
72 static void ps_document_init (PSDocument *gs);
73 static void ps_document_class_init (PSDocumentClass *klass);
74 static void send_ps (PSDocument *gs,
78 static void output (gpointer data,
80 GdkInputCondition condition);
81 static void input (gpointer data,
83 GdkInputCondition condition);
84 static void stop_interpreter (PSDocument *gs);
85 static gint start_interpreter (PSDocument *gs);
86 static void ps_document_document_iface_init (EvDocumentIface *iface);
87 static void ps_document_file_exporter_iface_init (EvFileExporterIface *iface);
88 static void ps_async_renderer_iface_init (EvAsyncRendererIface *iface);
90 G_DEFINE_TYPE_WITH_CODE (PSDocument, ps_document, G_TYPE_OBJECT,
92 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
93 ps_document_document_iface_init);
94 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
95 ps_document_file_exporter_iface_init);
96 G_IMPLEMENT_INTERFACE (EV_TYPE_ASYNC_RENDERER,
97 ps_async_renderer_iface_init);
100 static GObjectClass *parent_class = NULL;
101 static PSDocumentClass *gs_class = NULL;
104 ps_document_init (PSDocument *gs)
108 gs->interpreter_pid = -1;
112 gs->gs_filename_unc = 0;
116 gs->structured_doc = FALSE;
117 gs->reading_from_pipe = FALSE;
118 gs->send_filename_to_gs = FALSE;
122 gs->interpreter_input = -1;
123 gs->interpreter_output = -1;
124 gs->interpreter_err = -1;
125 gs->interpreter_input_id = 0;
126 gs->interpreter_output_id = 0;
127 gs->interpreter_error_id = 0;
130 gs->input_buffer = NULL;
131 gs->input_buffer_ptr = NULL;
133 gs->buffer_bytes_left = 0;
135 gs->gs_status = _("No document loaded.");
137 gs->ps_export_pagelist = NULL;
138 gs->ps_export_filename = NULL;
142 ps_document_dispose (GObject *object)
144 PSDocument *gs = PS_DOCUMENT (object);
146 g_return_if_fail (gs != NULL);
149 fclose (gs->gs_psfile);
150 gs->gs_psfile = NULL;
153 if (gs->gs_filename) {
154 g_free (gs->gs_filename);
155 gs->gs_filename = NULL;
163 if (gs->gs_filename_unc) {
164 unlink(gs->gs_filename_unc);
165 g_free(gs->gs_filename_unc);
166 gs->gs_filename_unc = NULL;
170 gdk_drawable_unref (gs->bpixmap);
173 if(gs->input_buffer) {
174 g_free(gs->input_buffer);
175 gs->input_buffer = NULL;
178 if (gs->target_window) {
179 gtk_widget_destroy (gs->target_window);
180 gs->target_window = NULL;
184 stop_interpreter (gs);
186 G_OBJECT_CLASS (parent_class)->dispose (object);
190 ps_document_class_init(PSDocumentClass *klass)
192 GObjectClass *object_class;
194 object_class = (GObjectClass *) klass;
195 parent_class = g_type_class_peek_parent (klass);
198 object_class->dispose = ps_document_dispose;
200 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
201 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
202 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
203 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
207 push_pixbuf (PSDocument *gs)
213 if (gs->pstarget == NULL)
216 cmap = gdk_window_get_colormap (gs->pstarget);
217 gdk_drawable_get_size (gs->bpixmap, &width, &height);
218 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
221 g_signal_emit_by_name (gs, "render_finished", pixbuf);
222 g_object_unref (pixbuf);
226 interpreter_failed (PSDocument *gs, char *msg)
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) {
253 send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
255 struct record_list *ps_new;
257 if (gs->interpreter_input < 0) {
258 g_critical("No pipe to gs: error in send_ps().");
262 ps_new = g_new0 (struct record_list, 1);
263 ps_new->fp = gs->gs_psfile;
264 ps_new->begin = begin;
266 ps_new->seek_needed = TRUE;
267 ps_new->close = close;
270 if (gs->input_buffer == NULL) {
271 gs->input_buffer = g_malloc(MAX_BUFSIZE);
274 if (gs->ps_input == NULL) {
275 gs->input_buffer_ptr = gs->input_buffer;
276 gs->bytes_left = len;
277 gs->buffer_bytes_left = 0;
278 gs->ps_input = ps_new;
279 gs->interpreter_input_id = gdk_input_add
280 (gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
282 struct record_list *p = gs->ps_input;
283 while (p->next != NULL) {
291 setup_pixmap (PSDocument *gs, int page, double scale, int rotation)
294 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
295 GdkColormap *colormap;
296 double width, height;
297 int pixmap_width, pixmap_height;
299 if (gs->pstarget == NULL)
302 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
304 if (rotation == 90 || rotation == 270) {
305 pixmap_height = width * scale + 0.5;
306 pixmap_width = height * scale + 0.5;
308 pixmap_width = width * scale + 0.5;
309 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);
326 fill = gdk_gc_new (gs->pstarget);
327 colormap = gdk_drawable_get_colormap (gs->pstarget);
328 gdk_color_alloc (colormap, &white);
329 gdk_gc_set_foreground (fill, &white);
330 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
332 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
333 0, 0, pixmap_width, pixmap_height);
337 #define DEFAULT_PAGE_SIZE 1
340 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
346 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
347 int new_pagesize = -1;
349 g_return_if_fail (PS_IS_DOCUMENT (gs));
351 if (new_pagesize == -1) {
352 new_pagesize = DEFAULT_PAGE_SIZE;
354 /* If we have a document:
355 * We use -- the page size (if specified)
356 * or the doc. size (if specified)
357 * or the page bbox (if specified)
358 * or the bounding box
360 if ((page >= 0) && (gs->doc->numpages > page) &&
361 (gs->doc->pages) && (gs->doc->pages[page].size)) {
362 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
363 } else if (gs->doc->default_page_size != NULL) {
364 new_pagesize = gs->doc->default_page_size - gs->doc->size;
365 } else if ((page >= 0) &&
366 (gs->doc->numpages > page) &&
368 (gs->doc->pages[page].boundingbox[URX] >
369 gs->doc->pages[page].boundingbox[LLX]) &&
370 (gs->doc->pages[page].boundingbox[URY] >
371 gs->doc->pages[page].boundingbox[LLY])) {
373 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
374 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
380 /* Compute bounding box */
381 if (gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
384 (gs->doc->pages[page].boundingbox[URX] >
385 gs->doc->pages[page].boundingbox[LLX]) &&
386 (gs->doc->pages[page].boundingbox[URY] >
387 gs->doc->pages[page].boundingbox[LLY])) {
389 new_llx = gs->doc->pages[page].boundingbox[LLX];
390 new_lly = gs->doc->pages[page].boundingbox[LLY];
391 new_urx = gs->doc->pages[page].boundingbox[URX];
392 new_ury = gs->doc->pages[page].boundingbox[URY];
393 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
394 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
396 new_llx = gs->doc->boundingbox[LLX];
397 new_lly = gs->doc->boundingbox[LLY];
398 new_urx = gs->doc->boundingbox[URX];
399 new_ury = gs->doc->boundingbox[URY];
402 if (new_pagesize < 0)
403 new_pagesize = DEFAULT_PAGE_SIZE;
404 new_llx = new_lly = 0;
405 if (gs->doc && gs->doc->size &&
406 (new_pagesize < gs->doc->numsizes)) {
407 new_urx = gs->doc->size[new_pagesize].width;
408 new_ury = gs->doc->size[new_pagesize].height;
410 new_urx = papersizes[new_pagesize].width;
411 new_ury = papersizes[new_pagesize].height;
415 if (new_urx <= new_llx)
416 new_urx = papersizes[12].width;
417 if (new_ury <= new_lly)
418 new_ury = papersizes[12].height;
427 setup_page (PSDocument *gs, int page, double scale, int rotation)
430 char scaled_dpi[G_ASCII_DTOSTR_BUF_SIZE];
431 int urx, ury, llx, lly;
433 get_page_box (gs, page, &urx, &ury, &llx, &lly);
434 g_ascii_dtostr (scaled_dpi, G_ASCII_DTOSTR_BUF_SIZE, 72.0 * scale);
436 buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
437 0L, rotation, llx, lly, urx, ury,
438 scaled_dpi, scaled_dpi,
441 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
442 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
449 close_pipe (int p[2])
460 is_interpreter_ready (PSDocument *gs)
462 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
466 output (gpointer data, gint source, GdkInputCondition condition)
468 char buf[MAX_BUFSIZE + 1];
470 PSDocument *gs = PS_DOCUMENT(data);
472 if (source == gs->interpreter_output) {
473 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
474 if (bytes == 0) { /* EOF occurred */
475 close (gs->interpreter_output);
476 gs->interpreter_output = -1;
477 gdk_input_remove (gs->interpreter_output_id);
479 } else if (bytes == -1) {
481 interpreter_failed (gs, NULL);
484 if (gs->interpreter_err == -1) {
485 interpreter_failed (gs, NULL);
487 } else if (source == gs->interpreter_err) {
488 bytes = read (gs->interpreter_err, buf, MAX_BUFSIZE);
489 if (bytes == 0) { /* EOF occurred */
490 close (gs->interpreter_err);
491 gs->interpreter_err = -1;
492 gdk_input_remove (gs->interpreter_error_id);
494 } else if (bytes == -1) {
496 interpreter_failed (gs, NULL);
499 if (gs->interpreter_output == -1) {
500 interpreter_failed(gs, NULL);
517 input(gpointer data, gint source, GdkInputCondition condition)
519 PSDocument *gs = PS_DOCUMENT(data);
521 void (*oldsig) (int);
522 oldsig = signal(SIGPIPE, catchPipe);
525 if (gs->buffer_bytes_left == 0) {
526 /* Get a new section if required */
527 if (gs->ps_input && gs->bytes_left == 0) {
528 struct record_list *ps_old = gs->ps_input;
529 gs->ps_input = ps_old->next;
530 if (ps_old->close && NULL != ps_old->fp)
535 /* Have to seek at the beginning of each section */
536 if (gs->ps_input && gs->ps_input->seek_needed) {
537 fseek (gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
538 gs->ps_input->seek_needed = FALSE;
539 gs->bytes_left = gs->ps_input->len;
542 if (gs->bytes_left > MAX_BUFSIZE) {
543 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
544 MAX_BUFSIZE, gs->ps_input->fp);
545 } else if (gs->bytes_left > 0) {
546 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
547 gs->bytes_left, gs->ps_input->fp);
549 gs->buffer_bytes_left = 0;
551 if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
552 interpreter_failed (gs, NULL); /* Error occurred */
554 gs->input_buffer_ptr = gs->input_buffer;
555 gs->bytes_left -= gs->buffer_bytes_left;
558 if (gs->buffer_bytes_left > 0) {
559 bytes_written = write (gs->interpreter_input,
560 gs->input_buffer_ptr, gs->buffer_bytes_left);
563 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
565 interpreter_failed (gs, NULL);
566 } else if (bytes_written == -1) {
567 if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
568 interpreter_failed (gs, NULL); /* Something bad happened */
571 gs->buffer_bytes_left -= bytes_written;
572 gs->input_buffer_ptr += bytes_written;
575 } while (gs->ps_input && gs->buffer_bytes_left == 0);
577 signal (SIGPIPE, oldsig);
579 if (gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
580 if (gs->interpreter_input_id != 0) {
581 gdk_input_remove (gs->interpreter_input_id);
582 gs->interpreter_input_id = 0;
588 start_interpreter (PSDocument *gs)
590 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
591 int std_out[2]; /* pipe from interp stdout */
592 int std_err[2]; /* pipe from interp stderr */
595 #define NUM_GS_ARGS (NUM_ARGS - 20)
596 #define NUM_ALPHA_ARGS 10
598 char *argv[NUM_ARGS], *dir, *gv_env, *gs_path;
599 char **gs_args, **alpha_args = NULL;
600 char **gv_env_vars = NULL;
606 stop_interpreter(gs);
608 /* set up the args... */
609 gs_path = g_find_program_in_path ("gs");
610 gs_args = g_strsplit (gs_path, " ", NUM_GS_ARGS);
612 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
613 argv[argc] = gs_args[i];
616 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
617 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
618 argv[argc] = alpha_args[i];
621 argv[argc++] = "-dNOPAUSE";
622 argv[argc++] = "-dQUIET";
623 argv[argc++] = "-dSAFER";
625 /* set up the pipes */
626 if (gs->send_filename_to_gs) {
627 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
629 argv[argc++] = "quit";
636 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
637 if (pipe (std_in) == -1) {
638 g_critical ("Unable to open pipe to Ghostscript.");
643 if (pipe (std_out) == -1) {
648 if (pipe(std_err) == -1) {
650 close_pipe (std_out);
654 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld;DISPLAY=%s",
655 gdk_x11_drawable_get_xid (gs->pstarget),
656 gdk_x11_drawable_get_xid (gs->bpixmap),
657 gdk_display_get_name (gdk_drawable_get_display (gs->pstarget)));
659 gs->interpreter_pid = fork ();
660 switch (gs->interpreter_pid) {
663 close_pipe (std_out);
664 close_pipe (std_err);
669 dup2 (std_out[1], 1);
673 dup2 (std_err[1], 2);
676 if (!gs->reading_from_pipe) {
677 if (gs->send_filename_to_gs) {
679 /* just in case gs tries to read from stdin */
680 stdinfd = open("/dev/null", O_RDONLY);
692 gv_env_vars = g_strsplit (gv_env, ";", -1);
694 for (i = 0; gv_env_vars[i]; i++) {
695 putenv (gv_env_vars[i]);
698 /* change to directory where the input file is. This helps
699 * with postscript-files which include other files using
701 dir = g_path_get_dirname (gs->gs_filename);
705 execvp (argv[0], argv);
708 g_critical ("Unable to execute [%s]\n", argv[0]);
709 g_strfreev (gs_args);
710 g_strfreev (alpha_args);
711 g_strfreev (gv_env_vars);
714 default: /* parent */
715 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
718 /* use non-blocking IO for pipe to ghostscript */
719 result = fcntl (std_in[1], F_GETFL, 0);
720 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
721 gs->interpreter_input = std_in[1];
723 gs->interpreter_input = -1;
727 gs->interpreter_output = std_out[0];
729 gs->interpreter_err = std_err[0];
730 gs->interpreter_output_id =
731 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
732 gs->interpreter_error_id =
733 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
745 stop_interpreter(PSDocument * gs)
747 if (gs->interpreter_pid > 0) {
749 kill (gs->interpreter_pid, SIGTERM);
750 while ((wait(&status) == -1) && (errno == EINTR));
751 gs->interpreter_pid = -1;
753 gs->gs_status = _("Interpreter failed.");
757 if (gs->interpreter_input >= 0) {
758 close (gs->interpreter_input);
759 gs->interpreter_input = -1;
760 if (gs->interpreter_input_id != 0) {
761 gdk_input_remove(gs->interpreter_input_id);
762 gs->interpreter_input_id = 0;
764 while (gs->ps_input) {
765 struct record_list *ps_old = gs->ps_input;
766 gs->ps_input = gs->ps_input->next;
767 if (ps_old->close && NULL != ps_old->fp)
773 if (gs->interpreter_output >= 0) {
774 close (gs->interpreter_output);
775 gs->interpreter_output = -1;
776 if (gs->interpreter_output_id) {
777 gdk_input_remove (gs->interpreter_output_id);
778 gs->interpreter_output_id = 0;
782 if (gs->interpreter_err >= 0) {
783 close (gs->interpreter_err);
784 gs->interpreter_err = -1;
785 if (gs->interpreter_error_id) {
786 gdk_input_remove (gs->interpreter_error_id);
787 gs->interpreter_error_id = 0;
794 /* If file exists and is a regular file then return its length, else -1 */
796 file_length (const gchar * filename)
798 struct stat stat_rec;
800 if (filename && (stat (filename, &stat_rec) == 0) && S_ISREG (stat_rec.st_mode))
801 return stat_rec.st_size;
806 /* Test if file exists, is a regular file and its length is > 0 */
808 file_readable(const char *filename)
810 return (file_length (filename) > 0);
814 * Decompress gs->gs_filename if necessary
815 * Set gs->filename_unc to the name of the uncompressed file or NULL.
816 * Error reporting via signal 'interpreter_message'
817 * Return name of input file to use or NULL on error..
820 check_filecompressed (PSDocument * gs)
824 gchar *filename, *filename_unc, *filename_err, *cmdline;
830 if ((file = fopen(gs->gs_filename, "r")) &&
831 (fread (buf, sizeof(gchar), 3, file) == 3)) {
832 if ((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
833 /* file is gzipped or compressed */
834 cmd = gtk_gs_defaults_get_ungzip_cmd ();
835 } else if (strncmp (buf, "BZh", 3) == 0) {
836 /* file is compressed with bzip2 */
837 cmd = gtk_gs_defaults_get_unbzip2_cmd ();
845 return gs->gs_filename;
847 /* do the decompression */
848 filename = g_shell_quote (gs->gs_filename);
849 filename_unc = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
850 if ((fd = mkstemp (filename_unc)) < 0) {
851 g_free (filename_unc);
857 filename_err = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
858 if ((fd = mkstemp(filename_err)) < 0) {
859 g_free (filename_err);
860 g_free (filename_unc);
866 cmdline = g_strdup_printf ("%s %s >%s 2>%s", cmd,
867 filename, filename_unc, filename_err);
868 if (system (cmdline) == 0 &&
869 file_readable (filename_unc) &&
870 file_length (filename_err) == 0) {
871 /* sucessfully uncompressed file */
872 gs->gs_filename_unc = filename_unc;
878 filename_dsp = g_filename_display_name (gs->gs_filename);
879 msg = g_strdup_printf (_("Error while decompressing file “%s”:\n"), filename_dsp);
880 g_free (filename_dsp);
882 interpreter_failed (gs, msg);
884 unlink (filename_unc);
885 g_free (filename_unc);
889 unlink (filename_err);
890 g_free (filename_err);
898 ps_document_enable_interpreter(PSDocument *gs)
900 g_return_val_if_fail (PS_IS_DOCUMENT (gs), FALSE);
902 if (!gs->gs_filename)
905 return start_interpreter (gs);
909 document_load (PSDocument *gs, const gchar *fname)
911 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
918 /* prepare this document */
919 gs->structured_doc = FALSE;
920 gs->send_filename_to_gs = TRUE;
921 gs->gs_filename = g_strdup (fname);
923 if ((gs->reading_from_pipe = (strcmp (fname, "-") == 0))) {
924 gs->send_filename_to_gs = FALSE;
927 * We need to make sure that the file is loadable/exists!
928 * otherwise we want to exit without loading new stuff...
930 gchar *filename = NULL;
932 if (!file_readable(fname)) {
936 filename_dsp = g_filename_display_name (fname);
937 msg = g_strdup_printf (_("Cannot open file “%s”.\n"), filename_dsp);
938 g_free (filename_dsp);
940 interpreter_failed (gs, msg);
942 gs->gs_status = _("File is not readable.");
944 filename = check_filecompressed(gs);
947 if (!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
948 interpreter_failed (gs, NULL);
952 /* we grab the vital statistics!!! */
953 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
955 if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
956 (gs->doc->epsf && gs->doc->numpages > 1)) {
957 gs->structured_doc = TRUE;
958 gs->send_filename_to_gs = FALSE;
962 gs->gs_status = _("Document loaded.");
968 ps_document_next_page (PSDocument *gs)
975 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
976 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
977 g_return_val_if_fail (gs->busy != TRUE, FALSE);
981 screen = gtk_window_get_screen (GTK_WINDOW (gs->target_window));
982 display = gdk_screen_get_display (screen);
983 dpy = gdk_x11_display_get_xdisplay (display);
985 event.xclient.type = ClientMessage;
986 event.xclient.display = dpy;
987 event.xclient.window = gs->message_window;
988 event.xclient.message_type =
989 gdk_x11_atom_to_xatom_for_display (display,
990 gs_class->next_atom);
991 event.xclient.format = 32;
993 gdk_error_trap_push ();
994 XSendEvent (dpy, gs->message_window, FALSE, 0, &event);
996 gdk_error_trap_pop ();
1002 render_page (PSDocument *gs, int page)
1004 g_return_val_if_fail(gs != NULL, FALSE);
1005 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1007 if(!gs->gs_filename) {
1011 if (gs->structured_doc && gs->doc) {
1013 if (is_interpreter_ready (gs)) {
1014 ps_document_next_page (gs);
1016 ps_document_enable_interpreter (gs);
1017 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1018 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1021 send_ps (gs, gs->doc->pages[page].begin,
1022 gs->doc->pages[page].len, FALSE);
1024 /* Unstructured document
1026 * In the case of non structured documents,
1027 * GS read the PS from the actual file (via command
1028 * line. Hence, ggv only send a signal next page.
1029 * If ghostview is not running it is usually because
1030 * the last page of the file was displayed. In that
1031 * case, ggv restarts GS again and the first page is displayed.
1034 if (!is_interpreter_ready (gs)) {
1035 ps_document_enable_interpreter(gs);
1037 ps_document_next_page(gs);
1044 ps_document_load (EvDocument *document,
1052 filename = g_filename_from_uri (uri, NULL, error);
1056 gs_path = g_find_program_in_path ("gs");
1058 gchar *filename_dsp;
1059 filename_dsp = g_filename_display_name (filename);
1063 _("Failed to load document “%s”. Ghostscript interpreter was not found in path"),
1065 g_free (filename_dsp);
1068 result = document_load (PS_DOCUMENT (document), filename);
1070 gchar *filename_dsp;
1071 filename_dsp = g_filename_display_name (filename);
1073 g_set_error (error, G_FILE_ERROR,
1074 G_FILE_ERROR_FAILED,
1075 _("Failed to load document “%s”"),
1077 g_free (filename_dsp);
1087 save_document (PSDocument *document, const char *filename)
1089 gboolean result = TRUE;
1090 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1094 src_file = fopen (PS_DOCUMENT_GET_PS_FILE(document), "r");
1096 struct stat stat_rec;
1098 if (stat (PS_DOCUMENT_GET_PS_FILE(document), &stat_rec) == 0) {
1099 pscopy (src_file, sink, 0, stat_rec.st_size - 1);
1105 buf = gtk_gs_doc_sink_get_buffer (sink);
1110 f = fopen (filename, "w");
1119 gtk_gs_doc_sink_free (sink);
1126 save_page_list (PSDocument *document, int *page_list, const char *filename)
1128 gboolean result = TRUE;
1129 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1133 pscopydoc (sink, PS_DOCUMENT_GET_PS_FILE(document),
1134 document->doc, page_list);
1136 buf = gtk_gs_doc_sink_get_buffer (sink);
1138 f = fopen (filename, "w");
1147 gtk_gs_doc_sink_free (sink);
1154 ps_document_save (EvDocument *document,
1158 PSDocument *ps = PS_DOCUMENT (document);
1162 filename = g_filename_from_uri (uri, NULL, error);
1166 result = save_document (ps, filename);
1174 ps_document_get_n_pages (EvDocument *document)
1176 PSDocument *ps = PS_DOCUMENT (document);
1178 g_return_val_if_fail (ps != NULL, -1);
1180 if (!ps->gs_filename || !ps->doc) {
1184 return ps->structured_doc ? ps->doc->numpages : 1;
1188 ps_document_get_page_size (EvDocument *document,
1193 PSDocument *gs = PS_DOCUMENT (document);
1194 int urx, ury, llx, lly;
1196 get_page_box (gs, page, &urx, &ury, &llx, &lly);
1199 *width = (urx - llx) + 0.5;
1203 *height = (ury - lly) + 0.5;
1208 ps_document_can_get_text (EvDocument *document)
1214 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer, int page, double scale, int rotation)
1216 PSDocument *gs = PS_DOCUMENT (renderer);
1218 if (gs->pstarget == NULL) {
1219 gs->target_window = gtk_window_new (GTK_WINDOW_POPUP);
1220 gtk_widget_realize (gs->target_window);
1221 gs->pstarget = gs->target_window->window;
1223 g_assert (gs->pstarget != NULL);
1225 g_signal_connect (gs->target_window, "event",
1226 G_CALLBACK (ps_document_widget_event),
1230 setup_pixmap (gs, page, scale, rotation);
1231 setup_page (gs, page, scale, rotation);
1233 render_page (gs, page);
1236 static EvDocumentInfo *
1237 ps_document_get_info (EvDocument *document)
1239 EvDocumentInfo *info;
1240 PSDocument *ps = PS_DOCUMENT (document);
1241 int urx, ury, llx, lly;
1243 info = g_new0 (EvDocumentInfo, 1);
1244 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
1245 EV_DOCUMENT_INFO_FORMAT |
1246 EV_DOCUMENT_INFO_CREATOR |
1247 EV_DOCUMENT_INFO_N_PAGES |
1248 EV_DOCUMENT_INFO_PAPER_SIZE;
1250 info->title = g_strdup (ps->doc->title);
1251 info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
1252 : g_strdup (_("PostScript"));
1253 info->creator = g_strdup (ps->doc->creator);
1254 info->n_pages = ev_document_get_n_pages (document);
1256 get_page_box (PS_DOCUMENT (document), 0, &urx, &ury, &llx, &lly);
1258 info->paper_width = (urx - llx) / 72.0f * 25.4f;
1259 info->paper_height = (ury - lly) / 72.0f * 25.4f;
1265 ps_document_document_iface_init (EvDocumentIface *iface)
1267 iface->load = ps_document_load;
1268 iface->save = ps_document_save;
1269 iface->can_get_text = ps_document_can_get_text;
1270 iface->get_n_pages = ps_document_get_n_pages;
1271 iface->get_page_size = ps_document_get_page_size;
1272 iface->get_info = ps_document_get_info;
1276 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
1278 iface->render_pixbuf = ps_async_renderer_render_pixbuf;
1282 ps_document_file_exporter_format_supported (EvFileExporter *exporter,
1283 EvFileExporterFormat format)
1285 return (format == EV_FILE_FORMAT_PS);
1289 ps_document_file_exporter_begin (EvFileExporter *exporter,
1290 EvFileExporterFormat format,
1291 const char *filename,
1298 PSDocument *document = PS_DOCUMENT (exporter);
1300 if (document->structured_doc) {
1301 g_free (document->ps_export_pagelist);
1303 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1306 document->ps_export_filename = g_strdup (filename);
1310 ps_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
1312 PSDocument *document = PS_DOCUMENT (exporter);
1314 if (document->structured_doc) {
1315 document->ps_export_pagelist[rc->page] = 1;
1320 ps_document_file_exporter_end (EvFileExporter *exporter)
1322 PSDocument *document = PS_DOCUMENT (exporter);
1324 if (!document->structured_doc) {
1325 save_document (document, document->ps_export_filename);
1327 save_page_list (document, document->ps_export_pagelist,
1328 document->ps_export_filename);
1329 g_free (document->ps_export_pagelist);
1330 g_free (document->ps_export_filename);
1331 document->ps_export_pagelist = NULL;
1332 document->ps_export_filename = NULL;
1337 ps_document_file_exporter_iface_init (EvFileExporterIface *iface)
1339 iface->format_supported = ps_document_file_exporter_format_supported;
1340 iface->begin = ps_document_file_exporter_begin;
1341 iface->do_page = ps_document_file_exporter_do_page;
1342 iface->end = ps_document_file_exporter_end;