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 #define NUM_GS_ARGS (NUM_ARGS - 20)
426 #define NUM_ALPHA_ARGS 10
429 setup_interpreter_env (gchar **envp)
433 for (i = 0; envp[i]; i++)
438 ps_interpreter_start (PSDocument *gs)
440 gchar *argv[NUM_ARGS], *dir, *gv_env, *gs_path;
441 gchar **gs_args, **alpha_args = NULL;
443 gint pin, pout, perr;
445 GError *error = NULL;
447 if (!gs->gs_filename)
450 ps_interpreter_stop (gs);
452 dir = g_path_get_dirname (gs->gs_filename);
454 /* set up the args... */
455 gs_path = g_find_program_in_path ("gs");
456 gs_args = g_strsplit (gs_path, " ", NUM_GS_ARGS);
458 for (i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
459 argv[argc] = gs_args[i];
462 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
463 for (i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
464 argv[argc] = alpha_args[i];
467 argv[argc++] = "-dNOPAUSE";
468 argv[argc++] = "-dQUIET";
469 argv[argc++] = "-dSAFER";
471 if (gs->send_filename_to_gs) {
472 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
474 argv[argc++] = "quit";
481 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld;DISPLAY=%s",
482 gdk_x11_drawable_get_xid (gs->pstarget),
483 gdk_x11_drawable_get_xid (gs->bpixmap),
484 gdk_display_get_name (gdk_drawable_get_display (gs->pstarget)));
485 envp = g_strsplit (gv_env, ";", 2);
488 if (g_spawn_async_with_pipes (dir, argv, NULL, 0,
489 (GSpawnChildSetupFunc)setup_interpreter_env, envp,
490 &(gs->interpreter_pid),
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)
592 ps_interpreter_stop (gs);
596 ps_interpreter_is_ready (PSDocument *gs)
598 return (gs->interpreter_pid != -1 &&
600 (g_queue_is_empty (gs->ps_input)));
603 /* EvDocumentIface */
605 * Decompress gs->gs_filename if necessary
606 * Set gs->filename_unc to the name of the uncompressed file or NULL.
607 * Error reporting via signal 'interpreter_message'
608 * Return name of input file to use or NULL on error..
611 check_filecompressed (PSDocument * gs)
615 gchar *filename, *filename_unc, *filename_err, *cmdline;
621 if ((file = fopen(gs->gs_filename, "r")) &&
622 (fread (buf, sizeof(gchar), 3, file) == 3)) {
623 if ((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
624 /* file is gzipped or compressed */
625 cmd = gtk_gs_defaults_get_ungzip_cmd ();
626 } else if (strncmp (buf, "BZh", 3) == 0) {
627 /* file is compressed with bzip2 */
628 cmd = gtk_gs_defaults_get_unbzip2_cmd ();
636 return gs->gs_filename;
638 /* do the decompression */
639 filename = g_shell_quote (gs->gs_filename);
640 filename_unc = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
641 if ((fd = g_mkstemp (filename_unc)) < 0) {
642 g_free (filename_unc);
648 filename_err = g_strconcat (g_get_tmp_dir (), "/evinceXXXXXX", NULL);
649 if ((fd = g_mkstemp (filename_err)) < 0) {
650 g_free (filename_err);
651 g_free (filename_unc);
657 cmdline = g_strdup_printf ("%s %s >%s 2>%s", cmd,
658 filename, filename_unc, filename_err);
660 if (system (cmdline) == 0 &&
661 g_file_test (filename_unc, G_FILE_TEST_IS_REGULAR)) {
662 /* sucessfully uncompressed file */
663 gs->gs_filename_unc = filename_unc;
669 filename_dsp = g_filename_display_name (gs->gs_filename);
670 msg = g_strdup_printf (_("Error while decompressing file “%s”:\n"),
672 g_free (filename_dsp);
674 ps_interpreter_failed (gs, msg);
676 g_unlink (filename_unc);
677 g_free (filename_unc);
681 g_unlink (filename_err);
682 g_free (filename_err);
690 document_load (PSDocument *gs, const gchar *fname)
692 const gchar *filename;
699 /* prepare this document */
700 gs->structured_doc = FALSE;
701 gs->send_filename_to_gs = TRUE;
702 gs->gs_filename = g_strdup (fname);
705 * We need to make sure that the file is loadable/exists!
706 * otherwise we want to exit without loading new stuff...
708 if (!g_file_test (fname, G_FILE_TEST_IS_REGULAR)) {
712 filename_dsp = g_filename_display_name (fname);
713 msg = g_strdup_printf (_("Cannot open file “%s”.\n"), filename_dsp);
714 g_free (filename_dsp);
716 ps_interpreter_failed (gs, msg);
718 gs->gs_status = _("File is not readable.");
723 filename = check_filecompressed (gs);
725 if (!filename || (gs->gs_psfile = fopen (filename, "r")) == NULL) {
726 ps_interpreter_failed (gs, NULL);
730 /* we grab the vital statistics!!! */
731 gs->doc = psscan (gs->gs_psfile, TRUE, filename);
733 if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
734 (gs->doc->epsf && gs->doc->numpages > 1)) {
735 gs->structured_doc = TRUE;
736 gs->send_filename_to_gs = FALSE;
739 gs->gs_status = _("Document loaded.");
745 ps_document_load (EvDocument *document,
753 filename = g_filename_from_uri (uri, NULL, error);
757 gs_path = g_find_program_in_path ("gs");
761 filename_dsp = g_filename_display_name (filename);
765 _("Failed to load document “%s”. Ghostscript interpreter was not found in path"),
767 g_free (filename_dsp);
773 result = document_load (PS_DOCUMENT (document), filename);
777 filename_dsp = g_filename_display_name (filename);
778 g_set_error (error, G_FILE_ERROR,
780 _("Failed to load document “%s”"),
782 g_free (filename_dsp);
792 save_document (PSDocument *document, const char *filename)
794 gboolean result = TRUE;
795 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
799 src_file = fopen (PS_DOCUMENT_GET_PS_FILE(document), "r");
801 struct stat stat_rec;
803 if (stat (PS_DOCUMENT_GET_PS_FILE(document), &stat_rec) == 0) {
804 pscopy (src_file, sink, 0, stat_rec.st_size - 1);
810 buf = gtk_gs_doc_sink_get_buffer (sink);
815 f = fopen (filename, "w");
824 gtk_gs_doc_sink_free (sink);
831 save_page_list (PSDocument *document, int *page_list, const char *filename)
833 gboolean result = TRUE;
834 GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
838 pscopydoc (sink, PS_DOCUMENT_GET_PS_FILE(document),
839 document->doc, page_list);
841 buf = gtk_gs_doc_sink_get_buffer (sink);
843 f = fopen (filename, "w");
852 gtk_gs_doc_sink_free (sink);
859 ps_document_save (EvDocument *document,
863 PSDocument *ps = PS_DOCUMENT (document);
867 filename = g_filename_from_uri (uri, NULL, error);
871 result = save_document (ps, filename);
879 ps_document_get_n_pages (EvDocument *document)
881 PSDocument *ps = PS_DOCUMENT (document);
883 if (!ps->gs_filename || !ps->doc) {
887 return ps->structured_doc ? ps->doc->numpages : 1;
890 #define DEFAULT_PAGE_SIZE 1
893 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
899 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes ();
900 int new_pagesize = -1;
902 if (new_pagesize == -1) {
903 new_pagesize = DEFAULT_PAGE_SIZE;
905 /* If we have a document:
906 * We use -- the page size (if specified)
907 * or the doc. size (if specified)
908 * or the page bbox (if specified)
909 * or the bounding box
911 if ((page >= 0) && (gs->doc->numpages > page) &&
912 (gs->doc->pages) && (gs->doc->pages[page].size)) {
913 new_pagesize = gs->doc->pages[page].size - gs->doc->size;
914 } else if (gs->doc->default_page_size != NULL) {
915 new_pagesize = gs->doc->default_page_size - gs->doc->size;
916 } else if ((page >= 0) &&
917 (gs->doc->numpages > page) &&
919 (gs->doc->pages[page].boundingbox[URX] >
920 gs->doc->pages[page].boundingbox[LLX]) &&
921 (gs->doc->pages[page].boundingbox[URY] >
922 gs->doc->pages[page].boundingbox[LLY])) {
924 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
925 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
931 /* Compute bounding box */
932 if (gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */
935 (gs->doc->pages[page].boundingbox[URX] >
936 gs->doc->pages[page].boundingbox[LLX]) &&
937 (gs->doc->pages[page].boundingbox[URY] >
938 gs->doc->pages[page].boundingbox[LLY])) {
940 new_llx = gs->doc->pages[page].boundingbox[LLX];
941 new_lly = gs->doc->pages[page].boundingbox[LLY];
942 new_urx = gs->doc->pages[page].boundingbox[URX];
943 new_ury = gs->doc->pages[page].boundingbox[URY];
944 } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
945 (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
947 new_llx = gs->doc->boundingbox[LLX];
948 new_lly = gs->doc->boundingbox[LLY];
949 new_urx = gs->doc->boundingbox[URX];
950 new_ury = gs->doc->boundingbox[URY];
953 if (new_pagesize < 0)
954 new_pagesize = DEFAULT_PAGE_SIZE;
955 new_llx = new_lly = 0;
956 if (gs->doc && gs->doc->size &&
957 (new_pagesize < gs->doc->numsizes)) {
958 new_urx = gs->doc->size[new_pagesize].width;
959 new_ury = gs->doc->size[new_pagesize].height;
961 new_urx = papersizes[new_pagesize].width;
962 new_ury = papersizes[new_pagesize].height;
966 if (new_urx <= new_llx)
967 new_urx = papersizes[12].width;
968 if (new_ury <= new_lly)
969 new_ury = papersizes[12].height;
978 ps_document_get_page_size (EvDocument *document,
983 PSDocument *gs = PS_DOCUMENT (document);
984 int urx, ury, llx, lly;
986 get_page_box (gs, page, &urx, &ury, &llx, &lly);
989 *width = (urx - llx) + 0.5;
993 *height = (ury - lly) + 0.5;
998 ps_document_can_get_text (EvDocument *document)
1003 static EvDocumentInfo *
1004 ps_document_get_info (EvDocument *document)
1006 EvDocumentInfo *info;
1007 PSDocument *ps = PS_DOCUMENT (document);
1008 int urx, ury, llx, lly;
1010 info = g_new0 (EvDocumentInfo, 1);
1011 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
1012 EV_DOCUMENT_INFO_FORMAT |
1013 EV_DOCUMENT_INFO_CREATOR |
1014 EV_DOCUMENT_INFO_N_PAGES |
1015 EV_DOCUMENT_INFO_PAPER_SIZE;
1017 info->title = g_strdup (ps->doc->title);
1018 info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
1019 : g_strdup (_("PostScript"));
1020 info->creator = g_strdup (ps->doc->creator);
1021 info->n_pages = ev_document_get_n_pages (document);
1023 get_page_box (PS_DOCUMENT (document), 0, &urx, &ury, &llx, &lly);
1025 info->paper_width = (urx - llx) / 72.0f * 25.4f;
1026 info->paper_height = (ury - lly) / 72.0f * 25.4f;
1032 ps_document_document_iface_init (EvDocumentIface *iface)
1034 iface->load = ps_document_load;
1035 iface->save = ps_document_save;
1036 iface->can_get_text = ps_document_can_get_text;
1037 iface->get_n_pages = ps_document_get_n_pages;
1038 iface->get_page_size = ps_document_get_page_size;
1039 iface->get_info = ps_document_get_info;
1042 /* EvAsyncRendererIface */
1044 setup_page (PSDocument *gs, int page, double scale, int rotation)
1047 char scaled_dpi[G_ASCII_DTOSTR_BUF_SIZE];
1048 int urx, ury, llx, lly;
1049 PSDocumentClass *gs_class;
1051 gs_class = PS_DOCUMENT_GET_CLASS (gs);
1053 get_page_box (gs, page, &urx, &ury, &llx, &lly);
1054 g_ascii_dtostr (scaled_dpi, G_ASCII_DTOSTR_BUF_SIZE, 72.0 * scale);
1056 buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
1057 0L, rotation, llx, lly, urx, ury,
1058 scaled_dpi, scaled_dpi,
1061 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
1062 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen (buf));
1069 setup_pixmap (PSDocument *gs, int page, double scale, int rotation)
1072 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
1073 GdkColormap *colormap;
1074 double width, height;
1075 int pixmap_width, pixmap_height;
1077 if (gs->pstarget == NULL)
1080 ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
1082 if (rotation == 90 || rotation == 270) {
1083 pixmap_height = width * scale + 0.5;
1084 pixmap_width = height * scale + 0.5;
1086 pixmap_width = width * scale + 0.5;
1087 pixmap_height = height * scale + 0.5;
1093 gdk_drawable_get_size (gs->bpixmap, &w, &h);
1095 if (pixmap_width != w || h != pixmap_height) {
1096 g_object_unref (gs->bpixmap);
1098 ps_interpreter_stop (gs);
1103 fill = gdk_gc_new (gs->pstarget);
1104 colormap = gdk_drawable_get_colormap (gs->pstarget);
1105 gdk_colormap_alloc_color (colormap, &white, FALSE, TRUE);
1106 gdk_gc_set_foreground (fill, &white);
1107 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
1109 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
1110 0, 0, pixmap_width, pixmap_height);
1115 push_pixbuf (PSDocument *gs)
1121 if (gs->pstarget == NULL)
1124 cmap = gdk_drawable_get_colormap (gs->pstarget);
1125 gdk_drawable_get_size (gs->bpixmap, &width, &height);
1126 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
1129 g_signal_emit_by_name (gs, "render_finished", pixbuf);
1130 g_object_unref (pixbuf);
1134 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
1136 PSDocument *gs = (PSDocument *) data;
1137 PSDocumentClass *gs_class;
1139 if (event->type != GDK_CLIENT_EVENT)
1142 gs_class = PS_DOCUMENT_GET_CLASS (gs);
1144 gs->message_window = event->client.data.l[0];
1146 if (event->client.message_type == gs_class->page_atom) {
1156 send_ps (PSDocument *gs, long begin, unsigned int len, gboolean close)
1160 if (!gs->interpreter_input) {
1161 g_critical ("No pipe to gs: error in send_ps().");
1165 ps_new = g_new0 (PSSection, 1);
1166 ps_new->fp = gs->gs_psfile;
1167 ps_new->begin = begin;
1169 ps_new->seek_needed = TRUE;
1170 ps_new->close = close;
1172 if (gs->input_buffer == NULL) {
1173 gs->input_buffer = g_malloc (MAX_BUFSIZE);
1176 if (g_queue_is_empty (gs->ps_input)) {
1177 gs->input_buffer_ptr = gs->input_buffer;
1178 gs->bytes_left = len;
1179 gs->buffer_bytes_left = 0;
1180 g_queue_push_head (gs->ps_input, ps_new);
1181 gs->interpreter_input_id =
1182 g_io_add_watch (gs->interpreter_input,
1183 G_IO_OUT/* | G_IO_HUP | G_IO_ERR | G_IO_NVAL*/,
1184 (GIOFunc)ps_interpreter_input,
1187 g_queue_push_head (gs->ps_input, ps_new);
1192 ps_document_next_page (PSDocument *gs)
1196 GdkDisplay *display;
1198 PSDocumentClass *gs_class;
1200 g_assert (gs->interpreter_pid != 0);
1201 g_assert (gs->busy != TRUE);
1203 gs_class = PS_DOCUMENT_GET_CLASS (gs);
1207 screen = gtk_window_get_screen (GTK_WINDOW (gs->target_window));
1208 display = gdk_screen_get_display (screen);
1209 dpy = gdk_x11_display_get_xdisplay (display);
1211 event.xclient.type = ClientMessage;
1212 event.xclient.display = dpy;
1213 event.xclient.window = gs->message_window;
1214 event.xclient.message_type =
1215 gdk_x11_atom_to_xatom_for_display (display,
1216 gs_class->next_atom);
1217 event.xclient.format = 32;
1219 gdk_error_trap_push ();
1220 XSendEvent (dpy, gs->message_window, FALSE, 0, &event);
1222 gdk_error_trap_pop ();
1226 render_page (PSDocument *gs, int page)
1228 g_assert (gs != NULL);
1230 if (!gs->gs_filename) {
1234 if (gs->structured_doc && gs->doc) {
1235 if (ps_interpreter_is_ready (gs)) {
1236 ps_document_next_page (gs);
1238 ps_interpreter_start (gs);
1239 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1240 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1243 send_ps (gs, gs->doc->pages[page].begin,
1244 gs->doc->pages[page].len, FALSE);
1246 /* Unstructured document
1248 * In the case of non structured documents,
1249 * GS read the PS from the actual file (via command
1250 * line. Hence, ggv only send a signal next page.
1251 * If ghostview is not running it is usually because
1252 * the last page of the file was displayed. In that
1253 * case, ggv restarts GS again and the first page is displayed.
1256 if (!ps_interpreter_is_ready (gs)) {
1257 ps_interpreter_start (gs);
1259 ps_document_next_page (gs);
1266 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer,
1271 PSDocument *gs = PS_DOCUMENT (renderer);
1273 if (gs->pstarget == NULL) {
1274 gs->target_window = gtk_window_new (GTK_WINDOW_POPUP);
1275 gtk_widget_realize (gs->target_window);
1276 gs->pstarget = gs->target_window->window;
1278 g_assert (gs->pstarget != NULL);
1280 g_signal_connect (gs->target_window, "event",
1281 G_CALLBACK (ps_document_widget_event),
1285 setup_pixmap (gs, page, scale, rotation);
1286 setup_page (gs, page, scale, rotation);
1288 render_page (gs, page);
1292 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
1294 iface->render_pixbuf = ps_async_renderer_render_pixbuf;
1297 /* EvFileExporterIface */
1299 ps_document_file_exporter_format_supported (EvFileExporter *exporter,
1300 EvFileExporterFormat format)
1302 return (format == EV_FILE_FORMAT_PS);
1306 ps_document_file_exporter_begin (EvFileExporter *exporter,
1307 EvFileExporterFormat format,
1308 const char *filename,
1315 PSDocument *document = PS_DOCUMENT (exporter);
1317 if (document->structured_doc) {
1318 g_free (document->ps_export_pagelist);
1320 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1323 document->ps_export_filename = g_strdup (filename);
1327 ps_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
1329 PSDocument *document = PS_DOCUMENT (exporter);
1331 if (document->structured_doc) {
1332 document->ps_export_pagelist[rc->page] = 1;
1337 ps_document_file_exporter_end (EvFileExporter *exporter)
1339 PSDocument *document = PS_DOCUMENT (exporter);
1341 if (!document->structured_doc) {
1342 save_document (document, document->ps_export_filename);
1344 save_page_list (document, document->ps_export_pagelist,
1345 document->ps_export_filename);
1346 g_free (document->ps_export_pagelist);
1347 g_free (document->ps_export_filename);
1348 document->ps_export_pagelist = NULL;
1349 document->ps_export_filename = NULL;
1354 ps_document_file_exporter_iface_init (EvFileExporterIface *iface)
1356 iface->format_supported = ps_document_file_exporter_format_supported;
1357 iface->begin = ps_document_file_exporter_begin;
1358 iface->do_page = ps_document_file_exporter_do_page;
1359 iface->end = ps_document_file_exporter_end;