]> www.fi.muni.cz Git - evince.git/blob - ps/ps-document.c
8f514cfffd7c9cb644b8f8756480ef0fa1cdae7f
[evince.git] / ps / ps-document.c
1 /* Ghostscript widget for GTK/GNOME
2  * 
3  * Copyright (C) 1998 - 2005 the Free Software Foundation
4  * 
5  * Authors: Jonathan Blandford, Jaka Mocnik
6  * 
7  * Based on code by: Federico Mena (Quartic), Szekeres Istvan (Pista)
8  *
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.
13  *
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.
18  *
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.
23  */
24  
25 #include "config.h"
26 #include <string.h>
27 #include <stdlib.h>
28 #include <signal.h>
29 #include <gtk/gtk.h>
30 #include <gtk/gtkobject.h>
31 #include <gdk/gdkprivate.h>
32 #include <gdk/gdkx.h>
33 #include <gdk/gdk.h>
34 #include <glib/gi18n.h>
35 #include <X11/Intrinsic.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <sys/wait.h>
43 #include <stdio.h>
44 #include <math.h>
45
46 #include "ps-document.h"
47 #include "ev-debug.h"
48 #include "gsdefaults.h"
49 #include "ev-ps-exporter.h"
50
51 #ifdef HAVE_LOCALE_H
52 #   include <locale.h>
53 #endif
54
55 /* if POSIX O_NONBLOCK is not available, use O_NDELAY */
56 #if !defined(O_NONBLOCK) && defined(O_NDELAY)
57 #   define O_NONBLOCK O_NDELAY
58 #endif
59
60 #define MAX_BUFSIZE 1024
61
62 #define PS_DOCUMENT_IS_COMPRESSED(gs)       (PS_DOCUMENT(gs)->gs_filename_unc != NULL)
63 #define PS_DOCUMENT_GET_PS_FILE(gs)         (PS_DOCUMENT_IS_COMPRESSED(gs) ? \
64                                         PS_DOCUMENT(gs)->gs_filename_unc : \
65                                         PS_DOCUMENT(gs)->gs_filename)
66
67 GCond* pixbuf_cond = NULL;
68 GMutex* pixbuf_mutex = NULL;
69 GdkPixbuf *current_pixbuf = NULL;
70
71 enum {
72         PROP_0,
73         PROP_TITLE
74 };
75
76 /* structure to describe section of file to send to ghostscript */
77 struct record_list {
78   FILE *fp;
79   long begin;
80   guint len;
81   gboolean seek_needed;
82   gboolean close;
83   struct record_list *next;
84 };
85
86 typedef struct {
87         int page;
88         double scale;
89         PSDocument *document;
90 } PSRenderJob;
91
92 static gboolean broken_pipe = FALSE;
93
94 /* Forward declarations */
95 static void ps_document_init(PSDocument * gs);
96 static void ps_document_class_init(PSDocumentClass * klass);
97 static void ps_document_finalize(GObject * object);
98 static void send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close);
99 static void close_pipe(int p[2]);
100 static void output(gpointer data, gint source, GdkInputCondition condition);
101 static void input(gpointer data, gint source, GdkInputCondition condition);
102 static void stop_interpreter(PSDocument * gs);
103 static gint start_interpreter(PSDocument * gs);
104 static void ps_document_document_iface_init (EvDocumentIface *iface);
105 static void ps_document_ps_exporter_iface_init (EvPSExporterIface *iface);
106 static gboolean ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data);
107
108 static GObjectClass *parent_class = NULL;
109
110 static PSDocumentClass *gs_class = NULL;
111
112 static void
113 ps_document_init (PSDocument *gs)
114 {
115         gs->bpixmap = NULL;
116
117         gs->interpreter_pid = -1;
118
119         gs->busy = FALSE;
120         gs->gs_filename = 0;
121         gs->gs_filename_unc = 0;
122
123         broken_pipe = FALSE;
124
125         gs->structured_doc = FALSE;
126         gs->reading_from_pipe = FALSE;
127         gs->send_filename_to_gs = FALSE;
128
129         gs->doc = NULL;
130         gs->loaded = FALSE;
131
132         gs->interpreter_input = -1;
133         gs->interpreter_output = -1;
134         gs->interpreter_err = -1;
135         gs->interpreter_input_id = 0;
136         gs->interpreter_output_id = 0;
137         gs->interpreter_error_id = 0;
138
139         gs->ps_input = NULL;
140         gs->input_buffer = NULL;
141         gs->input_buffer_ptr = NULL;
142         gs->bytes_left = 0;
143         gs->buffer_bytes_left = 0;
144
145         gs->gs_status = _("No document loaded.");
146
147         gs->ps_export_pagelist = NULL;
148         gs->ps_export_filename = NULL;
149
150         pixbuf_cond = g_cond_new ();
151         pixbuf_mutex = g_mutex_new ();
152 }
153
154 static void
155 ps_document_set_property (GObject *object,
156                           guint prop_id,
157                           const GValue *value,
158                           GParamSpec *pspec)
159 {
160         switch (prop_id)
161
162         {
163                 case PROP_TITLE:
164                         /* read only */
165                         break;
166         }
167 }
168
169 static void
170 ps_document_get_property (GObject *object,
171                           guint prop_id,
172                           GValue *value,
173                           GParamSpec *pspec)
174 {
175         PSDocument *ps = PS_DOCUMENT (object);
176
177         switch (prop_id)
178         {
179                 case PROP_TITLE:
180                         if (ps->doc) {
181                                 g_value_set_string (value, ps->doc->title);
182                         } else {
183                                 g_value_set_string (value, NULL);
184                         }
185                         break;
186         }
187 }
188
189 static void
190 ps_document_class_init(PSDocumentClass *klass)
191 {
192         GObjectClass *object_class;
193
194         object_class = (GObjectClass *) klass;
195         parent_class = g_type_class_peek_parent (klass);
196         gs_class = klass;
197
198         object_class->finalize = ps_document_finalize;
199         object_class->get_property = ps_document_get_property;
200         object_class->set_property = ps_document_set_property;
201
202         klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
203         klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
204         klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
205         klass->string_atom = gdk_atom_intern ("STRING", FALSE);
206
207         g_object_class_override_property (object_class, PROP_TITLE, "title");
208 }
209
210 static void
211 push_pixbuf (PSDocument *gs)
212 {
213         GdkColormap *cmap;
214         GdkPixbuf *pixbuf;
215         int width, height;
216
217         cmap = gdk_window_get_colormap (gs->pstarget);
218         gdk_drawable_get_size (gs->bpixmap, &width, &height);
219         pixbuf =  gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
220                                                 0, 0, 0, 0,
221                                                 width, height);
222         g_mutex_lock (pixbuf_mutex);
223         current_pixbuf = pixbuf;
224         g_cond_signal (pixbuf_cond);
225         g_mutex_unlock (pixbuf_mutex);
226
227 }
228
229 static void
230 interpreter_failed (PSDocument *gs, char *msg)
231 {
232         LOG ("Interpreter failed %s", msg);
233
234         push_pixbuf (gs);
235
236         stop_interpreter (gs);
237 }
238
239 static void
240 ps_document_cleanup (PSDocument *gs)
241 {
242         g_return_if_fail (gs != NULL);
243         g_return_if_fail (PS_IS_DOCUMENT (gs));
244
245         LOG ("Cleanup\n");
246
247         stop_interpreter (gs);
248
249         if (gs->gs_psfile) {
250                 fclose (gs->gs_psfile);
251                 gs->gs_psfile = NULL;
252         }
253
254         if (gs->gs_filename) {
255                 g_free (gs->gs_filename);
256                 gs->gs_filename = NULL;
257         }
258
259         if (gs->doc) {
260                 psfree (gs->doc);
261                 gs->doc = NULL;
262         }
263
264         if (gs->gs_filename_unc) {
265                 unlink(gs->gs_filename_unc);
266                 g_free(gs->gs_filename_unc);
267                 gs->gs_filename_unc = NULL;
268         }
269
270         gs->loaded = FALSE;
271 }
272
273 static gboolean
274 ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data)
275 {
276         PSDocument *gs = (PSDocument *) data;
277
278         if(event->type != GDK_CLIENT_EVENT)
279                 return FALSE;
280
281         gs->message_window = event->client.data.l[0];
282
283         if (event->client.message_type == gs_class->page_atom) {
284                 LOG ("GS rendered the document");
285                 gs->busy = FALSE;
286
287                 push_pixbuf (gs);
288         }
289
290         return TRUE;
291 }
292
293 static void
294 ps_document_finalize (GObject * object)
295 {
296         PSDocument *gs;
297
298         g_return_if_fail (object != NULL);
299         g_return_if_fail (PS_IS_DOCUMENT (object));
300
301         LOG ("Finalize");
302
303         gs = PS_DOCUMENT (object);
304
305         ps_document_cleanup (gs);
306         stop_interpreter (gs);
307
308         if(gs->input_buffer) {
309                 g_free(gs->input_buffer);
310                 gs->input_buffer = NULL;
311         }
312
313         (*G_OBJECT_CLASS (parent_class)->finalize) (object);
314 }
315
316 static void
317 send_ps(PSDocument * gs, long begin, unsigned int len, gboolean close)
318 {
319   struct record_list *ps_new;
320
321   if(gs->interpreter_input < 0) {
322     g_critical("No pipe to gs: error in send_ps().");
323     return;
324   }
325
326   ps_new = (struct record_list *) g_malloc(sizeof(struct record_list));
327   ps_new->fp = gs->gs_psfile;
328   ps_new->begin = begin;
329   ps_new->len = len;
330   ps_new->seek_needed = TRUE;
331   ps_new->close = close;
332   ps_new->next = NULL;
333
334   if(gs->input_buffer == NULL) {
335     gs->input_buffer = g_malloc(MAX_BUFSIZE);
336   }
337
338   if(gs->ps_input == NULL) {
339     gs->input_buffer_ptr = gs->input_buffer;
340     gs->bytes_left = len;
341     gs->buffer_bytes_left = 0;
342     gs->ps_input = ps_new;
343     gs->interpreter_input_id =
344       gdk_input_add(gs->interpreter_input, GDK_INPUT_WRITE, input, gs);
345   }
346   else {
347     struct record_list *p = gs->ps_input;
348     while(p->next != NULL) {
349       p = p->next;
350     }
351     p->next = ps_new;
352   }
353 }
354
355 static float
356 get_xdpi (PSDocument *gs)
357 {
358         return 25.4 * gdk_screen_width() / gdk_screen_width_mm();
359 }
360
361 static float
362 get_ydpi (PSDocument *gs)
363 {
364         return 25.4 * gdk_screen_height() / gdk_screen_height_mm();
365 }
366
367 static void
368 setup_pixmap (PSDocument *gs, int page, double scale)
369 {
370         GdkGC *fill;
371         GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF };   /* pixel, r, g, b */
372         GdkColormap *colormap;
373         double width, height;
374         int pixmap_width, pixmap_height;
375
376         ev_document_get_page_size (EV_DOCUMENT (gs), page, &width, &height);
377         pixmap_width = floor (width * scale);
378         pixmap_height = floor (height * scale);
379
380         if(gs->bpixmap) {
381                 int w, h;
382
383                 gdk_drawable_get_size (gs->bpixmap, &w, &h);
384
385                 if (pixmap_width != w || h != pixmap_height) {
386                         gdk_drawable_unref (gs->bpixmap);
387                         gs->bpixmap = NULL;
388                         stop_interpreter (gs);
389                 }
390         }
391
392         if (!gs->bpixmap) {
393                 LOG ("Create pixmap");
394
395                 fill = gdk_gc_new (gs->pstarget);
396                 colormap = gdk_drawable_get_colormap (gs->pstarget);
397                 gdk_color_alloc (colormap, &white);
398                 gdk_gc_set_foreground (fill, &white);
399                 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
400                                               pixmap_height, -1);
401                 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
402                                     0, 0, pixmap_width, pixmap_height);
403         }
404 }
405
406 #define DEFAULT_PAGE_SIZE 1
407
408 static void
409 get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly)
410 {
411   gint new_llx = 0;
412   gint new_lly = 0;
413   gint new_urx = 0;
414   gint new_ury = 0;
415   GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes();
416   int new_pagesize = -1;
417
418   g_return_if_fail(PS_IS_DOCUMENT(gs));
419
420   if(new_pagesize == -1) {
421     new_pagesize = DEFAULT_PAGE_SIZE;
422     if(gs->doc) {
423       /* If we have a document:
424          We use -- the page size (if specified)
425          or the doc. size (if specified)
426          or the page bbox (if specified)
427          or the bounding box
428        */
429       if((page >= 0) && (gs->doc->numpages > page) &&
430          (gs->doc->pages) && (gs->doc->pages[page].size)) {
431         new_pagesize = gs->doc->pages[page].size - gs->doc->size;
432       }
433       else if(gs->doc->default_page_size != NULL) {
434         new_pagesize = gs->doc->default_page_size - gs->doc->size;
435       }
436       else if((page >= 0) &&
437               (gs->doc->numpages > page) &&
438               (gs->doc->pages) &&
439               (gs->doc->pages[page].boundingbox[URX] >
440                gs->doc->pages[page].boundingbox[LLX]) &&
441               (gs->doc->pages[page].boundingbox[URY] >
442                gs->doc->pages[page].boundingbox[LLY])) {
443         new_pagesize = -1;
444       }
445       else if((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
446               (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
447         new_pagesize = -1;
448       }
449     }
450   }
451
452   /* Compute bounding box */
453   if(gs->doc && (gs->doc->epsf || new_pagesize == -1)) {    /* epsf or bbox */
454     if((page >= 0) &&
455        (gs->doc->pages) &&
456        (gs->doc->pages[page].boundingbox[URX] >
457         gs->doc->pages[page].boundingbox[LLX])
458        && (gs->doc->pages[page].boundingbox[URY] >
459            gs->doc->pages[page].boundingbox[LLY])) {
460       /* use page bbox */
461       new_llx = gs->doc->pages[page].boundingbox[LLX];
462       new_lly = gs->doc->pages[page].boundingbox[LLY];
463       new_urx = gs->doc->pages[page].boundingbox[URX];
464       new_ury = gs->doc->pages[page].boundingbox[URY];
465     }
466     else if((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) &&
467             (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) {
468       /* use doc bbox */
469       new_llx = gs->doc->boundingbox[LLX];
470       new_lly = gs->doc->boundingbox[LLY];
471       new_urx = gs->doc->boundingbox[URX];
472       new_ury = gs->doc->boundingbox[URY];
473     }
474   }
475   else {
476     if(new_pagesize < 0)
477       new_pagesize = DEFAULT_PAGE_SIZE;
478     new_llx = new_lly = 0;
479     if(gs->doc && gs->doc->size &&
480        (new_pagesize < gs->doc->numsizes)) {
481       new_urx = gs->doc->size[new_pagesize].width;
482       new_ury = gs->doc->size[new_pagesize].height;
483     }
484     else {
485       new_urx = papersizes[new_pagesize].width;
486       new_ury = papersizes[new_pagesize].height;
487     }
488   }
489
490   if(new_urx <= new_llx)
491     new_urx = papersizes[12].width;
492   if(new_ury <= new_lly)
493     new_ury = papersizes[12].height;
494
495   *urx = new_urx;
496   *ury = new_ury;
497   *llx = new_llx;
498   *lly = new_lly;
499 }
500
501 static int
502 get_page_orientation (PSDocument *gs, int page)
503 {
504         int orientation;
505
506         orientation = GTK_GS_ORIENTATION_NONE;
507
508         if (gs->structured_doc) {
509                 orientation = gs->doc->pages[page].orientation;
510         }
511         if (orientation == GTK_GS_ORIENTATION_NONE) {
512                 orientation = gs->doc->default_page_orientation;
513         }
514         if (orientation == GTK_GS_ORIENTATION_NONE) {
515                 orientation = gs->doc->orientation;
516         }
517         if (orientation == GTK_GS_ORIENTATION_NONE) {
518                 orientation = GTK_GS_ORIENTATION_PORTRAIT;
519         }
520
521         return orientation;
522 }
523
524 static void
525 setup_page (PSDocument *gs, int page, double scale)
526 {
527         char buf[1024];
528         int urx, ury, llx, lly, orientation;
529 #ifdef HAVE_LOCALE_H
530         char *savelocale;
531 #endif
532
533         LOG ("Setup the page");
534
535 #ifdef HAVE_LOCALE_H
536         /* gs needs floating point parameters with '.' as decimal point
537          * while some (european) locales use ',' instead, so we set the 
538          * locale for this snprintf to "C".
539          */
540         savelocale = setlocale (LC_NUMERIC, "C");
541 #endif
542         get_page_box (gs, page, &urx, &ury, &llx, &lly);
543         orientation = get_page_orientation (gs, page);
544
545         g_snprintf (buf, 1024, "%ld %d %d %d %d %d %f %f %d %d %d %d",
546                     0L, orientation * 90, llx, lly, urx, ury,
547                     get_xdpi (gs) * scale,
548                     get_ydpi (gs) * scale,
549                     0, 0, 0, 0);
550         LOG ("GS property %s", buf);
551
552 #ifdef HAVE_LOCALE_H
553         setlocale(LC_NUMERIC, savelocale);
554 #endif
555         gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
556                              8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen(buf));
557         gdk_flush ();
558 }
559
560 static void
561 close_pipe (int p[2])
562 {
563         if (p[0] != -1) {
564                 close (p[0]);
565         }
566         if (p[1] != -1) {
567                 close (p[1]);
568         }
569 }
570
571 static gboolean
572 is_interpreter_ready (PSDocument *gs)
573 {
574         return (gs->interpreter_pid != -1 && !gs->busy && gs->ps_input == NULL);
575 }
576
577 static void
578 output(gpointer data, gint source, GdkInputCondition condition)
579 {
580   char buf[MAX_BUFSIZE + 1], *msg;
581   guint bytes = 0;
582   PSDocument *gs = PS_DOCUMENT(data);
583
584   if(source == gs->interpreter_output) {
585     bytes = read(gs->interpreter_output, buf, MAX_BUFSIZE);
586     if(bytes == 0) {            /* EOF occurred */
587       close(gs->interpreter_output);
588       gs->interpreter_output = -1;
589       gdk_input_remove(gs->interpreter_output_id);
590       return;
591     }
592     else if(bytes == -1) {
593       /* trouble... */
594       interpreter_failed(gs, NULL);
595       return;
596     }
597     if(gs->interpreter_err == -1) {
598       interpreter_failed(gs, NULL);
599     }
600   }
601   else if(source == gs->interpreter_err) {
602     bytes = read(gs->interpreter_err, buf, MAX_BUFSIZE);
603     if(bytes == 0) {            /* EOF occurred */
604       close(gs->interpreter_err);
605       gs->interpreter_err = -1;
606       gdk_input_remove(gs->interpreter_error_id);
607       return;
608     }
609     else if(bytes == -1) {
610       /* trouble... */
611       interpreter_failed(gs, NULL);
612       return;
613     }
614     if(gs->interpreter_output == -1) {
615       interpreter_failed(gs, NULL);
616     }
617   }
618   if(bytes > 0) {
619     buf[bytes] = '\0';
620     msg = g_strdup(buf);
621     interpreter_failed (gs, msg);   
622   }
623 }
624
625 static void
626 catchPipe(int i)
627 {
628   broken_pipe = True;
629 }
630
631 static void
632 input(gpointer data, gint source, GdkInputCondition condition)
633 {
634   PSDocument *gs = PS_DOCUMENT(data);
635   int bytes_written;
636   void (*oldsig) (int);
637   oldsig = signal(SIGPIPE, catchPipe);
638
639   LOG ("Input");
640
641   do {
642     if(gs->buffer_bytes_left == 0) {
643       /* Get a new section if required */
644       if(gs->ps_input && gs->bytes_left == 0) {
645         struct record_list *ps_old = gs->ps_input;
646         gs->ps_input = ps_old->next;
647         if(ps_old->close && NULL != ps_old->fp)
648           fclose(ps_old->fp);
649         g_free((char *) ps_old);
650       }
651       /* Have to seek at the beginning of each section */
652       if(gs->ps_input && gs->ps_input->seek_needed) {
653         fseek(gs->ps_input->fp, gs->ps_input->begin, SEEK_SET);
654         gs->ps_input->seek_needed = FALSE;
655         gs->bytes_left = gs->ps_input->len;
656       }
657
658       if(gs->bytes_left > MAX_BUFSIZE) {
659         gs->buffer_bytes_left =
660           fread(gs->input_buffer, sizeof(char), MAX_BUFSIZE, gs->ps_input->fp);
661       }
662       else if(gs->bytes_left > 0) {
663         gs->buffer_bytes_left =
664           fread(gs->input_buffer,
665                 sizeof(char), gs->bytes_left, gs->ps_input->fp);
666       }
667       else {
668         gs->buffer_bytes_left = 0;
669       }
670       if(gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
671         interpreter_failed (gs, NULL); /* Error occurred */
672       }
673       gs->input_buffer_ptr = gs->input_buffer;
674       gs->bytes_left -= gs->buffer_bytes_left;
675     }
676
677     if(gs->buffer_bytes_left > 0) {
678       bytes_written = write(gs->interpreter_input,
679                             gs->input_buffer_ptr, gs->buffer_bytes_left);
680
681       if(broken_pipe) {
682         interpreter_failed (gs, g_strdup(_("Broken pipe.")));
683         broken_pipe = FALSE;
684         interpreter_failed (gs, NULL);
685       }
686       else if(bytes_written == -1) {
687         if((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
688           interpreter_failed (gs, NULL);   /* Something bad happened */
689         }
690       }
691       else {
692         gs->buffer_bytes_left -= bytes_written;
693         gs->input_buffer_ptr += bytes_written;
694       }
695     }
696   }
697   while(gs->ps_input && gs->buffer_bytes_left == 0);
698
699   signal(SIGPIPE, oldsig);
700
701   if(gs->ps_input == NULL && gs->buffer_bytes_left == 0) {
702     if(gs->interpreter_input_id != 0) {
703       gdk_input_remove(gs->interpreter_input_id);
704       gs->interpreter_input_id = 0;
705     }
706   }
707 }
708
709 static int
710 start_interpreter (PSDocument *gs)
711 {
712         int std_in[2] = { -1, -1 };   /* pipe to interp stdin */
713         int std_out[2];               /* pipe from interp stdout */
714         int std_err[2];               /* pipe from interp stderr */
715
716 #define NUM_ARGS    100
717 #define NUM_GS_ARGS (NUM_ARGS - 20)
718 #define NUM_ALPHA_ARGS 10
719
720         char *argv[NUM_ARGS], *dir, *gv_env;
721         char **gs_args, **alpha_args = NULL;
722         int argc = 0, i;
723
724         LOG ("Start the interpreter");
725
726         if(!gs->gs_filename)
727                 return 0;
728
729         stop_interpreter(gs);
730
731         /* set up the args... */
732         gs_args = g_strsplit (gtk_gs_defaults_get_interpreter_cmd (), " ", NUM_GS_ARGS);
733         for(i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
734                 argv[argc] = gs_args[i];
735         }
736
737         alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
738         for(i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
739                 argv[argc] = alpha_args[i];
740         }
741
742         argv[argc++] = "-dNOPAUSE";
743         argv[argc++] = "-dQUIET";
744         argv[argc++] = "-dSAFER";
745
746         /* set up the pipes */
747         if (gs->send_filename_to_gs) {
748                 argv[argc++] = PS_DOCUMENT_GET_PS_FILE (gs);
749                 argv[argc++] = "-c";
750                 argv[argc++] = "quit";
751         } else {
752                 argv[argc++] = "-";
753         }
754
755         argv[argc++] = NULL;
756
757         if (!gs->reading_from_pipe && !gs->send_filename_to_gs) {
758                 if (pipe (std_in) == -1) {
759                         g_critical ("Unable to open pipe to Ghostscript.");
760                         return -1;
761                 }
762         }
763
764         if (pipe (std_out) == -1) {
765                 close_pipe (std_in);
766                 return -1;
767         }
768
769         if (pipe(std_err) == -1) {
770                 close_pipe (std_in);
771                 close_pipe (std_out);
772                 return -1;
773         }
774
775         gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld",
776                                   gdk_x11_drawable_get_xid (gs->pstarget),
777                                   gdk_x11_drawable_get_xid (gs->bpixmap));
778         LOG ("Launching ghostview with env %s", gv_env);
779
780         gs->busy = TRUE;
781         gs->interpreter_pid = fork ();
782         switch (gs->interpreter_pid) {
783                 case -1:                     /* error */
784                         close_pipe (std_in);
785                         close_pipe (std_out);
786                         close_pipe (std_err);
787                         return -2;
788                         break;
789                 case 0:                      /* child */
790                         close (std_out[0]);
791                         dup2 (std_out[1], 1);
792                         close (std_out[1]);
793
794                         close (std_err[0]);
795                         dup2 (std_err[1], 2);
796                         close (std_err[1]);
797
798                         if (!gs->reading_from_pipe) {
799                                 if (gs->send_filename_to_gs) {
800                                         int stdinfd;
801                                         /* just in case gs tries to read from stdin */
802                                         stdinfd = open("/dev/null", O_RDONLY);
803                                         if (stdinfd != 0) {
804                                                 dup2(stdinfd, 0);
805                                                 close(stdinfd);
806                                         }
807                                 } else {
808                                         close (std_in[1]);
809                                         dup2 (std_in[0], 0);
810                                         close (std_in[0]);
811                                 }
812                         }
813
814                         putenv(gv_env);
815
816                         /* change to directory where the input file is. This helps
817                          * with postscript-files which include other files using
818                          * a relative path */
819                         dir = g_path_get_dirname (gs->gs_filename);
820                         chdir (dir);
821                         g_free (dir);
822
823                         execvp (argv[0], argv);
824
825                         /* Notify error */
826                         g_critical ("Unable to execute [%s]\n", argv[0]);
827                         g_strfreev (gs_args);
828                         g_free (gv_env);
829                         g_strfreev (alpha_args);
830                         _exit (1);
831                         break;
832                 default:                     /* parent */
833                         if (!gs->send_filename_to_gs && !gs->reading_from_pipe) {
834                                 int result;
835                                 close (std_in[0]);
836                                 /* use non-blocking IO for pipe to ghostscript */
837                                 result = fcntl (std_in[1], F_GETFL, 0);
838                                 fcntl (std_in[1], F_SETFL, result | O_NONBLOCK);
839                                 gs->interpreter_input = std_in[1];
840                         } else {
841                                 gs->interpreter_input = -1;
842                         }
843                         close (std_out[1]);
844
845                         gs->interpreter_output = std_out[0];
846                         close (std_err[1]);
847                         gs->interpreter_err = std_err[0];
848                         gs->interpreter_output_id =
849                                 gdk_input_add (std_out[0], GDK_INPUT_READ, output, gs);
850                         gs->interpreter_error_id =
851                                 gdk_input_add (std_err[0], GDK_INPUT_READ, output, gs);
852                         break;
853         }
854
855         return TRUE;
856 }
857
858 static void
859 stop_interpreter(PSDocument * gs)
860 {
861   if(gs->interpreter_pid > 0) {
862     int status = 0;
863     LOG ("Stop the interpreter");
864     kill(gs->interpreter_pid, SIGTERM);
865     while((wait(&status) == -1) && (errno == EINTR)) ;
866     gs->interpreter_pid = -1;
867     if(status == 1) {
868       ps_document_cleanup(gs);
869       gs->gs_status = _("Interpreter failed.");
870     }
871   }
872
873   if(gs->interpreter_input >= 0) {
874     close(gs->interpreter_input);
875     gs->interpreter_input = -1;
876     if(gs->interpreter_input_id != 0) {
877       gdk_input_remove(gs->interpreter_input_id);
878       gs->interpreter_input_id = 0;
879     }
880     while(gs->ps_input) {
881       struct record_list *ps_old = gs->ps_input;
882       gs->ps_input = gs->ps_input->next;
883       if(ps_old->close && NULL != ps_old->fp)
884         fclose(ps_old->fp);
885       g_free((char *) ps_old);
886     }
887   }
888
889   if(gs->interpreter_output >= 0) {
890     close(gs->interpreter_output);
891     gs->interpreter_output = -1;
892     if(gs->interpreter_output_id) {
893       gdk_input_remove(gs->interpreter_output_id);
894       gs->interpreter_output_id = 0;
895     }
896   }
897
898   if(gs->interpreter_err >= 0) {
899     close(gs->interpreter_err);
900     gs->interpreter_err = -1;
901     if(gs->interpreter_error_id) {
902       gdk_input_remove(gs->interpreter_error_id);
903       gs->interpreter_error_id = 0;
904     }
905   }
906
907   gs->busy = FALSE;
908 }
909
910 /* If file exists and is a regular file then return its length, else -1 */
911 static gint
912 file_length(const gchar * filename)
913 {
914   struct stat stat_rec;
915
916   if(filename && (stat(filename, &stat_rec) == 0)
917      && S_ISREG(stat_rec.st_mode))
918     return stat_rec.st_size;
919   else
920     return -1;
921 }
922
923 /* Test if file exists, is a regular file and its length is > 0 */
924 static gboolean
925 file_readable(const char *filename)
926 {
927   return (file_length(filename) > 0);
928 }
929
930 /*
931  * Decompress gs->gs_filename if necessary
932  * Set gs->filename_unc to the name of the uncompressed file or NULL.
933  * Error reporting via signal 'interpreter_message'
934  * Return name of input file to use or NULL on error..
935  */
936 static gchar *
937 check_filecompressed(PSDocument * gs)
938 {
939   FILE *file;
940   gchar buf[1024];
941   gchar *filename, *filename_unc, *filename_err, *cmdline;
942   const gchar *cmd;
943   int fd;
944
945   cmd = NULL;
946
947   if((file = fopen(gs->gs_filename, "r"))
948      && (fread(buf, sizeof(gchar), 3, file) == 3)) {
949     if((buf[0] == '\037') && ((buf[1] == '\235') || (buf[1] == '\213'))) {
950       /* file is gzipped or compressed */
951       cmd = gtk_gs_defaults_get_ungzip_cmd();
952     }
953     else if(strncmp(buf, "BZh", 3) == 0) {
954       /* file is compressed with bzip2 */
955       cmd = gtk_gs_defaults_get_unbzip2_cmd();
956     }
957   }
958   if(NULL != file)
959     fclose(file);
960
961   if(!cmd)
962     return gs->gs_filename;
963
964   /* do the decompression */
965   filename = g_shell_quote(gs->gs_filename);
966   filename_unc = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
967   if((fd = mkstemp(filename_unc)) < 0) {
968     g_free(filename_unc);
969     g_free(filename);
970     return NULL;
971   }
972   close(fd);
973   filename_err = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
974   if((fd = mkstemp(filename_err)) < 0) {
975     g_free(filename_err);
976     g_free(filename_unc);
977     g_free(filename);
978     return NULL;
979   }
980   close(fd);
981   cmdline = g_strdup_printf("%s %s >%s 2>%s", cmd,
982                             filename, filename_unc, filename_err);
983   if((system(cmdline) == 0)
984      && file_readable(filename_unc)
985      && (file_length(filename_err) == 0)) {
986     /* sucessfully uncompressed file */
987     gs->gs_filename_unc = filename_unc;
988   }
989   else {
990     /* report error */
991     g_snprintf(buf, 1024, _("Error while decompressing file %s:\n"),
992                gs->gs_filename);
993     interpreter_failed (gs, buf);
994     unlink(filename_unc);
995     g_free(filename_unc);
996     filename_unc = NULL;
997   }
998   unlink(filename_err);
999   g_free(filename_err);
1000   g_free(cmdline);
1001   g_free(filename);
1002   return filename_unc;
1003 }
1004
1005 static gint
1006 ps_document_enable_interpreter(PSDocument * gs)
1007 {
1008   g_return_val_if_fail(gs != NULL, FALSE);
1009   g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1010
1011   if(!gs->gs_filename)
1012     return 0;
1013
1014   return start_interpreter(gs);
1015 }
1016
1017 /* publicly accessible functions */
1018
1019 GType
1020 ps_document_get_type(void)
1021 {
1022   static GType gs_type = 0;
1023   if(!gs_type) {
1024     GTypeInfo gs_info = {
1025       sizeof(PSDocumentClass),
1026       (GBaseInitFunc) NULL,
1027       (GBaseFinalizeFunc) NULL,
1028       (GClassInitFunc) ps_document_class_init,
1029       (GClassFinalizeFunc) NULL,
1030       NULL,                     /* class_data */
1031       sizeof(PSDocument),
1032       0,                        /* n_preallocs */
1033       (GInstanceInitFunc) ps_document_init
1034     };
1035
1036     static const GInterfaceInfo document_info =
1037     {
1038         (GInterfaceInitFunc) ps_document_document_iface_init,
1039         NULL,
1040         NULL
1041     };
1042
1043     static const GInterfaceInfo ps_exporter_info =
1044     {
1045         (GInterfaceInitFunc) ps_document_ps_exporter_iface_init,
1046         NULL,
1047         NULL
1048     };
1049
1050     gs_type = g_type_register_static(G_TYPE_OBJECT,
1051                                      "PSDocument", &gs_info, 0);
1052
1053     g_type_add_interface_static (gs_type,
1054                                  EV_TYPE_DOCUMENT,
1055                                  &document_info);
1056     g_type_add_interface_static (gs_type,
1057                                  EV_TYPE_PS_EXPORTER,
1058                                  &ps_exporter_info);
1059   }
1060   return gs_type;
1061
1062
1063 }
1064
1065 static gboolean
1066 document_load(PSDocument * gs, const gchar * fname)
1067 {
1068   g_return_val_if_fail(gs != NULL, FALSE);
1069   g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1070
1071   LOG ("Load the document");
1072
1073   /* clean up previous document */
1074   ps_document_cleanup(gs);
1075
1076   if(fname == NULL) {
1077     gs->gs_status = "";
1078     return FALSE;
1079   }
1080
1081   /* prepare this document */
1082   gs->structured_doc = FALSE;
1083   gs->send_filename_to_gs = TRUE;
1084   gs->loaded = FALSE;
1085   if(*fname == '/') {
1086     /* an absolute path */
1087     gs->gs_filename = g_strdup(fname);
1088   }
1089   else {
1090     /* path relative to our cwd: make it absolute */
1091     gchar *cwd = g_get_current_dir();
1092     gs->gs_filename = g_strconcat(cwd, "/", fname, NULL);
1093     g_free(cwd);
1094   }
1095
1096   if((gs->reading_from_pipe = (strcmp(fname, "-") == 0))) {
1097     gs->send_filename_to_gs = FALSE;
1098   }
1099   else {
1100     /*
1101      * We need to make sure that the file is loadable/exists!
1102      * otherwise we want to exit without loading new stuff...
1103      */
1104     gchar *filename = NULL;
1105
1106     if(!file_readable(fname)) {
1107       gchar buf[1024];
1108       g_snprintf(buf, 1024, _("Cannot open file %s.\n"), fname);
1109       interpreter_failed (gs, buf);
1110       gs->gs_status = _("File is not readable.");
1111     }
1112     else {
1113       filename = check_filecompressed(gs);
1114     }
1115
1116     if(!filename || (gs->gs_psfile = fopen(filename, "r")) == NULL) {
1117       interpreter_failed (gs, NULL);
1118       ps_document_cleanup(gs);
1119       return FALSE;
1120     }
1121
1122     /* we grab the vital statistics!!! */
1123     gs->doc = psscan(gs->gs_psfile, TRUE, filename);
1124
1125     g_object_notify (G_OBJECT (gs), "title");
1126
1127     if(gs->doc == NULL) {
1128       /* File does not seem to be a Postscript one */
1129       gchar buf[1024];
1130       g_snprintf(buf, 1024, _("Error while scanning file %s\n"), fname);
1131       interpreter_failed (gs, buf);
1132       ps_document_cleanup(gs);
1133       gs->gs_status = _("The file is not a PostScript document.");
1134       return FALSE;
1135     }
1136
1137     if((!gs->doc->epsf && gs->doc->numpages > 0) ||
1138        (gs->doc->epsf && gs->doc->numpages > 1)) {
1139       gs->structured_doc = TRUE;
1140       gs->send_filename_to_gs = FALSE;
1141     }
1142   }
1143   gs->loaded = TRUE;
1144
1145   gs->gs_status = _("Document loaded.");
1146
1147   return gs->loaded;
1148 }
1149
1150
1151 static gboolean
1152 ps_document_next_page (PSDocument *gs)
1153 {
1154         XEvent event;
1155
1156         LOG ("Make ghostscript render next page");
1157
1158         g_return_val_if_fail (PS_IS_DOCUMENT(gs), FALSE);
1159         g_return_val_if_fail (gs->interpreter_pid != 0, FALSE);
1160         g_return_val_if_fail (gs->busy != TRUE, FALSE);
1161
1162         gs->busy = TRUE;
1163
1164         event.xclient.type = ClientMessage;
1165         event.xclient.display = gdk_display;
1166         event.xclient.window = gs->message_window;
1167         event.xclient.message_type = gdk_x11_atom_to_xatom(gs_class->next_atom);
1168         event.xclient.format = 32;
1169
1170         gdk_error_trap_push ();
1171         XSendEvent (gdk_display, gs->message_window, FALSE, 0, &event);
1172         gdk_flush ();
1173         gdk_error_trap_pop ();
1174
1175         return TRUE;
1176 }
1177
1178 static gboolean
1179 render_page (PSDocument *gs, int page)
1180 {
1181         g_return_val_if_fail(gs != NULL, FALSE);
1182         g_return_val_if_fail(PS_IS_DOCUMENT(gs), FALSE);
1183
1184         if(!gs->gs_filename) {
1185                 return FALSE;
1186         }
1187
1188         if (gs->structured_doc && gs->doc) {
1189                 LOG ("It's a structured document, let's send one page to gs");
1190
1191                 if (is_interpreter_ready (gs)) {
1192                         ps_document_next_page (gs);
1193                 } else {
1194                         ps_document_enable_interpreter (gs);
1195                         send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
1196                         send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
1197                 }
1198
1199                 send_ps (gs, gs->doc->pages[page].begin,
1200                          gs->doc->pages[page].len, FALSE);
1201         } else {
1202                 /* Unstructured document
1203                  *
1204                  * In the case of non structured documents,
1205                  * GS read the PS from the  actual file (via command
1206                  * line. Hence, ggv only send a signal next page.
1207                  * If ghostview is not running it is usually because
1208                  * the last page of the file was displayed. In that
1209                  * case, ggv restarts GS again and the first page is displayed.
1210                  */
1211
1212                 LOG ("It's an unstructured document, gs will just read the file");
1213
1214                 if (!is_interpreter_ready (gs)) {
1215                         ps_document_enable_interpreter(gs);
1216                 }
1217                 ps_document_next_page(gs);
1218         }
1219
1220         return TRUE;
1221 }
1222
1223 static gboolean
1224 ps_document_load (EvDocument  *document,
1225                   const char  *uri,
1226                   GError     **error)
1227 {
1228         gboolean result;
1229         char *filename;
1230
1231         filename = g_filename_from_uri (uri, NULL, error);
1232         if (!filename)
1233                 return FALSE;
1234
1235         result = document_load (PS_DOCUMENT (document), filename);
1236         if (!result) {
1237                 g_set_error (error, G_FILE_ERROR,
1238                              G_FILE_ERROR_FAILED,
1239                              "Failed to load document '%s'\n",
1240                              uri);
1241         }
1242
1243         g_free (filename);
1244
1245         return result;
1246 }
1247
1248 static gboolean
1249 save_page_list (PSDocument *document, int *page_list, const char *filename)
1250 {
1251         gboolean result = TRUE;
1252         GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
1253         FILE *f;
1254         gchar *buf;
1255
1256         pscopydoc (sink, document->gs_filename, document->doc, page_list);
1257         
1258         buf = gtk_gs_doc_sink_get_buffer (sink);
1259         
1260         f = fopen (filename, "w");
1261         if (f) {
1262                 fputs (buf, f);
1263                 fclose (f);
1264         } else {
1265                 result = FALSE;
1266         }
1267
1268         g_free (buf);
1269         gtk_gs_doc_sink_free (sink);
1270         g_free (sink);
1271
1272         return result;
1273 }
1274
1275 static gboolean
1276 ps_document_save (EvDocument  *document,
1277                   const char  *uri,
1278                   GError     **error)
1279 {
1280         PSDocument *ps = PS_DOCUMENT (document);
1281         int *page_list;
1282         gboolean result;
1283         int i;
1284         char *filename;
1285
1286         filename = g_filename_from_uri (uri, NULL, error);
1287         if (!filename)
1288                 return FALSE;
1289
1290         page_list = g_new0 (int, ps->doc->numpages);
1291         for (i = 0; i < ps->doc->numpages; i++) {
1292                 page_list[i] = 1;
1293         }
1294
1295         result = save_page_list (ps, page_list, filename);
1296
1297         g_free (page_list);
1298         g_free (filename);
1299
1300         return result;
1301 }
1302
1303 static int
1304 ps_document_get_n_pages (EvDocument  *document)
1305 {
1306         PSDocument *ps = PS_DOCUMENT (document);
1307
1308         g_return_val_if_fail (ps != NULL, -1);
1309
1310         if (!ps->gs_filename || !ps->doc) {
1311                 return -1;
1312         }
1313
1314         return ps->structured_doc ? ps->doc->numpages : 1;
1315 }
1316
1317 static void
1318 ps_document_get_page_size (EvDocument   *document,
1319                            int           page,
1320                            double       *width,
1321                            double       *height)
1322 {
1323         PSDocument *gs = PS_DOCUMENT (document);
1324         int w, h;
1325         int urx, ury, llx, lly, orientation;
1326
1327         get_page_box (PS_DOCUMENT (document), page, &urx, &ury, &llx, &lly);
1328         orientation = get_page_orientation (PS_DOCUMENT (document), page);
1329
1330         switch (orientation) {
1331                 case GTK_GS_ORIENTATION_PORTRAIT:
1332                 case GTK_GS_ORIENTATION_UPSIDEDOWN:
1333                         w = (urx - llx) / 72.0 * get_xdpi (gs) + 0.5;
1334                         h = (ury - lly) / 72.0 * get_ydpi (gs) + 0.5;
1335                         break;
1336                 case GTK_GS_ORIENTATION_LANDSCAPE:
1337                 case GTK_GS_ORIENTATION_SEASCAPE:
1338                         w = (ury - lly) / 72.0 * get_xdpi (gs) + 0.5;
1339                         h = (urx - llx) / 72.0 * get_ydpi (gs) + 0.5;
1340                         break;
1341                 default:
1342                         w = h = 0;
1343                         g_assert_not_reached ();
1344                         break;
1345         }
1346
1347         if (width) {
1348                 *width = w;
1349         }
1350
1351         if (height) {
1352                 *height = h;
1353         }
1354 }
1355
1356 static gboolean
1357 ps_document_can_get_text (EvDocument *document)
1358 {
1359         return FALSE;
1360 }
1361
1362 static gboolean
1363 render_pixbuf_idle (PSRenderJob *job)
1364 {
1365         PSDocument *gs = job->document;
1366
1367         if (gs->pstarget == NULL) {
1368                 GtkWidget *widget;
1369
1370                 widget = gtk_window_new (GTK_WINDOW_POPUP);
1371                 gtk_widget_realize (widget);
1372                 gs->pstarget = widget->window;
1373
1374                 g_assert (gs->pstarget != NULL);
1375
1376                 g_signal_connect (widget, "event",
1377                                   G_CALLBACK (ps_document_widget_event),
1378                                   gs);
1379         }
1380
1381         setup_pixmap (gs, job->page, job->scale);
1382         setup_page (gs, job->page, job->scale);
1383
1384         render_page (gs, job->page);
1385
1386         return FALSE;
1387 }
1388
1389 static GdkPixbuf *
1390 ps_document_render_pixbuf (EvDocument *document, int page, double scale)
1391 {
1392         GdkPixbuf *pixbuf;
1393         PSRenderJob job;
1394
1395         job.page = page;
1396         job.scale = scale;
1397         job.document = PS_DOCUMENT (document);
1398         g_idle_add ((GSourceFunc)render_pixbuf_idle, &job);
1399
1400         g_mutex_lock (pixbuf_mutex);
1401         while (!current_pixbuf)
1402                 g_cond_wait (pixbuf_cond, pixbuf_mutex);
1403         pixbuf = current_pixbuf;
1404         current_pixbuf = NULL;
1405         g_mutex_unlock (pixbuf_mutex);
1406
1407         LOG ("Pixbuf rendered %p\n", pixbuf);
1408
1409         return pixbuf;
1410 }
1411
1412 static void
1413 ps_document_document_iface_init (EvDocumentIface *iface)
1414 {
1415         iface->load = ps_document_load;
1416         iface->save = ps_document_save;
1417         iface->can_get_text = ps_document_can_get_text;
1418         iface->get_n_pages = ps_document_get_n_pages;
1419         iface->get_page_size = ps_document_get_page_size;
1420         iface->render_pixbuf = ps_document_render_pixbuf;
1421 }
1422
1423 static void
1424 ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename)
1425 {
1426         PSDocument *document = PS_DOCUMENT (exporter);
1427
1428         g_free (document->ps_export_pagelist);
1429         
1430         document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
1431         document->ps_export_filename = g_strdup (filename);
1432 }
1433
1434 static void
1435 ps_document_ps_export_do_page (EvPSExporter *exporter, int page)
1436 {
1437         PSDocument *document = PS_DOCUMENT (exporter);
1438         
1439         document->ps_export_pagelist[page] = 1;
1440 }
1441
1442 static void
1443 ps_document_ps_export_end (EvPSExporter *exporter)
1444 {
1445         PSDocument *document = PS_DOCUMENT (exporter);
1446
1447         save_page_list (document, document->ps_export_pagelist,
1448                         document->ps_export_filename);
1449
1450         g_free (document->ps_export_pagelist);
1451         g_free (document->ps_export_filename);  
1452         document->ps_export_pagelist = NULL;
1453         document->ps_export_filename = NULL;
1454 }
1455
1456 static void
1457 ps_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1458 {
1459         iface->begin = ps_document_ps_export_begin;
1460         iface->do_page = ps_document_ps_export_do_page;
1461         iface->end = ps_document_ps_export_end;
1462 }