1 /* Ghostscript widget for GTK/GNOME
3 * Copyright (C) 1998 - 2005 the Free Software Foundation
5 * Authors: Jonathan Blandford, Jaka Mocnik, Carlos Garcia Campos
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.
27 #include <glib/gstdio.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtkwidget.h>
30 #include <gtk/gtkwindow.h>
38 #include "ps-document.h"
41 #include "gsdefaults.h"
42 #include "ev-file-exporter.h"
43 #include "ev-async-renderer.h"
45 #define MAX_BUFSIZE 1024
47 /* structure to describe section of file to send to ghostscript */
59 GtkWidget *target_window;
62 glong message_window; /* Used by ghostview to receive messages from app */
64 GPid interpreter_pid; /* PID of interpreter, -1 if none */
65 GIOChannel *interpreter_input; /* stdin of interpreter */
66 GIOChannel *interpreter_output; /* stdout of interpreter */
67 GIOChannel *interpreter_err; /* stderr of interpreter */
68 guint interpreter_input_id;
69 guint interpreter_output_id;
70 guint interpreter_error_id;
72 gboolean busy; /* Is gs busy drawing? */
73 gboolean structured_doc;
76 gchar *input_buffer_ptr;
78 guint buffer_bytes_left;
80 FILE *gs_psfile; /* the currently loaded FILE */
81 gchar *gs_filename; /* the currently loaded filename */
83 gboolean send_filename_to_gs; /* True if gs should read from file directly */
86 gint *ps_export_pagelist;
87 gchar *ps_export_filename;
89 const gchar *gs_status; /* PSDocument status */
92 struct _PSDocumentClass {
93 GObjectClass parent_class;
101 static void ps_document_document_iface_init (EvDocumentIface *iface);
102 static void ps_document_file_exporter_iface_init (EvFileExporterIface *iface);
103 static void ps_async_renderer_iface_init (EvAsyncRendererIface *iface);
105 static void ps_interpreter_start (PSDocument *gs);
106 static void ps_interpreter_stop (PSDocument *gs);
107 static void ps_interpreter_failed (PSDocument *gs,
109 static gboolean ps_interpreter_is_ready (PSDocument *gs);
111 static void push_pixbuf (PSDocument *gs);
114 G_DEFINE_TYPE_WITH_CODE (PSDocument, ps_document, G_TYPE_OBJECT,
116 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
117 ps_document_document_iface_init);
118 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
119 ps_document_file_exporter_iface_init);
120 G_IMPLEMENT_INTERFACE (EV_TYPE_ASYNC_RENDERER,
121 ps_async_renderer_iface_init);
125 ps_section_free (PSSection *section)
130 if (section->close && section->fp)
131 fclose (section->fp);
138 ps_document_init (PSDocument *gs)
141 gs->interpreter_pid = -1;
144 gs->gs_filename = NULL;
146 gs->structured_doc = FALSE;
147 gs->send_filename_to_gs = FALSE;
151 gs->interpreter_input = NULL;
152 gs->interpreter_output = NULL;
153 gs->interpreter_err = NULL;
154 gs->interpreter_input_id = 0;
155 gs->interpreter_output_id = 0;
156 gs->interpreter_error_id = 0;
158 gs->ps_input = g_queue_new ();
159 gs->input_buffer = NULL;
160 gs->input_buffer_ptr = NULL;
162 gs->buffer_bytes_left = 0;
164 gs->gs_status = _("No document loaded.");
166 gs->ps_export_pagelist = NULL;
167 gs->ps_export_filename = NULL;
171 ps_document_dispose (GObject *object)
173 PSDocument *gs = PS_DOCUMENT (object);
176 fclose (gs->gs_psfile);
177 gs->gs_psfile = NULL;
180 if (gs->gs_filename) {
181 g_free (gs->gs_filename);
182 gs->gs_filename = NULL;
191 g_object_unref (gs->bpixmap);
195 if (gs->input_buffer) {
196 g_free (gs->input_buffer);
197 gs->input_buffer = NULL;
200 if (gs->target_window) {
201 gtk_widget_destroy (gs->target_window);
202 gs->target_window = NULL;
207 g_queue_foreach (gs->ps_input, (GFunc)ps_section_free, NULL);
208 g_queue_free (gs->ps_input);
212 ps_interpreter_stop (gs);
214 G_OBJECT_CLASS (ps_document_parent_class)->dispose (object);
218 ps_document_class_init (PSDocumentClass *klass)
220 GObjectClass *object_class;
222 object_class = G_OBJECT_CLASS (klass);
224 object_class->dispose = ps_document_dispose;
226 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
227 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
228 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
229 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
234 ps_interpreter_input (GIOChannel *io,
235 GIOCondition condition,
238 PSSection *section = NULL;
241 if (gs->buffer_bytes_left == 0) {
242 /* Get a new section if required */
243 if (gs->bytes_left == 0) {
244 ps_section_free (section);
246 g_queue_pop_tail (gs->ps_input);
249 if (section == NULL) {
250 section = g_queue_peek_tail (gs->ps_input);
253 /* Have to seek at the beginning of each section */
254 if (section && section->seek_needed) {
255 fseek (section->fp, section->begin, SEEK_SET);
256 section->seek_needed = FALSE;
257 gs->bytes_left = section->len;
260 if (gs->bytes_left > MAX_BUFSIZE) {
261 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof (char),
262 MAX_BUFSIZE, section->fp);
263 } else if (gs->bytes_left > 0) {
264 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof (char),
265 gs->bytes_left, section->fp);
267 gs->buffer_bytes_left = 0;
270 if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
271 ps_interpreter_failed (gs, NULL); /* Error occurred */
274 gs->input_buffer_ptr = gs->input_buffer;
275 gs->bytes_left -= gs->buffer_bytes_left;
278 if (gs->buffer_bytes_left > 0) {
281 GError *error = NULL;
283 status = g_io_channel_write_chars (gs->interpreter_input,
284 gs->input_buffer_ptr,
285 gs->buffer_bytes_left,
289 case G_IO_STATUS_NORMAL:
290 gs->buffer_bytes_left -= bytes_written;
291 gs->input_buffer_ptr += bytes_written;
294 case G_IO_STATUS_ERROR:
295 ps_interpreter_failed (gs, error->message);
296 g_error_free (error);
299 case G_IO_STATUS_AGAIN:
304 } while (!g_queue_is_empty (gs->ps_input) && gs->buffer_bytes_left == 0);
306 if (g_queue_is_empty (gs->ps_input) && gs->buffer_bytes_left == 0) {
309 flags = g_io_channel_get_flags (gs->interpreter_input);
311 g_io_channel_set_flags (gs->interpreter_input,
312 flags & ~G_IO_FLAG_NONBLOCK, NULL);
313 g_io_channel_flush (gs->interpreter_input, NULL);
314 g_io_channel_set_flags (gs->interpreter_input,
315 flags | G_IO_FLAG_NONBLOCK, NULL);
317 gs->interpreter_input_id = 0;
326 ps_interpreter_output (GIOChannel *io,
327 GIOCondition condition,
330 gchar buf[MAX_BUFSIZE + 1];
333 GError *error = NULL;
335 status = g_io_channel_read_chars (io, buf, MAX_BUFSIZE,
338 case G_IO_STATUS_NORMAL:
344 case G_IO_STATUS_EOF:
345 g_io_channel_unref (gs->interpreter_output);
346 gs->interpreter_output = NULL;
347 gs->interpreter_output_id = 0;
350 case G_IO_STATUS_ERROR:
351 ps_interpreter_failed (gs, error->message);
352 g_error_free (error);
353 gs->interpreter_output_id = 0;
360 if (!gs->interpreter_err) {
361 ps_interpreter_failed (gs, NULL);
368 ps_interpreter_error (GIOChannel *io,
369 GIOCondition condition,
372 gchar buf[MAX_BUFSIZE + 1];
375 GError *error = NULL;
377 status = g_io_channel_read_chars (io, buf, MAX_BUFSIZE,
380 case G_IO_STATUS_NORMAL:
387 case G_IO_STATUS_EOF:
388 g_io_channel_unref (gs->interpreter_err);
389 gs->interpreter_err = NULL;
390 gs->interpreter_error_id = 0;
393 case G_IO_STATUS_ERROR:
394 ps_interpreter_failed (gs, error->message);
395 g_error_free (error);
396 gs->interpreter_error_id = 0;
403 if (!gs->interpreter_output) {
404 ps_interpreter_failed (gs, NULL);
411 ps_interpreter_finished (GPid pid,
415 g_spawn_close_pid (gs->interpreter_pid);
416 gs->interpreter_pid = -1;
417 ps_interpreter_failed (gs, NULL);
421 #define NUM_GS_ARGS (NUM_ARGS - 20)
422 #define NUM_ALPHA_ARGS 10
425 setup_interpreter_env (gchar **envp)
429 for (i = 0; envp[i]; i++)
434 ps_interpreter_start (PSDocument *gs)
436 gchar *argv[NUM_ARGS], *dir, *gv_env, *gs_path;
437 gchar **gs_args, **alpha_args = NULL;
439 gint pin, pout, perr;
441 GError *error = NULL;
443 if (!gs->gs_filename)
446 ps_interpreter_stop (gs);
448 dir = g_path_get_dirname (gs->gs_filename);
450 /* set up the args... */
451 gs_path = g_find_program_in_path ("gs");
452 gs_args = g_strsplit (gs_path, " ", NUM_GS_ARGS);
454 for (i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
455 argv[argc] = gs_args[i];
458 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
459 for (i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
460 argv[argc] = alpha_args[i];
463 argv[argc++] = "-dNOPAUSE";
464 argv[argc++] = "-dQUIET";
465 argv[argc++] = "-dSAFER";
467 if (gs->send_filename_to_gs) {
468 argv[argc++] = gs->gs_filename;
470 argv[argc++] = "quit";
477 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld;DISPLAY=%s",
478 gdk_x11_drawable_get_xid (gs->pstarget),
479 gdk_x11_drawable_get_xid (gs->bpixmap),
480 gdk_display_get_name (gdk_drawable_get_display (gs->pstarget)));
481 envp = g_strsplit (gv_env, ";", 2);
484 if (g_spawn_async_with_pipes (dir, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
485 (GSpawnChildSetupFunc)setup_interpreter_env, envp,
486 &(gs->interpreter_pid),
491 g_child_watch_add (gs->interpreter_pid,
492 (GChildWatchFunc)ps_interpreter_finished,
495 gs->interpreter_input = g_io_channel_unix_new (pin);
496 g_io_channel_set_encoding (gs->interpreter_input, NULL, NULL);
497 flags = g_io_channel_get_flags (gs->interpreter_input);
498 g_io_channel_set_flags (gs->interpreter_input,
499 flags | G_IO_FLAG_NONBLOCK, NULL);
502 gs->interpreter_output = g_io_channel_unix_new (pout);
503 flags = g_io_channel_get_flags (gs->interpreter_output);
504 g_io_channel_set_flags (gs->interpreter_output,
505 flags | G_IO_FLAG_NONBLOCK, NULL);
506 gs->interpreter_output_id =
507 g_io_add_watch (gs->interpreter_output, G_IO_IN,
508 (GIOFunc)ps_interpreter_output,
511 gs->interpreter_err = g_io_channel_unix_new (perr);
512 flags = g_io_channel_get_flags (gs->interpreter_err);
513 g_io_channel_set_flags (gs->interpreter_err,
514 flags | G_IO_FLAG_NONBLOCK, NULL);
515 gs->interpreter_error_id =
516 g_io_add_watch (gs->interpreter_err, G_IO_IN,
517 (GIOFunc)ps_interpreter_error,
520 g_warning (error->message);
521 g_error_free (error);
526 g_strfreev (gs_args);
527 g_strfreev (alpha_args);
531 ps_interpreter_stop (PSDocument *gs)
533 if (gs->interpreter_pid > 0) {
536 kill (gs->interpreter_pid, SIGTERM);
537 while ((wait (&status) == -1) && (errno == EINTR));
538 g_spawn_close_pid (gs->interpreter_pid);
539 gs->interpreter_pid = -1;
542 gs->gs_status = _("Interpreter failed.");
546 if (gs->interpreter_input) {
547 g_io_channel_unref (gs->interpreter_input);
548 gs->interpreter_input = NULL;
550 if (gs->interpreter_input_id > 0) {
551 g_source_remove (gs->interpreter_input_id);
552 gs->interpreter_input_id = 0;
556 g_queue_foreach (gs->ps_input, (GFunc)ps_section_free, NULL);
557 g_queue_free (gs->ps_input);
558 gs->ps_input = g_queue_new ();
562 if (gs->interpreter_output) {
563 g_io_channel_unref (gs->interpreter_output);
564 gs->interpreter_output = NULL;
566 if (gs->interpreter_output_id > 0) {
567 g_source_remove (gs->interpreter_output_id);
568 gs->interpreter_output_id = 0;
572 if (gs->interpreter_err) {
573 g_io_channel_unref (gs->interpreter_err);
574 gs->interpreter_err = NULL;
576 if (gs->interpreter_error_id > 0) {
577 g_source_remove (gs->interpreter_error_id);
578 gs->interpreter_error_id = 0;
586 ps_interpreter_failed (PSDocument *gs, const char *msg)
588 g_warning (msg ? msg : _("Interpreter failed."));
591 ps_interpreter_stop (gs);
595 ps_interpreter_is_ready (PSDocument *gs)
597 return (gs->interpreter_pid != -1 &&
599 (g_queue_is_empty (gs->ps_input)));
602 /* EvDocumentIface */
604 document_load (PSDocument *gs, const gchar *fname)
611 /* prepare this document */
612 gs->structured_doc = FALSE;
613 gs->send_filename_to_gs = TRUE;
614 gs->gs_filename = g_strdup (fname);
617 * We need to make sure that the file is loadable/exists!
618 * otherwise we want to exit without loading new stuff...
620 if (!g_file_test (fname, G_FILE_TEST_IS_REGULAR)) {
624 filename_dsp = g_filename_display_name (fname);
625 msg = g_strdup_printf (_("Cannot open file “%s”.\n"), filename_dsp);
626 g_free (filename_dsp);
628 ps_interpreter_failed (gs, msg);
630 gs->gs_status = _("File is not readable.");
635 if (!gs->gs_filename || (gs->gs_psfile = fopen (gs->gs_filename, "r")) == NULL) {
636 ps_interpreter_failed (gs, NULL);
640 /* we grab the vital statistics!!! */
641 gs->doc = psscan (gs->gs_psfile, TRUE, gs->gs_filename);
643 if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
644 (gs->doc->epsf && gs->doc->numpages > 1)) {
645 gs->structured_doc = TRUE;
646 gs->send_filename_to_gs = FALSE;
649 gs->gs_status = _("Document loaded.");
655 ps_document_load (EvDocument *document,
663 filename = g_filename_from_uri (uri, NULL, error);
667 gs_path = g_find_program_in_path ("gs");
671 filename_dsp = g_filename_display_name (filename);
675 _("Failed to load document “%s”. Ghostscript interpreter was not found in path"),
677 g_free (filename_dsp);
683 result = document_load (PS_DOCUMENT (document), filename);
687 filename_dsp = g_filename_display_name (filename);
688 g_set_error (error, G_FILE_ERROR,
690 _("Failed to load document “%s”"),
692 g_free (filename_dsp);
702 save_document (PSDocument *document, const char *filename)
704 gboolean result = TRUE;
705 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
709 src_file = fopen (document->gs_filename, "r");
711 struct stat stat_rec;
713 if (stat (document->gs_filename, &stat_rec) == 0) {
714 pscopy (src_file, sink, 0, stat_rec.st_size - 1);
720 buf = gtk_gs_doc_sink_get_buffer (sink);
725 f = fopen (filename, "w");
734 gtk_gs_doc_sink_free (sink);
741 save_page_list (PSDocument *document, int *page_list, const char *filename)
743 gboolean result = TRUE;
744 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
748 pscopydoc (sink, document->gs_filename,
749 document->doc, page_list);
751 buf = gtk_gs_doc_sink_get_buffer (sink);
753 f = fopen (filename, "w");
762 gtk_gs_doc_sink_free (sink);
769 ps_document_save (EvDocument *document,
773 PSDocument *ps = PS_DOCUMENT (document);
777 filename = g_filename_from_uri (uri, NULL, error);
781 result = save_document (ps, filename);
789 ps_document_get_n_pages (EvDocument *document)
791 PSDocument *ps = PS_DOCUMENT (document);
793 if (!ps->gs_filename || !ps->doc) {
797 return ps->structured_doc ? ps->doc->numpages : 1;
800 #define DEFAULT_PAGE_SIZE 1
803 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
809 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
810 int new_pagesize = -1;
812 if (new_pagesize == -1) {
813 new_pagesize = DEFAULT_PAGE_SIZE;
815 /* If we have a document:
816 * We use -- the page size (if specified)
817 * or the doc. size (if specified)
818 * or the page bbox (if specified)
819 * or the bounding box
821 if ((page >= 0) && (gs->doc->numpages > page) &&
822 (gs->doc->pages) && (gs->doc->pages[page].size)) {
823 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
824 } else if (gs->doc->default_page_size != NULL) {
825 new_pagesize = gs->doc->default_page_size - gs->doc->size;
826 } else if ((page >= 0) &&
827 (gs->doc->numpages > page) &&
829 (gs->doc->pages[page].boundingbox[URX] >
830 gs->doc->pages[page].boundingbox[LLX]) &&
831 (gs->doc->pages[page].boundingbox[URY] >
832 gs->doc->pages[page].boundingbox[LLY])) {
834 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
835 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
841 /* Compute bounding box */
842 if (gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
845 (gs->doc->pages[page].boundingbox[URX] >
846 gs->doc->pages[page].boundingbox[LLX]) &&
847 (gs->doc->pages[page].boundingbox[URY] >
848 gs->doc->pages[page].boundingbox[LLY])) {
850 new_llx = gs->doc->pages[page].boundingbox[LLX];
851 new_lly = gs->doc->pages[page].boundingbox[LLY];
852 new_urx = gs->doc->pages[page].boundingbox[URX];
853 new_ury = gs->doc->pages[page].boundingbox[URY];
854 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
855 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
857 new_llx = gs->doc->boundingbox[LLX];
858 new_lly = gs->doc->boundingbox[LLY];
859 new_urx = gs->doc->boundingbox[URX];
860 new_ury = gs->doc->boundingbox[URY];
863 if (new_pagesize < 0)
864 new_pagesize = DEFAULT_PAGE_SIZE;
865 new_llx = new_lly = 0;
866 if (gs->doc && gs->doc->size &&
867 (new_pagesize < gs->doc->numsizes)) {
868 new_urx = gs->doc->size[new_pagesize].width;
869 new_ury = gs->doc->size[new_pagesize].height;
871 new_urx = papersizes[new_pagesize].width;
872 new_ury = papersizes[new_pagesize].height;
876 if (new_urx <= new_llx)
877 new_urx = papersizes[12].width;
878 if (new_ury <= new_lly)
879 new_ury = papersizes[12].height;
888 ps_document_get_page_size (EvDocument *document,
893 PSDocument *gs = PS_DOCUMENT (document);
894 int urx, ury, llx, lly;
896 get_page_box (gs, page, &urx, &ury, &llx, &lly);
899 *width = (urx - llx) + 0.5;
903 *height = (ury - lly) + 0.5;
908 ps_document_can_get_text (EvDocument *document)
913 static EvDocumentInfo *
914 ps_document_get_info (EvDocument *document)
916 EvDocumentInfo *info;
917 PSDocument *ps = PS_DOCUMENT (document);
918 int urx, ury, llx, lly;
920 info = g_new0 (EvDocumentInfo, 1);
921 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
922 EV_DOCUMENT_INFO_FORMAT |
923 EV_DOCUMENT_INFO_CREATOR |
924 EV_DOCUMENT_INFO_N_PAGES |
925 EV_DOCUMENT_INFO_PAPER_SIZE;
927 info->title = g_strdup (ps->doc->title);
928 info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
929 : g_strdup (_("PostScript"));
930 info->creator = g_strdup (ps->doc->creator);
931 info->n_pages = ev_document_get_n_pages (document);
933 get_page_box (PS_DOCUMENT (document), 0, &urx, &ury, &llx, &lly);
935 info->paper_width = (urx - llx) / 72.0f * 25.4f;
936 info->paper_height = (ury - lly) / 72.0f * 25.4f;
942 ps_document_document_iface_init (EvDocumentIface *iface)
944 iface->load = ps_document_load;
945 iface->save = ps_document_save;
946 iface->can_get_text = ps_document_can_get_text;
947 iface->get_n_pages = ps_document_get_n_pages;
948 iface->get_page_size = ps_document_get_page_size;
949 iface->get_info = ps_document_get_info;
952 /* EvAsyncRendererIface */
954 setup_page (PSDocument *gs, int page, double scale, int rotation)
957 char scaled_dpi[G_ASCII_DTOSTR_BUF_SIZE];
958 int urx, ury, llx, lly;
959 PSDocumentClass *gs_class;
961 gs_class = PS_DOCUMENT_GET_CLASS (gs);
963 get_page_box (gs, page, &urx, &ury, &llx, &lly);
964 g_ascii_dtostr (scaled_dpi, G_ASCII_DTOSTR_BUF_SIZE, 72.0 * scale);
966 buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
967 0L, rotation, llx, lly, urx, ury,
968 scaled_dpi, scaled_dpi,
971 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
972 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen (buf));
979 setup_pixmap (PSDocument *gs, int page, double scale, int rotation)
982 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
983 GdkColormap *colormap;
984 double width, height;
985 int pixmap_width, pixmap_height;
987 if (gs->pstarget == NULL)
990 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
992 if (rotation == 90 || rotation == 270) {
993 pixmap_height = width * scale + 0.5;
994 pixmap_width = height * scale + 0.5;
996 pixmap_width = width * scale + 0.5;
997 pixmap_height = height * scale + 0.5;
1003 gdk_drawable_get_size (gs->bpixmap, &w, &h);
1005 if (pixmap_width != w || h != pixmap_height) {
1006 g_object_unref (gs->bpixmap);
1008 ps_interpreter_stop (gs);
1013 fill = gdk_gc_new (gs->pstarget);
1014 colormap = gdk_drawable_get_colormap (gs->pstarget);
1015 gdk_colormap_alloc_color (colormap, &white, FALSE, TRUE);
1016 gdk_gc_set_foreground (fill, &white);
1017 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
1019 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
1020 0, 0, pixmap_width, pixmap_height);
1025 push_pixbuf (PSDocument *gs)
1031 if (gs->pstarget == NULL)
1034 cmap = gdk_drawable_get_colormap (gs->pstarget);
1035 gdk_drawable_get_size (gs->bpixmap, &width, &height);
1036 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
1039 g_signal_emit_by_name (gs, "render_finished", pixbuf);
1040 g_object_unref (pixbuf);
1044 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
1046 PSDocument *gs = (PSDocument *) data;
1047 PSDocumentClass *gs_class;
1049 if (event->type != GDK_CLIENT_EVENT)
1052 gs_class = PS_DOCUMENT_GET_CLASS (gs);
1054 gs->message_window = event->client.data.l[0];
1056 if (event->client.message_type == gs_class->page_atom) {
1066 send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
1070 if (!gs->interpreter_input) {
1071 g_critical ("No pipe to gs: error in send_ps().");
1075 ps_new = g_new0 (PSSection, 1);
1076 ps_new->fp = gs->gs_psfile;
1077 ps_new->begin = begin;
1079 ps_new->seek_needed = TRUE;
1080 ps_new->close = close;
1082 if (gs->input_buffer == NULL) {
1083 gs->input_buffer = g_malloc (MAX_BUFSIZE);
1086 if (g_queue_is_empty (gs->ps_input)) {
1087 gs->input_buffer_ptr = gs->input_buffer;
1088 gs->bytes_left = len;
1089 gs->buffer_bytes_left = 0;
1090 g_queue_push_head (gs->ps_input, ps_new);
1091 gs->interpreter_input_id =
1092 g_io_add_watch (gs->interpreter_input,
1093 G_IO_OUT/* | G_IO_HUP | G_IO_ERR | G_IO_NVAL*/,
1094 (GIOFunc)ps_interpreter_input,
1097 g_queue_push_head (gs->ps_input, ps_new);
1102 ps_document_next_page (PSDocument *gs)
1106 GdkDisplay *display;
1108 PSDocumentClass *gs_class;
1110 g_assert (gs->interpreter_pid != 0);
1111 g_assert (gs->busy != TRUE);
1113 gs_class = PS_DOCUMENT_GET_CLASS (gs);
1117 screen = gtk_window_get_screen (GTK_WINDOW (gs->target_window));
1118 display = gdk_screen_get_display (screen);
1119 dpy = gdk_x11_display_get_xdisplay (display);
1121 event.xclient.type = ClientMessage;
1122 event.xclient.display = dpy;
1123 event.xclient.window = gs->message_window;
1124 event.xclient.message_type =
1125 gdk_x11_atom_to_xatom_for_display (display,
1126 gs_class->next_atom);
1127 event.xclient.format = 32;
1129 gdk_error_trap_push ();
1130 XSendEvent (dpy, gs->message_window, FALSE, 0, &event);
1132 gdk_error_trap_pop ();
1136 render_page (PSDocument *gs, int page)
1138 g_assert (gs != NULL);
1140 if (!gs->gs_filename) {
1144 if (gs->structured_doc && gs->doc) {
1145 if (ps_interpreter_is_ready (gs)) {
1146 ps_document_next_page (gs);
1148 ps_interpreter_start (gs);
1149 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1150 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1153 send_ps (gs, gs->doc->pages[page].begin,
1154 gs->doc->pages[page].len, FALSE);
1156 /* Unstructured document
1158 * In the case of non structured documents,
1159 * GS read the PS from the actual file (via command
1160 * line. Hence, ggv only send a signal next page.
1161 * If ghostview is not running it is usually because
1162 * the last page of the file was displayed. In that
1163 * case, ggv restarts GS again and the first page is displayed.
1166 if (!ps_interpreter_is_ready (gs)) {
1167 ps_interpreter_start (gs);
1169 ps_document_next_page (gs);
1176 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer,
1181 PSDocument *gs = PS_DOCUMENT (renderer);
1183 if (gs->pstarget == NULL) {
1184 gs->target_window = gtk_window_new (GTK_WINDOW_POPUP);
1185 gtk_widget_realize (gs->target_window);
1186 gs->pstarget = gs->target_window->window;
1188 g_assert (gs->pstarget != NULL);
1190 g_signal_connect (gs->target_window, "event",
1191 G_CALLBACK (ps_document_widget_event),
1195 setup_pixmap (gs, page, scale, rotation);
1196 setup_page (gs, page, scale, rotation);
1198 render_page (gs, page);
1202 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
1204 iface->render_pixbuf = ps_async_renderer_render_pixbuf;
1207 /* EvFileExporterIface */
1209 ps_document_file_exporter_format_supported (EvFileExporter *exporter,
1210 EvFileExporterFormat format)
1212 return (format == EV_FILE_FORMAT_PS);
1216 ps_document_file_exporter_begin (EvFileExporter *exporter,
1217 EvFileExporterFormat format,
1218 const char *filename,
1225 PSDocument *document = PS_DOCUMENT (exporter);
1227 if (document->structured_doc) {
1228 g_free (document->ps_export_pagelist);
1230 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1233 document->ps_export_filename = g_strdup (filename);
1237 ps_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
1239 PSDocument *document = PS_DOCUMENT (exporter);
1241 if (document->structured_doc) {
1242 document->ps_export_pagelist[rc->page] = 1;
1247 ps_document_file_exporter_end (EvFileExporter *exporter)
1249 PSDocument *document = PS_DOCUMENT (exporter);
1251 if (!document->structured_doc) {
1252 save_document (document, document->ps_export_filename);
1254 save_page_list (document, document->ps_export_pagelist,
1255 document->ps_export_filename);
1256 g_free (document->ps_export_pagelist);
1257 g_free (document->ps_export_filename);
1258 document->ps_export_pagelist = NULL;
1259 document->ps_export_filename = NULL;
1264 ps_document_file_exporter_iface_init (EvFileExporterIface *iface)
1266 iface->format_supported = ps_document_file_exporter_format_supported;
1267 iface->begin = ps_document_file_exporter_begin;
1268 iface->do_page = ps_document_file_exporter_do_page;
1269 iface->end = ps_document_file_exporter_end;