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);
741 stop_interpreter(PSDocument * gs)
743 if (gs->interpreter_pid > 0) {
745 kill (gs->interpreter_pid, SIGTERM);
746 while ((wait(&status) == -1) && (errno == EINTR));
747 gs->interpreter_pid = -1;
749 gs->gs_status = _("Interpreter failed.");
753 if (gs->interpreter_input >= 0) {
754 close (gs->interpreter_input);
755 gs->interpreter_input = -1;
756 if (gs->interpreter_input_id != 0) {
757 gdk_input_remove(gs->interpreter_input_id);
758 gs->interpreter_input_id = 0;
760 while (gs->ps_input) {
761 struct record_list *ps_old = gs->ps_input;
762 gs->ps_input = gs->ps_input->next;
763 if (ps_old->close && NULL != ps_old->fp)
769 if (gs->interpreter_output >= 0) {
770 close (gs->interpreter_output);
771 gs->interpreter_output = -1;
772 if (gs->interpreter_output_id) {
773 gdk_input_remove (gs->interpreter_output_id);
774 gs->interpreter_output_id = 0;
778 if (gs->interpreter_err >= 0) {
779 close (gs->interpreter_err);
780 gs->interpreter_err = -1;
781 if (gs->interpreter_error_id) {
782 gdk_input_remove (gs->interpreter_error_id);
783 gs->interpreter_error_id = 0;
790 /* If file exists and is a regular file then return its length, else -1 */
792 file_length (const gchar * filename)
794 struct stat stat_rec;
796 if (filename && (stat (filename, &stat_rec) == 0) && S_ISREG (stat_rec.st_mode))
797 return stat_rec.st_size;
802 /* Test if file exists, is a regular file and its length is > 0 */
804 file_readable(const char *filename)
806 return (file_length (filename) > 0);
810 * Decompress gs->gs_filename if necessary
811 * Set gs->filename_unc to the name of the uncompressed file or NULL.
812 * Error reporting via signal 'interpreter_message'
813 * Return name of input file to use or NULL on error..
816 check_filecompressed (PSDocument * gs)
820 gchar *filename, *filename_unc, *filename_err, *cmdline;
826 if ((file = fopen(gs->gs_filename, "r")) &&
827 (fread (buf, sizeof(gchar), 3, file) == 3)) {
828 if ((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
829 /* file is gzipped or compressed */
830 cmd = gtk_gs_defaults_get_ungzip_cmd ();
831 } else if (strncmp (buf, "BZh", 3) == 0) {
832 /* file is compressed with bzip2 */
833 cmd = gtk_gs_defaults_get_unbzip2_cmd ();
841 return gs->gs_filename;
843 /* do the decompression */
844 filename = g_shell_quote (gs->gs_filename);
845 filename_unc = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
846 if ((fd = mkstemp (filename_unc)) < 0) {
847 g_free (filename_unc);
853 filename_err = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
854 if ((fd = mkstemp(filename_err)) < 0) {
855 g_free (filename_err);
856 g_free (filename_unc);
862 cmdline = g_strdup_printf ("%s %s >%s 2>%s", cmd,
863 filename, filename_unc, filename_err);
864 if (system (cmdline) == 0 &&
865 file_readable (filename_unc) &&
866 file_length (filename_err) == 0) {
867 /* sucessfully uncompressed file */
868 gs->gs_filename_unc = filename_unc;
874 filename_dsp = g_filename_display_name (gs->gs_filename);
875 msg = g_strdup_printf (_("Error while decompressing file “%s”:\n"), filename_dsp);
876 g_free (filename_dsp);
878 interpreter_failed (gs, msg);
880 unlink (filename_unc);
881 g_free (filename_unc);
885 unlink (filename_err);
886 g_free (filename_err);
894 ps_document_enable_interpreter(PSDocument *gs)
896 g_return_val_if_fail (PS_IS_DOCUMENT (gs), FALSE);
898 if (!gs->gs_filename)
901 return start_interpreter (gs);
905 document_load (PSDocument *gs, const gchar *fname)
907 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
914 /* prepare this document */
915 gs->structured_doc = FALSE;
916 gs->send_filename_to_gs = TRUE;
917 gs->gs_filename = g_strdup (fname);
919 if ((gs->reading_from_pipe = (strcmp (fname, "-") == 0))) {
920 gs->send_filename_to_gs = FALSE;
923 * We need to make sure that the file is loadable/exists!
924 * otherwise we want to exit without loading new stuff...
926 gchar *filename = NULL;
928 if (!file_readable(fname)) {
932 filename_dsp = g_filename_display_name (fname);
933 msg = g_strdup_printf (_("Cannot open file “%s”.\n"), filename_dsp);
934 g_free (filename_dsp);
936 interpreter_failed (gs, msg);
938 gs->gs_status = _("File is not readable.");
940 filename = check_filecompressed(gs);
943 if (!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
944 interpreter_failed (gs, NULL);
948 /* we grab the vital statistics!!! */
949 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
951 if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
952 (gs->doc->epsf && gs->doc->numpages > 1)) {
953 gs->structured_doc = TRUE;
954 gs->send_filename_to_gs = FALSE;
958 gs->gs_status = _("Document loaded.");
964 ps_document_next_page (PSDocument *gs)
971 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
972 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
973 g_return_val_if_fail (gs->busy != TRUE, FALSE);
977 screen = gtk_window_get_screen (GTK_WINDOW (gs->target_window));
978 display = gdk_screen_get_display (screen);
979 dpy = gdk_x11_display_get_xdisplay (display);
981 event.xclient.type = ClientMessage;
982 event.xclient.display = dpy;
983 event.xclient.window = gs->message_window;
984 event.xclient.message_type =
985 gdk_x11_atom_to_xatom_for_display (display,
986 gs_class->next_atom);
987 event.xclient.format = 32;
989 gdk_error_trap_push ();
990 XSendEvent (dpy, gs->message_window, FALSE, 0, &event);
992 gdk_error_trap_pop ();
998 render_page (PSDocument *gs, int page)
1000 g_return_val_if_fail(gs != NULL, FALSE);
1001 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1003 if(!gs->gs_filename) {
1007 if (gs->structured_doc && gs->doc) {
1009 if (is_interpreter_ready (gs)) {
1010 ps_document_next_page (gs);
1012 ps_document_enable_interpreter (gs);
1013 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1014 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1017 send_ps (gs, gs->doc->pages[page].begin,
1018 gs->doc->pages[page].len, FALSE);
1020 /* Unstructured document
1022 * In the case of non structured documents,
1023 * GS read the PS from the actual file (via command
1024 * line. Hence, ggv only send a signal next page.
1025 * If ghostview is not running it is usually because
1026 * the last page of the file was displayed. In that
1027 * case, ggv restarts GS again and the first page is displayed.
1030 if (!is_interpreter_ready (gs)) {
1031 ps_document_enable_interpreter(gs);
1033 ps_document_next_page(gs);
1040 ps_document_load (EvDocument *document,
1048 filename = g_filename_from_uri (uri, NULL, error);
1052 gs_path = g_find_program_in_path ("gs");
1054 gchar *filename_dsp;
1055 filename_dsp = g_filename_display_name (filename);
1059 _("Failed to load document “%s”. Ghostscript interpreter was not found in path"),
1061 g_free (filename_dsp);
1064 result = document_load (PS_DOCUMENT (document), filename);
1066 gchar *filename_dsp;
1067 filename_dsp = g_filename_display_name (filename);
1069 g_set_error (error, G_FILE_ERROR,
1070 G_FILE_ERROR_FAILED,
1071 _("Failed to load document “%s”"),
1073 g_free (filename_dsp);
1083 save_document (PSDocument *document, const char *filename)
1085 gboolean result = TRUE;
1086 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1090 src_file = fopen (PS_DOCUMENT_GET_PS_FILE(document), "r");
1092 struct stat stat_rec;
1094 if (stat (PS_DOCUMENT_GET_PS_FILE(document), &stat_rec) == 0) {
1095 pscopy (src_file, sink, 0, stat_rec.st_size - 1);
1101 buf = gtk_gs_doc_sink_get_buffer (sink);
1106 f = fopen (filename, "w");
1115 gtk_gs_doc_sink_free (sink);
1122 save_page_list (PSDocument *document, int *page_list, const char *filename)
1124 gboolean result = TRUE;
1125 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1129 pscopydoc (sink, PS_DOCUMENT_GET_PS_FILE(document),
1130 document->doc, page_list);
1132 buf = gtk_gs_doc_sink_get_buffer (sink);
1134 f = fopen (filename, "w");
1143 gtk_gs_doc_sink_free (sink);
1150 ps_document_save (EvDocument *document,
1154 PSDocument *ps = PS_DOCUMENT (document);
1158 filename = g_filename_from_uri (uri, NULL, error);
1162 result = save_document (ps, filename);
1170 ps_document_get_n_pages (EvDocument *document)
1172 PSDocument *ps = PS_DOCUMENT (document);
1174 g_return_val_if_fail (ps != NULL, -1);
1176 if (!ps->gs_filename || !ps->doc) {
1180 return ps->structured_doc ? ps->doc->numpages : 1;
1184 ps_document_get_page_size (EvDocument *document,
1189 PSDocument *gs = PS_DOCUMENT (document);
1190 int urx, ury, llx, lly;
1192 get_page_box (gs, page, &urx, &ury, &llx, &lly);
1195 *width = (urx - llx) + 0.5;
1199 *height = (ury - lly) + 0.5;
1204 ps_document_can_get_text (EvDocument *document)
1210 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer, int page, double scale, int rotation)
1212 PSDocument *gs = PS_DOCUMENT (renderer);
1214 if (gs->pstarget == NULL) {
1215 gs->target_window = gtk_window_new (GTK_WINDOW_POPUP);
1216 gtk_widget_realize (gs->target_window);
1217 gs->pstarget = gs->target_window->window;
1219 g_assert (gs->pstarget != NULL);
1221 g_signal_connect (gs->target_window, "event",
1222 G_CALLBACK (ps_document_widget_event),
1226 setup_pixmap (gs, page, scale, rotation);
1227 setup_page (gs, page, scale, rotation);
1229 render_page (gs, page);
1232 static EvDocumentInfo *
1233 ps_document_get_info (EvDocument *document)
1235 EvDocumentInfo *info;
1236 PSDocument *ps = PS_DOCUMENT (document);
1237 int urx, ury, llx, lly;
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 EV_DOCUMENT_INFO_PAPER_SIZE;
1246 info->title = g_strdup (ps->doc->title);
1247 info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
1248 : g_strdup (_("PostScript"));
1249 info->creator = g_strdup (ps->doc->creator);
1250 info->n_pages = ev_document_get_n_pages (document);
1252 get_page_box (PS_DOCUMENT (document), 0, &urx, &ury, &llx, &lly);
1254 info->paper_width = (urx - llx) / 72.0f * 25.4f;
1255 info->paper_height = (ury - lly) / 72.0f * 25.4f;
1261 ps_document_document_iface_init (EvDocumentIface *iface)
1263 iface->load = ps_document_load;
1264 iface->save = ps_document_save;
1265 iface->can_get_text = ps_document_can_get_text;
1266 iface->get_n_pages = ps_document_get_n_pages;
1267 iface->get_page_size = ps_document_get_page_size;
1268 iface->get_info = ps_document_get_info;
1272 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
1274 iface->render_pixbuf = ps_async_renderer_render_pixbuf;
1278 ps_document_file_exporter_format_supported (EvFileExporter *exporter,
1279 EvFileExporterFormat format)
1281 return (format == EV_FILE_FORMAT_PS);
1285 ps_document_file_exporter_begin (EvFileExporter *exporter,
1286 EvFileExporterFormat format,
1287 const char *filename,
1294 PSDocument *document = PS_DOCUMENT (exporter);
1296 if (document->structured_doc) {
1297 g_free (document->ps_export_pagelist);
1299 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1302 document->ps_export_filename = g_strdup (filename);
1306 ps_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
1308 PSDocument *document = PS_DOCUMENT (exporter);
1310 if (document->structured_doc) {
1311 document->ps_export_pagelist[rc->page] = 1;
1316 ps_document_file_exporter_end (EvFileExporter *exporter)
1318 PSDocument *document = PS_DOCUMENT (exporter);
1320 if (!document->structured_doc) {
1321 save_document (document, document->ps_export_filename);
1323 save_page_list (document, document->ps_export_pagelist,
1324 document->ps_export_filename);
1325 g_free (document->ps_export_pagelist);
1326 g_free (document->ps_export_filename);
1327 document->ps_export_pagelist = NULL;
1328 document->ps_export_filename = NULL;
1333 ps_document_file_exporter_iface_init (EvFileExporterIface *iface)
1335 iface->format_supported = ps_document_file_exporter_format_supported;
1336 iface->begin = ps_document_file_exporter_begin;
1337 iface->do_page = ps_document_file_exporter_do_page;
1338 iface->end = ps_document_file_exporter_end;