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>
39 #include "ps-document.h"
42 #include "gsdefaults.h"
43 #include "ev-file-exporter.h"
44 #include "ev-async-renderer.h"
46 #define MAX_BUFSIZE 1024
48 #define PS_DOCUMENT_IS_COMPRESSED(gs) (PS_DOCUMENT(gs)->gs_filename_unc != NULL)
49 #define PS_DOCUMENT_GET_PS_FILE(gs) (PS_DOCUMENT_IS_COMPRESSED(gs) ? \
50 PS_DOCUMENT(gs)->gs_filename_unc : \
51 PS_DOCUMENT(gs)->gs_filename)
53 /* structure to describe section of file to send to ghostscript */
65 GtkWidget *target_window;
68 glong message_window; /* Used by ghostview to receive messages from app */
70 GPid interpreter_pid; /* PID of interpreter, -1 if none */
71 GIOChannel *interpreter_input; /* stdin of interpreter */
72 GIOChannel *interpreter_output; /* stdout of interpreter */
73 GIOChannel *interpreter_err; /* stderr of interpreter */
74 guint interpreter_input_id;
75 guint interpreter_output_id;
76 guint interpreter_error_id;
78 gboolean busy; /* Is gs busy drawing? */
79 gboolean structured_doc;
82 gchar *input_buffer_ptr;
84 guint buffer_bytes_left;
86 FILE *gs_psfile; /* the currently loaded FILE */
87 gchar *gs_filename; /* the currently loaded filename */
88 gchar *gs_filename_unc; /* Uncompressed file */
90 gboolean send_filename_to_gs; /* True if gs should read from file directly */
93 gint *ps_export_pagelist;
94 gchar *ps_export_filename;
96 const gchar *gs_status; /* PSDocument status */
99 struct _PSDocumentClass {
100 GObjectClass parent_class;
108 static void ps_document_document_iface_init (EvDocumentIface *iface);
109 static void ps_document_file_exporter_iface_init (EvFileExporterIface *iface);
110 static void ps_async_renderer_iface_init (EvAsyncRendererIface *iface);
112 static void ps_interpreter_start (PSDocument *gs);
113 static void ps_interpreter_stop (PSDocument *gs);
114 static void ps_interpreter_failed (PSDocument *gs,
116 static gboolean ps_interpreter_is_ready (PSDocument *gs);
118 static void push_pixbuf (PSDocument *gs);
121 G_DEFINE_TYPE_WITH_CODE (PSDocument, ps_document, G_TYPE_OBJECT,
123 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
124 ps_document_document_iface_init);
125 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
126 ps_document_file_exporter_iface_init);
127 G_IMPLEMENT_INTERFACE (EV_TYPE_ASYNC_RENDERER,
128 ps_async_renderer_iface_init);
132 ps_section_free (PSSection *section)
137 if (section->close && section->fp)
138 fclose (section->fp);
145 ps_document_init (PSDocument *gs)
148 gs->interpreter_pid = -1;
152 gs->gs_filename_unc = 0;
154 gs->structured_doc = FALSE;
155 gs->send_filename_to_gs = FALSE;
159 gs->interpreter_input = NULL;
160 gs->interpreter_output = NULL;
161 gs->interpreter_err = NULL;
162 gs->interpreter_input_id = 0;
163 gs->interpreter_output_id = 0;
164 gs->interpreter_error_id = 0;
166 gs->ps_input = g_queue_new ();
167 gs->input_buffer = NULL;
168 gs->input_buffer_ptr = NULL;
170 gs->buffer_bytes_left = 0;
172 gs->gs_status = _("No document loaded.");
174 gs->ps_export_pagelist = NULL;
175 gs->ps_export_filename = NULL;
179 ps_document_dispose (GObject *object)
181 PSDocument *gs = PS_DOCUMENT (object);
184 fclose (gs->gs_psfile);
185 gs->gs_psfile = NULL;
188 if (gs->gs_filename) {
189 g_free (gs->gs_filename);
190 gs->gs_filename = NULL;
198 if (gs->gs_filename_unc) {
199 g_unlink(gs->gs_filename_unc);
200 g_free(gs->gs_filename_unc);
201 gs->gs_filename_unc = NULL;
205 g_object_unref (gs->bpixmap);
209 if (gs->input_buffer) {
210 g_free (gs->input_buffer);
211 gs->input_buffer = NULL;
214 if (gs->target_window) {
215 gtk_widget_destroy (gs->target_window);
216 gs->target_window = NULL;
221 g_queue_foreach (gs->ps_input, (GFunc)ps_section_free, NULL);
222 g_queue_free (gs->ps_input);
226 ps_interpreter_stop (gs);
228 G_OBJECT_CLASS (ps_document_parent_class)->dispose (object);
232 ps_document_class_init (PSDocumentClass *klass)
234 GObjectClass *object_class;
236 object_class = G_OBJECT_CLASS (klass);
238 object_class->dispose = ps_document_dispose;
240 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
241 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
242 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
243 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
248 ps_interpreter_input (GIOChannel *io,
249 GIOCondition condition,
252 PSSection *section = NULL;
255 if (gs->buffer_bytes_left == 0) {
256 /* Get a new section if required */
257 if (gs->bytes_left == 0) {
258 ps_section_free (section);
260 g_queue_pop_tail (gs->ps_input);
263 if (section == NULL) {
264 section = g_queue_peek_tail (gs->ps_input);
267 /* Have to seek at the beginning of each section */
268 if (section && section->seek_needed) {
269 fseek (section->fp, section->begin, SEEK_SET);
270 section->seek_needed = FALSE;
271 gs->bytes_left = section->len;
274 if (gs->bytes_left > MAX_BUFSIZE) {
275 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof (char),
276 MAX_BUFSIZE, section->fp);
277 } else if (gs->bytes_left > 0) {
278 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof (char),
279 gs->bytes_left, section->fp);
281 gs->buffer_bytes_left = 0;
284 if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
285 ps_interpreter_failed (gs, NULL); /* Error occurred */
288 gs->input_buffer_ptr = gs->input_buffer;
289 gs->bytes_left -= gs->buffer_bytes_left;
292 if (gs->buffer_bytes_left > 0) {
295 GError *error = NULL;
297 status = g_io_channel_write_chars (gs->interpreter_input,
298 gs->input_buffer_ptr,
299 gs->buffer_bytes_left,
303 case G_IO_STATUS_NORMAL:
304 gs->buffer_bytes_left -= bytes_written;
305 gs->input_buffer_ptr += bytes_written;
308 case G_IO_STATUS_ERROR:
309 ps_interpreter_failed (gs, error->message);
310 g_error_free (error);
313 case G_IO_STATUS_AGAIN:
318 } while (!g_queue_is_empty (gs->ps_input) && gs->buffer_bytes_left == 0);
320 if (g_queue_is_empty (gs->ps_input) && gs->buffer_bytes_left == 0) {
323 flags = g_io_channel_get_flags (gs->interpreter_input);
325 g_io_channel_set_flags (gs->interpreter_input,
326 flags & ~G_IO_FLAG_NONBLOCK, NULL);
327 g_io_channel_flush (gs->interpreter_input, NULL);
328 g_io_channel_set_flags (gs->interpreter_input,
329 flags | G_IO_FLAG_NONBLOCK, NULL);
331 gs->interpreter_input_id = 0;
340 ps_interpreter_output (GIOChannel *io,
341 GIOCondition condition,
344 gchar buf[MAX_BUFSIZE + 1];
347 GError *error = NULL;
349 status = g_io_channel_read_chars (io, buf, MAX_BUFSIZE,
352 case G_IO_STATUS_NORMAL:
358 case G_IO_STATUS_EOF:
359 g_io_channel_unref (gs->interpreter_output);
360 gs->interpreter_output = NULL;
361 gs->interpreter_output_id = 0;
364 case G_IO_STATUS_ERROR:
365 ps_interpreter_failed (gs, error->message);
366 g_error_free (error);
367 gs->interpreter_output_id = 0;
374 if (!gs->interpreter_err) {
375 ps_interpreter_failed (gs, NULL);
382 ps_interpreter_error (GIOChannel *io,
383 GIOCondition condition,
386 gchar buf[MAX_BUFSIZE + 1];
389 GError *error = NULL;
391 status = g_io_channel_read_chars (io, buf, MAX_BUFSIZE,
394 case G_IO_STATUS_NORMAL:
401 case G_IO_STATUS_EOF:
402 g_io_channel_unref (gs->interpreter_err);
403 gs->interpreter_err = NULL;
404 gs->interpreter_error_id = 0;
407 case G_IO_STATUS_ERROR:
408 ps_interpreter_failed (gs, error->message);
409 g_error_free (error);
410 gs->interpreter_error_id = 0;
417 if (!gs->interpreter_output) {
418 ps_interpreter_failed (gs, NULL);
425 ps_interpreter_finished (GPid pid,
429 g_spawn_close_pid (gs->interpreter_pid);
430 gs->interpreter_pid = -1;
431 ps_interpreter_failed (gs, NULL);
435 #define NUM_GS_ARGS (NUM_ARGS - 20)
436 #define NUM_ALPHA_ARGS 10
439 setup_interpreter_env (gchar **envp)
443 for (i = 0; envp[i]; i++)
448 ps_interpreter_start (PSDocument *gs)
450 gchar *argv[NUM_ARGS], *dir, *gv_env, *gs_path;
451 gchar **gs_args, **alpha_args = NULL;
453 gint pin, pout, perr;
455 GError *error = NULL;
457 if (!gs->gs_filename)
460 ps_interpreter_stop (gs);
462 dir = g_path_get_dirname (gs->gs_filename);
464 /* set up the args... */
465 gs_path = g_find_program_in_path ("gs");
466 gs_args = g_strsplit (gs_path, " ", NUM_GS_ARGS);
468 for (i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
469 argv[argc] = gs_args[i];
472 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
473 for (i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
474 argv[argc] = alpha_args[i];
477 argv[argc++] = "-dNOPAUSE";
478 argv[argc++] = "-dQUIET";
479 argv[argc++] = "-dSAFER";
481 if (gs->send_filename_to_gs) {
482 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
484 argv[argc++] = "quit";
491 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld;DISPLAY=%s",
492 gdk_x11_drawable_get_xid (gs->pstarget),
493 gdk_x11_drawable_get_xid (gs->bpixmap),
494 gdk_display_get_name (gdk_drawable_get_display (gs->pstarget)));
495 envp = g_strsplit (gv_env, ";", 2);
498 if (g_spawn_async_with_pipes (dir, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
499 (GSpawnChildSetupFunc)setup_interpreter_env, envp,
500 &(gs->interpreter_pid),
505 g_child_watch_add (gs->interpreter_pid,
506 (GChildWatchFunc)ps_interpreter_finished,
509 gs->interpreter_input = g_io_channel_unix_new (pin);
510 g_io_channel_set_encoding (gs->interpreter_input, NULL, NULL);
511 flags = g_io_channel_get_flags (gs->interpreter_input);
512 g_io_channel_set_flags (gs->interpreter_input,
513 flags | G_IO_FLAG_NONBLOCK, NULL);
516 gs->interpreter_output = g_io_channel_unix_new (pout);
517 flags = g_io_channel_get_flags (gs->interpreter_output);
518 g_io_channel_set_flags (gs->interpreter_output,
519 flags | G_IO_FLAG_NONBLOCK, NULL);
520 gs->interpreter_output_id =
521 g_io_add_watch (gs->interpreter_output, G_IO_IN,
522 (GIOFunc)ps_interpreter_output,
525 gs->interpreter_err = g_io_channel_unix_new (perr);
526 flags = g_io_channel_get_flags (gs->interpreter_err);
527 g_io_channel_set_flags (gs->interpreter_err,
528 flags | G_IO_FLAG_NONBLOCK, NULL);
529 gs->interpreter_error_id =
530 g_io_add_watch (gs->interpreter_err, G_IO_IN,
531 (GIOFunc)ps_interpreter_error,
534 g_warning (error->message);
535 g_error_free (error);
540 g_strfreev (gs_args);
541 g_strfreev (alpha_args);
545 ps_interpreter_stop (PSDocument *gs)
547 if (gs->interpreter_pid > 0) {
550 kill (gs->interpreter_pid, SIGTERM);
551 while ((wait (&status) == -1) && (errno == EINTR));
552 g_spawn_close_pid (gs->interpreter_pid);
553 gs->interpreter_pid = -1;
556 gs->gs_status = _("Interpreter failed.");
560 if (gs->interpreter_input) {
561 g_io_channel_unref (gs->interpreter_input);
562 gs->interpreter_input = NULL;
564 if (gs->interpreter_input_id > 0) {
565 g_source_remove (gs->interpreter_input_id);
566 gs->interpreter_input_id = 0;
570 g_queue_foreach (gs->ps_input, (GFunc)ps_section_free, NULL);
571 g_queue_free (gs->ps_input);
572 gs->ps_input = g_queue_new ();
576 if (gs->interpreter_output) {
577 g_io_channel_unref (gs->interpreter_output);
578 gs->interpreter_output = NULL;
580 if (gs->interpreter_output_id > 0) {
581 g_source_remove (gs->interpreter_output_id);
582 gs->interpreter_output_id = 0;
586 if (gs->interpreter_err) {
587 g_io_channel_unref (gs->interpreter_err);
588 gs->interpreter_err = NULL;
590 if (gs->interpreter_error_id > 0) {
591 g_source_remove (gs->interpreter_error_id);
592 gs->interpreter_error_id = 0;
600 ps_interpreter_failed (PSDocument *gs, const char *msg)
602 g_warning (msg ? msg : _("Interpreter failed."));
605 ps_interpreter_stop (gs);
609 ps_interpreter_is_ready (PSDocument *gs)
611 return (gs->interpreter_pid != -1 &&
613 (g_queue_is_empty (gs->ps_input)));
616 /* EvDocumentIface */
618 * Decompress gs->gs_filename if necessary
619 * Set gs->filename_unc to the name of the uncompressed file or NULL.
620 * Error reporting via signal 'interpreter_message'
621 * Return name of input file to use or NULL on error..
624 check_filecompressed (PSDocument * gs)
628 gchar *filename, *filename_unc, *filename_err, *cmdline;
634 if ((file = fopen(gs->gs_filename, "r")) &&
635 (fread (buf, sizeof(gchar), 3, file) == 3)) {
636 if ((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
637 /* file is gzipped or compressed */
638 cmd = gtk_gs_defaults_get_ungzip_cmd ();
639 } else if (strncmp (buf, "BZh", 3) == 0) {
640 /* file is compressed with bzip2 */
641 cmd = gtk_gs_defaults_get_unbzip2_cmd ();
649 return gs->gs_filename;
651 /* do the decompression */
652 filename = g_shell_quote (gs->gs_filename);
653 filename_unc = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
654 if ((fd = g_mkstemp (filename_unc)) < 0) {
655 g_free (filename_unc);
661 filename_err = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
662 if ((fd = g_mkstemp (filename_err)) < 0) {
663 g_free (filename_err);
664 g_free (filename_unc);
670 cmdline = g_strdup_printf ("%s %s >%s 2>%s", cmd,
671 filename, filename_unc, filename_err);
673 if (system (cmdline) == 0 &&
674 g_file_test (filename_unc, G_FILE_TEST_IS_REGULAR)) {
675 /* sucessfully uncompressed file */
676 gs->gs_filename_unc = filename_unc;
682 filename_dsp = g_filename_display_name (gs->gs_filename);
683 msg = g_strdup_printf (_("Error while decompressing file “%s”:\n"),
685 g_free (filename_dsp);
687 ps_interpreter_failed (gs, msg);
689 g_unlink (filename_unc);
690 g_free (filename_unc);
694 g_unlink (filename_err);
695 g_free (filename_err);
703 document_load (PSDocument *gs, const gchar *fname)
705 const gchar *filename;
712 /* prepare this document */
713 gs->structured_doc = FALSE;
714 gs->send_filename_to_gs = TRUE;
715 gs->gs_filename = g_strdup (fname);
718 * We need to make sure that the file is loadable/exists!
719 * otherwise we want to exit without loading new stuff...
721 if (!g_file_test (fname, G_FILE_TEST_IS_REGULAR)) {
725 filename_dsp = g_filename_display_name (fname);
726 msg = g_strdup_printf (_("Cannot open file “%s”.\n"), filename_dsp);
727 g_free (filename_dsp);
729 ps_interpreter_failed (gs, msg);
731 gs->gs_status = _("File is not readable.");
736 filename = check_filecompressed (gs);
738 if (!filename || (gs->gs_psfile = fopen (filename, "r")) == NULL) {
739 ps_interpreter_failed (gs, NULL);
743 /* we grab the vital statistics!!! */
744 gs->doc = psscan (gs->gs_psfile, TRUE, filename);
746 if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
747 (gs->doc->epsf && gs->doc->numpages > 1)) {
748 gs->structured_doc = TRUE;
749 gs->send_filename_to_gs = FALSE;
752 gs->gs_status = _("Document loaded.");
758 ps_document_load (EvDocument *document,
766 filename = g_filename_from_uri (uri, NULL, error);
770 gs_path = g_find_program_in_path ("gs");
774 filename_dsp = g_filename_display_name (filename);
778 _("Failed to load document “%s”. Ghostscript interpreter was not found in path"),
780 g_free (filename_dsp);
786 result = document_load (PS_DOCUMENT (document), filename);
790 filename_dsp = g_filename_display_name (filename);
791 g_set_error (error, G_FILE_ERROR,
793 _("Failed to load document “%s”"),
795 g_free (filename_dsp);
805 save_document (PSDocument *document, const char *filename)
807 gboolean result = TRUE;
808 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
812 src_file = fopen (PS_DOCUMENT_GET_PS_FILE(document), "r");
814 struct stat stat_rec;
816 if (stat (PS_DOCUMENT_GET_PS_FILE(document), &stat_rec) == 0) {
817 pscopy (src_file, sink, 0, stat_rec.st_size - 1);
823 buf = gtk_gs_doc_sink_get_buffer (sink);
828 f = fopen (filename, "w");
837 gtk_gs_doc_sink_free (sink);
844 save_page_list (PSDocument *document, int *page_list, const char *filename)
846 gboolean result = TRUE;
847 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
851 pscopydoc (sink, PS_DOCUMENT_GET_PS_FILE(document),
852 document->doc, page_list);
854 buf = gtk_gs_doc_sink_get_buffer (sink);
856 f = fopen (filename, "w");
865 gtk_gs_doc_sink_free (sink);
872 ps_document_save (EvDocument *document,
876 PSDocument *ps = PS_DOCUMENT (document);
880 filename = g_filename_from_uri (uri, NULL, error);
884 result = save_document (ps, filename);
892 ps_document_get_n_pages (EvDocument *document)
894 PSDocument *ps = PS_DOCUMENT (document);
896 if (!ps->gs_filename || !ps->doc) {
900 return ps->structured_doc ? ps->doc->numpages : 1;
903 #define DEFAULT_PAGE_SIZE 1
906 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
912 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
913 int new_pagesize = -1;
915 if (new_pagesize == -1) {
916 new_pagesize = DEFAULT_PAGE_SIZE;
918 /* If we have a document:
919 * We use -- the page size (if specified)
920 * or the doc. size (if specified)
921 * or the page bbox (if specified)
922 * or the bounding box
924 if ((page >= 0) && (gs->doc->numpages > page) &&
925 (gs->doc->pages) && (gs->doc->pages[page].size)) {
926 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
927 } else if (gs->doc->default_page_size != NULL) {
928 new_pagesize = gs->doc->default_page_size - gs->doc->size;
929 } else if ((page >= 0) &&
930 (gs->doc->numpages > page) &&
932 (gs->doc->pages[page].boundingbox[URX] >
933 gs->doc->pages[page].boundingbox[LLX]) &&
934 (gs->doc->pages[page].boundingbox[URY] >
935 gs->doc->pages[page].boundingbox[LLY])) {
937 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
938 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
944 /* Compute bounding box */
945 if (gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
948 (gs->doc->pages[page].boundingbox[URX] >
949 gs->doc->pages[page].boundingbox[LLX]) &&
950 (gs->doc->pages[page].boundingbox[URY] >
951 gs->doc->pages[page].boundingbox[LLY])) {
953 new_llx = gs->doc->pages[page].boundingbox[LLX];
954 new_lly = gs->doc->pages[page].boundingbox[LLY];
955 new_urx = gs->doc->pages[page].boundingbox[URX];
956 new_ury = gs->doc->pages[page].boundingbox[URY];
957 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
958 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
960 new_llx = gs->doc->boundingbox[LLX];
961 new_lly = gs->doc->boundingbox[LLY];
962 new_urx = gs->doc->boundingbox[URX];
963 new_ury = gs->doc->boundingbox[URY];
966 if (new_pagesize < 0)
967 new_pagesize = DEFAULT_PAGE_SIZE;
968 new_llx = new_lly = 0;
969 if (gs->doc && gs->doc->size &&
970 (new_pagesize < gs->doc->numsizes)) {
971 new_urx = gs->doc->size[new_pagesize].width;
972 new_ury = gs->doc->size[new_pagesize].height;
974 new_urx = papersizes[new_pagesize].width;
975 new_ury = papersizes[new_pagesize].height;
979 if (new_urx <= new_llx)
980 new_urx = papersizes[12].width;
981 if (new_ury <= new_lly)
982 new_ury = papersizes[12].height;
991 ps_document_get_page_size (EvDocument *document,
996 PSDocument *gs = PS_DOCUMENT (document);
997 int urx, ury, llx, lly;
999 get_page_box (gs, page, &urx, &ury, &llx, &lly);
1002 *width = (urx - llx) + 0.5;
1006 *height = (ury - lly) + 0.5;
1011 ps_document_can_get_text (EvDocument *document)
1016 static EvDocumentInfo *
1017 ps_document_get_info (EvDocument *document)
1019 EvDocumentInfo *info;
1020 PSDocument *ps = PS_DOCUMENT (document);
1021 int urx, ury, llx, lly;
1023 info = g_new0 (EvDocumentInfo, 1);
1024 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
1025 EV_DOCUMENT_INFO_FORMAT |
1026 EV_DOCUMENT_INFO_CREATOR |
1027 EV_DOCUMENT_INFO_N_PAGES |
1028 EV_DOCUMENT_INFO_PAPER_SIZE;
1030 info->title = g_strdup (ps->doc->title);
1031 info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
1032 : g_strdup (_("PostScript"));
1033 info->creator = g_strdup (ps->doc->creator);
1034 info->n_pages = ev_document_get_n_pages (document);
1036 get_page_box (PS_DOCUMENT (document), 0, &urx, &ury, &llx, &lly);
1038 info->paper_width = (urx - llx) / 72.0f * 25.4f;
1039 info->paper_height = (ury - lly) / 72.0f * 25.4f;
1045 ps_document_document_iface_init (EvDocumentIface *iface)
1047 iface->load = ps_document_load;
1048 iface->save = ps_document_save;
1049 iface->can_get_text = ps_document_can_get_text;
1050 iface->get_n_pages = ps_document_get_n_pages;
1051 iface->get_page_size = ps_document_get_page_size;
1052 iface->get_info = ps_document_get_info;
1055 /* EvAsyncRendererIface */
1057 setup_page (PSDocument *gs, int page, double scale, int rotation)
1060 char scaled_dpi[G_ASCII_DTOSTR_BUF_SIZE];
1061 int urx, ury, llx, lly;
1062 PSDocumentClass *gs_class;
1064 gs_class = PS_DOCUMENT_GET_CLASS (gs);
1066 get_page_box (gs, page, &urx, &ury, &llx, &lly);
1067 g_ascii_dtostr (scaled_dpi, G_ASCII_DTOSTR_BUF_SIZE, 72.0 * scale);
1069 buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
1070 0L, rotation, llx, lly, urx, ury,
1071 scaled_dpi, scaled_dpi,
1074 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
1075 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen (buf));
1082 setup_pixmap (PSDocument *gs, int page, double scale, int rotation)
1085 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
1086 GdkColormap *colormap;
1087 double width, height;
1088 int pixmap_width, pixmap_height;
1090 if (gs->pstarget == NULL)
1093 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
1095 if (rotation == 90 || rotation == 270) {
1096 pixmap_height = width * scale + 0.5;
1097 pixmap_width = height * scale + 0.5;
1099 pixmap_width = width * scale + 0.5;
1100 pixmap_height = height * scale + 0.5;
1106 gdk_drawable_get_size (gs->bpixmap, &w, &h);
1108 if (pixmap_width != w || h != pixmap_height) {
1109 g_object_unref (gs->bpixmap);
1111 ps_interpreter_stop (gs);
1116 fill = gdk_gc_new (gs->pstarget);
1117 colormap = gdk_drawable_get_colormap (gs->pstarget);
1118 gdk_colormap_alloc_color (colormap, &white, FALSE, TRUE);
1119 gdk_gc_set_foreground (fill, &white);
1120 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
1122 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
1123 0, 0, pixmap_width, pixmap_height);
1128 push_pixbuf (PSDocument *gs)
1134 if (gs->pstarget == NULL)
1137 cmap = gdk_drawable_get_colormap (gs->pstarget);
1138 gdk_drawable_get_size (gs->bpixmap, &width, &height);
1139 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
1142 g_signal_emit_by_name (gs, "render_finished", pixbuf);
1143 g_object_unref (pixbuf);
1147 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
1149 PSDocument *gs = (PSDocument *) data;
1150 PSDocumentClass *gs_class;
1152 if (event->type != GDK_CLIENT_EVENT)
1155 gs_class = PS_DOCUMENT_GET_CLASS (gs);
1157 gs->message_window = event->client.data.l[0];
1159 if (event->client.message_type == gs_class->page_atom) {
1169 send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
1173 if (!gs->interpreter_input) {
1174 g_critical ("No pipe to gs: error in send_ps().");
1178 ps_new = g_new0 (PSSection, 1);
1179 ps_new->fp = gs->gs_psfile;
1180 ps_new->begin = begin;
1182 ps_new->seek_needed = TRUE;
1183 ps_new->close = close;
1185 if (gs->input_buffer == NULL) {
1186 gs->input_buffer = g_malloc (MAX_BUFSIZE);
1189 if (g_queue_is_empty (gs->ps_input)) {
1190 gs->input_buffer_ptr = gs->input_buffer;
1191 gs->bytes_left = len;
1192 gs->buffer_bytes_left = 0;
1193 g_queue_push_head (gs->ps_input, ps_new);
1194 gs->interpreter_input_id =
1195 g_io_add_watch (gs->interpreter_input,
1196 G_IO_OUT/* | G_IO_HUP | G_IO_ERR | G_IO_NVAL*/,
1197 (GIOFunc)ps_interpreter_input,
1200 g_queue_push_head (gs->ps_input, ps_new);
1205 ps_document_next_page (PSDocument *gs)
1209 GdkDisplay *display;
1211 PSDocumentClass *gs_class;
1213 g_assert (gs->interpreter_pid != 0);
1214 g_assert (gs->busy != TRUE);
1216 gs_class = PS_DOCUMENT_GET_CLASS (gs);
1220 screen = gtk_window_get_screen (GTK_WINDOW (gs->target_window));
1221 display = gdk_screen_get_display (screen);
1222 dpy = gdk_x11_display_get_xdisplay (display);
1224 event.xclient.type = ClientMessage;
1225 event.xclient.display = dpy;
1226 event.xclient.window = gs->message_window;
1227 event.xclient.message_type =
1228 gdk_x11_atom_to_xatom_for_display (display,
1229 gs_class->next_atom);
1230 event.xclient.format = 32;
1232 gdk_error_trap_push ();
1233 XSendEvent (dpy, gs->message_window, FALSE, 0, &event);
1235 gdk_error_trap_pop ();
1239 render_page (PSDocument *gs, int page)
1241 g_assert (gs != NULL);
1243 if (!gs->gs_filename) {
1247 if (gs->structured_doc && gs->doc) {
1248 if (ps_interpreter_is_ready (gs)) {
1249 ps_document_next_page (gs);
1251 ps_interpreter_start (gs);
1252 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1253 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1256 send_ps (gs, gs->doc->pages[page].begin,
1257 gs->doc->pages[page].len, FALSE);
1259 /* Unstructured document
1261 * In the case of non structured documents,
1262 * GS read the PS from the actual file (via command
1263 * line. Hence, ggv only send a signal next page.
1264 * If ghostview is not running it is usually because
1265 * the last page of the file was displayed. In that
1266 * case, ggv restarts GS again and the first page is displayed.
1269 if (!ps_interpreter_is_ready (gs)) {
1270 ps_interpreter_start (gs);
1272 ps_document_next_page (gs);
1279 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer,
1284 PSDocument *gs = PS_DOCUMENT (renderer);
1286 if (gs->pstarget == NULL) {
1287 gs->target_window = gtk_window_new (GTK_WINDOW_POPUP);
1288 gtk_widget_realize (gs->target_window);
1289 gs->pstarget = gs->target_window->window;
1291 g_assert (gs->pstarget != NULL);
1293 g_signal_connect (gs->target_window, "event",
1294 G_CALLBACK (ps_document_widget_event),
1298 setup_pixmap (gs, page, scale, rotation);
1299 setup_page (gs, page, scale, rotation);
1301 render_page (gs, page);
1305 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
1307 iface->render_pixbuf = ps_async_renderer_render_pixbuf;
1310 /* EvFileExporterIface */
1312 ps_document_file_exporter_format_supported (EvFileExporter *exporter,
1313 EvFileExporterFormat format)
1315 return (format == EV_FILE_FORMAT_PS);
1319 ps_document_file_exporter_begin (EvFileExporter *exporter,
1320 EvFileExporterFormat format,
1321 const char *filename,
1328 PSDocument *document = PS_DOCUMENT (exporter);
1330 if (document->structured_doc) {
1331 g_free (document->ps_export_pagelist);
1333 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1336 document->ps_export_filename = g_strdup (filename);
1340 ps_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
1342 PSDocument *document = PS_DOCUMENT (exporter);
1344 if (document->structured_doc) {
1345 document->ps_export_pagelist[rc->page] = 1;
1350 ps_document_file_exporter_end (EvFileExporter *exporter)
1352 PSDocument *document = PS_DOCUMENT (exporter);
1354 if (!document->structured_doc) {
1355 save_document (document, document->ps_export_filename);
1357 save_page_list (document, document->ps_export_pagelist,
1358 document->ps_export_filename);
1359 g_free (document->ps_export_pagelist);
1360 g_free (document->ps_export_filename);
1361 document->ps_export_pagelist = NULL;
1362 document->ps_export_filename = NULL;
1367 ps_document_file_exporter_iface_init (EvFileExporterIface *iface)
1369 iface->format_supported = ps_document_file_exporter_format_supported;
1370 iface->begin = ps_document_file_exporter_begin;
1371 iface->do_page = ps_document_file_exporter_do_page;
1372 iface->end = ps_document_file_exporter_end;