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;
90 struct _PSDocumentClass {
91 GObjectClass parent_class;
99 static void ps_document_document_iface_init (EvDocumentIface *iface);
100 static void ps_document_file_exporter_iface_init (EvFileExporterIface *iface);
101 static void ps_async_renderer_iface_init (EvAsyncRendererIface *iface);
103 static void ps_interpreter_start (PSDocument *gs);
104 static void ps_interpreter_stop (PSDocument *gs);
105 static void ps_interpreter_failed (PSDocument *gs,
107 static gboolean ps_interpreter_is_ready (PSDocument *gs);
109 static void push_pixbuf (PSDocument *gs);
112 G_DEFINE_TYPE_WITH_CODE (PSDocument, ps_document, G_TYPE_OBJECT,
114 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
115 ps_document_document_iface_init);
116 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
117 ps_document_file_exporter_iface_init);
118 G_IMPLEMENT_INTERFACE (EV_TYPE_ASYNC_RENDERER,
119 ps_async_renderer_iface_init);
123 ps_section_free (PSSection *section)
128 if (section->close && section->fp)
129 fclose (section->fp);
136 ps_document_init (PSDocument *gs)
139 gs->interpreter_pid = -1;
142 gs->gs_filename = NULL;
144 gs->structured_doc = FALSE;
145 gs->send_filename_to_gs = FALSE;
149 gs->interpreter_input = NULL;
150 gs->interpreter_output = NULL;
151 gs->interpreter_err = NULL;
152 gs->interpreter_input_id = 0;
153 gs->interpreter_output_id = 0;
154 gs->interpreter_error_id = 0;
156 gs->ps_input = g_queue_new ();
157 gs->input_buffer = NULL;
158 gs->input_buffer_ptr = NULL;
160 gs->buffer_bytes_left = 0;
162 gs->ps_export_pagelist = NULL;
163 gs->ps_export_filename = NULL;
167 ps_document_dispose (GObject *object)
169 PSDocument *gs = PS_DOCUMENT (object);
172 fclose (gs->gs_psfile);
173 gs->gs_psfile = NULL;
176 if (gs->gs_filename) {
177 g_free (gs->gs_filename);
178 gs->gs_filename = NULL;
187 g_object_unref (gs->bpixmap);
191 if (gs->input_buffer) {
192 g_free (gs->input_buffer);
193 gs->input_buffer = NULL;
196 if (gs->target_window) {
197 gtk_widget_destroy (gs->target_window);
198 gs->target_window = NULL;
203 g_queue_foreach (gs->ps_input, (GFunc)ps_section_free, NULL);
204 g_queue_free (gs->ps_input);
208 ps_interpreter_stop (gs);
210 G_OBJECT_CLASS (ps_document_parent_class)->dispose (object);
214 ps_document_class_init (PSDocumentClass *klass)
216 GObjectClass *object_class;
218 object_class = G_OBJECT_CLASS (klass);
220 object_class->dispose = ps_document_dispose;
222 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
223 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
224 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
225 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
230 ps_interpreter_input (GIOChannel *io,
231 GIOCondition condition,
234 PSSection *section = NULL;
237 if (gs->buffer_bytes_left == 0) {
238 /* Get a new section if required */
239 if (gs->bytes_left == 0) {
240 ps_section_free (section);
242 g_queue_pop_tail (gs->ps_input);
245 if (section == NULL) {
246 section = g_queue_peek_tail (gs->ps_input);
249 /* Have to seek at the beginning of each section */
250 if (section && section->seek_needed) {
251 fseek (section->fp, section->begin, SEEK_SET);
252 section->seek_needed = FALSE;
253 gs->bytes_left = section->len;
256 if (gs->bytes_left > MAX_BUFSIZE) {
257 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof (char),
258 MAX_BUFSIZE, section->fp);
259 } else if (gs->bytes_left > 0) {
260 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof (char),
261 gs->bytes_left, section->fp);
263 gs->buffer_bytes_left = 0;
266 if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
267 ps_interpreter_failed (gs, NULL); /* Error occurred */
270 gs->input_buffer_ptr = gs->input_buffer;
271 gs->bytes_left -= gs->buffer_bytes_left;
274 if (gs->buffer_bytes_left > 0) {
277 GError *error = NULL;
279 status = g_io_channel_write_chars (gs->interpreter_input,
280 gs->input_buffer_ptr,
281 gs->buffer_bytes_left,
285 case G_IO_STATUS_NORMAL:
286 gs->buffer_bytes_left -= bytes_written;
287 gs->input_buffer_ptr += bytes_written;
290 case G_IO_STATUS_ERROR:
291 ps_interpreter_failed (gs, error->message);
292 g_error_free (error);
295 case G_IO_STATUS_AGAIN:
300 } while (!g_queue_is_empty (gs->ps_input) && gs->buffer_bytes_left == 0);
302 if (g_queue_is_empty (gs->ps_input) && gs->buffer_bytes_left == 0) {
305 flags = g_io_channel_get_flags (gs->interpreter_input);
307 g_io_channel_set_flags (gs->interpreter_input,
308 flags & ~G_IO_FLAG_NONBLOCK, NULL);
309 g_io_channel_flush (gs->interpreter_input, NULL);
310 g_io_channel_set_flags (gs->interpreter_input,
311 flags | G_IO_FLAG_NONBLOCK, NULL);
313 gs->interpreter_input_id = 0;
322 ps_interpreter_output (GIOChannel *io,
323 GIOCondition condition,
326 gchar buf[MAX_BUFSIZE + 1];
329 GError *error = NULL;
331 status = g_io_channel_read_chars (io, buf, MAX_BUFSIZE,
334 case G_IO_STATUS_NORMAL:
340 case G_IO_STATUS_EOF:
341 g_io_channel_unref (gs->interpreter_output);
342 gs->interpreter_output = NULL;
343 gs->interpreter_output_id = 0;
346 case G_IO_STATUS_ERROR:
347 ps_interpreter_failed (gs, error->message);
348 g_error_free (error);
349 gs->interpreter_output_id = 0;
356 if (!gs->interpreter_err) {
357 ps_interpreter_failed (gs, NULL);
364 ps_interpreter_error (GIOChannel *io,
365 GIOCondition condition,
368 gchar buf[MAX_BUFSIZE + 1];
371 GError *error = NULL;
373 status = g_io_channel_read_chars (io, buf, MAX_BUFSIZE,
376 case G_IO_STATUS_NORMAL:
383 case G_IO_STATUS_EOF:
384 g_io_channel_unref (gs->interpreter_err);
385 gs->interpreter_err = NULL;
386 gs->interpreter_error_id = 0;
389 case G_IO_STATUS_ERROR:
390 ps_interpreter_failed (gs, error->message);
391 g_error_free (error);
392 gs->interpreter_error_id = 0;
399 if (!gs->interpreter_output) {
400 ps_interpreter_failed (gs, NULL);
407 ps_interpreter_finished (GPid pid,
411 g_spawn_close_pid (gs->interpreter_pid);
412 gs->interpreter_pid = -1;
413 ps_interpreter_failed (gs, NULL);
417 #define NUM_GS_ARGS (NUM_ARGS - 20)
418 #define NUM_ALPHA_ARGS 10
421 setup_interpreter_env (gchar **envp)
425 for (i = 0; envp[i]; i++)
430 ps_interpreter_start (PSDocument *gs)
432 gchar *argv[NUM_ARGS], *dir, *gv_env, *gs_path;
433 gchar **gs_args, **alpha_args = NULL;
435 gint pin, pout, perr;
437 GError *error = NULL;
439 if (!gs->gs_filename)
442 ps_interpreter_stop (gs);
444 dir = g_path_get_dirname (gs->gs_filename);
446 /* set up the args... */
447 gs_path = g_find_program_in_path ("gs");
448 gs_args = g_strsplit (gs_path, " ", NUM_GS_ARGS);
450 for (i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
451 argv[argc] = gs_args[i];
454 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
455 for (i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
456 argv[argc] = alpha_args[i];
459 argv[argc++] = "-dNOPAUSE";
460 argv[argc++] = "-dQUIET";
461 argv[argc++] = "-dSAFER";
463 if (gs->send_filename_to_gs) {
464 argv[argc++] = gs->gs_filename;
466 argv[argc++] = "quit";
473 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld;DISPLAY=%s",
474 gdk_x11_drawable_get_xid (gs->pstarget),
475 gdk_x11_drawable_get_xid (gs->bpixmap),
476 gdk_display_get_name (gdk_drawable_get_display (gs->pstarget)));
477 envp = g_strsplit (gv_env, ";", 2);
480 if (g_spawn_async_with_pipes (dir, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
481 (GSpawnChildSetupFunc)setup_interpreter_env, envp,
482 &(gs->interpreter_pid),
487 g_child_watch_add (gs->interpreter_pid,
488 (GChildWatchFunc)ps_interpreter_finished,
491 gs->interpreter_input = g_io_channel_unix_new (pin);
492 g_io_channel_set_encoding (gs->interpreter_input, NULL, NULL);
493 flags = g_io_channel_get_flags (gs->interpreter_input);
494 g_io_channel_set_flags (gs->interpreter_input,
495 flags | G_IO_FLAG_NONBLOCK, NULL);
498 gs->interpreter_output = g_io_channel_unix_new (pout);
499 flags = g_io_channel_get_flags (gs->interpreter_output);
500 g_io_channel_set_flags (gs->interpreter_output,
501 flags | G_IO_FLAG_NONBLOCK, NULL);
502 gs->interpreter_output_id =
503 g_io_add_watch (gs->interpreter_output, G_IO_IN,
504 (GIOFunc)ps_interpreter_output,
507 gs->interpreter_err = g_io_channel_unix_new (perr);
508 flags = g_io_channel_get_flags (gs->interpreter_err);
509 g_io_channel_set_flags (gs->interpreter_err,
510 flags | G_IO_FLAG_NONBLOCK, NULL);
511 gs->interpreter_error_id =
512 g_io_add_watch (gs->interpreter_err, G_IO_IN,
513 (GIOFunc)ps_interpreter_error,
516 g_warning (error->message);
517 g_error_free (error);
522 g_strfreev (gs_args);
523 g_strfreev (alpha_args);
527 ps_interpreter_stop (PSDocument *gs)
529 if (gs->interpreter_pid > 0) {
532 kill (gs->interpreter_pid, SIGTERM);
533 while ((wait (&status) == -1) && (errno == EINTR));
534 g_spawn_close_pid (gs->interpreter_pid);
535 gs->interpreter_pid = -1;
538 if (gs->interpreter_input) {
539 g_io_channel_unref (gs->interpreter_input);
540 gs->interpreter_input = NULL;
542 if (gs->interpreter_input_id > 0) {
543 g_source_remove (gs->interpreter_input_id);
544 gs->interpreter_input_id = 0;
548 g_queue_foreach (gs->ps_input, (GFunc)ps_section_free, NULL);
549 g_queue_free (gs->ps_input);
550 gs->ps_input = g_queue_new ();
554 if (gs->interpreter_output) {
555 g_io_channel_unref (gs->interpreter_output);
556 gs->interpreter_output = NULL;
558 if (gs->interpreter_output_id > 0) {
559 g_source_remove (gs->interpreter_output_id);
560 gs->interpreter_output_id = 0;
564 if (gs->interpreter_err) {
565 g_io_channel_unref (gs->interpreter_err);
566 gs->interpreter_err = NULL;
568 if (gs->interpreter_error_id > 0) {
569 g_source_remove (gs->interpreter_error_id);
570 gs->interpreter_error_id = 0;
578 ps_interpreter_failed (PSDocument *gs, const char *msg)
580 g_warning (msg ? msg : _("Interpreter failed."));
583 ps_interpreter_stop (gs);
587 ps_interpreter_is_ready (PSDocument *gs)
589 return (gs->interpreter_pid != -1 &&
591 (g_queue_is_empty (gs->ps_input)));
594 /* EvDocumentIface */
596 document_load (PSDocument *gs, const gchar *fname, GError **error)
598 /* prepare this document */
599 gs->structured_doc = FALSE;
600 gs->send_filename_to_gs = TRUE;
601 gs->gs_filename = g_strdup (fname);
604 * We need to make sure that the file is loadable/exists!
605 * otherwise we want to exit without loading new stuff...
607 if (!g_file_test (fname, G_FILE_TEST_IS_REGULAR)) {
610 filename_dsp = g_filename_display_name (fname);
614 _("Cannot open file “%s”.\n"), /* FIXME: remove \n after freeze */
616 g_free (filename_dsp);
618 ps_interpreter_failed (gs, NULL);
622 if (!gs->gs_filename || (gs->gs_psfile = fopen (gs->gs_filename, "r")) == NULL) {
625 filename_dsp = g_filename_display_name (fname);
629 _("Cannot open file “%s”.\n"), /* FIXME: remove \n after freeze */
631 g_free (filename_dsp);
633 ps_interpreter_failed (gs, NULL);
637 /* we grab the vital statistics!!! */
638 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;
653 ps_document_load (EvDocument *document,
661 filename = g_filename_from_uri (uri, NULL, error);
665 gs_path = g_find_program_in_path ("gs");
669 filename_dsp = g_filename_display_name (filename);
673 _("Failed to load document “%s”. Ghostscript interpreter was not found in path"),
675 g_free (filename_dsp);
681 result = document_load (PS_DOCUMENT (document), filename, error);
682 if (!result && !(*error)) {
685 filename_dsp = g_filename_display_name (filename);
689 _("Failed to load document “%s”"),
691 g_free (filename_dsp);
701 save_document (PSDocument *document, const char *filename)
703 gboolean result = TRUE;
704 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
708 src_file = fopen (document->gs_filename, "r");
710 struct stat stat_rec;
712 if (stat (document->gs_filename, &stat_rec) == 0) {
713 pscopy (src_file, sink, 0, stat_rec.st_size - 1);
719 buf = gtk_gs_doc_sink_get_buffer (sink);
724 f = fopen (filename, "w");
733 gtk_gs_doc_sink_free (sink);
740 save_page_list (PSDocument *document, int *page_list, const char *filename)
742 gboolean result = TRUE;
743 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
747 pscopydoc (sink, document->gs_filename,
748 document->doc, page_list);
750 buf = gtk_gs_doc_sink_get_buffer (sink);
752 f = fopen (filename, "w");
761 gtk_gs_doc_sink_free (sink);
768 ps_document_save (EvDocument *document,
772 PSDocument *ps = PS_DOCUMENT (document);
776 filename = g_filename_from_uri (uri, NULL, error);
780 result = save_document (ps, filename);
788 ps_document_get_n_pages (EvDocument *document)
790 PSDocument *ps = PS_DOCUMENT (document);
792 if (!ps->gs_filename || !ps->doc) {
796 return ps->structured_doc ? ps->doc->numpages : 1;
799 #define DEFAULT_PAGE_SIZE 1
802 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
808 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
809 int new_pagesize = -1;
811 if (new_pagesize == -1) {
812 new_pagesize = DEFAULT_PAGE_SIZE;
814 /* If we have a document:
815 * We use -- the page size (if specified)
816 * or the doc. size (if specified)
817 * or the page bbox (if specified)
818 * or the bounding box
820 if ((page >= 0) && (gs->doc->numpages > page) &&
821 (gs->doc->pages) && (gs->doc->pages[page].size)) {
822 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
823 } else if (gs->doc->default_page_size != NULL) {
824 new_pagesize = gs->doc->default_page_size - gs->doc->size;
825 } else if ((page >= 0) &&
826 (gs->doc->numpages > page) &&
828 (gs->doc->pages[page].boundingbox[URX] >
829 gs->doc->pages[page].boundingbox[LLX]) &&
830 (gs->doc->pages[page].boundingbox[URY] >
831 gs->doc->pages[page].boundingbox[LLY])) {
833 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
834 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
840 /* Compute bounding box */
841 if (gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
844 (gs->doc->pages[page].boundingbox[URX] >
845 gs->doc->pages[page].boundingbox[LLX]) &&
846 (gs->doc->pages[page].boundingbox[URY] >
847 gs->doc->pages[page].boundingbox[LLY])) {
849 new_llx = gs->doc->pages[page].boundingbox[LLX];
850 new_lly = gs->doc->pages[page].boundingbox[LLY];
851 new_urx = gs->doc->pages[page].boundingbox[URX];
852 new_ury = gs->doc->pages[page].boundingbox[URY];
853 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
854 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
856 new_llx = gs->doc->boundingbox[LLX];
857 new_lly = gs->doc->boundingbox[LLY];
858 new_urx = gs->doc->boundingbox[URX];
859 new_ury = gs->doc->boundingbox[URY];
862 if (new_pagesize < 0)
863 new_pagesize = DEFAULT_PAGE_SIZE;
864 new_llx = new_lly = 0;
865 if (gs->doc && gs->doc->size &&
866 (new_pagesize < gs->doc->numsizes)) {
867 new_urx = gs->doc->size[new_pagesize].width;
868 new_ury = gs->doc->size[new_pagesize].height;
870 new_urx = papersizes[new_pagesize].width;
871 new_ury = papersizes[new_pagesize].height;
875 if (new_urx <= new_llx)
876 new_urx = papersizes[12].width;
877 if (new_ury <= new_lly)
878 new_ury = papersizes[12].height;
887 ps_document_get_page_size (EvDocument *document,
892 PSDocument *gs = PS_DOCUMENT (document);
893 int urx, ury, llx, lly;
895 get_page_box (gs, page, &urx, &ury, &llx, &lly);
898 *width = (urx - llx) + 0.5;
902 *height = (ury - lly) + 0.5;
907 ps_document_can_get_text (EvDocument *document)
912 static EvDocumentInfo *
913 ps_document_get_info (EvDocument *document)
915 EvDocumentInfo *info;
916 PSDocument *ps = PS_DOCUMENT (document);
917 int urx, ury, llx, lly;
919 info = g_new0 (EvDocumentInfo, 1);
920 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
921 EV_DOCUMENT_INFO_FORMAT |
922 EV_DOCUMENT_INFO_CREATOR |
923 EV_DOCUMENT_INFO_N_PAGES |
924 EV_DOCUMENT_INFO_PAPER_SIZE;
926 info->title = g_strdup (ps->doc->title);
927 info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
928 : g_strdup (_("PostScript"));
929 info->creator = g_strdup (ps->doc->creator);
930 info->n_pages = ev_document_get_n_pages (document);
932 get_page_box (PS_DOCUMENT (document), 0, &urx, &ury, &llx, &lly);
934 info->paper_width = (urx - llx) / 72.0f * 25.4f;
935 info->paper_height = (ury - lly) / 72.0f * 25.4f;
941 ps_document_document_iface_init (EvDocumentIface *iface)
943 iface->load = ps_document_load;
944 iface->save = ps_document_save;
945 iface->can_get_text = ps_document_can_get_text;
946 iface->get_n_pages = ps_document_get_n_pages;
947 iface->get_page_size = ps_document_get_page_size;
948 iface->get_info = ps_document_get_info;
951 /* EvAsyncRendererIface */
953 setup_page (PSDocument *gs, int page, double scale, int rotation)
956 char scaled_dpi[G_ASCII_DTOSTR_BUF_SIZE];
957 int urx, ury, llx, lly;
958 PSDocumentClass *gs_class;
960 gs_class = PS_DOCUMENT_GET_CLASS (gs);
962 get_page_box (gs, page, &urx, &ury, &llx, &lly);
963 g_ascii_dtostr (scaled_dpi, G_ASCII_DTOSTR_BUF_SIZE, 72.0 * scale);
965 buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
966 0L, rotation, llx, lly, urx, ury,
967 scaled_dpi, scaled_dpi,
970 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
971 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen (buf));
978 setup_pixmap (PSDocument *gs, int page, double scale, int rotation)
981 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
982 GdkColormap *colormap;
983 double width, height;
984 int pixmap_width, pixmap_height;
986 if (gs->pstarget == NULL)
989 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
991 if (rotation == 90 || rotation == 270) {
992 pixmap_height = width * scale + 0.5;
993 pixmap_width = height * scale + 0.5;
995 pixmap_width = width * scale + 0.5;
996 pixmap_height = height * scale + 0.5;
1002 gdk_drawable_get_size (gs->bpixmap, &w, &h);
1004 if (pixmap_width != w || h != pixmap_height) {
1005 g_object_unref (gs->bpixmap);
1007 ps_interpreter_stop (gs);
1012 fill = gdk_gc_new (gs->pstarget);
1013 colormap = gdk_drawable_get_colormap (gs->pstarget);
1014 gdk_colormap_alloc_color (colormap, &white, FALSE, TRUE);
1015 gdk_gc_set_foreground (fill, &white);
1016 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
1018 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
1019 0, 0, pixmap_width, pixmap_height);
1024 push_pixbuf (PSDocument *gs)
1030 if (gs->pstarget == NULL)
1033 cmap = gdk_drawable_get_colormap (gs->pstarget);
1034 gdk_drawable_get_size (gs->bpixmap, &width, &height);
1035 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
1038 g_signal_emit_by_name (gs, "render_finished", pixbuf);
1039 g_object_unref (pixbuf);
1043 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
1045 PSDocument *gs = (PSDocument *) data;
1046 PSDocumentClass *gs_class;
1048 if (event->type != GDK_CLIENT_EVENT)
1051 gs_class = PS_DOCUMENT_GET_CLASS (gs);
1053 gs->message_window = event->client.data.l[0];
1055 if (event->client.message_type == gs_class->page_atom) {
1065 send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
1069 if (!gs->interpreter_input) {
1070 g_critical ("No pipe to gs: error in send_ps().");
1074 ps_new = g_new0 (PSSection, 1);
1075 ps_new->fp = gs->gs_psfile;
1076 ps_new->begin = begin;
1078 ps_new->seek_needed = TRUE;
1079 ps_new->close = close;
1081 if (gs->input_buffer == NULL) {
1082 gs->input_buffer = g_malloc (MAX_BUFSIZE);
1085 if (g_queue_is_empty (gs->ps_input)) {
1086 gs->input_buffer_ptr = gs->input_buffer;
1087 gs->bytes_left = len;
1088 gs->buffer_bytes_left = 0;
1089 g_queue_push_head (gs->ps_input, ps_new);
1090 gs->interpreter_input_id =
1091 g_io_add_watch (gs->interpreter_input,
1092 G_IO_OUT/* | G_IO_HUP | G_IO_ERR | G_IO_NVAL*/,
1093 (GIOFunc)ps_interpreter_input,
1096 g_queue_push_head (gs->ps_input, ps_new);
1101 ps_document_next_page (PSDocument *gs)
1105 GdkDisplay *display;
1107 PSDocumentClass *gs_class;
1109 g_assert (gs->interpreter_pid != 0);
1110 g_assert (gs->busy != TRUE);
1112 gs_class = PS_DOCUMENT_GET_CLASS (gs);
1116 screen = gtk_window_get_screen (GTK_WINDOW (gs->target_window));
1117 display = gdk_screen_get_display (screen);
1118 dpy = gdk_x11_display_get_xdisplay (display);
1120 event.xclient.type = ClientMessage;
1121 event.xclient.display = dpy;
1122 event.xclient.window = gs->message_window;
1123 event.xclient.message_type =
1124 gdk_x11_atom_to_xatom_for_display (display,
1125 gs_class->next_atom);
1126 event.xclient.format = 32;
1128 gdk_error_trap_push ();
1129 XSendEvent (dpy, gs->message_window, FALSE, 0, &event);
1131 gdk_error_trap_pop ();
1135 render_page (PSDocument *gs, int page)
1137 g_assert (gs != NULL);
1139 if (!gs->gs_filename) {
1143 if (gs->structured_doc && gs->doc) {
1144 if (ps_interpreter_is_ready (gs)) {
1145 ps_document_next_page (gs);
1147 ps_interpreter_start (gs);
1148 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1149 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1152 send_ps (gs, gs->doc->pages[page].begin,
1153 gs->doc->pages[page].len, FALSE);
1155 /* Unstructured document
1157 * In the case of non structured documents,
1158 * GS read the PS from the actual file (via command
1159 * line. Hence, ggv only send a signal next page.
1160 * If ghostview is not running it is usually because
1161 * the last page of the file was displayed. In that
1162 * case, ggv restarts GS again and the first page is displayed.
1165 if (!ps_interpreter_is_ready (gs)) {
1166 ps_interpreter_start (gs);
1168 ps_document_next_page (gs);
1175 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer,
1180 PSDocument *gs = PS_DOCUMENT (renderer);
1182 if (gs->pstarget == NULL) {
1183 gs->target_window = gtk_window_new (GTK_WINDOW_POPUP);
1184 gtk_widget_realize (gs->target_window);
1185 gs->pstarget = gs->target_window->window;
1187 g_assert (gs->pstarget != NULL);
1189 g_signal_connect (gs->target_window, "event",
1190 G_CALLBACK (ps_document_widget_event),
1194 setup_pixmap (gs, page, scale, rotation);
1195 setup_page (gs, page, scale, rotation);
1197 render_page (gs, page);
1201 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
1203 iface->render_pixbuf = ps_async_renderer_render_pixbuf;
1206 /* EvFileExporterIface */
1208 ps_document_file_exporter_format_supported (EvFileExporter *exporter,
1209 EvFileExporterFormat format)
1211 return (format == EV_FILE_FORMAT_PS);
1215 ps_document_file_exporter_begin (EvFileExporter *exporter,
1216 EvFileExporterFormat format,
1217 const char *filename,
1224 PSDocument *document = PS_DOCUMENT (exporter);
1226 if (document->structured_doc) {
1227 g_free (document->ps_export_pagelist);
1229 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1232 document->ps_export_filename = g_strdup (filename);
1236 ps_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
1238 PSDocument *document = PS_DOCUMENT (exporter);
1240 if (document->structured_doc) {
1241 document->ps_export_pagelist[rc->page] = 1;
1246 ps_document_file_exporter_end (EvFileExporter *exporter)
1248 PSDocument *document = PS_DOCUMENT (exporter);
1250 if (!document->structured_doc) {
1251 save_document (document, document->ps_export_filename);
1253 save_page_list (document, document->ps_export_pagelist,
1254 document->ps_export_filename);
1255 g_free (document->ps_export_pagelist);
1256 g_free (document->ps_export_filename);
1257 document->ps_export_pagelist = NULL;
1258 document->ps_export_filename = NULL;
1263 ps_document_file_exporter_iface_init (EvFileExporterIface *iface)
1265 iface->format_supported = ps_document_file_exporter_format_supported;
1266 iface->begin = ps_document_file_exporter_begin;
1267 iface->do_page = ps_document_file_exporter_do_page;
1268 iface->end = ps_document_file_exporter_end;