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;
462 LOG ("Setup the page");
465 /* gs needs floating point parameters with '.' as decimal point
466 * while some (european) locales use ',' instead, so we set the
467 * locale for this snprintf to "C".
469 savelocale = setlocale (LC_NUMERIC, "C");
471 get_page_box (gs, page, &urx, &ury, &llx, &lly);
472 orientation = get_page_orientation (gs, page);
474 g_snprintf (buf, 1024, "%ld %d %d %d %d %d %f %f %d %d %d %d",
475 0L, orientation * 90, llx, lly, urx, ury,
476 get_xdpi (gs) * scale,
477 get_ydpi (gs) * scale,
479 LOG ("GS property %s", buf);
482 setlocale(LC_NUMERIC, savelocale);
484 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
485 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
490 close_pipe (int p[2])
501 is_interpreter_ready (PSDocument *gs)
503 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
507 output (gpointer data, gint source, GdkInputCondition condition)
509 char buf[MAX_BUFSIZE + 1];
511 PSDocument *gs = PS_DOCUMENT(data);
513 if (source == gs->interpreter_output) {
514 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
515 if (bytes == 0) { /* EOF occurred */
516 close (gs->interpreter_output);
517 gs->interpreter_output = -1;
518 gdk_input_remove (gs->interpreter_output_id);
520 } else if (bytes == -1) {
522 interpreter_failed (gs, NULL);
525 if (gs->interpreter_err == -1) {
526 interpreter_failed (gs, NULL);
528 } else if (source == gs->interpreter_err) {
529 bytes = read (gs->interpreter_err, buf, MAX_BUFSIZE);
530 if (bytes == 0) { /* EOF occurred */
531 close (gs->interpreter_err);
532 gs->interpreter_err = -1;
533 gdk_input_remove (gs->interpreter_error_id);
535 } else if (bytes == -1) {
537 interpreter_failed (gs, NULL);
540 if (gs->interpreter_output == -1) {
541 interpreter_failed(gs, NULL);
558 input(gpointer data, gint source, GdkInputCondition condition)
560 PSDocument *gs = PS_DOCUMENT(data);
562 void (*oldsig) (int);
563 oldsig = signal(SIGPIPE, catchPipe);
568 if (gs->buffer_bytes_left == 0) {
569 /* Get a new section if required */
570 if (gs->ps_input && gs->bytes_left == 0) {
571 struct record_list *ps_old = gs->ps_input;
572 gs->ps_input = ps_old->next;
573 if (ps_old->close && NULL != ps_old->fp)
578 /* Have to seek at the beginning of each section */
579 if (gs->ps_input && gs->ps_input->seek_needed) {
580 fseek (gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
581 gs->ps_input->seek_needed = FALSE;
582 gs->bytes_left = gs->ps_input->len;
585 if (gs->bytes_left > MAX_BUFSIZE) {
586 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
587 MAX_BUFSIZE, gs->ps_input->fp);
588 } else if (gs->bytes_left > 0) {
589 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof(char),
590 gs->bytes_left, gs->ps_input->fp);
592 gs->buffer_bytes_left = 0;
594 if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
595 interpreter_failed (gs, NULL); /* Error occurred */
597 gs->input_buffer_ptr = gs->input_buffer;
598 gs->bytes_left -= gs->buffer_bytes_left;
601 if (gs->buffer_bytes_left > 0) {
602 bytes_written = write (gs->interpreter_input,
603 gs->input_buffer_ptr, gs->buffer_bytes_left);
606 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
608 interpreter_failed (gs, NULL);
609 } else if (bytes_written == -1) {
610 if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
611 interpreter_failed (gs, NULL); /* Something bad happened */
614 gs->buffer_bytes_left -= bytes_written;
615 gs->input_buffer_ptr += bytes_written;
618 } while (gs->ps_input && gs->buffer_bytes_left == 0);
620 signal (SIGPIPE, oldsig);
622 if (gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
623 if (gs->interpreter_input_id != 0) {
624 gdk_input_remove (gs->interpreter_input_id);
625 gs->interpreter_input_id = 0;
631 start_interpreter (PSDocument *gs)
633 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
634 int std_out[2]; /* pipe from interp stdout */
635 int std_err[2]; /* pipe from interp stderr */
638 #define NUM_GS_ARGS (NUM_ARGS - 20)
639 #define NUM_ALPHA_ARGS 10
641 char *argv[NUM_ARGS], *dir, *gv_env;
642 char **gs_args, **alpha_args = NULL;
645 LOG ("Start the interpreter");
650 stop_interpreter(gs);
652 /* set up the args... */
653 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
654 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
655 argv[argc] = gs_args[i];
658 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
659 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
660 argv[argc] = alpha_args[i];
663 argv[argc++] = "-dNOPAUSE";
664 argv[argc++] = "-dQUIET";
665 argv[argc++] = "-dSAFER";
667 /* set up the pipes */
668 if (gs->send_filename_to_gs) {
669 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
671 argv[argc++] = "quit";
678 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
679 if (pipe (std_in) == -1) {
680 g_critical ("Unable to open pipe to Ghostscript.");
685 if (pipe (std_out) == -1) {
690 if (pipe(std_err) == -1) {
692 close_pipe (std_out);
696 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
697 gdk_x11_drawable_get_xid (gs->pstarget),
698 gdk_x11_drawable_get_xid (gs->bpixmap));
699 LOG ("Launching ghostview with env %s", gv_env);
701 gs->interpreter_pid = fork ();
702 switch (gs->interpreter_pid) {
705 close_pipe (std_out);
706 close_pipe (std_err);
711 dup2 (std_out[1], 1);
715 dup2 (std_err[1], 2);
718 if (!gs->reading_from_pipe) {
719 if (gs->send_filename_to_gs) {
721 /* just in case gs tries to read from stdin */
722 stdinfd = open("/dev/null", O_RDONLY);
736 /* change to directory where the input file is. This helps
737 * with postscript-files which include other files using
739 dir = g_path_get_dirname (gs->gs_filename);
743 execvp (argv[0], argv);
746 g_critical ("Unable to execute [%s]\n", argv[0]);
747 g_strfreev (gs_args);
749 g_strfreev (alpha_args);
752 default: /* parent */
753 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
756 /* use non-blocking IO for pipe to ghostscript */
757 result = fcntl (std_in[1], F_GETFL, 0);
758 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
759 gs->interpreter_input = std_in[1];
761 gs->interpreter_input = -1;
765 gs->interpreter_output = std_out[0];
767 gs->interpreter_err = std_err[0];
768 gs->interpreter_output_id =
769 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
770 gs->interpreter_error_id =
771 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
779 stop_interpreter(PSDocument * gs)
781 if (gs->interpreter_pid > 0) {
783 LOG ("Stop the interpreter");
784 kill (gs->interpreter_pid, SIGTERM);
785 while ((wait(&status) == -1) && (errno == EINTR));
786 gs->interpreter_pid = -1;
788 gs->gs_status = _("Interpreter failed.");
792 if (gs->interpreter_input >= 0) {
793 close (gs->interpreter_input);
794 gs->interpreter_input = -1;
795 if (gs->interpreter_input_id != 0) {
796 gdk_input_remove(gs->interpreter_input_id);
797 gs->interpreter_input_id = 0;
799 while (gs->ps_input) {
800 struct record_list *ps_old = gs->ps_input;
801 gs->ps_input = gs->ps_input->next;
802 if (ps_old->close && NULL != ps_old->fp)
808 if (gs->interpreter_output >= 0) {
809 close (gs->interpreter_output);
810 gs->interpreter_output = -1;
811 if (gs->interpreter_output_id) {
812 gdk_input_remove (gs->interpreter_output_id);
813 gs->interpreter_output_id = 0;
817 if (gs->interpreter_err >= 0) {
818 close (gs->interpreter_err);
819 gs->interpreter_err = -1;
820 if (gs->interpreter_error_id) {
821 gdk_input_remove (gs->interpreter_error_id);
822 gs->interpreter_error_id = 0;
829 /* If file exists and is a regular file then return its length, else -1 */
831 file_length (const gchar * filename)
833 struct stat stat_rec;
835 if (filename && (stat (filename, &stat_rec) == 0) && S_ISREG (stat_rec.st_mode))
836 return stat_rec.st_size;
841 /* Test if file exists, is a regular file and its length is > 0 */
843 file_readable(const char *filename)
845 return (file_length (filename) > 0);
849 * Decompress gs->gs_filename if necessary
850 * Set gs->filename_unc to the name of the uncompressed file or NULL.
851 * Error reporting via signal 'interpreter_message'
852 * Return name of input file to use or NULL on error..
855 check_filecompressed (PSDocument * gs)
859 gchar *filename, *filename_unc, *filename_err, *cmdline;
865 if ((file = fopen(gs->gs_filename, "r")) &&
866 (fread (buf, sizeof(gchar), 3, file) == 3)) {
867 if ((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
868 /* file is gzipped or compressed */
869 cmd = gtk_gs_defaults_get_ungzip_cmd ();
870 } else if (strncmp (buf, "BZh", 3) == 0) {
871 /* file is compressed with bzip2 */
872 cmd = gtk_gs_defaults_get_unbzip2_cmd ();
880 return gs->gs_filename;
882 /* do the decompression */
883 filename = g_shell_quote (gs->gs_filename);
884 filename_unc = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
885 if ((fd = mkstemp (filename_unc)) < 0) {
886 g_free (filename_unc);
892 filename_err = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
893 if ((fd = mkstemp(filename_err)) < 0) {
894 g_free (filename_err);
895 g_free (filename_unc);
901 cmdline = g_strdup_printf ("%s %s >%s 2>%s", cmd,
902 filename, filename_unc, filename_err);
903 if (system (cmdline) == 0 &&
904 file_readable (filename_unc) &&
905 file_length (filename_err) == 0) {
906 /* sucessfully uncompressed file */
907 gs->gs_filename_unc = filename_unc;
910 g_snprintf (buf, 1024, _("Error while decompressing file %s:\n"),
912 interpreter_failed (gs, buf);
913 unlink (filename_unc);
914 g_free (filename_unc);
918 unlink (filename_err);
919 g_free (filename_err);
927 ps_document_enable_interpreter(PSDocument *gs)
929 g_return_val_if_fail (PS_IS_DOCUMENT (gs), FALSE);
931 if (!gs->gs_filename)
934 return start_interpreter (gs);
938 document_load (PSDocument *gs, const gchar *fname)
940 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
942 LOG ("Load the document");
949 /* prepare this document */
950 gs->structured_doc = FALSE;
951 gs->send_filename_to_gs = TRUE;
952 gs->gs_filename = g_strdup (fname);
954 if ((gs->reading_from_pipe = (strcmp (fname, "-") == 0))) {
955 gs->send_filename_to_gs = FALSE;
958 * We need to make sure that the file is loadable/exists!
959 * otherwise we want to exit without loading new stuff...
961 gchar *filename = NULL;
963 if (!file_readable(fname)) {
966 g_snprintf (buf, 1024, _("Cannot open file %s.\n"), fname);
967 interpreter_failed (gs, buf);
968 gs->gs_status = _("File is not readable.");
970 filename = check_filecompressed(gs);
973 if (!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
974 interpreter_failed (gs, NULL);
978 /* we grab the vital statistics!!! */
979 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
981 if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
982 (gs->doc->epsf && gs->doc->numpages > 1)) {
983 gs->structured_doc = TRUE;
984 gs->send_filename_to_gs = FALSE;
988 gs->gs_status = _("Document loaded.");
994 ps_document_next_page (PSDocument *gs)
998 LOG ("Make ghostscript render next page");
1000 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
1001 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
1002 g_return_val_if_fail (gs->busy != TRUE, FALSE);
1006 event.xclient.type = ClientMessage;
1007 event.xclient.display = gdk_display;
1008 event.xclient.window = gs->message_window;
1009 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1010 event.xclient.format = 32;
1012 gdk_error_trap_push ();
1013 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1015 gdk_error_trap_pop ();
1021 render_page (PSDocument *gs, int page)
1023 g_return_val_if_fail(gs != NULL, FALSE);
1024 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1026 if(!gs->gs_filename) {
1030 if (gs->structured_doc && gs->doc) {
1031 LOG ("It's a structured document, let's send one page to gs");
1033 if (is_interpreter_ready (gs)) {
1034 ps_document_next_page (gs);
1036 ps_document_enable_interpreter (gs);
1037 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1038 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1041 send_ps (gs, gs->doc->pages[page].begin,
1042 gs->doc->pages[page].len, FALSE);
1044 /* Unstructured document
1046 * In the case of non structured documents,
1047 * GS read the PS from the actual file (via command
1048 * line. Hence, ggv only send a signal next page.
1049 * If ghostview is not running it is usually because
1050 * the last page of the file was displayed. In that
1051 * case, ggv restarts GS again and the first page is displayed.
1054 LOG ("It's an unstructured document, gs will just read the file");
1056 if (!is_interpreter_ready (gs)) {
1057 ps_document_enable_interpreter(gs);
1059 ps_document_next_page(gs);
1066 ps_document_load (EvDocument *document,
1073 filename = g_filename_from_uri (uri, NULL, error);
1077 result = document_load (PS_DOCUMENT (document), filename);
1079 g_set_error (error, G_FILE_ERROR,
1080 G_FILE_ERROR_FAILED,
1081 "Failed to load document '%s'\n",
1091 save_page_list (PSDocument *document, int *page_list, const char *filename)
1093 gboolean result = TRUE;
1094 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1098 pscopydoc (sink, document->gs_filename, document->doc, page_list);
1100 buf = gtk_gs_doc_sink_get_buffer (sink);
1102 f = fopen (filename, "w");
1111 gtk_gs_doc_sink_free (sink);
1118 ps_document_save (EvDocument *document,
1122 PSDocument *ps = PS_DOCUMENT (document);
1128 filename = g_filename_from_uri (uri, NULL, error);
1132 page_list = g_new0 (int, ps->doc->numpages);
1133 for (i = 0; i < ps->doc->numpages; i++) {
1137 result = save_page_list (ps, page_list, filename);
1146 ps_document_get_n_pages (EvDocument *document)
1148 PSDocument *ps = PS_DOCUMENT (document);
1150 g_return_val_if_fail (ps != NULL, -1);
1152 if (!ps->gs_filename || !ps->doc) {
1156 return ps->structured_doc ? ps->doc->numpages : 1;
1160 ps_document_get_page_size (EvDocument *document,
1165 PSDocument *gs = PS_DOCUMENT (document);
1167 int urx, ury, llx, lly, orientation;
1169 get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
1170 orientation = get_page_orientation (PS_DOCUMENT (document), page);
1172 switch (orientation) {
1173 case GTK_GS_ORIENTATION_PORTRAIT:
1174 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1175 w = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
1176 h = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
1178 case GTK_GS_ORIENTATION_LANDSCAPE:
1179 case GTK_GS_ORIENTATION_SEASCAPE:
1180 w = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
1181 h = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
1185 g_assert_not_reached ();
1199 ps_document_can_get_text (EvDocument *document)
1205 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer, int page, double scale)
1207 PSDocument *gs = PS_DOCUMENT (renderer);
1209 if (gs->pstarget == NULL) {
1212 widget = gtk_window_new (GTK_WINDOW_POPUP);
1213 gtk_widget_realize (widget);
1214 gs->pstarget = widget->window;
1216 g_assert (gs->pstarget != NULL);
1218 g_signal_connect (widget, "event",
1219 G_CALLBACK (ps_document_widget_event),
1223 setup_pixmap (gs, page, scale);
1224 setup_page (gs, page, scale);
1226 render_page (gs, page);
1229 static EvDocumentInfo *
1230 ps_document_get_info (EvDocument *document)
1232 EvDocumentInfo *info;
1233 PSDocument *ps = PS_DOCUMENT (document);
1235 info = g_new0 (EvDocumentInfo, 1);
1236 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
1237 EV_DOCUMENT_INFO_FORMAT |
1238 EV_DOCUMENT_INFO_CREATOR |
1239 EV_DOCUMENT_INFO_N_PAGES;
1240 info->title = g_strdup (ps->doc->title);
1241 info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
1242 : g_strdup (_("PostScript"));
1243 info->creator = g_strdup (ps->doc->creator);
1244 info->n_pages = ev_document_get_n_pages (document);
1249 static EvOrientation
1250 ps_document_get_orientation (EvDocument *document)
1252 EvOrientation orientation;
1253 PSDocument *ps = PS_DOCUMENT (document);
1255 g_return_val_if_fail (ps != NULL, EV_ORIENTATION_PORTRAIT);
1257 switch (ps->orientation) {
1258 case GTK_GS_ORIENTATION_PORTRAIT:
1259 orientation = EV_ORIENTATION_PORTRAIT;
1261 case GTK_GS_ORIENTATION_LANDSCAPE:
1262 orientation = EV_ORIENTATION_LANDSCAPE;
1264 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1265 orientation = EV_ORIENTATION_UPSIDEDOWN;
1267 case GTK_GS_ORIENTATION_SEASCAPE:
1268 orientation = EV_ORIENTATION_SEASCAPE;
1271 orientation = EV_ORIENTATION_PORTRAIT;
1279 ps_document_set_orientation (EvDocument *document, EvOrientation orientation)
1281 PSDocument *ps = PS_DOCUMENT (document);
1283 g_return_if_fail (ps != NULL);
1285 switch (orientation) {
1286 case EV_ORIENTATION_PORTRAIT:
1287 ps->orientation = GTK_GS_ORIENTATION_PORTRAIT;
1289 case EV_ORIENTATION_LANDSCAPE:
1290 ps->orientation = GTK_GS_ORIENTATION_LANDSCAPE;
1292 case EV_ORIENTATION_UPSIDEDOWN:
1293 ps->orientation = GTK_GS_ORIENTATION_UPSIDEDOWN;
1295 case EV_ORIENTATION_SEASCAPE:
1296 ps->orientation = GTK_GS_ORIENTATION_SEASCAPE;
1302 ps_document_document_iface_init (EvDocumentIface *iface)
1304 iface->load = ps_document_load;
1305 iface->save = ps_document_save;
1306 iface->can_get_text = ps_document_can_get_text;
1307 iface->get_n_pages = ps_document_get_n_pages;
1308 iface->get_page_size = ps_document_get_page_size;
1309 iface->get_info = ps_document_get_info;
1310 iface->set_orientation = ps_document_set_orientation;
1311 iface->get_orientation = ps_document_get_orientation;
1315 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
1317 iface->render_pixbuf = ps_async_renderer_render_pixbuf;
1321 ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
1322 int first_page, int last_page)
1324 PSDocument *document = PS_DOCUMENT (exporter);
1326 g_free (document->ps_export_pagelist);
1328 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1329 document->ps_export_filename = g_strdup (filename);
1333 ps_document_ps_export_do_page (EvPSExporter *exporter, int page)
1335 PSDocument *document = PS_DOCUMENT (exporter);
1337 document->ps_export_pagelist[page] = 1;
1341 ps_document_ps_export_end (EvPSExporter *exporter)
1343 PSDocument *document = PS_DOCUMENT (exporter);
1345 save_page_list (document, document->ps_export_pagelist,
1346 document->ps_export_filename);
1348 g_free (document->ps_export_pagelist);
1349 g_free (document->ps_export_filename);
1350 document->ps_export_pagelist = NULL;
1351 document->ps_export_filename = NULL;
1355 ps_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1357 iface->begin = ps_document_ps_export_begin;
1358 iface->do_page = ps_document_ps_export_do_page;
1359 iface->end = ps_document_ps_export_end;