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"
56 /* if POSIX O_NONBLOCK is not available, use O_NDELAY */
57 #if !defined(O_NONBLOCK) && defined(O_NDELAY)
58 # define O_NONBLOCK O_NDELAY
61 #define MAX_BUFSIZE 1024
63 #define PS_DOCUMENT_IS_COMPRESSED(gs) (PS_DOCUMENT(gs)->gs_filename_unc != NULL)
64 #define PS_DOCUMENT_GET_PS_FILE(gs) (PS_DOCUMENT_IS_COMPRESSED(gs) ? \
65 PS_DOCUMENT(gs)->gs_filename_unc : \
66 PS_DOCUMENT(gs)->gs_filename)
68 /* structure to describe section of file to send to ghostscript */
75 struct record_list *next;
84 static gboolean broken_pipe = FALSE;
86 /* Forward declarations */
87 static void ps_document_init(PSDocument * gs);
88 static void ps_document_class_init(PSDocumentClass * klass);
89 static void send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close);
90 static void close_pipe(int p[2]);
91 static void output(gpointer data, gint source, GdkInputCondition condition);
92 static void input(gpointer data, gint source, GdkInputCondition condition);
93 static void stop_interpreter(PSDocument * gs);
94 static gint start_interpreter(PSDocument * gs);
95 static void ps_document_document_iface_init (EvDocumentIface *iface);
96 static void ps_document_ps_exporter_iface_init (EvPSExporterIface *iface);
97 static void ps_async_renderer_iface_init (EvAsyncRendererIface *iface);
98 static gboolean ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data);
100 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;
124 gs->interpreter_input = -1;
125 gs->interpreter_output = -1;
126 gs->interpreter_err = -1;
127 gs->interpreter_input_id = 0;
128 gs->interpreter_output_id = 0;
129 gs->interpreter_error_id = 0;
132 gs->input_buffer = NULL;
133 gs->input_buffer_ptr = NULL;
135 gs->buffer_bytes_left = 0;
137 gs->gs_status = _("No document loaded.");
139 gs->ps_export_pagelist = NULL;
140 gs->ps_export_filename = NULL;
144 ps_document_dispose (GObject *object)
146 PSDocument *gs = PS_DOCUMENT (object);
148 g_return_if_fail (gs != NULL);
151 fclose (gs->gs_psfile);
152 gs->gs_psfile = NULL;
155 if (gs->gs_filename) {
156 g_free (gs->gs_filename);
157 gs->gs_filename = NULL;
165 if (gs->gs_filename_unc) {
166 unlink(gs->gs_filename_unc);
167 g_free(gs->gs_filename_unc);
168 gs->gs_filename_unc = NULL;
172 gdk_drawable_unref (gs->bpixmap);
175 if(gs->input_buffer) {
176 g_free(gs->input_buffer);
177 gs->input_buffer = NULL;
180 stop_interpreter (gs);
182 G_OBJECT_CLASS (parent_class)->dispose (object);
186 ps_document_class_init(PSDocumentClass *klass)
188 GObjectClass *object_class;
190 object_class = (GObjectClass *) klass;
191 parent_class = g_type_class_peek_parent (klass);
194 object_class->dispose = ps_document_dispose;
196 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
197 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
198 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
199 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
203 push_pixbuf (PSDocument *gs)
209 cmap = gdk_window_get_colormap (gs->pstarget);
210 gdk_drawable_get_size (gs->bpixmap, &width, &height);
211 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
214 g_signal_emit_by_name (gs, "render_finished", pixbuf);
215 g_object_unref (pixbuf);
219 interpreter_failed (PSDocument *gs, char *msg)
221 LOG ("Interpreter failed %s", msg);
225 stop_interpreter (gs);
229 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
231 PSDocument *gs = (PSDocument *) data;
233 if(event->type != GDK_CLIENT_EVENT)
236 gs->message_window = event->client.data.l[0];
238 if (event->client.message_type == gs_class->page_atom) {
239 LOG ("GS rendered the document");
249 send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close)
251 struct record_list *ps_new;
253 if(gs->interpreter_input < 0) {
254 g_critical("No pipe to gs: error in send_ps().");
258 ps_new = (struct record_list *) g_malloc(sizeof(struct record_list));
259 ps_new->fp = gs->gs_psfile;
260 ps_new->begin = begin;
262 ps_new->seek_needed = TRUE;
263 ps_new->close = close;
266 if(gs->input_buffer == NULL) {
267 gs->input_buffer = g_malloc(MAX_BUFSIZE);
270 if(gs->ps_input == NULL) {
271 gs->input_buffer_ptr = gs->input_buffer;
272 gs->bytes_left = len;
273 gs->buffer_bytes_left = 0;
274 gs->ps_input = ps_new;
275 gs->interpreter_input_id =
276 gdk_input_add(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)
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;
365 else if(gs->doc->default_page_size != NULL) {
366 new_pagesize = gs->doc->default_page_size - gs->doc->size;
368 else if((page >= 0) &&
369 (gs->doc->numpages > page) &&
371 (gs->doc->pages[page].boundingbox[URX] >
372 gs->doc->pages[page].boundingbox[LLX]) &&
373 (gs->doc->pages[page].boundingbox[URY] >
374 gs->doc->pages[page].boundingbox[LLY])) {
377 else if((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
378 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
384 /* Compute bounding box */
385 if(gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
388 (gs->doc->pages[page].boundingbox[URX] >
389 gs->doc->pages[page].boundingbox[LLX])
390 && (gs->doc->pages[page].boundingbox[URY] >
391 gs->doc->pages[page].boundingbox[LLY])) {
393 new_llx = gs->doc->pages[page].boundingbox[LLX];
394 new_lly = gs->doc->pages[page].boundingbox[LLY];
395 new_urx = gs->doc->pages[page].boundingbox[URX];
396 new_ury = gs->doc->pages[page].boundingbox[URY];
398 else if((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
399 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
401 new_llx = gs->doc->boundingbox[LLX];
402 new_lly = gs->doc->boundingbox[LLY];
403 new_urx = gs->doc->boundingbox[URX];
404 new_ury = gs->doc->boundingbox[URY];
409 new_pagesize = DEFAULT_PAGE_SIZE;
410 new_llx = new_lly = 0;
411 if(gs->doc && gs->doc->size &&
412 (new_pagesize < gs->doc->numsizes)) {
413 new_urx = gs->doc->size[new_pagesize].width;
414 new_ury = gs->doc->size[new_pagesize].height;
417 new_urx = papersizes[new_pagesize].width;
418 new_ury = papersizes[new_pagesize].height;
422 if(new_urx <= new_llx)
423 new_urx = papersizes[12].width;
424 if(new_ury <= new_lly)
425 new_ury = papersizes[12].height;
434 get_page_orientation (PSDocument *gs, int page)
438 orientation = GTK_GS_ORIENTATION_NONE;
440 if (gs->structured_doc) {
441 orientation = gs->doc->pages[page].orientation;
443 if (orientation == GTK_GS_ORIENTATION_NONE) {
444 orientation = gs->doc->default_page_orientation;
446 if (orientation == GTK_GS_ORIENTATION_NONE) {
447 orientation = gs->doc->orientation;
449 if (orientation == GTK_GS_ORIENTATION_NONE) {
450 orientation = GTK_GS_ORIENTATION_PORTRAIT;
457 setup_page (PSDocument *gs, int page, double scale)
460 int urx, ury, llx, lly, orientation;
465 LOG ("Setup the page");
468 /* gs needs floating point parameters with '.' as decimal point
469 * while some (european) locales use ',' instead, so we set the
470 * locale for this snprintf to "C".
472 savelocale = setlocale (LC_NUMERIC, "C");
474 get_page_box (gs, page, &urx, &ury, &llx, &lly);
475 orientation = get_page_orientation (gs, page);
477 g_snprintf (buf, 1024, "%ld %d %d %d %d %d %f %f %d %d %d %d",
478 0L, orientation * 90, llx, lly, urx, ury,
479 get_xdpi (gs) * scale,
480 get_ydpi (gs) * scale,
482 LOG ("GS property %s", buf);
485 setlocale(LC_NUMERIC, savelocale);
487 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
488 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
493 close_pipe (int p[2])
504 is_interpreter_ready (PSDocument *gs)
506 return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
510 output(gpointer data, gint source, GdkInputCondition condition)
512 char buf[MAX_BUFSIZE + 1];
514 PSDocument *gs = PS_DOCUMENT(data);
516 if(source == gs->interpreter_output) {
517 bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
518 if(bytes == 0) { /* EOF occurred */
519 close(gs->interpreter_output);
520 gs->interpreter_output = -1;
521 gdk_input_remove(gs->interpreter_output_id);
524 else if(bytes == -1) {
526 interpreter_failed(gs, NULL);
529 if(gs->interpreter_err == -1) {
530 interpreter_failed(gs, NULL);
533 else if(source == gs->interpreter_err) {
534 bytes = read(gs->interpreter_err, buf, MAX_BUFSIZE);
535 if(bytes == 0) { /* EOF occurred */
536 close(gs->interpreter_err);
537 gs->interpreter_err = -1;
538 gdk_input_remove(gs->interpreter_error_id);
541 else if(bytes == -1) {
543 interpreter_failed(gs, NULL);
546 if(gs->interpreter_output == -1) {
547 interpreter_failed(gs, NULL);
563 input(gpointer data, gint source, GdkInputCondition condition)
565 PSDocument *gs = PS_DOCUMENT(data);
567 void (*oldsig) (int);
568 oldsig = signal(SIGPIPE, catchPipe);
573 if(gs->buffer_bytes_left == 0) {
574 /* Get a new section if required */
575 if(gs->ps_input && gs->bytes_left == 0) {
576 struct record_list *ps_old = gs->ps_input;
577 gs->ps_input = ps_old->next;
578 if(ps_old->close && NULL != ps_old->fp)
580 g_free((char *) ps_old);
582 /* Have to seek at the beginning of each section */
583 if(gs->ps_input && gs->ps_input->seek_needed) {
584 fseek(gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
585 gs->ps_input->seek_needed = FALSE;
586 gs->bytes_left = gs->ps_input->len;
589 if(gs->bytes_left > MAX_BUFSIZE) {
590 gs->buffer_bytes_left =
591 fread(gs->input_buffer, sizeof(char), MAX_BUFSIZE, gs->ps_input->fp);
593 else if(gs->bytes_left > 0) {
594 gs->buffer_bytes_left =
595 fread(gs->input_buffer,
596 sizeof(char), gs->bytes_left, gs->ps_input->fp);
599 gs->buffer_bytes_left = 0;
601 if(gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
602 interpreter_failed (gs, NULL); /* Error occurred */
604 gs->input_buffer_ptr = gs->input_buffer;
605 gs->bytes_left -= gs->buffer_bytes_left;
608 if(gs->buffer_bytes_left > 0) {
609 bytes_written = write(gs->interpreter_input,
610 gs->input_buffer_ptr, gs->buffer_bytes_left);
613 interpreter_failed (gs, g_strdup(_("Broken pipe.")));
615 interpreter_failed (gs, NULL);
617 else if(bytes_written == -1) {
618 if((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
619 interpreter_failed (gs, NULL); /* Something bad happened */
623 gs->buffer_bytes_left -= bytes_written;
624 gs->input_buffer_ptr += bytes_written;
628 while(gs->ps_input && gs->buffer_bytes_left == 0);
630 signal(SIGPIPE, oldsig);
632 if(gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
633 if(gs->interpreter_input_id != 0) {
634 gdk_input_remove(gs->interpreter_input_id);
635 gs->interpreter_input_id = 0;
641 start_interpreter (PSDocument *gs)
643 int std_in[2] = { -1, -1 }; /* pipe to interp stdin */
644 int std_out[2]; /* pipe from interp stdout */
645 int std_err[2]; /* pipe from interp stderr */
648 #define NUM_GS_ARGS (NUM_ARGS - 20)
649 #define NUM_ALPHA_ARGS 10
651 char *argv[NUM_ARGS], *dir, *gv_env;
652 char **gs_args, **alpha_args = NULL;
655 LOG ("Start the interpreter");
660 stop_interpreter(gs);
662 /* set up the args... */
663 gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
664 for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
665 argv[argc] = gs_args[i];
668 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
669 for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
670 argv[argc] = alpha_args[i];
673 argv[argc++] = "-dNOPAUSE";
674 argv[argc++] = "-dQUIET";
675 argv[argc++] = "-dSAFER";
677 /* set up the pipes */
678 if (gs->send_filename_to_gs) {
679 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
681 argv[argc++] = "quit";
688 if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
689 if (pipe (std_in) == -1) {
690 g_critical ("Unable to open pipe to Ghostscript.");
695 if (pipe (std_out) == -1) {
700 if (pipe(std_err) == -1) {
702 close_pipe (std_out);
706 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
707 gdk_x11_drawable_get_xid (gs->pstarget),
708 gdk_x11_drawable_get_xid (gs->bpixmap));
709 LOG ("Launching ghostview with env %s", gv_env);
711 gs->interpreter_pid = fork ();
712 switch (gs->interpreter_pid) {
715 close_pipe (std_out);
716 close_pipe (std_err);
721 dup2 (std_out[1], 1);
725 dup2 (std_err[1], 2);
728 if (!gs->reading_from_pipe) {
729 if (gs->send_filename_to_gs) {
731 /* just in case gs tries to read from stdin */
732 stdinfd = open("/dev/null", O_RDONLY);
746 /* change to directory where the input file is. This helps
747 * with postscript-files which include other files using
749 dir = g_path_get_dirname (gs->gs_filename);
753 execvp (argv[0], argv);
756 g_critical ("Unable to execute [%s]\n", argv[0]);
757 g_strfreev (gs_args);
759 g_strfreev (alpha_args);
762 default: /* parent */
763 if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
766 /* use non-blocking IO for pipe to ghostscript */
767 result = fcntl (std_in[1], F_GETFL, 0);
768 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
769 gs->interpreter_input = std_in[1];
771 gs->interpreter_input = -1;
775 gs->interpreter_output = std_out[0];
777 gs->interpreter_err = std_err[0];
778 gs->interpreter_output_id =
779 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
780 gs->interpreter_error_id =
781 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
789 stop_interpreter(PSDocument * gs)
791 if(gs->interpreter_pid > 0) {
793 LOG ("Stop the interpreter");
794 kill(gs->interpreter_pid, SIGTERM);
795 while((wait(&status) == -1) && (errno == EINTR)) ;
796 gs->interpreter_pid = -1;
798 gs->gs_status = _("Interpreter failed.");
802 if(gs->interpreter_input >= 0) {
803 close(gs->interpreter_input);
804 gs->interpreter_input = -1;
805 if(gs->interpreter_input_id != 0) {
806 gdk_input_remove(gs->interpreter_input_id);
807 gs->interpreter_input_id = 0;
809 while(gs->ps_input) {
810 struct record_list *ps_old = gs->ps_input;
811 gs->ps_input = gs->ps_input->next;
812 if(ps_old->close && NULL != ps_old->fp)
814 g_free((char *) ps_old);
818 if(gs->interpreter_output >= 0) {
819 close(gs->interpreter_output);
820 gs->interpreter_output = -1;
821 if(gs->interpreter_output_id) {
822 gdk_input_remove(gs->interpreter_output_id);
823 gs->interpreter_output_id = 0;
827 if(gs->interpreter_err >= 0) {
828 close(gs->interpreter_err);
829 gs->interpreter_err = -1;
830 if(gs->interpreter_error_id) {
831 gdk_input_remove(gs->interpreter_error_id);
832 gs->interpreter_error_id = 0;
839 /* If file exists and is a regular file then return its length, else -1 */
841 file_length(const gchar * filename)
843 struct stat stat_rec;
845 if(filename && (stat(filename, &stat_rec) == 0)
846 && S_ISREG(stat_rec.st_mode))
847 return stat_rec.st_size;
852 /* Test if file exists, is a regular file and its length is > 0 */
854 file_readable(const char *filename)
856 return (file_length(filename) > 0);
860 * Decompress gs->gs_filename if necessary
861 * Set gs->filename_unc to the name of the uncompressed file or NULL.
862 * Error reporting via signal 'interpreter_message'
863 * Return name of input file to use or NULL on error..
866 check_filecompressed(PSDocument * gs)
870 gchar *filename, *filename_unc, *filename_err, *cmdline;
876 if((file = fopen(gs->gs_filename, "r"))
877 && (fread(buf, sizeof(gchar), 3, file) == 3)) {
878 if((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
879 /* file is gzipped or compressed */
880 cmd = gtk_gs_defaults_get_ungzip_cmd();
882 else if(strncmp(buf, "BZh", 3) == 0) {
883 /* file is compressed with bzip2 */
884 cmd = gtk_gs_defaults_get_unbzip2_cmd();
891 return gs->gs_filename;
893 /* do the decompression */
894 filename = g_shell_quote(gs->gs_filename);
895 filename_unc = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
896 if((fd = mkstemp(filename_unc)) < 0) {
897 g_free(filename_unc);
902 filename_err = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
903 if((fd = mkstemp(filename_err)) < 0) {
904 g_free(filename_err);
905 g_free(filename_unc);
910 cmdline = g_strdup_printf("%s %s >%s 2>%s", cmd,
911 filename, filename_unc, filename_err);
912 if((system(cmdline) == 0)
913 && file_readable(filename_unc)
914 && (file_length(filename_err) == 0)) {
915 /* sucessfully uncompressed file */
916 gs->gs_filename_unc = filename_unc;
920 g_snprintf(buf, 1024, _("Error while decompressing file %s:\n"),
922 interpreter_failed (gs, buf);
923 unlink(filename_unc);
924 g_free(filename_unc);
927 unlink(filename_err);
928 g_free(filename_err);
935 ps_document_enable_interpreter(PSDocument * gs)
937 g_return_val_if_fail(gs != NULL, FALSE);
938 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
943 return start_interpreter(gs);
946 /* publicly accessible functions */
949 ps_document_get_type(void)
951 static GType gs_type = 0;
953 GTypeInfo gs_info = {
954 sizeof(PSDocumentClass),
955 (GBaseInitFunc) NULL,
956 (GBaseFinalizeFunc) NULL,
957 (GClassInitFunc) ps_document_class_init,
958 (GClassFinalizeFunc) NULL,
959 NULL, /* class_data */
962 (GInstanceInitFunc) ps_document_init
965 static const GInterfaceInfo document_info =
967 (GInterfaceInitFunc) ps_document_document_iface_init,
972 static const GInterfaceInfo ps_exporter_info =
974 (GInterfaceInitFunc) ps_document_ps_exporter_iface_init,
979 static const GInterfaceInfo async_renderer_info =
981 (GInterfaceInitFunc) ps_async_renderer_iface_init,
986 gs_type = g_type_register_static(G_TYPE_OBJECT,
987 "PSDocument", &gs_info, 0);
989 g_type_add_interface_static (gs_type,
992 g_type_add_interface_static (gs_type,
995 g_type_add_interface_static (gs_type,
996 EV_TYPE_ASYNC_RENDERER,
997 &async_renderer_info);
1005 document_load(PSDocument * gs, const gchar * fname)
1007 g_return_val_if_fail(gs != NULL, FALSE);
1008 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1010 LOG ("Load the document");
1017 /* prepare this document */
1018 gs->structured_doc = FALSE;
1019 gs->send_filename_to_gs = TRUE;
1022 /* an absolute path */
1023 gs->gs_filename = g_strdup(fname);
1026 /* path relative to our cwd: make it absolute */
1027 gchar *cwd = g_get_current_dir();
1028 gs->gs_filename = g_strconcat(cwd, "/", fname, NULL);
1032 if((gs->reading_from_pipe = (strcmp(fname, "-") == 0))) {
1033 gs->send_filename_to_gs = FALSE;
1037 * We need to make sure that the file is loadable/exists!
1038 * otherwise we want to exit without loading new stuff...
1040 gchar *filename = NULL;
1042 if(!file_readable(fname)) {
1044 g_snprintf(buf, 1024, _("Cannot open file %s.\n"), fname);
1045 interpreter_failed (gs, buf);
1046 gs->gs_status = _("File is not readable.");
1049 filename = check_filecompressed(gs);
1052 if(!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
1053 interpreter_failed (gs, NULL);
1057 /* we grab the vital statistics!!! */
1058 gs->doc = psscan(gs->gs_psfile, TRUE, filename);
1060 if(gs->doc == NULL) {
1061 /* File does not seem to be a Postscript one */
1063 g_snprintf(buf, 1024, _("Error while scanning file %s\n"), fname);
1064 interpreter_failed (gs, buf);
1065 gs->gs_status = _("The file is not a PostScript document.");
1069 if((!gs->doc->epsf && gs->doc->numpages > 0) ||
1070 (gs->doc->epsf && gs->doc->numpages > 1)) {
1071 gs->structured_doc = TRUE;
1072 gs->send_filename_to_gs = FALSE;
1077 gs->gs_status = _("Document loaded.");
1084 ps_document_next_page (PSDocument *gs)
1088 LOG ("Make ghostscript render next page");
1090 g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
1091 g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
1092 g_return_val_if_fail (gs->busy != TRUE, FALSE);
1096 event.xclient.type = ClientMessage;
1097 event.xclient.display = gdk_display;
1098 event.xclient.window = gs->message_window;
1099 event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1100 event.xclient.format = 32;
1102 gdk_error_trap_push ();
1103 XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1105 gdk_error_trap_pop ();
1111 render_page (PSDocument *gs, int page)
1113 g_return_val_if_fail(gs != NULL, FALSE);
1114 g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1116 if(!gs->gs_filename) {
1120 if (gs->structured_doc && gs->doc) {
1121 LOG ("It's a structured document, let's send one page to gs");
1123 if (is_interpreter_ready (gs)) {
1124 ps_document_next_page (gs);
1126 ps_document_enable_interpreter (gs);
1127 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1128 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1131 send_ps (gs, gs->doc->pages[page].begin,
1132 gs->doc->pages[page].len, FALSE);
1134 /* Unstructured document
1136 * In the case of non structured documents,
1137 * GS read the PS from the actual file (via command
1138 * line. Hence, ggv only send a signal next page.
1139 * If ghostview is not running it is usually because
1140 * the last page of the file was displayed. In that
1141 * case, ggv restarts GS again and the first page is displayed.
1144 LOG ("It's an unstructured document, gs will just read the file");
1146 if (!is_interpreter_ready (gs)) {
1147 ps_document_enable_interpreter(gs);
1149 ps_document_next_page(gs);
1156 ps_document_load (EvDocument *document,
1163 filename = g_filename_from_uri (uri, NULL, error);
1167 result = document_load (PS_DOCUMENT (document), filename);
1169 g_set_error (error, G_FILE_ERROR,
1170 G_FILE_ERROR_FAILED,
1171 "Failed to load document '%s'\n",
1181 save_page_list (PSDocument *document, int *page_list, const char *filename)
1183 gboolean result = TRUE;
1184 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1188 pscopydoc (sink, document->gs_filename, document->doc, page_list);
1190 buf = gtk_gs_doc_sink_get_buffer (sink);
1192 f = fopen (filename, "w");
1201 gtk_gs_doc_sink_free (sink);
1208 ps_document_save (EvDocument *document,
1212 PSDocument *ps = PS_DOCUMENT (document);
1218 filename = g_filename_from_uri (uri, NULL, error);
1222 page_list = g_new0 (int, ps->doc->numpages);
1223 for (i = 0; i < ps->doc->numpages; i++) {
1227 result = save_page_list (ps, page_list, filename);
1236 ps_document_get_n_pages (EvDocument *document)
1238 PSDocument *ps = PS_DOCUMENT (document);
1240 g_return_val_if_fail (ps != NULL, -1);
1242 if (!ps->gs_filename || !ps->doc) {
1246 return ps->structured_doc ? ps->doc->numpages : 1;
1250 ps_document_get_page_size (EvDocument *document,
1255 PSDocument *gs = PS_DOCUMENT (document);
1257 int urx, ury, llx, lly, orientation;
1259 get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
1260 orientation = get_page_orientation (PS_DOCUMENT (document), page);
1262 switch (orientation) {
1263 case GTK_GS_ORIENTATION_PORTRAIT:
1264 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1265 w = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
1266 h = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
1268 case GTK_GS_ORIENTATION_LANDSCAPE:
1269 case GTK_GS_ORIENTATION_SEASCAPE:
1270 w = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
1271 h = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
1275 g_assert_not_reached ();
1289 ps_document_can_get_text (EvDocument *document)
1295 render_pixbuf_idle (PSRenderJob *job)
1297 PSDocument *gs = job->document;
1299 if (gs->pstarget == NULL) {
1302 widget = gtk_window_new (GTK_WINDOW_POPUP);
1303 gtk_widget_realize (widget);
1304 gs->pstarget = widget->window;
1306 g_assert (gs->pstarget != NULL);
1308 g_signal_connect (widget, "event",
1309 G_CALLBACK (ps_document_widget_event),
1313 setup_pixmap (gs, job->page, job->scale);
1314 setup_page (gs, job->page, job->scale);
1316 render_page (gs, job->page);
1322 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer, int page, double scale)
1328 job.document = PS_DOCUMENT (renderer);
1329 render_pixbuf_idle (&job);
1332 static EvDocumentInfo *
1333 ps_document_get_info (EvDocument *document)
1335 EvDocumentInfo *info;
1336 PSDocument *ps = PS_DOCUMENT (document);
1338 info = g_new0 (EvDocumentInfo, 1);
1339 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
1340 EV_DOCUMENT_INFO_N_PAGES;
1341 info->title = g_strdup (ps->doc->title);
1342 info->n_pages = ev_document_get_n_pages (document);
1348 ps_document_document_iface_init (EvDocumentIface *iface)
1350 iface->load = ps_document_load;
1351 iface->save = ps_document_save;
1352 iface->can_get_text = ps_document_can_get_text;
1353 iface->get_n_pages = ps_document_get_n_pages;
1354 iface->get_page_size = ps_document_get_page_size;
1355 iface->get_info = ps_document_get_info;
1359 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
1361 iface->render_pixbuf = ps_async_renderer_render_pixbuf;
1365 ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
1366 int first_page, int last_page)
1368 PSDocument *document = PS_DOCUMENT (exporter);
1370 g_free (document->ps_export_pagelist);
1372 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1373 document->ps_export_filename = g_strdup (filename);
1377 ps_document_ps_export_do_page (EvPSExporter *exporter, int page)
1379 PSDocument *document = PS_DOCUMENT (exporter);
1381 document->ps_export_pagelist[page] = 1;
1385 ps_document_ps_export_end (EvPSExporter *exporter)
1387 PSDocument *document = PS_DOCUMENT (exporter);
1389 save_page_list (document, document->ps_export_pagelist,
1390 document->ps_export_filename);
1392 g_free (document->ps_export_pagelist);
1393 g_free (document->ps_export_filename);
1394 document->ps_export_pagelist = NULL;
1395 document->ps_export_filename = NULL;
1399 ps_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1401 iface->begin = ps_document_ps_export_begin;
1402 iface->do_page = ps_document_ps_export_do_page;
1403 iface->end = ps_document_ps_export_end;